Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

rpmsq.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #if defined(__LCLINT__)
00008 #define _BITS_SIGTHREAD_H       /* XXX avoid __sigset_t heartburn. */
00009 
00010 /*@-incondefs -protoparammatch@*/
00011 /*@-exportheader@*/
00012 /*@constant int SA_SIGINFO@*/
00013 extern int sighold(int sig)
00014         /*@globals errno, systemState @*/;
00015 extern int sigignore(int sig)
00016         /*@globals errno, systemState @*/;
00017 extern int sigpause(int sig)
00018         /*@globals errno, systemState @*/;
00019 extern int sigrelse(int sig)
00020         /*@globals errno, systemState @*/;
00021 extern void (*sigset(int sig, void (*disp)(int)))(int)
00022         /*@globals errno, systemState @*/;
00023 
00024 struct qelem;
00025 extern  void insque(struct qelem * __elem, struct qelem * __prev)
00026         /*@modifies  __elem, __prev @*/;
00027 extern  void remque(struct qelem * __elem)
00028         /*@modifies  __elem @*/;
00029 
00030 extern pthread_t pthread_self(void)
00031         /*@*/;
00032 extern int pthread_equal(pthread_t t1, pthread_t t2)
00033         /*@*/;
00034 
00035 extern int pthread_create(/*@out@*/ pthread_t *restrict thread,
00036                 const pthread_attr_t *restrict attr,
00037                 void *(*start_routine)(void*), void *restrict arg)
00038         /*@modifies *thread @*/;
00039 extern int pthread_join(pthread_t thread, /*@out@*/ void **value_ptr)
00040         /*@modifies *value_ptr @*/;
00041 
00042 extern int pthread_setcancelstate(int state, /*@out@*/ int *oldstate)
00043         /*@globals internalState @*/
00044         /*@modifies *oldstate, internalState @*/;
00045 extern int pthread_setcanceltype(int type, /*@out@*/ int *oldtype)
00046         /*@globals internalState @*/
00047         /*@modifies *oldtype, internalState @*/;
00048 extern void pthread_testcancel(void)
00049         /*@globals internalState @*/
00050         /*@modifies internalState @*/;
00051 extern void pthread_cleanup_pop(int execute)
00052         /*@globals internalState @*/
00053         /*@modifies internalState @*/;
00054 extern void pthread_cleanup_push(void (*routine)(void*), void *arg)
00055         /*@globals internalState @*/
00056         /*@modifies internalState @*/;
00057 extern void _pthread_cleanup_pop(/*@out@*/ struct _pthread_cleanup_buffer *__buffer, int execute)
00058         /*@globals internalState @*/
00059         /*@modifies internalState @*/;
00060 extern void _pthread_cleanup_push(/*@out@*/ struct _pthread_cleanup_buffer *__buffer, void (*routine)(void*), /*@out@*/ void *arg)
00061         /*@globals internalState @*/
00062         /*@modifies internalState @*/;
00063 
00064 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
00065         /*@globals errno, internalState @*/
00066         /*@modifies *attr, errno, internalState @*/;
00067 extern int pthread_mutexattr_init(/*@out@*/ pthread_mutexattr_t *attr)
00068         /*@globals errno, internalState @*/
00069         /*@modifies *attr, errno, internalState @*/;
00070 
00071 int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,
00072                 /*@out@*/ int *restrict type)
00073         /*@modifies *type @*/;
00074 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
00075         /*@globals errno, internalState @*/
00076         /*@modifies *attr, errno, internalState @*/;
00077 
00078 extern int pthread_mutex_destroy(pthread_mutex_t *mutex)
00079         /*@modifies *mutex @*/;
00080 extern int pthread_mutex_init(/*@out@*/ pthread_mutex_t *restrict mutex,
00081                 /*@null@*/ const pthread_mutexattr_t *restrict attr)
00082         /*@globals errno, internalState @*/
00083         /*@modifies *mutex, errno, internalState @*/;
00084 
00085 extern int pthread_mutex_lock(pthread_mutex_t *mutex)
00086         /*@globals errno @*/
00087         /*@modifies *mutex, errno @*/;
00088 extern int pthread_mutex_trylock(pthread_mutex_t *mutex)
00089         /*@globals errno @*/
00090         /*@modifies *mutex, errno @*/;
00091 extern int pthread_mutex_unlock(pthread_mutex_t *mutex)
00092         /*@globals errno @*/
00093         /*@modifies *mutex, errno @*/;
00094 
00095 extern int pthread_cond_destroy(pthread_cond_t *cond)
00096         /*@modifies *cond @*/;
00097 extern int pthread_cond_init(/*@out@*/ pthread_cond_t *restrict cond,
00098                 const pthread_condattr_t *restrict attr)
00099         /*@globals errno, internalState @*/
00100         /*@modifies *cond, errno, internalState @*/;
00101 
00102 extern int pthread_cond_timedwait(pthread_cond_t *restrict cond,
00103                 pthread_mutex_t *restrict mutex,
00104                 const struct timespec *restrict abstime)
00105         /*@modifies *cond, *mutex @*/;
00106 extern int pthread_cond_wait(pthread_cond_t *restrict cond,
00107                 pthread_mutex_t *restrict mutex)
00108         /*@modifies *cond, *mutex @*/;
00109 extern int pthread_cond_broadcast(pthread_cond_t *cond)
00110         /*@globals errno, internalState @*/
00111         /*@modifies *cond, errno, internalState @*/;
00112 extern int pthread_cond_signal(pthread_cond_t *cond)
00113         /*@globals errno, internalState @*/
00114         /*@modifies *cond, errno, internalState @*/;
00115 
00116 /*@=exportheader@*/
00117 /*@=incondefs =protoparammatch@*/
00118 #endif
00119 
00120 #include <signal.h>
00121 #include <sys/signal.h>
00122 #include <sys/wait.h>
00123 #include <search.h>
00124 
00125 #if defined(HAVE_PTHREAD_H)
00126 
00127 #include <pthread.h>
00128 
00129 /*@unchecked@*/
00130 /*@-type@*/
00131 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
00132 /*@=type@*/
00133 
00134 #define DO_LOCK()       pthread_mutex_lock(&rpmsigTbl_lock);
00135 #define DO_UNLOCK()     pthread_mutex_unlock(&rpmsigTbl_lock);
00136 #define INIT_LOCK()     \
00137     {   pthread_mutexattr_t attr; \
00138         (void) pthread_mutexattr_init(&attr); \
00139         (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
00140         (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
00141         (void) pthread_mutexattr_destroy(&attr); \
00142         rpmsigTbl_sigchld->active = 0; \
00143     }
00144 #define ADD_REF(__tbl)  (__tbl)->active++
00145 #define SUB_REF(__tbl)  --(__tbl)->active
00146 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
00147     (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
00148         pthread_cleanup_push((__handler), (__arg));
00149 #define CLEANUP_RESET(__execute, __oldtype) \
00150     (void) pthread_cleanup_pop(__execute); \
00151     (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
00152 
00153 #define SAME_THREAD(_a, _b)     pthread_equal(((pthread_t)_a), ((pthread_t)_b))
00154 
00155 #define ME()    ((void *)pthread_self())
00156 
00157 #else
00158 
00159 #define DO_LOCK()
00160 #define DO_UNLOCK()
00161 #define INIT_LOCK()
00162 #define ADD_REF(__tbl)  /*@-noeffect@*/ (0) /*@=noeffect@*/
00163 #define SUB_REF(__tbl)  /*@-noeffect@*/ (0) /*@=noeffect@*/
00164 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
00165 #define CLEANUP_RESET(__execute, __oldtype)
00166 
00167 #define SAME_THREAD(_a, _b)     (42)
00168 
00169 #define ME()    (((void *)getpid()))
00170 
00171 #endif  /* HAVE_PTHREAD_H */
00172 
00173 #include <rpmsq.h>
00174 
00175 #include "debug.h"
00176 
00177 #define _RPMSQ_DEBUG    0
00178 /*@unchecked@*/
00179 int _rpmsq_debug = _RPMSQ_DEBUG;
00180 
00181 /*@unchecked@*/
00182 static struct rpmsqElem rpmsqRock;
00183 
00184 /*@-compmempass@*/
00185 /*@unchecked@*/
00186 rpmsq rpmsqQueue = &rpmsqRock;
00187 /*@=compmempass@*/
00188 
00189 int rpmsqInsert(void * elem, void * prev)
00190 {
00191     rpmsq sq = (rpmsq) elem;
00192     int ret = -1;
00193 
00194     if (sq != NULL) {
00195 #ifdef _RPMSQ_DEBUG
00196 if (_rpmsq_debug)
00197 fprintf(stderr, "    Insert(%p): %p\n", ME(), sq);
00198 #endif
00199         ret = sighold(SIGCHLD);
00200         if (ret == 0) {
00201             sq->child = 0;
00202             sq->reaped = 0;
00203             sq->status = 0;
00204             sq->reaper = 1;
00205 /*@-bounds@*/
00206             sq->pipes[0] = sq->pipes[1] = -1;
00207 /*@=bounds@*/
00208 
00209             sq->id = ME();
00210             ret = pthread_mutex_init(&sq->mutex, NULL);
00211             insque(elem, (prev != NULL ? prev : rpmsqQueue));
00212             ret = sigrelse(SIGCHLD);
00213         }
00214     }
00215     return ret;
00216 }
00217 
00218 int rpmsqRemove(void * elem)
00219 {
00220     rpmsq sq = (rpmsq) elem;
00221     int ret = -1;
00222 
00223     if (elem != NULL) {
00224 
00225 #ifdef _RPMSQ_DEBUG
00226 if (_rpmsq_debug)
00227 fprintf(stderr, "    Remove(%p): %p\n", ME(), sq);
00228 #endif
00229         ret = sighold (SIGCHLD);
00230         if (ret == 0) {
00231             remque(elem);
00232            
00233             /* Unlock the mutex and then destroy it */ 
00234             if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
00235                 ret = pthread_mutex_destroy(&sq->mutex);
00236 
00237             sq->id = NULL;
00238 /*@-bounds@*/
00239             if (sq->pipes[1])   ret = close(sq->pipes[1]);
00240             if (sq->pipes[0])   ret = close(sq->pipes[0]);
00241             sq->pipes[0] = sq->pipes[1] = -1;
00242 /*@=bounds@*/
00243 #ifdef  NOTYET  /* rpmpsmWait debugging message needs */
00244             sq->reaper = 1;
00245             sq->status = 0;
00246             sq->reaped = 0;
00247             sq->child = 0;
00248 #endif
00249             ret = sigrelse(SIGCHLD);
00250         }
00251     }
00252     return ret;
00253 }
00254 
00255 /*@unchecked@*/
00256 sigset_t rpmsqCaught;
00257 
00258 /*@unchecked@*/
00259 /*@-fullinitblock@*/
00260 static struct rpmsig_s {
00261     int signum;
00262     void (*handler) (int signum, void * info, void * context);
00263     int active;
00264     struct sigaction oact;
00265 } rpmsigTbl[] = {
00266     { SIGINT,   rpmsqAction },
00267 #define rpmsigTbl_sigint        (&rpmsigTbl[0])
00268     { SIGQUIT,  rpmsqAction },
00269 #define rpmsigTbl_sigquit       (&rpmsigTbl[1])
00270     { SIGCHLD,  rpmsqAction },
00271 #define rpmsigTbl_sigchld       (&rpmsigTbl[2])
00272     { SIGHUP,   rpmsqAction },
00273 #define rpmsigTbl_sighup        (&rpmsigTbl[3])
00274     { SIGTERM,  rpmsqAction },
00275 #define rpmsigTbl_sigterm       (&rpmsigTbl[4])
00276     { SIGPIPE,  rpmsqAction },
00277 #define rpmsigTbl_sigpipe       (&rpmsigTbl[5])
00278     { -1,       NULL },
00279 };
00280 /*@=fullinitblock@*/
00281 
00282 void rpmsqAction(int signum,
00283                 /*@unused@*/ void * info, /*@unused@*/ void * context)
00284 {
00285     int save = errno;
00286     rpmsig tbl;
00287 
00288     for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
00289         if (tbl->signum != signum)
00290             continue;
00291 
00292         (void) sigaddset(&rpmsqCaught, signum);
00293 
00294         switch (signum) {
00295         case SIGCHLD:
00296             while (1) {
00297                 rpmsq sq;
00298                 int status = 0;
00299                 pid_t reaped = waitpid(0, &status, WNOHANG);
00300 
00301                 /* XXX errno set to ECHILD/EINVAL/EINTR. */
00302                 if (reaped <= 0)
00303                     /*@innerbreak@*/ break;
00304 
00305                 /* XXX insque(3)/remque(3) are dequeue, not ring. */
00306                 for (sq = rpmsqQueue->q_forw;
00307                      sq != NULL && sq != rpmsqQueue;
00308                      sq = sq->q_forw)
00309                 {
00310                     int ret;
00311 
00312                     if (sq->child != reaped)
00313                         /*@innercontinue@*/ continue;
00314                     sq->reaped = reaped;
00315                     sq->status = status;
00316 
00317                     /* Unlock the mutex.  The waiter will then be able to 
00318                      * aquire the lock.  
00319                      *
00320                      * XXX: jbj, wtd, if this fails? 
00321                      */
00322                     ret = pthread_mutex_unlock(&sq->mutex); 
00323 
00324                     /*@innerbreak@*/ break;
00325                 }
00326             }
00327             /*@switchbreak@*/ break;
00328         default:
00329             /*@switchbreak@*/ break;
00330         }
00331         break;
00332     }
00333     errno = save;
00334 }
00335 
00336 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
00337         /*@globals rpmsigTbl @*/
00338         /*@modifies rpmsigTbl @*/
00339 {
00340     int tblsignum = (signum >= 0 ? signum : -signum);
00341     struct sigaction sa;
00342     rpmsig tbl;
00343     int ret = -1;
00344 
00345     (void) DO_LOCK ();
00346     if (rpmsqQueue->id == NULL)
00347         rpmsqQueue->id = ME();
00348     for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
00349         if (tblsignum != tbl->signum)
00350             continue;
00351 
00352         if (signum >= 0) {                      /* Enable. */
00353             if (ADD_REF(tbl) <= 0) {
00354                 (void) sigdelset(&rpmsqCaught, tbl->signum);
00355 
00356                 /* XXX Don't set a signal handler if already SIG_IGN */
00357                 (void) sigaction(tbl->signum, NULL, &tbl->oact);
00358                 if (tbl->oact.sa_handler == SIG_IGN)
00359                     continue;
00360 
00361                 (void) sigemptyset (&sa.sa_mask);
00362                 sa.sa_flags = SA_SIGINFO;
00363 #if defined(__LCLINT__) /* XXX glibc has union to track handler prototype. */
00364                 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
00365 #else
00366                 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
00367 #endif
00368                 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
00369                     SUB_REF(tbl);
00370                     break;
00371                 }
00372                 tbl->active = 1;                /* XXX just in case */
00373                 if (handler != NULL)
00374                     tbl->handler = handler;
00375             }
00376         } else {                                /* Disable. */
00377             if (SUB_REF(tbl) <= 0) {
00378                 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
00379                     break;
00380                 tbl->active = 0;                /* XXX just in case */
00381                 tbl->handler = (handler != NULL ? handler : rpmsqAction);
00382             }
00383         }
00384         ret = tbl->active;
00385         break;
00386     }
00387     (void) DO_UNLOCK ();
00388     return ret;
00389 }
00390 
00391 pid_t rpmsqFork(rpmsq sq)
00392 {
00393     pid_t pid;
00394     int xx;
00395     int nothreads = 0;   /* XXX: Shouldn't this be a global? */
00396 
00397     if (sq->reaper) {
00398         xx = rpmsqInsert(sq, NULL);
00399 #ifdef _RPMSQ_DEBUG
00400 if (_rpmsq_debug)
00401 fprintf(stderr, "    Enable(%p): %p\n", ME(), sq);
00402 #endif
00403         xx = rpmsqEnable(SIGCHLD, NULL);
00404     }
00405 
00406     xx = pipe(sq->pipes);
00407 
00408     xx = sighold(SIGCHLD);
00409 
00410     /* 
00411      * Initialize the cond var mutex.   We have to aquire the lock we 
00412      * use for the condition before we fork.  Otherwise it is possible for
00413      * the child to exit, we get sigchild and the sig handler to send 
00414      * the condition signal before we are waiting on the condition.
00415      */
00416     if (!nothreads) {
00417         if(pthread_mutex_lock(&sq->mutex)) {
00418             /* Yack we did not get the lock, lets just give up */
00419 /*@-bounds@*/
00420             xx = close(sq->pipes[0]);
00421             xx = close(sq->pipes[1]);
00422             sq->pipes[0] = sq->pipes[1] = -1;
00423 /*@=bounds@*/
00424             goto out;
00425         }
00426     }
00427 
00428     pid = fork();
00429     if (pid < (pid_t) 0) {              /* fork failed.  */
00430 /*@-bounds@*/
00431         xx = close(sq->pipes[0]);
00432         xx = close(sq->pipes[1]);
00433         sq->pipes[0] = sq->pipes[1] = -1;
00434 /*@=bounds@*/
00435         goto out;
00436     } else if (pid == (pid_t) 0) {      /* Child. */
00437         int yy;
00438 
00439         /* Block to permit parent time to wait. */
00440 /*@-bounds@*/
00441         xx = close(sq->pipes[1]);
00442         xx = read(sq->pipes[0], &yy, sizeof(yy));
00443         xx = close(sq->pipes[0]);
00444         sq->pipes[0] = sq->pipes[1] = -1;
00445 /*@=bounds@*/
00446 
00447 #ifdef _RPMSQ_DEBUG
00448 if (_rpmsq_debug)
00449 fprintf(stderr, "     Child(%p): %p child %d\n", ME(), sq, getpid());
00450 #endif
00451 
00452     } else {                            /* Parent. */
00453 
00454         sq->child = pid;
00455 
00456 #ifdef _RPMSQ_DEBUG
00457 if (_rpmsq_debug)
00458 fprintf(stderr, "    Parent(%p): %p child %d\n", ME(), sq, sq->child);
00459 #endif
00460 
00461     }
00462 
00463 out:
00464     xx = sigrelse(SIGCHLD);
00465     return sq->child;
00466 }
00467 
00474 static int rpmsqWaitUnregister(rpmsq sq)
00475         /*@globals fileSystem, internalState @*/
00476         /*@modifies sq, fileSystem, internalState @*/
00477 {
00478     int nothreads = 0;
00479     int ret = 0;
00480     int xx;
00481 
00482     /* Protect sq->reaped from handler changes. */
00483     ret = sighold(SIGCHLD);
00484 
00485     /* Start the child, linux often runs child before parent. */
00486 /*@-bounds@*/
00487     if (sq->pipes[0] >= 0)
00488         xx = close(sq->pipes[0]);
00489     if (sq->pipes[1] >= 0)
00490         xx = close(sq->pipes[1]);
00491     sq->pipes[0] = sq->pipes[1] = -1;
00492 /*@=bounds@*/
00493 
00494     /* Put a stopwatch on the time spent waiting to measure performance gain. */
00495     (void) rpmswEnter(&sq->op, -1);
00496 
00497     /* Wait for handler to receive SIGCHLD. */
00498     /*@-infloops@*/
00499     while (ret == 0 && sq->reaped != sq->child) {
00500         if (nothreads)
00501             /* Note that sigpause re-enables SIGCHLD. */
00502             ret = sigpause(SIGCHLD);
00503         else {
00504             xx = sigrelse(SIGCHLD);
00505             
00506             /* 
00507              * We start before the fork with this mutex locked;
00508              * The only one that unlocks this the signal handler.
00509              * So if we get the lock the child has been reaped.
00510              */
00511             ret = pthread_mutex_lock(&sq->mutex);
00512             xx = sighold(SIGCHLD);
00513         }
00514     }
00515     /*@=infloops@*/
00516 
00517     /* Accumulate stopwatch time spent waiting, potential performance gain. */
00518     sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
00519 
00520     xx = sigrelse(SIGCHLD);
00521 
00522 #ifdef _RPMSQ_DEBUG
00523 if (_rpmsq_debug)
00524 fprintf(stderr, "      Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
00525 #endif
00526 
00527     /* Remove processed SIGCHLD item from queue. */
00528     xx = rpmsqRemove(sq);
00529 
00530     /* Disable SIGCHLD handler on refcount == 0. */
00531     xx = rpmsqEnable(-SIGCHLD, NULL);
00532 #ifdef _RPMSQ_DEBUG
00533 if (_rpmsq_debug)
00534 fprintf(stderr, "   Disable(%p): %p\n", ME(), sq);
00535 #endif
00536 
00537     return ret;
00538 }
00539 
00540 pid_t rpmsqWait(rpmsq sq)
00541 {
00542 
00543 #ifdef _RPMSQ_DEBUG
00544 if (_rpmsq_debug)
00545 fprintf(stderr, "      Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
00546 #endif
00547 
00548     if (sq->reaper) {
00549         (void) rpmsqWaitUnregister(sq);
00550     } else {
00551         pid_t reaped;
00552         int status;
00553         do {
00554             reaped = waitpid(sq->child, &status, 0);
00555         } while (reaped >= 0 && reaped != sq->child);
00556         sq->reaped = reaped;
00557         sq->status = status;
00558 #ifdef _RPMSQ_DEBUG
00559 if (_rpmsq_debug)
00560 fprintf(stderr, "   Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
00561 #endif
00562     }
00563 
00564 #ifdef _RPMSQ_DEBUG
00565 if (_rpmsq_debug)
00566 fprintf(stderr, "      Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
00567 #endif
00568 
00569     return sq->reaped;
00570 }
00571 
00572 void * rpmsqThread(void * (*start) (void * arg), void * arg)
00573 {
00574     pthread_t pth;
00575     int ret;
00576 
00577     ret = pthread_create(&pth, NULL, start, arg);
00578     return (ret == 0 ? (void *)pth : NULL);
00579 }
00580 
00581 int rpmsqJoin(void * thread)
00582 {
00583     pthread_t pth = (pthread_t) thread;
00584     if (thread == NULL)
00585         return EINVAL;
00586     return pthread_join(pth, NULL);
00587 }
00588 
00589 int rpmsqThreadEqual(void * thread)
00590 {
00591     pthread_t t1 = (pthread_t) thread;
00592     pthread_t t2 = pthread_self();
00593     return pthread_equal(t1, t2);
00594 }
00595 
00599 static void
00600 sigchld_cancel (void *arg)
00601         /*@globals rpmsigTbl, fileSystem, internalState @*/
00602         /*@modifies rpmsigTbl, fileSystem, internalState @*/
00603 {
00604     pid_t child = *(pid_t *) arg;
00605     pid_t result;
00606 
00607     (void) kill(child, SIGKILL);
00608 
00609     do {
00610         result = waitpid(child, NULL, 0);
00611     } while (result == (pid_t)-1 && errno == EINTR);
00612 
00613     (void) DO_LOCK ();
00614     if (SUB_REF (rpmsigTbl_sigchld) == 0) {
00615         (void) rpmsqEnable(-SIGQUIT, NULL);
00616         (void) rpmsqEnable(-SIGINT, NULL);
00617     }
00618     (void) DO_UNLOCK ();
00619 }
00620 
00624 int
00625 rpmsqExecve (const char ** argv)
00626         /*@globals rpmsigTbl @*/
00627         /*@modifies rpmsigTbl @*/
00628 {
00629     int oldtype;
00630     int status = -1;
00631     pid_t pid = 0;
00632     pid_t result;
00633     sigset_t newMask, oldMask;
00634     rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
00635 
00636     (void) DO_LOCK ();
00637     if (ADD_REF (rpmsigTbl_sigchld) == 0) {
00638         if (rpmsqEnable(SIGINT, NULL) < 0) {
00639             SUB_REF (rpmsigTbl_sigchld);
00640             goto out;
00641         }
00642         if (rpmsqEnable(SIGQUIT, NULL) < 0) {
00643             SUB_REF (rpmsigTbl_sigchld);
00644             goto out_restore_sigint;
00645         }
00646     }
00647     (void) DO_UNLOCK ();
00648 
00649     (void) sigemptyset (&newMask);
00650     (void) sigaddset (&newMask, SIGCHLD);
00651     if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
00652         (void) DO_LOCK ();
00653         if (SUB_REF (rpmsigTbl_sigchld) == 0)
00654             goto out_restore_sigquit_and_sigint;
00655         goto out;
00656     }
00657 
00658     CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
00659 
00660     pid = fork ();
00661     if (pid < (pid_t) 0) {              /* fork failed.  */
00662         goto out;
00663     } else if (pid == (pid_t) 0) {      /* Child. */
00664 
00665         /* Restore the signals.  */
00666         (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
00667         (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
00668         (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
00669 
00670         /* Reset rpmsigTbl lock and refcnt. */
00671         INIT_LOCK ();
00672 
00673 #if defined(__GLIBC__)
00674 
00678         {
00679            char* bypassVar = (char*) malloc(1024*sizeof(char));
00680            if (bypassVar != NULL)
00681            {
00682               snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00683               bypassVar[1023] = '\0';
00684               if (getenv(bypassVar) != NULL)
00685               {
00686                  char* bypassVal = (char*) malloc(1024*sizeof(char));
00687                  if (bypassVal != NULL)
00688                  {
00689                     snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00690                     unsetenv(bypassVar);
00691                     snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00692                     bypassVar[1023] = '\0';
00693                     putenv(bypassVar);
00694                     free(bypassVal);
00695                  }
00696                  else
00697                  {
00698                     free(bypassVar);
00699                  }
00700               }
00701            }
00702         }
00703 #endif
00704 
00705         (void) execve (argv[0], (char *const *) argv, environ);
00706         _exit (127);
00707     } else {                            /* Parent. */
00708         do {
00709             result = waitpid(pid, &status, 0);
00710         } while (result == (pid_t)-1 && errno == EINTR);
00711         if (result != pid)
00712             status = -1;
00713     }
00714 
00715     CLEANUP_RESET(0, oldtype);
00716 
00717     (void) DO_LOCK ();
00718     if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
00719         (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
00720       || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
00721     {
00722         status = -1;
00723     }
00724     goto out;
00725 
00726 out_restore_sigquit_and_sigint:
00727     (void) rpmsqEnable(-SIGQUIT, NULL);
00728 out_restore_sigint:
00729     (void) rpmsqEnable(-SIGINT, NULL);
00730 out:
00731     (void) DO_UNLOCK ();
00732     return status;
00733 }

Generated on Tue May 30 14:44:31 2006 for rpm by  doxygen 1.3.9.1