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