1 /* test 37 - signals */
20 int iteration
, cumsig
, sig1
, sig2
;
22 int sigarray
[SIGS
] = {SIGHUP
, SIGILL
, SIGTRAP
, SIGABRT
, SIGIOT
,
23 SIGFPE
, SIGUSR1
, SIGSEGV
, SIGUSR2
, SIGPIPE
, SIGALRM
,
26 /* Prototypes produced automatically by mkptypes. */
27 int main(int argc
, char *argv
[]);
32 void catch1(int signo
);
33 void catch2(int signo
);
35 void catch3(int signo
);
37 void catch4(int signo
);
39 void catch5(int signo
);
41 void sigint_handler(int signo
);
42 void sigpipe_handler(int signo
);
44 void sighup8(int signo
);
45 void sigpip8(int signo
);
46 void sigter8(int signo
);
48 void sighup9(int signo
);
49 void sigter9(int signo
);
51 void sighup10(int signo
);
52 void sigalrm_handler10(int signo
);
62 void catch14(int signo
, int code
, struct sigcontext
* scp
);
64 void catch15(int signo
);
66 void clearsigstate(void);
67 void wait_for(int pid
);
79 if (argc
== 2) m
= atoi(argv
[1]);
81 for (i
= 0; i
< ITERATIONS
; i
++) {
83 if (m
& 0000001) test37a();
84 if (m
& 0000002) test37b();
85 if (m
& 0000004) test37c();
86 if (m
& 0000010) test37d();
87 if (m
& 0000020) test37e();
88 if (m
& 0000040) test37f();
89 if (m
& 0000100) test37g();
90 if (m
& 0000200) test37h();
91 if (m
& 0000400) test37i();
92 if (m
& 0001000) test37j();
93 if (m
& 0002000) test37k();
94 if (m
& 0004000) test37l();
95 if (m
& 0010000) test37m();
96 if (m
& 0020000) test37n();
97 if (m
& 0040000) test37o();
98 if (m
& 0100000) test37p();
99 if (m
& 0200000) test37q();
104 return(-1); /* Unreachable */
109 /* Test signal set management. */
116 /* Create an empty set and see if any bits are on. */
117 if (sigemptyset(&s
) != 0) e(1);
118 if (sigismember(&s
, SIGHUP
) != 0) e(2);
119 if (sigismember(&s
, SIGINT
) != 0) e(3);
120 if (sigismember(&s
, SIGQUIT
) != 0) e(4);
121 if (sigismember(&s
, SIGILL
) != 0) e(5);
122 if (sigismember(&s
, SIGTRAP
) != 0) e(6);
123 if (sigismember(&s
, SIGABRT
) != 0) e(7);
124 if (sigismember(&s
, SIGIOT
) != 0) e(8);
125 if (sigismember(&s
, SIGFPE
) != 0) e(10);
126 if (sigismember(&s
, SIGKILL
) != 0) e(11);
127 if (sigismember(&s
, SIGUSR1
) != 0) e(12);
128 if (sigismember(&s
, SIGSEGV
) != 0) e(13);
129 if (sigismember(&s
, SIGUSR2
) != 0) e(14);
130 if (sigismember(&s
, SIGPIPE
) != 0) e(15);
131 if (sigismember(&s
, SIGALRM
) != 0) e(16);
132 if (sigismember(&s
, SIGTERM
) != 0) e(17);
134 /* Create a full set and see if any bits are off. */
135 if (sigfillset(&s
) != 0) e(19);
136 if (sigemptyset(&s
) != 0) e(20);
137 if (sigfillset(&s
) != 0) e(21);
138 if (sigismember(&s
, SIGHUP
) != 1) e(22);
139 if (sigismember(&s
, SIGINT
) != 1) e(23);
140 if (sigismember(&s
, SIGQUIT
) != 1) e(24);
141 if (sigismember(&s
, SIGILL
) != 1) e(25);
142 if (sigismember(&s
, SIGTRAP
) != 1) e(26);
143 if (sigismember(&s
, SIGABRT
) != 1) e(27);
144 if (sigismember(&s
, SIGIOT
) != 1) e(28);
145 if (sigismember(&s
, SIGFPE
) != 1) e(30);
146 if (sigismember(&s
, SIGKILL
) != 1) e(31);
147 if (sigismember(&s
, SIGUSR1
) != 1) e(32);
148 if (sigismember(&s
, SIGSEGV
) != 1) e(33);
149 if (sigismember(&s
, SIGUSR2
) != 1) e(34);
150 if (sigismember(&s
, SIGPIPE
) != 1) e(35);
151 if (sigismember(&s
, SIGALRM
) != 1) e(36);
152 if (sigismember(&s
, SIGTERM
) != 1) e(37);
154 /* Create an empty set, then turn on bits individually. */
155 if (sigemptyset(&s
) != 0) e(39);
156 if (sigaddset(&s
, SIGHUP
) != 0) e(40);
157 if (sigaddset(&s
, SIGINT
) != 0) e(41);
158 if (sigaddset(&s
, SIGQUIT
) != 0) e(42);
159 if (sigaddset(&s
, SIGILL
) != 0) e(43);
160 if (sigaddset(&s
, SIGTRAP
) != 0) e(44);
162 /* See if the bits just turned on are indeed on. */
163 if (sigismember(&s
, SIGHUP
) != 1) e(45);
164 if (sigismember(&s
, SIGINT
) != 1) e(46);
165 if (sigismember(&s
, SIGQUIT
) != 1) e(47);
166 if (sigismember(&s
, SIGILL
) != 1) e(48);
167 if (sigismember(&s
, SIGTRAP
) != 1) e(49);
169 /* The others should be turned off. */
170 if (sigismember(&s
, SIGABRT
) != 0) e(50);
171 if (sigismember(&s
, SIGIOT
) != 0) e(51);
172 if (sigismember(&s
, SIGFPE
) != 0) e(53);
173 if (sigismember(&s
, SIGKILL
) != 0) e(54);
174 if (sigismember(&s
, SIGUSR1
) != 0) e(55);
175 if (sigismember(&s
, SIGSEGV
) != 0) e(56);
176 if (sigismember(&s
, SIGUSR2
) != 0) e(57);
177 if (sigismember(&s
, SIGPIPE
) != 0) e(58);
178 if (sigismember(&s
, SIGALRM
) != 0) e(59);
179 if (sigismember(&s
, SIGTERM
) != 0) e(60);
181 /* Now turn them off and see if all are off. */
182 if (sigdelset(&s
, SIGHUP
) != 0) e(62);
183 if (sigdelset(&s
, SIGINT
) != 0) e(63);
184 if (sigdelset(&s
, SIGQUIT
) != 0) e(64);
185 if (sigdelset(&s
, SIGILL
) != 0) e(65);
186 if (sigdelset(&s
, SIGTRAP
) != 0) e(66);
188 if (sigismember(&s
, SIGHUP
) != 0) e(67);
189 if (sigismember(&s
, SIGINT
) != 0) e(68);
190 if (sigismember(&s
, SIGQUIT
) != 0) e(69);
191 if (sigismember(&s
, SIGILL
) != 0) e(70);
192 if (sigismember(&s
, SIGTRAP
) != 0) e(71);
193 if (sigismember(&s
, SIGABRT
) != 0) e(72);
194 if (sigismember(&s
, SIGIOT
) != 0) e(73);
195 if (sigismember(&s
, SIGFPE
) != 0) e(75);
196 if (sigismember(&s
, SIGKILL
) != 0) e(76);
197 if (sigismember(&s
, SIGUSR1
) != 0) e(77);
198 if (sigismember(&s
, SIGSEGV
) != 0) e(78);
199 if (sigismember(&s
, SIGUSR2
) != 0) e(79);
200 if (sigismember(&s
, SIGPIPE
) != 0) e(80);
201 if (sigismember(&s
, SIGALRM
) != 0) e(81);
202 if (sigismember(&s
, SIGTERM
) != 0) e(82);
219 /* Test sigprocmask and sigpending. */
222 sigset_t s
, s1
, s_empty
, s_full
, s_ill
, s_ill_pip
, s_nokill
, s_nokill_stop
;
223 struct sigaction sa
, osa
;
228 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
229 if (sigemptyset(&s_empty
) != 0) e(1);
230 if (sigemptyset(&s_ill
) != 0) e(2);
231 if (sigemptyset(&s_ill_pip
) != 0) e(3);
232 if (sigaddset(&s_ill
, SIGILL
) != 0) e(4);
233 if (sigaddset(&s_ill_pip
, SIGILL
) != 0) e(5);
234 if (sigaddset(&s_ill_pip
, SIGPIPE
) != 0) e(6);
235 if (sigfillset(&s_full
) != 0) e(7);
237 if (sigdelset(&s_nokill
, SIGKILL
) != 0) e(8);
238 s_nokill_stop
= s_nokill
;
239 if (sigdelset(&s_nokill_stop
, SIGSTOP
) != 0) e(8);
240 if (SIGSTOP
>= _NSIG
) e(666);
241 if (SIGSTOP
< _NSIG
&& sigdelset(&s_nokill
, SIGSTOP
) != 0) e(888);
243 /* Now get most of the signals into default state. Don't change SIGINT
244 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
246 sa
.sa_handler
= SIG_DFL
;
247 sa
.sa_mask
= s_empty
;
249 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, &sa
, &osa
);
251 /* The second argument may be zero. See if it wipes out the system. */
252 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, (struct sigaction
*) NULL
, &osa
);
254 /* Install a signal handler. */
255 sa
.sa_handler
= func1
;
257 sa
.sa_flags
= SA_NODEFER
| SA_NOCLDSTOP
;
258 osa
.sa_handler
= SIG_IGN
;
259 osa
.sa_mask
= s_empty
;
261 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(9);
262 if (osa
.sa_handler
!= SIG_DFL
) e(10);
263 if (osa
.sa_mask
!= 0) e(11);
264 if (osa
.sa_flags
!= s_empty
) e(12);
266 /* Replace action and see if old value is read back correctly. */
267 sa
.sa_handler
= func2
;
268 sa
.sa_mask
= s_ill_pip
;
269 sa
.sa_flags
= SA_RESETHAND
| SA_NODEFER
;
270 osa
.sa_handler
= SIG_IGN
;
271 osa
.sa_mask
= s_empty
;
273 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(13);
274 if (osa
.sa_handler
!= func1
) e(14);
275 if (osa
.sa_mask
!= s_ill
) e(15);
276 if (osa
.sa_flags
!= SA_NODEFER
277 && osa
.sa_flags
!= (SA_NODEFER
| SA_NOCLDSTOP
)) e(16);
279 /* Replace action once more and check what is read back. */
280 sa
.sa_handler
= SIG_DFL
;
281 sa
.sa_mask
= s_empty
;
282 osa
.sa_handler
= SIG_IGN
;
283 osa
.sa_mask
= s_empty
;
285 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(17);
286 if (osa
.sa_handler
!= func2
) e(18);
287 if (osa
.sa_mask
!= s_ill_pip
) e(19);
288 if (osa
.sa_flags
!= (SA_RESETHAND
| SA_NODEFER
)) e(20);
290 /* Test sigprocmask(SIG_SETMASK, ...). */
291 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(18); /* block all */
292 if (sigemptyset(&s1
) != 0) e(19);
294 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(20); /* block none */
295 if (s1
!= s_nokill_stop
) e(21);
296 if (sigprocmask(SIG_SETMASK
, &s_ill
, &s1
) != 0) e(22); /* block SIGILL */
298 if (s1
!= s_empty
) e(23);
299 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(24); /* SIGILL+PIP */
300 if (s1
!= s_ill
) e(25);
301 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(26); /* block all */
302 if (s1
!= s_ill_pip
) e(27);
304 /* Test sigprocmask(SIG_UNBLOCK, ...) */
305 if (sigprocmask(SIG_UNBLOCK
, &s_ill
, &s1
) != 0) e(28);
306 if (s1
!= s_nokill_stop
) e(29);
307 if (sigprocmask(SIG_UNBLOCK
, &s_ill_pip
, &s1
) != 0) e(30);
309 if (sigdelset(&s
, SIGILL
) != 0) e(31);
311 if (sigprocmask(SIG_UNBLOCK
, &s_empty
, &s1
) != 0) e(33);
313 if (sigdelset(&s
, SIGILL
) != 0) e(34);
314 if (sigdelset(&s
, SIGPIPE
) != 0) e(35);
317 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(37);
320 /* Test sigprocmask(SIG_BLOCK, ...) */
321 if (sigprocmask(SIG_BLOCK
, &s_ill
, &s1
) != 0) e(39);
322 if (s1
!= s_empty
) e(40);
323 if (sigprocmask(SIG_BLOCK
, &s_ill_pip
, &s1
) != 0) e(41);
324 if (s1
!= s_ill
) e(42);
325 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(43);
326 if (s1
!= s_ill_pip
) e(44);
328 /* Check error condition. */
330 if (sigprocmask(20000, &s_full
, &s1
) != -1) e(45);
331 if (errno
!= EINVAL
) e(46);
332 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(47);
333 if (s1
!= s_nokill_stop
) e(48);
335 /* If second arg is 0, nothing is set. */
336 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(49);
337 if (s1
!= s_nokill_stop
) e(50);
338 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(51);
339 if (s1
!= s_nokill_stop
) e(52);
340 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(53);
341 if (s1
!= s_ill_pip
) e(54);
342 if (sigprocmask(SIG_BLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(55);
343 if (s1
!= s_ill_pip
) e(56);
344 if (sigprocmask(SIG_UNBLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(57);
345 if (s1
!= s_ill_pip
) e(58);
347 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
349 if (sigaddset(&s
, SIGKILL
) != 0) e(59);
350 if (sigprocmask(SIG_BLOCK
, &s
, &s1
) != 0) e(60);
351 if (s1
!= s_ill_pip
) e(61);
352 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(62);
353 if (s1
!= s_ill_pip
) e(63);
355 /* Test sigpending. At this moment, all signals are blocked. */
356 sa
.sa_handler
= func2
;
357 sa
.sa_mask
= s_empty
;
358 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(64);
360 kill(p
, SIGHUP
); /* send SIGHUP to self */
361 if (sigpending(&s
) != 0) e(65);
362 if (sigemptyset(&s1
) != 0) e(66);
363 if (sigaddset(&s1
, SIGHUP
) != 0) e(67);
365 sa
.sa_handler
= SIG_IGN
;
366 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(69);
367 if (sigpending(&s
) != 0) e(70);
368 if (s
!= s_empty
) e(71);
371 /*---------------------------------------------------------------------------*/
373 sigset_t glo_vol_set
;
384 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, (sigset_t
*) &glo_vol_set
) != 0)
388 /* Verify that signal(2), which is now built on top of sigaction(2), still
400 /* Verify an installed signal handler persists across a fork(2). */
401 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(1);
402 switch (pid
= fork()) {
407 exit(errct
== 0 ? 0 : 1);
408 case -1: e(3); break;
409 default: /* parent */
411 if (kill(pid
, SIGTERM
) != 0) e(4);
416 /* Verify that the return value is the previous handler. */
417 signal(SIGINT
, SIG_IGN
);
418 if (signal(SIGINT
, catch2
) != SIG_IGN
) e(5);
419 if (signal(SIGINT
, catch1
) != catch2
) e(6);
420 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(7);
421 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(8);
422 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(9);
423 if (signal(SIGINT
, SIG_DFL
) != SIG_DFL
) e(10);
424 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(11);
426 /* Verify that SIG_ERR is correctly generated. */
427 if (signal(_NSIG
, catch1
) != SIG_ERR
) e(12);
428 if (signal(0, catch1
) != SIG_ERR
) e(13);
429 if (signal(-1, SIG_DFL
) != SIG_ERR
) e(14);
431 /* Verify that caught signals are automatically reset to the default,
432 * and that further instances of the same signal are not blocked here
433 * or in the signal handler.
435 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(15);
436 switch ((pid
= fork())) {
441 if (sigismember((sigset_t
*) &glo_vol_set
, SIGTERM
)) e(17);
442 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &sigset_var
) != 0) e(18);
443 if (sigismember(&sigset_var
, SIGTERM
)) e(19);
446 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
447 if (signal(SIGTERM
, catch1
) != SIG_DFL
) e(20);
449 if (signal(SIGTERM
, catch1
) != catch1
) e(20);
451 exit(errct
== 0 ? 0 : 1);
452 default: /* parent */
454 if (kill(pid
, SIGTERM
) != 0) e(21);
457 case -1: e(22); break;
461 /*---------------------------------------------------------------------------*/
462 /* Test that the signal handler can be invoked recursively with the
463 * state being properly saved and restored.
472 if (z
== 1) { /* catching a nested signal */
477 if (kill(getpid(), SIGHUP
) != 0) e(1);
484 struct sigaction act
;
491 act
.sa_handler
= catch3
;
493 act
.sa_flags
= SA_NODEFER
; /* Otherwise, nested occurence of
494 * SIGINT is blocked. */
495 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) != 0) e(2);
496 if (kill(getpid(), SIGHUP
) != 0) e(3);
500 /*---------------------------------------------------------------------------*/
502 /* Test that the signal mask in effect for the duration of a signal handler
503 * is as specified in POSIX Section 3, lines 718 -724. Test that the
504 * previous signal mask is restored when the signal handler returns.
513 if (sigemptyset(&set
) == -1) e(5001);
514 if (sigaddset(&set
, SIGTERM
) == -1) e(5002);
515 if (sigaddset(&set
, SIGHUP
) == -1) e(5003);
516 if (sigaddset(&set
, SIGINT
) == -1) e(5004);
517 if (sigaddset(&set
, SIGPIPE
) == -1) e(5005);
518 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) != 0) e(5006);
519 if (oset
!= set
) e(5007);
524 struct sigaction act
, oact
;
530 act
.sa_handler
= catch4
;
531 sigemptyset(&act
.sa_mask
);
532 sigaddset(&act
.sa_mask
, SIGTERM
);
533 sigaddset(&act
.sa_mask
, SIGHUP
);
535 if (sigaction(SIGINT
, &act
, &oact
) == -1) e(2);
537 if (sigemptyset(&set
) == -1) e(3);
538 if (sigaddset(&set
, SIGPIPE
) == -1) e(4);
539 if (sigprocmask(SIG_SETMASK
, &set
, &oset
) == -1) e(5);
540 if (kill(getpid(), SIGINT
) == -1) e(6);
541 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) == -1) e(7);
542 if (sigemptyset(&set
) == -1) e(8);
543 if (sigaddset(&set
, SIGPIPE
) == -1) e(9);
544 if (set
!= oset
) e(10);
547 /*---------------------------------------------------------------------------*/
549 /* Test the basic functionality of sigsuspend(2). */
561 struct sigaction act
;
567 switch (pid
= fork()) {
571 if (kill(getppid(), SIGINT
) == -1) e(1);
572 exit(errct
== 0 ? 0 : 1);
573 case -1: e(2); break;
574 default: /* parent */
575 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
577 act
.sa_handler
= catch5
;
578 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
580 if (sigemptyset(&set
) == -1) e(5);
581 r
= sigsuspend(&set
);
583 if (r
!= -1 || errno
!= EINTR
|| x
!= 1) e(6);
589 /*----------------------------------------------------------------------*/
591 /* Test that sigsuspend() does block the signals specified in its
592 * argument, and after sigsuspend returns, the previous signal
595 * The child sends two signals to the parent SIGINT and then SIGPIPE,
596 * separated by a long delay. The parent executes sigsuspend() with
597 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
598 * will be invoked, then sigsuspend will return restoring the
599 * original signal mask, and then the SIGPIPE handler will be
603 void sigint_handler(signo
)
610 void sigpipe_handler(signo
)
621 struct sigaction act
;
629 switch (pid
= fork()) {
633 if (kill(getppid(), SIGINT
) == -1) e(1);
635 if (kill(getppid(), SIGPIPE
) == -1) e(2);
636 exit(errct
== 0 ? 0 : 1);
637 case -1: e(3); break;
638 default: /* parent */
639 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
641 act
.sa_handler
= sigint_handler
;
642 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
644 act
.sa_handler
= sigpipe_handler
;
645 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(5);
647 if (sigemptyset(&set
) == -1) e(6);
648 if (sigaddset(&set
, SIGINT
) == -1) e(7);
649 r
= sigsuspend(&set
);
651 if (errno
!= EINTR
) e(9);
659 /*--------------------------------------------------------------------------*/
661 /* Test that sigsuspend() does block the signals specified in its
662 * argument, and after sigsuspend returns, the previous signal
665 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
666 * and then SIGTERM, separated by a long delay. The parent executes
667 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
668 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
669 * will return restoring the original signal mask, and then the other
670 * two handlers will be invoked.
698 struct sigaction act
;
706 switch (pid
= fork()) {
710 if (kill(getppid(), SIGHUP
) == -1) e(1);
712 if (kill(getppid(), SIGPIPE
) == -1) e(2);
714 if (kill(getppid(), SIGTERM
) == -1) e(3);
715 exit(errct
== 0 ? 0 : 1);
716 case -1: e(5); break;
717 default: /* parent */
718 if (sigemptyset(&act
.sa_mask
) == -1) e(6);
720 act
.sa_handler
= sighup8
;
721 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(7);
723 act
.sa_handler
= sigpip8
;
724 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(8);
726 act
.sa_handler
= sigter8
;
727 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(9);
729 if (sigemptyset(&set
) == -1) e(10);
730 if (sigaddset(&set
, SIGHUP
) == -1) e(11);
731 if (sigaddset(&set
, SIGPIPE
) == -1) e(12);
732 r
= sigsuspend(&set
);
734 if (errno
!= EINTR
) e(14);
742 /*--------------------------------------------------------------------------*/
744 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
745 * and SIGTERM, unblock these signals with sigprocmask, and verify
746 * that these signals are delivered.
764 struct sigaction act
;
771 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
774 act
.sa_handler
= sighup9
;
775 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
777 act
.sa_handler
= sigter9
;
778 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
780 if (sigemptyset(&set
) == -1) e(4);
781 if (sigaddset(&set
, SIGTERM
) == -1) e(5);
782 if (sigaddset(&set
, SIGHUP
) == -1) e(6);
783 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(7);
785 if (kill(getpid(), SIGHUP
) == -1) e(8);
786 if (kill(getpid(), SIGTERM
) == -1) e(9);
790 if (sigemptyset(&set
) == -1) e(12);
791 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(12);
796 /*---------------------------------------------------------------------------*/
798 /* Block SIGINT and then send this signal to ourself.
800 * Install signal handlers for SIGALRM and SIGINT.
802 * Set an alarm for 6 seconds, then sleep for 7.
804 * The SIGALRM should interrupt the sleep, but the SIGINT
805 * should remain pending.
814 void sigalrm_handler10(signo
)
823 struct sigaction act
;
830 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
833 act
.sa_handler
= sighup10
;
834 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
836 act
.sa_handler
= sigalrm_handler10
;
837 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
839 if (sigemptyset(&set
) == -1) e(4);
840 if (sigaddset(&set
, SIGHUP
) == -1) e(5);
841 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(6);
843 if (kill(getpid(), SIGHUP
) == -1) e(7);
844 if (sigpending(&set
) == -1) e(8);
845 if (sigemptyset(&set2
) == -1) e(9);
846 if (sigaddset(&set2
, SIGHUP
) == -1) e(10);
847 if (set2
!= set
) e(11);
850 if (sigpending(&set
) == -1) e(12);
851 if (set
!= set2
) e(13);
856 /*--------------------------------------------------------------------------*/
867 /*---------------------------------------------------------------------------*/
869 /* Basic test for setjmp/longjmp. This includes testing that the
870 * signal mask is properly restored.
873 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
877 sigset_t ss, ssexp; \
879 subtest = _subtest; \
883 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
885 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
886 ssexp = _save ? 0x32 : 0x3abc; \
887 if ((ss ^ ssexp) & ~(1 << SIGKILL)) e(388); \
891 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
895 TEST_SETJMP(test37m
, 13, jmp_buf, setjmp(jb
), longjmp(jb
, 1), 1)
896 TEST_SETJMP(test37p
, 16, sigjmp_buf
, sigsetjmp(jb
, 0), siglongjmp(jb
, 1), 0)
897 TEST_SETJMP(test37q
, 17, sigjmp_buf
, sigsetjmp(jb
, 1), siglongjmp(jb
, 1), 1)
904 /*--------------------------------------------------------------------------*/
906 /* Test for setjmp/longjmp.
908 * Catch a signal. While in signal handler do setjmp/longjmp.
911 void catch14(signo
, code
, scp
)
914 struct sigcontext
*scp
;
930 struct sigaction act
;
931 typedef void(*sighandler_t
) (int sig
);
940 act
.sa_handler
= (sighandler_t
) catch14
; /* fudge */
941 if (sigaction(SIGSEGV
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
942 if (kill(getpid(), SIGSEGV
) == -1) e(4);
948 /*---------------------------------------------------------------------------*/
950 /* Test for setjmp/longjmp.
952 * Catch a signal. Longjmp out of signal handler.
967 struct sigaction act
;
976 act
.sa_handler
= catch15
;
977 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
979 if ((k
= setjmp(glo_jb
))) {
984 if (kill(getpid(), SIGALRM
) == -1) e(5);
992 /* Clear the signal state. */
993 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_IGN
);
994 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_DFL
);
995 sigfillset(&sigset_var
);
996 sigprocmask(SIG_UNBLOCK
, &sigset_var
, (sigset_t
*)NULL
);
1002 /* Expect exactly one child, and that it exits with 0. */
1013 if (status
!= 0) e(90);