1 /* test 37 - signals */
19 int iteration
, cumsig
, sig1
, sig2
;
21 int sigarray
[SIGS
] = {SIGHUP
, SIGILL
, SIGTRAP
, SIGABRT
, SIGIOT
,
22 SIGFPE
, SIGUSR1
, SIGSEGV
, SIGUSR2
, SIGPIPE
, SIGALRM
,
25 /* Prototypes produced automatically by mkptypes. */
26 int main(int argc
, char *argv
[]);
31 void catch1(int signo
);
32 void catch2(int signo
);
34 void catch3(int signo
);
36 void catch4(int signo
);
38 void catch5(int signo
);
40 void sigint_handler(int signo
);
41 void sigpipe_handler(int signo
);
43 void sighup8(int signo
);
44 void sigpip8(int signo
);
45 void sigter8(int signo
);
47 void sighup9(int signo
);
48 void sigter9(int signo
);
50 void sighup10(int signo
);
51 void sigalrm_handler10(int signo
);
61 void catch14(int signo
, int code
, struct sigcontext
* scp
);
63 void catch15(int signo
);
65 void clearsigstate(void);
66 void wait_for(int pid
);
78 if (argc
== 2) m
= atoi(argv
[1]);
80 for (i
= 0; i
< ITERATIONS
; i
++) {
82 if (m
& 0000001) test37a();
83 if (m
& 0000002) test37b();
84 if (m
& 0000004) test37c();
85 if (m
& 0000010) test37d();
86 if (m
& 0000020) test37e();
87 if (m
& 0000040) test37f();
88 if (m
& 0000100) test37g();
89 if (m
& 0000200) test37h();
90 if (m
& 0000400) test37i();
91 if (m
& 0001000) test37j();
92 if (m
& 0002000) test37k();
93 if (m
& 0004000) test37l();
94 if (m
& 0010000) test37m();
95 if (m
& 0020000) test37n();
96 if (m
& 0040000) test37o();
97 if (m
& 0100000) test37p();
98 if (m
& 0200000) test37q();
103 return(-1); /* Unreachable */
108 /* Test signal set management. */
115 /* Create an empty set and see if any bits are on. */
116 if (sigemptyset(&s
) != 0) e(1);
117 if (sigismember(&s
, SIGHUP
) != 0) e(2);
118 if (sigismember(&s
, SIGINT
) != 0) e(3);
119 if (sigismember(&s
, SIGQUIT
) != 0) e(4);
120 if (sigismember(&s
, SIGILL
) != 0) e(5);
121 if (sigismember(&s
, SIGTRAP
) != 0) e(6);
122 if (sigismember(&s
, SIGABRT
) != 0) e(7);
123 if (sigismember(&s
, SIGIOT
) != 0) e(8);
124 if (sigismember(&s
, SIGFPE
) != 0) e(10);
125 if (sigismember(&s
, SIGKILL
) != 0) e(11);
126 if (sigismember(&s
, SIGUSR1
) != 0) e(12);
127 if (sigismember(&s
, SIGSEGV
) != 0) e(13);
128 if (sigismember(&s
, SIGUSR2
) != 0) e(14);
129 if (sigismember(&s
, SIGPIPE
) != 0) e(15);
130 if (sigismember(&s
, SIGALRM
) != 0) e(16);
131 if (sigismember(&s
, SIGTERM
) != 0) e(17);
133 /* Create a full set and see if any bits are off. */
134 if (sigfillset(&s
) != 0) e(19);
135 if (sigemptyset(&s
) != 0) e(20);
136 if (sigfillset(&s
) != 0) e(21);
137 if (sigismember(&s
, SIGHUP
) != 1) e(22);
138 if (sigismember(&s
, SIGINT
) != 1) e(23);
139 if (sigismember(&s
, SIGQUIT
) != 1) e(24);
140 if (sigismember(&s
, SIGILL
) != 1) e(25);
141 if (sigismember(&s
, SIGTRAP
) != 1) e(26);
142 if (sigismember(&s
, SIGABRT
) != 1) e(27);
143 if (sigismember(&s
, SIGIOT
) != 1) e(28);
144 if (sigismember(&s
, SIGFPE
) != 1) e(30);
145 if (sigismember(&s
, SIGKILL
) != 1) e(31);
146 if (sigismember(&s
, SIGUSR1
) != 1) e(32);
147 if (sigismember(&s
, SIGSEGV
) != 1) e(33);
148 if (sigismember(&s
, SIGUSR2
) != 1) e(34);
149 if (sigismember(&s
, SIGPIPE
) != 1) e(35);
150 if (sigismember(&s
, SIGALRM
) != 1) e(36);
151 if (sigismember(&s
, SIGTERM
) != 1) e(37);
153 /* Create an empty set, then turn on bits individually. */
154 if (sigemptyset(&s
) != 0) e(39);
155 if (sigaddset(&s
, SIGHUP
) != 0) e(40);
156 if (sigaddset(&s
, SIGINT
) != 0) e(41);
157 if (sigaddset(&s
, SIGQUIT
) != 0) e(42);
158 if (sigaddset(&s
, SIGILL
) != 0) e(43);
159 if (sigaddset(&s
, SIGTRAP
) != 0) e(44);
161 /* See if the bits just turned on are indeed on. */
162 if (sigismember(&s
, SIGHUP
) != 1) e(45);
163 if (sigismember(&s
, SIGINT
) != 1) e(46);
164 if (sigismember(&s
, SIGQUIT
) != 1) e(47);
165 if (sigismember(&s
, SIGILL
) != 1) e(48);
166 if (sigismember(&s
, SIGTRAP
) != 1) e(49);
168 /* The others should be turned off. */
169 if (sigismember(&s
, SIGABRT
) != 0) e(50);
170 if (sigismember(&s
, SIGIOT
) != 0) e(51);
171 if (sigismember(&s
, SIGFPE
) != 0) e(53);
172 if (sigismember(&s
, SIGKILL
) != 0) e(54);
173 if (sigismember(&s
, SIGUSR1
) != 0) e(55);
174 if (sigismember(&s
, SIGSEGV
) != 0) e(56);
175 if (sigismember(&s
, SIGUSR2
) != 0) e(57);
176 if (sigismember(&s
, SIGPIPE
) != 0) e(58);
177 if (sigismember(&s
, SIGALRM
) != 0) e(59);
178 if (sigismember(&s
, SIGTERM
) != 0) e(60);
180 /* Now turn them off and see if all are off. */
181 if (sigdelset(&s
, SIGHUP
) != 0) e(62);
182 if (sigdelset(&s
, SIGINT
) != 0) e(63);
183 if (sigdelset(&s
, SIGQUIT
) != 0) e(64);
184 if (sigdelset(&s
, SIGILL
) != 0) e(65);
185 if (sigdelset(&s
, SIGTRAP
) != 0) e(66);
187 if (sigismember(&s
, SIGHUP
) != 0) e(67);
188 if (sigismember(&s
, SIGINT
) != 0) e(68);
189 if (sigismember(&s
, SIGQUIT
) != 0) e(69);
190 if (sigismember(&s
, SIGILL
) != 0) e(70);
191 if (sigismember(&s
, SIGTRAP
) != 0) e(71);
192 if (sigismember(&s
, SIGABRT
) != 0) e(72);
193 if (sigismember(&s
, SIGIOT
) != 0) e(73);
194 if (sigismember(&s
, SIGFPE
) != 0) e(75);
195 if (sigismember(&s
, SIGKILL
) != 0) e(76);
196 if (sigismember(&s
, SIGUSR1
) != 0) e(77);
197 if (sigismember(&s
, SIGSEGV
) != 0) e(78);
198 if (sigismember(&s
, SIGUSR2
) != 0) e(79);
199 if (sigismember(&s
, SIGPIPE
) != 0) e(80);
200 if (sigismember(&s
, SIGALRM
) != 0) e(81);
201 if (sigismember(&s
, SIGTERM
) != 0) e(82);
218 /* Test sigprocmask and sigpending. */
221 sigset_t s
, s1
, s_empty
, s_full
, s_ill
, s_ill_pip
, s_nokill
, s_nokill_stop
;
222 struct sigaction sa
, osa
;
227 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
228 if (sigemptyset(&s_empty
) != 0) e(1);
229 if (sigemptyset(&s_ill
) != 0) e(2);
230 if (sigemptyset(&s_ill_pip
) != 0) e(3);
231 if (sigaddset(&s_ill
, SIGILL
) != 0) e(4);
232 if (sigaddset(&s_ill_pip
, SIGILL
) != 0) e(5);
233 if (sigaddset(&s_ill_pip
, SIGPIPE
) != 0) e(6);
234 if (sigfillset(&s_full
) != 0) e(7);
236 if (sigdelset(&s_nokill
, SIGKILL
) != 0) e(8);
237 s_nokill_stop
= s_nokill
;
238 if (sigdelset(&s_nokill_stop
, SIGSTOP
) != 0) e(8);
239 if (SIGSTOP
>= _NSIG
) e(666);
240 if (SIGSTOP
< _NSIG
&& sigdelset(&s_nokill
, SIGSTOP
) != 0) e(888);
242 /* Now get most of the signals into default state. Don't change SIGINT
243 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
245 sa
.sa_handler
= SIG_DFL
;
246 sa
.sa_mask
= s_empty
;
248 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, &sa
, &osa
);
250 /* The second argument may be zero. See if it wipes out the system. */
251 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, (struct sigaction
*) NULL
, &osa
);
253 /* Install a signal handler. */
254 sa
.sa_handler
= func1
;
256 sa
.sa_flags
= SA_NODEFER
| SA_NOCLDSTOP
;
257 osa
.sa_handler
= SIG_IGN
;
258 osa
.sa_mask
= s_empty
;
260 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(9);
261 if (osa
.sa_handler
!= SIG_DFL
) e(10);
262 if (osa
.sa_mask
!= 0) e(11);
263 if (osa
.sa_flags
!= s_empty
) e(12);
265 /* Replace action and see if old value is read back correctly. */
266 sa
.sa_handler
= func2
;
267 sa
.sa_mask
= s_ill_pip
;
268 sa
.sa_flags
= SA_RESETHAND
| SA_NODEFER
;
269 osa
.sa_handler
= SIG_IGN
;
270 osa
.sa_mask
= s_empty
;
272 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(13);
273 if (osa
.sa_handler
!= func1
) e(14);
274 if (osa
.sa_mask
!= s_ill
) e(15);
275 if (osa
.sa_flags
!= SA_NODEFER
276 && osa
.sa_flags
!= (SA_NODEFER
| SA_NOCLDSTOP
)) e(16);
278 /* Replace action once more and check what is read back. */
279 sa
.sa_handler
= SIG_DFL
;
280 sa
.sa_mask
= s_empty
;
281 osa
.sa_handler
= SIG_IGN
;
282 osa
.sa_mask
= s_empty
;
284 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(17);
285 if (osa
.sa_handler
!= func2
) e(18);
286 if (osa
.sa_mask
!= s_ill_pip
) e(19);
287 if (osa
.sa_flags
!= (SA_RESETHAND
| SA_NODEFER
)) e(20);
289 /* Test sigprocmask(SIG_SETMASK, ...). */
290 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(18); /* block all */
291 if (sigemptyset(&s1
) != 0) e(19);
293 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(20); /* block none */
294 if (s1
!= s_nokill_stop
) e(21);
295 if (sigprocmask(SIG_SETMASK
, &s_ill
, &s1
) != 0) e(22); /* block SIGILL */
297 if (s1
!= s_empty
) e(23);
298 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(24); /* SIGILL+PIP */
299 if (s1
!= s_ill
) e(25);
300 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(26); /* block all */
301 if (s1
!= s_ill_pip
) e(27);
303 /* Test sigprocmask(SIG_UNBLOCK, ...) */
304 if (sigprocmask(SIG_UNBLOCK
, &s_ill
, &s1
) != 0) e(28);
305 if (s1
!= s_nokill_stop
) e(29);
306 if (sigprocmask(SIG_UNBLOCK
, &s_ill_pip
, &s1
) != 0) e(30);
308 if (sigdelset(&s
, SIGILL
) != 0) e(31);
310 if (sigprocmask(SIG_UNBLOCK
, &s_empty
, &s1
) != 0) e(33);
312 if (sigdelset(&s
, SIGILL
) != 0) e(34);
313 if (sigdelset(&s
, SIGPIPE
) != 0) e(35);
316 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(37);
319 /* Test sigprocmask(SIG_BLOCK, ...) */
320 if (sigprocmask(SIG_BLOCK
, &s_ill
, &s1
) != 0) e(39);
321 if (s1
!= s_empty
) e(40);
322 if (sigprocmask(SIG_BLOCK
, &s_ill_pip
, &s1
) != 0) e(41);
323 if (s1
!= s_ill
) e(42);
324 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(43);
325 if (s1
!= s_ill_pip
) e(44);
327 /* Check error condition. */
329 if (sigprocmask(20000, &s_full
, &s1
) != -1) e(45);
330 if (errno
!= EINVAL
) e(46);
331 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(47);
332 if (s1
!= s_nokill_stop
) e(48);
334 /* If second arg is 0, nothing is set. */
335 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(49);
336 if (s1
!= s_nokill_stop
) e(50);
337 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(51);
338 if (s1
!= s_nokill_stop
) e(52);
339 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(53);
340 if (s1
!= s_ill_pip
) e(54);
341 if (sigprocmask(SIG_BLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(55);
342 if (s1
!= s_ill_pip
) e(56);
343 if (sigprocmask(SIG_UNBLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(57);
344 if (s1
!= s_ill_pip
) e(58);
346 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
348 if (sigaddset(&s
, SIGKILL
) != 0) e(59);
349 if (sigprocmask(SIG_BLOCK
, &s
, &s1
) != 0) e(60);
350 if (s1
!= s_ill_pip
) e(61);
351 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(62);
352 if (s1
!= s_ill_pip
) e(63);
354 /* Test sigpending. At this moment, all signals are blocked. */
355 sa
.sa_handler
= func2
;
356 sa
.sa_mask
= s_empty
;
357 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(64);
359 kill(p
, SIGHUP
); /* send SIGHUP to self */
360 if (sigpending(&s
) != 0) e(65);
361 if (sigemptyset(&s1
) != 0) e(66);
362 if (sigaddset(&s1
, SIGHUP
) != 0) e(67);
364 sa
.sa_handler
= SIG_IGN
;
365 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(69);
366 if (sigpending(&s
) != 0) e(70);
367 if (s
!= s_empty
) e(71);
370 /*---------------------------------------------------------------------------*/
372 sigset_t glo_vol_set
;
383 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, (sigset_t
*) &glo_vol_set
) != 0)
387 /* Verify that signal(2), which is now built on top of sigaction(2), still
399 /* Verify an installed signal handler persists across a fork(2). */
400 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(1);
401 switch (pid
= fork()) {
406 exit(errct
== 0 ? 0 : 1);
407 case -1: e(3); break;
408 default: /* parent */
410 if (kill(pid
, SIGTERM
) != 0) e(4);
415 /* Verify that the return value is the previous handler. */
416 signal(SIGINT
, SIG_IGN
);
417 if (signal(SIGINT
, catch2
) != SIG_IGN
) e(5);
418 if (signal(SIGINT
, catch1
) != catch2
) e(6);
419 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(7);
420 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(8);
421 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(9);
422 if (signal(SIGINT
, SIG_DFL
) != SIG_DFL
) e(10);
423 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(11);
425 /* Verify that SIG_ERR is correctly generated. */
426 if (signal(_NSIG
, catch1
) != SIG_ERR
) e(12);
427 if (signal(0, catch1
) != SIG_ERR
) e(13);
428 if (signal(-1, SIG_DFL
) != SIG_ERR
) e(14);
430 /* Verify that caught signals are automatically reset to the default,
431 * and that further instances of the same signal are not blocked here
432 * or in the signal handler.
434 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(15);
435 switch ((pid
= fork())) {
440 if (sigismember((sigset_t
*) &glo_vol_set
, SIGTERM
)) e(17);
441 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &sigset_var
) != 0) e(18);
442 if (sigismember(&sigset_var
, SIGTERM
)) e(19);
445 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
446 if (signal(SIGTERM
, catch1
) != SIG_DFL
) e(20);
448 if (signal(SIGTERM
, catch1
) != catch1
) e(20);
450 exit(errct
== 0 ? 0 : 1);
451 default: /* parent */
453 if (kill(pid
, SIGTERM
) != 0) e(21);
456 case -1: e(22); break;
460 /*---------------------------------------------------------------------------*/
461 /* Test that the signal handler can be invoked recursively with the
462 * state being properly saved and restored.
471 if (z
== 1) { /* catching a nested signal */
476 if (kill(getpid(), SIGHUP
) != 0) e(1);
483 struct sigaction act
;
490 act
.sa_handler
= catch3
;
492 act
.sa_flags
= SA_NODEFER
; /* Otherwise, nested occurence of
493 * SIGINT is blocked. */
494 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) != 0) e(2);
495 if (kill(getpid(), SIGHUP
) != 0) e(3);
499 /*---------------------------------------------------------------------------*/
501 /* Test that the signal mask in effect for the duration of a signal handler
502 * is as specified in POSIX Section 3, lines 718 -724. Test that the
503 * previous signal mask is restored when the signal handler returns.
512 if (sigemptyset(&set
) == -1) e(5001);
513 if (sigaddset(&set
, SIGTERM
) == -1) e(5002);
514 if (sigaddset(&set
, SIGHUP
) == -1) e(5003);
515 if (sigaddset(&set
, SIGINT
) == -1) e(5004);
516 if (sigaddset(&set
, SIGPIPE
) == -1) e(5005);
517 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) != 0) e(5006);
518 if (oset
!= set
) e(5007);
523 struct sigaction act
, oact
;
529 act
.sa_handler
= catch4
;
530 sigemptyset(&act
.sa_mask
);
531 sigaddset(&act
.sa_mask
, SIGTERM
);
532 sigaddset(&act
.sa_mask
, SIGHUP
);
534 if (sigaction(SIGINT
, &act
, &oact
) == -1) e(2);
536 if (sigemptyset(&set
) == -1) e(3);
537 if (sigaddset(&set
, SIGPIPE
) == -1) e(4);
538 if (sigprocmask(SIG_SETMASK
, &set
, &oset
) == -1) e(5);
539 if (kill(getpid(), SIGINT
) == -1) e(6);
540 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) == -1) e(7);
541 if (sigemptyset(&set
) == -1) e(8);
542 if (sigaddset(&set
, SIGPIPE
) == -1) e(9);
543 if (set
!= oset
) e(10);
546 /*---------------------------------------------------------------------------*/
548 /* Test the basic functionality of sigsuspend(2). */
560 struct sigaction act
;
566 switch (pid
= fork()) {
570 if (kill(getppid(), SIGINT
) == -1) e(1);
571 exit(errct
== 0 ? 0 : 1);
572 case -1: e(2); break;
573 default: /* parent */
574 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
576 act
.sa_handler
= catch5
;
577 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
579 if (sigemptyset(&set
) == -1) e(5);
580 r
= sigsuspend(&set
);
582 if (r
!= -1 || errno
!= EINTR
|| x
!= 1) e(6);
588 /*----------------------------------------------------------------------*/
590 /* Test that sigsuspend() does block the signals specified in its
591 * argument, and after sigsuspend returns, the previous signal
594 * The child sends two signals to the parent SIGINT and then SIGPIPE,
595 * separated by a long delay. The parent executes sigsuspend() with
596 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
597 * will be invoked, then sigsuspend will return restoring the
598 * original signal mask, and then the SIGPIPE handler will be
602 void sigint_handler(signo
)
609 void sigpipe_handler(signo
)
620 struct sigaction act
;
628 switch (pid
= fork()) {
632 if (kill(getppid(), SIGINT
) == -1) e(1);
634 if (kill(getppid(), SIGPIPE
) == -1) e(2);
635 exit(errct
== 0 ? 0 : 1);
636 case -1: e(3); break;
637 default: /* parent */
638 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
640 act
.sa_handler
= sigint_handler
;
641 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
643 act
.sa_handler
= sigpipe_handler
;
644 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(5);
646 if (sigemptyset(&set
) == -1) e(6);
647 if (sigaddset(&set
, SIGINT
) == -1) e(7);
648 r
= sigsuspend(&set
);
650 if (errno
!= EINTR
) e(9);
658 /*--------------------------------------------------------------------------*/
660 /* Test that sigsuspend() does block the signals specified in its
661 * argument, and after sigsuspend returns, the previous signal
664 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
665 * and then SIGTERM, separated by a long delay. The parent executes
666 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
667 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
668 * will return restoring the original signal mask, and then the other
669 * two handlers will be invoked.
697 struct sigaction act
;
705 switch (pid
= fork()) {
709 if (kill(getppid(), SIGHUP
) == -1) e(1);
711 if (kill(getppid(), SIGPIPE
) == -1) e(2);
713 if (kill(getppid(), SIGTERM
) == -1) e(3);
714 exit(errct
== 0 ? 0 : 1);
715 case -1: e(5); break;
716 default: /* parent */
717 if (sigemptyset(&act
.sa_mask
) == -1) e(6);
719 act
.sa_handler
= sighup8
;
720 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(7);
722 act
.sa_handler
= sigpip8
;
723 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(8);
725 act
.sa_handler
= sigter8
;
726 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(9);
728 if (sigemptyset(&set
) == -1) e(10);
729 if (sigaddset(&set
, SIGHUP
) == -1) e(11);
730 if (sigaddset(&set
, SIGPIPE
) == -1) e(12);
731 r
= sigsuspend(&set
);
733 if (errno
!= EINTR
) e(14);
741 /*--------------------------------------------------------------------------*/
743 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
744 * and SIGTERM, unblock these signals with sigprocmask, and verify
745 * that these signals are delivered.
763 struct sigaction act
;
770 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
773 act
.sa_handler
= sighup9
;
774 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
776 act
.sa_handler
= sigter9
;
777 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
779 if (sigemptyset(&set
) == -1) e(4);
780 if (sigaddset(&set
, SIGTERM
) == -1) e(5);
781 if (sigaddset(&set
, SIGHUP
) == -1) e(6);
782 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(7);
784 if (kill(getpid(), SIGHUP
) == -1) e(8);
785 if (kill(getpid(), SIGTERM
) == -1) e(9);
789 if (sigemptyset(&set
) == -1) e(12);
790 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(12);
795 /*---------------------------------------------------------------------------*/
797 /* Block SIGINT and then send this signal to ourself.
799 * Install signal handlers for SIGALRM and SIGINT.
801 * Set an alarm for 6 seconds, then sleep for 7.
803 * The SIGALRM should interrupt the sleep, but the SIGINT
804 * should remain pending.
813 void sigalrm_handler10(signo
)
822 struct sigaction act
;
829 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
832 act
.sa_handler
= sighup10
;
833 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
835 act
.sa_handler
= sigalrm_handler10
;
836 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
838 if (sigemptyset(&set
) == -1) e(4);
839 if (sigaddset(&set
, SIGHUP
) == -1) e(5);
840 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(6);
842 if (kill(getpid(), SIGHUP
) == -1) e(7);
843 if (sigpending(&set
) == -1) e(8);
844 if (sigemptyset(&set2
) == -1) e(9);
845 if (sigaddset(&set2
, SIGHUP
) == -1) e(10);
846 if (set2
!= set
) e(11);
849 if (sigpending(&set
) == -1) e(12);
850 if (set
!= set2
) e(13);
855 /*--------------------------------------------------------------------------*/
866 /*---------------------------------------------------------------------------*/
868 /* Basic test for setjmp/longjmp. This includes testing that the
869 * signal mask is properly restored.
872 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
876 sigset_t ss, ssexp; \
878 subtest = _subtest; \
882 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
884 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
885 ssexp = _save ? 0x32 : 0x3abc; \
886 if ((ss ^ ssexp) & ~(1 << SIGKILL)) e(388); \
890 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
894 TEST_SETJMP(test37m
, 13, jmp_buf, setjmp(jb
), longjmp(jb
, 1), 1)
895 TEST_SETJMP(test37p
, 16, sigjmp_buf
, sigsetjmp(jb
, 0), siglongjmp(jb
, 1), 0)
896 TEST_SETJMP(test37q
, 17, sigjmp_buf
, sigsetjmp(jb
, 1), siglongjmp(jb
, 1), 1)
903 /*--------------------------------------------------------------------------*/
905 /* Test for setjmp/longjmp.
907 * Catch a signal. While in signal handler do setjmp/longjmp.
910 void catch14(signo
, code
, scp
)
913 struct sigcontext
*scp
;
929 struct sigaction act
;
930 typedef void(*sighandler_t
) (int sig
);
939 act
.sa_handler
= (sighandler_t
) catch14
; /* fudge */
940 if (sigaction(SIGSEGV
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
941 if (kill(getpid(), SIGSEGV
) == -1) e(4);
947 /*---------------------------------------------------------------------------*/
949 /* Test for setjmp/longjmp.
951 * Catch a signal. Longjmp out of signal handler.
966 struct sigaction act
;
975 act
.sa_handler
= catch15
;
976 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
978 if ((k
= setjmp(glo_jb
))) {
983 if (kill(getpid(), SIGALRM
) == -1) e(5);
991 /* Clear the signal state. */
992 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_IGN
);
993 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_DFL
);
994 sigfillset(&sigset_var
);
995 sigprocmask(SIG_UNBLOCK
, &sigset_var
, (sigset_t
*)NULL
);
1001 /* Expect exactly one child, and that it exits with 0. */
1012 if (status
!= 0) e(90);