2003-03-17 06:36:52 +08:00
|
|
|
/** \ingroup rpmio
|
|
|
|
* \file rpmio/rpmsq.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
2003-04-03 05:16:26 +08:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <search.h>
|
|
|
|
|
2003-04-10 05:46:31 +08:00
|
|
|
#if defined(HAVE_PTHREAD_H)
|
2003-03-18 10:41:33 +08:00
|
|
|
|
2003-03-17 06:36:52 +08:00
|
|
|
#include <pthread.h>
|
2003-03-18 10:41:33 +08:00
|
|
|
|
2005-07-13 18:47:56 +08:00
|
|
|
/* XXX suggested in bugzilla #159024 */
|
|
|
|
#if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
|
|
|
|
#error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
|
|
|
|
#endif
|
|
|
|
|
2005-01-05 01:46:10 +08:00
|
|
|
#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
|
|
|
|
/*@unchecked@*/
|
|
|
|
static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
#else
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@unchecked@*/
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-type@*/
|
2003-04-03 05:16:26 +08:00
|
|
|
static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=type@*/
|
2005-01-05 01:46:10 +08:00
|
|
|
#endif
|
2003-04-03 05:16:26 +08:00
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
#define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
|
|
|
|
#define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
|
|
|
|
#define INIT_LOCK() \
|
2003-04-10 05:46:31 +08:00
|
|
|
{ pthread_mutexattr_t attr; \
|
|
|
|
(void) pthread_mutexattr_init(&attr); \
|
|
|
|
(void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
|
|
|
|
(void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
|
|
|
|
(void) pthread_mutexattr_destroy(&attr); \
|
2003-03-18 10:41:33 +08:00
|
|
|
rpmsigTbl_sigchld->active = 0; \
|
2003-04-10 05:46:31 +08:00
|
|
|
}
|
2003-03-18 10:41:33 +08:00
|
|
|
#define ADD_REF(__tbl) (__tbl)->active++
|
|
|
|
#define SUB_REF(__tbl) --(__tbl)->active
|
|
|
|
#define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
|
2003-03-18 10:41:33 +08:00
|
|
|
pthread_cleanup_push((__handler), (__arg));
|
|
|
|
#define CLEANUP_RESET(__execute, __oldtype) \
|
2005-01-05 01:46:10 +08:00
|
|
|
pthread_cleanup_pop(__execute); \
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) pthread_setcanceltype ((__oldtype), &(__oldtype));
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
#define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
|
|
|
|
|
|
|
|
#define ME() ((void *)pthread_self())
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DO_LOCK()
|
|
|
|
#define DO_UNLOCK()
|
|
|
|
#define INIT_LOCK()
|
2003-04-03 05:16:26 +08:00
|
|
|
#define ADD_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
|
|
|
|
#define SUB_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
|
2003-03-18 10:41:33 +08:00
|
|
|
#define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
|
|
|
|
#define CLEANUP_RESET(__execute, __oldtype)
|
|
|
|
|
|
|
|
#define SAME_THREAD(_a, _b) (42)
|
|
|
|
|
2003-04-03 05:16:26 +08:00
|
|
|
#define ME() (((void *)getpid()))
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
#endif /* HAVE_PTHREAD_H */
|
2003-03-17 06:36:52 +08:00
|
|
|
|
|
|
|
#include <rpmsq.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
#define _RPMSQ_DEBUG 0
|
|
|
|
/*@unchecked@*/
|
|
|
|
int _rpmsq_debug = _RPMSQ_DEBUG;
|
|
|
|
|
2003-03-17 06:36:52 +08:00
|
|
|
/*@unchecked@*/
|
|
|
|
static struct rpmsqElem rpmsqRock;
|
2003-04-03 05:16:26 +08:00
|
|
|
|
|
|
|
/*@-compmempass@*/
|
2003-03-17 06:36:52 +08:00
|
|
|
/*@unchecked@*/
|
|
|
|
rpmsq rpmsqQueue = &rpmsqRock;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=compmempass@*/
|
2003-03-17 06:36:52 +08:00
|
|
|
|
2003-04-10 05:46:31 +08:00
|
|
|
int rpmsqInsert(void * elem, void * prev)
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
2003-03-18 10:41:33 +08:00
|
|
|
rpmsq sq = (rpmsq) elem;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (sq != NULL) {
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
|
|
|
|
#endif
|
2003-03-19 11:00:02 +08:00
|
|
|
ret = sighold(SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
sq->child = 0;
|
|
|
|
sq->reaped = 0;
|
|
|
|
sq->status = 0;
|
2005-02-14 09:34:12 +08:00
|
|
|
sq->reaper = 1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@-bounds@*/
|
2003-03-19 11:00:02 +08:00
|
|
|
sq->pipes[0] = sq->pipes[1] = -1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=bounds@*/
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
sq->id = ME();
|
2003-03-19 11:00:02 +08:00
|
|
|
ret = pthread_mutex_init(&sq->mutex, NULL);
|
2003-04-10 05:46:31 +08:00
|
|
|
insque(elem, (prev != NULL ? prev : rpmsqQueue));
|
2003-03-19 11:00:02 +08:00
|
|
|
ret = sigrelse(SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
}
|
|
|
|
}
|
2003-03-19 11:00:02 +08:00
|
|
|
return ret;
|
2003-03-17 06:36:52 +08:00
|
|
|
}
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
int rpmsqRemove(void * elem)
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
2003-03-18 10:41:33 +08:00
|
|
|
rpmsq sq = (rpmsq) elem;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (elem != NULL) {
|
|
|
|
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
|
|
|
|
#endif
|
2003-03-19 11:00:02 +08:00
|
|
|
ret = sighold (SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
remque(elem);
|
2007-02-22 20:26:56 +08:00
|
|
|
|
|
|
|
/* Unlock the mutex and then destroy it */
|
|
|
|
if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
|
|
|
|
ret = pthread_mutex_destroy(&sq->mutex);
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
sq->id = NULL;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@-bounds@*/
|
|
|
|
if (sq->pipes[1]) ret = close(sq->pipes[1]);
|
|
|
|
if (sq->pipes[0]) ret = close(sq->pipes[0]);
|
2003-03-19 11:00:02 +08:00
|
|
|
sq->pipes[0] = sq->pipes[1] = -1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=bounds@*/
|
2003-03-21 07:42:05 +08:00
|
|
|
#ifdef NOTYET /* rpmpsmWait debugging message needs */
|
2003-03-19 11:00:02 +08:00
|
|
|
sq->reaper = 1;
|
2003-03-18 10:41:33 +08:00
|
|
|
sq->status = 0;
|
2003-03-19 11:00:02 +08:00
|
|
|
sq->reaped = 0;
|
|
|
|
sq->child = 0;
|
2003-03-21 07:42:05 +08:00
|
|
|
#endif
|
2003-03-19 11:00:02 +08:00
|
|
|
ret = sigrelse(SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2003-03-17 06:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*@unchecked@*/
|
|
|
|
sigset_t rpmsqCaught;
|
|
|
|
|
|
|
|
/*@unchecked@*/
|
|
|
|
/*@-fullinitblock@*/
|
|
|
|
static struct rpmsig_s {
|
|
|
|
int signum;
|
2003-04-03 05:16:26 +08:00
|
|
|
void (*handler) (int signum, void * info, void * context);
|
2003-03-17 06:36:52 +08:00
|
|
|
int active;
|
|
|
|
struct sigaction oact;
|
|
|
|
} rpmsigTbl[] = {
|
2003-03-19 11:00:02 +08:00
|
|
|
{ SIGINT, rpmsqAction },
|
2003-03-17 06:36:52 +08:00
|
|
|
#define rpmsigTbl_sigint (&rpmsigTbl[0])
|
2003-03-19 11:00:02 +08:00
|
|
|
{ SIGQUIT, rpmsqAction },
|
2003-03-17 06:36:52 +08:00
|
|
|
#define rpmsigTbl_sigquit (&rpmsigTbl[1])
|
2003-03-19 11:00:02 +08:00
|
|
|
{ SIGCHLD, rpmsqAction },
|
2003-03-17 06:36:52 +08:00
|
|
|
#define rpmsigTbl_sigchld (&rpmsigTbl[2])
|
2003-03-19 11:00:02 +08:00
|
|
|
{ SIGHUP, rpmsqAction },
|
2003-03-17 06:36:52 +08:00
|
|
|
#define rpmsigTbl_sighup (&rpmsigTbl[3])
|
2003-03-19 11:00:02 +08:00
|
|
|
{ SIGTERM, rpmsqAction },
|
2003-03-17 06:36:52 +08:00
|
|
|
#define rpmsigTbl_sigterm (&rpmsigTbl[4])
|
2003-03-19 11:00:02 +08:00
|
|
|
{ SIGPIPE, rpmsqAction },
|
2003-03-17 06:36:52 +08:00
|
|
|
#define rpmsigTbl_sigpipe (&rpmsigTbl[5])
|
|
|
|
{ -1, NULL },
|
|
|
|
};
|
|
|
|
/*@=fullinitblock@*/
|
|
|
|
|
2003-04-03 05:16:26 +08:00
|
|
|
void rpmsqAction(int signum,
|
|
|
|
/*@unused@*/ void * info, /*@unused@*/ void * context)
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
2003-03-18 10:41:33 +08:00
|
|
|
int save = errno;
|
2003-03-17 06:36:52 +08:00
|
|
|
rpmsig tbl;
|
|
|
|
|
|
|
|
for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
|
|
|
|
if (tbl->signum != signum)
|
|
|
|
continue;
|
|
|
|
|
2004-10-14 23:09:07 +08:00
|
|
|
(void) sigaddset(&rpmsqCaught, signum);
|
2003-03-17 06:36:52 +08:00
|
|
|
|
|
|
|
switch (signum) {
|
|
|
|
case SIGCHLD:
|
|
|
|
while (1) {
|
|
|
|
rpmsq sq;
|
|
|
|
int status = 0;
|
|
|
|
pid_t reaped = waitpid(0, &status, WNOHANG);
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
/* XXX errno set to ECHILD/EINVAL/EINTR. */
|
2003-03-17 06:36:52 +08:00
|
|
|
if (reaped <= 0)
|
|
|
|
/*@innerbreak@*/ break;
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
/* XXX insque(3)/remque(3) are dequeue, not ring. */
|
2003-03-17 06:36:52 +08:00
|
|
|
for (sq = rpmsqQueue->q_forw;
|
|
|
|
sq != NULL && sq != rpmsqQueue;
|
|
|
|
sq = sq->q_forw)
|
|
|
|
{
|
2007-02-22 20:26:56 +08:00
|
|
|
int ret;
|
|
|
|
|
2003-03-17 06:36:52 +08:00
|
|
|
if (sq->child != reaped)
|
|
|
|
/*@innercontinue@*/ continue;
|
|
|
|
sq->reaped = reaped;
|
|
|
|
sq->status = status;
|
2007-02-22 20:26:56 +08:00
|
|
|
|
|
|
|
/* Unlock the mutex. The waiter will then be able to
|
|
|
|
* aquire the lock.
|
|
|
|
*
|
|
|
|
* XXX: jbj, wtd, if this fails?
|
|
|
|
*/
|
|
|
|
ret = pthread_mutex_unlock(&sq->mutex);
|
|
|
|
|
2003-03-17 06:36:52 +08:00
|
|
|
/*@innerbreak@*/ break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
default:
|
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-03-18 10:41:33 +08:00
|
|
|
errno = save;
|
2003-03-17 06:36:52 +08:00
|
|
|
}
|
|
|
|
|
2003-03-19 11:00:02 +08:00
|
|
|
int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@globals rpmsigTbl @*/
|
|
|
|
/*@modifies rpmsigTbl @*/
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
|
|
|
int tblsignum = (signum >= 0 ? signum : -signum);
|
|
|
|
struct sigaction sa;
|
|
|
|
rpmsig tbl;
|
|
|
|
int ret = -1;
|
|
|
|
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_LOCK ();
|
2003-03-18 10:41:33 +08:00
|
|
|
if (rpmsqQueue->id == NULL)
|
|
|
|
rpmsqQueue->id = ME();
|
2003-03-17 06:36:52 +08:00
|
|
|
for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
|
|
|
|
if (tblsignum != tbl->signum)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (signum >= 0) { /* Enable. */
|
|
|
|
if (ADD_REF(tbl) <= 0) {
|
|
|
|
(void) sigdelset(&rpmsqCaught, tbl->signum);
|
2004-10-14 23:09:07 +08:00
|
|
|
|
|
|
|
/* XXX Don't set a signal handler if already SIG_IGN */
|
|
|
|
(void) sigaction(tbl->signum, NULL, &tbl->oact);
|
|
|
|
if (tbl->oact.sa_handler == SIG_IGN)
|
|
|
|
continue;
|
|
|
|
|
2003-04-03 05:16:26 +08:00
|
|
|
(void) sigemptyset (&sa.sa_mask);
|
2003-03-19 11:00:02 +08:00
|
|
|
sa.sa_flags = SA_SIGINFO;
|
2007-07-10 18:34:17 +08:00
|
|
|
sa.sa_sigaction = (void*)(handler != NULL ? handler : tbl->handler);
|
2003-03-17 06:36:52 +08:00
|
|
|
if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
|
|
|
|
SUB_REF(tbl);
|
|
|
|
break;
|
|
|
|
}
|
2003-03-19 11:00:02 +08:00
|
|
|
tbl->active = 1; /* XXX just in case */
|
|
|
|
if (handler != NULL)
|
|
|
|
tbl->handler = handler;
|
2003-03-17 06:36:52 +08:00
|
|
|
}
|
|
|
|
} else { /* Disable. */
|
|
|
|
if (SUB_REF(tbl) <= 0) {
|
|
|
|
if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
|
|
|
|
break;
|
2003-03-19 11:00:02 +08:00
|
|
|
tbl->active = 0; /* XXX just in case */
|
|
|
|
tbl->handler = (handler != NULL ? handler : rpmsqAction);
|
2003-03-17 06:36:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = tbl->active;
|
|
|
|
break;
|
|
|
|
}
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_UNLOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
pid_t rpmsqFork(rpmsq sq)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int xx;
|
2007-02-22 20:26:56 +08:00
|
|
|
int nothreads = 0; /* XXX: Shouldn't this be a global? */
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
if (sq->reaper) {
|
|
|
|
xx = rpmsqInsert(sq, NULL);
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
|
|
|
|
#endif
|
|
|
|
xx = rpmsqEnable(SIGCHLD, NULL);
|
|
|
|
}
|
|
|
|
|
2003-03-19 11:00:02 +08:00
|
|
|
xx = pipe(sq->pipes);
|
2003-03-18 10:41:33 +08:00
|
|
|
|
2003-03-19 11:00:02 +08:00
|
|
|
xx = sighold(SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
|
2007-02-22 20:26:56 +08:00
|
|
|
/*
|
|
|
|
* Initialize the cond var mutex. We have to aquire the lock we
|
|
|
|
* use for the condition before we fork. Otherwise it is possible for
|
|
|
|
* the child to exit, we get sigchild and the sig handler to send
|
|
|
|
* the condition signal before we are waiting on the condition.
|
|
|
|
*/
|
|
|
|
if (!nothreads) {
|
|
|
|
if(pthread_mutex_lock(&sq->mutex)) {
|
|
|
|
/* Yack we did not get the lock, lets just give up */
|
|
|
|
/*@-bounds@*/
|
|
|
|
xx = close(sq->pipes[0]);
|
|
|
|
xx = close(sq->pipes[1]);
|
|
|
|
sq->pipes[0] = sq->pipes[1] = -1;
|
|
|
|
/*@=bounds@*/
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-18 10:41:33 +08:00
|
|
|
pid = fork();
|
|
|
|
if (pid < (pid_t) 0) { /* fork failed. */
|
2007-04-16 19:49:38 +08:00
|
|
|
sq->child = (pid_t)-1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@-bounds@*/
|
2003-03-19 11:00:02 +08:00
|
|
|
xx = close(sq->pipes[0]);
|
|
|
|
xx = close(sq->pipes[1]);
|
|
|
|
sq->pipes[0] = sq->pipes[1] = -1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=bounds@*/
|
2003-03-18 10:41:33 +08:00
|
|
|
goto out;
|
|
|
|
} else if (pid == (pid_t) 0) { /* Child. */
|
|
|
|
int yy;
|
|
|
|
|
2004-03-08 05:11:27 +08:00
|
|
|
/* Block to permit parent time to wait. */
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@-bounds@*/
|
2003-03-19 11:00:02 +08:00
|
|
|
xx = close(sq->pipes[1]);
|
|
|
|
xx = read(sq->pipes[0], &yy, sizeof(yy));
|
|
|
|
xx = close(sq->pipes[0]);
|
|
|
|
sq->pipes[0] = sq->pipes[1] = -1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=bounds@*/
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} else { /* Parent. */
|
|
|
|
|
|
|
|
sq->child = pid;
|
|
|
|
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
|
2003-03-25 01:25:42 +08:00
|
|
|
#endif
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2003-03-19 11:00:02 +08:00
|
|
|
xx = sigrelse(SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
return sq->child;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for child process to be reaped, and unregister SIGCHLD handler.
|
2004-03-08 05:11:27 +08:00
|
|
|
* @todo Rewrite to use waitpid on helper thread.
|
2003-03-18 10:41:33 +08:00
|
|
|
* @param sq scriptlet queue element
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
|
|
|
static int rpmsqWaitUnregister(rpmsq sq)
|
|
|
|
/*@globals fileSystem, internalState @*/
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@modifies sq, fileSystem, internalState @*/
|
2003-03-18 10:41:33 +08:00
|
|
|
{
|
2004-03-08 05:11:27 +08:00
|
|
|
int nothreads = 0;
|
2003-03-18 10:41:33 +08:00
|
|
|
int ret = 0;
|
|
|
|
int xx;
|
|
|
|
|
2004-03-08 05:11:27 +08:00
|
|
|
/* Protect sq->reaped from handler changes. */
|
2004-03-07 20:50:36 +08:00
|
|
|
ret = sighold(SIGCHLD);
|
2004-03-08 05:11:27 +08:00
|
|
|
|
|
|
|
/* Start the child, linux often runs child before parent. */
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@-bounds@*/
|
2003-03-25 01:25:42 +08:00
|
|
|
if (sq->pipes[0] >= 0)
|
|
|
|
xx = close(sq->pipes[0]);
|
|
|
|
if (sq->pipes[1] >= 0)
|
|
|
|
xx = close(sq->pipes[1]);
|
|
|
|
sq->pipes[0] = sq->pipes[1] = -1;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=bounds@*/
|
2003-03-25 01:25:42 +08:00
|
|
|
|
2004-03-08 05:11:27 +08:00
|
|
|
/* Put a stopwatch on the time spent waiting to measure performance gain. */
|
2003-04-03 07:26:06 +08:00
|
|
|
(void) rpmswEnter(&sq->op, -1);
|
2003-03-25 08:22:23 +08:00
|
|
|
|
2004-03-08 05:11:27 +08:00
|
|
|
/* Wait for handler to receive SIGCHLD. */
|
2003-03-18 10:41:33 +08:00
|
|
|
/*@-infloops@*/
|
2004-03-08 05:11:27 +08:00
|
|
|
while (ret == 0 && sq->reaped != sq->child) {
|
|
|
|
if (nothreads)
|
|
|
|
/* Note that sigpause re-enables SIGCHLD. */
|
2003-03-19 11:00:02 +08:00
|
|
|
ret = sigpause(SIGCHLD);
|
2004-03-07 20:50:36 +08:00
|
|
|
else {
|
|
|
|
xx = sigrelse(SIGCHLD);
|
2007-02-22 20:26:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We start before the fork with this mutex locked;
|
|
|
|
* The only one that unlocks this the signal handler.
|
|
|
|
* So if we get the lock the child has been reaped.
|
|
|
|
*/
|
|
|
|
ret = pthread_mutex_lock(&sq->mutex);
|
2004-03-07 20:50:36 +08:00
|
|
|
xx = sighold(SIGCHLD);
|
|
|
|
}
|
2004-03-08 05:11:27 +08:00
|
|
|
}
|
2003-03-18 10:41:33 +08:00
|
|
|
/*@=infloops@*/
|
|
|
|
|
2004-03-08 05:11:27 +08:00
|
|
|
/* Accumulate stopwatch time spent waiting, potential performance gain. */
|
2003-04-03 07:26:06 +08:00
|
|
|
sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
|
2003-03-25 08:22:23 +08:00
|
|
|
|
2004-03-07 20:50:36 +08:00
|
|
|
xx = sigrelse(SIGCHLD);
|
2003-03-18 10:41:33 +08:00
|
|
|
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
|
|
|
|
#endif
|
|
|
|
|
2004-03-08 05:11:27 +08:00
|
|
|
/* Remove processed SIGCHLD item from queue. */
|
2003-03-18 10:41:33 +08:00
|
|
|
xx = rpmsqRemove(sq);
|
2004-03-08 05:11:27 +08:00
|
|
|
|
|
|
|
/* Disable SIGCHLD handler on refcount == 0. */
|
2003-03-18 10:41:33 +08:00
|
|
|
xx = rpmsqEnable(-SIGCHLD, NULL);
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t rpmsqWait(rpmsq sq)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
2003-03-19 11:00:02 +08:00
|
|
|
fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
|
2003-03-18 10:41:33 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (sq->reaper) {
|
|
|
|
(void) rpmsqWaitUnregister(sq);
|
|
|
|
} else {
|
|
|
|
pid_t reaped;
|
|
|
|
int status;
|
|
|
|
do {
|
|
|
|
reaped = waitpid(sq->child, &status, 0);
|
|
|
|
} while (reaped >= 0 && reaped != sq->child);
|
|
|
|
sq->reaped = reaped;
|
|
|
|
sq->status = status;
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _RPMSQ_DEBUG
|
|
|
|
if (_rpmsq_debug)
|
|
|
|
fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return sq->reaped;
|
|
|
|
}
|
|
|
|
|
2003-04-07 20:05:35 +08:00
|
|
|
void * rpmsqThread(void * (*start) (void * arg), void * arg)
|
2003-03-19 11:00:02 +08:00
|
|
|
{
|
|
|
|
pthread_t pth;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pthread_create(&pth, NULL, start, arg);
|
2003-04-07 20:05:35 +08:00
|
|
|
return (ret == 0 ? (void *)pth : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpmsqJoin(void * thread)
|
|
|
|
{
|
|
|
|
pthread_t pth = (pthread_t) thread;
|
|
|
|
if (thread == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
return pthread_join(pth, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpmsqThreadEqual(void * thread)
|
|
|
|
{
|
|
|
|
pthread_t t1 = (pthread_t) thread;
|
|
|
|
pthread_t t2 = pthread_self();
|
|
|
|
return pthread_equal(t1, t2);
|
2003-03-19 11:00:02 +08:00
|
|
|
}
|
|
|
|
|
2003-03-17 06:36:52 +08:00
|
|
|
/**
|
|
|
|
* SIGCHLD cancellation handler.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sigchld_cancel (void *arg)
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@globals rpmsigTbl, fileSystem, internalState @*/
|
|
|
|
/*@modifies rpmsigTbl, fileSystem, internalState @*/
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
|
|
|
pid_t child = *(pid_t *) arg;
|
|
|
|
pid_t result;
|
|
|
|
|
|
|
|
(void) kill(child, SIGKILL);
|
|
|
|
|
|
|
|
do {
|
|
|
|
result = waitpid(child, NULL, 0);
|
|
|
|
} while (result == (pid_t)-1 && errno == EINTR);
|
|
|
|
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_LOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
if (SUB_REF (rpmsigTbl_sigchld) == 0) {
|
|
|
|
(void) rpmsqEnable(-SIGQUIT, NULL);
|
|
|
|
(void) rpmsqEnable(-SIGINT, NULL);
|
|
|
|
}
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_UNLOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a command, returning its status.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rpmsqExecve (const char ** argv)
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@globals rpmsigTbl @*/
|
|
|
|
/*@modifies rpmsigTbl @*/
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
|
|
|
int oldtype;
|
|
|
|
int status = -1;
|
2003-04-10 05:46:31 +08:00
|
|
|
pid_t pid = 0;
|
2003-03-17 06:36:52 +08:00
|
|
|
pid_t result;
|
|
|
|
sigset_t newMask, oldMask;
|
2003-03-18 10:41:33 +08:00
|
|
|
rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
|
2003-03-17 06:36:52 +08:00
|
|
|
|
2005-01-05 01:46:10 +08:00
|
|
|
#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
|
|
|
|
INIT_LOCK ();
|
|
|
|
#endif
|
|
|
|
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_LOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
if (ADD_REF (rpmsigTbl_sigchld) == 0) {
|
|
|
|
if (rpmsqEnable(SIGINT, NULL) < 0) {
|
|
|
|
SUB_REF (rpmsigTbl_sigchld);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (rpmsqEnable(SIGQUIT, NULL) < 0) {
|
|
|
|
SUB_REF (rpmsigTbl_sigchld);
|
|
|
|
goto out_restore_sigint;
|
|
|
|
}
|
|
|
|
}
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_UNLOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
|
2003-04-03 05:16:26 +08:00
|
|
|
(void) sigemptyset (&newMask);
|
|
|
|
(void) sigaddset (&newMask, SIGCHLD);
|
2003-03-17 06:36:52 +08:00
|
|
|
if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_LOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
if (SUB_REF (rpmsigTbl_sigchld) == 0)
|
|
|
|
goto out_restore_sigquit_and_sigint;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
|
|
|
|
|
|
|
|
pid = fork ();
|
|
|
|
if (pid < (pid_t) 0) { /* fork failed. */
|
|
|
|
goto out;
|
|
|
|
} else if (pid == (pid_t) 0) { /* Child. */
|
|
|
|
|
|
|
|
/* Restore the signals. */
|
|
|
|
(void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
|
|
|
|
(void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
|
|
|
|
(void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
|
|
|
|
|
|
|
|
/* Reset rpmsigTbl lock and refcnt. */
|
|
|
|
INIT_LOCK ();
|
|
|
|
|
|
|
|
(void) execve (argv[0], (char *const *) argv, environ);
|
|
|
|
_exit (127);
|
|
|
|
} else { /* Parent. */
|
|
|
|
do {
|
|
|
|
result = waitpid(pid, &status, 0);
|
|
|
|
} while (result == (pid_t)-1 && errno == EINTR);
|
|
|
|
if (result != pid)
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP_RESET(0, oldtype);
|
|
|
|
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_LOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
|
|
|
|
(rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
|
2003-03-18 10:41:33 +08:00
|
|
|
|| sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
|
2003-03-17 06:36:52 +08:00
|
|
|
{
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out_restore_sigquit_and_sigint:
|
|
|
|
(void) rpmsqEnable(-SIGQUIT, NULL);
|
|
|
|
out_restore_sigint:
|
|
|
|
(void) rpmsqEnable(-SIGINT, NULL);
|
|
|
|
out:
|
2003-04-10 05:46:31 +08:00
|
|
|
(void) DO_UNLOCK ();
|
2003-03-17 06:36:52 +08:00
|
|
|
return status;
|
|
|
|
}
|