]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <threads.h> | |
4 | #include <unistd.h> | |
5 | ||
6 | #include "errno-util.h" | |
7 | #include "missing_syscall.h" | |
8 | #include "parse-util.h" | |
9 | #include "signal-util.h" | |
10 | #include "stdio-util.h" | |
11 | #include "string-table.h" | |
12 | #include "string-util.h" | |
13 | ||
14 | int reset_all_signal_handlers(void) { | |
15 | int ret = 0, r; | |
16 | ||
17 | for (int sig = 1; sig < _NSIG; sig++) { | |
18 | ||
19 | /* These two cannot be caught... */ | |
20 | if (IN_SET(sig, SIGKILL, SIGSTOP)) | |
21 | continue; | |
22 | ||
23 | /* On Linux the first two RT signals are reserved by glibc, and sigaction() will return | |
24 | * EINVAL for them. */ | |
25 | r = RET_NERRNO(sigaction(sig, &sigaction_default, NULL)); | |
26 | if (r != -EINVAL) | |
27 | RET_GATHER(ret, r); | |
28 | } | |
29 | ||
30 | return ret; | |
31 | } | |
32 | ||
33 | int reset_signal_mask(void) { | |
34 | sigset_t ss; | |
35 | ||
36 | if (sigemptyset(&ss) < 0) | |
37 | return -errno; | |
38 | ||
39 | return RET_NERRNO(sigprocmask(SIG_SETMASK, &ss, NULL)); | |
40 | } | |
41 | ||
42 | int sigaction_many_internal(const struct sigaction *sa, ...) { | |
43 | int sig, r = 0; | |
44 | va_list ap; | |
45 | ||
46 | va_start(ap, sa); | |
47 | ||
48 | /* negative signal ends the list. 0 signal is skipped. */ | |
49 | while ((sig = va_arg(ap, int)) >= 0) { | |
50 | ||
51 | if (sig == 0) | |
52 | continue; | |
53 | ||
54 | RET_GATHER(r, RET_NERRNO(sigaction(sig, sa, NULL))); | |
55 | } | |
56 | ||
57 | va_end(ap); | |
58 | ||
59 | return r; | |
60 | } | |
61 | ||
62 | static int sigset_add_many_ap(sigset_t *ss, va_list ap) { | |
63 | int sig, r = 0; | |
64 | ||
65 | assert(ss); | |
66 | ||
67 | while ((sig = va_arg(ap, int)) >= 0) { | |
68 | ||
69 | if (sig == 0) | |
70 | continue; | |
71 | ||
72 | if (sigaddset(ss, sig) < 0) { | |
73 | if (r >= 0) | |
74 | r = -errno; | |
75 | } | |
76 | } | |
77 | ||
78 | return r; | |
79 | } | |
80 | ||
81 | int sigset_add_many_internal(sigset_t *ss, ...) { | |
82 | va_list ap; | |
83 | int r; | |
84 | ||
85 | va_start(ap, ss); | |
86 | r = sigset_add_many_ap(ss, ap); | |
87 | va_end(ap); | |
88 | ||
89 | return r; | |
90 | } | |
91 | ||
92 | int sigprocmask_many_internal(int how, sigset_t *old, ...) { | |
93 | va_list ap; | |
94 | sigset_t ss; | |
95 | int r; | |
96 | ||
97 | if (sigemptyset(&ss) < 0) | |
98 | return -errno; | |
99 | ||
100 | va_start(ap, old); | |
101 | r = sigset_add_many_ap(&ss, ap); | |
102 | va_end(ap); | |
103 | ||
104 | if (r < 0) | |
105 | return r; | |
106 | ||
107 | return RET_NERRNO(sigprocmask(how, &ss, old)); | |
108 | } | |
109 | ||
110 | static const char *const static_signal_table[] = { | |
111 | [SIGHUP] = "HUP", | |
112 | [SIGINT] = "INT", | |
113 | [SIGQUIT] = "QUIT", | |
114 | [SIGILL] = "ILL", | |
115 | [SIGTRAP] = "TRAP", | |
116 | [SIGABRT] = "ABRT", | |
117 | [SIGBUS] = "BUS", | |
118 | [SIGFPE] = "FPE", | |
119 | [SIGKILL] = "KILL", | |
120 | [SIGUSR1] = "USR1", | |
121 | [SIGSEGV] = "SEGV", | |
122 | [SIGUSR2] = "USR2", | |
123 | [SIGPIPE] = "PIPE", | |
124 | [SIGALRM] = "ALRM", | |
125 | [SIGTERM] = "TERM", | |
126 | #ifdef SIGSTKFLT | |
127 | [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */ | |
128 | #endif | |
129 | [SIGCHLD] = "CHLD", | |
130 | [SIGCONT] = "CONT", | |
131 | [SIGSTOP] = "STOP", | |
132 | [SIGTSTP] = "TSTP", | |
133 | [SIGTTIN] = "TTIN", | |
134 | [SIGTTOU] = "TTOU", | |
135 | [SIGURG] = "URG", | |
136 | [SIGXCPU] = "XCPU", | |
137 | [SIGXFSZ] = "XFSZ", | |
138 | [SIGVTALRM] = "VTALRM", | |
139 | [SIGPROF] = "PROF", | |
140 | [SIGWINCH] = "WINCH", | |
141 | [SIGIO] = "IO", | |
142 | [SIGPWR] = "PWR", | |
143 | [SIGSYS] = "SYS" | |
144 | }; | |
145 | ||
146 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(static_signal, int); | |
147 | ||
148 | const char* signal_to_string(int signo) { | |
149 | static thread_local char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int)]; | |
150 | const char *name; | |
151 | ||
152 | name = static_signal_to_string(signo); | |
153 | if (name) | |
154 | return name; | |
155 | ||
156 | if (signo >= SIGRTMIN && signo <= SIGRTMAX) | |
157 | xsprintf(buf, "RTMIN+%d", signo - SIGRTMIN); | |
158 | else | |
159 | xsprintf(buf, "%d", signo); | |
160 | ||
161 | return buf; | |
162 | } | |
163 | ||
164 | int signal_from_string(const char *s) { | |
165 | const char *p; | |
166 | int signo, r; | |
167 | ||
168 | /* Check that the input is a signal number. */ | |
169 | if (safe_atoi(s, &signo) >= 0) { | |
170 | if (SIGNAL_VALID(signo)) | |
171 | return signo; | |
172 | else | |
173 | return -ERANGE; | |
174 | } | |
175 | ||
176 | /* Drop "SIG" prefix. */ | |
177 | if (startswith(s, "SIG")) | |
178 | s += 3; | |
179 | ||
180 | /* Check that the input is a signal name. */ | |
181 | signo = static_signal_from_string(s); | |
182 | if (signo > 0) | |
183 | return signo; | |
184 | ||
185 | /* Check that the input is RTMIN or | |
186 | * RTMIN+n (0 <= n <= SIGRTMAX-SIGRTMIN). */ | |
187 | p = startswith(s, "RTMIN"); | |
188 | if (p) { | |
189 | if (*p == '\0') | |
190 | return SIGRTMIN; | |
191 | if (*p != '+') | |
192 | return -EINVAL; | |
193 | ||
194 | r = safe_atoi(p, &signo); | |
195 | if (r < 0) | |
196 | return r; | |
197 | ||
198 | if (signo < 0 || signo > SIGRTMAX - SIGRTMIN) | |
199 | return -ERANGE; | |
200 | ||
201 | return signo + SIGRTMIN; | |
202 | } | |
203 | ||
204 | /* Check that the input is RTMAX or | |
205 | * RTMAX-n (0 <= n <= SIGRTMAX-SIGRTMIN). */ | |
206 | p = startswith(s, "RTMAX"); | |
207 | if (p) { | |
208 | if (*p == '\0') | |
209 | return SIGRTMAX; | |
210 | if (*p != '-') | |
211 | return -EINVAL; | |
212 | ||
213 | r = safe_atoi(p, &signo); | |
214 | if (r < 0) | |
215 | return r; | |
216 | ||
217 | if (signo > 0 || signo < SIGRTMIN - SIGRTMAX) | |
218 | return -ERANGE; | |
219 | ||
220 | return signo + SIGRTMAX; | |
221 | } | |
222 | ||
223 | return -EINVAL; | |
224 | } | |
225 | ||
226 | void nop_signal_handler(int sig) { | |
227 | /* nothing here */ | |
228 | } | |
229 | ||
230 | int signal_is_blocked(int sig) { | |
231 | sigset_t ss; | |
232 | int r; | |
233 | ||
234 | r = pthread_sigmask(SIG_SETMASK, NULL, &ss); | |
235 | if (r != 0) | |
236 | return -r; | |
237 | ||
238 | return RET_NERRNO(sigismember(&ss, sig)); | |
239 | } | |
240 | ||
241 | int pop_pending_signal_internal(int sig, ...) { | |
242 | sigset_t ss; | |
243 | va_list ap; | |
244 | int r; | |
245 | ||
246 | if (sig < 0) /* Empty list? */ | |
247 | return -EINVAL; | |
248 | ||
249 | if (sigemptyset(&ss) < 0) | |
250 | return -errno; | |
251 | ||
252 | /* Add first signal (if the signal is zero, we'll silently skip it, to make it easier to build | |
253 | * parameter lists where some element are sometimes off, similar to how sigset_add_many_ap() handles | |
254 | * this.) */ | |
255 | if (sig > 0 && sigaddset(&ss, sig) < 0) | |
256 | return -errno; | |
257 | ||
258 | /* Add all other signals */ | |
259 | va_start(ap, sig); | |
260 | r = sigset_add_many_ap(&ss, ap); | |
261 | va_end(ap); | |
262 | if (r < 0) | |
263 | return r; | |
264 | ||
265 | r = sigtimedwait(&ss, NULL, &(const struct timespec) {}); | |
266 | if (r < 0) { | |
267 | if (errno == EAGAIN) | |
268 | return 0; | |
269 | ||
270 | return -errno; | |
271 | } | |
272 | ||
273 | return r; /* Returns the signal popped */ | |
274 | } | |
275 | ||
276 | void propagate_signal(int sig, siginfo_t *siginfo) { | |
277 | pid_t p; | |
278 | ||
279 | /* To be called from a signal handler. Will raise the same signal again, in our process + in our threads. | |
280 | * | |
281 | * Note that we use getpid() instead of getpid_cached(). We might have forked with raw_clone() | |
282 | * earlier (see PID 1), and hence let's go to the raw syscall here. In particular as this is not | |
283 | * performance sensitive code. | |
284 | * | |
285 | * Note that we use kill() rather than raise() as fallback, for similar reasons. */ | |
286 | ||
287 | p = getpid(); | |
288 | ||
289 | if (rt_tgsigqueueinfo(p, gettid(), sig, siginfo) < 0) | |
290 | assert_se(kill(p, sig) >= 0); | |
291 | } | |
292 | ||
293 | const struct sigaction sigaction_ignore = { | |
294 | .sa_handler = SIG_IGN, | |
295 | .sa_flags = SA_RESTART, | |
296 | }; | |
297 | ||
298 | const struct sigaction sigaction_default = { | |
299 | .sa_handler = SIG_DFL, | |
300 | .sa_flags = SA_RESTART, | |
301 | }; | |
302 | ||
303 | const struct sigaction sigaction_nop_nocldstop = { | |
304 | .sa_handler = nop_signal_handler, | |
305 | .sa_flags = SA_NOCLDSTOP|SA_RESTART, | |
306 | }; | |
307 | ||
308 | int parse_signo(const char *s, int *ret) { | |
309 | int sig, r; | |
310 | ||
311 | r = safe_atoi(s, &sig); | |
312 | if (r < 0) | |
313 | return r; | |
314 | ||
315 | if (!SIGNAL_VALID(sig)) | |
316 | return -EINVAL; | |
317 | ||
318 | if (ret) | |
319 | *ret = sig; | |
320 | ||
321 | return 0; | |
322 | } |