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 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
131 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
135 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
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; \
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));
158 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
160 #define ME() ((void *)pthread_self())
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)
172 #define SAME_THREAD(_a, _b) (42)
174 #define ME() (((void *)getpid()))
176 #endif /* HAVE_PTHREAD_H */
182 #define _RPMSQ_DEBUG 0
184 int _rpmsq_debug = _RPMSQ_DEBUG;
187 static struct rpmsqElem rpmsqRock;
191 rpmsq rpmsqQueue = &rpmsqRock;
194 int rpmsqInsert(void * elem, void * prev)
196 rpmsq sq = (rpmsq) elem;
202 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
204 ret = sighold(SIGCHLD);
211 sq->pipes[0] = sq->pipes[1] = -1;
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);
224 int rpmsqRemove(void * elem)
226 rpmsq sq = (rpmsq) elem;
233 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
235 ret = sighold (SIGCHLD);
238 ret = pthread_cond_destroy(&sq->cond);
239 ret = pthread_mutex_destroy(&sq->mutex);
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;
246 #ifdef NOTYET /* rpmpsmWait debugging message needs */
252 ret = sigrelse(SIGCHLD);
259 sigset_t rpmsqCaught;
263 static struct rpmsig_s {
265 void (*handler) (int signum, void * info, void * context);
267 struct sigaction oact;
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])
285 void rpmsqAction(int signum,
286 /*@unused@*/ void * info, /*@unused@*/ void * context)
291 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
292 if (tbl->signum != signum)
295 (void) sigaddset(&rpmsqCaught, signum);
302 pid_t reaped = waitpid(0, &status, WNOHANG);
304 /* XXX errno set to ECHILD/EINVAL/EINTR. */
306 /*@innerbreak@*/ break;
308 /* XXX insque(3)/remque(3) are dequeue, not ring. */
309 for (sq = rpmsqQueue->q_forw;
310 sq != NULL && sq != rpmsqQueue;
313 if (sq->child != reaped)
314 /*@innercontinue@*/ continue;
317 (void) pthread_cond_signal(&sq->cond);
318 /*@innerbreak@*/ break;
321 /*@switchbreak@*/ break;
323 /*@switchbreak@*/ break;
330 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
331 /*@globals rpmsigTbl @*/
332 /*@modifies rpmsigTbl @*/
334 int tblsignum = (signum >= 0 ? signum : -signum);
340 if (rpmsqQueue->id == NULL)
341 rpmsqQueue->id = ME();
342 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
343 if (tblsignum != tbl->signum)
346 if (signum >= 0) { /* Enable. */
347 if (ADD_REF(tbl) <= 0) {
348 (void) sigdelset(&rpmsqCaught, tbl->signum);
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)
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);
360 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
362 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
366 tbl->active = 1; /* XXX just in case */
368 tbl->handler = handler;
370 } else { /* Disable. */
371 if (SUB_REF(tbl) <= 0) {
372 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
374 tbl->active = 0; /* XXX just in case */
375 tbl->handler = (handler != NULL ? handler : rpmsqAction);
385 pid_t rpmsqFork(rpmsq sq)
391 xx = rpmsqInsert(sq, NULL);
394 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
396 xx = rpmsqEnable(SIGCHLD, NULL);
399 xx = pipe(sq->pipes);
401 xx = sighold(SIGCHLD);
404 if (pid < (pid_t) 0) { /* fork failed. */
406 xx = close(sq->pipes[0]);
407 xx = close(sq->pipes[1]);
408 sq->pipes[0] = sq->pipes[1] = -1;
411 } else if (pid == (pid_t) 0) { /* Child. */
414 /* Block to permit parent time to wait. */
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;
424 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
427 } else { /* Parent. */
433 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
439 xx = sigrelse(SIGCHLD);
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
449 static int rpmsqWaitUnregister(rpmsq sq)
450 /*@globals fileSystem, internalState @*/
451 /*@modifies sq, fileSystem, internalState @*/
457 /* Protect sq->reaped from handler changes. */
458 ret = sighold(SIGCHLD);
460 /* Initialize the cond var mutex. */
462 ret = pthread_mutex_lock(&sq->mutex);
464 /* Start the child, linux often runs child before parent. */
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;
473 /* Put a stopwatch on the time spent waiting to measure performance gain. */
474 (void) rpmswEnter(&sq->op, -1);
476 /* Wait for handler to receive SIGCHLD. */
478 while (ret == 0 && sq->reaped != sq->child) {
480 /* Note that sigpause re-enables SIGCHLD. */
481 ret = sigpause(SIGCHLD);
483 xx = sigrelse(SIGCHLD);
484 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
485 xx = sighold(SIGCHLD);
490 /* Accumulate stopwatch time spent waiting, potential performance gain. */
491 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
493 /* Tear down cond var mutex, our child has been reaped. */
495 xx = pthread_mutex_unlock(&sq->mutex);
496 xx = sigrelse(SIGCHLD);
500 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
503 /* Remove processed SIGCHLD item from queue. */
504 xx = rpmsqRemove(sq);
506 /* Disable SIGCHLD handler on refcount == 0. */
507 xx = rpmsqEnable(-SIGCHLD, NULL);
510 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
516 pid_t rpmsqWait(rpmsq sq)
521 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
525 (void) rpmsqWaitUnregister(sq);
530 reaped = waitpid(sq->child, &status, 0);
531 } while (reaped >= 0 && reaped != sq->child);
536 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
542 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
548 void * rpmsqThread(void * (*start) (void * arg), void * arg)
553 ret = pthread_create(&pth, NULL, start, arg);
554 return (ret == 0 ? (void *)pth : NULL);
557 int rpmsqJoin(void * thread)
559 pthread_t pth = (pthread_t) thread;
562 return pthread_join(pth, NULL);
565 int rpmsqThreadEqual(void * thread)
567 pthread_t t1 = (pthread_t) thread;
568 pthread_t t2 = pthread_self();
569 return pthread_equal(t1, t2);
573 * SIGCHLD cancellation handler.
576 sigchld_cancel (void *arg)
577 /*@globals rpmsigTbl, fileSystem, internalState @*/
578 /*@modifies rpmsigTbl, fileSystem, internalState @*/
580 pid_t child = *(pid_t *) arg;
583 (void) kill(child, SIGKILL);
586 result = waitpid(child, NULL, 0);
587 } while (result == (pid_t)-1 && errno == EINTR);
590 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
591 (void) rpmsqEnable(-SIGQUIT, NULL);
592 (void) rpmsqEnable(-SIGINT, NULL);
598 * Execute a command, returning its status.
601 rpmsqExecve (const char ** argv)
602 /*@globals rpmsigTbl @*/
603 /*@modifies rpmsigTbl @*/
609 sigset_t newMask, oldMask;
610 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
612 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
617 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
618 if (rpmsqEnable(SIGINT, NULL) < 0) {
619 SUB_REF (rpmsigTbl_sigchld);
622 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
623 SUB_REF (rpmsigTbl_sigchld);
624 goto out_restore_sigint;
629 (void) sigemptyset (&newMask);
630 (void) sigaddset (&newMask, SIGCHLD);
631 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
633 if (SUB_REF (rpmsigTbl_sigchld) == 0)
634 goto out_restore_sigquit_and_sigint;
638 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
641 if (pid < (pid_t) 0) { /* fork failed. */
643 } else if (pid == (pid_t) 0) { /* Child. */
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);
650 /* Reset rpmsigTbl lock and refcnt. */
653 (void) execve (argv[0], (char *const *) argv, environ);
655 } else { /* Parent. */
657 result = waitpid(pid, &status, 0);
658 } while (result == (pid_t)-1 && errno == EINTR);
663 CLEANUP_RESET(0, oldtype);
666 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
667 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
668 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
674 out_restore_sigquit_and_sigint:
675 (void) rpmsqEnable(-SIGQUIT, NULL);
677 (void) rpmsqEnable(-SIGINT, NULL);