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