there is no elvis.
[minix.git] / test / test37.c
blobd6e5c6f287b256f7d7288d76f274d4ac8631f627
1 /* test 37 - signals */
3 #include <sys/types.h>
4 #include <sys/times.h>
5 #ifdef _MINIX
6 #include <sys/sigcontext.h>
7 #endif
8 #include <sys/wait.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <setjmp.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
16 #define ITERATIONS 2
17 #define SIGS 14
18 #define MAX_ERROR 4
20 int iteration, cumsig, subtest, errct = 0, sig1, sig2;
22 int sigarray[SIGS] = {SIGHUP, SIGILL, SIGTRAP, SIGABRT, SIGIOT,
23 SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM,
24 SIGTERM};
26 /* Prototypes produced automatically by mkptypes. */
27 _PROTOTYPE(int main, (int argc, char *argv []));
28 _PROTOTYPE(void test37a, (void));
29 _PROTOTYPE(void func1, (int sig));
30 _PROTOTYPE(void func2, (int sig));
31 _PROTOTYPE(void test37b, (void));
32 _PROTOTYPE(void catch1, (int signo));
33 _PROTOTYPE(void catch2, (int signo));
34 _PROTOTYPE(void test37c, (void));
35 _PROTOTYPE(void catch3, (int signo));
36 _PROTOTYPE(void test37d, (void));
37 _PROTOTYPE(void catch4, (int signo));
38 _PROTOTYPE(void test37e, (void));
39 _PROTOTYPE(void catch5, (int signo));
40 _PROTOTYPE(void test37f, (void));
41 _PROTOTYPE(void sigint_handler, (int signo));
42 _PROTOTYPE(void sigpipe_handler, (int signo));
43 _PROTOTYPE(void test37g, (void));
44 _PROTOTYPE(void sighup8, (int signo));
45 _PROTOTYPE(void sigpip8, (int signo));
46 _PROTOTYPE(void sigter8, (int signo));
47 _PROTOTYPE(void test37h, (void));
48 _PROTOTYPE(void sighup9, (int signo));
49 _PROTOTYPE(void sigter9, (int signo));
50 _PROTOTYPE(void test37i, (void));
51 _PROTOTYPE(void sighup10, (int signo));
52 _PROTOTYPE(void sigalrm_handler10, (int signo));
53 _PROTOTYPE(void test37j, (void));
54 _PROTOTYPE(void test37k, (void));
55 _PROTOTYPE(void test37l, (void));
56 _PROTOTYPE(void func_m1, (void));
57 _PROTOTYPE(void func_m2, (void));
58 _PROTOTYPE(void test37m, (void));
59 _PROTOTYPE(void longjerr, (void));
60 _PROTOTYPE(void catch14, (int signo, int code, struct sigcontext * scp));
61 _PROTOTYPE(void test37n, (void));
62 _PROTOTYPE(void catch15, (int signo));
63 _PROTOTYPE(void test37o, (void));
64 _PROTOTYPE(void clearsigstate, (void));
65 _PROTOTYPE(void quit, (void));
66 _PROTOTYPE(void wait_for, (int pid));
67 _PROTOTYPE(void e, (int n));
69 int main(argc, argv)
70 int argc;
71 char *argv[];
73 int i, m = 0xFFFF;
75 sync();
77 if (argc == 2) m = atoi(argv[1]);
79 printf("Test 37 ");
80 fflush(stdout); /* have to flush for child's benefit */
82 system("rm -rf DIR_37; mkdir DIR_37");
83 chdir("DIR_37");
85 for (i = 0; i < ITERATIONS; i++) {
86 iteration = i;
87 if (m & 0000001) test37a();
88 if (m & 0000002) test37b();
89 if (m & 0000004) test37c();
90 if (m & 0000010) test37d();
91 if (m & 0000020) test37e();
92 if (m & 0000040) test37f();
93 if (m & 0000100) test37g();
94 if (m & 0000200) test37h();
95 if (m & 0000400) test37i();
96 if (m & 0001000) test37j();
97 if (m & 0002000) test37k();
98 if (m & 0004000) test37l();
99 if (m & 0010000) test37m();
100 if (m & 0020000) test37n();
101 if (m & 0040000) test37o();
104 quit();
105 return(-1); /* impossible */
108 void test37a()
110 /* Test signal set management. */
112 sigset_t s;
114 subtest = 1;
115 clearsigstate();
117 /* Create an empty set and see if any bits are on. */
118 if (sigemptyset(&s) != 0) e(1);
119 if (sigismember(&s, SIGHUP) != 0) e(2);
120 if (sigismember(&s, SIGINT) != 0) e(3);
121 if (sigismember(&s, SIGQUIT) != 0) e(4);
122 if (sigismember(&s, SIGILL) != 0) e(5);
123 if (sigismember(&s, SIGTRAP) != 0) e(6);
124 if (sigismember(&s, SIGABRT) != 0) e(7);
125 if (sigismember(&s, SIGIOT) != 0) e(8);
126 if (sigismember(&s, SIGFPE) != 0) e(10);
127 if (sigismember(&s, SIGKILL) != 0) e(11);
128 if (sigismember(&s, SIGUSR1) != 0) e(12);
129 if (sigismember(&s, SIGSEGV) != 0) e(13);
130 if (sigismember(&s, SIGUSR2) != 0) e(14);
131 if (sigismember(&s, SIGPIPE) != 0) e(15);
132 if (sigismember(&s, SIGALRM) != 0) e(16);
133 if (sigismember(&s, SIGTERM) != 0) e(17);
135 /* Create a full set and see if any bits are off. */
136 if (sigfillset(&s) != 0) e(19);
137 if (sigemptyset(&s) != 0) e(20);
138 if (sigfillset(&s) != 0) e(21);
139 if (sigismember(&s, SIGHUP) != 1) e(22);
140 if (sigismember(&s, SIGINT) != 1) e(23);
141 if (sigismember(&s, SIGQUIT) != 1) e(24);
142 if (sigismember(&s, SIGILL) != 1) e(25);
143 if (sigismember(&s, SIGTRAP) != 1) e(26);
144 if (sigismember(&s, SIGABRT) != 1) e(27);
145 if (sigismember(&s, SIGIOT) != 1) e(28);
146 if (sigismember(&s, SIGFPE) != 1) e(30);
147 if (sigismember(&s, SIGKILL) != 1) e(31);
148 if (sigismember(&s, SIGUSR1) != 1) e(32);
149 if (sigismember(&s, SIGSEGV) != 1) e(33);
150 if (sigismember(&s, SIGUSR2) != 1) e(34);
151 if (sigismember(&s, SIGPIPE) != 1) e(35);
152 if (sigismember(&s, SIGALRM) != 1) e(36);
153 if (sigismember(&s, SIGTERM) != 1) e(37);
155 /* Create an empty set, then turn on bits individually. */
156 if (sigemptyset(&s) != 0) e(39);
157 if (sigaddset(&s, SIGHUP) != 0) e(40);
158 if (sigaddset(&s, SIGINT) != 0) e(41);
159 if (sigaddset(&s, SIGQUIT) != 0) e(42);
160 if (sigaddset(&s, SIGILL) != 0) e(43);
161 if (sigaddset(&s, SIGTRAP) != 0) e(44);
163 /* See if the bits just turned on are indeed on. */
164 if (sigismember(&s, SIGHUP) != 1) e(45);
165 if (sigismember(&s, SIGINT) != 1) e(46);
166 if (sigismember(&s, SIGQUIT) != 1) e(47);
167 if (sigismember(&s, SIGILL) != 1) e(48);
168 if (sigismember(&s, SIGTRAP) != 1) e(49);
170 /* The others should be turned off. */
171 if (sigismember(&s, SIGABRT) != 0) e(50);
172 if (sigismember(&s, SIGIOT) != 0) e(51);
173 if (sigismember(&s, SIGFPE) != 0) e(53);
174 if (sigismember(&s, SIGKILL) != 0) e(54);
175 if (sigismember(&s, SIGUSR1) != 0) e(55);
176 if (sigismember(&s, SIGSEGV) != 0) e(56);
177 if (sigismember(&s, SIGUSR2) != 0) e(57);
178 if (sigismember(&s, SIGPIPE) != 0) e(58);
179 if (sigismember(&s, SIGALRM) != 0) e(59);
180 if (sigismember(&s, SIGTERM) != 0) e(60);
182 /* Now turn them off and see if all are off. */
183 if (sigdelset(&s, SIGHUP) != 0) e(62);
184 if (sigdelset(&s, SIGINT) != 0) e(63);
185 if (sigdelset(&s, SIGQUIT) != 0) e(64);
186 if (sigdelset(&s, SIGILL) != 0) e(65);
187 if (sigdelset(&s, SIGTRAP) != 0) e(66);
189 if (sigismember(&s, SIGHUP) != 0) e(67);
190 if (sigismember(&s, SIGINT) != 0) e(68);
191 if (sigismember(&s, SIGQUIT) != 0) e(69);
192 if (sigismember(&s, SIGILL) != 0) e(70);
193 if (sigismember(&s, SIGTRAP) != 0) e(71);
194 if (sigismember(&s, SIGABRT) != 0) e(72);
195 if (sigismember(&s, SIGIOT) != 0) e(73);
196 if (sigismember(&s, SIGFPE) != 0) e(75);
197 if (sigismember(&s, SIGKILL) != 0) e(76);
198 if (sigismember(&s, SIGUSR1) != 0) e(77);
199 if (sigismember(&s, SIGSEGV) != 0) e(78);
200 if (sigismember(&s, SIGUSR2) != 0) e(79);
201 if (sigismember(&s, SIGPIPE) != 0) e(80);
202 if (sigismember(&s, SIGALRM) != 0) e(81);
203 if (sigismember(&s, SIGTERM) != 0) e(82);
206 void func1(sig)
207 int sig;
209 sig1++;
212 void func2(sig)
213 int sig;
215 sig2++;
218 void test37b()
220 /* Test sigprocmask and sigpending. */
221 int i;
222 pid_t p;
223 sigset_t s, s1, s_empty, s_full, s_ill, s_ill_pip, s_nokill, s_nokill_stop;
224 struct sigaction sa, osa;
226 subtest = 2;
227 clearsigstate();
229 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
230 if (sigemptyset(&s_empty) != 0) e(1);
231 if (sigemptyset(&s_ill) != 0) e(2);
232 if (sigemptyset(&s_ill_pip) != 0) e(3);
233 if (sigaddset(&s_ill, SIGILL) != 0) e(4);
234 if (sigaddset(&s_ill_pip, SIGILL) != 0) e(5);
235 if (sigaddset(&s_ill_pip, SIGPIPE) != 0) e(6);
236 if (sigfillset(&s_full) != 0) e(7);
237 s_nokill = s_full;
238 if (sigdelset(&s_nokill, SIGKILL) != 0) e(8);
239 s_nokill_stop = s_nokill;
240 if (sigdelset(&s_nokill_stop, SIGSTOP) != 0) e(8);
241 #ifndef _MINIX /* XXX - should unsupported signals be <= _NSIG? */
242 if (SIGSTOP > _NSIG) e(666);
243 if (SIGSTOP <= _NSIG && sigdelset(&s_nokill, SIGSTOP) != 0) e(888);
244 #endif /* _MINIX */
246 /* Now get most of the signals into default state. Don't change SIGINT
247 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
249 sa.sa_handler = SIG_DFL;
250 sa.sa_mask = s_empty;
251 sa.sa_flags = 0;
252 for (i = 0; i < SIGS; i++) sigaction(i, &sa, &osa);
254 /* The second argument may be zero. See if it wipes out the system. */
255 for (i = 0; i < SIGS; i++) sigaction(i, (struct sigaction *) NULL, &osa);
257 /* Install a signal handler. */
258 sa.sa_handler = func1;
259 sa.sa_mask = s_ill;
260 sa.sa_flags = SA_NODEFER | SA_NOCLDSTOP;
261 osa.sa_handler = SIG_IGN;
262 osa.sa_mask = s_empty;
263 osa.sa_flags = 0;
264 if (sigaction(SIGHUP, &sa, &osa) != 0) e(9);
265 if (osa.sa_handler != SIG_DFL) e(10);
266 if (osa.sa_mask != 0) e(11);
267 if (osa.sa_flags != s_empty) e(12);
269 /* Replace action and see if old value is read back correctly. */
270 sa.sa_handler = func2;
271 sa.sa_mask = s_ill_pip;
272 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
273 osa.sa_handler = SIG_IGN;
274 osa.sa_mask = s_empty;
275 osa.sa_flags = 0;
276 if (sigaction(SIGHUP, &sa, &osa) != 0) e(13);
277 if (osa.sa_handler != func1) e(14);
278 if (osa.sa_mask != s_ill) e(15);
279 if (osa.sa_flags != SA_NODEFER
280 && osa.sa_flags != (SA_NODEFER | SA_NOCLDSTOP)) e(16);
282 /* Replace action once more and check what is read back. */
283 sa.sa_handler = SIG_DFL;
284 sa.sa_mask = s_empty;
285 osa.sa_handler = SIG_IGN;
286 osa.sa_mask = s_empty;
287 osa.sa_flags = 0;
288 if (sigaction(SIGHUP, &sa, &osa) != 0) e(17);
289 if (osa.sa_handler != func2) e(18);
290 if (osa.sa_mask != s_ill_pip) e(19);
291 if (osa.sa_flags != (SA_RESETHAND | SA_NODEFER)) e(20);
293 /* Test sigprocmask(SIG_SETMASK, ...). */
294 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(18); /* block all */
295 if (sigemptyset(&s1) != 0) e(19);
296 errno = 0;
297 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(20); /* block none */
298 if (s1 != s_nokill_stop) e(21);
299 if (sigprocmask(SIG_SETMASK, &s_ill, &s1) != 0) e(22); /* block SIGILL */
300 errno = 0;
301 if (s1 != s_empty) e(23);
302 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(24); /* SIGILL+PIP */
303 if (s1 != s_ill) e(25);
304 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(26); /* block all */
305 if (s1 != s_ill_pip) e(27);
307 /* Test sigprocmask(SIG_UNBLOCK, ...) */
308 if (sigprocmask(SIG_UNBLOCK, &s_ill, &s1) != 0) e(28);
309 if (s1 != s_nokill_stop) e(29);
310 if (sigprocmask(SIG_UNBLOCK, &s_ill_pip, &s1) != 0) e(30);
311 s = s_nokill_stop;
312 if (sigdelset(&s, SIGILL) != 0) e(31);
313 if (s != s1) e(32);
314 if (sigprocmask(SIG_UNBLOCK, &s_empty, &s1) != 0) e(33);
315 s = s_nokill_stop;
316 if (sigdelset(&s, SIGILL) != 0) e(34);
317 if (sigdelset(&s, SIGPIPE) != 0) e(35);
318 if (s != s1) e(36);
319 s1 = s_nokill_stop;
320 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(37);
321 if (s != s1) e(38);
323 /* Test sigprocmask(SIG_BLOCK, ...) */
324 if (sigprocmask(SIG_BLOCK, &s_ill, &s1) != 0) e(39);
325 if (s1 != s_empty) e(40);
326 if (sigprocmask(SIG_BLOCK, &s_ill_pip, &s1) != 0) e(41);
327 if (s1 != s_ill) e(42);
328 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(43);
329 if (s1 != s_ill_pip) e(44);
331 /* Check error condition. */
332 errno = 0;
333 if (sigprocmask(20000, &s_full, &s1) != -1) e(45);
334 if (errno != EINVAL) e(46);
335 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(47);
336 if (s1 != s_nokill_stop) e(48);
338 /* If second arg is 0, nothing is set. */
339 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(49);
340 if (s1 != s_nokill_stop) e(50);
341 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(51);
342 if (s1 != s_nokill_stop) e(52);
343 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(53);
344 if (s1 != s_ill_pip) e(54);
345 if (sigprocmask(SIG_BLOCK, (sigset_t *) NULL, &s1) != 0) e(55);
346 if (s1 != s_ill_pip) e(56);
347 if (sigprocmask(SIG_UNBLOCK, (sigset_t *) NULL, &s1) != 0) e(57);
348 if (s1 != s_ill_pip) e(58);
350 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
351 s = s_empty;
352 if (sigaddset(&s, SIGKILL) != 0) e(59);
353 if (sigprocmask(SIG_BLOCK, &s, &s1) != 0) e(60);
354 if (s1 != s_ill_pip) e(61);
355 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(62);
356 if (s1 != s_ill_pip) e(63);
358 /* Test sigpending. At this moment, all signals are blocked. */
359 sa.sa_handler = func2;
360 sa.sa_mask = s_empty;
361 if (sigaction(SIGHUP, &sa, &osa) != 0) e(64);
362 p = getpid();
363 kill(p, SIGHUP); /* send SIGHUP to self */
364 if (sigpending(&s) != 0) e(65);
365 if (sigemptyset(&s1) != 0) e(66);
366 if (sigaddset(&s1, SIGHUP) != 0) e(67);
367 if (s != s1) e(68);
368 sa.sa_handler = SIG_IGN;
369 if (sigaction(SIGHUP, &sa, &osa) != 0) e(69);
370 if (sigpending(&s) != 0) e(70);
371 if (s != s_empty) e(71);
374 /*---------------------------------------------------------------------------*/
375 int x;
376 sigset_t glo_vol_set;
378 void catch1(signo)
379 int signo;
381 x = 42;
384 void catch2(signo)
385 int signo;
387 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0)
388 e(1);
391 /* Verify that signal(2), which is now built on top of sigaction(2), still
392 * works.
394 void test37c()
396 pid_t pid;
397 sigset_t sigset_var;
399 subtest = 3;
400 clearsigstate();
401 x = 0;
403 /* Verify an installed signal handler persists across a fork(2). */
404 if (signal(SIGTERM, catch1) == SIG_ERR) e(1);
405 switch (pid = fork()) {
406 case 0: /* child */
407 errct = 0;
408 while (x == 0);
409 if (x != 42) e(2);
410 exit(errct == 0 ? 0 : 1);
411 case -1: e(3); break;
412 default: /* parent */
413 sleep(1);
414 if (kill(pid, SIGTERM) != 0) e(4);
415 wait_for(pid);
416 break;
419 /* Verify that the return value is the previous handler. */
420 signal(SIGINT, SIG_IGN);
421 if (signal(SIGINT, catch2) != SIG_IGN) e(5);
422 if (signal(SIGINT, catch1) != catch2) e(6);
423 if (signal(SIGINT, SIG_DFL) != catch1) e(7);
424 if (signal(SIGINT, catch1) != SIG_DFL) e(8);
425 if (signal(SIGINT, SIG_DFL) != catch1) e(9);
426 if (signal(SIGINT, SIG_DFL) != SIG_DFL) e(10);
427 if (signal(SIGINT, catch1) != SIG_DFL) e(11);
429 /* Verify that SIG_ERR is correctly generated. */
430 if (signal(_NSIG + 1, catch1) != SIG_ERR) e(12);
431 if (signal(0, catch1) != SIG_ERR) e(13);
432 if (signal(-1, SIG_DFL) != SIG_ERR) e(14);
434 /* Verify that caught signals are automatically reset to the default,
435 * and that further instances of the same signal are not blocked here
436 * or in the signal handler.
438 if (signal(SIGTERM, catch1) == SIG_ERR) e(15);
439 switch ((pid = fork())) {
440 case 0: /* child */
441 errct = 0;
442 while (x == 0);
443 if (x != 42) e(16);
444 if (sigismember((sigset_t *) &glo_vol_set, SIGTERM)) e(17);
445 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &sigset_var) != 0) e(18);
446 if (sigismember(&sigset_var, SIGTERM)) e(19);
448 #if 0
449 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
450 if (signal(SIGTERM, catch1) != SIG_DFL) e(20);
451 #else
452 if (signal(SIGTERM, catch1) != catch1) e(20);
453 #endif
454 exit(errct == 0 ? 0 : 1);
455 default: /* parent */
456 sleep(1);
457 if (kill(pid, SIGTERM) != 0) e(21);
458 wait_for(pid);
459 break;
460 case -1: e(22); break;
464 /*---------------------------------------------------------------------------*/
465 /* Test that the signal handler can be invoked recursively with the
466 * state being properly saved and restored.
469 static int y;
470 static int z;
472 void catch3(signo)
473 int signo;
475 if (z == 1) { /* catching a nested signal */
476 y = 2;
477 return;
479 z = 1;
480 if (kill(getpid(), SIGHUP) != 0) e(1);
481 while (y != 2);
482 y = 1;
485 void test37d()
487 struct sigaction act;
489 subtest = 4;
490 clearsigstate();
491 y = 0;
492 z = 0;
494 act.sa_handler = catch3;
495 act.sa_mask = 0;
496 act.sa_flags = SA_NODEFER; /* Otherwise, nested occurence of
497 * SIGINT is blocked. */
498 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) != 0) e(2);
499 if (kill(getpid(), SIGHUP) != 0) e(3);
500 if (y != 1) e(4);
503 /*---------------------------------------------------------------------------*/
505 /* Test that the signal mask in effect for the duration of a signal handler
506 * is as specified in POSIX Section 3, lines 718 -724. Test that the
507 * previous signal mask is restored when the signal handler returns.
510 void catch4(signo)
511 int signo;
513 sigset_t oset;
514 sigset_t set;
516 if (sigemptyset(&set) == -1) e(5001);
517 if (sigaddset(&set, SIGTERM) == -1) e(5002);
518 if (sigaddset(&set, SIGHUP) == -1) e(5003);
519 if (sigaddset(&set, SIGINT) == -1) e(5004);
520 if (sigaddset(&set, SIGPIPE) == -1) e(5005);
521 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) != 0) e(5006);
522 if (oset != set) e(5007);
525 void test37e()
527 struct sigaction act, oact;
528 sigset_t set, oset;
530 subtest = 5;
531 clearsigstate();
533 act.sa_handler = catch4;
534 sigemptyset(&act.sa_mask);
535 sigaddset(&act.sa_mask, SIGTERM);
536 sigaddset(&act.sa_mask, SIGHUP);
537 act.sa_flags = 0;
538 if (sigaction(SIGINT, &act, &oact) == -1) e(2);
540 if (sigemptyset(&set) == -1) e(3);
541 if (sigaddset(&set, SIGPIPE) == -1) e(4);
542 if (sigprocmask(SIG_SETMASK, &set, &oset) == -1) e(5);
543 if (kill(getpid(), SIGINT) == -1) e(6);
544 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) == -1) e(7);
545 if (sigemptyset(&set) == -1) e(8);
546 if (sigaddset(&set, SIGPIPE) == -1) e(9);
547 if (set != oset) e(10);
550 /*---------------------------------------------------------------------------*/
552 /* Test the basic functionality of sigsuspend(2). */
554 void catch5(signo)
555 int signo;
557 x = 1;
560 void test37f()
562 sigset_t set;
563 int r;
564 struct sigaction act;
565 pid_t pid;
567 subtest = 6;
568 clearsigstate();
570 switch (pid = fork()) {
571 case 0: /* child */
572 errct = 0;
573 sleep(1);
574 if (kill(getppid(), SIGINT) == -1) e(1);
575 exit(errct == 0 ? 0 : 1);
576 case -1: e(2); break;
577 default: /* parent */
578 if (sigemptyset(&act.sa_mask) == -1) e(3);
579 act.sa_flags = 0;
580 act.sa_handler = catch5;
581 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
583 if (sigemptyset(&set) == -1) e(5);
584 r = sigsuspend(&set);
586 if (r != -1 || errno != EINTR || x != 1) e(6);
587 wait_for(pid);
588 break;
592 /*----------------------------------------------------------------------*/
594 /* Test that sigsuspend() does block the signals specified in its
595 * argument, and after sigsuspend returns, the previous signal
596 * mask is restored.
598 * The child sends two signals to the parent SIGINT and then SIGPIPE,
599 * separated by a long delay. The parent executes sigsuspend() with
600 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
601 * will be invoked, then sigsuspend will return restoring the
602 * original signal mask, and then the SIGPIPE handler will be
603 * invoked.
606 void sigint_handler(signo)
607 int signo;
609 x = 1;
610 z++;
613 void sigpipe_handler(signo)
614 int signo;
616 x = 2;
617 z++;
620 void test37g()
622 sigset_t set;
623 int r;
624 struct sigaction act;
625 pid_t pid;
627 subtest = 7;
628 clearsigstate();
629 x = 0;
630 z = 0;
632 switch (pid = fork()) {
633 case 0: /* child */
634 errct = 0;
635 sleep(1);
636 if (kill(getppid(), SIGINT) == -1) e(1);
637 sleep(1);
638 if (kill(getppid(), SIGPIPE) == -1) e(2);
639 exit(errct == 0 ? 0 : 1);
640 case -1: e(3); break;
641 default: /* parent */
642 if (sigemptyset(&act.sa_mask) == -1) e(3);
643 act.sa_flags = 0;
644 act.sa_handler = sigint_handler;
645 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
647 act.sa_handler = sigpipe_handler;
648 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);
650 if (sigemptyset(&set) == -1) e(6);
651 if (sigaddset(&set, SIGINT) == -1) e(7);
652 r = sigsuspend(&set);
653 if (r != -1) e(8);
654 if (errno != EINTR) e(9);
655 if (z != 2) e(10);
656 if (x != 1) e(11);
657 wait_for(pid);
658 break;
662 /*--------------------------------------------------------------------------*/
664 /* Test that sigsuspend() does block the signals specified in its
665 * argument, and after sigsuspend returns, the previous signal
666 * mask is restored.
668 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
669 * and then SIGTERM, separated by a long delay. The parent executes
670 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
671 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
672 * will return restoring the original signal mask, and then the other
673 * two handlers will be invoked.
676 void sighup8(signo)
677 int signo;
679 x = 1;
680 z++;
683 void sigpip8(signo)
684 int signo;
686 x = 1;
687 z++;
690 void sigter8(signo)
691 int signo;
693 x = 2;
694 z++;
697 void test37h()
699 sigset_t set;
700 int r;
701 struct sigaction act;
702 pid_t pid;
704 subtest = 8;
705 clearsigstate();
706 x = 0;
707 z = 0;
709 switch (pid = fork()) {
710 case 0: /* child */
711 errct = 0;
712 sleep(1);
713 if (kill(getppid(), SIGHUP) == -1) e(1);
714 sleep(1);
715 if (kill(getppid(), SIGPIPE) == -1) e(2);
716 sleep(1);
717 if (kill(getppid(), SIGTERM) == -1) e(3);
718 exit(errct == 0 ? 0 : 1);
719 case -1: e(5); break;
720 default: /* parent */
721 if (sigemptyset(&act.sa_mask) == -1) e(6);
722 act.sa_flags = 0;
723 act.sa_handler = sighup8;
724 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7);
726 act.sa_handler = sigpip8;
727 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);
729 act.sa_handler = sigter8;
730 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);
732 if (sigemptyset(&set) == -1) e(10);
733 if (sigaddset(&set, SIGHUP) == -1) e(11);
734 if (sigaddset(&set, SIGPIPE) == -1) e(12);
735 r = sigsuspend(&set);
736 if (r != -1) e(13);
737 if (errno != EINTR) e(14);
738 if (z != 3) e(15);
739 if (x != 1) e(16);
740 wait_for(pid);
741 break;
745 /*--------------------------------------------------------------------------*/
747 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
748 * and SIGTERM, unblock these signals with sigprocmask, and verify
749 * that these signals are delivered.
752 void sighup9(signo)
753 int signo;
755 y++;
758 void sigter9(signo)
759 int signo;
761 z++;
764 void test37i()
766 sigset_t set;
767 struct sigaction act;
769 subtest = 9;
770 clearsigstate();
771 y = 0;
772 z = 0;
774 if (sigemptyset(&act.sa_mask) == -1) e(1);
775 act.sa_flags = 0;
777 act.sa_handler = sighup9;
778 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
780 act.sa_handler = sigter9;
781 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);
783 if (sigemptyset(&set) == -1) e(4);
784 if (sigaddset(&set, SIGTERM) == -1) e(5);
785 if (sigaddset(&set, SIGHUP) == -1) e(6);
786 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(7);
788 if (kill(getpid(), SIGHUP) == -1) e(8);
789 if (kill(getpid(), SIGTERM) == -1) e(9);
790 if (y != 0) e(10);
791 if (z != 0) e(11);
793 if (sigemptyset(&set) == -1) e(12);
794 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12);
795 if (y != 1) e(13);
796 if (z != 1) e(14);
799 /*---------------------------------------------------------------------------*/
801 /* Block SIGINT and then send this signal to ourself.
803 * Install signal handlers for SIGALRM and SIGINT.
805 * Set an alarm for 6 seconds, then sleep for 7.
807 * The SIGALRM should interrupt the sleep, but the SIGINT
808 * should remain pending.
811 void sighup10(signo)
812 int signo;
814 y++;
817 void sigalrm_handler10(signo)
818 int signo;
820 z++;
823 void test37j()
825 sigset_t set, set2;
826 struct sigaction act;
828 subtest = 10;
829 clearsigstate();
830 y = 0;
831 z = 0;
833 if (sigemptyset(&act.sa_mask) == -1) e(1);
834 act.sa_flags = 0;
836 act.sa_handler = sighup10;
837 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
839 act.sa_handler = sigalrm_handler10;
840 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);
842 if (sigemptyset(&set) == -1) e(4);
843 if (sigaddset(&set, SIGHUP) == -1) e(5);
844 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(6);
846 if (kill(getpid(), SIGHUP) == -1) e(7);
847 if (sigpending(&set) == -1) e(8);
848 if (sigemptyset(&set2) == -1) e(9);
849 if (sigaddset(&set2, SIGHUP) == -1) e(10);
850 if (set2 != set) e(11);
851 alarm(6);
852 sleep(7);
853 if (sigpending(&set) == -1) e(12);
854 if (set != set2) e(13);
855 if (y != 0) e(14);
856 if (z != 1) e(15);
859 /*--------------------------------------------------------------------------*/
861 void test37k()
863 subtest = 11;
865 void test37l()
867 subtest = 12;
870 /*---------------------------------------------------------------------------*/
872 /* Basic test for setjmp/longjmp. This includes testing that the
873 * signal mask is properly restored.
876 void test37m()
878 jmp_buf jb;
879 sigset_t ss;
881 subtest = 13;
882 clearsigstate();
884 ss = 0x32;
885 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1);
886 if (setjmp(jb)) {
887 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2);
888 if (ss != 0x32) e(388);
889 return;
891 ss = 0x3abc;
892 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4);
893 longjmp(jb, 1);
896 void longjerr()
898 e(5);
901 /*--------------------------------------------------------------------------*/
903 /* Test for setjmp/longjmp.
905 * Catch a signal. While in signal handler do setjmp/longjmp.
908 void catch14(signo, code, scp)
909 int signo;
910 int code;
911 struct sigcontext *scp;
913 jmp_buf jb;
915 if (setjmp(jb)) {
916 x++;
917 sigreturn(scp);
918 e(1);
920 y++;
921 longjmp(jb, 1);
922 e(2);
925 void test37n()
927 struct sigaction act;
928 typedef _PROTOTYPE( void (*sighandler_t), (int sig) );
930 subtest = 14;
931 clearsigstate();
932 x = 0;
933 y = 0;
935 act.sa_flags = 0;
936 act.sa_mask = 0;
937 act.sa_handler = (sighandler_t) catch14; /* fudge */
938 if (sigaction(SIGSEGV, &act, (struct sigaction *) NULL) == -1) e(3);
939 if (kill(getpid(), SIGSEGV) == -1) e(4);
941 if (x != 1) e(5);
942 if (y != 1) e(6);
945 /*---------------------------------------------------------------------------*/
947 /* Test for setjmp/longjmp.
949 * Catch a signal. Longjmp out of signal handler.
951 jmp_buf glo_jb;
953 void catch15(signo)
954 int signo;
956 z++;
957 longjmp(glo_jb, 7);
958 e(1);
962 void test37o()
964 struct sigaction act;
965 int k;
967 subtest = 15;
968 clearsigstate();
969 z = 0;
971 act.sa_flags = 0;
972 act.sa_mask = 0;
973 act.sa_handler = catch15;
974 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2);
976 if ((k = setjmp(glo_jb))) {
977 if (z != 1) e(399);
978 if (k != 7) e(4);
979 return;
981 if (kill(getpid(), SIGALRM) == -1) e(5);
984 void clearsigstate()
986 int i;
987 sigset_t sigset_var;
989 /* Clear the signal state. */
990 for (i = 1; i <= _NSIG; i++) signal(i, SIG_IGN);
991 for (i = 1; i <= _NSIG; i++) signal(i, SIG_DFL);
992 sigfillset(&sigset_var);
993 sigprocmask(SIG_UNBLOCK, &sigset_var, (sigset_t *)NULL);
996 void quit()
999 chdir("..");
1000 system("rm -rf DIR*");
1002 if (errct == 0) {
1003 printf("ok\n");
1004 exit(0);
1005 } else {
1006 printf("%d errors\n", errct);
1007 exit(4);
1011 void wait_for(pid)
1012 pid_t pid;
1014 /* Expect exactly one child, and that it exits with 0. */
1016 int r;
1017 int status;
1019 errno = 0;
1020 while (1) {
1021 errno = 0;
1022 r = wait(&status);
1023 if (r == pid) {
1024 errno = 0;
1025 if (status != 0) e(90);
1026 return;
1028 if (r < 0) {
1029 e(91);
1030 return;
1032 e(92);
1036 void e(n)
1037 int n;
1039 char msgbuf[80];
1041 sprintf(msgbuf, "Subtest %d, error %d errno=%d ", subtest, n, errno);
1042 perror(msgbuf);
1043 if (errct++ > MAX_ERROR) {
1044 fprintf(stderr, "Too many errors; test aborted\n");
1045 chdir("..");
1046 system("rm -rf DIR*");
1047 exit(1);