ipfw3: list loaded modules
[dragonfly.git] / lib / libc_r / test / sigwait_d.c
blob32ec72cb9b5990d76b6ec43848ffe7695dc9c188
1 /*
2 * Copyright (c) 1998 Daniel M. Eischen <eischen@vigrid.com>
3 * All rights reserved.
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.
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.
32 * $FreeBSD: src/lib/libc_r/test/sigwait_d.c,v 1.1.2.2 2001/06/22 21:44:27 jasone Exp $
33 * $DragonFly: src/lib/libc_r/test/sigwait_d.c,v 1.2 2003/06/17 04:26:48 dillon Exp $
35 #include <stdlib.h>
36 #include <unistd.h>
38 #include <errno.h>
39 #include <pthread.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <string.h>
44 #if defined(_LIBC_R_)
45 #include <pthread_np.h>
46 #endif
48 static int sigcounts[NSIG + 1];
49 static sigset_t wait_mask;
50 static pthread_mutex_t waiter_mutex;
53 static void *
54 sigwaiter (void *arg)
56 int signo;
57 sigset_t mask;
59 /* Block SIGHUP */
60 sigemptyset (&mask);
61 sigaddset (&mask, SIGHUP);
62 sigprocmask (SIG_BLOCK, &mask, NULL);
64 while (sigcounts[SIGINT] == 0) {
65 if (sigwait (&wait_mask, &signo) != 0) {
66 fprintf (stderr,
67 "Unable to wait for signal, errno %d\n",
68 errno);
69 exit (1);
71 sigcounts[signo]++;
72 fprintf (stderr, "Sigwait caught signal %d\n", signo);
74 /* Allow the main thread to prevent the sigwait. */
75 pthread_mutex_lock (&waiter_mutex);
76 pthread_mutex_unlock (&waiter_mutex);
79 pthread_exit (arg);
80 return (NULL);
84 static void
85 sighandler (int signo)
87 fprintf (stderr, " -> Signal handler caught signal %d\n", signo);
89 if ((signo >= 0) && (signo <= NSIG))
90 sigcounts[signo]++;
93 static void
94 send_thread_signal (pthread_t tid, int signo)
96 if (pthread_kill (tid, signo) != 0) {
97 fprintf (stderr, "Unable to send thread signal, errno %d.\n",
98 errno);
99 exit (1);
103 static void
104 send_process_signal (int signo)
106 if (kill (getpid (), signo) != 0) {
107 fprintf (stderr, "Unable to send process signal, errno %d.\n",
108 errno);
109 exit (1);
114 int main (int argc, char *argv[])
116 pthread_mutexattr_t mattr;
117 pthread_attr_t pattr;
118 pthread_t tid;
119 void * exit_status;
120 struct sigaction act;
122 /* Initialize our signal counts. */
123 memset ((void *) sigcounts, 0, NSIG * sizeof (int));
125 /* Setup our wait mask. */
126 sigemptyset (&wait_mask); /* Default action */
127 sigaddset (&wait_mask, SIGHUP); /* terminate */
128 sigaddset (&wait_mask, SIGINT); /* terminate */
129 sigaddset (&wait_mask, SIGQUIT); /* create core image */
130 sigaddset (&wait_mask, SIGURG); /* ignore */
131 sigaddset (&wait_mask, SIGIO); /* ignore */
132 sigaddset (&wait_mask, SIGUSR1); /* terminate */
134 /* Ignore signals SIGHUP and SIGIO. */
135 sigemptyset (&act.sa_mask);
136 sigaddset (&act.sa_mask, SIGHUP);
137 sigaddset (&act.sa_mask, SIGIO);
138 act.sa_handler = SIG_IGN;
139 act.sa_flags = 0;
140 sigaction (SIGHUP, &act, NULL);
141 sigaction (SIGIO, &act, NULL);
143 /* Install a signal handler for SIGURG */
144 sigemptyset (&act.sa_mask);
145 sigaddset (&act.sa_mask, SIGURG);
146 act.sa_handler = sighandler;
147 act.sa_flags = SA_RESTART;
148 sigaction (SIGURG, &act, NULL);
150 /* Install a signal handler for SIGXCPU */
151 sigemptyset (&act.sa_mask);
152 sigaddset (&act.sa_mask, SIGXCPU);
153 sigaction (SIGXCPU, &act, NULL);
156 * Initialize the thread attribute.
158 if ((pthread_attr_init (&pattr) != 0) ||
159 (pthread_attr_setdetachstate (&pattr,
160 PTHREAD_CREATE_JOINABLE) != 0)) {
161 fprintf (stderr, "Unable to initialize thread attributes.\n");
162 exit (1);
166 * Initialize and create a mutex.
168 if ((pthread_mutexattr_init (&mattr) != 0) ||
169 (pthread_mutex_init (&waiter_mutex, &mattr) != 0)) {
170 fprintf (stderr, "Unable to create waiter mutex.\n");
171 exit (1);
175 * Create the sigwaiter thread.
177 if (pthread_create (&tid, &pattr, sigwaiter, NULL) != 0) {
178 fprintf (stderr, "Unable to create thread.\n");
179 exit (1);
181 #if defined(_LIBC_R_)
182 pthread_set_name_np (tid, "sigwaiter");
183 #endif
186 * Verify that an ignored signal doesn't cause a wakeup.
187 * We don't have a handler installed for SIGIO.
189 send_thread_signal (tid, SIGIO);
190 sleep (1);
191 send_process_signal (SIGIO);
192 sleep (1);
193 if (sigcounts[SIGIO] != 0)
194 fprintf (stderr,
195 "FAIL: sigwait wakes up for ignored signal SIGIO.\n");
198 * Verify that a signal with a default action of ignore, for
199 * which we have a signal handler installed, will release a sigwait.
201 send_thread_signal (tid, SIGURG);
202 sleep (1);
203 send_process_signal (SIGURG);
204 sleep (1);
205 if (sigcounts[SIGURG] != 2)
206 fprintf (stderr, "FAIL: sigwait doesn't wake up for SIGURG.\n");
209 * Verify that a signal with a default action that terminates
210 * the process will release a sigwait.
212 send_thread_signal (tid, SIGUSR1);
213 sleep (1);
214 send_process_signal (SIGUSR1);
215 sleep (1);
216 if (sigcounts[SIGUSR1] != 2)
217 fprintf (stderr,
218 "FAIL: sigwait doesn't wake up for SIGUSR1.\n");
221 * Verify that if we install a signal handler for a previously
222 * ignored signal, an occurrence of this signal will release
223 * the (already waiting) sigwait.
226 /* Install a signal handler for SIGHUP. */
227 sigemptyset (&act.sa_mask);
228 sigaddset (&act.sa_mask, SIGHUP);
229 act.sa_handler = sighandler;
230 act.sa_flags = SA_RESTART;
231 sigaction (SIGHUP, &act, NULL);
233 /* Sending SIGHUP should release the sigwait. */
234 send_process_signal (SIGHUP);
235 sleep (1);
236 send_thread_signal (tid, SIGHUP);
237 sleep (1);
238 if (sigcounts[SIGHUP] != 2)
239 fprintf (stderr, "FAIL: sigwait doesn't wake up for SIGHUP.\n");
242 * Verify that a pending signal in the waiters mask will
243 * cause sigwait to return the pending signal. We do this
244 * by taking the waiters mutex and signaling the waiter to
245 * release him from the sigwait. The waiter will block
246 * on taking the mutex, and we can then send the waiter a
247 * signal which should be added to his pending signals.
248 * The next time the waiter does a sigwait, he should
249 * return with the pending signal.
251 sigcounts[SIGHUP] = 0;
252 pthread_mutex_lock (&waiter_mutex);
253 /* Release the waiter from sigwait. */
254 send_process_signal (SIGHUP);
255 sleep (1);
256 if (sigcounts[SIGHUP] != 1)
257 fprintf (stderr, "FAIL: sigwait doesn't wake up for SIGHUP.\n");
259 * Add SIGHUP to the process pending signals. Since there is
260 * a signal handler installed for SIGHUP and this signal is
261 * blocked from the waiter thread and unblocked in the main
262 * thread, the signal handler should be called once for SIGHUP.
264 send_process_signal (SIGHUP);
265 /* Release the waiter thread and allow him to run. */
266 pthread_mutex_unlock (&waiter_mutex);
267 sleep (1);
268 if (sigcounts[SIGHUP] != 2)
269 fprintf (stderr,
270 "FAIL: sigwait doesn't return for pending SIGHUP.\n");
273 * Repeat the above test using pthread_kill and SIGUSR1.
275 sigcounts[SIGUSR1] = 0;
276 pthread_mutex_lock (&waiter_mutex);
277 /* Release the waiter from sigwait. */
278 send_thread_signal (tid, SIGUSR1);
279 sleep (1);
280 if (sigcounts[SIGUSR1] != 1)
281 fprintf (stderr,
282 "FAIL: sigwait doesn't wake up for SIGUSR1.\n");
283 /* Add SIGUSR1 to the waiters pending signals. */
284 send_thread_signal (tid, SIGUSR1);
285 /* Release the waiter thread and allow him to run. */
286 pthread_mutex_unlock (&waiter_mutex);
287 sleep (1);
288 if (sigcounts[SIGUSR1] != 2)
289 fprintf (stderr,
290 "FAIL: sigwait doesn't return for pending SIGUSR1.\n");
293 * Verify that we can still kill the process for a signal
294 * not being waited on by sigwait.
296 send_process_signal (SIGPIPE);
297 fprintf (stderr, "FAIL: SIGPIPE did not terminate process.\n");
300 * Wait for the thread to finish.
302 pthread_join (tid, &exit_status);
304 return (0);