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);
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) {
352 /*@=compdef =type @*/
353 tbl->active = 1; /* XXX just in case */
355 tbl->handler = handler;
357 } else { /* Disable. */
358 if (SUB_REF(tbl) <= 0) {
359 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
361 tbl->active = 0; /* XXX just in case */
362 tbl->handler = (handler != NULL ? handler : rpmsqAction);
372 pid_t rpmsqFork(rpmsq sq)
378 xx = rpmsqInsert(sq, NULL);
381 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
383 xx = rpmsqEnable(SIGCHLD, NULL);
386 xx = pipe(sq->pipes);
388 xx = sighold(SIGCHLD);
391 if (pid < (pid_t) 0) { /* fork failed. */
393 xx = close(sq->pipes[0]);
394 xx = close(sq->pipes[1]);
395 sq->pipes[0] = sq->pipes[1] = -1;
398 } else if (pid == (pid_t) 0) { /* Child. */
401 /* Block to permit parent to wait. */
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;
411 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
414 } else { /* Parent. */
420 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
426 xx = sigrelse(SIGCHLD);
431 * Wait for child process to be reaped, and unregister SIGCHLD handler.
432 * @param sq scriptlet queue element
433 * @return 0 on success
435 static int rpmsqWaitUnregister(rpmsq sq)
436 /*@globals fileSystem, internalState @*/
437 /*@modifies sq, fileSystem, internalState @*/
443 ret = sighold(SIGCHLD);
445 ret = pthread_mutex_lock(&sq->mutex);
447 /* Start the child. */
449 if (sq->pipes[0] >= 0)
450 xx = close(sq->pipes[0]);
451 if (sq->pipes[1] >= 0)
452 xx = close(sq->pipes[1]);
453 sq->pipes[0] = sq->pipes[1] = -1;
456 (void) rpmswEnter(&sq->op, -1);
461 ret = sigpause(SIGCHLD);
463 xx = sigrelse(SIGCHLD);
464 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
465 xx = sighold(SIGCHLD);
467 } while (ret == 0 && sq->reaped != sq->child);
470 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
473 xx = pthread_mutex_unlock(&sq->mutex);
474 xx = sigrelse(SIGCHLD);
478 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
481 xx = rpmsqRemove(sq);
482 xx = rpmsqEnable(-SIGCHLD, NULL);
485 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
491 pid_t rpmsqWait(rpmsq sq)
496 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
500 (void) rpmsqWaitUnregister(sq);
505 reaped = waitpid(sq->child, &status, 0);
506 } while (reaped >= 0 && reaped != sq->child);
511 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
517 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
523 void * rpmsqThread(void * (*start) (void * arg), void * arg)
528 ret = pthread_create(&pth, NULL, start, arg);
529 return (ret == 0 ? (void *)pth : NULL);
532 int rpmsqJoin(void * thread)
534 pthread_t pth = (pthread_t) thread;
537 return pthread_join(pth, NULL);
540 int rpmsqThreadEqual(void * thread)
542 pthread_t t1 = (pthread_t) thread;
543 pthread_t t2 = pthread_self();
544 return pthread_equal(t1, t2);
548 * SIGCHLD cancellation handler.
551 sigchld_cancel (void *arg)
552 /*@globals rpmsigTbl, fileSystem, internalState @*/
553 /*@modifies rpmsigTbl, fileSystem, internalState @*/
555 pid_t child = *(pid_t *) arg;
558 (void) kill(child, SIGKILL);
561 result = waitpid(child, NULL, 0);
562 } while (result == (pid_t)-1 && errno == EINTR);
565 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
566 (void) rpmsqEnable(-SIGQUIT, NULL);
567 (void) rpmsqEnable(-SIGINT, NULL);
573 * Execute a command, returning its status.
576 rpmsqExecve (const char ** argv)
577 /*@globals rpmsigTbl @*/
578 /*@modifies rpmsigTbl @*/
584 sigset_t newMask, oldMask;
585 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
588 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
589 if (rpmsqEnable(SIGINT, NULL) < 0) {
590 SUB_REF (rpmsigTbl_sigchld);
593 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
594 SUB_REF (rpmsigTbl_sigchld);
595 goto out_restore_sigint;
600 (void) sigemptyset (&newMask);
601 (void) sigaddset (&newMask, SIGCHLD);
602 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
604 if (SUB_REF (rpmsigTbl_sigchld) == 0)
605 goto out_restore_sigquit_and_sigint;
609 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
612 if (pid < (pid_t) 0) { /* fork failed. */
614 } else if (pid == (pid_t) 0) { /* Child. */
616 /* Restore the signals. */
617 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
618 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
619 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
621 /* Reset rpmsigTbl lock and refcnt. */
624 (void) execve (argv[0], (char *const *) argv, environ);
626 } else { /* Parent. */
628 result = waitpid(pid, &status, 0);
629 } while (result == (pid_t)-1 && errno == EINTR);
634 CLEANUP_RESET(0, oldtype);
637 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
638 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
639 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
645 out_restore_sigquit_and_sigint:
646 (void) rpmsqEnable(-SIGQUIT, NULL);
648 (void) rpmsqEnable(-SIGINT, NULL);