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)
131 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
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; \
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));
153 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
155 #define ME() ((void *)pthread_self())
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)
167 #define SAME_THREAD(_a, _b) (42)
169 #define ME() (((void *)getpid()))
171 #endif /* HAVE_PTHREAD_H */
177 #define _RPMSQ_DEBUG 0
179 int _rpmsq_debug = _RPMSQ_DEBUG;
182 static struct rpmsqElem rpmsqRock;
186 rpmsq rpmsqQueue = &rpmsqRock;
189 int rpmsqInsert(void * elem, void * prev)
191 rpmsq sq = (rpmsq) elem;
197 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
199 ret = sighold(SIGCHLD);
206 sq->pipes[0] = sq->pipes[1] = -1;
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);
219 int rpmsqRemove(void * elem)
221 rpmsq sq = (rpmsq) elem;
228 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
230 ret = sighold (SIGCHLD);
233 ret = pthread_cond_destroy(&sq->cond);
234 ret = pthread_mutex_destroy(&sq->mutex);
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;
241 #ifdef NOTYET /* rpmpsmWait debugging message needs */
247 ret = sigrelse(SIGCHLD);
254 sigset_t rpmsqCaught;
258 static struct rpmsig_s {
260 void (*handler) (int signum, void * info, void * context);
262 struct sigaction oact;
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])
280 void rpmsqAction(int signum,
281 /*@unused@*/ void * info, /*@unused@*/ void * context)
286 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
287 if (tbl->signum != signum)
290 (void) sigaddset(&rpmsqCaught, signum);
297 pid_t reaped = waitpid(0, &status, WNOHANG);
299 /* XXX errno set to ECHILD/EINVAL/EINTR. */
301 /*@innerbreak@*/ break;
303 /* XXX insque(3)/remque(3) are dequeue, not ring. */
304 for (sq = rpmsqQueue->q_forw;
305 sq != NULL && sq != rpmsqQueue;
308 if (sq->child != reaped)
309 /*@innercontinue@*/ continue;
312 (void) pthread_cond_signal(&sq->cond);
313 /*@innerbreak@*/ break;
316 /*@switchbreak@*/ break;
318 /*@switchbreak@*/ break;
325 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
326 /*@globals rpmsigTbl @*/
327 /*@modifies rpmsigTbl @*/
329 int tblsignum = (signum >= 0 ? signum : -signum);
335 if (rpmsqQueue->id == NULL)
336 rpmsqQueue->id = ME();
337 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
338 if (tblsignum != tbl->signum)
341 if (signum >= 0) { /* Enable. */
342 if (ADD_REF(tbl) <= 0) {
343 (void) sigdelset(&rpmsqCaught, tbl->signum);
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)
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);
355 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
357 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
361 tbl->active = 1; /* XXX just in case */
363 tbl->handler = handler;
365 } else { /* Disable. */
366 if (SUB_REF(tbl) <= 0) {
367 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
369 tbl->active = 0; /* XXX just in case */
370 tbl->handler = (handler != NULL ? handler : rpmsqAction);
380 pid_t rpmsqFork(rpmsq sq)
386 xx = rpmsqInsert(sq, NULL);
389 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
391 xx = rpmsqEnable(SIGCHLD, NULL);
394 xx = pipe(sq->pipes);
396 xx = sighold(SIGCHLD);
399 if (pid < (pid_t) 0) { /* fork failed. */
401 xx = close(sq->pipes[0]);
402 xx = close(sq->pipes[1]);
403 sq->pipes[0] = sq->pipes[1] = -1;
406 } else if (pid == (pid_t) 0) { /* Child. */
409 /* Block to permit parent time to wait. */
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;
419 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
422 } else { /* Parent. */
428 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
434 xx = sigrelse(SIGCHLD);
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
444 static int rpmsqWaitUnregister(rpmsq sq)
445 /*@globals fileSystem, internalState @*/
446 /*@modifies sq, fileSystem, internalState @*/
452 /* Protect sq->reaped from handler changes. */
453 ret = sighold(SIGCHLD);
455 /* Initialize the cond var mutex. */
457 ret = pthread_mutex_lock(&sq->mutex);
459 /* Start the child, linux often runs child before parent. */
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;
468 /* Put a stopwatch on the time spent waiting to measure performance gain. */
469 (void) rpmswEnter(&sq->op, -1);
471 /* Wait for handler to receive SIGCHLD. */
473 while (ret == 0 && sq->reaped != sq->child) {
475 /* Note that sigpause re-enables SIGCHLD. */
476 ret = sigpause(SIGCHLD);
478 xx = sigrelse(SIGCHLD);
479 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
480 xx = sighold(SIGCHLD);
485 /* Accumulate stopwatch time spent waiting, potential performance gain. */
486 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
488 /* Tear down cond var mutex, our child has been reaped. */
490 xx = pthread_mutex_unlock(&sq->mutex);
491 xx = sigrelse(SIGCHLD);
495 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
498 /* Remove processed SIGCHLD item from queue. */
499 xx = rpmsqRemove(sq);
501 /* Disable SIGCHLD handler on refcount == 0. */
502 xx = rpmsqEnable(-SIGCHLD, NULL);
505 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
511 pid_t rpmsqWait(rpmsq sq)
516 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
520 (void) rpmsqWaitUnregister(sq);
525 reaped = waitpid(sq->child, &status, 0);
526 } while (reaped >= 0 && reaped != sq->child);
531 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
537 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
543 void * rpmsqThread(void * (*start) (void * arg), void * arg)
548 ret = pthread_create(&pth, NULL, start, arg);
549 return (ret == 0 ? (void *)pth : NULL);
552 int rpmsqJoin(void * thread)
554 pthread_t pth = (pthread_t) thread;
557 return pthread_join(pth, NULL);
560 int rpmsqThreadEqual(void * thread)
562 pthread_t t1 = (pthread_t) thread;
563 pthread_t t2 = pthread_self();
564 return pthread_equal(t1, t2);
568 * SIGCHLD cancellation handler.
571 sigchld_cancel (void *arg)
572 /*@globals rpmsigTbl, fileSystem, internalState @*/
573 /*@modifies rpmsigTbl, fileSystem, internalState @*/
575 pid_t child = *(pid_t *) arg;
578 (void) kill(child, SIGKILL);
581 result = waitpid(child, NULL, 0);
582 } while (result == (pid_t)-1 && errno == EINTR);
585 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
586 (void) rpmsqEnable(-SIGQUIT, NULL);
587 (void) rpmsqEnable(-SIGINT, NULL);
593 * Execute a command, returning its status.
596 rpmsqExecve (const char ** argv)
597 /*@globals rpmsigTbl @*/
598 /*@modifies rpmsigTbl @*/
604 sigset_t newMask, oldMask;
605 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
608 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
609 if (rpmsqEnable(SIGINT, NULL) < 0) {
610 SUB_REF (rpmsigTbl_sigchld);
613 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
614 SUB_REF (rpmsigTbl_sigchld);
615 goto out_restore_sigint;
620 (void) sigemptyset (&newMask);
621 (void) sigaddset (&newMask, SIGCHLD);
622 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
624 if (SUB_REF (rpmsigTbl_sigchld) == 0)
625 goto out_restore_sigquit_and_sigint;
629 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
632 if (pid < (pid_t) 0) { /* fork failed. */
634 } else if (pid == (pid_t) 0) { /* Child. */
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);
641 /* Reset rpmsigTbl lock and refcnt. */
644 (void) execve (argv[0], (char *const *) argv, environ);
646 } else { /* Parent. */
648 result = waitpid(pid, &status, 0);
649 } while (result == (pid_t)-1 && errno == EINTR);
654 CLEANUP_RESET(0, oldtype);
657 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
658 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
659 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
665 out_restore_sigquit_and_sigint:
666 (void) rpmsqEnable(-SIGQUIT, NULL);
668 (void) rpmsqEnable(-SIGINT, NULL);