source: trunk/minix/test/test37.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 25.9 KB
RevLine 
[9]1/* test 37 - signals */
2
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>
15
16#define ITERATIONS 2
17#define SIGS 14
18#define MAX_ERROR 4
19
20int iteration, cumsig, subtest, errct = 0, sig1, sig2;
21
22int sigarray[SIGS] = {SIGHUP, SIGILL, SIGTRAP, SIGABRT, SIGIOT,
23 SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM,
24 SIGTERM};
25
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));
68
69int main(argc, argv)
70int argc;
71char *argv[];
72{
73 int i, m = 0xFFFF;
74
75 sync();
76
77 if (argc == 2) m = atoi(argv[1]);
78
79 printf("Test 37 ");
80 fflush(stdout); /* have to flush for child's benefit */
81
82 system("rm -rf DIR_37; mkdir DIR_37");
83 chdir("DIR_37");
84
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();
102 }
103
104 quit();
105 return(-1); /* impossible */
106}
107
108void test37a()
109{
110/* Test signal set management. */
111
112 sigset_t s;
113
114 subtest = 1;
115 clearsigstate();
116
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);
134
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);
154
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);
162
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);
169
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);
181
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);
188
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);
204}
205
206void func1(sig)
207int sig;
208{
209 sig1++;
210}
211
212void func2(sig)
213int sig;
214{
215 sig2++;
216}
217
218void test37b()
219{
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;
225
226 subtest = 2;
227 clearsigstate();
228
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 */
245
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.
248 */
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);
253
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);
256
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);
268
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);
281
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);
292
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);
306
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);
322
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);
330
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);
337
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);
349
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);
357
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);
372}
373
374/*---------------------------------------------------------------------------*/
375int x;
376sigset_t glo_vol_set;
377
378void catch1(signo)
379int signo;
380{
381 x = 42;
382}
383
384void catch2(signo)
385int signo;
386{
387 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0)
388 e(1);
389}
390
391/* Verify that signal(2), which is now built on top of sigaction(2), still
392* works.
393*/
394void test37c()
395{
396 pid_t pid;
397 sigset_t sigset_var;
398
399 subtest = 3;
400 clearsigstate();
401 x = 0;
402
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;
417 }
418
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);
428
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);
433
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.
437 */
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);
447
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;
461 }
462}
463
464/*---------------------------------------------------------------------------*/
465/* Test that the signal handler can be invoked recursively with the
466* state being properly saved and restored.
467*/
468
469static int y;
470static int z;
471
472void catch3(signo)
473int signo;
474{
475 if (z == 1) { /* catching a nested signal */
476 y = 2;
477 return;
478 }
479 z = 1;
480 if (kill(getpid(), SIGHUP) != 0) e(1);
481 while (y != 2);
482 y = 1;
483}
484
485void test37d()
486{
487 struct sigaction act;
488
489 subtest = 4;
490 clearsigstate();
491 y = 0;
492 z = 0;
493
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);
501}
502
503/*---------------------------------------------------------------------------*/
504
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.
508*/
509
510void catch4(signo)
511int signo;
512{
513 sigset_t oset;
514 sigset_t set;
515
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);
523}
524
525void test37e()
526{
527 struct sigaction act, oact;
528 sigset_t set, oset;
529
530 subtest = 5;
531 clearsigstate();
532
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);
539
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);
548}
549
550/*---------------------------------------------------------------------------*/
551
552/* Test the basic functionality of sigsuspend(2). */
553
554void catch5(signo)
555int signo;
556{
557 x = 1;
558}
559
560void test37f()
561{
562 sigset_t set;
563 int r;
564 struct sigaction act;
565 pid_t pid;
566
567 subtest = 6;
568 clearsigstate();
569
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);
582
583 if (sigemptyset(&set) == -1) e(5);
584 r = sigsuspend(&set);
585
586 if (r != -1 || errno != EINTR || x != 1) e(6);
587 wait_for(pid);
588 break;
589 }
590}
591
592/*----------------------------------------------------------------------*/
593
594/* Test that sigsuspend() does block the signals specified in its
595* argument, and after sigsuspend returns, the previous signal
596* mask is restored.
597*
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.
604*/
605
606void sigint_handler(signo)
607int signo;
608{
609 x = 1;
610 z++;
611}
612
613void sigpipe_handler(signo)
614int signo;
615{
616 x = 2;
617 z++;
618}
619
620void test37g()
621{
622 sigset_t set;
623 int r;
624 struct sigaction act;
625 pid_t pid;
626
627 subtest = 7;
628 clearsigstate();
629 x = 0;
630 z = 0;
631
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);
646
647 act.sa_handler = sigpipe_handler;
648 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);
649
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;
659 }
660}
661
662/*--------------------------------------------------------------------------*/
663
664/* Test that sigsuspend() does block the signals specified in its
665* argument, and after sigsuspend returns, the previous signal
666* mask is restored.
667*
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.
674*/
675
676void sighup8(signo)
677int signo;
678{
679 x = 1;
680 z++;
681}
682
683void sigpip8(signo)
684int signo;
685{
686 x = 1;
687 z++;
688}
689
690void sigter8(signo)
691int signo;
692{
693 x = 2;
694 z++;
695}
696
697void test37h()
698{
699 sigset_t set;
700 int r;
701 struct sigaction act;
702 pid_t pid;
703
704 subtest = 8;
705 clearsigstate();
706 x = 0;
707 z = 0;
708
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);
725
726 act.sa_handler = sigpip8;
727 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);
728
729 act.sa_handler = sigter8;
730 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);
731
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;
742 }
743}
744
745/*--------------------------------------------------------------------------*/
746
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.
750*/
751
752void sighup9(signo)
753int signo;
754{
755 y++;
756}
757
758void sigter9(signo)
759int signo;
760{
761 z++;
762}
763
764void test37i()
765{
766 sigset_t set;
767 struct sigaction act;
768
769 subtest = 9;
770 clearsigstate();
771 y = 0;
772 z = 0;
773
774 if (sigemptyset(&act.sa_mask) == -1) e(1);
775 act.sa_flags = 0;
776
777 act.sa_handler = sighup9;
778 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
779
780 act.sa_handler = sigter9;
781 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);
782
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);
787
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);
792
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);
797}
798
799/*---------------------------------------------------------------------------*/
800
801/* Block SIGINT and then send this signal to ourself.
802*
803* Install signal handlers for SIGALRM and SIGINT.
804*
805* Set an alarm for 6 seconds, then sleep for 7.
806*
807* The SIGALRM should interrupt the sleep, but the SIGINT
808* should remain pending.
809*/
810
811void sighup10(signo)
812int signo;
813{
814 y++;
815}
816
817void sigalrm_handler10(signo)
818int signo;
819{
820 z++;
821}
822
823void test37j()
824{
825 sigset_t set, set2;
826 struct sigaction act;
827
828 subtest = 10;
829 clearsigstate();
830 y = 0;
831 z = 0;
832
833 if (sigemptyset(&act.sa_mask) == -1) e(1);
834 act.sa_flags = 0;
835
836 act.sa_handler = sighup10;
837 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
838
839 act.sa_handler = sigalrm_handler10;
840 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);
841
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);
845
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);
857}
858
859/*--------------------------------------------------------------------------*/
860
861void test37k()
862{
863 subtest = 11;
864}
865void test37l()
866{
867 subtest = 12;
868}
869
870/*---------------------------------------------------------------------------*/
871
872/* Basic test for setjmp/longjmp. This includes testing that the
873* signal mask is properly restored.
874*/
875
876void test37m()
877{
878 jmp_buf jb;
879 sigset_t ss;
880
881 subtest = 13;
882 clearsigstate();
883
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;
890 }
891 ss = 0x3abc;
892 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4);
893 longjmp(jb, 1);
894}
895
896void longjerr()
897{
898 e(5);
899}
900
901/*--------------------------------------------------------------------------*/
902
903/* Test for setjmp/longjmp.
904*
905* Catch a signal. While in signal handler do setjmp/longjmp.
906*/
907
908void catch14(signo, code, scp)
909int signo;
910int code;
911struct sigcontext *scp;
912{
913 jmp_buf jb;
914
915 if (setjmp(jb)) {
916 x++;
917 sigreturn(scp);
918 e(1);
919 }
920 y++;
921 longjmp(jb, 1);
922 e(2);
923}
924
925void test37n()
926{
927 struct sigaction act;
928 typedef _PROTOTYPE( void (*sighandler_t), (int sig) );
929
930 subtest = 14;
931 clearsigstate();
932 x = 0;
933 y = 0;
934
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);
940
941 if (x != 1) e(5);
942 if (y != 1) e(6);
943}
944
945/*---------------------------------------------------------------------------*/
946
947/* Test for setjmp/longjmp.
948 *
949 * Catch a signal. Longjmp out of signal handler.
950 */
951jmp_buf glo_jb;
952
953void catch15(signo)
954int signo;
955{
956 z++;
957 longjmp(glo_jb, 7);
958 e(1);
959
960}
961
962void test37o()
963{
964 struct sigaction act;
965 int k;
966
967 subtest = 15;
968 clearsigstate();
969 z = 0;
970
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);
975
976 if ((k = setjmp(glo_jb))) {
977 if (z != 1) e(399);
978 if (k != 7) e(4);
979 return;
980 }
981 if (kill(getpid(), SIGALRM) == -1) e(5);
982}
983
984void clearsigstate()
985{
986 int i;
987 sigset_t sigset_var;
988
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);
994}
995
996void quit()
997{
998
999 chdir("..");
1000 system("rm -rf DIR*");
1001
1002 if (errct == 0) {
1003 printf("ok\n");
1004 exit(0);
1005 } else {
1006 printf("%d errors\n", errct);
1007 exit(4);
1008 }
1009}
1010
1011void wait_for(pid)
1012pid_t pid;
1013{
1014/* Expect exactly one child, and that it exits with 0. */
1015
1016 int r;
1017 int status;
1018
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;
1027 }
1028 if (r < 0) {
1029 e(91);
1030 return;
1031 }
1032 e(92);
1033 }
1034}
1035
1036void e(n)
1037int n;
1038{
1039 char msgbuf[80];
1040
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);
1048 }
1049}
Note: See TracBrowser for help on using the repository browser.