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