xref: /openbsd-src/regress/lib/libpthread/sigwait/sigwait.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: sigwait.c,v 1.1.1.1 2001/08/15 14:37:16 fgsch Exp $	*/
2 /*
3  * Copyright (c) 1998 Daniel M. Eischen <eischen@vigrid.com>
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 Daniel M. Eischen.
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 DANIEL M. EISCHEN AND CONTRIBUTORS ``AS IS''
22  * AND 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 REGENTS 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  */
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #include <errno.h>
38 #include <pthread.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include <pthread_np.h>
44 #include "test.h"
45 
46 static int		sigcounts[NSIG + 1];
47 static sigset_t		wait_mask;
48 static pthread_mutex_t	waiter_mutex;
49 
50 
51 static void *
52 sigwaiter (void *arg)
53 {
54 	int signo;
55 	sigset_t mask;
56 
57 	SET_NAME("sigwaiter");
58 
59 	/* Block SIGHUP */
60 	sigemptyset (&mask);
61 	sigaddset (&mask, SIGHUP);
62 	CHECKe(sigprocmask (SIG_BLOCK, &mask, NULL));
63 
64 	while (sigcounts[SIGINT] == 0) {
65 		printf("Sigwait waiting (thread %p)\n", pthread_self());
66 		CHECKe(sigwait (&wait_mask, &signo));
67 		sigcounts[signo]++;
68 		printf ("Sigwait caught signal %d (%s)\n", signo,
69 		    strsignal(signo));
70 
71 		/* Allow the main thread to prevent the sigwait. */
72 		CHECKr(pthread_mutex_lock (&waiter_mutex));
73 		CHECKr(pthread_mutex_unlock (&waiter_mutex));
74 	}
75 
76 	return (arg);
77 }
78 
79 
80 static void
81 sighandler (int signo)
82 {
83 	printf ("  -> Signal handler caught signal %d (%s) in thread %p\n",
84 	    signo, strsignal(signo), pthread_self());
85 
86 	if ((signo >= 0) && (signo <= NSIG))
87 		sigcounts[signo]++;
88 }
89 
90 int main (int argc, char *argv[])
91 {
92 	pthread_mutexattr_t mattr;
93 	pthread_attr_t	pattr;
94 	pthread_t	tid;
95 	struct sigaction act;
96 
97 	/* Initialize our signal counts. */
98 	memset ((void *) sigcounts, 0, NSIG * sizeof (int));
99 
100 	/* Setupt our wait mask. */
101 	sigemptyset (&wait_mask);		/* Default action	*/
102 	sigaddset (&wait_mask, SIGHUP);		/* terminate		*/
103 	sigaddset (&wait_mask, SIGINT);		/* terminate		*/
104 	sigaddset (&wait_mask, SIGQUIT);	/* create core image	*/
105 	sigaddset (&wait_mask, SIGURG);		/* ignore		*/
106 	sigaddset (&wait_mask, SIGIO);		/* ignore		*/
107 	sigaddset (&wait_mask, SIGUSR1);	/* terminate		*/
108 
109 	/* Ignore signals SIGHUP and SIGIO. */
110 	sigemptyset (&act.sa_mask);
111 	sigaddset (&act.sa_mask, SIGHUP);
112 	sigaddset (&act.sa_mask, SIGIO);
113 	act.sa_handler = SIG_IGN;
114 	act.sa_flags = 0;
115 	CHECKe(sigaction (SIGHUP, &act, NULL));
116 	CHECKe(sigaction (SIGIO, &act, NULL));
117 
118 	/* Install a signal handler for SIGURG */
119 	sigemptyset (&act.sa_mask);
120 	sigaddset (&act.sa_mask, SIGURG);
121 	act.sa_handler = sighandler;
122 	act.sa_flags = SA_RESTART;
123 	CHECKe(sigaction (SIGURG, &act, NULL));
124 
125 	/* Install a signal handler for SIGXCPU */
126 	sigemptyset (&act.sa_mask);
127 	sigaddset (&act.sa_mask, SIGXCPU);
128 	CHECKe(sigaction (SIGXCPU, &act, NULL));
129 
130 	/*
131 	 * Initialize the thread attribute.
132 	 */
133 	CHECKr(pthread_attr_init (&pattr));
134 	CHECKr(pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE));
135 
136 	/*
137 	 * Initialize and create a mutex.
138 	 */
139 	CHECKr(pthread_mutexattr_init (&mattr));
140 	CHECKr(pthread_mutex_init (&waiter_mutex, &mattr));
141 
142 	/*
143 	 * Create the sigwaiter thread.
144 	 */
145 	CHECKr(pthread_create (&tid, &pattr, sigwaiter, NULL));
146 
147 	/*
148 	 * Verify that an ignored signal doesn't cause a wakeup.
149 	 * We don't have a handler installed for SIGIO.
150 	 */
151 	CHECKr(pthread_kill (tid, SIGIO));
152 	sleep (1);
153 	CHECKe(kill(getpid(), SIGIO));
154 	sleep (1);
155 	/* sigwait should not wake up for ignored signal SIGIO */
156 	ASSERT(sigcounts[SIGIO] == 0);
157 
158 	/*
159 	 * Verify that a signal with a default action of ignore, for
160 	 * which we have a signal handler installed, will release a sigwait.
161 	 */
162 	CHECKr(pthread_kill (tid, SIGURG));
163 	sleep (1);
164 	CHECKe(kill(getpid(), SIGURG));
165 	sleep (1);
166 	/* sigwait should wake up for SIGURG */
167 	ASSERT(sigcounts[SIGURG] == 2);
168 
169 	/*
170 	 * Verify that a signal with a default action that terminates
171 	 * the process will release a sigwait.
172 	 */
173 	CHECKr(pthread_kill (tid, SIGUSR1));
174 	sleep (1);
175 	CHECKe(kill(getpid(), SIGUSR1));
176 	sleep (1);
177 	if (sigcounts[SIGUSR1] != 2)
178 		printf ("FAIL: sigwait doesn't wake up for SIGUSR1.\n");
179 
180 	/*
181 	 * Verify that if we install a signal handler for a previously
182 	 * ignored signal, an occurrence of this signal will release
183 	 * the (already waiting) sigwait.
184 	 */
185 
186 	/* Install a signal handler for SIGHUP. */
187 	sigemptyset (&act.sa_mask);
188 	sigaddset (&act.sa_mask, SIGHUP);
189 	act.sa_handler = sighandler;
190 	act.sa_flags = SA_RESTART;
191 	CHECKe(sigaction (SIGHUP, &act, NULL));
192 
193 	/* Sending SIGHUP should release the sigwait. */
194 	CHECKe(kill(getpid(), SIGHUP));
195 	sleep (1);
196 	CHECKr(pthread_kill (tid, SIGHUP));
197 	sleep (1);
198 	/* sigwait should wake up for SIGHUP */
199 	ASSERT(sigcounts[SIGHUP] == 2);
200 
201 	/*
202 	 * Verify that a pending signal in the waiters mask will
203 	 * cause sigwait to return the pending signal.  We do this
204 	 * by taking the waiters mutex and signaling the waiter to
205 	 * release him from the sigwait.  The waiter will block
206 	 * on taking the mutex, and we can then send the waiter a
207 	 * signal which should be added to his pending signals.
208 	 * The next time the waiter does a sigwait, he should
209 	 * return with the pending signal.
210 	 */
211 	sigcounts[SIGHUP] = 0;
212  	CHECKr(pthread_mutex_lock (&waiter_mutex));
213 	/* Release the waiter from sigwait. */
214 	CHECKe(kill(getpid(), SIGHUP));
215 	sleep (1);
216 	/* sigwait should wake up for SIGHUP */
217 	ASSERT(sigcounts[SIGHUP] == 1);
218 	/*
219 	 * Add SIGHUP to all threads pending signals.  Since there is
220 	 * a signal handler installed for SIGHUP and this signal is
221 	 * blocked from the waiter thread and unblocked in the main
222 	 * thread, the signal handler should be called once for SIGHUP.
223 	 */
224 	CHECKe(kill(getpid(), SIGHUP));
225 	/* Release the waiter thread and allow him to run. */
226 	CHECKr(pthread_mutex_unlock (&waiter_mutex));
227 	sleep (1);
228 	/* sigwait should return for pending SIGHUP */
229 	ASSERT(sigcounts[SIGHUP] == 3);
230 
231 	/*
232 	 * Repeat the above test using pthread_kill and SIGUSR1
233 	 */
234 	sigcounts[SIGUSR1] = 0;
235  	CHECKr(pthread_mutex_lock (&waiter_mutex));
236 	/* Release the waiter from sigwait. */
237 	CHECKr(pthread_kill (tid, SIGUSR1));
238 	sleep (1);
239 	/* sigwait should wake up for SIGUSR1 */
240 	ASSERT(sigcounts[SIGUSR1] == 1);
241 	/* Add SIGHUP to the waiters pending signals. */
242 	CHECKr(pthread_kill (tid, SIGUSR1));
243 	/* Release the waiter thread and allow him to run. */
244 	CHECKe(pthread_mutex_unlock (&waiter_mutex));
245 	sleep (1);
246 	/* sigwait should return for pending SIGUSR1 */
247 	ASSERT(sigcounts[SIGUSR1] == 2);
248 
249 #if 0
250 	/*
251 	 * Verify that we can still kill the process for a signal
252 	 * not being waited on by sigwait.
253 	 */
254 	CHECKe(kill(getpid(), SIGPIPE));
255 	PANIC("SIGPIPE did not terminate process");
256 
257 	/*
258 	 * Wait for the thread to finish.
259 	 */
260 	CHECKr(pthread_join (tid, NULL));
261 #endif
262 
263 	SUCCEED;
264 }
265