7 #if defined(__LCLINT__)
8 #define _BITS_SIGTHREAD_H
13 extern int sighold(
int sig)
15 extern int sigignore(
int sig)
17 extern int sigpause(
int sig)
19 extern int sigrelse(
int sig)
21 extern void (*sigset(
int sig,
void (*disp)(
int)))(int)
25 extern void insque(
struct qelem * __elem,
struct qelem * __prev)
27 extern void remque(
struct qelem * __elem)
30 extern pthread_t pthread_self(
void)
32 extern int pthread_equal(pthread_t t1, pthread_t t2)
35 extern int pthread_create( pthread_t *restrict thread,
36 const pthread_attr_t *restrict attr,
37 void *(*start_routine)(
void*),
void *restrict arg)
39 extern int pthread_join(pthread_t thread,
void **value_ptr)
42 extern int pthread_setcancelstate(
int state,
int *oldstate)
45 extern int pthread_setcanceltype(
int type,
int *oldtype)
48 extern void pthread_testcancel(
void)
51 extern void pthread_cleanup_pop(
int execute)
54 extern void pthread_cleanup_push(
void (*routine)(
void*),
void *arg)
57 extern void _pthread_cleanup_pop(
struct _pthread_cleanup_buffer *__buffer,
int execute)
60 extern void _pthread_cleanup_push(
struct _pthread_cleanup_buffer *__buffer,
void (*routine)(
void*),
void *arg)
64 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
67 extern int pthread_mutexattr_init( pthread_mutexattr_t *attr)
71 int pthread_mutexattr_gettype(
const pthread_mutexattr_t *restrict attr,
74 int pthread_mutexattr_settype(pthread_mutexattr_t *attr,
int type)
78 extern int pthread_mutex_destroy(pthread_mutex_t *mutex)
80 extern int pthread_mutex_init( pthread_mutex_t *restrict mutex,
81 const pthread_mutexattr_t *restrict attr)
85 extern int pthread_mutex_lock(pthread_mutex_t *mutex)
88 extern int pthread_mutex_trylock(pthread_mutex_t *mutex)
91 extern int pthread_mutex_unlock(pthread_mutex_t *mutex)
95 extern int pthread_cond_destroy(pthread_cond_t *
cond)
97 extern int pthread_cond_init( pthread_cond_t *restrict
cond,
98 const pthread_condattr_t *restrict attr)
102 extern int pthread_cond_timedwait(pthread_cond_t *restrict
cond,
103 pthread_mutex_t *restrict mutex,
104 const struct timespec *restrict abstime)
106 extern int pthread_cond_wait(pthread_cond_t *restrict
cond,
107 pthread_mutex_t *restrict mutex)
109 extern int pthread_cond_broadcast(pthread_cond_t *
cond)
112 extern int pthread_cond_signal(pthread_cond_t *
cond)
121 #include <sys/signal.h>
122 #include <sys/wait.h>
125 #if defined(HAVE_PTHREAD_H)
130 #if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
131 #error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
134 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
136 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
140 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
144 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
145 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
146 #define INIT_LOCK() \
147 { pthread_mutexattr_t attr; \
148 (void) pthread_mutexattr_init(&attr); \
149 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
150 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
151 (void) pthread_mutexattr_destroy(&attr); \
152 rpmsigTbl_sigchld->active = 0; \
154 #define ADD_REF(__tbl) (__tbl)->active++
155 #define SUB_REF(__tbl) --(__tbl)->active
156 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
157 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
158 pthread_cleanup_push((__handler), (__arg));
159 #define CLEANUP_RESET(__execute, __oldtype) \
160 pthread_cleanup_pop(__execute); \
161 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
163 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
165 #define ME() ((void *)pthread_self())
172 #define ADD_REF(__tbl) (0)
173 #define SUB_REF(__tbl) (0)
174 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
175 #define CLEANUP_RESET(__execute, __oldtype)
177 #define SAME_THREAD(_a, _b) (42)
179 #define ME() (((void *)getpid()))
187 #define _RPMSQ_DEBUG 0
207 fprintf(stderr,
" Insert(%p): %p\n",
ME(), sq);
209 ret = sighold(SIGCHLD);
221 insque(elem, (prev != NULL ? prev : rpmsqQueue));
222 ret = sigrelse(SIGCHLD);
237 fprintf(stderr,
" Remove(%p): %p\n",
ME(), sq);
239 ret = sighold (SIGCHLD);
244 if (sq->
pipes[1] > 0) ret = close(sq->
pipes[1]);
245 if (sq->
pipes[0] > 0) ret = close(sq->
pipes[0]);
253 ret = sigrelse(SIGCHLD);
271 #define rpmsigTbl_sigint (&rpmsigTbl[0])
273 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
275 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
277 #define rpmsigTbl_sighup (&rpmsigTbl[3])
279 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
281 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
287 void * info,
void * context)
293 if (tbl->
signum != signum)
296 (void) sigaddset(&rpmsqCaught, signum);
303 pid_t reaped = waitpid(0, &status, WNOHANG);
310 for (sq = rpmsqQueue->
q_forw;
311 sq != NULL && sq != rpmsqQueue;
316 if (sq->
child != reaped)
339 int tblsignum = (signum >= 0 ? signum : -signum);
345 if (rpmsqQueue->
id == NULL)
346 rpmsqQueue->
id =
ME();
348 if (tblsignum != tbl->signum)
353 (void) sigdelset(&rpmsqCaught, tbl->signum);
356 (void) sigaction(tbl->signum, NULL, &tbl->oact);
357 if (tbl->oact.sa_handler == SIG_IGN)
360 (void) sigemptyset (&sa.sa_mask);
361 sa.sa_flags = SA_SIGINFO;
362 #if defined(__LCLINT__)
363 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
365 sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
367 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
373 tbl->handler = handler;
377 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
380 tbl->handler = (handler != NULL ? handler :
rpmsqAction);
399 fprintf(stderr,
" Enable(%p): %p\n",
ME(), sq);
404 xx = pipe(sq->
pipes);
406 xx = sighold(SIGCHLD);
409 if (pid < (pid_t) 0) {
411 xx = close(sq->
pipes[0]);
412 xx = close(sq->
pipes[1]);
416 }
else if (pid == (pid_t) 0) {
421 xx = close(sq->
pipes[1]);
423 xx = read(sq->
pipes[0], &yy,
sizeof(yy));
424 xx = close(sq->
pipes[0]);
430 fprintf(stderr,
" Child(%p): %p child %d\n",
ME(), sq, getpid());
439 fprintf(stderr,
" Parent(%p): %p child %d\n",
ME(), sq, sq->
child);
445 xx = sigrelse(SIGCHLD);
465 ret = sighold(SIGCHLD);
469 if (sq->
pipes[0] >= 0)
470 xx = close(sq->
pipes[0]);
471 if (sq->
pipes[1] >= 0)
472 xx = close(sq->
pipes[1]);
476 xx = pipe(sq->
pipes);
486 ret = sigpause(SIGCHLD);
488 xx = sigrelse(SIGCHLD);
491 if (read(sq->
pipes[0], &xx,
sizeof(xx)) == 0) {
496 xx = sighold(SIGCHLD);
504 xx = sigrelse(SIGCHLD);
508 fprintf(stderr,
" Wake(%p): %p child %d reaper %d ret %d\n",
ME(), sq, sq->
child, sq->
reaper, ret);
518 fprintf(stderr,
" Disable(%p): %p\n",
ME(), sq);
529 fprintf(stderr,
" Wait(%p): %p child %d reaper %d\n",
ME(), sq, sq->
child, sq->
reaper);
538 reaped = waitpid(sq->
child, &status, 0);
539 }
while (reaped >= 0 && reaped != sq->
child);
544 fprintf(stderr,
" Waitpid(%p): %p child %d reaped %d\n",
ME(), sq, sq->
child, sq->
reaped);
550 fprintf(stderr,
" Fini(%p): %p child %d status 0x%x\n",
ME(), sq, sq->
child, sq->
status);
561 ret = pthread_create(&pth, NULL, start, arg);
562 return (ret == 0 ? (
void *)pth : NULL);
567 pthread_t pth = (pthread_t) thread;
570 return pthread_join(pth, NULL);
575 pthread_t t1 = (pthread_t) thread;
576 pthread_t t2 = pthread_self();
577 return pthread_equal(t1, t2);
588 pid_t child = *(pid_t *) arg;
591 (void) kill(child, SIGKILL);
594 result = waitpid(child, NULL, 0);
595 }
while (result == (pid_t)-1 &&
errno == EINTR);
617 sigset_t newMask, oldMask;
618 rpmsq sq = memset(
alloca(
sizeof(*sq)), 0,
sizeof(*sq));
620 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
632 goto out_restore_sigint;
637 (void) sigemptyset (&newMask);
638 (void) sigaddset (&newMask, SIGCHLD);
639 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
642 goto out_restore_sigquit_and_sigint;
649 if (pid < (pid_t) 0) {
651 }
else if (pid == (pid_t) 0) {
656 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
661 (void) execve (argv[0], (
char *
const *) argv, environ);
665 result = waitpid(pid, &status, 0);
666 }
while (result == (pid_t)-1 &&
errno == EINTR);
676 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
682 out_restore_sigquit_and_sigint: