signal.h 17 KB

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