xref: /illumos-gate/usr/src/uts/common/os/exit.c (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2018 Joyent, Inc.
25  * Copyright 2026 Oxide Computer Company
26  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
27  */
28 
29 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/cred.h>
36 #include <sys/user.h>
37 #include <sys/errno.h>
38 #include <sys/proc.h>
39 #include <sys/ucontext.h>
40 #include <sys/procfs.h>
41 #include <sys/vnode.h>
42 #include <sys/acct.h>
43 #include <sys/var.h>
44 #include <sys/cmn_err.h>
45 #include <sys/debug.h>
46 #include <sys/wait.h>
47 #include <sys/siginfo.h>
48 #include <sys/procset.h>
49 #include <sys/class.h>
50 #include <sys/file.h>
51 #include <sys/session.h>
52 #include <sys/kmem.h>
53 #include <sys/vtrace.h>
54 #include <sys/prsystm.h>
55 #include <sys/ipc.h>
56 #include <sys/sem_impl.h>
57 #include <c2/audit.h>
58 #include <sys/aio_impl.h>
59 #include <vm/as.h>
60 #include <sys/poll.h>
61 #include <sys/door.h>
62 #include <sys/lwpchan_impl.h>
63 #include <sys/utrap.h>
64 #include <sys/task.h>
65 #include <sys/exacct.h>
66 #include <sys/cyclic.h>
67 #include <sys/schedctl.h>
68 #include <sys/rctl.h>
69 #include <sys/contract_impl.h>
70 #include <sys/contract/process_impl.h>
71 #include <sys/list.h>
72 #include <sys/dtrace.h>
73 #include <sys/pool.h>
74 #include <sys/sdt.h>
75 #include <sys/corectl.h>
76 #include <sys/core.h>
77 #include <sys/brand.h>
78 #include <sys/libc_kernel.h>
79 #include <sys/spawn_impl.h>
80 
81 /*
82  * convert code/data pair into old style wait status
83  */
84 int
85 wstat(int code, int data)
86 {
87 	int stat = (data & 0377);
88 
89 	switch (code) {
90 	case CLD_EXITED:
91 		stat <<= 8;
92 		break;
93 	case CLD_DUMPED:
94 		stat |= WCOREFLG;
95 		break;
96 	case CLD_KILLED:
97 		break;
98 	case CLD_TRAPPED:
99 	case CLD_STOPPED:
100 		stat <<= 8;
101 		stat |= WSTOPFLG;
102 		break;
103 	case CLD_CONTINUED:
104 		stat = WCONTFLG;
105 		break;
106 	default:
107 		cmn_err(CE_PANIC, "wstat: bad code");
108 		/* NOTREACHED */
109 	}
110 	return (stat);
111 }
112 
113 static char *
114 exit_reason(char *buf, size_t bufsz, int what, int why)
115 {
116 	switch (why) {
117 	case CLD_EXITED:
118 		(void) snprintf(buf, bufsz, "exited with status %d", what);
119 		break;
120 	case CLD_KILLED:
121 		(void) snprintf(buf, bufsz, "exited on fatal signal %d", what);
122 		break;
123 	case CLD_DUMPED:
124 		(void) snprintf(buf, bufsz, "core dumped on signal %d", what);
125 		break;
126 	default:
127 		(void) snprintf(buf, bufsz, "encountered unknown error "
128 		    "(%d, %d)", why, what);
129 		break;
130 	}
131 
132 	return (buf);
133 }
134 
135 /*
136  * exit system call: pass back caller's arg.
137  */
138 void
139 rexit(int rval)
140 {
141 	exit(CLD_EXITED, rval);
142 }
143 
144 /*
145  * Called by proc_exit() when a zone's init exits, presumably because
146  * it failed.  As long as the given zone is still in the "running"
147  * state, we will re-exec() init, but first we need to reset things
148  * which are usually inherited across exec() but will break init's
149  * assumption that it is being exec()'d from a virgin process.  Most
150  * importantly this includes closing all file descriptors (exec only
151  * closes those marked close-on-exec) and resetting signals (exec only
152  * resets handled signals, and we need to clear any signals which
153  * killed init).  Anything else that exec(2) says would be inherited,
154  * but would affect the execution of init, needs to be reset.
155  */
156 static int
157 restart_init(int what, int why)
158 {
159 	kthread_t *t = curthread;
160 	klwp_t *lwp = ttolwp(t);
161 	proc_t *p = ttoproc(t);
162 	proc_t *pp = p->p_zone->zone_zsched;
163 	user_t *up = PTOU(p);
164 
165 	vnode_t *oldcd, *oldrd;
166 	int i, err;
167 	char reason_buf[64];
168 
169 	/*
170 	 * Let zone admin (and global zone admin if this is for a non-global
171 	 * zone) know that init has failed and will be restarted.
172 	 */
173 	zcmn_err(p->p_zone->zone_id, CE_WARN,
174 	    "init(8) %s: restarting automatically",
175 	    exit_reason(reason_buf, sizeof (reason_buf), what, why));
176 
177 	if (!INGLOBALZONE(p)) {
178 		cmn_err(CE_WARN, "init(8) for zone %s (pid %d) %s: "
179 		    "restarting automatically",
180 		    p->p_zone->zone_name, p->p_pid, reason_buf);
181 	}
182 
183 	/*
184 	 * Remove any fpollinfo_t's for this (last) thread from our file
185 	 * descriptors so closeall() can ASSERT() that they're all gone.
186 	 * Then close all open file descriptors in the process.
187 	 */
188 	pollcleanup();
189 	closeall(P_FINFO(p));
190 
191 	/*
192 	 * Grab p_lock and begin clearing miscellaneous global process
193 	 * state that needs to be reset before we exec the new init(8).
194 	 */
195 
196 	mutex_enter(&p->p_lock);
197 	prbarrier(p);
198 
199 	p->p_flag &= ~(SKILLED | SEXTKILLED | SEXITING | SDOCORE);
200 	up->u_cmask = CMASK;
201 
202 	sigemptyset(&t->t_hold);
203 	sigemptyset(&t->t_sig);
204 	sigemptyset(&t->t_extsig);
205 
206 	sigemptyset(&p->p_sig);
207 	sigemptyset(&p->p_extsig);
208 
209 	sigdelq(p, t, 0);
210 	sigdelq(p, NULL, 0);
211 
212 	if (p->p_killsqp) {
213 		siginfofree(p->p_killsqp);
214 		p->p_killsqp = NULL;
215 	}
216 
217 	/*
218 	 * Reset any signals that are ignored back to the default disposition.
219 	 * Other u_signal members will be cleared when exec calls sigdefault().
220 	 */
221 	for (i = 1; i < NSIG; i++) {
222 		if (up->u_signal[i - 1] == SIG_IGN) {
223 			up->u_signal[i - 1] = SIG_DFL;
224 			sigemptyset(&up->u_sigmask[i - 1]);
225 		}
226 	}
227 
228 	/*
229 	 * Clear the current signal, any signal info associated with it, and
230 	 * any signal information from contracts and/or contract templates.
231 	 */
232 	lwp->lwp_cursig = 0;
233 	lwp->lwp_extsig = 0;
234 	if (lwp->lwp_curinfo != NULL) {
235 		siginfofree(lwp->lwp_curinfo);
236 		lwp->lwp_curinfo = NULL;
237 	}
238 	lwp_ctmpl_clear(lwp);
239 
240 	/*
241 	 * Reset both the process root directory and the current working
242 	 * directory to the root of the zone just as we do during boot.
243 	 */
244 	VN_HOLD(p->p_zone->zone_rootvp);
245 	oldrd = up->u_rdir;
246 	up->u_rdir = p->p_zone->zone_rootvp;
247 
248 	VN_HOLD(p->p_zone->zone_rootvp);
249 	oldcd = up->u_cdir;
250 	up->u_cdir = p->p_zone->zone_rootvp;
251 
252 	if (up->u_cwd != NULL) {
253 		refstr_rele(up->u_cwd);
254 		up->u_cwd = NULL;
255 	}
256 
257 	/* Reset security flags */
258 	mutex_enter(&pp->p_lock);
259 	p->p_secflags = pp->p_secflags;
260 	mutex_exit(&pp->p_lock);
261 
262 	mutex_exit(&p->p_lock);
263 
264 	if (oldrd != NULL)
265 		VN_RELE(oldrd);
266 	if (oldcd != NULL)
267 		VN_RELE(oldcd);
268 
269 	/*
270 	 * It's possible that a zone's init will have become privilege aware
271 	 * and modified privilege sets; reset them.
272 	 */
273 	cred_t *oldcr, *newcr;
274 
275 	mutex_enter(&p->p_crlock);
276 	oldcr = p->p_cred;
277 	mutex_enter(&pp->p_crlock);
278 	crhold(newcr = p->p_cred = pp->p_cred);
279 	mutex_exit(&pp->p_crlock);
280 	mutex_exit(&p->p_crlock);
281 	crfree(oldcr);
282 	/* Additional hold for the current thread - expected by crset() */
283 	crhold(newcr);
284 	crset(p, newcr);
285 
286 	/* Free the controlling tty.  (freectty() always assumes curproc.) */
287 	ASSERT(p == curproc);
288 	(void) freectty(B_TRUE);
289 
290 	/*
291 	 * Now exec() the new init(8) on top of the current process.  If we
292 	 * succeed, the caller will treat this like a successful system call.
293 	 * If we fail, we issue messages and the caller will proceed with exit.
294 	 */
295 	err = exec_init(p->p_zone->zone_initname, NULL);
296 
297 	if (err == 0)
298 		return (0);
299 
300 	zcmn_err(p->p_zone->zone_id, CE_WARN,
301 	    "failed to restart init(8) (err=%d): system reboot required", err);
302 
303 	if (!INGLOBALZONE(p)) {
304 		cmn_err(CE_WARN, "failed to restart init(8) for zone %s "
305 		    "(pid %d, err=%d): zoneadm(8) boot required",
306 		    p->p_zone->zone_name, p->p_pid, err);
307 	}
308 
309 	return (-1);
310 }
311 
312 /*
313  * Release resources.
314  * Enter zombie state.
315  * Wake up parent and init processes,
316  * and dispose of children.
317  */
318 void
319 exit(int why, int what)
320 {
321 	/*
322 	 * If proc_exit() fails, then some other lwp in the process
323 	 * got there first.  We just have to call lwp_exit() to allow
324 	 * the other lwp to finish exiting the process.  Otherwise we're
325 	 * restarting init, and should return.
326 	 */
327 	if (proc_exit(why, what) != 0) {
328 		mutex_enter(&curproc->p_lock);
329 		ASSERT(curproc->p_flag & SEXITLWPS);
330 		lwp_exit();
331 		/* NOTREACHED */
332 	}
333 }
334 
335 /*
336  * Set the SEXITING flag on the process, after making sure /proc does
337  * not have it locked.  This is done in more places than proc_exit(),
338  * so it is a separate function.
339  */
340 void
341 proc_is_exiting(proc_t *p)
342 {
343 	mutex_enter(&p->p_lock);
344 	prbarrier(p);
345 	p->p_flag |= SEXITING;
346 	mutex_exit(&p->p_lock);
347 }
348 
349 /*
350  * Return true if zone's init is restarted, false if exit processing should
351  * proceeed.
352  */
353 static boolean_t
354 zone_init_exit(zone_t *z, int why, int what)
355 {
356 	/*
357 	 * Typically we don't let the zone's init exit unless zone_start_init()
358 	 * failed its exec, or we are shutting down the zone or the machine,
359 	 * although the various flags handled within this function will control
360 	 * the behavior.
361 	 *
362 	 * Since we are single threaded, we don't need to lock the following
363 	 * accesses to zone_proc_initpid.
364 	 */
365 	if (z->zone_boot_err != 0 ||
366 	    zone_status_get(z) >= ZONE_IS_SHUTTING_DOWN ||
367 	    zone_status_get(global_zone) >= ZONE_IS_SHUTTING_DOWN) {
368 		/*
369 		 * Clear the zone's init pid and proceed with exit processing.
370 		 */
371 		z->zone_proc_initpid = -1;
372 		return (B_FALSE);
373 	}
374 
375 	/*
376 	 * There are a variety of configuration flags on the zone to control
377 	 * init exit behavior.
378 	 *
379 	 * If the init process should be restarted, the "zone_restart_init"
380 	 * member will be set.
381 	 */
382 	if (!z->zone_restart_init) {
383 		/*
384 		 * The zone has been set up to halt when init exits.
385 		 */
386 		(void) zone_kadmin(A_SHUTDOWN, AD_HALT, NULL, zone_kcred());
387 		z->zone_proc_initpid = -1;
388 		return (B_FALSE);
389 	}
390 
391 	/*
392 	 * At this point we know we're configured to restart init, but there
393 	 * are various modifiers to that behavior.
394 	 */
395 
396 	if (z->zone_reboot_on_init_exit) {
397 		/*
398 		 * Some init programs in branded zones do not tolerate a
399 		 * restart in the traditional manner; setting
400 		 * "zone_reboot_on_init_exit" will cause the entire zone to be
401 		 * rebooted instead.
402 		 */
403 
404 		if (z->zone_restart_init_0) {
405 			/*
406 			 * Some init programs in branded zones only want to
407 			 * restart if they exit 0, otherwise the zone should
408 			 * shutdown. Setting the "zone_restart_init_0" member
409 			 * controls this behavior.
410 			 */
411 			if (why == CLD_EXITED && what == 0) {
412 				/* Trigger a zone reboot */
413 				(void) zone_kadmin(A_REBOOT, 0, NULL,
414 				    zone_kcred());
415 			} else {
416 				/* Shutdown instead of reboot */
417 				(void) zone_kadmin(A_SHUTDOWN, AD_HALT, NULL,
418 				    zone_kcred());
419 			}
420 		} else {
421 			/* Trigger a zone reboot */
422 			(void) zone_kadmin(A_REBOOT, 0, NULL, zone_kcred());
423 		}
424 
425 		z->zone_proc_initpid = -1;
426 		return (B_FALSE);
427 	}
428 
429 	if (z->zone_restart_init_0) {
430 		/*
431 		 * Some init programs in branded zones only want to restart if
432 		 * they exit 0, otherwise the zone should shutdown. Setting the
433 		 * "zone_restart_init_0" member controls this behavior.
434 		 *
435 		 * In this case we only restart init if it exited successfully.
436 		 */
437 		if (why == CLD_EXITED && what == 0 &&
438 		    restart_init(what, why) == 0) {
439 			return (B_TRUE);
440 		}
441 	} else {
442 		/*
443 		 * No restart modifiers on the zone, attempt to restart init.
444 		 */
445 		if (restart_init(what, why) == 0)
446 			return (B_TRUE);
447 	}
448 
449 	/*
450 	 * The restart failed, or the criteria for a restart are not met;
451 	 * the zone will shut down.
452 	 */
453 	(void) zone_kadmin(A_SHUTDOWN, AD_HALT, NULL, zone_kcred());
454 	z->zone_proc_initpid = -1;
455 	return (B_FALSE);
456 }
457 
458 /*
459  * Return value:
460  *   1 - exitlwps() failed, call (or continue) lwp_exit()
461  *   0 - restarting init.  Return through system call path
462  */
463 int
464 proc_exit(int why, int what)
465 {
466 	kthread_t *t = curthread;
467 	klwp_t *lwp = ttolwp(t);
468 	proc_t *p = ttoproc(t);
469 	zone_t *z = p->p_zone;
470 	timeout_id_t tmp_id;
471 	int rv;
472 	proc_t *q;
473 	task_t *tk;
474 	vnode_t *exec_vp, *execdir_vp, *cdir, *rdir;
475 	sigqueue_t *sqp;
476 	lwpdir_t *lwpdir;
477 	uint_t lwpdir_sz;
478 	tidhash_t *tidhash;
479 	uint_t tidhash_sz;
480 	ret_tidhash_t *ret_tidhash;
481 	refstr_t *cwd;
482 	hrtime_t hrutime, hrstime;
483 	int evaporate;
484 
485 	/*
486 	 * Stop and discard the process's lwps except for the current one,
487 	 * unless some other lwp beat us to it.  If exitlwps() fails then
488 	 * return and the calling lwp will call (or continue in) lwp_exit().
489 	 */
490 	proc_is_exiting(p);
491 	if (exitlwps(0) != 0)
492 		return (1);
493 
494 	/*
495 	 * If this is a spawn(2) child that has not yet reported back to its
496 	 * parent, do that now and unblock the parent. This is done as soon as
497 	 * the exit is committed, past exitlwps(), so the parent's spawn(2)
498 	 * returns promptly. It has no ordering requirement against the DTrace
499 	 * exit probes below.
500 	 */
501 	if (p->p_spawn_ksp != NULL)
502 		spawn_complete(p->p_spawn_ksp, 0);
503 
504 	mutex_enter(&p->p_lock);
505 	if (p->p_ttime > 0) {
506 		/*
507 		 * Account any remaining ticks charged to this process
508 		 * on its way out.
509 		 */
510 		(void) task_cpu_time_incr(p->p_task, p->p_ttime);
511 		p->p_ttime = 0;
512 	}
513 	mutex_exit(&p->p_lock);
514 
515 	DTRACE_PROC(lwp__exit);
516 	DTRACE_PROC1(exit, int, why);
517 
518 	/*
519 	 * Will perform any brand specific proc exit processing, since this
520 	 * is always the last lwp, will also perform lwp_exit and free brand
521 	 * data
522 	 */
523 	if (PROC_IS_BRANDED(p)) {
524 		lwp_detach_brand_hdlrs(lwp);
525 		brand_clearbrand(p, B_FALSE);
526 	}
527 
528 	/*
529 	 * Don't let init exit unless zone_start_init() failed its exec, or
530 	 * we are shutting down the zone or the machine.
531 	 *
532 	 * Since we are single threaded, we don't need to lock the
533 	 * following accesses to zone_proc_initpid.
534 	 */
535 	if (p->p_pid == z->zone_proc_initpid) {
536 		/* If zone's init restarts, we're done here. */
537 		if (zone_init_exit(z, why, what))
538 			return (0);
539 	}
540 
541 	lwp_pcb_exit();
542 
543 	/*
544 	 * Allocate a sigqueue now, before we grab locks.
545 	 * It will be given to sigcld(), below.
546 	 * Special case:  If we will be making the process disappear
547 	 * without a trace because it is either:
548 	 *	* an exiting SSYS process, or
549 	 *	* a vfork child exiting with _EVAPORATE (no longer used by
550 	 *	  libc but retained for branded userlands), or
551 	 *	* a spawn(2) child whose setup failed (CLDEVAPORATE),
552 	 * we don't bother to allocate a useless sigqueue.
553 	 *
554 	 * p_pidflag is ordinarily protected by pidlock but CLDEVAPORATE is
555 	 * only ever set by this process itself, before it exits.
556 	 */
557 	evaporate = (p->p_flag & SSYS) || ((p->p_flag & SVFORK) &&
558 	    why == CLD_EXITED && what == _EVAPORATE) ||
559 	    (p->p_pidflag & CLDEVAPORATE);
560 	if (!evaporate)
561 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
562 
563 	/*
564 	 * revoke any doors created by the process.
565 	 */
566 	if (p->p_door_list)
567 		door_exit();
568 
569 	/*
570 	 * Release schedctl data structures.
571 	 */
572 	if (p->p_pagep)
573 		schedctl_proc_cleanup();
574 
575 	/*
576 	 * make sure all pending kaio has completed.
577 	 */
578 	if (p->p_aio)
579 		aio_cleanup_exit();
580 
581 	/*
582 	 * discard the lwpchan cache.
583 	 */
584 	if (p->p_lcp != NULL)
585 		lwpchan_destroy_cache(0);
586 
587 	/*
588 	 * Clean up any DTrace helper actions or probes for the process.
589 	 */
590 	if (p->p_dtrace_helpers != NULL) {
591 		ASSERT(dtrace_helpers_cleanup != NULL);
592 		(*dtrace_helpers_cleanup)(p);
593 	}
594 
595 	/*
596 	 * Clean up any signalfd state for the process.
597 	 */
598 	if (p->p_sigfd != NULL) {
599 		VERIFY(sigfd_exit_helper != NULL);
600 		(*sigfd_exit_helper)();
601 	}
602 
603 	/* untimeout the realtime timers */
604 	if (p->p_itimer != NULL)
605 		timer_exit();
606 
607 	if ((tmp_id = p->p_alarmid) != 0) {
608 		p->p_alarmid = 0;
609 		(void) untimeout(tmp_id);
610 	}
611 
612 	/*
613 	 * If we had generated any upanic(2) state, free that now.
614 	 */
615 	if (p->p_upanic != NULL) {
616 		kmem_free(p->p_upanic, PRUPANIC_BUFLEN);
617 		p->p_upanic = NULL;
618 	}
619 
620 	/*
621 	 * Remove any fpollinfo_t's for this (last) thread from our file
622 	 * descriptors so closeall() can ASSERT() that they're all gone.
623 	 */
624 	pollcleanup();
625 
626 	if (p->p_rprof_cyclic != CYCLIC_NONE) {
627 		mutex_enter(&cpu_lock);
628 		cyclic_remove(p->p_rprof_cyclic);
629 		mutex_exit(&cpu_lock);
630 	}
631 
632 	mutex_enter(&p->p_lock);
633 
634 	/*
635 	 * Clean up any DTrace probes associated with this process.
636 	 */
637 	if (p->p_dtrace_probes) {
638 		ASSERT(dtrace_fasttrap_exit_ptr != NULL);
639 		dtrace_fasttrap_exit_ptr(p);
640 	}
641 
642 	while ((tmp_id = p->p_itimerid) != 0) {
643 		p->p_itimerid = 0;
644 		mutex_exit(&p->p_lock);
645 		(void) untimeout(tmp_id);
646 		mutex_enter(&p->p_lock);
647 	}
648 
649 	lwp_cleanup();
650 
651 	/*
652 	 * We are about to exit; prevent our resource associations from
653 	 * being changed.
654 	 */
655 	pool_barrier_enter();
656 
657 	/*
658 	 * Block the process against /proc now that we have really
659 	 * acquired p->p_lock (to manipulate p_tlist at least).
660 	 */
661 	prbarrier(p);
662 
663 	sigfillset(&p->p_ignore);
664 	sigemptyset(&p->p_siginfo);
665 	sigemptyset(&p->p_sig);
666 	sigemptyset(&p->p_extsig);
667 	sigemptyset(&t->t_sig);
668 	sigemptyset(&t->t_extsig);
669 	sigemptyset(&p->p_sigmask);
670 	sigdelq(p, t, 0);
671 	lwp->lwp_cursig = 0;
672 	lwp->lwp_extsig = 0;
673 	p->p_flag &= ~(SKILLED | SEXTKILLED);
674 	if (lwp->lwp_curinfo) {
675 		siginfofree(lwp->lwp_curinfo);
676 		lwp->lwp_curinfo = NULL;
677 	}
678 
679 	t->t_proc_flag |= TP_LWPEXIT;
680 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
681 	prlwpexit(t);		/* notify /proc */
682 	lwp_hash_out(p, t->t_tid);
683 	prexit(p);
684 
685 	p->p_lwpcnt = 0;
686 	p->p_tlist = NULL;
687 	sigqfree(p);
688 	term_mstate(t);
689 	p->p_mterm = gethrtime();
690 
691 	exec_vp = p->p_exec;
692 	execdir_vp = p->p_execdir;
693 	p->p_exec = NULLVP;
694 	p->p_execdir = NULLVP;
695 	mutex_exit(&p->p_lock);
696 
697 	pr_free_watched_pages(p);
698 
699 	closeall(P_FINFO(p));
700 
701 	/* Free the controlling tty.  (freectty() always assumes curproc.) */
702 	ASSERT(p == curproc);
703 	(void) freectty(B_TRUE);
704 
705 #if defined(__sparc)
706 	if (p->p_utraps != NULL)
707 		utrap_free(p);
708 #endif
709 	if (p->p_semacct)			/* IPC semaphore exit */
710 		semexit(p);
711 	rv = wstat(why, what);
712 
713 	acct(rv & 0xff);
714 	exacct_commit_proc(p, rv);
715 
716 	/*
717 	 * Release any resources associated with C2 auditing
718 	 */
719 	if (AU_AUDITING()) {
720 		/*
721 		 * audit exit system call
722 		 */
723 		audit_exit(why, what);
724 	}
725 
726 	/*
727 	 * Free address space.
728 	 */
729 	relvm();
730 
731 	if (exec_vp) {
732 		/*
733 		 * Close this executable which has been opened when the process
734 		 * was created by getproc().
735 		 */
736 		(void) VOP_CLOSE(exec_vp, FREAD, 1, (offset_t)0, CRED(), NULL);
737 		VN_RELE(exec_vp);
738 	}
739 	if (execdir_vp)
740 		VN_RELE(execdir_vp);
741 
742 	/*
743 	 * Release held contracts.
744 	 */
745 	contract_exit(p);
746 
747 	/*
748 	 * Depart our encapsulating process contract.
749 	 */
750 	if ((p->p_flag & SSYS) == 0) {
751 		ASSERT(p->p_ct_process);
752 		contract_process_exit(p->p_ct_process, p, rv);
753 	}
754 
755 	/*
756 	 * Remove pool association, and block if requested by pool_do_bind.
757 	 */
758 	mutex_enter(&p->p_lock);
759 	ASSERT(p->p_pool->pool_ref > 0);
760 	atomic_dec_32(&p->p_pool->pool_ref);
761 	p->p_pool = pool_default;
762 	/*
763 	 * Now that our address space has been freed and all other threads
764 	 * in this process have exited, set the PEXITED pool flag.  This
765 	 * tells the pools subsystems to ignore this process if it was
766 	 * requested to rebind this process to a new pool.
767 	 */
768 	p->p_poolflag |= PEXITED;
769 	pool_barrier_exit();
770 	mutex_exit(&p->p_lock);
771 
772 	mutex_enter(&pidlock);
773 
774 	/*
775 	 * Delete this process from the newstate list of its parent. We
776 	 * will put it in the right place in the sigcld in the end.
777 	 */
778 	delete_ns(p->p_parent, p);
779 
780 	/*
781 	 * Reassign the orphans to the next of kin.
782 	 * Don't rearrange init's orphanage.
783 	 */
784 	if ((q = p->p_orphan) != NULL && p != proc_init) {
785 
786 		proc_t *nokp = p->p_nextofkin;
787 
788 		for (;;) {
789 			q->p_nextofkin = nokp;
790 			if (q->p_nextorph == NULL)
791 				break;
792 			q = q->p_nextorph;
793 		}
794 		q->p_nextorph = nokp->p_orphan;
795 		nokp->p_orphan = p->p_orphan;
796 		p->p_orphan = NULL;
797 	}
798 
799 	/*
800 	 * Reassign the children to init.
801 	 * Don't try to assign init's children to init.
802 	 */
803 	if ((q = p->p_child) != NULL && p != proc_init) {
804 		struct proc	*np;
805 		struct proc	*initp = proc_init;
806 		boolean_t	setzonetop = B_FALSE;
807 
808 		if (!INGLOBALZONE(curproc))
809 			setzonetop = B_TRUE;
810 
811 		pgdetach(p);
812 
813 		do {
814 			np = q->p_sibling;
815 			/*
816 			 * Delete it from its current parent new state
817 			 * list and add it to init new state list
818 			 */
819 			delete_ns(q->p_parent, q);
820 
821 			q->p_ppid = 1;
822 			q->p_pidflag &= ~(CLDNOSIGCHLD | CLDWAITPID);
823 			if (setzonetop) {
824 				mutex_enter(&q->p_lock);
825 				q->p_flag |= SZONETOP;
826 				mutex_exit(&q->p_lock);
827 			}
828 			q->p_parent = initp;
829 
830 			/*
831 			 * Since q will be the first child,
832 			 * it will not have a previous sibling.
833 			 */
834 			q->p_psibling = NULL;
835 			if (initp->p_child) {
836 				initp->p_child->p_psibling = q;
837 			}
838 			q->p_sibling = initp->p_child;
839 			initp->p_child = q;
840 			if (q->p_proc_flag & P_PR_PTRACE) {
841 				mutex_enter(&q->p_lock);
842 				sigtoproc(q, NULL, SIGKILL);
843 				mutex_exit(&q->p_lock);
844 			}
845 			/*
846 			 * sigcld() will add the child to parents
847 			 * newstate list.
848 			 */
849 			if (q->p_stat == SZOMB)
850 				sigcld(q, NULL);
851 		} while ((q = np) != NULL);
852 
853 		p->p_child = NULL;
854 		ASSERT(p->p_child_ns == NULL);
855 	}
856 
857 	TRACE_1(TR_FAC_PROC, TR_PROC_EXIT, "proc_exit: %p", p);
858 
859 	mutex_enter(&p->p_lock);
860 	CL_EXIT(curthread); /* tell the scheduler that curthread is exiting */
861 
862 	/*
863 	 * Have our task accummulate our resource usage data before they
864 	 * become contaminated by p_cacct etc., and before we renounce
865 	 * membership of the task.
866 	 *
867 	 * We do this regardless of whether or not task accounting is active.
868 	 * This is to avoid having nonsense data reported for this task if
869 	 * task accounting is subsequently enabled. The overhead is minimal;
870 	 * by this point, this process has accounted for the usage of all its
871 	 * LWPs. We nonetheless do the work here, and under the protection of
872 	 * pidlock, so that the movement of the process's usage to the task
873 	 * happens at the same time as the removal of the process from the
874 	 * task, from the point of view of exacct_snapshot_task_usage().
875 	 */
876 	exacct_update_task_mstate(p);
877 
878 	hrutime = mstate_aggr_state(p, LMS_USER);
879 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
880 	p->p_utime = (clock_t)NSEC_TO_TICK(hrutime) + p->p_cutime;
881 	p->p_stime = (clock_t)NSEC_TO_TICK(hrstime) + p->p_cstime;
882 
883 	p->p_acct[LMS_USER]	+= p->p_cacct[LMS_USER];
884 	p->p_acct[LMS_SYSTEM]	+= p->p_cacct[LMS_SYSTEM];
885 	p->p_acct[LMS_TRAP]	+= p->p_cacct[LMS_TRAP];
886 	p->p_acct[LMS_TFAULT]	+= p->p_cacct[LMS_TFAULT];
887 	p->p_acct[LMS_DFAULT]	+= p->p_cacct[LMS_DFAULT];
888 	p->p_acct[LMS_KFAULT]	+= p->p_cacct[LMS_KFAULT];
889 	p->p_acct[LMS_USER_LOCK] += p->p_cacct[LMS_USER_LOCK];
890 	p->p_acct[LMS_SLEEP]	+= p->p_cacct[LMS_SLEEP];
891 	p->p_acct[LMS_WAIT_CPU]	+= p->p_cacct[LMS_WAIT_CPU];
892 	p->p_acct[LMS_STOPPED]	+= p->p_cacct[LMS_STOPPED];
893 
894 	p->p_ru.minflt	+= p->p_cru.minflt;
895 	p->p_ru.majflt	+= p->p_cru.majflt;
896 	p->p_ru.nswap	+= p->p_cru.nswap;
897 	p->p_ru.inblock	+= p->p_cru.inblock;
898 	p->p_ru.oublock	+= p->p_cru.oublock;
899 	p->p_ru.msgsnd	+= p->p_cru.msgsnd;
900 	p->p_ru.msgrcv	+= p->p_cru.msgrcv;
901 	p->p_ru.nsignals += p->p_cru.nsignals;
902 	p->p_ru.nvcsw	+= p->p_cru.nvcsw;
903 	p->p_ru.nivcsw	+= p->p_cru.nivcsw;
904 	p->p_ru.sysc	+= p->p_cru.sysc;
905 	p->p_ru.ioch	+= p->p_cru.ioch;
906 
907 	p->p_stat = SZOMB;
908 	p->p_proc_flag &= ~P_PR_PTRACE;
909 	p->p_wdata = what;
910 	p->p_wcode = (char)why;
911 
912 	cdir = PTOU(p)->u_cdir;
913 	rdir = PTOU(p)->u_rdir;
914 	cwd = PTOU(p)->u_cwd;
915 
916 	ASSERT(cdir != NULL || p->p_parent == &p0);
917 
918 	/*
919 	 * Release resource controls, as they are no longer enforceable.
920 	 */
921 	rctl_set_free(p->p_rctls);
922 
923 	/*
924 	 * Decrement tk_nlwps counter for our task.max-lwps resource control.
925 	 * An extended accounting record, if that facility is active, is
926 	 * scheduled to be written.  We cannot give up task and project
927 	 * membership at this point because that would allow zombies to escape
928 	 * from the max-processes resource controls.  Zombies stay in their
929 	 * current task and project until the process table slot is released
930 	 * in freeproc().
931 	 */
932 	tk = p->p_task;
933 
934 	mutex_enter(&p->p_zone->zone_nlwps_lock);
935 	tk->tk_nlwps--;
936 	tk->tk_proj->kpj_nlwps--;
937 	p->p_zone->zone_nlwps--;
938 	mutex_exit(&p->p_zone->zone_nlwps_lock);
939 
940 	/*
941 	 * Clear the lwp directory and the lwpid hash table
942 	 * now that /proc can't bother us any more.
943 	 * We free the memory below, after dropping p->p_lock.
944 	 */
945 	lwpdir = p->p_lwpdir;
946 	lwpdir_sz = p->p_lwpdir_sz;
947 	tidhash = p->p_tidhash;
948 	tidhash_sz = p->p_tidhash_sz;
949 	ret_tidhash = p->p_ret_tidhash;
950 	p->p_lwpdir = NULL;
951 	p->p_lwpfree = NULL;
952 	p->p_lwpdir_sz = 0;
953 	p->p_tidhash = NULL;
954 	p->p_tidhash_sz = 0;
955 	p->p_ret_tidhash = NULL;
956 
957 	/*
958 	 * If the process has context ops installed, call the exit routine
959 	 * on behalf of this last remaining thread. Normally exitpctx() is
960 	 * called during thread_exit() or lwp_exit(), but because this is the
961 	 * last thread in the process, we must call it here. By the time
962 	 * thread_exit() is called (below), the association with the relevant
963 	 * process has been lost.
964 	 *
965 	 * We also free the context here.
966 	 */
967 	if (p->p_pctx) {
968 		kpreempt_disable();
969 		exitpctx(p);
970 		kpreempt_enable();
971 
972 		freepctx(p, 0);
973 	}
974 
975 	/*
976 	 * curthread's proc pointer is changed to point to the 'sched'
977 	 * process for the corresponding zone, except in the case when
978 	 * the exiting process is in fact a zsched instance, in which
979 	 * case the proc pointer is set to p0.  We do so, so that the
980 	 * process still points at the right zone when we call the VN_RELE()
981 	 * below.
982 	 *
983 	 * This is because curthread's original proc pointer can be freed as
984 	 * soon as the child sends a SIGCLD to its parent.  We use zsched so
985 	 * that for user processes, even in the final moments of death, the
986 	 * process is still associated with its zone.
987 	 */
988 	if (p != t->t_procp->p_zone->zone_zsched)
989 		t->t_procp = t->t_procp->p_zone->zone_zsched;
990 	else
991 		t->t_procp = &p0;
992 
993 	mutex_exit(&p->p_lock);
994 	if (!evaporate) {
995 		p->p_pidflag &= ~CLDPEND;
996 		sigcld(p, sqp);
997 	} else {
998 		/*
999 		 * Do what sigcld() would do if the disposition
1000 		 * of the SIGCHLD signal were set to be ignored.
1001 		 */
1002 		cv_broadcast(&p->p_srwchan_cv);
1003 		freeproc(p);
1004 	}
1005 	mutex_exit(&pidlock);
1006 
1007 	/*
1008 	 * We don't release u_cdir and u_rdir until SZOMB is set.
1009 	 * This protects us against dofusers().
1010 	 */
1011 	if (cdir)
1012 		VN_RELE(cdir);
1013 	if (rdir)
1014 		VN_RELE(rdir);
1015 	if (cwd)
1016 		refstr_rele(cwd);
1017 
1018 	/*
1019 	 * task_rele() may ultimately cause the zone to go away (or
1020 	 * may cause the last user process in a zone to go away, which
1021 	 * signals zsched to go away).  So prior to this call, we must
1022 	 * no longer point at zsched.
1023 	 */
1024 	t->t_procp = &p0;
1025 
1026 	kmem_free(lwpdir, lwpdir_sz * sizeof (lwpdir_t));
1027 	kmem_free(tidhash, tidhash_sz * sizeof (tidhash_t));
1028 	while (ret_tidhash != NULL) {
1029 		ret_tidhash_t *next = ret_tidhash->rth_next;
1030 		kmem_free(ret_tidhash->rth_tidhash,
1031 		    ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
1032 		kmem_free(ret_tidhash, sizeof (*ret_tidhash));
1033 		ret_tidhash = next;
1034 	}
1035 
1036 	thread_exit();
1037 	/* NOTREACHED */
1038 }
1039 
1040 /*
1041  * Format siginfo structure for wait system calls.
1042  */
1043 void
1044 winfo(proc_t *pp, k_siginfo_t *ip, int waitflag)
1045 {
1046 	ASSERT(MUTEX_HELD(&pidlock));
1047 
1048 	bzero(ip, sizeof (k_siginfo_t));
1049 	ip->si_signo = SIGCLD;
1050 	ip->si_code = pp->p_wcode;
1051 	ip->si_pid = pp->p_pid;
1052 	ip->si_ctid = PRCTID(pp);
1053 	ip->si_zoneid = pp->p_zone->zone_id;
1054 	ip->si_status = pp->p_wdata;
1055 	ip->si_stime = pp->p_stime;
1056 	ip->si_utime = pp->p_utime;
1057 
1058 	if (waitflag) {
1059 		pp->p_wcode = 0;
1060 		pp->p_wdata = 0;
1061 		pp->p_pidflag &= ~CLDPEND;
1062 	}
1063 }
1064 
1065 /*
1066  * Wait system call.
1067  * Search for a terminated (zombie) child,
1068  * finally lay it to rest, and collect its status.
1069  * Look also for stopped children,
1070  * and pass back status from them.
1071  */
1072 int
1073 waitid(idtype_t idtype, id_t id, k_siginfo_t *ip, int options)
1074 {
1075 	int found;
1076 	proc_t *cp, *pp;
1077 	int proc_gone;
1078 	int waitflag = !(options & WNOWAIT);
1079 
1080 	/*
1081 	 * Obsolete flag, defined here only for binary compatibility
1082 	 * with old statically linked executables.  Delete this when
1083 	 * we no longer care about these old and broken applications.
1084 	 */
1085 #define	_WNOCHLD	0400
1086 	options &= ~_WNOCHLD;
1087 
1088 	if (options == 0 || (options & ~WOPTMASK))
1089 		return (EINVAL);
1090 
1091 	switch (idtype) {
1092 	case P_PID:
1093 	case P_PGID:
1094 		if (id < 0 || id >= maxpid)
1095 			return (EINVAL);
1096 		/* FALLTHROUGH */
1097 	case P_ALL:
1098 		break;
1099 	default:
1100 		return (EINVAL);
1101 	}
1102 
1103 	pp = ttoproc(curthread);
1104 
1105 	/*
1106 	 * lock parent mutex so that sibling chain can be searched.
1107 	 */
1108 	mutex_enter(&pidlock);
1109 
1110 	/*
1111 	 * if we are only looking for exited processes and child_ns list
1112 	 * is empty no reason to look at all children.
1113 	 */
1114 	if (idtype == P_ALL &&
1115 	    (options & ~WNOWAIT) == (WNOHANG | WEXITED) &&
1116 	    pp->p_child_ns == NULL) {
1117 		if (pp->p_child) {
1118 			mutex_exit(&pidlock);
1119 			bzero(ip, sizeof (k_siginfo_t));
1120 			return (0);
1121 		}
1122 		mutex_exit(&pidlock);
1123 		return (ECHILD);
1124 	}
1125 
1126 	while (pp->p_child != NULL) {
1127 
1128 		proc_gone = 0;
1129 
1130 		for (cp = pp->p_child_ns; cp != NULL; cp = cp->p_sibling_ns) {
1131 			if (idtype != P_PID && (cp->p_pidflag & CLDWAITPID))
1132 				continue;
1133 			if (idtype == P_PID && id != cp->p_pid)
1134 				continue;
1135 			if (idtype == P_PGID && id != cp->p_pgrp)
1136 				continue;
1137 
1138 			switch (cp->p_wcode) {
1139 
1140 			case CLD_TRAPPED:
1141 			case CLD_STOPPED:
1142 			case CLD_CONTINUED:
1143 				cmn_err(CE_PANIC,
1144 				    "waitid: wrong state %d on the p_newstate"
1145 				    " list", cp->p_wcode);
1146 				break;
1147 
1148 			case CLD_EXITED:
1149 			case CLD_DUMPED:
1150 			case CLD_KILLED:
1151 				if (!(options & WEXITED)) {
1152 					/*
1153 					 * Count how many are already gone
1154 					 * for good.
1155 					 */
1156 					proc_gone++;
1157 					break;
1158 				}
1159 				if (!waitflag) {
1160 					winfo(cp, ip, 0);
1161 				} else {
1162 					winfo(cp, ip, 1);
1163 					freeproc(cp);
1164 				}
1165 				mutex_exit(&pidlock);
1166 				if (waitflag) {		/* accept SIGCLD */
1167 					sigcld_delete(ip);
1168 					sigcld_repost();
1169 				}
1170 				return (0);
1171 			}
1172 
1173 			if (idtype == P_PID)
1174 				break;
1175 		}
1176 
1177 		/*
1178 		 * Wow! None of the threads on the p_sibling_ns list were
1179 		 * interesting threads. Check all the kids!
1180 		 */
1181 		found = 0;
1182 		for (cp = pp->p_child; cp != NULL; cp = cp->p_sibling) {
1183 			if (idtype == P_PID && id != cp->p_pid)
1184 				continue;
1185 			if (idtype == P_PGID && id != cp->p_pgrp)
1186 				continue;
1187 
1188 			switch (cp->p_wcode) {
1189 			case CLD_TRAPPED:
1190 				if (!(options & WTRAPPED))
1191 					break;
1192 				winfo(cp, ip, waitflag);
1193 				mutex_exit(&pidlock);
1194 				if (waitflag) {		/* accept SIGCLD */
1195 					sigcld_delete(ip);
1196 					sigcld_repost();
1197 				}
1198 				return (0);
1199 
1200 			case CLD_STOPPED:
1201 				if (!(options & WSTOPPED))
1202 					break;
1203 				/* Is it still stopped? */
1204 				mutex_enter(&cp->p_lock);
1205 				if (!jobstopped(cp)) {
1206 					mutex_exit(&cp->p_lock);
1207 					break;
1208 				}
1209 				mutex_exit(&cp->p_lock);
1210 				winfo(cp, ip, waitflag);
1211 				mutex_exit(&pidlock);
1212 				if (waitflag) {		/* accept SIGCLD */
1213 					sigcld_delete(ip);
1214 					sigcld_repost();
1215 				}
1216 				return (0);
1217 
1218 			case CLD_CONTINUED:
1219 				if (!(options & WCONTINUED))
1220 					break;
1221 				winfo(cp, ip, waitflag);
1222 				mutex_exit(&pidlock);
1223 				if (waitflag) {		/* accept SIGCLD */
1224 					sigcld_delete(ip);
1225 					sigcld_repost();
1226 				}
1227 				return (0);
1228 
1229 			case CLD_EXITED:
1230 			case CLD_DUMPED:
1231 			case CLD_KILLED:
1232 				if (idtype != P_PID &&
1233 				    (cp->p_pidflag & CLDWAITPID))
1234 					continue;
1235 				/*
1236 				 * Don't complain if a process was found in
1237 				 * the first loop but we broke out of the loop
1238 				 * because of the arguments passed to us.
1239 				 */
1240 				if (proc_gone == 0) {
1241 					cmn_err(CE_PANIC,
1242 					    "waitid: wrong state on the"
1243 					    " p_child list");
1244 				} else {
1245 					break;
1246 				}
1247 			}
1248 
1249 			found++;
1250 
1251 			if (idtype == P_PID)
1252 				break;
1253 		}
1254 
1255 		/*
1256 		 * If we found no interesting processes at all,
1257 		 * break out and return ECHILD.
1258 		 */
1259 		if (found + proc_gone == 0)
1260 			break;
1261 
1262 		if (options & WNOHANG) {
1263 			mutex_exit(&pidlock);
1264 			bzero(ip, sizeof (k_siginfo_t));
1265 			/*
1266 			 * We should set ip->si_signo = SIGCLD,
1267 			 * but there is an SVVS test that expects
1268 			 * ip->si_signo to be zero in this case.
1269 			 */
1270 			return (0);
1271 		}
1272 
1273 		/*
1274 		 * If we found no processes of interest that could
1275 		 * change state while we wait, we don't wait at all.
1276 		 * Get out with ECHILD according to SVID.
1277 		 */
1278 		if (found == proc_gone)
1279 			break;
1280 
1281 		if (!cv_wait_sig_swap(&pp->p_cv, &pidlock)) {
1282 			mutex_exit(&pidlock);
1283 			return (EINTR);
1284 		}
1285 	}
1286 	mutex_exit(&pidlock);
1287 	return (ECHILD);
1288 }
1289 
1290 int
1291 waitsys(idtype_t idtype, id_t id, siginfo_t *infop, int options)
1292 {
1293 	int error;
1294 	k_siginfo_t info;
1295 
1296 	if (error = waitid(idtype, id, &info, options))
1297 		return (set_errno(error));
1298 	if (copyout(&info, infop, sizeof (k_siginfo_t)))
1299 		return (set_errno(EFAULT));
1300 	return (0);
1301 }
1302 
1303 #ifdef _SYSCALL32_IMPL
1304 
1305 int
1306 waitsys32(idtype_t idtype, id_t id, siginfo_t *infop, int options)
1307 {
1308 	int error;
1309 	k_siginfo_t info;
1310 	siginfo32_t info32;
1311 
1312 	if (error = waitid(idtype, id, &info, options))
1313 		return (set_errno(error));
1314 	siginfo_kto32(&info, &info32);
1315 	if (copyout(&info32, infop, sizeof (info32)))
1316 		return (set_errno(EFAULT));
1317 	return (0);
1318 }
1319 
1320 #endif	/* _SYSCALL32_IMPL */
1321 
1322 void
1323 proc_detach(proc_t *p)
1324 {
1325 	proc_t *q;
1326 
1327 	ASSERT(MUTEX_HELD(&pidlock));
1328 
1329 	q = p->p_parent;
1330 	ASSERT(q != NULL);
1331 
1332 	/*
1333 	 * Take it off the newstate list of its parent
1334 	 */
1335 	delete_ns(q, p);
1336 
1337 	if (q->p_child == p) {
1338 		q->p_child = p->p_sibling;
1339 		/*
1340 		 * If the parent has no children, it better not
1341 		 * have any with new states either!
1342 		 */
1343 		ASSERT(q->p_child ? 1 : q->p_child_ns == NULL);
1344 	}
1345 
1346 	if (p->p_sibling) {
1347 		p->p_sibling->p_psibling = p->p_psibling;
1348 	}
1349 
1350 	if (p->p_psibling) {
1351 		p->p_psibling->p_sibling = p->p_sibling;
1352 	}
1353 }
1354 
1355 /*
1356  * Remove zombie children from the process table.
1357  */
1358 void
1359 freeproc(proc_t *p)
1360 {
1361 	proc_t *q;
1362 	task_t *tk;
1363 
1364 	ASSERT(p->p_stat == SZOMB);
1365 	ASSERT(p->p_tlist == NULL);
1366 	ASSERT(MUTEX_HELD(&pidlock));
1367 
1368 	sigdelq(p, NULL, 0);
1369 	if (p->p_killsqp) {
1370 		siginfofree(p->p_killsqp);
1371 		p->p_killsqp = NULL;
1372 	}
1373 
1374 	prfree(p);	/* inform /proc */
1375 
1376 	/*
1377 	 * Don't free the init processes.
1378 	 * Other dying processes will access it.
1379 	 */
1380 	if (p == proc_init)
1381 		return;
1382 
1383 
1384 	/*
1385 	 * We wait until now to free the cred structure because a
1386 	 * zombie process's credentials may be examined by /proc.
1387 	 * No cred locking needed because there are no threads at this point.
1388 	 */
1389 	upcount_dec(crgetruid(p->p_cred), crgetzoneid(p->p_cred));
1390 	crfree(p->p_cred);
1391 	if (p->p_corefile != NULL) {
1392 		corectl_path_rele(p->p_corefile);
1393 		p->p_corefile = NULL;
1394 	}
1395 	if (p->p_content != NULL) {
1396 		corectl_content_rele(p->p_content);
1397 		p->p_content = NULL;
1398 	}
1399 
1400 	if (p->p_nextofkin && !((p->p_nextofkin->p_flag & SNOWAIT) ||
1401 	    (PTOU(p->p_nextofkin)->u_signal[SIGCLD - 1] == SIG_IGN))) {
1402 		/*
1403 		 * This should still do the right thing since p_utime/stime
1404 		 * get set to the correct value on process exit, so it
1405 		 * should get properly updated
1406 		 */
1407 		p->p_nextofkin->p_cutime += p->p_utime;
1408 		p->p_nextofkin->p_cstime += p->p_stime;
1409 
1410 		p->p_nextofkin->p_cacct[LMS_USER] += p->p_acct[LMS_USER];
1411 		p->p_nextofkin->p_cacct[LMS_SYSTEM] += p->p_acct[LMS_SYSTEM];
1412 		p->p_nextofkin->p_cacct[LMS_TRAP] += p->p_acct[LMS_TRAP];
1413 		p->p_nextofkin->p_cacct[LMS_TFAULT] += p->p_acct[LMS_TFAULT];
1414 		p->p_nextofkin->p_cacct[LMS_DFAULT] += p->p_acct[LMS_DFAULT];
1415 		p->p_nextofkin->p_cacct[LMS_KFAULT] += p->p_acct[LMS_KFAULT];
1416 		p->p_nextofkin->p_cacct[LMS_USER_LOCK]
1417 		    += p->p_acct[LMS_USER_LOCK];
1418 		p->p_nextofkin->p_cacct[LMS_SLEEP] += p->p_acct[LMS_SLEEP];
1419 		p->p_nextofkin->p_cacct[LMS_WAIT_CPU]
1420 		    += p->p_acct[LMS_WAIT_CPU];
1421 		p->p_nextofkin->p_cacct[LMS_STOPPED] += p->p_acct[LMS_STOPPED];
1422 
1423 		p->p_nextofkin->p_cru.minflt	+= p->p_ru.minflt;
1424 		p->p_nextofkin->p_cru.majflt	+= p->p_ru.majflt;
1425 		p->p_nextofkin->p_cru.nswap	+= p->p_ru.nswap;
1426 		p->p_nextofkin->p_cru.inblock	+= p->p_ru.inblock;
1427 		p->p_nextofkin->p_cru.oublock	+= p->p_ru.oublock;
1428 		p->p_nextofkin->p_cru.msgsnd	+= p->p_ru.msgsnd;
1429 		p->p_nextofkin->p_cru.msgrcv	+= p->p_ru.msgrcv;
1430 		p->p_nextofkin->p_cru.nsignals	+= p->p_ru.nsignals;
1431 		p->p_nextofkin->p_cru.nvcsw	+= p->p_ru.nvcsw;
1432 		p->p_nextofkin->p_cru.nivcsw	+= p->p_ru.nivcsw;
1433 		p->p_nextofkin->p_cru.sysc	+= p->p_ru.sysc;
1434 		p->p_nextofkin->p_cru.ioch	+= p->p_ru.ioch;
1435 
1436 	}
1437 
1438 	q = p->p_nextofkin;
1439 	if (q && q->p_orphan == p)
1440 		q->p_orphan = p->p_nextorph;
1441 	else if (q) {
1442 		for (q = q->p_orphan; q; q = q->p_nextorph)
1443 			if (q->p_nextorph == p)
1444 				break;
1445 		ASSERT(q && q->p_nextorph == p);
1446 		q->p_nextorph = p->p_nextorph;
1447 	}
1448 
1449 	/*
1450 	 * The process table slot is being freed, so it is now safe to give up
1451 	 * task and project membership.
1452 	 */
1453 	mutex_enter(&p->p_lock);
1454 	tk = p->p_task;
1455 	task_detach(p);
1456 	mutex_exit(&p->p_lock);
1457 
1458 	proc_detach(p);
1459 	pid_exit(p, tk);	/* frees pid and proc structure */
1460 
1461 	task_rele(tk);
1462 }
1463 
1464 /*
1465  * Delete process "child" from the newstate list of process "parent"
1466  */
1467 void
1468 delete_ns(proc_t *parent, proc_t *child)
1469 {
1470 	proc_t **ns;
1471 
1472 	ASSERT(MUTEX_HELD(&pidlock));
1473 	ASSERT(child->p_parent == parent);
1474 	for (ns = &parent->p_child_ns; *ns != NULL; ns = &(*ns)->p_sibling_ns) {
1475 		if (*ns == child) {
1476 
1477 			ASSERT((*ns)->p_parent == parent);
1478 
1479 			*ns = child->p_sibling_ns;
1480 			child->p_sibling_ns = NULL;
1481 			return;
1482 		}
1483 	}
1484 }
1485 
1486 /*
1487  * Add process "child" to the new state list of process "parent"
1488  */
1489 void
1490 add_ns(proc_t *parent, proc_t *child)
1491 {
1492 	ASSERT(child->p_sibling_ns == NULL);
1493 	child->p_sibling_ns = parent->p_child_ns;
1494 	parent->p_child_ns = child;
1495 }
1496