Add comments, minor performance improvement.
[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                 (void) sigemptyset (&sa.sa_mask);
345 /*@-compdef -type @*/
346                 sa.sa_flags = SA_SIGINFO;
347                 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
348                 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
349                     SUB_REF(tbl);
350                     break;
351                 }
352 /*@=compdef =type @*/
353                 tbl->active = 1;                /* XXX just in case */
354                 if (handler != NULL)
355                     tbl->handler = handler;
356             }
357         } else {                                /* Disable. */
358             if (SUB_REF(tbl) <= 0) {
359                 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
360                     break;
361                 tbl->active = 0;                /* XXX just in case */
362                 tbl->handler = (handler != NULL ? handler : rpmsqAction);
363             }
364         }
365         ret = tbl->active;
366         break;
367     }
368     (void) DO_UNLOCK ();
369     return ret;
370 }
371
372 pid_t rpmsqFork(rpmsq sq)
373 {
374     pid_t pid;
375     int xx;
376
377     if (sq->reaper) {
378         xx = rpmsqInsert(sq, NULL);
379 #ifdef _RPMSQ_DEBUG
380 if (_rpmsq_debug)
381 fprintf(stderr, "    Enable(%p): %p\n", ME(), sq);
382 #endif
383         xx = rpmsqEnable(SIGCHLD, NULL);
384     }
385
386     xx = pipe(sq->pipes);
387
388     xx = sighold(SIGCHLD);
389
390     pid = fork();
391     if (pid < (pid_t) 0) {              /* fork failed.  */
392 /*@-bounds@*/
393         xx = close(sq->pipes[0]);
394         xx = close(sq->pipes[1]);
395         sq->pipes[0] = sq->pipes[1] = -1;
396 /*@=bounds@*/
397         goto out;
398     } else if (pid == (pid_t) 0) {      /* Child. */
399         int yy;
400
401         /* Block to permit parent time to wait. */
402 /*@-bounds@*/
403         xx = close(sq->pipes[1]);
404         xx = read(sq->pipes[0], &yy, sizeof(yy));
405         xx = close(sq->pipes[0]);
406         sq->pipes[0] = sq->pipes[1] = -1;
407 /*@=bounds@*/
408
409 #ifdef _RPMSQ_DEBUG
410 if (_rpmsq_debug)
411 fprintf(stderr, "     Child(%p): %p child %d\n", ME(), sq, getpid());
412 #endif
413
414     } else {                            /* Parent. */
415
416         sq->child = pid;
417
418 #ifdef _RPMSQ_DEBUG
419 if (_rpmsq_debug)
420 fprintf(stderr, "    Parent(%p): %p child %d\n", ME(), sq, sq->child);
421 #endif
422
423     }
424
425 out:
426     xx = sigrelse(SIGCHLD);
427     return sq->child;
428 }
429
430 /**
431  * Wait for child process to be reaped, and unregister SIGCHLD handler.
432  * @todo Rewrite to use waitpid on helper thread.
433  * @param sq            scriptlet queue element
434  * @return              0 on success
435  */
436 static int rpmsqWaitUnregister(rpmsq sq)
437         /*@globals fileSystem, internalState @*/
438         /*@modifies sq, fileSystem, internalState @*/
439 {
440     int nothreads = 0;
441     int ret = 0;
442     int xx;
443
444     /* Protect sq->reaped from handler changes. */
445     ret = sighold(SIGCHLD);
446
447     /* Initialize the cond var mutex. */
448     if (!nothreads)
449         ret = pthread_mutex_lock(&sq->mutex);
450
451     /* Start the child, linux often runs child before parent. */
452 /*@-bounds@*/
453     if (sq->pipes[0] >= 0)
454         xx = close(sq->pipes[0]);
455     if (sq->pipes[1] >= 0)
456         xx = close(sq->pipes[1]);
457     sq->pipes[0] = sq->pipes[1] = -1;
458 /*@=bounds@*/
459
460     /* Put a stopwatch on the time spent waiting to measure performance gain. */
461     (void) rpmswEnter(&sq->op, -1);
462
463     /* Wait for handler to receive SIGCHLD. */
464     /*@-infloops@*/
465     while (ret == 0 && sq->reaped != sq->child) {
466         if (nothreads)
467             /* Note that sigpause re-enables SIGCHLD. */
468             ret = sigpause(SIGCHLD);
469         else {
470             xx = sigrelse(SIGCHLD);
471             ret = pthread_cond_wait(&sq->cond, &sq->mutex);
472             xx = sighold(SIGCHLD);
473         }
474     }
475     /*@=infloops@*/
476
477     /* Accumulate stopwatch time spent waiting, potential performance gain. */
478     sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
479
480     /* Tear down cond var mutex, our child has been reaped. */
481     if (!nothreads)
482         xx = pthread_mutex_unlock(&sq->mutex);
483     xx = sigrelse(SIGCHLD);
484
485 #ifdef _RPMSQ_DEBUG
486 if (_rpmsq_debug)
487 fprintf(stderr, "      Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
488 #endif
489
490     /* Remove processed SIGCHLD item from queue. */
491     xx = rpmsqRemove(sq);
492
493     /* Disable SIGCHLD handler on refcount == 0. */
494     xx = rpmsqEnable(-SIGCHLD, NULL);
495 #ifdef _RPMSQ_DEBUG
496 if (_rpmsq_debug)
497 fprintf(stderr, "   Disable(%p): %p\n", ME(), sq);
498 #endif
499
500     return ret;
501 }
502
503 pid_t rpmsqWait(rpmsq sq)
504 {
505
506 #ifdef _RPMSQ_DEBUG
507 if (_rpmsq_debug)
508 fprintf(stderr, "      Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
509 #endif
510
511     if (sq->reaper) {
512         (void) rpmsqWaitUnregister(sq);
513     } else {
514         pid_t reaped;
515         int status;
516         do {
517             reaped = waitpid(sq->child, &status, 0);
518         } while (reaped >= 0 && reaped != sq->child);
519         sq->reaped = reaped;
520         sq->status = status;
521 #ifdef _RPMSQ_DEBUG
522 if (_rpmsq_debug)
523 fprintf(stderr, "   Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
524 #endif
525     }
526
527 #ifdef _RPMSQ_DEBUG
528 if (_rpmsq_debug)
529 fprintf(stderr, "      Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
530 #endif
531
532     return sq->reaped;
533 }
534
535 void * rpmsqThread(void * (*start) (void * arg), void * arg)
536 {
537     pthread_t pth;
538     int ret;
539
540     ret = pthread_create(&pth, NULL, start, arg);
541     return (ret == 0 ? (void *)pth : NULL);
542 }
543
544 int rpmsqJoin(void * thread)
545 {
546     pthread_t pth = (pthread_t) thread;
547     if (thread == NULL)
548         return EINVAL;
549     return pthread_join(pth, NULL);
550 }
551
552 int rpmsqThreadEqual(void * thread)
553 {
554     pthread_t t1 = (pthread_t) thread;
555     pthread_t t2 = pthread_self();
556     return pthread_equal(t1, t2);
557 }
558
559 /**
560  * SIGCHLD cancellation handler.
561  */
562 static void
563 sigchld_cancel (void *arg)
564         /*@globals rpmsigTbl, fileSystem, internalState @*/
565         /*@modifies rpmsigTbl, fileSystem, internalState @*/
566 {
567     pid_t child = *(pid_t *) arg;
568     pid_t result;
569
570     (void) kill(child, SIGKILL);
571
572     do {
573         result = waitpid(child, NULL, 0);
574     } while (result == (pid_t)-1 && errno == EINTR);
575
576     (void) DO_LOCK ();
577     if (SUB_REF (rpmsigTbl_sigchld) == 0) {
578         (void) rpmsqEnable(-SIGQUIT, NULL);
579         (void) rpmsqEnable(-SIGINT, NULL);
580     }
581     (void) DO_UNLOCK ();
582 }
583
584 /**
585  * Execute a command, returning its status.
586  */
587 int
588 rpmsqExecve (const char ** argv)
589         /*@globals rpmsigTbl @*/
590         /*@modifies rpmsigTbl @*/
591 {
592     int oldtype;
593     int status = -1;
594     pid_t pid = 0;
595     pid_t result;
596     sigset_t newMask, oldMask;
597     rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
598
599     (void) DO_LOCK ();
600     if (ADD_REF (rpmsigTbl_sigchld) == 0) {
601         if (rpmsqEnable(SIGINT, NULL) < 0) {
602             SUB_REF (rpmsigTbl_sigchld);
603             goto out;
604         }
605         if (rpmsqEnable(SIGQUIT, NULL) < 0) {
606             SUB_REF (rpmsigTbl_sigchld);
607             goto out_restore_sigint;
608         }
609     }
610     (void) DO_UNLOCK ();
611
612     (void) sigemptyset (&newMask);
613     (void) sigaddset (&newMask, SIGCHLD);
614     if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
615         (void) DO_LOCK ();
616         if (SUB_REF (rpmsigTbl_sigchld) == 0)
617             goto out_restore_sigquit_and_sigint;
618         goto out;
619     }
620
621     CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
622
623     pid = fork ();
624     if (pid < (pid_t) 0) {              /* fork failed.  */
625         goto out;
626     } else if (pid == (pid_t) 0) {      /* Child. */
627
628         /* Restore the signals.  */
629         (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
630         (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
631         (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
632
633         /* Reset rpmsigTbl lock and refcnt. */
634         INIT_LOCK ();
635
636         (void) execve (argv[0], (char *const *) argv, environ);
637         _exit (127);
638     } else {                            /* Parent. */
639         do {
640             result = waitpid(pid, &status, 0);
641         } while (result == (pid_t)-1 && errno == EINTR);
642         if (result != pid)
643             status = -1;
644     }
645
646     CLEANUP_RESET(0, oldtype);
647
648     (void) DO_LOCK ();
649     if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
650         (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
651       || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
652     {
653         status = -1;
654     }
655     goto out;
656
657 out_restore_sigquit_and_sigint:
658     (void) rpmsqEnable(-SIGQUIT, NULL);
659 out_restore_sigint:
660     (void) rpmsqEnable(-SIGINT, NULL);
661 out:
662     (void) DO_UNLOCK ();
663     return status;
664 }