Fork failure patch from OpenSuSE
[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 /* XXX suggested in bugzilla #159024 */
130 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
131   #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
132 #endif
133
134 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
135 /*@unchecked@*/
136 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
137 #else
138 /*@unchecked@*/
139 /*@-type@*/
140 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
141 /*@=type@*/
142 #endif
143
144 #define DO_LOCK()       pthread_mutex_lock(&rpmsigTbl_lock);
145 #define DO_UNLOCK()     pthread_mutex_unlock(&rpmsigTbl_lock);
146 #define INIT_LOCK()     \
147     {   pthread_mutexattr_t attr; \
148         (void) pthread_mutexattr_init(&attr); \
149         (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
150         (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
151         (void) pthread_mutexattr_destroy(&attr); \
152         rpmsigTbl_sigchld->active = 0; \
153     }
154 #define ADD_REF(__tbl)  (__tbl)->active++
155 #define SUB_REF(__tbl)  --(__tbl)->active
156 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
157     (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
158         pthread_cleanup_push((__handler), (__arg));
159 #define CLEANUP_RESET(__execute, __oldtype) \
160     pthread_cleanup_pop(__execute); \
161     (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
162
163 #define SAME_THREAD(_a, _b)     pthread_equal(((pthread_t)_a), ((pthread_t)_b))
164
165 #define ME()    ((void *)pthread_self())
166
167 #else
168
169 #define DO_LOCK()
170 #define DO_UNLOCK()
171 #define INIT_LOCK()
172 #define ADD_REF(__tbl)  /*@-noeffect@*/ (0) /*@=noeffect@*/
173 #define SUB_REF(__tbl)  /*@-noeffect@*/ (0) /*@=noeffect@*/
174 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
175 #define CLEANUP_RESET(__execute, __oldtype)
176
177 #define SAME_THREAD(_a, _b)     (42)
178
179 #define ME()    (((void *)getpid()))
180
181 #endif  /* HAVE_PTHREAD_H */
182
183 #include <rpmsq.h>
184
185 #include "debug.h"
186
187 #define _RPMSQ_DEBUG    0
188 /*@unchecked@*/
189 int _rpmsq_debug = _RPMSQ_DEBUG;
190
191 /*@unchecked@*/
192 static struct rpmsqElem rpmsqRock;
193
194 /*@-compmempass@*/
195 /*@unchecked@*/
196 rpmsq rpmsqQueue = &rpmsqRock;
197 /*@=compmempass@*/
198
199 int rpmsqInsert(void * elem, void * prev)
200 {
201     rpmsq sq = (rpmsq) elem;
202     int ret = -1;
203
204     if (sq != NULL) {
205 #ifdef _RPMSQ_DEBUG
206 if (_rpmsq_debug)
207 fprintf(stderr, "    Insert(%p): %p\n", ME(), sq);
208 #endif
209         ret = sighold(SIGCHLD);
210         if (ret == 0) {
211             sq->child = 0;
212             sq->reaped = 0;
213             sq->status = 0;
214             sq->reaper = 1;
215 /*@-bounds@*/
216             sq->pipes[0] = sq->pipes[1] = -1;
217 /*@=bounds@*/
218
219             sq->id = ME();
220             ret = pthread_mutex_init(&sq->mutex, NULL);
221             insque(elem, (prev != NULL ? prev : rpmsqQueue));
222             ret = sigrelse(SIGCHLD);
223         }
224     }
225     return ret;
226 }
227
228 int rpmsqRemove(void * elem)
229 {
230     rpmsq sq = (rpmsq) elem;
231     int ret = -1;
232
233     if (elem != NULL) {
234
235 #ifdef _RPMSQ_DEBUG
236 if (_rpmsq_debug)
237 fprintf(stderr, "    Remove(%p): %p\n", ME(), sq);
238 #endif
239         ret = sighold (SIGCHLD);
240         if (ret == 0) {
241             remque(elem);
242            
243             /* Unlock the mutex and then destroy it */ 
244             if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
245                 ret = pthread_mutex_destroy(&sq->mutex);
246
247             sq->id = NULL;
248 /*@-bounds@*/
249             if (sq->pipes[1])   ret = close(sq->pipes[1]);
250             if (sq->pipes[0])   ret = close(sq->pipes[0]);
251             sq->pipes[0] = sq->pipes[1] = -1;
252 /*@=bounds@*/
253 #ifdef  NOTYET  /* rpmpsmWait debugging message needs */
254             sq->reaper = 1;
255             sq->status = 0;
256             sq->reaped = 0;
257             sq->child = 0;
258 #endif
259             ret = sigrelse(SIGCHLD);
260         }
261     }
262     return ret;
263 }
264
265 /*@unchecked@*/
266 sigset_t rpmsqCaught;
267
268 /*@unchecked@*/
269 /*@-fullinitblock@*/
270 static struct rpmsig_s {
271     int signum;
272     void (*handler) (int signum, void * info, void * context);
273     int active;
274     struct sigaction oact;
275 } rpmsigTbl[] = {
276     { SIGINT,   rpmsqAction },
277 #define rpmsigTbl_sigint        (&rpmsigTbl[0])
278     { SIGQUIT,  rpmsqAction },
279 #define rpmsigTbl_sigquit       (&rpmsigTbl[1])
280     { SIGCHLD,  rpmsqAction },
281 #define rpmsigTbl_sigchld       (&rpmsigTbl[2])
282     { SIGHUP,   rpmsqAction },
283 #define rpmsigTbl_sighup        (&rpmsigTbl[3])
284     { SIGTERM,  rpmsqAction },
285 #define rpmsigTbl_sigterm       (&rpmsigTbl[4])
286     { SIGPIPE,  rpmsqAction },
287 #define rpmsigTbl_sigpipe       (&rpmsigTbl[5])
288     { -1,       NULL },
289 };
290 /*@=fullinitblock@*/
291
292 void rpmsqAction(int signum,
293                 /*@unused@*/ void * info, /*@unused@*/ void * context)
294 {
295     int save = errno;
296     rpmsig tbl;
297
298     for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
299         if (tbl->signum != signum)
300             continue;
301
302         (void) sigaddset(&rpmsqCaught, signum);
303
304         switch (signum) {
305         case SIGCHLD:
306             while (1) {
307                 rpmsq sq;
308                 int status = 0;
309                 pid_t reaped = waitpid(0, &status, WNOHANG);
310
311                 /* XXX errno set to ECHILD/EINVAL/EINTR. */
312                 if (reaped <= 0)
313                     /*@innerbreak@*/ break;
314
315                 /* XXX insque(3)/remque(3) are dequeue, not ring. */
316                 for (sq = rpmsqQueue->q_forw;
317                      sq != NULL && sq != rpmsqQueue;
318                      sq = sq->q_forw)
319                 {
320                     int ret;
321
322                     if (sq->child != reaped)
323                         /*@innercontinue@*/ continue;
324                     sq->reaped = reaped;
325                     sq->status = status;
326
327                     /* Unlock the mutex.  The waiter will then be able to 
328                      * aquire the lock.  
329                      *
330                      * XXX: jbj, wtd, if this fails? 
331                      */
332                     ret = pthread_mutex_unlock(&sq->mutex); 
333
334                     /*@innerbreak@*/ break;
335                 }
336             }
337             /*@switchbreak@*/ break;
338         default:
339             /*@switchbreak@*/ break;
340         }
341         break;
342     }
343     errno = save;
344 }
345
346 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
347         /*@globals rpmsigTbl @*/
348         /*@modifies rpmsigTbl @*/
349 {
350     int tblsignum = (signum >= 0 ? signum : -signum);
351     struct sigaction sa;
352     rpmsig tbl;
353     int ret = -1;
354
355     (void) DO_LOCK ();
356     if (rpmsqQueue->id == NULL)
357         rpmsqQueue->id = ME();
358     for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
359         if (tblsignum != tbl->signum)
360             continue;
361
362         if (signum >= 0) {                      /* Enable. */
363             if (ADD_REF(tbl) <= 0) {
364                 (void) sigdelset(&rpmsqCaught, tbl->signum);
365
366                 /* XXX Don't set a signal handler if already SIG_IGN */
367                 (void) sigaction(tbl->signum, NULL, &tbl->oact);
368                 if (tbl->oact.sa_handler == SIG_IGN)
369                     continue;
370
371                 (void) sigemptyset (&sa.sa_mask);
372                 sa.sa_flags = SA_SIGINFO;
373 #if defined(__LCLINT__) /* XXX glibc has union to track handler prototype. */
374                 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
375 #else
376                 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
377 #endif
378                 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
379                     SUB_REF(tbl);
380                     break;
381                 }
382                 tbl->active = 1;                /* XXX just in case */
383                 if (handler != NULL)
384                     tbl->handler = handler;
385             }
386         } else {                                /* Disable. */
387             if (SUB_REF(tbl) <= 0) {
388                 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
389                     break;
390                 tbl->active = 0;                /* XXX just in case */
391                 tbl->handler = (handler != NULL ? handler : rpmsqAction);
392             }
393         }
394         ret = tbl->active;
395         break;
396     }
397     (void) DO_UNLOCK ();
398     return ret;
399 }
400
401 pid_t rpmsqFork(rpmsq sq)
402 {
403     pid_t pid;
404     int xx;
405     int nothreads = 0;   /* XXX: Shouldn't this be a global? */
406
407     if (sq->reaper) {
408         xx = rpmsqInsert(sq, NULL);
409 #ifdef _RPMSQ_DEBUG
410 if (_rpmsq_debug)
411 fprintf(stderr, "    Enable(%p): %p\n", ME(), sq);
412 #endif
413         xx = rpmsqEnable(SIGCHLD, NULL);
414     }
415
416     xx = pipe(sq->pipes);
417
418     xx = sighold(SIGCHLD);
419
420     /* 
421      * Initialize the cond var mutex.   We have to aquire the lock we 
422      * use for the condition before we fork.  Otherwise it is possible for
423      * the child to exit, we get sigchild and the sig handler to send 
424      * the condition signal before we are waiting on the condition.
425      */
426     if (!nothreads) {
427         if(pthread_mutex_lock(&sq->mutex)) {
428             /* Yack we did not get the lock, lets just give up */
429 /*@-bounds@*/
430             xx = close(sq->pipes[0]);
431             xx = close(sq->pipes[1]);
432             sq->pipes[0] = sq->pipes[1] = -1;
433 /*@=bounds@*/
434             goto out;
435         }
436     }
437
438     pid = fork();
439     if (pid < (pid_t) 0) {              /* fork failed.  */
440         sq->child = (pid_t)-1;
441 /*@-bounds@*/
442         xx = close(sq->pipes[0]);
443         xx = close(sq->pipes[1]);
444         sq->pipes[0] = sq->pipes[1] = -1;
445 /*@=bounds@*/
446         goto out;
447     } else if (pid == (pid_t) 0) {      /* Child. */
448         int yy;
449
450         /* Block to permit parent time to wait. */
451 /*@-bounds@*/
452         xx = close(sq->pipes[1]);
453         xx = read(sq->pipes[0], &yy, sizeof(yy));
454         xx = close(sq->pipes[0]);
455         sq->pipes[0] = sq->pipes[1] = -1;
456 /*@=bounds@*/
457
458 #ifdef _RPMSQ_DEBUG
459 if (_rpmsq_debug)
460 fprintf(stderr, "     Child(%p): %p child %d\n", ME(), sq, getpid());
461 #endif
462
463     } else {                            /* Parent. */
464
465         sq->child = pid;
466
467 #ifdef _RPMSQ_DEBUG
468 if (_rpmsq_debug)
469 fprintf(stderr, "    Parent(%p): %p child %d\n", ME(), sq, sq->child);
470 #endif
471
472     }
473
474 out:
475     xx = sigrelse(SIGCHLD);
476     return sq->child;
477 }
478
479 /**
480  * Wait for child process to be reaped, and unregister SIGCHLD handler.
481  * @todo Rewrite to use waitpid on helper thread.
482  * @param sq            scriptlet queue element
483  * @return              0 on success
484  */
485 static int rpmsqWaitUnregister(rpmsq sq)
486         /*@globals fileSystem, internalState @*/
487         /*@modifies sq, fileSystem, internalState @*/
488 {
489     int nothreads = 0;
490     int ret = 0;
491     int xx;
492
493     /* Protect sq->reaped from handler changes. */
494     ret = sighold(SIGCHLD);
495
496     /* Start the child, linux often runs child before parent. */
497 /*@-bounds@*/
498     if (sq->pipes[0] >= 0)
499         xx = close(sq->pipes[0]);
500     if (sq->pipes[1] >= 0)
501         xx = close(sq->pipes[1]);
502     sq->pipes[0] = sq->pipes[1] = -1;
503 /*@=bounds@*/
504
505     /* Put a stopwatch on the time spent waiting to measure performance gain. */
506     (void) rpmswEnter(&sq->op, -1);
507
508     /* Wait for handler to receive SIGCHLD. */
509     /*@-infloops@*/
510     while (ret == 0 && sq->reaped != sq->child) {
511         if (nothreads)
512             /* Note that sigpause re-enables SIGCHLD. */
513             ret = sigpause(SIGCHLD);
514         else {
515             xx = sigrelse(SIGCHLD);
516             
517             /* 
518              * We start before the fork with this mutex locked;
519              * The only one that unlocks this the signal handler.
520              * So if we get the lock the child has been reaped.
521              */
522             ret = pthread_mutex_lock(&sq->mutex);
523             xx = sighold(SIGCHLD);
524         }
525     }
526     /*@=infloops@*/
527
528     /* Accumulate stopwatch time spent waiting, potential performance gain. */
529     sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
530
531     xx = sigrelse(SIGCHLD);
532
533 #ifdef _RPMSQ_DEBUG
534 if (_rpmsq_debug)
535 fprintf(stderr, "      Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
536 #endif
537
538     /* Remove processed SIGCHLD item from queue. */
539     xx = rpmsqRemove(sq);
540
541     /* Disable SIGCHLD handler on refcount == 0. */
542     xx = rpmsqEnable(-SIGCHLD, NULL);
543 #ifdef _RPMSQ_DEBUG
544 if (_rpmsq_debug)
545 fprintf(stderr, "   Disable(%p): %p\n", ME(), sq);
546 #endif
547
548     return ret;
549 }
550
551 pid_t rpmsqWait(rpmsq sq)
552 {
553
554 #ifdef _RPMSQ_DEBUG
555 if (_rpmsq_debug)
556 fprintf(stderr, "      Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
557 #endif
558
559     if (sq->reaper) {
560         (void) rpmsqWaitUnregister(sq);
561     } else {
562         pid_t reaped;
563         int status;
564         do {
565             reaped = waitpid(sq->child, &status, 0);
566         } while (reaped >= 0 && reaped != sq->child);
567         sq->reaped = reaped;
568         sq->status = status;
569 #ifdef _RPMSQ_DEBUG
570 if (_rpmsq_debug)
571 fprintf(stderr, "   Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
572 #endif
573     }
574
575 #ifdef _RPMSQ_DEBUG
576 if (_rpmsq_debug)
577 fprintf(stderr, "      Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
578 #endif
579
580     return sq->reaped;
581 }
582
583 void * rpmsqThread(void * (*start) (void * arg), void * arg)
584 {
585     pthread_t pth;
586     int ret;
587
588     ret = pthread_create(&pth, NULL, start, arg);
589     return (ret == 0 ? (void *)pth : NULL);
590 }
591
592 int rpmsqJoin(void * thread)
593 {
594     pthread_t pth = (pthread_t) thread;
595     if (thread == NULL)
596         return EINVAL;
597     return pthread_join(pth, NULL);
598 }
599
600 int rpmsqThreadEqual(void * thread)
601 {
602     pthread_t t1 = (pthread_t) thread;
603     pthread_t t2 = pthread_self();
604     return pthread_equal(t1, t2);
605 }
606
607 /**
608  * SIGCHLD cancellation handler.
609  */
610 static void
611 sigchld_cancel (void *arg)
612         /*@globals rpmsigTbl, fileSystem, internalState @*/
613         /*@modifies rpmsigTbl, fileSystem, internalState @*/
614 {
615     pid_t child = *(pid_t *) arg;
616     pid_t result;
617
618     (void) kill(child, SIGKILL);
619
620     do {
621         result = waitpid(child, NULL, 0);
622     } while (result == (pid_t)-1 && errno == EINTR);
623
624     (void) DO_LOCK ();
625     if (SUB_REF (rpmsigTbl_sigchld) == 0) {
626         (void) rpmsqEnable(-SIGQUIT, NULL);
627         (void) rpmsqEnable(-SIGINT, NULL);
628     }
629     (void) DO_UNLOCK ();
630 }
631
632 /**
633  * Execute a command, returning its status.
634  */
635 int
636 rpmsqExecve (const char ** argv)
637         /*@globals rpmsigTbl @*/
638         /*@modifies rpmsigTbl @*/
639 {
640     int oldtype;
641     int status = -1;
642     pid_t pid = 0;
643     pid_t result;
644     sigset_t newMask, oldMask;
645     rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
646
647 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
648         INIT_LOCK ();
649 #endif
650
651     (void) DO_LOCK ();
652     if (ADD_REF (rpmsigTbl_sigchld) == 0) {
653         if (rpmsqEnable(SIGINT, NULL) < 0) {
654             SUB_REF (rpmsigTbl_sigchld);
655             goto out;
656         }
657         if (rpmsqEnable(SIGQUIT, NULL) < 0) {
658             SUB_REF (rpmsigTbl_sigchld);
659             goto out_restore_sigint;
660         }
661     }
662     (void) DO_UNLOCK ();
663
664     (void) sigemptyset (&newMask);
665     (void) sigaddset (&newMask, SIGCHLD);
666     if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
667         (void) DO_LOCK ();
668         if (SUB_REF (rpmsigTbl_sigchld) == 0)
669             goto out_restore_sigquit_and_sigint;
670         goto out;
671     }
672
673     CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
674
675     pid = fork ();
676     if (pid < (pid_t) 0) {              /* fork failed.  */
677         goto out;
678     } else if (pid == (pid_t) 0) {      /* Child. */
679
680         /* Restore the signals.  */
681         (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
682         (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
683         (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
684
685         /* Reset rpmsigTbl lock and refcnt. */
686         INIT_LOCK ();
687
688         (void) execve (argv[0], (char *const *) argv, environ);
689         _exit (127);
690     } else {                            /* Parent. */
691         do {
692             result = waitpid(pid, &status, 0);
693         } while (result == (pid_t)-1 && errno == EINTR);
694         if (result != pid)
695             status = -1;
696     }
697
698     CLEANUP_RESET(0, oldtype);
699
700     (void) DO_LOCK ();
701     if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
702         (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
703       || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
704     {
705         status = -1;
706     }
707     goto out;
708
709 out_restore_sigquit_and_sigint:
710     (void) rpmsqEnable(-SIGQUIT, NULL);
711 out_restore_sigint:
712     (void) rpmsqEnable(-SIGINT, NULL);
713 out:
714     (void) DO_UNLOCK ();
715     return status;
716 }