1 /* $OpenBSD: sigmask.c,v 1.3 2008/04/24 03:31:33 kurt Exp $ */
2 /* PUBLIC DOMAIN July 2003 Marco S Hyman <marc@snafu.org> */
3 
4 #include <sys/time.h>
5 
6 #include <pthread.h>
7 #include <signal.h>
8 #include <unistd.h>
9 
10 #include "test.h"
11 
12 /*
13  * Test that masked signals with a default action of terminate process
14  * do NOT terminate the process.
15  */
16 int main (int argc, char *argv[])
17 {
18 	sigset_t mask;
19 	int sig;
20 
21 	/* any two (or more) command line args should cause the program
22 	   to die */
23 	if (argc > 2) {
24 		printf("trigger sigalrm[1] [test should die]\n");
25 		ualarm(100000, 0);
26 		CHECKe(sleep(1));
27 	}
28 
29 	/* mask sigalrm */
30 	CHECKe(sigemptyset(&mask));
31 	CHECKe(sigaddset(&mask, SIGALRM));
32 	CHECKr(pthread_sigmask(SIG_BLOCK, &mask, NULL));
33 
34 	/* make sure pthread_sigmask() returns the right value on failure */
35 	CHECKe(pthread_sigmask(-1, &mask, NULL));
36 
37 	/* now trigger sigalrm and wait for it */
38 	printf("trigger sigalrm[2] [masked, test should not die]\n");
39 	ualarm(100000, 0);
40 	CHECKe(sleep(1));
41 
42 	/* sigwait for sigalrm, it should be pending.   If it is not
43 	   the test will hang. */
44 	CHECKr(sigwait(&mask, &sig));
45 	ASSERT(sig == SIGALRM);
46 
47 	/* make sure sigwait didn't muck with the mask by triggering
48 	   sigalrm, again */
49 	printf("trigger sigalrm[3] after sigwait [masked, test should not die]\n");
50 	ualarm(100000, 0);
51 	CHECKe(sleep(1));
52 
53 	/* any single command line arg will run this code wich unmasks the
54 	   signal and then makes sure the program terminates when sigalrm
55 	   is triggered. */
56 	if (argc > 1) {
57 		printf("trigger sigalrm[4] [unmasked, test should die]\n");
58 		CHECKr(pthread_sigmask(SIG_UNBLOCK, &mask, NULL));
59 		ualarm(100000, 0);
60 		CHECKe(sleep(1));
61 	}
62 
63 	SUCCEED;
64 }
65