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 = sigrelse(SIGCHLD);
351 * Wait for child process to be reaped, and unregister SIGCHLD handler.
352 * @param sq scriptlet queue element
353 * @return 0 on success
355 static int rpmsqWaitUnregister(rpmsq sq)
356 /*@globals fileSystem, internalState @*/
357 /*@modifies sq, fileSystem, internalState @*/
364 ret = sighold(SIGCHLD);
366 ret = pthread_mutex_lock(&sq->mutex);
368 /* Start the child. */
370 if (sq->pipes[0] >= 0)
371 xx = close(sq->pipes[0]);
372 if (sq->pipes[1] >= 0)
373 xx = close(sq->pipes[1]);
374 sq->pipes[0] = sq->pipes[1] = -1;
377 (void) rpmswEnter(&sq->op, -1);
380 while (ret == 0 && sq->reaped != sq->child) {
382 ret = sigpause(SIGCHLD);
384 ret = pthread_cond_wait(&sq->cond, &sq->mutex);
388 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
391 xx = sigrelse(SIGCHLD);
393 xx = pthread_mutex_unlock(&sq->mutex);
398 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
402 xx = rpmsqRemove(sq);
403 xx = rpmsqEnable(-SIGCHLD, NULL);
407 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
414 pid_t rpmsqWait(rpmsq sq)
420 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
425 (void) rpmsqWaitUnregister(sq);
430 reaped = waitpid(sq->child, &status, 0);
431 } while (reaped >= 0 && reaped != sq->child);
437 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
445 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
452 void * rpmsqThread(void * (*start) (void * arg), void * arg)
457 ret = pthread_create(&pth, NULL, start, arg);
458 return (ret == 0 ? (void *)pth : NULL);
461 int rpmsqJoin(void * thread)
463 pthread_t pth = (pthread_t) thread;
466 return pthread_join(pth, NULL);
469 int rpmsqThreadEqual(void * thread)
471 pthread_t t1 = (pthread_t) thread;
472 pthread_t t2 = pthread_self();
473 return pthread_equal(t1, t2);
477 * SIGCHLD cancellation handler.
480 sigchld_cancel (void *arg)
481 /*@globals fileSystem, internalState @*/
482 /*@modifies fileSystem, internalState @*/
484 pid_t child = *(pid_t *) arg;
487 (void) kill(child, SIGKILL);
490 result = waitpid(child, NULL, 0);
491 } while (result == (pid_t)-1 && errno == EINTR);
494 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
495 (void) rpmsqEnable(-SIGQUIT, NULL);
496 (void) rpmsqEnable(-SIGINT, NULL);
502 * Execute a command, returning its status.
506 rpmsqExecve (const char ** argv)
512 sigset_t newMask, oldMask;
513 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
516 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
517 if (rpmsqEnable(SIGINT, NULL) < 0) {
518 SUB_REF (rpmsigTbl_sigchld);
521 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
522 SUB_REF (rpmsigTbl_sigchld);
523 goto out_restore_sigint;
528 (void) sigemptyset (&newMask);
529 (void) sigaddset (&newMask, SIGCHLD);
530 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
532 if (SUB_REF (rpmsigTbl_sigchld) == 0)
533 goto out_restore_sigquit_and_sigint;
537 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
540 if (pid < (pid_t) 0) { /* fork failed. */
542 } else if (pid == (pid_t) 0) { /* Child. */
544 /* Restore the signals. */
545 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
546 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
547 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
549 /* Reset rpmsigTbl lock and refcnt. */
552 (void) execve (argv[0], (char *const *) argv, environ);
554 } else { /* Parent. */
556 result = waitpid(pid, &status, 0);
557 } while (result == (pid_t)-1 && errno == EINTR);
562 CLEANUP_RESET(0, oldtype);
565 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
566 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
567 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
573 out_restore_sigquit_and_sigint:
574 (void) rpmsqEnable(-SIGQUIT, NULL);
576 (void) rpmsqEnable(-SIGINT, NULL);