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