1 /* test 37 - signals */
21 int iteration
, cumsig
, sig1
, sig2
;
23 int sigarray
[SIGS
] = {SIGHUP
, SIGILL
, SIGTRAP
, SIGABRT
, SIGIOT
,
24 SIGFPE
, SIGUSR1
, SIGSEGV
, SIGUSR2
, SIGPIPE
, SIGALRM
,
27 /* Prototypes produced automatically by mkptypes. */
28 int main(int argc
, char *argv
[]);
33 void catch1(int signo
);
34 void catch2(int signo
);
36 void catch3(int signo
);
38 void catch4(int signo
);
40 void catch5(int signo
);
42 void sigint_handler(int signo
);
43 void sigpipe_handler(int signo
);
45 void sighup8(int signo
);
46 void sigpip8(int signo
);
47 void sigter8(int signo
);
49 void sighup9(int signo
);
50 void sigter9(int signo
);
52 void sighup10(int signo
);
53 void sigalrm_handler10(int signo
);
63 void catch14(int signo
, int code
, struct sigcontext
* scp
);
65 void catch15(int signo
);
67 void clearsigstate(void);
68 void wait_for(int pid
);
80 if (argc
== 2) m
= atoi(argv
[1]);
82 for (i
= 0; i
< ITERATIONS
; i
++) {
84 if (m
& 0000001) test37a();
85 if (m
& 0000002) test37b();
86 if (m
& 0000004) test37c();
87 if (m
& 0000010) test37d();
88 if (m
& 0000020) test37e();
89 if (m
& 0000040) test37f();
90 if (m
& 0000100) test37g();
91 if (m
& 0000200) test37h();
92 if (m
& 0000400) test37i();
93 if (m
& 0001000) test37j();
94 if (m
& 0002000) test37k();
95 if (m
& 0004000) test37l();
96 if (m
& 0010000) test37m();
97 if (m
& 0020000) test37n();
98 if (m
& 0040000) test37o();
99 if (m
& 0100000) test37p();
100 if (m
& 0200000) test37q();
105 return(-1); /* Unreachable */
110 /* Test signal set management. */
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 if (sigismember(&s
, SIGPWR
) != 0) e(17);
136 /* Create a full set and see if any bits are off. */
137 if (sigfillset(&s
) != 0) e(19);
138 if (sigemptyset(&s
) != 0) e(20);
139 if (sigfillset(&s
) != 0) e(21);
140 if (sigismember(&s
, SIGHUP
) != 1) e(22);
141 if (sigismember(&s
, SIGINT
) != 1) e(23);
142 if (sigismember(&s
, SIGQUIT
) != 1) e(24);
143 if (sigismember(&s
, SIGILL
) != 1) e(25);
144 if (sigismember(&s
, SIGTRAP
) != 1) e(26);
145 if (sigismember(&s
, SIGABRT
) != 1) e(27);
146 if (sigismember(&s
, SIGIOT
) != 1) e(28);
147 if (sigismember(&s
, SIGFPE
) != 1) e(30);
148 if (sigismember(&s
, SIGKILL
) != 1) e(31);
149 if (sigismember(&s
, SIGUSR1
) != 1) e(32);
150 if (sigismember(&s
, SIGSEGV
) != 1) e(33);
151 if (sigismember(&s
, SIGUSR2
) != 1) e(34);
152 if (sigismember(&s
, SIGPIPE
) != 1) e(35);
153 if (sigismember(&s
, SIGALRM
) != 1) e(36);
154 if (sigismember(&s
, SIGTERM
) != 1) e(37);
155 if (sigismember(&s
, SIGPWR
) != 1) e(37);
157 /* Create an empty set, then turn on bits individually. */
158 if (sigemptyset(&s
) != 0) e(39);
159 if (sigaddset(&s
, SIGHUP
) != 0) e(40);
160 if (sigaddset(&s
, SIGINT
) != 0) e(41);
161 if (sigaddset(&s
, SIGQUIT
) != 0) e(42);
162 if (sigaddset(&s
, SIGILL
) != 0) e(43);
163 if (sigaddset(&s
, SIGTRAP
) != 0) e(44);
164 if (sigaddset(&s
, SIGPWR
) != 0) e(44);
166 /* See if the bits just turned on are indeed on. */
167 if (sigismember(&s
, SIGHUP
) != 1) e(45);
168 if (sigismember(&s
, SIGINT
) != 1) e(46);
169 if (sigismember(&s
, SIGQUIT
) != 1) e(47);
170 if (sigismember(&s
, SIGILL
) != 1) e(48);
171 if (sigismember(&s
, SIGTRAP
) != 1) e(49);
172 if (sigismember(&s
, SIGPWR
) != 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);
192 if (sigdelset(&s
, SIGPWR
) != 0) e(66);
194 if (sigismember(&s
, SIGHUP
) != 0) e(67);
195 if (sigismember(&s
, SIGINT
) != 0) e(68);
196 if (sigismember(&s
, SIGQUIT
) != 0) e(69);
197 if (sigismember(&s
, SIGILL
) != 0) e(70);
198 if (sigismember(&s
, SIGTRAP
) != 0) e(71);
199 if (sigismember(&s
, SIGABRT
) != 0) e(72);
200 if (sigismember(&s
, SIGIOT
) != 0) e(73);
201 if (sigismember(&s
, SIGFPE
) != 0) e(75);
202 if (sigismember(&s
, SIGKILL
) != 0) e(76);
203 if (sigismember(&s
, SIGUSR1
) != 0) e(77);
204 if (sigismember(&s
, SIGSEGV
) != 0) e(78);
205 if (sigismember(&s
, SIGUSR2
) != 0) e(79);
206 if (sigismember(&s
, SIGPIPE
) != 0) e(80);
207 if (sigismember(&s
, SIGALRM
) != 0) e(81);
208 if (sigismember(&s
, SIGTERM
) != 0) e(82);
209 if (sigismember(&s
, SIGPWR
) != 0) e(82);
225 int sigmemcmp(sigset_t
*s1
, sigset_t
*s2
, int size
)
229 assert(size
== sizeof(sigset_t
));
230 for(i
= 1; i
< _NSIG
; i
++) {
231 if(sigismember(s1
, i
) && !sigismember(s2
, i
)) {
232 fprintf(stderr
, "sig %d set in first but not in 2nd\n", i
);
235 if(!sigismember(s1
, i
) && sigismember(s2
, i
)) {
236 fprintf(stderr
, "sig %d not set in first but is in 2nd\n", i
);
246 /* Test sigprocmask and sigpending. */
249 sigset_t s
, s1
, s_empty
, s_full
, s_ill
, s_ill_pip
, s_nokill
, s_nokill_stop
;
250 struct sigaction sa
, osa
;
255 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
256 if (sigemptyset(&s_empty
) != 0) e(1);
257 if (sigemptyset(&s_ill
) != 0) e(2);
258 if (sigemptyset(&s_ill_pip
) != 0) e(3);
259 if (sigaddset(&s_ill
, SIGILL
) != 0) e(4);
260 if (sigaddset(&s_ill_pip
, SIGILL
) != 0) e(5);
261 if (sigaddset(&s_ill_pip
, SIGPIPE
) != 0) e(6);
262 if (sigfillset(&s_full
) != 0) e(7);
264 if (sigdelset(&s_nokill
, SIGKILL
) != 0) e(8);
265 s_nokill_stop
= s_nokill
;
266 if (sigdelset(&s_nokill_stop
, SIGSTOP
) != 0) e(8);
267 if (SIGSTOP
>= _NSIG
) e(666);
268 if (SIGSTOP
< _NSIG
&& sigdelset(&s_nokill
, SIGSTOP
) != 0) e(888);
270 /* Now get most of the signals into default state. Don't change SIGINT
271 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
273 sa
.sa_handler
= SIG_DFL
;
274 sa
.sa_mask
= s_empty
;
276 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, &sa
, &osa
);
278 /* The second argument may be zero. See if it wipes out the system. */
279 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, (struct sigaction
*) NULL
, &osa
);
281 /* Install a signal handler. */
282 sa
.sa_handler
= func1
;
284 sa
.sa_flags
= SA_NODEFER
| SA_NOCLDSTOP
;
285 osa
.sa_handler
= SIG_IGN
;
286 osa
.sa_mask
= s_empty
;
288 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(9);
289 if (osa
.sa_handler
!= SIG_DFL
) e(10);
290 if (sigmemcmp(&osa
.sa_mask
, &s_empty
, sizeof(s_empty
))) e(11);
291 if (osa
.sa_flags
!= 0) e(12);
293 /* Replace action and see if old value is read back correctly. */
294 sa
.sa_handler
= func2
;
295 sa
.sa_mask
= s_ill_pip
;
296 sa
.sa_flags
= SA_RESETHAND
| SA_NODEFER
;
297 osa
.sa_handler
= SIG_IGN
;
298 osa
.sa_mask
= s_empty
;
300 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(13);
301 if (osa
.sa_handler
!= func1
) e(14);
302 if (sigmemcmp(&osa
.sa_mask
, &s_ill
, sizeof(s_ill
))) e(15);
303 if (osa
.sa_flags
!= SA_NODEFER
304 && osa
.sa_flags
!= (SA_NODEFER
| SA_NOCLDSTOP
)) e(16);
306 /* Replace action once more and check what is read back. */
307 sa
.sa_handler
= SIG_DFL
;
308 sa
.sa_mask
= s_empty
;
309 osa
.sa_handler
= SIG_IGN
;
310 osa
.sa_mask
= s_empty
;
312 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(17);
313 if (osa
.sa_handler
!= func2
) e(18);
314 if (sigmemcmp(&osa
.sa_mask
, &s_ill_pip
, sizeof(s_ill_pip
))) e(19);
315 if (osa
.sa_flags
!= (SA_RESETHAND
| SA_NODEFER
)) e(20);
317 /* Test sigprocmask(SIG_SETMASK, ...). */
318 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(18); /* block all */
319 if (sigemptyset(&s1
) != 0) e(19);
321 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(20); /* block none */
322 if (sigmemcmp(&s1
, &s_nokill_stop
, sizeof(s1
))) e(21);
323 if (sigprocmask(SIG_SETMASK
, &s_ill
, &s1
) != 0) e(22); /* block SIGILL */
325 if (sigmemcmp(&s1
, &s_empty
, sizeof(s1
))) e(23);
326 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(24); /* SIGILL+PIP */
327 if (sigmemcmp(&s1
, &s_ill
, sizeof(s1
))) e(25);
328 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(26); /* block all */
329 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(27);
331 /* Test sigprocmask(SIG_UNBLOCK, ...) */
332 if (sigprocmask(SIG_UNBLOCK
, &s_ill
, &s1
) != 0) e(28);
333 if (sigmemcmp(&s1
, &s_nokill_stop
, sizeof(s1
))) e(29);
334 if (sigprocmask(SIG_UNBLOCK
, &s_ill_pip
, &s1
) != 0) e(30);
336 if (sigdelset(&s
, SIGILL
) != 0) e(31);
337 if (sigmemcmp(&s
, &s1
, sizeof(s
))) e(32);
338 if (sigprocmask(SIG_UNBLOCK
, &s_empty
, &s1
) != 0) e(33);
340 if (sigdelset(&s
, SIGILL
) != 0) e(34);
341 if (sigdelset(&s
, SIGPIPE
) != 0) e(35);
342 if (sigmemcmp(&s
, &s1
, sizeof(s
))) e(36);
344 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(37);
345 if (sigmemcmp(&s
, &s1
, sizeof(s
))) e(38);
347 /* Test sigprocmask(SIG_BLOCK, ...) */
348 if (sigprocmask(SIG_BLOCK
, &s_ill
, &s1
) != 0) e(39);
349 if (sigmemcmp(&s1
, &s_empty
, sizeof(s1
))) e(40);
350 if (sigprocmask(SIG_BLOCK
, &s_ill_pip
, &s1
) != 0) e(41);
351 if (sigmemcmp(&s1
, &s_ill
, sizeof(s1
))) e(42);
352 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(43);
353 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(44);
355 /* Check error condition. */
357 if (sigprocmask(20000, &s_full
, &s1
) != -1) e(45);
358 if (errno
!= EINVAL
) e(46);
359 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(47);
360 if (sigmemcmp(&s1
, &s_nokill_stop
, sizeof(s1
))) e(48);
362 /* If second arg is 0, nothing is set. */
363 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(49);
364 if (sigmemcmp(&s1
, &s_nokill_stop
, sizeof(s1
))) e(50);
365 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(51);
366 if (sigmemcmp(&s1
, &s_nokill_stop
, sizeof(s1
))) e(52);
367 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(53);
368 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(54);
369 if (sigprocmask(SIG_BLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(55);
370 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(56);
371 if (sigprocmask(SIG_UNBLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(57);
372 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(58);
374 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
376 if (sigaddset(&s
, SIGKILL
) != 0) e(59);
377 if (sigprocmask(SIG_BLOCK
, &s
, &s1
) != 0) e(60);
378 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(61);
379 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(62);
380 if (sigmemcmp(&s1
, &s_ill_pip
, sizeof(s1
))) e(63);
382 /* Test sigpending. At this moment, all signals are blocked. */
383 sa
.sa_handler
= func2
;
384 sa
.sa_mask
= s_empty
;
385 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(64);
387 kill(p
, SIGHUP
); /* send SIGHUP to self */
388 if (sigpending(&s
) != 0) e(65);
389 if (sigemptyset(&s1
) != 0) e(66);
390 if (sigaddset(&s1
, SIGHUP
) != 0) e(67);
391 if (sigmemcmp(&s
, &s1
, sizeof(s
))) e(68);
392 sa
.sa_handler
= SIG_IGN
;
393 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(69);
394 if (sigpending(&s
) != 0) e(70);
395 if (sigmemcmp(&s
, &s_empty
, sizeof(s
))) e(71);
398 /*---------------------------------------------------------------------------*/
400 sigset_t glo_vol_set
;
411 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, (sigset_t
*) &glo_vol_set
) != 0)
415 /* Verify that signal(2), which is now built on top of sigaction(2), still
427 /* Verify an installed signal handler persists across a fork(2). */
428 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(1);
429 switch (pid
= fork()) {
434 exit(errct
== 0 ? 0 : 1);
435 case -1: e(3); break;
436 default: /* parent */
438 if (kill(pid
, SIGTERM
) != 0) e(4);
443 /* Verify that the return value is the previous handler. */
444 signal(SIGINT
, SIG_IGN
);
445 if (signal(SIGINT
, catch2
) != SIG_IGN
) e(5);
446 if (signal(SIGINT
, catch1
) != catch2
) e(6);
447 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(7);
448 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(8);
449 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(9);
450 if (signal(SIGINT
, SIG_DFL
) != SIG_DFL
) e(10);
451 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(11);
453 /* Verify that SIG_ERR is correctly generated. */
454 if (signal(_NSIG
, catch1
) != SIG_ERR
) e(12);
455 if (signal(0, catch1
) != SIG_ERR
) e(13);
456 if (signal(-1, SIG_DFL
) != SIG_ERR
) e(14);
458 /* Verify that caught signals are automatically reset to the default,
459 * and that further instances of the same signal are not blocked here
460 * or in the signal handler.
462 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(15);
463 switch ((pid
= fork())) {
468 if (sigismember((sigset_t
*) &glo_vol_set
, SIGTERM
)) e(17);
469 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &sigset_var
) != 0) e(18);
470 if (sigismember(&sigset_var
, SIGTERM
)) e(19);
473 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
474 if (signal(SIGTERM
, catch1
) != SIG_DFL
) e(20);
476 if (signal(SIGTERM
, catch1
) != catch1
) e(20);
478 exit(errct
== 0 ? 0 : 1);
479 default: /* parent */
481 if (kill(pid
, SIGTERM
) != 0) e(21);
484 case -1: e(22); break;
488 /*---------------------------------------------------------------------------*/
489 /* Test that the signal handler can be invoked recursively with the
490 * state being properly saved and restored.
499 if (z
== 1) { /* catching a nested signal */
504 if (kill(getpid(), SIGHUP
) != 0) e(1);
511 struct sigaction act
;
518 act
.sa_handler
= catch3
;
519 sigemptyset(&act
.sa_mask
);
520 act
.sa_flags
= SA_NODEFER
; /* Otherwise, nested occurence of
521 * SIGINT is blocked. */
522 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) != 0) e(2);
523 if (kill(getpid(), SIGHUP
) != 0) e(3);
527 /*---------------------------------------------------------------------------*/
529 /* Test that the signal mask in effect for the duration of a signal handler
530 * is as specified in POSIX Section 3, lines 718 -724. Test that the
531 * previous signal mask is restored when the signal handler returns.
541 if (sigemptyset(&set
) == -1) e(5001);
542 if (sigaddset(&set
, SIGTERM
) == -1) e(5002);
543 if (sigaddset(&set
, SIGHUP
) == -1) e(5003);
544 if (sigaddset(&set
, SIGINT
) == -1) e(5004);
545 if (sigaddset(&set
, SIGPIPE
) == -1) e(5005);
546 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) != 0) e(5006);
547 if (sigmemcmp(&oset
, &set
, sizeof(set
))) e(5007);
552 struct sigaction act
, oact
;
558 act
.sa_handler
= catch4
;
559 sigemptyset(&act
.sa_mask
);
560 sigaddset(&act
.sa_mask
, SIGTERM
);
561 sigaddset(&act
.sa_mask
, SIGHUP
);
563 if (sigaction(SIGINT
, &act
, &oact
) == -1) e(2);
565 if (sigemptyset(&set
) == -1) e(3);
566 if (sigaddset(&set
, SIGPIPE
) == -1) e(4);
567 if (sigprocmask(SIG_SETMASK
, &set
, &oset
) == -1) e(5);
568 if (kill(getpid(), SIGINT
) == -1) e(6);
569 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) == -1) e(7);
570 if (sigemptyset(&set
) == -1) e(8);
571 if (sigaddset(&set
, SIGPIPE
) == -1) e(9);
572 if (sigmemcmp(&set
, &oset
, sizeof(set
))) e(10);
575 /*---------------------------------------------------------------------------*/
577 /* Test the basic functionality of sigsuspend(2). */
589 struct sigaction act
;
595 switch (pid
= fork()) {
599 if (kill(getppid(), SIGINT
) == -1) e(1);
600 exit(errct
== 0 ? 0 : 1);
601 case -1: e(2); break;
602 default: /* parent */
603 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
605 act
.sa_handler
= catch5
;
606 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
608 if (sigemptyset(&set
) == -1) e(5);
609 r
= sigsuspend(&set
);
611 if (r
!= -1 || errno
!= EINTR
|| x
!= 1) e(6);
617 /*----------------------------------------------------------------------*/
619 /* Test that sigsuspend() does block the signals specified in its
620 * argument, and after sigsuspend returns, the previous signal
623 * The child sends two signals to the parent SIGINT and then SIGPIPE,
624 * separated by a long delay. The parent executes sigsuspend() with
625 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
626 * will be invoked, then sigsuspend will return restoring the
627 * original signal mask, and then the SIGPIPE handler will be
631 void sigint_handler(signo
)
638 void sigpipe_handler(signo
)
649 struct sigaction act
;
657 switch (pid
= fork()) {
661 if (kill(getppid(), SIGINT
) == -1) e(1);
663 if (kill(getppid(), SIGPIPE
) == -1) e(2);
664 exit(errct
== 0 ? 0 : 1);
665 case -1: e(3); break;
666 default: /* parent */
667 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
669 act
.sa_handler
= sigint_handler
;
670 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
672 act
.sa_handler
= sigpipe_handler
;
673 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(5);
675 if (sigemptyset(&set
) == -1) e(6);
676 if (sigaddset(&set
, SIGINT
) == -1) e(7);
677 r
= sigsuspend(&set
);
679 if (errno
!= EINTR
) e(9);
687 /*--------------------------------------------------------------------------*/
689 /* Test that sigsuspend() does block the signals specified in its
690 * argument, and after sigsuspend returns, the previous signal
693 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
694 * and then SIGTERM, separated by a long delay. The parent executes
695 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
696 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
697 * will return restoring the original signal mask, and then the other
698 * two handlers will be invoked.
726 struct sigaction act
;
734 switch (pid
= fork()) {
738 if (kill(getppid(), SIGHUP
) == -1) e(1);
740 if (kill(getppid(), SIGPIPE
) == -1) e(2);
742 if (kill(getppid(), SIGTERM
) == -1) e(3);
743 exit(errct
== 0 ? 0 : 1);
744 case -1: e(5); break;
745 default: /* parent */
746 if (sigemptyset(&act
.sa_mask
) == -1) e(6);
748 act
.sa_handler
= sighup8
;
749 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(7);
751 act
.sa_handler
= sigpip8
;
752 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(8);
754 act
.sa_handler
= sigter8
;
755 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(9);
757 if (sigemptyset(&set
) == -1) e(10);
758 if (sigaddset(&set
, SIGHUP
) == -1) e(11);
759 if (sigaddset(&set
, SIGPIPE
) == -1) e(12);
760 r
= sigsuspend(&set
);
762 if (errno
!= EINTR
) e(14);
770 /*--------------------------------------------------------------------------*/
772 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
773 * and SIGTERM, unblock these signals with sigprocmask, and verify
774 * that these signals are delivered.
792 struct sigaction act
;
799 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
802 act
.sa_handler
= sighup9
;
803 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
805 act
.sa_handler
= sigter9
;
806 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
808 if (sigemptyset(&set
) == -1) e(4);
809 if (sigaddset(&set
, SIGTERM
) == -1) e(5);
810 if (sigaddset(&set
, SIGHUP
) == -1) e(6);
811 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(7);
813 if (kill(getpid(), SIGHUP
) == -1) e(8);
814 if (kill(getpid(), SIGTERM
) == -1) e(9);
818 if (sigemptyset(&set
) == -1) e(12);
819 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(12);
824 /*---------------------------------------------------------------------------*/
826 /* Block SIGINT and then send this signal to ourself.
828 * Install signal handlers for SIGALRM and SIGINT.
830 * Set an alarm for 6 seconds, then sleep for 7.
832 * The SIGALRM should interrupt the sleep, but the SIGINT
833 * should remain pending.
842 void sigalrm_handler10(signo
)
851 struct sigaction act
;
858 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
861 act
.sa_handler
= sighup10
;
862 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
864 act
.sa_handler
= sigalrm_handler10
;
865 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
867 if (sigemptyset(&set
) == -1) e(4);
868 if (sigaddset(&set
, SIGHUP
) == -1) e(5);
869 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(6);
871 if (kill(getpid(), SIGHUP
) == -1) e(7);
872 if (sigpending(&set
) == -1) e(8);
873 if (sigemptyset(&set2
) == -1) e(9);
874 if (sigaddset(&set2
, SIGHUP
) == -1) e(10);
875 if (sigmemcmp(&set2
, &set
, sizeof(set
))) e(11);
878 if (sigpending(&set
) == -1) e(12);
879 if (sigmemcmp(&set
, &set2
, sizeof(set
))) e(13);
884 /*--------------------------------------------------------------------------*/
895 /*---------------------------------------------------------------------------*/
897 /* Basic test for setjmp/longjmp. This includes testing that the
898 * signal mask is properly restored.
901 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
905 sigset_t ss, ss2, ss_orig; \
907 subtest = _subtest; \
910 sigemptyset(&ss); sigemptyset(&ss2); \
911 sigaddset(&ss, 2); sigaddset(&ss, 4); sigaddset(&ss, 5); \
912 sigaddset(&ss2, 20); sigaddset(&ss2, 22); sigaddset(&ss2, 65); \
913 memcpy(&ss_orig, &ss, sizeof(ss)); \
914 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
917 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
918 ssexp = _save ? ss_orig : ss2; \
919 sigdelset(&ssexp, SIGKILL); \
920 if (sigmemcmp(&ss, &ssexp, sizeof(ss))) e(388); \
924 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
928 TEST_SETJMP(test37m
, 13, jmp_buf, setjmp(jb
), longjmp(jb
, 1), 1)
929 TEST_SETJMP(test37p
, 16, sigjmp_buf
, sigsetjmp(jb
, 0), siglongjmp(jb
, 1), 0)
930 TEST_SETJMP(test37q
, 17, sigjmp_buf
, sigsetjmp(jb
, 1), siglongjmp(jb
, 1), 1)
937 /*--------------------------------------------------------------------------*/
939 /* Test for setjmp/longjmp.
941 * Catch a signal. While in signal handler do setjmp/longjmp.
944 void catch14(signo
, code
, scp
)
947 struct sigcontext
*scp
;
963 struct sigaction act
;
964 typedef void(*sighandler_t
) (int sig
);
972 sigemptyset(&act
.sa_mask
);
973 act
.sa_handler
= (sighandler_t
) catch14
; /* fudge */
974 if (sigaction(SIGSEGV
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
975 if (kill(getpid(), SIGSEGV
) == -1) e(4);
981 /*---------------------------------------------------------------------------*/
983 /* Test for setjmp/longjmp.
985 * Catch a signal. Longjmp out of signal handler.
1000 struct sigaction act
;
1008 sigemptyset(&act
.sa_mask
);
1009 act
.sa_handler
= catch15
;
1010 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
1012 if ((k
= setjmp(glo_jb
))) {
1017 if (kill(getpid(), SIGALRM
) == -1) e(5);
1020 void clearsigstate()
1023 sigset_t sigset_var
;
1025 /* Clear the signal state. */
1026 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_IGN
);
1027 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_DFL
);
1028 sigfillset(&sigset_var
);
1029 sigprocmask(SIG_UNBLOCK
, &sigset_var
, (sigset_t
*)NULL
);
1035 /* Expect exactly one child, and that it exits with 0. */
1046 if (status
!= 0) e(90);