summaryrefslogtreecommitdiff
path: root/tests/sigprocmask.c
blob: a69d6581cf7851ef1ce2e43905c354a5398d300b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Check decoding of sigprocmask syscall.
 *
 * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2017-2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"

#ifdef __NR_sigprocmask

# include <signal.h>
# include <stdint.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>

static const char *errstr;

static long
k_sigprocmask(const kernel_ulong_t how, const kernel_ulong_t new_set,
	      const kernel_ulong_t old_set)
{
	const long rc = syscall(__NR_sigprocmask, how, new_set, old_set);
	errstr = sprintrc(rc);
	return rc;
}

int
main(void)
{
	static const kernel_ulong_t sig_block =
		(kernel_ulong_t) 0xfacefeed00000000ULL | SIG_BLOCK;
	static const kernel_ulong_t sig_unblock =
		(kernel_ulong_t) 0xfacefeed00000000ULL | SIG_UNBLOCK;
	static const kernel_ulong_t sig_setmask =
		(kernel_ulong_t) 0xfacefeed00000000ULL | SIG_SETMASK;

	if (k_sigprocmask(sig_setmask, 0, 0))
		perror_msg_and_skip("sigprocmask");
	puts("sigprocmask(SIG_SETMASK, NULL, NULL) = 0");

	TAIL_ALLOC_OBJECT_CONST_PTR(kernel_ulong_t, new_set);
	TAIL_ALLOC_OBJECT_CONST_PTR(kernel_ulong_t, old_set);
	TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, libc_set);

	memset(new_set, 0, sizeof(*new_set));
	k_sigprocmask(sig_setmask, (uintptr_t) new_set, 0);
	printf("sigprocmask(SIG_SETMASK, [], NULL) = %s\n", errstr);

	k_sigprocmask(sig_unblock,
		      (uintptr_t) (new_set - 1), (uintptr_t) old_set);
	puts("sigprocmask(SIG_UNBLOCK, ~[], []) = 0");

	if (F8ILL_KULONG_SUPPORTED) {
		k_sigprocmask(sig_unblock, f8ill_ptr_to_kulong(new_set), 0);
		printf("sigprocmask(SIG_UNBLOCK, %#jx, NULL) = %s\n",
		       (uintmax_t) f8ill_ptr_to_kulong(new_set), errstr);

		k_sigprocmask(sig_unblock, 0, f8ill_ptr_to_kulong(old_set));
		printf("sigprocmask(SIG_UNBLOCK, NULL, %#jx) = %s\n",
		       (uintmax_t) f8ill_ptr_to_kulong(old_set), errstr);
	}

	sigemptyset(libc_set);
	sigaddset(libc_set, SIGHUP);
	memcpy(new_set, libc_set, sizeof(*new_set));

	k_sigprocmask(sig_block, (uintptr_t) new_set, (uintptr_t) old_set);
	puts("sigprocmask(SIG_BLOCK, [HUP], []) = 0");

	memset(libc_set, -1, sizeof(*libc_set));
	sigdelset(libc_set, SIGHUP);
	memcpy(new_set, libc_set, sizeof(*new_set));

	k_sigprocmask(sig_unblock, (uintptr_t) new_set, (uintptr_t) old_set);
	puts("sigprocmask(SIG_UNBLOCK, ~[HUP], [HUP]) = 0");

	sigdelset(libc_set, SIGKILL);
	memcpy(new_set, libc_set, sizeof(*new_set));

	k_sigprocmask(sig_unblock, (uintptr_t) new_set, (uintptr_t) old_set);
	puts("sigprocmask(SIG_UNBLOCK, ~[HUP KILL], [HUP]) = 0");

	sigemptyset(libc_set);
	sigaddset(libc_set, SIGHUP);
	sigaddset(libc_set, SIGINT);
	sigaddset(libc_set, SIGQUIT);
	sigaddset(libc_set, SIGALRM);
	sigaddset(libc_set, SIGTERM);
	memcpy(new_set, libc_set, sizeof(*new_set));

	k_sigprocmask(sig_block, (uintptr_t) new_set, (uintptr_t) old_set);
	printf("sigprocmask(SIG_BLOCK, %s, [HUP]) = 0\n",
	       "[HUP INT QUIT ALRM TERM]");

	k_sigprocmask(sig_setmask, 0, (uintptr_t) old_set);
	printf("sigprocmask(SIG_SETMASK, NULL, %s) = 0\n",
	       "[HUP INT QUIT ALRM TERM]");

	k_sigprocmask(sig_setmask, (uintptr_t) (new_set + 1), 0);
	printf("sigprocmask(SIG_SETMASK, %p, NULL) = %s\n",
	       new_set + 1, errstr);

	k_sigprocmask(sig_setmask,
		      (uintptr_t) new_set, (uintptr_t) (old_set + 1));
	printf("sigprocmask(SIG_SETMASK, %s, %p) = %s\n",
	       "[HUP INT QUIT ALRM TERM]", old_set + 1, errstr);

	uintptr_t efault = sizeof(*new_set) / 2 + (uintptr_t) new_set;

	k_sigprocmask(sig_setmask, efault, 0);
	printf("sigprocmask(SIG_SETMASK, %#jx, NULL) = %s\n",
	       (uintmax_t) efault, errstr);

	k_sigprocmask(sig_setmask, 0, efault);
	printf("sigprocmask(SIG_SETMASK, NULL, %#jx) = %s\n",
	       (uintmax_t) efault, errstr);

	puts("+++ exited with 0 +++");
	return 0;
}

#else

SKIP_MAIN_UNDEFINED("__NR_sigprocmask")

#endif