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
00131 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
00132
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) (0)
00163 #define SUB_REF(__tbl) (0)
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
00172
00173 #include <rpmsq.h>
00174
00175 #include "debug.h"
00176
00177 #define _RPMSQ_DEBUG 0
00178
00179 int _rpmsq_debug = _RPMSQ_DEBUG;
00180
00181
00182 static struct rpmsqElem rpmsqRock;
00183
00184
00185
00186 rpmsq rpmsqQueue = &rpmsqRock;
00187
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
00206 sq->pipes[0] = sq->pipes[1] = -1;
00207
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
00234 if((ret = pthread_mutex_unlock(&sq->mutex)) == 0)
00235 ret = pthread_mutex_destroy(&sq->mutex);
00236
00237 sq->id = NULL;
00238
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
00243 #ifdef NOTYET
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
00256 sigset_t rpmsqCaught;
00257
00258
00259
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
00281
00282 void rpmsqAction(int signum,
00283 void * info, 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
00302 if (reaped <= 0)
00303 break;
00304
00305
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 continue;
00314 sq->reaped = reaped;
00315 sq->status = status;
00316
00317
00318
00319
00320
00321
00322 ret = pthread_mutex_unlock(&sq->mutex);
00323
00324 break;
00325 }
00326 }
00327 break;
00328 default:
00329 break;
00330 }
00331 break;
00332 }
00333 errno = save;
00334 }
00335
00336 int rpmsqEnable(int signum, rpmsqAction_t handler)
00337
00338
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) {
00353 if (ADD_REF(tbl) <= 0) {
00354 (void) sigdelset(&rpmsqCaught, tbl->signum);
00355 (void) sigemptyset (&sa.sa_mask);
00356
00357 sa.sa_flags = SA_SIGINFO;
00358 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
00359 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
00360 SUB_REF(tbl);
00361 break;
00362 }
00363
00364 tbl->active = 1;
00365 if (handler != NULL)
00366 tbl->handler = handler;
00367 }
00368 } else {
00369 if (SUB_REF(tbl) <= 0) {
00370 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
00371 break;
00372 tbl->active = 0;
00373 tbl->handler = (handler != NULL ? handler : rpmsqAction);
00374 }
00375 }
00376 ret = tbl->active;
00377 break;
00378 }
00379 (void) DO_UNLOCK ();
00380 return ret;
00381 }
00382
00383 pid_t rpmsqFork(rpmsq sq)
00384 {
00385 pid_t pid;
00386 int xx;
00387 int nothreads = 0;
00388
00389 if (sq->reaper) {
00390 xx = rpmsqInsert(sq, NULL);
00391 #ifdef _RPMSQ_DEBUG
00392 if (_rpmsq_debug)
00393 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
00394 #endif
00395 xx = rpmsqEnable(SIGCHLD, NULL);
00396 }
00397
00398 xx = pipe(sq->pipes);
00399
00400 xx = sighold(SIGCHLD);
00401
00402
00403
00404
00405
00406
00407
00408 if (!nothreads) {
00409 if(pthread_mutex_lock(&sq->mutex)) {
00410
00411
00412 xx = close(sq->pipes[0]);
00413 xx = close(sq->pipes[1]);
00414 sq->pipes[0] = sq->pipes[1] = -1;
00415
00416 goto out;
00417 }
00418 }
00419
00420 pid = fork();
00421 if (pid < (pid_t) 0) {
00422
00423 xx = close(sq->pipes[0]);
00424 xx = close(sq->pipes[1]);
00425 sq->pipes[0] = sq->pipes[1] = -1;
00426
00427 goto out;
00428 } else if (pid == (pid_t) 0) {
00429 int yy;
00430
00431
00432
00433 xx = close(sq->pipes[1]);
00434 xx = read(sq->pipes[0], &yy, sizeof(yy));
00435 xx = close(sq->pipes[0]);
00436 sq->pipes[0] = sq->pipes[1] = -1;
00437
00438
00439 #ifdef _RPMSQ_DEBUG
00440 if (_rpmsq_debug)
00441 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
00442 #endif
00443
00444 } else {
00445
00446 sq->child = pid;
00447
00448 #ifdef _RPMSQ_DEBUG
00449 if (_rpmsq_debug)
00450 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
00451 #endif
00452
00453 }
00454
00455 out:
00456 xx = sigrelse(SIGCHLD);
00457 return sq->child;
00458 }
00459
00466 static int rpmsqWaitUnregister(rpmsq sq)
00467
00468
00469 {
00470 int nothreads = 0;
00471 int ret = 0;
00472 int xx;
00473
00474
00475 ret = sighold(SIGCHLD);
00476
00477
00478
00479 if (sq->pipes[0] >= 0)
00480 xx = close(sq->pipes[0]);
00481 if (sq->pipes[1] >= 0)
00482 xx = close(sq->pipes[1]);
00483 sq->pipes[0] = sq->pipes[1] = -1;
00484
00485
00486
00487 (void) rpmswEnter(&sq->op, -1);
00488
00489
00490
00491 while (ret == 0 && sq->reaped != sq->child) {
00492 if (nothreads)
00493
00494 ret = sigpause(SIGCHLD);
00495 else {
00496 xx = sigrelse(SIGCHLD);
00497
00498
00499
00500
00501
00502
00503 ret = pthread_mutex_lock(&sq->mutex);
00504 xx = sighold(SIGCHLD);
00505 }
00506 }
00507
00508
00509
00510 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
00511
00512 xx = sigrelse(SIGCHLD);
00513
00514 #ifdef _RPMSQ_DEBUG
00515 if (_rpmsq_debug)
00516 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
00517 #endif
00518
00519
00520 xx = rpmsqRemove(sq);
00521
00522
00523 xx = rpmsqEnable(-SIGCHLD, NULL);
00524 #ifdef _RPMSQ_DEBUG
00525 if (_rpmsq_debug)
00526 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
00527 #endif
00528
00529 return ret;
00530 }
00531
00532 pid_t rpmsqWait(rpmsq sq)
00533 {
00534
00535 #ifdef _RPMSQ_DEBUG
00536 if (_rpmsq_debug)
00537 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
00538 #endif
00539
00540 if (sq->reaper) {
00541 (void) rpmsqWaitUnregister(sq);
00542 } else {
00543 pid_t reaped;
00544 int status;
00545 do {
00546 reaped = waitpid(sq->child, &status, 0);
00547 } while (reaped >= 0 && reaped != sq->child);
00548 sq->reaped = reaped;
00549 sq->status = status;
00550 #ifdef _RPMSQ_DEBUG
00551 if (_rpmsq_debug)
00552 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
00553 #endif
00554 }
00555
00556 #ifdef _RPMSQ_DEBUG
00557 if (_rpmsq_debug)
00558 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
00559 #endif
00560
00561 return sq->reaped;
00562 }
00563
00564 void * rpmsqThread(void * (*start) (void * arg), void * arg)
00565 {
00566 pthread_t pth;
00567 int ret;
00568
00569 ret = pthread_create(&pth, NULL, start, arg);
00570 return (ret == 0 ? (void *)pth : NULL);
00571 }
00572
00573 int rpmsqJoin(void * thread)
00574 {
00575 pthread_t pth = (pthread_t) thread;
00576 if (thread == NULL)
00577 return EINVAL;
00578 return pthread_join(pth, NULL);
00579 }
00580
00581 int rpmsqThreadEqual(void * thread)
00582 {
00583 pthread_t t1 = (pthread_t) thread;
00584 pthread_t t2 = pthread_self();
00585 return pthread_equal(t1, t2);
00586 }
00587
00591 static void
00592 sigchld_cancel (void *arg)
00593
00594
00595 {
00596 pid_t child = *(pid_t *) arg;
00597 pid_t result;
00598
00599 (void) kill(child, SIGKILL);
00600
00601 do {
00602 result = waitpid(child, NULL, 0);
00603 } while (result == (pid_t)-1 && errno == EINTR);
00604
00605 (void) DO_LOCK ();
00606 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
00607 (void) rpmsqEnable(-SIGQUIT, NULL);
00608 (void) rpmsqEnable(-SIGINT, NULL);
00609 }
00610 (void) DO_UNLOCK ();
00611 }
00612
00616 int
00617 rpmsqExecve (const char ** argv)
00618
00619
00620 {
00621 int oldtype;
00622 int status = -1;
00623 pid_t pid = 0;
00624 pid_t result;
00625 sigset_t newMask, oldMask;
00626 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
00627
00628 (void) DO_LOCK ();
00629 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
00630 if (rpmsqEnable(SIGINT, NULL) < 0) {
00631 SUB_REF (rpmsigTbl_sigchld);
00632 goto out;
00633 }
00634 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
00635 SUB_REF (rpmsigTbl_sigchld);
00636 goto out_restore_sigint;
00637 }
00638 }
00639 (void) DO_UNLOCK ();
00640
00641 (void) sigemptyset (&newMask);
00642 (void) sigaddset (&newMask, SIGCHLD);
00643 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
00644 (void) DO_LOCK ();
00645 if (SUB_REF (rpmsigTbl_sigchld) == 0)
00646 goto out_restore_sigquit_and_sigint;
00647 goto out;
00648 }
00649
00650 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
00651
00652 pid = fork ();
00653 if (pid < (pid_t) 0) {
00654 goto out;
00655 } else if (pid == (pid_t) 0) {
00656
00657
00658 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
00659 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
00660 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
00661
00662
00663 INIT_LOCK ();
00664
00665 #if defined(__GLIBC__)
00666
00670 {
00671 char* bypassVar = (char*) malloc(1024*sizeof(char));
00672 if (bypassVar != NULL)
00673 {
00674 snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00675 bypassVar[1023] = '\0';
00676 if (getenv(bypassVar) != NULL)
00677 {
00678 char* bypassVal = (char*) malloc(1024*sizeof(char));
00679 if (bypassVal != NULL)
00680 {
00681 snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00682 unsetenv(bypassVar);
00683 snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00684 bypassVar[1023] = '\0';
00685 putenv(bypassVar);
00686 free(bypassVal);
00687 }
00688 else
00689 {
00690 free(bypassVar);
00691 }
00692 }
00693 }
00694 }
00695 #endif
00696
00697 (void) execve (argv[0], (char *const *) argv, environ);
00698 _exit (127);
00699 } else {
00700 do {
00701 result = waitpid(pid, &status, 0);
00702 } while (result == (pid_t)-1 && errno == EINTR);
00703 if (result != pid)
00704 status = -1;
00705 }
00706
00707 CLEANUP_RESET(0, oldtype);
00708
00709 (void) DO_LOCK ();
00710 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
00711 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
00712 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
00713 {
00714 status = -1;
00715 }
00716 goto out;
00717
00718 out_restore_sigquit_and_sigint:
00719 (void) rpmsqEnable(-SIGQUIT, NULL);
00720 out_restore_sigint:
00721 (void) rpmsqEnable(-SIGINT, NULL);
00722 out:
00723 (void) DO_UNLOCK ();
00724 return status;
00725 }