1*b268ffd3Sguenther /* $OpenBSD: sigmask.c,v 1.5 2012/02/20 01:49:09 guenther Exp $ */
22c323b51Smarc /* PUBLIC DOMAIN July 2003 Marco S Hyman <marc@snafu.org> */
32c323b51Smarc
42c323b51Smarc #include <sys/time.h>
52c323b51Smarc
62c323b51Smarc #include <pthread.h>
72c323b51Smarc #include <signal.h>
82c323b51Smarc #include <unistd.h>
92c323b51Smarc
102c323b51Smarc #include "test.h"
112c323b51Smarc
122c323b51Smarc /*
132c323b51Smarc * Test that masked signals with a default action of terminate process
142c323b51Smarc * do NOT terminate the process.
152c323b51Smarc */
main(int argc,char * argv[])162c323b51Smarc int main (int argc, char *argv[])
172c323b51Smarc {
182c323b51Smarc sigset_t mask;
19a0189cc0Smarc int sig;
20*b268ffd3Sguenther int r;
21a0189cc0Smarc
22a0189cc0Smarc /* any two (or more) command line args should cause the program
23a0189cc0Smarc to die */
24a0189cc0Smarc if (argc > 2) {
25a0189cc0Smarc printf("trigger sigalrm[1] [test should die]\n");
26a0189cc0Smarc ualarm(100000, 0);
27a0189cc0Smarc CHECKe(sleep(1));
28a0189cc0Smarc }
292c323b51Smarc
302c323b51Smarc /* mask sigalrm */
312c323b51Smarc CHECKe(sigemptyset(&mask));
322c323b51Smarc CHECKe(sigaddset(&mask, SIGALRM));
33416f625fSkurt CHECKr(pthread_sigmask(SIG_BLOCK, &mask, NULL));
34416f625fSkurt
35416f625fSkurt /* make sure pthread_sigmask() returns the right value on failure */
36*b268ffd3Sguenther r = pthread_sigmask(-1, &mask, NULL);
37*b268ffd3Sguenther ASSERTe(r, == EINVAL);
382c323b51Smarc
39a0189cc0Smarc /* now trigger sigalrm and wait for it */
40a0189cc0Smarc printf("trigger sigalrm[2] [masked, test should not die]\n");
412c323b51Smarc ualarm(100000, 0);
422c323b51Smarc CHECKe(sleep(1));
432c323b51Smarc
44a0189cc0Smarc /* sigwait for sigalrm, it should be pending. If it is not
45a0189cc0Smarc the test will hang. */
46416f625fSkurt CHECKr(sigwait(&mask, &sig));
47a0189cc0Smarc ASSERT(sig == SIGALRM);
48a0189cc0Smarc
49a0189cc0Smarc /* make sure sigwait didn't muck with the mask by triggering
50a0189cc0Smarc sigalrm, again */
51a0189cc0Smarc printf("trigger sigalrm[3] after sigwait [masked, test should not die]\n");
52a0189cc0Smarc ualarm(100000, 0);
53a0189cc0Smarc CHECKe(sleep(1));
54a0189cc0Smarc
55a0189cc0Smarc /* any single command line arg will run this code wich unmasks the
56a0189cc0Smarc signal and then makes sure the program terminates when sigalrm
57a0189cc0Smarc is triggered. */
58a0189cc0Smarc if (argc > 1) {
59a0189cc0Smarc printf("trigger sigalrm[4] [unmasked, test should die]\n");
60416f625fSkurt CHECKr(pthread_sigmask(SIG_UNBLOCK, &mask, NULL));
61a0189cc0Smarc ualarm(100000, 0);
62a0189cc0Smarc CHECKe(sleep(1));
63a0189cc0Smarc }
64a0189cc0Smarc
652c323b51Smarc SUCCEED;
662c323b51Smarc }
67