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 static 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.
540 if (sigemptyset(&set
) == -1) e(5001);
541 if (sigaddset(&set
, SIGTERM
) == -1) e(5002);
542 if (sigaddset(&set
, SIGHUP
) == -1) e(5003);
543 if (sigaddset(&set
, SIGINT
) == -1) e(5004);
544 if (sigaddset(&set
, SIGPIPE
) == -1) e(5005);
545 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) != 0) e(5006);
546 if (sigmemcmp(&oset
, &set
, sizeof(set
))) e(5007);
551 struct sigaction act
, oact
;
557 act
.sa_handler
= catch4
;
558 sigemptyset(&act
.sa_mask
);
559 sigaddset(&act
.sa_mask
, SIGTERM
);
560 sigaddset(&act
.sa_mask
, SIGHUP
);
562 if (sigaction(SIGINT
, &act
, &oact
) == -1) e(2);
564 if (sigemptyset(&set
) == -1) e(3);
565 if (sigaddset(&set
, SIGPIPE
) == -1) e(4);
566 if (sigprocmask(SIG_SETMASK
, &set
, &oset
) == -1) e(5);
567 if (kill(getpid(), SIGINT
) == -1) e(6);
568 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) == -1) e(7);
569 if (sigemptyset(&set
) == -1) e(8);
570 if (sigaddset(&set
, SIGPIPE
) == -1) e(9);
571 if (sigmemcmp(&set
, &oset
, sizeof(set
))) e(10);
574 /*---------------------------------------------------------------------------*/
576 /* Test the basic functionality of sigsuspend(2). */
588 struct sigaction act
;
594 switch (pid
= fork()) {
598 if (kill(getppid(), SIGINT
) == -1) e(1);
599 exit(errct
== 0 ? 0 : 1);
600 case -1: e(2); break;
601 default: /* parent */
602 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
604 act
.sa_handler
= catch5
;
605 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
607 if (sigemptyset(&set
) == -1) e(5);
608 r
= sigsuspend(&set
);
610 if (r
!= -1 || errno
!= EINTR
|| x
!= 1) e(6);
616 /*----------------------------------------------------------------------*/
618 /* Test that sigsuspend() does block the signals specified in its
619 * argument, and after sigsuspend returns, the previous signal
622 * The child sends two signals to the parent SIGINT and then SIGPIPE,
623 * separated by a long delay. The parent executes sigsuspend() with
624 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
625 * will be invoked, then sigsuspend will return restoring the
626 * original signal mask, and then the SIGPIPE handler will be
630 void sigint_handler(signo
)
637 void sigpipe_handler(signo
)
648 struct sigaction act
;
656 switch (pid
= fork()) {
660 if (kill(getppid(), SIGINT
) == -1) e(1);
662 if (kill(getppid(), SIGPIPE
) == -1) e(2);
663 exit(errct
== 0 ? 0 : 1);
664 case -1: e(3); break;
665 default: /* parent */
666 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
668 act
.sa_handler
= sigint_handler
;
669 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
671 act
.sa_handler
= sigpipe_handler
;
672 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(5);
674 if (sigemptyset(&set
) == -1) e(6);
675 if (sigaddset(&set
, SIGINT
) == -1) e(7);
676 r
= sigsuspend(&set
);
678 if (errno
!= EINTR
) e(9);
686 /*--------------------------------------------------------------------------*/
688 /* Test that sigsuspend() does block the signals specified in its
689 * argument, and after sigsuspend returns, the previous signal
692 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
693 * and then SIGTERM, separated by a long delay. The parent executes
694 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
695 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
696 * will return restoring the original signal mask, and then the other
697 * two handlers will be invoked.
725 struct sigaction act
;
733 switch (pid
= fork()) {
737 if (kill(getppid(), SIGHUP
) == -1) e(1);
739 if (kill(getppid(), SIGPIPE
) == -1) e(2);
741 if (kill(getppid(), SIGTERM
) == -1) e(3);
742 exit(errct
== 0 ? 0 : 1);
743 case -1: e(5); break;
744 default: /* parent */
745 if (sigemptyset(&act
.sa_mask
) == -1) e(6);
747 act
.sa_handler
= sighup8
;
748 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(7);
750 act
.sa_handler
= sigpip8
;
751 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(8);
753 act
.sa_handler
= sigter8
;
754 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(9);
756 if (sigemptyset(&set
) == -1) e(10);
757 if (sigaddset(&set
, SIGHUP
) == -1) e(11);
758 if (sigaddset(&set
, SIGPIPE
) == -1) e(12);
759 r
= sigsuspend(&set
);
761 if (errno
!= EINTR
) e(14);
769 /*--------------------------------------------------------------------------*/
771 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
772 * and SIGTERM, unblock these signals with sigprocmask, and verify
773 * that these signals are delivered.
791 struct sigaction act
;
798 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
801 act
.sa_handler
= sighup9
;
802 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
804 act
.sa_handler
= sigter9
;
805 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
807 if (sigemptyset(&set
) == -1) e(4);
808 if (sigaddset(&set
, SIGTERM
) == -1) e(5);
809 if (sigaddset(&set
, SIGHUP
) == -1) e(6);
810 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(7);
812 if (kill(getpid(), SIGHUP
) == -1) e(8);
813 if (kill(getpid(), SIGTERM
) == -1) e(9);
817 if (sigemptyset(&set
) == -1) e(12);
818 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(12);
823 /*---------------------------------------------------------------------------*/
825 /* Block SIGINT and then send this signal to ourself.
827 * Install signal handlers for SIGALRM and SIGINT.
829 * Set an alarm for 6 seconds, then sleep for 7.
831 * The SIGALRM should interrupt the sleep, but the SIGINT
832 * should remain pending.
841 void sigalrm_handler10(signo
)
850 struct sigaction act
;
857 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
860 act
.sa_handler
= sighup10
;
861 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
863 act
.sa_handler
= sigalrm_handler10
;
864 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
866 if (sigemptyset(&set
) == -1) e(4);
867 if (sigaddset(&set
, SIGHUP
) == -1) e(5);
868 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(6);
870 if (kill(getpid(), SIGHUP
) == -1) e(7);
871 if (sigpending(&set
) == -1) e(8);
872 if (sigemptyset(&set2
) == -1) e(9);
873 if (sigaddset(&set2
, SIGHUP
) == -1) e(10);
874 if (sigmemcmp(&set2
, &set
, sizeof(set
))) e(11);
877 if (sigpending(&set
) == -1) e(12);
878 if (sigmemcmp(&set
, &set2
, sizeof(set
))) e(13);
883 /*--------------------------------------------------------------------------*/
894 /*---------------------------------------------------------------------------*/
896 /* Basic test for setjmp/longjmp. This includes testing that the
897 * signal mask is properly restored.
900 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
904 sigset_t ss, ss2, ss_orig; \
906 subtest = _subtest; \
909 sigemptyset(&ss); sigemptyset(&ss2); \
910 sigaddset(&ss, 2); sigaddset(&ss, 4); sigaddset(&ss, 5); \
911 sigaddset(&ss2, 20); sigaddset(&ss2, 22); sigaddset(&ss2, 65); \
912 memcpy(&ss_orig, &ss, sizeof(ss)); \
913 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
916 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
917 ssexp = _save ? ss_orig : ss2; \
918 sigdelset(&ssexp, SIGKILL); \
919 if (sigmemcmp(&ss, &ssexp, sizeof(ss))) e(388); \
923 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
927 TEST_SETJMP(test37m
, 13, jmp_buf, setjmp(jb
), longjmp(jb
, 1), 1)
928 TEST_SETJMP(test37p
, 16, sigjmp_buf
, sigsetjmp(jb
, 0), siglongjmp(jb
, 1), 0)
929 TEST_SETJMP(test37q
, 17, sigjmp_buf
, sigsetjmp(jb
, 1), siglongjmp(jb
, 1), 1)
936 /*--------------------------------------------------------------------------*/
938 /* Test for setjmp/longjmp.
940 * Catch a signal. While in signal handler do setjmp/longjmp.
943 void catch14(signo
, code
, scp
)
946 struct sigcontext
*scp
;
962 struct sigaction act
;
963 typedef void(*sighandler_t
) (int sig
);
971 sigemptyset(&act
.sa_mask
);
972 act
.sa_handler
= (sighandler_t
) catch14
; /* fudge */
973 if (sigaction(SIGSEGV
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
974 if (kill(getpid(), SIGSEGV
) == -1) e(4);
980 /*---------------------------------------------------------------------------*/
982 /* Test for setjmp/longjmp.
984 * Catch a signal. Longjmp out of signal handler.
999 struct sigaction act
;
1007 sigemptyset(&act
.sa_mask
);
1008 act
.sa_handler
= catch15
;
1009 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
1011 if ((k
= setjmp(glo_jb
))) {
1016 if (kill(getpid(), SIGALRM
) == -1) e(5);
1019 void clearsigstate()
1022 sigset_t sigset_var
;
1024 /* Clear the signal state. */
1025 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_IGN
);
1026 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_DFL
);
1027 sigfillset(&sigset_var
);
1028 sigprocmask(SIG_UNBLOCK
, &sigset_var
, (sigset_t
*)NULL
);
1034 /* Expect exactly one child, and that it exits with 0. */
1045 if (status
!= 0) e(90);