7 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
11 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
12 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
14 { pthread_mutexattr_t attr; \
15 pthread_mutexattr_init(&attr); \
16 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
17 pthread_mutex_init (&rpmsigTbl_lock, &attr); \
18 pthread_mutexattr_destroy(&attr); \
19 rpmsigTbl_sigchld->active = 0; \
21 #define ADD_REF(__tbl) (__tbl)->active++
22 #define SUB_REF(__tbl) --(__tbl)->active
23 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
24 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr)); \
25 pthread_cleanup_push((__handler), (__arg));
26 #define CLEANUP_RESET(__execute, __oldtype) \
27 pthread_cleanup_pop(__execute); \
28 pthread_setcanceltype ((__oldtype), &(__oldtype));
30 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
32 #define ME() ((void *)pthread_self())
39 #define ADD_REF(__tbl)
40 #define SUB_REF(__tbl)
41 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
42 #define CLEANUP_RESET(__execute, __oldtype)
44 #define SAME_THREAD(_a, _b) (42)
46 #define ME() (((void *))getpid())
48 #endif /* HAVE_PTHREAD_H */
54 #define _RPMSQ_DEBUG 0
56 int _rpmsq_debug = _RPMSQ_DEBUG;
59 static struct rpmsqElem rpmsqRock;
61 rpmsq rpmsqQueue = &rpmsqRock;
63 int rpmsqInsert(void * elem, void * prev)
65 rpmsq sq = (rpmsq) elem;
72 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
75 ret = sighold(SIGCHLD);
81 sq->pipes[0] = sq->pipes[1] = -1;
84 ret = pthread_mutex_init(&sq->mutex, NULL);
85 ret = pthread_cond_init(&sq->cond, NULL);
86 insque(elem, (prev ? prev : rpmsqQueue));
87 ret = sigrelse(SIGCHLD);
93 int rpmsqRemove(void * elem)
95 rpmsq sq = (rpmsq) elem;
103 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
106 ret = sighold (SIGCHLD);
109 ret = pthread_cond_destroy(&sq->cond);
110 ret = pthread_mutex_destroy(&sq->mutex);
112 if (sq->pipes[1]) close(sq->pipes[1]);
113 if (sq->pipes[0]) close(sq->pipes[0]);
114 sq->pipes[0] = sq->pipes[1] = -1;
115 #ifdef NOTYET /* rpmpsmWait debugging message needs */
121 ret = sigrelse(SIGCHLD);
128 sigset_t rpmsqCaught;
131 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
135 static struct rpmsig_s {
137 void (*handler) (int signum, siginfo_t * info, void * context);
139 struct sigaction oact;
141 { SIGINT, rpmsqAction },
142 #define rpmsigTbl_sigint (&rpmsigTbl[0])
143 { SIGQUIT, rpmsqAction },
144 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
145 { SIGCHLD, rpmsqAction },
146 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
147 { SIGHUP, rpmsqAction },
148 #define rpmsigTbl_sighup (&rpmsigTbl[3])
149 { SIGTERM, rpmsqAction },
150 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
151 { SIGPIPE, rpmsqAction },
152 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
158 void rpmsqAction(int signum, siginfo_t * info, void * context)
163 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
164 if (tbl->signum != signum)
167 (void) sigaddset(&rpmsqCaught, signum);
174 pid_t reaped = waitpid(0, &status, WNOHANG);
176 /* XXX errno set to ECHILD/EINVAL/EINTR. */
178 /*@innerbreak@*/ break;
180 /* XXX insque(3)/remque(3) are dequeue, not ring. */
181 for (sq = rpmsqQueue->q_forw;
182 sq != NULL && sq != rpmsqQueue;
185 if (sq->child != reaped)
186 /*@innercontinue@*/ continue;
189 (void) pthread_cond_signal(&sq->cond);
190 /*@innerbreak@*/ break;
193 /*@switchbreak@*/ break;
195 /*@switchbreak@*/ break;
203 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
205 int tblsignum = (signum >= 0 ? signum : -signum);
211 if (rpmsqQueue->id == NULL)
212 rpmsqQueue->id = ME();
213 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
214 if (tblsignum != tbl->signum)
217 if (signum >= 0) { /* Enable. */
218 if (ADD_REF(tbl) <= 0) {
219 (void) sigdelset(&rpmsqCaught, tbl->signum);
220 sigemptyset (&sa.sa_mask);
221 sa.sa_flags = SA_SIGINFO;
222 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
223 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
227 tbl->active = 1; /* XXX just in case */
229 tbl->handler = handler;
231 } else { /* Disable. */
232 if (SUB_REF(tbl) <= 0) {
233 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
235 tbl->active = 0; /* XXX just in case */
236 tbl->handler = (handler != NULL ? handler : rpmsqAction);
246 pid_t rpmsqFork(rpmsq sq)
252 xx = rpmsqInsert(sq, NULL);
256 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
259 xx = rpmsqEnable(SIGCHLD, NULL);
262 xx = pipe(sq->pipes);
264 xx = sighold(SIGCHLD);
267 if (pid < (pid_t) 0) { /* fork failed. */
268 xx = close(sq->pipes[0]);
269 xx = close(sq->pipes[1]);
270 sq->pipes[0] = sq->pipes[1] = -1;
272 } else if (pid == (pid_t) 0) { /* Child. */
275 /* Block to permit parent to wait. */
276 xx = close(sq->pipes[1]);
277 xx = read(sq->pipes[0], &yy, sizeof(yy));
278 xx = close(sq->pipes[0]);
279 sq->pipes[0] = sq->pipes[1] = -1;
284 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
288 } else { /* Parent. */
295 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
301 xx = close(sq->pipes[0]);
302 xx = close(sq->pipes[1]);
303 sq->pipes[0] = sq->pipes[1] = -1;
309 xx = sigrelse(SIGCHLD);
314 * Wait for child process to be reaped, and unregister SIGCHLD handler.
315 * @param sq scriptlet queue element
316 * @return 0 on success
318 static int rpmsqWaitUnregister(rpmsq sq)
319 /*@globals fileSystem, internalState @*/
320 /*@modifies fileSystem, internalState @*/
328 ret = sighold(SIGCHLD);
330 ret = pthread_mutex_lock(&sq->mutex);
332 /* Start the child. */
333 if (sq->pipes[0] >= 0)
334 xx = close(sq->pipes[0]);
335 if (sq->pipes[1] >= 0)
336 xx = close(sq->pipes[1]);
337 sq->pipes[0] = sq->pipes[1] = -1;
339 (void) rpmswNow(&sq->begin);
342 while (ret == 0 && sq->reaped != sq->child) {
344 ret = sigpause(SIGCHLD);
346 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
350 sq->msecs = rpmswDiff(rpmswNow(&end), &sq->begin)/1000;
351 sq->script_msecs += sq->msecs;
354 xx = sigrelse(SIGCHLD);
356 xx = pthread_mutex_unlock(&sq->mutex);
361 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
365 xx = rpmsqRemove(sq);
366 xx = rpmsqEnable(-SIGCHLD, NULL);
370 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
377 pid_t rpmsqWait(rpmsq sq)
383 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
388 (void) rpmsqWaitUnregister(sq);
393 reaped = waitpid(sq->child, &status, 0);
394 } while (reaped >= 0 && reaped != sq->child);
400 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
408 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
415 int rpmsqThread(void * (*start) (void * arg), void * arg)
420 ret = pthread_create(&pth, NULL, start, arg);
423 fprintf(stderr, " Thread(%p): %p\n", ME(), pth);
425 ret = pthread_join(pth, NULL);
431 * SIGCHLD cancellation handler.
434 sigchld_cancel (void *arg)
436 pid_t child = *(pid_t *) arg;
439 (void) kill(child, SIGKILL);
442 result = waitpid(child, NULL, 0);
443 } while (result == (pid_t)-1 && errno == EINTR);
446 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
447 (void) rpmsqEnable(-SIGQUIT, NULL);
448 (void) rpmsqEnable(-SIGINT, NULL);
454 * Execute a command, returning its status.
457 rpmsqExecve (const char ** argv)
463 sigset_t newMask, oldMask;
464 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
467 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
468 if (rpmsqEnable(SIGINT, NULL) < 0) {
469 SUB_REF (rpmsigTbl_sigchld);
472 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
473 SUB_REF (rpmsigTbl_sigchld);
474 goto out_restore_sigint;
479 sigemptyset (&newMask);
480 sigaddset (&newMask, SIGCHLD);
481 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
483 if (SUB_REF (rpmsigTbl_sigchld) == 0)
484 goto out_restore_sigquit_and_sigint;
488 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
491 if (pid < (pid_t) 0) { /* fork failed. */
493 } else if (pid == (pid_t) 0) { /* Child. */
495 /* Restore the signals. */
496 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
497 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
498 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
500 /* Reset rpmsigTbl lock and refcnt. */
503 (void) execve (argv[0], (char *const *) argv, environ);
505 } else { /* Parent. */
507 result = waitpid(pid, &status, 0);
508 } while (result == (pid_t)-1 && errno == EINTR);
513 CLEANUP_RESET(0, oldtype);
516 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
517 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
518 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
524 out_restore_sigquit_and_sigint:
525 (void) rpmsqEnable(-SIGQUIT, NULL);
527 (void) rpmsqEnable(-SIGINT, NULL);