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