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