VM: memtype fix
[minix3.git] / test / test37.c
blob17635b785668dd066e4fea1f14caeca2d4f246c5
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 int max_error = 4;
16 #include "common.h"
20 int iteration, cumsig, 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 int main(int argc, char *argv []);
28 void test37a(void);
29 void func1(int sig);
30 void func2(int sig);
31 void test37b(void);
32 void catch1(int signo);
33 void catch2(int signo);
34 void test37c(void);
35 void catch3(int signo);
36 void test37d(void);
37 void catch4(int signo);
38 void test37e(void);
39 void catch5(int signo);
40 void test37f(void);
41 void sigint_handler(int signo);
42 void sigpipe_handler(int signo);
43 void test37g(void);
44 void sighup8(int signo);
45 void sigpip8(int signo);
46 void sigter8(int signo);
47 void test37h(void);
48 void sighup9(int signo);
49 void sigter9(int signo);
50 void test37i(void);
51 void sighup10(int signo);
52 void sigalrm_handler10(int signo);
53 void test37j(void);
54 void test37k(void);
55 void test37l(void);
56 void func_m1(void);
57 void func_m2(void);
58 void test37m(void);
59 void test37p(void);
60 void test37q(void);
61 void longjerr(void);
62 void catch14(int signo, int code, struct sigcontext * scp);
63 void test37n(void);
64 void catch15(int signo);
65 void test37o(void);
66 void clearsigstate(void);
67 void wait_for(int pid);
69 int main(argc, argv)
70 int argc;
71 char *argv[];
73 int i, m = 0377777;
75 sync();
77 start(37);
79 if (argc == 2) m = atoi(argv[1]);
81 for (i = 0; i < ITERATIONS; i++) {
82 iteration = i;
83 if (m & 0000001) test37a();
84 if (m & 0000002) test37b();
85 if (m & 0000004) test37c();
86 if (m & 0000010) test37d();
87 if (m & 0000020) test37e();
88 if (m & 0000040) test37f();
89 if (m & 0000100) test37g();
90 if (m & 0000200) test37h();
91 if (m & 0000400) test37i();
92 if (m & 0001000) test37j();
93 if (m & 0002000) test37k();
94 if (m & 0004000) test37l();
95 if (m & 0010000) test37m();
96 if (m & 0020000) test37n();
97 if (m & 0040000) test37o();
98 if (m & 0100000) test37p();
99 if (m & 0200000) test37q();
102 quit();
104 return(-1); /* Unreachable */
107 void test37a()
109 /* Test signal set management. */
111 sigset_t s;
113 subtest = 1;
114 clearsigstate();
116 /* Create an empty set and see if any bits are on. */
117 if (sigemptyset(&s) != 0) e(1);
118 if (sigismember(&s, SIGHUP) != 0) e(2);
119 if (sigismember(&s, SIGINT) != 0) e(3);
120 if (sigismember(&s, SIGQUIT) != 0) e(4);
121 if (sigismember(&s, SIGILL) != 0) e(5);
122 if (sigismember(&s, SIGTRAP) != 0) e(6);
123 if (sigismember(&s, SIGABRT) != 0) e(7);
124 if (sigismember(&s, SIGIOT) != 0) e(8);
125 if (sigismember(&s, SIGFPE) != 0) e(10);
126 if (sigismember(&s, SIGKILL) != 0) e(11);
127 if (sigismember(&s, SIGUSR1) != 0) e(12);
128 if (sigismember(&s, SIGSEGV) != 0) e(13);
129 if (sigismember(&s, SIGUSR2) != 0) e(14);
130 if (sigismember(&s, SIGPIPE) != 0) e(15);
131 if (sigismember(&s, SIGALRM) != 0) e(16);
132 if (sigismember(&s, SIGTERM) != 0) e(17);
134 /* Create a full set and see if any bits are off. */
135 if (sigfillset(&s) != 0) e(19);
136 if (sigemptyset(&s) != 0) e(20);
137 if (sigfillset(&s) != 0) e(21);
138 if (sigismember(&s, SIGHUP) != 1) e(22);
139 if (sigismember(&s, SIGINT) != 1) e(23);
140 if (sigismember(&s, SIGQUIT) != 1) e(24);
141 if (sigismember(&s, SIGILL) != 1) e(25);
142 if (sigismember(&s, SIGTRAP) != 1) e(26);
143 if (sigismember(&s, SIGABRT) != 1) e(27);
144 if (sigismember(&s, SIGIOT) != 1) e(28);
145 if (sigismember(&s, SIGFPE) != 1) e(30);
146 if (sigismember(&s, SIGKILL) != 1) e(31);
147 if (sigismember(&s, SIGUSR1) != 1) e(32);
148 if (sigismember(&s, SIGSEGV) != 1) e(33);
149 if (sigismember(&s, SIGUSR2) != 1) e(34);
150 if (sigismember(&s, SIGPIPE) != 1) e(35);
151 if (sigismember(&s, SIGALRM) != 1) e(36);
152 if (sigismember(&s, SIGTERM) != 1) e(37);
154 /* Create an empty set, then turn on bits individually. */
155 if (sigemptyset(&s) != 0) e(39);
156 if (sigaddset(&s, SIGHUP) != 0) e(40);
157 if (sigaddset(&s, SIGINT) != 0) e(41);
158 if (sigaddset(&s, SIGQUIT) != 0) e(42);
159 if (sigaddset(&s, SIGILL) != 0) e(43);
160 if (sigaddset(&s, SIGTRAP) != 0) e(44);
162 /* See if the bits just turned on are indeed on. */
163 if (sigismember(&s, SIGHUP) != 1) e(45);
164 if (sigismember(&s, SIGINT) != 1) e(46);
165 if (sigismember(&s, SIGQUIT) != 1) e(47);
166 if (sigismember(&s, SIGILL) != 1) e(48);
167 if (sigismember(&s, SIGTRAP) != 1) e(49);
169 /* The others should be turned off. */
170 if (sigismember(&s, SIGABRT) != 0) e(50);
171 if (sigismember(&s, SIGIOT) != 0) e(51);
172 if (sigismember(&s, SIGFPE) != 0) e(53);
173 if (sigismember(&s, SIGKILL) != 0) e(54);
174 if (sigismember(&s, SIGUSR1) != 0) e(55);
175 if (sigismember(&s, SIGSEGV) != 0) e(56);
176 if (sigismember(&s, SIGUSR2) != 0) e(57);
177 if (sigismember(&s, SIGPIPE) != 0) e(58);
178 if (sigismember(&s, SIGALRM) != 0) e(59);
179 if (sigismember(&s, SIGTERM) != 0) e(60);
181 /* Now turn them off and see if all are off. */
182 if (sigdelset(&s, SIGHUP) != 0) e(62);
183 if (sigdelset(&s, SIGINT) != 0) e(63);
184 if (sigdelset(&s, SIGQUIT) != 0) e(64);
185 if (sigdelset(&s, SIGILL) != 0) e(65);
186 if (sigdelset(&s, SIGTRAP) != 0) e(66);
188 if (sigismember(&s, SIGHUP) != 0) e(67);
189 if (sigismember(&s, SIGINT) != 0) e(68);
190 if (sigismember(&s, SIGQUIT) != 0) e(69);
191 if (sigismember(&s, SIGILL) != 0) e(70);
192 if (sigismember(&s, SIGTRAP) != 0) e(71);
193 if (sigismember(&s, SIGABRT) != 0) e(72);
194 if (sigismember(&s, SIGIOT) != 0) e(73);
195 if (sigismember(&s, SIGFPE) != 0) e(75);
196 if (sigismember(&s, SIGKILL) != 0) e(76);
197 if (sigismember(&s, SIGUSR1) != 0) e(77);
198 if (sigismember(&s, SIGSEGV) != 0) e(78);
199 if (sigismember(&s, SIGUSR2) != 0) e(79);
200 if (sigismember(&s, SIGPIPE) != 0) e(80);
201 if (sigismember(&s, SIGALRM) != 0) e(81);
202 if (sigismember(&s, SIGTERM) != 0) e(82);
205 void func1(sig)
206 int sig;
208 sig1++;
211 void func2(sig)
212 int sig;
214 sig2++;
217 void test37b()
219 /* Test sigprocmask and sigpending. */
220 int i;
221 pid_t p;
222 sigset_t s, s1, s_empty, s_full, s_ill, s_ill_pip, s_nokill, s_nokill_stop;
223 struct sigaction sa, osa;
225 subtest = 2;
226 clearsigstate();
228 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
229 if (sigemptyset(&s_empty) != 0) e(1);
230 if (sigemptyset(&s_ill) != 0) e(2);
231 if (sigemptyset(&s_ill_pip) != 0) e(3);
232 if (sigaddset(&s_ill, SIGILL) != 0) e(4);
233 if (sigaddset(&s_ill_pip, SIGILL) != 0) e(5);
234 if (sigaddset(&s_ill_pip, SIGPIPE) != 0) e(6);
235 if (sigfillset(&s_full) != 0) e(7);
236 s_nokill = s_full;
237 if (sigdelset(&s_nokill, SIGKILL) != 0) e(8);
238 s_nokill_stop = s_nokill;
239 if (sigdelset(&s_nokill_stop, SIGSTOP) != 0) e(8);
240 if (SIGSTOP >= _NSIG) e(666);
241 if (SIGSTOP < _NSIG && sigdelset(&s_nokill, SIGSTOP) != 0) e(888);
243 /* Now get most of the signals into default state. Don't change SIGINT
244 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
246 sa.sa_handler = SIG_DFL;
247 sa.sa_mask = s_empty;
248 sa.sa_flags = 0;
249 for (i = 0; i < SIGS; i++) sigaction(i, &sa, &osa);
251 /* The second argument may be zero. See if it wipes out the system. */
252 for (i = 0; i < SIGS; i++) sigaction(i, (struct sigaction *) NULL, &osa);
254 /* Install a signal handler. */
255 sa.sa_handler = func1;
256 sa.sa_mask = s_ill;
257 sa.sa_flags = SA_NODEFER | SA_NOCLDSTOP;
258 osa.sa_handler = SIG_IGN;
259 osa.sa_mask = s_empty;
260 osa.sa_flags = 0;
261 if (sigaction(SIGHUP, &sa, &osa) != 0) e(9);
262 if (osa.sa_handler != SIG_DFL) e(10);
263 if (osa.sa_mask != 0) e(11);
264 if (osa.sa_flags != s_empty) e(12);
266 /* Replace action and see if old value is read back correctly. */
267 sa.sa_handler = func2;
268 sa.sa_mask = s_ill_pip;
269 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
270 osa.sa_handler = SIG_IGN;
271 osa.sa_mask = s_empty;
272 osa.sa_flags = 0;
273 if (sigaction(SIGHUP, &sa, &osa) != 0) e(13);
274 if (osa.sa_handler != func1) e(14);
275 if (osa.sa_mask != s_ill) e(15);
276 if (osa.sa_flags != SA_NODEFER
277 && osa.sa_flags != (SA_NODEFER | SA_NOCLDSTOP)) e(16);
279 /* Replace action once more and check what is read back. */
280 sa.sa_handler = SIG_DFL;
281 sa.sa_mask = s_empty;
282 osa.sa_handler = SIG_IGN;
283 osa.sa_mask = s_empty;
284 osa.sa_flags = 0;
285 if (sigaction(SIGHUP, &sa, &osa) != 0) e(17);
286 if (osa.sa_handler != func2) e(18);
287 if (osa.sa_mask != s_ill_pip) e(19);
288 if (osa.sa_flags != (SA_RESETHAND | SA_NODEFER)) e(20);
290 /* Test sigprocmask(SIG_SETMASK, ...). */
291 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(18); /* block all */
292 if (sigemptyset(&s1) != 0) e(19);
293 errno = 0;
294 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(20); /* block none */
295 if (s1 != s_nokill_stop) e(21);
296 if (sigprocmask(SIG_SETMASK, &s_ill, &s1) != 0) e(22); /* block SIGILL */
297 errno = 0;
298 if (s1 != s_empty) e(23);
299 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(24); /* SIGILL+PIP */
300 if (s1 != s_ill) e(25);
301 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(26); /* block all */
302 if (s1 != s_ill_pip) e(27);
304 /* Test sigprocmask(SIG_UNBLOCK, ...) */
305 if (sigprocmask(SIG_UNBLOCK, &s_ill, &s1) != 0) e(28);
306 if (s1 != s_nokill_stop) e(29);
307 if (sigprocmask(SIG_UNBLOCK, &s_ill_pip, &s1) != 0) e(30);
308 s = s_nokill_stop;
309 if (sigdelset(&s, SIGILL) != 0) e(31);
310 if (s != s1) e(32);
311 if (sigprocmask(SIG_UNBLOCK, &s_empty, &s1) != 0) e(33);
312 s = s_nokill_stop;
313 if (sigdelset(&s, SIGILL) != 0) e(34);
314 if (sigdelset(&s, SIGPIPE) != 0) e(35);
315 if (s != s1) e(36);
316 s1 = s_nokill_stop;
317 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(37);
318 if (s != s1) e(38);
320 /* Test sigprocmask(SIG_BLOCK, ...) */
321 if (sigprocmask(SIG_BLOCK, &s_ill, &s1) != 0) e(39);
322 if (s1 != s_empty) e(40);
323 if (sigprocmask(SIG_BLOCK, &s_ill_pip, &s1) != 0) e(41);
324 if (s1 != s_ill) e(42);
325 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(43);
326 if (s1 != s_ill_pip) e(44);
328 /* Check error condition. */
329 errno = 0;
330 if (sigprocmask(20000, &s_full, &s1) != -1) e(45);
331 if (errno != EINVAL) e(46);
332 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(47);
333 if (s1 != s_nokill_stop) e(48);
335 /* If second arg is 0, nothing is set. */
336 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(49);
337 if (s1 != s_nokill_stop) e(50);
338 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(51);
339 if (s1 != s_nokill_stop) e(52);
340 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(53);
341 if (s1 != s_ill_pip) e(54);
342 if (sigprocmask(SIG_BLOCK, (sigset_t *) NULL, &s1) != 0) e(55);
343 if (s1 != s_ill_pip) e(56);
344 if (sigprocmask(SIG_UNBLOCK, (sigset_t *) NULL, &s1) != 0) e(57);
345 if (s1 != s_ill_pip) e(58);
347 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
348 s = s_empty;
349 if (sigaddset(&s, SIGKILL) != 0) e(59);
350 if (sigprocmask(SIG_BLOCK, &s, &s1) != 0) e(60);
351 if (s1 != s_ill_pip) e(61);
352 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(62);
353 if (s1 != s_ill_pip) e(63);
355 /* Test sigpending. At this moment, all signals are blocked. */
356 sa.sa_handler = func2;
357 sa.sa_mask = s_empty;
358 if (sigaction(SIGHUP, &sa, &osa) != 0) e(64);
359 p = getpid();
360 kill(p, SIGHUP); /* send SIGHUP to self */
361 if (sigpending(&s) != 0) e(65);
362 if (sigemptyset(&s1) != 0) e(66);
363 if (sigaddset(&s1, SIGHUP) != 0) e(67);
364 if (s != s1) e(68);
365 sa.sa_handler = SIG_IGN;
366 if (sigaction(SIGHUP, &sa, &osa) != 0) e(69);
367 if (sigpending(&s) != 0) e(70);
368 if (s != s_empty) e(71);
371 /*---------------------------------------------------------------------------*/
372 int x;
373 sigset_t glo_vol_set;
375 void catch1(signo)
376 int signo;
378 x = 42;
381 void catch2(signo)
382 int signo;
384 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0)
385 e(1);
388 /* Verify that signal(2), which is now built on top of sigaction(2), still
389 * works.
391 void test37c()
393 pid_t pid;
394 sigset_t sigset_var;
396 subtest = 3;
397 clearsigstate();
398 x = 0;
400 /* Verify an installed signal handler persists across a fork(2). */
401 if (signal(SIGTERM, catch1) == SIG_ERR) e(1);
402 switch (pid = fork()) {
403 case 0: /* child */
404 errct = 0;
405 while (x == 0);
406 if (x != 42) e(2);
407 exit(errct == 0 ? 0 : 1);
408 case -1: e(3); break;
409 default: /* parent */
410 sleep(1);
411 if (kill(pid, SIGTERM) != 0) e(4);
412 wait_for(pid);
413 break;
416 /* Verify that the return value is the previous handler. */
417 signal(SIGINT, SIG_IGN);
418 if (signal(SIGINT, catch2) != SIG_IGN) e(5);
419 if (signal(SIGINT, catch1) != catch2) e(6);
420 if (signal(SIGINT, SIG_DFL) != catch1) e(7);
421 if (signal(SIGINT, catch1) != SIG_DFL) e(8);
422 if (signal(SIGINT, SIG_DFL) != catch1) e(9);
423 if (signal(SIGINT, SIG_DFL) != SIG_DFL) e(10);
424 if (signal(SIGINT, catch1) != SIG_DFL) e(11);
426 /* Verify that SIG_ERR is correctly generated. */
427 if (signal(_NSIG, catch1) != SIG_ERR) e(12);
428 if (signal(0, catch1) != SIG_ERR) e(13);
429 if (signal(-1, SIG_DFL) != SIG_ERR) e(14);
431 /* Verify that caught signals are automatically reset to the default,
432 * and that further instances of the same signal are not blocked here
433 * or in the signal handler.
435 if (signal(SIGTERM, catch1) == SIG_ERR) e(15);
436 switch ((pid = fork())) {
437 case 0: /* child */
438 errct = 0;
439 while (x == 0);
440 if (x != 42) e(16);
441 if (sigismember((sigset_t *) &glo_vol_set, SIGTERM)) e(17);
442 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &sigset_var) != 0) e(18);
443 if (sigismember(&sigset_var, SIGTERM)) e(19);
445 #if 0
446 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
447 if (signal(SIGTERM, catch1) != SIG_DFL) e(20);
448 #else
449 if (signal(SIGTERM, catch1) != catch1) e(20);
450 #endif
451 exit(errct == 0 ? 0 : 1);
452 default: /* parent */
453 sleep(1);
454 if (kill(pid, SIGTERM) != 0) e(21);
455 wait_for(pid);
456 break;
457 case -1: e(22); break;
461 /*---------------------------------------------------------------------------*/
462 /* Test that the signal handler can be invoked recursively with the
463 * state being properly saved and restored.
466 static int y;
467 static int z;
469 void catch3(signo)
470 int signo;
472 if (z == 1) { /* catching a nested signal */
473 y = 2;
474 return;
476 z = 1;
477 if (kill(getpid(), SIGHUP) != 0) e(1);
478 while (y != 2);
479 y = 1;
482 void test37d()
484 struct sigaction act;
486 subtest = 4;
487 clearsigstate();
488 y = 0;
489 z = 0;
491 act.sa_handler = catch3;
492 act.sa_mask = 0;
493 act.sa_flags = SA_NODEFER; /* Otherwise, nested occurence of
494 * SIGINT is blocked. */
495 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) != 0) e(2);
496 if (kill(getpid(), SIGHUP) != 0) e(3);
497 if (y != 1) e(4);
500 /*---------------------------------------------------------------------------*/
502 /* Test that the signal mask in effect for the duration of a signal handler
503 * is as specified in POSIX Section 3, lines 718 -724. Test that the
504 * previous signal mask is restored when the signal handler returns.
507 void catch4(signo)
508 int signo;
510 sigset_t oset;
511 sigset_t set;
513 if (sigemptyset(&set) == -1) e(5001);
514 if (sigaddset(&set, SIGTERM) == -1) e(5002);
515 if (sigaddset(&set, SIGHUP) == -1) e(5003);
516 if (sigaddset(&set, SIGINT) == -1) e(5004);
517 if (sigaddset(&set, SIGPIPE) == -1) e(5005);
518 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) != 0) e(5006);
519 if (oset != set) e(5007);
522 void test37e()
524 struct sigaction act, oact;
525 sigset_t set, oset;
527 subtest = 5;
528 clearsigstate();
530 act.sa_handler = catch4;
531 sigemptyset(&act.sa_mask);
532 sigaddset(&act.sa_mask, SIGTERM);
533 sigaddset(&act.sa_mask, SIGHUP);
534 act.sa_flags = 0;
535 if (sigaction(SIGINT, &act, &oact) == -1) e(2);
537 if (sigemptyset(&set) == -1) e(3);
538 if (sigaddset(&set, SIGPIPE) == -1) e(4);
539 if (sigprocmask(SIG_SETMASK, &set, &oset) == -1) e(5);
540 if (kill(getpid(), SIGINT) == -1) e(6);
541 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) == -1) e(7);
542 if (sigemptyset(&set) == -1) e(8);
543 if (sigaddset(&set, SIGPIPE) == -1) e(9);
544 if (set != oset) e(10);
547 /*---------------------------------------------------------------------------*/
549 /* Test the basic functionality of sigsuspend(2). */
551 void catch5(signo)
552 int signo;
554 x = 1;
557 void test37f()
559 sigset_t set;
560 int r;
561 struct sigaction act;
562 pid_t pid;
564 subtest = 6;
565 clearsigstate();
567 switch (pid = fork()) {
568 case 0: /* child */
569 errct = 0;
570 sleep(1);
571 if (kill(getppid(), SIGINT) == -1) e(1);
572 exit(errct == 0 ? 0 : 1);
573 case -1: e(2); break;
574 default: /* parent */
575 if (sigemptyset(&act.sa_mask) == -1) e(3);
576 act.sa_flags = 0;
577 act.sa_handler = catch5;
578 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
580 if (sigemptyset(&set) == -1) e(5);
581 r = sigsuspend(&set);
583 if (r != -1 || errno != EINTR || x != 1) e(6);
584 wait_for(pid);
585 break;
589 /*----------------------------------------------------------------------*/
591 /* Test that sigsuspend() does block the signals specified in its
592 * argument, and after sigsuspend returns, the previous signal
593 * mask is restored.
595 * The child sends two signals to the parent SIGINT and then SIGPIPE,
596 * separated by a long delay. The parent executes sigsuspend() with
597 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
598 * will be invoked, then sigsuspend will return restoring the
599 * original signal mask, and then the SIGPIPE handler will be
600 * invoked.
603 void sigint_handler(signo)
604 int signo;
606 x = 1;
607 z++;
610 void sigpipe_handler(signo)
611 int signo;
613 x = 2;
614 z++;
617 void test37g()
619 sigset_t set;
620 int r;
621 struct sigaction act;
622 pid_t pid;
624 subtest = 7;
625 clearsigstate();
626 x = 0;
627 z = 0;
629 switch (pid = fork()) {
630 case 0: /* child */
631 errct = 0;
632 sleep(1);
633 if (kill(getppid(), SIGINT) == -1) e(1);
634 sleep(1);
635 if (kill(getppid(), SIGPIPE) == -1) e(2);
636 exit(errct == 0 ? 0 : 1);
637 case -1: e(3); break;
638 default: /* parent */
639 if (sigemptyset(&act.sa_mask) == -1) e(3);
640 act.sa_flags = 0;
641 act.sa_handler = sigint_handler;
642 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
644 act.sa_handler = sigpipe_handler;
645 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);
647 if (sigemptyset(&set) == -1) e(6);
648 if (sigaddset(&set, SIGINT) == -1) e(7);
649 r = sigsuspend(&set);
650 if (r != -1) e(8);
651 if (errno != EINTR) e(9);
652 if (z != 2) e(10);
653 if (x != 1) e(11);
654 wait_for(pid);
655 break;
659 /*--------------------------------------------------------------------------*/
661 /* Test that sigsuspend() does block the signals specified in its
662 * argument, and after sigsuspend returns, the previous signal
663 * mask is restored.
665 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
666 * and then SIGTERM, separated by a long delay. The parent executes
667 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
668 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
669 * will return restoring the original signal mask, and then the other
670 * two handlers will be invoked.
673 void sighup8(signo)
674 int signo;
676 x = 1;
677 z++;
680 void sigpip8(signo)
681 int signo;
683 x = 1;
684 z++;
687 void sigter8(signo)
688 int signo;
690 x = 2;
691 z++;
694 void test37h()
696 sigset_t set;
697 int r;
698 struct sigaction act;
699 pid_t pid;
701 subtest = 8;
702 clearsigstate();
703 x = 0;
704 z = 0;
706 switch (pid = fork()) {
707 case 0: /* child */
708 errct = 0;
709 sleep(1);
710 if (kill(getppid(), SIGHUP) == -1) e(1);
711 sleep(1);
712 if (kill(getppid(), SIGPIPE) == -1) e(2);
713 sleep(1);
714 if (kill(getppid(), SIGTERM) == -1) e(3);
715 exit(errct == 0 ? 0 : 1);
716 case -1: e(5); break;
717 default: /* parent */
718 if (sigemptyset(&act.sa_mask) == -1) e(6);
719 act.sa_flags = 0;
720 act.sa_handler = sighup8;
721 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7);
723 act.sa_handler = sigpip8;
724 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);
726 act.sa_handler = sigter8;
727 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);
729 if (sigemptyset(&set) == -1) e(10);
730 if (sigaddset(&set, SIGHUP) == -1) e(11);
731 if (sigaddset(&set, SIGPIPE) == -1) e(12);
732 r = sigsuspend(&set);
733 if (r != -1) e(13);
734 if (errno != EINTR) e(14);
735 if (z != 3) e(15);
736 if (x != 1) e(16);
737 wait_for(pid);
738 break;
742 /*--------------------------------------------------------------------------*/
744 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
745 * and SIGTERM, unblock these signals with sigprocmask, and verify
746 * that these signals are delivered.
749 void sighup9(signo)
750 int signo;
752 y++;
755 void sigter9(signo)
756 int signo;
758 z++;
761 void test37i()
763 sigset_t set;
764 struct sigaction act;
766 subtest = 9;
767 clearsigstate();
768 y = 0;
769 z = 0;
771 if (sigemptyset(&act.sa_mask) == -1) e(1);
772 act.sa_flags = 0;
774 act.sa_handler = sighup9;
775 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
777 act.sa_handler = sigter9;
778 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);
780 if (sigemptyset(&set) == -1) e(4);
781 if (sigaddset(&set, SIGTERM) == -1) e(5);
782 if (sigaddset(&set, SIGHUP) == -1) e(6);
783 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(7);
785 if (kill(getpid(), SIGHUP) == -1) e(8);
786 if (kill(getpid(), SIGTERM) == -1) e(9);
787 if (y != 0) e(10);
788 if (z != 0) e(11);
790 if (sigemptyset(&set) == -1) e(12);
791 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12);
792 if (y != 1) e(13);
793 if (z != 1) e(14);
796 /*---------------------------------------------------------------------------*/
798 /* Block SIGINT and then send this signal to ourself.
800 * Install signal handlers for SIGALRM and SIGINT.
802 * Set an alarm for 6 seconds, then sleep for 7.
804 * The SIGALRM should interrupt the sleep, but the SIGINT
805 * should remain pending.
808 void sighup10(signo)
809 int signo;
811 y++;
814 void sigalrm_handler10(signo)
815 int signo;
817 z++;
820 void test37j()
822 sigset_t set, set2;
823 struct sigaction act;
825 subtest = 10;
826 clearsigstate();
827 y = 0;
828 z = 0;
830 if (sigemptyset(&act.sa_mask) == -1) e(1);
831 act.sa_flags = 0;
833 act.sa_handler = sighup10;
834 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
836 act.sa_handler = sigalrm_handler10;
837 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);
839 if (sigemptyset(&set) == -1) e(4);
840 if (sigaddset(&set, SIGHUP) == -1) e(5);
841 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(6);
843 if (kill(getpid(), SIGHUP) == -1) e(7);
844 if (sigpending(&set) == -1) e(8);
845 if (sigemptyset(&set2) == -1) e(9);
846 if (sigaddset(&set2, SIGHUP) == -1) e(10);
847 if (set2 != set) e(11);
848 alarm(6);
849 sleep(7);
850 if (sigpending(&set) == -1) e(12);
851 if (set != set2) e(13);
852 if (y != 0) e(14);
853 if (z != 1) e(15);
856 /*--------------------------------------------------------------------------*/
858 void test37k()
860 subtest = 11;
862 void test37l()
864 subtest = 12;
867 /*---------------------------------------------------------------------------*/
869 /* Basic test for setjmp/longjmp. This includes testing that the
870 * signal mask is properly restored.
873 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
874 void _name(void) \
876 _type jb; \
877 sigset_t ss, ssexp; \
879 subtest = _subtest; \
880 clearsigstate(); \
882 ss = 0x32; \
883 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
884 if (_setjmp) { \
885 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
886 ssexp = _save ? 0x32 : 0x3abc; \
887 if ((ss ^ ssexp) & ~(1 << SIGKILL)) e(388); \
888 return; \
890 ss = 0x3abc; \
891 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
892 _longjmp; \
895 TEST_SETJMP(test37m, 13, jmp_buf, setjmp(jb), longjmp(jb, 1), 1)
896 TEST_SETJMP(test37p, 16, sigjmp_buf, sigsetjmp(jb, 0), siglongjmp(jb, 1), 0)
897 TEST_SETJMP(test37q, 17, sigjmp_buf, sigsetjmp(jb, 1), siglongjmp(jb, 1), 1)
899 void longjerr()
901 e(5);
904 /*--------------------------------------------------------------------------*/
906 /* Test for setjmp/longjmp.
908 * Catch a signal. While in signal handler do setjmp/longjmp.
911 void catch14(signo, code, scp)
912 int signo;
913 int code;
914 struct sigcontext *scp;
916 jmp_buf jb;
918 if (setjmp(jb)) {
919 x++;
920 sigreturn(scp);
921 e(1);
923 y++;
924 longjmp(jb, 1);
925 e(2);
928 void test37n()
930 struct sigaction act;
931 typedef void(*sighandler_t) (int sig);
933 subtest = 14;
934 clearsigstate();
935 x = 0;
936 y = 0;
938 act.sa_flags = 0;
939 act.sa_mask = 0;
940 act.sa_handler = (sighandler_t) catch14; /* fudge */
941 if (sigaction(SIGSEGV, &act, (struct sigaction *) NULL) == -1) e(3);
942 if (kill(getpid(), SIGSEGV) == -1) e(4);
944 if (x != 1) e(5);
945 if (y != 1) e(6);
948 /*---------------------------------------------------------------------------*/
950 /* Test for setjmp/longjmp.
952 * Catch a signal. Longjmp out of signal handler.
954 jmp_buf glo_jb;
956 void catch15(signo)
957 int signo;
959 z++;
960 longjmp(glo_jb, 7);
961 e(1);
965 void test37o()
967 struct sigaction act;
968 int k;
970 subtest = 15;
971 clearsigstate();
972 z = 0;
974 act.sa_flags = 0;
975 act.sa_mask = 0;
976 act.sa_handler = catch15;
977 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2);
979 if ((k = setjmp(glo_jb))) {
980 if (z != 1) e(399);
981 if (k != 7) e(4);
982 return;
984 if (kill(getpid(), SIGALRM) == -1) e(5);
987 void clearsigstate()
989 int i;
990 sigset_t sigset_var;
992 /* Clear the signal state. */
993 for (i = 1; i < _NSIG; i++) signal(i, SIG_IGN);
994 for (i = 1; i < _NSIG; i++) signal(i, SIG_DFL);
995 sigfillset(&sigset_var);
996 sigprocmask(SIG_UNBLOCK, &sigset_var, (sigset_t *)NULL);
999 void wait_for(pid)
1000 pid_t pid;
1002 /* Expect exactly one child, and that it exits with 0. */
1004 int r;
1005 int status;
1007 errno = 0;
1008 while (1) {
1009 errno = 0;
1010 r = wait(&status);
1011 if (r == pid) {
1012 errno = 0;
1013 if (status != 0) e(90);
1014 return;
1016 if (r < 0) {
1017 e(91);
1018 return;
1020 e(92);