]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _LINUX_SIGNAL_H |
3 | #define _LINUX_SIGNAL_H | |
4 | ||
1c3bea0e | 5 | #include <linux/bug.h> |
e6d930b4 | 6 | #include <linux/signal_types.h> |
7994200c | 7 | #include <linux/string.h> |
1da177e4 | 8 | |
1477fcc2 SR |
9 | struct task_struct; |
10 | ||
d33ed52d DY |
11 | /* for sysctl */ |
12 | extern int print_fatal_signals; | |
1da177e4 | 13 | |
ca9eb49a JH |
14 | static inline void copy_siginfo(struct siginfo *to, struct siginfo *from) |
15 | { | |
16 | if (from->si_code < 0) | |
17 | memcpy(to, from, sizeof(*to)); | |
18 | else | |
19 | /* _sigchld is currently the largest know union member */ | |
20 | memcpy(to, from, __ARCH_SI_PREAMBLE_SIZE + sizeof(from->_sifields._sigchld)); | |
21 | } | |
22 | ||
b9253a43 CH |
23 | int copy_siginfo_to_user(struct siginfo __user *to, const struct siginfo *from); |
24 | ||
cc731525 EB |
25 | enum siginfo_layout { |
26 | SIL_KILL, | |
27 | SIL_TIMER, | |
28 | SIL_POLL, | |
29 | SIL_FAULT, | |
30 | SIL_CHLD, | |
31 | SIL_RT, | |
32 | #ifdef __ARCH_SIGSYS | |
33 | SIL_SYS, | |
34 | #endif | |
35 | }; | |
36 | ||
9cbc4a39 | 37 | enum siginfo_layout siginfo_layout(unsigned sig, int si_code); |
cc731525 | 38 | |
1da177e4 LT |
39 | /* |
40 | * Define some primitives to manipulate sigset_t. | |
41 | */ | |
42 | ||
43 | #ifndef __HAVE_ARCH_SIG_BITOPS | |
44 | #include <linux/bitops.h> | |
45 | ||
46 | /* We don't use <linux/bitops.h> for these because there is no need to | |
47 | be atomic. */ | |
48 | static inline void sigaddset(sigset_t *set, int _sig) | |
49 | { | |
50 | unsigned long sig = _sig - 1; | |
51 | if (_NSIG_WORDS == 1) | |
52 | set->sig[0] |= 1UL << sig; | |
53 | else | |
54 | set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW); | |
55 | } | |
56 | ||
57 | static inline void sigdelset(sigset_t *set, int _sig) | |
58 | { | |
59 | unsigned long sig = _sig - 1; | |
60 | if (_NSIG_WORDS == 1) | |
61 | set->sig[0] &= ~(1UL << sig); | |
62 | else | |
63 | set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW)); | |
64 | } | |
65 | ||
66 | static inline int sigismember(sigset_t *set, int _sig) | |
67 | { | |
68 | unsigned long sig = _sig - 1; | |
69 | if (_NSIG_WORDS == 1) | |
70 | return 1 & (set->sig[0] >> sig); | |
71 | else | |
72 | return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW)); | |
73 | } | |
74 | ||
1da177e4 LT |
75 | #endif /* __HAVE_ARCH_SIG_BITOPS */ |
76 | ||
71fabd5e GA |
77 | static inline int sigisemptyset(sigset_t *set) |
78 | { | |
71fabd5e GA |
79 | switch (_NSIG_WORDS) { |
80 | case 4: | |
81 | return (set->sig[3] | set->sig[2] | | |
82 | set->sig[1] | set->sig[0]) == 0; | |
83 | case 2: | |
84 | return (set->sig[1] | set->sig[0]) == 0; | |
85 | case 1: | |
86 | return set->sig[0] == 0; | |
87 | default: | |
1c3bea0e | 88 | BUILD_BUG(); |
71fabd5e GA |
89 | return 0; |
90 | } | |
91 | } | |
92 | ||
c7be96af WL |
93 | static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2) |
94 | { | |
95 | switch (_NSIG_WORDS) { | |
96 | case 4: | |
97 | return (set1->sig[3] == set2->sig[3]) && | |
98 | (set1->sig[2] == set2->sig[2]) && | |
99 | (set1->sig[1] == set2->sig[1]) && | |
100 | (set1->sig[0] == set2->sig[0]); | |
101 | case 2: | |
102 | return (set1->sig[1] == set2->sig[1]) && | |
103 | (set1->sig[0] == set2->sig[0]); | |
104 | case 1: | |
105 | return set1->sig[0] == set2->sig[0]; | |
106 | } | |
107 | return 0; | |
108 | } | |
109 | ||
1da177e4 LT |
110 | #define sigmask(sig) (1UL << ((sig) - 1)) |
111 | ||
112 | #ifndef __HAVE_ARCH_SIG_SETOPS | |
113 | #include <linux/string.h> | |
114 | ||
115 | #define _SIG_SET_BINOP(name, op) \ | |
116 | static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \ | |
117 | { \ | |
1da177e4 LT |
118 | unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \ |
119 | \ | |
120 | switch (_NSIG_WORDS) { \ | |
1c3bea0e | 121 | case 4: \ |
1da177e4 LT |
122 | a3 = a->sig[3]; a2 = a->sig[2]; \ |
123 | b3 = b->sig[3]; b2 = b->sig[2]; \ | |
124 | r->sig[3] = op(a3, b3); \ | |
125 | r->sig[2] = op(a2, b2); \ | |
1c3bea0e | 126 | case 2: \ |
1da177e4 LT |
127 | a1 = a->sig[1]; b1 = b->sig[1]; \ |
128 | r->sig[1] = op(a1, b1); \ | |
1c3bea0e | 129 | case 1: \ |
1da177e4 LT |
130 | a0 = a->sig[0]; b0 = b->sig[0]; \ |
131 | r->sig[0] = op(a0, b0); \ | |
132 | break; \ | |
1c3bea0e ON |
133 | default: \ |
134 | BUILD_BUG(); \ | |
1da177e4 LT |
135 | } \ |
136 | } | |
137 | ||
138 | #define _sig_or(x,y) ((x) | (y)) | |
139 | _SIG_SET_BINOP(sigorsets, _sig_or) | |
140 | ||
141 | #define _sig_and(x,y) ((x) & (y)) | |
142 | _SIG_SET_BINOP(sigandsets, _sig_and) | |
143 | ||
702a5073 ON |
144 | #define _sig_andn(x,y) ((x) & ~(y)) |
145 | _SIG_SET_BINOP(sigandnsets, _sig_andn) | |
1da177e4 LT |
146 | |
147 | #undef _SIG_SET_BINOP | |
148 | #undef _sig_or | |
149 | #undef _sig_and | |
702a5073 | 150 | #undef _sig_andn |
1da177e4 LT |
151 | |
152 | #define _SIG_SET_OP(name, op) \ | |
153 | static inline void name(sigset_t *set) \ | |
154 | { \ | |
1da177e4 | 155 | switch (_NSIG_WORDS) { \ |
1c3bea0e ON |
156 | case 4: set->sig[3] = op(set->sig[3]); \ |
157 | set->sig[2] = op(set->sig[2]); \ | |
158 | case 2: set->sig[1] = op(set->sig[1]); \ | |
159 | case 1: set->sig[0] = op(set->sig[0]); \ | |
1da177e4 | 160 | break; \ |
1c3bea0e ON |
161 | default: \ |
162 | BUILD_BUG(); \ | |
1da177e4 LT |
163 | } \ |
164 | } | |
165 | ||
166 | #define _sig_not(x) (~(x)) | |
167 | _SIG_SET_OP(signotset, _sig_not) | |
168 | ||
169 | #undef _SIG_SET_OP | |
170 | #undef _sig_not | |
171 | ||
172 | static inline void sigemptyset(sigset_t *set) | |
173 | { | |
174 | switch (_NSIG_WORDS) { | |
175 | default: | |
176 | memset(set, 0, sizeof(sigset_t)); | |
177 | break; | |
178 | case 2: set->sig[1] = 0; | |
179 | case 1: set->sig[0] = 0; | |
180 | break; | |
181 | } | |
182 | } | |
183 | ||
184 | static inline void sigfillset(sigset_t *set) | |
185 | { | |
186 | switch (_NSIG_WORDS) { | |
187 | default: | |
188 | memset(set, -1, sizeof(sigset_t)); | |
189 | break; | |
190 | case 2: set->sig[1] = -1; | |
191 | case 1: set->sig[0] = -1; | |
192 | break; | |
193 | } | |
194 | } | |
195 | ||
196 | /* Some extensions for manipulating the low 32 signals in particular. */ | |
197 | ||
198 | static inline void sigaddsetmask(sigset_t *set, unsigned long mask) | |
199 | { | |
200 | set->sig[0] |= mask; | |
201 | } | |
202 | ||
203 | static inline void sigdelsetmask(sigset_t *set, unsigned long mask) | |
204 | { | |
205 | set->sig[0] &= ~mask; | |
206 | } | |
207 | ||
208 | static inline int sigtestsetmask(sigset_t *set, unsigned long mask) | |
209 | { | |
210 | return (set->sig[0] & mask) != 0; | |
211 | } | |
212 | ||
213 | static inline void siginitset(sigset_t *set, unsigned long mask) | |
214 | { | |
215 | set->sig[0] = mask; | |
216 | switch (_NSIG_WORDS) { | |
217 | default: | |
218 | memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1)); | |
219 | break; | |
220 | case 2: set->sig[1] = 0; | |
221 | case 1: ; | |
222 | } | |
223 | } | |
224 | ||
225 | static inline void siginitsetinv(sigset_t *set, unsigned long mask) | |
226 | { | |
227 | set->sig[0] = ~mask; | |
228 | switch (_NSIG_WORDS) { | |
229 | default: | |
230 | memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1)); | |
231 | break; | |
232 | case 2: set->sig[1] = -1; | |
233 | case 1: ; | |
234 | } | |
235 | } | |
236 | ||
237 | #endif /* __HAVE_ARCH_SIG_SETOPS */ | |
238 | ||
239 | static inline void init_sigpending(struct sigpending *sig) | |
240 | { | |
241 | sigemptyset(&sig->signal); | |
242 | INIT_LIST_HEAD(&sig->list); | |
243 | } | |
244 | ||
6a14c5c9 ON |
245 | extern void flush_sigqueue(struct sigpending *queue); |
246 | ||
e5bdd883 JJ |
247 | /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */ |
248 | static inline int valid_signal(unsigned long sig) | |
249 | { | |
250 | return sig <= _NSIG ? 1 : 0; | |
251 | } | |
252 | ||
b2b07e4f ON |
253 | struct timespec; |
254 | struct pt_regs; | |
255 | ||
fba2afaa | 256 | extern int next_signal(struct sigpending *pending, sigset_t *mask); |
4a30debf ON |
257 | extern int do_send_sig_info(int sig, struct siginfo *info, |
258 | struct task_struct *p, bool group); | |
1da177e4 LT |
259 | extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p); |
260 | extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *); | |
1da177e4 | 261 | extern int sigprocmask(int, sigset_t *, sigset_t *); |
77097ae5 AV |
262 | extern void set_current_blocked(sigset_t *); |
263 | extern void __set_current_blocked(const sigset_t *); | |
abd4f750 | 264 | extern int show_unhandled_signals; |
1da177e4 | 265 | |
828b1f65 | 266 | extern int get_signal(struct ksignal *ksig); |
2ce5da17 | 267 | extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping); |
d12619b5 | 268 | extern void exit_signals(struct task_struct *tsk); |
b4e74264 ON |
269 | extern void kernel_sigaction(int, __sighandler_t); |
270 | ||
271 | static inline void allow_signal(int sig) | |
272 | { | |
273 | /* | |
274 | * Kernel threads handle their own signals. Let the signal code | |
275 | * know it'll be handled, so that they don't get converted to | |
276 | * SIGKILL or just silently dropped. | |
277 | */ | |
278 | kernel_sigaction(sig, (__force __sighandler_t)2); | |
279 | } | |
280 | ||
281 | static inline void disallow_signal(int sig) | |
282 | { | |
283 | kernel_sigaction(sig, SIG_IGN); | |
284 | } | |
1da177e4 | 285 | |
298ec1e2 CL |
286 | extern struct kmem_cache *sighand_cachep; |
287 | ||
abd4f750 MAS |
288 | int unhandled_signal(struct task_struct *tsk, int sig); |
289 | ||
55c0d1f8 RM |
290 | /* |
291 | * In POSIX a signal is sent either to a specific thread (Linux task) | |
292 | * or to the process as a whole (Linux thread group). How the signal | |
293 | * is sent determines whether it's to one thread or the whole group, | |
294 | * which determines which signal mask(s) are involved in blocking it | |
295 | * from being delivered until later. When the signal is delivered, | |
296 | * either it's caught or ignored by a user handler or it has a default | |
297 | * effect that applies to the whole thread group (POSIX process). | |
298 | * | |
299 | * The possible effects an unblocked signal set to SIG_DFL can have are: | |
300 | * ignore - Nothing Happens | |
301 | * terminate - kill the process, i.e. all threads in the group, | |
302 | * similar to exit_group. The group leader (only) reports | |
303 | * WIFSIGNALED status to its parent. | |
304 | * coredump - write a core dump file describing all threads using | |
305 | * the same mm and then kill all those threads | |
306 | * stop - stop all the threads in the group, i.e. TASK_STOPPED state | |
307 | * | |
308 | * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. | |
309 | * Other signals when not blocked and set to SIG_DFL behaves as follows. | |
310 | * The job control signals also have other special effects. | |
311 | * | |
312 | * +--------------------+------------------+ | |
313 | * | POSIX signal | default action | | |
314 | * +--------------------+------------------+ | |
315 | * | SIGHUP | terminate | | |
316 | * | SIGINT | terminate | | |
317 | * | SIGQUIT | coredump | | |
318 | * | SIGILL | coredump | | |
319 | * | SIGTRAP | coredump | | |
320 | * | SIGABRT/SIGIOT | coredump | | |
321 | * | SIGBUS | coredump | | |
322 | * | SIGFPE | coredump | | |
323 | * | SIGKILL | terminate(+) | | |
324 | * | SIGUSR1 | terminate | | |
325 | * | SIGSEGV | coredump | | |
326 | * | SIGUSR2 | terminate | | |
327 | * | SIGPIPE | terminate | | |
328 | * | SIGALRM | terminate | | |
329 | * | SIGTERM | terminate | | |
330 | * | SIGCHLD | ignore | | |
331 | * | SIGCONT | ignore(*) | | |
332 | * | SIGSTOP | stop(*)(+) | | |
333 | * | SIGTSTP | stop(*) | | |
334 | * | SIGTTIN | stop(*) | | |
335 | * | SIGTTOU | stop(*) | | |
336 | * | SIGURG | ignore | | |
337 | * | SIGXCPU | coredump | | |
338 | * | SIGXFSZ | coredump | | |
339 | * | SIGVTALRM | terminate | | |
340 | * | SIGPROF | terminate | | |
341 | * | SIGPOLL/SIGIO | terminate | | |
342 | * | SIGSYS/SIGUNUSED | coredump | | |
343 | * | SIGSTKFLT | terminate | | |
344 | * | SIGWINCH | ignore | | |
345 | * | SIGPWR | terminate | | |
346 | * | SIGRTMIN-SIGRTMAX | terminate | | |
347 | * +--------------------+------------------+ | |
348 | * | non-POSIX signal | default action | | |
349 | * +--------------------+------------------+ | |
350 | * | SIGEMT | coredump | | |
351 | * +--------------------+------------------+ | |
352 | * | |
353 | * (+) For SIGKILL and SIGSTOP the action is "always", not just "default". | |
354 | * (*) Special job control effects: | |
355 | * When SIGCONT is sent, it resumes the process (all threads in the group) | |
356 | * from TASK_STOPPED state and also clears any pending/queued stop signals | |
357 | * (any of those marked with "stop(*)"). This happens regardless of blocking, | |
358 | * catching, or ignoring SIGCONT. When any stop signal is sent, it clears | |
359 | * any pending/queued SIGCONT signals; this happens regardless of blocking, | |
360 | * catching, or ignored the stop signal, though (except for SIGSTOP) the | |
361 | * default action of stopping the process may happen later or never. | |
362 | */ | |
363 | ||
364 | #ifdef SIGEMT | |
365 | #define SIGEMT_MASK rt_sigmask(SIGEMT) | |
366 | #else | |
367 | #define SIGEMT_MASK 0 | |
368 | #endif | |
369 | ||
370 | #if SIGRTMIN > BITS_PER_LONG | |
371 | #define rt_sigmask(sig) (1ULL << ((sig)-1)) | |
372 | #else | |
373 | #define rt_sigmask(sig) sigmask(sig) | |
374 | #endif | |
5c8ccefd ON |
375 | |
376 | #define siginmask(sig, mask) \ | |
377 | ((sig) < SIGRTMIN && (rt_sigmask(sig) & (mask))) | |
55c0d1f8 RM |
378 | |
379 | #define SIG_KERNEL_ONLY_MASK (\ | |
380 | rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP)) | |
381 | ||
382 | #define SIG_KERNEL_STOP_MASK (\ | |
383 | rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \ | |
384 | rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) ) | |
385 | ||
386 | #define SIG_KERNEL_COREDUMP_MASK (\ | |
387 | rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \ | |
388 | rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \ | |
389 | rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \ | |
390 | rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \ | |
391 | rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \ | |
392 | SIGEMT_MASK ) | |
393 | ||
394 | #define SIG_KERNEL_IGNORE_MASK (\ | |
395 | rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \ | |
396 | rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) ) | |
397 | ||
d08477aa EB |
398 | #define SIG_SPECIFIC_SICODES_MASK (\ |
399 | rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \ | |
400 | rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \ | |
401 | rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \ | |
402 | rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \ | |
403 | SIGEMT_MASK ) | |
404 | ||
5c8ccefd ON |
405 | #define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK) |
406 | #define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK) | |
407 | #define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK) | |
408 | #define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK) | |
d08477aa | 409 | #define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK) |
55c0d1f8 | 410 | |
55c0d1f8 RM |
411 | #define sig_fatal(t, signr) \ |
412 | (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \ | |
413 | (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL) | |
414 | ||
a1c9eea9 AB |
415 | void signals_init(void); |
416 | ||
5c49574f | 417 | int restore_altstack(const stack_t __user *); |
c40702c4 | 418 | int __save_altstack(stack_t __user *, unsigned long); |
5c49574f | 419 | |
bd1c149a AV |
420 | #define save_altstack_ex(uss, sp) do { \ |
421 | stack_t __user *__uss = uss; \ | |
422 | struct task_struct *t = current; \ | |
423 | put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \ | |
2a742138 | 424 | put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \ |
bd1c149a | 425 | put_user_ex(t->sas_ss_size, &__uss->ss_size); \ |
2a742138 SS |
426 | if (t->sas_ss_flags & SS_AUTODISARM) \ |
427 | sas_ss_reset(t); \ | |
bd1c149a AV |
428 | } while (0); |
429 | ||
34db8aaf DH |
430 | #ifdef CONFIG_PROC_FS |
431 | struct seq_file; | |
432 | extern void render_sigset_t(struct seq_file *, const char *, sigset_t *); | |
433 | #endif | |
434 | ||
1da177e4 | 435 | #endif /* _LINUX_SIGNAL_H */ |