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 */
66 #define _RPMSQ_INTERNAL
67 #include <rpm/rpmsq.h>
71 #define _RPMSQ_DEBUG 0
72 int _rpmsq_debug = _RPMSQ_DEBUG;
74 static struct rpmsqElem rpmsqRock;
76 static rpmsq rpmsqQueue = &rpmsqRock;
79 * Insert node into from queue.
80 * @param elem node to link
81 * @param prev previous node from queue
82 * @return 0 on success
84 static int rpmsqInsert(void * elem, void * prev)
86 rpmsq sq = (rpmsq) elem;
92 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
94 ret = sighold(SIGCHLD);
100 sq->pipes[0] = sq->pipes[1] = -1;
103 ret = pthread_mutex_init(&sq->mutex, NULL);
104 insque(elem, (prev != NULL ? prev : rpmsqQueue));
105 ret = sigrelse(SIGCHLD);
112 * Remove node from queue.
113 * @param elem node to link
114 * @return 0 on success
116 static int rpmsqRemove(void * elem)
118 rpmsq sq = (rpmsq) elem;
125 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
127 ret = sighold (SIGCHLD);
131 /* Unlock the mutex and then destroy it */
132 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
133 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;
139 #ifdef NOTYET /* rpmpsmWait debugging message needs */
145 ret = sigrelse(SIGCHLD);
151 static sigset_t rpmsqCaught;
153 static struct rpmsig_s {
155 rpmsqAction_t handler;
157 struct sigaction oact;
159 { SIGINT, rpmsqAction },
160 #define rpmsigTbl_sigint (&rpmsigTbl[0])
161 { SIGQUIT, rpmsqAction },
162 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
163 { SIGCHLD, rpmsqAction },
164 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
165 { SIGHUP, rpmsqAction },
166 #define rpmsigTbl_sighup (&rpmsigTbl[3])
167 { SIGTERM, rpmsqAction },
168 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
169 { SIGPIPE, rpmsqAction },
170 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
174 int rpmsqIsCaught(int signum)
176 return sigismember(&rpmsqCaught, signum);
180 void rpmsqAction(int signum,
181 void * info, void * context)
183 void rpmsqAction(int signum)
189 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
190 if (tbl->signum != signum)
193 (void) sigaddset(&rpmsqCaught, signum);
200 pid_t reaped = waitpid(0, &status, WNOHANG);
202 /* XXX errno set to ECHILD/EINVAL/EINTR. */
206 /* XXX insque(3)/remque(3) are dequeue, not ring. */
207 for (sq = rpmsqQueue->q_forw;
208 sq != NULL && sq != rpmsqQueue;
213 if (sq->child != reaped)
218 /* Unlock the mutex. The waiter will then be able to
221 * XXX: jbj, wtd, if this fails?
223 ret = pthread_mutex_unlock(&sq->mutex);
237 int rpmsqEnable(int signum, rpmsqAction_t handler)
239 int tblsignum = (signum >= 0 ? signum : -signum);
245 if (rpmsqQueue->id == NULL)
246 rpmsqQueue->id = ME();
247 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
248 if (tblsignum != tbl->signum)
251 if (signum >= 0) { /* Enable. */
252 if (ADD_REF(tbl) <= 0) {
253 (void) sigdelset(&rpmsqCaught, tbl->signum);
255 /* XXX Don't set a signal handler if already SIG_IGN */
256 (void) sigaction(tbl->signum, NULL, &tbl->oact);
257 if (tbl->oact.sa_handler == SIG_IGN)
260 (void) sigemptyset (&sa.sa_mask);
262 sa.sa_flags = SA_SIGINFO;
266 sa.sa_sigaction = (void*)(handler != NULL ? handler : tbl->handler);
267 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
271 tbl->active = 1; /* XXX just in case */
273 tbl->handler = handler;
275 } else { /* Disable. */
276 if (SUB_REF(tbl) <= 0) {
277 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
279 tbl->active = 0; /* XXX just in case */
280 tbl->handler = (handler != NULL ? handler : rpmsqAction);
290 pid_t rpmsqFork(rpmsq sq)
294 int nothreads = 0; /* XXX: Shouldn't this be a global? */
297 xx = rpmsqInsert(sq, NULL);
300 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
302 xx = rpmsqEnable(SIGCHLD, NULL);
305 xx = pipe(sq->pipes);
307 xx = sighold(SIGCHLD);
310 * Initialize the cond var mutex. We have to aquire the lock we
311 * use for the condition before we fork. Otherwise it is possible for
312 * the child to exit, we get sigchild and the sig handler to send
313 * the condition signal before we are waiting on the condition.
316 if(pthread_mutex_lock(&sq->mutex)) {
317 /* Yack we did not get the lock, lets just give up */
318 xx = close(sq->pipes[0]);
319 xx = close(sq->pipes[1]);
320 sq->pipes[0] = sq->pipes[1] = -1;
326 if (pid < (pid_t) 0) { /* fork failed. */
327 sq->child = (pid_t)-1;
328 xx = close(sq->pipes[0]);
329 xx = close(sq->pipes[1]);
330 sq->pipes[0] = sq->pipes[1] = -1;
332 } else if (pid == (pid_t) 0) { /* Child. */
335 /* Block to permit parent time to wait. */
336 xx = close(sq->pipes[1]);
337 xx = read(sq->pipes[0], &yy, sizeof(yy));
338 xx = close(sq->pipes[0]);
339 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)
374 /* Protect sq->reaped from handler changes. */
375 ret = sighold(SIGCHLD);
377 /* Start the child, linux often runs child before parent. */
378 if (sq->pipes[0] >= 0)
379 xx = close(sq->pipes[0]);
380 if (sq->pipes[1] >= 0)
381 xx = close(sq->pipes[1]);
382 sq->pipes[0] = sq->pipes[1] = -1;
384 /* Put a stopwatch on the time spent waiting to measure performance gain. */
385 (void) rpmswEnter(&sq->op, -1);
387 /* Wait for handler to receive SIGCHLD. */
388 while (ret == 0 && sq->reaped != sq->child) {
390 /* Note that sigpause re-enables SIGCHLD. */
391 ret = sigpause(SIGCHLD);
393 xx = sigrelse(SIGCHLD);
396 * We start before the fork with this mutex locked;
397 * The only one that unlocks this the signal handler.
398 * So if we get the lock the child has been reaped.
400 ret = pthread_mutex_lock(&sq->mutex);
401 xx = sighold(SIGCHLD);
405 /* Accumulate stopwatch time spent waiting, potential performance gain. */
406 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
408 xx = sigrelse(SIGCHLD);
412 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
415 /* Remove processed SIGCHLD item from queue. */
416 xx = rpmsqRemove(sq);
418 /* Disable SIGCHLD handler on refcount == 0. */
419 xx = rpmsqEnable(-SIGCHLD, NULL);
422 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
428 pid_t rpmsqWait(rpmsq sq)
433 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
437 (void) rpmsqWaitUnregister(sq);
442 reaped = waitpid(sq->child, &status, 0);
443 } while (reaped >= 0 && reaped != sq->child);
448 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
454 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
460 void * rpmsqThread(void * (*start) (void * arg), void * arg)
465 ret = pthread_create(&pth, NULL, start, arg);
466 return (ret == 0 ? (void *)pth : NULL);
469 int rpmsqJoin(void * thread)
471 pthread_t pth = (pthread_t) thread;
474 return pthread_join(pth, NULL);
477 int rpmsqThreadEqual(void * thread)
479 pthread_t t1 = (pthread_t) thread;
480 pthread_t t2 = pthread_self();
481 return pthread_equal(t1, t2);
485 * SIGCHLD cancellation handler.
488 sigchld_cancel (void *arg)
490 pid_t child = *(pid_t *) arg;
493 (void) kill(child, SIGKILL);
496 result = waitpid(child, NULL, 0);
497 } while (result == (pid_t)-1 && errno == EINTR);
500 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
501 (void) rpmsqEnable(-SIGQUIT, NULL);
502 (void) rpmsqEnable(-SIGINT, NULL);
508 * Execute a command, returning its status.
511 rpmsqExecve (const char ** argv)
517 sigset_t newMask, oldMask;
519 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
524 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
525 if (rpmsqEnable(SIGINT, NULL) < 0) {
526 SUB_REF (rpmsigTbl_sigchld);
529 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
530 SUB_REF (rpmsigTbl_sigchld);
531 goto out_restore_sigint;
536 (void) sigemptyset (&newMask);
537 (void) sigaddset (&newMask, SIGCHLD);
538 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
540 if (SUB_REF (rpmsigTbl_sigchld) == 0)
541 goto out_restore_sigquit_and_sigint;
545 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
548 if (pid < (pid_t) 0) { /* fork failed. */
550 } else if (pid == (pid_t) 0) { /* Child. */
552 /* Restore the signals. */
553 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
554 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
555 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
557 /* Reset rpmsigTbl lock and refcnt. */
560 (void) execve (argv[0], (char *const *) argv, environ);
562 } else { /* Parent. */
564 result = waitpid(pid, &status, 0);
565 } while (result == (pid_t)-1 && errno == EINTR);
570 CLEANUP_RESET(0, oldtype);
573 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
574 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
575 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
581 out_restore_sigquit_and_sigint:
582 (void) rpmsqEnable(-SIGQUIT, NULL);
584 (void) rpmsqEnable(-SIGINT, NULL);