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 sigset_t rpmsqCaught;
153 static struct rpmsig_s {
155 void (*handler) (int signum, void * info, void * context);
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 void rpmsqAction(int signum,
175 void * info, void * context)
180 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
181 if (tbl->signum != signum)
184 (void) sigaddset(&rpmsqCaught, signum);
191 pid_t reaped = waitpid(0, &status, WNOHANG);
193 /* XXX errno set to ECHILD/EINVAL/EINTR. */
197 /* XXX insque(3)/remque(3) are dequeue, not ring. */
198 for (sq = rpmsqQueue->q_forw;
199 sq != NULL && sq != rpmsqQueue;
204 if (sq->child != reaped)
209 /* Unlock the mutex. The waiter will then be able to
212 * XXX: jbj, wtd, if this fails?
214 ret = pthread_mutex_unlock(&sq->mutex);
228 int rpmsqEnable(int signum, rpmsqAction_t handler)
230 int tblsignum = (signum >= 0 ? signum : -signum);
236 if (rpmsqQueue->id == NULL)
237 rpmsqQueue->id = ME();
238 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
239 if (tblsignum != tbl->signum)
242 if (signum >= 0) { /* Enable. */
243 if (ADD_REF(tbl) <= 0) {
244 (void) sigdelset(&rpmsqCaught, tbl->signum);
246 /* XXX Don't set a signal handler if already SIG_IGN */
247 (void) sigaction(tbl->signum, NULL, &tbl->oact);
248 if (tbl->oact.sa_handler == SIG_IGN)
251 (void) sigemptyset (&sa.sa_mask);
252 sa.sa_flags = SA_SIGINFO;
253 sa.sa_sigaction = (void*)(handler != NULL ? handler : tbl->handler);
254 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
258 tbl->active = 1; /* XXX just in case */
260 tbl->handler = handler;
262 } else { /* Disable. */
263 if (SUB_REF(tbl) <= 0) {
264 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
266 tbl->active = 0; /* XXX just in case */
267 tbl->handler = (handler != NULL ? handler : rpmsqAction);
277 pid_t rpmsqFork(rpmsq sq)
281 int nothreads = 0; /* XXX: Shouldn't this be a global? */
284 xx = rpmsqInsert(sq, NULL);
287 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
289 xx = rpmsqEnable(SIGCHLD, NULL);
292 xx = pipe(sq->pipes);
294 xx = sighold(SIGCHLD);
297 * Initialize the cond var mutex. We have to aquire the lock we
298 * use for the condition before we fork. Otherwise it is possible for
299 * the child to exit, we get sigchild and the sig handler to send
300 * the condition signal before we are waiting on the condition.
303 if(pthread_mutex_lock(&sq->mutex)) {
304 /* Yack we did not get the lock, lets just give up */
305 xx = close(sq->pipes[0]);
306 xx = close(sq->pipes[1]);
307 sq->pipes[0] = sq->pipes[1] = -1;
313 if (pid < (pid_t) 0) { /* fork failed. */
314 sq->child = (pid_t)-1;
315 xx = close(sq->pipes[0]);
316 xx = close(sq->pipes[1]);
317 sq->pipes[0] = sq->pipes[1] = -1;
319 } else if (pid == (pid_t) 0) { /* Child. */
322 /* Block to permit parent time to wait. */
323 xx = close(sq->pipes[1]);
324 xx = read(sq->pipes[0], &yy, sizeof(yy));
325 xx = close(sq->pipes[0]);
326 sq->pipes[0] = sq->pipes[1] = -1;
330 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
333 } else { /* Parent. */
339 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
345 xx = sigrelse(SIGCHLD);
350 * Wait for child process to be reaped, and unregister SIGCHLD handler.
351 * @todo Rewrite to use waitpid on helper thread.
352 * @param sq scriptlet queue element
353 * @return 0 on success
355 static int rpmsqWaitUnregister(rpmsq sq)
361 /* Protect sq->reaped from handler changes. */
362 ret = sighold(SIGCHLD);
364 /* Start the child, linux often runs child before parent. */
365 if (sq->pipes[0] >= 0)
366 xx = close(sq->pipes[0]);
367 if (sq->pipes[1] >= 0)
368 xx = close(sq->pipes[1]);
369 sq->pipes[0] = sq->pipes[1] = -1;
371 /* Put a stopwatch on the time spent waiting to measure performance gain. */
372 (void) rpmswEnter(&sq->op, -1);
374 /* Wait for handler to receive SIGCHLD. */
375 while (ret == 0 && sq->reaped != sq->child) {
377 /* Note that sigpause re-enables SIGCHLD. */
378 ret = sigpause(SIGCHLD);
380 xx = sigrelse(SIGCHLD);
383 * We start before the fork with this mutex locked;
384 * The only one that unlocks this the signal handler.
385 * So if we get the lock the child has been reaped.
387 ret = pthread_mutex_lock(&sq->mutex);
388 xx = sighold(SIGCHLD);
392 /* Accumulate stopwatch time spent waiting, potential performance gain. */
393 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
395 xx = sigrelse(SIGCHLD);
399 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
402 /* Remove processed SIGCHLD item from queue. */
403 xx = rpmsqRemove(sq);
405 /* Disable SIGCHLD handler on refcount == 0. */
406 xx = rpmsqEnable(-SIGCHLD, NULL);
409 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
415 pid_t rpmsqWait(rpmsq sq)
420 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
424 (void) rpmsqWaitUnregister(sq);
429 reaped = waitpid(sq->child, &status, 0);
430 } while (reaped >= 0 && reaped != sq->child);
435 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
441 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
447 void * rpmsqThread(void * (*start) (void * arg), void * arg)
452 ret = pthread_create(&pth, NULL, start, arg);
453 return (ret == 0 ? (void *)pth : NULL);
456 int rpmsqJoin(void * thread)
458 pthread_t pth = (pthread_t) thread;
461 return pthread_join(pth, NULL);
464 int rpmsqThreadEqual(void * thread)
466 pthread_t t1 = (pthread_t) thread;
467 pthread_t t2 = pthread_self();
468 return pthread_equal(t1, t2);
472 * SIGCHLD cancellation handler.
475 sigchld_cancel (void *arg)
477 pid_t child = *(pid_t *) arg;
480 (void) kill(child, SIGKILL);
483 result = waitpid(child, NULL, 0);
484 } while (result == (pid_t)-1 && errno == EINTR);
487 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
488 (void) rpmsqEnable(-SIGQUIT, NULL);
489 (void) rpmsqEnable(-SIGINT, NULL);
495 * Execute a command, returning its status.
498 rpmsqExecve (const char ** argv)
504 sigset_t newMask, oldMask;
506 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
511 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
512 if (rpmsqEnable(SIGINT, NULL) < 0) {
513 SUB_REF (rpmsigTbl_sigchld);
516 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
517 SUB_REF (rpmsigTbl_sigchld);
518 goto out_restore_sigint;
523 (void) sigemptyset (&newMask);
524 (void) sigaddset (&newMask, SIGCHLD);
525 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
527 if (SUB_REF (rpmsigTbl_sigchld) == 0)
528 goto out_restore_sigquit_and_sigint;
532 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
535 if (pid < (pid_t) 0) { /* fork failed. */
537 } else if (pid == (pid_t) 0) { /* Child. */
539 /* Restore the signals. */
540 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
541 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
542 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
544 /* Reset rpmsigTbl lock and refcnt. */
547 (void) execve (argv[0], (char *const *) argv, environ);
549 } else { /* Parent. */
551 result = waitpid(pid, &status, 0);
552 } while (result == (pid_t)-1 && errno == EINTR);
557 CLEANUP_RESET(0, oldtype);
560 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
561 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
562 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
568 out_restore_sigquit_and_sigint:
569 (void) rpmsqEnable(-SIGQUIT, NULL);
571 (void) rpmsqEnable(-SIGINT, NULL);