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 longjerr
, (void));
60 _PROTOTYPE(void catch14
, (int signo
, int code
, struct sigcontext
* scp
));
61 _PROTOTYPE(void test37n
, (void));
62 _PROTOTYPE(void catch15
, (int signo
));
63 _PROTOTYPE(void test37o
, (void));
64 _PROTOTYPE(void clearsigstate
, (void));
65 _PROTOTYPE(void quit
, (void));
66 _PROTOTYPE(void wait_for
, (int pid
));
67 _PROTOTYPE(void e
, (int n
));
77 if (argc
== 2) m
= atoi(argv
[1]);
80 fflush(stdout
); /* have to flush for child's benefit */
82 system("rm -rf DIR_37; mkdir DIR_37");
85 for (i
= 0; i
< ITERATIONS
; i
++) {
87 if (m
& 0000001) test37a();
88 if (m
& 0000002) test37b();
89 if (m
& 0000004) test37c();
90 if (m
& 0000010) test37d();
91 if (m
& 0000020) test37e();
92 if (m
& 0000040) test37f();
93 if (m
& 0000100) test37g();
94 if (m
& 0000200) test37h();
95 if (m
& 0000400) test37i();
96 if (m
& 0001000) test37j();
97 if (m
& 0002000) test37k();
98 if (m
& 0004000) test37l();
99 if (m
& 0010000) test37m();
100 if (m
& 0020000) test37n();
101 if (m
& 0040000) test37o();
105 return(-1); /* impossible */
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);
135 /* Create a full set and see if any bits are off. */
136 if (sigfillset(&s
) != 0) e(19);
137 if (sigemptyset(&s
) != 0) e(20);
138 if (sigfillset(&s
) != 0) e(21);
139 if (sigismember(&s
, SIGHUP
) != 1) e(22);
140 if (sigismember(&s
, SIGINT
) != 1) e(23);
141 if (sigismember(&s
, SIGQUIT
) != 1) e(24);
142 if (sigismember(&s
, SIGILL
) != 1) e(25);
143 if (sigismember(&s
, SIGTRAP
) != 1) e(26);
144 if (sigismember(&s
, SIGABRT
) != 1) e(27);
145 if (sigismember(&s
, SIGIOT
) != 1) e(28);
146 if (sigismember(&s
, SIGFPE
) != 1) e(30);
147 if (sigismember(&s
, SIGKILL
) != 1) e(31);
148 if (sigismember(&s
, SIGUSR1
) != 1) e(32);
149 if (sigismember(&s
, SIGSEGV
) != 1) e(33);
150 if (sigismember(&s
, SIGUSR2
) != 1) e(34);
151 if (sigismember(&s
, SIGPIPE
) != 1) e(35);
152 if (sigismember(&s
, SIGALRM
) != 1) e(36);
153 if (sigismember(&s
, SIGTERM
) != 1) e(37);
155 /* Create an empty set, then turn on bits individually. */
156 if (sigemptyset(&s
) != 0) e(39);
157 if (sigaddset(&s
, SIGHUP
) != 0) e(40);
158 if (sigaddset(&s
, SIGINT
) != 0) e(41);
159 if (sigaddset(&s
, SIGQUIT
) != 0) e(42);
160 if (sigaddset(&s
, SIGILL
) != 0) e(43);
161 if (sigaddset(&s
, SIGTRAP
) != 0) e(44);
163 /* See if the bits just turned on are indeed on. */
164 if (sigismember(&s
, SIGHUP
) != 1) e(45);
165 if (sigismember(&s
, SIGINT
) != 1) e(46);
166 if (sigismember(&s
, SIGQUIT
) != 1) e(47);
167 if (sigismember(&s
, SIGILL
) != 1) e(48);
168 if (sigismember(&s
, SIGTRAP
) != 1) e(49);
170 /* The others should be turned off. */
171 if (sigismember(&s
, SIGABRT
) != 0) e(50);
172 if (sigismember(&s
, SIGIOT
) != 0) e(51);
173 if (sigismember(&s
, SIGFPE
) != 0) e(53);
174 if (sigismember(&s
, SIGKILL
) != 0) e(54);
175 if (sigismember(&s
, SIGUSR1
) != 0) e(55);
176 if (sigismember(&s
, SIGSEGV
) != 0) e(56);
177 if (sigismember(&s
, SIGUSR2
) != 0) e(57);
178 if (sigismember(&s
, SIGPIPE
) != 0) e(58);
179 if (sigismember(&s
, SIGALRM
) != 0) e(59);
180 if (sigismember(&s
, SIGTERM
) != 0) e(60);
182 /* Now turn them off and see if all are off. */
183 if (sigdelset(&s
, SIGHUP
) != 0) e(62);
184 if (sigdelset(&s
, SIGINT
) != 0) e(63);
185 if (sigdelset(&s
, SIGQUIT
) != 0) e(64);
186 if (sigdelset(&s
, SIGILL
) != 0) e(65);
187 if (sigdelset(&s
, SIGTRAP
) != 0) e(66);
189 if (sigismember(&s
, SIGHUP
) != 0) e(67);
190 if (sigismember(&s
, SIGINT
) != 0) e(68);
191 if (sigismember(&s
, SIGQUIT
) != 0) e(69);
192 if (sigismember(&s
, SIGILL
) != 0) e(70);
193 if (sigismember(&s
, SIGTRAP
) != 0) e(71);
194 if (sigismember(&s
, SIGABRT
) != 0) e(72);
195 if (sigismember(&s
, SIGIOT
) != 0) e(73);
196 if (sigismember(&s
, SIGFPE
) != 0) e(75);
197 if (sigismember(&s
, SIGKILL
) != 0) e(76);
198 if (sigismember(&s
, SIGUSR1
) != 0) e(77);
199 if (sigismember(&s
, SIGSEGV
) != 0) e(78);
200 if (sigismember(&s
, SIGUSR2
) != 0) e(79);
201 if (sigismember(&s
, SIGPIPE
) != 0) e(80);
202 if (sigismember(&s
, SIGALRM
) != 0) e(81);
203 if (sigismember(&s
, SIGTERM
) != 0) e(82);
220 /* Test sigprocmask and sigpending. */
223 sigset_t s
, s1
, s_empty
, s_full
, s_ill
, s_ill_pip
, s_nokill
, s_nokill_stop
;
224 struct sigaction sa
, osa
;
229 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */
230 if (sigemptyset(&s_empty
) != 0) e(1);
231 if (sigemptyset(&s_ill
) != 0) e(2);
232 if (sigemptyset(&s_ill_pip
) != 0) e(3);
233 if (sigaddset(&s_ill
, SIGILL
) != 0) e(4);
234 if (sigaddset(&s_ill_pip
, SIGILL
) != 0) e(5);
235 if (sigaddset(&s_ill_pip
, SIGPIPE
) != 0) e(6);
236 if (sigfillset(&s_full
) != 0) e(7);
238 if (sigdelset(&s_nokill
, SIGKILL
) != 0) e(8);
239 s_nokill_stop
= s_nokill
;
240 if (sigdelset(&s_nokill_stop
, SIGSTOP
) != 0) e(8);
241 #ifndef _MINIX /* XXX - should unsupported signals be <= _NSIG? */
242 if (SIGSTOP
> _NSIG
) e(666);
243 if (SIGSTOP
<= _NSIG
&& sigdelset(&s_nokill
, SIGSTOP
) != 0) e(888);
246 /* Now get most of the signals into default state. Don't change SIGINT
247 * or SIGQUIT, so this program can be killed. SIGKILL is also special.
249 sa
.sa_handler
= SIG_DFL
;
250 sa
.sa_mask
= s_empty
;
252 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, &sa
, &osa
);
254 /* The second argument may be zero. See if it wipes out the system. */
255 for (i
= 0; i
< SIGS
; i
++) sigaction(i
, (struct sigaction
*) NULL
, &osa
);
257 /* Install a signal handler. */
258 sa
.sa_handler
= func1
;
260 sa
.sa_flags
= SA_NODEFER
| SA_NOCLDSTOP
;
261 osa
.sa_handler
= SIG_IGN
;
262 osa
.sa_mask
= s_empty
;
264 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(9);
265 if (osa
.sa_handler
!= SIG_DFL
) e(10);
266 if (osa
.sa_mask
!= 0) e(11);
267 if (osa
.sa_flags
!= s_empty
) e(12);
269 /* Replace action and see if old value is read back correctly. */
270 sa
.sa_handler
= func2
;
271 sa
.sa_mask
= s_ill_pip
;
272 sa
.sa_flags
= SA_RESETHAND
| SA_NODEFER
;
273 osa
.sa_handler
= SIG_IGN
;
274 osa
.sa_mask
= s_empty
;
276 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(13);
277 if (osa
.sa_handler
!= func1
) e(14);
278 if (osa
.sa_mask
!= s_ill
) e(15);
279 if (osa
.sa_flags
!= SA_NODEFER
280 && osa
.sa_flags
!= (SA_NODEFER
| SA_NOCLDSTOP
)) e(16);
282 /* Replace action once more and check what is read back. */
283 sa
.sa_handler
= SIG_DFL
;
284 sa
.sa_mask
= s_empty
;
285 osa
.sa_handler
= SIG_IGN
;
286 osa
.sa_mask
= s_empty
;
288 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(17);
289 if (osa
.sa_handler
!= func2
) e(18);
290 if (osa
.sa_mask
!= s_ill_pip
) e(19);
291 if (osa
.sa_flags
!= (SA_RESETHAND
| SA_NODEFER
)) e(20);
293 /* Test sigprocmask(SIG_SETMASK, ...). */
294 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(18); /* block all */
295 if (sigemptyset(&s1
) != 0) e(19);
297 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(20); /* block none */
298 if (s1
!= s_nokill_stop
) e(21);
299 if (sigprocmask(SIG_SETMASK
, &s_ill
, &s1
) != 0) e(22); /* block SIGILL */
301 if (s1
!= s_empty
) e(23);
302 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(24); /* SIGILL+PIP */
303 if (s1
!= s_ill
) e(25);
304 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(26); /* block all */
305 if (s1
!= s_ill_pip
) e(27);
307 /* Test sigprocmask(SIG_UNBLOCK, ...) */
308 if (sigprocmask(SIG_UNBLOCK
, &s_ill
, &s1
) != 0) e(28);
309 if (s1
!= s_nokill_stop
) e(29);
310 if (sigprocmask(SIG_UNBLOCK
, &s_ill_pip
, &s1
) != 0) e(30);
312 if (sigdelset(&s
, SIGILL
) != 0) e(31);
314 if (sigprocmask(SIG_UNBLOCK
, &s_empty
, &s1
) != 0) e(33);
316 if (sigdelset(&s
, SIGILL
) != 0) e(34);
317 if (sigdelset(&s
, SIGPIPE
) != 0) e(35);
320 if (sigprocmask(SIG_SETMASK
, &s_empty
, &s1
) != 0) e(37);
323 /* Test sigprocmask(SIG_BLOCK, ...) */
324 if (sigprocmask(SIG_BLOCK
, &s_ill
, &s1
) != 0) e(39);
325 if (s1
!= s_empty
) e(40);
326 if (sigprocmask(SIG_BLOCK
, &s_ill_pip
, &s1
) != 0) e(41);
327 if (s1
!= s_ill
) e(42);
328 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(43);
329 if (s1
!= s_ill_pip
) e(44);
331 /* Check error condition. */
333 if (sigprocmask(20000, &s_full
, &s1
) != -1) e(45);
334 if (errno
!= EINVAL
) e(46);
335 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(47);
336 if (s1
!= s_nokill_stop
) e(48);
338 /* If second arg is 0, nothing is set. */
339 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(49);
340 if (s1
!= s_nokill_stop
) e(50);
341 if (sigprocmask(SIG_SETMASK
, &s_ill_pip
, &s1
) != 0) e(51);
342 if (s1
!= s_nokill_stop
) e(52);
343 if (sigprocmask(SIG_SETMASK
, (sigset_t
*) NULL
, &s1
) != 0) e(53);
344 if (s1
!= s_ill_pip
) e(54);
345 if (sigprocmask(SIG_BLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(55);
346 if (s1
!= s_ill_pip
) e(56);
347 if (sigprocmask(SIG_UNBLOCK
, (sigset_t
*) NULL
, &s1
) != 0) e(57);
348 if (s1
!= s_ill_pip
) e(58);
350 /* Trying to block SIGKILL is not allowed, but is not an error, either. */
352 if (sigaddset(&s
, SIGKILL
) != 0) e(59);
353 if (sigprocmask(SIG_BLOCK
, &s
, &s1
) != 0) e(60);
354 if (s1
!= s_ill_pip
) e(61);
355 if (sigprocmask(SIG_SETMASK
, &s_full
, &s1
) != 0) e(62);
356 if (s1
!= s_ill_pip
) e(63);
358 /* Test sigpending. At this moment, all signals are blocked. */
359 sa
.sa_handler
= func2
;
360 sa
.sa_mask
= s_empty
;
361 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(64);
363 kill(p
, SIGHUP
); /* send SIGHUP to self */
364 if (sigpending(&s
) != 0) e(65);
365 if (sigemptyset(&s1
) != 0) e(66);
366 if (sigaddset(&s1
, SIGHUP
) != 0) e(67);
368 sa
.sa_handler
= SIG_IGN
;
369 if (sigaction(SIGHUP
, &sa
, &osa
) != 0) e(69);
370 if (sigpending(&s
) != 0) e(70);
371 if (s
!= s_empty
) e(71);
374 /*---------------------------------------------------------------------------*/
376 sigset_t glo_vol_set
;
387 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, (sigset_t
*) &glo_vol_set
) != 0)
391 /* Verify that signal(2), which is now built on top of sigaction(2), still
403 /* Verify an installed signal handler persists across a fork(2). */
404 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(1);
405 switch (pid
= fork()) {
410 exit(errct
== 0 ? 0 : 1);
411 case -1: e(3); break;
412 default: /* parent */
414 if (kill(pid
, SIGTERM
) != 0) e(4);
419 /* Verify that the return value is the previous handler. */
420 signal(SIGINT
, SIG_IGN
);
421 if (signal(SIGINT
, catch2
) != SIG_IGN
) e(5);
422 if (signal(SIGINT
, catch1
) != catch2
) e(6);
423 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(7);
424 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(8);
425 if (signal(SIGINT
, SIG_DFL
) != catch1
) e(9);
426 if (signal(SIGINT
, SIG_DFL
) != SIG_DFL
) e(10);
427 if (signal(SIGINT
, catch1
) != SIG_DFL
) e(11);
429 /* Verify that SIG_ERR is correctly generated. */
430 if (signal(_NSIG
+ 1, catch1
) != SIG_ERR
) e(12);
431 if (signal(0, catch1
) != SIG_ERR
) e(13);
432 if (signal(-1, SIG_DFL
) != SIG_ERR
) e(14);
434 /* Verify that caught signals are automatically reset to the default,
435 * and that further instances of the same signal are not blocked here
436 * or in the signal handler.
438 if (signal(SIGTERM
, catch1
) == SIG_ERR
) e(15);
439 switch ((pid
= fork())) {
444 if (sigismember((sigset_t
*) &glo_vol_set
, SIGTERM
)) e(17);
445 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &sigset_var
) != 0) e(18);
446 if (sigismember(&sigset_var
, SIGTERM
)) e(19);
449 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
450 if (signal(SIGTERM
, catch1
) != SIG_DFL
) e(20);
452 if (signal(SIGTERM
, catch1
) != catch1
) e(20);
454 exit(errct
== 0 ? 0 : 1);
455 default: /* parent */
457 if (kill(pid
, SIGTERM
) != 0) e(21);
460 case -1: e(22); break;
464 /*---------------------------------------------------------------------------*/
465 /* Test that the signal handler can be invoked recursively with the
466 * state being properly saved and restored.
475 if (z
== 1) { /* catching a nested signal */
480 if (kill(getpid(), SIGHUP
) != 0) e(1);
487 struct sigaction act
;
494 act
.sa_handler
= catch3
;
496 act
.sa_flags
= SA_NODEFER
; /* Otherwise, nested occurence of
497 * SIGINT is blocked. */
498 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) != 0) e(2);
499 if (kill(getpid(), SIGHUP
) != 0) e(3);
503 /*---------------------------------------------------------------------------*/
505 /* Test that the signal mask in effect for the duration of a signal handler
506 * is as specified in POSIX Section 3, lines 718 -724. Test that the
507 * previous signal mask is restored when the signal handler returns.
516 if (sigemptyset(&set
) == -1) e(5001);
517 if (sigaddset(&set
, SIGTERM
) == -1) e(5002);
518 if (sigaddset(&set
, SIGHUP
) == -1) e(5003);
519 if (sigaddset(&set
, SIGINT
) == -1) e(5004);
520 if (sigaddset(&set
, SIGPIPE
) == -1) e(5005);
521 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) != 0) e(5006);
522 if (oset
!= set
) e(5007);
527 struct sigaction act
, oact
;
533 act
.sa_handler
= catch4
;
534 sigemptyset(&act
.sa_mask
);
535 sigaddset(&act
.sa_mask
, SIGTERM
);
536 sigaddset(&act
.sa_mask
, SIGHUP
);
538 if (sigaction(SIGINT
, &act
, &oact
) == -1) e(2);
540 if (sigemptyset(&set
) == -1) e(3);
541 if (sigaddset(&set
, SIGPIPE
) == -1) e(4);
542 if (sigprocmask(SIG_SETMASK
, &set
, &oset
) == -1) e(5);
543 if (kill(getpid(), SIGINT
) == -1) e(6);
544 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &oset
) == -1) e(7);
545 if (sigemptyset(&set
) == -1) e(8);
546 if (sigaddset(&set
, SIGPIPE
) == -1) e(9);
547 if (set
!= oset
) e(10);
550 /*---------------------------------------------------------------------------*/
552 /* Test the basic functionality of sigsuspend(2). */
564 struct sigaction act
;
570 switch (pid
= fork()) {
574 if (kill(getppid(), SIGINT
) == -1) e(1);
575 exit(errct
== 0 ? 0 : 1);
576 case -1: e(2); break;
577 default: /* parent */
578 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
580 act
.sa_handler
= catch5
;
581 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
583 if (sigemptyset(&set
) == -1) e(5);
584 r
= sigsuspend(&set
);
586 if (r
!= -1 || errno
!= EINTR
|| x
!= 1) e(6);
592 /*----------------------------------------------------------------------*/
594 /* Test that sigsuspend() does block the signals specified in its
595 * argument, and after sigsuspend returns, the previous signal
598 * The child sends two signals to the parent SIGINT and then SIGPIPE,
599 * separated by a long delay. The parent executes sigsuspend() with
600 * SIGINT blocked. It is expected that the parent's SIGPIPE handler
601 * will be invoked, then sigsuspend will return restoring the
602 * original signal mask, and then the SIGPIPE handler will be
606 void sigint_handler(signo
)
613 void sigpipe_handler(signo
)
624 struct sigaction act
;
632 switch (pid
= fork()) {
636 if (kill(getppid(), SIGINT
) == -1) e(1);
638 if (kill(getppid(), SIGPIPE
) == -1) e(2);
639 exit(errct
== 0 ? 0 : 1);
640 case -1: e(3); break;
641 default: /* parent */
642 if (sigemptyset(&act
.sa_mask
) == -1) e(3);
644 act
.sa_handler
= sigint_handler
;
645 if (sigaction(SIGINT
, &act
, (struct sigaction
*) NULL
) == -1) e(4);
647 act
.sa_handler
= sigpipe_handler
;
648 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(5);
650 if (sigemptyset(&set
) == -1) e(6);
651 if (sigaddset(&set
, SIGINT
) == -1) e(7);
652 r
= sigsuspend(&set
);
654 if (errno
!= EINTR
) e(9);
662 /*--------------------------------------------------------------------------*/
664 /* Test that sigsuspend() does block the signals specified in its
665 * argument, and after sigsuspend returns, the previous signal
668 * The child sends three signals to the parent: SIGHUP, then SIGPIPE,
669 * and then SIGTERM, separated by a long delay. The parent executes
670 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that
671 * the parent's SIGTERM handler will be invoked first, then sigsuspend()
672 * will return restoring the original signal mask, and then the other
673 * two handlers will be invoked.
701 struct sigaction act
;
709 switch (pid
= fork()) {
713 if (kill(getppid(), SIGHUP
) == -1) e(1);
715 if (kill(getppid(), SIGPIPE
) == -1) e(2);
717 if (kill(getppid(), SIGTERM
) == -1) e(3);
718 exit(errct
== 0 ? 0 : 1);
719 case -1: e(5); break;
720 default: /* parent */
721 if (sigemptyset(&act
.sa_mask
) == -1) e(6);
723 act
.sa_handler
= sighup8
;
724 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(7);
726 act
.sa_handler
= sigpip8
;
727 if (sigaction(SIGPIPE
, &act
, (struct sigaction
*) NULL
) == -1) e(8);
729 act
.sa_handler
= sigter8
;
730 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(9);
732 if (sigemptyset(&set
) == -1) e(10);
733 if (sigaddset(&set
, SIGHUP
) == -1) e(11);
734 if (sigaddset(&set
, SIGPIPE
) == -1) e(12);
735 r
= sigsuspend(&set
);
737 if (errno
!= EINTR
) e(14);
745 /*--------------------------------------------------------------------------*/
747 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
748 * and SIGTERM, unblock these signals with sigprocmask, and verify
749 * that these signals are delivered.
767 struct sigaction act
;
774 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
777 act
.sa_handler
= sighup9
;
778 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
780 act
.sa_handler
= sigter9
;
781 if (sigaction(SIGTERM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
783 if (sigemptyset(&set
) == -1) e(4);
784 if (sigaddset(&set
, SIGTERM
) == -1) e(5);
785 if (sigaddset(&set
, SIGHUP
) == -1) e(6);
786 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(7);
788 if (kill(getpid(), SIGHUP
) == -1) e(8);
789 if (kill(getpid(), SIGTERM
) == -1) e(9);
793 if (sigemptyset(&set
) == -1) e(12);
794 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(12);
799 /*---------------------------------------------------------------------------*/
801 /* Block SIGINT and then send this signal to ourself.
803 * Install signal handlers for SIGALRM and SIGINT.
805 * Set an alarm for 6 seconds, then sleep for 7.
807 * The SIGALRM should interrupt the sleep, but the SIGINT
808 * should remain pending.
817 void sigalrm_handler10(signo
)
826 struct sigaction act
;
833 if (sigemptyset(&act
.sa_mask
) == -1) e(1);
836 act
.sa_handler
= sighup10
;
837 if (sigaction(SIGHUP
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
839 act
.sa_handler
= sigalrm_handler10
;
840 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
842 if (sigemptyset(&set
) == -1) e(4);
843 if (sigaddset(&set
, SIGHUP
) == -1) e(5);
844 if (sigprocmask(SIG_SETMASK
, &set
, (sigset_t
*)NULL
) == -1) e(6);
846 if (kill(getpid(), SIGHUP
) == -1) e(7);
847 if (sigpending(&set
) == -1) e(8);
848 if (sigemptyset(&set2
) == -1) e(9);
849 if (sigaddset(&set2
, SIGHUP
) == -1) e(10);
850 if (set2
!= set
) e(11);
853 if (sigpending(&set
) == -1) e(12);
854 if (set
!= set2
) e(13);
859 /*--------------------------------------------------------------------------*/
870 /*---------------------------------------------------------------------------*/
872 /* Basic test for setjmp/longjmp. This includes testing that the
873 * signal mask is properly restored.
885 if (sigprocmask(SIG_SETMASK
, &ss
, (sigset_t
*)NULL
) == -1) e(1);
887 if (sigprocmask(SIG_BLOCK
, (sigset_t
*)NULL
, &ss
) == -1) e(2);
888 if (ss
!= 0x32) e(388);
892 if (sigprocmask(SIG_SETMASK
, &ss
, (sigset_t
*)NULL
) == -1) e(4);
901 /*--------------------------------------------------------------------------*/
903 /* Test for setjmp/longjmp.
905 * Catch a signal. While in signal handler do setjmp/longjmp.
908 void catch14(signo
, code
, scp
)
911 struct sigcontext
*scp
;
927 struct sigaction act
;
928 typedef _PROTOTYPE( void (*sighandler_t
), (int sig
) );
937 act
.sa_handler
= (sighandler_t
) catch14
; /* fudge */
938 if (sigaction(SIGSEGV
, &act
, (struct sigaction
*) NULL
) == -1) e(3);
939 if (kill(getpid(), SIGSEGV
) == -1) e(4);
945 /*---------------------------------------------------------------------------*/
947 /* Test for setjmp/longjmp.
949 * Catch a signal. Longjmp out of signal handler.
964 struct sigaction act
;
973 act
.sa_handler
= catch15
;
974 if (sigaction(SIGALRM
, &act
, (struct sigaction
*) NULL
) == -1) e(2);
976 if ((k
= setjmp(glo_jb
))) {
981 if (kill(getpid(), SIGALRM
) == -1) e(5);
989 /* Clear the signal state. */
990 for (i
= 1; i
<= _NSIG
; i
++) signal(i
, SIG_IGN
);
991 for (i
= 1; i
<= _NSIG
; i
++) signal(i
, SIG_DFL
);
992 sigfillset(&sigset_var
);
993 sigprocmask(SIG_UNBLOCK
, &sigset_var
, (sigset_t
*)NULL
);
1000 system("rm -rf DIR*");
1006 printf("%d errors\n", errct
);
1014 /* Expect exactly one child, and that it exits with 0. */
1025 if (status
!= 0) e(90);
1041 sprintf(msgbuf
, "Subtest %d, error %d errno=%d ", subtest
, n
, errno
);
1043 if (errct
++ > MAX_ERROR
) {
1044 fprintf(stderr
, "Too many errors; test aborted\n");
1046 system("rm -rf DIR*");