1 /* $OpenBSD: sigmask.c,v 1.5 2012/02/20 01:49:09 guenther 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 */
main(int argc,char * argv[])16 int main (int argc, char *argv[])
17 {
18 sigset_t mask;
19 int sig;
20 int r;
21
22 /* any two (or more) command line args should cause the program
23 to die */
24 if (argc > 2) {
25 printf("trigger sigalrm[1] [test should die]\n");
26 ualarm(100000, 0);
27 CHECKe(sleep(1));
28 }
29
30 /* mask sigalrm */
31 CHECKe(sigemptyset(&mask));
32 CHECKe(sigaddset(&mask, SIGALRM));
33 CHECKr(pthread_sigmask(SIG_BLOCK, &mask, NULL));
34
35 /* make sure pthread_sigmask() returns the right value on failure */
36 r = pthread_sigmask(-1, &mask, NULL);
37 ASSERTe(r, == EINVAL);
38
39 /* now trigger sigalrm and wait for it */
40 printf("trigger sigalrm[2] [masked, test should not die]\n");
41 ualarm(100000, 0);
42 CHECKe(sleep(1));
43
44 /* sigwait for sigalrm, it should be pending. If it is not
45 the test will hang. */
46 CHECKr(sigwait(&mask, &sig));
47 ASSERT(sig == SIGALRM);
48
49 /* make sure sigwait didn't muck with the mask by triggering
50 sigalrm, again */
51 printf("trigger sigalrm[3] after sigwait [masked, test should not die]\n");
52 ualarm(100000, 0);
53 CHECKe(sleep(1));
54
55 /* any single command line arg will run this code wich unmasks the
56 signal and then makes sure the program terminates when sigalrm
57 is triggered. */
58 if (argc > 1) {
59 printf("trigger sigalrm[4] [unmasked, test should die]\n");
60 CHECKr(pthread_sigmask(SIG_UNBLOCK, &mask, NULL));
61 ualarm(100000, 0);
62 CHECKe(sleep(1));
63 }
64
65 SUCCEED;
66 }
67