use oxpcie only if enabled to avoid baud bottleneck of uart.
[minix.git] / test / test37.c
blobcb21f1ec3f480686e1e53f6f3143b33e0290dc59
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 test37p, (void));
60 _PROTOTYPE(void test37q, (void));
61 _PROTOTYPE(void longjerr, (void));
62 _PROTOTYPE(void catch14, (int signo, int code, struct sigcontext * scp));
63 _PROTOTYPE(void test37n, (void));
64 _PROTOTYPE(void catch15, (int signo));
65 _PROTOTYPE(void test37o, (void));
66 _PROTOTYPE(void clearsigstate, (void));
67 _PROTOTYPE(void quit, (void));
68 _PROTOTYPE(void wait_for, (int pid));
69 _PROTOTYPE(void e, (int n));
71 int main(argc, argv)
72 int argc;
73 char *argv[];
75 int i, m = 0377777;
77 sync();
79 if (argc == 2) m = atoi(argv[1]);
81 printf("Test 37 ");
82 fflush(stdout); /* have to flush for child's benefit */
84 system("rm -rf DIR_37; mkdir DIR_37");
85 chdir("DIR_37");
87 for (i = 0; i < ITERATIONS; i++) {
88 iteration = i;
89 if (m & 0000001) test37a();
90 if (m & 0000002) test37b();
91 if (m & 0000004) test37c();
92 if (m & 0000010) test37d();
93 if (m & 0000020) test37e();
94 if (m & 0000040) test37f();
95 if (m & 0000100) test37g();
96 if (m & 0000200) test37h();
97 if (m & 0000400) test37i();
98 if (m & 0001000) test37j();
99 if (m & 0002000) test37k();
100 if (m & 0004000) test37l();
101 if (m & 0010000) test37m();
102 if (m & 0020000) test37n();
103 if (m & 0040000) test37o();
104 if (m & 0100000) test37p();
105 if (m & 0200000) test37q();
108 quit();
109 return(-1); /* impossible */
112 void test37a()
114 /* Test signal set management. */
116 sigset_t s;
118 subtest = 1;
119 clearsigstate();
121 /* Create an empty set and see if any bits are on. */
122 if (sigemptyset(&s) != 0) e(1);
123 if (sigismember(&s, SIGHUP) != 0) e(2);
124 if (sigismember(&s, SIGINT) != 0) e(3);
125 if (sigismember(&s, SIGQUIT) != 0) e(4);
126 if (sigismember(&s, SIGILL) != 0) e(5);
127 if (sigismember(&s, SIGTRAP) != 0) e(6);
128 if (sigismember(&s, SIGABRT) != 0) e(7);
129 if (sigismember(&s, SIGIOT) != 0) e(8);
130 if (sigismember(&s, SIGFPE) != 0) e(10);
131 if (sigismember(&s, SIGKILL) != 0) e(11);
132 if (sigismember(&s, SIGUSR1) != 0) e(12);
133 if (sigismember(&s, SIGSEGV) != 0) e(13);
134 if (sigismember(&s, SIGUSR2) != 0) e(14);
135 if (sigismember(&s, SIGPIPE) != 0) e(15);
136 if (sigismember(&s, SIGALRM) != 0) e(16);
137 if (sigismember(&s, SIGTERM) != 0) e(17);
139 /* Create a full set and see if any bits are off. */
140 if (sigfillset(&s) != 0) e(19);
141 if (sigemptyset(&s) != 0) e(20);
142 if (sigfillset(&s) != 0) e(21);
143 if (sigismember(&s, SIGHUP) != 1) e(22);
144 if (sigismember(&s, SIGINT) != 1) e(23);
145 if (sigismember(&s, SIGQUIT) != 1) e(24);
146 if (sigismember(&s, SIGILL) != 1) e(25);
147 if (sigismember(&s, SIGTRAP) != 1) e(26);
148 if (sigismember(&s, SIGABRT) != 1) e(27);
149 if (sigismember(&s, SIGIOT) != 1) e(28);
150 if (sigismember(&s, SIGFPE) != 1) e(30);
151 if (sigismember(&s, SIGKILL) != 1) e(31);
152 if (sigismember(&s, SIGUSR1) != 1) e(32);
153 if (sigismember(&s, SIGSEGV) != 1) e(33);
154 if (sigismember(&s, SIGUSR2) != 1) e(34);
155 if (sigismember(&s, SIGPIPE) != 1) e(35);
156 if (sigismember(&s, SIGALRM) != 1) e(36);
157 if (sigismember(&s, SIGTERM) != 1) e(37);
159 /* Create an empty set, then turn on bits individually. */
160 if (sigemptyset(&s) != 0) e(39);
161 if (sigaddset(&s, SIGHUP) != 0) e(40);
162 if (sigaddset(&s, SIGINT) != 0) e(41);
163 if (sigaddset(&s, SIGQUIT) != 0) e(42);
164 if (sigaddset(&s, SIGILL) != 0) e(43);
165 if (sigaddset(&s, SIGTRAP) != 0) e(44);
167 /* See if the bits just turned on are indeed on. */
168 if (sigismember(&s, SIGHUP) != 1) e(45);
169 if (sigismember(&s, SIGINT) != 1) e(46);
170 if (sigismember(&s, SIGQUIT) != 1) e(47);
171 if (sigismember(&s, SIGILL) != 1) e(48);
172 if (sigismember(&s, SIGTRAP) != 1) e(49);
174 /* The others should be turned off. */
175 if (sigismember(&s, SIGABRT) != 0) e(50);
176 if (sigismember(&s, SIGIOT) != 0) e(51);
177 if (sigismember(&s, SIGFPE) != 0) e(53);
178 if (sigismember(&s, SIGKILL) != 0) e(54);
179 if (sigismember(&s, SIGUSR1) != 0) e(55);
180 if (sigismember(&s, SIGSEGV) != 0) e(56);
181 if (sigismember(&s, SIGUSR2) != 0) e(57);
182 if (sigismember(&s, SIGPIPE) != 0) e(58);
183 if (sigismember(&s, SIGALRM) != 0) e(59);
184 if (sigismember(&s, SIGTERM) != 0) e(60);
186 /* Now turn them off and see if all are off. */
187 if (sigdelset(&s, SIGHUP) != 0) e(62);
188 if (sigdelset(&s, SIGINT) != 0) e(63);
189 if (sigdelset(&s, SIGQUIT) != 0) e(64);
190 if (sigdelset(&s, SIGILL) != 0) e(65);
191 if (sigdelset(&s, SIGTRAP) != 0) e(66);
193 if (sigismember(&s, SIGHUP) != 0) e(67);
194 if (sigismember(&s, SIGINT) != 0) e(68);
195 if (sigismember(&s, SIGQUIT) != 0) e(69);
196 if (sigismember(&s, SIGILL) != 0) e(70);
197 if (sigismember(&s, SIGTRAP) != 0) e(71);
198 if (sigismember(&s, SIGABRT) != 0) e(72);
199 if (sigismember(&s, SIGIOT) != 0) e(73);
200 if (sigismember(&s, SIGFPE) != 0) e(75);
201 if (sigismember(&s, SIGKILL) != 0) e(76);
202 if (sigismember(&s, SIGUSR1) != 0) e(77);
203 if (sigismember(&s, SIGSEGV) != 0) e(78);
204 if (sigismember(&s, SIGUSR2) != 0) e(79);
205 if (sigismember(&s, SIGPIPE) != 0) e(80);
206 if (sigismember(&s, SIGALRM) != 0) e(81);
207 if (sigismember(&s, SIGTERM) != 0) e(82);
210 void func1(sig)
211 int sig;
213 sig1++;
216 void func2(sig)
217 int sig;
219 sig2++;
222 void test37b()
224 /* Test sigprocmask and sigpending. */
225 int i;
226 pid_t p;
227 sigset_t s, s1, s_empty, s_full, s_ill, s_ill_pip, s_nokill, s_nokill_stop;
228 struct sigaction sa, osa;
230 subtest = 2;
231 clearsigstate();
233 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
234 if (sigemptyset(&s_empty) != 0) e(1);
235 if (sigemptyset(&s_ill) != 0) e(2);
236 if (sigemptyset(&s_ill_pip) != 0) e(3);
237 if (sigaddset(&s_ill, SIGILL) != 0) e(4);
238 if (sigaddset(&s_ill_pip, SIGILL) != 0) e(5);
239 if (sigaddset(&s_ill_pip, SIGPIPE) != 0) e(6);
240 if (sigfillset(&s_full) != 0) e(7);
241 s_nokill = s_full;
242 if (sigdelset(&s_nokill, SIGKILL) != 0) e(8);
243 s_nokill_stop = s_nokill;
244 if (sigdelset(&s_nokill_stop, SIGSTOP) != 0) e(8);
245 if (SIGSTOP >= _NSIG) e(666);
246 if (SIGSTOP < _NSIG && sigdelset(&s_nokill, SIGSTOP) != 0) e(888);
248 /* Now get most of the signals into default state. Don't change SIGINT
249 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
251 sa.sa_handler = SIG_DFL;
252 sa.sa_mask = s_empty;
253 sa.sa_flags = 0;
254 for (i = 0; i < SIGS; i++) sigaction(i, &sa, &osa);
256 /* The second argument may be zero. See if it wipes out the system. */
257 for (i = 0; i < SIGS; i++) sigaction(i, (struct sigaction *) NULL, &osa);
259 /* Install a signal handler. */
260 sa.sa_handler = func1;
261 sa.sa_mask = s_ill;
262 sa.sa_flags = SA_NODEFER | SA_NOCLDSTOP;
263 osa.sa_handler = SIG_IGN;
264 osa.sa_mask = s_empty;
265 osa.sa_flags = 0;
266 if (sigaction(SIGHUP, &sa, &osa) != 0) e(9);
267 if (osa.sa_handler != SIG_DFL) e(10);
268 if (osa.sa_mask != 0) e(11);
269 if (osa.sa_flags != s_empty) e(12);
271 /* Replace action and see if old value is read back correctly. */
272 sa.sa_handler = func2;
273 sa.sa_mask = s_ill_pip;
274 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
275 osa.sa_handler = SIG_IGN;
276 osa.sa_mask = s_empty;
277 osa.sa_flags = 0;
278 if (sigaction(SIGHUP, &sa, &osa) != 0) e(13);
279 if (osa.sa_handler != func1) e(14);
280 if (osa.sa_mask != s_ill) e(15);
281 if (osa.sa_flags != SA_NODEFER
282 && osa.sa_flags != (SA_NODEFER | SA_NOCLDSTOP)) e(16);
284 /* Replace action once more and check what is read back. */
285 sa.sa_handler = SIG_DFL;
286 sa.sa_mask = s_empty;
287 osa.sa_handler = SIG_IGN;
288 osa.sa_mask = s_empty;
289 osa.sa_flags = 0;
290 if (sigaction(SIGHUP, &sa, &osa) != 0) e(17);
291 if (osa.sa_handler != func2) e(18);
292 if (osa.sa_mask != s_ill_pip) e(19);
293 if (osa.sa_flags != (SA_RESETHAND | SA_NODEFER)) e(20);
295 /* Test sigprocmask(SIG_SETMASK, ...). */
296 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(18); /* block all */
297 if (sigemptyset(&s1) != 0) e(19);
298 errno = 0;
299 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(20); /* block none */
300 if (s1 != s_nokill_stop) e(21);
301 if (sigprocmask(SIG_SETMASK, &s_ill, &s1) != 0) e(22); /* block SIGILL */
302 errno = 0;
303 if (s1 != s_empty) e(23);
304 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(24); /* SIGILL+PIP */
305 if (s1 != s_ill) e(25);
306 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(26); /* block all */
307 if (s1 != s_ill_pip) e(27);
309 /* Test sigprocmask(SIG_UNBLOCK, ...) */
310 if (sigprocmask(SIG_UNBLOCK, &s_ill, &s1) != 0) e(28);
311 if (s1 != s_nokill_stop) e(29);
312 if (sigprocmask(SIG_UNBLOCK, &s_ill_pip, &s1) != 0) e(30);
313 s = s_nokill_stop;
314 if (sigdelset(&s, SIGILL) != 0) e(31);
315 if (s != s1) e(32);
316 if (sigprocmask(SIG_UNBLOCK, &s_empty, &s1) != 0) e(33);
317 s = s_nokill_stop;
318 if (sigdelset(&s, SIGILL) != 0) e(34);
319 if (sigdelset(&s, SIGPIPE) != 0) e(35);
320 if (s != s1) e(36);
321 s1 = s_nokill_stop;
322 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(37);
323 if (s != s1) e(38);
325 /* Test sigprocmask(SIG_BLOCK, ...) */
326 if (sigprocmask(SIG_BLOCK, &s_ill, &s1) != 0) e(39);
327 if (s1 != s_empty) e(40);
328 if (sigprocmask(SIG_BLOCK, &s_ill_pip, &s1) != 0) e(41);
329 if (s1 != s_ill) e(42);
330 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(43);
331 if (s1 != s_ill_pip) e(44);
333 /* Check error condition. */
334 errno = 0;
335 if (sigprocmask(20000, &s_full, &s1) != -1) e(45);
336 if (errno != EINVAL) e(46);
337 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(47);
338 if (s1 != s_nokill_stop) e(48);
340 /* If second arg is 0, nothing is set. */
341 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(49);
342 if (s1 != s_nokill_stop) e(50);
343 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(51);
344 if (s1 != s_nokill_stop) e(52);
345 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(53);
346 if (s1 != s_ill_pip) e(54);
347 if (sigprocmask(SIG_BLOCK, (sigset_t *) NULL, &s1) != 0) e(55);
348 if (s1 != s_ill_pip) e(56);
349 if (sigprocmask(SIG_UNBLOCK, (sigset_t *) NULL, &s1) != 0) e(57);
350 if (s1 != s_ill_pip) e(58);
352 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
353 s = s_empty;
354 if (sigaddset(&s, SIGKILL) != 0) e(59);
355 if (sigprocmask(SIG_BLOCK, &s, &s1) != 0) e(60);
356 if (s1 != s_ill_pip) e(61);
357 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(62);
358 if (s1 != s_ill_pip) e(63);
360 /* Test sigpending. At this moment, all signals are blocked. */
361 sa.sa_handler = func2;
362 sa.sa_mask = s_empty;
363 if (sigaction(SIGHUP, &sa, &osa) != 0) e(64);
364 p = getpid();
365 kill(p, SIGHUP); /* send SIGHUP to self */
366 if (sigpending(&s) != 0) e(65);
367 if (sigemptyset(&s1) != 0) e(66);
368 if (sigaddset(&s1, SIGHUP) != 0) e(67);
369 if (s != s1) e(68);
370 sa.sa_handler = SIG_IGN;
371 if (sigaction(SIGHUP, &sa, &osa) != 0) e(69);
372 if (sigpending(&s) != 0) e(70);
373 if (s != s_empty) e(71);
376 /*---------------------------------------------------------------------------*/
377 int x;
378 sigset_t glo_vol_set;
380 void catch1(signo)
381 int signo;
383 x = 42;
386 void catch2(signo)
387 int signo;
389 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0)
390 e(1);
393 /* Verify that signal(2), which is now built on top of sigaction(2), still
394 * works.
396 void test37c()
398 pid_t pid;
399 sigset_t sigset_var;
401 subtest = 3;
402 clearsigstate();
403 x = 0;
405 /* Verify an installed signal handler persists across a fork(2). */
406 if (signal(SIGTERM, catch1) == SIG_ERR) e(1);
407 switch (pid = fork()) {
408 case 0: /* child */
409 errct = 0;
410 while (x == 0);
411 if (x != 42) e(2);
412 exit(errct == 0 ? 0 : 1);
413 case -1: e(3); break;
414 default: /* parent */
415 sleep(1);
416 if (kill(pid, SIGTERM) != 0) e(4);
417 wait_for(pid);
418 break;
421 /* Verify that the return value is the previous handler. */
422 signal(SIGINT, SIG_IGN);
423 if (signal(SIGINT, catch2) != SIG_IGN) e(5);
424 if (signal(SIGINT, catch1) != catch2) e(6);
425 if (signal(SIGINT, SIG_DFL) != catch1) e(7);
426 if (signal(SIGINT, catch1) != SIG_DFL) e(8);
427 if (signal(SIGINT, SIG_DFL) != catch1) e(9);
428 if (signal(SIGINT, SIG_DFL) != SIG_DFL) e(10);
429 if (signal(SIGINT, catch1) != SIG_DFL) e(11);
431 /* Verify that SIG_ERR is correctly generated. */
432 if (signal(_NSIG, catch1) != SIG_ERR) e(12);
433 if (signal(0, catch1) != SIG_ERR) e(13);
434 if (signal(-1, SIG_DFL) != SIG_ERR) e(14);
436 /* Verify that caught signals are automatically reset to the default,
437 * and that further instances of the same signal are not blocked here
438 * or in the signal handler.
440 if (signal(SIGTERM, catch1) == SIG_ERR) e(15);
441 switch ((pid = fork())) {
442 case 0: /* child */
443 errct = 0;
444 while (x == 0);
445 if (x != 42) e(16);
446 if (sigismember((sigset_t *) &glo_vol_set, SIGTERM)) e(17);
447 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &sigset_var) != 0) e(18);
448 if (sigismember(&sigset_var, SIGTERM)) e(19);
450 #if 0
451 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
452 if (signal(SIGTERM, catch1) != SIG_DFL) e(20);
453 #else
454 if (signal(SIGTERM, catch1) != catch1) e(20);
455 #endif
456 exit(errct == 0 ? 0 : 1);
457 default: /* parent */
458 sleep(1);
459 if (kill(pid, SIGTERM) != 0) e(21);
460 wait_for(pid);
461 break;
462 case -1: e(22); break;
466 /*---------------------------------------------------------------------------*/
467 /* Test that the signal handler can be invoked recursively with the
468 * state being properly saved and restored.
471 static int y;
472 static int z;
474 void catch3(signo)
475 int signo;
477 if (z == 1) { /* catching a nested signal */
478 y = 2;
479 return;
481 z = 1;
482 if (kill(getpid(), SIGHUP) != 0) e(1);
483 while (y != 2);
484 y = 1;
487 void test37d()
489 struct sigaction act;
491 subtest = 4;
492 clearsigstate();
493 y = 0;
494 z = 0;
496 act.sa_handler = catch3;
497 act.sa_mask = 0;
498 act.sa_flags = SA_NODEFER; /* Otherwise, nested occurence of
499 * SIGINT is blocked. */
500 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) != 0) e(2);
501 if (kill(getpid(), SIGHUP) != 0) e(3);
502 if (y != 1) e(4);
505 /*---------------------------------------------------------------------------*/
507 /* Test that the signal mask in effect for the duration of a signal handler
508 * is as specified in POSIX Section 3, lines 718 -724. Test that the
509 * previous signal mask is restored when the signal handler returns.
512 void catch4(signo)
513 int signo;
515 sigset_t oset;
516 sigset_t set;
518 if (sigemptyset(&set) == -1) e(5001);
519 if (sigaddset(&set, SIGTERM) == -1) e(5002);
520 if (sigaddset(&set, SIGHUP) == -1) e(5003);
521 if (sigaddset(&set, SIGINT) == -1) e(5004);
522 if (sigaddset(&set, SIGPIPE) == -1) e(5005);
523 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) != 0) e(5006);
524 if (oset != set) e(5007);
527 void test37e()
529 struct sigaction act, oact;
530 sigset_t set, oset;
532 subtest = 5;
533 clearsigstate();
535 act.sa_handler = catch4;
536 sigemptyset(&act.sa_mask);
537 sigaddset(&act.sa_mask, SIGTERM);
538 sigaddset(&act.sa_mask, SIGHUP);
539 act.sa_flags = 0;
540 if (sigaction(SIGINT, &act, &oact) == -1) e(2);
542 if (sigemptyset(&set) == -1) e(3);
543 if (sigaddset(&set, SIGPIPE) == -1) e(4);
544 if (sigprocmask(SIG_SETMASK, &set, &oset) == -1) e(5);
545 if (kill(getpid(), SIGINT) == -1) e(6);
546 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) == -1) e(7);
547 if (sigemptyset(&set) == -1) e(8);
548 if (sigaddset(&set, SIGPIPE) == -1) e(9);
549 if (set != oset) e(10);
552 /*---------------------------------------------------------------------------*/
554 /* Test the basic functionality of sigsuspend(2). */
556 void catch5(signo)
557 int signo;
559 x = 1;
562 void test37f()
564 sigset_t set;
565 int r;
566 struct sigaction act;
567 pid_t pid;
569 subtest = 6;
570 clearsigstate();
572 switch (pid = fork()) {
573 case 0: /* child */
574 errct = 0;
575 sleep(1);
576 if (kill(getppid(), SIGINT) == -1) e(1);
577 exit(errct == 0 ? 0 : 1);
578 case -1: e(2); break;
579 default: /* parent */
580 if (sigemptyset(&act.sa_mask) == -1) e(3);
581 act.sa_flags = 0;
582 act.sa_handler = catch5;
583 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
585 if (sigemptyset(&set) == -1) e(5);
586 r = sigsuspend(&set);
588 if (r != -1 || errno != EINTR || x != 1) e(6);
589 wait_for(pid);
590 break;
594 /*----------------------------------------------------------------------*/
596 /* Test that sigsuspend() does block the signals specified in its
597 * argument, and after sigsuspend returns, the previous signal
598 * mask is restored.
600 * The child sends two signals to the parent SIGINT and then SIGPIPE,
601 * separated by a long delay. The parent executes sigsuspend() with
602 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
603 * will be invoked, then sigsuspend will return restoring the
604 * original signal mask, and then the SIGPIPE handler will be
605 * invoked.
608 void sigint_handler(signo)
609 int signo;
611 x = 1;
612 z++;
615 void sigpipe_handler(signo)
616 int signo;
618 x = 2;
619 z++;
622 void test37g()
624 sigset_t set;
625 int r;
626 struct sigaction act;
627 pid_t pid;
629 subtest = 7;
630 clearsigstate();
631 x = 0;
632 z = 0;
634 switch (pid = fork()) {
635 case 0: /* child */
636 errct = 0;
637 sleep(1);
638 if (kill(getppid(), SIGINT) == -1) e(1);
639 sleep(1);
640 if (kill(getppid(), SIGPIPE) == -1) e(2);
641 exit(errct == 0 ? 0 : 1);
642 case -1: e(3); break;
643 default: /* parent */
644 if (sigemptyset(&act.sa_mask) == -1) e(3);
645 act.sa_flags = 0;
646 act.sa_handler = sigint_handler;
647 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
649 act.sa_handler = sigpipe_handler;
650 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);
652 if (sigemptyset(&set) == -1) e(6);
653 if (sigaddset(&set, SIGINT) == -1) e(7);
654 r = sigsuspend(&set);
655 if (r != -1) e(8);
656 if (errno != EINTR) e(9);
657 if (z != 2) e(10);
658 if (x != 1) e(11);
659 wait_for(pid);
660 break;
664 /*--------------------------------------------------------------------------*/
666 /* Test that sigsuspend() does block the signals specified in its
667 * argument, and after sigsuspend returns, the previous signal
668 * mask is restored.
670 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
671 * and then SIGTERM, separated by a long delay. The parent executes
672 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
673 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
674 * will return restoring the original signal mask, and then the other
675 * two handlers will be invoked.
678 void sighup8(signo)
679 int signo;
681 x = 1;
682 z++;
685 void sigpip8(signo)
686 int signo;
688 x = 1;
689 z++;
692 void sigter8(signo)
693 int signo;
695 x = 2;
696 z++;
699 void test37h()
701 sigset_t set;
702 int r;
703 struct sigaction act;
704 pid_t pid;
706 subtest = 8;
707 clearsigstate();
708 x = 0;
709 z = 0;
711 switch (pid = fork()) {
712 case 0: /* child */
713 errct = 0;
714 sleep(1);
715 if (kill(getppid(), SIGHUP) == -1) e(1);
716 sleep(1);
717 if (kill(getppid(), SIGPIPE) == -1) e(2);
718 sleep(1);
719 if (kill(getppid(), SIGTERM) == -1) e(3);
720 exit(errct == 0 ? 0 : 1);
721 case -1: e(5); break;
722 default: /* parent */
723 if (sigemptyset(&act.sa_mask) == -1) e(6);
724 act.sa_flags = 0;
725 act.sa_handler = sighup8;
726 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7);
728 act.sa_handler = sigpip8;
729 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);
731 act.sa_handler = sigter8;
732 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);
734 if (sigemptyset(&set) == -1) e(10);
735 if (sigaddset(&set, SIGHUP) == -1) e(11);
736 if (sigaddset(&set, SIGPIPE) == -1) e(12);
737 r = sigsuspend(&set);
738 if (r != -1) e(13);
739 if (errno != EINTR) e(14);
740 if (z != 3) e(15);
741 if (x != 1) e(16);
742 wait_for(pid);
743 break;
747 /*--------------------------------------------------------------------------*/
749 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
750 * and SIGTERM, unblock these signals with sigprocmask, and verify
751 * that these signals are delivered.
754 void sighup9(signo)
755 int signo;
757 y++;
760 void sigter9(signo)
761 int signo;
763 z++;
766 void test37i()
768 sigset_t set;
769 struct sigaction act;
771 subtest = 9;
772 clearsigstate();
773 y = 0;
774 z = 0;
776 if (sigemptyset(&act.sa_mask) == -1) e(1);
777 act.sa_flags = 0;
779 act.sa_handler = sighup9;
780 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
782 act.sa_handler = sigter9;
783 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);
785 if (sigemptyset(&set) == -1) e(4);
786 if (sigaddset(&set, SIGTERM) == -1) e(5);
787 if (sigaddset(&set, SIGHUP) == -1) e(6);
788 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(7);
790 if (kill(getpid(), SIGHUP) == -1) e(8);
791 if (kill(getpid(), SIGTERM) == -1) e(9);
792 if (y != 0) e(10);
793 if (z != 0) e(11);
795 if (sigemptyset(&set) == -1) e(12);
796 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12);
797 if (y != 1) e(13);
798 if (z != 1) e(14);
801 /*---------------------------------------------------------------------------*/
803 /* Block SIGINT and then send this signal to ourself.
805 * Install signal handlers for SIGALRM and SIGINT.
807 * Set an alarm for 6 seconds, then sleep for 7.
809 * The SIGALRM should interrupt the sleep, but the SIGINT
810 * should remain pending.
813 void sighup10(signo)
814 int signo;
816 y++;
819 void sigalrm_handler10(signo)
820 int signo;
822 z++;
825 void test37j()
827 sigset_t set, set2;
828 struct sigaction act;
830 subtest = 10;
831 clearsigstate();
832 y = 0;
833 z = 0;
835 if (sigemptyset(&act.sa_mask) == -1) e(1);
836 act.sa_flags = 0;
838 act.sa_handler = sighup10;
839 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
841 act.sa_handler = sigalrm_handler10;
842 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);
844 if (sigemptyset(&set) == -1) e(4);
845 if (sigaddset(&set, SIGHUP) == -1) e(5);
846 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(6);
848 if (kill(getpid(), SIGHUP) == -1) e(7);
849 if (sigpending(&set) == -1) e(8);
850 if (sigemptyset(&set2) == -1) e(9);
851 if (sigaddset(&set2, SIGHUP) == -1) e(10);
852 if (set2 != set) e(11);
853 alarm(6);
854 sleep(7);
855 if (sigpending(&set) == -1) e(12);
856 if (set != set2) e(13);
857 if (y != 0) e(14);
858 if (z != 1) e(15);
861 /*--------------------------------------------------------------------------*/
863 void test37k()
865 subtest = 11;
867 void test37l()
869 subtest = 12;
872 /*---------------------------------------------------------------------------*/
874 /* Basic test for setjmp/longjmp. This includes testing that the
875 * signal mask is properly restored.
878 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
879 void _name(void) \
881 _type jb; \
882 sigset_t ss, ssexp; \
884 subtest = _subtest; \
885 clearsigstate(); \
887 ss = 0x32; \
888 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
889 if (_setjmp) { \
890 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
891 ssexp = _save ? 0x32 : 0x3abc; \
892 if ((ss ^ ssexp) & ~(1 << SIGKILL)) e(388); \
893 return; \
895 ss = 0x3abc; \
896 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
897 _longjmp; \
900 TEST_SETJMP(test37m, 13, jmp_buf, setjmp(jb), longjmp(jb, 1), 1)
901 TEST_SETJMP(test37p, 16, sigjmp_buf, sigsetjmp(jb, 0), siglongjmp(jb, 1), 0)
902 TEST_SETJMP(test37q, 17, sigjmp_buf, sigsetjmp(jb, 1), siglongjmp(jb, 1), 1)
904 void longjerr()
906 e(5);
909 /*--------------------------------------------------------------------------*/
911 /* Test for setjmp/longjmp.
913 * Catch a signal. While in signal handler do setjmp/longjmp.
916 void catch14(signo, code, scp)
917 int signo;
918 int code;
919 struct sigcontext *scp;
921 jmp_buf jb;
923 if (setjmp(jb)) {
924 x++;
925 sigreturn(scp);
926 e(1);
928 y++;
929 longjmp(jb, 1);
930 e(2);
933 void test37n()
935 struct sigaction act;
936 typedef _PROTOTYPE( void (*sighandler_t), (int sig) );
938 subtest = 14;
939 clearsigstate();
940 x = 0;
941 y = 0;
943 act.sa_flags = 0;
944 act.sa_mask = 0;
945 act.sa_handler = (sighandler_t) catch14; /* fudge */
946 if (sigaction(SIGSEGV, &act, (struct sigaction *) NULL) == -1) e(3);
947 if (kill(getpid(), SIGSEGV) == -1) e(4);
949 if (x != 1) e(5);
950 if (y != 1) e(6);
953 /*---------------------------------------------------------------------------*/
955 /* Test for setjmp/longjmp.
957 * Catch a signal. Longjmp out of signal handler.
959 jmp_buf glo_jb;
961 void catch15(signo)
962 int signo;
964 z++;
965 longjmp(glo_jb, 7);
966 e(1);
970 void test37o()
972 struct sigaction act;
973 int k;
975 subtest = 15;
976 clearsigstate();
977 z = 0;
979 act.sa_flags = 0;
980 act.sa_mask = 0;
981 act.sa_handler = catch15;
982 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2);
984 if ((k = setjmp(glo_jb))) {
985 if (z != 1) e(399);
986 if (k != 7) e(4);
987 return;
989 if (kill(getpid(), SIGALRM) == -1) e(5);
992 void clearsigstate()
994 int i;
995 sigset_t sigset_var;
997 /* Clear the signal state. */
998 for (i = 1; i < _NSIG; i++) signal(i, SIG_IGN);
999 for (i = 1; i < _NSIG; i++) signal(i, SIG_DFL);
1000 sigfillset(&sigset_var);
1001 sigprocmask(SIG_UNBLOCK, &sigset_var, (sigset_t *)NULL);
1004 void quit()
1007 chdir("..");
1008 system("rm -rf DIR*");
1010 if (errct == 0) {
1011 printf("ok\n");
1012 exit(0);
1013 } else {
1014 printf("%d errors\n", errct);
1015 exit(4);
1019 void wait_for(pid)
1020 pid_t pid;
1022 /* Expect exactly one child, and that it exits with 0. */
1024 int r;
1025 int status;
1027 errno = 0;
1028 while (1) {
1029 errno = 0;
1030 r = wait(&status);
1031 if (r == pid) {
1032 errno = 0;
1033 if (status != 0) e(90);
1034 return;
1036 if (r < 0) {
1037 e(91);
1038 return;
1040 e(92);
1044 void e(n)
1045 int n;
1047 char msgbuf[80];
1049 sprintf(msgbuf, "Subtest %d, error %d errno=%d ", subtest, n, errno);
1050 perror(msgbuf);
1051 if (errct++ > MAX_ERROR) {
1052 fprintf(stderr, "Too many errors; test aborted\n");
1053 chdir("..");
1054 system("rm -rf DIR*");
1055 exit(1);