1 #ifndef _LINUX_SCHED_SIGNAL_H
2 #define _LINUX_SCHED_SIGNAL_H
4 #include <linux/rculist.h>
5 #include <linux/signal.h>
6 #include <linux/sched.h>
7 #include <linux/sched/jobctl.h>
8 #include <linux/sched/task.h>
9 #include <linux/cred.h>
12 * Types defining task->signal and task->sighand and APIs using them:
15 struct sighand_struct {
17 struct k_sigaction action[_NSIG];
19 wait_queue_head_t signalfd_wqh;
23 * Per-process accounting stats:
29 u64 ac_utime, ac_stime;
30 unsigned long ac_minflt, ac_majflt;
39 * 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.
45 struct signal_struct {
49 struct list_head thread_head;
51 wait_queue_head_t wait_chldexit; /* for wait4() */
53 /* current thread group signal load-balancing target: */
54 struct task_struct *curr_target;
56 /* shared signal handling: */
57 struct sigpending shared_pending;
59 /* thread group exit support */
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.
67 struct task_struct *group_exit_task;
69 /* thread group stop support, overloads group_exit_code too */
71 unsigned int flags; /* see SIGNAL_* flags below */
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.
82 unsigned int is_child_subreaper:1;
83 unsigned int has_child_subreaper:1;
85 #ifdef CONFIG_POSIX_TIMERS
87 /* POSIX.1b Interval Timers */
89 struct list_head posix_timers;
91 /* ITIMER_REAL timer for the process */
92 struct hrtimer real_timer;
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
100 struct cpu_itimer it[2];
103 * Thread group totals for process CPU timers.
104 * See thread_group_cputimer(), et al, for details.
106 struct thread_group_cputimer cputimer;
108 /* Earliest-expiration cache. */
109 struct task_cputime cputime_expires;
111 struct list_head cpu_timers[3];
115 struct pid *leader_pid;
117 #ifdef CONFIG_NO_HZ_FULL
118 atomic_t tick_dep_mask;
121 struct pid *tty_old_pgrp;
123 /* boolean value for session group leader */
126 struct tty_struct *tty; /* NULL if no tty */
128 #ifdef CONFIG_SCHED_AUTOGROUP
129 struct autogroup *autogroup;
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.
137 seqlock_t stats_lock;
138 u64 utime, stime, cutime, cstime;
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;
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.)
154 unsigned long long sum_sched_runtime;
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.
165 struct rlimit rlim[RLIM_NLIMITS];
167 #ifdef CONFIG_BSD_PROCESS_ACCT
168 struct pacct_struct pacct; /* per-process accounting information */
170 #ifdef CONFIG_TASKSTATS
171 struct taskstats *stats;
175 struct tty_audit_buf *tty_audit_buf;
179 * Thread is the potential origin of an oom condition; kill first on
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 */
189 struct mutex cred_guard_mutex; /* guard against foreign influences on
190 * credential calculations
191 * (notably. ptrace) */
195 * Bits in flags field of signal_struct.
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 */
202 * Pending notifications to parent.
204 #define SIGNAL_CLD_STOPPED 0x00000010
205 #define SIGNAL_CLD_CONTINUED 0x00000020
206 #define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
208 #define SIGNAL_UNKILLABLE 0x00000040 /* for init: ignore fatal signals */
210 #define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
211 SIGNAL_STOP_CONTINUED)
213 static inline void signal_set_stop_flags(struct signal_struct *sig,
216 WARN_ON(sig->flags & (SIGNAL_GROUP_EXIT|SIGNAL_GROUP_COREDUMP));
217 sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
220 /* If true, all threads except ->group_exit_task have pending SIGKILL */
221 static inline int signal_group_exit(const struct signal_struct *sig)
223 return (sig->flags & SIGNAL_GROUP_EXIT) ||
224 (sig->group_exit_task != NULL);
227 extern void flush_signals(struct task_struct *);
228 extern void ignore_signals(struct task_struct *);
229 extern void flush_signal_handlers(struct task_struct *, int force_default);
230 extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
232 static inline int kernel_dequeue_signal(siginfo_t *info)
234 struct task_struct *tsk = current;
238 spin_lock_irq(&tsk->sighand->siglock);
239 ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
240 spin_unlock_irq(&tsk->sighand->siglock);
245 static inline void kernel_signal_stop(void)
247 spin_lock_irq(¤t->sighand->siglock);
248 if (current->jobctl & JOBCTL_STOP_DEQUEUED)
249 __set_current_state(TASK_STOPPED);
250 spin_unlock_irq(¤t->sighand->siglock);
254 extern int send_sig_info(int, struct siginfo *, struct task_struct *);
255 extern int force_sigsegv(int, struct task_struct *);
256 extern int force_sig_info(int, struct siginfo *, struct task_struct *);
257 extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
258 extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
259 extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
260 const struct cred *, u32);
261 extern int kill_pgrp(struct pid *pid, int sig, int priv);
262 extern int kill_pid(struct pid *pid, int sig, int priv);
263 extern int kill_proc_info(int, struct siginfo *, pid_t);
264 extern __must_check bool do_notify_parent(struct task_struct *, int);
265 extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
266 extern void force_sig(int, struct task_struct *);
267 extern int send_sig(int, struct task_struct *, int);
268 extern int zap_other_threads(struct task_struct *p);
269 extern struct sigqueue *sigqueue_alloc(void);
270 extern void sigqueue_free(struct sigqueue *);
271 extern int send_sigqueue(struct sigqueue *, struct task_struct *, int group);
272 extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
274 static inline int restart_syscall(void)
276 set_tsk_thread_flag(current, TIF_SIGPENDING);
277 return -ERESTARTNOINTR;
280 static inline int signal_pending(struct task_struct *p)
282 return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
285 static inline int __fatal_signal_pending(struct task_struct *p)
287 return unlikely(sigismember(&p->pending.signal, SIGKILL));
290 static inline int fatal_signal_pending(struct task_struct *p)
292 return signal_pending(p) && __fatal_signal_pending(p);
295 static inline int signal_pending_state(long state, struct task_struct *p)
297 if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
299 if (!signal_pending(p))
302 return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
306 * Reevaluate whether the task has signals pending delivery.
307 * Wake the task if so.
308 * This is required every time the blocked sigset_t changes.
309 * callers must hold sighand->siglock.
311 extern void recalc_sigpending_and_wake(struct task_struct *t);
312 extern void recalc_sigpending(void);
314 extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
316 static inline void signal_wake_up(struct task_struct *t, bool resume)
318 signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
320 static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
322 signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
325 #ifdef TIF_RESTORE_SIGMASK
327 * Legacy restore_sigmask accessors. These are inefficient on
328 * SMP architectures because they require atomic operations.
332 * set_restore_sigmask() - make sure saved_sigmask processing gets done
334 * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
335 * will run before returning to user mode, to process the flag. For
336 * all callers, TIF_SIGPENDING is already set or it's no harm to set
337 * it. TIF_RESTORE_SIGMASK need not be in the set of bits that the
338 * arch code will notice on return to user mode, in case those bits
339 * are scarce. We set TIF_SIGPENDING here to ensure that the arch
340 * signal code always gets run when TIF_RESTORE_SIGMASK is set.
342 static inline void set_restore_sigmask(void)
344 set_thread_flag(TIF_RESTORE_SIGMASK);
345 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
347 static inline void clear_restore_sigmask(void)
349 clear_thread_flag(TIF_RESTORE_SIGMASK);
351 static inline bool test_restore_sigmask(void)
353 return test_thread_flag(TIF_RESTORE_SIGMASK);
355 static inline bool test_and_clear_restore_sigmask(void)
357 return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
360 #else /* TIF_RESTORE_SIGMASK */
362 /* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
363 static inline void set_restore_sigmask(void)
365 current->restore_sigmask = true;
366 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
368 static inline void clear_restore_sigmask(void)
370 current->restore_sigmask = false;
372 static inline bool test_restore_sigmask(void)
374 return current->restore_sigmask;
376 static inline bool test_and_clear_restore_sigmask(void)
378 if (!current->restore_sigmask)
380 current->restore_sigmask = false;
385 static inline void restore_saved_sigmask(void)
387 if (test_and_clear_restore_sigmask())
388 __set_current_blocked(¤t->saved_sigmask);
391 static inline sigset_t *sigmask_to_save(void)
393 sigset_t *res = ¤t->blocked;
394 if (unlikely(test_restore_sigmask()))
395 res = ¤t->saved_sigmask;
399 static inline int kill_cad_pid(int sig, int priv)
401 return kill_pid(cad_pid, sig, priv);
404 /* These can be the second arg to send_sig_info/send_group_sig_info. */
405 #define SEND_SIG_NOINFO ((struct siginfo *) 0)
406 #define SEND_SIG_PRIV ((struct siginfo *) 1)
407 #define SEND_SIG_FORCED ((struct siginfo *) 2)
410 * True if we are on the alternate signal stack.
412 static inline int on_sig_stack(unsigned long sp)
415 * If the signal stack is SS_AUTODISARM then, by construction, we
416 * can't be on the signal stack unless user code deliberately set
417 * SS_AUTODISARM when we were already on it.
419 * This improves reliability: if user state gets corrupted such that
420 * the stack pointer points very close to the end of the signal stack,
421 * then this check will enable the signal to be handled anyway.
423 if (current->sas_ss_flags & SS_AUTODISARM)
426 #ifdef CONFIG_STACK_GROWSUP
427 return sp >= current->sas_ss_sp &&
428 sp - current->sas_ss_sp < current->sas_ss_size;
430 return sp > current->sas_ss_sp &&
431 sp - current->sas_ss_sp <= current->sas_ss_size;
435 static inline int sas_ss_flags(unsigned long sp)
437 if (!current->sas_ss_size)
440 return on_sig_stack(sp) ? SS_ONSTACK : 0;
443 static inline void sas_ss_reset(struct task_struct *p)
447 p->sas_ss_flags = SS_DISABLE;
450 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
452 if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
453 #ifdef CONFIG_STACK_GROWSUP
454 return current->sas_ss_sp;
456 return current->sas_ss_sp + current->sas_ss_size;
461 extern void __cleanup_sighand(struct sighand_struct *);
462 extern void flush_itimer_signals(void);
464 #define tasklist_empty() \
465 list_empty(&init_task.tasks)
467 #define next_task(p) \
468 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
470 #define for_each_process(p) \
471 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
473 extern bool current_is_single_threaded(void);
476 * Careful: do_each_thread/while_each_thread is a double loop so
477 * 'break' will not work as expected - use goto instead.
479 #define do_each_thread(g, t) \
480 for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
482 #define while_each_thread(g, t) \
483 while ((t = next_thread(t)) != g)
485 #define __for_each_thread(signal, t) \
486 list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
488 #define for_each_thread(p, t) \
489 __for_each_thread((p)->signal, t)
491 /* Careful: this is a double loop, 'break' won't work as expected. */
492 #define for_each_process_thread(p, t) \
493 for_each_process(p) for_each_thread(p, t)
495 typedef int (*proc_visitor)(struct task_struct *p, void *data);
496 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
498 static inline int get_nr_threads(struct task_struct *tsk)
500 return tsk->signal->nr_threads;
503 static inline bool thread_group_leader(struct task_struct *p)
505 return p->exit_signal >= 0;
508 /* Do to the insanities of de_thread it is possible for a process
509 * to have the pid of the thread group leader without actually being
510 * the thread group leader. For iteration through the pids in proc
511 * all we care about is that we have a task with the appropriate
512 * pid, we don't actually care if we have the right task.
514 static inline bool has_group_leader_pid(struct task_struct *p)
516 return task_pid(p) == p->signal->leader_pid;
520 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
522 return p1->signal == p2->signal;
525 static inline struct task_struct *next_thread(const struct task_struct *p)
527 return list_entry_rcu(p->thread_group.next,
528 struct task_struct, thread_group);
531 static inline int thread_group_empty(struct task_struct *p)
533 return list_empty(&p->thread_group);
536 #define delay_group_leader(p) \
537 (thread_group_leader(p) && !thread_group_empty(p))
539 extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
540 unsigned long *flags);
542 static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
543 unsigned long *flags)
545 struct sighand_struct *ret;
547 ret = __lock_task_sighand(tsk, flags);
548 (void)__cond_lock(&tsk->sighand->siglock, ret);
552 static inline void unlock_task_sighand(struct task_struct *tsk,
553 unsigned long *flags)
555 spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
558 static inline unsigned long task_rlimit(const struct task_struct *tsk,
561 return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
564 static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
567 return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
570 static inline unsigned long rlimit(unsigned int limit)
572 return task_rlimit(current, limit);
575 static inline unsigned long rlimit_max(unsigned int limit)
577 return task_rlimit_max(current, limit);
580 #endif /* _LINUX_SCHED_SIGNAL_H */