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
22 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
24 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
27 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
28 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
30 { pthread_mutexattr_t attr; \
31 (void) pthread_mutexattr_init(&attr); \
32 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
33 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
34 (void) pthread_mutexattr_destroy(&attr); \
35 rpmsigTbl_sigchld->active = 0; \
37 #define ADD_REF(__tbl) (__tbl)->active++
38 #define SUB_REF(__tbl) --(__tbl)->active
39 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
40 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
41 pthread_cleanup_push((__handler), (__arg));
42 #define CLEANUP_RESET(__execute, __oldtype) \
43 pthread_cleanup_pop(__execute); \
44 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
46 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
48 #define ME() ((void *)pthread_self())
55 #define ADD_REF(__tbl) (0)
56 #define SUB_REF(__tbl) (0)
57 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
58 #define CLEANUP_RESET(__execute, __oldtype)
60 #define SAME_THREAD(_a, _b) (42)
62 #define ME() (((void *)getpid()))
64 #endif /* HAVE_PTHREAD_H */
70 #define _RPMSQ_DEBUG 0
71 int _rpmsq_debug = _RPMSQ_DEBUG;
73 static struct rpmsqElem rpmsqRock;
75 rpmsq rpmsqQueue = &rpmsqRock;
77 int rpmsqInsert(void * elem, void * prev)
79 rpmsq sq = (rpmsq) elem;
85 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
87 ret = sighold(SIGCHLD);
93 sq->pipes[0] = sq->pipes[1] = -1;
96 ret = pthread_mutex_init(&sq->mutex, NULL);
97 insque(elem, (prev != NULL ? prev : rpmsqQueue));
98 ret = sigrelse(SIGCHLD);
104 int rpmsqRemove(void * elem)
106 rpmsq sq = (rpmsq) elem;
113 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
115 ret = sighold (SIGCHLD);
119 /* Unlock the mutex and then destroy it */
120 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
121 ret = pthread_mutex_destroy(&sq->mutex);
124 if (sq->pipes[1]) ret = close(sq->pipes[1]);
125 if (sq->pipes[0]) ret = close(sq->pipes[0]);
126 sq->pipes[0] = sq->pipes[1] = -1;
127 #ifdef NOTYET /* rpmpsmWait debugging message needs */
133 ret = sigrelse(SIGCHLD);
139 sigset_t rpmsqCaught;
141 static struct rpmsig_s {
143 void (*handler) (int signum, void * info, void * context);
145 struct sigaction oact;
147 { SIGINT, rpmsqAction },
148 #define rpmsigTbl_sigint (&rpmsigTbl[0])
149 { SIGQUIT, rpmsqAction },
150 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
151 { SIGCHLD, rpmsqAction },
152 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
153 { SIGHUP, rpmsqAction },
154 #define rpmsigTbl_sighup (&rpmsigTbl[3])
155 { SIGTERM, rpmsqAction },
156 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
157 { SIGPIPE, rpmsqAction },
158 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
162 void rpmsqAction(int signum,
163 void * info, void * context)
168 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
169 if (tbl->signum != signum)
172 (void) sigaddset(&rpmsqCaught, signum);
179 pid_t reaped = waitpid(0, &status, WNOHANG);
181 /* XXX errno set to ECHILD/EINVAL/EINTR. */
185 /* XXX insque(3)/remque(3) are dequeue, not ring. */
186 for (sq = rpmsqQueue->q_forw;
187 sq != NULL && sq != rpmsqQueue;
192 if (sq->child != reaped)
197 /* Unlock the mutex. The waiter will then be able to
200 * XXX: jbj, wtd, if this fails?
202 ret = pthread_mutex_unlock(&sq->mutex);
216 int rpmsqEnable(int signum, rpmsqAction_t handler)
218 int tblsignum = (signum >= 0 ? signum : -signum);
224 if (rpmsqQueue->id == NULL)
225 rpmsqQueue->id = ME();
226 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
227 if (tblsignum != tbl->signum)
230 if (signum >= 0) { /* Enable. */
231 if (ADD_REF(tbl) <= 0) {
232 (void) sigdelset(&rpmsqCaught, tbl->signum);
234 /* XXX Don't set a signal handler if already SIG_IGN */
235 (void) sigaction(tbl->signum, NULL, &tbl->oact);
236 if (tbl->oact.sa_handler == SIG_IGN)
239 (void) sigemptyset (&sa.sa_mask);
240 sa.sa_flags = SA_SIGINFO;
241 sa.sa_sigaction = (void*)(handler != NULL ? handler : tbl->handler);
242 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
246 tbl->active = 1; /* XXX just in case */
248 tbl->handler = handler;
250 } else { /* Disable. */
251 if (SUB_REF(tbl) <= 0) {
252 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
254 tbl->active = 0; /* XXX just in case */
255 tbl->handler = (handler != NULL ? handler : rpmsqAction);
265 pid_t rpmsqFork(rpmsq sq)
269 int nothreads = 0; /* XXX: Shouldn't this be a global? */
272 xx = rpmsqInsert(sq, NULL);
275 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
277 xx = rpmsqEnable(SIGCHLD, NULL);
280 xx = pipe(sq->pipes);
282 xx = sighold(SIGCHLD);
285 * Initialize the cond var mutex. We have to aquire the lock we
286 * use for the condition before we fork. Otherwise it is possible for
287 * the child to exit, we get sigchild and the sig handler to send
288 * the condition signal before we are waiting on the condition.
291 if(pthread_mutex_lock(&sq->mutex)) {
292 /* Yack we did not get the lock, lets just give up */
293 xx = close(sq->pipes[0]);
294 xx = close(sq->pipes[1]);
295 sq->pipes[0] = sq->pipes[1] = -1;
301 if (pid < (pid_t) 0) { /* fork failed. */
302 sq->child = (pid_t)-1;
303 xx = close(sq->pipes[0]);
304 xx = close(sq->pipes[1]);
305 sq->pipes[0] = sq->pipes[1] = -1;
307 } else if (pid == (pid_t) 0) { /* Child. */
310 /* Block to permit parent time to wait. */
311 xx = close(sq->pipes[1]);
312 xx = read(sq->pipes[0], &yy, sizeof(yy));
313 xx = close(sq->pipes[0]);
314 sq->pipes[0] = sq->pipes[1] = -1;
318 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
321 } else { /* Parent. */
327 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
333 xx = sigrelse(SIGCHLD);
338 * Wait for child process to be reaped, and unregister SIGCHLD handler.
339 * @todo Rewrite to use waitpid on helper thread.
340 * @param sq scriptlet queue element
341 * @return 0 on success
343 static int rpmsqWaitUnregister(rpmsq sq)
349 /* Protect sq->reaped from handler changes. */
350 ret = sighold(SIGCHLD);
352 /* Start the child, linux often runs child before parent. */
353 if (sq->pipes[0] >= 0)
354 xx = close(sq->pipes[0]);
355 if (sq->pipes[1] >= 0)
356 xx = close(sq->pipes[1]);
357 sq->pipes[0] = sq->pipes[1] = -1;
359 /* Put a stopwatch on the time spent waiting to measure performance gain. */
360 (void) rpmswEnter(&sq->op, -1);
362 /* Wait for handler to receive SIGCHLD. */
363 while (ret == 0 && sq->reaped != sq->child) {
365 /* Note that sigpause re-enables SIGCHLD. */
366 ret = sigpause(SIGCHLD);
368 xx = sigrelse(SIGCHLD);
371 * We start before the fork with this mutex locked;
372 * The only one that unlocks this the signal handler.
373 * So if we get the lock the child has been reaped.
375 ret = pthread_mutex_lock(&sq->mutex);
376 xx = sighold(SIGCHLD);
380 /* Accumulate stopwatch time spent waiting, potential performance gain. */
381 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
383 xx = sigrelse(SIGCHLD);
387 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
390 /* Remove processed SIGCHLD item from queue. */
391 xx = rpmsqRemove(sq);
393 /* Disable SIGCHLD handler on refcount == 0. */
394 xx = rpmsqEnable(-SIGCHLD, NULL);
397 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
403 pid_t rpmsqWait(rpmsq sq)
408 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
412 (void) rpmsqWaitUnregister(sq);
417 reaped = waitpid(sq->child, &status, 0);
418 } while (reaped >= 0 && reaped != sq->child);
423 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
429 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
435 void * rpmsqThread(void * (*start) (void * arg), void * arg)
440 ret = pthread_create(&pth, NULL, start, arg);
441 return (ret == 0 ? (void *)pth : NULL);
444 int rpmsqJoin(void * thread)
446 pthread_t pth = (pthread_t) thread;
449 return pthread_join(pth, NULL);
452 int rpmsqThreadEqual(void * thread)
454 pthread_t t1 = (pthread_t) thread;
455 pthread_t t2 = pthread_self();
456 return pthread_equal(t1, t2);
460 * SIGCHLD cancellation handler.
463 sigchld_cancel (void *arg)
465 pid_t child = *(pid_t *) arg;
468 (void) kill(child, SIGKILL);
471 result = waitpid(child, NULL, 0);
472 } while (result == (pid_t)-1 && errno == EINTR);
475 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
476 (void) rpmsqEnable(-SIGQUIT, NULL);
477 (void) rpmsqEnable(-SIGINT, NULL);
483 * Execute a command, returning its status.
486 rpmsqExecve (const char ** argv)
492 sigset_t newMask, oldMask;
493 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
495 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
500 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
501 if (rpmsqEnable(SIGINT, NULL) < 0) {
502 SUB_REF (rpmsigTbl_sigchld);
505 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
506 SUB_REF (rpmsigTbl_sigchld);
507 goto out_restore_sigint;
512 (void) sigemptyset (&newMask);
513 (void) sigaddset (&newMask, SIGCHLD);
514 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
516 if (SUB_REF (rpmsigTbl_sigchld) == 0)
517 goto out_restore_sigquit_and_sigint;
521 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
524 if (pid < (pid_t) 0) { /* fork failed. */
526 } else if (pid == (pid_t) 0) { /* Child. */
528 /* Restore the signals. */
529 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
530 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
531 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
533 /* Reset rpmsigTbl lock and refcnt. */
536 (void) execve (argv[0], (char *const *) argv, environ);
538 } else { /* Parent. */
540 result = waitpid(pid, &status, 0);
541 } while (result == (pid_t)-1 && errno == EINTR);
546 CLEANUP_RESET(0, oldtype);
549 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
550 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
551 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
557 out_restore_sigquit_and_sigint:
558 (void) rpmsqEnable(-SIGQUIT, NULL);
560 (void) rpmsqEnable(-SIGINT, NULL);