8 #if defined(__LCLINT__)
11 extern void insque(struct qelem * __elem, struct qelem * __prev)
12 /*@modifies __elem, prev @*/;
13 extern void remque(struct qelem * __elem)
14 /*@modifies __elem @*/;
19 #include <sys/signal.h>
23 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
28 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
30 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
31 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
33 { pthread_mutexattr_t attr; \
34 pthread_mutexattr_init(&attr); \
35 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
36 pthread_mutex_init (&rpmsigTbl_lock, &attr); \
37 pthread_mutexattr_destroy(&attr); \
38 rpmsigTbl_sigchld->active = 0; \
40 #define ADD_REF(__tbl) (__tbl)->active++
41 #define SUB_REF(__tbl) --(__tbl)->active
42 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
43 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr)); \
44 pthread_cleanup_push((__handler), (__arg));
45 #define CLEANUP_RESET(__execute, __oldtype) \
46 pthread_cleanup_pop(__execute); \
47 pthread_setcanceltype ((__oldtype), &(__oldtype));
49 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
51 #define ME() ((void *)pthread_self())
58 #define ADD_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
59 #define SUB_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
60 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
61 #define CLEANUP_RESET(__execute, __oldtype)
63 #define SAME_THREAD(_a, _b) (42)
65 #define ME() (((void *)getpid()))
67 #endif /* HAVE_PTHREAD_H */
73 #define _RPMSQ_DEBUG 0
75 int _rpmsq_debug = _RPMSQ_DEBUG;
78 static struct rpmsqElem rpmsqRock;
82 rpmsq rpmsqQueue = &rpmsqRock;
86 int rpmsqInsert(void * elem, /*@unused@*/ void * prev)
88 rpmsq sq = (rpmsq) elem;
95 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
98 ret = sighold(SIGCHLD);
105 sq->pipes[0] = sq->pipes[1] = -1;
108 /*@-unqualifiedtrans@*/
110 /*@=unqualifiedtrans@*/
111 ret = pthread_mutex_init(&sq->mutex, NULL);
112 ret = pthread_cond_init(&sq->cond, NULL);
113 #if !defined(__LCLINT__) /* XXX FIXME */
114 insque(elem, (prev ? prev : rpmsqQueue));
116 ret = sigrelse(SIGCHLD);
123 int rpmsqRemove(void * elem)
125 rpmsq sq = (rpmsq) elem;
133 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
136 ret = sighold (SIGCHLD);
139 ret = pthread_cond_destroy(&sq->cond);
140 ret = pthread_mutex_destroy(&sq->mutex);
143 if (sq->pipes[1]) ret = close(sq->pipes[1]);
144 if (sq->pipes[0]) ret = close(sq->pipes[0]);
145 sq->pipes[0] = sq->pipes[1] = -1;
147 #ifdef NOTYET /* rpmpsmWait debugging message needs */
153 ret = sigrelse(SIGCHLD);
160 sigset_t rpmsqCaught;
164 static struct rpmsig_s {
166 void (*handler) (int signum, void * info, void * context);
168 struct sigaction oact;
170 { SIGINT, rpmsqAction },
171 #define rpmsigTbl_sigint (&rpmsigTbl[0])
172 { SIGQUIT, rpmsqAction },
173 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
174 { SIGCHLD, rpmsqAction },
175 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
176 { SIGHUP, rpmsqAction },
177 #define rpmsigTbl_sighup (&rpmsigTbl[3])
178 { SIGTERM, rpmsqAction },
179 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
180 { SIGPIPE, rpmsqAction },
181 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
187 void rpmsqAction(int signum,
188 /*@unused@*/ void * info, /*@unused@*/ void * context)
189 /*@globals rpmsqQueue @*/
190 /*@modifies rpmsqQueue @*/
195 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
196 if (tbl->signum != signum)
199 (void) sigaddset(&rpmsqCaught, signum);
206 pid_t reaped = waitpid(0, &status, WNOHANG);
208 /* XXX errno set to ECHILD/EINVAL/EINTR. */
210 /*@innerbreak@*/ break;
212 /* XXX insque(3)/remque(3) are dequeue, not ring. */
213 for (sq = rpmsqQueue->q_forw;
214 sq != NULL && sq != rpmsqQueue;
217 if (sq->child != reaped)
218 /*@innercontinue@*/ continue;
221 #if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
222 (void) pthread_cond_signal(&sq->cond);
224 /*@innerbreak@*/ break;
227 /*@switchbreak@*/ break;
229 /*@switchbreak@*/ break;
237 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
238 /*@globals rpmsigTbl @*/
239 /*@modifies rpmsigTbl @*/
241 int tblsignum = (signum >= 0 ? signum : -signum);
247 #if !defined(__LCLINT__)
248 if (rpmsqQueue->id == NULL)
249 rpmsqQueue->id = ME();
251 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
252 if (tblsignum != tbl->signum)
255 if (signum >= 0) { /* Enable. */
256 if (ADD_REF(tbl) <= 0) {
257 (void) sigdelset(&rpmsqCaught, tbl->signum);
258 (void) sigemptyset (&sa.sa_mask);
259 /*@-compdef -type @*/
260 sa.sa_flags = SA_SIGINFO;
261 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
262 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
266 /*@=compdef =type @*/
267 tbl->active = 1; /* XXX just in case */
269 tbl->handler = handler;
271 } else { /* Disable. */
272 if (SUB_REF(tbl) <= 0) {
273 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
275 tbl->active = 0; /* XXX just in case */
276 tbl->handler = (handler != NULL ? handler : rpmsqAction);
286 pid_t rpmsqFork(rpmsq sq)
292 xx = rpmsqInsert(sq, NULL);
296 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
299 xx = rpmsqEnable(SIGCHLD, NULL);
302 xx = pipe(sq->pipes);
304 xx = sighold(SIGCHLD);
307 if (pid < (pid_t) 0) { /* fork failed. */
309 xx = close(sq->pipes[0]);
310 xx = close(sq->pipes[1]);
311 sq->pipes[0] = sq->pipes[1] = -1;
314 } else if (pid == (pid_t) 0) { /* Child. */
317 /* Block to permit parent to wait. */
319 xx = close(sq->pipes[1]);
320 xx = read(sq->pipes[0], &yy, sizeof(yy));
321 xx = close(sq->pipes[0]);
322 sq->pipes[0] = sq->pipes[1] = -1;
328 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
332 } else { /* Parent. */
339 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
346 xx = close(sq->pipes[0]);
347 xx = close(sq->pipes[1]);
348 sq->pipes[0] = sq->pipes[1] = -1;
355 xx = sigrelse(SIGCHLD);
360 * Wait for child process to be reaped, and unregister SIGCHLD handler.
361 * @param sq scriptlet queue element
362 * @return 0 on success
364 static int rpmsqWaitUnregister(rpmsq sq)
365 /*@globals fileSystem, internalState @*/
366 /*@modifies sq, fileSystem, internalState @*/
374 ret = sighold(SIGCHLD);
376 ret = pthread_mutex_lock(&sq->mutex);
378 /* Start the child. */
380 if (sq->pipes[0] >= 0)
381 xx = close(sq->pipes[0]);
382 if (sq->pipes[1] >= 0)
383 xx = close(sq->pipes[1]);
384 sq->pipes[0] = sq->pipes[1] = -1;
387 (void) rpmswNow(&sq->begin);
390 while (ret == 0 && sq->reaped != sq->child) {
392 ret = sigpause(SIGCHLD);
394 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
399 sq->msecs = rpmswDiff(rpmswNow(&end), &sq->begin)/1000;
401 sq->script_msecs += sq->msecs;
404 xx = sigrelse(SIGCHLD);
406 xx = pthread_mutex_unlock(&sq->mutex);
411 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
415 xx = rpmsqRemove(sq);
416 xx = rpmsqEnable(-SIGCHLD, NULL);
420 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
427 pid_t rpmsqWait(rpmsq sq)
433 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);
450 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
458 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
465 int rpmsqThread(void * (*start) (void * arg), void * arg)
470 ret = pthread_create(&pth, NULL, start, arg);
473 fprintf(stderr, " Thread(%p): %p\n", ME(), pth);
475 ret = pthread_join(pth, NULL);
481 * SIGCHLD cancellation handler.
484 sigchld_cancel (void *arg)
485 /*@globals fileSystem, internalState @*/
486 /*@modifies fileSystem, internalState @*/
488 pid_t child = *(pid_t *) arg;
491 (void) kill(child, SIGKILL);
494 result = waitpid(child, NULL, 0);
495 } while (result == (pid_t)-1 && errno == EINTR);
498 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
499 (void) rpmsqEnable(-SIGQUIT, NULL);
500 (void) rpmsqEnable(-SIGINT, NULL);
506 * Execute a command, returning its status.
510 rpmsqExecve (const char ** argv)
516 sigset_t newMask, oldMask;
517 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
520 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
521 if (rpmsqEnable(SIGINT, NULL) < 0) {
522 SUB_REF (rpmsigTbl_sigchld);
525 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
526 SUB_REF (rpmsigTbl_sigchld);
527 goto out_restore_sigint;
532 (void) sigemptyset (&newMask);
533 (void) sigaddset (&newMask, SIGCHLD);
534 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
536 if (SUB_REF (rpmsigTbl_sigchld) == 0)
537 goto out_restore_sigquit_and_sigint;
541 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
544 if (pid < (pid_t) 0) { /* fork failed. */
546 } else if (pid == (pid_t) 0) { /* Child. */
548 /* Restore the signals. */
549 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
550 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
551 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
553 /* Reset rpmsigTbl lock and refcnt. */
556 (void) execve (argv[0], (char *const *) argv, environ);
558 } else { /* Parent. */
560 result = waitpid(pid, &status, 0);
561 } while (result == (pid_t)-1 && errno == EINTR);
566 CLEANUP_RESET(0, oldtype);
569 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
570 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
571 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
577 out_restore_sigquit_and_sigint:
578 (void) rpmsqEnable(-SIGQUIT, NULL);
580 (void) rpmsqEnable(-SIGINT, NULL);