signal.h 15 KB

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