2038 lines · 50.6 KB · C
RawDownload
	ret = eligible_child(wo, ptrace, p);	if (!ret)		return ret; 	if (unlikely(exit_state == EXIT_TRACE)) {		/*		 * ptrace == 0 means we are the natural parent. In this case		 * we should clear notask_error, debugger will notify us.		 */		if (likely(!ptrace))			wo->notask_error = 0;		return 0;	} 	if (likely(!ptrace) && unlikely(p->ptrace)) {		/*		 * If it is traced by its real parent's group, just pretend		 * the caller is ptrace_do_wait() and reap this child if it		 * is zombie.		 *		 * This also hides group stop state from real parent; otherwise		 * a single stop can be reported twice as group and ptrace stop.		 * If a ptracer wants to distinguish these two events for its		 * own children it should create a separate process which takes		 * the role of real parent.		 */		if (!ptrace_reparented(p))			ptrace = 1;	} 	/* slay zombie? */	if (exit_state == EXIT_ZOMBIE) {		/* we don't reap group leaders with subthreads */		if (!delay_group_leader(p)) {			/*			 * A zombie ptracee is only visible to its ptracer.			 * Notification and reaping will be cascaded to the			 * real parent when the ptracer detaches.			 */			if (unlikely(ptrace) || likely(!p->ptrace))				return wait_task_zombie(wo, p);		} 		/*		 * Allow access to stopped/continued state via zombie by		 * falling through.  Clearing of notask_error is complex.		 *		 * When !@ptrace:		 *		 * If WEXITED is set, notask_error should naturally be		 * cleared.  If not, subset of WSTOPPED|WCONTINUED is set,		 * so, if there are live subthreads, there are events to		 * wait for.  If all subthreads are dead, it's still safe		 * to clear - this function will be called again in finite		 * amount time once all the subthreads are released and		 * will then return without clearing.		 *		 * When @ptrace:		 *		 * Stopped state is per-task and thus can't change once the		 * target task dies.  Only continued and exited can happen.		 * Clear notask_error if WCONTINUED | WEXITED.		 */		if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))			wo->notask_error = 0;	} else {		/*		 * @p is alive and it's gonna stop, continue or exit, so		 * there always is something to wait for.		 */		wo->notask_error = 0;	} 	/*	 * Wait for stopped.  Depending on @ptrace, different stopped state	 * is used and the two don't interact with each other.	 */	ret = wait_task_stopped(wo, ptrace, p);	if (ret)		return ret; 	/*	 * Wait for continued.  There's only one continued state and the	 * ptracer can consume it which can confuse the real parent.  Don't	 * use WCONTINUED from ptracer.  You don't need or want it.	 */	return wait_task_continued(wo, p);} /* * Do the work of do_wait() for one thread in the group, @tsk. * * -ECHILD should be in ->notask_error before the first call. * Returns nonzero for a final return, when we have unlocked tasklist_lock. * Returns zero if the search for a child should continue; then * ->notask_error is 0 if there were any eligible children, * or still -ECHILD. */static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk){	struct task_struct *p; 	list_for_each_entry(p, &tsk->children, sibling) {		int ret = wait_consider_task(wo, 0, p); 		if (ret)			return ret;	} 	return 0;} static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk){	struct task_struct *p; 	list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {		int ret = wait_consider_task(wo, 1, p); 		if (ret)			return ret;	} 	return 0;} bool pid_child_should_wake(struct wait_opts *wo, struct task_struct *p){	if (!eligible_pid(wo, p))		return false; 	if ((wo->wo_flags & __WNOTHREAD) && wo->child_wait.private != p->parent)		return false; 	return true;} static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,				int sync, void *key){	struct wait_opts *wo = container_of(wait, struct wait_opts,						child_wait);	struct task_struct *p = key; 	if (pid_child_should_wake(wo, p))		return default_wake_function(wait, mode, sync, key); 	return 0;} void __wake_up_parent(struct task_struct *p, struct task_struct *parent){	__wake_up_sync_key(&parent->signal->wait_chldexit,			   TASK_INTERRUPTIBLE, p);} static bool is_effectively_child(struct wait_opts *wo, bool ptrace,				 struct task_struct *target){	struct task_struct *parent =		!ptrace ? target->real_parent : target->parent; 	return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&				     same_thread_group(current, parent));} /* * Optimization for waiting on PIDTYPE_PID. No need to iterate through child * and tracee lists to find the target task. */static int do_wait_pid(struct wait_opts *wo){	bool ptrace;	struct task_struct *target;	int retval; 	ptrace = false;	target = pid_task(wo->wo_pid, PIDTYPE_TGID);	if (target && is_effectively_child(wo, ptrace, target)) {		retval = wait_consider_task(wo, ptrace, target);		if (retval)			return retval;	} 	ptrace = true;	target = pid_task(wo->wo_pid, PIDTYPE_PID);	if (target && target->ptrace &&	    is_effectively_child(wo, ptrace, target)) {		retval = wait_consider_task(wo, ptrace, target);		if (retval)			return retval;	} 	return 0;} long __do_wait(struct wait_opts *wo){	long retval; 	/*	 * If there is nothing that can match our criteria, just get out.	 * We will clear ->notask_error to zero if we see any child that	 * might later match our criteria, even if we are not able to reap	 * it yet.	 */	wo->notask_error = -ECHILD;	if ((wo->wo_type < PIDTYPE_MAX) &&	   (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))		goto notask; 	read_lock(&tasklist_lock); 	if (wo->wo_type == PIDTYPE_PID) {		retval = do_wait_pid(wo);		if (retval)			return retval;	} else {		struct task_struct *tsk = current; 		do {			retval = do_wait_thread(wo, tsk);			if (retval)				return retval; 			retval = ptrace_do_wait(wo, tsk);			if (retval)				return retval; 			if (wo->wo_flags & __WNOTHREAD)				break;		} while_each_thread(current, tsk);	}	read_unlock(&tasklist_lock); notask:	retval = wo->notask_error;	if (!retval && !(wo->wo_flags & WNOHANG))		return -ERESTARTSYS; 	return retval;} static long do_wait(struct wait_opts *wo){	int retval; 	trace_sched_process_wait(wo->wo_pid); 	init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);	wo->child_wait.private = current;	add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait); 	do {		set_current_state(TASK_INTERRUPTIBLE);		retval = __do_wait(wo);		if (retval != -ERESTARTSYS)			break;		if (signal_pending(current))			break;		schedule();	} while (1); 	__set_current_state(TASK_RUNNING);	remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);	return retval;} int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid,			  struct waitid_info *infop, int options,			  struct rusage *ru){	unsigned int f_flags = 0;	struct pid *pid = NULL;	enum pid_type type; 	if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|			__WNOTHREAD|__WCLONE|__WALL))		return -EINVAL;	if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))		return -EINVAL; 	switch (which) {	case P_ALL:		type = PIDTYPE_MAX;		break;	case P_PID:		type = PIDTYPE_PID;		if (upid <= 0)			return -EINVAL; 		pid = find_get_pid(upid);		break;	case P_PGID:		type = PIDTYPE_PGID;		if (upid < 0)			return -EINVAL; 		if (upid)			pid = find_get_pid(upid);		else			pid = get_task_pid(current, PIDTYPE_PGID);		break;	case P_PIDFD:		type = PIDTYPE_PID;		if (upid < 0)			return -EINVAL; 		pid = pidfd_get_pid(upid, &f_flags);		if (IS_ERR(pid))			return PTR_ERR(pid); 		break;	default:		return -EINVAL;	} 	wo->wo_type	= type;	wo->wo_pid	= pid;	wo->wo_flags	= options;	wo->wo_info	= infop;	wo->wo_rusage	= ru;	if (f_flags & O_NONBLOCK)		wo->wo_flags |= WNOHANG; 	return 0;} static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,			  int options, struct rusage *ru){	struct wait_opts wo;	long ret; 	ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru);	if (ret)		return ret; 	ret = do_wait(&wo);	if (!ret && !(options & WNOHANG) && (wo.wo_flags & WNOHANG))		ret = -EAGAIN; 	put_pid(wo.wo_pid);	return ret;} SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,		infop, int, options, struct rusage __user *, ru){	struct rusage r;	struct waitid_info info = {.status = 0};	long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);	int signo = 0; 	if (err > 0) {		signo = SIGCHLD;		err = 0;		if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))			return -EFAULT;	}	if (!infop)		return err; 	if (!user_write_access_begin(infop, sizeof(*infop)))		return -EFAULT; 	unsafe_put_user(signo, &infop->si_signo, Efault);	unsafe_put_user(0, &infop->si_errno, Efault);	unsafe_put_user(info.cause, &infop->si_code, Efault);	unsafe_put_user(info.pid, &infop->si_pid, Efault);	unsafe_put_user(info.uid, &infop->si_uid, Efault);	unsafe_put_user(info.status, &infop->si_status, Efault);	user_write_access_end();	return err;Efault:	user_write_access_end();	return -EFAULT;} long kernel_wait4(pid_t upid, int __user *stat_addr, int options,		  struct rusage *ru){	struct wait_opts wo;	struct pid *pid = NULL;	enum pid_type type;	long ret; 	if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|			__WNOTHREAD|__WCLONE|__WALL))		return -EINVAL; 	/* -INT_MIN is not defined */	if (upid == INT_MIN)		return -ESRCH; 	if (upid == -1)		type = PIDTYPE_MAX;	else if (upid < 0) {		type = PIDTYPE_PGID;		pid = find_get_pid(-upid);	} else if (upid == 0) {		type = PIDTYPE_PGID;		pid = get_task_pid(current, PIDTYPE_PGID);	} else /* upid > 0 */ {		type = PIDTYPE_PID;		pid = find_get_pid(upid);	} 	wo.wo_type	= type;	wo.wo_pid	= pid;	wo.wo_flags	= options | WEXITED;	wo.wo_info	= NULL;	wo.wo_stat	= 0;	wo.wo_rusage	= ru;	ret = do_wait(&wo);	put_pid(pid);	if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))		ret = -EFAULT; 	return ret;} int kernel_wait(pid_t pid, int *stat){	struct wait_opts wo = {		.wo_type	= PIDTYPE_PID,		.wo_pid		= find_get_pid(pid),		.wo_flags	= WEXITED,	};	int ret; 	ret = do_wait(&wo);	if (ret > 0 && wo.wo_stat)		*stat = wo.wo_stat;	put_pid(wo.wo_pid);	return ret;} SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,		int, options, struct rusage __user *, ru){	struct rusage r;	long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL); 	if (err > 0) {		if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))			return -EFAULT;	}	return err;} #ifdef __ARCH_WANT_SYS_WAITPID /* * sys_waitpid() remains for compatibility. waitpid() should be * implemented by calling sys_wait4() from libc.a. */SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options){	return kernel_wait4(pid, stat_addr, options, NULL);} #endif #ifdef CONFIG_COMPATCOMPAT_SYSCALL_DEFINE4(wait4,	compat_pid_t, pid,	compat_uint_t __user *, stat_addr,	int, options,	struct compat_rusage __user *, ru){	struct rusage r;	long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);	if (err > 0) {		if (ru && put_compat_rusage(&r, ru))			return -EFAULT;	}	return err;} COMPAT_SYSCALL_DEFINE5(waitid,		int, which, compat_pid_t, pid,		struct compat_siginfo __user *, infop, int, options,		struct compat_rusage __user *, uru){	struct rusage ru;	struct waitid_info info = {.status = 0};	long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);	int signo = 0;	if (err > 0) {		signo = SIGCHLD;		err = 0;		if (uru) {			/* kernel_waitid() overwrites everything in ru */			if (COMPAT_USE_64BIT_TIME)				err = copy_to_user(uru, &ru, sizeof(ru));			else				err = put_compat_rusage(&ru, uru);			if (err)				return -EFAULT;

Lines 1501–2000 of 2038. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t