blob: 30e7ceed3ef62d35b4c0d1ca4191d2f373ab80b7 [file] [log] [blame]
Ingo Molnar3f07c012017-02-08 18:51:30 +01001#ifndef _LINUX_SCHED_SIGNAL_H
2#define _LINUX_SCHED_SIGNAL_H
3
Ingo Molnarb2d09102017-02-04 01:27:20 +01004#include <linux/rculist.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01005#include <linux/signal.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01006#include <linux/cred.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +01007#include <linux/sched.h>
Ingo Molnar1e4bae62017-02-08 18:51:32 +01008#include <linux/sched/jobctl.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +01009#include <linux/sched/task.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010
Ingo Molnarc3edc402017-02-02 08:35:14 +010011/*
12 * Types defining task->signal and task->sighand and APIs using them:
13 */
14
15struct sighand_struct {
16 atomic_t count;
17 struct k_sigaction action[_NSIG];
18 spinlock_t siglock;
19 wait_queue_head_t signalfd_wqh;
20};
21
22/*
Ingo Molnar8d884602017-02-02 12:06:10 +010023 * Per-process accounting stats:
24 */
25struct pacct_struct {
26 int ac_flag;
27 long ac_exitcode;
28 unsigned long ac_mem;
29 u64 ac_utime, ac_stime;
30 unsigned long ac_minflt, ac_majflt;
31};
32
33struct cpu_itimer {
34 u64 expires;
35 u64 incr;
36};
37
38/*
Ingo Molnarc3edc402017-02-02 08:35:14 +010039 * NOTE! "signal_struct" does not have its own
40 * locking, because a shared signal_struct always
41 * implies a shared sighand_struct, so locking
42 * sighand_struct is always a proper superset of
43 * the locking of signal_struct.
44 */
45struct signal_struct {
46 atomic_t sigcnt;
47 atomic_t live;
48 int nr_threads;
49 struct list_head thread_head;
50
51 wait_queue_head_t wait_chldexit; /* for wait4() */
52
53 /* current thread group signal load-balancing target: */
54 struct task_struct *curr_target;
55
56 /* shared signal handling: */
57 struct sigpending shared_pending;
58
59 /* thread group exit support */
60 int group_exit_code;
61 /* overloaded:
62 * - notify group_exit_task when ->count is equal to notify_count
63 * - everyone except group_exit_task is stopped during signal delivery
64 * of fatal signals, group_exit_task processes the signal.
65 */
66 int notify_count;
67 struct task_struct *group_exit_task;
68
69 /* thread group stop support, overloads group_exit_code too */
70 int group_stop_count;
71 unsigned int flags; /* see SIGNAL_* flags below */
72
73 /*
74 * PR_SET_CHILD_SUBREAPER marks a process, like a service
75 * manager, to re-parent orphan (double-forking) child processes
76 * to this process instead of 'init'. The service manager is
77 * able to receive SIGCHLD signals and is able to investigate
78 * the process until it calls wait(). All children of this
79 * process will inherit a flag if they should look for a
80 * child_subreaper process at exit.
81 */
82 unsigned int is_child_subreaper:1;
83 unsigned int has_child_subreaper:1;
84
85#ifdef CONFIG_POSIX_TIMERS
86
87 /* POSIX.1b Interval Timers */
88 int posix_timer_id;
89 struct list_head posix_timers;
90
91 /* ITIMER_REAL timer for the process */
92 struct hrtimer real_timer;
93 ktime_t it_real_incr;
94
95 /*
96 * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
97 * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
98 * values are defined to 0 and 1 respectively
99 */
100 struct cpu_itimer it[2];
101
102 /*
103 * Thread group totals for process CPU timers.
104 * See thread_group_cputimer(), et al, for details.
105 */
106 struct thread_group_cputimer cputimer;
107
108 /* Earliest-expiration cache. */
109 struct task_cputime cputime_expires;
110
111 struct list_head cpu_timers[3];
112
113#endif
114
115 struct pid *leader_pid;
116
117#ifdef CONFIG_NO_HZ_FULL
118 atomic_t tick_dep_mask;
119#endif
120
121 struct pid *tty_old_pgrp;
122
123 /* boolean value for session group leader */
124 int leader;
125
126 struct tty_struct *tty; /* NULL if no tty */
127
128#ifdef CONFIG_SCHED_AUTOGROUP
129 struct autogroup *autogroup;
130#endif
131 /*
132 * Cumulative resource counters for dead threads in the group,
133 * and for reaped dead child processes forked by this group.
134 * Live threads maintain their own counters and add to these
135 * in __exit_signal, except for the group leader.
136 */
137 seqlock_t stats_lock;
138 u64 utime, stime, cutime, cstime;
139 u64 gtime;
140 u64 cgtime;
141 struct prev_cputime prev_cputime;
142 unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
143 unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
144 unsigned long inblock, oublock, cinblock, coublock;
145 unsigned long maxrss, cmaxrss;
146 struct task_io_accounting ioac;
147
148 /*
149 * Cumulative ns of schedule CPU time fo dead threads in the
150 * group, not including a zombie group leader, (This only differs
151 * from jiffies_to_ns(utime + stime) if sched_clock uses something
152 * other than jiffies.)
153 */
154 unsigned long long sum_sched_runtime;
155
156 /*
157 * We don't bother to synchronize most readers of this at all,
158 * because there is no reader checking a limit that actually needs
159 * to get both rlim_cur and rlim_max atomically, and either one
160 * alone is a single word that can safely be read normally.
161 * getrlimit/setrlimit use task_lock(current->group_leader) to
162 * protect this instead of the siglock, because they really
163 * have no need to disable irqs.
164 */
165 struct rlimit rlim[RLIM_NLIMITS];
166
167#ifdef CONFIG_BSD_PROCESS_ACCT
168 struct pacct_struct pacct; /* per-process accounting information */
169#endif
170#ifdef CONFIG_TASKSTATS
171 struct taskstats *stats;
172#endif
173#ifdef CONFIG_AUDIT
174 unsigned audit_tty;
175 struct tty_audit_buf *tty_audit_buf;
176#endif
177
178 /*
179 * Thread is the potential origin of an oom condition; kill first on
180 * oom
181 */
182 bool oom_flag_origin;
183 short oom_score_adj; /* OOM kill score adjustment */
184 short oom_score_adj_min; /* OOM kill score adjustment min value.
185 * Only settable by CAP_SYS_RESOURCE. */
186 struct mm_struct *oom_mm; /* recorded mm when the thread group got
187 * killed by the oom killer */
188
189 struct mutex cred_guard_mutex; /* guard against foreign influences on
190 * credential calculations
191 * (notably. ptrace) */
192};
193
194/*
195 * Bits in flags field of signal_struct.
196 */
197#define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */
198#define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */
199#define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */
200#define SIGNAL_GROUP_COREDUMP 0x00000008 /* coredump in progress */
201/*
202 * Pending notifications to parent.
203 */
204#define SIGNAL_CLD_STOPPED 0x00000010
205#define SIGNAL_CLD_CONTINUED 0x00000020
206#define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
207
208#define SIGNAL_UNKILLABLE 0x00000040 /* for init: ignore fatal signals */
209
210#define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
211 SIGNAL_STOP_CONTINUED)
212
213static inline void signal_set_stop_flags(struct signal_struct *sig,
214 unsigned int flags)
215{
216 WARN_ON(sig->flags & (SIGNAL_GROUP_EXIT|SIGNAL_GROUP_COREDUMP));
217 sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
218}
219
220/* If true, all threads except ->group_exit_task have pending SIGKILL */
221static inline int signal_group_exit(const struct signal_struct *sig)
222{
223 return (sig->flags & SIGNAL_GROUP_EXIT) ||
224 (sig->group_exit_task != NULL);
225}
226
227extern void flush_signals(struct task_struct *);
228extern void ignore_signals(struct task_struct *);
229extern void flush_signal_handlers(struct task_struct *, int force_default);
230extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
231
232static inline int kernel_dequeue_signal(siginfo_t *info)
233{
234 struct task_struct *tsk = current;
235 siginfo_t __info;
236 int ret;
237
238 spin_lock_irq(&tsk->sighand->siglock);
239 ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
240 spin_unlock_irq(&tsk->sighand->siglock);
241
242 return ret;
243}
244
245static inline void kernel_signal_stop(void)
246{
247 spin_lock_irq(&current->sighand->siglock);
248 if (current->jobctl & JOBCTL_STOP_DEQUEUED)
249 __set_current_state(TASK_STOPPED);
250 spin_unlock_irq(&current->sighand->siglock);
251
252 schedule();
253}
254extern int send_sig_info(int, struct siginfo *, struct task_struct *);
255extern int force_sigsegv(int, struct task_struct *);
256extern int force_sig_info(int, struct siginfo *, struct task_struct *);
257extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
258extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
259extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
260 const struct cred *, u32);
261extern int kill_pgrp(struct pid *pid, int sig, int priv);
262extern int kill_pid(struct pid *pid, int sig, int priv);
263extern int kill_proc_info(int, struct siginfo *, pid_t);
264extern __must_check bool do_notify_parent(struct task_struct *, int);
265extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
266extern void force_sig(int, struct task_struct *);
267extern int send_sig(int, struct task_struct *, int);
268extern int zap_other_threads(struct task_struct *p);
269extern struct sigqueue *sigqueue_alloc(void);
270extern void sigqueue_free(struct sigqueue *);
271extern int send_sigqueue(struct sigqueue *, struct task_struct *, int group);
272extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
273
274#ifdef TIF_RESTORE_SIGMASK
275/*
276 * Legacy restore_sigmask accessors. These are inefficient on
277 * SMP architectures because they require atomic operations.
278 */
279
280/**
281 * set_restore_sigmask() - make sure saved_sigmask processing gets done
282 *
283 * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
284 * will run before returning to user mode, to process the flag. For
285 * all callers, TIF_SIGPENDING is already set or it's no harm to set
286 * it. TIF_RESTORE_SIGMASK need not be in the set of bits that the
287 * arch code will notice on return to user mode, in case those bits
288 * are scarce. We set TIF_SIGPENDING here to ensure that the arch
289 * signal code always gets run when TIF_RESTORE_SIGMASK is set.
290 */
291static inline void set_restore_sigmask(void)
292{
293 set_thread_flag(TIF_RESTORE_SIGMASK);
294 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
295}
296static inline void clear_restore_sigmask(void)
297{
298 clear_thread_flag(TIF_RESTORE_SIGMASK);
299}
300static inline bool test_restore_sigmask(void)
301{
302 return test_thread_flag(TIF_RESTORE_SIGMASK);
303}
304static inline bool test_and_clear_restore_sigmask(void)
305{
306 return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
307}
308
309#else /* TIF_RESTORE_SIGMASK */
310
311/* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
312static inline void set_restore_sigmask(void)
313{
314 current->restore_sigmask = true;
315 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
316}
317static inline void clear_restore_sigmask(void)
318{
319 current->restore_sigmask = false;
320}
321static inline bool test_restore_sigmask(void)
322{
323 return current->restore_sigmask;
324}
325static inline bool test_and_clear_restore_sigmask(void)
326{
327 if (!current->restore_sigmask)
328 return false;
329 current->restore_sigmask = false;
330 return true;
331}
332#endif
333
334static inline void restore_saved_sigmask(void)
335{
336 if (test_and_clear_restore_sigmask())
337 __set_current_blocked(&current->saved_sigmask);
338}
339
340static inline sigset_t *sigmask_to_save(void)
341{
342 sigset_t *res = &current->blocked;
343 if (unlikely(test_restore_sigmask()))
344 res = &current->saved_sigmask;
345 return res;
346}
347
348static inline int kill_cad_pid(int sig, int priv)
349{
350 return kill_pid(cad_pid, sig, priv);
351}
352
353/* These can be the second arg to send_sig_info/send_group_sig_info. */
354#define SEND_SIG_NOINFO ((struct siginfo *) 0)
355#define SEND_SIG_PRIV ((struct siginfo *) 1)
356#define SEND_SIG_FORCED ((struct siginfo *) 2)
357
358/*
359 * True if we are on the alternate signal stack.
360 */
361static inline int on_sig_stack(unsigned long sp)
362{
363 /*
364 * If the signal stack is SS_AUTODISARM then, by construction, we
365 * can't be on the signal stack unless user code deliberately set
366 * SS_AUTODISARM when we were already on it.
367 *
368 * This improves reliability: if user state gets corrupted such that
369 * the stack pointer points very close to the end of the signal stack,
370 * then this check will enable the signal to be handled anyway.
371 */
372 if (current->sas_ss_flags & SS_AUTODISARM)
373 return 0;
374
375#ifdef CONFIG_STACK_GROWSUP
376 return sp >= current->sas_ss_sp &&
377 sp - current->sas_ss_sp < current->sas_ss_size;
378#else
379 return sp > current->sas_ss_sp &&
380 sp - current->sas_ss_sp <= current->sas_ss_size;
381#endif
382}
383
384static inline int sas_ss_flags(unsigned long sp)
385{
386 if (!current->sas_ss_size)
387 return SS_DISABLE;
388
389 return on_sig_stack(sp) ? SS_ONSTACK : 0;
390}
391
392static inline void sas_ss_reset(struct task_struct *p)
393{
394 p->sas_ss_sp = 0;
395 p->sas_ss_size = 0;
396 p->sas_ss_flags = SS_DISABLE;
397}
398
399static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
400{
401 if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
402#ifdef CONFIG_STACK_GROWSUP
403 return current->sas_ss_sp;
404#else
405 return current->sas_ss_sp + current->sas_ss_size;
406#endif
407 return sp;
408}
409
410extern void __cleanup_sighand(struct sighand_struct *);
411extern void flush_itimer_signals(void);
412
413#define tasklist_empty() \
414 list_empty(&init_task.tasks)
415
416#define next_task(p) \
417 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
418
419#define for_each_process(p) \
420 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
421
422extern bool current_is_single_threaded(void);
423
424/*
425 * Careful: do_each_thread/while_each_thread is a double loop so
426 * 'break' will not work as expected - use goto instead.
427 */
428#define do_each_thread(g, t) \
429 for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
430
431#define while_each_thread(g, t) \
432 while ((t = next_thread(t)) != g)
433
434#define __for_each_thread(signal, t) \
435 list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
436
437#define for_each_thread(p, t) \
438 __for_each_thread((p)->signal, t)
439
440/* Careful: this is a double loop, 'break' won't work as expected. */
441#define for_each_process_thread(p, t) \
442 for_each_process(p) for_each_thread(p, t)
443
444typedef int (*proc_visitor)(struct task_struct *p, void *data);
445void walk_process_tree(struct task_struct *top, proc_visitor, void *);
446
447static inline int get_nr_threads(struct task_struct *tsk)
448{
449 return tsk->signal->nr_threads;
450}
451
452static inline bool thread_group_leader(struct task_struct *p)
453{
454 return p->exit_signal >= 0;
455}
456
457/* Do to the insanities of de_thread it is possible for a process
458 * to have the pid of the thread group leader without actually being
459 * the thread group leader. For iteration through the pids in proc
460 * all we care about is that we have a task with the appropriate
461 * pid, we don't actually care if we have the right task.
462 */
463static inline bool has_group_leader_pid(struct task_struct *p)
464{
465 return task_pid(p) == p->signal->leader_pid;
466}
467
468static inline
469bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
470{
471 return p1->signal == p2->signal;
472}
473
474static inline struct task_struct *next_thread(const struct task_struct *p)
475{
476 return list_entry_rcu(p->thread_group.next,
477 struct task_struct, thread_group);
478}
479
480static inline int thread_group_empty(struct task_struct *p)
481{
482 return list_empty(&p->thread_group);
483}
484
485#define delay_group_leader(p) \
486 (thread_group_leader(p) && !thread_group_empty(p))
487
488extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
489 unsigned long *flags);
490
491static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
492 unsigned long *flags)
493{
494 struct sighand_struct *ret;
495
496 ret = __lock_task_sighand(tsk, flags);
497 (void)__cond_lock(&tsk->sighand->siglock, ret);
498 return ret;
499}
500
501static inline void unlock_task_sighand(struct task_struct *tsk,
502 unsigned long *flags)
503{
504 spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
505}
506
507static inline unsigned long task_rlimit(const struct task_struct *tsk,
508 unsigned int limit)
509{
510 return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
511}
512
513static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
514 unsigned int limit)
515{
516 return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
517}
518
519static inline unsigned long rlimit(unsigned int limit)
520{
521 return task_rlimit(current, limit);
522}
523
524static inline unsigned long rlimit_max(unsigned int limit)
525{
526 return task_rlimit_max(current, limit);
527}
528
Ingo Molnar3f07c012017-02-08 18:51:30 +0100529#endif /* _LINUX_SCHED_SIGNAL_H */