xref: /titanic_51/usr/src/uts/common/os/exit.c (revision 3d729aecc03ea6ebb9bd5d56b8dccd24f57daa41)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53f2f09c1Sdp  * Common Development and Distribution License (the "License").
63f2f09c1Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
23ff19e029SMenno Lageman  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24bafd1f14SJerry Jelinek  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/user.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
387c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/acct.h>
417c478bd9Sstevel@tonic-gate #include <sys/var.h>
427c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
457c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
467c478bd9Sstevel@tonic-gate #include <sys/procset.h>
477c478bd9Sstevel@tonic-gate #include <sys/class.h>
487c478bd9Sstevel@tonic-gate #include <sys/file.h>
497c478bd9Sstevel@tonic-gate #include <sys/session.h>
507c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
517c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
527c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
537c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
547c478bd9Sstevel@tonic-gate #include <sys/sem_impl.h>
557c478bd9Sstevel@tonic-gate #include <c2/audit.h>
567c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
577c478bd9Sstevel@tonic-gate #include <vm/as.h>
587c478bd9Sstevel@tonic-gate #include <sys/poll.h>
597c478bd9Sstevel@tonic-gate #include <sys/door.h>
607c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
617c478bd9Sstevel@tonic-gate #include <sys/utrap.h>
627c478bd9Sstevel@tonic-gate #include <sys/task.h>
637c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
647c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
657c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
667c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
677c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
687c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
697c478bd9Sstevel@tonic-gate #include <sys/list.h>
707c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
717c478bd9Sstevel@tonic-gate #include <sys/pool.h>
727c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
737c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
749acbbeafSnn35248 #include <sys/brand.h>
758ceba33eSraf #include <sys/libc_kernel.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * convert code/data pair into old style wait status
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate int
817c478bd9Sstevel@tonic-gate wstat(int code, int data)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	int stat = (data & 0377);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	switch (code) {
867c478bd9Sstevel@tonic-gate 	case CLD_EXITED:
877c478bd9Sstevel@tonic-gate 		stat <<= 8;
887c478bd9Sstevel@tonic-gate 		break;
897c478bd9Sstevel@tonic-gate 	case CLD_DUMPED:
907c478bd9Sstevel@tonic-gate 		stat |= WCOREFLG;
917c478bd9Sstevel@tonic-gate 		break;
927c478bd9Sstevel@tonic-gate 	case CLD_KILLED:
937c478bd9Sstevel@tonic-gate 		break;
947c478bd9Sstevel@tonic-gate 	case CLD_TRAPPED:
957c478bd9Sstevel@tonic-gate 	case CLD_STOPPED:
967c478bd9Sstevel@tonic-gate 		stat <<= 8;
977c478bd9Sstevel@tonic-gate 		stat |= WSTOPFLG;
987c478bd9Sstevel@tonic-gate 		break;
997c478bd9Sstevel@tonic-gate 	case CLD_CONTINUED:
1007c478bd9Sstevel@tonic-gate 		stat = WCONTFLG;
1017c478bd9Sstevel@tonic-gate 		break;
1027c478bd9Sstevel@tonic-gate 	default:
1037c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "wstat: bad code");
1047c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	return (stat);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static char *
1107c478bd9Sstevel@tonic-gate exit_reason(char *buf, size_t bufsz, int what, int why)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	switch (why) {
1137c478bd9Sstevel@tonic-gate 	case CLD_EXITED:
1147c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited with status %d", what);
1157c478bd9Sstevel@tonic-gate 		break;
1167c478bd9Sstevel@tonic-gate 	case CLD_KILLED:
1177c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited on fatal signal %d", what);
1187c478bd9Sstevel@tonic-gate 		break;
1197c478bd9Sstevel@tonic-gate 	case CLD_DUMPED:
1207c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "core dumped on signal %d", what);
1217c478bd9Sstevel@tonic-gate 		break;
1227c478bd9Sstevel@tonic-gate 	default:
1237c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "encountered unknown error "
1247c478bd9Sstevel@tonic-gate 		    "(%d, %d)", why, what);
1257c478bd9Sstevel@tonic-gate 		break;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	return (buf);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * exit system call: pass back caller's arg.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate void
1357c478bd9Sstevel@tonic-gate rexit(int rval)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, rval);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Called by proc_exit() when a zone's init exits, presumably because
1427c478bd9Sstevel@tonic-gate  * it failed.  As long as the given zone is still in the "running"
1437c478bd9Sstevel@tonic-gate  * state, we will re-exec() init, but first we need to reset things
1447c478bd9Sstevel@tonic-gate  * which are usually inherited across exec() but will break init's
1457c478bd9Sstevel@tonic-gate  * assumption that it is being exec()'d from a virgin process.  Most
1467c478bd9Sstevel@tonic-gate  * importantly this includes closing all file descriptors (exec only
1477c478bd9Sstevel@tonic-gate  * closes those marked close-on-exec) and resetting signals (exec only
1487c478bd9Sstevel@tonic-gate  * resets handled signals, and we need to clear any signals which
1497c478bd9Sstevel@tonic-gate  * killed init).  Anything else that exec(2) says would be inherited,
1507c478bd9Sstevel@tonic-gate  * but would affect the execution of init, needs to be reset.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate static int
1537c478bd9Sstevel@tonic-gate restart_init(int what, int why)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1567c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1577c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1587c478bd9Sstevel@tonic-gate 	user_t *up = PTOU(p);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	vnode_t *oldcd, *oldrd;
1617c478bd9Sstevel@tonic-gate 	int i, err;
1627c478bd9Sstevel@tonic-gate 	char reason_buf[64];
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Let zone admin (and global zone admin if this is for a non-global
1667c478bd9Sstevel@tonic-gate 	 * zone) know that init has failed and will be restarted.
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
1697c478bd9Sstevel@tonic-gate 	    "init(1M) %s: restarting automatically",
1707c478bd9Sstevel@tonic-gate 	    exit_reason(reason_buf, sizeof (reason_buf), what, why));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
1737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "init(1M) for zone %s (pid %d) %s: "
1747c478bd9Sstevel@tonic-gate 		    "restarting automatically",
1757c478bd9Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, reason_buf);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
1807c478bd9Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
1817c478bd9Sstevel@tonic-gate 	 * Then close all open file descriptors in the process.
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	pollcleanup();
1847c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(p));
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * Grab p_lock and begin clearing miscellaneous global process
1887c478bd9Sstevel@tonic-gate 	 * state that needs to be reset before we exec the new init(1M).
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19297eda132Sraf 	prbarrier(p);
1937c478bd9Sstevel@tonic-gate 
19497eda132Sraf 	p->p_flag &= ~(SKILLED | SEXTKILLED | SEXITING | SDOCORE);
1957c478bd9Sstevel@tonic-gate 	up->u_cmask = CMASK;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_hold);
1987c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
1997c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
2027c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	sigdelq(p, t, 0);
2057c478bd9Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (p->p_killsqp) {
2087c478bd9Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
2097c478bd9Sstevel@tonic-gate 		p->p_killsqp = NULL;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Reset any signals that are ignored back to the default disposition.
2147c478bd9Sstevel@tonic-gate 	 * Other u_signal members will be cleared when exec calls sigdefault().
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++) {
2177c478bd9Sstevel@tonic-gate 		if (up->u_signal[i - 1] == SIG_IGN) {
2187c478bd9Sstevel@tonic-gate 			up->u_signal[i - 1] = SIG_DFL;
2197c478bd9Sstevel@tonic-gate 			sigemptyset(&up->u_sigmask[i - 1]);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Clear the current signal, any signal info associated with it, and
2257c478bd9Sstevel@tonic-gate 	 * any signal information from contracts and/or contract templates.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
2287c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
2297c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo != NULL) {
2307c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
2317c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Reset both the process root directory and the current working
2377c478bd9Sstevel@tonic-gate 	 * directory to the root of the zone just as we do during boot.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
2407c478bd9Sstevel@tonic-gate 	oldrd = up->u_rdir;
2417c478bd9Sstevel@tonic-gate 	up->u_rdir = p->p_zone->zone_rootvp;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
2447c478bd9Sstevel@tonic-gate 	oldcd = up->u_cdir;
2457c478bd9Sstevel@tonic-gate 	up->u_cdir = p->p_zone->zone_rootvp;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (up->u_cwd != NULL) {
2487c478bd9Sstevel@tonic-gate 		refstr_rele(up->u_cwd);
2497c478bd9Sstevel@tonic-gate 		up->u_cwd = NULL;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (oldrd != NULL)
2557c478bd9Sstevel@tonic-gate 		VN_RELE(oldrd);
2567c478bd9Sstevel@tonic-gate 	if (oldcd != NULL)
2577c478bd9Sstevel@tonic-gate 		VN_RELE(oldcd);
2587c478bd9Sstevel@tonic-gate 
2599acbbeafSnn35248 	/* Free the controlling tty.  (freectty() always assumes curproc.) */
2609acbbeafSnn35248 	ASSERT(p == curproc);
2619acbbeafSnn35248 	(void) freectty(B_TRUE);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Now exec() the new init(1M) on top of the current process.  If we
2657c478bd9Sstevel@tonic-gate 	 * succeed, the caller will treat this like a successful system call.
2667c478bd9Sstevel@tonic-gate 	 * If we fail, we issue messages and the caller will proceed with exit.
2677c478bd9Sstevel@tonic-gate 	 */
2683f2f09c1Sdp 	err = exec_init(p->p_zone->zone_initname, NULL);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (err == 0)
2717c478bd9Sstevel@tonic-gate 		return (0);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
2747c478bd9Sstevel@tonic-gate 	    "failed to restart init(1M) (err=%d): system reboot required", err);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
2777c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to restart init(1M) for zone %s "
2787c478bd9Sstevel@tonic-gate 		    "(pid %d, err=%d): zoneadm(1M) boot required",
2797c478bd9Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, err);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	return (-1);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * Release resources.
2877c478bd9Sstevel@tonic-gate  * Enter zombie state.
2887c478bd9Sstevel@tonic-gate  * Wake up parent and init processes,
2897c478bd9Sstevel@tonic-gate  * and dispose of children.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate void
2927c478bd9Sstevel@tonic-gate exit(int why, int what)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * If proc_exit() fails, then some other lwp in the process
2967c478bd9Sstevel@tonic-gate 	 * got there first.  We just have to call lwp_exit() to allow
2977c478bd9Sstevel@tonic-gate 	 * the other lwp to finish exiting the process.  Otherwise we're
2987c478bd9Sstevel@tonic-gate 	 * restarting init, and should return.
2997c478bd9Sstevel@tonic-gate 	 */
3007c478bd9Sstevel@tonic-gate 	if (proc_exit(why, what) != 0) {
3017c478bd9Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
3027c478bd9Sstevel@tonic-gate 		ASSERT(curproc->p_flag & SEXITLWPS);
3037c478bd9Sstevel@tonic-gate 		lwp_exit();
3047c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
30997eda132Sraf  * Set the SEXITING flag on the process, after making sure /proc does
31097eda132Sraf  * not have it locked.  This is done in more places than proc_exit(),
31197eda132Sraf  * so it is a separate function.
31297eda132Sraf  */
31397eda132Sraf void
31497eda132Sraf proc_is_exiting(proc_t *p)
31597eda132Sraf {
31697eda132Sraf 	mutex_enter(&p->p_lock);
31797eda132Sraf 	prbarrier(p);
31897eda132Sraf 	p->p_flag |= SEXITING;
31997eda132Sraf 	mutex_exit(&p->p_lock);
32097eda132Sraf }
32197eda132Sraf 
32297eda132Sraf /*
3237c478bd9Sstevel@tonic-gate  * Return value:
3247c478bd9Sstevel@tonic-gate  *   1 - exitlwps() failed, call (or continue) lwp_exit()
3257c478bd9Sstevel@tonic-gate  *   0 - restarting init.  Return through system call path
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate int
3287c478bd9Sstevel@tonic-gate proc_exit(int why, int what)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3317c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3327c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3337c478bd9Sstevel@tonic-gate 	zone_t *z = p->p_zone;
3347c478bd9Sstevel@tonic-gate 	timeout_id_t tmp_id;
3357c478bd9Sstevel@tonic-gate 	int rv;
3367c478bd9Sstevel@tonic-gate 	proc_t *q;
3377c478bd9Sstevel@tonic-gate 	task_t *tk;
3387c478bd9Sstevel@tonic-gate 	vnode_t *exec_vp, *execdir_vp, *cdir, *rdir;
3397c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp;
3407c478bd9Sstevel@tonic-gate 	lwpdir_t *lwpdir;
3417c478bd9Sstevel@tonic-gate 	uint_t lwpdir_sz;
3426eb30ec3SRoger A. Faulkner 	tidhash_t *tidhash;
3437c478bd9Sstevel@tonic-gate 	uint_t tidhash_sz;
3446eb30ec3SRoger A. Faulkner 	ret_tidhash_t *ret_tidhash;
3457c478bd9Sstevel@tonic-gate 	refstr_t *cwd;
3467c478bd9Sstevel@tonic-gate 	hrtime_t hrutime, hrstime;
3478ceba33eSraf 	int evaporate;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Stop and discard the process's lwps except for the current one,
3517c478bd9Sstevel@tonic-gate 	 * unless some other lwp beat us to it.  If exitlwps() fails then
3527c478bd9Sstevel@tonic-gate 	 * return and the calling lwp will call (or continue in) lwp_exit().
3537c478bd9Sstevel@tonic-gate 	 */
35497eda132Sraf 	proc_is_exiting(p);
3557c478bd9Sstevel@tonic-gate 	if (exitlwps(0) != 0)
3567c478bd9Sstevel@tonic-gate 		return (1);
3577c478bd9Sstevel@tonic-gate 
3582850d85bSmv143129 	mutex_enter(&p->p_lock);
3592850d85bSmv143129 	if (p->p_ttime > 0) {
3602850d85bSmv143129 		/*
3612850d85bSmv143129 		 * Account any remaining ticks charged to this process
3622850d85bSmv143129 		 * on its way out.
3632850d85bSmv143129 		 */
3642850d85bSmv143129 		(void) task_cpu_time_incr(p->p_task, p->p_ttime);
3652850d85bSmv143129 		p->p_ttime = 0;
3662850d85bSmv143129 	}
3672850d85bSmv143129 	mutex_exit(&p->p_lock);
3682850d85bSmv143129 
3697c478bd9Sstevel@tonic-gate 	DTRACE_PROC(lwp__exit);
3707c478bd9Sstevel@tonic-gate 	DTRACE_PROC1(exit, int, why);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3739acbbeafSnn35248 	 * Will perform any brand specific proc exit processing, since this
3749acbbeafSnn35248 	 * is always the last lwp, will also perform lwp_exit and free brand
3759acbbeafSnn35248 	 * data
3769acbbeafSnn35248 	 */
377fd9e7635Sedp 	if (PROC_IS_BRANDED(p)) {
378fd9e7635Sedp 		lwp_detach_brand_hdlrs(lwp);
379e9f7cbf0SVamsi Nagineni 		brand_clearbrand(p, B_FALSE);
380fd9e7635Sedp 	}
3819acbbeafSnn35248 
3829acbbeafSnn35248 	/*
3833f2f09c1Sdp 	 * Don't let init exit unless zone_start_init() failed its exec, or
3847c478bd9Sstevel@tonic-gate 	 * we are shutting down the zone or the machine.
3857c478bd9Sstevel@tonic-gate 	 *
3867c478bd9Sstevel@tonic-gate 	 * Since we are single threaded, we don't need to lock the
3877c478bd9Sstevel@tonic-gate 	 * following accesses to zone_proc_initpid.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	if (p->p_pid == z->zone_proc_initpid) {
3907c478bd9Sstevel@tonic-gate 		if (z->zone_boot_err == 0 &&
3917c478bd9Sstevel@tonic-gate 		    zone_status_get(z) < ZONE_IS_SHUTTING_DOWN &&
392bafd1f14SJerry Jelinek 		    zone_status_get(global_zone) < ZONE_IS_SHUTTING_DOWN) {
393bafd1f14SJerry Jelinek 			if (z->zone_restart_init == B_TRUE) {
394bafd1f14SJerry Jelinek 				if (restart_init(what, why) == 0)
3957c478bd9Sstevel@tonic-gate 					return (0);
396bafd1f14SJerry Jelinek 			} else {
397bafd1f14SJerry Jelinek 				(void) zone_kadmin(A_SHUTDOWN, AD_HALT, NULL,
398bafd1f14SJerry Jelinek 				    CRED());
399bafd1f14SJerry Jelinek 			}
400bafd1f14SJerry Jelinek 		}
401bafd1f14SJerry Jelinek 
4027c478bd9Sstevel@tonic-gate 		/*
4037c478bd9Sstevel@tonic-gate 		 * Since we didn't or couldn't restart init, we clear
4047c478bd9Sstevel@tonic-gate 		 * the zone's init state and proceed with exit
4057c478bd9Sstevel@tonic-gate 		 * processing.
4067c478bd9Sstevel@tonic-gate 		 */
4077c478bd9Sstevel@tonic-gate 		z->zone_proc_initpid = -1;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
410061d7437SJakub Jermar 	lwp_pcb_exit();
411061d7437SJakub Jermar 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * Allocate a sigqueue now, before we grab locks.
4147c478bd9Sstevel@tonic-gate 	 * It will be given to sigcld(), below.
4158ceba33eSraf 	 * Special case:  If we will be making the process disappear
41635a5a358SJonathan Adams 	 * without a trace because it is either:
41735a5a358SJonathan Adams 	 *	* an exiting SSYS process, or
41835a5a358SJonathan Adams 	 *	* a posix_spawn() vfork child who requests it,
41935a5a358SJonathan Adams 	 * we don't bother to allocate a useless sigqueue.
4207c478bd9Sstevel@tonic-gate 	 */
42135a5a358SJonathan Adams 	evaporate = (p->p_flag & SSYS) || ((p->p_flag & SVFORK) &&
4228ceba33eSraf 	    why == CLD_EXITED && what == _EVAPORATE);
4238ceba33eSraf 	if (!evaporate)
4247c478bd9Sstevel@tonic-gate 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * revoke any doors created by the process.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	if (p->p_door_list)
4307c478bd9Sstevel@tonic-gate 		door_exit();
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * Release schedctl data structures.
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	if (p->p_pagep)
4367c478bd9Sstevel@tonic-gate 		schedctl_proc_cleanup();
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * make sure all pending kaio has completed.
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	if (p->p_aio)
4427c478bd9Sstevel@tonic-gate 		aio_cleanup_exit();
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/*
4457c478bd9Sstevel@tonic-gate 	 * discard the lwpchan cache.
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	if (p->p_lcp != NULL)
4487c478bd9Sstevel@tonic-gate 		lwpchan_destroy_cache(0);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * Clean up any DTrace helper actions or probes for the process.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
4547c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
4557c478bd9Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
458*3d729aecSJerry Jelinek 	/*
459*3d729aecSJerry Jelinek 	 * Clean up any signalfd state for the process.
460*3d729aecSJerry Jelinek 	 */
461*3d729aecSJerry Jelinek 	if (p->p_sigfd != NULL) {
462*3d729aecSJerry Jelinek 		VERIFY(sigfd_exit_helper != NULL);
463*3d729aecSJerry Jelinek 		(*sigfd_exit_helper)();
464*3d729aecSJerry Jelinek 	}
465*3d729aecSJerry Jelinek 
4667c478bd9Sstevel@tonic-gate 	/* untimeout the realtime timers */
4677c478bd9Sstevel@tonic-gate 	if (p->p_itimer != NULL)
4687c478bd9Sstevel@tonic-gate 		timer_exit();
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if ((tmp_id = p->p_alarmid) != 0) {
4717c478bd9Sstevel@tonic-gate 		p->p_alarmid = 0;
4727c478bd9Sstevel@tonic-gate 		(void) untimeout(tmp_id);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
4777c478bd9Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	pollcleanup();
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (p->p_rprof_cyclic != CYCLIC_NONE) {
4827c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4837c478bd9Sstevel@tonic-gate 		cyclic_remove(p->p_rprof_cyclic);
4847c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Clean up any DTrace probes associated with this process.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
4937c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exit_ptr != NULL);
4947c478bd9Sstevel@tonic-gate 		dtrace_fasttrap_exit_ptr(p);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	while ((tmp_id = p->p_itimerid) != 0) {
4987c478bd9Sstevel@tonic-gate 		p->p_itimerid = 0;
4997c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5007c478bd9Sstevel@tonic-gate 		(void) untimeout(tmp_id);
5017c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	lwp_cleanup();
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * We are about to exit; prevent our resource associations from
5087c478bd9Sstevel@tonic-gate 	 * being changed.
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	pool_barrier_enter();
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Block the process against /proc now that we have really
5147c478bd9Sstevel@tonic-gate 	 * acquired p->p_lock (to manipulate p_tlist at least).
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	prbarrier(p);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	sigfillset(&p->p_ignore);
5197c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_siginfo);
5207c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
5217c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
5227c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
5237c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
5247c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_sigmask);
5257c478bd9Sstevel@tonic-gate 	sigdelq(p, t, 0);
5267c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
5277c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
5287c478bd9Sstevel@tonic-gate 	p->p_flag &= ~(SKILLED | SEXTKILLED);
5297c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
5307c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
5317c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_LWPEXIT;
5357c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
5367c478bd9Sstevel@tonic-gate 	prlwpexit(t);		/* notify /proc */
5377c478bd9Sstevel@tonic-gate 	lwp_hash_out(p, t->t_tid);
5387c478bd9Sstevel@tonic-gate 	prexit(p);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	p->p_lwpcnt = 0;
5417c478bd9Sstevel@tonic-gate 	p->p_tlist = NULL;
5427c478bd9Sstevel@tonic-gate 	sigqfree(p);
5437c478bd9Sstevel@tonic-gate 	term_mstate(t);
5447c478bd9Sstevel@tonic-gate 	p->p_mterm = gethrtime();
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	exec_vp = p->p_exec;
5477c478bd9Sstevel@tonic-gate 	execdir_vp = p->p_execdir;
5487c478bd9Sstevel@tonic-gate 	p->p_exec = NULLVP;
5497c478bd9Sstevel@tonic-gate 	p->p_execdir = NULLVP;
5507c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	pr_free_watched_pages(p);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(p));
5557c478bd9Sstevel@tonic-gate 
5569acbbeafSnn35248 	/* Free the controlling tty.  (freectty() always assumes curproc.) */
5579acbbeafSnn35248 	ASSERT(p == curproc);
5589acbbeafSnn35248 	(void) freectty(B_TRUE);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate #if defined(__sparc)
5617c478bd9Sstevel@tonic-gate 	if (p->p_utraps != NULL)
5627c478bd9Sstevel@tonic-gate 		utrap_free(p);
5637c478bd9Sstevel@tonic-gate #endif
5647c478bd9Sstevel@tonic-gate 	if (p->p_semacct)			/* IPC semaphore exit */
5657c478bd9Sstevel@tonic-gate 		semexit(p);
5667c478bd9Sstevel@tonic-gate 	rv = wstat(why, what);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	acct(rv & 0xff);
5697c478bd9Sstevel@tonic-gate 	exacct_commit_proc(p, rv);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * Release any resources associated with C2 auditing
5737c478bd9Sstevel@tonic-gate 	 */
574005d3febSMarek Pospisil 	if (AU_AUDITING()) {
5757c478bd9Sstevel@tonic-gate 		/*
5767c478bd9Sstevel@tonic-gate 		 * audit exit system call
5777c478bd9Sstevel@tonic-gate 		 */
5787c478bd9Sstevel@tonic-gate 		audit_exit(why, what);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * Free address space.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	relvm();
5857c478bd9Sstevel@tonic-gate 
586a4aeef46SDonghai Qiao 	if (exec_vp) {
587a4aeef46SDonghai Qiao 		/*
588a4aeef46SDonghai Qiao 		 * Close this executable which has been opened when the process
589a4aeef46SDonghai Qiao 		 * was created by getproc().
590a4aeef46SDonghai Qiao 		 */
591a4aeef46SDonghai Qiao 		(void) VOP_CLOSE(exec_vp, FREAD, 1, (offset_t)0, CRED(), NULL);
592a4aeef46SDonghai Qiao 		VN_RELE(exec_vp);
593a4aeef46SDonghai Qiao 	}
594a4aeef46SDonghai Qiao 	if (execdir_vp)
595a4aeef46SDonghai Qiao 		VN_RELE(execdir_vp);
596a4aeef46SDonghai Qiao 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * Release held contracts.
5997c478bd9Sstevel@tonic-gate 	 */
6007c478bd9Sstevel@tonic-gate 	contract_exit(p);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Depart our encapsulating process contract.
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 	if ((p->p_flag & SSYS) == 0) {
6067c478bd9Sstevel@tonic-gate 		ASSERT(p->p_ct_process);
6077c478bd9Sstevel@tonic-gate 		contract_process_exit(p->p_ct_process, p, rv);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * Remove pool association, and block if requested by pool_do_bind.
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6147c478bd9Sstevel@tonic-gate 	ASSERT(p->p_pool->pool_ref > 0);
6151a5e258fSJosef 'Jeff' Sipek 	atomic_dec_32(&p->p_pool->pool_ref);
6167c478bd9Sstevel@tonic-gate 	p->p_pool = pool_default;
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * Now that our address space has been freed and all other threads
6197c478bd9Sstevel@tonic-gate 	 * in this process have exited, set the PEXITED pool flag.  This
6207c478bd9Sstevel@tonic-gate 	 * tells the pools subsystems to ignore this process if it was
6217c478bd9Sstevel@tonic-gate 	 * requested to rebind this process to a new pool.
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	p->p_poolflag |= PEXITED;
6247c478bd9Sstevel@tonic-gate 	pool_barrier_exit();
6257c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Delete this process from the newstate list of its parent. We
6317c478bd9Sstevel@tonic-gate 	 * will put it in the right place in the sigcld in the end.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	delete_ns(p->p_parent, p);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Reassign the orphans to the next of kin.
6377c478bd9Sstevel@tonic-gate 	 * Don't rearrange init's orphanage.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	if ((q = p->p_orphan) != NULL && p != proc_init) {
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		proc_t *nokp = p->p_nextofkin;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		for (;;) {
6447c478bd9Sstevel@tonic-gate 			q->p_nextofkin = nokp;
6457c478bd9Sstevel@tonic-gate 			if (q->p_nextorph == NULL)
6467c478bd9Sstevel@tonic-gate 				break;
6477c478bd9Sstevel@tonic-gate 			q = q->p_nextorph;
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 		q->p_nextorph = nokp->p_orphan;
6507c478bd9Sstevel@tonic-gate 		nokp->p_orphan = p->p_orphan;
6517c478bd9Sstevel@tonic-gate 		p->p_orphan = NULL;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * Reassign the children to init.
6567c478bd9Sstevel@tonic-gate 	 * Don't try to assign init's children to init.
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	if ((q = p->p_child) != NULL && p != proc_init) {
6597c478bd9Sstevel@tonic-gate 		struct proc	*np;
6607c478bd9Sstevel@tonic-gate 		struct proc	*initp = proc_init;
6617c478bd9Sstevel@tonic-gate 		boolean_t	setzonetop = B_FALSE;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		if (!INGLOBALZONE(curproc))
6647c478bd9Sstevel@tonic-gate 			setzonetop = B_TRUE;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		pgdetach(p);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		do {
6697c478bd9Sstevel@tonic-gate 			np = q->p_sibling;
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			 * Delete it from its current parent new state
6727c478bd9Sstevel@tonic-gate 			 * list and add it to init new state list
6737c478bd9Sstevel@tonic-gate 			 */
6747c478bd9Sstevel@tonic-gate 			delete_ns(q->p_parent, q);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			q->p_ppid = 1;
677657b1f3dSraf 			q->p_pidflag &= ~(CLDNOSIGCHLD | CLDWAITPID);
6787c478bd9Sstevel@tonic-gate 			if (setzonetop) {
6797c478bd9Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
6807c478bd9Sstevel@tonic-gate 				q->p_flag |= SZONETOP;
6817c478bd9Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
6827c478bd9Sstevel@tonic-gate 			}
6837c478bd9Sstevel@tonic-gate 			q->p_parent = initp;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * Since q will be the first child,
6877c478bd9Sstevel@tonic-gate 			 * it will not have a previous sibling.
6887c478bd9Sstevel@tonic-gate 			 */
6897c478bd9Sstevel@tonic-gate 			q->p_psibling = NULL;
6907c478bd9Sstevel@tonic-gate 			if (initp->p_child) {
6917c478bd9Sstevel@tonic-gate 				initp->p_child->p_psibling = q;
6927c478bd9Sstevel@tonic-gate 			}
6937c478bd9Sstevel@tonic-gate 			q->p_sibling = initp->p_child;
6947c478bd9Sstevel@tonic-gate 			initp->p_child = q;
6957c478bd9Sstevel@tonic-gate 			if (q->p_proc_flag & P_PR_PTRACE) {
6967c478bd9Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
6977c478bd9Sstevel@tonic-gate 				sigtoproc(q, NULL, SIGKILL);
6987c478bd9Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 			/*
7017c478bd9Sstevel@tonic-gate 			 * sigcld() will add the child to parents
7027c478bd9Sstevel@tonic-gate 			 * newstate list.
7037c478bd9Sstevel@tonic-gate 			 */
7047c478bd9Sstevel@tonic-gate 			if (q->p_stat == SZOMB)
7057c478bd9Sstevel@tonic-gate 				sigcld(q, NULL);
7067c478bd9Sstevel@tonic-gate 		} while ((q = np) != NULL);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		p->p_child = NULL;
7097c478bd9Sstevel@tonic-gate 		ASSERT(p->p_child_ns == NULL);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_PROC, TR_PROC_EXIT, "proc_exit: %p", p);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
7157c478bd9Sstevel@tonic-gate 	CL_EXIT(curthread); /* tell the scheduler that curthread is exiting */
7167c478bd9Sstevel@tonic-gate 
7177eceb558Srh87107 	/*
7187eceb558Srh87107 	 * Have our task accummulate our resource usage data before they
7197eceb558Srh87107 	 * become contaminated by p_cacct etc., and before we renounce
7207eceb558Srh87107 	 * membership of the task.
7217eceb558Srh87107 	 *
7227eceb558Srh87107 	 * We do this regardless of whether or not task accounting is active.
7237eceb558Srh87107 	 * This is to avoid having nonsense data reported for this task if
7247eceb558Srh87107 	 * task accounting is subsequently enabled. The overhead is minimal;
7257eceb558Srh87107 	 * by this point, this process has accounted for the usage of all its
7267eceb558Srh87107 	 * LWPs. We nonetheless do the work here, and under the protection of
7277eceb558Srh87107 	 * pidlock, so that the movement of the process's usage to the task
7287eceb558Srh87107 	 * happens at the same time as the removal of the process from the
7297eceb558Srh87107 	 * task, from the point of view of exacct_snapshot_task_usage().
7307eceb558Srh87107 	 */
7317eceb558Srh87107 	exacct_update_task_mstate(p);
7327eceb558Srh87107 
7337c478bd9Sstevel@tonic-gate 	hrutime = mstate_aggr_state(p, LMS_USER);
7347c478bd9Sstevel@tonic-gate 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
7357c478bd9Sstevel@tonic-gate 	p->p_utime = (clock_t)NSEC_TO_TICK(hrutime) + p->p_cutime;
7367c478bd9Sstevel@tonic-gate 	p->p_stime = (clock_t)NSEC_TO_TICK(hrstime) + p->p_cstime;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_USER]	+= p->p_cacct[LMS_USER];
7397c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_SYSTEM]	+= p->p_cacct[LMS_SYSTEM];
7407c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_TRAP]	+= p->p_cacct[LMS_TRAP];
7417c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_TFAULT]	+= p->p_cacct[LMS_TFAULT];
7427c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_DFAULT]	+= p->p_cacct[LMS_DFAULT];
7437c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_KFAULT]	+= p->p_cacct[LMS_KFAULT];
7447c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_USER_LOCK] += p->p_cacct[LMS_USER_LOCK];
7457c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_SLEEP]	+= p->p_cacct[LMS_SLEEP];
7467c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_WAIT_CPU]	+= p->p_cacct[LMS_WAIT_CPU];
7477c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_STOPPED]	+= p->p_cacct[LMS_STOPPED];
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	p->p_ru.minflt	+= p->p_cru.minflt;
7507c478bd9Sstevel@tonic-gate 	p->p_ru.majflt	+= p->p_cru.majflt;
7517c478bd9Sstevel@tonic-gate 	p->p_ru.nswap	+= p->p_cru.nswap;
7527c478bd9Sstevel@tonic-gate 	p->p_ru.inblock	+= p->p_cru.inblock;
7537c478bd9Sstevel@tonic-gate 	p->p_ru.oublock	+= p->p_cru.oublock;
7547c478bd9Sstevel@tonic-gate 	p->p_ru.msgsnd	+= p->p_cru.msgsnd;
7557c478bd9Sstevel@tonic-gate 	p->p_ru.msgrcv	+= p->p_cru.msgrcv;
7567c478bd9Sstevel@tonic-gate 	p->p_ru.nsignals += p->p_cru.nsignals;
7577c478bd9Sstevel@tonic-gate 	p->p_ru.nvcsw	+= p->p_cru.nvcsw;
7587c478bd9Sstevel@tonic-gate 	p->p_ru.nivcsw	+= p->p_cru.nivcsw;
7597c478bd9Sstevel@tonic-gate 	p->p_ru.sysc	+= p->p_cru.sysc;
7607c478bd9Sstevel@tonic-gate 	p->p_ru.ioch	+= p->p_cru.ioch;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	p->p_stat = SZOMB;
7637c478bd9Sstevel@tonic-gate 	p->p_proc_flag &= ~P_PR_PTRACE;
7647c478bd9Sstevel@tonic-gate 	p->p_wdata = what;
7657c478bd9Sstevel@tonic-gate 	p->p_wcode = (char)why;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	cdir = PTOU(p)->u_cdir;
7687c478bd9Sstevel@tonic-gate 	rdir = PTOU(p)->u_rdir;
7697c478bd9Sstevel@tonic-gate 	cwd = PTOU(p)->u_cwd;
7707c478bd9Sstevel@tonic-gate 
77135a5a358SJonathan Adams 	ASSERT(cdir != NULL || p->p_parent == &p0);
77235a5a358SJonathan Adams 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * Release resource controls, as they are no longer enforceable.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	rctl_set_free(p->p_rctls);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/*
779ff19e029SMenno Lageman 	 * Decrement tk_nlwps counter for our task.max-lwps resource control.
780ff19e029SMenno Lageman 	 * An extended accounting record, if that facility is active, is
781ff19e029SMenno Lageman 	 * scheduled to be written.  We cannot give up task and project
782ff19e029SMenno Lageman 	 * membership at this point because that would allow zombies to escape
783ff19e029SMenno Lageman 	 * from the max-processes resource controls.  Zombies stay in their
784ff19e029SMenno Lageman 	 * current task and project until the process table slot is released
785ff19e029SMenno Lageman 	 * in freeproc().
7867c478bd9Sstevel@tonic-gate 	 */
7877c478bd9Sstevel@tonic-gate 	tk = p->p_task;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
7907c478bd9Sstevel@tonic-gate 	tk->tk_nlwps--;
7917c478bd9Sstevel@tonic-gate 	tk->tk_proj->kpj_nlwps--;
7927c478bd9Sstevel@tonic-gate 	p->p_zone->zone_nlwps--;
7937c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/*
7967c478bd9Sstevel@tonic-gate 	 * Clear the lwp directory and the lwpid hash table
7977c478bd9Sstevel@tonic-gate 	 * now that /proc can't bother us any more.
7987c478bd9Sstevel@tonic-gate 	 * We free the memory below, after dropping p->p_lock.
7997c478bd9Sstevel@tonic-gate 	 */
8007c478bd9Sstevel@tonic-gate 	lwpdir = p->p_lwpdir;
8017c478bd9Sstevel@tonic-gate 	lwpdir_sz = p->p_lwpdir_sz;
8027c478bd9Sstevel@tonic-gate 	tidhash = p->p_tidhash;
8037c478bd9Sstevel@tonic-gate 	tidhash_sz = p->p_tidhash_sz;
8046eb30ec3SRoger A. Faulkner 	ret_tidhash = p->p_ret_tidhash;
8057c478bd9Sstevel@tonic-gate 	p->p_lwpdir = NULL;
8067c478bd9Sstevel@tonic-gate 	p->p_lwpfree = NULL;
8077c478bd9Sstevel@tonic-gate 	p->p_lwpdir_sz = 0;
8087c478bd9Sstevel@tonic-gate 	p->p_tidhash = NULL;
8097c478bd9Sstevel@tonic-gate 	p->p_tidhash_sz = 0;
8106eb30ec3SRoger A. Faulkner 	p->p_ret_tidhash = NULL;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	/*
8130baeff3dSrab 	 * If the process has context ops installed, call the exit routine
8140baeff3dSrab 	 * on behalf of this last remaining thread. Normally exitpctx() is
8150baeff3dSrab 	 * called during thread_exit() or lwp_exit(), but because this is the
8160baeff3dSrab 	 * last thread in the process, we must call it here. By the time
8170baeff3dSrab 	 * thread_exit() is called (below), the association with the relevant
8180baeff3dSrab 	 * process has been lost.
8190baeff3dSrab 	 *
8200baeff3dSrab 	 * We also free the context here.
8210baeff3dSrab 	 */
8220baeff3dSrab 	if (p->p_pctx) {
8230baeff3dSrab 		kpreempt_disable();
8240baeff3dSrab 		exitpctx(p);
8250baeff3dSrab 		kpreempt_enable();
8260baeff3dSrab 
8270baeff3dSrab 		freepctx(p, 0);
8280baeff3dSrab 	}
8290baeff3dSrab 
8300baeff3dSrab 	/*
83127e6fb21Sdp 	 * curthread's proc pointer is changed to point to the 'sched'
83227e6fb21Sdp 	 * process for the corresponding zone, except in the case when
83327e6fb21Sdp 	 * the exiting process is in fact a zsched instance, in which
83427e6fb21Sdp 	 * case the proc pointer is set to p0.  We do so, so that the
83527e6fb21Sdp 	 * process still points at the right zone when we call the VN_RELE()
83627e6fb21Sdp 	 * below.
83727e6fb21Sdp 	 *
83827e6fb21Sdp 	 * This is because curthread's original proc pointer can be freed as
83927e6fb21Sdp 	 * soon as the child sends a SIGCLD to its parent.  We use zsched so
84027e6fb21Sdp 	 * that for user processes, even in the final moments of death, the
84127e6fb21Sdp 	 * process is still associated with its zone.
8427c478bd9Sstevel@tonic-gate 	 */
84327e6fb21Sdp 	if (p != t->t_procp->p_zone->zone_zsched)
84427e6fb21Sdp 		t->t_procp = t->t_procp->p_zone->zone_zsched;
84527e6fb21Sdp 	else
8467c478bd9Sstevel@tonic-gate 		t->t_procp = &p0;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
849657b1f3dSraf 	if (!evaporate) {
850657b1f3dSraf 		p->p_pidflag &= ~CLDPEND;
8517c478bd9Sstevel@tonic-gate 		sigcld(p, sqp);
852657b1f3dSraf 	} else {
8538ceba33eSraf 		/*
8548ceba33eSraf 		 * Do what sigcld() would do if the disposition
8558ceba33eSraf 		 * of the SIGCHLD signal were set to be ignored.
8568ceba33eSraf 		 */
8578ceba33eSraf 		cv_broadcast(&p->p_srwchan_cv);
8588ceba33eSraf 		freeproc(p);
8598ceba33eSraf 	}
8607c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/*
8637c478bd9Sstevel@tonic-gate 	 * We don't release u_cdir and u_rdir until SZOMB is set.
8647c478bd9Sstevel@tonic-gate 	 * This protects us against dofusers().
8657c478bd9Sstevel@tonic-gate 	 */
86635a5a358SJonathan Adams 	if (cdir)
8677c478bd9Sstevel@tonic-gate 		VN_RELE(cdir);
8687c478bd9Sstevel@tonic-gate 	if (rdir)
8697c478bd9Sstevel@tonic-gate 		VN_RELE(rdir);
8707c478bd9Sstevel@tonic-gate 	if (cwd)
8717c478bd9Sstevel@tonic-gate 		refstr_rele(cwd);
8727c478bd9Sstevel@tonic-gate 
87327e6fb21Sdp 	/*
87427e6fb21Sdp 	 * task_rele() may ultimately cause the zone to go away (or
87527e6fb21Sdp 	 * may cause the last user process in a zone to go away, which
87627e6fb21Sdp 	 * signals zsched to go away).  So prior to this call, we must
87727e6fb21Sdp 	 * no longer point at zsched.
87827e6fb21Sdp 	 */
87927e6fb21Sdp 	t->t_procp = &p0;
88027e6fb21Sdp 
88127e6fb21Sdp 	kmem_free(lwpdir, lwpdir_sz * sizeof (lwpdir_t));
8826eb30ec3SRoger A. Faulkner 	kmem_free(tidhash, tidhash_sz * sizeof (tidhash_t));
8836eb30ec3SRoger A. Faulkner 	while (ret_tidhash != NULL) {
8846eb30ec3SRoger A. Faulkner 		ret_tidhash_t *next = ret_tidhash->rth_next;
8856eb30ec3SRoger A. Faulkner 		kmem_free(ret_tidhash->rth_tidhash,
8866eb30ec3SRoger A. Faulkner 		    ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
8876eb30ec3SRoger A. Faulkner 		kmem_free(ret_tidhash, sizeof (*ret_tidhash));
8886eb30ec3SRoger A. Faulkner 		ret_tidhash = next;
8896eb30ec3SRoger A. Faulkner 	}
89027e6fb21Sdp 
8917c478bd9Sstevel@tonic-gate 	thread_exit();
8927c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate  * Format siginfo structure for wait system calls.
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate void
8997c478bd9Sstevel@tonic-gate winfo(proc_t *pp, k_siginfo_t *ip, int waitflag)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	bzero(ip, sizeof (k_siginfo_t));
9047c478bd9Sstevel@tonic-gate 	ip->si_signo = SIGCLD;
9057c478bd9Sstevel@tonic-gate 	ip->si_code = pp->p_wcode;
9067c478bd9Sstevel@tonic-gate 	ip->si_pid = pp->p_pid;
9077c478bd9Sstevel@tonic-gate 	ip->si_ctid = PRCTID(pp);
9087c478bd9Sstevel@tonic-gate 	ip->si_zoneid = pp->p_zone->zone_id;
9097c478bd9Sstevel@tonic-gate 	ip->si_status = pp->p_wdata;
9107c478bd9Sstevel@tonic-gate 	ip->si_stime = pp->p_stime;
9117c478bd9Sstevel@tonic-gate 	ip->si_utime = pp->p_utime;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (waitflag) {
9147c478bd9Sstevel@tonic-gate 		pp->p_wcode = 0;
9157c478bd9Sstevel@tonic-gate 		pp->p_wdata = 0;
9167c478bd9Sstevel@tonic-gate 		pp->p_pidflag &= ~CLDPEND;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * Wait system call.
9227c478bd9Sstevel@tonic-gate  * Search for a terminated (zombie) child,
9237c478bd9Sstevel@tonic-gate  * finally lay it to rest, and collect its status.
9247c478bd9Sstevel@tonic-gate  * Look also for stopped children,
9257c478bd9Sstevel@tonic-gate  * and pass back status from them.
9267c478bd9Sstevel@tonic-gate  */
9277c478bd9Sstevel@tonic-gate int
9287c478bd9Sstevel@tonic-gate waitid(idtype_t idtype, id_t id, k_siginfo_t *ip, int options)
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate 	int found;
9317c478bd9Sstevel@tonic-gate 	proc_t *cp, *pp;
9327c478bd9Sstevel@tonic-gate 	int proc_gone;
9337c478bd9Sstevel@tonic-gate 	int waitflag = !(options & WNOWAIT);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * Obsolete flag, defined here only for binary compatibility
9377c478bd9Sstevel@tonic-gate 	 * with old statically linked executables.  Delete this when
9387c478bd9Sstevel@tonic-gate 	 * we no longer care about these old and broken applications.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate #define	_WNOCHLD	0400
9417c478bd9Sstevel@tonic-gate 	options &= ~_WNOCHLD;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (options == 0 || (options & ~WOPTMASK))
9447c478bd9Sstevel@tonic-gate 		return (EINVAL);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	switch (idtype) {
9477c478bd9Sstevel@tonic-gate 	case P_PID:
9487c478bd9Sstevel@tonic-gate 	case P_PGID:
9497c478bd9Sstevel@tonic-gate 		if (id < 0 || id >= maxpid)
9507c478bd9Sstevel@tonic-gate 			return (EINVAL);
9517c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9527c478bd9Sstevel@tonic-gate 	case P_ALL:
9537c478bd9Sstevel@tonic-gate 		break;
9547c478bd9Sstevel@tonic-gate 	default:
9557c478bd9Sstevel@tonic-gate 		return (EINVAL);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	pp = ttoproc(curthread);
959e44bd21cSsusans 
9607c478bd9Sstevel@tonic-gate 	/*
9617c478bd9Sstevel@tonic-gate 	 * lock parent mutex so that sibling chain can be searched.
9627c478bd9Sstevel@tonic-gate 	 */
9637c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
964e44bd21cSsusans 
965e44bd21cSsusans 	/*
966e44bd21cSsusans 	 * if we are only looking for exited processes and child_ns list
967e44bd21cSsusans 	 * is empty no reason to look at all children.
968e44bd21cSsusans 	 */
969e44bd21cSsusans 	if (idtype == P_ALL &&
970657b1f3dSraf 	    (options & ~WNOWAIT) == (WNOHANG | WEXITED) &&
971e44bd21cSsusans 	    pp->p_child_ns == NULL) {
972e44bd21cSsusans 		if (pp->p_child) {
973e44bd21cSsusans 			mutex_exit(&pidlock);
974e44bd21cSsusans 			bzero(ip, sizeof (k_siginfo_t));
975e44bd21cSsusans 			return (0);
976e44bd21cSsusans 		}
977e44bd21cSsusans 		mutex_exit(&pidlock);
978e44bd21cSsusans 		return (ECHILD);
979e44bd21cSsusans 	}
980e44bd21cSsusans 
981657b1f3dSraf 	while (pp->p_child != NULL) {
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		proc_gone = 0;
9847c478bd9Sstevel@tonic-gate 
985657b1f3dSraf 		for (cp = pp->p_child_ns; cp != NULL; cp = cp->p_sibling_ns) {
986657b1f3dSraf 			if (idtype != P_PID && (cp->p_pidflag & CLDWAITPID))
9877c478bd9Sstevel@tonic-gate 				continue;
988657b1f3dSraf 			if (idtype == P_PID && id != cp->p_pid)
9897c478bd9Sstevel@tonic-gate 				continue;
990657b1f3dSraf 			if (idtype == P_PGID && id != cp->p_pgrp)
991657b1f3dSraf 				continue;
9927c478bd9Sstevel@tonic-gate 
993657b1f3dSraf 			switch (cp->p_wcode) {
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 			case CLD_TRAPPED:
9967c478bd9Sstevel@tonic-gate 			case CLD_STOPPED:
9977c478bd9Sstevel@tonic-gate 			case CLD_CONTINUED:
9987c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC,
9997c478bd9Sstevel@tonic-gate 				    "waitid: wrong state %d on the p_newstate"
1000657b1f3dSraf 				    " list", cp->p_wcode);
10017c478bd9Sstevel@tonic-gate 				break;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 			case CLD_EXITED:
10047c478bd9Sstevel@tonic-gate 			case CLD_DUMPED:
10057c478bd9Sstevel@tonic-gate 			case CLD_KILLED:
10067c478bd9Sstevel@tonic-gate 				if (!(options & WEXITED)) {
10077c478bd9Sstevel@tonic-gate 					/*
10087c478bd9Sstevel@tonic-gate 					 * Count how many are already gone
10097c478bd9Sstevel@tonic-gate 					 * for good.
10107c478bd9Sstevel@tonic-gate 					 */
10117c478bd9Sstevel@tonic-gate 					proc_gone++;
10127c478bd9Sstevel@tonic-gate 					break;
10137c478bd9Sstevel@tonic-gate 				}
10147c478bd9Sstevel@tonic-gate 				if (!waitflag) {
1015657b1f3dSraf 					winfo(cp, ip, 0);
10167c478bd9Sstevel@tonic-gate 				} else {
1017657b1f3dSraf 					winfo(cp, ip, 1);
1018657b1f3dSraf 					freeproc(cp);
10197c478bd9Sstevel@tonic-gate 				}
10207c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
10217c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10227c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
10237c478bd9Sstevel@tonic-gate 					sigcld_repost();
10247c478bd9Sstevel@tonic-gate 				}
10257c478bd9Sstevel@tonic-gate 				return (0);
10267c478bd9Sstevel@tonic-gate 			}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 			if (idtype == P_PID)
10297c478bd9Sstevel@tonic-gate 				break;
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		/*
10337c478bd9Sstevel@tonic-gate 		 * Wow! None of the threads on the p_sibling_ns list were
10347c478bd9Sstevel@tonic-gate 		 * interesting threads. Check all the kids!
10357c478bd9Sstevel@tonic-gate 		 */
10367c478bd9Sstevel@tonic-gate 		found = 0;
1037657b1f3dSraf 		for (cp = pp->p_child; cp != NULL; cp = cp->p_sibling) {
1038657b1f3dSraf 			if (idtype == P_PID && id != cp->p_pid)
10397c478bd9Sstevel@tonic-gate 				continue;
1040657b1f3dSraf 			if (idtype == P_PGID && id != cp->p_pgrp)
10417c478bd9Sstevel@tonic-gate 				continue;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 			switch (cp->p_wcode) {
10447c478bd9Sstevel@tonic-gate 			case CLD_TRAPPED:
10457c478bd9Sstevel@tonic-gate 				if (!(options & WTRAPPED))
10467c478bd9Sstevel@tonic-gate 					break;
10477c478bd9Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
10487c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
10497c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10507c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
10517c478bd9Sstevel@tonic-gate 					sigcld_repost();
10527c478bd9Sstevel@tonic-gate 				}
10537c478bd9Sstevel@tonic-gate 				return (0);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 			case CLD_STOPPED:
10567c478bd9Sstevel@tonic-gate 				if (!(options & WSTOPPED))
10577c478bd9Sstevel@tonic-gate 					break;
10587c478bd9Sstevel@tonic-gate 				/* Is it still stopped? */
10597c478bd9Sstevel@tonic-gate 				mutex_enter(&cp->p_lock);
10607c478bd9Sstevel@tonic-gate 				if (!jobstopped(cp)) {
10617c478bd9Sstevel@tonic-gate 					mutex_exit(&cp->p_lock);
10627c478bd9Sstevel@tonic-gate 					break;
10637c478bd9Sstevel@tonic-gate 				}
10647c478bd9Sstevel@tonic-gate 				mutex_exit(&cp->p_lock);
10657c478bd9Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
10667c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
10677c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10687c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
10697c478bd9Sstevel@tonic-gate 					sigcld_repost();
10707c478bd9Sstevel@tonic-gate 				}
10717c478bd9Sstevel@tonic-gate 				return (0);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 			case CLD_CONTINUED:
10747c478bd9Sstevel@tonic-gate 				if (!(options & WCONTINUED))
10757c478bd9Sstevel@tonic-gate 					break;
10767c478bd9Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
10777c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
10787c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10797c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
10807c478bd9Sstevel@tonic-gate 					sigcld_repost();
10817c478bd9Sstevel@tonic-gate 				}
10827c478bd9Sstevel@tonic-gate 				return (0);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 			case CLD_EXITED:
10857c478bd9Sstevel@tonic-gate 			case CLD_DUMPED:
10867c478bd9Sstevel@tonic-gate 			case CLD_KILLED:
1087657b1f3dSraf 				if (idtype != P_PID &&
1088657b1f3dSraf 				    (cp->p_pidflag & CLDWAITPID))
1089657b1f3dSraf 					continue;
10907c478bd9Sstevel@tonic-gate 				/*
10917c478bd9Sstevel@tonic-gate 				 * Don't complain if a process was found in
10927c478bd9Sstevel@tonic-gate 				 * the first loop but we broke out of the loop
10937c478bd9Sstevel@tonic-gate 				 * because of the arguments passed to us.
10947c478bd9Sstevel@tonic-gate 				 */
10957c478bd9Sstevel@tonic-gate 				if (proc_gone == 0) {
10967c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC,
10977c478bd9Sstevel@tonic-gate 					    "waitid: wrong state on the"
10987c478bd9Sstevel@tonic-gate 					    " p_child list");
10997c478bd9Sstevel@tonic-gate 				} else {
11007c478bd9Sstevel@tonic-gate 					break;
11017c478bd9Sstevel@tonic-gate 				}
11027c478bd9Sstevel@tonic-gate 			}
11037c478bd9Sstevel@tonic-gate 
1104657b1f3dSraf 			found++;
1105657b1f3dSraf 
11067c478bd9Sstevel@tonic-gate 			if (idtype == P_PID)
11077c478bd9Sstevel@tonic-gate 				break;
1108657b1f3dSraf 		}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		/*
11117c478bd9Sstevel@tonic-gate 		 * If we found no interesting processes at all,
11127c478bd9Sstevel@tonic-gate 		 * break out and return ECHILD.
11137c478bd9Sstevel@tonic-gate 		 */
11147c478bd9Sstevel@tonic-gate 		if (found + proc_gone == 0)
11157c478bd9Sstevel@tonic-gate 			break;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 		if (options & WNOHANG) {
1118657b1f3dSraf 			mutex_exit(&pidlock);
11197c478bd9Sstevel@tonic-gate 			bzero(ip, sizeof (k_siginfo_t));
11207c478bd9Sstevel@tonic-gate 			/*
11217c478bd9Sstevel@tonic-gate 			 * We should set ip->si_signo = SIGCLD,
11227c478bd9Sstevel@tonic-gate 			 * but there is an SVVS test that expects
11237c478bd9Sstevel@tonic-gate 			 * ip->si_signo to be zero in this case.
11247c478bd9Sstevel@tonic-gate 			 */
11257c478bd9Sstevel@tonic-gate 			return (0);
11267c478bd9Sstevel@tonic-gate 		}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 		/*
11297c478bd9Sstevel@tonic-gate 		 * If we found no processes of interest that could
11307c478bd9Sstevel@tonic-gate 		 * change state while we wait, we don't wait at all.
11317c478bd9Sstevel@tonic-gate 		 * Get out with ECHILD according to SVID.
11327c478bd9Sstevel@tonic-gate 		 */
11337c478bd9Sstevel@tonic-gate 		if (found == proc_gone)
11347c478bd9Sstevel@tonic-gate 			break;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig_swap(&pp->p_cv, &pidlock)) {
11377c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
11387c478bd9Sstevel@tonic-gate 			return (EINTR);
11397c478bd9Sstevel@tonic-gate 		}
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
11427c478bd9Sstevel@tonic-gate 	return (ECHILD);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate int
11467c478bd9Sstevel@tonic-gate waitsys(idtype_t idtype, id_t id, siginfo_t *infop, int options)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	int error;
11497c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
11527c478bd9Sstevel@tonic-gate 		return (set_errno(error));
11537c478bd9Sstevel@tonic-gate 	if (copyout(&info, infop, sizeof (k_siginfo_t)))
11547c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
11557c478bd9Sstevel@tonic-gate 	return (0);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate int
11617c478bd9Sstevel@tonic-gate waitsys32(idtype_t idtype, id_t id, siginfo_t *infop, int options)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	int error;
11647c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
11657c478bd9Sstevel@tonic-gate 	siginfo32_t info32;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
11687c478bd9Sstevel@tonic-gate 		return (set_errno(error));
11697c478bd9Sstevel@tonic-gate 	siginfo_kto32(&info, &info32);
11707c478bd9Sstevel@tonic-gate 	if (copyout(&info32, infop, sizeof (info32)))
11717c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
11727c478bd9Sstevel@tonic-gate 	return (0);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate void
11787c478bd9Sstevel@tonic-gate proc_detach(proc_t *p)
11797c478bd9Sstevel@tonic-gate {
11807c478bd9Sstevel@tonic-gate 	proc_t *q;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	q = p->p_parent;
11857c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	/*
11887c478bd9Sstevel@tonic-gate 	 * Take it off the newstate list of its parent
11897c478bd9Sstevel@tonic-gate 	 */
11907c478bd9Sstevel@tonic-gate 	delete_ns(q, p);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (q->p_child == p) {
11937c478bd9Sstevel@tonic-gate 		q->p_child = p->p_sibling;
11947c478bd9Sstevel@tonic-gate 		/*
11957c478bd9Sstevel@tonic-gate 		 * If the parent has no children, it better not
11967c478bd9Sstevel@tonic-gate 		 * have any with new states either!
11977c478bd9Sstevel@tonic-gate 		 */
11987c478bd9Sstevel@tonic-gate 		ASSERT(q->p_child ? 1 : q->p_child_ns == NULL);
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (p->p_sibling) {
12027c478bd9Sstevel@tonic-gate 		p->p_sibling->p_psibling = p->p_psibling;
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (p->p_psibling) {
12067c478bd9Sstevel@tonic-gate 		p->p_psibling->p_sibling = p->p_sibling;
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate /*
12117c478bd9Sstevel@tonic-gate  * Remove zombie children from the process table.
12127c478bd9Sstevel@tonic-gate  */
12137c478bd9Sstevel@tonic-gate void
12147c478bd9Sstevel@tonic-gate freeproc(proc_t *p)
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	proc_t *q;
1217ff19e029SMenno Lageman 	task_t *tk;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	ASSERT(p->p_stat == SZOMB);
12207c478bd9Sstevel@tonic-gate 	ASSERT(p->p_tlist == NULL);
12217c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
12247c478bd9Sstevel@tonic-gate 	if (p->p_killsqp) {
12257c478bd9Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
12267c478bd9Sstevel@tonic-gate 		p->p_killsqp = NULL;
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	prfree(p);	/* inform /proc */
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * Don't free the init processes.
12337c478bd9Sstevel@tonic-gate 	 * Other dying processes will access it.
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	if (p == proc_init)
12367c478bd9Sstevel@tonic-gate 		return;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	/*
12407c478bd9Sstevel@tonic-gate 	 * We wait until now to free the cred structure because a
12417c478bd9Sstevel@tonic-gate 	 * zombie process's credentials may be examined by /proc.
12427c478bd9Sstevel@tonic-gate 	 * No cred locking needed because there are no threads at this point.
12437c478bd9Sstevel@tonic-gate 	 */
12447c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(p->p_cred), crgetzoneid(p->p_cred));
12457c478bd9Sstevel@tonic-gate 	crfree(p->p_cred);
12467c478bd9Sstevel@tonic-gate 	if (p->p_corefile != NULL) {
12477c478bd9Sstevel@tonic-gate 		corectl_path_rele(p->p_corefile);
12487c478bd9Sstevel@tonic-gate 		p->p_corefile = NULL;
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 	if (p->p_content != NULL) {
12517c478bd9Sstevel@tonic-gate 		corectl_content_rele(p->p_content);
12527c478bd9Sstevel@tonic-gate 		p->p_content = NULL;
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	if (p->p_nextofkin && !((p->p_nextofkin->p_flag & SNOWAIT) ||
12567c478bd9Sstevel@tonic-gate 	    (PTOU(p->p_nextofkin)->u_signal[SIGCLD - 1] == SIG_IGN))) {
12577c478bd9Sstevel@tonic-gate 		/*
12587c478bd9Sstevel@tonic-gate 		 * This should still do the right thing since p_utime/stime
12597c478bd9Sstevel@tonic-gate 		 * get set to the correct value on process exit, so it
12607c478bd9Sstevel@tonic-gate 		 * should get properly updated
12617c478bd9Sstevel@tonic-gate 		 */
12627c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cutime += p->p_utime;
12637c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cstime += p->p_stime;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER] += p->p_acct[LMS_USER];
12667c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SYSTEM] += p->p_acct[LMS_SYSTEM];
12677c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TRAP] += p->p_acct[LMS_TRAP];
12687c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TFAULT] += p->p_acct[LMS_TFAULT];
12697c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_DFAULT] += p->p_acct[LMS_DFAULT];
12707c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_KFAULT] += p->p_acct[LMS_KFAULT];
12717c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER_LOCK]
12727c478bd9Sstevel@tonic-gate 		    += p->p_acct[LMS_USER_LOCK];
12737c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SLEEP] += p->p_acct[LMS_SLEEP];
12747c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_WAIT_CPU]
12757c478bd9Sstevel@tonic-gate 		    += p->p_acct[LMS_WAIT_CPU];
12767c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_STOPPED] += p->p_acct[LMS_STOPPED];
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.minflt	+= p->p_ru.minflt;
12797c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.majflt	+= p->p_ru.majflt;
12807c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nswap	+= p->p_ru.nswap;
12817c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.inblock	+= p->p_ru.inblock;
12827c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.oublock	+= p->p_ru.oublock;
12837c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgsnd	+= p->p_ru.msgsnd;
12847c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgrcv	+= p->p_ru.msgrcv;
12857c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nsignals	+= p->p_ru.nsignals;
12867c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nvcsw	+= p->p_ru.nvcsw;
12877c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nivcsw	+= p->p_ru.nivcsw;
12887c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.sysc	+= p->p_ru.sysc;
12897c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.ioch	+= p->p_ru.ioch;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	q = p->p_nextofkin;
12947c478bd9Sstevel@tonic-gate 	if (q && q->p_orphan == p)
12957c478bd9Sstevel@tonic-gate 		q->p_orphan = p->p_nextorph;
12967c478bd9Sstevel@tonic-gate 	else if (q) {
12977c478bd9Sstevel@tonic-gate 		for (q = q->p_orphan; q; q = q->p_nextorph)
12987c478bd9Sstevel@tonic-gate 			if (q->p_nextorph == p)
12997c478bd9Sstevel@tonic-gate 				break;
13007c478bd9Sstevel@tonic-gate 		ASSERT(q && q->p_nextorph == p);
13017c478bd9Sstevel@tonic-gate 		q->p_nextorph = p->p_nextorph;
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
1304ff19e029SMenno Lageman 	/*
1305ff19e029SMenno Lageman 	 * The process table slot is being freed, so it is now safe to give up
1306ff19e029SMenno Lageman 	 * task and project membership.
1307ff19e029SMenno Lageman 	 */
1308ff19e029SMenno Lageman 	mutex_enter(&p->p_lock);
1309ff19e029SMenno Lageman 	tk = p->p_task;
1310ff19e029SMenno Lageman 	task_detach(p);
1311ff19e029SMenno Lageman 	mutex_exit(&p->p_lock);
1312ff19e029SMenno Lageman 
13137c478bd9Sstevel@tonic-gate 	proc_detach(p);
13144bcbe6e7SMenno Lageman 	pid_exit(p, tk);	/* frees pid and proc structure */
1315ff19e029SMenno Lageman 
1316ff19e029SMenno Lageman 	task_rele(tk);
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate /*
13207c478bd9Sstevel@tonic-gate  * Delete process "child" from the newstate list of process "parent"
13217c478bd9Sstevel@tonic-gate  */
13227c478bd9Sstevel@tonic-gate void
13237c478bd9Sstevel@tonic-gate delete_ns(proc_t *parent, proc_t *child)
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate 	proc_t **ns;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
13287c478bd9Sstevel@tonic-gate 	ASSERT(child->p_parent == parent);
13297c478bd9Sstevel@tonic-gate 	for (ns = &parent->p_child_ns; *ns != NULL; ns = &(*ns)->p_sibling_ns) {
13307c478bd9Sstevel@tonic-gate 		if (*ns == child) {
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 			ASSERT((*ns)->p_parent == parent);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 			*ns = child->p_sibling_ns;
13357c478bd9Sstevel@tonic-gate 			child->p_sibling_ns = NULL;
13367c478bd9Sstevel@tonic-gate 			return;
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate /*
13427c478bd9Sstevel@tonic-gate  * Add process "child" to the new state list of process "parent"
13437c478bd9Sstevel@tonic-gate  */
13447c478bd9Sstevel@tonic-gate void
13457c478bd9Sstevel@tonic-gate add_ns(proc_t *parent, proc_t *child)
13467c478bd9Sstevel@tonic-gate {
13477c478bd9Sstevel@tonic-gate 	ASSERT(child->p_sibling_ns == NULL);
13487c478bd9Sstevel@tonic-gate 	child->p_sibling_ns = parent->p_child_ns;
13497c478bd9Sstevel@tonic-gate 	parent->p_child_ns = child;
13507c478bd9Sstevel@tonic-gate }
1351