1 /*
2 * Check decoding of sigprocmask syscall.
3 *
4 * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2017-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include "scno.h"
13
14 #ifdef __NR_sigprocmask
15
16 # include <signal.h>
17 # include <stdint.h>
18 # include <stdio.h>
19 # include <string.h>
20 # include <unistd.h>
21
22 static const char *errstr;
23
24 static long
25 k_sigprocmask(const kernel_ulong_t how, const kernel_ulong_t new_set,
26 const kernel_ulong_t old_set)
27 {
28 const long rc = syscall(__NR_sigprocmask, how, new_set, old_set);
29 errstr = sprintrc(rc);
30 return rc;
31 }
32
33 int
34 main(void)
35 {
36 static const kernel_ulong_t sig_block =
37 (kernel_ulong_t) 0xfacefeed00000000ULL | SIG_BLOCK;
38 static const kernel_ulong_t sig_unblock =
39 (kernel_ulong_t) 0xfacefeed00000000ULL | SIG_UNBLOCK;
40 static const kernel_ulong_t sig_setmask =
41 (kernel_ulong_t) 0xfacefeed00000000ULL | SIG_SETMASK;
42
43 if (k_sigprocmask(sig_setmask, 0, 0))
44 perror_msg_and_skip("sigprocmask");
45 puts("sigprocmask(SIG_SETMASK, NULL, NULL) = 0");
46
47 TAIL_ALLOC_OBJECT_CONST_PTR(kernel_ulong_t, new_set);
48 TAIL_ALLOC_OBJECT_CONST_PTR(kernel_ulong_t, old_set);
49 TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, libc_set);
50
51 memset(new_set, 0, sizeof(*new_set));
52 k_sigprocmask(sig_setmask, (uintptr_t) new_set, 0);
53 printf("sigprocmask(SIG_SETMASK, [], NULL) = %s\n", errstr);
54
55 k_sigprocmask(sig_unblock,
56 (uintptr_t) (new_set - 1), (uintptr_t) old_set);
57 puts("sigprocmask(SIG_UNBLOCK, ~[], []) = 0");
58
59 if (F8ILL_KULONG_SUPPORTED) {
60 k_sigprocmask(sig_unblock, f8ill_ptr_to_kulong(new_set), 0);
61 printf("sigprocmask(SIG_UNBLOCK, %#jx, NULL) = %s\n",
62 (uintmax_t) f8ill_ptr_to_kulong(new_set), errstr);
63
64 k_sigprocmask(sig_unblock, 0, f8ill_ptr_to_kulong(old_set));
65 printf("sigprocmask(SIG_UNBLOCK, NULL, %#jx) = %s\n",
66 (uintmax_t) f8ill_ptr_to_kulong(old_set), errstr);
67 }
68
69 sigemptyset(libc_set);
70 sigaddset(libc_set, SIGHUP);
71 memcpy(new_set, libc_set, sizeof(*new_set));
72
73 k_sigprocmask(sig_block, (uintptr_t) new_set, (uintptr_t) old_set);
74 puts("sigprocmask(SIG_BLOCK, [HUP], []) = 0");
75
76 memset(libc_set, -1, sizeof(*libc_set));
77 sigdelset(libc_set, SIGHUP);
78 memcpy(new_set, libc_set, sizeof(*new_set));
79
80 k_sigprocmask(sig_unblock, (uintptr_t) new_set, (uintptr_t) old_set);
81 puts("sigprocmask(SIG_UNBLOCK, ~[HUP], [HUP]) = 0");
82
83 sigdelset(libc_set, SIGKILL);
84 memcpy(new_set, libc_set, sizeof(*new_set));
85
86 k_sigprocmask(sig_unblock, (uintptr_t) new_set, (uintptr_t) old_set);
87 puts("sigprocmask(SIG_UNBLOCK, ~[HUP KILL], [HUP]) = 0");
88
89 sigemptyset(libc_set);
90 sigaddset(libc_set, SIGHUP);
91 sigaddset(libc_set, SIGINT);
92 sigaddset(libc_set, SIGQUIT);
93 sigaddset(libc_set, SIGALRM);
94 sigaddset(libc_set, SIGTERM);
95 memcpy(new_set, libc_set, sizeof(*new_set));
96
97 k_sigprocmask(sig_block, (uintptr_t) new_set, (uintptr_t) old_set);
98 printf("sigprocmask(SIG_BLOCK, %s, [HUP]) = 0\n",
99 "[HUP INT QUIT ALRM TERM]");
100
101 k_sigprocmask(sig_setmask, 0, (uintptr_t) old_set);
102 printf("sigprocmask(SIG_SETMASK, NULL, %s) = 0\n",
103 "[HUP INT QUIT ALRM TERM]");
104
105 k_sigprocmask(sig_setmask, (uintptr_t) (new_set + 1), 0);
106 printf("sigprocmask(SIG_SETMASK, %p, NULL) = %s\n",
107 new_set + 1, errstr);
108
109 k_sigprocmask(sig_setmask,
110 (uintptr_t) new_set, (uintptr_t) (old_set + 1));
111 printf("sigprocmask(SIG_SETMASK, %s, %p) = %s\n",
112 "[HUP INT QUIT ALRM TERM]", old_set + 1, errstr);
113
114 uintptr_t efault = sizeof(*new_set) / 2 + (uintptr_t) new_set;
115
116 k_sigprocmask(sig_setmask, efault, 0);
117 printf("sigprocmask(SIG_SETMASK, %#jx, NULL) = %s\n",
118 (uintmax_t) efault, errstr);
119
120 k_sigprocmask(sig_setmask, 0, efault);
121 printf("sigprocmask(SIG_SETMASK, NULL, %#jx) = %s\n",
122 (uintmax_t) efault, errstr);
123
124 puts("+++ exited with 0 +++");
125 return 0;
126 }
127
128 #else
129
130 SKIP_MAIN_UNDEFINED("__NR_sigprocmask")
131
132 #endif