xref: /minix/minix/tests/test37.c (revision e3b78ef1)
1 /* test 37 - signals */
2 
3 #include <sys/types.h>
4 #include <sys/times.h>
5 #include <sys/wait.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <setjmp.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <assert.h>
13 
14 #define ITERATIONS 2
15 #define SIGS 14
16 int max_error = 4;
17 #include "common.h"
18 
19 
20 
21 int iteration, cumsig, sig1, sig2;
22 
23 int sigarray[SIGS] = {SIGHUP, SIGILL, SIGTRAP, SIGABRT, SIGIOT,
24 	      SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM,
25 	      SIGTERM};
26 
27 /* Prototypes produced automatically by mkptypes. */
28 int main(int argc, char *argv []);
29 void test37a(void);
30 void func1(int sig);
31 void func2(int sig);
32 void test37b(void);
33 void catch1(int signo);
34 void catch2(int signo);
35 void test37c(void);
36 void catch3(int signo);
37 void test37d(void);
38 void catch4(int signo);
39 void test37e(void);
40 void catch5(int signo);
41 void test37f(void);
42 void sigint_handler(int signo);
43 void sigpipe_handler(int signo);
44 void test37g(void);
45 void sighup8(int signo);
46 void sigpip8(int signo);
47 void sigter8(int signo);
48 void test37h(void);
49 void sighup9(int signo);
50 void sigter9(int signo);
51 void test37i(void);
52 void sighup10(int signo);
53 void sigalrm_handler10(int signo);
54 void test37j(void);
55 void test37k(void);
56 void test37l(void);
57 void func_m1(void);
58 void func_m2(void);
59 void test37m(void);
60 void test37p(void);
61 void test37q(void);
62 void longjerr(void);
63 void catch14(int signo, int code, struct sigcontext * scp);
64 void test37n(void);
65 void catch15(int signo);
66 void test37o(void);
67 void clearsigstate(void);
68 void wait_for(int pid);
69 
70 int main(argc, argv)
71 int argc;
72 char *argv[];
73 {
74   int i, m = 0377777;
75 
76   sync();
77 
78   start(37);
79 
80   if (argc == 2) m = atoi(argv[1]);
81 
82   for (i = 0; i < ITERATIONS; i++) {
83 	iteration = 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();
101   }
102 
103   quit();
104 
105   return(-1);	/* Unreachable */
106 }
107 
108 void test37a()
109 {
110 /* Test signal set management. */
111 
112   sigset_t s;
113 
114   subtest = 1;
115   clearsigstate();
116 
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);
135 
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);
156 
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);
165 
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);
173 
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);
185 
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);
193 
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);
210 }
211 
212 void func1(sig)
213 int sig;
214 {
215   sig1++;
216 }
217 
218 void func2(sig)
219 int sig;
220 {
221   sig2++;
222 }
223 
224 
225 static int sigmemcmp(sigset_t *s1, sigset_t *s2, int size)
226 {
227 	int i;
228 	int mismatch = 0;
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);
233 			mismatch = 1;
234 		}
235 		if(!sigismember(s1, i) && sigismember(s2, i)) {
236 			fprintf(stderr, "sig %d not set in first but is in 2nd\n", i);
237 			mismatch = 1;
238 		}
239 	}
240 
241 	return mismatch;
242 }
243 
244 void test37b()
245 {
246 /* Test sigprocmask and sigpending. */
247   int i;
248   pid_t p;
249   sigset_t s, s1, s_empty, s_full, s_ill, s_ill_pip, s_nokill, s_nokill_stop;
250   struct sigaction sa, osa;
251 
252   subtest = 2;
253   clearsigstate();
254 
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);
263   s_nokill = s_full;
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);
269 
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.
272   */
273   sa.sa_handler = SIG_DFL;
274   sa.sa_mask = s_empty;
275   sa.sa_flags = 0;
276   for (i = 0; i < SIGS; i++) sigaction(i, &sa, &osa);
277 
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);
280 
281   /* Install a signal handler. */
282   sa.sa_handler = func1;
283   sa.sa_mask = s_ill;
284   sa.sa_flags = SA_NODEFER | SA_NOCLDSTOP;
285   osa.sa_handler = SIG_IGN;
286   osa.sa_mask = s_empty;
287   osa.sa_flags = 0;
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);
292 
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;
299   osa.sa_flags = 0;
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);
305 
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;
311   osa.sa_flags = 0;
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);
316 
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);
320   errno = 0;
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 */
324   errno = 0;
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);
330 
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);
335   s = s_nokill_stop;
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);
339   s = s_nokill_stop;
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);
343   s1 = s_nokill_stop;
344   if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(37);
345   if (sigmemcmp(&s, &s1, sizeof(s))) e(38);
346 
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);
354 
355   /* Check error condition. */
356   errno = 0;
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);
361 
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);
373 
374   /* Trying to block SIGKILL is not allowed, but is not an error, either. */
375   s = s_empty;
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);
381 
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);
386   p = getpid();
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);
396 }
397 
398 /*---------------------------------------------------------------------------*/
399 int x;
400 sigset_t glo_vol_set;
401 
402 void catch1(signo)
403 int signo;
404 {
405   x = 42;
406 }
407 
408 void catch2(signo)
409 int signo;
410 {
411   if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0)
412 	e(1);
413 }
414 
415 /* Verify that signal(2), which is now built on top of sigaction(2), still
416 * works.
417 */
418 void test37c()
419 {
420   pid_t pid;
421   sigset_t sigset_var;
422 
423   subtest = 3;
424   clearsigstate();
425   x = 0;
426 
427   /* Verify an installed signal handler persists across a fork(2). */
428   if (signal(SIGTERM, catch1) == SIG_ERR) e(1);
429   switch (pid = fork()) {
430       case 0:			/* child */
431 	errct = 0;
432 	while (x == 0);
433 	if (x != 42) e(2);
434 	exit(errct == 0 ? 0 : 1);
435       case -1:	e(3);	break;
436       default:			/* parent */
437 	sleep(1);
438 	if (kill(pid, SIGTERM) != 0) e(4);
439 	wait_for(pid);
440 	break;
441   }
442 
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);
452 
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);
457 
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.
461    */
462   if (signal(SIGTERM, catch1) == SIG_ERR) e(15);
463   switch ((pid = fork())) {
464       case 0:			/* child */
465 	errct = 0;
466 	while (x == 0);
467 	if (x != 42) e(16);
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);
471 
472 #if 0
473 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */
474 	if (signal(SIGTERM, catch1) != SIG_DFL) e(20);
475 #else
476 	if (signal(SIGTERM, catch1) != catch1) e(20);
477 #endif
478 	exit(errct == 0 ? 0 : 1);
479       default:			/* parent */
480 	sleep(1);
481 	if (kill(pid, SIGTERM) != 0) e(21);
482 	wait_for(pid);
483 	break;
484       case -1:	e(22);	break;
485   }
486 }
487 
488 /*---------------------------------------------------------------------------*/
489 /* Test that the signal handler can be invoked recursively with the
490 * state being properly saved and restored.
491 */
492 
493 static int y;
494 static int z;
495 
496 void catch3(signo)
497 int signo;
498 {
499   if (z == 1) {			/* catching a nested signal */
500 	y = 2;
501 	return;
502   }
503   z = 1;
504   if (kill(getpid(), SIGHUP) != 0) e(1);
505   while (y != 2);
506   y = 1;
507 }
508 
509 void test37d()
510 {
511   struct sigaction act;
512 
513   subtest = 4;
514   clearsigstate();
515   y = 0;
516   z = 0;
517 
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);
524   if (y != 1) e(4);
525 }
526 
527 /*---------------------------------------------------------------------------*/
528 
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.
532 */
533 
534 void catch4(signo)
535 int signo;
536 {
537   sigset_t oset;
538   sigset_t set;
539 
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);
547 }
548 
549 void test37e()
550 {
551   struct sigaction act, oact;
552   sigset_t set, oset;
553 
554   subtest = 5;
555   clearsigstate();
556 
557   act.sa_handler = catch4;
558   sigemptyset(&act.sa_mask);
559   sigaddset(&act.sa_mask, SIGTERM);
560   sigaddset(&act.sa_mask, SIGHUP);
561   act.sa_flags = 0;
562   if (sigaction(SIGINT, &act, &oact) == -1) e(2);
563 
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);
572 }
573 
574 /*---------------------------------------------------------------------------*/
575 
576 /* Test the basic functionality of sigsuspend(2). */
577 
578 void catch5(signo)
579 int signo;
580 {
581   x = 1;
582 }
583 
584 void test37f()
585 {
586   sigset_t set;
587   int r;
588   struct sigaction act;
589   pid_t pid;
590 
591   subtest = 6;
592   clearsigstate();
593 
594   switch (pid = fork()) {
595       case 0:			/* child */
596 	errct = 0;
597 	sleep(1);
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);
603 	act.sa_flags = 0;
604 	act.sa_handler = catch5;
605 	if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
606 
607 	if (sigemptyset(&set) == -1) e(5);
608 	r = sigsuspend(&set);
609 
610 	if (r != -1 || errno != EINTR || x != 1) e(6);
611 	wait_for(pid);
612 	break;
613   }
614 }
615 
616 /*----------------------------------------------------------------------*/
617 
618 /* Test that sigsuspend() does block the signals specified in its
619 * argument, and after sigsuspend returns, the previous signal
620 * mask is restored.
621 *
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
627 * invoked.
628 */
629 
630 void sigint_handler(signo)
631 int signo;
632 {
633   x = 1;
634   z++;
635 }
636 
637 void sigpipe_handler(signo)
638 int signo;
639 {
640   x = 2;
641   z++;
642 }
643 
644 void test37g()
645 {
646   sigset_t set;
647   int r;
648   struct sigaction act;
649   pid_t pid;
650 
651   subtest = 7;
652   clearsigstate();
653   x = 0;
654   z = 0;
655 
656   switch (pid = fork()) {
657       case 0:			/* child */
658 	errct = 0;
659 	sleep(1);
660 	if (kill(getppid(), SIGINT) == -1) e(1);
661 	sleep(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);
667 	act.sa_flags = 0;
668 	act.sa_handler = sigint_handler;
669 	if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);
670 
671 	act.sa_handler = sigpipe_handler;
672 	if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);
673 
674 	if (sigemptyset(&set) == -1) e(6);
675 	if (sigaddset(&set, SIGINT) == -1) e(7);
676 	r = sigsuspend(&set);
677 	if (r != -1) e(8);
678 	if (errno != EINTR) e(9);
679 	if (z != 2) e(10);
680 	if (x != 1) e(11);
681 	wait_for(pid);
682 	break;
683   }
684 }
685 
686 /*--------------------------------------------------------------------------*/
687 
688 /* Test that sigsuspend() does block the signals specified in its
689 * argument, and after sigsuspend returns, the previous signal
690 * mask is restored.
691 *
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.
698 */
699 
700 void sighup8(signo)
701 int signo;
702 {
703   x = 1;
704   z++;
705 }
706 
707 void sigpip8(signo)
708 int signo;
709 {
710   x = 1;
711   z++;
712 }
713 
714 void sigter8(signo)
715 int signo;
716 {
717   x = 2;
718   z++;
719 }
720 
721 void test37h()
722 {
723   sigset_t set;
724   int r;
725   struct sigaction act;
726   pid_t pid;
727 
728   subtest = 8;
729   clearsigstate();
730   x = 0;
731   z = 0;
732 
733   switch (pid = fork()) {
734       case 0:			/* child */
735 	errct = 0;
736 	sleep(1);
737 	if (kill(getppid(), SIGHUP) == -1) e(1);
738 	sleep(1);
739 	if (kill(getppid(), SIGPIPE) == -1) e(2);
740 	sleep(1);
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);
746 	act.sa_flags = 0;
747 	act.sa_handler = sighup8;
748 	if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7);
749 
750 	act.sa_handler = sigpip8;
751 	if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);
752 
753 	act.sa_handler = sigter8;
754 	if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);
755 
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);
760 	if (r != -1) e(13);
761 	if (errno != EINTR) e(14);
762 	if (z != 3) e(15);
763 	if (x != 1) e(16);
764 	wait_for(pid);
765 	break;
766   }
767 }
768 
769 /*--------------------------------------------------------------------------*/
770 
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.
774 */
775 
776 void sighup9(signo)
777 int signo;
778 {
779   y++;
780 }
781 
782 void sigter9(signo)
783 int signo;
784 {
785   z++;
786 }
787 
788 void test37i()
789 {
790   sigset_t set;
791   struct sigaction act;
792 
793   subtest = 9;
794   clearsigstate();
795   y = 0;
796   z = 0;
797 
798   if (sigemptyset(&act.sa_mask) == -1) e(1);
799   act.sa_flags = 0;
800 
801   act.sa_handler = sighup9;
802   if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
803 
804   act.sa_handler = sigter9;
805   if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);
806 
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);
811 
812   if (kill(getpid(), SIGHUP) == -1) e(8);
813   if (kill(getpid(), SIGTERM) == -1) e(9);
814   if (y != 0) e(10);
815   if (z != 0) e(11);
816 
817   if (sigemptyset(&set) == -1) e(12);
818   if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12);
819   if (y != 1) e(13);
820   if (z != 1) e(14);
821 }
822 
823 /*---------------------------------------------------------------------------*/
824 
825 /* Block SIGINT and then send this signal to ourself.
826 *
827 * Install signal handlers for SIGALRM and SIGINT.
828 *
829 * Set an alarm for 6 seconds, then sleep for 7.
830 *
831 * The SIGALRM should interrupt the sleep, but the SIGINT
832 * should remain pending.
833 */
834 
835 void sighup10(signo)
836 int signo;
837 {
838   y++;
839 }
840 
841 void sigalrm_handler10(signo)
842 int signo;
843 {
844   z++;
845 }
846 
847 void test37j()
848 {
849   sigset_t set, set2;
850   struct sigaction act;
851 
852   subtest = 10;
853   clearsigstate();
854   y = 0;
855   z = 0;
856 
857   if (sigemptyset(&act.sa_mask) == -1) e(1);
858   act.sa_flags = 0;
859 
860   act.sa_handler = sighup10;
861   if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);
862 
863   act.sa_handler = sigalrm_handler10;
864   if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);
865 
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);
869 
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);
875   alarm(6);
876   sleep(7);
877   if (sigpending(&set) == -1) e(12);
878   if (sigmemcmp(&set, &set2, sizeof(set))) e(13);
879   if (y != 0) e(14);
880   if (z != 1) e(15);
881 }
882 
883 /*--------------------------------------------------------------------------*/
884 
885 void test37k()
886 {
887   subtest = 11;
888 }
889 void test37l()
890 {
891   subtest = 12;
892 }
893 
894 /*---------------------------------------------------------------------------*/
895 
896 /* Basic test for setjmp/longjmp.  This includes testing that the
897 * signal mask is properly restored.
898 */
899 
900 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save)	\
901 void _name(void)							\
902 {									\
903   _type jb;								\
904   sigset_t ss, ss2, ss_orig;						\
905 									\
906   subtest = _subtest;							\
907   clearsigstate();							\
908 									\
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);	\
914   if (_setjmp) {							\
915 	sigset_t ssexp;							\
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);			\
920 	return;								\
921   }									\
922   ss = ss2;								\
923   if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4);	\
924   _longjmp;								\
925 }
926 
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)
930 
931 void longjerr()
932 {
933   e(5);
934 }
935 
936 /*--------------------------------------------------------------------------*/
937 
938 /* Test for setjmp/longjmp.
939 *
940 * Catch a signal.  While in signal handler do setjmp/longjmp.
941 */
942 
943 void catch14(signo, code, scp)
944 int signo;
945 int code;
946 struct sigcontext *scp;
947 {
948   jmp_buf jb;
949 
950   if (setjmp(jb)) {
951 	x++;
952 	sigreturn(scp);
953 	e(1);
954   }
955   y++;
956   longjmp(jb, 1);
957   e(2);
958 }
959 
960 void test37n()
961 {
962   struct sigaction act;
963   typedef void(*sighandler_t) (int sig);
964 
965   subtest = 14;
966   clearsigstate();
967   x = 0;
968   y = 0;
969 
970   act.sa_flags = 0;
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);
975 
976   if (x != 1) e(5);
977   if (y != 1) e(6);
978 }
979 
980 /*---------------------------------------------------------------------------*/
981 
982 /* Test for setjmp/longjmp.
983  *
984  * Catch a signal.  Longjmp out of signal handler.
985  */
986 jmp_buf glo_jb;
987 
988 void catch15(signo)
989 int signo;
990 {
991   z++;
992   longjmp(glo_jb, 7);
993   e(1);
994 
995 }
996 
997 void test37o()
998 {
999   struct sigaction act;
1000   int k;
1001 
1002   subtest = 15;
1003   clearsigstate();
1004   z = 0;
1005 
1006   act.sa_flags = 0;
1007   sigemptyset(&act.sa_mask);
1008   act.sa_handler = catch15;
1009   if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2);
1010 
1011   if ((k = setjmp(glo_jb))) {
1012 	if (z != 1) e(399);
1013 	if (k != 7) e(4);
1014 	return;
1015   }
1016   if (kill(getpid(), SIGALRM) == -1) e(5);
1017 }
1018 
1019 void clearsigstate()
1020 {
1021   int i;
1022   sigset_t sigset_var;
1023 
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);
1029 }
1030 
1031 void wait_for(pid)
1032 pid_t pid;
1033 {
1034 /* Expect exactly one child, and that it exits with 0. */
1035 
1036   int r;
1037   int status;
1038 
1039   errno = 0;
1040   while (1) {
1041 	errno = 0;
1042 	r = wait(&status);
1043 	if (r == pid) {
1044 		errno = 0;
1045 		if (status != 0) e(90);
1046 		return;
1047 	}
1048 	if (r < 0) {
1049 		e(91);
1050 		return;
1051 	}
1052 	e(92);
1053   }
1054 }
1055 
1056