2017-01-09 03:31:30 +03:00
|
|
|
/* $NetBSD: kern_exit.c,v 1.268 2017/01/09 00:31:30 kamil Exp $ */
|
1998-09-09 03:57:58 +04:00
|
|
|
|
|
|
|
/*-
|
2008-04-24 22:39:20 +04:00
|
|
|
* Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
|
1998-09-09 03:57:58 +04:00
|
|
|
* All rights reserved.
|
1998-09-12 21:20:02 +04:00
|
|
|
*
|
1998-09-09 03:57:58 +04:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
2007-02-10 00:55:00 +03:00
|
|
|
* NASA Ames Research Center, and by Andrew Doran.
|
1998-09-09 03:57:58 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
1998-09-12 21:20:02 +04:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
1998-09-09 03:57:58 +04:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1998-09-12 21:20:02 +04:00
|
|
|
*
|
1998-09-09 03:57:58 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1989, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-06-29 10:29:24 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)kern_exit.c 8.10 (Berkeley) 2/23/95
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2017-01-09 03:31:30 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.268 2017/01/09 00:31:30 kamil Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1998-06-26 01:17:15 +04:00
|
|
|
#include "opt_ktrace.h"
|
2015-10-02 19:54:15 +03:00
|
|
|
#include "opt_dtrace.h"
|
2002-08-07 09:14:47 +04:00
|
|
|
#include "opt_perfctrs.h"
|
1998-10-20 02:19:26 +04:00
|
|
|
#include "opt_sysv.h"
|
1998-02-10 17:08:44 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/syslog.h>
|
1998-08-02 08:41:32 +04:00
|
|
|
#include <sys/pool.h>
|
2008-10-11 17:40:57 +04:00
|
|
|
#include <sys/uidinfo.h>
|
2002-08-07 15:13:40 +04:00
|
|
|
#if defined(PERFCTRS)
|
2002-08-07 09:14:47 +04:00
|
|
|
#include <sys/pmc.h>
|
2002-08-07 15:13:40 +04:00
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/ptrace.h>
|
1994-12-24 18:07:22 +03:00
|
|
|
#include <sys/acct.h>
|
1996-02-04 05:15:01 +03:00
|
|
|
#include <sys/filedesc.h>
|
2002-08-28 11:16:33 +04:00
|
|
|
#include <sys/ras.h>
|
1996-02-04 05:15:01 +03:00
|
|
|
#include <sys/signalvar.h>
|
1999-02-23 05:57:18 +03:00
|
|
|
#include <sys/sched.h>
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/syscallargs.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/sleepq.h>
|
|
|
|
#include <sys/lockdebug.h>
|
|
|
|
#include <sys/ktrace.h>
|
2007-10-19 15:59:34 +04:00
|
|
|
#include <sys/cpu.h>
|
2007-11-13 02:11:58 +03:00
|
|
|
#include <sys/lwpctl.h>
|
2007-12-01 02:05:43 +03:00
|
|
|
#include <sys/atomic.h>
|
DTrace: Add an SDT (Statically Defined Tracing) provider framework, and
implement most of the proc provider. Adds proc:::create, exec,
exec_success, exec_faillure, signal_send, signal_discard, signal_handle,
lwp_create, lwp_start, lwp_exit.
2010-03-02 00:10:13 +03:00
|
|
|
#include <sys/sdt.h>
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
#ifdef DEBUG_EXIT
|
|
|
|
int debug_exit = 0;
|
|
|
|
#define DPRINTF(x) if (debug_exit) printf x
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#endif
|
|
|
|
|
2016-04-02 23:38:40 +03:00
|
|
|
static int find_stopped_child(struct proc *, idtype_t, id_t, int,
|
2016-04-05 17:07:31 +03:00
|
|
|
struct proc **, struct wrusage *, siginfo_t *);
|
2016-04-02 23:38:40 +03:00
|
|
|
static void proc_free(struct proc *, struct wrusage *);
|
2007-05-07 20:53:17 +04:00
|
|
|
|
DTrace: Add an SDT (Statically Defined Tracing) provider framework, and
implement most of the proc provider. Adds proc:::create, exec,
exec_success, exec_faillure, signal_send, signal_discard, signal_handle,
lwp_create, lwp_start, lwp_exit.
2010-03-02 00:10:13 +03:00
|
|
|
/*
|
|
|
|
* DTrace SDT provider definitions
|
|
|
|
*/
|
2015-10-02 19:54:15 +03:00
|
|
|
SDT_PROVIDER_DECLARE(proc);
|
|
|
|
SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
|
|
|
|
|
2003-09-13 19:32:40 +04:00
|
|
|
/*
|
2004-01-03 22:43:55 +03:00
|
|
|
* Fill in the appropriate signal information, and signal the parent.
|
2003-09-13 19:32:40 +04:00
|
|
|
*/
|
2011-10-10 00:30:37 +04:00
|
|
|
/* XXX noclone works around a gcc 4.5 bug on arm */
|
|
|
|
static void __noclone
|
2006-11-01 13:17:58 +03:00
|
|
|
exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
|
2003-09-13 19:32:40 +04:00
|
|
|
{
|
|
|
|
|
2006-12-06 13:02:22 +03:00
|
|
|
KSI_INIT(ksi);
|
2004-05-05 01:23:39 +04:00
|
|
|
if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
|
2016-04-04 23:47:57 +03:00
|
|
|
if (p->p_xsig) {
|
|
|
|
if (p->p_sflag & PS_COREDUMP)
|
2004-05-05 01:23:39 +04:00
|
|
|
ksi->ksi_code = CLD_DUMPED;
|
2003-09-13 19:32:40 +04:00
|
|
|
else
|
2004-05-05 01:23:39 +04:00
|
|
|
ksi->ksi_code = CLD_KILLED;
|
2016-04-04 23:47:57 +03:00
|
|
|
ksi->ksi_status = p->p_xsig;
|
2003-09-13 19:32:40 +04:00
|
|
|
} else {
|
2004-05-05 01:23:39 +04:00
|
|
|
ksi->ksi_code = CLD_EXITED;
|
2016-04-04 23:47:57 +03:00
|
|
|
ksi->ksi_status = p->p_xexit;
|
2003-09-13 19:32:40 +04:00
|
|
|
}
|
2016-04-04 23:47:57 +03:00
|
|
|
} else {
|
|
|
|
ksi->ksi_code = SI_USER;
|
|
|
|
ksi->ksi_status = p->p_xsig;
|
2003-09-13 19:32:40 +04:00
|
|
|
}
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* We fill those in, even for non-SIGCHLD.
|
|
|
|
* It's safe to access p->p_cred unlocked here.
|
2003-09-13 19:32:40 +04:00
|
|
|
*/
|
2004-05-05 01:23:39 +04:00
|
|
|
ksi->ksi_pid = p->p_pid;
|
2006-05-15 01:15:11 +04:00
|
|
|
ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
|
2003-09-13 19:32:40 +04:00
|
|
|
/* XXX: is this still valid? */
|
2007-05-01 00:11:41 +04:00
|
|
|
ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
|
|
|
|
ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
|
2003-09-13 19:32:40 +04:00
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* exit --
|
|
|
|
* Death of process.
|
|
|
|
*/
|
1995-09-20 01:40:36 +04:00
|
|
|
int
|
2007-12-21 02:02:38 +03:00
|
|
|
sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2007-12-21 02:02:38 +03:00
|
|
|
/* {
|
2001-02-27 00:09:57 +03:00
|
|
|
syscallarg(int) rval;
|
2007-12-21 02:02:38 +03:00
|
|
|
} */
|
2007-02-10 00:55:00 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/* Don't call exit1() multiple times in the same process. */
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
if (p->p_sflag & PS_WEXIT) {
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2003-01-18 13:06:22 +03:00
|
|
|
lwp_exit(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/* exit1() will release the mutex. */
|
2016-04-05 02:07:06 +03:00
|
|
|
exit1(l, SCARG(uap, rval), 0);
|
1994-06-29 10:29:24 +04:00
|
|
|
/* NOTREACHED */
|
1995-09-20 01:40:36 +04:00
|
|
|
return (0);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit: deallocate address space and other resources, change proc state
|
|
|
|
* to zombie, and unlink proc from allproc and parent's lists. Save exit
|
|
|
|
* status and rusage for wait(). Check for child processes and orphan them.
|
2007-02-10 00:55:00 +03:00
|
|
|
*
|
2008-04-24 22:39:20 +04:00
|
|
|
* Must be called with p->p_lock held. Does not return.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
1995-09-20 01:40:36 +04:00
|
|
|
void
|
2016-04-05 02:07:06 +03:00
|
|
|
exit1(struct lwp *l, int exitcode, int signo)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2012-08-05 18:53:25 +04:00
|
|
|
struct proc *p, *child, *next_child, *old_parent, *new_parent;
|
2008-03-23 19:53:45 +03:00
|
|
|
struct pgrp *pgrp;
|
2004-05-05 01:23:39 +04:00
|
|
|
ksiginfo_t ksi;
|
2007-02-10 00:55:00 +03:00
|
|
|
ksiginfoq_t kq;
|
2012-02-20 01:05:51 +04:00
|
|
|
int wakeinit;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
p = l->l_proc;
|
|
|
|
|
2008-04-24 22:39:20 +04:00
|
|
|
KASSERT(mutex_owned(p->p_lock));
|
2012-04-08 15:27:44 +04:00
|
|
|
KASSERT(p->p_vmspace != NULL);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2012-02-20 01:05:51 +04:00
|
|
|
if (__predict_false(p == initproc)) {
|
2016-04-04 23:47:57 +03:00
|
|
|
panic("init died (signal %d, exit %d)", signo, exitcode);
|
2008-10-15 10:51:17 +04:00
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_sflag |= PS_WEXIT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force all other LWPs to exit before we do. Only then can we
|
|
|
|
* begin to tear down the rest of the process state.
|
|
|
|
*/
|
2012-02-20 01:05:51 +04:00
|
|
|
if (p->p_nlwps > 1) {
|
2007-02-10 00:55:00 +03:00
|
|
|
exit_lwps(l);
|
2012-02-20 01:05:51 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
ksiginfo_queue_init(&kq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have been asked to stop on exit, do so now.
|
|
|
|
*/
|
2008-06-16 13:51:14 +04:00
|
|
|
if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
|
2007-02-10 00:55:00 +03:00
|
|
|
KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
|
|
|
|
sigclearall(p, &contsigmask, &kq);
|
For processes marked with PS_STOPEXIT, update the process's p_waited
value, and update its parent's p_nstopchild value when marking the
process's p_stat to SSTOP. The process needed to be SACTIVE to get
here, so this transition represents an additional process for which
the parent needs to wait.
Fixes PR kern/50308
Pullups will be requested for:
NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2
2015-10-13 03:28:22 +03:00
|
|
|
|
|
|
|
if (!mutex_tryenter(proc_lock)) {
|
|
|
|
mutex_exit(p->p_lock);
|
|
|
|
mutex_enter(proc_lock);
|
|
|
|
mutex_enter(p->p_lock);
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_waited = 0;
|
For processes marked with PS_STOPEXIT, update the process's p_waited
value, and update its parent's p_nstopchild value when marking the
process's p_stat to SSTOP. The process needed to be SACTIVE to get
here, so this transition represents an additional process for which
the parent needs to wait.
Fixes PR kern/50308
Pullups will be requested for:
NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2
2015-10-13 03:28:22 +03:00
|
|
|
p->p_pptr->p_nstopchild++;
|
2003-12-06 07:16:33 +03:00
|
|
|
p->p_stat = SSTOP;
|
2015-10-13 09:47:21 +03:00
|
|
|
mutex_exit(proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_lock(l);
|
2003-12-06 07:16:33 +03:00
|
|
|
p->p_nrlwps--;
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_stat = LSSTOP;
|
2010-12-18 04:36:19 +03:00
|
|
|
lwp_unlock(l);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2010-12-18 04:36:19 +03:00
|
|
|
lwp_lock(l);
|
2007-05-17 18:51:11 +04:00
|
|
|
mi_switch(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
KERNEL_LOCK(l->l_biglocks, l);
|
2008-06-16 13:51:14 +04:00
|
|
|
mutex_enter(p->p_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bin any remaining signals and mark the process as dying so it will
|
2012-08-05 18:53:25 +04:00
|
|
|
* not be found for, e.g. signals.
|
2008-06-16 13:51:14 +04:00
|
|
|
*/
|
|
|
|
sigfillset(&p->p_sigctx.ps_sigignore);
|
|
|
|
sigclearall(p, NULL, &kq);
|
|
|
|
p->p_stat = SDYING;
|
|
|
|
mutex_exit(p->p_lock);
|
|
|
|
ksiginfo_queue_drain(&kq);
|
2003-02-22 04:00:14 +03:00
|
|
|
|
2007-11-13 02:11:58 +03:00
|
|
|
/* Destroy any lwpctl info. */
|
|
|
|
if (p->p_lwpctl != NULL)
|
|
|
|
lwp_ctl_exit();
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Drain all remaining references that procfs, ptrace and others may
|
|
|
|
* have on the process.
|
2003-01-18 13:06:22 +03:00
|
|
|
*/
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_enter(&p->p_reflock, RW_WRITER);
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2016-04-04 23:47:57 +03:00
|
|
|
DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid));
|
2004-01-04 14:33:29 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
timers_free(p, TIMERS_ALL);
|
2002-08-28 11:16:33 +04:00
|
|
|
#if defined(__HAVE_RAS)
|
2007-10-24 18:50:38 +04:00
|
|
|
ras_purgeall();
|
2002-08-28 11:16:33 +04:00
|
|
|
#endif
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Close open files, release open-file table and free signal
|
|
|
|
* actions. This may block!
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2008-03-22 00:54:58 +03:00
|
|
|
fd_free();
|
2004-05-31 19:33:38 +04:00
|
|
|
cwdfree(p->p_cwdi);
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_cwdi = NULL;
|
2002-03-18 02:41:30 +03:00
|
|
|
doexithooks(p);
|
2007-02-10 00:55:00 +03:00
|
|
|
sigactsfree(p->p_sigacts);
|
2006-07-20 01:11:37 +04:00
|
|
|
|
|
|
|
/*
|
2006-12-22 11:04:01 +03:00
|
|
|
* Write out accounting data.
|
2006-07-20 01:11:37 +04:00
|
|
|
*/
|
2005-12-11 15:16:03 +03:00
|
|
|
(void)acct_process(l);
|
2006-07-20 01:11:37 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
2005-02-27 00:34:55 +03:00
|
|
|
/*
|
2006-07-20 01:11:37 +04:00
|
|
|
* Release trace file.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
if (p->p_tracep != NULL) {
|
2007-08-15 16:07:23 +04:00
|
|
|
mutex_enter(&ktrace_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
ktrderef(p);
|
2007-08-15 16:07:23 +04:00
|
|
|
mutex_exit(&ktrace_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
#endif
|
2006-07-20 01:11:37 +04:00
|
|
|
|
2016-04-04 23:47:57 +03:00
|
|
|
p->p_xexit = exitcode;
|
|
|
|
p->p_xsig = signo;
|
|
|
|
|
2002-08-13 09:42:27 +04:00
|
|
|
/*
|
|
|
|
* If emulation has process exit hook, call it now.
|
2006-08-23 23:49:09 +04:00
|
|
|
* Set the exit status now so that the exit hook has
|
|
|
|
* an opportunity to tweak it (COMPAT_LINUX requires
|
|
|
|
* this for thread group emulation)
|
2002-08-13 09:42:27 +04:00
|
|
|
*/
|
|
|
|
if (p->p_emul->e_proc_exit)
|
|
|
|
(*p->p_emul->e_proc_exit)(p);
|
|
|
|
|
2004-05-05 01:23:39 +04:00
|
|
|
/*
|
|
|
|
* Free the VM resources we're still holding on to.
|
|
|
|
* We must do this from a valid thread because doing
|
|
|
|
* so may block. This frees vmspace, which we don't
|
|
|
|
* need anymore. The only remaining lwp is the one
|
|
|
|
* we run at this moment, nothing runs in userland
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
uvm_proc_exit(p);
|
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Stop profiling.
|
2004-05-05 01:23:39 +04:00
|
|
|
*/
|
2008-06-16 13:51:14 +04:00
|
|
|
if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_spin_enter(&p->p_stmutex);
|
|
|
|
stopprofclock(p);
|
|
|
|
mutex_spin_exit(&p->p_stmutex);
|
|
|
|
}
|
2004-05-05 01:23:39 +04:00
|
|
|
|
|
|
|
/*
|
2008-10-25 18:10:26 +04:00
|
|
|
* If parent is waiting for us to exit or exec, PL_PPWAIT is set; we
|
2007-02-10 00:55:00 +03:00
|
|
|
* wake up the parent early to avoid deadlock. We can do this once
|
|
|
|
* the VM resources are released.
|
2004-05-05 01:23:39 +04:00
|
|
|
*/
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_enter(proc_lock);
|
2008-06-16 13:51:14 +04:00
|
|
|
if (p->p_lflag & PL_PPWAIT) {
|
2012-07-28 00:52:49 +04:00
|
|
|
l->l_lwpctl = NULL; /* was on loan from blocked parent */
|
|
|
|
p->p_lflag &= ~PL_PPWAIT;
|
|
|
|
cv_broadcast(&p->p_pptr->p_waitcv);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SESS_LEADER(p)) {
|
|
|
|
struct vnode *vprele = NULL, *vprevoke = NULL;
|
|
|
|
struct session *sp = p->p_session;
|
|
|
|
struct tty *tp;
|
|
|
|
|
|
|
|
if (sp->s_ttyvp) {
|
|
|
|
/*
|
|
|
|
* Controlling process.
|
|
|
|
* Signal foreground pgrp,
|
|
|
|
* drain controlling terminal
|
|
|
|
* and revoke access to controlling terminal.
|
|
|
|
*/
|
|
|
|
tp = sp->s_ttyp;
|
2007-11-07 18:56:11 +03:00
|
|
|
mutex_spin_enter(&tty_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
if (tp->t_session == sp) {
|
|
|
|
/* we can't guarantee the revoke will do this */
|
2008-03-23 19:53:45 +03:00
|
|
|
pgrp = tp->t_pgrp;
|
2007-02-10 00:55:00 +03:00
|
|
|
tp->t_pgrp = NULL;
|
|
|
|
tp->t_session = NULL;
|
2007-11-07 18:56:11 +03:00
|
|
|
mutex_spin_exit(&tty_lock);
|
2008-03-23 19:53:45 +03:00
|
|
|
if (pgrp != NULL) {
|
|
|
|
pgsignal(pgrp, SIGHUP, 1);
|
|
|
|
}
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
(void) ttywait(tp);
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_enter(proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/* The tty could have been revoked. */
|
2007-02-10 00:55:00 +03:00
|
|
|
vprevoke = sp->s_ttyvp;
|
2007-11-07 18:56:11 +03:00
|
|
|
} else
|
|
|
|
mutex_spin_exit(&tty_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
vprele = sp->s_ttyvp;
|
|
|
|
sp->s_ttyvp = NULL;
|
|
|
|
/*
|
|
|
|
* s_ttyp is not zero'd; we use this to indicate
|
|
|
|
* that the session once had a controlling terminal.
|
|
|
|
* (for logging and informational purposes)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
sp->s_leader = NULL;
|
|
|
|
|
|
|
|
if (vprevoke != NULL || vprele != NULL) {
|
2008-01-02 14:48:20 +03:00
|
|
|
if (vprevoke != NULL) {
|
2009-04-25 19:06:31 +04:00
|
|
|
/* Releases proc_lock. */
|
|
|
|
proc_sessrele(sp);
|
2007-02-10 00:55:00 +03:00
|
|
|
VOP_REVOKE(vprevoke, REVOKEALL);
|
2008-01-02 14:48:20 +03:00
|
|
|
} else
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
if (vprele != NULL)
|
|
|
|
vrele(vprele);
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_enter(proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fixjobc(p, p->p_pgrp, 0);
|
2004-05-05 01:23:39 +04:00
|
|
|
|
2007-03-12 02:40:58 +03:00
|
|
|
/*
|
|
|
|
* Finalize the last LWP's specificdata, as well as the
|
|
|
|
* specificdata for the proc itself.
|
|
|
|
*/
|
|
|
|
lwp_finispecific(l);
|
|
|
|
proc_finispecific(p);
|
|
|
|
|
2004-05-05 01:23:39 +04:00
|
|
|
/*
|
|
|
|
* Notify interested parties of our demise.
|
|
|
|
*/
|
|
|
|
KNOTE(&p->p_klist, NOTE_EXIT);
|
|
|
|
|
2015-10-02 19:54:15 +03:00
|
|
|
SDT_PROBE(proc, kernel, , exit,
|
2016-04-04 23:47:57 +03:00
|
|
|
((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED :
|
|
|
|
(p->p_xsig ? CLD_KILLED : CLD_EXITED)),
|
DTrace: Add an SDT (Statically Defined Tracing) provider framework, and
implement most of the proc provider. Adds proc:::create, exec,
exec_success, exec_faillure, signal_send, signal_discard, signal_handle,
lwp_create, lwp_start, lwp_exit.
2010-03-02 00:10:13 +03:00
|
|
|
0,0,0,0);
|
|
|
|
|
2004-05-05 01:23:39 +04:00
|
|
|
#if PERFCTRS
|
|
|
|
/*
|
|
|
|
* Save final PMC information in parent process & clean up.
|
|
|
|
*/
|
|
|
|
if (PMC_ENABLED(p)) {
|
|
|
|
pmc_save_context(p);
|
|
|
|
pmc_accumulate(p->p_pptr, p);
|
|
|
|
pmc_process_exit(p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-13 00:07:37 +03:00
|
|
|
/*
|
|
|
|
* Reset p_opptr pointer of all former children which got
|
|
|
|
* traced by another process and were reparented. We reset
|
|
|
|
* it to NULL here; the trace detach code then reparents
|
|
|
|
* the child to initproc. We only check allproc list, since
|
|
|
|
* eventual former children on zombproc list won't reference
|
|
|
|
* p_opptr anymore.
|
|
|
|
*/
|
2008-04-29 20:21:01 +04:00
|
|
|
if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
|
2012-08-05 18:53:25 +04:00
|
|
|
struct proc *q;
|
2004-10-01 20:30:52 +04:00
|
|
|
PROCLIST_FOREACH(q, &allproc) {
|
2003-11-13 00:07:37 +03:00
|
|
|
if (q->p_opptr == p)
|
|
|
|
q->p_opptr = NULL;
|
|
|
|
}
|
2016-11-04 21:12:06 +03:00
|
|
|
PROCLIST_FOREACH(q, &zombproc) {
|
|
|
|
if (q->p_opptr == p)
|
|
|
|
q->p_opptr = NULL;
|
|
|
|
}
|
2003-11-13 00:07:37 +03:00
|
|
|
}
|
|
|
|
|
1999-07-22 22:13:36 +04:00
|
|
|
/*
|
|
|
|
* Give orphaned children to init(8).
|
|
|
|
*/
|
2012-08-05 18:53:25 +04:00
|
|
|
child = LIST_FIRST(&p->p_children);
|
|
|
|
wakeinit = (child != NULL);
|
|
|
|
for (; child != NULL; child = next_child) {
|
|
|
|
next_child = LIST_NEXT(child, p_sibling);
|
2002-11-29 00:41:29 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2002-11-29 00:41:29 +03:00
|
|
|
* Traced processes are killed since their existence
|
|
|
|
* means someone is screwing up. Since we reset the
|
|
|
|
* trace flags, the logic in sys_wait4() would not be
|
|
|
|
* triggered to reparent the process to its
|
2002-11-30 12:59:22 +03:00
|
|
|
* original parent, so we must do this here.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2012-08-05 18:53:25 +04:00
|
|
|
if (__predict_false(child->p_slflag & PSL_TRACED)) {
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2012-08-05 18:53:25 +04:00
|
|
|
child->p_slflag &=
|
|
|
|
~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2012-08-05 18:53:25 +04:00
|
|
|
if (child->p_opptr != child->p_pptr) {
|
|
|
|
struct proc *t = child->p_opptr;
|
|
|
|
proc_reparent(child, t ? t : initproc);
|
|
|
|
child->p_opptr = NULL;
|
2002-11-30 12:54:43 +03:00
|
|
|
} else
|
2012-08-05 18:53:25 +04:00
|
|
|
proc_reparent(child, initproc);
|
|
|
|
killproc(child, "orphaned traced process");
|
2007-02-10 00:55:00 +03:00
|
|
|
} else
|
2012-08-05 18:53:25 +04:00
|
|
|
proc_reparent(child, initproc);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2003-03-19 14:36:32 +03:00
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Move proc from allproc to zombproc, it's now nearly ready to be
|
|
|
|
* collected by parent.
|
2003-03-19 14:36:32 +03:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
LIST_REMOVE(l, l_list);
|
2003-03-19 14:36:32 +03:00
|
|
|
LIST_REMOVE(p, p_list);
|
|
|
|
LIST_INSERT_HEAD(&zombproc, p, p_list);
|
2004-01-04 14:33:29 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* Mark the process as dead. We must do this before we signal
|
|
|
|
* the parent.
|
|
|
|
*/
|
|
|
|
p->p_stat = SDEAD;
|
2004-01-04 14:33:29 +03:00
|
|
|
|
|
|
|
/* Put in front of parent's sibling list for parent to collect it */
|
2012-08-05 18:53:25 +04:00
|
|
|
old_parent = p->p_pptr;
|
|
|
|
old_parent->p_nstopchild++;
|
|
|
|
if (LIST_FIRST(&old_parent->p_children) != p) {
|
2004-01-04 14:33:29 +03:00
|
|
|
/* Put child where it can be found quickly */
|
|
|
|
LIST_REMOVE(p, p_sibling);
|
2012-08-05 18:53:25 +04:00
|
|
|
LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling);
|
2004-01-04 14:33:29 +03:00
|
|
|
}
|
|
|
|
|
1998-09-18 22:48:22 +04:00
|
|
|
/*
|
|
|
|
* Notify parent that we're gone. If parent has the P_NOCLDWAIT
|
|
|
|
* flag set, notify init instead (and hope it will handle
|
|
|
|
* this situation).
|
|
|
|
*/
|
2012-08-05 18:53:25 +04:00
|
|
|
if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
|
1998-09-18 22:48:22 +04:00
|
|
|
proc_reparent(p, initproc);
|
2007-02-10 00:55:00 +03:00
|
|
|
wakeinit = 1;
|
2004-01-04 14:33:29 +03:00
|
|
|
|
1998-09-18 22:48:22 +04:00
|
|
|
/*
|
|
|
|
* If this was the last child of our parent, notify
|
|
|
|
* parent, so in case he was wait(2)ing, he will
|
|
|
|
* continue.
|
|
|
|
*/
|
2012-08-05 18:53:25 +04:00
|
|
|
if (LIST_FIRST(&old_parent->p_children) == NULL)
|
|
|
|
cv_broadcast(&old_parent->p_waitcv);
|
1998-09-18 22:48:22 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2004-05-05 01:23:39 +04:00
|
|
|
/* Reload parent pointer, since p may have been reparented above */
|
2012-08-05 18:53:25 +04:00
|
|
|
new_parent = p->p_pptr;
|
2004-05-05 01:23:39 +04:00
|
|
|
|
2012-08-05 18:53:25 +04:00
|
|
|
if (__predict_false((p->p_slflag & PSL_FSTRACE) == 0 &&
|
2009-06-28 15:42:07 +04:00
|
|
|
p->p_exitsig != 0)) {
|
2012-08-05 18:53:25 +04:00
|
|
|
exit_psignal(p, new_parent, &ksi);
|
|
|
|
kpsignal(new_parent, &ksi, NULL);
|
2004-05-05 01:23:39 +04:00
|
|
|
}
|
|
|
|
|
2007-05-01 00:11:41 +04:00
|
|
|
/* Calculate the final rusage info. */
|
|
|
|
calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
|
|
|
|
NULL, NULL);
|
2004-05-05 01:23:39 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (wakeinit)
|
2007-08-07 16:45:54 +04:00
|
|
|
cv_broadcast(&initproc->p_waitcv);
|
2004-05-05 01:23:39 +04:00
|
|
|
|
2007-09-07 03:58:56 +04:00
|
|
|
callout_destroy(&l->l_timeout_ch);
|
2007-07-10 00:51:58 +04:00
|
|
|
|
2011-06-07 02:04:34 +04:00
|
|
|
/*
|
|
|
|
* Release any PCU resources before becoming a zombie.
|
|
|
|
*/
|
|
|
|
pcu_discard_all(l);
|
|
|
|
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2014-05-05 19:45:32 +04:00
|
|
|
/* Free the linux lwp id */
|
|
|
|
if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid)
|
|
|
|
proc_free_pid(l->l_lid);
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_drainrefs(l);
|
|
|
|
lwp_lock(l);
|
|
|
|
l->l_prflag &= ~LPR_DETACHED;
|
|
|
|
l->l_stat = LSZOMB;
|
|
|
|
lwp_unlock(l);
|
|
|
|
KASSERT(curlwp == l);
|
|
|
|
KASSERT(p->p_nrlwps == 1);
|
|
|
|
KASSERT(p->p_nlwps == 1);
|
|
|
|
p->p_stat = SZOMB;
|
|
|
|
p->p_nrlwps--;
|
|
|
|
p->p_nzlwps++;
|
|
|
|
p->p_ndlwps = 0;
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2004-05-05 01:23:39 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* Signal the parent to collect us, and drop the proclist lock.
|
2007-11-07 03:23:13 +03:00
|
|
|
* Drop debugger/procfs lock; no new references can be gained.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
2007-08-07 16:45:54 +04:00
|
|
|
cv_broadcast(&p->p_pptr->p_waitcv);
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_exit(&p->p_reflock);
|
2009-05-08 17:32:59 +04:00
|
|
|
mutex_exit(proc_lock);
|
2000-08-22 21:28:28 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/* Verify that we hold no locks other than the kernel lock. */
|
|
|
|
LOCKDEBUG_BARRIER(&kernel_lock, 0);
|
2006-07-20 01:11:37 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give machine-dependent code a chance to free any MD LWP
|
|
|
|
* resources. This must be done before uvm_lwp_exit(), in
|
|
|
|
* case these resources are in the PCB.
|
|
|
|
*/
|
|
|
|
cpu_lwp_free(l, 1);
|
2012-04-08 15:27:44 +04:00
|
|
|
|
|
|
|
pmap_deactivate(l);
|
2004-01-04 14:33:29 +03:00
|
|
|
|
2004-05-05 01:23:39 +04:00
|
|
|
/* This process no longer needs to hold the kernel lock. */
|
2007-02-10 00:55:00 +03:00
|
|
|
#ifdef notyet
|
|
|
|
/* XXXSMP hold in lwp_userret() */
|
|
|
|
KERNEL_UNLOCK_LAST(l);
|
|
|
|
#else
|
|
|
|
KERNEL_UNLOCK_ALL(l, NULL);
|
|
|
|
#endif
|
2004-05-05 01:23:39 +04:00
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
lwp_exit_switchaway(l);
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
exit_lwps(struct lwp *l)
|
|
|
|
{
|
2012-09-28 00:43:15 +04:00
|
|
|
proc_t *p = l->l_proc;
|
|
|
|
lwp_t *l2;
|
2007-02-10 00:55:00 +03:00
|
|
|
int nlocks;
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
KERNEL_UNLOCK_ALL(l, &nlocks);
|
2012-02-20 01:05:51 +04:00
|
|
|
retry:
|
2012-09-28 00:43:15 +04:00
|
|
|
KASSERT(mutex_owned(p->p_lock));
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
/*
|
|
|
|
* Interrupt LWPs in interruptable sleep, unsuspend suspended
|
2007-02-10 00:55:00 +03:00
|
|
|
* LWPs and then wait for everyone else to finish.
|
2003-01-18 13:06:22 +03:00
|
|
|
*/
|
|
|
|
LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
|
2007-02-10 00:55:00 +03:00
|
|
|
if (l2 == l)
|
|
|
|
continue;
|
|
|
|
lwp_lock(l2);
|
2007-02-18 01:31:36 +03:00
|
|
|
l2->l_flag |= LW_WEXIT;
|
|
|
|
if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
|
2003-07-17 22:16:58 +04:00
|
|
|
l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
|
2007-02-10 00:55:00 +03:00
|
|
|
/* setrunnable() will release the lock. */
|
2003-01-18 13:06:22 +03:00
|
|
|
setrunnable(l2);
|
2007-02-10 00:55:00 +03:00
|
|
|
continue;
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_unlock(l2);
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
2012-09-28 00:43:15 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for every LWP to exit. Note: LWPs can get suspended/slept
|
|
|
|
* behind us or there may even be new LWPs created. Therefore, a
|
|
|
|
* full retry is required on error.
|
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
while (p->p_nlwps > 1) {
|
2012-09-28 00:43:15 +04:00
|
|
|
if (lwp_wait(l, 0, NULL, true)) {
|
2005-08-10 13:44:02 +04:00
|
|
|
goto retry;
|
|
|
|
}
|
2005-02-27 00:34:55 +03:00
|
|
|
}
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2008-05-19 21:06:02 +04:00
|
|
|
KERNEL_LOCK(nlocks, l);
|
2007-06-13 16:14:10 +04:00
|
|
|
KASSERT(p->p_nlwps == 1);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
2016-09-23 17:09:39 +03:00
|
|
|
int
|
2016-04-02 23:38:40 +03:00
|
|
|
do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options,
|
|
|
|
struct wrusage *wru, siginfo_t *si)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2009-11-05 00:23:02 +03:00
|
|
|
proc_t *child;
|
|
|
|
int error;
|
1997-02-07 08:33:07 +03:00
|
|
|
|
2016-09-23 17:16:32 +03:00
|
|
|
|
2016-04-02 23:38:40 +03:00
|
|
|
if (wru != NULL)
|
|
|
|
memset(wru, 0, sizeof(*wru));
|
|
|
|
if (si != NULL)
|
|
|
|
memset(si, 0, sizeof(*si));
|
|
|
|
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_enter(proc_lock);
|
2016-04-05 17:07:31 +03:00
|
|
|
error = find_stopped_child(curproc, idtype, id, options, &child,
|
2016-04-02 23:38:40 +03:00
|
|
|
wru, si);
|
2007-05-07 20:53:17 +04:00
|
|
|
if (child == NULL) {
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
2007-05-07 20:53:17 +04:00
|
|
|
*pid = 0;
|
2003-02-14 13:11:56 +03:00
|
|
|
return error;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2007-05-07 20:53:17 +04:00
|
|
|
*pid = child->p_pid;
|
|
|
|
|
|
|
|
if (child->p_stat == SZOMB) {
|
2016-04-05 17:07:31 +03:00
|
|
|
/* Child is exiting */
|
|
|
|
*status = P_WAITSTATUS(child);
|
2008-04-24 19:35:27 +04:00
|
|
|
/* proc_free() will release the proc_lock. */
|
2009-11-02 00:05:30 +03:00
|
|
|
if (options & WNOWAIT) {
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
2009-11-02 00:05:30 +03:00
|
|
|
} else {
|
2016-04-02 23:38:40 +03:00
|
|
|
proc_free(child, wru);
|
2007-05-07 20:53:17 +04:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-03 23:58:25 +03:00
|
|
|
/* Don't mark SIGCONT if we are being stopped */
|
|
|
|
*status = (child->p_xsig == SIGCONT && child->p_stat != SSTOP) ?
|
|
|
|
W_CONTCODE() : W_STOPCODE(child->p_xsig);
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
2003-02-14 13:11:56 +03:00
|
|
|
}
|
2007-05-07 20:53:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2003-02-14 13:11:56 +03:00
|
|
|
|
2016-04-02 23:38:40 +03:00
|
|
|
int
|
|
|
|
do_sys_wait(int *pid, int *status, int options, struct rusage *ru)
|
|
|
|
{
|
|
|
|
idtype_t idtype;
|
|
|
|
id_t id;
|
|
|
|
int ret;
|
|
|
|
struct wrusage wru;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate the special pid values into the (idtype, pid)
|
|
|
|
* pair for wait6. The WAIT_MYPGRP case is handled by
|
|
|
|
* find_stopped_child() on its own.
|
|
|
|
*/
|
|
|
|
if (*pid == WAIT_ANY) {
|
|
|
|
idtype = P_ALL;
|
|
|
|
id = 0;
|
|
|
|
} else if (*pid < 0) {
|
|
|
|
idtype = P_PGID;
|
|
|
|
id = (id_t)-*pid;
|
|
|
|
} else {
|
|
|
|
idtype = P_PID;
|
|
|
|
id = (id_t)*pid;
|
|
|
|
}
|
|
|
|
options |= WEXITED | WTRAPPED;
|
|
|
|
ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL,
|
|
|
|
NULL);
|
|
|
|
if (ru)
|
|
|
|
*ru = wru.wru_self;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-05-07 20:53:17 +04:00
|
|
|
int
|
2009-11-05 00:23:02 +03:00
|
|
|
sys___wait450(struct lwp *l, const struct sys___wait450_args *uap,
|
|
|
|
register_t *retval)
|
2007-05-07 20:53:17 +04:00
|
|
|
{
|
2007-12-21 02:02:38 +03:00
|
|
|
/* {
|
2007-05-07 20:53:17 +04:00
|
|
|
syscallarg(int) pid;
|
|
|
|
syscallarg(int *) status;
|
|
|
|
syscallarg(int) options;
|
|
|
|
syscallarg(struct rusage *) rusage;
|
2007-12-21 02:02:38 +03:00
|
|
|
} */
|
2009-11-05 00:23:02 +03:00
|
|
|
int error, status, pid = SCARG(uap, pid);
|
|
|
|
struct rusage ru;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2009-11-05 00:23:02 +03:00
|
|
|
error = do_sys_wait(&pid, &status, SCARG(uap, options),
|
|
|
|
SCARG(uap, rusage) != NULL ? &ru : NULL);
|
2003-02-14 13:11:56 +03:00
|
|
|
|
2007-12-21 02:02:38 +03:00
|
|
|
retval[0] = pid;
|
2009-11-05 00:23:02 +03:00
|
|
|
if (pid == 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
return error;
|
2009-11-05 00:23:02 +03:00
|
|
|
}
|
|
|
|
if (SCARG(uap, status)) {
|
2007-05-07 20:53:17 +04:00
|
|
|
error = copyout(&status, SCARG(uap, status), sizeof(status));
|
2009-11-05 00:23:02 +03:00
|
|
|
}
|
|
|
|
if (SCARG(uap, rusage) && error == 0) {
|
|
|
|
error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
|
|
|
|
}
|
2007-05-07 20:53:17 +04:00
|
|
|
return error;
|
2003-02-14 13:11:56 +03:00
|
|
|
}
|
|
|
|
|
2016-04-02 23:38:40 +03:00
|
|
|
int
|
|
|
|
sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval)
|
|
|
|
{
|
|
|
|
/* {
|
|
|
|
syscallarg(idtype_t) idtype;
|
|
|
|
syscallarg(id_t) id;
|
|
|
|
syscallarg(int *) status;
|
|
|
|
syscallarg(int) options;
|
|
|
|
syscallarg(struct wrusage *) wru;
|
|
|
|
syscallarg(siginfo_t *) si;
|
|
|
|
} */
|
|
|
|
struct wrusage wru, *wrup;
|
|
|
|
siginfo_t si, *sip;
|
|
|
|
idtype_t idtype;
|
|
|
|
int pid;
|
|
|
|
id_t id;
|
|
|
|
int error, status;
|
|
|
|
|
|
|
|
idtype = SCARG(uap, idtype);
|
|
|
|
id = SCARG(uap, id);
|
|
|
|
|
|
|
|
if (SCARG(uap, wru) != NULL)
|
|
|
|
wrup = &wru;
|
|
|
|
else
|
|
|
|
wrup = NULL;
|
|
|
|
|
|
|
|
if (SCARG(uap, info) != NULL)
|
|
|
|
sip = &si;
|
|
|
|
else
|
|
|
|
sip = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We expect all callers of wait6() to know about WEXITED and
|
|
|
|
* WTRAPPED.
|
|
|
|
*/
|
|
|
|
error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options),
|
|
|
|
wrup, sip);
|
|
|
|
|
2016-04-25 19:35:47 +03:00
|
|
|
retval[0] = pid; /* tell userland who it was */
|
|
|
|
|
|
|
|
#if 0
|
2016-09-23 17:16:32 +03:00
|
|
|
/*
|
2016-04-25 19:35:47 +03:00
|
|
|
* should we copyout if there was no process, hence no useful data?
|
|
|
|
* We don't for an old sytle wait4() (etc) but I believe
|
|
|
|
* FreeBSD does for wait6(), so a tossup... Go with FreeBSD for now.
|
|
|
|
*/
|
|
|
|
if (pid == 0)
|
|
|
|
return error;
|
|
|
|
#endif
|
|
|
|
|
2016-04-02 23:38:40 +03:00
|
|
|
if (SCARG(uap, status) != NULL && error == 0)
|
|
|
|
error = copyout(&status, SCARG(uap, status), sizeof(status));
|
|
|
|
if (SCARG(uap, wru) != NULL && error == 0)
|
|
|
|
error = copyout(&wru, SCARG(uap, wru), sizeof(wru));
|
|
|
|
if (SCARG(uap, info) != NULL && error == 0)
|
|
|
|
error = copyout(&si, SCARG(uap, info), sizeof(si));
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-04 02:50:49 +03:00
|
|
|
/*
|
|
|
|
* Find a process that matches the provided criteria, and fill siginfo
|
|
|
|
* and resources if found.
|
|
|
|
* Returns:
|
|
|
|
* -1: Not found, abort early
|
|
|
|
* 0: Not matched
|
|
|
|
* 1: Matched, there might be more matches
|
|
|
|
* 2: This is the only match
|
|
|
|
*/
|
2016-04-02 23:38:40 +03:00
|
|
|
static int
|
PR kern/51600 ; PR standards/51606
Revert 1.264 - that was intended to fix 51600, but didn't, it just
hid the problem, and caused 51606. This fixes 51606.
Handle waiting on a process that has been detatched from its parent
because of being ptrace'd by some other process. This fixes 51600.
("handle" here means that the wait() hangs, or with WNOHANG, returns 0,
we cannot actually wait on a process that is not currently an attached
child.)
Note: the detatched process waiting is not yet perfect (it fails to
take account of options like WALLSIG and WALTSIG) - suport for those
(that is, ignoring a detatched child that one of those options will
later cause to be ignored when the process is re-attached.)
For now, for ither than when waiting for a specific process ID, when
a process does a wait() sys call (any of them), has no applicable
children attached that can be returned, and has at least one detatched
child, then we do a linear search of all processes to look for a
suitable detatched child. This is likely to be slow - but very rare.
Eventually it might be better to keep a list of detatched children
per process.
2016-11-09 03:30:17 +03:00
|
|
|
match_process(const struct proc *pp, struct proc **q, idtype_t idtype, id_t id,
|
2016-04-02 23:38:40 +03:00
|
|
|
int options, struct wrusage *wrusage, siginfo_t *siginfo)
|
|
|
|
{
|
|
|
|
struct rusage *rup;
|
|
|
|
struct proc *p = *q;
|
2016-04-04 02:50:49 +03:00
|
|
|
int rv = 1;
|
2016-04-02 23:38:40 +03:00
|
|
|
|
|
|
|
mutex_enter(p->p_lock);
|
|
|
|
switch (idtype) {
|
|
|
|
case P_ALL:
|
|
|
|
break;
|
|
|
|
case P_PID:
|
|
|
|
if (p->p_pid != (pid_t)id) {
|
|
|
|
mutex_exit(p->p_lock);
|
|
|
|
p = *q = proc_find_raw((pid_t)id);
|
|
|
|
if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) {
|
|
|
|
*q = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mutex_enter(p->p_lock);
|
|
|
|
}
|
2016-04-04 02:50:49 +03:00
|
|
|
rv++;
|
2016-04-02 23:38:40 +03:00
|
|
|
break;
|
|
|
|
case P_PGID:
|
|
|
|
if (p->p_pgid != (pid_t)id)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case P_SID:
|
|
|
|
if (p->p_session->s_sid != (pid_t)id)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case P_UID:
|
2016-04-03 05:28:46 +03:00
|
|
|
if (kauth_cred_geteuid(p->p_cred) != (uid_t)id)
|
2016-04-02 23:38:40 +03:00
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case P_GID:
|
2016-04-03 05:28:46 +03:00
|
|
|
if (kauth_cred_getegid(p->p_cred) != (gid_t)id)
|
2016-04-02 23:38:40 +03:00
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case P_CID:
|
|
|
|
case P_PSETID:
|
|
|
|
case P_CPUID:
|
|
|
|
/* XXX: Implement me */
|
|
|
|
default:
|
|
|
|
out:
|
|
|
|
mutex_exit(p->p_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((options & WEXITED) == 0 && p->p_stat == SZOMB)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (siginfo != NULL) {
|
|
|
|
siginfo->si_errno = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SUSv4 requires that the si_signo value is always
|
|
|
|
* SIGCHLD. Obey it despite the rfork(2) interface
|
|
|
|
* allows to request other signal for child exit
|
|
|
|
* notification.
|
|
|
|
*/
|
|
|
|
siginfo->si_signo = SIGCHLD;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is still a rough estimate. We will fix the
|
|
|
|
* cases TRAPPED, STOPPED, and CONTINUED later.
|
|
|
|
*/
|
2016-04-04 23:47:57 +03:00
|
|
|
if (p->p_sflag & PS_COREDUMP) {
|
2016-04-02 23:38:40 +03:00
|
|
|
siginfo->si_code = CLD_DUMPED;
|
2016-04-04 23:47:57 +03:00
|
|
|
siginfo->si_status = p->p_xsig;
|
|
|
|
} else if (p->p_xsig) {
|
2016-04-02 23:38:40 +03:00
|
|
|
siginfo->si_code = CLD_KILLED;
|
2016-04-04 23:47:57 +03:00
|
|
|
siginfo->si_status = p->p_xsig;
|
2016-04-02 23:38:40 +03:00
|
|
|
} else {
|
|
|
|
siginfo->si_code = CLD_EXITED;
|
2016-04-04 23:47:57 +03:00
|
|
|
siginfo->si_status = p->p_xexit;
|
2016-04-02 23:38:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
siginfo->si_pid = p->p_pid;
|
2016-04-03 05:28:46 +03:00
|
|
|
siginfo->si_uid = kauth_cred_geteuid(p->p_cred);
|
|
|
|
siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
|
|
|
|
siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
|
2016-04-02 23:38:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be no reason to limit resources usage info to
|
|
|
|
* exited processes only. A snapshot about any resources used
|
|
|
|
* by a stopped process may be exactly what is needed.
|
|
|
|
*/
|
|
|
|
if (wrusage != NULL) {
|
|
|
|
rup = &wrusage->wru_self;
|
|
|
|
*rup = p->p_stats->p_ru;
|
|
|
|
calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
|
|
|
|
|
|
|
|
rup = &wrusage->wru_children;
|
|
|
|
*rup = p->p_stats->p_cru;
|
|
|
|
calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_exit(p->p_lock);
|
2016-04-04 02:50:49 +03:00
|
|
|
return rv;
|
2016-04-02 23:38:40 +03:00
|
|
|
}
|
|
|
|
|
PR kern/51600 ; PR standards/51606
Revert 1.264 - that was intended to fix 51600, but didn't, it just
hid the problem, and caused 51606. This fixes 51606.
Handle waiting on a process that has been detatched from its parent
because of being ptrace'd by some other process. This fixes 51600.
("handle" here means that the wait() hangs, or with WNOHANG, returns 0,
we cannot actually wait on a process that is not currently an attached
child.)
Note: the detatched process waiting is not yet perfect (it fails to
take account of options like WALLSIG and WALTSIG) - suport for those
(that is, ignoring a detatched child that one of those options will
later cause to be ignored when the process is re-attached.)
For now, for ither than when waiting for a specific process ID, when
a process does a wait() sys call (any of them), has no applicable
children attached that can be returned, and has at least one detatched
child, then we do a linear search of all processes to look for a
suitable detatched child. This is likely to be slow - but very rare.
Eventually it might be better to keep a list of detatched children
per process.
2016-11-09 03:30:17 +03:00
|
|
|
/*
|
|
|
|
* Determine if there are existing processes being debugged
|
|
|
|
* that used to be (and sometime later will be again) children
|
|
|
|
* of a specific parent (while matching wait criteria)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
debugged_child_exists(idtype_t idtype, id_t id, int options, siginfo_t *si,
|
|
|
|
const struct proc *parent)
|
|
|
|
{
|
|
|
|
struct proc *pp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are searching for a specific pid, we can optimise a little
|
|
|
|
*/
|
|
|
|
if (idtype == P_PID) {
|
|
|
|
/*
|
|
|
|
* Check the specific process to see if its real parent is us
|
|
|
|
*/
|
|
|
|
pp = proc_find_raw((pid_t)id);
|
|
|
|
if (pp != NULL && pp->p_stat != SIDL && pp->p_opptr == parent) {
|
|
|
|
/*
|
|
|
|
* using P_ALL here avoids match_process() doing the
|
|
|
|
* same work that we just did, but incorrectly for
|
|
|
|
* this scenario.
|
|
|
|
*/
|
|
|
|
if (match_process(parent, &pp, P_ALL, id, options,
|
|
|
|
NULL, si))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For the hard cases, just look everywhere to see if some
|
|
|
|
* stolen (reparented) process is really our lost child.
|
|
|
|
* Then check if that process could satisfy the wait conditions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX inefficient, but hopefully fairly rare.
|
|
|
|
* XXX should really use a list of reparented processes.
|
|
|
|
*/
|
|
|
|
PROCLIST_FOREACH(pp, &allproc) {
|
|
|
|
if (pp->p_stat == SIDL) /* XXX impossible ?? */
|
|
|
|
continue;
|
|
|
|
if (pp->p_opptr == parent &&
|
|
|
|
match_process(parent, &pp, idtype, id, options, NULL, si))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
PROCLIST_FOREACH(pp, &zombproc) {
|
|
|
|
if (pp->p_stat == SIDL) /* XXX impossible ?? */
|
|
|
|
continue;
|
|
|
|
if (pp->p_opptr == parent &&
|
|
|
|
match_process(parent, &pp, idtype, id, options, NULL, si))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-02-14 13:11:56 +03:00
|
|
|
/*
|
|
|
|
* Scan list of child processes for a child process that has stopped or
|
|
|
|
* exited. Used by sys_wait4 and 'compat' equivalents.
|
2007-02-10 00:55:00 +03:00
|
|
|
*
|
2008-04-24 19:35:27 +04:00
|
|
|
* Must be called with the proc_lock held, and may release while waiting.
|
2003-02-14 13:11:56 +03:00
|
|
|
*/
|
2007-05-07 20:53:17 +04:00
|
|
|
static int
|
2016-04-02 23:38:40 +03:00
|
|
|
find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options,
|
2016-04-05 17:07:31 +03:00
|
|
|
struct proc **child_p, struct wrusage *wru, siginfo_t *si)
|
2003-02-14 13:11:56 +03:00
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
struct proc *child, *dead;
|
PR kern/51600 ; PR standards/51606
Revert 1.264 - that was intended to fix 51600, but didn't, it just
hid the problem, and caused 51606. This fixes 51606.
Handle waiting on a process that has been detatched from its parent
because of being ptrace'd by some other process. This fixes 51600.
("handle" here means that the wait() hangs, or with WNOHANG, returns 0,
we cannot actually wait on a process that is not currently an attached
child.)
Note: the detatched process waiting is not yet perfect (it fails to
take account of options like WALLSIG and WALTSIG) - suport for those
(that is, ignoring a detatched child that one of those options will
later cause to be ignored when the process is re-attached.)
For now, for ither than when waiting for a specific process ID, when
a process does a wait() sys call (any of them), has no applicable
children attached that can be returned, and has at least one detatched
child, then we do a linear search of all processes to look for a
suitable detatched child. This is likely to be slow - but very rare.
Eventually it might be better to keep a list of detatched children
per process.
2016-11-09 03:30:17 +03:00
|
|
|
int error;
|
2003-02-14 13:11:56 +03:00
|
|
|
|
2008-04-24 19:35:27 +04:00
|
|
|
KASSERT(mutex_owned(proc_lock));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2016-11-10 20:07:14 +03:00
|
|
|
if (options & ~WALLOPTS) {
|
2007-05-07 20:53:17 +04:00
|
|
|
*child_p = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-11-10 20:07:14 +03:00
|
|
|
if ((options & WSELECTOPTS) == 0) {
|
2016-04-02 23:38:40 +03:00
|
|
|
/*
|
|
|
|
* We will be unable to find any matching processes,
|
|
|
|
* because there are no known events to look for.
|
|
|
|
* Prefer to return error instead of blocking
|
|
|
|
* indefinitely.
|
|
|
|
*/
|
|
|
|
*child_p = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
|
|
|
|
mutex_enter(parent->p_lock);
|
|
|
|
id = (id_t)parent->p_pgid;
|
|
|
|
mutex_exit(parent->p_lock);
|
|
|
|
idtype = P_PGID;
|
|
|
|
}
|
2007-05-07 20:53:17 +04:00
|
|
|
|
2003-08-20 17:50:53 +04:00
|
|
|
for (;;) {
|
2003-11-13 00:07:37 +03:00
|
|
|
error = ECHILD;
|
2007-02-10 00:55:00 +03:00
|
|
|
dead = NULL;
|
|
|
|
|
2003-02-14 13:11:56 +03:00
|
|
|
LIST_FOREACH(child, &parent->p_children, p_sibling) {
|
2016-04-02 23:38:40 +03:00
|
|
|
int rv = match_process(parent, &child, idtype, id,
|
|
|
|
options, wru, si);
|
|
|
|
if (rv == -1)
|
|
|
|
break;
|
|
|
|
if (rv == 0)
|
2007-02-10 00:55:00 +03:00
|
|
|
continue;
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2003-02-14 13:11:56 +03:00
|
|
|
* Wait for processes with p_exitsig != SIGCHLD
|
|
|
|
* processes only if WALTSIG is set; wait for
|
|
|
|
* processes with p_exitsig == SIGCHLD only
|
|
|
|
* if WALTSIG is clear.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2003-02-14 13:11:56 +03:00
|
|
|
if (((options & WALLSIG) == 0) &&
|
|
|
|
(options & WALTSIG ? child->p_exitsig == SIGCHLD
|
2003-11-13 00:07:37 +03:00
|
|
|
: P_EXITSIG(child) != SIGCHLD)){
|
2016-04-04 02:50:49 +03:00
|
|
|
if (rv == 2) {
|
|
|
|
child = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2003-02-14 13:11:56 +03:00
|
|
|
continue;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
2003-11-13 00:07:37 +03:00
|
|
|
error = 0;
|
2007-02-10 00:55:00 +03:00
|
|
|
if ((options & WNOZOMBIE) == 0) {
|
|
|
|
if (child->p_stat == SZOMB)
|
|
|
|
break;
|
|
|
|
if (child->p_stat == SDEAD) {
|
|
|
|
/*
|
|
|
|
* We may occasionally arrive here
|
|
|
|
* after receiving a signal, but
|
2010-05-11 13:30:29 +04:00
|
|
|
* immediately before the child
|
2007-02-10 00:55:00 +03:00
|
|
|
* process is zombified. The wait
|
|
|
|
* will be short, so avoid returning
|
|
|
|
* to userspace.
|
|
|
|
*/
|
|
|
|
dead = child;
|
|
|
|
}
|
|
|
|
}
|
2003-11-13 00:07:37 +03:00
|
|
|
|
2016-04-03 05:28:46 +03:00
|
|
|
if ((options & WCONTINUED) != 0 &&
|
2016-04-28 00:15:40 +03:00
|
|
|
child->p_xsig == SIGCONT &&
|
|
|
|
(child->p_sflag & PS_CONTINUED)) {
|
2016-04-03 05:28:46 +03:00
|
|
|
if ((options & WNOWAIT) == 0) {
|
2016-04-28 00:15:40 +03:00
|
|
|
child->p_sflag &= ~PS_CONTINUED;
|
2016-04-03 05:28:46 +03:00
|
|
|
child->p_waited = 1;
|
|
|
|
parent->p_nstopchild--;
|
|
|
|
}
|
|
|
|
if (si) {
|
2016-04-04 23:47:57 +03:00
|
|
|
si->si_status = child->p_xsig;
|
2016-04-03 05:28:46 +03:00
|
|
|
si->si_code = CLD_CONTINUED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((options & (WTRAPPED|WSTOPPED)) != 0 &&
|
2016-04-02 23:38:40 +03:00
|
|
|
child->p_stat == SSTOP &&
|
2007-02-10 00:55:00 +03:00
|
|
|
child->p_waited == 0 &&
|
2016-04-03 05:28:46 +03:00
|
|
|
((child->p_slflag & PSL_TRACED) ||
|
|
|
|
options & (WUNTRACED|WSTOPPED))) {
|
2003-11-13 00:07:37 +03:00
|
|
|
if ((options & WNOWAIT) == 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
child->p_waited = 1;
|
2003-11-13 00:07:37 +03:00
|
|
|
parent->p_nstopchild--;
|
|
|
|
}
|
2016-04-02 23:38:40 +03:00
|
|
|
if (si) {
|
2016-04-04 23:47:57 +03:00
|
|
|
si->si_status = child->p_xsig;
|
2016-09-23 17:16:32 +03:00
|
|
|
si->si_code =
|
2016-04-03 05:28:46 +03:00
|
|
|
(child->p_slflag & PSL_TRACED) ?
|
|
|
|
CLD_TRAPPED : CLD_STOPPED;
|
2016-04-02 23:38:40 +03:00
|
|
|
}
|
2003-11-13 00:07:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-04-04 02:50:49 +03:00
|
|
|
if (parent->p_nstopchild == 0 || rv == 2) {
|
2003-11-13 00:07:37 +03:00
|
|
|
child = NULL;
|
|
|
|
break;
|
2003-02-14 13:11:56 +03:00
|
|
|
}
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
PR kern/51600 ; PR standards/51606
Revert 1.264 - that was intended to fix 51600, but didn't, it just
hid the problem, and caused 51606. This fixes 51606.
Handle waiting on a process that has been detatched from its parent
because of being ptrace'd by some other process. This fixes 51600.
("handle" here means that the wait() hangs, or with WNOHANG, returns 0,
we cannot actually wait on a process that is not currently an attached
child.)
Note: the detatched process waiting is not yet perfect (it fails to
take account of options like WALLSIG and WALTSIG) - suport for those
(that is, ignoring a detatched child that one of those options will
later cause to be ignored when the process is re-attached.)
For now, for ither than when waiting for a specific process ID, when
a process does a wait() sys call (any of them), has no applicable
children attached that can be returned, and has at least one detatched
child, then we do a linear search of all processes to look for a
suitable detatched child. This is likely to be slow - but very rare.
Eventually it might be better to keep a list of detatched children
per process.
2016-11-09 03:30:17 +03:00
|
|
|
/*
|
|
|
|
* If we found nothing, but we are the bereaved parent
|
|
|
|
* of a stolen child, look and see if that child (or
|
|
|
|
* one of them) meets our search criteria. If so, then
|
|
|
|
* we cannot succeed, but we can hang (wait...),
|
|
|
|
* or if WNOHANG, return 0 instead of ECHILD
|
|
|
|
*/
|
|
|
|
if (child == NULL && error == ECHILD &&
|
|
|
|
(parent->p_slflag & PSL_CHTRACED) &&
|
|
|
|
debugged_child_exists(idtype, id, options, si, parent))
|
|
|
|
error = 0;
|
|
|
|
|
|
|
|
if (child != NULL || error != 0 ||
|
|
|
|
((options & WNOHANG) != 0 && dead == NULL)) {
|
2003-11-13 00:07:37 +03:00
|
|
|
*child_p = child;
|
PR kern/51600 ; PR standards/51606
Revert 1.264 - that was intended to fix 51600, but didn't, it just
hid the problem, and caused 51606. This fixes 51606.
Handle waiting on a process that has been detatched from its parent
because of being ptrace'd by some other process. This fixes 51600.
("handle" here means that the wait() hangs, or with WNOHANG, returns 0,
we cannot actually wait on a process that is not currently an attached
child.)
Note: the detatched process waiting is not yet perfect (it fails to
take account of options like WALLSIG and WALTSIG) - suport for those
(that is, ignoring a detatched child that one of those options will
later cause to be ignored when the process is re-attached.)
For now, for ither than when waiting for a specific process ID, when
a process does a wait() sys call (any of them), has no applicable
children attached that can be returned, and has at least one detatched
child, then we do a linear search of all processes to look for a
suitable detatched child. This is likely to be slow - but very rare.
Eventually it might be better to keep a list of detatched children
per process.
2016-11-09 03:30:17 +03:00
|
|
|
return error;
|
2003-02-14 13:11:56 +03:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for another child process to stop.
|
|
|
|
*/
|
2008-04-24 19:35:27 +04:00
|
|
|
error = cv_wait_sig(&parent->p_waitcv, proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-05-07 20:53:17 +04:00
|
|
|
if (error != 0) {
|
|
|
|
*child_p = NULL;
|
2003-02-14 13:11:56 +03:00
|
|
|
return error;
|
2007-05-07 20:53:17 +04:00
|
|
|
}
|
2003-02-14 13:11:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Free a process after parent has taken all the state info. Must be called
|
2007-03-09 17:11:22 +03:00
|
|
|
* with the proclist lock held, and will release before returning.
|
2007-02-10 00:55:00 +03:00
|
|
|
*
|
|
|
|
* *ru is returned to the caller, and must be freed by the caller.
|
2003-02-14 13:11:56 +03:00
|
|
|
*/
|
2007-05-09 00:10:14 +04:00
|
|
|
static void
|
2016-04-02 23:38:40 +03:00
|
|
|
proc_free(struct proc *p, struct wrusage *wru)
|
2003-02-14 13:11:56 +03:00
|
|
|
{
|
2009-03-29 00:38:55 +03:00
|
|
|
struct proc *parent = p->p_pptr;
|
2007-02-10 00:55:00 +03:00
|
|
|
struct lwp *l;
|
2004-05-05 01:23:39 +04:00
|
|
|
ksiginfo_t ksi;
|
2007-06-15 22:29:53 +04:00
|
|
|
kauth_cred_t cred1, cred2;
|
2007-02-10 00:55:00 +03:00
|
|
|
uid_t uid;
|
|
|
|
|
2008-04-24 19:35:27 +04:00
|
|
|
KASSERT(mutex_owned(proc_lock));
|
2007-02-10 00:55:00 +03:00
|
|
|
KASSERT(p->p_nlwps == 1);
|
|
|
|
KASSERT(p->p_nzlwps == 1);
|
2004-03-02 12:15:26 +03:00
|
|
|
KASSERT(p->p_nrlwps == 0);
|
2007-02-10 00:55:00 +03:00
|
|
|
KASSERT(p->p_stat == SZOMB);
|
2004-03-02 12:15:26 +03:00
|
|
|
|
2003-02-14 13:11:56 +03:00
|
|
|
/*
|
|
|
|
* If we got the child via ptrace(2) or procfs, and
|
|
|
|
* the parent is different (meaning the process was
|
|
|
|
* attached, rather than run as a child), then we need
|
|
|
|
* to give it back to the old parent, and send the
|
|
|
|
* parent the exit signal. The rest of the cleanup
|
|
|
|
* will be done when the old parent waits on the child.
|
|
|
|
*/
|
2009-03-29 00:38:55 +03:00
|
|
|
if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
|
|
|
|
mutex_enter(p->p_lock);
|
|
|
|
p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
|
|
|
|
mutex_exit(p->p_lock);
|
|
|
|
parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
|
|
|
|
proc_reparent(p, parent);
|
|
|
|
p->p_opptr = NULL;
|
|
|
|
if (p->p_exitsig != 0) {
|
|
|
|
exit_psignal(p, parent, &ksi);
|
|
|
|
kpsignal(parent, &ksi, NULL);
|
2004-05-05 01:23:39 +04:00
|
|
|
}
|
2009-03-29 00:38:55 +03:00
|
|
|
cv_broadcast(&parent->p_waitcv);
|
|
|
|
mutex_exit(proc_lock);
|
|
|
|
return;
|
2003-02-14 13:11:56 +03:00
|
|
|
}
|
1998-09-09 03:57:58 +04:00
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
sched_proc_exit(parent, p);
|
2008-03-27 22:06:51 +03:00
|
|
|
|
2007-05-09 00:10:14 +04:00
|
|
|
/*
|
|
|
|
* Add child times of exiting process onto its own times.
|
|
|
|
* This cannot be done any earlier else it might get done twice.
|
|
|
|
*/
|
2008-03-27 22:06:51 +03:00
|
|
|
l = LIST_FIRST(&p->p_lwps);
|
|
|
|
p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
|
|
|
|
p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
|
|
|
|
ruadd(&p->p_stats->p_ru, &l->l_ru);
|
2007-05-09 00:10:14 +04:00
|
|
|
ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
|
2007-05-01 00:11:41 +04:00
|
|
|
ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
|
2016-04-02 23:38:40 +03:00
|
|
|
if (wru != NULL) {
|
|
|
|
wru->wru_self = p->p_stats->p_ru;
|
|
|
|
wru->wru_children = p->p_stats->p_cru;
|
|
|
|
}
|
2016-04-04 23:47:57 +03:00
|
|
|
p->p_xsig = 0;
|
|
|
|
p->p_xexit = 0;
|
2003-03-19 14:36:32 +03:00
|
|
|
|
|
|
|
/*
|
2016-09-23 17:16:32 +03:00
|
|
|
* At this point we are going to start freeing the final resources.
|
2007-02-10 00:55:00 +03:00
|
|
|
* If anyone tries to access the proc structure after here they will
|
|
|
|
* get a shock - bits are missing. Attempt to make it hard! We
|
|
|
|
* don't bother with any further locking past this point.
|
2003-03-19 14:36:32 +03:00
|
|
|
*/
|
|
|
|
p->p_stat = SIDL; /* not even a zombie any more */
|
2007-02-10 00:55:00 +03:00
|
|
|
LIST_REMOVE(p, p_list); /* off zombproc */
|
2009-03-29 00:38:55 +03:00
|
|
|
parent->p_nstopchild--;
|
2007-02-10 00:55:00 +03:00
|
|
|
LIST_REMOVE(p, p_sibling);
|
|
|
|
|
2007-10-12 18:29:37 +04:00
|
|
|
/*
|
|
|
|
* Let pid be reallocated.
|
|
|
|
*/
|
2010-07-07 05:30:32 +04:00
|
|
|
proc_free_pid(p->p_pid);
|
2009-04-25 19:06:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlink process from its process group.
|
|
|
|
* Releases the proc_lock.
|
|
|
|
*/
|
|
|
|
proc_leavepgrp(p);
|
2007-06-15 22:29:53 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
2007-10-12 18:29:37 +04:00
|
|
|
* Delay release until after lwp_free.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
2007-06-15 22:29:53 +04:00
|
|
|
cred2 = l->l_cred;
|
|
|
|
|
|
|
|
/*
|
2007-10-12 18:29:37 +04:00
|
|
|
* Free the last LWP's resources.
|
|
|
|
*
|
|
|
|
* lwp_free ensures the LWP is no longer running on another CPU.
|
2007-06-15 22:29:53 +04:00
|
|
|
*/
|
|
|
|
lwp_free(l, false, true);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-08-15 16:20:28 +04:00
|
|
|
/*
|
2007-10-12 18:29:37 +04:00
|
|
|
* Now no one except us can reach the process p.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2003-02-14 13:11:56 +03:00
|
|
|
/*
|
|
|
|
* Decrement the count of procs running with this uid.
|
|
|
|
*/
|
2007-10-12 18:29:37 +04:00
|
|
|
cred1 = p->p_cred;
|
|
|
|
uid = kauth_cred_getuid(cred1);
|
2007-02-10 00:55:00 +03:00
|
|
|
(void)chgproccnt(uid, -1);
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2003-02-14 13:11:56 +03:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Release substructures.
|
2003-02-14 13:11:56 +03:00
|
|
|
*/
|
2007-10-12 18:29:37 +04:00
|
|
|
|
2011-05-01 04:11:52 +04:00
|
|
|
lim_free(p->p_limit);
|
2007-10-12 18:29:37 +04:00
|
|
|
pstatsfree(p->p_stats);
|
2007-06-15 22:29:53 +04:00
|
|
|
kauth_cred_free(cred1);
|
|
|
|
kauth_cred_free(cred2);
|
2003-02-14 13:11:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Release reference to text vnode
|
|
|
|
*/
|
2007-10-12 18:29:37 +04:00
|
|
|
if (p->p_textvp)
|
|
|
|
vrele(p->p_textvp);
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_destroy(&p->p_auxlock);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_obj_free(p->p_lock);
|
2007-10-12 18:29:37 +04:00
|
|
|
mutex_destroy(&p->p_stmutex);
|
|
|
|
cv_destroy(&p->p_waitcv);
|
|
|
|
cv_destroy(&p->p_lwpcv);
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_destroy(&p->p_reflock);
|
2007-10-12 18:29:37 +04:00
|
|
|
|
2007-12-26 19:01:34 +03:00
|
|
|
proc_free_mem(p);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
2016-11-04 21:14:04 +03:00
|
|
|
/*
|
|
|
|
* Change the parent of a process for tracing purposes.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
proc_changeparent(struct proc *t, struct proc *p)
|
|
|
|
{
|
|
|
|
SET(t->p_slflag, PSL_TRACED);
|
|
|
|
t->p_opptr = t->p_pptr;
|
|
|
|
if (t->p_pptr == p)
|
|
|
|
return;
|
|
|
|
struct proc *parent = t->p_pptr;
|
|
|
|
|
|
|
|
if (parent->p_lock < t->p_lock) {
|
|
|
|
if (!mutex_tryenter(parent->p_lock)) {
|
|
|
|
mutex_exit(t->p_lock);
|
|
|
|
mutex_enter(parent->p_lock);
|
|
|
|
mutex_enter(t->p_lock);
|
|
|
|
}
|
|
|
|
} else if (parent->p_lock > t->p_lock) {
|
|
|
|
mutex_enter(parent->p_lock);
|
|
|
|
}
|
|
|
|
parent->p_slflag |= PSL_CHTRACED;
|
|
|
|
proc_reparent(t, p);
|
|
|
|
if (parent->p_lock != t->p_lock)
|
|
|
|
mutex_exit(parent->p_lock);
|
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* make process 'parent' the new parent of process 'child'.
|
2003-11-13 00:07:37 +03:00
|
|
|
*
|
2008-04-24 19:35:27 +04:00
|
|
|
* Must be called with proc_lock held.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
void
|
2000-08-01 08:57:28 +04:00
|
|
|
proc_reparent(struct proc *child, struct proc *parent)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
|
|
|
|
2008-04-24 19:35:27 +04:00
|
|
|
KASSERT(mutex_owned(proc_lock));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
if (child->p_pptr == parent)
|
|
|
|
return;
|
|
|
|
|
Currently, if a process is exiting and its parent has indicated no intent
of reaping the process (nor any other children), the process wil get
reparented to init. Since the state of the exiting process at this point
is SDEAD, proc_reparent() will not update either the old or new parent's
p_nstopchild counters.
This change causes both old and new parents to be properly updated.
Fixes PR kern/50300
Pullups will be requested for:
NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2
2015-10-13 03:27:19 +03:00
|
|
|
if (child->p_stat == SZOMB || child->p_stat == SDEAD ||
|
2007-02-10 00:55:00 +03:00
|
|
|
(child->p_stat == SSTOP && !child->p_waited)) {
|
2003-11-13 00:07:37 +03:00
|
|
|
child->p_pptr->p_nstopchild--;
|
|
|
|
parent->p_nstopchild++;
|
|
|
|
}
|
2016-11-13 18:25:01 +03:00
|
|
|
if (parent == initproc) {
|
1999-07-16 03:18:41 +04:00
|
|
|
child->p_exitsig = SIGCHLD;
|
2016-11-13 18:25:01 +03:00
|
|
|
child->p_ppid = parent->p_pid;
|
|
|
|
}
|
1999-07-16 03:18:41 +04:00
|
|
|
|
1994-08-30 07:04:28 +04:00
|
|
|
LIST_REMOVE(child, p_sibling);
|
|
|
|
LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
|
1994-06-29 10:29:24 +04:00
|
|
|
child->p_pptr = parent;
|
|
|
|
}
|