xref: /openbsd-src/lib/libc/gen/sigwait.c (revision 13e54a3ce65615faaf13e4717a510eb52bfb2efe)
1*13e54a3cSjca /*	$OpenBSD: sigwait.c,v 1.1 2019/01/12 00:16:03 jca Exp $ */
2*13e54a3cSjca /*
3*13e54a3cSjca  * Copyright (c) 2005 Ted Unangst <tedu@openbsd.org>
4*13e54a3cSjca  * All Rights Reserved.
5*13e54a3cSjca  *
6*13e54a3cSjca  * Permission to use, copy, modify, and distribute this software for any
7*13e54a3cSjca  * purpose with or without fee is hereby granted, provided that the above
8*13e54a3cSjca  * copyright notice and this permission notice appear in all copies.
9*13e54a3cSjca  *
10*13e54a3cSjca  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*13e54a3cSjca  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*13e54a3cSjca  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*13e54a3cSjca  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*13e54a3cSjca  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*13e54a3cSjca  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*13e54a3cSjca  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*13e54a3cSjca  */
18*13e54a3cSjca /*
19*13e54a3cSjca  * signals
20*13e54a3cSjca  */
21*13e54a3cSjca 
22*13e54a3cSjca #include <signal.h>
23*13e54a3cSjca #include <errno.h>
24*13e54a3cSjca 
25*13e54a3cSjca #include <pthread.h>
26*13e54a3cSjca 
27*13e54a3cSjca #include "thread/rthread.h"
28*13e54a3cSjca #include "cancel.h"		/* in libc/include */
29*13e54a3cSjca 
30*13e54a3cSjca int
sigwait(const sigset_t * set,int * sig)31*13e54a3cSjca sigwait(const sigset_t *set, int *sig)
32*13e54a3cSjca {
33*13e54a3cSjca 	sigset_t s = *set;
34*13e54a3cSjca 	int ret;
35*13e54a3cSjca 
36*13e54a3cSjca 	sigdelset(&s, SIGTHR);
37*13e54a3cSjca 	do {
38*13e54a3cSjca 		ENTER_CANCEL_POINT(1);
39*13e54a3cSjca 		ret = __thrsigdivert(s, NULL, NULL);
40*13e54a3cSjca 		LEAVE_CANCEL_POINT(ret == -1);
41*13e54a3cSjca 	} while (ret == -1 && errno == EINTR);
42*13e54a3cSjca 	if (ret == -1)
43*13e54a3cSjca 		return (errno);
44*13e54a3cSjca 	*sig = ret;
45*13e54a3cSjca 	return (0);
46*13e54a3cSjca }
47*13e54a3cSjca 
48*13e54a3cSjca #if 0		/* need kernel to fill in more siginfo_t bits first */
49*13e54a3cSjca int
50*13e54a3cSjca sigwaitinfo(const sigset_t *set, siginfo_t *info)
51*13e54a3cSjca {
52*13e54a3cSjca 	sigset_t s = *set;
53*13e54a3cSjca 	int ret;
54*13e54a3cSjca 
55*13e54a3cSjca 	sigdelset(&s, SIGTHR);
56*13e54a3cSjca 	ENTER_CANCEL_POINT(1);
57*13e54a3cSjca 	ret = __thrsigdivert(s, info, NULL);
58*13e54a3cSjca 	LEAVE_CANCEL_POINT(ret == -1);
59*13e54a3cSjca 	return (ret);
60*13e54a3cSjca }
61*13e54a3cSjca 
62*13e54a3cSjca int
63*13e54a3cSjca sigtimedwait(const sigset_t *set, siginfo_t *info,
64*13e54a3cSjca     const struct timespec *timeout)
65*13e54a3cSjca {
66*13e54a3cSjca 	sigset_t s = *set;
67*13e54a3cSjca 	int ret;
68*13e54a3cSjca 
69*13e54a3cSjca 	sigdelset(&s, SIGTHR);
70*13e54a3cSjca 	ENTER_CANCEL_POINT(1);
71*13e54a3cSjca 	ret = __thrsigdivert(s, info, timeout);
72*13e54a3cSjca 	LEAVE_CANCEL_POINT(ret == -1);
73*13e54a3cSjca 	return (ret);
74*13e54a3cSjca }
75*13e54a3cSjca #endif
76