]> granicus.if.org Git - strace/blob - tests/sigaction.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / sigaction.c
1 /*
2  * Check decoding of sigaction syscall.
3  *
4  * Copyright (c) 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_sigaction
14
15 # include <signal.h>
16 # include <stdint.h>
17 # include <stdio.h>
18 # include <string.h>
19 # include <unistd.h>
20
21 struct set_sa {
22 #if defined MIPS
23         unsigned int flags;
24         unsigned long handler;
25         unsigned long mask[1];
26 #elif defined ALPHA
27         unsigned long handler;
28         unsigned long mask[1];
29         unsigned int flags;
30 #else
31         unsigned long handler;
32         unsigned long mask[1];
33         unsigned long flags;
34         unsigned long restorer;
35 #endif
36 }
37 #ifdef ALPHA
38         ATTRIBUTE_PACKED
39 #endif
40 ;
41
42 typedef struct set_sa struct_set_sa;
43
44 #ifdef MIPS
45
46 struct get_sa {
47         unsigned int flags;
48         unsigned long handler;
49         unsigned long mask[4];
50 };
51
52 typedef struct get_sa struct_get_sa;
53
54 #else
55
56 typedef struct set_sa struct_get_sa;
57
58 #endif
59
60 static long
61 k_sigaction(const kernel_ulong_t signum, const kernel_ulong_t new_act,
62             const kernel_ulong_t old_act)
63 {
64         return syscall(__NR_sigaction, signum, new_act, old_act);
65 }
66
67 #if defined SPARC || defined SPARC64
68 /*
69  * See arch/sparc/kernel/sys_sparc_32.c:sys_sparc_sigaction
70  * and arch/sparc/kernel/sys_sparc32.c:compat_sys_sparc_sigaction
71  */
72 # define ADDR_INT ((unsigned int) -0xdefaced)
73 # define SIGNO_INT ((unsigned int) -SIGUSR1)
74 # define SIG_STR "-SIGUSR1"
75 #else
76 # define ADDR_INT ((unsigned int) 0xdefaced)
77 # define SIGNO_INT ((unsigned int) SIGUSR1)
78 # define SIG_STR "SIGUSR1"
79 #endif
80 static const kernel_ulong_t signo =
81         (kernel_ulong_t) 0xbadc0ded00000000ULL | SIGNO_INT;
82 static const kernel_ulong_t addr =
83         (kernel_ulong_t) 0xfacefeed00000000ULL | ADDR_INT;
84
85 int
86 main(void)
87 {
88         union {
89                 sigset_t libc[1];
90                 unsigned long old[1];
91         } mask;
92
93         TAIL_ALLOC_OBJECT_CONST_PTR(struct_set_sa, new_act);
94         TAIL_ALLOC_OBJECT_CONST_PTR(struct_get_sa, old_act);
95
96         if (k_sigaction(signo, 0, 0))
97                 perror_msg_and_skip("sigaction");
98         puts("sigaction(" SIG_STR ", NULL, NULL) = 0");
99
100         k_sigaction(signo, 0, 0);
101         puts("sigaction(" SIG_STR ", NULL, NULL) = 0");
102
103         k_sigaction(signo, (uintptr_t) (new_act + 1), 0);
104         printf("sigaction(" SIG_STR ", %p, NULL) = -1 EFAULT (%m)\n",
105                new_act + 1);
106
107         k_sigaction(signo, (uintptr_t) new_act + 2, 0);
108         printf("sigaction(" SIG_STR ", %#lx, NULL) = -1 EFAULT (%m)\n",
109                (unsigned long) new_act + 2);
110
111         k_sigaction(signo, 0, (uintptr_t) (old_act + 1));
112         printf("sigaction(" SIG_STR ", NULL, %p) = -1 EFAULT (%m)\n",
113                old_act + 1);
114
115         k_sigaction(signo, 0, (uintptr_t) old_act + 2);
116         printf("sigaction(" SIG_STR ", NULL, %#lx) = -1 EFAULT (%m)\n",
117                (unsigned long) old_act + 2);
118
119         k_sigaction(addr, 0, 0);
120         printf("sigaction(%d, NULL, NULL) = -1 EINVAL (%m)\n", ADDR_INT);
121
122         memset(new_act, 0, sizeof(*new_act));
123
124         k_sigaction(signo, (uintptr_t) new_act, 0);
125         puts("sigaction(" SIG_STR ", {sa_handler=SIG_DFL, sa_mask=[]"
126              ", sa_flags=0}, NULL) = 0");
127
128         sigemptyset(mask.libc);
129         sigaddset(mask.libc, SIGHUP);
130         sigaddset(mask.libc, SIGINT);
131
132         new_act->handler = (uintptr_t) SIG_IGN;
133         memcpy(new_act->mask, mask.old, sizeof(mask.old));
134         new_act->flags = SA_SIGINFO;
135
136         k_sigaction(signo, (uintptr_t) new_act, (uintptr_t) old_act);
137         puts("sigaction(" SIG_STR ", {sa_handler=SIG_IGN, sa_mask=[HUP INT]"
138              ", sa_flags=SA_SIGINFO}, {sa_handler=SIG_DFL, sa_mask=[]"
139              ", sa_flags=0}) = 0");
140
141         sigemptyset(mask.libc);
142         sigaddset(mask.libc, SIGQUIT);
143         sigaddset(mask.libc, SIGTERM);
144
145         new_act->handler = (unsigned long) addr;
146         memcpy(new_act->mask, mask.old, sizeof(mask.old));
147         new_act->flags = SA_ONSTACK | SA_RESTART;
148
149         k_sigaction(signo, (uintptr_t) new_act, (uintptr_t) old_act);
150         printf("sigaction(" SIG_STR ", {sa_handler=%#lx, sa_mask=[QUIT TERM]"
151                ", sa_flags=SA_ONSTACK|SA_RESTART}, {sa_handler=SIG_IGN"
152                ", sa_mask=[HUP INT], sa_flags=SA_SIGINFO}) = 0\n",
153                new_act->handler);
154
155         memset(mask.old, -1, sizeof(mask.old));
156         sigdelset(mask.libc, SIGHUP);
157
158         memcpy(new_act->mask, mask.old, sizeof(mask.old));
159 #ifdef SA_RESTORER
160         new_act->flags = SA_RESTORER;
161         new_act->restorer = (unsigned long) 0xdeadfacecafef00dULL;
162 # define SA_RESTORER_FMT ", sa_flags=SA_RESTORER, sa_restorer=%#lx"
163 # define SA_RESTORER_ARGS , new_act->restorer
164 #else
165         new_act->flags = SA_NODEFER;
166 # define SA_RESTORER_FMT ", sa_flags=SA_NODEFER"
167 # define SA_RESTORER_ARGS
168 #endif
169
170         k_sigaction(signo, (uintptr_t) new_act, (uintptr_t) old_act);
171         printf("sigaction(" SIG_STR ", {sa_handler=%#lx, sa_mask=~[HUP]"
172                SA_RESTORER_FMT "}, {sa_handler=%#lx, sa_mask=[QUIT TERM]"
173                ", sa_flags=SA_ONSTACK|SA_RESTART}) = 0\n",
174                new_act->handler SA_RESTORER_ARGS,
175                new_act->handler);
176
177         puts("+++ exited with 0 +++");
178         return 0;
179 }
180
181 #else
182
183 SKIP_MAIN_UNDEFINED("__NR_sigaction")
184
185 #endif