xref: /openbsd/regress/lib/libc/sigthr/sigthr_test.c (revision 3ba3a6ef)
1 /*
2  * Copyright (c) 2015  Philip Guenther <guenther@openbsd.org>
3  *
4  * Public domain.
5  *
6  * Verify that SIGTHR can't be blocked or caught by applications.
7  */
8 
9 #include <err.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <signal.h>
15 
sighandler(int sig)16 void sighandler(int sig) { }
17 
18 int
main(void)19 main(void)
20 {
21 	struct sigaction sa;
22 	sigset_t set, oset;
23 
24 	/*
25 	 * check sigprocmask
26 	 */
27 	if (sigprocmask(SIG_BLOCK, NULL, &set))
28 		err(1, "sigprocmask");
29 	if (sigismember(&set, SIGTHR))
30 		errx(1, "SIGTHR already blocked");
31 	sigaddset(&set, SIGTHR);
32 	if (sigprocmask(SIG_BLOCK, &set, NULL))
33 		err(1, "sigprocmask");
34 	if (sigprocmask(SIG_SETMASK, &set, &oset))
35 		err(1, "sigprocmask");
36 	if (sigismember(&oset, SIGTHR))
37 		errx(1, "SIGTHR blocked with SIG_BLOCK");
38 	if (sigprocmask(SIG_BLOCK, NULL, &oset))
39 		err(1, "sigprocmask");
40 	if (sigismember(&oset, SIGTHR))
41 		errx(1, "SIGTHR blocked with SIG_SETMASK");
42 
43 	/*
44 	 * check sigaction
45 	 */
46 	if (sigaction(SIGTHR, NULL, &sa) == 0)
47 		errx(1, "sigaction(SIGTHR) succeeded");
48 	else if (errno != EINVAL)
49 		err(1, "sigaction(SIGTHR) didn't fail with EINVAL");
50 	memset(&sa, 0, sizeof sa);
51 	sa.sa_handler = sighandler;
52 	sigfillset(&sa.sa_mask);
53 	sa.sa_flags = 0;
54 	if (sigaction(SIGTHR, &sa, NULL) == 0)
55 		errx(1, "sigaction(SIGTHR) succeeded");
56 	else if (errno != EINVAL)
57 		err(1, "sigaction(SIGTHR) didn't fail with EINVAL");
58 	if (sigaction(SIGUSR1, &sa, NULL))
59 		err(1, "sigaction(SIGUSR1)");
60 	if (sigaction(SIGUSR1, NULL, &sa))
61 		err(1, "sigaction(SIGUSR1)");
62 	if (sigismember(&sa.sa_mask, SIGTHR))
63 		errx(1, "SIGTHR blocked with sigaction");
64 
65 	return 0;
66 }
67