1 /*	$OpenBSD: uthread_sigwait.c,v 1.13 2003/01/31 04:46:17 marc Exp $	*/
2 /*
3  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by John Birrell.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: uthread_sigwait.c,v 1.10 1999/09/30 14:51:31 marcel Exp $
34  */
35 #include <signal.h>
36 #include <errno.h>
37 #ifdef _THREAD_SAFE
38 #include <pthread.h>
39 #include "pthread_private.h"
40 
41 int
sigwait(const sigset_t * set,int * sig)42 sigwait(const sigset_t * set, int *sig)
43 {
44 	struct pthread	*curthread = _get_curthread();
45 	int		ret = 0;
46 	int		i;
47 	sigset_t	tempset, waitset;
48 	struct sigaction act;
49 
50 	_thread_enter_cancellation_point();
51 
52 	/*
53 	 * Specify the thread kernel signal handler.
54 	 */
55 	act.sa_handler = (void (*) (int)) _thread_sig_handler;
56 	act.sa_flags = SA_SIGINFO | SA_RESTART;
57 
58 	/* Ensure the signal handler cannot be interrupted by other signals: */
59 	sigfillset(&act.sa_mask);
60 
61 	/*
62 	 * Initialize the set of signals that will be waited on:
63 	 */
64 	waitset = *set;
65 
66 	/* These signals can't be waited on. */
67 	sigdelset(&waitset, SIGKILL);
68 	sigdelset(&waitset, SIGSTOP);
69 	sigdelset(&waitset, _SCHED_SIGNAL);
70 	sigdelset(&waitset, SIGCHLD);
71 	sigdelset(&waitset, SIGINFO);
72 
73 	/* Check to see if a pending signal is in the wait mask. */
74 	tempset = curthread->sigpend;
75 	tempset |= _process_sigpending;
76 	tempset &= waitset;
77 	if (tempset != 0) {
78 		/* Enter a loop to find a pending signal: */
79 		for (i = 1; i < NSIG; i++) {
80 			if (sigismember(&tempset, i))
81 				break;
82 		}
83 
84 		/* Clear the pending signal: */
85 		_thread_clear_pending(i, curthread);
86 
87 		/* Return the signal number to the caller: */
88 		*sig = i;
89 
90 		_thread_leave_cancellation_point();
91 		return (0);
92 	}
93 
94 	/*
95 	 * Access the _thread_dfl_count array under the protection of signal
96 	 * deferral.
97 	 */
98 	_thread_kern_sig_defer();
99 
100 	/*
101 	 * Enter a loop to find the signals that are SIG_DFL.  For
102 	 * these signals we must install a dummy signal handler in
103 	 * order for the kernel to pass them in to us.  POSIX says
104 	 * that the _application_ must explicitly install a dummy
105 	 * handler for signals that are SIG_IGN in order to sigwait
106 	 * on them.  Note that SIG_IGN signals are left in the
107 	 * mask because a subsequent sigaction could enable an
108 	 * ignored signal.
109 	 */
110 	sigemptyset(&tempset);
111 	for (i = 1; i < NSIG; i++) {
112 		if (sigismember(&waitset, i) &&
113 		    (_thread_sigact[i - 1].sa_handler == SIG_DFL)) {
114 			_thread_dfl_count[i]++;
115 			sigaddset(&tempset, i);
116 			if (_thread_dfl_count[i] == 1) {
117 				if (_thread_sys_sigaction(i, &act, NULL) != 0)
118 					ret = -1;
119 			}
120 		}
121 	}
122 
123 	/* Done accessing _thread_dfl_count for now. */
124 	_thread_kern_sig_undefer();
125 
126 	if (ret == 0) {
127 		/*
128 		 * Save the wait signal mask.  The wait signal
129 		 * mask is independent of the threads signal mask
130 		 * and requires separate storage.
131 		 */
132 		curthread->data.sigwait = &waitset;
133 
134 		/* Wait for a signal: */
135 		_thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__);
136 
137 		/* Return the signal number to the caller: */
138 		*sig = curthread->signo;
139 
140 		/*
141 		 * Probably unnecessary, but since it's in a union struct
142 		 * we don't know how it could be used in the future.
143 		 */
144 		curthread->data.sigwait = NULL;
145 	}
146 
147 	/*
148 	 * Access the _thread_dfl_count array under the protection of signal
149 	 * deferral.
150 	 */
151 	_thread_kern_sig_defer();
152 
153 	/* Restore the sigactions: */
154 	act.sa_handler = SIG_DFL;
155 	for (i = 1; i < NSIG; i++) {
156 		if (sigismember(&tempset, i)) {
157 			_thread_dfl_count[i]--;
158 			if ((_thread_sigact[i - 1].sa_handler == SIG_DFL) &&
159 			    (_thread_dfl_count[i] == 0)) {
160 				if (_thread_sys_sigaction(i, &act, NULL) != 0)
161 					ret = -1;
162 			}
163 		}
164 	}
165 
166 	/* Done accessing _thread_dfl_count. */
167 	_thread_kern_sig_undefer();
168 
169 	_thread_leave_cancellation_point();
170 
171 	/* Return the completion status: */
172 	return (ret);
173 }
174 #endif
175