- fix: don't set handler if SIG_IGN is already set (#134474).
[platform/upstream/rpm.git] / rpmio / rpmsq.c
1 /** \ingroup rpmio
2  * \file rpmio/rpmsq.c
3  */
4
5 #include "system.h"
6
7 #if defined(__LCLINT__)
8 #define _BITS_SIGTHREAD_H       /* XXX avoid __sigset_t heartburn. */
9
10 /*@-incondefs -protoparammatch@*/
11 /*@-exportheader@*/
12 /*@constant int SA_SIGINFO@*/
13 extern int sighold(int sig)
14         /*@globals errno, systemState @*/;
15 extern int sigignore(int sig)
16         /*@globals errno, systemState @*/;
17 extern int sigpause(int sig)
18         /*@globals errno, systemState @*/;
19 extern int sigrelse(int sig)
20         /*@globals errno, systemState @*/;
21 extern void (*sigset(int sig, void (*disp)(int)))(int)
22         /*@globals errno, systemState @*/;
23
24 struct qelem;
25 extern  void insque(struct qelem * __elem, struct qelem * __prev)
26         /*@modifies  __elem, __prev @*/;
27 extern  void remque(struct qelem * __elem)
28         /*@modifies  __elem @*/;
29
30 extern pthread_t pthread_self(void)
31         /*@*/;
32 extern int pthread_equal(pthread_t t1, pthread_t t2)
33         /*@*/;
34
35 extern int pthread_create(/*@out@*/ pthread_t *restrict thread,
36                 const pthread_attr_t *restrict attr,
37                 void *(*start_routine)(void*), void *restrict arg)
38         /*@modifies *thread @*/;
39 extern int pthread_join(pthread_t thread, /*@out@*/ void **value_ptr)
40         /*@modifies *value_ptr @*/;
41
42 extern int pthread_setcancelstate(int state, /*@out@*/ int *oldstate)
43         /*@globals internalState @*/
44         /*@modifies *oldstate, internalState @*/;
45 extern int pthread_setcanceltype(int type, /*@out@*/ int *oldtype)
46         /*@globals internalState @*/
47         /*@modifies *oldtype, internalState @*/;
48 extern void pthread_testcancel(void)
49         /*@globals internalState @*/
50         /*@modifies internalState @*/;
51 extern void pthread_cleanup_pop(int execute)
52         /*@globals internalState @*/
53         /*@modifies internalState @*/;
54 extern void pthread_cleanup_push(void (*routine)(void*), void *arg)
55         /*@globals internalState @*/
56         /*@modifies internalState @*/;
57 extern void _pthread_cleanup_pop(/*@out@*/ struct _pthread_cleanup_buffer *__buffer, int execute)
58         /*@globals internalState @*/
59         /*@modifies internalState @*/;
60 extern void _pthread_cleanup_push(/*@out@*/ struct _pthread_cleanup_buffer *__buffer, void (*routine)(void*), /*@out@*/ void *arg)
61         /*@globals internalState @*/
62         /*@modifies internalState @*/;
63
64 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
65         /*@globals errno, internalState @*/
66         /*@modifies *attr, errno, internalState @*/;
67 extern int pthread_mutexattr_init(/*@out@*/ pthread_mutexattr_t *attr)
68         /*@globals errno, internalState @*/
69         /*@modifies *attr, errno, internalState @*/;
70
71 int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,
72                 /*@out@*/ int *restrict type)
73         /*@modifies *type @*/;
74 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
75         /*@globals errno, internalState @*/
76         /*@modifies *attr, errno, internalState @*/;
77
78 extern int pthread_mutex_destroy(pthread_mutex_t *mutex)
79         /*@modifies *mutex @*/;
80 extern int pthread_mutex_init(/*@out@*/ pthread_mutex_t *restrict mutex,
81                 /*@null@*/ const pthread_mutexattr_t *restrict attr)
82         /*@globals errno, internalState @*/
83         /*@modifies *mutex, errno, internalState @*/;
84
85 extern int pthread_mutex_lock(pthread_mutex_t *mutex)
86         /*@globals errno @*/
87         /*@modifies *mutex, errno @*/;
88 extern int pthread_mutex_trylock(pthread_mutex_t *mutex)
89         /*@globals errno @*/
90         /*@modifies *mutex, errno @*/;
91 extern int pthread_mutex_unlock(pthread_mutex_t *mutex)
92         /*@globals errno @*/
93         /*@modifies *mutex, errno @*/;
94
95 extern int pthread_cond_destroy(pthread_cond_t *cond)
96         /*@modifies *cond @*/;
97 extern int pthread_cond_init(/*@out@*/ pthread_cond_t *restrict cond,
98                 const pthread_condattr_t *restrict attr)
99         /*@globals errno, internalState @*/
100         /*@modifies *cond, errno, internalState @*/;
101
102 extern int pthread_cond_timedwait(pthread_cond_t *restrict cond,
103                 pthread_mutex_t *restrict mutex,
104                 const struct timespec *restrict abstime)
105         /*@modifies *cond, *mutex @*/;
106 extern int pthread_cond_wait(pthread_cond_t *restrict cond,
107                 pthread_mutex_t *restrict mutex)
108         /*@modifies *cond, *mutex @*/;
109 extern int pthread_cond_broadcast(pthread_cond_t *cond)
110         /*@globals errno, internalState @*/
111         /*@modifies *cond, errno, internalState @*/;
112 extern int pthread_cond_signal(pthread_cond_t *cond)
113         /*@globals errno, internalState @*/
114         /*@modifies *cond, errno, internalState @*/;
115
116 /*@=exportheader@*/
117 /*@=incondefs =protoparammatch@*/
118 #endif
119
120 #include <signal.h>
121 #include <sys/signal.h>
122 #include <sys/wait.h>
123 #include <search.h>
124
125 #if defined(HAVE_PTHREAD_H)
126
127 #include <pthread.h>
128
129 /*@unchecked@*/
130 /*@-type@*/
131 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
132 /*@=type@*/
133
134 #define DO_LOCK()       pthread_mutex_lock(&rpmsigTbl_lock);
135 #define DO_UNLOCK()     pthread_mutex_unlock(&rpmsigTbl_lock);
136 #define INIT_LOCK()     \
137     {   pthread_mutexattr_t attr; \
138         (void) pthread_mutexattr_init(&attr); \
139         (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
140         (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
141         (void) pthread_mutexattr_destroy(&attr); \
142         rpmsigTbl_sigchld->active = 0; \
143     }
144 #define ADD_REF(__tbl)  (__tbl)->active++
145 #define SUB_REF(__tbl)  --(__tbl)->active
146 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
147     (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
148         pthread_cleanup_push((__handler), (__arg));
149 #define CLEANUP_RESET(__execute, __oldtype) \
150     (void) pthread_cleanup_pop(__execute); \
151     (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
152
153 #define SAME_THREAD(_a, _b)     pthread_equal(((pthread_t)_a), ((pthread_t)_b))
154
155 #define ME()    ((void *)pthread_self())
156
157 #else
158
159 #define DO_LOCK()
160 #define DO_UNLOCK()
161 #define INIT_LOCK()
162 #define ADD_REF(__tbl)  /*@-noeffect@*/ (0) /*@=noeffect@*/
163 #define SUB_REF(__tbl)  /*@-noeffect@*/ (0) /*@=noeffect@*/
164 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
165 #define CLEANUP_RESET(__execute, __oldtype)
166
167 #define SAME_THREAD(_a, _b)     (42)
168
169 #define ME()    (((void *)getpid()))
170
171 #endif  /* HAVE_PTHREAD_H */
172
173 #include <rpmsq.h>
174
175 #include "debug.h"
176
177 #define _RPMSQ_DEBUG    0
178 /*@unchecked@*/
179 int _rpmsq_debug = _RPMSQ_DEBUG;
180
181 /*@unchecked@*/
182 static struct rpmsqElem rpmsqRock;
183
184 /*@-compmempass@*/
185 /*@unchecked@*/
186 rpmsq rpmsqQueue = &rpmsqRock;
187 /*@=compmempass@*/
188
189 int rpmsqInsert(void * elem, void * prev)
190 {
191     rpmsq sq = (rpmsq) elem;
192     int ret = -1;
193
194     if (sq != NULL) {
195 #ifdef _RPMSQ_DEBUG
196 if (_rpmsq_debug)
197 fprintf(stderr, "    Insert(%p): %p\n", ME(), sq);
198 #endif
199         ret = sighold(SIGCHLD);
200         if (ret == 0) {
201             sq->child = 0;
202             sq->reaped = 0;
203             sq->status = 0;
204             sq->reaper = 1;
205 /*@-bounds@*/
206             sq->pipes[0] = sq->pipes[1] = -1;
207 /*@=bounds@*/
208
209             sq->id = ME();
210             ret = pthread_mutex_init(&sq->mutex, NULL);
211             ret = pthread_cond_init(&sq->cond, NULL);
212             insque(elem, (prev != NULL ? prev : rpmsqQueue));
213             ret = sigrelse(SIGCHLD);
214         }
215     }
216     return ret;
217 }
218
219 int rpmsqRemove(void * elem)
220 {
221     rpmsq sq = (rpmsq) elem;
222     int ret = -1;
223
224     if (elem != NULL) {
225
226 #ifdef _RPMSQ_DEBUG
227 if (_rpmsq_debug)
228 fprintf(stderr, "    Remove(%p): %p\n", ME(), sq);
229 #endif
230         ret = sighold (SIGCHLD);
231         if (ret == 0) {
232             remque(elem);
233             ret = pthread_cond_destroy(&sq->cond);
234             ret = pthread_mutex_destroy(&sq->mutex);
235             sq->id = NULL;
236 /*@-bounds@*/
237             if (sq->pipes[1])   ret = close(sq->pipes[1]);
238             if (sq->pipes[0])   ret = close(sq->pipes[0]);
239             sq->pipes[0] = sq->pipes[1] = -1;
240 /*@=bounds@*/
241 #ifdef  NOTYET  /* rpmpsmWait debugging message needs */
242             sq->reaper = 1;
243             sq->status = 0;
244             sq->reaped = 0;
245             sq->child = 0;
246 #endif
247             ret = sigrelse(SIGCHLD);
248         }
249     }
250     return ret;
251 }
252
253 /*@unchecked@*/
254 sigset_t rpmsqCaught;
255
256 /*@unchecked@*/
257 /*@-fullinitblock@*/
258 static struct rpmsig_s {
259     int signum;
260     void (*handler) (int signum, void * info, void * context);
261     int active;
262     struct sigaction oact;
263 } rpmsigTbl[] = {
264     { SIGINT,   rpmsqAction },
265 #define rpmsigTbl_sigint        (&rpmsigTbl[0])
266     { SIGQUIT,  rpmsqAction },
267 #define rpmsigTbl_sigquit       (&rpmsigTbl[1])
268     { SIGCHLD,  rpmsqAction },
269 #define rpmsigTbl_sigchld       (&rpmsigTbl[2])
270     { SIGHUP,   rpmsqAction },
271 #define rpmsigTbl_sighup        (&rpmsigTbl[3])
272     { SIGTERM,  rpmsqAction },
273 #define rpmsigTbl_sigterm       (&rpmsigTbl[4])
274     { SIGPIPE,  rpmsqAction },
275 #define rpmsigTbl_sigpipe       (&rpmsigTbl[5])
276     { -1,       NULL },
277 };
278 /*@=fullinitblock@*/
279
280 void rpmsqAction(int signum,
281                 /*@unused@*/ void * info, /*@unused@*/ void * context)
282 {
283     int save = errno;
284     rpmsig tbl;
285
286     for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
287         if (tbl->signum != signum)
288             continue;
289
290         (void) sigaddset(&rpmsqCaught, signum);
291
292         switch (signum) {
293         case SIGCHLD:
294             while (1) {
295                 rpmsq sq;
296                 int status = 0;
297                 pid_t reaped = waitpid(0, &status, WNOHANG);
298
299                 /* XXX errno set to ECHILD/EINVAL/EINTR. */
300                 if (reaped <= 0)
301                     /*@innerbreak@*/ break;
302
303                 /* XXX insque(3)/remque(3) are dequeue, not ring. */
304                 for (sq = rpmsqQueue->q_forw;
305                      sq != NULL && sq != rpmsqQueue;
306                      sq = sq->q_forw)
307                 {
308                     if (sq->child != reaped)
309                         /*@innercontinue@*/ continue;
310                     sq->reaped = reaped;
311                     sq->status = status;
312                     (void) pthread_cond_signal(&sq->cond);
313                     /*@innerbreak@*/ break;
314                 }
315             }
316             /*@switchbreak@*/ break;
317         default:
318             /*@switchbreak@*/ break;
319         }
320         break;
321     }
322     errno = save;
323 }
324
325 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
326         /*@globals rpmsigTbl @*/
327         /*@modifies rpmsigTbl @*/
328 {
329     int tblsignum = (signum >= 0 ? signum : -signum);
330     struct sigaction sa;
331     rpmsig tbl;
332     int ret = -1;
333
334     (void) DO_LOCK ();
335     if (rpmsqQueue->id == NULL)
336         rpmsqQueue->id = ME();
337     for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
338         if (tblsignum != tbl->signum)
339             continue;
340
341         if (signum >= 0) {                      /* Enable. */
342             if (ADD_REF(tbl) <= 0) {
343                 (void) sigdelset(&rpmsqCaught, tbl->signum);
344
345                 /* XXX Don't set a signal handler if already SIG_IGN */
346                 (void) sigaction(tbl->signum, NULL, &tbl->oact);
347                 if (tbl->oact.sa_handler == SIG_IGN)
348                     continue;
349
350                 (void) sigemptyset (&sa.sa_mask);
351                 sa.sa_flags = SA_SIGINFO;
352 #if defined(__LCLINT__) /* XXX glibc has union to track handler prototype. */
353                 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
354 #else
355                 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
356 #endif
357                 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
358                     SUB_REF(tbl);
359                     break;
360                 }
361                 tbl->active = 1;                /* XXX just in case */
362                 if (handler != NULL)
363                     tbl->handler = handler;
364             }
365         } else {                                /* Disable. */
366             if (SUB_REF(tbl) <= 0) {
367                 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
368                     break;
369                 tbl->active = 0;                /* XXX just in case */
370                 tbl->handler = (handler != NULL ? handler : rpmsqAction);
371             }
372         }
373         ret = tbl->active;
374         break;
375     }
376     (void) DO_UNLOCK ();
377     return ret;
378 }
379
380 pid_t rpmsqFork(rpmsq sq)
381 {
382     pid_t pid;
383     int xx;
384
385     if (sq->reaper) {
386         xx = rpmsqInsert(sq, NULL);
387 #ifdef _RPMSQ_DEBUG
388 if (_rpmsq_debug)
389 fprintf(stderr, "    Enable(%p): %p\n", ME(), sq);
390 #endif
391         xx = rpmsqEnable(SIGCHLD, NULL);
392     }
393
394     xx = pipe(sq->pipes);
395
396     xx = sighold(SIGCHLD);
397
398     pid = fork();
399     if (pid < (pid_t) 0) {              /* fork failed.  */
400 /*@-bounds@*/
401         xx = close(sq->pipes[0]);
402         xx = close(sq->pipes[1]);
403         sq->pipes[0] = sq->pipes[1] = -1;
404 /*@=bounds@*/
405         goto out;
406     } else if (pid == (pid_t) 0) {      /* Child. */
407         int yy;
408
409         /* Block to permit parent time to wait. */
410 /*@-bounds@*/
411         xx = close(sq->pipes[1]);
412         xx = read(sq->pipes[0], &yy, sizeof(yy));
413         xx = close(sq->pipes[0]);
414         sq->pipes[0] = sq->pipes[1] = -1;
415 /*@=bounds@*/
416
417 #ifdef _RPMSQ_DEBUG
418 if (_rpmsq_debug)
419 fprintf(stderr, "     Child(%p): %p child %d\n", ME(), sq, getpid());
420 #endif
421
422     } else {                            /* Parent. */
423
424         sq->child = pid;
425
426 #ifdef _RPMSQ_DEBUG
427 if (_rpmsq_debug)
428 fprintf(stderr, "    Parent(%p): %p child %d\n", ME(), sq, sq->child);
429 #endif
430
431     }
432
433 out:
434     xx = sigrelse(SIGCHLD);
435     return sq->child;
436 }
437
438 /**
439  * Wait for child process to be reaped, and unregister SIGCHLD handler.
440  * @todo Rewrite to use waitpid on helper thread.
441  * @param sq            scriptlet queue element
442  * @return              0 on success
443  */
444 static int rpmsqWaitUnregister(rpmsq sq)
445         /*@globals fileSystem, internalState @*/
446         /*@modifies sq, fileSystem, internalState @*/
447 {
448     int nothreads = 0;
449     int ret = 0;
450     int xx;
451
452     /* Protect sq->reaped from handler changes. */
453     ret = sighold(SIGCHLD);
454
455     /* Initialize the cond var mutex. */
456     if (!nothreads)
457         ret = pthread_mutex_lock(&sq->mutex);
458
459     /* Start the child, linux often runs child before parent. */
460 /*@-bounds@*/
461     if (sq->pipes[0] >= 0)
462         xx = close(sq->pipes[0]);
463     if (sq->pipes[1] >= 0)
464         xx = close(sq->pipes[1]);
465     sq->pipes[0] = sq->pipes[1] = -1;
466 /*@=bounds@*/
467
468     /* Put a stopwatch on the time spent waiting to measure performance gain. */
469     (void) rpmswEnter(&sq->op, -1);
470
471     /* Wait for handler to receive SIGCHLD. */
472     /*@-infloops@*/
473     while (ret == 0 && sq->reaped != sq->child) {
474         if (nothreads)
475             /* Note that sigpause re-enables SIGCHLD. */
476             ret = sigpause(SIGCHLD);
477         else {
478             xx = sigrelse(SIGCHLD);
479             ret = pthread_cond_wait(&sq->cond, &sq->mutex);
480             xx = sighold(SIGCHLD);
481         }
482     }
483     /*@=infloops@*/
484
485     /* Accumulate stopwatch time spent waiting, potential performance gain. */
486     sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
487
488     /* Tear down cond var mutex, our child has been reaped. */
489     if (!nothreads)
490         xx = pthread_mutex_unlock(&sq->mutex);
491     xx = sigrelse(SIGCHLD);
492
493 #ifdef _RPMSQ_DEBUG
494 if (_rpmsq_debug)
495 fprintf(stderr, "      Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
496 #endif
497
498     /* Remove processed SIGCHLD item from queue. */
499     xx = rpmsqRemove(sq);
500
501     /* Disable SIGCHLD handler on refcount == 0. */
502     xx = rpmsqEnable(-SIGCHLD, NULL);
503 #ifdef _RPMSQ_DEBUG
504 if (_rpmsq_debug)
505 fprintf(stderr, "   Disable(%p): %p\n", ME(), sq);
506 #endif
507
508     return ret;
509 }
510
511 pid_t rpmsqWait(rpmsq sq)
512 {
513
514 #ifdef _RPMSQ_DEBUG
515 if (_rpmsq_debug)
516 fprintf(stderr, "      Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
517 #endif
518
519     if (sq->reaper) {
520         (void) rpmsqWaitUnregister(sq);
521     } else {
522         pid_t reaped;
523         int status;
524         do {
525             reaped = waitpid(sq->child, &status, 0);
526         } while (reaped >= 0 && reaped != sq->child);
527         sq->reaped = reaped;
528         sq->status = status;
529 #ifdef _RPMSQ_DEBUG
530 if (_rpmsq_debug)
531 fprintf(stderr, "   Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
532 #endif
533     }
534
535 #ifdef _RPMSQ_DEBUG
536 if (_rpmsq_debug)
537 fprintf(stderr, "      Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
538 #endif
539
540     return sq->reaped;
541 }
542
543 void * rpmsqThread(void * (*start) (void * arg), void * arg)
544 {
545     pthread_t pth;
546     int ret;
547
548     ret = pthread_create(&pth, NULL, start, arg);
549     return (ret == 0 ? (void *)pth : NULL);
550 }
551
552 int rpmsqJoin(void * thread)
553 {
554     pthread_t pth = (pthread_t) thread;
555     if (thread == NULL)
556         return EINVAL;
557     return pthread_join(pth, NULL);
558 }
559
560 int rpmsqThreadEqual(void * thread)
561 {
562     pthread_t t1 = (pthread_t) thread;
563     pthread_t t2 = pthread_self();
564     return pthread_equal(t1, t2);
565 }
566
567 /**
568  * SIGCHLD cancellation handler.
569  */
570 static void
571 sigchld_cancel (void *arg)
572         /*@globals rpmsigTbl, fileSystem, internalState @*/
573         /*@modifies rpmsigTbl, fileSystem, internalState @*/
574 {
575     pid_t child = *(pid_t *) arg;
576     pid_t result;
577
578     (void) kill(child, SIGKILL);
579
580     do {
581         result = waitpid(child, NULL, 0);
582     } while (result == (pid_t)-1 && errno == EINTR);
583
584     (void) DO_LOCK ();
585     if (SUB_REF (rpmsigTbl_sigchld) == 0) {
586         (void) rpmsqEnable(-SIGQUIT, NULL);
587         (void) rpmsqEnable(-SIGINT, NULL);
588     }
589     (void) DO_UNLOCK ();
590 }
591
592 /**
593  * Execute a command, returning its status.
594  */
595 int
596 rpmsqExecve (const char ** argv)
597         /*@globals rpmsigTbl @*/
598         /*@modifies rpmsigTbl @*/
599 {
600     int oldtype;
601     int status = -1;
602     pid_t pid = 0;
603     pid_t result;
604     sigset_t newMask, oldMask;
605     rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
606
607     (void) DO_LOCK ();
608     if (ADD_REF (rpmsigTbl_sigchld) == 0) {
609         if (rpmsqEnable(SIGINT, NULL) < 0) {
610             SUB_REF (rpmsigTbl_sigchld);
611             goto out;
612         }
613         if (rpmsqEnable(SIGQUIT, NULL) < 0) {
614             SUB_REF (rpmsigTbl_sigchld);
615             goto out_restore_sigint;
616         }
617     }
618     (void) DO_UNLOCK ();
619
620     (void) sigemptyset (&newMask);
621     (void) sigaddset (&newMask, SIGCHLD);
622     if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
623         (void) DO_LOCK ();
624         if (SUB_REF (rpmsigTbl_sigchld) == 0)
625             goto out_restore_sigquit_and_sigint;
626         goto out;
627     }
628
629     CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
630
631     pid = fork ();
632     if (pid < (pid_t) 0) {              /* fork failed.  */
633         goto out;
634     } else if (pid == (pid_t) 0) {      /* Child. */
635
636         /* Restore the signals.  */
637         (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
638         (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
639         (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
640
641         /* Reset rpmsigTbl lock and refcnt. */
642         INIT_LOCK ();
643
644         (void) execve (argv[0], (char *const *) argv, environ);
645         _exit (127);
646     } else {                            /* Parent. */
647         do {
648             result = waitpid(pid, &status, 0);
649         } while (result == (pid_t)-1 && errno == EINTR);
650         if (result != pid)
651             status = -1;
652     }
653
654     CLEANUP_RESET(0, oldtype);
655
656     (void) DO_LOCK ();
657     if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
658         (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
659       || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
660     {
661         status = -1;
662     }
663     goto out;
664
665 out_restore_sigquit_and_sigint:
666     (void) rpmsqEnable(-SIGQUIT, NULL);
667 out_restore_sigint:
668     (void) rpmsqEnable(-SIGINT, NULL);
669 out:
670     (void) DO_UNLOCK ();
671     return status;
672 }