8 #include <sys/signal.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);
29 #define ADD_REF(__tbl) (__tbl)->active++
30 #define SUB_REF(__tbl) --(__tbl)->active
32 #define ME() ((void *)pthread_self())
34 #define _RPMSQ_INTERNAL
35 #include <rpm/rpmsq.h>
39 static struct rpmsqElem rpmsqRock;
41 static rpmsq rpmsqQueue = &rpmsqRock;
44 * Insert node into from queue.
45 * @param elem node to link
46 * @param prev previous node from queue
47 * @return 0 on success
49 static int rpmsqInsert(void * elem, void * prev)
51 rpmsq sq = (rpmsq) elem;
55 ret = sighold(SIGCHLD);
61 sq->pipes[0] = sq->pipes[1] = -1;
64 ret = pthread_mutex_init(&sq->mutex, NULL);
65 insque(elem, (prev != NULL ? prev : rpmsqQueue));
66 ret = sigrelse(SIGCHLD);
73 * Remove node from queue.
74 * @param elem node to link
75 * @return 0 on success
77 static int rpmsqRemove(void * elem)
79 rpmsq sq = (rpmsq) elem;
83 ret = sighold (SIGCHLD);
87 /* Unlock the mutex and then destroy it */
88 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
89 ret = pthread_mutex_destroy(&sq->mutex);
92 if (sq->pipes[1]) ret = close(sq->pipes[1]);
93 if (sq->pipes[0]) ret = close(sq->pipes[0]);
94 sq->pipes[0] = sq->pipes[1] = -1;
95 ret = sigrelse(SIGCHLD);
101 static sigset_t rpmsqCaught;
103 static struct rpmsig_s {
105 rpmsqAction_t handler;
107 struct sigaction oact;
109 { SIGINT, rpmsqAction },
110 #define rpmsigTbl_sigint (&rpmsigTbl[0])
111 { SIGQUIT, rpmsqAction },
112 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
113 { SIGCHLD, rpmsqAction },
114 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
115 { SIGHUP, rpmsqAction },
116 #define rpmsigTbl_sighup (&rpmsigTbl[3])
117 { SIGTERM, rpmsqAction },
118 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
119 { SIGPIPE, rpmsqAction },
120 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
124 int rpmsqIsCaught(int signum)
126 return sigismember(&rpmsqCaught, signum);
130 void rpmsqAction(int signum, siginfo_t * info, void * context)
132 void rpmsqAction(int signum)
138 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
139 if (tbl->signum != signum)
142 (void) sigaddset(&rpmsqCaught, signum);
149 pid_t reaped = waitpid(0, &status, WNOHANG);
151 /* XXX errno set to ECHILD/EINVAL/EINTR. */
155 /* XXX insque(3)/remque(3) are dequeue, not ring. */
156 for (sq = rpmsqQueue->q_forw;
157 sq != NULL && sq != rpmsqQueue;
162 if (sq->child != reaped)
167 /* Unlock the mutex. The waiter will then be able to
170 * XXX: jbj, wtd, if this fails?
172 ret = pthread_mutex_unlock(&sq->mutex);
186 int rpmsqEnable(int signum, rpmsqAction_t handler)
188 int tblsignum = (signum >= 0 ? signum : -signum);
194 if (rpmsqQueue->id == NULL)
195 rpmsqQueue->id = ME();
196 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
197 if (tblsignum != tbl->signum)
200 if (signum >= 0) { /* Enable. */
201 if (ADD_REF(tbl) <= 0) {
202 (void) sigdelset(&rpmsqCaught, tbl->signum);
204 /* XXX Don't set a signal handler if already SIG_IGN */
205 (void) sigaction(tbl->signum, NULL, &tbl->oact);
206 if (tbl->oact.sa_handler == SIG_IGN)
209 (void) sigemptyset (&sa.sa_mask);
211 sa.sa_flags = SA_SIGINFO;
215 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
216 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
220 tbl->active = 1; /* XXX just in case */
222 tbl->handler = handler;
224 } else { /* Disable. */
225 if (SUB_REF(tbl) <= 0) {
226 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
228 tbl->active = 0; /* XXX just in case */
229 tbl->handler = (handler != NULL ? handler : rpmsqAction);
239 pid_t rpmsqFork(rpmsq sq)
243 int nothreads = 0; /* XXX: Shouldn't this be a global? */
246 xx = rpmsqInsert(sq, NULL);
247 xx = rpmsqEnable(SIGCHLD, NULL);
250 xx = pipe(sq->pipes);
252 xx = sighold(SIGCHLD);
255 * Initialize the cond var mutex. We have to aquire the lock we
256 * use for the condition before we fork. Otherwise it is possible for
257 * the child to exit, we get sigchild and the sig handler to send
258 * the condition signal before we are waiting on the condition.
261 if(pthread_mutex_lock(&sq->mutex)) {
262 /* Yack we did not get the lock, lets just give up */
263 xx = close(sq->pipes[0]);
264 xx = close(sq->pipes[1]);
265 sq->pipes[0] = sq->pipes[1] = -1;
271 if (pid < (pid_t) 0) { /* fork failed. */
272 sq->child = (pid_t)-1;
273 xx = close(sq->pipes[0]);
274 xx = close(sq->pipes[1]);
275 sq->pipes[0] = sq->pipes[1] = -1;
277 } else if (pid == (pid_t) 0) { /* Child. */
280 /* Block to permit parent time to wait. */
281 xx = close(sq->pipes[1]);
282 xx = read(sq->pipes[0], &yy, sizeof(yy));
283 xx = close(sq->pipes[0]);
284 sq->pipes[0] = sq->pipes[1] = -1;
285 } else { /* Parent. */
290 xx = sigrelse(SIGCHLD);
295 * Wait for child process to be reaped, and unregister SIGCHLD handler.
296 * @todo Rewrite to use waitpid on helper thread.
297 * @param sq scriptlet queue element
298 * @return 0 on success
300 static int rpmsqWaitUnregister(rpmsq sq)
306 /* Protect sq->reaped from handler changes. */
307 ret = sighold(SIGCHLD);
309 /* Start the child, linux often runs child before parent. */
310 if (sq->pipes[0] >= 0)
311 xx = close(sq->pipes[0]);
312 if (sq->pipes[1] >= 0)
313 xx = close(sq->pipes[1]);
314 sq->pipes[0] = sq->pipes[1] = -1;
316 /* Put a stopwatch on the time spent waiting to measure performance gain. */
317 (void) rpmswEnter(&sq->op, -1);
319 /* Wait for handler to receive SIGCHLD. */
320 while (ret == 0 && sq->reaped != sq->child) {
322 /* Note that sigpause re-enables SIGCHLD. */
323 ret = sigpause(SIGCHLD);
325 xx = sigrelse(SIGCHLD);
328 * We start before the fork with this mutex locked;
329 * The only one that unlocks this the signal handler.
330 * So if we get the lock the child has been reaped.
332 ret = pthread_mutex_lock(&sq->mutex);
333 xx = sighold(SIGCHLD);
337 /* Accumulate stopwatch time spent waiting, potential performance gain. */
338 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
340 xx = sigrelse(SIGCHLD);
342 /* Remove processed SIGCHLD item from queue. */
343 xx = rpmsqRemove(sq);
345 /* Disable SIGCHLD handler on refcount == 0. */
346 xx = rpmsqEnable(-SIGCHLD, NULL);
351 pid_t rpmsqWait(rpmsq sq)
354 (void) rpmsqWaitUnregister(sq);
359 reaped = waitpid(sq->child, &status, 0);
360 } while (reaped >= 0 && reaped != sq->child);