xref: /titanic_51/usr/src/uts/common/os/exit.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.74 */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/acct.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/var.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/session.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/sem_impl.h>
57*7c478bd9Sstevel@tonic-gate #include <c2/audit.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
59*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/poll.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/door.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/utrap.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/list.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
73*7c478bd9Sstevel@tonic-gate #include <sys/pool.h>
74*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
75*7c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate #if defined(__x86)
78*7c478bd9Sstevel@tonic-gate extern void ldt_free(proc_t *pp);
79*7c478bd9Sstevel@tonic-gate #endif
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate  * convert code/data pair into old style wait status
83*7c478bd9Sstevel@tonic-gate  */
84*7c478bd9Sstevel@tonic-gate int
85*7c478bd9Sstevel@tonic-gate wstat(int code, int data)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	int stat = (data & 0377);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	switch (code) {
90*7c478bd9Sstevel@tonic-gate 	case CLD_EXITED:
91*7c478bd9Sstevel@tonic-gate 		stat <<= 8;
92*7c478bd9Sstevel@tonic-gate 		break;
93*7c478bd9Sstevel@tonic-gate 	case CLD_DUMPED:
94*7c478bd9Sstevel@tonic-gate 		stat |= WCOREFLG;
95*7c478bd9Sstevel@tonic-gate 		break;
96*7c478bd9Sstevel@tonic-gate 	case CLD_KILLED:
97*7c478bd9Sstevel@tonic-gate 		break;
98*7c478bd9Sstevel@tonic-gate 	case CLD_TRAPPED:
99*7c478bd9Sstevel@tonic-gate 	case CLD_STOPPED:
100*7c478bd9Sstevel@tonic-gate 		stat <<= 8;
101*7c478bd9Sstevel@tonic-gate 		stat |= WSTOPFLG;
102*7c478bd9Sstevel@tonic-gate 		break;
103*7c478bd9Sstevel@tonic-gate 	case CLD_CONTINUED:
104*7c478bd9Sstevel@tonic-gate 		stat = WCONTFLG;
105*7c478bd9Sstevel@tonic-gate 		break;
106*7c478bd9Sstevel@tonic-gate 	default:
107*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "wstat: bad code");
108*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	return (stat);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate static char *
114*7c478bd9Sstevel@tonic-gate exit_reason(char *buf, size_t bufsz, int what, int why)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	switch (why) {
117*7c478bd9Sstevel@tonic-gate 	case CLD_EXITED:
118*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited with status %d", what);
119*7c478bd9Sstevel@tonic-gate 		break;
120*7c478bd9Sstevel@tonic-gate 	case CLD_KILLED:
121*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited on fatal signal %d", what);
122*7c478bd9Sstevel@tonic-gate 		break;
123*7c478bd9Sstevel@tonic-gate 	case CLD_DUMPED:
124*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "core dumped on signal %d", what);
125*7c478bd9Sstevel@tonic-gate 		break;
126*7c478bd9Sstevel@tonic-gate 	default:
127*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "encountered unknown error "
128*7c478bd9Sstevel@tonic-gate 		    "(%d, %d)", why, what);
129*7c478bd9Sstevel@tonic-gate 		break;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	return (buf);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * exit system call: pass back caller's arg.
137*7c478bd9Sstevel@tonic-gate  */
138*7c478bd9Sstevel@tonic-gate void
139*7c478bd9Sstevel@tonic-gate rexit(int rval)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, rval);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate  * Called by proc_exit() when a zone's init exits, presumably because
146*7c478bd9Sstevel@tonic-gate  * it failed.  As long as the given zone is still in the "running"
147*7c478bd9Sstevel@tonic-gate  * state, we will re-exec() init, but first we need to reset things
148*7c478bd9Sstevel@tonic-gate  * which are usually inherited across exec() but will break init's
149*7c478bd9Sstevel@tonic-gate  * assumption that it is being exec()'d from a virgin process.  Most
150*7c478bd9Sstevel@tonic-gate  * importantly this includes closing all file descriptors (exec only
151*7c478bd9Sstevel@tonic-gate  * closes those marked close-on-exec) and resetting signals (exec only
152*7c478bd9Sstevel@tonic-gate  * resets handled signals, and we need to clear any signals which
153*7c478bd9Sstevel@tonic-gate  * killed init).  Anything else that exec(2) says would be inherited,
154*7c478bd9Sstevel@tonic-gate  * but would affect the execution of init, needs to be reset.
155*7c478bd9Sstevel@tonic-gate  */
156*7c478bd9Sstevel@tonic-gate static int
157*7c478bd9Sstevel@tonic-gate restart_init(int what, int why)
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
160*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
161*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
162*7c478bd9Sstevel@tonic-gate 	user_t *up = PTOU(p);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	vnode_t *oldcd, *oldrd;
165*7c478bd9Sstevel@tonic-gate 	sess_t *sp;
166*7c478bd9Sstevel@tonic-gate 	int i, err;
167*7c478bd9Sstevel@tonic-gate 	char reason_buf[64];
168*7c478bd9Sstevel@tonic-gate 	const char *ipath;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	/*
171*7c478bd9Sstevel@tonic-gate 	 * Let zone admin (and global zone admin if this is for a non-global
172*7c478bd9Sstevel@tonic-gate 	 * zone) know that init has failed and will be restarted.
173*7c478bd9Sstevel@tonic-gate 	 */
174*7c478bd9Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
175*7c478bd9Sstevel@tonic-gate 	    "init(1M) %s: restarting automatically",
176*7c478bd9Sstevel@tonic-gate 	    exit_reason(reason_buf, sizeof (reason_buf), what, why));
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
179*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "init(1M) for zone %s (pid %d) %s: "
180*7c478bd9Sstevel@tonic-gate 		    "restarting automatically",
181*7c478bd9Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, reason_buf);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	/*
185*7c478bd9Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
186*7c478bd9Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
187*7c478bd9Sstevel@tonic-gate 	 * Then close all open file descriptors in the process.
188*7c478bd9Sstevel@tonic-gate 	 */
189*7c478bd9Sstevel@tonic-gate 	pollcleanup();
190*7c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(p));
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	/*
193*7c478bd9Sstevel@tonic-gate 	 * Grab p_lock and begin clearing miscellaneous global process
194*7c478bd9Sstevel@tonic-gate 	 * state that needs to be reset before we exec the new init(1M).
195*7c478bd9Sstevel@tonic-gate 	 */
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	p->p_flag &= ~(SKILLED | SEXITLWPS | SEXTKILLED | SCOREDUMP | SDOCORE);
200*7c478bd9Sstevel@tonic-gate 	up->u_cmask = CMASK;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_hold);
203*7c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
204*7c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
207*7c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	sigdelq(p, t, 0);
210*7c478bd9Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if (p->p_killsqp) {
213*7c478bd9Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
214*7c478bd9Sstevel@tonic-gate 		p->p_killsqp = NULL;
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	/*
218*7c478bd9Sstevel@tonic-gate 	 * Reset any signals that are ignored back to the default disposition.
219*7c478bd9Sstevel@tonic-gate 	 * Other u_signal members will be cleared when exec calls sigdefault().
220*7c478bd9Sstevel@tonic-gate 	 */
221*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++) {
222*7c478bd9Sstevel@tonic-gate 		if (up->u_signal[i - 1] == SIG_IGN) {
223*7c478bd9Sstevel@tonic-gate 			up->u_signal[i - 1] = SIG_DFL;
224*7c478bd9Sstevel@tonic-gate 			sigemptyset(&up->u_sigmask[i - 1]);
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	/*
229*7c478bd9Sstevel@tonic-gate 	 * Clear the current signal, any signal info associated with it, and
230*7c478bd9Sstevel@tonic-gate 	 * any signal information from contracts and/or contract templates.
231*7c478bd9Sstevel@tonic-gate 	 */
232*7c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
233*7c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
234*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo != NULL) {
235*7c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
236*7c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	/*
241*7c478bd9Sstevel@tonic-gate 	 * Reset both the process root directory and the current working
242*7c478bd9Sstevel@tonic-gate 	 * directory to the root of the zone just as we do during boot.
243*7c478bd9Sstevel@tonic-gate 	 */
244*7c478bd9Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
245*7c478bd9Sstevel@tonic-gate 	oldrd = up->u_rdir;
246*7c478bd9Sstevel@tonic-gate 	up->u_rdir = p->p_zone->zone_rootvp;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
249*7c478bd9Sstevel@tonic-gate 	oldcd = up->u_cdir;
250*7c478bd9Sstevel@tonic-gate 	up->u_cdir = p->p_zone->zone_rootvp;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	if (up->u_cwd != NULL) {
253*7c478bd9Sstevel@tonic-gate 		refstr_rele(up->u_cwd);
254*7c478bd9Sstevel@tonic-gate 		up->u_cwd = NULL;
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (oldrd != NULL)
260*7c478bd9Sstevel@tonic-gate 		VN_RELE(oldrd);
261*7c478bd9Sstevel@tonic-gate 	if (oldcd != NULL)
262*7c478bd9Sstevel@tonic-gate 		VN_RELE(oldcd);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	/*
265*7c478bd9Sstevel@tonic-gate 	 * Free the controlling tty.
266*7c478bd9Sstevel@tonic-gate 	 */
267*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
268*7c478bd9Sstevel@tonic-gate 	sp = p->p_sessp;
269*7c478bd9Sstevel@tonic-gate 	if (sp->s_sidp == p->p_pidp && sp->s_vp != NULL) {
270*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
271*7c478bd9Sstevel@tonic-gate 		freectty(sp);
272*7c478bd9Sstevel@tonic-gate 	} else {
273*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	/*
277*7c478bd9Sstevel@tonic-gate 	 * Now exec() the new init(1M) on top of the current process.  If we
278*7c478bd9Sstevel@tonic-gate 	 * succeed, the caller will treat this like a successful system call.
279*7c478bd9Sstevel@tonic-gate 	 * If we fail, we issue messages and the caller will proceed with exit.
280*7c478bd9Sstevel@tonic-gate 	 */
281*7c478bd9Sstevel@tonic-gate 	ipath = INGLOBALZONE(p) ? initname : zone_initname;
282*7c478bd9Sstevel@tonic-gate 	err = exec_init(ipath, 0, NULL);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	if (err == 0)
285*7c478bd9Sstevel@tonic-gate 		return (0);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
288*7c478bd9Sstevel@tonic-gate 	    "failed to restart init(1M) (err=%d): system reboot required", err);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
291*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to restart init(1M) for zone %s "
292*7c478bd9Sstevel@tonic-gate 		    "(pid %d, err=%d): zoneadm(1M) boot required",
293*7c478bd9Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, err);
294*7c478bd9Sstevel@tonic-gate 	}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	return (-1);
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /*
300*7c478bd9Sstevel@tonic-gate  * Release resources.
301*7c478bd9Sstevel@tonic-gate  * Enter zombie state.
302*7c478bd9Sstevel@tonic-gate  * Wake up parent and init processes,
303*7c478bd9Sstevel@tonic-gate  * and dispose of children.
304*7c478bd9Sstevel@tonic-gate  */
305*7c478bd9Sstevel@tonic-gate void
306*7c478bd9Sstevel@tonic-gate exit(int why, int what)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	/*
309*7c478bd9Sstevel@tonic-gate 	 * If proc_exit() fails, then some other lwp in the process
310*7c478bd9Sstevel@tonic-gate 	 * got there first.  We just have to call lwp_exit() to allow
311*7c478bd9Sstevel@tonic-gate 	 * the other lwp to finish exiting the process.  Otherwise we're
312*7c478bd9Sstevel@tonic-gate 	 * restarting init, and should return.
313*7c478bd9Sstevel@tonic-gate 	 */
314*7c478bd9Sstevel@tonic-gate 	if (proc_exit(why, what) != 0) {
315*7c478bd9Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
316*7c478bd9Sstevel@tonic-gate 		ASSERT(curproc->p_flag & SEXITLWPS);
317*7c478bd9Sstevel@tonic-gate 		lwp_exit();
318*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
319*7c478bd9Sstevel@tonic-gate 	}
320*7c478bd9Sstevel@tonic-gate }
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate /*
323*7c478bd9Sstevel@tonic-gate  * Return value:
324*7c478bd9Sstevel@tonic-gate  *   1 - exitlwps() failed, call (or continue) lwp_exit()
325*7c478bd9Sstevel@tonic-gate  *   0 - restarting init.  Return through system call path
326*7c478bd9Sstevel@tonic-gate  */
327*7c478bd9Sstevel@tonic-gate int
328*7c478bd9Sstevel@tonic-gate proc_exit(int why, int what)
329*7c478bd9Sstevel@tonic-gate {
330*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
331*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
332*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
333*7c478bd9Sstevel@tonic-gate 	zone_t *z = p->p_zone;
334*7c478bd9Sstevel@tonic-gate 	timeout_id_t tmp_id;
335*7c478bd9Sstevel@tonic-gate 	int rv;
336*7c478bd9Sstevel@tonic-gate 	proc_t *q;
337*7c478bd9Sstevel@tonic-gate 	sess_t *sp;
338*7c478bd9Sstevel@tonic-gate 	task_t *tk;
339*7c478bd9Sstevel@tonic-gate 	vnode_t *exec_vp, *execdir_vp, *cdir, *rdir;
340*7c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp;
341*7c478bd9Sstevel@tonic-gate 	lwpdir_t *lwpdir;
342*7c478bd9Sstevel@tonic-gate 	uint_t lwpdir_sz;
343*7c478bd9Sstevel@tonic-gate 	lwpdir_t **tidhash;
344*7c478bd9Sstevel@tonic-gate 	uint_t tidhash_sz;
345*7c478bd9Sstevel@tonic-gate 	refstr_t *cwd;
346*7c478bd9Sstevel@tonic-gate 	hrtime_t hrutime, hrstime;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/*
349*7c478bd9Sstevel@tonic-gate 	 * Stop and discard the process's lwps except for the current one,
350*7c478bd9Sstevel@tonic-gate 	 * unless some other lwp beat us to it.  If exitlwps() fails then
351*7c478bd9Sstevel@tonic-gate 	 * return and the calling lwp will call (or continue in) lwp_exit().
352*7c478bd9Sstevel@tonic-gate 	 */
353*7c478bd9Sstevel@tonic-gate 	if (exitlwps(0) != 0)
354*7c478bd9Sstevel@tonic-gate 		return (1);
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	DTRACE_PROC(lwp__exit);
357*7c478bd9Sstevel@tonic-gate 	DTRACE_PROC1(exit, int, why);
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	/*
360*7c478bd9Sstevel@tonic-gate 	 * Don't let init exit unless zone_icode() failed its exec, or
361*7c478bd9Sstevel@tonic-gate 	 * we are shutting down the zone or the machine.
362*7c478bd9Sstevel@tonic-gate 	 *
363*7c478bd9Sstevel@tonic-gate 	 * Since we are single threaded, we don't need to lock the
364*7c478bd9Sstevel@tonic-gate 	 * following accesses to zone_proc_initpid.
365*7c478bd9Sstevel@tonic-gate 	 */
366*7c478bd9Sstevel@tonic-gate 	if (p->p_pid == z->zone_proc_initpid) {
367*7c478bd9Sstevel@tonic-gate 		if (z->zone_boot_err == 0 &&
368*7c478bd9Sstevel@tonic-gate 		    zone_status_get(z) < ZONE_IS_SHUTTING_DOWN &&
369*7c478bd9Sstevel@tonic-gate 		    zone_status_get(global_zone) < ZONE_IS_SHUTTING_DOWN &&
370*7c478bd9Sstevel@tonic-gate 		    restart_init(what, why) == 0)
371*7c478bd9Sstevel@tonic-gate 			return (0);
372*7c478bd9Sstevel@tonic-gate 		/*
373*7c478bd9Sstevel@tonic-gate 		 * Since we didn't or couldn't restart init, we clear
374*7c478bd9Sstevel@tonic-gate 		 * the zone's init state and proceed with exit
375*7c478bd9Sstevel@tonic-gate 		 * processing.
376*7c478bd9Sstevel@tonic-gate 		 */
377*7c478bd9Sstevel@tonic-gate 		z->zone_proc_initpid = -1;
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	/*
381*7c478bd9Sstevel@tonic-gate 	 * Allocate a sigqueue now, before we grab locks.
382*7c478bd9Sstevel@tonic-gate 	 * It will be given to sigcld(), below.
383*7c478bd9Sstevel@tonic-gate 	 */
384*7c478bd9Sstevel@tonic-gate 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	/*
387*7c478bd9Sstevel@tonic-gate 	 * revoke any doors created by the process.
388*7c478bd9Sstevel@tonic-gate 	 */
389*7c478bd9Sstevel@tonic-gate 	if (p->p_door_list)
390*7c478bd9Sstevel@tonic-gate 		door_exit();
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/*
393*7c478bd9Sstevel@tonic-gate 	 * Release schedctl data structures.
394*7c478bd9Sstevel@tonic-gate 	 */
395*7c478bd9Sstevel@tonic-gate 	if (p->p_pagep)
396*7c478bd9Sstevel@tonic-gate 		schedctl_proc_cleanup();
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	/*
399*7c478bd9Sstevel@tonic-gate 	 * make sure all pending kaio has completed.
400*7c478bd9Sstevel@tonic-gate 	 */
401*7c478bd9Sstevel@tonic-gate 	if (p->p_aio)
402*7c478bd9Sstevel@tonic-gate 		aio_cleanup_exit();
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	/*
405*7c478bd9Sstevel@tonic-gate 	 * discard the lwpchan cache.
406*7c478bd9Sstevel@tonic-gate 	 */
407*7c478bd9Sstevel@tonic-gate 	if (p->p_lcp != NULL)
408*7c478bd9Sstevel@tonic-gate 		lwpchan_destroy_cache(0);
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	/*
411*7c478bd9Sstevel@tonic-gate 	 * Clean up any DTrace helper actions or probes for the process.
412*7c478bd9Sstevel@tonic-gate 	 */
413*7c478bd9Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
414*7c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
415*7c478bd9Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
416*7c478bd9Sstevel@tonic-gate 	}
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	/* untimeout the realtime timers */
419*7c478bd9Sstevel@tonic-gate 	if (p->p_itimer != NULL)
420*7c478bd9Sstevel@tonic-gate 		timer_exit();
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	if ((tmp_id = p->p_alarmid) != 0) {
423*7c478bd9Sstevel@tonic-gate 		p->p_alarmid = 0;
424*7c478bd9Sstevel@tonic-gate 		(void) untimeout(tmp_id);
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	/*
428*7c478bd9Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
429*7c478bd9Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
430*7c478bd9Sstevel@tonic-gate 	 */
431*7c478bd9Sstevel@tonic-gate 	pollcleanup();
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	if (p->p_rprof_cyclic != CYCLIC_NONE) {
434*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
435*7c478bd9Sstevel@tonic-gate 		cyclic_remove(p->p_rprof_cyclic);
436*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	/*
442*7c478bd9Sstevel@tonic-gate 	 * Clean up any DTrace probes associated with this process.
443*7c478bd9Sstevel@tonic-gate 	 */
444*7c478bd9Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
445*7c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exit_ptr != NULL);
446*7c478bd9Sstevel@tonic-gate 		dtrace_fasttrap_exit_ptr(p);
447*7c478bd9Sstevel@tonic-gate 	}
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	while ((tmp_id = p->p_itimerid) != 0) {
450*7c478bd9Sstevel@tonic-gate 		p->p_itimerid = 0;
451*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
452*7c478bd9Sstevel@tonic-gate 		(void) untimeout(tmp_id);
453*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	lwp_cleanup();
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	/*
459*7c478bd9Sstevel@tonic-gate 	 * We are about to exit; prevent our resource associations from
460*7c478bd9Sstevel@tonic-gate 	 * being changed.
461*7c478bd9Sstevel@tonic-gate 	 */
462*7c478bd9Sstevel@tonic-gate 	pool_barrier_enter();
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	/*
465*7c478bd9Sstevel@tonic-gate 	 * Block the process against /proc now that we have really
466*7c478bd9Sstevel@tonic-gate 	 * acquired p->p_lock (to manipulate p_tlist at least).
467*7c478bd9Sstevel@tonic-gate 	 */
468*7c478bd9Sstevel@tonic-gate 	prbarrier(p);
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate #ifdef	SUN_SRC_COMPAT
471*7c478bd9Sstevel@tonic-gate 	if (code == CLD_KILLED)
472*7c478bd9Sstevel@tonic-gate 		u.u_acflag |= AXSIG;
473*7c478bd9Sstevel@tonic-gate #endif
474*7c478bd9Sstevel@tonic-gate 	sigfillset(&p->p_ignore);
475*7c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_siginfo);
476*7c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
477*7c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
478*7c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
479*7c478bd9Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
480*7c478bd9Sstevel@tonic-gate 	sigemptyset(&p->p_sigmask);
481*7c478bd9Sstevel@tonic-gate 	sigdelq(p, t, 0);
482*7c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
483*7c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
484*7c478bd9Sstevel@tonic-gate 	p->p_flag &= ~(SKILLED | SEXTKILLED);
485*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
486*7c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
487*7c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
488*7c478bd9Sstevel@tonic-gate 	}
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_LWPEXIT;
491*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
492*7c478bd9Sstevel@tonic-gate 	prlwpexit(t);		/* notify /proc */
493*7c478bd9Sstevel@tonic-gate 	lwp_hash_out(p, t->t_tid);
494*7c478bd9Sstevel@tonic-gate 	prexit(p);
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	p->p_lwpcnt = 0;
497*7c478bd9Sstevel@tonic-gate 	p->p_tlist = NULL;
498*7c478bd9Sstevel@tonic-gate 	sigqfree(p);
499*7c478bd9Sstevel@tonic-gate 	term_mstate(t);
500*7c478bd9Sstevel@tonic-gate 	p->p_mterm = gethrtime();
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	exec_vp = p->p_exec;
503*7c478bd9Sstevel@tonic-gate 	execdir_vp = p->p_execdir;
504*7c478bd9Sstevel@tonic-gate 	p->p_exec = NULLVP;
505*7c478bd9Sstevel@tonic-gate 	p->p_execdir = NULLVP;
506*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
507*7c478bd9Sstevel@tonic-gate 	if (exec_vp)
508*7c478bd9Sstevel@tonic-gate 		VN_RELE(exec_vp);
509*7c478bd9Sstevel@tonic-gate 	if (execdir_vp)
510*7c478bd9Sstevel@tonic-gate 		VN_RELE(execdir_vp);
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	pr_free_watched_pages(p);
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(p));
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
517*7c478bd9Sstevel@tonic-gate 	sp = p->p_sessp;
518*7c478bd9Sstevel@tonic-gate 	if (sp->s_sidp == p->p_pidp && sp->s_vp != NULL) {
519*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
520*7c478bd9Sstevel@tonic-gate 		freectty(sp);
521*7c478bd9Sstevel@tonic-gate 	} else
522*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate #if defined(__x86)
525*7c478bd9Sstevel@tonic-gate 	/*
526*7c478bd9Sstevel@tonic-gate 	 * If the process was using a private LDT then free it.
527*7c478bd9Sstevel@tonic-gate 	 */
528*7c478bd9Sstevel@tonic-gate 	if (p->p_ldt)
529*7c478bd9Sstevel@tonic-gate 		ldt_free(p);
530*7c478bd9Sstevel@tonic-gate #endif
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
533*7c478bd9Sstevel@tonic-gate 	if (p->p_utraps != NULL)
534*7c478bd9Sstevel@tonic-gate 		utrap_free(p);
535*7c478bd9Sstevel@tonic-gate #endif
536*7c478bd9Sstevel@tonic-gate 	if (p->p_semacct)			/* IPC semaphore exit */
537*7c478bd9Sstevel@tonic-gate 		semexit(p);
538*7c478bd9Sstevel@tonic-gate 	rv = wstat(why, what);
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	acct(rv & 0xff);
541*7c478bd9Sstevel@tonic-gate 	exacct_commit_proc(p, rv);
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	/*
544*7c478bd9Sstevel@tonic-gate 	 * Release any resources associated with C2 auditing
545*7c478bd9Sstevel@tonic-gate 	 */
546*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
547*7c478bd9Sstevel@tonic-gate 	if (audit_active) {
548*7c478bd9Sstevel@tonic-gate 		/*
549*7c478bd9Sstevel@tonic-gate 		 * audit exit system call
550*7c478bd9Sstevel@tonic-gate 		 */
551*7c478bd9Sstevel@tonic-gate 		audit_exit(why, what);
552*7c478bd9Sstevel@tonic-gate 	}
553*7c478bd9Sstevel@tonic-gate #endif
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	/*
556*7c478bd9Sstevel@tonic-gate 	 * Free address space.
557*7c478bd9Sstevel@tonic-gate 	 */
558*7c478bd9Sstevel@tonic-gate 	relvm();
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 	/*
561*7c478bd9Sstevel@tonic-gate 	 * Release held contracts.
562*7c478bd9Sstevel@tonic-gate 	 */
563*7c478bd9Sstevel@tonic-gate 	contract_exit(p);
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	/*
566*7c478bd9Sstevel@tonic-gate 	 * Depart our encapsulating process contract.
567*7c478bd9Sstevel@tonic-gate 	 */
568*7c478bd9Sstevel@tonic-gate 	if ((p->p_flag & SSYS) == 0) {
569*7c478bd9Sstevel@tonic-gate 		ASSERT(p->p_ct_process);
570*7c478bd9Sstevel@tonic-gate 		contract_process_exit(p->p_ct_process, p, rv);
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	/*
574*7c478bd9Sstevel@tonic-gate 	 * Remove pool association, and block if requested by pool_do_bind.
575*7c478bd9Sstevel@tonic-gate 	 */
576*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
577*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_pool->pool_ref > 0);
578*7c478bd9Sstevel@tonic-gate 	atomic_add_32(&p->p_pool->pool_ref, -1);
579*7c478bd9Sstevel@tonic-gate 	p->p_pool = pool_default;
580*7c478bd9Sstevel@tonic-gate 	/*
581*7c478bd9Sstevel@tonic-gate 	 * Now that our address space has been freed and all other threads
582*7c478bd9Sstevel@tonic-gate 	 * in this process have exited, set the PEXITED pool flag.  This
583*7c478bd9Sstevel@tonic-gate 	 * tells the pools subsystems to ignore this process if it was
584*7c478bd9Sstevel@tonic-gate 	 * requested to rebind this process to a new pool.
585*7c478bd9Sstevel@tonic-gate 	 */
586*7c478bd9Sstevel@tonic-gate 	p->p_poolflag |= PEXITED;
587*7c478bd9Sstevel@tonic-gate 	pool_barrier_exit();
588*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	/*
593*7c478bd9Sstevel@tonic-gate 	 * Delete this process from the newstate list of its parent. We
594*7c478bd9Sstevel@tonic-gate 	 * will put it in the right place in the sigcld in the end.
595*7c478bd9Sstevel@tonic-gate 	 */
596*7c478bd9Sstevel@tonic-gate 	delete_ns(p->p_parent, p);
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	/*
599*7c478bd9Sstevel@tonic-gate 	 * Reassign the orphans to the next of kin.
600*7c478bd9Sstevel@tonic-gate 	 * Don't rearrange init's orphanage.
601*7c478bd9Sstevel@tonic-gate 	 */
602*7c478bd9Sstevel@tonic-gate 	if ((q = p->p_orphan) != NULL && p != proc_init) {
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 		proc_t *nokp = p->p_nextofkin;
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 		for (;;) {
607*7c478bd9Sstevel@tonic-gate 			q->p_nextofkin = nokp;
608*7c478bd9Sstevel@tonic-gate 			if (q->p_nextorph == NULL)
609*7c478bd9Sstevel@tonic-gate 				break;
610*7c478bd9Sstevel@tonic-gate 			q = q->p_nextorph;
611*7c478bd9Sstevel@tonic-gate 		}
612*7c478bd9Sstevel@tonic-gate 		q->p_nextorph = nokp->p_orphan;
613*7c478bd9Sstevel@tonic-gate 		nokp->p_orphan = p->p_orphan;
614*7c478bd9Sstevel@tonic-gate 		p->p_orphan = NULL;
615*7c478bd9Sstevel@tonic-gate 	}
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 	/*
618*7c478bd9Sstevel@tonic-gate 	 * Reassign the children to init.
619*7c478bd9Sstevel@tonic-gate 	 * Don't try to assign init's children to init.
620*7c478bd9Sstevel@tonic-gate 	 */
621*7c478bd9Sstevel@tonic-gate 	if ((q = p->p_child) != NULL && p != proc_init) {
622*7c478bd9Sstevel@tonic-gate 		struct proc	*np;
623*7c478bd9Sstevel@tonic-gate 		struct proc	*initp = proc_init;
624*7c478bd9Sstevel@tonic-gate 		boolean_t	setzonetop = B_FALSE;
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 		if (!INGLOBALZONE(curproc))
627*7c478bd9Sstevel@tonic-gate 			setzonetop = B_TRUE;
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 		pgdetach(p);
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 		do {
632*7c478bd9Sstevel@tonic-gate 			np = q->p_sibling;
633*7c478bd9Sstevel@tonic-gate 			/*
634*7c478bd9Sstevel@tonic-gate 			 * Delete it from its current parent new state
635*7c478bd9Sstevel@tonic-gate 			 * list and add it to init new state list
636*7c478bd9Sstevel@tonic-gate 			 */
637*7c478bd9Sstevel@tonic-gate 			delete_ns(q->p_parent, q);
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 			q->p_ppid = 1;
640*7c478bd9Sstevel@tonic-gate 			if (setzonetop) {
641*7c478bd9Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
642*7c478bd9Sstevel@tonic-gate 				q->p_flag |= SZONETOP;
643*7c478bd9Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
644*7c478bd9Sstevel@tonic-gate 			}
645*7c478bd9Sstevel@tonic-gate 			q->p_parent = initp;
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 			/*
648*7c478bd9Sstevel@tonic-gate 			 * Since q will be the first child,
649*7c478bd9Sstevel@tonic-gate 			 * it will not have a previous sibling.
650*7c478bd9Sstevel@tonic-gate 			 */
651*7c478bd9Sstevel@tonic-gate 			q->p_psibling = NULL;
652*7c478bd9Sstevel@tonic-gate 			if (initp->p_child) {
653*7c478bd9Sstevel@tonic-gate 				initp->p_child->p_psibling = q;
654*7c478bd9Sstevel@tonic-gate 			}
655*7c478bd9Sstevel@tonic-gate 			q->p_sibling = initp->p_child;
656*7c478bd9Sstevel@tonic-gate 			initp->p_child = q;
657*7c478bd9Sstevel@tonic-gate 			if (q->p_proc_flag & P_PR_PTRACE) {
658*7c478bd9Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
659*7c478bd9Sstevel@tonic-gate 				sigtoproc(q, NULL, SIGKILL);
660*7c478bd9Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
661*7c478bd9Sstevel@tonic-gate 			}
662*7c478bd9Sstevel@tonic-gate 			/*
663*7c478bd9Sstevel@tonic-gate 			 * sigcld() will add the child to parents
664*7c478bd9Sstevel@tonic-gate 			 * newstate list.
665*7c478bd9Sstevel@tonic-gate 			 */
666*7c478bd9Sstevel@tonic-gate 			if (q->p_stat == SZOMB)
667*7c478bd9Sstevel@tonic-gate 				sigcld(q, NULL);
668*7c478bd9Sstevel@tonic-gate 		} while ((q = np) != NULL);
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 		p->p_child = NULL;
671*7c478bd9Sstevel@tonic-gate 		ASSERT(p->p_child_ns == NULL);
672*7c478bd9Sstevel@tonic-gate 	}
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_PROC, TR_PROC_EXIT, "proc_exit: %p", p);
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
677*7c478bd9Sstevel@tonic-gate 	CL_EXIT(curthread); /* tell the scheduler that curthread is exiting */
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 	hrutime = mstate_aggr_state(p, LMS_USER);
680*7c478bd9Sstevel@tonic-gate 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
681*7c478bd9Sstevel@tonic-gate 	p->p_utime = (clock_t)NSEC_TO_TICK(hrutime) + p->p_cutime;
682*7c478bd9Sstevel@tonic-gate 	p->p_stime = (clock_t)NSEC_TO_TICK(hrstime) + p->p_cstime;
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_USER]	+= p->p_cacct[LMS_USER];
685*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_SYSTEM]	+= p->p_cacct[LMS_SYSTEM];
686*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_TRAP]	+= p->p_cacct[LMS_TRAP];
687*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_TFAULT]	+= p->p_cacct[LMS_TFAULT];
688*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_DFAULT]	+= p->p_cacct[LMS_DFAULT];
689*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_KFAULT]	+= p->p_cacct[LMS_KFAULT];
690*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_USER_LOCK] += p->p_cacct[LMS_USER_LOCK];
691*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_SLEEP]	+= p->p_cacct[LMS_SLEEP];
692*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_WAIT_CPU]	+= p->p_cacct[LMS_WAIT_CPU];
693*7c478bd9Sstevel@tonic-gate 	p->p_acct[LMS_STOPPED]	+= p->p_cacct[LMS_STOPPED];
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 	p->p_ru.minflt	+= p->p_cru.minflt;
696*7c478bd9Sstevel@tonic-gate 	p->p_ru.majflt	+= p->p_cru.majflt;
697*7c478bd9Sstevel@tonic-gate 	p->p_ru.nswap	+= p->p_cru.nswap;
698*7c478bd9Sstevel@tonic-gate 	p->p_ru.inblock	+= p->p_cru.inblock;
699*7c478bd9Sstevel@tonic-gate 	p->p_ru.oublock	+= p->p_cru.oublock;
700*7c478bd9Sstevel@tonic-gate 	p->p_ru.msgsnd	+= p->p_cru.msgsnd;
701*7c478bd9Sstevel@tonic-gate 	p->p_ru.msgrcv	+= p->p_cru.msgrcv;
702*7c478bd9Sstevel@tonic-gate 	p->p_ru.nsignals += p->p_cru.nsignals;
703*7c478bd9Sstevel@tonic-gate 	p->p_ru.nvcsw	+= p->p_cru.nvcsw;
704*7c478bd9Sstevel@tonic-gate 	p->p_ru.nivcsw	+= p->p_cru.nivcsw;
705*7c478bd9Sstevel@tonic-gate 	p->p_ru.sysc	+= p->p_cru.sysc;
706*7c478bd9Sstevel@tonic-gate 	p->p_ru.ioch	+= p->p_cru.ioch;
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	p->p_stat = SZOMB;
709*7c478bd9Sstevel@tonic-gate 	p->p_proc_flag &= ~P_PR_PTRACE;
710*7c478bd9Sstevel@tonic-gate 	p->p_wdata = what;
711*7c478bd9Sstevel@tonic-gate 	p->p_wcode = (char)why;
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 	cdir = PTOU(p)->u_cdir;
714*7c478bd9Sstevel@tonic-gate 	rdir = PTOU(p)->u_rdir;
715*7c478bd9Sstevel@tonic-gate 	cwd = PTOU(p)->u_cwd;
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	/*
718*7c478bd9Sstevel@tonic-gate 	 * Release resource controls, as they are no longer enforceable.
719*7c478bd9Sstevel@tonic-gate 	 */
720*7c478bd9Sstevel@tonic-gate 	rctl_set_free(p->p_rctls);
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	/*
723*7c478bd9Sstevel@tonic-gate 	 * Give up task and project memberships.  Decrement tk_nlwps counter
724*7c478bd9Sstevel@tonic-gate 	 * for our task.max-lwps resource control.  An extended accounting
725*7c478bd9Sstevel@tonic-gate 	 * record, if that facility is active, is scheduled to be written.
726*7c478bd9Sstevel@tonic-gate 	 * Zombie processes are false members of task0 for the remainder of
727*7c478bd9Sstevel@tonic-gate 	 * their lifetime; no accounting information is recorded for them.
728*7c478bd9Sstevel@tonic-gate 	 */
729*7c478bd9Sstevel@tonic-gate 	tk = p->p_task;
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
732*7c478bd9Sstevel@tonic-gate 	tk->tk_nlwps--;
733*7c478bd9Sstevel@tonic-gate 	tk->tk_proj->kpj_nlwps--;
734*7c478bd9Sstevel@tonic-gate 	p->p_zone->zone_nlwps--;
735*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
736*7c478bd9Sstevel@tonic-gate 	task_detach(p);
737*7c478bd9Sstevel@tonic-gate 	p->p_task = task0p;
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	/*
740*7c478bd9Sstevel@tonic-gate 	 * Clear the lwp directory and the lwpid hash table
741*7c478bd9Sstevel@tonic-gate 	 * now that /proc can't bother us any more.
742*7c478bd9Sstevel@tonic-gate 	 * We free the memory below, after dropping p->p_lock.
743*7c478bd9Sstevel@tonic-gate 	 */
744*7c478bd9Sstevel@tonic-gate 	lwpdir = p->p_lwpdir;
745*7c478bd9Sstevel@tonic-gate 	lwpdir_sz = p->p_lwpdir_sz;
746*7c478bd9Sstevel@tonic-gate 	tidhash = p->p_tidhash;
747*7c478bd9Sstevel@tonic-gate 	tidhash_sz = p->p_tidhash_sz;
748*7c478bd9Sstevel@tonic-gate 	p->p_lwpdir = NULL;
749*7c478bd9Sstevel@tonic-gate 	p->p_lwpfree = NULL;
750*7c478bd9Sstevel@tonic-gate 	p->p_lwpdir_sz = 0;
751*7c478bd9Sstevel@tonic-gate 	p->p_tidhash = NULL;
752*7c478bd9Sstevel@tonic-gate 	p->p_tidhash_sz = 0;
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	/*
755*7c478bd9Sstevel@tonic-gate 	 * curthread's proc pointer is changed to point at p0 because
756*7c478bd9Sstevel@tonic-gate 	 * curthread's original proc pointer can be freed as soon as
757*7c478bd9Sstevel@tonic-gate 	 * the child sends a SIGCLD to its parent.
758*7c478bd9Sstevel@tonic-gate 	 */
759*7c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
762*7c478bd9Sstevel@tonic-gate 	sigcld(p, sqp);
763*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 	task_rele(tk);
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	kmem_free(lwpdir, lwpdir_sz * sizeof (lwpdir_t));
768*7c478bd9Sstevel@tonic-gate 	kmem_free(tidhash, tidhash_sz * sizeof (lwpdir_t *));
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 	/*
771*7c478bd9Sstevel@tonic-gate 	 * We don't release u_cdir and u_rdir until SZOMB is set.
772*7c478bd9Sstevel@tonic-gate 	 * This protects us against dofusers().
773*7c478bd9Sstevel@tonic-gate 	 */
774*7c478bd9Sstevel@tonic-gate 	VN_RELE(cdir);
775*7c478bd9Sstevel@tonic-gate 	if (rdir)
776*7c478bd9Sstevel@tonic-gate 		VN_RELE(rdir);
777*7c478bd9Sstevel@tonic-gate 	if (cwd)
778*7c478bd9Sstevel@tonic-gate 		refstr_rele(cwd);
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 	lwp_pcb_exit();
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate 	thread_exit();
783*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
784*7c478bd9Sstevel@tonic-gate }
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate /*
787*7c478bd9Sstevel@tonic-gate  * Format siginfo structure for wait system calls.
788*7c478bd9Sstevel@tonic-gate  */
789*7c478bd9Sstevel@tonic-gate void
790*7c478bd9Sstevel@tonic-gate winfo(proc_t *pp, k_siginfo_t *ip, int waitflag)
791*7c478bd9Sstevel@tonic-gate {
792*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate 	bzero(ip, sizeof (k_siginfo_t));
795*7c478bd9Sstevel@tonic-gate 	ip->si_signo = SIGCLD;
796*7c478bd9Sstevel@tonic-gate 	ip->si_code = pp->p_wcode;
797*7c478bd9Sstevel@tonic-gate 	ip->si_pid = pp->p_pid;
798*7c478bd9Sstevel@tonic-gate 	ip->si_ctid = PRCTID(pp);
799*7c478bd9Sstevel@tonic-gate 	ip->si_zoneid = pp->p_zone->zone_id;
800*7c478bd9Sstevel@tonic-gate 	ip->si_status = pp->p_wdata;
801*7c478bd9Sstevel@tonic-gate 	ip->si_stime = pp->p_stime;
802*7c478bd9Sstevel@tonic-gate 	ip->si_utime = pp->p_utime;
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 	if (waitflag) {
805*7c478bd9Sstevel@tonic-gate 		pp->p_wcode = 0;
806*7c478bd9Sstevel@tonic-gate 		pp->p_wdata = 0;
807*7c478bd9Sstevel@tonic-gate 		pp->p_pidflag &= ~CLDPEND;
808*7c478bd9Sstevel@tonic-gate 	}
809*7c478bd9Sstevel@tonic-gate }
810*7c478bd9Sstevel@tonic-gate 
811*7c478bd9Sstevel@tonic-gate /*
812*7c478bd9Sstevel@tonic-gate  * Wait system call.
813*7c478bd9Sstevel@tonic-gate  * Search for a terminated (zombie) child,
814*7c478bd9Sstevel@tonic-gate  * finally lay it to rest, and collect its status.
815*7c478bd9Sstevel@tonic-gate  * Look also for stopped children,
816*7c478bd9Sstevel@tonic-gate  * and pass back status from them.
817*7c478bd9Sstevel@tonic-gate  */
818*7c478bd9Sstevel@tonic-gate int
819*7c478bd9Sstevel@tonic-gate waitid(idtype_t idtype, id_t id, k_siginfo_t *ip, int options)
820*7c478bd9Sstevel@tonic-gate {
821*7c478bd9Sstevel@tonic-gate 	int found;
822*7c478bd9Sstevel@tonic-gate 	proc_t *cp, *pp;
823*7c478bd9Sstevel@tonic-gate 	proc_t **nsp;
824*7c478bd9Sstevel@tonic-gate 	int proc_gone;
825*7c478bd9Sstevel@tonic-gate 	int waitflag = !(options & WNOWAIT);
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate 	/*
828*7c478bd9Sstevel@tonic-gate 	 * Obsolete flag, defined here only for binary compatibility
829*7c478bd9Sstevel@tonic-gate 	 * with old statically linked executables.  Delete this when
830*7c478bd9Sstevel@tonic-gate 	 * we no longer care about these old and broken applications.
831*7c478bd9Sstevel@tonic-gate 	 */
832*7c478bd9Sstevel@tonic-gate #define	_WNOCHLD	0400
833*7c478bd9Sstevel@tonic-gate 	options &= ~_WNOCHLD;
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 	if (options == 0 || (options & ~WOPTMASK))
836*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
837*7c478bd9Sstevel@tonic-gate 
838*7c478bd9Sstevel@tonic-gate 	switch (idtype) {
839*7c478bd9Sstevel@tonic-gate 	case P_PID:
840*7c478bd9Sstevel@tonic-gate 	case P_PGID:
841*7c478bd9Sstevel@tonic-gate 		if (id < 0 || id >= maxpid)
842*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
843*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
844*7c478bd9Sstevel@tonic-gate 	case P_ALL:
845*7c478bd9Sstevel@tonic-gate 		break;
846*7c478bd9Sstevel@tonic-gate 	default:
847*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
848*7c478bd9Sstevel@tonic-gate 	}
849*7c478bd9Sstevel@tonic-gate 
850*7c478bd9Sstevel@tonic-gate 	pp = ttoproc(curthread);
851*7c478bd9Sstevel@tonic-gate 	/*
852*7c478bd9Sstevel@tonic-gate 	 * lock parent mutex so that sibling chain can be searched.
853*7c478bd9Sstevel@tonic-gate 	 */
854*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
855*7c478bd9Sstevel@tonic-gate 	while ((cp = pp->p_child) != NULL) {
856*7c478bd9Sstevel@tonic-gate 
857*7c478bd9Sstevel@tonic-gate 		proc_gone = 0;
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate 		for (nsp = &pp->p_child_ns; *nsp; nsp = &(*nsp)->p_sibling_ns) {
860*7c478bd9Sstevel@tonic-gate 			if (idtype == P_PID && id != (*nsp)->p_pid) {
861*7c478bd9Sstevel@tonic-gate 				continue;
862*7c478bd9Sstevel@tonic-gate 			}
863*7c478bd9Sstevel@tonic-gate 			if (idtype == P_PGID && id != (*nsp)->p_pgrp) {
864*7c478bd9Sstevel@tonic-gate 				continue;
865*7c478bd9Sstevel@tonic-gate 			}
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 			switch ((*nsp)->p_wcode) {
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 			case CLD_TRAPPED:
870*7c478bd9Sstevel@tonic-gate 			case CLD_STOPPED:
871*7c478bd9Sstevel@tonic-gate 			case CLD_CONTINUED:
872*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC,
873*7c478bd9Sstevel@tonic-gate 				    "waitid: wrong state %d on the p_newstate"
874*7c478bd9Sstevel@tonic-gate 				    " list", (*nsp)->p_wcode);
875*7c478bd9Sstevel@tonic-gate 				break;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 			case CLD_EXITED:
878*7c478bd9Sstevel@tonic-gate 			case CLD_DUMPED:
879*7c478bd9Sstevel@tonic-gate 			case CLD_KILLED:
880*7c478bd9Sstevel@tonic-gate 				if (!(options & WEXITED)) {
881*7c478bd9Sstevel@tonic-gate 					/*
882*7c478bd9Sstevel@tonic-gate 					 * Count how many are already gone
883*7c478bd9Sstevel@tonic-gate 					 * for good.
884*7c478bd9Sstevel@tonic-gate 					 */
885*7c478bd9Sstevel@tonic-gate 					proc_gone++;
886*7c478bd9Sstevel@tonic-gate 					break;
887*7c478bd9Sstevel@tonic-gate 				}
888*7c478bd9Sstevel@tonic-gate 				if (!waitflag) {
889*7c478bd9Sstevel@tonic-gate 					winfo((*nsp), ip, 0);
890*7c478bd9Sstevel@tonic-gate 				} else {
891*7c478bd9Sstevel@tonic-gate 					proc_t *xp = *nsp;
892*7c478bd9Sstevel@tonic-gate 					winfo(xp, ip, 1);
893*7c478bd9Sstevel@tonic-gate 					freeproc(xp);
894*7c478bd9Sstevel@tonic-gate 				}
895*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
896*7c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
897*7c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
898*7c478bd9Sstevel@tonic-gate 					sigcld_repost();
899*7c478bd9Sstevel@tonic-gate 				}
900*7c478bd9Sstevel@tonic-gate 				return (0);
901*7c478bd9Sstevel@tonic-gate 			}
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 			if (idtype == P_PID)
904*7c478bd9Sstevel@tonic-gate 				break;
905*7c478bd9Sstevel@tonic-gate 		}
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate 		/*
908*7c478bd9Sstevel@tonic-gate 		 * Wow! None of the threads on the p_sibling_ns list were
909*7c478bd9Sstevel@tonic-gate 		 * interesting threads. Check all the kids!
910*7c478bd9Sstevel@tonic-gate 		 */
911*7c478bd9Sstevel@tonic-gate 		found = 0;
912*7c478bd9Sstevel@tonic-gate 		cp = pp->p_child;
913*7c478bd9Sstevel@tonic-gate 		do {
914*7c478bd9Sstevel@tonic-gate 			if (idtype == P_PID && id != cp->p_pid) {
915*7c478bd9Sstevel@tonic-gate 				continue;
916*7c478bd9Sstevel@tonic-gate 			}
917*7c478bd9Sstevel@tonic-gate 			if (idtype == P_PGID && id != cp->p_pgrp) {
918*7c478bd9Sstevel@tonic-gate 				continue;
919*7c478bd9Sstevel@tonic-gate 			}
920*7c478bd9Sstevel@tonic-gate 
921*7c478bd9Sstevel@tonic-gate 			found++;
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 			switch (cp->p_wcode) {
924*7c478bd9Sstevel@tonic-gate 			case CLD_TRAPPED:
925*7c478bd9Sstevel@tonic-gate 				if (!(options & WTRAPPED))
926*7c478bd9Sstevel@tonic-gate 					break;
927*7c478bd9Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
928*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
929*7c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
930*7c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
931*7c478bd9Sstevel@tonic-gate 					sigcld_repost();
932*7c478bd9Sstevel@tonic-gate 				}
933*7c478bd9Sstevel@tonic-gate 				return (0);
934*7c478bd9Sstevel@tonic-gate 
935*7c478bd9Sstevel@tonic-gate 			case CLD_STOPPED:
936*7c478bd9Sstevel@tonic-gate 				if (!(options & WSTOPPED))
937*7c478bd9Sstevel@tonic-gate 					break;
938*7c478bd9Sstevel@tonic-gate 				/* Is it still stopped? */
939*7c478bd9Sstevel@tonic-gate 				mutex_enter(&cp->p_lock);
940*7c478bd9Sstevel@tonic-gate 				if (!jobstopped(cp)) {
941*7c478bd9Sstevel@tonic-gate 					mutex_exit(&cp->p_lock);
942*7c478bd9Sstevel@tonic-gate 					break;
943*7c478bd9Sstevel@tonic-gate 				}
944*7c478bd9Sstevel@tonic-gate 				mutex_exit(&cp->p_lock);
945*7c478bd9Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
946*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
947*7c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
948*7c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
949*7c478bd9Sstevel@tonic-gate 					sigcld_repost();
950*7c478bd9Sstevel@tonic-gate 				}
951*7c478bd9Sstevel@tonic-gate 				return (0);
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 			case CLD_CONTINUED:
954*7c478bd9Sstevel@tonic-gate 				if (!(options & WCONTINUED))
955*7c478bd9Sstevel@tonic-gate 					break;
956*7c478bd9Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
957*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
958*7c478bd9Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
959*7c478bd9Sstevel@tonic-gate 					sigcld_delete(ip);
960*7c478bd9Sstevel@tonic-gate 					sigcld_repost();
961*7c478bd9Sstevel@tonic-gate 				}
962*7c478bd9Sstevel@tonic-gate 				return (0);
963*7c478bd9Sstevel@tonic-gate 
964*7c478bd9Sstevel@tonic-gate 			case CLD_EXITED:
965*7c478bd9Sstevel@tonic-gate 			case CLD_DUMPED:
966*7c478bd9Sstevel@tonic-gate 			case CLD_KILLED:
967*7c478bd9Sstevel@tonic-gate 				/*
968*7c478bd9Sstevel@tonic-gate 				 * Don't complain if a process was found in
969*7c478bd9Sstevel@tonic-gate 				 * the first loop but we broke out of the loop
970*7c478bd9Sstevel@tonic-gate 				 * because of the arguments passed to us.
971*7c478bd9Sstevel@tonic-gate 				 */
972*7c478bd9Sstevel@tonic-gate 				if (proc_gone == 0) {
973*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC,
974*7c478bd9Sstevel@tonic-gate 					    "waitid: wrong state on the"
975*7c478bd9Sstevel@tonic-gate 					    " p_child list");
976*7c478bd9Sstevel@tonic-gate 				} else {
977*7c478bd9Sstevel@tonic-gate 					break;
978*7c478bd9Sstevel@tonic-gate 				}
979*7c478bd9Sstevel@tonic-gate 			}
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 			if (idtype == P_PID)
982*7c478bd9Sstevel@tonic-gate 				break;
983*7c478bd9Sstevel@tonic-gate 		} while ((cp = cp->p_sibling) != NULL);
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate 		/*
986*7c478bd9Sstevel@tonic-gate 		 * If we found no interesting processes at all,
987*7c478bd9Sstevel@tonic-gate 		 * break out and return ECHILD.
988*7c478bd9Sstevel@tonic-gate 		 */
989*7c478bd9Sstevel@tonic-gate 		if (found + proc_gone == 0)
990*7c478bd9Sstevel@tonic-gate 			break;
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate 		if (options & WNOHANG) {
993*7c478bd9Sstevel@tonic-gate 			bzero(ip, sizeof (k_siginfo_t));
994*7c478bd9Sstevel@tonic-gate 			/*
995*7c478bd9Sstevel@tonic-gate 			 * We should set ip->si_signo = SIGCLD,
996*7c478bd9Sstevel@tonic-gate 			 * but there is an SVVS test that expects
997*7c478bd9Sstevel@tonic-gate 			 * ip->si_signo to be zero in this case.
998*7c478bd9Sstevel@tonic-gate 			 */
999*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1000*7c478bd9Sstevel@tonic-gate 			return (0);
1001*7c478bd9Sstevel@tonic-gate 		}
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate 		/*
1004*7c478bd9Sstevel@tonic-gate 		 * If we found no processes of interest that could
1005*7c478bd9Sstevel@tonic-gate 		 * change state while we wait, we don't wait at all.
1006*7c478bd9Sstevel@tonic-gate 		 * Get out with ECHILD according to SVID.
1007*7c478bd9Sstevel@tonic-gate 		 */
1008*7c478bd9Sstevel@tonic-gate 		if (found == proc_gone)
1009*7c478bd9Sstevel@tonic-gate 			break;
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig_swap(&pp->p_cv, &pidlock)) {
1012*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1013*7c478bd9Sstevel@tonic-gate 			return (EINTR);
1014*7c478bd9Sstevel@tonic-gate 		}
1015*7c478bd9Sstevel@tonic-gate 	}
1016*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1017*7c478bd9Sstevel@tonic-gate 	return (ECHILD);
1018*7c478bd9Sstevel@tonic-gate }
1019*7c478bd9Sstevel@tonic-gate 
1020*7c478bd9Sstevel@tonic-gate /*
1021*7c478bd9Sstevel@tonic-gate  * For implementations that don't require binary compatibility,
1022*7c478bd9Sstevel@tonic-gate  * the wait system call may be made into a library call to the
1023*7c478bd9Sstevel@tonic-gate  * waitid system call.
1024*7c478bd9Sstevel@tonic-gate  */
1025*7c478bd9Sstevel@tonic-gate int64_t
1026*7c478bd9Sstevel@tonic-gate wait(void)
1027*7c478bd9Sstevel@tonic-gate {
1028*7c478bd9Sstevel@tonic-gate 	int error;
1029*7c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
1030*7c478bd9Sstevel@tonic-gate 	rval_t	r;
1031*7c478bd9Sstevel@tonic-gate 
1032*7c478bd9Sstevel@tonic-gate 	if (error =  waitid(P_ALL, (id_t)0, &info, WEXITED|WTRAPPED))
1033*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1034*7c478bd9Sstevel@tonic-gate 	r.r_val1 = info.si_pid;
1035*7c478bd9Sstevel@tonic-gate 	r.r_val2 = wstat(info.si_code, info.si_status);
1036*7c478bd9Sstevel@tonic-gate 	return (r.r_vals);
1037*7c478bd9Sstevel@tonic-gate }
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate int
1040*7c478bd9Sstevel@tonic-gate waitsys(idtype_t idtype, id_t id, siginfo_t *infop, int options)
1041*7c478bd9Sstevel@tonic-gate {
1042*7c478bd9Sstevel@tonic-gate 	int error;
1043*7c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
1044*7c478bd9Sstevel@tonic-gate 
1045*7c478bd9Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
1046*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1047*7c478bd9Sstevel@tonic-gate 	if (copyout(&info, infop, sizeof (k_siginfo_t)))
1048*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
1049*7c478bd9Sstevel@tonic-gate 	return (0);
1050*7c478bd9Sstevel@tonic-gate }
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate int
1055*7c478bd9Sstevel@tonic-gate waitsys32(idtype_t idtype, id_t id, siginfo_t *infop, int options)
1056*7c478bd9Sstevel@tonic-gate {
1057*7c478bd9Sstevel@tonic-gate 	int error;
1058*7c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
1059*7c478bd9Sstevel@tonic-gate 	siginfo32_t info32;
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
1062*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1063*7c478bd9Sstevel@tonic-gate 	siginfo_kto32(&info, &info32);
1064*7c478bd9Sstevel@tonic-gate 	if (copyout(&info32, infop, sizeof (info32)))
1065*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
1066*7c478bd9Sstevel@tonic-gate 	return (0);
1067*7c478bd9Sstevel@tonic-gate }
1068*7c478bd9Sstevel@tonic-gate 
1069*7c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate void
1072*7c478bd9Sstevel@tonic-gate proc_detach(proc_t *p)
1073*7c478bd9Sstevel@tonic-gate {
1074*7c478bd9Sstevel@tonic-gate 	proc_t *q;
1075*7c478bd9Sstevel@tonic-gate 
1076*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1077*7c478bd9Sstevel@tonic-gate 
1078*7c478bd9Sstevel@tonic-gate 	q = p->p_parent;
1079*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
1080*7c478bd9Sstevel@tonic-gate 
1081*7c478bd9Sstevel@tonic-gate 	/*
1082*7c478bd9Sstevel@tonic-gate 	 * Take it off the newstate list of its parent
1083*7c478bd9Sstevel@tonic-gate 	 */
1084*7c478bd9Sstevel@tonic-gate 	delete_ns(q, p);
1085*7c478bd9Sstevel@tonic-gate 
1086*7c478bd9Sstevel@tonic-gate 	if (q->p_child == p) {
1087*7c478bd9Sstevel@tonic-gate 		q->p_child = p->p_sibling;
1088*7c478bd9Sstevel@tonic-gate 		/*
1089*7c478bd9Sstevel@tonic-gate 		 * If the parent has no children, it better not
1090*7c478bd9Sstevel@tonic-gate 		 * have any with new states either!
1091*7c478bd9Sstevel@tonic-gate 		 */
1092*7c478bd9Sstevel@tonic-gate 		ASSERT(q->p_child ? 1 : q->p_child_ns == NULL);
1093*7c478bd9Sstevel@tonic-gate 	}
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate 	if (p->p_sibling) {
1096*7c478bd9Sstevel@tonic-gate 		p->p_sibling->p_psibling = p->p_psibling;
1097*7c478bd9Sstevel@tonic-gate 	}
1098*7c478bd9Sstevel@tonic-gate 
1099*7c478bd9Sstevel@tonic-gate 	if (p->p_psibling) {
1100*7c478bd9Sstevel@tonic-gate 		p->p_psibling->p_sibling = p->p_sibling;
1101*7c478bd9Sstevel@tonic-gate 	}
1102*7c478bd9Sstevel@tonic-gate }
1103*7c478bd9Sstevel@tonic-gate 
1104*7c478bd9Sstevel@tonic-gate /*
1105*7c478bd9Sstevel@tonic-gate  * Remove zombie children from the process table.
1106*7c478bd9Sstevel@tonic-gate  */
1107*7c478bd9Sstevel@tonic-gate void
1108*7c478bd9Sstevel@tonic-gate freeproc(proc_t *p)
1109*7c478bd9Sstevel@tonic-gate {
1110*7c478bd9Sstevel@tonic-gate 	proc_t *q;
1111*7c478bd9Sstevel@tonic-gate 
1112*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_stat == SZOMB);
1113*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_tlist == NULL);
1114*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1115*7c478bd9Sstevel@tonic-gate 
1116*7c478bd9Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
1117*7c478bd9Sstevel@tonic-gate 	if (p->p_killsqp) {
1118*7c478bd9Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
1119*7c478bd9Sstevel@tonic-gate 		p->p_killsqp = NULL;
1120*7c478bd9Sstevel@tonic-gate 	}
1121*7c478bd9Sstevel@tonic-gate 
1122*7c478bd9Sstevel@tonic-gate 	prfree(p);	/* inform /proc */
1123*7c478bd9Sstevel@tonic-gate 
1124*7c478bd9Sstevel@tonic-gate 	/*
1125*7c478bd9Sstevel@tonic-gate 	 * Don't free the init processes.
1126*7c478bd9Sstevel@tonic-gate 	 * Other dying processes will access it.
1127*7c478bd9Sstevel@tonic-gate 	 */
1128*7c478bd9Sstevel@tonic-gate 	if (p == proc_init)
1129*7c478bd9Sstevel@tonic-gate 		return;
1130*7c478bd9Sstevel@tonic-gate 
1131*7c478bd9Sstevel@tonic-gate 
1132*7c478bd9Sstevel@tonic-gate 	/*
1133*7c478bd9Sstevel@tonic-gate 	 * We wait until now to free the cred structure because a
1134*7c478bd9Sstevel@tonic-gate 	 * zombie process's credentials may be examined by /proc.
1135*7c478bd9Sstevel@tonic-gate 	 * No cred locking needed because there are no threads at this point.
1136*7c478bd9Sstevel@tonic-gate 	 */
1137*7c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(p->p_cred), crgetzoneid(p->p_cred));
1138*7c478bd9Sstevel@tonic-gate 	crfree(p->p_cred);
1139*7c478bd9Sstevel@tonic-gate 	if (p->p_corefile != NULL) {
1140*7c478bd9Sstevel@tonic-gate 		corectl_path_rele(p->p_corefile);
1141*7c478bd9Sstevel@tonic-gate 		p->p_corefile = NULL;
1142*7c478bd9Sstevel@tonic-gate 	}
1143*7c478bd9Sstevel@tonic-gate 	if (p->p_content != NULL) {
1144*7c478bd9Sstevel@tonic-gate 		corectl_content_rele(p->p_content);
1145*7c478bd9Sstevel@tonic-gate 		p->p_content = NULL;
1146*7c478bd9Sstevel@tonic-gate 	}
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 	if (p->p_nextofkin && !((p->p_nextofkin->p_flag & SNOWAIT) ||
1149*7c478bd9Sstevel@tonic-gate 	    (PTOU(p->p_nextofkin)->u_signal[SIGCLD - 1] == SIG_IGN))) {
1150*7c478bd9Sstevel@tonic-gate 		/*
1151*7c478bd9Sstevel@tonic-gate 		 * This should still do the right thing since p_utime/stime
1152*7c478bd9Sstevel@tonic-gate 		 * get set to the correct value on process exit, so it
1153*7c478bd9Sstevel@tonic-gate 		 * should get properly updated
1154*7c478bd9Sstevel@tonic-gate 		 */
1155*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cutime += p->p_utime;
1156*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cstime += p->p_stime;
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER] += p->p_acct[LMS_USER];
1159*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SYSTEM] += p->p_acct[LMS_SYSTEM];
1160*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TRAP] += p->p_acct[LMS_TRAP];
1161*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TFAULT] += p->p_acct[LMS_TFAULT];
1162*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_DFAULT] += p->p_acct[LMS_DFAULT];
1163*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_KFAULT] += p->p_acct[LMS_KFAULT];
1164*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER_LOCK]
1165*7c478bd9Sstevel@tonic-gate 		    += p->p_acct[LMS_USER_LOCK];
1166*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SLEEP] += p->p_acct[LMS_SLEEP];
1167*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_WAIT_CPU]
1168*7c478bd9Sstevel@tonic-gate 		    += p->p_acct[LMS_WAIT_CPU];
1169*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_STOPPED] += p->p_acct[LMS_STOPPED];
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.minflt	+= p->p_ru.minflt;
1172*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.majflt	+= p->p_ru.majflt;
1173*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nswap	+= p->p_ru.nswap;
1174*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.inblock	+= p->p_ru.inblock;
1175*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.oublock	+= p->p_ru.oublock;
1176*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgsnd	+= p->p_ru.msgsnd;
1177*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgrcv	+= p->p_ru.msgrcv;
1178*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nsignals	+= p->p_ru.nsignals;
1179*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nvcsw	+= p->p_ru.nvcsw;
1180*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nivcsw	+= p->p_ru.nivcsw;
1181*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.sysc	+= p->p_ru.sysc;
1182*7c478bd9Sstevel@tonic-gate 		p->p_nextofkin->p_cru.ioch	+= p->p_ru.ioch;
1183*7c478bd9Sstevel@tonic-gate 
1184*7c478bd9Sstevel@tonic-gate 	}
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate 	q = p->p_nextofkin;
1187*7c478bd9Sstevel@tonic-gate 	if (q && q->p_orphan == p)
1188*7c478bd9Sstevel@tonic-gate 		q->p_orphan = p->p_nextorph;
1189*7c478bd9Sstevel@tonic-gate 	else if (q) {
1190*7c478bd9Sstevel@tonic-gate 		for (q = q->p_orphan; q; q = q->p_nextorph)
1191*7c478bd9Sstevel@tonic-gate 			if (q->p_nextorph == p)
1192*7c478bd9Sstevel@tonic-gate 				break;
1193*7c478bd9Sstevel@tonic-gate 		ASSERT(q && q->p_nextorph == p);
1194*7c478bd9Sstevel@tonic-gate 		q->p_nextorph = p->p_nextorph;
1195*7c478bd9Sstevel@tonic-gate 	}
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 	proc_detach(p);
1198*7c478bd9Sstevel@tonic-gate 	pid_exit(p);	/* frees pid and proc structure */
1199*7c478bd9Sstevel@tonic-gate }
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate /*
1202*7c478bd9Sstevel@tonic-gate  * Delete process "child" from the newstate list of process "parent"
1203*7c478bd9Sstevel@tonic-gate  */
1204*7c478bd9Sstevel@tonic-gate void
1205*7c478bd9Sstevel@tonic-gate delete_ns(proc_t *parent, proc_t *child)
1206*7c478bd9Sstevel@tonic-gate {
1207*7c478bd9Sstevel@tonic-gate 	proc_t **ns;
1208*7c478bd9Sstevel@tonic-gate 
1209*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1210*7c478bd9Sstevel@tonic-gate 	ASSERT(child->p_parent == parent);
1211*7c478bd9Sstevel@tonic-gate 	for (ns = &parent->p_child_ns; *ns != NULL; ns = &(*ns)->p_sibling_ns) {
1212*7c478bd9Sstevel@tonic-gate 		if (*ns == child) {
1213*7c478bd9Sstevel@tonic-gate 
1214*7c478bd9Sstevel@tonic-gate 			ASSERT((*ns)->p_parent == parent);
1215*7c478bd9Sstevel@tonic-gate 
1216*7c478bd9Sstevel@tonic-gate 			*ns = child->p_sibling_ns;
1217*7c478bd9Sstevel@tonic-gate 			child->p_sibling_ns = NULL;
1218*7c478bd9Sstevel@tonic-gate 			return;
1219*7c478bd9Sstevel@tonic-gate 		}
1220*7c478bd9Sstevel@tonic-gate 	}
1221*7c478bd9Sstevel@tonic-gate }
1222*7c478bd9Sstevel@tonic-gate 
1223*7c478bd9Sstevel@tonic-gate /*
1224*7c478bd9Sstevel@tonic-gate  * Add process "child" to the new state list of process "parent"
1225*7c478bd9Sstevel@tonic-gate  */
1226*7c478bd9Sstevel@tonic-gate void
1227*7c478bd9Sstevel@tonic-gate add_ns(proc_t *parent, proc_t *child)
1228*7c478bd9Sstevel@tonic-gate {
1229*7c478bd9Sstevel@tonic-gate 	ASSERT(child->p_sibling_ns == NULL);
1230*7c478bd9Sstevel@tonic-gate 	child->p_sibling_ns = parent->p_child_ns;
1231*7c478bd9Sstevel@tonic-gate 	parent->p_child_ns = child;
1232*7c478bd9Sstevel@tonic-gate }
1233