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 rpmsq rpmsqQueue = &rpmsqRock;
78 int rpmsqInsert(void * elem, void * prev)
80 rpmsq sq = (rpmsq) elem;
86 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
88 ret = sighold(SIGCHLD);
94 sq->pipes[0] = sq->pipes[1] = -1;
97 ret = pthread_mutex_init(&sq->mutex, NULL);
98 insque(elem, (prev != NULL ? prev : rpmsqQueue));
99 ret = sigrelse(SIGCHLD);
105 int rpmsqRemove(void * elem)
107 rpmsq sq = (rpmsq) elem;
114 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
116 ret = sighold (SIGCHLD);
120 /* Unlock the mutex and then destroy it */
121 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
122 ret = pthread_mutex_destroy(&sq->mutex);
125 if (sq->pipes[1]) ret = close(sq->pipes[1]);
126 if (sq->pipes[0]) ret = close(sq->pipes[0]);
127 sq->pipes[0] = sq->pipes[1] = -1;
128 #ifdef NOTYET /* rpmpsmWait debugging message needs */
134 ret = sigrelse(SIGCHLD);
140 sigset_t rpmsqCaught;
142 static struct rpmsig_s {
144 void (*handler) (int signum, void * info, void * context);
146 struct sigaction oact;
148 { SIGINT, rpmsqAction },
149 #define rpmsigTbl_sigint (&rpmsigTbl[0])
150 { SIGQUIT, rpmsqAction },
151 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
152 { SIGCHLD, rpmsqAction },
153 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
154 { SIGHUP, rpmsqAction },
155 #define rpmsigTbl_sighup (&rpmsigTbl[3])
156 { SIGTERM, rpmsqAction },
157 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
158 { SIGPIPE, rpmsqAction },
159 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
163 void rpmsqAction(int signum,
164 void * info, void * context)
169 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
170 if (tbl->signum != signum)
173 (void) sigaddset(&rpmsqCaught, signum);
180 pid_t reaped = waitpid(0, &status, WNOHANG);
182 /* XXX errno set to ECHILD/EINVAL/EINTR. */
186 /* XXX insque(3)/remque(3) are dequeue, not ring. */
187 for (sq = rpmsqQueue->q_forw;
188 sq != NULL && sq != rpmsqQueue;
193 if (sq->child != reaped)
198 /* Unlock the mutex. The waiter will then be able to
201 * XXX: jbj, wtd, if this fails?
203 ret = pthread_mutex_unlock(&sq->mutex);
217 int rpmsqEnable(int signum, rpmsqAction_t handler)
219 int tblsignum = (signum >= 0 ? signum : -signum);
225 if (rpmsqQueue->id == NULL)
226 rpmsqQueue->id = ME();
227 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
228 if (tblsignum != tbl->signum)
231 if (signum >= 0) { /* Enable. */
232 if (ADD_REF(tbl) <= 0) {
233 (void) sigdelset(&rpmsqCaught, tbl->signum);
235 /* XXX Don't set a signal handler if already SIG_IGN */
236 (void) sigaction(tbl->signum, NULL, &tbl->oact);
237 if (tbl->oact.sa_handler == SIG_IGN)
240 (void) sigemptyset (&sa.sa_mask);
241 sa.sa_flags = SA_SIGINFO;
242 sa.sa_sigaction = (void*)(handler != NULL ? handler : tbl->handler);
243 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
247 tbl->active = 1; /* XXX just in case */
249 tbl->handler = handler;
251 } else { /* Disable. */
252 if (SUB_REF(tbl) <= 0) {
253 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
255 tbl->active = 0; /* XXX just in case */
256 tbl->handler = (handler != NULL ? handler : rpmsqAction);
266 pid_t rpmsqFork(rpmsq sq)
270 int nothreads = 0; /* XXX: Shouldn't this be a global? */
273 xx = rpmsqInsert(sq, NULL);
276 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
278 xx = rpmsqEnable(SIGCHLD, NULL);
281 xx = pipe(sq->pipes);
283 xx = sighold(SIGCHLD);
286 * Initialize the cond var mutex. We have to aquire the lock we
287 * use for the condition before we fork. Otherwise it is possible for
288 * the child to exit, we get sigchild and the sig handler to send
289 * the condition signal before we are waiting on the condition.
292 if(pthread_mutex_lock(&sq->mutex)) {
293 /* Yack we did not get the lock, lets just give up */
294 xx = close(sq->pipes[0]);
295 xx = close(sq->pipes[1]);
296 sq->pipes[0] = sq->pipes[1] = -1;
302 if (pid < (pid_t) 0) { /* fork failed. */
303 sq->child = (pid_t)-1;
304 xx = close(sq->pipes[0]);
305 xx = close(sq->pipes[1]);
306 sq->pipes[0] = sq->pipes[1] = -1;
308 } else if (pid == (pid_t) 0) { /* Child. */
311 /* Block to permit parent time to wait. */
312 xx = close(sq->pipes[1]);
313 xx = read(sq->pipes[0], &yy, sizeof(yy));
314 xx = close(sq->pipes[0]);
315 sq->pipes[0] = sq->pipes[1] = -1;
319 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
322 } else { /* Parent. */
328 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
334 xx = sigrelse(SIGCHLD);
339 * Wait for child process to be reaped, and unregister SIGCHLD handler.
340 * @todo Rewrite to use waitpid on helper thread.
341 * @param sq scriptlet queue element
342 * @return 0 on success
344 static int rpmsqWaitUnregister(rpmsq sq)
350 /* Protect sq->reaped from handler changes. */
351 ret = sighold(SIGCHLD);
353 /* Start the child, linux often runs child before parent. */
354 if (sq->pipes[0] >= 0)
355 xx = close(sq->pipes[0]);
356 if (sq->pipes[1] >= 0)
357 xx = close(sq->pipes[1]);
358 sq->pipes[0] = sq->pipes[1] = -1;
360 /* Put a stopwatch on the time spent waiting to measure performance gain. */
361 (void) rpmswEnter(&sq->op, -1);
363 /* Wait for handler to receive SIGCHLD. */
364 while (ret == 0 && sq->reaped != sq->child) {
366 /* Note that sigpause re-enables SIGCHLD. */
367 ret = sigpause(SIGCHLD);
369 xx = sigrelse(SIGCHLD);
372 * We start before the fork with this mutex locked;
373 * The only one that unlocks this the signal handler.
374 * So if we get the lock the child has been reaped.
376 ret = pthread_mutex_lock(&sq->mutex);
377 xx = sighold(SIGCHLD);
381 /* Accumulate stopwatch time spent waiting, potential performance gain. */
382 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
384 xx = sigrelse(SIGCHLD);
388 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
391 /* Remove processed SIGCHLD item from queue. */
392 xx = rpmsqRemove(sq);
394 /* Disable SIGCHLD handler on refcount == 0. */
395 xx = rpmsqEnable(-SIGCHLD, NULL);
398 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
404 pid_t rpmsqWait(rpmsq sq)
409 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
413 (void) rpmsqWaitUnregister(sq);
418 reaped = waitpid(sq->child, &status, 0);
419 } while (reaped >= 0 && reaped != sq->child);
424 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
430 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
436 void * rpmsqThread(void * (*start) (void * arg), void * arg)
441 ret = pthread_create(&pth, NULL, start, arg);
442 return (ret == 0 ? (void *)pth : NULL);
445 int rpmsqJoin(void * thread)
447 pthread_t pth = (pthread_t) thread;
450 return pthread_join(pth, NULL);
453 int rpmsqThreadEqual(void * thread)
455 pthread_t t1 = (pthread_t) thread;
456 pthread_t t2 = pthread_self();
457 return pthread_equal(t1, t2);
461 * SIGCHLD cancellation handler.
464 sigchld_cancel (void *arg)
466 pid_t child = *(pid_t *) arg;
469 (void) kill(child, SIGKILL);
472 result = waitpid(child, NULL, 0);
473 } while (result == (pid_t)-1 && errno == EINTR);
476 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
477 (void) rpmsqEnable(-SIGQUIT, NULL);
478 (void) rpmsqEnable(-SIGINT, NULL);
484 * Execute a command, returning its status.
487 rpmsqExecve (const char ** argv)
493 sigset_t newMask, oldMask;
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);