7 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
11 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
12 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
14 { pthread_mutexattr_t attr; \
15 pthread_mutexattr_init(&attr); \
16 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
17 pthread_mutex_init (&rpmsigTbl_lock, &attr); \
18 pthread_mutexattr_destroy(&attr); \
19 rpmsigTbl_sigchld->active = 0; \
21 #define ADD_REF(__tbl) (__tbl)->active++
22 #define SUB_REF(__tbl) --(__tbl)->active
23 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
24 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr)); \
25 pthread_cleanup_push((__handler), (__arg));
26 #define CLEANUP_RESET(__execute, __oldtype) \
27 pthread_cleanup_pop(__execute); \
28 pthread_setcanceltype ((__oldtype), &(__oldtype));
30 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
32 #define ME() ((void *)pthread_self())
39 #define ADD_REF(__tbl)
40 #define SUB_REF(__tbl)
41 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
42 #define CLEANUP_RESET(__execute, __oldtype)
44 #define SAME_THREAD(_a, _b) (42)
46 #define ME() (((void *))getpid())
48 #endif /* HAVE_PTHREAD_H */
54 #define _RPMSQ_DEBUG 0
56 int _rpmsq_debug = _RPMSQ_DEBUG;
59 static struct rpmsqElem rpmsqRock;
61 rpmsq rpmsqQueue = &rpmsqRock;
63 int rpmsqInsert(void * elem, void * prev)
65 rpmsq sq = (rpmsq) elem;
72 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
75 ret = sighold(SIGCHLD);
81 sq->pipes[0] = sq->pipes[1] = -1;
84 ret = pthread_mutex_init(&sq->mutex, NULL);
85 ret = pthread_cond_init(&sq->cond, NULL);
86 insque(elem, (prev ? prev : rpmsqQueue));
87 ret = sigrelse(SIGCHLD);
93 int rpmsqRemove(void * elem)
95 rpmsq sq = (rpmsq) elem;
103 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
106 ret = sighold (SIGCHLD);
109 ret = pthread_cond_destroy(&sq->cond);
110 ret = pthread_mutex_destroy(&sq->mutex);
112 if (sq->pipes[1]) close(sq->pipes[1]);
113 if (sq->pipes[0]) close(sq->pipes[0]);
114 sq->pipes[0] = sq->pipes[1] = -1;
119 ret = sigrelse(SIGCHLD);
126 sigset_t rpmsqCaught;
129 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
133 static struct rpmsig_s {
135 void (*handler) (int signum, siginfo_t * info, void * context);
137 struct sigaction oact;
139 { SIGINT, rpmsqAction },
140 #define rpmsigTbl_sigint (&rpmsigTbl[0])
141 { SIGQUIT, rpmsqAction },
142 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
143 { SIGCHLD, rpmsqAction },
144 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
145 { SIGHUP, rpmsqAction },
146 #define rpmsigTbl_sighup (&rpmsigTbl[3])
147 { SIGTERM, rpmsqAction },
148 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
149 { SIGPIPE, rpmsqAction },
150 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
156 void rpmsqAction(int signum, siginfo_t * info, void * context)
161 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
162 if (tbl->signum != signum)
165 (void) sigaddset(&rpmsqCaught, signum);
172 pid_t reaped = waitpid(0, &status, WNOHANG);
174 /* XXX errno set to ECHILD/EINVAL/EINTR. */
176 /*@innerbreak@*/ break;
178 /* XXX insque(3)/remque(3) are dequeue, not ring. */
179 for (sq = rpmsqQueue->q_forw;
180 sq != NULL && sq != rpmsqQueue;
183 if (sq->child != reaped)
184 /*@innercontinue@*/ continue;
187 (void) pthread_cond_signal(&sq->cond);
188 /*@innerbreak@*/ break;
191 /*@switchbreak@*/ break;
193 /*@switchbreak@*/ break;
201 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
203 int tblsignum = (signum >= 0 ? signum : -signum);
209 if (rpmsqQueue->id == NULL)
210 rpmsqQueue->id = ME();
211 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
212 if (tblsignum != tbl->signum)
215 if (signum >= 0) { /* Enable. */
216 if (ADD_REF(tbl) <= 0) {
217 (void) sigdelset(&rpmsqCaught, tbl->signum);
218 sigemptyset (&sa.sa_mask);
219 sa.sa_flags = SA_SIGINFO;
220 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
221 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
225 tbl->active = 1; /* XXX just in case */
227 tbl->handler = handler;
229 } else { /* Disable. */
230 if (SUB_REF(tbl) <= 0) {
231 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
233 tbl->active = 0; /* XXX just in case */
234 tbl->handler = (handler != NULL ? handler : rpmsqAction);
244 pid_t rpmsqFork(rpmsq sq)
250 xx = rpmsqInsert(sq, NULL);
254 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
257 xx = rpmsqEnable(SIGCHLD, NULL);
260 xx = pipe(sq->pipes);
262 xx = sighold(SIGCHLD);
265 if (pid < (pid_t) 0) { /* fork failed. */
266 xx = close(sq->pipes[0]);
267 xx = close(sq->pipes[1]);
268 sq->pipes[0] = sq->pipes[1] = -1;
270 } else if (pid == (pid_t) 0) { /* Child. */
273 /* Block to permit parent to wait. */
274 xx = close(sq->pipes[1]);
275 xx = read(sq->pipes[0], &yy, sizeof(yy));
276 xx = close(sq->pipes[0]);
277 sq->pipes[0] = sq->pipes[1] = -1;
282 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
286 } else { /* Parent. */
293 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
298 xx = close(sq->pipes[0]);
299 xx = close(sq->pipes[1]);
300 sq->pipes[0] = sq->pipes[1] = -1;
305 xx = sigrelse(SIGCHLD);
310 * Wait for child process to be reaped, and unregister SIGCHLD handler.
311 * @param sq scriptlet queue element
312 * @return 0 on success
314 static int rpmsqWaitUnregister(rpmsq sq)
315 /*@globals fileSystem, internalState @*/
316 /*@modifies fileSystem, internalState @*/
322 if (same_thread) ret = sighold(SIGCHLD);
325 while (ret == 0 && sq->reaped != sq->child) {
327 ret = sigpause(SIGCHLD);
329 ret = pthread_mutex_lock(&sq->mutex);
330 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
331 xx = pthread_mutex_unlock(&sq->mutex);
336 if (same_thread) xx = sigrelse(SIGCHLD);
341 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
345 xx = rpmsqRemove(sq);
346 xx = rpmsqEnable(-SIGCHLD, NULL);
350 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
357 pid_t rpmsqWait(rpmsq sq)
363 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
368 (void) rpmsqWaitUnregister(sq);
373 reaped = waitpid(sq->child, &status, 0);
374 } while (reaped >= 0 && reaped != sq->child);
380 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
388 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
395 int rpmsqThread(void * (*start) (void * arg), void * arg)
400 ret = pthread_create(&pth, NULL, start, arg);
403 fprintf(stderr, " Thread(%p): %p\n", ME(), pth);
405 ret = pthread_join(pth, NULL);
411 * SIGCHLD cancellation handler.
414 sigchld_cancel (void *arg)
416 pid_t child = *(pid_t *) arg;
419 (void) kill(child, SIGKILL);
422 result = waitpid(child, NULL, 0);
423 } while (result == (pid_t)-1 && errno == EINTR);
426 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
427 (void) rpmsqEnable(-SIGQUIT, NULL);
428 (void) rpmsqEnable(-SIGINT, NULL);
434 * Execute a command, returning its status.
437 rpmsqExecve (const char ** argv)
443 sigset_t newMask, oldMask;
444 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
447 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
448 if (rpmsqEnable(SIGINT, NULL) < 0) {
449 SUB_REF (rpmsigTbl_sigchld);
452 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
453 SUB_REF (rpmsigTbl_sigchld);
454 goto out_restore_sigint;
459 sigemptyset (&newMask);
460 sigaddset (&newMask, SIGCHLD);
461 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
463 if (SUB_REF (rpmsigTbl_sigchld) == 0)
464 goto out_restore_sigquit_and_sigint;
468 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
471 if (pid < (pid_t) 0) { /* fork failed. */
473 } else if (pid == (pid_t) 0) { /* Child. */
475 /* Restore the signals. */
476 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
477 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
478 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
480 /* Reset rpmsigTbl lock and refcnt. */
483 (void) execve (argv[0], (char *const *) argv, environ);
485 } else { /* Parent. */
487 result = waitpid(pid, &status, 0);
488 } while (result == (pid_t)-1 && errno == EINTR);
493 CLEANUP_RESET(0, oldtype);
496 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
497 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
498 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
504 out_restore_sigquit_and_sigint:
505 (void) rpmsqEnable(-SIGQUIT, NULL);
507 (void) rpmsqEnable(-SIGINT, NULL);