Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libc_r / test / sigsuspend_d.c
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 <signal.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #if defined(_LIBC_R_)
44 #include <pthread_np.h>
45 #endif
46
47 static int      sigcounts[NSIG + 1];
48 static int      sigfifo[NSIG + 1];
49 static int      fifo_depth = 0;
50 static sigset_t suspender_mask;
51 static pthread_t suspender_tid;
52
53
54 static void *
55 sigsuspender (void *arg)
56 {
57         int save_count, status, i;
58         sigset_t run_mask;
59
60         /* Run with all signals blocked. */
61         sigfillset (&run_mask);
62         sigprocmask (SIG_SETMASK, &run_mask, NULL);
63
64         /* Allow these signals to wake us up during a sigsuspend. */
65         sigfillset (&suspender_mask);           /* Default action       */
66         sigdelset (&suspender_mask, SIGINT);    /* terminate            */
67         sigdelset (&suspender_mask, SIGHUP);    /* terminate            */
68         sigdelset (&suspender_mask, SIGQUIT);   /* create core image    */
69         sigdelset (&suspender_mask, SIGURG);    /* ignore               */
70         sigdelset (&suspender_mask, SIGIO);     /* ignore               */
71         sigdelset (&suspender_mask, SIGUSR2);   /* terminate            */
72
73         while (sigcounts[SIGINT] == 0) {
74                 save_count = sigcounts[SIGUSR2];
75
76                 status = sigsuspend (&suspender_mask);
77                 if ((status == 0) || (errno != EINTR)) {
78                         fprintf (stderr, "Unable to suspend for signals, "
79                                 "errno %d, return value %d\n",
80                                 errno, status);
81                         exit (1);
82                 }
83                 for (i = 0; i < fifo_depth; i++)
84                         fprintf (stderr, "Sigsuspend woke up by signal %d\n",
85                                 sigfifo[i]);
86                 fifo_depth = 0;
87         }
88
89         pthread_exit (arg);
90         return (NULL);
91 }
92
93
94 static void
95 sighandler (int signo)
96 {
97         sigset_t set, suspend_set;
98         pthread_t self;
99
100         if ((signo >= 0) && (signo <= NSIG))
101                 sigcounts[signo]++;
102
103         /*
104          * If we are running on behalf of the suspender thread,
105          * ensure that we have the correct mask set.
106          */
107         self = pthread_self ();
108         if (self == suspender_tid) {
109                 sigfifo[fifo_depth] = signo;
110                 fifo_depth++;
111                 fprintf (stderr,
112                     "  -> Suspender thread signal handler caught signal %d\n",
113                     signo);
114
115                 /* Get the current signal mask. */
116                 sigprocmask (SIG_SETMASK, NULL, &set);
117
118                 /* The handler should run with the current signal masked. */
119                 suspend_set = suspender_mask;
120                 sigaddset(&suspend_set, signo);
121
122                 if (memcmp(&set, &suspend_set, sizeof(set)))
123                         fprintf (stderr,
124                             "  >>> FAIL: sigsuspender signal handler running "
125                             "with incorrect mask.\n");
126         }
127         else
128                 fprintf (stderr,
129                     "  -> Main thread signal handler caught signal %d\n",
130                     signo);
131 }
132
133
134 static void
135 send_thread_signal (pthread_t tid, int signo)
136 {
137         if (pthread_kill (tid, signo) != 0) {
138                 fprintf (stderr, "Unable to send thread signal, errno %d.\n",
139                     errno);
140                 exit (1);
141         }
142 }
143
144
145 static void
146 send_process_signal (int signo)
147 {
148         if (kill (getpid (), signo) != 0) {
149                 fprintf (stderr, "Unable to send process signal, errno %d.\n",
150                     errno);
151                 exit (1);
152         }
153 }
154
155
156 int main (int argc, char *argv[])
157 {
158         pthread_attr_t  pattr;
159         void *          exit_status;
160         struct sigaction act;
161         sigset_t        oldset;
162         sigset_t        newset;
163
164         /* Initialize our signal counts. */
165         memset ((void *) sigcounts, 0, NSIG * sizeof (int));
166
167         /* Ignore signal SIGIO. */
168         sigemptyset (&act.sa_mask);
169         sigaddset (&act.sa_mask, SIGIO);
170         act.sa_handler = SIG_IGN;
171         act.sa_flags = 0;
172         sigaction (SIGIO, &act, NULL);
173
174         /* Install a signal handler for SIGURG. */
175         sigemptyset (&act.sa_mask);
176         sigaddset (&act.sa_mask, SIGURG);
177         act.sa_handler = sighandler;
178         act.sa_flags = SA_RESTART;
179         sigaction (SIGURG, &act, NULL);
180
181         /* Install a signal handler for SIGXCPU */
182         sigemptyset (&act.sa_mask);
183         sigaddset (&act.sa_mask, SIGXCPU);
184         sigaction (SIGXCPU, &act, NULL);
185
186         /* Get our current signal mask. */
187         sigprocmask (SIG_SETMASK, NULL, &oldset);
188
189         /* Mask out SIGUSR1 and SIGUSR2. */
190         newset = oldset;
191         sigaddset (&newset, SIGUSR1);
192         sigaddset (&newset, SIGUSR2);
193         sigprocmask (SIG_SETMASK, &newset, NULL);
194
195         /* Install a signal handler for SIGUSR1 */
196         sigemptyset (&act.sa_mask);
197         sigaddset (&act.sa_mask, SIGUSR1);
198         sigaction (SIGUSR1, &act, NULL);
199
200         /* Install a signal handler for SIGUSR2 */
201         sigemptyset (&act.sa_mask);
202         sigaddset (&act.sa_mask, SIGUSR2);
203         sigaction (SIGUSR2, &act, NULL);
204
205         /*
206          * Initialize the thread attribute.
207          */
208         if ((pthread_attr_init (&pattr) != 0) ||
209             (pthread_attr_setdetachstate (&pattr,
210             PTHREAD_CREATE_JOINABLE) != 0)) {
211                 fprintf (stderr, "Unable to initialize thread attributes.\n");
212                 exit (1);
213         }
214
215         /*
216          * Create the sigsuspender thread.
217          */
218         if (pthread_create (&suspender_tid, &pattr, sigsuspender, NULL) != 0) {
219                 fprintf (stderr, "Unable to create thread, errno %d.\n", errno);
220                 exit (1);
221         }
222 #if defined(_LIBC_R)
223         pthread_set_name_np (suspender_tid, "sigsuspender");
224 #endif
225
226         /*
227          * Verify that an ignored signal doesn't cause a wakeup.
228          * We don't have a handler installed for SIGIO.
229          */
230         send_thread_signal (suspender_tid, SIGIO);
231         sleep (1);
232         send_process_signal (SIGIO);
233         sleep (1);
234         if (sigcounts[SIGIO] != 0)
235                 fprintf (stderr, "FAIL: sigsuspend wakes up for ignored signal "
236                         "SIGIO.\n");
237
238         /*
239          * Verify that a signal with a default action of ignore, for
240          * which we have a signal handler installed, will release a
241          * sigsuspend.
242          */
243         send_thread_signal (suspender_tid, SIGURG);
244         sleep (1);
245         send_process_signal (SIGURG);
246         sleep (1);
247         if (sigcounts[SIGURG] != 2)
248                 fprintf (stderr,
249                     "FAIL: sigsuspend doesn't wake up for SIGURG.\n");
250
251         /*
252          * Verify that a SIGUSR2 signal will release a sigsuspended
253          * thread.
254          */
255         send_thread_signal (suspender_tid, SIGUSR2);
256         sleep (1);
257         send_process_signal (SIGUSR2);
258         sleep (1);
259         if (sigcounts[SIGUSR2] != 2)
260                 fprintf (stderr,
261                     "FAIL: sigsuspend doesn't wake up for SIGUSR2.\n");
262
263         /*
264          * Verify that a signal, blocked in both the main and
265          * sigsuspender threads, does not cause the signal handler
266          * to be called.
267          */
268         send_thread_signal (suspender_tid, SIGUSR1);
269         sleep (1);
270         send_process_signal (SIGUSR1);
271         sleep (1);
272         if (sigcounts[SIGUSR1] != 0)
273                 fprintf (stderr, "FAIL: signal hander called for SIGUSR1.\n");
274
275         /*
276          * Verify that we can still kill the process for a signal
277          * not being waited on by sigwait.
278          */
279         send_process_signal (SIGPIPE);
280         fprintf (stderr, "FAIL: SIGPIPE did not terminate process.\n");
281
282         /*
283          * Wait for the thread to finish.
284          */
285         pthread_join (suspender_tid, &exit_status);
286
287         return (0);
288 }