signal.h 20 KB

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