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