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 #if !defined(__QNX__)
122 # include <sys/signal.h>
124 #include <sys/wait.h>
128 #if !defined(HAVE_SIGHOLD) && defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGADDSET)
129 static int __RPM_sighold(
int sig)
132 if (sigprocmask(SIG_SETMASK, NULL, &
set) < 0)
134 if (sigaddset(&
set, sig) < 0)
136 return sigprocmask(SIG_SETMASK, &
set, NULL);
138 #define sighold(sig) __RPM_sighold(sig)
142 #if !defined(HAVE_SIGRELSE) && defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGDELSET)
143 static int __RPM_sigrelse(
int sig)
146 if (sigprocmask(SIG_SETMASK, NULL, &
set) < 0)
148 if (sigdelset(&
set, sig) < 0)
150 return sigprocmask(SIG_SETMASK, &
set, NULL);
152 #define sigrelse(sig) __RPM_sigrelse(sig)
156 #if !defined(HAVE_SIGPAUSE) && defined(HAVE_SIGEMPTYSET) && defined(HAVE_SIGADDSET) && defined(HAVE_SIGSUSPEND)
157 static int __RPM_sigpause(
int sig)
160 if (sigemptyset(&
set) < 0)
162 if (sigaddset(&
set, sig) < 0)
164 return sigsuspend(&
set);
166 #define sigpause(sig) __RPM_sigpause(sig)
170 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__QNX__)
172 struct qelem *q_forw;
173 struct qelem *q_back;
176 static void __insque(
struct qelem * elem,
struct qelem * pred)
178 elem -> q_forw = pred -> q_forw;
179 pred -> q_forw -> q_back = elem;
180 elem -> q_back = pred;
181 pred -> q_forw = elem;
183 #define insque(_e, _p) __insque((_e), (_p))
185 static void __remque(
struct qelem * elem)
187 elem -> q_forw -> q_back = elem -> q_back;
188 elem -> q_back -> q_forw = elem -> q_forw;
190 #define remque(_e) __remque(_e)
193 #if defined(WITH_PTHREADS)
195 # if !defined(__QNX__)
197 # if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
198 # error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
202 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
204 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
208 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
213 #define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
214 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
215 #define INIT_LOCK() \
216 { pthread_mutexattr_t attr; \
217 (void) pthread_mutexattr_init(&attr); \
218 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
219 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
220 (void) pthread_mutexattr_destroy(&attr); \
221 rpmsigTbl_sigchld->active = 0; \
223 #define ADD_REF(__tbl) (__tbl)->active++
224 #define SUB_REF(__tbl) --(__tbl)->active
225 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
226 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
227 pthread_cleanup_push((__handler), (__arg));
228 #define CLEANUP_RESET(__execute, __oldtype) \
229 pthread_cleanup_pop(__execute); \
230 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
232 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
235 #define ME() __tid2vp(pthread_self())
237 static void *__tid2vp(pthread_t tid)
240 union { pthread_t tid;
void *vp; } u;
248 #define DO_LOCK() (0)
249 #define DO_UNLOCK() (0)
251 #define ADD_REF(__tbl) (0)
252 #define SUB_REF(__tbl) (0)
253 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
254 #define CLEANUP_RESET(__execute, __oldtype)
256 #define SAME_THREAD(_a, _b) (42)
259 #define ME() __pid2vp(getpid())
264 union { pid_t pid;
void *vp; } u;
271 #define _RPMSQ_INTERNAL
276 #define _RPMSQ_DEBUG 0
283 static struct rpmsqElem
rpmsqRock = { .q_forw = &rpmsqRock };
299 fprintf(stderr,
" Insert(%p): %p\n",
ME(), sq);
301 ret = sighold(SIGCHLD);
308 sq->pipes[0] = sq->pipes[1] = -1;
312 insque(elem, (prev != NULL ? prev : rpmsqQueue));
314 ret = sigrelse(SIGCHLD);
329 fprintf(stderr,
" Remove(%p): %p\n",
ME(), sq);
331 ret = sighold (SIGCHLD);
337 if (sq->pipes[1] > 0) ret = close(sq->pipes[1]);
338 if (sq->pipes[0] > 0) ret = close(sq->pipes[0]);
339 sq->pipes[0] = sq->pipes[1] = -1;
345 ret = sigrelse(SIGCHLD);
363 #define rpmsigTbl_sigint (&rpmsigTbl[0])
365 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
367 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
369 #define rpmsigTbl_sighup (&rpmsigTbl[3])
371 #define rpmsigTbl_sigterm (&rpmsigTbl[4])
373 #define rpmsigTbl_sigpipe (&rpmsigTbl[5])
378 #define rpmsigTbl_sigxcpu (&rpmsigTbl[6])
382 #define rpmsigTbl_sigxfsz (&rpmsigTbl[7])
397 if (tbl->
signum != signum)
400 (void) sigaddset(&rpmsqCaught, signum);
407 pid_t reaped = waitpid(0, &status, WNOHANG);
414 for (sq = rpmsqQueue->q_forw;
415 sq != NULL && sq != rpmsqQueue;
420 if (sq->child != reaped)
425 ret = close(sq->pipes[1]); sq->pipes[1] = -1;
443 int tblsignum = (signum >= 0 ? signum : -signum);
450 if (rpmsqQueue->id == NULL)
451 rpmsqQueue->id =
ME();
453 if (tblsignum != tbl->signum)
458 (void) sigdelset(&rpmsqCaught, tbl->signum);
461 (void) sigaction(tbl->signum, NULL, &tbl->oact);
462 if (tbl->oact.sa_handler == SIG_IGN)
465 (void) sigemptyset (&sa.sa_mask);
466 sa.sa_flags = SA_SIGINFO;
467 #if defined(__LCLINT__)
468 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
470 sa.sa_sigaction = (
void *) (handler != NULL ? handler : tbl->handler);
472 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
478 tbl->handler = handler;
482 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
485 tbl->handler = (handler != NULL ? handler :
rpmsqAction);
504 fprintf(stderr,
" Enable(%p): %p\n",
ME(), sq);
509 xx = pipe(sq->pipes);
511 xx = sighold(SIGCHLD);
514 if (pid < (pid_t) 0) {
515 sq->child = (pid_t)-1;
516 xx = close(sq->pipes[0]);
517 xx = close(sq->pipes[1]);
518 sq->pipes[0] = sq->pipes[1] = -1;
520 }
else if (pid == (pid_t) 0) {
524 xx = close(sq->pipes[1]);
526 xx = (int)read(sq->pipes[0], &yy,
sizeof(yy));
527 xx = close(sq->pipes[0]);
528 sq->pipes[0] = sq->pipes[1] = -1;
532 fprintf(stderr,
" Child(%p): %p child %d\n",
ME(), sq, (
int)getpid());
541 fprintf(stderr,
" Parent(%p): %p child %d\n",
ME(), sq, (
int)sq->child);
547 xx = sigrelse(SIGCHLD);
567 ret = sighold(SIGCHLD);
570 if (sq->pipes[0] >= 0)
571 xx = close(sq->pipes[0]);
572 if (sq->pipes[1] >= 0)
573 xx = close(sq->pipes[1]);
576 xx = pipe(sq->pipes);
583 while (ret == 0 && sq->reaped != sq->child) {
586 ret = sigpause(SIGCHLD);
588 xx = sigrelse(SIGCHLD);
591 if (read(sq->pipes[0], &xx,
sizeof(xx)) == 0) {
592 xx = close(sq->pipes[0]); sq->pipes[0] = -1;
596 xx = sighold(SIGCHLD);
602 sq->ms_scriptlets +=
rpmswExit(&sq->op, -1)/1000;
604 xx = sigrelse(SIGCHLD);
608 fprintf(stderr,
" Wake(%p): %p child %d reaper %d ret %d\n",
ME(), sq, (
int)sq->child, sq->reaper, ret);
618 fprintf(stderr,
" Disable(%p): %p\n",
ME(), sq);
629 fprintf(stderr,
" Wait(%p): %p child %d reaper %d\n",
ME(), sq, (
int)sq->child, sq->reaper);
638 reaped = waitpid(sq->child, &status, 0);
639 }
while (reaped >= 0 && reaped != sq->child);
644 fprintf(stderr,
" Waitpid(%p): %p child %d reaped %d\n",
ME(), sq, (
int)sq->child, (
int)sq->reaped);
650 fprintf(stderr,
" Fini(%p): %p child %d status 0x%x\n",
ME(), sq, (
int)sq->child, sq->status);
658 #if defined(WITH_PTHREADS)
662 ret = pthread_create(&pth, NULL, start, arg);
663 return (ret == 0 ? (
void *)pth : NULL);
673 #if defined(WITH_PTHREADS)
674 pthread_t pth = (pthread_t) thread;
677 return pthread_join(pth, NULL);
686 #if defined(WITH_PTHREADS)
687 pthread_t t1 = (pthread_t) thread;
688 pthread_t t2 = pthread_self();
689 return pthread_equal(t1, t2);
699 #if defined(WITH_PTHREADS)
701 sigchld_cancel (
void *arg)
705 pid_t child = *(pid_t *) arg;
709 xx = kill(child, SIGKILL);
712 result = waitpid(child, NULL, 0);
713 }
while (result == (pid_t)-1 && errno == EINTR);
716 if (
SUB_REF (rpmsigTbl_sigchld) == 0) {
732 #if defined(WITH_PTHREADS)
738 sigset_t newMask, oldMask;
739 rpmsq sq = memset(
alloca(
sizeof(*sq)), 0,
sizeof(*sq));
742 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
754 goto out_restore_sigint;
759 (void) sigemptyset (&newMask);
760 (void) sigaddset (&newMask, SIGCHLD);
761 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
764 goto out_restore_sigquit_and_sigint;
773 if (pid < (pid_t) 0) {
775 }
else if (pid == (pid_t) 0) {
780 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
785 (void) execve (argv[0], (
char *
const *) argv, environ);
789 result = waitpid(pid, &status, 0);
790 }
while (result == (pid_t)-1 &&
errno == EINTR);
800 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
806 out_restore_sigquit_and_sigint: