8 #include <sys/signal.h>
12 #if defined(HAVE_PTHREAD_H)
16 /* XXX suggested in bugzilla #159024 */
17 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
18 #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
21 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
23 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
27 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
31 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
32 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
34 { pthread_mutexattr_t attr; \
35 (void) pthread_mutexattr_init(&attr); \
36 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
37 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
38 (void) pthread_mutexattr_destroy(&attr); \
39 rpmsigTbl_sigchld->active = 0; \
41 #define ADD_REF(__tbl) (__tbl)->active++
42 #define SUB_REF(__tbl) --(__tbl)->active
43 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
44 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
45 pthread_cleanup_push((__handler), (__arg));
46 #define CLEANUP_RESET(__execute, __oldtype) \
47 pthread_cleanup_pop(__execute); \
48 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
50 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
52 #define ME() ((void *)pthread_self())
59 #define ADD_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
60 #define SUB_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
61 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
62 #define CLEANUP_RESET(__execute, __oldtype)
64 #define SAME_THREAD(_a, _b) (42)
66 #define ME() (((void *)getpid()))
68 #endif /* HAVE_PTHREAD_H */
74 #define _RPMSQ_DEBUG 0
76 int _rpmsq_debug = _RPMSQ_DEBUG;
79 static struct rpmsqElem rpmsqRock;
83 rpmsq rpmsqQueue = &rpmsqRock;
86 int rpmsqInsert(void * elem, void * prev)
88 rpmsq sq = (rpmsq) elem;
94 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
96 ret = sighold(SIGCHLD);
103 sq->pipes[0] = sq->pipes[1] = -1;
107 ret = pthread_mutex_init(&sq->mutex, NULL);
108 insque(elem, (prev != NULL ? prev : rpmsqQueue));
109 ret = sigrelse(SIGCHLD);
115 int rpmsqRemove(void * elem)
117 rpmsq sq = (rpmsq) elem;
124 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
126 ret = sighold (SIGCHLD);
130 /* Unlock the mutex and then destroy it */
131 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
132 ret = pthread_mutex_destroy(&sq->mutex);
136 if (sq->pipes[1]) ret = close(sq->pipes[1]);
137 if (sq->pipes[0]) ret = close(sq->pipes[0]);
138 sq->pipes[0] = sq->pipes[1] = -1;
140 #ifdef NOTYET /* rpmpsmWait debugging message needs */
146 ret = sigrelse(SIGCHLD);
153 sigset_t rpmsqCaught;
157 static struct rpmsig_s {
159 void (*handler) (int signum, void * info, void * context);
161 struct sigaction oact;
163 { SIGINT, rpmsqAction },
164 #define rpmsigTbl_sigint (&rpmsigTbl[0])
165 { SIGQUIT, rpmsqAction },
166 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
167 { SIGCHLD, rpmsqAction },
168 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
169 { SIGHUP, rpmsqAction },
170 #define rpmsigTbl_sighup (&rpmsigTbl[3])
171 { SIGTERM, rpmsqAction },
172 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
173 { SIGPIPE, rpmsqAction },
174 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
179 void rpmsqAction(int signum,
180 /*@unused@*/ void * info, /*@unused@*/ void * context)
185 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
186 if (tbl->signum != signum)
189 (void) sigaddset(&rpmsqCaught, signum);
196 pid_t reaped = waitpid(0, &status, WNOHANG);
198 /* XXX errno set to ECHILD/EINVAL/EINTR. */
200 /*@innerbreak@*/ break;
202 /* XXX insque(3)/remque(3) are dequeue, not ring. */
203 for (sq = rpmsqQueue->q_forw;
204 sq != NULL && sq != rpmsqQueue;
209 if (sq->child != reaped)
210 /*@innercontinue@*/ continue;
214 /* Unlock the mutex. The waiter will then be able to
217 * XXX: jbj, wtd, if this fails?
219 ret = pthread_mutex_unlock(&sq->mutex);
221 /*@innerbreak@*/ break;
224 /*@switchbreak@*/ break;
226 /*@switchbreak@*/ break;
233 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
234 /*@globals rpmsigTbl @*/
235 /*@modifies rpmsigTbl @*/
237 int tblsignum = (signum >= 0 ? signum : -signum);
243 if (rpmsqQueue->id == NULL)
244 rpmsqQueue->id = ME();
245 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
246 if (tblsignum != tbl->signum)
249 if (signum >= 0) { /* Enable. */
250 if (ADD_REF(tbl) <= 0) {
251 (void) sigdelset(&rpmsqCaught, tbl->signum);
253 /* XXX Don't set a signal handler if already SIG_IGN */
254 (void) sigaction(tbl->signum, NULL, &tbl->oact);
255 if (tbl->oact.sa_handler == SIG_IGN)
258 (void) sigemptyset (&sa.sa_mask);
259 sa.sa_flags = SA_SIGINFO;
260 sa.sa_sigaction = (void*)(handler != NULL ? handler : tbl->handler);
261 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
265 tbl->active = 1; /* XXX just in case */
267 tbl->handler = handler;
269 } else { /* Disable. */
270 if (SUB_REF(tbl) <= 0) {
271 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
273 tbl->active = 0; /* XXX just in case */
274 tbl->handler = (handler != NULL ? handler : rpmsqAction);
284 pid_t rpmsqFork(rpmsq sq)
288 int nothreads = 0; /* XXX: Shouldn't this be a global? */
291 xx = rpmsqInsert(sq, NULL);
294 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
296 xx = rpmsqEnable(SIGCHLD, NULL);
299 xx = pipe(sq->pipes);
301 xx = sighold(SIGCHLD);
304 * Initialize the cond var mutex. We have to aquire the lock we
305 * use for the condition before we fork. Otherwise it is possible for
306 * the child to exit, we get sigchild and the sig handler to send
307 * the condition signal before we are waiting on the condition.
310 if(pthread_mutex_lock(&sq->mutex)) {
311 /* Yack we did not get the lock, lets just give up */
313 xx = close(sq->pipes[0]);
314 xx = close(sq->pipes[1]);
315 sq->pipes[0] = sq->pipes[1] = -1;
322 if (pid < (pid_t) 0) { /* fork failed. */
323 sq->child = (pid_t)-1;
325 xx = close(sq->pipes[0]);
326 xx = close(sq->pipes[1]);
327 sq->pipes[0] = sq->pipes[1] = -1;
330 } else if (pid == (pid_t) 0) { /* Child. */
333 /* Block to permit parent time to wait. */
335 xx = close(sq->pipes[1]);
336 xx = read(sq->pipes[0], &yy, sizeof(yy));
337 xx = close(sq->pipes[0]);
338 sq->pipes[0] = sq->pipes[1] = -1;
343 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
346 } else { /* Parent. */
352 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
358 xx = sigrelse(SIGCHLD);
363 * Wait for child process to be reaped, and unregister SIGCHLD handler.
364 * @todo Rewrite to use waitpid on helper thread.
365 * @param sq scriptlet queue element
366 * @return 0 on success
368 static int rpmsqWaitUnregister(rpmsq sq)
369 /*@globals fileSystem, internalState @*/
370 /*@modifies sq, fileSystem, internalState @*/
376 /* Protect sq->reaped from handler changes. */
377 ret = sighold(SIGCHLD);
379 /* Start the child, linux often runs child before parent. */
381 if (sq->pipes[0] >= 0)
382 xx = close(sq->pipes[0]);
383 if (sq->pipes[1] >= 0)
384 xx = close(sq->pipes[1]);
385 sq->pipes[0] = sq->pipes[1] = -1;
388 /* Put a stopwatch on the time spent waiting to measure performance gain. */
389 (void) rpmswEnter(&sq->op, -1);
391 /* Wait for handler to receive SIGCHLD. */
393 while (ret == 0 && sq->reaped != sq->child) {
395 /* Note that sigpause re-enables SIGCHLD. */
396 ret = sigpause(SIGCHLD);
398 xx = sigrelse(SIGCHLD);
401 * We start before the fork with this mutex locked;
402 * The only one that unlocks this the signal handler.
403 * So if we get the lock the child has been reaped.
405 ret = pthread_mutex_lock(&sq->mutex);
406 xx = sighold(SIGCHLD);
411 /* Accumulate stopwatch time spent waiting, potential performance gain. */
412 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
414 xx = sigrelse(SIGCHLD);
418 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
421 /* Remove processed SIGCHLD item from queue. */
422 xx = rpmsqRemove(sq);
424 /* Disable SIGCHLD handler on refcount == 0. */
425 xx = rpmsqEnable(-SIGCHLD, NULL);
428 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
434 pid_t rpmsqWait(rpmsq sq)
439 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
443 (void) rpmsqWaitUnregister(sq);
448 reaped = waitpid(sq->child, &status, 0);
449 } while (reaped >= 0 && reaped != sq->child);
454 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
460 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
466 void * rpmsqThread(void * (*start) (void * arg), void * arg)
471 ret = pthread_create(&pth, NULL, start, arg);
472 return (ret == 0 ? (void *)pth : NULL);
475 int rpmsqJoin(void * thread)
477 pthread_t pth = (pthread_t) thread;
480 return pthread_join(pth, NULL);
483 int rpmsqThreadEqual(void * thread)
485 pthread_t t1 = (pthread_t) thread;
486 pthread_t t2 = pthread_self();
487 return pthread_equal(t1, t2);
491 * SIGCHLD cancellation handler.
494 sigchld_cancel (void *arg)
495 /*@globals rpmsigTbl, fileSystem, internalState @*/
496 /*@modifies rpmsigTbl, fileSystem, internalState @*/
498 pid_t child = *(pid_t *) arg;
501 (void) kill(child, SIGKILL);
504 result = waitpid(child, NULL, 0);
505 } while (result == (pid_t)-1 && errno == EINTR);
508 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
509 (void) rpmsqEnable(-SIGQUIT, NULL);
510 (void) rpmsqEnable(-SIGINT, NULL);
516 * Execute a command, returning its status.
519 rpmsqExecve (const char ** argv)
520 /*@globals rpmsigTbl @*/
521 /*@modifies rpmsigTbl @*/
527 sigset_t newMask, oldMask;
528 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
530 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
535 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
536 if (rpmsqEnable(SIGINT, NULL) < 0) {
537 SUB_REF (rpmsigTbl_sigchld);
540 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
541 SUB_REF (rpmsigTbl_sigchld);
542 goto out_restore_sigint;
547 (void) sigemptyset (&newMask);
548 (void) sigaddset (&newMask, SIGCHLD);
549 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
551 if (SUB_REF (rpmsigTbl_sigchld) == 0)
552 goto out_restore_sigquit_and_sigint;
556 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
559 if (pid < (pid_t) 0) { /* fork failed. */
561 } else if (pid == (pid_t) 0) { /* Child. */
563 /* Restore the signals. */
564 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
565 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
566 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
568 /* Reset rpmsigTbl lock and refcnt. */
571 (void) execve (argv[0], (char *const *) argv, environ);
573 } else { /* Parent. */
575 result = waitpid(pid, &status, 0);
576 } while (result == (pid_t)-1 && errno == EINTR);
581 CLEANUP_RESET(0, oldtype);
584 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
585 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
586 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
592 out_restore_sigquit_and_sigint:
593 (void) rpmsqEnable(-SIGQUIT, NULL);
595 (void) rpmsqEnable(-SIGINT, NULL);