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