xref: /onnv-gate/usr/src/uts/common/os/exit.c (revision 13045:76bc2e54d54b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52267Sdp  * Common Development and Distribution License (the "License").
62267Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
2312725SMenno.Lageman@Sun.COM  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/cred.h>
330Sstevel@tonic-gate #include <sys/user.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/ucontext.h>
370Sstevel@tonic-gate #include <sys/procfs.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/acct.h>
400Sstevel@tonic-gate #include <sys/var.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/wait.h>
440Sstevel@tonic-gate #include <sys/siginfo.h>
450Sstevel@tonic-gate #include <sys/procset.h>
460Sstevel@tonic-gate #include <sys/class.h>
470Sstevel@tonic-gate #include <sys/file.h>
480Sstevel@tonic-gate #include <sys/session.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/vtrace.h>
510Sstevel@tonic-gate #include <sys/prsystm.h>
520Sstevel@tonic-gate #include <sys/ipc.h>
530Sstevel@tonic-gate #include <sys/sem_impl.h>
540Sstevel@tonic-gate #include <c2/audit.h>
550Sstevel@tonic-gate #include <sys/aio_impl.h>
560Sstevel@tonic-gate #include <vm/as.h>
570Sstevel@tonic-gate #include <sys/poll.h>
580Sstevel@tonic-gate #include <sys/door.h>
590Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
600Sstevel@tonic-gate #include <sys/utrap.h>
610Sstevel@tonic-gate #include <sys/task.h>
620Sstevel@tonic-gate #include <sys/exacct.h>
630Sstevel@tonic-gate #include <sys/cyclic.h>
640Sstevel@tonic-gate #include <sys/schedctl.h>
650Sstevel@tonic-gate #include <sys/rctl.h>
660Sstevel@tonic-gate #include <sys/contract_impl.h>
670Sstevel@tonic-gate #include <sys/contract/process_impl.h>
680Sstevel@tonic-gate #include <sys/list.h>
690Sstevel@tonic-gate #include <sys/dtrace.h>
700Sstevel@tonic-gate #include <sys/pool.h>
710Sstevel@tonic-gate #include <sys/sdt.h>
720Sstevel@tonic-gate #include <sys/corectl.h>
732712Snn35248 #include <sys/brand.h>
743086Sraf #include <sys/libc_kernel.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * convert code/data pair into old style wait status
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int
wstat(int code,int data)800Sstevel@tonic-gate wstat(int code, int data)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	int stat = (data & 0377);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	switch (code) {
850Sstevel@tonic-gate 	case CLD_EXITED:
860Sstevel@tonic-gate 		stat <<= 8;
870Sstevel@tonic-gate 		break;
880Sstevel@tonic-gate 	case CLD_DUMPED:
890Sstevel@tonic-gate 		stat |= WCOREFLG;
900Sstevel@tonic-gate 		break;
910Sstevel@tonic-gate 	case CLD_KILLED:
920Sstevel@tonic-gate 		break;
930Sstevel@tonic-gate 	case CLD_TRAPPED:
940Sstevel@tonic-gate 	case CLD_STOPPED:
950Sstevel@tonic-gate 		stat <<= 8;
960Sstevel@tonic-gate 		stat |= WSTOPFLG;
970Sstevel@tonic-gate 		break;
980Sstevel@tonic-gate 	case CLD_CONTINUED:
990Sstevel@tonic-gate 		stat = WCONTFLG;
1000Sstevel@tonic-gate 		break;
1010Sstevel@tonic-gate 	default:
1020Sstevel@tonic-gate 		cmn_err(CE_PANIC, "wstat: bad code");
1030Sstevel@tonic-gate 		/* NOTREACHED */
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	return (stat);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static char *
exit_reason(char * buf,size_t bufsz,int what,int why)1090Sstevel@tonic-gate exit_reason(char *buf, size_t bufsz, int what, int why)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	switch (why) {
1120Sstevel@tonic-gate 	case CLD_EXITED:
1130Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited with status %d", what);
1140Sstevel@tonic-gate 		break;
1150Sstevel@tonic-gate 	case CLD_KILLED:
1160Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "exited on fatal signal %d", what);
1170Sstevel@tonic-gate 		break;
1180Sstevel@tonic-gate 	case CLD_DUMPED:
1190Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "core dumped on signal %d", what);
1200Sstevel@tonic-gate 		break;
1210Sstevel@tonic-gate 	default:
1220Sstevel@tonic-gate 		(void) snprintf(buf, bufsz, "encountered unknown error "
1230Sstevel@tonic-gate 		    "(%d, %d)", why, what);
1240Sstevel@tonic-gate 		break;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (buf);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * exit system call: pass back caller's arg.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate void
rexit(int rval)1340Sstevel@tonic-gate rexit(int rval)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	exit(CLD_EXITED, rval);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * Called by proc_exit() when a zone's init exits, presumably because
1410Sstevel@tonic-gate  * it failed.  As long as the given zone is still in the "running"
1420Sstevel@tonic-gate  * state, we will re-exec() init, but first we need to reset things
1430Sstevel@tonic-gate  * which are usually inherited across exec() but will break init's
1440Sstevel@tonic-gate  * assumption that it is being exec()'d from a virgin process.  Most
1450Sstevel@tonic-gate  * importantly this includes closing all file descriptors (exec only
1460Sstevel@tonic-gate  * closes those marked close-on-exec) and resetting signals (exec only
1470Sstevel@tonic-gate  * resets handled signals, and we need to clear any signals which
1480Sstevel@tonic-gate  * killed init).  Anything else that exec(2) says would be inherited,
1490Sstevel@tonic-gate  * but would affect the execution of init, needs to be reset.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate static int
restart_init(int what,int why)1520Sstevel@tonic-gate restart_init(int what, int why)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	kthread_t *t = curthread;
1550Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1560Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1570Sstevel@tonic-gate 	user_t *up = PTOU(p);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	vnode_t *oldcd, *oldrd;
1600Sstevel@tonic-gate 	int i, err;
1610Sstevel@tonic-gate 	char reason_buf[64];
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/*
1640Sstevel@tonic-gate 	 * Let zone admin (and global zone admin if this is for a non-global
1650Sstevel@tonic-gate 	 * zone) know that init has failed and will be restarted.
1660Sstevel@tonic-gate 	 */
1670Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
1680Sstevel@tonic-gate 	    "init(1M) %s: restarting automatically",
1690Sstevel@tonic-gate 	    exit_reason(reason_buf, sizeof (reason_buf), what, why));
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
1720Sstevel@tonic-gate 		cmn_err(CE_WARN, "init(1M) for zone %s (pid %d) %s: "
1730Sstevel@tonic-gate 		    "restarting automatically",
1740Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, reason_buf);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
1790Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
1800Sstevel@tonic-gate 	 * Then close all open file descriptors in the process.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	pollcleanup();
1830Sstevel@tonic-gate 	closeall(P_FINFO(p));
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Grab p_lock and begin clearing miscellaneous global process
1870Sstevel@tonic-gate 	 * state that needs to be reset before we exec the new init(1M).
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
191390Sraf 	prbarrier(p);
1920Sstevel@tonic-gate 
193390Sraf 	p->p_flag &= ~(SKILLED | SEXTKILLED | SEXITING | SDOCORE);
1940Sstevel@tonic-gate 	up->u_cmask = CMASK;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	sigemptyset(&t->t_hold);
1970Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
1980Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
2010Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	sigdelq(p, t, 0);
2040Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (p->p_killsqp) {
2070Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
2080Sstevel@tonic-gate 		p->p_killsqp = NULL;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 	 * Reset any signals that are ignored back to the default disposition.
2130Sstevel@tonic-gate 	 * Other u_signal members will be cleared when exec calls sigdefault().
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++) {
2160Sstevel@tonic-gate 		if (up->u_signal[i - 1] == SIG_IGN) {
2170Sstevel@tonic-gate 			up->u_signal[i - 1] = SIG_DFL;
2180Sstevel@tonic-gate 			sigemptyset(&up->u_sigmask[i - 1]);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Clear the current signal, any signal info associated with it, and
2240Sstevel@tonic-gate 	 * any signal information from contracts and/or contract templates.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
2270Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
2280Sstevel@tonic-gate 	if (lwp->lwp_curinfo != NULL) {
2290Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
2300Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Reset both the process root directory and the current working
2360Sstevel@tonic-gate 	 * directory to the root of the zone just as we do during boot.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
2390Sstevel@tonic-gate 	oldrd = up->u_rdir;
2400Sstevel@tonic-gate 	up->u_rdir = p->p_zone->zone_rootvp;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	VN_HOLD(p->p_zone->zone_rootvp);
2430Sstevel@tonic-gate 	oldcd = up->u_cdir;
2440Sstevel@tonic-gate 	up->u_cdir = p->p_zone->zone_rootvp;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (up->u_cwd != NULL) {
2470Sstevel@tonic-gate 		refstr_rele(up->u_cwd);
2480Sstevel@tonic-gate 		up->u_cwd = NULL;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (oldrd != NULL)
2540Sstevel@tonic-gate 		VN_RELE(oldrd);
2550Sstevel@tonic-gate 	if (oldcd != NULL)
2560Sstevel@tonic-gate 		VN_RELE(oldcd);
2570Sstevel@tonic-gate 
2582712Snn35248 	/* Free the controlling tty.  (freectty() always assumes curproc.) */
2592712Snn35248 	ASSERT(p == curproc);
2602712Snn35248 	(void) freectty(B_TRUE);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Now exec() the new init(1M) on top of the current process.  If we
2640Sstevel@tonic-gate 	 * succeed, the caller will treat this like a successful system call.
2650Sstevel@tonic-gate 	 * If we fail, we issue messages and the caller will proceed with exit.
2660Sstevel@tonic-gate 	 */
2672267Sdp 	err = exec_init(p->p_zone->zone_initname, NULL);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (err == 0)
2700Sstevel@tonic-gate 		return (0);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	zcmn_err(p->p_zone->zone_id, CE_WARN,
2730Sstevel@tonic-gate 	    "failed to restart init(1M) (err=%d): system reboot required", err);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (!INGLOBALZONE(p)) {
2760Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to restart init(1M) for zone %s "
2770Sstevel@tonic-gate 		    "(pid %d, err=%d): zoneadm(1M) boot required",
2780Sstevel@tonic-gate 		    p->p_zone->zone_name, p->p_pid, err);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	return (-1);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * Release resources.
2860Sstevel@tonic-gate  * Enter zombie state.
2870Sstevel@tonic-gate  * Wake up parent and init processes,
2880Sstevel@tonic-gate  * and dispose of children.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate void
exit(int why,int what)2910Sstevel@tonic-gate exit(int why, int what)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * If proc_exit() fails, then some other lwp in the process
2950Sstevel@tonic-gate 	 * got there first.  We just have to call lwp_exit() to allow
2960Sstevel@tonic-gate 	 * the other lwp to finish exiting the process.  Otherwise we're
2970Sstevel@tonic-gate 	 * restarting init, and should return.
2980Sstevel@tonic-gate 	 */
2990Sstevel@tonic-gate 	if (proc_exit(why, what) != 0) {
3000Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
3010Sstevel@tonic-gate 		ASSERT(curproc->p_flag & SEXITLWPS);
3020Sstevel@tonic-gate 		lwp_exit();
3030Sstevel@tonic-gate 		/* NOTREACHED */
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
308390Sraf  * Set the SEXITING flag on the process, after making sure /proc does
309390Sraf  * not have it locked.  This is done in more places than proc_exit(),
310390Sraf  * so it is a separate function.
311390Sraf  */
312390Sraf void
proc_is_exiting(proc_t * p)313390Sraf proc_is_exiting(proc_t *p)
314390Sraf {
315390Sraf 	mutex_enter(&p->p_lock);
316390Sraf 	prbarrier(p);
317390Sraf 	p->p_flag |= SEXITING;
318390Sraf 	mutex_exit(&p->p_lock);
319390Sraf }
320390Sraf 
321390Sraf /*
3220Sstevel@tonic-gate  * Return value:
3230Sstevel@tonic-gate  *   1 - exitlwps() failed, call (or continue) lwp_exit()
3240Sstevel@tonic-gate  *   0 - restarting init.  Return through system call path
3250Sstevel@tonic-gate  */
3260Sstevel@tonic-gate int
proc_exit(int why,int what)3270Sstevel@tonic-gate proc_exit(int why, int what)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	kthread_t *t = curthread;
3300Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3310Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3320Sstevel@tonic-gate 	zone_t *z = p->p_zone;
3330Sstevel@tonic-gate 	timeout_id_t tmp_id;
3340Sstevel@tonic-gate 	int rv;
3350Sstevel@tonic-gate 	proc_t *q;
3360Sstevel@tonic-gate 	task_t *tk;
3370Sstevel@tonic-gate 	vnode_t *exec_vp, *execdir_vp, *cdir, *rdir;
3380Sstevel@tonic-gate 	sigqueue_t *sqp;
3390Sstevel@tonic-gate 	lwpdir_t *lwpdir;
3400Sstevel@tonic-gate 	uint_t lwpdir_sz;
3419393SRoger.Faulkner@Sun.COM 	tidhash_t *tidhash;
3420Sstevel@tonic-gate 	uint_t tidhash_sz;
3439393SRoger.Faulkner@Sun.COM 	ret_tidhash_t *ret_tidhash;
3440Sstevel@tonic-gate 	refstr_t *cwd;
3450Sstevel@tonic-gate 	hrtime_t hrutime, hrstime;
3463086Sraf 	int evaporate;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * Stop and discard the process's lwps except for the current one,
3500Sstevel@tonic-gate 	 * unless some other lwp beat us to it.  If exitlwps() fails then
3510Sstevel@tonic-gate 	 * return and the calling lwp will call (or continue in) lwp_exit().
3520Sstevel@tonic-gate 	 */
353390Sraf 	proc_is_exiting(p);
3540Sstevel@tonic-gate 	if (exitlwps(0) != 0)
3550Sstevel@tonic-gate 		return (1);
3560Sstevel@tonic-gate 
3575788Smv143129 	mutex_enter(&p->p_lock);
3585788Smv143129 	if (p->p_ttime > 0) {
3595788Smv143129 		/*
3605788Smv143129 		 * Account any remaining ticks charged to this process
3615788Smv143129 		 * on its way out.
3625788Smv143129 		 */
3635788Smv143129 		(void) task_cpu_time_incr(p->p_task, p->p_ttime);
3645788Smv143129 		p->p_ttime = 0;
3655788Smv143129 	}
3665788Smv143129 	mutex_exit(&p->p_lock);
3675788Smv143129 
3680Sstevel@tonic-gate 	DTRACE_PROC(lwp__exit);
3690Sstevel@tonic-gate 	DTRACE_PROC1(exit, int, why);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3722712Snn35248 	 * Will perform any brand specific proc exit processing, since this
3732712Snn35248 	 * is always the last lwp, will also perform lwp_exit and free brand
3742712Snn35248 	 * data
3752712Snn35248 	 */
3766994Sedp 	if (PROC_IS_BRANDED(p)) {
3776994Sedp 		lwp_detach_brand_hdlrs(lwp);
378*13045SVamsi.Krishna@Sun.COM 		brand_clearbrand(p, B_FALSE);
3796994Sedp 	}
3802712Snn35248 
3812712Snn35248 	/*
3822267Sdp 	 * Don't let init exit unless zone_start_init() failed its exec, or
3830Sstevel@tonic-gate 	 * we are shutting down the zone or the machine.
3840Sstevel@tonic-gate 	 *
3850Sstevel@tonic-gate 	 * Since we are single threaded, we don't need to lock the
3860Sstevel@tonic-gate 	 * following accesses to zone_proc_initpid.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	if (p->p_pid == z->zone_proc_initpid) {
3890Sstevel@tonic-gate 		if (z->zone_boot_err == 0 &&
3900Sstevel@tonic-gate 		    zone_status_get(z) < ZONE_IS_SHUTTING_DOWN &&
3910Sstevel@tonic-gate 		    zone_status_get(global_zone) < ZONE_IS_SHUTTING_DOWN &&
3922712Snn35248 		    z->zone_restart_init == B_TRUE &&
3930Sstevel@tonic-gate 		    restart_init(what, why) == 0)
3940Sstevel@tonic-gate 			return (0);
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * Since we didn't or couldn't restart init, we clear
3970Sstevel@tonic-gate 		 * the zone's init state and proceed with exit
3980Sstevel@tonic-gate 		 * processing.
3990Sstevel@tonic-gate 		 */
4000Sstevel@tonic-gate 		z->zone_proc_initpid = -1;
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
40311595SJakub.Jermar@Sun.COM 	lwp_pcb_exit();
40411595SJakub.Jermar@Sun.COM 
4050Sstevel@tonic-gate 	/*
4060Sstevel@tonic-gate 	 * Allocate a sigqueue now, before we grab locks.
4070Sstevel@tonic-gate 	 * It will be given to sigcld(), below.
4083086Sraf 	 * Special case:  If we will be making the process disappear
40911173SJonathan.Adams@Sun.COM 	 * without a trace because it is either:
41011173SJonathan.Adams@Sun.COM 	 *	* an exiting SSYS process, or
41111173SJonathan.Adams@Sun.COM 	 *	* a posix_spawn() vfork child who requests it,
41211173SJonathan.Adams@Sun.COM 	 * we don't bother to allocate a useless sigqueue.
4130Sstevel@tonic-gate 	 */
41411173SJonathan.Adams@Sun.COM 	evaporate = (p->p_flag & SSYS) || ((p->p_flag & SVFORK) &&
4153086Sraf 	    why == CLD_EXITED && what == _EVAPORATE);
4163086Sraf 	if (!evaporate)
4173086Sraf 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * revoke any doors created by the process.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	if (p->p_door_list)
4230Sstevel@tonic-gate 		door_exit();
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * Release schedctl data structures.
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 	if (p->p_pagep)
4290Sstevel@tonic-gate 		schedctl_proc_cleanup();
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * make sure all pending kaio has completed.
4330Sstevel@tonic-gate 	 */
4340Sstevel@tonic-gate 	if (p->p_aio)
4350Sstevel@tonic-gate 		aio_cleanup_exit();
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * discard the lwpchan cache.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	if (p->p_lcp != NULL)
4410Sstevel@tonic-gate 		lwpchan_destroy_cache(0);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * Clean up any DTrace helper actions or probes for the process.
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
4470Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
4480Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	/* untimeout the realtime timers */
4520Sstevel@tonic-gate 	if (p->p_itimer != NULL)
4530Sstevel@tonic-gate 		timer_exit();
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if ((tmp_id = p->p_alarmid) != 0) {
4560Sstevel@tonic-gate 		p->p_alarmid = 0;
4570Sstevel@tonic-gate 		(void) untimeout(tmp_id);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/*
4610Sstevel@tonic-gate 	 * Remove any fpollinfo_t's for this (last) thread from our file
4620Sstevel@tonic-gate 	 * descriptors so closeall() can ASSERT() that they're all gone.
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	pollcleanup();
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (p->p_rprof_cyclic != CYCLIC_NONE) {
4670Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4680Sstevel@tonic-gate 		cyclic_remove(p->p_rprof_cyclic);
4690Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Clean up any DTrace probes associated with this process.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
4780Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exit_ptr != NULL);
4790Sstevel@tonic-gate 		dtrace_fasttrap_exit_ptr(p);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	while ((tmp_id = p->p_itimerid) != 0) {
4830Sstevel@tonic-gate 		p->p_itimerid = 0;
4840Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4850Sstevel@tonic-gate 		(void) untimeout(tmp_id);
4860Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	lwp_cleanup();
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * We are about to exit; prevent our resource associations from
4930Sstevel@tonic-gate 	 * being changed.
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	pool_barrier_enter();
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	/*
4980Sstevel@tonic-gate 	 * Block the process against /proc now that we have really
4990Sstevel@tonic-gate 	 * acquired p->p_lock (to manipulate p_tlist at least).
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	prbarrier(p);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	sigfillset(&p->p_ignore);
5040Sstevel@tonic-gate 	sigemptyset(&p->p_siginfo);
5050Sstevel@tonic-gate 	sigemptyset(&p->p_sig);
5060Sstevel@tonic-gate 	sigemptyset(&p->p_extsig);
5070Sstevel@tonic-gate 	sigemptyset(&t->t_sig);
5080Sstevel@tonic-gate 	sigemptyset(&t->t_extsig);
5090Sstevel@tonic-gate 	sigemptyset(&p->p_sigmask);
5100Sstevel@tonic-gate 	sigdelq(p, t, 0);
5110Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
5120Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
5130Sstevel@tonic-gate 	p->p_flag &= ~(SKILLED | SEXTKILLED);
5140Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
5150Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
5160Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	t->t_proc_flag |= TP_LWPEXIT;
5200Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
5210Sstevel@tonic-gate 	prlwpexit(t);		/* notify /proc */
5220Sstevel@tonic-gate 	lwp_hash_out(p, t->t_tid);
5230Sstevel@tonic-gate 	prexit(p);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	p->p_lwpcnt = 0;
5260Sstevel@tonic-gate 	p->p_tlist = NULL;
5270Sstevel@tonic-gate 	sigqfree(p);
5280Sstevel@tonic-gate 	term_mstate(t);
5290Sstevel@tonic-gate 	p->p_mterm = gethrtime();
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	exec_vp = p->p_exec;
5320Sstevel@tonic-gate 	execdir_vp = p->p_execdir;
5330Sstevel@tonic-gate 	p->p_exec = NULLVP;
5340Sstevel@tonic-gate 	p->p_execdir = NULLVP;
5350Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	pr_free_watched_pages(p);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	closeall(P_FINFO(p));
5400Sstevel@tonic-gate 
5412712Snn35248 	/* Free the controlling tty.  (freectty() always assumes curproc.) */
5422712Snn35248 	ASSERT(p == curproc);
5432712Snn35248 	(void) freectty(B_TRUE);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate #if defined(__sparc)
5460Sstevel@tonic-gate 	if (p->p_utraps != NULL)
5470Sstevel@tonic-gate 		utrap_free(p);
5480Sstevel@tonic-gate #endif
5490Sstevel@tonic-gate 	if (p->p_semacct)			/* IPC semaphore exit */
5500Sstevel@tonic-gate 		semexit(p);
5510Sstevel@tonic-gate 	rv = wstat(why, what);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	acct(rv & 0xff);
5540Sstevel@tonic-gate 	exacct_commit_proc(p, rv);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/*
5570Sstevel@tonic-gate 	 * Release any resources associated with C2 auditing
5580Sstevel@tonic-gate 	 */
55911861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING()) {
5600Sstevel@tonic-gate 		/*
5610Sstevel@tonic-gate 		 * audit exit system call
5620Sstevel@tonic-gate 		 */
5630Sstevel@tonic-gate 		audit_exit(why, what);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * Free address space.
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	relvm();
5700Sstevel@tonic-gate 
57111736SDonghai.Qiao@Sun.COM 	if (exec_vp) {
57211736SDonghai.Qiao@Sun.COM 		/*
57311736SDonghai.Qiao@Sun.COM 		 * Close this executable which has been opened when the process
57411736SDonghai.Qiao@Sun.COM 		 * was created by getproc().
57511736SDonghai.Qiao@Sun.COM 		 */
57611736SDonghai.Qiao@Sun.COM 		(void) VOP_CLOSE(exec_vp, FREAD, 1, (offset_t)0, CRED(), NULL);
57711736SDonghai.Qiao@Sun.COM 		VN_RELE(exec_vp);
57811736SDonghai.Qiao@Sun.COM 	}
57911736SDonghai.Qiao@Sun.COM 	if (execdir_vp)
58011736SDonghai.Qiao@Sun.COM 		VN_RELE(execdir_vp);
58111736SDonghai.Qiao@Sun.COM 
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * Release held contracts.
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	contract_exit(p);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * Depart our encapsulating process contract.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	if ((p->p_flag & SSYS) == 0) {
5910Sstevel@tonic-gate 		ASSERT(p->p_ct_process);
5920Sstevel@tonic-gate 		contract_process_exit(p->p_ct_process, p, rv);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/*
5960Sstevel@tonic-gate 	 * Remove pool association, and block if requested by pool_do_bind.
5970Sstevel@tonic-gate 	 */
5980Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5990Sstevel@tonic-gate 	ASSERT(p->p_pool->pool_ref > 0);
6000Sstevel@tonic-gate 	atomic_add_32(&p->p_pool->pool_ref, -1);
6010Sstevel@tonic-gate 	p->p_pool = pool_default;
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * Now that our address space has been freed and all other threads
6040Sstevel@tonic-gate 	 * in this process have exited, set the PEXITED pool flag.  This
6050Sstevel@tonic-gate 	 * tells the pools subsystems to ignore this process if it was
6060Sstevel@tonic-gate 	 * requested to rebind this process to a new pool.
6070Sstevel@tonic-gate 	 */
6080Sstevel@tonic-gate 	p->p_poolflag |= PEXITED;
6090Sstevel@tonic-gate 	pool_barrier_exit();
6100Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	mutex_enter(&pidlock);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * Delete this process from the newstate list of its parent. We
6160Sstevel@tonic-gate 	 * will put it in the right place in the sigcld in the end.
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	delete_ns(p->p_parent, p);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Reassign the orphans to the next of kin.
6220Sstevel@tonic-gate 	 * Don't rearrange init's orphanage.
6230Sstevel@tonic-gate 	 */
6240Sstevel@tonic-gate 	if ((q = p->p_orphan) != NULL && p != proc_init) {
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		proc_t *nokp = p->p_nextofkin;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 		for (;;) {
6290Sstevel@tonic-gate 			q->p_nextofkin = nokp;
6300Sstevel@tonic-gate 			if (q->p_nextorph == NULL)
6310Sstevel@tonic-gate 				break;
6320Sstevel@tonic-gate 			q = q->p_nextorph;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		q->p_nextorph = nokp->p_orphan;
6350Sstevel@tonic-gate 		nokp->p_orphan = p->p_orphan;
6360Sstevel@tonic-gate 		p->p_orphan = NULL;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	/*
6400Sstevel@tonic-gate 	 * Reassign the children to init.
6410Sstevel@tonic-gate 	 * Don't try to assign init's children to init.
6420Sstevel@tonic-gate 	 */
6430Sstevel@tonic-gate 	if ((q = p->p_child) != NULL && p != proc_init) {
6440Sstevel@tonic-gate 		struct proc	*np;
6450Sstevel@tonic-gate 		struct proc	*initp = proc_init;
6460Sstevel@tonic-gate 		boolean_t	setzonetop = B_FALSE;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		if (!INGLOBALZONE(curproc))
6490Sstevel@tonic-gate 			setzonetop = B_TRUE;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		pgdetach(p);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		do {
6540Sstevel@tonic-gate 			np = q->p_sibling;
6550Sstevel@tonic-gate 			/*
6560Sstevel@tonic-gate 			 * Delete it from its current parent new state
6570Sstevel@tonic-gate 			 * list and add it to init new state list
6580Sstevel@tonic-gate 			 */
6590Sstevel@tonic-gate 			delete_ns(q->p_parent, q);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 			q->p_ppid = 1;
6623235Sraf 			q->p_pidflag &= ~(CLDNOSIGCHLD | CLDWAITPID);
6630Sstevel@tonic-gate 			if (setzonetop) {
6640Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
6650Sstevel@tonic-gate 				q->p_flag |= SZONETOP;
6660Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
6670Sstevel@tonic-gate 			}
6680Sstevel@tonic-gate 			q->p_parent = initp;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 			/*
6710Sstevel@tonic-gate 			 * Since q will be the first child,
6720Sstevel@tonic-gate 			 * it will not have a previous sibling.
6730Sstevel@tonic-gate 			 */
6740Sstevel@tonic-gate 			q->p_psibling = NULL;
6750Sstevel@tonic-gate 			if (initp->p_child) {
6760Sstevel@tonic-gate 				initp->p_child->p_psibling = q;
6770Sstevel@tonic-gate 			}
6780Sstevel@tonic-gate 			q->p_sibling = initp->p_child;
6790Sstevel@tonic-gate 			initp->p_child = q;
6800Sstevel@tonic-gate 			if (q->p_proc_flag & P_PR_PTRACE) {
6810Sstevel@tonic-gate 				mutex_enter(&q->p_lock);
6820Sstevel@tonic-gate 				sigtoproc(q, NULL, SIGKILL);
6830Sstevel@tonic-gate 				mutex_exit(&q->p_lock);
6840Sstevel@tonic-gate 			}
6850Sstevel@tonic-gate 			/*
6860Sstevel@tonic-gate 			 * sigcld() will add the child to parents
6870Sstevel@tonic-gate 			 * newstate list.
6880Sstevel@tonic-gate 			 */
6890Sstevel@tonic-gate 			if (q->p_stat == SZOMB)
6900Sstevel@tonic-gate 				sigcld(q, NULL);
6910Sstevel@tonic-gate 		} while ((q = np) != NULL);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		p->p_child = NULL;
6940Sstevel@tonic-gate 		ASSERT(p->p_child_ns == NULL);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	TRACE_1(TR_FAC_PROC, TR_PROC_EXIT, "proc_exit: %p", p);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
7000Sstevel@tonic-gate 	CL_EXIT(curthread); /* tell the scheduler that curthread is exiting */
7010Sstevel@tonic-gate 
7024584Srh87107 	/*
7034584Srh87107 	 * Have our task accummulate our resource usage data before they
7044584Srh87107 	 * become contaminated by p_cacct etc., and before we renounce
7054584Srh87107 	 * membership of the task.
7064584Srh87107 	 *
7074584Srh87107 	 * We do this regardless of whether or not task accounting is active.
7084584Srh87107 	 * This is to avoid having nonsense data reported for this task if
7094584Srh87107 	 * task accounting is subsequently enabled. The overhead is minimal;
7104584Srh87107 	 * by this point, this process has accounted for the usage of all its
7114584Srh87107 	 * LWPs. We nonetheless do the work here, and under the protection of
7124584Srh87107 	 * pidlock, so that the movement of the process's usage to the task
7134584Srh87107 	 * happens at the same time as the removal of the process from the
7144584Srh87107 	 * task, from the point of view of exacct_snapshot_task_usage().
7154584Srh87107 	 */
7164584Srh87107 	exacct_update_task_mstate(p);
7174584Srh87107 
7180Sstevel@tonic-gate 	hrutime = mstate_aggr_state(p, LMS_USER);
7190Sstevel@tonic-gate 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
7200Sstevel@tonic-gate 	p->p_utime = (clock_t)NSEC_TO_TICK(hrutime) + p->p_cutime;
7210Sstevel@tonic-gate 	p->p_stime = (clock_t)NSEC_TO_TICK(hrstime) + p->p_cstime;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	p->p_acct[LMS_USER]	+= p->p_cacct[LMS_USER];
7240Sstevel@tonic-gate 	p->p_acct[LMS_SYSTEM]	+= p->p_cacct[LMS_SYSTEM];
7250Sstevel@tonic-gate 	p->p_acct[LMS_TRAP]	+= p->p_cacct[LMS_TRAP];
7260Sstevel@tonic-gate 	p->p_acct[LMS_TFAULT]	+= p->p_cacct[LMS_TFAULT];
7270Sstevel@tonic-gate 	p->p_acct[LMS_DFAULT]	+= p->p_cacct[LMS_DFAULT];
7280Sstevel@tonic-gate 	p->p_acct[LMS_KFAULT]	+= p->p_cacct[LMS_KFAULT];
7290Sstevel@tonic-gate 	p->p_acct[LMS_USER_LOCK] += p->p_cacct[LMS_USER_LOCK];
7300Sstevel@tonic-gate 	p->p_acct[LMS_SLEEP]	+= p->p_cacct[LMS_SLEEP];
7310Sstevel@tonic-gate 	p->p_acct[LMS_WAIT_CPU]	+= p->p_cacct[LMS_WAIT_CPU];
7320Sstevel@tonic-gate 	p->p_acct[LMS_STOPPED]	+= p->p_cacct[LMS_STOPPED];
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	p->p_ru.minflt	+= p->p_cru.minflt;
7350Sstevel@tonic-gate 	p->p_ru.majflt	+= p->p_cru.majflt;
7360Sstevel@tonic-gate 	p->p_ru.nswap	+= p->p_cru.nswap;
7370Sstevel@tonic-gate 	p->p_ru.inblock	+= p->p_cru.inblock;
7380Sstevel@tonic-gate 	p->p_ru.oublock	+= p->p_cru.oublock;
7390Sstevel@tonic-gate 	p->p_ru.msgsnd	+= p->p_cru.msgsnd;
7400Sstevel@tonic-gate 	p->p_ru.msgrcv	+= p->p_cru.msgrcv;
7410Sstevel@tonic-gate 	p->p_ru.nsignals += p->p_cru.nsignals;
7420Sstevel@tonic-gate 	p->p_ru.nvcsw	+= p->p_cru.nvcsw;
7430Sstevel@tonic-gate 	p->p_ru.nivcsw	+= p->p_cru.nivcsw;
7440Sstevel@tonic-gate 	p->p_ru.sysc	+= p->p_cru.sysc;
7450Sstevel@tonic-gate 	p->p_ru.ioch	+= p->p_cru.ioch;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	p->p_stat = SZOMB;
7480Sstevel@tonic-gate 	p->p_proc_flag &= ~P_PR_PTRACE;
7490Sstevel@tonic-gate 	p->p_wdata = what;
7500Sstevel@tonic-gate 	p->p_wcode = (char)why;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	cdir = PTOU(p)->u_cdir;
7530Sstevel@tonic-gate 	rdir = PTOU(p)->u_rdir;
7540Sstevel@tonic-gate 	cwd = PTOU(p)->u_cwd;
7550Sstevel@tonic-gate 
75611173SJonathan.Adams@Sun.COM 	ASSERT(cdir != NULL || p->p_parent == &p0);
75711173SJonathan.Adams@Sun.COM 
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * Release resource controls, as they are no longer enforceable.
7600Sstevel@tonic-gate 	 */
7610Sstevel@tonic-gate 	rctl_set_free(p->p_rctls);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/*
76412725SMenno.Lageman@Sun.COM 	 * Decrement tk_nlwps counter for our task.max-lwps resource control.
76512725SMenno.Lageman@Sun.COM 	 * An extended accounting record, if that facility is active, is
76612725SMenno.Lageman@Sun.COM 	 * scheduled to be written.  We cannot give up task and project
76712725SMenno.Lageman@Sun.COM 	 * membership at this point because that would allow zombies to escape
76812725SMenno.Lageman@Sun.COM 	 * from the max-processes resource controls.  Zombies stay in their
76912725SMenno.Lageman@Sun.COM 	 * current task and project until the process table slot is released
77012725SMenno.Lageman@Sun.COM 	 * in freeproc().
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	tk = p->p_task;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
7750Sstevel@tonic-gate 	tk->tk_nlwps--;
7760Sstevel@tonic-gate 	tk->tk_proj->kpj_nlwps--;
7770Sstevel@tonic-gate 	p->p_zone->zone_nlwps--;
7780Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/*
7810Sstevel@tonic-gate 	 * Clear the lwp directory and the lwpid hash table
7820Sstevel@tonic-gate 	 * now that /proc can't bother us any more.
7830Sstevel@tonic-gate 	 * We free the memory below, after dropping p->p_lock.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	lwpdir = p->p_lwpdir;
7860Sstevel@tonic-gate 	lwpdir_sz = p->p_lwpdir_sz;
7870Sstevel@tonic-gate 	tidhash = p->p_tidhash;
7880Sstevel@tonic-gate 	tidhash_sz = p->p_tidhash_sz;
7899393SRoger.Faulkner@Sun.COM 	ret_tidhash = p->p_ret_tidhash;
7900Sstevel@tonic-gate 	p->p_lwpdir = NULL;
7910Sstevel@tonic-gate 	p->p_lwpfree = NULL;
7920Sstevel@tonic-gate 	p->p_lwpdir_sz = 0;
7930Sstevel@tonic-gate 	p->p_tidhash = NULL;
7940Sstevel@tonic-gate 	p->p_tidhash_sz = 0;
7959393SRoger.Faulkner@Sun.COM 	p->p_ret_tidhash = NULL;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/*
7981217Srab 	 * If the process has context ops installed, call the exit routine
7991217Srab 	 * on behalf of this last remaining thread. Normally exitpctx() is
8001217Srab 	 * called during thread_exit() or lwp_exit(), but because this is the
8011217Srab 	 * last thread in the process, we must call it here. By the time
8021217Srab 	 * thread_exit() is called (below), the association with the relevant
8031217Srab 	 * process has been lost.
8041217Srab 	 *
8051217Srab 	 * We also free the context here.
8061217Srab 	 */
8071217Srab 	if (p->p_pctx) {
8081217Srab 		kpreempt_disable();
8091217Srab 		exitpctx(p);
8101217Srab 		kpreempt_enable();
8111217Srab 
8121217Srab 		freepctx(p, 0);
8131217Srab 	}
8141217Srab 
8151217Srab 	/*
8163813Sdp 	 * curthread's proc pointer is changed to point to the 'sched'
8173813Sdp 	 * process for the corresponding zone, except in the case when
8183813Sdp 	 * the exiting process is in fact a zsched instance, in which
8193813Sdp 	 * case the proc pointer is set to p0.  We do so, so that the
8203813Sdp 	 * process still points at the right zone when we call the VN_RELE()
8213813Sdp 	 * below.
8223813Sdp 	 *
8233813Sdp 	 * This is because curthread's original proc pointer can be freed as
8243813Sdp 	 * soon as the child sends a SIGCLD to its parent.  We use zsched so
8253813Sdp 	 * that for user processes, even in the final moments of death, the
8263813Sdp 	 * process is still associated with its zone.
8270Sstevel@tonic-gate 	 */
8283813Sdp 	if (p != t->t_procp->p_zone->zone_zsched)
8293813Sdp 		t->t_procp = t->t_procp->p_zone->zone_zsched;
8303813Sdp 	else
8313813Sdp 		t->t_procp = &p0;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8343235Sraf 	if (!evaporate) {
8353235Sraf 		p->p_pidflag &= ~CLDPEND;
8363086Sraf 		sigcld(p, sqp);
8373235Sraf 	} else {
8383086Sraf 		/*
8393086Sraf 		 * Do what sigcld() would do if the disposition
8403086Sraf 		 * of the SIGCHLD signal were set to be ignored.
8413086Sraf 		 */
8423086Sraf 		cv_broadcast(&p->p_srwchan_cv);
8433086Sraf 		freeproc(p);
8443086Sraf 	}
8450Sstevel@tonic-gate 	mutex_exit(&pidlock);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * We don't release u_cdir and u_rdir until SZOMB is set.
8490Sstevel@tonic-gate 	 * This protects us against dofusers().
8500Sstevel@tonic-gate 	 */
85111173SJonathan.Adams@Sun.COM 	if (cdir)
85211173SJonathan.Adams@Sun.COM 		VN_RELE(cdir);
8530Sstevel@tonic-gate 	if (rdir)
8540Sstevel@tonic-gate 		VN_RELE(rdir);
8550Sstevel@tonic-gate 	if (cwd)
8560Sstevel@tonic-gate 		refstr_rele(cwd);
8570Sstevel@tonic-gate 
8583813Sdp 	/*
8593813Sdp 	 * task_rele() may ultimately cause the zone to go away (or
8603813Sdp 	 * may cause the last user process in a zone to go away, which
8613813Sdp 	 * signals zsched to go away).  So prior to this call, we must
8623813Sdp 	 * no longer point at zsched.
8633813Sdp 	 */
8643813Sdp 	t->t_procp = &p0;
8653813Sdp 
8663813Sdp 	kmem_free(lwpdir, lwpdir_sz * sizeof (lwpdir_t));
8679393SRoger.Faulkner@Sun.COM 	kmem_free(tidhash, tidhash_sz * sizeof (tidhash_t));
8689393SRoger.Faulkner@Sun.COM 	while (ret_tidhash != NULL) {
8699393SRoger.Faulkner@Sun.COM 		ret_tidhash_t *next = ret_tidhash->rth_next;
8709393SRoger.Faulkner@Sun.COM 		kmem_free(ret_tidhash->rth_tidhash,
8719393SRoger.Faulkner@Sun.COM 		    ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
8729393SRoger.Faulkner@Sun.COM 		kmem_free(ret_tidhash, sizeof (*ret_tidhash));
8739393SRoger.Faulkner@Sun.COM 		ret_tidhash = next;
8749393SRoger.Faulkner@Sun.COM 	}
8753813Sdp 
8760Sstevel@tonic-gate 	thread_exit();
8770Sstevel@tonic-gate 	/* NOTREACHED */
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate  * Format siginfo structure for wait system calls.
8820Sstevel@tonic-gate  */
8830Sstevel@tonic-gate void
winfo(proc_t * pp,k_siginfo_t * ip,int waitflag)8840Sstevel@tonic-gate winfo(proc_t *pp, k_siginfo_t *ip, int waitflag)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	bzero(ip, sizeof (k_siginfo_t));
8890Sstevel@tonic-gate 	ip->si_signo = SIGCLD;
8900Sstevel@tonic-gate 	ip->si_code = pp->p_wcode;
8910Sstevel@tonic-gate 	ip->si_pid = pp->p_pid;
8920Sstevel@tonic-gate 	ip->si_ctid = PRCTID(pp);
8930Sstevel@tonic-gate 	ip->si_zoneid = pp->p_zone->zone_id;
8940Sstevel@tonic-gate 	ip->si_status = pp->p_wdata;
8950Sstevel@tonic-gate 	ip->si_stime = pp->p_stime;
8960Sstevel@tonic-gate 	ip->si_utime = pp->p_utime;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (waitflag) {
8990Sstevel@tonic-gate 		pp->p_wcode = 0;
9000Sstevel@tonic-gate 		pp->p_wdata = 0;
9010Sstevel@tonic-gate 		pp->p_pidflag &= ~CLDPEND;
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate /*
9060Sstevel@tonic-gate  * Wait system call.
9070Sstevel@tonic-gate  * Search for a terminated (zombie) child,
9080Sstevel@tonic-gate  * finally lay it to rest, and collect its status.
9090Sstevel@tonic-gate  * Look also for stopped children,
9100Sstevel@tonic-gate  * and pass back status from them.
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate int
waitid(idtype_t idtype,id_t id,k_siginfo_t * ip,int options)9130Sstevel@tonic-gate waitid(idtype_t idtype, id_t id, k_siginfo_t *ip, int options)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	int found;
9160Sstevel@tonic-gate 	proc_t *cp, *pp;
9170Sstevel@tonic-gate 	int proc_gone;
9180Sstevel@tonic-gate 	int waitflag = !(options & WNOWAIT);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	/*
9210Sstevel@tonic-gate 	 * Obsolete flag, defined here only for binary compatibility
9220Sstevel@tonic-gate 	 * with old statically linked executables.  Delete this when
9230Sstevel@tonic-gate 	 * we no longer care about these old and broken applications.
9240Sstevel@tonic-gate 	 */
9250Sstevel@tonic-gate #define	_WNOCHLD	0400
9260Sstevel@tonic-gate 	options &= ~_WNOCHLD;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (options == 0 || (options & ~WOPTMASK))
9290Sstevel@tonic-gate 		return (EINVAL);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	switch (idtype) {
9320Sstevel@tonic-gate 	case P_PID:
9330Sstevel@tonic-gate 	case P_PGID:
9340Sstevel@tonic-gate 		if (id < 0 || id >= maxpid)
9350Sstevel@tonic-gate 			return (EINVAL);
9360Sstevel@tonic-gate 		/* FALLTHROUGH */
9370Sstevel@tonic-gate 	case P_ALL:
9380Sstevel@tonic-gate 		break;
9390Sstevel@tonic-gate 	default:
9400Sstevel@tonic-gate 		return (EINVAL);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	pp = ttoproc(curthread);
944749Ssusans 
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * lock parent mutex so that sibling chain can be searched.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	mutex_enter(&pidlock);
949749Ssusans 
950749Ssusans 	/*
951749Ssusans 	 * if we are only looking for exited processes and child_ns list
952749Ssusans 	 * is empty no reason to look at all children.
953749Ssusans 	 */
954749Ssusans 	if (idtype == P_ALL &&
9553235Sraf 	    (options & ~WNOWAIT) == (WNOHANG | WEXITED) &&
9562267Sdp 	    pp->p_child_ns == NULL) {
957749Ssusans 		if (pp->p_child) {
958749Ssusans 			mutex_exit(&pidlock);
959749Ssusans 			bzero(ip, sizeof (k_siginfo_t));
960749Ssusans 			return (0);
961749Ssusans 		}
962749Ssusans 		mutex_exit(&pidlock);
963749Ssusans 		return (ECHILD);
964749Ssusans 	}
965749Ssusans 
9663235Sraf 	while (pp->p_child != NULL) {
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		proc_gone = 0;
9690Sstevel@tonic-gate 
9703235Sraf 		for (cp = pp->p_child_ns; cp != NULL; cp = cp->p_sibling_ns) {
9713235Sraf 			if (idtype != P_PID && (cp->p_pidflag & CLDWAITPID))
9723235Sraf 				continue;
9733235Sraf 			if (idtype == P_PID && id != cp->p_pid)
9740Sstevel@tonic-gate 				continue;
9753235Sraf 			if (idtype == P_PGID && id != cp->p_pgrp)
9760Sstevel@tonic-gate 				continue;
9770Sstevel@tonic-gate 
9783235Sraf 			switch (cp->p_wcode) {
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 			case CLD_TRAPPED:
9810Sstevel@tonic-gate 			case CLD_STOPPED:
9820Sstevel@tonic-gate 			case CLD_CONTINUED:
9830Sstevel@tonic-gate 				cmn_err(CE_PANIC,
9840Sstevel@tonic-gate 				    "waitid: wrong state %d on the p_newstate"
9853235Sraf 				    " list", cp->p_wcode);
9860Sstevel@tonic-gate 				break;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 			case CLD_EXITED:
9890Sstevel@tonic-gate 			case CLD_DUMPED:
9900Sstevel@tonic-gate 			case CLD_KILLED:
9910Sstevel@tonic-gate 				if (!(options & WEXITED)) {
9920Sstevel@tonic-gate 					/*
9930Sstevel@tonic-gate 					 * Count how many are already gone
9940Sstevel@tonic-gate 					 * for good.
9950Sstevel@tonic-gate 					 */
9960Sstevel@tonic-gate 					proc_gone++;
9970Sstevel@tonic-gate 					break;
9980Sstevel@tonic-gate 				}
9990Sstevel@tonic-gate 				if (!waitflag) {
10003235Sraf 					winfo(cp, ip, 0);
10010Sstevel@tonic-gate 				} else {
10023235Sraf 					winfo(cp, ip, 1);
10033235Sraf 					freeproc(cp);
10040Sstevel@tonic-gate 				}
10050Sstevel@tonic-gate 				mutex_exit(&pidlock);
10060Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10070Sstevel@tonic-gate 					sigcld_delete(ip);
10080Sstevel@tonic-gate 					sigcld_repost();
10090Sstevel@tonic-gate 				}
10100Sstevel@tonic-gate 				return (0);
10110Sstevel@tonic-gate 			}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 			if (idtype == P_PID)
10140Sstevel@tonic-gate 				break;
10150Sstevel@tonic-gate 		}
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 		/*
10180Sstevel@tonic-gate 		 * Wow! None of the threads on the p_sibling_ns list were
10190Sstevel@tonic-gate 		 * interesting threads. Check all the kids!
10200Sstevel@tonic-gate 		 */
10210Sstevel@tonic-gate 		found = 0;
10223235Sraf 		for (cp = pp->p_child; cp != NULL; cp = cp->p_sibling) {
10233235Sraf 			if (idtype == P_PID && id != cp->p_pid)
10240Sstevel@tonic-gate 				continue;
10253235Sraf 			if (idtype == P_PGID && id != cp->p_pgrp)
10260Sstevel@tonic-gate 				continue;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			switch (cp->p_wcode) {
10290Sstevel@tonic-gate 			case CLD_TRAPPED:
10300Sstevel@tonic-gate 				if (!(options & WTRAPPED))
10310Sstevel@tonic-gate 					break;
10320Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
10330Sstevel@tonic-gate 				mutex_exit(&pidlock);
10340Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10350Sstevel@tonic-gate 					sigcld_delete(ip);
10360Sstevel@tonic-gate 					sigcld_repost();
10370Sstevel@tonic-gate 				}
10380Sstevel@tonic-gate 				return (0);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 			case CLD_STOPPED:
10410Sstevel@tonic-gate 				if (!(options & WSTOPPED))
10420Sstevel@tonic-gate 					break;
10430Sstevel@tonic-gate 				/* Is it still stopped? */
10440Sstevel@tonic-gate 				mutex_enter(&cp->p_lock);
10450Sstevel@tonic-gate 				if (!jobstopped(cp)) {
10460Sstevel@tonic-gate 					mutex_exit(&cp->p_lock);
10470Sstevel@tonic-gate 					break;
10480Sstevel@tonic-gate 				}
10490Sstevel@tonic-gate 				mutex_exit(&cp->p_lock);
10500Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
10510Sstevel@tonic-gate 				mutex_exit(&pidlock);
10520Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10530Sstevel@tonic-gate 					sigcld_delete(ip);
10540Sstevel@tonic-gate 					sigcld_repost();
10550Sstevel@tonic-gate 				}
10560Sstevel@tonic-gate 				return (0);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 			case CLD_CONTINUED:
10590Sstevel@tonic-gate 				if (!(options & WCONTINUED))
10600Sstevel@tonic-gate 					break;
10610Sstevel@tonic-gate 				winfo(cp, ip, waitflag);
10620Sstevel@tonic-gate 				mutex_exit(&pidlock);
10630Sstevel@tonic-gate 				if (waitflag) {		/* accept SIGCLD */
10640Sstevel@tonic-gate 					sigcld_delete(ip);
10650Sstevel@tonic-gate 					sigcld_repost();
10660Sstevel@tonic-gate 				}
10670Sstevel@tonic-gate 				return (0);
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 			case CLD_EXITED:
10700Sstevel@tonic-gate 			case CLD_DUMPED:
10710Sstevel@tonic-gate 			case CLD_KILLED:
10723235Sraf 				if (idtype != P_PID &&
10733235Sraf 				    (cp->p_pidflag & CLDWAITPID))
10743235Sraf 					continue;
10750Sstevel@tonic-gate 				/*
10760Sstevel@tonic-gate 				 * Don't complain if a process was found in
10770Sstevel@tonic-gate 				 * the first loop but we broke out of the loop
10780Sstevel@tonic-gate 				 * because of the arguments passed to us.
10790Sstevel@tonic-gate 				 */
10800Sstevel@tonic-gate 				if (proc_gone == 0) {
10810Sstevel@tonic-gate 					cmn_err(CE_PANIC,
10820Sstevel@tonic-gate 					    "waitid: wrong state on the"
10830Sstevel@tonic-gate 					    " p_child list");
10840Sstevel@tonic-gate 				} else {
10850Sstevel@tonic-gate 					break;
10860Sstevel@tonic-gate 				}
10870Sstevel@tonic-gate 			}
10880Sstevel@tonic-gate 
10893235Sraf 			found++;
10903235Sraf 
10910Sstevel@tonic-gate 			if (idtype == P_PID)
10920Sstevel@tonic-gate 				break;
10933235Sraf 		}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 		/*
10960Sstevel@tonic-gate 		 * If we found no interesting processes at all,
10970Sstevel@tonic-gate 		 * break out and return ECHILD.
10980Sstevel@tonic-gate 		 */
10990Sstevel@tonic-gate 		if (found + proc_gone == 0)
11000Sstevel@tonic-gate 			break;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		if (options & WNOHANG) {
11033235Sraf 			mutex_exit(&pidlock);
11040Sstevel@tonic-gate 			bzero(ip, sizeof (k_siginfo_t));
11050Sstevel@tonic-gate 			/*
11060Sstevel@tonic-gate 			 * We should set ip->si_signo = SIGCLD,
11070Sstevel@tonic-gate 			 * but there is an SVVS test that expects
11080Sstevel@tonic-gate 			 * ip->si_signo to be zero in this case.
11090Sstevel@tonic-gate 			 */
11100Sstevel@tonic-gate 			return (0);
11110Sstevel@tonic-gate 		}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		/*
11140Sstevel@tonic-gate 		 * If we found no processes of interest that could
11150Sstevel@tonic-gate 		 * change state while we wait, we don't wait at all.
11160Sstevel@tonic-gate 		 * Get out with ECHILD according to SVID.
11170Sstevel@tonic-gate 		 */
11180Sstevel@tonic-gate 		if (found == proc_gone)
11190Sstevel@tonic-gate 			break;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		if (!cv_wait_sig_swap(&pp->p_cv, &pidlock)) {
11220Sstevel@tonic-gate 			mutex_exit(&pidlock);
11230Sstevel@tonic-gate 			return (EINTR);
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	mutex_exit(&pidlock);
11270Sstevel@tonic-gate 	return (ECHILD);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate int
waitsys(idtype_t idtype,id_t id,siginfo_t * infop,int options)11310Sstevel@tonic-gate waitsys(idtype_t idtype, id_t id, siginfo_t *infop, int options)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	int error;
11340Sstevel@tonic-gate 	k_siginfo_t info;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
11370Sstevel@tonic-gate 		return (set_errno(error));
11380Sstevel@tonic-gate 	if (copyout(&info, infop, sizeof (k_siginfo_t)))
11390Sstevel@tonic-gate 		return (set_errno(EFAULT));
11400Sstevel@tonic-gate 	return (0);
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate int
waitsys32(idtype_t idtype,id_t id,siginfo_t * infop,int options)11460Sstevel@tonic-gate waitsys32(idtype_t idtype, id_t id, siginfo_t *infop, int options)
11470Sstevel@tonic-gate {
11480Sstevel@tonic-gate 	int error;
11490Sstevel@tonic-gate 	k_siginfo_t info;
11500Sstevel@tonic-gate 	siginfo32_t info32;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	if (error = waitid(idtype, id, &info, options))
11530Sstevel@tonic-gate 		return (set_errno(error));
11540Sstevel@tonic-gate 	siginfo_kto32(&info, &info32);
11550Sstevel@tonic-gate 	if (copyout(&info32, infop, sizeof (info32)))
11560Sstevel@tonic-gate 		return (set_errno(EFAULT));
11570Sstevel@tonic-gate 	return (0);
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate void
proc_detach(proc_t * p)11630Sstevel@tonic-gate proc_detach(proc_t *p)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate 	proc_t *q;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	q = p->p_parent;
11700Sstevel@tonic-gate 	ASSERT(q != NULL);
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	/*
11730Sstevel@tonic-gate 	 * Take it off the newstate list of its parent
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	delete_ns(q, p);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	if (q->p_child == p) {
11780Sstevel@tonic-gate 		q->p_child = p->p_sibling;
11790Sstevel@tonic-gate 		/*
11800Sstevel@tonic-gate 		 * If the parent has no children, it better not
11810Sstevel@tonic-gate 		 * have any with new states either!
11820Sstevel@tonic-gate 		 */
11830Sstevel@tonic-gate 		ASSERT(q->p_child ? 1 : q->p_child_ns == NULL);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if (p->p_sibling) {
11870Sstevel@tonic-gate 		p->p_sibling->p_psibling = p->p_psibling;
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if (p->p_psibling) {
11910Sstevel@tonic-gate 		p->p_psibling->p_sibling = p->p_sibling;
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate  * Remove zombie children from the process table.
11970Sstevel@tonic-gate  */
11980Sstevel@tonic-gate void
freeproc(proc_t * p)11990Sstevel@tonic-gate freeproc(proc_t *p)
12000Sstevel@tonic-gate {
12010Sstevel@tonic-gate 	proc_t *q;
120212725SMenno.Lageman@Sun.COM 	task_t *tk;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	ASSERT(p->p_stat == SZOMB);
12050Sstevel@tonic-gate 	ASSERT(p->p_tlist == NULL);
12060Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	sigdelq(p, NULL, 0);
12090Sstevel@tonic-gate 	if (p->p_killsqp) {
12100Sstevel@tonic-gate 		siginfofree(p->p_killsqp);
12110Sstevel@tonic-gate 		p->p_killsqp = NULL;
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	prfree(p);	/* inform /proc */
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
12170Sstevel@tonic-gate 	 * Don't free the init processes.
12180Sstevel@tonic-gate 	 * Other dying processes will access it.
12190Sstevel@tonic-gate 	 */
12200Sstevel@tonic-gate 	if (p == proc_init)
12210Sstevel@tonic-gate 		return;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * We wait until now to free the cred structure because a
12260Sstevel@tonic-gate 	 * zombie process's credentials may be examined by /proc.
12270Sstevel@tonic-gate 	 * No cred locking needed because there are no threads at this point.
12280Sstevel@tonic-gate 	 */
12290Sstevel@tonic-gate 	upcount_dec(crgetruid(p->p_cred), crgetzoneid(p->p_cred));
12300Sstevel@tonic-gate 	crfree(p->p_cred);
12310Sstevel@tonic-gate 	if (p->p_corefile != NULL) {
12320Sstevel@tonic-gate 		corectl_path_rele(p->p_corefile);
12330Sstevel@tonic-gate 		p->p_corefile = NULL;
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate 	if (p->p_content != NULL) {
12360Sstevel@tonic-gate 		corectl_content_rele(p->p_content);
12370Sstevel@tonic-gate 		p->p_content = NULL;
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (p->p_nextofkin && !((p->p_nextofkin->p_flag & SNOWAIT) ||
12410Sstevel@tonic-gate 	    (PTOU(p->p_nextofkin)->u_signal[SIGCLD - 1] == SIG_IGN))) {
12420Sstevel@tonic-gate 		/*
12430Sstevel@tonic-gate 		 * This should still do the right thing since p_utime/stime
12440Sstevel@tonic-gate 		 * get set to the correct value on process exit, so it
12450Sstevel@tonic-gate 		 * should get properly updated
12460Sstevel@tonic-gate 		 */
12470Sstevel@tonic-gate 		p->p_nextofkin->p_cutime += p->p_utime;
12480Sstevel@tonic-gate 		p->p_nextofkin->p_cstime += p->p_stime;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER] += p->p_acct[LMS_USER];
12510Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SYSTEM] += p->p_acct[LMS_SYSTEM];
12520Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TRAP] += p->p_acct[LMS_TRAP];
12530Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_TFAULT] += p->p_acct[LMS_TFAULT];
12540Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_DFAULT] += p->p_acct[LMS_DFAULT];
12550Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_KFAULT] += p->p_acct[LMS_KFAULT];
12560Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_USER_LOCK]
12570Sstevel@tonic-gate 		    += p->p_acct[LMS_USER_LOCK];
12580Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_SLEEP] += p->p_acct[LMS_SLEEP];
12590Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_WAIT_CPU]
12600Sstevel@tonic-gate 		    += p->p_acct[LMS_WAIT_CPU];
12610Sstevel@tonic-gate 		p->p_nextofkin->p_cacct[LMS_STOPPED] += p->p_acct[LMS_STOPPED];
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		p->p_nextofkin->p_cru.minflt	+= p->p_ru.minflt;
12640Sstevel@tonic-gate 		p->p_nextofkin->p_cru.majflt	+= p->p_ru.majflt;
12650Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nswap	+= p->p_ru.nswap;
12660Sstevel@tonic-gate 		p->p_nextofkin->p_cru.inblock	+= p->p_ru.inblock;
12670Sstevel@tonic-gate 		p->p_nextofkin->p_cru.oublock	+= p->p_ru.oublock;
12680Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgsnd	+= p->p_ru.msgsnd;
12690Sstevel@tonic-gate 		p->p_nextofkin->p_cru.msgrcv	+= p->p_ru.msgrcv;
12700Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nsignals	+= p->p_ru.nsignals;
12710Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nvcsw	+= p->p_ru.nvcsw;
12720Sstevel@tonic-gate 		p->p_nextofkin->p_cru.nivcsw	+= p->p_ru.nivcsw;
12730Sstevel@tonic-gate 		p->p_nextofkin->p_cru.sysc	+= p->p_ru.sysc;
12740Sstevel@tonic-gate 		p->p_nextofkin->p_cru.ioch	+= p->p_ru.ioch;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	q = p->p_nextofkin;
12790Sstevel@tonic-gate 	if (q && q->p_orphan == p)
12800Sstevel@tonic-gate 		q->p_orphan = p->p_nextorph;
12810Sstevel@tonic-gate 	else if (q) {
12820Sstevel@tonic-gate 		for (q = q->p_orphan; q; q = q->p_nextorph)
12830Sstevel@tonic-gate 			if (q->p_nextorph == p)
12840Sstevel@tonic-gate 				break;
12850Sstevel@tonic-gate 		ASSERT(q && q->p_nextorph == p);
12860Sstevel@tonic-gate 		q->p_nextorph = p->p_nextorph;
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
128912725SMenno.Lageman@Sun.COM 	/*
129012725SMenno.Lageman@Sun.COM 	 * The process table slot is being freed, so it is now safe to give up
129112725SMenno.Lageman@Sun.COM 	 * task and project membership.
129212725SMenno.Lageman@Sun.COM 	 */
129312725SMenno.Lageman@Sun.COM 	mutex_enter(&p->p_lock);
129412725SMenno.Lageman@Sun.COM 	tk = p->p_task;
129512725SMenno.Lageman@Sun.COM 	task_detach(p);
129612725SMenno.Lageman@Sun.COM 	mutex_exit(&p->p_lock);
129712725SMenno.Lageman@Sun.COM 
12980Sstevel@tonic-gate 	proc_detach(p);
129912834SMenno.Lageman@Sun.COM 	pid_exit(p, tk);	/* frees pid and proc structure */
130012725SMenno.Lageman@Sun.COM 
130112725SMenno.Lageman@Sun.COM 	task_rele(tk);
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate /*
13050Sstevel@tonic-gate  * Delete process "child" from the newstate list of process "parent"
13060Sstevel@tonic-gate  */
13070Sstevel@tonic-gate void
delete_ns(proc_t * parent,proc_t * child)13080Sstevel@tonic-gate delete_ns(proc_t *parent, proc_t *child)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	proc_t **ns;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
13130Sstevel@tonic-gate 	ASSERT(child->p_parent == parent);
13140Sstevel@tonic-gate 	for (ns = &parent->p_child_ns; *ns != NULL; ns = &(*ns)->p_sibling_ns) {
13150Sstevel@tonic-gate 		if (*ns == child) {
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 			ASSERT((*ns)->p_parent == parent);
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 			*ns = child->p_sibling_ns;
13200Sstevel@tonic-gate 			child->p_sibling_ns = NULL;
13210Sstevel@tonic-gate 			return;
13220Sstevel@tonic-gate 		}
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate  * Add process "child" to the new state list of process "parent"
13280Sstevel@tonic-gate  */
13290Sstevel@tonic-gate void
add_ns(proc_t * parent,proc_t * child)13300Sstevel@tonic-gate add_ns(proc_t *parent, proc_t *child)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	ASSERT(child->p_sibling_ns == NULL);
13330Sstevel@tonic-gate 	child->p_sibling_ns = parent->p_child_ns;
13340Sstevel@tonic-gate 	parent->p_child_ns = child;
13350Sstevel@tonic-gate }
1336