summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sigthr/sigthr_test.c
blob: c2eaa1f77964c64f51cf8cf24e0e7068c23c711d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (c) 2015  Philip Guenther <guenther@openbsd.org>
 *
 * Public domain.
 *
 * Verify that SIGTHR can't be blocked or caught by applications.
 */

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

void sighandler(int sig) { }

int
main(void)
{
	struct sigaction sa;
	sigset_t set, oset;

	/*
	 * check sigprocmask
	 */
	if (sigprocmask(SIG_BLOCK, NULL, &set))
		err(1, "sigprocmask");
	if (sigismember(&set, SIGTHR))
		errx(1, "SIGTHR already blocked");
	sigaddset(&set, SIGTHR);
	if (sigprocmask(SIG_BLOCK, &set, NULL))
		err(1, "sigprocmask");
	if (sigprocmask(SIG_SETMASK, &set, &oset))
		err(1, "sigprocmask");
	if (sigismember(&oset, SIGTHR))
		errx(1, "SIGTHR blocked with SIG_BLOCK");
	if (sigprocmask(SIG_BLOCK, NULL, &oset))
		err(1, "sigprocmask");
	if (sigismember(&oset, SIGTHR))
		errx(1, "SIGTHR blocked with SIG_SETMASK");

	/*
	 * check sigaction
	 */
	if (sigaction(SIGTHR, NULL, &sa) == 0)
		errx(1, "sigaction(SIGTHR) succeeded");
	else if (errno != EINVAL)
		err(1, "sigaction(SIGTHR) didn't fail with EINVAL");
	memset(&sa, 0, sizeof sa);
	sa.sa_handler = sighandler;
	sigfillset(&sa.sa_mask);
	sa.sa_flags = 0;
	if (sigaction(SIGTHR, &sa, NULL) == 0)
		errx(1, "sigaction(SIGTHR) succeeded");
	else if (errno != EINVAL)
		err(1, "sigaction(SIGTHR) didn't fail with EINVAL");
	if (sigaction(SIGUSR1, &sa, NULL))
		err(1, "sigaction(SIGUSR1)");
	if (sigaction(SIGUSR1, NULL, &sa))
		err(1, "sigaction(SIGUSR1)");
	if (sigismember(&sa.sa_mask, SIGTHR))
		errx(1, "SIGTHR blocked with sigaction");

	return 0;
}