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