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