1 /* test 37 - signals */
6 #include <sys/sigcontext.h>
20 int iteration
, cumsig
, subtest
, errct
= 0, 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 _PROTOTYPE(int main
, (int argc
, char *argv
[]));
28 _PROTOTYPE(void test37a
, (void));
29 _PROTOTYPE(void func1
, (int sig
));
30 _PROTOTYPE(void func2
, (int sig
));
31 _PROTOTYPE(void test37b
, (void));
32 _PROTOTYPE(void catch1
, (int signo
));
33 _PROTOTYPE(void catch2
, (int signo
));
34 _PROTOTYPE(void test37c
, (void));
35 _PROTOTYPE(void catch3
, (int signo
));
36 _PROTOTYPE(void test37d
, (void));
37 _PROTOTYPE(void catch4
, (int signo
));
38 _PROTOTYPE(void test37e
, (void));
39 _PROTOTYPE(void catch5
, (int signo
));
40 _PROTOTYPE(void test37f
, (void));
41 _PROTOTYPE(void sigint_handler
, (int signo
));
42 _PROTOTYPE(void sigpipe_handler
, (int signo
));
43 _PROTOTYPE(void test37g
, (void));
44 _PROTOTYPE(void sighup8
, (int signo
));
45 _PROTOTYPE(void sigpip8
, (int signo
));
46 _PROTOTYPE(void sigter8
, (int signo
));
47 _PROTOTYPE(void test37h
, (void));
48 _PROTOTYPE(void sighup9
, (int signo
));
49 _PROTOTYPE(void sigter9
, (int signo
));
50 _PROTOTYPE(void test37i
, (void));
51 _PROTOTYPE(void sighup10
, (int signo
));
52 _PROTOTYPE(void sigalrm_handler10
, (int signo
));
53 _PROTOTYPE(void test37j
, (void));
54 _PROTOTYPE(void test37k
, (void));
55 _PROTOTYPE(void test37l
, (void));
56 _PROTOTYPE(void func_m1
, (void));
57 _PROTOTYPE(void func_m2
, (void));
58 _PROTOTYPE(void test37m
, (void));
59 _PROTOTYPE(void test37p
, (void));
60 _PROTOTYPE(void test37q
, (void));
61 _PROTOTYPE(void longjerr
, (void));
62 _PROTOTYPE(void catch14
, (int signo
, int code
, struct sigcontext
* scp
));
63 _PROTOTYPE(void test37n
, (void));
64 _PROTOTYPE(void catch15
, (int signo
));
65 _PROTOTYPE(void test37o
, (void));
66 _PROTOTYPE(void clearsigstate
, (void));
67 _PROTOTYPE(void quit
, (void));
68 _PROTOTYPE(void wait_for
, (int pid
));
69 _PROTOTYPE(void e
, (int n
));
79 if (argc
== 2) m
= atoi(argv
[1]);
82 fflush(stdout
); /* have to flush for child's benefit */
84 system("rm -rf DIR_37; mkdir DIR_37");
87 for (i
= 0; i
< ITERATIONS
; i
++) {
89 if (m
& 0000001) test37a();
90 if (m
& 0000002) test37b();
91 if (m
& 0000004) test37c();
92 if (m
& 0000010) test37d();
93 if (m
& 0000020) test37e();
94 if (m
& 0000040) test37f();
95 if (m
& 0000100) test37g();
96 if (m
& 0000200) test37h();
97 if (m
& 0000400) test37i();
98 if (m
& 0001000) test37j();
99 if (m
& 0002000) test37k();
100 if (m
& 0004000) test37l();
101 if (m
& 0010000) test37m();
102 if (m
& 0020000) test37n();
103 if (m
& 0040000) test37o();
104 if (m
& 0100000) test37p();
105 if (m
& 0200000) test37q();
109 return(-1); /* impossible */
114 /* Test signal set management. */
121 /* Create an empty set and see if any bits are on. */
122 if (sigemptyset(&s
) != 0) e(1);
123 if (sigismember(&s
, SIGHUP
) != 0) e(2);
124 if (sigismember(&s
, SIGINT
) != 0) e(3);
125 if (sigismember(&s
, SIGQUIT
) != 0) e(4);
126 if (sigismember(&s
, SIGILL
) != 0) e(5);
127 if (sigismember(&s
, SIGTRAP
) != 0) e(6);
128 if (sigismember(&s
, SIGABRT
) != 0) e(7);
129 if (sigismember(&s
, SIGIOT
) != 0) e(8);
130 if (sigismember(&s
, SIGFPE
) != 0) e(10);
131 if (sigismember(&s
, SIGKILL
) != 0) e(11);
132 if (sigismember(&s
, SIGUSR1
) != 0) e(12);
133 if (sigismember(&s
, SIGSEGV
) != 0) e(13);
134 if (sigismember(&s
, SIGUSR2
) != 0) e(14);
135 if (sigismember(&s
, SIGPIPE
) != 0) e(15);
136 if (sigismember(&s
, SIGALRM
) != 0) e(16);
137 if (sigismember(&s
, SIGTERM
) != 0) e(17);
139 /* Create a full set and see if any bits are off. */
140 if (sigfillset(&s
) != 0) e(19);
141 if (sigemptyset(&s
) != 0) e(20);
142 if (sigfillset(&s
) != 0) e(21);
143 if (sigismember(&s
, SIGHUP
) != 1) e(22);
144 if (sigismember(&s
, SIGINT
) != 1) e(23);
145 if (sigismember(&s
, SIGQUIT
) != 1) e(24);
146 if (sigismember(&s
, SIGILL
) != 1) e(25);
147 if (sigismember(&s
, SIGTRAP
) != 1) e(26);
148 if (sigismember(&s
, SIGABRT
) != 1) e(27);
149 if (sigismember(&s
, SIGIOT
) != 1) e(28);
150 if (sigismember(&s
, SIGFPE
) != 1) e(30);
151 if (sigismember(&s
, SIGKILL
) != 1) e(31);
152 if (sigismember(&s
, SIGUSR1
) != 1) e(32);
153 if (sigismember(&s
, SIGSEGV
) != 1) e(33);
154 if (sigismember(&s
, SIGUSR2
) != 1) e(34);
155 if (sigismember(&s
, SIGPIPE
) != 1) e(35);
156 if (sigismember(&s
, SIGALRM
) != 1) e(36);
157 if (sigismember(&s
, SIGTERM
) != 1) e(37);
159 /* Create an empty set, then turn on bits individually. */
160 if (sigemptyset(&s
) != 0) e(39);
161 if (sigaddset(&s
, SIGHUP
) != 0) e(40);
162 if (sigaddset(&s
, SIGINT
) != 0) e(41);
163 if (sigaddset(&s
, SIGQUIT
) != 0) e(42);
164 if (sigaddset(&s
, SIGILL
) != 0) e(43);
165 if (sigaddset(&s
, SIGTRAP
) != 0) e(44);
167 /* See if the bits just turned on are indeed on. */
168 if (sigismember(&s
, SIGHUP
) != 1) e(45);
169 if (sigismember(&s
, SIGINT
) != 1) e(46);
170 if (sigismember(&s
, SIGQUIT
) != 1) e(47);
171 if (sigismember(&s
, SIGILL
) != 1) e(48);
172 if (sigismember(&s
, SIGTRAP
) != 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);
193 if (sigismember(&s
, SIGHUP
) != 0) e(67);
194 if (sigismember(&s
, SIGINT
) != 0) e(68);
195 if (sigismember(&s
, SIGQUIT
) != 0) e(69);
196 if (sigismember(&s
, SIGILL
) != 0) e(70);
197 if (sigismember(&s
, SIGTRAP
) != 0) e(71);
198 if (sigismember(&s
, SIGABRT
) != 0) e(72);
199 if (sigismember(&s
, SIGIOT
) != 0) e(73);
200 if (sigismember(&s
, SIGFPE
) != 0) e(75);
201 if (sigismember(&s
, SIGKILL
) != 0) e(76);
202 if (sigismember(&s
, SIGUSR1
) != 0) e(77);
203 if (sigismember(&s
, SIGSEGV
) != 0) e(78);
204 if (sigismember(&s
, SIGUSR2
) != 0) e(79);
205 if (sigismember(&s
, SIGPIPE
) != 0) e(80);
206 if (sigismember(&s
, SIGALRM
) != 0) e(81);
207 if (sigismember(&s
, SIGTERM
) != 0) e(82);
224 /* Test sigprocmask and sigpending. */
227 sigset_t s
, s1
, s_empty
, s_full
, s_ill
, s_ill_pip
, s_nokill
, s_nokill_stop
;
228 struct sigaction sa
, osa
;
233 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
234 if (sigemptyset(&s_empty
) != 0) e(1);
235 if (sigemptyset(&s_ill
) != 0) e(2);
236 if (sigemptyset(&s_ill_pip
) != 0) e(3);
237 if (sigaddset(&s_ill
, SIGILL
) != 0) e(4);
238 if (sigaddset(&s_ill_pip
, SIGILL
) != 0) e(5);
239 if (sigaddset(&s_ill_pip
, SIGPIPE
) != 0) e(6);
240 if (sigfillset(&s_full
) != 0) e(7);
242 if (sigdelset(&s_nokill
, SIGKILL
) != 0) e(8);
243 s_nokill_stop
= s_nokill
;
244 if (sigdelset(&s_nokill_stop
, SIGSTOP
) != 0) e(8);
245 if (SIGSTOP
>= _NSIG
) e(666);
246 if (SIGSTOP
< _NSIG
&& sigdelset(&s_nokill
, SIGSTOP
) != 0) e(888);
248 /* Now get most of the signals into default state. Don't change SIGINT
249 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
251 sa
.sa_handler
= SIG_DFL
;
252 sa
.sa_mask
= s_empty
;
254 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, &sa
, &osa
);
256 /* The second argument may be zero. See if it wipes out the system. */
257 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, (struct sigaction
*) NULL
, &osa
);
259 /* Install a signal handler. */
260 sa
.sa_handler
= func1
;
262 sa
.sa_flags
= SA_NODEFER
| SA_NOCLDSTOP
;
263 osa
.sa_handler
= SIG_IGN
;
264 osa
.sa_mask
= s_empty
;
266 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(9);
267 if (osa
.sa_handler
!= SIG_DFL
) e(10);
268 if (osa
.sa_mask
!= 0) e(11);
269 if (osa
.sa_flags
!= s_empty
) e(12);
271 /* Replace action and see if old value is read back correctly. */
272 sa
.sa_handler
= func2
;
273 sa
.sa_mask
= s_ill_pip
;
274 sa
.sa_flags
= SA_RESETHAND
| SA_NODEFER
;
275 osa
.sa_handler
= SIG_IGN
;
276 osa
.sa_mask
= s_empty
;
278 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(13);
279 if (osa
.sa_handler
!= func1
) e(14);
280 if (osa
.sa_mask
!= s_ill
) e(15);
281 if (osa
.sa_flags
!= SA_NODEFER
282 && osa
.sa_flags
!= (SA_NODEFER
| SA_NOCLDSTOP
)) e(16);
284 /* Replace action once more and check what is read back. */
285 sa
.sa_handler
= SIG_DFL
;
286 sa
.sa_mask
= s_empty
;
287 osa
.sa_handler
= SIG_IGN
;
288 osa
.sa_mask
= s_empty
;
290 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(17);
291 if (osa
.sa_handler
!= func2
) e(18);
292 if (osa
.sa_mask
!= s_ill_pip
) e(19);
293 if (osa
.sa_flags
!= (SA_RESETHAND
| SA_NODEFER
)) e(20);
295 /* Test sigprocmask(SIG_SETMASK, ...). */
296 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(18); /* block all */
297 if (sigemptyset(&s1
) != 0) e(19);
299 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(20); /* block none */
300 if (s1
!= s_nokill_stop
) e(21);
301 if (sigprocmask(SIG_SETMASK
, &s_ill
, &s1
) != 0) e(22); /* block SIGILL */
303 if (s1
!= s_empty
) e(23);
304 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(24); /* SIGILL+PIP */
305 if (s1
!= s_ill
) e(25);
306 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(26); /* block all */
307 if (s1
!= s_ill_pip
) e(27);
309 /* Test sigprocmask(SIG_UNBLOCK, ...) */
310 if (sigprocmask(SIG_UNBLOCK
, &s_ill
, &s1
) != 0) e(28);
311 if (s1
!= s_nokill_stop
) e(29);
312 if (sigprocmask(SIG_UNBLOCK
, &s_ill_pip
, &s1
) != 0) e(30);
314 if (sigdelset(&s
, SIGILL
) != 0) e(31);
316 if (sigprocmask(SIG_UNBLOCK
, &s_empty
, &s1
) != 0) e(33);
318 if (sigdelset(&s
, SIGILL
) != 0) e(34);
319 if (sigdelset(&s
, SIGPIPE
) != 0) e(35);
322 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(37);
325 /* Test sigprocmask(SIG_BLOCK, ...) */
326 if (sigprocmask(SIG_BLOCK
, &s_ill
, &s1
) != 0) e(39);
327 if (s1
!= s_empty
) e(40);
328 if (sigprocmask(SIG_BLOCK
, &s_ill_pip
, &s1
) != 0) e(41);
329 if (s1
!= s_ill
) e(42);
330 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(43);
331 if (s1
!= s_ill_pip
) e(44);
333 /* Check error condition. */
335 if (sigprocmask(20000, &s_full
, &s1
) != -1) e(45);
336 if (errno
!= EINVAL
) e(46);
337 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(47);
338 if (s1
!= s_nokill_stop
) e(48);
340 /* If second arg is 0, nothing is set. */
341 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(49);
342 if (s1
!= s_nokill_stop
) e(50);
343 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(51);
344 if (s1
!= s_nokill_stop
) e(52);
345 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(53);
346 if (s1
!= s_ill_pip
) e(54);
347 if (sigprocmask(SIG_BLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(55);
348 if (s1
!= s_ill_pip
) e(56);
349 if (sigprocmask(SIG_UNBLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(57);
350 if (s1
!= s_ill_pip
) e(58);
352 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
354 if (sigaddset(&s
, SIGKILL
) != 0) e(59);
355 if (sigprocmask(SIG_BLOCK
, &s
, &s1
) != 0) e(60);
356 if (s1
!= s_ill_pip
) e(61);
357 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(62);
358 if (s1
!= s_ill_pip
) e(63);
360 /* Test sigpending. At this moment, all signals are blocked. */
361 sa
.sa_handler
= func2
;
362 sa
.sa_mask
= s_empty
;
363 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(64);
365 kill(p
, SIGHUP
); /* send SIGHUP to self */
366 if (sigpending(&s
) != 0) e(65);
367 if (sigemptyset(&s1
) != 0) e(66);
368 if (sigaddset(&s1
, SIGHUP
) != 0) e(67);
370 sa
.sa_handler
= SIG_IGN
;
371 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(69);
372 if (sigpending(&s
) != 0) e(70);
373 if (s
!= s_empty
) e(71);
376 /*---------------------------------------------------------------------------*/
378 sigset_t glo_vol_set
;
389 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, (sigset_t
*) &glo_vol_set
) != 0)
393 /* Verify that signal(2), which is now built on top of sigaction(2), still
405 /* Verify an installed signal handler persists across a fork(2). */
406 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(1);
407 switch (pid
= fork()) {
412 exit(errct
== 0 ? 0 : 1);
413 case -1: e(3); break;
414 default: /* parent */
416 if (kill(pid
, SIGTERM
) != 0) e(4);
421 /* Verify that the return value is the previous handler. */
422 signal(SIGINT
, SIG_IGN
);
423 if (signal(SIGINT
, catch2
) != SIG_IGN
) e(5);
424 if (signal(SIGINT
, catch1
) != catch2
) e(6);
425 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(7);
426 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(8);
427 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(9);
428 if (signal(SIGINT
, SIG_DFL
) != SIG_DFL
) e(10);
429 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(11);
431 /* Verify that SIG_ERR is correctly generated. */
432 if (signal(_NSIG
, catch1
) != SIG_ERR
) e(12);
433 if (signal(0, catch1
) != SIG_ERR
) e(13);
434 if (signal(-1, SIG_DFL
) != SIG_ERR
) e(14);
436 /* Verify that caught signals are automatically reset to the default,
437 * and that further instances of the same signal are not blocked here
438 * or in the signal handler.
440 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(15);
441 switch ((pid
= fork())) {
446 if (sigismember((sigset_t
*) &glo_vol_set
, SIGTERM
)) e(17);
447 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &sigset_var
) != 0) e(18);
448 if (sigismember(&sigset_var
, SIGTERM
)) e(19);
451 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
452 if (signal(SIGTERM
, catch1
) != SIG_DFL
) e(20);
454 if (signal(SIGTERM
, catch1
) != catch1
) e(20);
456 exit(errct
== 0 ? 0 : 1);
457 default: /* parent */
459 if (kill(pid
, SIGTERM
) != 0) e(21);
462 case -1: e(22); break;
466 /*---------------------------------------------------------------------------*/
467 /* Test that the signal handler can be invoked recursively with the
468 * state being properly saved and restored.
477 if (z
== 1) { /* catching a nested signal */
482 if (kill(getpid(), SIGHUP
) != 0) e(1);
489 struct sigaction act
;
496 act
.sa_handler
= catch3
;
498 act
.sa_flags
= SA_NODEFER
; /* Otherwise, nested occurence of
499 * SIGINT is blocked. */
500 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) != 0) e(2);
501 if (kill(getpid(), SIGHUP
) != 0) e(3);
505 /*---------------------------------------------------------------------------*/
507 /* Test that the signal mask in effect for the duration of a signal handler
508 * is as specified in POSIX Section 3, lines 718 -724. Test that the
509 * previous signal mask is restored when the signal handler returns.
518 if (sigemptyset(&set
) == -1) e(5001);
519 if (sigaddset(&set
, SIGTERM
) == -1) e(5002);
520 if (sigaddset(&set
, SIGHUP
) == -1) e(5003);
521 if (sigaddset(&set
, SIGINT
) == -1) e(5004);
522 if (sigaddset(&set
, SIGPIPE
) == -1) e(5005);
523 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) != 0) e(5006);
524 if (oset
!= set
) e(5007);
529 struct sigaction act
, oact
;
535 act
.sa_handler
= catch4
;
536 sigemptyset(&act
.sa_mask
);
537 sigaddset(&act
.sa_mask
, SIGTERM
);
538 sigaddset(&act
.sa_mask
, SIGHUP
);
540 if (sigaction(SIGINT
, &act
, &oact
) == -1) e(2);
542 if (sigemptyset(&set
) == -1) e(3);
543 if (sigaddset(&set
, SIGPIPE
) == -1) e(4);
544 if (sigprocmask(SIG_SETMASK
, &set
, &oset
) == -1) e(5);
545 if (kill(getpid(), SIGINT
) == -1) e(6);
546 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) == -1) e(7);
547 if (sigemptyset(&set
) == -1) e(8);
548 if (sigaddset(&set
, SIGPIPE
) == -1) e(9);
549 if (set
!= oset
) e(10);
552 /*---------------------------------------------------------------------------*/
554 /* Test the basic functionality of sigsuspend(2). */
566 struct sigaction act
;
572 switch (pid
= fork()) {
576 if (kill(getppid(), SIGINT
) == -1) e(1);
577 exit(errct
== 0 ? 0 : 1);
578 case -1: e(2); break;
579 default: /* parent */
580 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
582 act
.sa_handler
= catch5
;
583 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
585 if (sigemptyset(&set
) == -1) e(5);
586 r
= sigsuspend(&set
);
588 if (r
!= -1 || errno
!= EINTR
|| x
!= 1) e(6);
594 /*----------------------------------------------------------------------*/
596 /* Test that sigsuspend() does block the signals specified in its
597 * argument, and after sigsuspend returns, the previous signal
600 * The child sends two signals to the parent SIGINT and then SIGPIPE,
601 * separated by a long delay. The parent executes sigsuspend() with
602 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
603 * will be invoked, then sigsuspend will return restoring the
604 * original signal mask, and then the SIGPIPE handler will be
608 void sigint_handler(signo
)
615 void sigpipe_handler(signo
)
626 struct sigaction act
;
634 switch (pid
= fork()) {
638 if (kill(getppid(), SIGINT
) == -1) e(1);
640 if (kill(getppid(), SIGPIPE
) == -1) e(2);
641 exit(errct
== 0 ? 0 : 1);
642 case -1: e(3); break;
643 default: /* parent */
644 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
646 act
.sa_handler
= sigint_handler
;
647 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
649 act
.sa_handler
= sigpipe_handler
;
650 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(5);
652 if (sigemptyset(&set
) == -1) e(6);
653 if (sigaddset(&set
, SIGINT
) == -1) e(7);
654 r
= sigsuspend(&set
);
656 if (errno
!= EINTR
) e(9);
664 /*--------------------------------------------------------------------------*/
666 /* Test that sigsuspend() does block the signals specified in its
667 * argument, and after sigsuspend returns, the previous signal
670 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
671 * and then SIGTERM, separated by a long delay. The parent executes
672 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
673 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
674 * will return restoring the original signal mask, and then the other
675 * two handlers will be invoked.
703 struct sigaction act
;
711 switch (pid
= fork()) {
715 if (kill(getppid(), SIGHUP
) == -1) e(1);
717 if (kill(getppid(), SIGPIPE
) == -1) e(2);
719 if (kill(getppid(), SIGTERM
) == -1) e(3);
720 exit(errct
== 0 ? 0 : 1);
721 case -1: e(5); break;
722 default: /* parent */
723 if (sigemptyset(&act
.sa_mask
) == -1) e(6);
725 act
.sa_handler
= sighup8
;
726 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(7);
728 act
.sa_handler
= sigpip8
;
729 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(8);
731 act
.sa_handler
= sigter8
;
732 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(9);
734 if (sigemptyset(&set
) == -1) e(10);
735 if (sigaddset(&set
, SIGHUP
) == -1) e(11);
736 if (sigaddset(&set
, SIGPIPE
) == -1) e(12);
737 r
= sigsuspend(&set
);
739 if (errno
!= EINTR
) e(14);
747 /*--------------------------------------------------------------------------*/
749 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
750 * and SIGTERM, unblock these signals with sigprocmask, and verify
751 * that these signals are delivered.
769 struct sigaction act
;
776 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
779 act
.sa_handler
= sighup9
;
780 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
782 act
.sa_handler
= sigter9
;
783 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
785 if (sigemptyset(&set
) == -1) e(4);
786 if (sigaddset(&set
, SIGTERM
) == -1) e(5);
787 if (sigaddset(&set
, SIGHUP
) == -1) e(6);
788 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(7);
790 if (kill(getpid(), SIGHUP
) == -1) e(8);
791 if (kill(getpid(), SIGTERM
) == -1) e(9);
795 if (sigemptyset(&set
) == -1) e(12);
796 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(12);
801 /*---------------------------------------------------------------------------*/
803 /* Block SIGINT and then send this signal to ourself.
805 * Install signal handlers for SIGALRM and SIGINT.
807 * Set an alarm for 6 seconds, then sleep for 7.
809 * The SIGALRM should interrupt the sleep, but the SIGINT
810 * should remain pending.
819 void sigalrm_handler10(signo
)
828 struct sigaction act
;
835 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
838 act
.sa_handler
= sighup10
;
839 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
841 act
.sa_handler
= sigalrm_handler10
;
842 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
844 if (sigemptyset(&set
) == -1) e(4);
845 if (sigaddset(&set
, SIGHUP
) == -1) e(5);
846 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(6);
848 if (kill(getpid(), SIGHUP
) == -1) e(7);
849 if (sigpending(&set
) == -1) e(8);
850 if (sigemptyset(&set2
) == -1) e(9);
851 if (sigaddset(&set2
, SIGHUP
) == -1) e(10);
852 if (set2
!= set
) e(11);
855 if (sigpending(&set
) == -1) e(12);
856 if (set
!= set2
) e(13);
861 /*--------------------------------------------------------------------------*/
872 /*---------------------------------------------------------------------------*/
874 /* Basic test for setjmp/longjmp. This includes testing that the
875 * signal mask is properly restored.
878 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \
882 sigset_t ss, ssexp; \
884 subtest = _subtest; \
888 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \
890 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \
891 ssexp = _save ? 0x32 : 0x3abc; \
892 if ((ss ^ ssexp) & ~(1 << SIGKILL)) e(388); \
896 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \
900 TEST_SETJMP(test37m
, 13, jmp_buf, setjmp(jb
), longjmp(jb
, 1), 1)
901 TEST_SETJMP(test37p
, 16, sigjmp_buf
, sigsetjmp(jb
, 0), siglongjmp(jb
, 1), 0)
902 TEST_SETJMP(test37q
, 17, sigjmp_buf
, sigsetjmp(jb
, 1), siglongjmp(jb
, 1), 1)
909 /*--------------------------------------------------------------------------*/
911 /* Test for setjmp/longjmp.
913 * Catch a signal. While in signal handler do setjmp/longjmp.
916 void catch14(signo
, code
, scp
)
919 struct sigcontext
*scp
;
935 struct sigaction act
;
936 typedef _PROTOTYPE( void (*sighandler_t
), (int sig
) );
945 act
.sa_handler
= (sighandler_t
) catch14
; /* fudge */
946 if (sigaction(SIGSEGV
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
947 if (kill(getpid(), SIGSEGV
) == -1) e(4);
953 /*---------------------------------------------------------------------------*/
955 /* Test for setjmp/longjmp.
957 * Catch a signal. Longjmp out of signal handler.
972 struct sigaction act
;
981 act
.sa_handler
= catch15
;
982 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
984 if ((k
= setjmp(glo_jb
))) {
989 if (kill(getpid(), SIGALRM
) == -1) e(5);
997 /* Clear the signal state. */
998 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_IGN
);
999 for (i
= 1; i
< _NSIG
; i
++) signal(i
, SIG_DFL
);
1000 sigfillset(&sigset_var
);
1001 sigprocmask(SIG_UNBLOCK
, &sigset_var
, (sigset_t
*)NULL
);
1008 system("rm -rf DIR*");
1014 printf("%d errors\n", errct
);
1022 /* Expect exactly one child, and that it exits with 0. */
1033 if (status
!= 0) e(90);
1049 sprintf(msgbuf
, "Subtest %d, error %d errno=%d ", subtest
, n
, errno
);
1051 if (errct
++ > MAX_ERROR
) {
1052 fprintf(stderr
, "Too many errors; test aborted\n");
1054 system("rm -rf DIR*");