mkfs: symlink support
[minix.git] / test / test37.c
blob751d67c91063f8e3cee9bf663e01f918cbddb584
1 /* test 37 - signals */
3 #include <sys/types.h>
4 #include <sys/times.h>
5 #include <sys/wait.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <setjmp.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
13 #define ITERATIONS 2
14 #define SIGS 14
15 #define MAX_ERROR 4
17 #include "common.c"
19 int iteration, cumsig, sig1, sig2;
21 int sigarray[SIGS] = {SIGHUP, SIGILL, SIGTRAP, SIGABRT, SIGIOT,
22 SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM,
23 SIGTERM};
25 /* Prototypes produced automatically by mkptypes. */
26 int main(int argc, char *argv []);
27 void test37a(void);
28 void func1(int sig);
29 void func2(int sig);
30 void test37b(void);
31 void catch1(int signo);
32 void catch2(int signo);
33 void test37c(void);
34 void catch3(int signo);
35 void test37d(void);
36 void catch4(int signo);
37 void test37e(void);
38 void catch5(int signo);
39 void test37f(void);
40 void sigint_handler(int signo);
41 void sigpipe_handler(int signo);
42 void test37g(void);
43 void sighup8(int signo);
44 void sigpip8(int signo);
45 void sigter8(int signo);
46 void test37h(void);
47 void sighup9(int signo);
48 void sigter9(int signo);
49 void test37i(void);
50 void sighup10(int signo);
51 void sigalrm_handler10(int signo);
52 void test37j(void);
53 void test37k(void);
54 void test37l(void);
55 void func_m1(void);
56 void func_m2(void);
57 void test37m(void);
58 void test37p(void);
59 void test37q(void);
60 void longjerr(void);
61 void catch14(int signo, int code, struct sigcontext * scp);
62 void test37n(void);
63 void catch15(int signo);
64 void test37o(void);
65 void clearsigstate(void);
66 void wait_for(int pid);
68 int main(argc, argv)
69 int argc;
70 char *argv[];
72 int i, m = 0377777;
74 sync();
76 start(37);
78 if (argc == 2) m = atoi(argv[1]);
80 for (i = 0; i < ITERATIONS; i++) {
81 iteration = i;
82 if (m & 0000001) test37a();
83 if (m & 0000002) test37b();
84 if (m & 0000004) test37c();
85 if (m & 0000010) test37d();
86 if (m & 0000020) test37e();
87 if (m & 0000040) test37f();
88 if (m & 0000100) test37g();
89 if (m & 0000200) test37h();
90 if (m & 0000400) test37i();
91 if (m & 0001000) test37j();
92 if (m & 0002000) test37k();
93 if (m & 0004000) test37l();
94 if (m & 0010000) test37m();
95 if (m & 0020000) test37n();
96 if (m & 0040000) test37o();
97 if (m & 0100000) test37p();
98 if (m & 0200000) test37q();
101 quit();
103 return(-1); /* Unreachable */
106 void test37a()
108 /* Test signal set management. */
110 sigset_t s;
112 subtest = 1;
113 clearsigstate();
115 /* Create an empty set and see if any bits are on. */
116 if (sigemptyset(&s) != 0) e(1);
117 if (sigismember(&s, SIGHUP) != 0) e(2);
118 if (sigismember(&s, SIGINT) != 0) e(3);
119 if (sigismember(&s, SIGQUIT) != 0) e(4);
120 if (sigismember(&s, SIGILL) != 0) e(5);
121 if (sigismember(&s, SIGTRAP) != 0) e(6);
122 if (sigismember(&s, SIGABRT) != 0) e(7);
123 if (sigismember(&s, SIGIOT) != 0) e(8);
124 if (sigismember(&s, SIGFPE) != 0) e(10);
125 if (sigismember(&s, SIGKILL) != 0) e(11);
126 if (sigismember(&s, SIGUSR1) != 0) e(12);
127 if (sigismember(&s, SIGSEGV) != 0) e(13);
128 if (sigismember(&s, SIGUSR2) != 0) e(14);
129 if (sigismember(&s, SIGPIPE) != 0) e(15);
130 if (sigismember(&s, SIGALRM) != 0) e(16);
131 if (sigismember(&s, SIGTERM) != 0) e(17);
133 /* Create a full set and see if any bits are off. */
134 if (sigfillset(&s) != 0) e(19);
135 if (sigemptyset(&s) != 0) e(20);
136 if (sigfillset(&s) != 0) e(21);
137 if (sigismember(&s, SIGHUP) != 1) e(22);
138 if (sigismember(&s, SIGINT) != 1) e(23);
139 if (sigismember(&s, SIGQUIT) != 1) e(24);
140 if (sigismember(&s, SIGILL) != 1) e(25);
141 if (sigismember(&s, SIGTRAP) != 1) e(26);
142 if (sigismember(&s, SIGABRT) != 1) e(27);
143 if (sigismember(&s, SIGIOT) != 1) e(28);
144 if (sigismember(&s, SIGFPE) != 1) e(30);
145 if (sigismember(&s, SIGKILL) != 1) e(31);
146 if (sigismember(&s, SIGUSR1) != 1) e(32);
147 if (sigismember(&s, SIGSEGV) != 1) e(33);
148 if (sigismember(&s, SIGUSR2) != 1) e(34);
149 if (sigismember(&s, SIGPIPE) != 1) e(35);
150 if (sigismember(&s, SIGALRM) != 1) e(36);
151 if (sigismember(&s, SIGTERM) != 1) e(37);
153 /* Create an empty set, then turn on bits individually. */
154 if (sigemptyset(&s) != 0) e(39);
155 if (sigaddset(&s, SIGHUP) != 0) e(40);
156 if (sigaddset(&s, SIGINT) != 0) e(41);
157 if (sigaddset(&s, SIGQUIT) != 0) e(42);
158 if (sigaddset(&s, SIGILL) != 0) e(43);
159 if (sigaddset(&s, SIGTRAP) != 0) e(44);
161 /* See if the bits just turned on are indeed on. */
162 if (sigismember(&s, SIGHUP) != 1) e(45);
163 if (sigismember(&s, SIGINT) != 1) e(46);
164 if (sigismember(&s, SIGQUIT) != 1) e(47);
165 if (sigismember(&s, SIGILL) != 1) e(48);
166 if (sigismember(&s, SIGTRAP) != 1) e(49);
168 /* The others should be turned off. */
169 if (sigismember(&s, SIGABRT) != 0) e(50);
170 if (sigismember(&s, SIGIOT) != 0) e(51);
171 if (sigismember(&s, SIGFPE) != 0) e(53);
172 if (sigismember(&s, SIGKILL) != 0) e(54);
173 if (sigismember(&s, SIGUSR1) != 0) e(55);
174 if (sigismember(&s, SIGSEGV) != 0) e(56);
175 if (sigismember(&s, SIGUSR2) != 0) e(57);
176 if (sigismember(&s, SIGPIPE) != 0) e(58);
177 if (sigismember(&s, SIGALRM) != 0) e(59);
178 if (sigismember(&s, SIGTERM) != 0) e(60);
180 /* Now turn them off and see if all are off. */
181 if (sigdelset(&s, SIGHUP) != 0) e(62);
182 if (sigdelset(&s, SIGINT) != 0) e(63);
183 if (sigdelset(&s, SIGQUIT) != 0) e(64);
184 if (sigdelset(&s, SIGILL) != 0) e(65);
185 if (sigdelset(&s, SIGTRAP) != 0) e(66);
187 if (sigismember(&s, SIGHUP) != 0) e(67);
188 if (sigismember(&s, SIGINT) != 0) e(68);
189 if (sigismember(&s, SIGQUIT) != 0) e(69);
190 if (sigismember(&s, SIGILL) != 0) e(70);
191 if (sigismember(&s, SIGTRAP) != 0) e(71);
192 if (sigismember(&s, SIGABRT) != 0) e(72);
193 if (sigismember(&s, SIGIOT) != 0) e(73);
194 if (sigismember(&s, SIGFPE) != 0) e(75);
195 if (sigismember(&s, SIGKILL) != 0) e(76);
196 if (sigismember(&s, SIGUSR1) != 0) e(77);
197 if (sigismember(&s, SIGSEGV) != 0) e(78);
198 if (sigismember(&s, SIGUSR2) != 0) e(79);
199 if (sigismember(&s, SIGPIPE) != 0) e(80);
200 if (sigismember(&s, SIGALRM) != 0) e(81);
201 if (sigismember(&s, SIGTERM) != 0) e(82);
204 void func1(sig)
205 int sig;
207 sig1++;
210 void func2(sig)
211 int sig;
213 sig2++;
216 void test37b()
218 /* Test sigprocmask and sigpending. */
219 int i;
220 pid_t p;
221 sigset_t s, s1, s_empty, s_full, s_ill, s_ill_pip, s_nokill, s_nokill_stop;
222 struct sigaction sa, osa;
224 subtest = 2;
225 clearsigstate();
227 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
228 if (sigemptyset(&s_empty) != 0) e(1);
229 if (sigemptyset(&s_ill) != 0) e(2);
230 if (sigemptyset(&s_ill_pip) != 0) e(3);
231 if (sigaddset(&s_ill, SIGILL) != 0) e(4);
232 if (sigaddset(&s_ill_pip, SIGILL) != 0) e(5);
233 if (sigaddset(&s_ill_pip, SIGPIPE) != 0) e(6);
234 if (sigfillset(&s_full) != 0) e(7);
235 s_nokill = s_full;
236 if (sigdelset(&s_nokill, SIGKILL) != 0) e(8);
237 s_nokill_stop = s_nokill;
238 if (sigdelset(&s_nokill_stop, SIGSTOP) != 0) e(8);
239 if (SIGSTOP >= _NSIG) e(666);
240 if (SIGSTOP < _NSIG && sigdelset(&s_nokill, SIGSTOP) != 0) e(888);
242 /* Now get most of the signals into default state. Don't change SIGINT
243 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
245 sa.sa_handler = SIG_DFL;
246 sa.sa_mask = s_empty;
247 sa.sa_flags = 0;
248 for (i = 0; i < SIGS; i++) sigaction(i, &sa, &osa);
250 /* The second argument may be zero. See if it wipes out the system. */
251 for (i = 0; i < SIGS; i++) sigaction(i, (struct sigaction *) NULL, &osa);
253 /* Install a signal handler. */
254 sa.sa_handler = func1;
255 sa.sa_mask = s_ill;
256 sa.sa_flags = SA_NODEFER | SA_NOCLDSTOP;
257 osa.sa_handler = SIG_IGN;
258 osa.sa_mask = s_empty;
259 osa.sa_flags = 0;
260 if (sigaction(SIGHUP, &sa, &osa) != 0) e(9);
261 if (osa.sa_handler != SIG_DFL) e(10);
262 if (osa.sa_mask != 0) e(11);
263 if (osa.sa_flags != s_empty) e(12);
265 /* Replace action and see if old value is read back correctly. */
266 sa.sa_handler = func2;
267 sa.sa_mask = s_ill_pip;
268 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
269 osa.sa_handler = SIG_IGN;
270 osa.sa_mask = s_empty;
271 osa.sa_flags = 0;
272 if (sigaction(SIGHUP, &sa, &osa) != 0) e(13);
273 if (osa.sa_handler != func1) e(14);
274 if (osa.sa_mask != s_ill) e(15);
275 if (osa.sa_flags != SA_NODEFER
276 && osa.sa_flags != (SA_NODEFER | SA_NOCLDSTOP)) e(16);
278 /* Replace action once more and check what is read back. */
279 sa.sa_handler = SIG_DFL;
280 sa.sa_mask = s_empty;
281 osa.sa_handler = SIG_IGN;
282 osa.sa_mask = s_empty;
283 osa.sa_flags = 0;
284 if (sigaction(SIGHUP, &sa, &osa) != 0) e(17);
285 if (osa.sa_handler != func2) e(18);
286 if (osa.sa_mask != s_ill_pip) e(19);
287 if (osa.sa_flags != (SA_RESETHAND | SA_NODEFER)) e(20);
289 /* Test sigprocmask(SIG_SETMASK, ...). */
290 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(18); /* block all */
291 if (sigemptyset(&s1) != 0) e(19);
292 errno = 0;
293 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(20); /* block none */
294 if (s1 != s_nokill_stop) e(21);
295 if (sigprocmask(SIG_SETMASK, &s_ill, &s1) != 0) e(22); /* block SIGILL */
296 errno = 0;
297 if (s1 != s_empty) e(23);
298 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(24); /* SIGILL+PIP */
299 if (s1 != s_ill) e(25);
300 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(26); /* block all */
301 if (s1 != s_ill_pip) e(27);
303 /* Test sigprocmask(SIG_UNBLOCK, ...) */
304 if (sigprocmask(SIG_UNBLOCK, &s_ill, &s1) != 0) e(28);
305 if (s1 != s_nokill_stop) e(29);
306 if (sigprocmask(SIG_UNBLOCK, &s_ill_pip, &s1) != 0) e(30);
307 s = s_nokill_stop;
308 if (sigdelset(&s, SIGILL) != 0) e(31);
309 if (s != s1) e(32);
310 if (sigprocmask(SIG_UNBLOCK, &s_empty, &s1) != 0) e(33);
311 s = s_nokill_stop;
312 if (sigdelset(&s, SIGILL) != 0) e(34);
313 if (sigdelset(&s, SIGPIPE) != 0) e(35);
314 if (s != s1) e(36);
315 s1 = s_nokill_stop;
316 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(37);
317 if (s != s1) e(38);
319 /* Test sigprocmask(SIG_BLOCK, ...) */
320 if (sigprocmask(SIG_BLOCK, &s_ill, &s1) != 0) e(39);
321 if (s1 != s_empty) e(40);
322 if (sigprocmask(SIG_BLOCK, &s_ill_pip, &s1) != 0) e(41);
323 if (s1 != s_ill) e(42);
324 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(43);
325 if (s1 != s_ill_pip) e(44);
327 /* Check error condition. */
328 errno = 0;
329 if (sigprocmask(20000, &s_full, &s1) != -1) e(45);
330 if (errno != EINVAL) e(46);
331 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(47);
332 if (s1 != s_nokill_stop) e(48);
334 /* If second arg is 0, nothing is set. */
335 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(49);
336 if (s1 != s_nokill_stop) e(50);
337 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(51);
338 if (s1 != s_nokill_stop) e(52);
339 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(53);
340 if (s1 != s_ill_pip) e(54);
341 if (sigprocmask(SIG_BLOCK, (sigset_t *) NULL, &s1) != 0) e(55);
342 if (s1 != s_ill_pip) e(56);
343 if (sigprocmask(SIG_UNBLOCK, (sigset_t *) NULL, &s1) != 0) e(57);
344 if (s1 != s_ill_pip) e(58);
346 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
347 s = s_empty;
348 if (sigaddset(&s, SIGKILL) != 0) e(59);
349 if (sigprocmask(SIG_BLOCK, &s, &s1) != 0) e(60);
350 if (s1 != s_ill_pip) e(61);
351 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(62);
352 if (s1 != s_ill_pip) e(63);
354 /* Test sigpending. At this moment, all signals are blocked. */
355 sa.sa_handler = func2;
356 sa.sa_mask = s_empty;
357 if (sigaction(SIGHUP, &sa, &osa) != 0) e(64);
358 p = getpid();
359 kill(p, SIGHUP); /* send SIGHUP to self */
360 if (sigpending(&s) != 0) e(65);
361 if (sigemptyset(&s1) != 0) e(66);
362 if (sigaddset(&s1, SIGHUP) != 0) e(67);
363 if (s != s1) e(68);
364 sa.sa_handler = SIG_IGN;
365 if (sigaction(SIGHUP, &sa, &osa) != 0) e(69);
366 if (sigpending(&s) != 0) e(70);
367 if (s != s_empty) e(71);
370 /*---------------------------------------------------------------------------*/
371 int x;
372 sigset_t glo_vol_set;
374 void catch1(signo)
375 int signo;
377 x = 42;
380 void catch2(signo)
381 int signo;
383 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0)
384 e(1);
387 /* Verify that signal(2), which is now built on top of sigaction(2), still
388 * works.
390 void test37c()
392 pid_t pid;
393 sigset_t sigset_var;
395 subtest = 3;
396 clearsigstate();
397 x = 0;
399 /* Verify an installed signal handler persists across a fork(2). */
400 if (signal(SIGTERM, catch1) == SIG_ERR) e(1);
401 switch (pid = fork()) {
402 case 0: /* child */
403 errct = 0;
404 while (x == 0);
405 if (x != 42) e(2);
406 exit(errct == 0 ? 0 : 1);
407 case -1: e(3); break;
408 default: /* parent */
409 sleep(1);
410 if (kill(pid, SIGTERM) != 0) e(4);
411 wait_for(pid);
412 break;
415 /* Verify that the return value is the previous handler. */
416 signal(SIGINT, SIG_IGN);
417 if (signal(SIGINT, catch2) != SIG_IGN) e(5);
418 if (signal(SIGINT, catch1) != catch2) e(6);
419 if (signal(SIGINT, SIG_DFL) != catch1) e(7);
420 if (signal(SIGINT, catch1) != SIG_DFL) e(8);
421 if (signal(SIGINT, SIG_DFL) != catch1) e(9);
422 if (signal(SIGINT, SIG_DFL) != SIG_DFL) e(10);
423 if (signal(SIGINT, catch1) != SIG_DFL) e(11);
425 /* Verify that SIG_ERR is correctly generated. */
426 if (signal(_NSIG, catch1) != SIG_ERR) e(12);
427 if (signal(0, catch1) != SIG_ERR) e(13);
428 if (signal(-1, SIG_DFL) != SIG_ERR) e(14);
430 /* Verify that caught signals are automatically reset to the default,
431 * and that further instances of the same signal are not blocked here
432 * or in the signal handler.
434 if (signal(SIGTERM, catch1) == SIG_ERR) e(15);
435 switch ((pid = fork())) {
436 case 0: /* child */
437 errct = 0;
438 while (x == 0);
439 if (x != 42) e(16);
440 if (sigismember((sigset_t *) &glo_vol_set, SIGTERM)) e(17);
441 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &sigset_var) != 0) e(18);
442 if (sigismember(&sigset_var, SIGTERM)) e(19);
444 #if 0
445 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
446 if (signal(SIGTERM, catch1) != SIG_DFL) e(20);
447 #else
448 if (signal(SIGTERM, catch1) != catch1) e(20);
449 #endif
450 exit(errct == 0 ? 0 : 1);
451 default: /* parent */
452 sleep(1);
453 if (kill(pid, SIGTERM) != 0) e(21);
454 wait_for(pid);
455 break;
456 case -1: e(22); break;
460 /*---------------------------------------------------------------------------*/
461 /* Test that the signal handler can be invoked recursively with the
462 * state being properly saved and restored.
465 static int y;
466 static int z;
468 void catch3(signo)
469 int signo;
471 if (z == 1) { /* catching a nested signal */
472 y = 2;
473 return;
475 z = 1;
476 if (kill(getpid(), SIGHUP) != 0) e(1);
477 while (y != 2);
478 y = 1;
481 void test37d()
483 struct sigaction act;
485 subtest = 4;
486 clearsigstate();
487 y = 0;
488 z = 0;
490 act.sa_handler = catch3;
491 act.sa_mask = 0;
492 act.sa_flags = SA_NODEFER; /* Otherwise, nested occurence of
493 * SIGINT is blocked. */
494 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) != 0) e(2);
495 if (kill(getpid(), SIGHUP) != 0) e(3);
496 if (y != 1) e(4);
499 /*---------------------------------------------------------------------------*/
501 /* Test that the signal mask in effect for the duration of a signal handler
502 * is as specified in POSIX Section 3, lines 718 -724. Test that the
503 * previous signal mask is restored when the signal handler returns.
506 void catch4(signo)
507 int signo;
509 sigset_t oset;
510 sigset_t set;
512 if (sigemptyset(&set) == -1) e(5001);
513 if (sigaddset(&set, SIGTERM) == -1) e(5002);
514 if (sigaddset(&set, SIGHUP) == -1) e(5003);
515 if (sigaddset(&set, SIGINT) == -1) e(5004);
516 if (sigaddset(&set, SIGPIPE) == -1) e(5005);
517 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) != 0) e(5006);
518 if (oset != set) e(5007);
521 void test37e()
523 struct sigaction act, oact;
524 sigset_t set, oset;
526 subtest = 5;
527 clearsigstate();
529 act.sa_handler = catch4;
530 sigemptyset(&act.sa_mask);
531 sigaddset(&act.sa_mask, SIGTERM);
532 sigaddset(&act.sa_mask, SIGHUP);
533 act.sa_flags = 0;
534 if (sigaction(SIGINT, &act, &oact) == -1) e(2);
536 if (sigemptyset(&set) == -1) e(3);
537 if (sigaddset(&set, SIGPIPE) == -1) e(4);
538 if (sigprocmask(SIG_SETMASK, &set, &oset) == -1) e(5);
539 if (kill(getpid(), SIGINT) == -1) e(6);
540 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) == -1) e(7);
541 if (sigemptyset(&set) == -1) e(8);
542 if (sigaddset(&set, SIGPIPE) == -1) e(9);
543 if (set != oset) e(10);
546 /*---------------------------------------------------------------------------*/
548 /* Test the basic functionality of sigsuspend(2). */
550 void catch5(signo)
551 int signo;
553 x = 1;
556 void test37f()
558 sigset_t set;
559 int r;
560 struct sigaction act;
561 pid_t pid;
563 subtest = 6;
564 clearsigstate();
566 switch (pid = fork()) {
567 case 0: /* child */
568 errct = 0;
569 sleep(1);
570 if (kill(getppid(), SIGINT) == -1) e(1);
571 exit(errct == 0 ? 0 : 1);
572 case -1: e(2); break;
573 default: /* parent */
574 if (sigemptyset(&act.sa_mask) == -1) e(3);
575 act.sa_flags = 0;
576 act.sa_handler = catch5;
577 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
579 if (sigemptyset(&set) == -1) e(5);
580 r = sigsuspend(&set);
582 if (r != -1 || errno != EINTR || x != 1) e(6);
583 wait_for(pid);
584 break;
588 /*----------------------------------------------------------------------*/
590 /* Test that sigsuspend() does block the signals specified in its
591 * argument, and after sigsuspend returns, the previous signal
592 * mask is restored.
594 * The child sends two signals to the parent SIGINT and then SIGPIPE,
595 * separated by a long delay. The parent executes sigsuspend() with
596 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
597 * will be invoked, then sigsuspend will return restoring the
598 * original signal mask, and then the SIGPIPE handler will be
599 * invoked.
602 void sigint_handler(signo)
603 int signo;
605 x = 1;
606 z++;
609 void sigpipe_handler(signo)
610 int signo;
612 x = 2;
613 z++;
616 void test37g()
618 sigset_t set;
619 int r;
620 struct sigaction act;
621 pid_t pid;
623 subtest = 7;
624 clearsigstate();
625 x = 0;
626 z = 0;
628 switch (pid = fork()) {
629 case 0: /* child */
630 errct = 0;
631 sleep(1);
632 if (kill(getppid(), SIGINT) == -1) e(1);
633 sleep(1);
634 if (kill(getppid(), SIGPIPE) == -1) e(2);
635 exit(errct == 0 ? 0 : 1);
636 case -1: e(3); break;
637 default: /* parent */
638 if (sigemptyset(&act.sa_mask) == -1) e(3);
639 act.sa_flags = 0;
640 act.sa_handler = sigint_handler;
641 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
643 act.sa_handler = sigpipe_handler;
644 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);
646 if (sigemptyset(&set) == -1) e(6);
647 if (sigaddset(&set, SIGINT) == -1) e(7);
648 r = sigsuspend(&set);
649 if (r != -1) e(8);
650 if (errno != EINTR) e(9);
651 if (z != 2) e(10);
652 if (x != 1) e(11);
653 wait_for(pid);
654 break;
658 /*--------------------------------------------------------------------------*/
660 /* Test that sigsuspend() does block the signals specified in its
661 * argument, and after sigsuspend returns, the previous signal
662 * mask is restored.
664 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
665 * and then SIGTERM, separated by a long delay. The parent executes
666 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
667 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
668 * will return restoring the original signal mask, and then the other
669 * two handlers will be invoked.
672 void sighup8(signo)
673 int signo;
675 x = 1;
676 z++;
679 void sigpip8(signo)
680 int signo;
682 x = 1;
683 z++;
686 void sigter8(signo)
687 int signo;
689 x = 2;
690 z++;
693 void test37h()
695 sigset_t set;
696 int r;
697 struct sigaction act;
698 pid_t pid;
700 subtest = 8;
701 clearsigstate();
702 x = 0;
703 z = 0;
705 switch (pid = fork()) {
706 case 0: /* child */
707 errct = 0;
708 sleep(1);
709 if (kill(getppid(), SIGHUP) == -1) e(1);
710 sleep(1);
711 if (kill(getppid(), SIGPIPE) == -1) e(2);
712 sleep(1);
713 if (kill(getppid(), SIGTERM) == -1) e(3);
714 exit(errct == 0 ? 0 : 1);
715 case -1: e(5); break;
716 default: /* parent */
717 if (sigemptyset(&act.sa_mask) == -1) e(6);
718 act.sa_flags = 0;
719 act.sa_handler = sighup8;
720 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7);
722 act.sa_handler = sigpip8;
723 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);
725 act.sa_handler = sigter8;
726 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);
728 if (sigemptyset(&set) == -1) e(10);
729 if (sigaddset(&set, SIGHUP) == -1) e(11);
730 if (sigaddset(&set, SIGPIPE) == -1) e(12);
731 r = sigsuspend(&set);
732 if (r != -1) e(13);
733 if (errno != EINTR) e(14);
734 if (z != 3) e(15);
735 if (x != 1) e(16);
736 wait_for(pid);
737 break;
741 /*--------------------------------------------------------------------------*/
743 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
744 * and SIGTERM, unblock these signals with sigprocmask, and verify
745 * that these signals are delivered.
748 void sighup9(signo)
749 int signo;
751 y++;
754 void sigter9(signo)
755 int signo;
757 z++;
760 void test37i()
762 sigset_t set;
763 struct sigaction act;
765 subtest = 9;
766 clearsigstate();
767 y = 0;
768 z = 0;
770 if (sigemptyset(&act.sa_mask) == -1) e(1);
771 act.sa_flags = 0;
773 act.sa_handler = sighup9;
774 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
776 act.sa_handler = sigter9;
777 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);
779 if (sigemptyset(&set) == -1) e(4);
780 if (sigaddset(&set, SIGTERM) == -1) e(5);
781 if (sigaddset(&set, SIGHUP) == -1) e(6);
782 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(7);
784 if (kill(getpid(), SIGHUP) == -1) e(8);
785 if (kill(getpid(), SIGTERM) == -1) e(9);
786 if (y != 0) e(10);
787 if (z != 0) e(11);
789 if (sigemptyset(&set) == -1) e(12);
790 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12);
791 if (y != 1) e(13);
792 if (z != 1) e(14);
795 /*---------------------------------------------------------------------------*/
797 /* Block SIGINT and then send this signal to ourself.
799 * Install signal handlers for SIGALRM and SIGINT.
801 * Set an alarm for 6 seconds, then sleep for 7.
803 * The SIGALRM should interrupt the sleep, but the SIGINT
804 * should remain pending.
807 void sighup10(signo)
808 int signo;
810 y++;
813 void sigalrm_handler10(signo)
814 int signo;
816 z++;
819 void test37j()
821 sigset_t set, set2;
822 struct sigaction act;
824 subtest = 10;
825 clearsigstate();
826 y = 0;
827 z = 0;
829 if (sigemptyset(&act.sa_mask) == -1) e(1);
830 act.sa_flags = 0;
832 act.sa_handler = sighup10;
833 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
835 act.sa_handler = sigalrm_handler10;
836 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);
838 if (sigemptyset(&set) == -1) e(4);
839 if (sigaddset(&set, SIGHUP) == -1) e(5);
840 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(6);
842 if (kill(getpid(), SIGHUP) == -1) e(7);
843 if (sigpending(&set) == -1) e(8);
844 if (sigemptyset(&set2) == -1) e(9);
845 if (sigaddset(&set2, SIGHUP) == -1) e(10);
846 if (set2 != set) e(11);
847 alarm(6);
848 sleep(7);
849 if (sigpending(&set) == -1) e(12);
850 if (set != set2) e(13);
851 if (y != 0) e(14);
852 if (z != 1) e(15);
855 /*--------------------------------------------------------------------------*/
857 void test37k()
859 subtest = 11;
861 void test37l()
863 subtest = 12;
866 /*---------------------------------------------------------------------------*/
868 /* Basic test for setjmp/longjmp. This includes testing that the
869 * signal mask is properly restored.
872 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
873 void _name(void) \
875 _type jb; \
876 sigset_t ss, ssexp; \
878 subtest = _subtest; \
879 clearsigstate(); \
881 ss = 0x32; \
882 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
883 if (_setjmp) { \
884 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
885 ssexp = _save ? 0x32 : 0x3abc; \
886 if ((ss ^ ssexp) & ~(1 << SIGKILL)) e(388); \
887 return; \
889 ss = 0x3abc; \
890 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
891 _longjmp; \
894 TEST_SETJMP(test37m, 13, jmp_buf, setjmp(jb), longjmp(jb, 1), 1)
895 TEST_SETJMP(test37p, 16, sigjmp_buf, sigsetjmp(jb, 0), siglongjmp(jb, 1), 0)
896 TEST_SETJMP(test37q, 17, sigjmp_buf, sigsetjmp(jb, 1), siglongjmp(jb, 1), 1)
898 void longjerr()
900 e(5);
903 /*--------------------------------------------------------------------------*/
905 /* Test for setjmp/longjmp.
907 * Catch a signal. While in signal handler do setjmp/longjmp.
910 void catch14(signo, code, scp)
911 int signo;
912 int code;
913 struct sigcontext *scp;
915 jmp_buf jb;
917 if (setjmp(jb)) {
918 x++;
919 sigreturn(scp);
920 e(1);
922 y++;
923 longjmp(jb, 1);
924 e(2);
927 void test37n()
929 struct sigaction act;
930 typedef void(*sighandler_t) (int sig);
932 subtest = 14;
933 clearsigstate();
934 x = 0;
935 y = 0;
937 act.sa_flags = 0;
938 act.sa_mask = 0;
939 act.sa_handler = (sighandler_t) catch14; /* fudge */
940 if (sigaction(SIGSEGV, &act, (struct sigaction *) NULL) == -1) e(3);
941 if (kill(getpid(), SIGSEGV) == -1) e(4);
943 if (x != 1) e(5);
944 if (y != 1) e(6);
947 /*---------------------------------------------------------------------------*/
949 /* Test for setjmp/longjmp.
951 * Catch a signal. Longjmp out of signal handler.
953 jmp_buf glo_jb;
955 void catch15(signo)
956 int signo;
958 z++;
959 longjmp(glo_jb, 7);
960 e(1);
964 void test37o()
966 struct sigaction act;
967 int k;
969 subtest = 15;
970 clearsigstate();
971 z = 0;
973 act.sa_flags = 0;
974 act.sa_mask = 0;
975 act.sa_handler = catch15;
976 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2);
978 if ((k = setjmp(glo_jb))) {
979 if (z != 1) e(399);
980 if (k != 7) e(4);
981 return;
983 if (kill(getpid(), SIGALRM) == -1) e(5);
986 void clearsigstate()
988 int i;
989 sigset_t sigset_var;
991 /* Clear the signal state. */
992 for (i = 1; i < _NSIG; i++) signal(i, SIG_IGN);
993 for (i = 1; i < _NSIG; i++) signal(i, SIG_DFL);
994 sigfillset(&sigset_var);
995 sigprocmask(SIG_UNBLOCK, &sigset_var, (sigset_t *)NULL);
998 void wait_for(pid)
999 pid_t pid;
1001 /* Expect exactly one child, and that it exits with 0. */
1003 int r;
1004 int status;
1006 errno = 0;
1007 while (1) {
1008 errno = 0;
1009 r = wait(&status);
1010 if (r == pid) {
1011 errno = 0;
1012 if (status != 0) e(90);
1013 return;
1015 if (r < 0) {
1016 e(91);
1017 return;
1019 e(92);