summaryrefslogtreecommitdiffstats
path: root/lib/librthread/rthread_sig.c
diff options
context:
space:
mode:
authorguenther <guenther@openbsd.org>2008-10-03 04:22:37 +0000
committerguenther <guenther@openbsd.org>2008-10-03 04:22:37 +0000
commit45ad980a3e7a38b31b9cf80cc36f277e18ce65c8 (patch)
treea0018f14fe34ec02a5b83797323b7a112dc05ed3 /lib/librthread/rthread_sig.c
parentReturn ENOTTY for unknown ioctl's. (diff)
downloadwireguard-openbsd-45ad980a3e7a38b31b9cf80cc36f277e18ce65c8.tar.xz
wireguard-openbsd-45ad980a3e7a38b31b9cf80cc36f277e18ce65c8.zip
Make sigwait() work correctly. In particular, it'll work when the
signal is already pending in the calling thread or the main thread and there's no longer a race condition where the signal could be diverted but sigwait() would still block. There were some off-by-one errors too. (The checking of the main thread's pending list is just until a pending list for the process can be added to struct process. For now, such signals end up as pending on the main thread.) oks tedu@ and art@
Diffstat (limited to 'lib/librthread/rthread_sig.c')
-rw-r--r--lib/librthread/rthread_sig.c45
1 files changed, 7 insertions, 38 deletions
diff --git a/lib/librthread/rthread_sig.c b/lib/librthread/rthread_sig.c
index 1591cd87ccf..83dc5eee2b7 100644
--- a/lib/librthread/rthread_sig.c
+++ b/lib/librthread/rthread_sig.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rthread_sig.c,v 1.5 2008/04/24 11:44:26 kurt Exp $ */
+/* $OpenBSD: rthread_sig.c,v 1.6 2008/10/03 04:22:37 guenther Exp $ */
/*
* Copyright (c) 2005 Ted Unangst <tedu@openbsd.org>
* All Rights Reserved.
@@ -42,45 +42,14 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
return (sigprocmask(how, set, oset) ? errno : 0);
}
-/*
- * implementation of sigwait:
- * 1. we install a handler for each masked signal.
- * 2. we inform the kernel we are interested in this signal set.
- * 3. sleep. the handler will wake us up.
- *
- * this is atomic because the kernel will only divert one signal
- * to a thread until it asks for more.
- */
-static void
-sigwait_handler(int sig)
-{
- pthread_t self = pthread_self();
- self->sigpend = sig;
- thrwakeup(&self->sigpend, 0);
-}
-
-typedef void (*sigfn)(int);
-
int
sigwait(const sigset_t *set, int *sig)
{
- int i;
- sigset_t mask = *set;
- pthread_t self = pthread_self();
- sigfn oldhandlers[NSIG];
-
- for (i = 0; i < NSIG; i++) {
- if (mask & (1 << i))
- oldhandlers[i] = signal(i, sigwait_handler);
- }
-
- thrsigdivert(set);
- thrsleep(&self->sigpend, 0, NULL);
+ int ret;
- for (i = 0; i < NSIG; i++) {
- if (mask & (1 << i))
- signal(i, oldhandlers[i]);
- }
- *sig = self->sigpend;
- return (0);
+ ret = thrsigdivert(set);
+ if (ret == -1)
+ return errno;
+ *sig = ret;
+ return 0;
}