xref: /onnv-gate/usr/src/uts/common/os/exit.c (revision 1217)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22390Sraf 
230Sstevel@tonic-gate /*
24*1217Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.74 */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/proc.h>
400Sstevel@tonic-gate #include <sys/ucontext.h>
410Sstevel@tonic-gate #include <sys/procfs.h>
420Sstevel@tonic-gate #include <sys/vnode.h>
430Sstevel@tonic-gate #include <sys/acct.h>
440Sstevel@tonic-gate #include <sys/var.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/debug.h>
470Sstevel@tonic-gate #include <sys/wait.h>
480Sstevel@tonic-gate #include <sys/siginfo.h>
490Sstevel@tonic-gate #include <sys/procset.h>
500Sstevel@tonic-gate #include <sys/class.h>
510Sstevel@tonic-gate #include <sys/file.h>
520Sstevel@tonic-gate #include <sys/session.h>
530Sstevel@tonic-gate #include <sys/kmem.h>
540Sstevel@tonic-gate #include <sys/vtrace.h>
550Sstevel@tonic-gate #include <sys/prsystm.h>
560Sstevel@tonic-gate #include <sys/ipc.h>
570Sstevel@tonic-gate #include <sys/sem_impl.h>
580Sstevel@tonic-gate #include <c2/audit.h>
590Sstevel@tonic-gate #include <sys/aio_impl.h>
600Sstevel@tonic-gate #include <vm/as.h>
610Sstevel@tonic-gate #include <sys/poll.h>
620Sstevel@tonic-gate #include <sys/door.h>
630Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
640Sstevel@tonic-gate #include <sys/utrap.h>
650Sstevel@tonic-gate #include <sys/task.h>
660Sstevel@tonic-gate #include <sys/exacct.h>
670Sstevel@tonic-gate #include <sys/cyclic.h>
680Sstevel@tonic-gate #include <sys/schedctl.h>
690Sstevel@tonic-gate #include <sys/rctl.h>
700Sstevel@tonic-gate #include <sys/contract_impl.h>
710Sstevel@tonic-gate #include <sys/contract/process_impl.h>
720Sstevel@tonic-gate #include <sys/list.h>
730Sstevel@tonic-gate #include <sys/dtrace.h>
740Sstevel@tonic-gate #include <sys/pool.h>
750Sstevel@tonic-gate #include <sys/sdt.h>
760Sstevel@tonic-gate #include <sys/corectl.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * convert code/data pair into old style wait status
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate int
820Sstevel@tonic-gate wstat(int code, int data)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	int stat = (data & 0377);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	switch (code) {
870Sstevel@tonic-gate 	case CLD_EXITED:
880Sstevel@tonic-gate 		stat <<= 8;
890Sstevel@tonic-gate 		break;
900Sstevel@tonic-gate 	case CLD_DUMPED:
910Sstevel@tonic-gate 		stat |= WCOREFLG;
920Sstevel@tonic-gate 		break;
930Sstevel@tonic-gate 	case CLD_KILLED:
940Sstevel@tonic-gate 		break;
950Sstevel@tonic-gate 	case CLD_TRAPPED:
960Sstevel@tonic-gate 	case CLD_STOPPED:
970Sstevel@tonic-gate 		stat <<= 8;
980Sstevel@tonic-gate 		stat |= WSTOPFLG;
990Sstevel@tonic-gate 		break;
1000Sstevel@tonic-gate 	case CLD_CONTINUED:
1010Sstevel@tonic-gate 		stat = WCONTFLG;
1020Sstevel@tonic-gate 		break;
1030Sstevel@tonic-gate 	default:
1040Sstevel@tonic-gate 		cmn_err(CE_PANIC, "wstat: bad code");
1050Sstevel@tonic-gate 		/* NOTREACHED */
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	return (stat);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static char *
1110Sstevel@tonic-gate exit_reason(char *buf, size_t bufsz, int what, int why)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	switch (why) {
1140Sstevel@tonic-gate 	case CLD_EXITED:
1150Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited with status %d", what);
1160Sstevel@tonic-gate 		break;
1170Sstevel@tonic-gate 	case CLD_KILLED:
1180Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited on fatal signal %d", what);
1190Sstevel@tonic-gate 		break;
1200Sstevel@tonic-gate 	case CLD_DUMPED:
1210Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "core dumped on signal %d", what);
1220Sstevel@tonic-gate 		break;
1230Sstevel@tonic-gate 	default:
1240Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "encountered unknown error "
1250Sstevel@tonic-gate 		    "(%d, %d)", why, what);
1260Sstevel@tonic-gate 		break;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	return (buf);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * exit system call: pass back caller's arg.
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate void
1360Sstevel@tonic-gate rexit(int rval)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	exit(CLD_EXITED, rval);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Called by proc_exit() when a zone's init exits, presumably because
1430Sstevel@tonic-gate  * it failed.  As long as the given zone is still in the "running"
1440Sstevel@tonic-gate  * state, we will re-exec() init, but first we need to reset things
1450Sstevel@tonic-gate  * which are usually inherited across exec() but will break init's
1460Sstevel@tonic-gate  * assumption that it is being exec()'d from a virgin process.  Most
1470Sstevel@tonic-gate  * importantly this includes closing all file descriptors (exec only
1480Sstevel@tonic-gate  * closes those marked close-on-exec) and resetting signals (exec only
1490Sstevel@tonic-gate  * resets handled signals, and we need to clear any signals which
1500Sstevel@tonic-gate  * killed init).  Anything else that exec(2) says would be inherited,
1510Sstevel@tonic-gate  * but would affect the execution of init, needs to be reset.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static int
1540Sstevel@tonic-gate restart_init(int what, int why)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	kthread_t *t = curthread;
1570Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1580Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1590Sstevel@tonic-gate 	user_t *up = PTOU(p);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	vnode_t *oldcd, *oldrd;
1620Sstevel@tonic-gate 	sess_t *sp;
1630Sstevel@tonic-gate 	int i, err;
1640Sstevel@tonic-gate 	char reason_buf[64];
1650Sstevel@tonic-gate 	const char *ipath;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Let zone admin (and global zone admin if this is for a non-global
1690Sstevel@tonic-gate 	 * zone) know that init has failed and will be restarted.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
1720Sstevel@tonic-gate 	    "init(1M) %s: restarting automatically",
1730Sstevel@tonic-gate 	    exit_reason(reason_buf, sizeof (reason_buf), what, why));
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
1760Sstevel@tonic-gate 		cmn_err(CE_WARN, "init(1M) for zone %s (pid %d) %s: "
1770Sstevel@tonic-gate 		    "restarting automatically",
1780Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, reason_buf);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
1830Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
1840Sstevel@tonic-gate 	 * Then close all open file descriptors in the process.
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate 	pollcleanup();
1870Sstevel@tonic-gate 	closeall(P_FINFO(p));
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Grab p_lock and begin clearing miscellaneous global process
1910Sstevel@tonic-gate 	 * state that needs to be reset before we exec the new init(1M).
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
195390Sraf 	prbarrier(p);
1960Sstevel@tonic-gate 
197390Sraf 	p->p_flag &= ~(SKILLED | SEXTKILLED | SEXITING | SDOCORE);
1980Sstevel@tonic-gate 	up->u_cmask = CMASK;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	sigemptyset(&t->t_hold);
2010Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
2020Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
2050Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	sigdelq(p, t, 0);
2080Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (p->p_killsqp) {
2110Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
2120Sstevel@tonic-gate 		p->p_killsqp = NULL;
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/*
2160Sstevel@tonic-gate 	 * Reset any signals that are ignored back to the default disposition.
2170Sstevel@tonic-gate 	 * Other u_signal members will be cleared when exec calls sigdefault().
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++) {
2200Sstevel@tonic-gate 		if (up->u_signal[i - 1] == SIG_IGN) {
2210Sstevel@tonic-gate 			up->u_signal[i - 1] = SIG_DFL;
2220Sstevel@tonic-gate 			sigemptyset(&up->u_sigmask[i - 1]);
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * Clear the current signal, any signal info associated with it, and
2280Sstevel@tonic-gate 	 * any signal information from contracts and/or contract templates.
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
2310Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
2320Sstevel@tonic-gate 	if (lwp->lwp_curinfo != NULL) {
2330Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
2340Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * Reset both the process root directory and the current working
2400Sstevel@tonic-gate 	 * directory to the root of the zone just as we do during boot.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
2430Sstevel@tonic-gate 	oldrd = up->u_rdir;
2440Sstevel@tonic-gate 	up->u_rdir = p->p_zone->zone_rootvp;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
2470Sstevel@tonic-gate 	oldcd = up->u_cdir;
2480Sstevel@tonic-gate 	up->u_cdir = p->p_zone->zone_rootvp;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (up->u_cwd != NULL) {
2510Sstevel@tonic-gate 		refstr_rele(up->u_cwd);
2520Sstevel@tonic-gate 		up->u_cwd = NULL;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (oldrd != NULL)
2580Sstevel@tonic-gate 		VN_RELE(oldrd);
2590Sstevel@tonic-gate 	if (oldcd != NULL)
2600Sstevel@tonic-gate 		VN_RELE(oldcd);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Free the controlling tty.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	mutex_enter(&pidlock);
2660Sstevel@tonic-gate 	sp = p->p_sessp;
2670Sstevel@tonic-gate 	if (sp->s_sidp == p->p_pidp && sp->s_vp != NULL) {
2680Sstevel@tonic-gate 		mutex_exit(&pidlock);
2690Sstevel@tonic-gate 		freectty(sp);
2700Sstevel@tonic-gate 	} else {
2710Sstevel@tonic-gate 		mutex_exit(&pidlock);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Now exec() the new init(1M) on top of the current process.  If we
2760Sstevel@tonic-gate 	 * succeed, the caller will treat this like a successful system call.
2770Sstevel@tonic-gate 	 * If we fail, we issue messages and the caller will proceed with exit.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	ipath = INGLOBALZONE(p) ? initname : zone_initname;
2800Sstevel@tonic-gate 	err = exec_init(ipath, 0, NULL);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (err == 0)
2830Sstevel@tonic-gate 		return (0);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
2860Sstevel@tonic-gate 	    "failed to restart init(1M) (err=%d): system reboot required", err);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
2890Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to restart init(1M) for zone %s "
2900Sstevel@tonic-gate 		    "(pid %d, err=%d): zoneadm(1M) boot required",
2910Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, err);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (-1);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Release resources.
2990Sstevel@tonic-gate  * Enter zombie state.
3000Sstevel@tonic-gate  * Wake up parent and init processes,
3010Sstevel@tonic-gate  * and dispose of children.
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate void
3040Sstevel@tonic-gate exit(int why, int what)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * If proc_exit() fails, then some other lwp in the process
3080Sstevel@tonic-gate 	 * got there first.  We just have to call lwp_exit() to allow
3090Sstevel@tonic-gate 	 * the other lwp to finish exiting the process.  Otherwise we're
3100Sstevel@tonic-gate 	 * restarting init, and should return.
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	if (proc_exit(why, what) != 0) {
3130Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
3140Sstevel@tonic-gate 		ASSERT(curproc->p_flag & SEXITLWPS);
3150Sstevel@tonic-gate 		lwp_exit();
3160Sstevel@tonic-gate 		/* NOTREACHED */
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
321390Sraf  * Set the SEXITING flag on the process, after making sure /proc does
322390Sraf  * not have it locked.  This is done in more places than proc_exit(),
323390Sraf  * so it is a separate function.
324390Sraf  */
325390Sraf void
326390Sraf proc_is_exiting(proc_t *p)
327390Sraf {
328390Sraf 	mutex_enter(&p->p_lock);
329390Sraf 	prbarrier(p);
330390Sraf 	p->p_flag |= SEXITING;
331390Sraf 	mutex_exit(&p->p_lock);
332390Sraf }
333390Sraf 
334390Sraf /*
3350Sstevel@tonic-gate  * Return value:
3360Sstevel@tonic-gate  *   1 - exitlwps() failed, call (or continue) lwp_exit()
3370Sstevel@tonic-gate  *   0 - restarting init.  Return through system call path
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate int
3400Sstevel@tonic-gate proc_exit(int why, int what)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	kthread_t *t = curthread;
3430Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3440Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3450Sstevel@tonic-gate 	zone_t *z = p->p_zone;
3460Sstevel@tonic-gate 	timeout_id_t tmp_id;
3470Sstevel@tonic-gate 	int rv;
3480Sstevel@tonic-gate 	proc_t *q;
3490Sstevel@tonic-gate 	sess_t *sp;
3500Sstevel@tonic-gate 	task_t *tk;
3510Sstevel@tonic-gate 	vnode_t *exec_vp, *execdir_vp, *cdir, *rdir;
3520Sstevel@tonic-gate 	sigqueue_t *sqp;
3530Sstevel@tonic-gate 	lwpdir_t *lwpdir;
3540Sstevel@tonic-gate 	uint_t lwpdir_sz;
3550Sstevel@tonic-gate 	lwpdir_t **tidhash;
3560Sstevel@tonic-gate 	uint_t tidhash_sz;
3570Sstevel@tonic-gate 	refstr_t *cwd;
3580Sstevel@tonic-gate 	hrtime_t hrutime, hrstime;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * Stop and discard the process's lwps except for the current one,
3620Sstevel@tonic-gate 	 * unless some other lwp beat us to it.  If exitlwps() fails then
3630Sstevel@tonic-gate 	 * return and the calling lwp will call (or continue in) lwp_exit().
3640Sstevel@tonic-gate 	 */
365390Sraf 	proc_is_exiting(p);
3660Sstevel@tonic-gate 	if (exitlwps(0) != 0)
3670Sstevel@tonic-gate 		return (1);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	DTRACE_PROC(lwp__exit);
3700Sstevel@tonic-gate 	DTRACE_PROC1(exit, int, why);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * Don't let init exit unless zone_icode() failed its exec, or
3740Sstevel@tonic-gate 	 * we are shutting down the zone or the machine.
3750Sstevel@tonic-gate 	 *
3760Sstevel@tonic-gate 	 * Since we are single threaded, we don't need to lock the
3770Sstevel@tonic-gate 	 * following accesses to zone_proc_initpid.
3780Sstevel@tonic-gate 	 */
3790Sstevel@tonic-gate 	if (p->p_pid == z->zone_proc_initpid) {
3800Sstevel@tonic-gate 		if (z->zone_boot_err == 0 &&
3810Sstevel@tonic-gate 		    zone_status_get(z) < ZONE_IS_SHUTTING_DOWN &&
3820Sstevel@tonic-gate 		    zone_status_get(global_zone) < ZONE_IS_SHUTTING_DOWN &&
3830Sstevel@tonic-gate 		    restart_init(what, why) == 0)
3840Sstevel@tonic-gate 			return (0);
3850Sstevel@tonic-gate 		/*
3860Sstevel@tonic-gate 		 * Since we didn't or couldn't restart init, we clear
3870Sstevel@tonic-gate 		 * the zone's init state and proceed with exit
3880Sstevel@tonic-gate 		 * processing.
3890Sstevel@tonic-gate 		 */
3900Sstevel@tonic-gate 		z->zone_proc_initpid = -1;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * Allocate a sigqueue now, before we grab locks.
3950Sstevel@tonic-gate 	 * It will be given to sigcld(), below.
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/*
4000Sstevel@tonic-gate 	 * revoke any doors created by the process.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	if (p->p_door_list)
4030Sstevel@tonic-gate 		door_exit();
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/*
4060Sstevel@tonic-gate 	 * Release schedctl data structures.
4070Sstevel@tonic-gate 	 */
4080Sstevel@tonic-gate 	if (p->p_pagep)
4090Sstevel@tonic-gate 		schedctl_proc_cleanup();
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * make sure all pending kaio has completed.
4130Sstevel@tonic-gate 	 */
4140Sstevel@tonic-gate 	if (p->p_aio)
4150Sstevel@tonic-gate 		aio_cleanup_exit();
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * discard the lwpchan cache.
4190Sstevel@tonic-gate 	 */
4200Sstevel@tonic-gate 	if (p->p_lcp != NULL)
4210Sstevel@tonic-gate 		lwpchan_destroy_cache(0);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * Clean up any DTrace helper actions or probes for the process.
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
4270Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
4280Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/* untimeout the realtime timers */
4320Sstevel@tonic-gate 	if (p->p_itimer != NULL)
4330Sstevel@tonic-gate 		timer_exit();
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if ((tmp_id = p->p_alarmid) != 0) {
4360Sstevel@tonic-gate 		p->p_alarmid = 0;
4370Sstevel@tonic-gate 		(void) untimeout(tmp_id);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
4420Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	pollcleanup();
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (p->p_rprof_cyclic != CYCLIC_NONE) {
4470Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4480Sstevel@tonic-gate 		cyclic_remove(p->p_rprof_cyclic);
4490Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/*
4550Sstevel@tonic-gate 	 * Clean up any DTrace probes associated with this process.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
4580Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exit_ptr != NULL);
4590Sstevel@tonic-gate 		dtrace_fasttrap_exit_ptr(p);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	while ((tmp_id = p->p_itimerid) != 0) {
4630Sstevel@tonic-gate 		p->p_itimerid = 0;
4640Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4650Sstevel@tonic-gate 		(void) untimeout(tmp_id);
4660Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	lwp_cleanup();
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/*
4720Sstevel@tonic-gate 	 * We are about to exit; prevent our resource associations from
4730Sstevel@tonic-gate 	 * being changed.
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	pool_barrier_enter();
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/*
4780Sstevel@tonic-gate 	 * Block the process against /proc now that we have really
4790Sstevel@tonic-gate 	 * acquired p->p_lock (to manipulate p_tlist at least).
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	prbarrier(p);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate #ifdef	SUN_SRC_COMPAT
4840Sstevel@tonic-gate 	if (code == CLD_KILLED)
4850Sstevel@tonic-gate 		u.u_acflag |= AXSIG;
4860Sstevel@tonic-gate #endif
4870Sstevel@tonic-gate 	sigfillset(&p->p_ignore);
4880Sstevel@tonic-gate 	sigemptyset(&p->p_siginfo);
4890Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
4900Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
4910Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
4920Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
4930Sstevel@tonic-gate 	sigemptyset(&p->p_sigmask);
4940Sstevel@tonic-gate 	sigdelq(p, t, 0);
4950Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
4960Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
4970Sstevel@tonic-gate 	p->p_flag &= ~(SKILLED | SEXTKILLED);
4980Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
4990Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
5000Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	t->t_proc_flag |= TP_LWPEXIT;
5040Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
5050Sstevel@tonic-gate 	prlwpexit(t);		/* notify /proc */
5060Sstevel@tonic-gate 	lwp_hash_out(p, t->t_tid);
5070Sstevel@tonic-gate 	prexit(p);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	p->p_lwpcnt = 0;
5100Sstevel@tonic-gate 	p->p_tlist = NULL;
5110Sstevel@tonic-gate 	sigqfree(p);
5120Sstevel@tonic-gate 	term_mstate(t);
5130Sstevel@tonic-gate 	p->p_mterm = gethrtime();
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	exec_vp = p->p_exec;
5160Sstevel@tonic-gate 	execdir_vp = p->p_execdir;
5170Sstevel@tonic-gate 	p->p_exec = NULLVP;
5180Sstevel@tonic-gate 	p->p_execdir = NULLVP;
5190Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5200Sstevel@tonic-gate 	if (exec_vp)
5210Sstevel@tonic-gate 		VN_RELE(exec_vp);
5220Sstevel@tonic-gate 	if (execdir_vp)
5230Sstevel@tonic-gate 		VN_RELE(execdir_vp);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	pr_free_watched_pages(p);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	closeall(P_FINFO(p));
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	mutex_enter(&pidlock);
5300Sstevel@tonic-gate 	sp = p->p_sessp;
5310Sstevel@tonic-gate 	if (sp->s_sidp == p->p_pidp && sp->s_vp != NULL) {
5320Sstevel@tonic-gate 		mutex_exit(&pidlock);
5330Sstevel@tonic-gate 		freectty(sp);
5340Sstevel@tonic-gate 	} else
5350Sstevel@tonic-gate 		mutex_exit(&pidlock);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate #if defined(__sparc)
5380Sstevel@tonic-gate 	if (p->p_utraps != NULL)
5390Sstevel@tonic-gate 		utrap_free(p);
5400Sstevel@tonic-gate #endif
5410Sstevel@tonic-gate 	if (p->p_semacct)			/* IPC semaphore exit */
5420Sstevel@tonic-gate 		semexit(p);
5430Sstevel@tonic-gate 	rv = wstat(why, what);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	acct(rv & 0xff);
5460Sstevel@tonic-gate 	exacct_commit_proc(p, rv);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * Release any resources associated with C2 auditing
5500Sstevel@tonic-gate 	 */
5510Sstevel@tonic-gate #ifdef C2_AUDIT
5520Sstevel@tonic-gate 	if (audit_active) {
5530Sstevel@tonic-gate 		/*
5540Sstevel@tonic-gate 		 * audit exit system call
5550Sstevel@tonic-gate 		 */
5560Sstevel@tonic-gate 		audit_exit(why, what);
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate #endif
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/*
5610Sstevel@tonic-gate 	 * Free address space.
5620Sstevel@tonic-gate 	 */
5630Sstevel@tonic-gate 	relvm();
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Release held contracts.
5670Sstevel@tonic-gate 	 */
5680Sstevel@tonic-gate 	contract_exit(p);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * Depart our encapsulating process contract.
5720Sstevel@tonic-gate 	 */
5730Sstevel@tonic-gate 	if ((p->p_flag & SSYS) == 0) {
5740Sstevel@tonic-gate 		ASSERT(p->p_ct_process);
5750Sstevel@tonic-gate 		contract_process_exit(p->p_ct_process, p, rv);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * Remove pool association, and block if requested by pool_do_bind.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5820Sstevel@tonic-gate 	ASSERT(p->p_pool->pool_ref > 0);
5830Sstevel@tonic-gate 	atomic_add_32(&p->p_pool->pool_ref, -1);
5840Sstevel@tonic-gate 	p->p_pool = pool_default;
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * Now that our address space has been freed and all other threads
5870Sstevel@tonic-gate 	 * in this process have exited, set the PEXITED pool flag.  This
5880Sstevel@tonic-gate 	 * tells the pools subsystems to ignore this process if it was
5890Sstevel@tonic-gate 	 * requested to rebind this process to a new pool.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	p->p_poolflag |= PEXITED;
5920Sstevel@tonic-gate 	pool_barrier_exit();
5930Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	mutex_enter(&pidlock);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * Delete this process from the newstate list of its parent. We
5990Sstevel@tonic-gate 	 * will put it in the right place in the sigcld in the end.
6000Sstevel@tonic-gate 	 */
6010Sstevel@tonic-gate 	delete_ns(p->p_parent, p);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * Reassign the orphans to the next of kin.
6050Sstevel@tonic-gate 	 * Don't rearrange init's orphanage.
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	if ((q = p->p_orphan) != NULL && p != proc_init) {
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		proc_t *nokp = p->p_nextofkin;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		for (;;) {
6120Sstevel@tonic-gate 			q->p_nextofkin = nokp;
6130Sstevel@tonic-gate 			if (q->p_nextorph == NULL)
6140Sstevel@tonic-gate 				break;
6150Sstevel@tonic-gate 			q = q->p_nextorph;
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 		q->p_nextorph = nokp->p_orphan;
6180Sstevel@tonic-gate 		nokp->p_orphan = p->p_orphan;
6190Sstevel@tonic-gate 		p->p_orphan = NULL;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 * Reassign the children to init.
6240Sstevel@tonic-gate 	 * Don't try to assign init's children to init.
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	if ((q = p->p_child) != NULL && p != proc_init) {
6270Sstevel@tonic-gate 		struct proc	*np;
6280Sstevel@tonic-gate 		struct proc	*initp = proc_init;
6290Sstevel@tonic-gate 		boolean_t	setzonetop = B_FALSE;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		if (!INGLOBALZONE(curproc))
6320Sstevel@tonic-gate 			setzonetop = B_TRUE;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		pgdetach(p);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		do {
6370Sstevel@tonic-gate 			np = q->p_sibling;
6380Sstevel@tonic-gate 			/*
6390Sstevel@tonic-gate 			 * Delete it from its current parent new state
6400Sstevel@tonic-gate 			 * list and add it to init new state list
6410Sstevel@tonic-gate 			 */
6420Sstevel@tonic-gate 			delete_ns(q->p_parent, q);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 			q->p_ppid = 1;
6450Sstevel@tonic-gate 			if (setzonetop) {
6460Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
6470Sstevel@tonic-gate 				q->p_flag |= SZONETOP;
6480Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
6490Sstevel@tonic-gate 			}
6500Sstevel@tonic-gate 			q->p_parent = initp;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 			/*
6530Sstevel@tonic-gate 			 * Since q will be the first child,
6540Sstevel@tonic-gate 			 * it will not have a previous sibling.
6550Sstevel@tonic-gate 			 */
6560Sstevel@tonic-gate 			q->p_psibling = NULL;
6570Sstevel@tonic-gate 			if (initp->p_child) {
6580Sstevel@tonic-gate 				initp->p_child->p_psibling = q;
6590Sstevel@tonic-gate 			}
6600Sstevel@tonic-gate 			q->p_sibling = initp->p_child;
6610Sstevel@tonic-gate 			initp->p_child = q;
6620Sstevel@tonic-gate 			if (q->p_proc_flag & P_PR_PTRACE) {
6630Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
6640Sstevel@tonic-gate 				sigtoproc(q, NULL, SIGKILL);
6650Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
6660Sstevel@tonic-gate 			}
6670Sstevel@tonic-gate 			/*
6680Sstevel@tonic-gate 			 * sigcld() will add the child to parents
6690Sstevel@tonic-gate 			 * newstate list.
6700Sstevel@tonic-gate 			 */
6710Sstevel@tonic-gate 			if (q->p_stat == SZOMB)
6720Sstevel@tonic-gate 				sigcld(q, NULL);
6730Sstevel@tonic-gate 		} while ((q = np) != NULL);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		p->p_child = NULL;
6760Sstevel@tonic-gate 		ASSERT(p->p_child_ns == NULL);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	TRACE_1(TR_FAC_PROC, TR_PROC_EXIT, "proc_exit: %p", p);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6820Sstevel@tonic-gate 	CL_EXIT(curthread); /* tell the scheduler that curthread is exiting */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	hrutime = mstate_aggr_state(p, LMS_USER);
6850Sstevel@tonic-gate 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
6860Sstevel@tonic-gate 	p->p_utime = (clock_t)NSEC_TO_TICK(hrutime) + p->p_cutime;
6870Sstevel@tonic-gate 	p->p_stime = (clock_t)NSEC_TO_TICK(hrstime) + p->p_cstime;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	p->p_acct[LMS_USER]	+= p->p_cacct[LMS_USER];
6900Sstevel@tonic-gate 	p->p_acct[LMS_SYSTEM]	+= p->p_cacct[LMS_SYSTEM];
6910Sstevel@tonic-gate 	p->p_acct[LMS_TRAP]	+= p->p_cacct[LMS_TRAP];
6920Sstevel@tonic-gate 	p->p_acct[LMS_TFAULT]	+= p->p_cacct[LMS_TFAULT];
6930Sstevel@tonic-gate 	p->p_acct[LMS_DFAULT]	+= p->p_cacct[LMS_DFAULT];
6940Sstevel@tonic-gate 	p->p_acct[LMS_KFAULT]	+= p->p_cacct[LMS_KFAULT];
6950Sstevel@tonic-gate 	p->p_acct[LMS_USER_LOCK] += p->p_cacct[LMS_USER_LOCK];
6960Sstevel@tonic-gate 	p->p_acct[LMS_SLEEP]	+= p->p_cacct[LMS_SLEEP];
6970Sstevel@tonic-gate 	p->p_acct[LMS_WAIT_CPU]	+= p->p_cacct[LMS_WAIT_CPU];
6980Sstevel@tonic-gate 	p->p_acct[LMS_STOPPED]	+= p->p_cacct[LMS_STOPPED];
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	p->p_ru.minflt	+= p->p_cru.minflt;
7010Sstevel@tonic-gate 	p->p_ru.majflt	+= p->p_cru.majflt;
7020Sstevel@tonic-gate 	p->p_ru.nswap	+= p->p_cru.nswap;
7030Sstevel@tonic-gate 	p->p_ru.inblock	+= p->p_cru.inblock;
7040Sstevel@tonic-gate 	p->p_ru.oublock	+= p->p_cru.oublock;
7050Sstevel@tonic-gate 	p->p_ru.msgsnd	+= p->p_cru.msgsnd;
7060Sstevel@tonic-gate 	p->p_ru.msgrcv	+= p->p_cru.msgrcv;
7070Sstevel@tonic-gate 	p->p_ru.nsignals += p->p_cru.nsignals;
7080Sstevel@tonic-gate 	p->p_ru.nvcsw	+= p->p_cru.nvcsw;
7090Sstevel@tonic-gate 	p->p_ru.nivcsw	+= p->p_cru.nivcsw;
7100Sstevel@tonic-gate 	p->p_ru.sysc	+= p->p_cru.sysc;
7110Sstevel@tonic-gate 	p->p_ru.ioch	+= p->p_cru.ioch;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	p->p_stat = SZOMB;
7140Sstevel@tonic-gate 	p->p_proc_flag &= ~P_PR_PTRACE;
7150Sstevel@tonic-gate 	p->p_wdata = what;
7160Sstevel@tonic-gate 	p->p_wcode = (char)why;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	cdir = PTOU(p)->u_cdir;
7190Sstevel@tonic-gate 	rdir = PTOU(p)->u_rdir;
7200Sstevel@tonic-gate 	cwd = PTOU(p)->u_cwd;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*
7230Sstevel@tonic-gate 	 * Release resource controls, as they are no longer enforceable.
7240Sstevel@tonic-gate 	 */
7250Sstevel@tonic-gate 	rctl_set_free(p->p_rctls);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/*
7280Sstevel@tonic-gate 	 * Give up task and project memberships.  Decrement tk_nlwps counter
7290Sstevel@tonic-gate 	 * for our task.max-lwps resource control.  An extended accounting
7300Sstevel@tonic-gate 	 * record, if that facility is active, is scheduled to be written.
7310Sstevel@tonic-gate 	 * Zombie processes are false members of task0 for the remainder of
7320Sstevel@tonic-gate 	 * their lifetime; no accounting information is recorded for them.
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	tk = p->p_task;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
7370Sstevel@tonic-gate 	tk->tk_nlwps--;
7380Sstevel@tonic-gate 	tk->tk_proj->kpj_nlwps--;
7390Sstevel@tonic-gate 	p->p_zone->zone_nlwps--;
7400Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
7410Sstevel@tonic-gate 	task_detach(p);
7420Sstevel@tonic-gate 	p->p_task = task0p;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Clear the lwp directory and the lwpid hash table
7460Sstevel@tonic-gate 	 * now that /proc can't bother us any more.
7470Sstevel@tonic-gate 	 * We free the memory below, after dropping p->p_lock.
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	lwpdir = p->p_lwpdir;
7500Sstevel@tonic-gate 	lwpdir_sz = p->p_lwpdir_sz;
7510Sstevel@tonic-gate 	tidhash = p->p_tidhash;
7520Sstevel@tonic-gate 	tidhash_sz = p->p_tidhash_sz;
7530Sstevel@tonic-gate 	p->p_lwpdir = NULL;
7540Sstevel@tonic-gate 	p->p_lwpfree = NULL;
7550Sstevel@tonic-gate 	p->p_lwpdir_sz = 0;
7560Sstevel@tonic-gate 	p->p_tidhash = NULL;
7570Sstevel@tonic-gate 	p->p_tidhash_sz = 0;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
760*1217Srab 	 * If the process has context ops installed, call the exit routine
761*1217Srab 	 * on behalf of this last remaining thread. Normally exitpctx() is
762*1217Srab 	 * called during thread_exit() or lwp_exit(), but because this is the
763*1217Srab 	 * last thread in the process, we must call it here. By the time
764*1217Srab 	 * thread_exit() is called (below), the association with the relevant
765*1217Srab 	 * process has been lost.
766*1217Srab 	 *
767*1217Srab 	 * We also free the context here.
768*1217Srab 	 */
769*1217Srab 	if (p->p_pctx) {
770*1217Srab 		kpreempt_disable();
771*1217Srab 		exitpctx(p);
772*1217Srab 		kpreempt_enable();
773*1217Srab 
774*1217Srab 		freepctx(p, 0);
775*1217Srab 	}
776*1217Srab 
777*1217Srab 	/*
7780Sstevel@tonic-gate 	 * curthread's proc pointer is changed to point at p0 because
7790Sstevel@tonic-gate 	 * curthread's original proc pointer can be freed as soon as
7800Sstevel@tonic-gate 	 * the child sends a SIGCLD to its parent.
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	t->t_procp = &p0;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7850Sstevel@tonic-gate 	sigcld(p, sqp);
7860Sstevel@tonic-gate 	mutex_exit(&pidlock);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	task_rele(tk);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	kmem_free(lwpdir, lwpdir_sz * sizeof (lwpdir_t));
7910Sstevel@tonic-gate 	kmem_free(tidhash, tidhash_sz * sizeof (lwpdir_t *));
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * We don't release u_cdir and u_rdir until SZOMB is set.
7950Sstevel@tonic-gate 	 * This protects us against dofusers().
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	VN_RELE(cdir);
7980Sstevel@tonic-gate 	if (rdir)
7990Sstevel@tonic-gate 		VN_RELE(rdir);
8000Sstevel@tonic-gate 	if (cwd)
8010Sstevel@tonic-gate 		refstr_rele(cwd);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	lwp_pcb_exit();
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	thread_exit();
8060Sstevel@tonic-gate 	/* NOTREACHED */
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate  * Format siginfo structure for wait system calls.
8110Sstevel@tonic-gate  */
8120Sstevel@tonic-gate void
8130Sstevel@tonic-gate winfo(proc_t *pp, k_siginfo_t *ip, int waitflag)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	bzero(ip, sizeof (k_siginfo_t));
8180Sstevel@tonic-gate 	ip->si_signo = SIGCLD;
8190Sstevel@tonic-gate 	ip->si_code = pp->p_wcode;
8200Sstevel@tonic-gate 	ip->si_pid = pp->p_pid;
8210Sstevel@tonic-gate 	ip->si_ctid = PRCTID(pp);
8220Sstevel@tonic-gate 	ip->si_zoneid = pp->p_zone->zone_id;
8230Sstevel@tonic-gate 	ip->si_status = pp->p_wdata;
8240Sstevel@tonic-gate 	ip->si_stime = pp->p_stime;
8250Sstevel@tonic-gate 	ip->si_utime = pp->p_utime;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (waitflag) {
8280Sstevel@tonic-gate 		pp->p_wcode = 0;
8290Sstevel@tonic-gate 		pp->p_wdata = 0;
8300Sstevel@tonic-gate 		pp->p_pidflag &= ~CLDPEND;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate /*
8350Sstevel@tonic-gate  * Wait system call.
8360Sstevel@tonic-gate  * Search for a terminated (zombie) child,
8370Sstevel@tonic-gate  * finally lay it to rest, and collect its status.
8380Sstevel@tonic-gate  * Look also for stopped children,
8390Sstevel@tonic-gate  * and pass back status from them.
8400Sstevel@tonic-gate  */
8410Sstevel@tonic-gate int
8420Sstevel@tonic-gate waitid(idtype_t idtype, id_t id, k_siginfo_t *ip, int options)
8430Sstevel@tonic-gate {
8440Sstevel@tonic-gate 	int found;
8450Sstevel@tonic-gate 	proc_t *cp, *pp;
8460Sstevel@tonic-gate 	proc_t **nsp;
8470Sstevel@tonic-gate 	int proc_gone;
8480Sstevel@tonic-gate 	int waitflag = !(options & WNOWAIT);
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * Obsolete flag, defined here only for binary compatibility
8520Sstevel@tonic-gate 	 * with old statically linked executables.  Delete this when
8530Sstevel@tonic-gate 	 * we no longer care about these old and broken applications.
8540Sstevel@tonic-gate 	 */
8550Sstevel@tonic-gate #define	_WNOCHLD	0400
8560Sstevel@tonic-gate 	options &= ~_WNOCHLD;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if (options == 0 || (options & ~WOPTMASK))
8590Sstevel@tonic-gate 		return (EINVAL);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	switch (idtype) {
8620Sstevel@tonic-gate 	case P_PID:
8630Sstevel@tonic-gate 	case P_PGID:
8640Sstevel@tonic-gate 		if (id < 0 || id >= maxpid)
8650Sstevel@tonic-gate 			return (EINVAL);
8660Sstevel@tonic-gate 		/* FALLTHROUGH */
8670Sstevel@tonic-gate 	case P_ALL:
8680Sstevel@tonic-gate 		break;
8690Sstevel@tonic-gate 	default:
8700Sstevel@tonic-gate 		return (EINVAL);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	pp = ttoproc(curthread);
874749Ssusans 
8750Sstevel@tonic-gate 	/*
8760Sstevel@tonic-gate 	 * lock parent mutex so that sibling chain can be searched.
8770Sstevel@tonic-gate 	 */
8780Sstevel@tonic-gate 	mutex_enter(&pidlock);
879749Ssusans 
880749Ssusans 	/*
881749Ssusans 	 * if we are only looking for exited processes and child_ns list
882749Ssusans 	 * is empty no reason to look at all children.
883749Ssusans 	 */
884749Ssusans 	if (idtype == P_ALL &&
885749Ssusans 	    (options & (WOPTMASK & ~WNOWAIT)) == (WNOHANG | WEXITED) &&
886749Ssusans 		pp->p_child_ns == NULL) {
887749Ssusans 
888749Ssusans 		if (pp->p_child) {
889749Ssusans 			mutex_exit(&pidlock);
890749Ssusans 			bzero(ip, sizeof (k_siginfo_t));
891749Ssusans 			return (0);
892749Ssusans 		}
893749Ssusans 		mutex_exit(&pidlock);
894749Ssusans 		return (ECHILD);
895749Ssusans 	}
896749Ssusans 
8970Sstevel@tonic-gate 	while ((cp = pp->p_child) != NULL) {
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 		proc_gone = 0;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		for (nsp = &pp->p_child_ns; *nsp; nsp = &(*nsp)->p_sibling_ns) {
9020Sstevel@tonic-gate 			if (idtype == P_PID && id != (*nsp)->p_pid) {
9030Sstevel@tonic-gate 				continue;
9040Sstevel@tonic-gate 			}
9050Sstevel@tonic-gate 			if (idtype == P_PGID && id != (*nsp)->p_pgrp) {
9060Sstevel@tonic-gate 				continue;
9070Sstevel@tonic-gate 			}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 			switch ((*nsp)->p_wcode) {
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 			case CLD_TRAPPED:
9120Sstevel@tonic-gate 			case CLD_STOPPED:
9130Sstevel@tonic-gate 			case CLD_CONTINUED:
9140Sstevel@tonic-gate 				cmn_err(CE_PANIC,
9150Sstevel@tonic-gate 				    "waitid: wrong state %d on the p_newstate"
9160Sstevel@tonic-gate 				    " list", (*nsp)->p_wcode);
9170Sstevel@tonic-gate 				break;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 			case CLD_EXITED:
9200Sstevel@tonic-gate 			case CLD_DUMPED:
9210Sstevel@tonic-gate 			case CLD_KILLED:
9220Sstevel@tonic-gate 				if (!(options & WEXITED)) {
9230Sstevel@tonic-gate 					/*
9240Sstevel@tonic-gate 					 * Count how many are already gone
9250Sstevel@tonic-gate 					 * for good.
9260Sstevel@tonic-gate 					 */
9270Sstevel@tonic-gate 					proc_gone++;
9280Sstevel@tonic-gate 					break;
9290Sstevel@tonic-gate 				}
9300Sstevel@tonic-gate 				if (!waitflag) {
9310Sstevel@tonic-gate 					winfo((*nsp), ip, 0);
9320Sstevel@tonic-gate 				} else {
9330Sstevel@tonic-gate 					proc_t *xp = *nsp;
9340Sstevel@tonic-gate 					winfo(xp, ip, 1);
9350Sstevel@tonic-gate 					freeproc(xp);
9360Sstevel@tonic-gate 				}
9370Sstevel@tonic-gate 				mutex_exit(&pidlock);
9380Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
9390Sstevel@tonic-gate 					sigcld_delete(ip);
9400Sstevel@tonic-gate 					sigcld_repost();
9410Sstevel@tonic-gate 				}
9420Sstevel@tonic-gate 				return (0);
9430Sstevel@tonic-gate 			}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 			if (idtype == P_PID)
9460Sstevel@tonic-gate 				break;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 		/*
9500Sstevel@tonic-gate 		 * Wow! None of the threads on the p_sibling_ns list were
9510Sstevel@tonic-gate 		 * interesting threads. Check all the kids!
9520Sstevel@tonic-gate 		 */
9530Sstevel@tonic-gate 		found = 0;
9540Sstevel@tonic-gate 		cp = pp->p_child;
9550Sstevel@tonic-gate 		do {
9560Sstevel@tonic-gate 			if (idtype == P_PID && id != cp->p_pid) {
9570Sstevel@tonic-gate 				continue;
9580Sstevel@tonic-gate 			}
9590Sstevel@tonic-gate 			if (idtype == P_PGID && id != cp->p_pgrp) {
9600Sstevel@tonic-gate 				continue;
9610Sstevel@tonic-gate 			}
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 			found++;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 			switch (cp->p_wcode) {
9660Sstevel@tonic-gate 			case CLD_TRAPPED:
9670Sstevel@tonic-gate 				if (!(options & WTRAPPED))
9680Sstevel@tonic-gate 					break;
9690Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
9700Sstevel@tonic-gate 				mutex_exit(&pidlock);
9710Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
9720Sstevel@tonic-gate 					sigcld_delete(ip);
9730Sstevel@tonic-gate 					sigcld_repost();
9740Sstevel@tonic-gate 				}
9750Sstevel@tonic-gate 				return (0);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 			case CLD_STOPPED:
9780Sstevel@tonic-gate 				if (!(options & WSTOPPED))
9790Sstevel@tonic-gate 					break;
9800Sstevel@tonic-gate 				/* Is it still stopped? */
9810Sstevel@tonic-gate 				mutex_enter(&cp->p_lock);
9820Sstevel@tonic-gate 				if (!jobstopped(cp)) {
9830Sstevel@tonic-gate 					mutex_exit(&cp->p_lock);
9840Sstevel@tonic-gate 					break;
9850Sstevel@tonic-gate 				}
9860Sstevel@tonic-gate 				mutex_exit(&cp->p_lock);
9870Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
9880Sstevel@tonic-gate 				mutex_exit(&pidlock);
9890Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
9900Sstevel@tonic-gate 					sigcld_delete(ip);
9910Sstevel@tonic-gate 					sigcld_repost();
9920Sstevel@tonic-gate 				}
9930Sstevel@tonic-gate 				return (0);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 			case CLD_CONTINUED:
9960Sstevel@tonic-gate 				if (!(options & WCONTINUED))
9970Sstevel@tonic-gate 					break;
9980Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
9990Sstevel@tonic-gate 				mutex_exit(&pidlock);
10000Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10010Sstevel@tonic-gate 					sigcld_delete(ip);
10020Sstevel@tonic-gate 					sigcld_repost();
10030Sstevel@tonic-gate 				}
10040Sstevel@tonic-gate 				return (0);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 			case CLD_EXITED:
10070Sstevel@tonic-gate 			case CLD_DUMPED:
10080Sstevel@tonic-gate 			case CLD_KILLED:
10090Sstevel@tonic-gate 				/*
10100Sstevel@tonic-gate 				 * Don't complain if a process was found in
10110Sstevel@tonic-gate 				 * the first loop but we broke out of the loop
10120Sstevel@tonic-gate 				 * because of the arguments passed to us.
10130Sstevel@tonic-gate 				 */
10140Sstevel@tonic-gate 				if (proc_gone == 0) {
10150Sstevel@tonic-gate 					cmn_err(CE_PANIC,
10160Sstevel@tonic-gate 					    "waitid: wrong state on the"
10170Sstevel@tonic-gate 					    " p_child list");
10180Sstevel@tonic-gate 				} else {
10190Sstevel@tonic-gate 					break;
10200Sstevel@tonic-gate 				}
10210Sstevel@tonic-gate 			}
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 			if (idtype == P_PID)
10240Sstevel@tonic-gate 				break;
10250Sstevel@tonic-gate 		} while ((cp = cp->p_sibling) != NULL);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		/*
10280Sstevel@tonic-gate 		 * If we found no interesting processes at all,
10290Sstevel@tonic-gate 		 * break out and return ECHILD.
10300Sstevel@tonic-gate 		 */
10310Sstevel@tonic-gate 		if (found + proc_gone == 0)
10320Sstevel@tonic-gate 			break;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 		if (options & WNOHANG) {
10350Sstevel@tonic-gate 			bzero(ip, sizeof (k_siginfo_t));
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * We should set ip->si_signo = SIGCLD,
10380Sstevel@tonic-gate 			 * but there is an SVVS test that expects
10390Sstevel@tonic-gate 			 * ip->si_signo to be zero in this case.
10400Sstevel@tonic-gate 			 */
10410Sstevel@tonic-gate 			mutex_exit(&pidlock);
10420Sstevel@tonic-gate 			return (0);
10430Sstevel@tonic-gate 		}
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 		/*
10460Sstevel@tonic-gate 		 * If we found no processes of interest that could
10470Sstevel@tonic-gate 		 * change state while we wait, we don't wait at all.
10480Sstevel@tonic-gate 		 * Get out with ECHILD according to SVID.
10490Sstevel@tonic-gate 		 */
10500Sstevel@tonic-gate 		if (found == proc_gone)
10510Sstevel@tonic-gate 			break;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		if (!cv_wait_sig_swap(&pp->p_cv, &pidlock)) {
10540Sstevel@tonic-gate 			mutex_exit(&pidlock);
10550Sstevel@tonic-gate 			return (EINTR);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 	mutex_exit(&pidlock);
10590Sstevel@tonic-gate 	return (ECHILD);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * For implementations that don't require binary compatibility,
10640Sstevel@tonic-gate  * the wait system call may be made into a library call to the
10650Sstevel@tonic-gate  * waitid system call.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate int64_t
10680Sstevel@tonic-gate wait(void)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	int error;
10710Sstevel@tonic-gate 	k_siginfo_t info;
10720Sstevel@tonic-gate 	rval_t	r;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (error =  waitid(P_ALL, (id_t)0, &info, WEXITED|WTRAPPED))
10750Sstevel@tonic-gate 		return (set_errno(error));
10760Sstevel@tonic-gate 	r.r_val1 = info.si_pid;
10770Sstevel@tonic-gate 	r.r_val2 = wstat(info.si_code, info.si_status);
10780Sstevel@tonic-gate 	return (r.r_vals);
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate int
10820Sstevel@tonic-gate waitsys(idtype_t idtype, id_t id, siginfo_t *infop, int options)
10830Sstevel@tonic-gate {
10840Sstevel@tonic-gate 	int error;
10850Sstevel@tonic-gate 	k_siginfo_t info;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
10880Sstevel@tonic-gate 		return (set_errno(error));
10890Sstevel@tonic-gate 	if (copyout(&info, infop, sizeof (k_siginfo_t)))
10900Sstevel@tonic-gate 		return (set_errno(EFAULT));
10910Sstevel@tonic-gate 	return (0);
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate int
10970Sstevel@tonic-gate waitsys32(idtype_t idtype, id_t id, siginfo_t *infop, int options)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	int error;
11000Sstevel@tonic-gate 	k_siginfo_t info;
11010Sstevel@tonic-gate 	siginfo32_t info32;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
11040Sstevel@tonic-gate 		return (set_errno(error));
11050Sstevel@tonic-gate 	siginfo_kto32(&info, &info32);
11060Sstevel@tonic-gate 	if (copyout(&info32, infop, sizeof (info32)))
11070Sstevel@tonic-gate 		return (set_errno(EFAULT));
11080Sstevel@tonic-gate 	return (0);
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate void
11140Sstevel@tonic-gate proc_detach(proc_t *p)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate 	proc_t *q;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	q = p->p_parent;
11210Sstevel@tonic-gate 	ASSERT(q != NULL);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	/*
11240Sstevel@tonic-gate 	 * Take it off the newstate list of its parent
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	delete_ns(q, p);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if (q->p_child == p) {
11290Sstevel@tonic-gate 		q->p_child = p->p_sibling;
11300Sstevel@tonic-gate 		/*
11310Sstevel@tonic-gate 		 * If the parent has no children, it better not
11320Sstevel@tonic-gate 		 * have any with new states either!
11330Sstevel@tonic-gate 		 */
11340Sstevel@tonic-gate 		ASSERT(q->p_child ? 1 : q->p_child_ns == NULL);
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	if (p->p_sibling) {
11380Sstevel@tonic-gate 		p->p_sibling->p_psibling = p->p_psibling;
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if (p->p_psibling) {
11420Sstevel@tonic-gate 		p->p_psibling->p_sibling = p->p_sibling;
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate /*
11470Sstevel@tonic-gate  * Remove zombie children from the process table.
11480Sstevel@tonic-gate  */
11490Sstevel@tonic-gate void
11500Sstevel@tonic-gate freeproc(proc_t *p)
11510Sstevel@tonic-gate {
11520Sstevel@tonic-gate 	proc_t *q;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	ASSERT(p->p_stat == SZOMB);
11550Sstevel@tonic-gate 	ASSERT(p->p_tlist == NULL);
11560Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
11590Sstevel@tonic-gate 	if (p->p_killsqp) {
11600Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
11610Sstevel@tonic-gate 		p->p_killsqp = NULL;
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	prfree(p);	/* inform /proc */
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	/*
11670Sstevel@tonic-gate 	 * Don't free the init processes.
11680Sstevel@tonic-gate 	 * Other dying processes will access it.
11690Sstevel@tonic-gate 	 */
11700Sstevel@tonic-gate 	if (p == proc_init)
11710Sstevel@tonic-gate 		return;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/*
11750Sstevel@tonic-gate 	 * We wait until now to free the cred structure because a
11760Sstevel@tonic-gate 	 * zombie process's credentials may be examined by /proc.
11770Sstevel@tonic-gate 	 * No cred locking needed because there are no threads at this point.
11780Sstevel@tonic-gate 	 */
11790Sstevel@tonic-gate 	upcount_dec(crgetruid(p->p_cred), crgetzoneid(p->p_cred));
11800Sstevel@tonic-gate 	crfree(p->p_cred);
11810Sstevel@tonic-gate 	if (p->p_corefile != NULL) {
11820Sstevel@tonic-gate 		corectl_path_rele(p->p_corefile);
11830Sstevel@tonic-gate 		p->p_corefile = NULL;
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 	if (p->p_content != NULL) {
11860Sstevel@tonic-gate 		corectl_content_rele(p->p_content);
11870Sstevel@tonic-gate 		p->p_content = NULL;
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if (p->p_nextofkin && !((p->p_nextofkin->p_flag & SNOWAIT) ||
11910Sstevel@tonic-gate 	    (PTOU(p->p_nextofkin)->u_signal[SIGCLD - 1] == SIG_IGN))) {
11920Sstevel@tonic-gate 		/*
11930Sstevel@tonic-gate 		 * This should still do the right thing since p_utime/stime
11940Sstevel@tonic-gate 		 * get set to the correct value on process exit, so it
11950Sstevel@tonic-gate 		 * should get properly updated
11960Sstevel@tonic-gate 		 */
11970Sstevel@tonic-gate 		p->p_nextofkin->p_cutime += p->p_utime;
11980Sstevel@tonic-gate 		p->p_nextofkin->p_cstime += p->p_stime;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER] += p->p_acct[LMS_USER];
12010Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SYSTEM] += p->p_acct[LMS_SYSTEM];
12020Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TRAP] += p->p_acct[LMS_TRAP];
12030Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TFAULT] += p->p_acct[LMS_TFAULT];
12040Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_DFAULT] += p->p_acct[LMS_DFAULT];
12050Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_KFAULT] += p->p_acct[LMS_KFAULT];
12060Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER_LOCK]
12070Sstevel@tonic-gate 		    += p->p_acct[LMS_USER_LOCK];
12080Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SLEEP] += p->p_acct[LMS_SLEEP];
12090Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_WAIT_CPU]
12100Sstevel@tonic-gate 		    += p->p_acct[LMS_WAIT_CPU];
12110Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_STOPPED] += p->p_acct[LMS_STOPPED];
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		p->p_nextofkin->p_cru.minflt	+= p->p_ru.minflt;
12140Sstevel@tonic-gate 		p->p_nextofkin->p_cru.majflt	+= p->p_ru.majflt;
12150Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nswap	+= p->p_ru.nswap;
12160Sstevel@tonic-gate 		p->p_nextofkin->p_cru.inblock	+= p->p_ru.inblock;
12170Sstevel@tonic-gate 		p->p_nextofkin->p_cru.oublock	+= p->p_ru.oublock;
12180Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgsnd	+= p->p_ru.msgsnd;
12190Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgrcv	+= p->p_ru.msgrcv;
12200Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nsignals	+= p->p_ru.nsignals;
12210Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nvcsw	+= p->p_ru.nvcsw;
12220Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nivcsw	+= p->p_ru.nivcsw;
12230Sstevel@tonic-gate 		p->p_nextofkin->p_cru.sysc	+= p->p_ru.sysc;
12240Sstevel@tonic-gate 		p->p_nextofkin->p_cru.ioch	+= p->p_ru.ioch;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	q = p->p_nextofkin;
12290Sstevel@tonic-gate 	if (q && q->p_orphan == p)
12300Sstevel@tonic-gate 		q->p_orphan = p->p_nextorph;
12310Sstevel@tonic-gate 	else if (q) {
12320Sstevel@tonic-gate 		for (q = q->p_orphan; q; q = q->p_nextorph)
12330Sstevel@tonic-gate 			if (q->p_nextorph == p)
12340Sstevel@tonic-gate 				break;
12350Sstevel@tonic-gate 		ASSERT(q && q->p_nextorph == p);
12360Sstevel@tonic-gate 		q->p_nextorph = p->p_nextorph;
12370Sstevel@tonic-gate 	}
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	proc_detach(p);
12400Sstevel@tonic-gate 	pid_exit(p);	/* frees pid and proc structure */
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * Delete process "child" from the newstate list of process "parent"
12450Sstevel@tonic-gate  */
12460Sstevel@tonic-gate void
12470Sstevel@tonic-gate delete_ns(proc_t *parent, proc_t *child)
12480Sstevel@tonic-gate {
12490Sstevel@tonic-gate 	proc_t **ns;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
12520Sstevel@tonic-gate 	ASSERT(child->p_parent == parent);
12530Sstevel@tonic-gate 	for (ns = &parent->p_child_ns; *ns != NULL; ns = &(*ns)->p_sibling_ns) {
12540Sstevel@tonic-gate 		if (*ns == child) {
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			ASSERT((*ns)->p_parent == parent);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 			*ns = child->p_sibling_ns;
12590Sstevel@tonic-gate 			child->p_sibling_ns = NULL;
12600Sstevel@tonic-gate 			return;
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate /*
12660Sstevel@tonic-gate  * Add process "child" to the new state list of process "parent"
12670Sstevel@tonic-gate  */
12680Sstevel@tonic-gate void
12690Sstevel@tonic-gate add_ns(proc_t *parent, proc_t *child)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	ASSERT(child->p_sibling_ns == NULL);
12720Sstevel@tonic-gate 	child->p_sibling_ns = parent->p_child_ns;
12730Sstevel@tonic-gate 	parent->p_child_ns = child;
12740Sstevel@tonic-gate }
1275