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