From 2f058413cfe2577ff949673b79467acaae6f84c1 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Thu, 21 Feb 2002 20:57:42 +0000 Subject: account for the process signal mask when dealing with signals; tested a while ago by marc@ and brad@ --- lib/libc_r/uthread/pthread_private.h | 13 +++++++- lib/libc_r/uthread/uthread_init.c | 8 ++++- lib/libc_r/uthread/uthread_kern.c | 20 +++++++++++- lib/libc_r/uthread/uthread_sigmask.c | 20 +++++++++--- lib/libc_r/uthread/uthread_sigwait.c | 63 +++++++++++++++++++++++++----------- 5 files changed, 99 insertions(+), 25 deletions(-) (limited to 'lib/libc_r') diff --git a/lib/libc_r/uthread/pthread_private.h b/lib/libc_r/uthread/pthread_private.h index cc9abefa7c6..23723ca68e0 100644 --- a/lib/libc_r/uthread/pthread_private.h +++ b/lib/libc_r/uthread/pthread_private.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pthread_private.h,v 1.34 2002/01/19 23:49:32 fgsch Exp $ */ +/* $OpenBSD: pthread_private.h,v 1.35 2002/02/21 20:57:41 fgsch Exp $ */ /* * Copyright (c) 1995-1998 John Birrell . * All rights reserved. @@ -996,6 +996,16 @@ SCLASS struct sigaction _thread_sigact[NSIG]; */ SCLASS int _thread_dfl_count[NSIG]; +/* + * Pending signals and mask for this process: + */ +SCLASS sigset_t _process_sigpending; +SCLASS sigset_t _process_sigmask +#ifdef GLOBAL_PTHREAD_PRIVATE += 0 +#endif +; + /* * Scheduling queues: */ @@ -1098,6 +1108,7 @@ void _thread_cleanupspecific(void); void _thread_dump_info(void); void _thread_init(void); void _thread_kern_sched(struct sigcontext *); +void _thread_kern_sched_sig(void); void _thread_kern_sched_state(enum pthread_state,char *fname,int lineno); void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno); diff --git a/lib/libc_r/uthread/uthread_init.c b/lib/libc_r/uthread/uthread_init.c index 39242c8fd22..d9f398c381b 100644 --- a/lib/libc_r/uthread/uthread_init.c +++ b/lib/libc_r/uthread/uthread_init.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_init.c,v 1.20 2002/01/17 23:12:09 fgsch Exp $ */ +/* $OpenBSD: uthread_init.c,v 1.21 2002/02/21 20:57:41 fgsch Exp $ */ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. @@ -224,6 +224,9 @@ _thread_init(void) act.sa_handler = (void (*) ()) _thread_sig_handler; act.sa_flags = 0; + /* Clear pending signals for the process: */ + sigemptyset(&_process_sigpending); + /* Initialize signal handling: */ _thread_sig_init(); @@ -261,6 +264,9 @@ _thread_init(void) PANIC("Cannot initialize signal handler"); } + /* Get the process signal mask: */ + _thread_sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); + /* Get the kernel clockrate: */ mib[0] = CTL_KERN; mib[1] = KERN_CLOCKRATE; diff --git a/lib/libc_r/uthread/uthread_kern.c b/lib/libc_r/uthread/uthread_kern.c index 4cd8035d7ca..da3c91ff933 100644 --- a/lib/libc_r/uthread/uthread_kern.c +++ b/lib/libc_r/uthread/uthread_kern.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_kern.c,v 1.20 2002/01/04 03:39:09 fgsch Exp $ */ +/* $OpenBSD: uthread_kern.c,v 1.21 2002/02/21 20:57:41 fgsch Exp $ */ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. @@ -63,6 +63,15 @@ thread_run_switch_hook(pthread_t thread_out, pthread_t thread_in); /* Static variables: */ static int last_tick = 0; +void +_thread_kern_sched_sig(void) +{ + struct pthread *curthread = _get_curthread(); + + curthread->check_pending = 1; + _thread_kern_sched(NULL); +} + void _thread_kern_sched(struct sigcontext * scp) { @@ -83,6 +92,15 @@ _thread_kern_sched(struct sigcontext * scp) /* Check if this function was called from the signal handler: */ if (scp != NULL) { + /* + * The signal handler should have saved the state of + * the current thread. Restore the process signal + * mask. + */ + if (_thread_sys_sigprocmask(SIG_SETMASK, + &_process_sigmask, NULL) != 0) + PANIC("Unable to restore process mask after signal"); + /* * Copy the signal context to the current thread's jump * buffer: diff --git a/lib/libc_r/uthread/uthread_sigmask.c b/lib/libc_r/uthread/uthread_sigmask.c index 03fb4963395..12dd6ec3c69 100644 --- a/lib/libc_r/uthread/uthread_sigmask.c +++ b/lib/libc_r/uthread/uthread_sigmask.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_sigmask.c,v 1.4 2001/08/21 19:24:53 fgsch Exp $ */ +/* $OpenBSD: uthread_sigmask.c,v 1.5 2002/02/21 20:57:41 fgsch Exp $ */ /* * Copyright (c) 1997 John Birrell . * All rights reserved. @@ -42,6 +42,7 @@ int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) { struct pthread *curthread = _get_curthread(); + sigset_t sigset; int ret = 0; /* Check if the existing signal process mask is to be returned: */ @@ -79,11 +80,22 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) break; } + /* Increment the sequence number: */ + curthread->sigmask_seqno++; + /* - * Dispatch signals to the running thread that are pending - * and now unblocked: + * Check if there are pending signals for the running + * thread or process that aren't blocked: */ - _dispatch_signals(); + sigset = curthread->sigpend; + sigset |= _process_sigpending; + sigset &= ~curthread->sigmask; + if (sigset != 0) + /* + * Call the kernel scheduler which will safely + * install a signal frame for the running thread: + */ + _thread_kern_sched_sig(); } /* Return the completion status: */ diff --git a/lib/libc_r/uthread/uthread_sigwait.c b/lib/libc_r/uthread/uthread_sigwait.c index 1ae76ab1926..0d1bf618be1 100644 --- a/lib/libc_r/uthread/uthread_sigwait.c +++ b/lib/libc_r/uthread/uthread_sigwait.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_sigwait.c,v 1.9 2001/08/21 19:24:53 fgsch Exp $ */ +/* $OpenBSD: uthread_sigwait.c,v 1.10 2002/02/21 20:57:41 fgsch Exp $ */ /* * Copyright (c) 1997 John Birrell . * All rights reserved. @@ -47,7 +47,6 @@ sigwait(const sigset_t * set, int *sig) sigset_t tempset, waitset; struct sigaction act; - /* This is a cancellation point: */ _thread_enter_cancellation_point(); /* @@ -55,10 +54,9 @@ sigwait(const sigset_t * set, int *sig) */ act.sa_handler = (void (*) ()) _thread_sig_handler; act.sa_flags = SA_RESTART; - act.sa_mask = *set; - /* Ensure the scheduling signal is masked: */ - sigaddset(&act.sa_mask, _SCHED_SIGNAL); + /* Ensure the signal handler cannot be interrupted by other signals: */ + sigfillset(&act.sa_mask); /* * Initialize the set of signals that will be waited on: @@ -73,25 +71,35 @@ sigwait(const sigset_t * set, int *sig) sigdelset(&waitset, SIGINFO); /* Check to see if a pending signal is in the wait mask. */ - if ((tempset = (curthread->sigpend & waitset)) != 0) { + tempset = curthread->sigpend; + tempset |= _process_sigpending; + tempset &= waitset; + if (tempset != 0) { /* Enter a loop to find a pending signal: */ for (i = 1; i < NSIG; i++) { - if (sigismember (&tempset, i)) + if (sigismember(&tempset, i)) break; } /* Clear the pending signal: */ - sigdelset(&curthread->sigpend,i); + if (sigismember(&curthread->sigpend, i)) + sigdelset(&curthread->sigpend, i); + else + sigdelset(&_process_sigpending, i); /* Return the signal number to the caller: */ *sig = i; - /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); - return (0); } + /* + * Access the _thread_dfl_count array under the protection of signal + * deferral. + */ + _thread_kern_sig_defer(); + /* * Enter a loop to find the signals that are SIG_DFL. For * these signals we must install a dummy signal handler in @@ -102,15 +110,23 @@ sigwait(const sigset_t * set, int *sig) * mask because a subsequent sigaction could enable an * ignored signal. */ + sigemptyset(&tempset); for (i = 1; i < NSIG; i++) { if (sigismember(&waitset, i) && (_thread_sigact[i - 1].sa_handler == SIG_DFL)) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) - ret = -1; + _thread_dfl_count[i]++; + sigaddset(&tempset, i); + if (_thread_dfl_count[i] == 1) { + if (_thread_sys_sigaction(i, &act, NULL) != 0) + ret = -1; + } } } - if (ret == 0) { + /* Done accessing _thread_dfl_count for now. */ + _thread_kern_sig_undefer(); + + if (ret == 0) { /* * Save the wait signal mask. The wait signal * mask is independent of the threads signal mask @@ -131,17 +147,28 @@ sigwait(const sigset_t * set, int *sig) curthread->data.sigwait = NULL; } + /* + * Access the _thread_dfl_count array under the protection of signal + * deferral. + */ + _thread_kern_sig_defer(); + /* Restore the sigactions: */ act.sa_handler = SIG_DFL; for (i = 1; i < NSIG; i++) { - if (sigismember(&waitset, i) && - (_thread_sigact[i - 1].sa_handler == SIG_DFL)) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) - ret = -1; + if (sigismember(&tempset, i)) { + _thread_dfl_count[i]--; + if ((_thread_sigact[i - 1].sa_handler == SIG_DFL) && + (_thread_dfl_count[i] == 0)) { + if (_thread_sys_sigaction(i, &act, NULL) != 0) + ret = -1; + } } } - /* No longer in a cancellation point: */ + /* Done accessing _thread_dfl_count. */ + _thread_kern_sig_undefer(); + _thread_leave_cancellation_point(); /* Return the completion status: */ -- cgit v1.2.3