8 #include <sys/signal.h>
14 #if defined(HAVE_PTHREAD_H)
18 /* XXX suggested in bugzilla #159024 */
19 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
20 #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
23 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
24 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
29 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
30 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
31 #define ADD_REF(__tbl) (__tbl)->active++
32 #define SUB_REF(__tbl) --(__tbl)->active
34 #define ME() ((void *)pthread_self())
40 #define ADD_REF(__tbl) (0)
41 #define SUB_REF(__tbl) (0)
43 #define ME() (((void *)getpid()))
45 #endif /* HAVE_PTHREAD_H */
47 #define _RPMSQ_INTERNAL
48 #include <rpm/rpmsq.h>
52 static struct rpmsqElem rpmsqRock;
54 static rpmsq rpmsqQueue = &rpmsqRock;
57 * Insert node into from queue.
58 * @param elem node to link
59 * @param prev previous node from queue
60 * @return 0 on success
62 static int rpmsqInsert(void * elem, void * prev)
64 rpmsq sq = (rpmsq) elem;
68 ret = sighold(SIGCHLD);
74 sq->pipes[0] = sq->pipes[1] = -1;
77 ret = pthread_mutex_init(&sq->mutex, NULL);
78 insque(elem, (prev != NULL ? prev : rpmsqQueue));
79 ret = sigrelse(SIGCHLD);
86 * Remove node from queue.
87 * @param elem node to link
88 * @return 0 on success
90 static int rpmsqRemove(void * elem)
92 rpmsq sq = (rpmsq) elem;
96 ret = sighold (SIGCHLD);
100 /* Unlock the mutex and then destroy it */
101 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
102 ret = pthread_mutex_destroy(&sq->mutex);
105 if (sq->pipes[1]) ret = close(sq->pipes[1]);
106 if (sq->pipes[0]) ret = close(sq->pipes[0]);
107 sq->pipes[0] = sq->pipes[1] = -1;
108 ret = sigrelse(SIGCHLD);
114 static sigset_t rpmsqCaught;
116 static struct rpmsig_s {
118 rpmsqAction_t handler;
120 struct sigaction oact;
122 { SIGINT, rpmsqAction },
123 #define rpmsigTbl_sigint (&rpmsigTbl[0])
124 { SIGQUIT, rpmsqAction },
125 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
126 { SIGCHLD, rpmsqAction },
127 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
128 { SIGHUP, rpmsqAction },
129 #define rpmsigTbl_sighup (&rpmsigTbl[3])
130 { SIGTERM, rpmsqAction },
131 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
132 { SIGPIPE, rpmsqAction },
133 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
137 int rpmsqIsCaught(int signum)
139 return sigismember(&rpmsqCaught, signum);
143 void rpmsqAction(int signum, siginfo_t * info, void * context)
145 void rpmsqAction(int signum)
151 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
152 if (tbl->signum != signum)
155 (void) sigaddset(&rpmsqCaught, signum);
162 pid_t reaped = waitpid(0, &status, WNOHANG);
164 /* XXX errno set to ECHILD/EINVAL/EINTR. */
168 /* XXX insque(3)/remque(3) are dequeue, not ring. */
169 for (sq = rpmsqQueue->q_forw;
170 sq != NULL && sq != rpmsqQueue;
175 if (sq->child != reaped)
180 /* Unlock the mutex. The waiter will then be able to
183 * XXX: jbj, wtd, if this fails?
185 ret = pthread_mutex_unlock(&sq->mutex);
199 int rpmsqEnable(int signum, rpmsqAction_t handler)
201 int tblsignum = (signum >= 0 ? signum : -signum);
207 if (rpmsqQueue->id == NULL)
208 rpmsqQueue->id = ME();
209 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
210 if (tblsignum != tbl->signum)
213 if (signum >= 0) { /* Enable. */
214 if (ADD_REF(tbl) <= 0) {
215 (void) sigdelset(&rpmsqCaught, tbl->signum);
217 /* XXX Don't set a signal handler if already SIG_IGN */
218 (void) sigaction(tbl->signum, NULL, &tbl->oact);
219 if (tbl->oact.sa_handler == SIG_IGN)
222 (void) sigemptyset (&sa.sa_mask);
224 sa.sa_flags = SA_SIGINFO;
228 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
229 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
233 tbl->active = 1; /* XXX just in case */
235 tbl->handler = handler;
237 } else { /* Disable. */
238 if (SUB_REF(tbl) <= 0) {
239 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
241 tbl->active = 0; /* XXX just in case */
242 tbl->handler = (handler != NULL ? handler : rpmsqAction);
252 pid_t rpmsqFork(rpmsq sq)
256 int nothreads = 0; /* XXX: Shouldn't this be a global? */
259 xx = rpmsqInsert(sq, NULL);
260 xx = rpmsqEnable(SIGCHLD, NULL);
263 xx = pipe(sq->pipes);
265 xx = sighold(SIGCHLD);
268 * Initialize the cond var mutex. We have to aquire the lock we
269 * use for the condition before we fork. Otherwise it is possible for
270 * the child to exit, we get sigchild and the sig handler to send
271 * the condition signal before we are waiting on the condition.
274 if(pthread_mutex_lock(&sq->mutex)) {
275 /* Yack we did not get the lock, lets just give up */
276 xx = close(sq->pipes[0]);
277 xx = close(sq->pipes[1]);
278 sq->pipes[0] = sq->pipes[1] = -1;
284 if (pid < (pid_t) 0) { /* fork failed. */
285 sq->child = (pid_t)-1;
286 xx = close(sq->pipes[0]);
287 xx = close(sq->pipes[1]);
288 sq->pipes[0] = sq->pipes[1] = -1;
290 } else if (pid == (pid_t) 0) { /* Child. */
293 /* Block to permit parent time to wait. */
294 xx = close(sq->pipes[1]);
295 xx = read(sq->pipes[0], &yy, sizeof(yy));
296 xx = close(sq->pipes[0]);
297 sq->pipes[0] = sq->pipes[1] = -1;
298 } else { /* Parent. */
303 xx = sigrelse(SIGCHLD);
308 * Wait for child process to be reaped, and unregister SIGCHLD handler.
309 * @todo Rewrite to use waitpid on helper thread.
310 * @param sq scriptlet queue element
311 * @return 0 on success
313 static int rpmsqWaitUnregister(rpmsq sq)
319 /* Protect sq->reaped from handler changes. */
320 ret = sighold(SIGCHLD);
322 /* Start the child, linux often runs child before parent. */
323 if (sq->pipes[0] >= 0)
324 xx = close(sq->pipes[0]);
325 if (sq->pipes[1] >= 0)
326 xx = close(sq->pipes[1]);
327 sq->pipes[0] = sq->pipes[1] = -1;
329 /* Put a stopwatch on the time spent waiting to measure performance gain. */
330 (void) rpmswEnter(&sq->op, -1);
332 /* Wait for handler to receive SIGCHLD. */
333 while (ret == 0 && sq->reaped != sq->child) {
335 /* Note that sigpause re-enables SIGCHLD. */
336 ret = sigpause(SIGCHLD);
338 xx = sigrelse(SIGCHLD);
341 * We start before the fork with this mutex locked;
342 * The only one that unlocks this the signal handler.
343 * So if we get the lock the child has been reaped.
345 ret = pthread_mutex_lock(&sq->mutex);
346 xx = sighold(SIGCHLD);
350 /* Accumulate stopwatch time spent waiting, potential performance gain. */
351 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
353 xx = sigrelse(SIGCHLD);
355 /* Remove processed SIGCHLD item from queue. */
356 xx = rpmsqRemove(sq);
358 /* Disable SIGCHLD handler on refcount == 0. */
359 xx = rpmsqEnable(-SIGCHLD, NULL);
364 pid_t rpmsqWait(rpmsq sq)
367 (void) rpmsqWaitUnregister(sq);
372 reaped = waitpid(sq->child, &status, 0);
373 } while (reaped >= 0 && reaped != sq->child);