00001
00005 #include "system.h"
00006
00007 #if defined(__LCLINT__)
00008 #define _BITS_SIGTHREAD_H
00009
00010
00011
00012
00013 extern int sighold(int sig)
00014 ;
00015 extern int sigignore(int sig)
00016 ;
00017 extern int sigpause(int sig)
00018 ;
00019 extern int sigrelse(int sig)
00020 ;
00021 extern void (*sigset(int sig, void (*disp)(int)))(int)
00022 ;
00023
00024 struct qelem;
00025 extern void insque(struct qelem * __elem, struct qelem * __prev)
00026 ;
00027 extern void remque(struct qelem * __elem)
00028 ;
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( pthread_t *restrict thread,
00036 const pthread_attr_t *restrict attr,
00037 void *(*start_routine)(void*), void *restrict arg)
00038 ;
00039 extern int pthread_join(pthread_t thread, void **value_ptr)
00040 ;
00041
00042 extern int pthread_setcancelstate(int state, int *oldstate)
00043
00044 ;
00045 extern int pthread_setcanceltype(int type, int *oldtype)
00046
00047 ;
00048 extern void pthread_testcancel(void)
00049
00050 ;
00051 extern void pthread_cleanup_pop(int execute)
00052
00053 ;
00054 extern void pthread_cleanup_push(void (*routine)(void*), void *arg)
00055
00056 ;
00057 extern void _pthread_cleanup_pop( struct _pthread_cleanup_buffer *__buffer, int execute)
00058
00059 ;
00060 extern void _pthread_cleanup_push( struct _pthread_cleanup_buffer *__buffer, void (*routine)(void*), void *arg)
00061
00062 ;
00063
00064 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
00065
00066 ;
00067 extern int pthread_mutexattr_init( pthread_mutexattr_t *attr)
00068
00069 ;
00070
00071 int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,
00072 int *restrict type)
00073 ;
00074 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
00075
00076 ;
00077
00078 extern int pthread_mutex_destroy(pthread_mutex_t *mutex)
00079 ;
00080 extern int pthread_mutex_init( pthread_mutex_t *restrict mutex,
00081 const pthread_mutexattr_t *restrict attr)
00082
00083 ;
00084
00085 extern int pthread_mutex_lock(pthread_mutex_t *mutex)
00086
00087 ;
00088 extern int pthread_mutex_trylock(pthread_mutex_t *mutex)
00089
00090 ;
00091 extern int pthread_mutex_unlock(pthread_mutex_t *mutex)
00092
00093 ;
00094
00095 extern int pthread_cond_destroy(pthread_cond_t *cond)
00096 ;
00097 extern int pthread_cond_init( pthread_cond_t *restrict cond,
00098 const pthread_condattr_t *restrict attr)
00099
00100 ;
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 ;
00106 extern int pthread_cond_wait(pthread_cond_t *restrict cond,
00107 pthread_mutex_t *restrict mutex)
00108 ;
00109 extern int pthread_cond_broadcast(pthread_cond_t *cond)
00110
00111 ;
00112 extern int pthread_cond_signal(pthread_cond_t *cond)
00113
00114 ;
00115
00116
00117
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
00130 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
00131 #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
00132 #endif
00133
00134 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
00135
00136 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
00137 #else
00138
00139
00140 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
00141
00142 #endif
00143
00144 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
00145 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
00146 #define INIT_LOCK() \
00147 { pthread_mutexattr_t attr; \
00148 (void) pthread_mutexattr_init(&attr); \
00149 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
00150 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
00151 (void) pthread_mutexattr_destroy(&attr); \
00152 rpmsigTbl_sigchld->active = 0; \
00153 }
00154 #define ADD_REF(__tbl) (__tbl)->active++
00155 #define SUB_REF(__tbl) --(__tbl)->active
00156 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
00157 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
00158 pthread_cleanup_push((__handler), (__arg));
00159 #define CLEANUP_RESET(__execute, __oldtype) \
00160 pthread_cleanup_pop(__execute); \
00161 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
00162
00163 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
00164
00165 #define ME() ((void *)pthread_self())
00166
00167 #else
00168
00169 #define DO_LOCK()
00170 #define DO_UNLOCK()
00171 #define INIT_LOCK()
00172 #define ADD_REF(__tbl) (0)
00173 #define SUB_REF(__tbl) (0)
00174 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
00175 #define CLEANUP_RESET(__execute, __oldtype)
00176
00177 #define SAME_THREAD(_a, _b) (42)
00178
00179 #define ME() (((void *)getpid()))
00180
00181 #endif
00182
00183 #include <rpmsq.h>
00184
00185 #include "debug.h"
00186
00187 #define _RPMSQ_DEBUG 0
00188
00189 int _rpmsq_debug = _RPMSQ_DEBUG;
00190
00191
00192 static struct rpmsqElem rpmsqRock;
00193
00194
00195
00196 rpmsq rpmsqQueue = &rpmsqRock;
00197
00198
00199 int rpmsqInsert(void * elem, void * prev)
00200 {
00201 rpmsq sq = (rpmsq) elem;
00202 int ret = -1;
00203
00204 if (sq != NULL) {
00205 #ifdef _RPMSQ_DEBUG
00206 if (_rpmsq_debug)
00207 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
00208 #endif
00209 ret = sighold(SIGCHLD);
00210 if (ret == 0) {
00211 sq->child = 0;
00212 sq->reaped = 0;
00213 sq->status = 0;
00214 sq->reaper = 1;
00215
00216 sq->pipes[0] = sq->pipes[1] = -1;
00217
00218
00219 sq->id = ME();
00220 ret = pthread_mutex_init(&sq->mutex, NULL);
00221 insque(elem, (prev != NULL ? prev : rpmsqQueue));
00222 ret = sigrelse(SIGCHLD);
00223 }
00224 }
00225 return ret;
00226 }
00227
00228 int rpmsqRemove(void * elem)
00229 {
00230 rpmsq sq = (rpmsq) elem;
00231 int ret = -1;
00232
00233 if (elem != NULL) {
00234
00235 #ifdef _RPMSQ_DEBUG
00236 if (_rpmsq_debug)
00237 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
00238 #endif
00239 ret = sighold (SIGCHLD);
00240 if (ret == 0) {
00241 remque(elem);
00242
00243
00244 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
00245 ret = pthread_mutex_destroy(&sq->mutex);
00246
00247 sq->id = NULL;
00248
00249 if (sq->pipes[1]) ret = close(sq->pipes[1]);
00250 if (sq->pipes[0]) ret = close(sq->pipes[0]);
00251 sq->pipes[0] = sq->pipes[1] = -1;
00252
00253 #ifdef NOTYET
00254 sq->reaper = 1;
00255 sq->status = 0;
00256 sq->reaped = 0;
00257 sq->child = 0;
00258 #endif
00259 ret = sigrelse(SIGCHLD);
00260 }
00261 }
00262 return ret;
00263 }
00264
00265
00266 sigset_t rpmsqCaught;
00267
00268
00269
00270 static struct rpmsig_s {
00271 int signum;
00272 void (*handler) (int signum, void * info, void * context);
00273 int active;
00274 struct sigaction oact;
00275 } rpmsigTbl[] = {
00276 { SIGINT, rpmsqAction },
00277 #define rpmsigTbl_sigint (&rpmsigTbl[0])
00278 { SIGQUIT, rpmsqAction },
00279 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
00280 { SIGCHLD, rpmsqAction },
00281 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
00282 { SIGHUP, rpmsqAction },
00283 #define rpmsigTbl_sighup (&rpmsigTbl[3])
00284 { SIGTERM, rpmsqAction },
00285 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
00286 { SIGPIPE, rpmsqAction },
00287 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
00288 { -1, NULL },
00289 };
00290
00291
00292 void rpmsqAction(int signum,
00293 void * info, void * context)
00294 {
00295 int save = errno;
00296 rpmsig tbl;
00297
00298 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
00299 if (tbl->signum != signum)
00300 continue;
00301
00302 (void) sigaddset(&rpmsqCaught, signum);
00303
00304 switch (signum) {
00305 case SIGCHLD:
00306 while (1) {
00307 rpmsq sq;
00308 int status = 0;
00309 pid_t reaped = waitpid(0, &status, WNOHANG);
00310
00311
00312 if (reaped <= 0)
00313 break;
00314
00315
00316 for (sq = rpmsqQueue->q_forw;
00317 sq != NULL && sq != rpmsqQueue;
00318 sq = sq->q_forw)
00319 {
00320 int ret;
00321
00322 if (sq->child != reaped)
00323 continue;
00324 sq->reaped = reaped;
00325 sq->status = status;
00326
00327
00328
00329
00330
00331
00332 ret = pthread_mutex_unlock(&sq->mutex);
00333
00334 break;
00335 }
00336 }
00337 break;
00338 default:
00339 break;
00340 }
00341 break;
00342 }
00343 errno = save;
00344 }
00345
00346 int rpmsqEnable(int signum, rpmsqAction_t handler)
00347
00348
00349 {
00350 int tblsignum = (signum >= 0 ? signum : -signum);
00351 struct sigaction sa;
00352 rpmsig tbl;
00353 int ret = -1;
00354
00355 (void) DO_LOCK ();
00356 if (rpmsqQueue->id == NULL)
00357 rpmsqQueue->id = ME();
00358 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
00359 if (tblsignum != tbl->signum)
00360 continue;
00361
00362 if (signum >= 0) {
00363 if (ADD_REF(tbl) <= 0) {
00364 (void) sigdelset(&rpmsqCaught, tbl->signum);
00365
00366
00367 (void) sigaction(tbl->signum, NULL, &tbl->oact);
00368 if (tbl->oact.sa_handler == SIG_IGN)
00369 continue;
00370
00371 (void) sigemptyset (&sa.sa_mask);
00372 sa.sa_flags = SA_SIGINFO;
00373 #if defined(__LCLINT__)
00374 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
00375 #else
00376 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
00377 #endif
00378 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
00379 SUB_REF(tbl);
00380 break;
00381 }
00382 tbl->active = 1;
00383 if (handler != NULL)
00384 tbl->handler = handler;
00385 }
00386 } else {
00387 if (SUB_REF(tbl) <= 0) {
00388 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
00389 break;
00390 tbl->active = 0;
00391 tbl->handler = (handler != NULL ? handler : rpmsqAction);
00392 }
00393 }
00394 ret = tbl->active;
00395 break;
00396 }
00397 (void) DO_UNLOCK ();
00398 return ret;
00399 }
00400
00401 pid_t rpmsqFork(rpmsq sq)
00402 {
00403 pid_t pid;
00404 int xx;
00405 int nothreads = 0;
00406
00407 if (sq->reaper) {
00408 xx = rpmsqInsert(sq, NULL);
00409 #ifdef _RPMSQ_DEBUG
00410 if (_rpmsq_debug)
00411 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
00412 #endif
00413 xx = rpmsqEnable(SIGCHLD, NULL);
00414 }
00415
00416 xx = pipe(sq->pipes);
00417
00418 xx = sighold(SIGCHLD);
00419
00420
00421
00422
00423
00424
00425
00426 if (!nothreads) {
00427 if(pthread_mutex_lock(&sq->mutex)) {
00428
00429
00430 xx = close(sq->pipes[0]);
00431 xx = close(sq->pipes[1]);
00432 sq->pipes[0] = sq->pipes[1] = -1;
00433
00434 goto out;
00435 }
00436 }
00437
00438 pid = fork();
00439 if (pid < (pid_t) 0) {
00440 sq->child = (pid_t)-1;
00441
00442 xx = close(sq->pipes[0]);
00443 xx = close(sq->pipes[1]);
00444 sq->pipes[0] = sq->pipes[1] = -1;
00445
00446 goto out;
00447 } else if (pid == (pid_t) 0) {
00448 int yy;
00449
00450
00451
00452 xx = close(sq->pipes[1]);
00453 xx = read(sq->pipes[0], &yy, sizeof(yy));
00454 xx = close(sq->pipes[0]);
00455 sq->pipes[0] = sq->pipes[1] = -1;
00456
00457
00458 #ifdef _RPMSQ_DEBUG
00459 if (_rpmsq_debug)
00460 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
00461 #endif
00462
00463 } else {
00464
00465 sq->child = pid;
00466
00467 #ifdef _RPMSQ_DEBUG
00468 if (_rpmsq_debug)
00469 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
00470 #endif
00471
00472 }
00473
00474 out:
00475 xx = sigrelse(SIGCHLD);
00476 return sq->child;
00477 }
00478
00485 static int rpmsqWaitUnregister(rpmsq sq)
00486
00487
00488 {
00489 int nothreads = 0;
00490 int ret = 0;
00491 int xx;
00492
00493
00494 ret = sighold(SIGCHLD);
00495
00496
00497
00498 if (sq->pipes[0] >= 0)
00499 xx = close(sq->pipes[0]);
00500 if (sq->pipes[1] >= 0)
00501 xx = close(sq->pipes[1]);
00502 sq->pipes[0] = sq->pipes[1] = -1;
00503
00504
00505
00506 (void) rpmswEnter(&sq->op, -1);
00507
00508
00509
00510 while (ret == 0 && sq->reaped != sq->child) {
00511 if (nothreads)
00512
00513 ret = sigpause(SIGCHLD);
00514 else {
00515 xx = sigrelse(SIGCHLD);
00516
00517
00518
00519
00520
00521
00522 ret = pthread_mutex_lock(&sq->mutex);
00523 xx = sighold(SIGCHLD);
00524 }
00525 }
00526
00527
00528
00529 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
00530
00531 xx = sigrelse(SIGCHLD);
00532
00533 #ifdef _RPMSQ_DEBUG
00534 if (_rpmsq_debug)
00535 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
00536 #endif
00537
00538
00539 xx = rpmsqRemove(sq);
00540
00541
00542 xx = rpmsqEnable(-SIGCHLD, NULL);
00543 #ifdef _RPMSQ_DEBUG
00544 if (_rpmsq_debug)
00545 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
00546 #endif
00547
00548 return ret;
00549 }
00550
00551 pid_t rpmsqWait(rpmsq sq)
00552 {
00553
00554 #ifdef _RPMSQ_DEBUG
00555 if (_rpmsq_debug)
00556 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
00557 #endif
00558
00559 if (sq->reaper) {
00560 (void) rpmsqWaitUnregister(sq);
00561 } else {
00562 pid_t reaped;
00563 int status;
00564 do {
00565 reaped = waitpid(sq->child, &status, 0);
00566 } while (reaped >= 0 && reaped != sq->child);
00567 sq->reaped = reaped;
00568 sq->status = status;
00569 #ifdef _RPMSQ_DEBUG
00570 if (_rpmsq_debug)
00571 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
00572 #endif
00573 }
00574
00575 #ifdef _RPMSQ_DEBUG
00576 if (_rpmsq_debug)
00577 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
00578 #endif
00579
00580 return sq->reaped;
00581 }
00582
00583 void * rpmsqThread(void * (*start) (void * arg), void * arg)
00584 {
00585 pthread_t pth;
00586 int ret;
00587
00588 ret = pthread_create(&pth, NULL, start, arg);
00589 return (ret == 0 ? (void *)pth : NULL);
00590 }
00591
00592 int rpmsqJoin(void * thread)
00593 {
00594 pthread_t pth = (pthread_t) thread;
00595 if (thread == NULL)
00596 return EINVAL;
00597 return pthread_join(pth, NULL);
00598 }
00599
00600 int rpmsqThreadEqual(void * thread)
00601 {
00602 pthread_t t1 = (pthread_t) thread;
00603 pthread_t t2 = pthread_self();
00604 return pthread_equal(t1, t2);
00605 }
00606
00610 static void
00611 sigchld_cancel (void *arg)
00612
00613
00614 {
00615 pid_t child = *(pid_t *) arg;
00616 pid_t result;
00617
00618 (void) kill(child, SIGKILL);
00619
00620 do {
00621 result = waitpid(child, NULL, 0);
00622 } while (result == (pid_t)-1 && errno == EINTR);
00623
00624 (void) DO_LOCK ();
00625 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
00626 (void) rpmsqEnable(-SIGQUIT, NULL);
00627 (void) rpmsqEnable(-SIGINT, NULL);
00628 }
00629 (void) DO_UNLOCK ();
00630 }
00631
00635 int
00636 rpmsqExecve (const char ** argv)
00637
00638
00639 {
00640 int oldtype;
00641 int status = -1;
00642 pid_t pid = 0;
00643 pid_t result;
00644 sigset_t newMask, oldMask;
00645 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
00646
00647 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
00648 INIT_LOCK ();
00649 #endif
00650
00651 (void) DO_LOCK ();
00652 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
00653 if (rpmsqEnable(SIGINT, NULL) < 0) {
00654 SUB_REF (rpmsigTbl_sigchld);
00655 goto out;
00656 }
00657 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
00658 SUB_REF (rpmsigTbl_sigchld);
00659 goto out_restore_sigint;
00660 }
00661 }
00662 (void) DO_UNLOCK ();
00663
00664 (void) sigemptyset (&newMask);
00665 (void) sigaddset (&newMask, SIGCHLD);
00666 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
00667 (void) DO_LOCK ();
00668 if (SUB_REF (rpmsigTbl_sigchld) == 0)
00669 goto out_restore_sigquit_and_sigint;
00670 goto out;
00671 }
00672
00673 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
00674
00675 pid = fork ();
00676 if (pid < (pid_t) 0) {
00677 goto out;
00678 } else if (pid == (pid_t) 0) {
00679
00680
00681 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
00682 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
00683 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
00684
00685
00686 INIT_LOCK ();
00687
00688 (void) execve (argv[0], (char *const *) argv, environ);
00689 _exit (127);
00690 } else {
00691 do {
00692 result = waitpid(pid, &status, 0);
00693 } while (result == (pid_t)-1 && errno == EINTR);
00694 if (result != pid)
00695 status = -1;
00696 }
00697
00698 CLEANUP_RESET(0, oldtype);
00699
00700 (void) DO_LOCK ();
00701 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
00702 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
00703 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
00704 {
00705 status = -1;
00706 }
00707 goto out;
00708
00709 out_restore_sigquit_and_sigint:
00710 (void) rpmsqEnable(-SIGQUIT, NULL);
00711 out_restore_sigint:
00712 (void) rpmsqEnable(-SIGINT, NULL);
00713 out:
00714 (void) DO_UNLOCK ();
00715 return status;
00716 }