8 #include <sys/signal.h>
14 #if defined(HAVE_PTHREAD_H)
18 /* XXX suggested in bugzilla #159024 */
19 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
20 #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
23 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
24 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
29 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
30 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
32 { pthread_mutexattr_t attr; \
33 (void) pthread_mutexattr_init(&attr); \
34 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
35 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
36 (void) pthread_mutexattr_destroy(&attr); \
37 rpmsigTbl_sigchld->active = 0; \
39 #define ADD_REF(__tbl) (__tbl)->active++
40 #define SUB_REF(__tbl) --(__tbl)->active
41 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
42 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
43 pthread_cleanup_push((__handler), (__arg));
44 #define CLEANUP_RESET(__execute, __oldtype) \
45 pthread_cleanup_pop(__execute); \
46 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
48 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
50 #define ME() ((void *)pthread_self())
57 #define ADD_REF(__tbl) (0)
58 #define SUB_REF(__tbl) (0)
59 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
60 #define CLEANUP_RESET(__execute, __oldtype)
62 #define SAME_THREAD(_a, _b) (42)
64 #define ME() (((void *)getpid()))
66 #endif /* HAVE_PTHREAD_H */
68 #define _RPMSQ_INTERNAL
69 #include <rpm/rpmsq.h>
73 #define _RPMSQ_DEBUG 0
74 int _rpmsq_debug = _RPMSQ_DEBUG;
76 static struct rpmsqElem rpmsqRock;
78 static rpmsq rpmsqQueue = &rpmsqRock;
81 * Insert node into from queue.
82 * @param elem node to link
83 * @param prev previous node from queue
84 * @return 0 on success
86 static int rpmsqInsert(void * elem, void * prev)
88 rpmsq sq = (rpmsq) elem;
94 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
96 ret = sighold(SIGCHLD);
102 sq->pipes[0] = sq->pipes[1] = -1;
105 ret = pthread_mutex_init(&sq->mutex, NULL);
106 insque(elem, (prev != NULL ? prev : rpmsqQueue));
107 ret = sigrelse(SIGCHLD);
114 * Remove node from queue.
115 * @param elem node to link
116 * @return 0 on success
118 static int rpmsqRemove(void * elem)
120 rpmsq sq = (rpmsq) elem;
127 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
129 ret = sighold (SIGCHLD);
133 /* Unlock the mutex and then destroy it */
134 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
135 ret = pthread_mutex_destroy(&sq->mutex);
138 if (sq->pipes[1]) ret = close(sq->pipes[1]);
139 if (sq->pipes[0]) ret = close(sq->pipes[0]);
140 sq->pipes[0] = sq->pipes[1] = -1;
141 #ifdef NOTYET /* rpmpsmWait debugging message needs */
147 ret = sigrelse(SIGCHLD);
153 static sigset_t rpmsqCaught;
155 static struct rpmsig_s {
157 rpmsqAction_t handler;
159 struct sigaction oact;
161 { SIGINT, rpmsqAction },
162 #define rpmsigTbl_sigint (&rpmsigTbl[0])
163 { SIGQUIT, rpmsqAction },
164 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
165 { SIGCHLD, rpmsqAction },
166 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
167 { SIGHUP, rpmsqAction },
168 #define rpmsigTbl_sighup (&rpmsigTbl[3])
169 { SIGTERM, rpmsqAction },
170 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
171 { SIGPIPE, rpmsqAction },
172 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
176 int rpmsqIsCaught(int signum)
178 return sigismember(&rpmsqCaught, signum);
182 void rpmsqAction(int signum, siginfo_t * info, void * context)
184 void rpmsqAction(int signum)
190 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
191 if (tbl->signum != signum)
194 (void) sigaddset(&rpmsqCaught, signum);
201 pid_t reaped = waitpid(0, &status, WNOHANG);
203 /* XXX errno set to ECHILD/EINVAL/EINTR. */
207 /* XXX insque(3)/remque(3) are dequeue, not ring. */
208 for (sq = rpmsqQueue->q_forw;
209 sq != NULL && sq != rpmsqQueue;
214 if (sq->child != reaped)
219 /* Unlock the mutex. The waiter will then be able to
222 * XXX: jbj, wtd, if this fails?
224 ret = pthread_mutex_unlock(&sq->mutex);
238 int rpmsqEnable(int signum, rpmsqAction_t handler)
240 int tblsignum = (signum >= 0 ? signum : -signum);
246 if (rpmsqQueue->id == NULL)
247 rpmsqQueue->id = ME();
248 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
249 if (tblsignum != tbl->signum)
252 if (signum >= 0) { /* Enable. */
253 if (ADD_REF(tbl) <= 0) {
254 (void) sigdelset(&rpmsqCaught, tbl->signum);
256 /* XXX Don't set a signal handler if already SIG_IGN */
257 (void) sigaction(tbl->signum, NULL, &tbl->oact);
258 if (tbl->oact.sa_handler == SIG_IGN)
261 (void) sigemptyset (&sa.sa_mask);
263 sa.sa_flags = SA_SIGINFO;
267 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
268 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
272 tbl->active = 1; /* XXX just in case */
274 tbl->handler = handler;
276 } else { /* Disable. */
277 if (SUB_REF(tbl) <= 0) {
278 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
280 tbl->active = 0; /* XXX just in case */
281 tbl->handler = (handler != NULL ? handler : rpmsqAction);
291 pid_t rpmsqFork(rpmsq sq)
295 int nothreads = 0; /* XXX: Shouldn't this be a global? */
298 xx = rpmsqInsert(sq, NULL);
301 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
303 xx = rpmsqEnable(SIGCHLD, NULL);
306 xx = pipe(sq->pipes);
308 xx = sighold(SIGCHLD);
311 * Initialize the cond var mutex. We have to aquire the lock we
312 * use for the condition before we fork. Otherwise it is possible for
313 * the child to exit, we get sigchild and the sig handler to send
314 * the condition signal before we are waiting on the condition.
317 if(pthread_mutex_lock(&sq->mutex)) {
318 /* Yack we did not get the lock, lets just give up */
319 xx = close(sq->pipes[0]);
320 xx = close(sq->pipes[1]);
321 sq->pipes[0] = sq->pipes[1] = -1;
327 if (pid < (pid_t) 0) { /* fork failed. */
328 sq->child = (pid_t)-1;
329 xx = close(sq->pipes[0]);
330 xx = close(sq->pipes[1]);
331 sq->pipes[0] = sq->pipes[1] = -1;
333 } else if (pid == (pid_t) 0) { /* Child. */
336 /* Block to permit parent time to wait. */
337 xx = close(sq->pipes[1]);
338 xx = read(sq->pipes[0], &yy, sizeof(yy));
339 xx = close(sq->pipes[0]);
340 sq->pipes[0] = sq->pipes[1] = -1;
344 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
347 } else { /* Parent. */
353 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
359 xx = sigrelse(SIGCHLD);
364 * Wait for child process to be reaped, and unregister SIGCHLD handler.
365 * @todo Rewrite to use waitpid on helper thread.
366 * @param sq scriptlet queue element
367 * @return 0 on success
369 static int rpmsqWaitUnregister(rpmsq sq)
375 /* Protect sq->reaped from handler changes. */
376 ret = sighold(SIGCHLD);
378 /* Start the child, linux often runs child before parent. */
379 if (sq->pipes[0] >= 0)
380 xx = close(sq->pipes[0]);
381 if (sq->pipes[1] >= 0)
382 xx = close(sq->pipes[1]);
383 sq->pipes[0] = sq->pipes[1] = -1;
385 /* Put a stopwatch on the time spent waiting to measure performance gain. */
386 (void) rpmswEnter(&sq->op, -1);
388 /* Wait for handler to receive SIGCHLD. */
389 while (ret == 0 && sq->reaped != sq->child) {
391 /* Note that sigpause re-enables SIGCHLD. */
392 ret = sigpause(SIGCHLD);
394 xx = sigrelse(SIGCHLD);
397 * We start before the fork with this mutex locked;
398 * The only one that unlocks this the signal handler.
399 * So if we get the lock the child has been reaped.
401 ret = pthread_mutex_lock(&sq->mutex);
402 xx = sighold(SIGCHLD);
406 /* Accumulate stopwatch time spent waiting, potential performance gain. */
407 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
409 xx = sigrelse(SIGCHLD);
413 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
416 /* Remove processed SIGCHLD item from queue. */
417 xx = rpmsqRemove(sq);
419 /* Disable SIGCHLD handler on refcount == 0. */
420 xx = rpmsqEnable(-SIGCHLD, NULL);
423 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
429 pid_t rpmsqWait(rpmsq sq)
434 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
438 (void) rpmsqWaitUnregister(sq);
443 reaped = waitpid(sq->child, &status, 0);
444 } while (reaped >= 0 && reaped != sq->child);
449 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
455 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
461 void * rpmsqThread(void * (*start) (void * arg), void * arg)
466 ret = pthread_create(&pth, NULL, start, arg);
467 return (ret == 0 ? (void *)pth : NULL);
470 int rpmsqJoin(void * thread)
472 pthread_t pth = (pthread_t) thread;
475 return pthread_join(pth, NULL);
478 int rpmsqThreadEqual(void * thread)
480 pthread_t t1 = (pthread_t) thread;
481 pthread_t t2 = pthread_self();
482 return pthread_equal(t1, t2);
486 * SIGCHLD cancellation handler.
489 sigchld_cancel (void *arg)
491 pid_t child = *(pid_t *) arg;
494 (void) kill(child, SIGKILL);
497 result = waitpid(child, NULL, 0);
498 } while (result == (pid_t)-1 && errno == EINTR);
501 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
502 (void) rpmsqEnable(-SIGQUIT, NULL);
503 (void) rpmsqEnable(-SIGINT, NULL);
509 * Execute a command, returning its status.
512 rpmsqExecve (const char ** argv)
518 sigset_t newMask, oldMask;
520 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
525 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
526 if (rpmsqEnable(SIGINT, NULL) < 0) {
527 SUB_REF (rpmsigTbl_sigchld);
530 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
531 SUB_REF (rpmsigTbl_sigchld);
532 goto out_restore_sigint;
537 (void) sigemptyset (&newMask);
538 (void) sigaddset (&newMask, SIGCHLD);
539 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
541 if (SUB_REF (rpmsigTbl_sigchld) == 0)
542 goto out_restore_sigquit_and_sigint;
546 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
549 if (pid < (pid_t) 0) { /* fork failed. */
551 } else if (pid == (pid_t) 0) { /* Child. */
553 /* Restore the signals. */
554 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
555 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
556 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
558 /* Reset rpmsigTbl lock and refcnt. */
561 (void) execve (argv[0], (char *const *) argv, environ);
563 } else { /* Parent. */
565 result = waitpid(pid, &status, 0);
566 } while (result == (pid_t)-1 && errno == EINTR);
571 CLEANUP_RESET(0, oldtype);
574 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
575 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
576 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
582 out_restore_sigquit_and_sigint:
583 (void) rpmsqEnable(-SIGQUIT, NULL);
585 (void) rpmsqEnable(-SIGINT, NULL);