7 #if defined(__LCLINT__)
8 #define _BITS_SIGTHREAD_H /* XXX avoid __sigset_t heartburn. */
10 /*@-incondefs -protoparammatch@*/
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 @*/;
25 extern void insque(struct qelem * __elem, struct qelem * __prev)
26 /*@modifies __elem, __prev @*/;
27 extern void remque(struct qelem * __elem)
28 /*@modifies __elem @*/;
30 extern pthread_t pthread_self(void)
32 extern int pthread_equal(pthread_t t1, pthread_t t2)
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 @*/;
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 @*/;
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 @*/;
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 @*/;
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 @*/;
85 extern int pthread_mutex_lock(pthread_mutex_t *mutex)
87 /*@modifies *mutex, errno @*/;
88 extern int pthread_mutex_trylock(pthread_mutex_t *mutex)
90 /*@modifies *mutex, errno @*/;
91 extern int pthread_mutex_unlock(pthread_mutex_t *mutex)
93 /*@modifies *mutex, errno @*/;
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 @*/;
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 @*/;
117 /*@=incondefs =protoparammatch@*/
121 #include <sys/signal.h>
122 #include <sys/wait.h>
125 #if defined(HAVE_PTHREAD_H)
129 /* XXX suggested in bugzilla #159024 */
130 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
131 #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
134 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
136 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
140 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
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; \
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));
163 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
165 #define ME() ((void *)pthread_self())
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)
177 #define SAME_THREAD(_a, _b) (42)
179 #define ME() (((void *)getpid()))
181 #endif /* HAVE_PTHREAD_H */
187 #define _RPMSQ_DEBUG 0
189 int _rpmsq_debug = _RPMSQ_DEBUG;
192 static struct rpmsqElem rpmsqRock;
196 rpmsq rpmsqQueue = &rpmsqRock;
199 int rpmsqInsert(void * elem, void * prev)
201 rpmsq sq = (rpmsq) elem;
207 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
209 ret = sighold(SIGCHLD);
216 sq->pipes[0] = sq->pipes[1] = -1;
220 ret = pthread_mutex_init(&sq->mutex, NULL);
221 ret = pthread_cond_init(&sq->cond, NULL);
222 insque(elem, (prev != NULL ? prev : rpmsqQueue));
223 ret = sigrelse(SIGCHLD);
229 int rpmsqRemove(void * elem)
231 rpmsq sq = (rpmsq) elem;
238 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
240 ret = sighold (SIGCHLD);
243 ret = pthread_cond_destroy(&sq->cond);
244 ret = pthread_mutex_destroy(&sq->mutex);
247 if (sq->pipes[1]) ret = close(sq->pipes[1]);
248 if (sq->pipes[0]) ret = close(sq->pipes[0]);
249 sq->pipes[0] = sq->pipes[1] = -1;
251 #ifdef NOTYET /* rpmpsmWait debugging message needs */
257 ret = sigrelse(SIGCHLD);
264 sigset_t rpmsqCaught;
268 static struct rpmsig_s {
270 void (*handler) (int signum, void * info, void * context);
272 struct sigaction oact;
274 { SIGINT, rpmsqAction },
275 #define rpmsigTbl_sigint (&rpmsigTbl[0])
276 { SIGQUIT, rpmsqAction },
277 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
278 { SIGCHLD, rpmsqAction },
279 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
280 { SIGHUP, rpmsqAction },
281 #define rpmsigTbl_sighup (&rpmsigTbl[3])
282 { SIGTERM, rpmsqAction },
283 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
284 { SIGPIPE, rpmsqAction },
285 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
290 void rpmsqAction(int signum,
291 /*@unused@*/ void * info, /*@unused@*/ void * context)
296 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
297 if (tbl->signum != signum)
300 (void) sigaddset(&rpmsqCaught, signum);
307 pid_t reaped = waitpid(0, &status, WNOHANG);
309 /* XXX errno set to ECHILD/EINVAL/EINTR. */
311 /*@innerbreak@*/ break;
313 /* XXX insque(3)/remque(3) are dequeue, not ring. */
314 for (sq = rpmsqQueue->q_forw;
315 sq != NULL && sq != rpmsqQueue;
318 if (sq->child != reaped)
319 /*@innercontinue@*/ continue;
322 (void) pthread_cond_signal(&sq->cond);
323 /*@innerbreak@*/ break;
326 /*@switchbreak@*/ break;
328 /*@switchbreak@*/ break;
335 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
336 /*@globals rpmsigTbl @*/
337 /*@modifies rpmsigTbl @*/
339 int tblsignum = (signum >= 0 ? signum : -signum);
345 if (rpmsqQueue->id == NULL)
346 rpmsqQueue->id = ME();
347 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
348 if (tblsignum != tbl->signum)
351 if (signum >= 0) { /* Enable. */
352 if (ADD_REF(tbl) <= 0) {
353 (void) sigdelset(&rpmsqCaught, tbl->signum);
355 /* XXX Don't set a signal handler if already SIG_IGN */
356 (void) sigaction(tbl->signum, NULL, &tbl->oact);
357 if (tbl->oact.sa_handler == SIG_IGN)
360 (void) sigemptyset (&sa.sa_mask);
361 sa.sa_flags = SA_SIGINFO;
362 #if defined(__LCLINT__) /* XXX glibc has union to track handler prototype. */
363 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
365 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
367 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
371 tbl->active = 1; /* XXX just in case */
373 tbl->handler = handler;
375 } else { /* Disable. */
376 if (SUB_REF(tbl) <= 0) {
377 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
379 tbl->active = 0; /* XXX just in case */
380 tbl->handler = (handler != NULL ? handler : rpmsqAction);
390 pid_t rpmsqFork(rpmsq sq)
396 xx = rpmsqInsert(sq, NULL);
399 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
401 xx = rpmsqEnable(SIGCHLD, NULL);
404 xx = pipe(sq->pipes);
406 xx = sighold(SIGCHLD);
409 if (pid < (pid_t) 0) { /* fork failed. */
411 xx = close(sq->pipes[0]);
412 xx = close(sq->pipes[1]);
413 sq->pipes[0] = sq->pipes[1] = -1;
416 } else if (pid == (pid_t) 0) { /* Child. */
419 /* Block to permit parent time to wait. */
421 xx = close(sq->pipes[1]);
422 xx = read(sq->pipes[0], &yy, sizeof(yy));
423 xx = close(sq->pipes[0]);
424 sq->pipes[0] = sq->pipes[1] = -1;
429 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
432 } else { /* Parent. */
438 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
444 xx = sigrelse(SIGCHLD);
449 * Wait for child process to be reaped, and unregister SIGCHLD handler.
450 * @todo Rewrite to use waitpid on helper thread.
451 * @param sq scriptlet queue element
452 * @return 0 on success
454 static int rpmsqWaitUnregister(rpmsq sq)
455 /*@globals fileSystem, internalState @*/
456 /*@modifies sq, fileSystem, internalState @*/
462 /* Protect sq->reaped from handler changes. */
463 ret = sighold(SIGCHLD);
465 /* Initialize the cond var mutex. */
467 ret = pthread_mutex_lock(&sq->mutex);
469 /* Start the child, linux often runs child before parent. */
471 if (sq->pipes[0] >= 0)
472 xx = close(sq->pipes[0]);
473 if (sq->pipes[1] >= 0)
474 xx = close(sq->pipes[1]);
475 sq->pipes[0] = sq->pipes[1] = -1;
478 /* Put a stopwatch on the time spent waiting to measure performance gain. */
479 (void) rpmswEnter(&sq->op, -1);
481 /* Wait for handler to receive SIGCHLD. */
483 while (ret == 0 && sq->reaped != sq->child) {
485 /* Note that sigpause re-enables SIGCHLD. */
486 ret = sigpause(SIGCHLD);
488 xx = sigrelse(SIGCHLD);
489 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
490 xx = sighold(SIGCHLD);
495 /* Accumulate stopwatch time spent waiting, potential performance gain. */
496 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
498 /* Tear down cond var mutex, our child has been reaped. */
500 xx = pthread_mutex_unlock(&sq->mutex);
501 xx = sigrelse(SIGCHLD);
505 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
508 /* Remove processed SIGCHLD item from queue. */
509 xx = rpmsqRemove(sq);
511 /* Disable SIGCHLD handler on refcount == 0. */
512 xx = rpmsqEnable(-SIGCHLD, NULL);
515 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
521 pid_t rpmsqWait(rpmsq sq)
526 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
530 (void) rpmsqWaitUnregister(sq);
535 reaped = waitpid(sq->child, &status, 0);
536 } while (reaped >= 0 && reaped != sq->child);
541 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
547 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
553 void * rpmsqThread(void * (*start) (void * arg), void * arg)
558 ret = pthread_create(&pth, NULL, start, arg);
559 return (ret == 0 ? (void *)pth : NULL);
562 int rpmsqJoin(void * thread)
564 pthread_t pth = (pthread_t) thread;
567 return pthread_join(pth, NULL);
570 int rpmsqThreadEqual(void * thread)
572 pthread_t t1 = (pthread_t) thread;
573 pthread_t t2 = pthread_self();
574 return pthread_equal(t1, t2);
578 * SIGCHLD cancellation handler.
581 sigchld_cancel (void *arg)
582 /*@globals rpmsigTbl, fileSystem, internalState @*/
583 /*@modifies rpmsigTbl, fileSystem, internalState @*/
585 pid_t child = *(pid_t *) arg;
588 (void) kill(child, SIGKILL);
591 result = waitpid(child, NULL, 0);
592 } while (result == (pid_t)-1 && errno == EINTR);
595 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
596 (void) rpmsqEnable(-SIGQUIT, NULL);
597 (void) rpmsqEnable(-SIGINT, NULL);
603 * Execute a command, returning its status.
606 rpmsqExecve (const char ** argv)
607 /*@globals rpmsigTbl @*/
608 /*@modifies rpmsigTbl @*/
614 sigset_t newMask, oldMask;
615 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
617 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
622 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
623 if (rpmsqEnable(SIGINT, NULL) < 0) {
624 SUB_REF (rpmsigTbl_sigchld);
627 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
628 SUB_REF (rpmsigTbl_sigchld);
629 goto out_restore_sigint;
634 (void) sigemptyset (&newMask);
635 (void) sigaddset (&newMask, SIGCHLD);
636 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
638 if (SUB_REF (rpmsigTbl_sigchld) == 0)
639 goto out_restore_sigquit_and_sigint;
643 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
646 if (pid < (pid_t) 0) { /* fork failed. */
648 } else if (pid == (pid_t) 0) { /* Child. */
650 /* Restore the signals. */
651 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
652 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
653 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
655 /* Reset rpmsigTbl lock and refcnt. */
658 (void) execve (argv[0], (char *const *) argv, environ);
660 } else { /* Parent. */
662 result = waitpid(pid, &status, 0);
663 } while (result == (pid_t)-1 && errno == EINTR);
668 CLEANUP_RESET(0, oldtype);
671 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
672 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
673 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
679 out_restore_sigquit_and_sigint:
680 (void) rpmsqEnable(-SIGQUIT, NULL);
682 (void) rpmsqEnable(-SIGINT, NULL);