xref: /dragonfly/test/libpthread/sigsuspend_d.c (revision 62481538)
1 /*
2  * Copyright (c) 1998 Daniel M. Eischen <eischen@vigrid.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Daniel M. Eischen.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY DANIEL M. EISCHEN AND CONTRIBUTORS ``AS IS''
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/lib/libc_r/test/sigsuspend_d.c,v 1.1.2.1 2000/07/17 22:18:32 jasone Exp $
33  */
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #include <errno.h>
38 #include <pthread.h>
39 #include <pthread_np.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <string.h>
43 
44 static int	sigcounts[NSIG + 1];
45 static int	sigfifo[NSIG + 1];
46 static int	fifo_depth = 0;
47 static sigset_t suspender_mask;
48 static pthread_t suspender_tid;
49 
50 
51 static void *
sigsuspender(void * arg)52 sigsuspender (void *arg)
53 {
54 	int status, i;
55 	sigset_t run_mask;
56 
57 	/* Run with all signals blocked. */
58 	sigfillset (&run_mask);
59 	sigprocmask (SIG_SETMASK, &run_mask, NULL);
60 
61 	/* Allow these signals to wake us up during a sigsuspend. */
62 	sigfillset (&suspender_mask);		/* Default action	*/
63 	sigdelset (&suspender_mask, SIGINT);	/* terminate		*/
64 	sigdelset (&suspender_mask, SIGHUP);	/* terminate		*/
65 	sigdelset (&suspender_mask, SIGQUIT);	/* create core image	*/
66 	sigdelset (&suspender_mask, SIGURG);	/* ignore		*/
67 	sigdelset (&suspender_mask, SIGIO);	/* ignore		*/
68 	sigdelset (&suspender_mask, SIGUSR2);	/* terminate		*/
69 
70 	while (sigcounts[SIGINT] == 0) {
71 		status = sigsuspend (&suspender_mask);
72 		if ((status == 0) || (errno != EINTR)) {
73 			fprintf (stderr, "Unable to suspend for signals, "
74 				"errno %d, return value %d\n",
75 				errno, status);
76 			exit (1);
77 		}
78 		for (i = 0; i < fifo_depth; i++)
79 			fprintf (stderr, "Sigsuspend woke up by signal %d\n",
80 				sigfifo[i]);
81 		fifo_depth = 0;
82 	}
83 
84 	pthread_exit (arg);
85 	return (NULL);
86 }
87 
88 
89 static void
sighandler(int signo)90 sighandler (int signo)
91 {
92 	sigset_t set, suspend_set;
93 	pthread_t self;
94 
95 	if ((signo >= 0) && (signo <= NSIG))
96 		sigcounts[signo]++;
97 
98 	/*
99 	 * If we are running on behalf of the suspender thread,
100 	 * ensure that we have the correct mask set.
101 	 */
102 	self = pthread_self ();
103 	if (self == suspender_tid) {
104 		sigfifo[fifo_depth] = signo;
105 		fifo_depth++;
106 		fprintf (stderr,
107 		    "  -> Suspender thread signal handler caught signal %d\n",
108 		    signo);
109 
110 		/* Get the current signal mask. */
111 		sigprocmask (SIG_SETMASK, NULL, &set);
112 
113 		/* The handler should run with the current signal masked. */
114 		suspend_set = suspender_mask;
115 		sigaddset(&suspend_set, signo);
116 
117 		if (memcmp(&set, &suspend_set, sizeof(set)))
118 			fprintf (stderr,
119 			    "  >>> FAIL: sigsuspender signal handler running "
120 			    "with incorrect mask.\n");
121 	}
122 	else
123 		fprintf (stderr,
124 		    "  -> Main thread signal handler caught signal %d\n",
125 		    signo);
126 }
127 
128 
129 static void
send_thread_signal(pthread_t tid,int signo)130 send_thread_signal (pthread_t tid, int signo)
131 {
132 	if (pthread_kill (tid, signo) != 0) {
133 		fprintf (stderr, "Unable to send thread signal, errno %d.\n",
134 		    errno);
135 		exit (1);
136 	}
137 }
138 
139 
140 static void
send_process_signal(int signo)141 send_process_signal (int signo)
142 {
143 	if (kill (getpid (), signo) != 0) {
144 		fprintf (stderr, "Unable to send process signal, errno %d.\n",
145 		    errno);
146 		exit (1);
147 	}
148 }
149 
150 
main(int argc,char * argv[])151 int main (int argc, char *argv[])
152 {
153 	pthread_attr_t	pattr;
154 	void *		exit_status;
155 	struct sigaction act;
156 	sigset_t	oldset;
157 	sigset_t	newset;
158 
159 	/* Initialize our signal counts. */
160 	memset ((void *) sigcounts, 0, NSIG * sizeof (int));
161 
162 	/* Ignore signal SIGIO. */
163 	sigemptyset (&act.sa_mask);
164 	sigaddset (&act.sa_mask, SIGIO);
165 	act.sa_handler = SIG_IGN;
166 	act.sa_flags = 0;
167 	sigaction (SIGIO, &act, NULL);
168 
169 	/* Install a signal handler for SIGURG. */
170 	sigemptyset (&act.sa_mask);
171 	sigaddset (&act.sa_mask, SIGURG);
172 	act.sa_handler = sighandler;
173 	act.sa_flags = SA_RESTART;
174 	sigaction (SIGURG, &act, NULL);
175 
176 	/* Install a signal handler for SIGXCPU */
177 	sigemptyset (&act.sa_mask);
178 	sigaddset (&act.sa_mask, SIGXCPU);
179 	sigaction (SIGXCPU, &act, NULL);
180 
181 	/* Get our current signal mask. */
182 	sigprocmask (SIG_SETMASK, NULL, &oldset);
183 
184 	/* Mask out SIGUSR1 and SIGUSR2. */
185 	newset = oldset;
186 	sigaddset (&newset, SIGUSR1);
187 	sigaddset (&newset, SIGUSR2);
188 	sigprocmask (SIG_SETMASK, &newset, NULL);
189 
190 	/* Install a signal handler for SIGUSR1 */
191 	sigemptyset (&act.sa_mask);
192 	sigaddset (&act.sa_mask, SIGUSR1);
193 	sigaction (SIGUSR1, &act, NULL);
194 
195 	/* Install a signal handler for SIGUSR2 */
196 	sigemptyset (&act.sa_mask);
197 	sigaddset (&act.sa_mask, SIGUSR2);
198 	sigaction (SIGUSR2, &act, NULL);
199 
200 	/*
201 	 * Initialize the thread attribute.
202 	 */
203 	if ((pthread_attr_init (&pattr) != 0) ||
204 	    (pthread_attr_setdetachstate (&pattr,
205 	    PTHREAD_CREATE_JOINABLE) != 0)) {
206 		fprintf (stderr, "Unable to initialize thread attributes.\n");
207 		exit (1);
208 	}
209 
210 	/*
211 	 * Create the sigsuspender thread.
212 	 */
213 	if (pthread_create (&suspender_tid, &pattr, sigsuspender, NULL) != 0) {
214 		fprintf (stderr, "Unable to create thread, errno %d.\n", errno);
215 		exit (1);
216 	}
217 	pthread_set_name_np (suspender_tid, "sigsuspender");
218 
219 	/*
220 	 * Verify that an ignored signal doesn't cause a wakeup.
221 	 * We don't have a handler installed for SIGIO.
222 	 */
223 	send_thread_signal (suspender_tid, SIGIO);
224 	sleep (1);
225 	send_process_signal (SIGIO);
226 	sleep (1);
227 	if (sigcounts[SIGIO] != 0)
228 		fprintf (stderr, "FAIL: sigsuspend wakes up for ignored signal "
229 			"SIGIO.\n");
230 
231 	/*
232 	 * Verify that a signal with a default action of ignore, for
233 	 * which we have a signal handler installed, will release a
234 	 * sigsuspend.
235 	 */
236 	send_thread_signal (suspender_tid, SIGURG);
237 	sleep (1);
238 	send_process_signal (SIGURG);
239 	sleep (1);
240 	if (sigcounts[SIGURG] != 2)
241 		fprintf (stderr,
242 		    "FAIL: sigsuspend doesn't wake up for SIGURG.\n");
243 
244 	/*
245 	 * Verify that a SIGUSR2 signal will release a sigsuspended
246 	 * thread.
247 	 */
248 	send_thread_signal (suspender_tid, SIGUSR2);
249 	sleep (1);
250 	send_process_signal (SIGUSR2);
251 	sleep (1);
252 	if (sigcounts[SIGUSR2] != 2)
253 		fprintf (stderr,
254 		    "FAIL: sigsuspend doesn't wake up for SIGUSR2.\n");
255 
256 	/*
257 	 * Verify that a signal, blocked in both the main and
258 	 * sigsuspender threads, does not cause the signal handler
259 	 * to be called.
260 	 */
261 	send_thread_signal (suspender_tid, SIGUSR1);
262 	sleep (1);
263 	send_process_signal (SIGUSR1);
264 	sleep (1);
265 	if (sigcounts[SIGUSR1] != 0)
266 		fprintf (stderr, "FAIL: signal hander called for SIGUSR1.\n");
267 
268 	/*
269 	 * Verify that we can still kill the process for a signal
270 	 * not being waited on by sigwait.
271 	 */
272 	send_process_signal (SIGPIPE);
273 	fprintf (stderr, "FAIL: SIGPIPE did not terminate process.\n");
274 
275 	/*
276 	 * Wait for the thread to finish.
277 	 */
278 	pthread_join (suspender_tid, &exit_status);
279 
280 	return (0);
281 }
282