xref: /titanic_44/usr/src/uts/common/os/fork.c (revision c1500db945971ce16b2509a64fc04cd590779a2e)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/sysmacros.h>
35 #include <sys/signal.h>
36 #include <sys/cred.h>
37 #include <sys/policy.h>
38 #include <sys/user.h>
39 #include <sys/systm.h>
40 #include <sys/cpuvar.h>
41 #include <sys/vfs.h>
42 #include <sys/vnode.h>
43 #include <sys/file.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/proc.h>
47 #include <sys/cmn_err.h>
48 #include <sys/acct.h>
49 #include <sys/tuneable.h>
50 #include <sys/class.h>
51 #include <sys/kmem.h>
52 #include <sys/session.h>
53 #include <sys/ucontext.h>
54 #include <sys/stack.h>
55 #include <sys/procfs.h>
56 #include <sys/prsystm.h>
57 #include <sys/vmsystm.h>
58 #include <sys/vtrace.h>
59 #include <sys/debug.h>
60 #include <sys/shm_impl.h>
61 #include <sys/door_data.h>
62 #include <vm/as.h>
63 #include <vm/rm.h>
64 #include <c2/audit.h>
65 #include <sys/var.h>
66 #include <sys/schedctl.h>
67 #include <sys/utrap.h>
68 #include <sys/task.h>
69 #include <sys/resource.h>
70 #include <sys/cyclic.h>
71 #include <sys/lgrp.h>
72 #include <sys/rctl.h>
73 #include <sys/contract_impl.h>
74 #include <sys/contract/process_impl.h>
75 #include <sys/list.h>
76 #include <sys/dtrace.h>
77 #include <sys/pool.h>
78 #include <sys/zone.h>
79 #include <sys/sdt.h>
80 #include <sys/class.h>
81 #include <sys/corectl.h>
82 #include <sys/brand.h>
83 #include <sys/fork.h>
84 
85 static int64_t cfork(int, int, int);
86 static int getproc(proc_t **, int);
87 static void fork_fail(proc_t *);
88 static void forklwp_fail(proc_t *);
89 
90 int fork_fail_pending;
91 
92 extern struct kmem_cache *process_cache;
93 
94 /*
95  * forkall system call.
96  */
97 int64_t
98 forkall(void)
99 {
100 	return (cfork(0, 0, 0));
101 }
102 
103 /*
104  * The parent is stopped until the child invokes relvm().
105  */
106 int64_t
107 vfork(void)
108 {
109 	curthread->t_post_sys = 1;	/* so vfwait() will be called */
110 	return (cfork(1, 1, 0));
111 }
112 
113 /*
114  * fork system call, aka fork1.
115  */
116 int64_t
117 fork1(void)
118 {
119 	return (cfork(0, 1, 0));
120 }
121 
122 /*
123  * The forkall(), vfork(), and fork1() system calls are no longer
124  * invoked by libc.  They are retained only for the benefit of
125  * old statically-linked applications.  They should be eliminated
126  * when we no longer care about such old and broken applications.
127  */
128 
129 /*
130  * forksys system call - forkx, forkallx, vforkx.
131  * This is the interface now invoked by libc.
132  */
133 int64_t
134 forksys(int subcode, int flags)
135 {
136 	switch (subcode) {
137 	case 0:
138 		return (cfork(0, 1, flags));	/* forkx(flags) */
139 	case 1:
140 		return (cfork(0, 0, flags));	/* forkallx(flags) */
141 	case 2:
142 		curthread->t_post_sys = 1;	/* so vfwait() will be called */
143 		return (cfork(1, 1, flags));	/* vforkx(flags) */
144 	default:
145 		return ((int64_t)set_errno(EINVAL));
146 	}
147 }
148 
149 /* ARGSUSED */
150 static int64_t
151 cfork(int isvfork, int isfork1, int flags)
152 {
153 	proc_t *p = ttoproc(curthread);
154 	struct as *as;
155 	proc_t *cp, **orphpp;
156 	klwp_t *clone;
157 	kthread_t *t;
158 	task_t *tk;
159 	rval_t	r;
160 	int error;
161 	int i;
162 	rctl_set_t *dup_set;
163 	rctl_alloc_gp_t *dup_gp;
164 	rctl_entity_p_t e;
165 	lwpdir_t *ldp;
166 	lwpent_t *lep;
167 	lwpent_t *clep;
168 
169 	/*
170 	 * Allow only these two flags.
171 	 */
172 	if ((flags & ~(FORK_NOSIGCHLD | FORK_WAITPID)) != 0) {
173 		error = EINVAL;
174 		goto forkerr;
175 	}
176 
177 	/*
178 	 * fork is not supported for the /proc agent lwp.
179 	 */
180 	if (curthread == p->p_agenttp) {
181 		error = ENOTSUP;
182 		goto forkerr;
183 	}
184 
185 	if ((error = secpolicy_basic_fork(CRED())) != 0)
186 		goto forkerr;
187 
188 	/*
189 	 * If the calling lwp is doing a fork1() then the
190 	 * other lwps in this process are not duplicated and
191 	 * don't need to be held where their kernel stacks can be
192 	 * cloned.  If doing forkall(), the process is held with
193 	 * SHOLDFORK, so that the lwps are at a point where their
194 	 * stacks can be copied which is on entry or exit from
195 	 * the kernel.
196 	 */
197 	if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) {
198 		aston(curthread);
199 		error = EINTR;
200 		goto forkerr;
201 	}
202 
203 #if defined(__sparc)
204 	/*
205 	 * Ensure that the user stack is fully constructed
206 	 * before creating the child process structure.
207 	 */
208 	(void) flush_user_windows_to_stack(NULL);
209 #endif
210 
211 	mutex_enter(&p->p_lock);
212 	/*
213 	 * If this is vfork(), cancel any suspend request we might
214 	 * have gotten from some other thread via lwp_suspend().
215 	 * Otherwise we could end up with a deadlock on return
216 	 * from the vfork() in both the parent and the child.
217 	 */
218 	if (isvfork)
219 		curthread->t_proc_flag &= ~TP_HOLDLWP;
220 	/*
221 	 * Prevent our resource set associations from being changed during fork.
222 	 */
223 	pool_barrier_enter();
224 	mutex_exit(&p->p_lock);
225 
226 	/*
227 	 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes.
228 	 */
229 	if (getproc(&cp, 0) < 0) {
230 		mutex_enter(&p->p_lock);
231 		pool_barrier_exit();
232 		continuelwps(p);
233 		mutex_exit(&p->p_lock);
234 		error = EAGAIN;
235 		goto forkerr;
236 	}
237 
238 	TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p);
239 
240 	/*
241 	 * Assign an address space to child
242 	 */
243 	if (isvfork) {
244 		/*
245 		 * Clear any watched areas and remember the
246 		 * watched pages for restoring in vfwait().
247 		 */
248 		as = p->p_as;
249 		if (avl_numnodes(&as->a_wpage) != 0) {
250 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
251 			as_clearwatch(as);
252 			p->p_wpage = as->a_wpage;
253 			avl_create(&as->a_wpage, wp_compare,
254 			    sizeof (struct watched_page),
255 			    offsetof(struct watched_page, wp_link));
256 			AS_LOCK_EXIT(as, &as->a_lock);
257 		}
258 		cp->p_as = as;
259 		cp->p_flag |= SVFORK;
260 	} else {
261 		/*
262 		 * We need to hold P_PR_LOCK until the address space has
263 		 * been duplicated and we've had a chance to remove from the
264 		 * child any DTrace probes that were in the parent. Holding
265 		 * P_PR_LOCK prevents any new probes from being added and any
266 		 * extant probes from being removed.
267 		 */
268 		mutex_enter(&p->p_lock);
269 		sprlock_proc(p);
270 		p->p_flag |= SFORKING;
271 		mutex_exit(&p->p_lock);
272 
273 		error = as_dup(p->p_as, &cp->p_as);
274 		if (error != 0) {
275 			mutex_enter(&p->p_lock);
276 			sprunlock(p);
277 			fork_fail(cp);
278 			mutex_enter(&pidlock);
279 			orphpp = &p->p_orphan;
280 			while (*orphpp != cp)
281 				orphpp = &(*orphpp)->p_nextorph;
282 			*orphpp = cp->p_nextorph;
283 			if (p->p_child == cp)
284 				p->p_child = cp->p_sibling;
285 			if (cp->p_sibling)
286 				cp->p_sibling->p_psibling = cp->p_psibling;
287 			if (cp->p_psibling)
288 				cp->p_psibling->p_sibling = cp->p_sibling;
289 			mutex_enter(&cp->p_lock);
290 			tk = cp->p_task;
291 			task_detach(cp);
292 			ASSERT(cp->p_pool->pool_ref > 0);
293 			atomic_add_32(&cp->p_pool->pool_ref, -1);
294 			mutex_exit(&cp->p_lock);
295 			pid_exit(cp);
296 			mutex_exit(&pidlock);
297 			task_rele(tk);
298 
299 			mutex_enter(&p->p_lock);
300 			p->p_flag &= ~SFORKING;
301 			pool_barrier_exit();
302 			continuelwps(p);
303 			mutex_exit(&p->p_lock);
304 			/*
305 			 * Preserve ENOMEM error condition but
306 			 * map all others to EAGAIN.
307 			 */
308 			error = (error == ENOMEM) ? ENOMEM : EAGAIN;
309 			goto forkerr;
310 		}
311 		cp->p_as->a_proc = cp;
312 
313 		/* Duplicate parent's shared memory */
314 		if (p->p_segacct)
315 			shmfork(p, cp);
316 
317 		/*
318 		 * Remove all DTrace tracepoints from the child process. We
319 		 * need to do this _before_ duplicating USDT providers since
320 		 * any associated probes may be immediately enabled.
321 		 */
322 		if (p->p_dtrace_count > 0)
323 			dtrace_fasttrap_fork(p, cp);
324 
325 		/*
326 		 * Duplicate any helper actions and providers. The SFORKING
327 		 * we set above informs the code to enable USDT probes that
328 		 * sprlock() may fail because the child is being forked.
329 		 */
330 		if (p->p_dtrace_helpers != NULL) {
331 			ASSERT(dtrace_helpers_fork != NULL);
332 			(*dtrace_helpers_fork)(p, cp);
333 		}
334 
335 		mutex_enter(&p->p_lock);
336 		p->p_flag &= ~SFORKING;
337 		sprunlock(p);
338 	}
339 
340 	/*
341 	 * Duplicate parent's resource controls.
342 	 */
343 	dup_set = rctl_set_create();
344 	for (;;) {
345 		dup_gp = rctl_set_dup_prealloc(p->p_rctls);
346 		mutex_enter(&p->p_rctls->rcs_lock);
347 		if (rctl_set_dup_ready(p->p_rctls, dup_gp))
348 			break;
349 		mutex_exit(&p->p_rctls->rcs_lock);
350 		rctl_prealloc_destroy(dup_gp);
351 	}
352 	e.rcep_p.proc = cp;
353 	e.rcep_t = RCENTITY_PROCESS;
354 	cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp,
355 	    RCD_DUP | RCD_CALLBACK);
356 	mutex_exit(&p->p_rctls->rcs_lock);
357 
358 	rctl_prealloc_destroy(dup_gp);
359 
360 	/*
361 	 * Allocate the child's lwp directory and lwpid hash table.
362 	 */
363 	if (isfork1)
364 		cp->p_lwpdir_sz = 2;
365 	else
366 		cp->p_lwpdir_sz = p->p_lwpdir_sz;
367 	cp->p_lwpdir = cp->p_lwpfree = ldp =
368 		kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP);
369 	for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++)
370 		ldp->ld_next = ldp + 1;
371 	cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2;
372 	cp->p_tidhash =
373 		kmem_zalloc(cp->p_tidhash_sz * sizeof (lwpdir_t *), KM_SLEEP);
374 
375 	/*
376 	 * Duplicate parent's lwps.
377 	 * Mutual exclusion is not needed because the process is
378 	 * in the hold state and only the current lwp is running.
379 	 */
380 	klgrpset_clear(cp->p_lgrpset);
381 	if (isfork1) {
382 		clone = forklwp(ttolwp(curthread), cp, curthread->t_tid);
383 		if (clone == NULL)
384 			goto forklwperr;
385 		/*
386 		 * Inherit only the lwp_wait()able flag,
387 		 * Daemon threads should not call fork1(), but oh well...
388 		 */
389 		lwptot(clone)->t_proc_flag |=
390 			(curthread->t_proc_flag & TP_TWAIT);
391 	} else {
392 		/* this is forkall(), no one can be in lwp_wait() */
393 		ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0);
394 		/* for each entry in the parent's lwp directory... */
395 		for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) {
396 			klwp_t *clwp;
397 			kthread_t *ct;
398 
399 			if ((lep = ldp->ld_entry) == NULL)
400 				continue;
401 
402 			if ((t = lep->le_thread) != NULL) {
403 				clwp = forklwp(ttolwp(t), cp, t->t_tid);
404 				if (clwp == NULL)
405 					goto forklwperr;
406 				ct = lwptot(clwp);
407 				/*
408 				 * Inherit lwp_wait()able and daemon flags.
409 				 */
410 				ct->t_proc_flag |=
411 				    (t->t_proc_flag & (TP_TWAIT|TP_DAEMON));
412 				/*
413 				 * Keep track of the clone of curthread to
414 				 * post return values through lwp_setrval().
415 				 * Mark other threads for special treatment
416 				 * by lwp_rtt() / post_syscall().
417 				 */
418 				if (t == curthread)
419 					clone = clwp;
420 				else
421 					ct->t_flag |= T_FORKALL;
422 			} else {
423 				/*
424 				 * Replicate zombie lwps in the child.
425 				 */
426 				clep = kmem_zalloc(sizeof (*clep), KM_SLEEP);
427 				clep->le_lwpid = lep->le_lwpid;
428 				clep->le_start = lep->le_start;
429 				lwp_hash_in(cp, clep);
430 			}
431 		}
432 	}
433 
434 	/*
435 	 * Put new process in the parent's process contract, or put it
436 	 * in a new one if there is an active process template.  Send a
437 	 * fork event (if requested) to whatever contract the child is
438 	 * a member of.  Fails if the parent has been SIGKILLed.
439 	 */
440 	if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL)
441 		goto forklwperr;
442 
443 	/*
444 	 * No fork failures occur beyond this point.
445 	 */
446 
447 	cp->p_lwpid = p->p_lwpid;
448 	if (!isfork1) {
449 		cp->p_lwpdaemon = p->p_lwpdaemon;
450 		cp->p_zombcnt = p->p_zombcnt;
451 		/*
452 		 * If the parent's lwp ids have wrapped around, so have the
453 		 * child's.
454 		 */
455 		cp->p_flag |= p->p_flag & SLWPWRAP;
456 	}
457 
458 	mutex_enter(&p->p_lock);
459 	corectl_path_hold(cp->p_corefile = p->p_corefile);
460 	corectl_content_hold(cp->p_content = p->p_content);
461 	mutex_exit(&p->p_lock);
462 
463 	/*
464 	 * Duplicate process context ops, if any.
465 	 */
466 	if (p->p_pctx)
467 		forkpctx(p, cp);
468 
469 #ifdef __sparc
470 	utrap_dup(p, cp);
471 #endif
472 	/*
473 	 * If the child process has been marked to stop on exit
474 	 * from this fork, arrange for all other lwps to stop in
475 	 * sympathy with the active lwp.
476 	 */
477 	if (PTOU(cp)->u_systrap &&
478 	    prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) {
479 		mutex_enter(&cp->p_lock);
480 		t = cp->p_tlist;
481 		do {
482 			t->t_proc_flag |= TP_PRSTOP;
483 			aston(t);	/* so TP_PRSTOP will be seen */
484 		} while ((t = t->t_forw) != cp->p_tlist);
485 		mutex_exit(&cp->p_lock);
486 	}
487 	/*
488 	 * If the parent process has been marked to stop on exit
489 	 * from this fork, and its asynchronous-stop flag has not
490 	 * been set, arrange for all other lwps to stop before
491 	 * they return back to user level.
492 	 */
493 	if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap &&
494 	    prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) {
495 		mutex_enter(&p->p_lock);
496 		t = p->p_tlist;
497 		do {
498 			t->t_proc_flag |= TP_PRSTOP;
499 			aston(t);	/* so TP_PRSTOP will be seen */
500 		} while ((t = t->t_forw) != p->p_tlist);
501 		mutex_exit(&p->p_lock);
502 	}
503 
504 	if (PROC_IS_BRANDED(p))
505 		BROP(p)->b_lwp_setrval(clone, p->p_pid, 1);
506 	else
507 		lwp_setrval(clone, p->p_pid, 1);
508 
509 	/* set return values for parent */
510 	r.r_val1 = (int)cp->p_pid;
511 	r.r_val2 = 0;
512 
513 	/*
514 	 * pool_barrier_exit() can now be called because the child process has:
515 	 * - all identifying features cloned or set (p_pid, p_task, p_pool)
516 	 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset)
517 	 * - any other fields set which are used in resource set binding.
518 	 */
519 	mutex_enter(&p->p_lock);
520 	pool_barrier_exit();
521 	mutex_exit(&p->p_lock);
522 
523 	mutex_enter(&pidlock);
524 	mutex_enter(&cp->p_lock);
525 
526 	/*
527 	 * Set flags telling the child what (not) to do on exit.
528 	 */
529 	if (flags & FORK_NOSIGCHLD)
530 		cp->p_pidflag |= CLDNOSIGCHLD;
531 	if (flags & FORK_WAITPID)
532 		cp->p_pidflag |= CLDWAITPID;
533 
534 	/*
535 	 * Now that there are lwps and threads attached, add the new
536 	 * process to the process group.
537 	 */
538 	pgjoin(cp, p->p_pgidp);
539 	cp->p_stat = SRUN;
540 	/*
541 	 * We are now done with all the lwps in the child process.
542 	 */
543 	t = cp->p_tlist;
544 	do {
545 		/*
546 		 * Set the lwp_suspend()ed lwps running.
547 		 * They will suspend properly at syscall exit.
548 		 */
549 		if (t->t_proc_flag & TP_HOLDLWP)
550 			lwp_create_done(t);
551 		else {
552 			/* set TS_CREATE to allow continuelwps() to work */
553 			thread_lock(t);
554 			ASSERT(t->t_state == TS_STOPPED &&
555 			    !(t->t_schedflag & (TS_CREATE|TS_CSTART)));
556 			t->t_schedflag |= TS_CREATE;
557 			thread_unlock(t);
558 		}
559 	} while ((t = t->t_forw) != cp->p_tlist);
560 	mutex_exit(&cp->p_lock);
561 
562 	if (isvfork) {
563 		CPU_STATS_ADDQ(CPU, sys, sysvfork, 1);
564 		mutex_enter(&p->p_lock);
565 		p->p_flag |= SVFWAIT;
566 		curthread->t_flag |= T_VFPARENT;
567 		DTRACE_PROC1(create, proc_t *, cp);
568 		cv_broadcast(&pr_pid_cv[p->p_slot]);	/* inform /proc */
569 		mutex_exit(&p->p_lock);
570 		/*
571 		 * Grab child's p_lock before dropping pidlock to ensure
572 		 * the process will not disappear before we set it running.
573 		 */
574 		mutex_enter(&cp->p_lock);
575 		mutex_exit(&pidlock);
576 		sigdefault(cp);
577 		continuelwps(cp);
578 		mutex_exit(&cp->p_lock);
579 	} else {
580 		CPU_STATS_ADDQ(CPU, sys, sysfork, 1);
581 		DTRACE_PROC1(create, proc_t *, cp);
582 		/*
583 		 * It is CL_FORKRET's job to drop pidlock.
584 		 * If we do it here, the process could be set running
585 		 * and disappear before CL_FORKRET() is called.
586 		 */
587 		CL_FORKRET(curthread, cp->p_tlist);
588 		ASSERT(MUTEX_NOT_HELD(&pidlock));
589 	}
590 
591 	return (r.r_vals);
592 
593 forklwperr:
594 	if (isvfork) {
595 		if (avl_numnodes(&p->p_wpage) != 0) {
596 			/* restore watchpoints to parent */
597 			as = p->p_as;
598 			AS_LOCK_ENTER(as, &as->a_lock,
599 				RW_WRITER);
600 			as->a_wpage = p->p_wpage;
601 			avl_create(&p->p_wpage, wp_compare,
602 			    sizeof (struct watched_page),
603 			    offsetof(struct watched_page, wp_link));
604 			as_setwatch(as);
605 			AS_LOCK_EXIT(as, &as->a_lock);
606 		}
607 	} else {
608 		if (cp->p_segacct)
609 			shmexit(cp);
610 		as = cp->p_as;
611 		cp->p_as = &kas;
612 		as_free(as);
613 	}
614 
615 	if (cp->p_lwpdir) {
616 		for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++)
617 			if ((lep = ldp->ld_entry) != NULL)
618 				kmem_free(lep, sizeof (*lep));
619 		kmem_free(cp->p_lwpdir,
620 		    cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir));
621 	}
622 	cp->p_lwpdir = NULL;
623 	cp->p_lwpfree = NULL;
624 	cp->p_lwpdir_sz = 0;
625 
626 	if (cp->p_tidhash)
627 		kmem_free(cp->p_tidhash,
628 		    cp->p_tidhash_sz * sizeof (*cp->p_tidhash));
629 	cp->p_tidhash = NULL;
630 	cp->p_tidhash_sz = 0;
631 
632 	forklwp_fail(cp);
633 	fork_fail(cp);
634 	rctl_set_free(cp->p_rctls);
635 	mutex_enter(&pidlock);
636 
637 	/*
638 	 * Detach failed child from task.
639 	 */
640 	mutex_enter(&cp->p_lock);
641 	tk = cp->p_task;
642 	task_detach(cp);
643 	ASSERT(cp->p_pool->pool_ref > 0);
644 	atomic_add_32(&cp->p_pool->pool_ref, -1);
645 	mutex_exit(&cp->p_lock);
646 
647 	orphpp = &p->p_orphan;
648 	while (*orphpp != cp)
649 		orphpp = &(*orphpp)->p_nextorph;
650 	*orphpp = cp->p_nextorph;
651 	if (p->p_child == cp)
652 		p->p_child = cp->p_sibling;
653 	if (cp->p_sibling)
654 		cp->p_sibling->p_psibling = cp->p_psibling;
655 	if (cp->p_psibling)
656 		cp->p_psibling->p_sibling = cp->p_sibling;
657 	pid_exit(cp);
658 	mutex_exit(&pidlock);
659 
660 	task_rele(tk);
661 
662 	mutex_enter(&p->p_lock);
663 	pool_barrier_exit();
664 	continuelwps(p);
665 	mutex_exit(&p->p_lock);
666 	error = EAGAIN;
667 forkerr:
668 	return ((int64_t)set_errno(error));
669 }
670 
671 /*
672  * Free allocated resources from getproc() if a fork failed.
673  */
674 static void
675 fork_fail(proc_t *cp)
676 {
677 	uf_info_t *fip = P_FINFO(cp);
678 
679 	fcnt_add(fip, -1);
680 	sigdelq(cp, NULL, 0);
681 
682 	mutex_enter(&pidlock);
683 	upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred));
684 	mutex_exit(&pidlock);
685 
686 	/*
687 	 * single threaded, so no locking needed here
688 	 */
689 	crfree(cp->p_cred);
690 
691 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
692 
693 	VN_RELE(PTOU(curproc)->u_cdir);
694 	if (PTOU(curproc)->u_rdir)
695 		VN_RELE(PTOU(curproc)->u_rdir);
696 	if (cp->p_exec)
697 		VN_RELE(cp->p_exec);
698 	if (cp->p_execdir)
699 		VN_RELE(cp->p_execdir);
700 	if (PTOU(curproc)->u_cwd)
701 		refstr_rele(PTOU(curproc)->u_cwd);
702 }
703 
704 /*
705  * Clean up the lwps already created for this child process.
706  * The fork failed while duplicating all the lwps of the parent
707  * and those lwps already created must be freed.
708  * This process is invisible to the rest of the system,
709  * so we don't need to hold p->p_lock to protect the list.
710  */
711 static void
712 forklwp_fail(proc_t *p)
713 {
714 	kthread_t *t;
715 	task_t *tk;
716 
717 	while ((t = p->p_tlist) != NULL) {
718 		/*
719 		 * First remove the lwp from the process's p_tlist.
720 		 */
721 		if (t != t->t_forw)
722 			p->p_tlist = t->t_forw;
723 		else
724 			p->p_tlist = NULL;
725 		p->p_lwpcnt--;
726 		t->t_forw->t_back = t->t_back;
727 		t->t_back->t_forw = t->t_forw;
728 
729 		tk = p->p_task;
730 		mutex_enter(&p->p_zone->zone_nlwps_lock);
731 		tk->tk_nlwps--;
732 		tk->tk_proj->kpj_nlwps--;
733 		p->p_zone->zone_nlwps--;
734 		mutex_exit(&p->p_zone->zone_nlwps_lock);
735 
736 		ASSERT(t->t_schedctl == NULL);
737 
738 		if (t->t_door != NULL) {
739 			kmem_free(t->t_door, sizeof (door_data_t));
740 			t->t_door = NULL;
741 		}
742 		lwp_ctmpl_clear(ttolwp(t));
743 
744 		/*
745 		 * Remove the thread from the all threads list.
746 		 * We need to hold pidlock for this.
747 		 */
748 		mutex_enter(&pidlock);
749 		t->t_next->t_prev = t->t_prev;
750 		t->t_prev->t_next = t->t_next;
751 		CL_EXIT(t);	/* tell the scheduler that we're exiting */
752 		cv_broadcast(&t->t_joincv);	/* tell anyone in thread_join */
753 		mutex_exit(&pidlock);
754 
755 		/*
756 		 * Let the lgroup load averages know that this thread isn't
757 		 * going to show up (i.e. un-do what was done on behalf of
758 		 * this thread by the earlier lgrp_move_thread()).
759 		 */
760 		kpreempt_disable();
761 		lgrp_move_thread(t, NULL, 1);
762 		kpreempt_enable();
763 
764 		/*
765 		 * The thread was created TS_STOPPED.
766 		 * We change it to TS_FREE to avoid an
767 		 * ASSERT() panic in thread_free().
768 		 */
769 		t->t_state = TS_FREE;
770 		thread_rele(t);
771 		thread_free(t);
772 	}
773 }
774 
775 extern struct as kas;
776 
777 /*
778  * fork a kernel process.
779  */
780 int
781 newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct)
782 {
783 	proc_t *p;
784 	struct user *up;
785 	klwp_t *lwp;
786 	cont_process_t *ctp = NULL;
787 	rctl_entity_p_t e;
788 
789 	ASSERT(!(cid == syscid && ct != NULL));
790 	if (cid == syscid) {
791 		rctl_alloc_gp_t *init_gp;
792 		rctl_set_t *init_set;
793 
794 		if (getproc(&p, 1) < 0)
795 			return (EAGAIN);
796 
797 		p->p_flag |= SNOWAIT;
798 		p->p_exec = NULL;
799 		p->p_execdir = NULL;
800 
801 		init_set = rctl_set_create();
802 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
803 
804 		/*
805 		 * kernel processes do not inherit /proc tracing flags.
806 		 */
807 		sigemptyset(&p->p_sigmask);
808 		premptyset(&p->p_fltmask);
809 		up = PTOU(p);
810 		up->u_systrap = 0;
811 		premptyset(&(up->u_entrymask));
812 		premptyset(&(up->u_exitmask));
813 		mutex_enter(&p->p_lock);
814 		e.rcep_p.proc = p;
815 		e.rcep_t = RCENTITY_PROCESS;
816 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
817 		    init_gp);
818 		mutex_exit(&p->p_lock);
819 
820 		rctl_prealloc_destroy(init_gp);
821 	} else  {
822 		rctl_alloc_gp_t *init_gp, *default_gp;
823 		rctl_set_t *init_set;
824 		task_t *tk, *tk_old;
825 
826 		if (getproc(&p, 0) < 0)
827 			return (EAGAIN);
828 		/*
829 		 * init creates a new task, distinct from the task
830 		 * containing kernel "processes".
831 		 */
832 		tk = task_create(0, p->p_zone);
833 		mutex_enter(&tk->tk_zone->zone_nlwps_lock);
834 		tk->tk_proj->kpj_ntasks++;
835 		mutex_exit(&tk->tk_zone->zone_nlwps_lock);
836 
837 		default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS);
838 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
839 		init_set = rctl_set_create();
840 
841 		mutex_enter(&pidlock);
842 		mutex_enter(&p->p_lock);
843 		tk_old = p->p_task;	/* switch to new task */
844 
845 		task_detach(p);
846 		task_begin(tk, p);
847 		mutex_exit(&pidlock);
848 
849 		e.rcep_p.proc = p;
850 		e.rcep_t = RCENTITY_PROCESS;
851 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
852 		    init_gp);
853 		rctlproc_default_init(p, default_gp);
854 		mutex_exit(&p->p_lock);
855 
856 		task_rele(tk_old);
857 		rctl_prealloc_destroy(default_gp);
858 		rctl_prealloc_destroy(init_gp);
859 	}
860 
861 	p->p_as = &kas;
862 
863 	if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri,
864 	    &curthread->t_hold, cid, 1)) == NULL) {
865 		task_t *tk;
866 		fork_fail(p);
867 		mutex_enter(&pidlock);
868 		mutex_enter(&p->p_lock);
869 		tk = p->p_task;
870 		task_detach(p);
871 		ASSERT(p->p_pool->pool_ref > 0);
872 		atomic_add_32(&p->p_pool->pool_ref, -1);
873 		mutex_exit(&p->p_lock);
874 		pid_exit(p);
875 		mutex_exit(&pidlock);
876 		task_rele(tk);
877 
878 		return (EAGAIN);
879 	}
880 
881 	if (cid != syscid) {
882 		ctp = contract_process_fork(sys_process_tmpl, p, curproc,
883 		    B_FALSE);
884 		ASSERT(ctp != NULL);
885 		if (ct != NULL)
886 			*ct = &ctp->conp_contract;
887 	}
888 
889 	p->p_lwpid = 1;
890 	mutex_enter(&pidlock);
891 	pgjoin(p, curproc->p_pgidp);
892 	p->p_stat = SRUN;
893 	mutex_enter(&p->p_lock);
894 	lwptot(lwp)->t_proc_flag &= ~TP_HOLDLWP;
895 	lwp_create_done(lwptot(lwp));
896 	mutex_exit(&p->p_lock);
897 	mutex_exit(&pidlock);
898 	return (0);
899 }
900 
901 /*
902  * create a child proc struct.
903  */
904 static int
905 getproc(proc_t **cpp, int kernel)
906 {
907 	proc_t		*pp, *cp;
908 	pid_t		newpid;
909 	struct user	*uarea;
910 	extern uint_t	nproc;
911 	struct cred	*cr;
912 	uid_t		ruid;
913 	zoneid_t	zoneid;
914 
915 	if (!page_mem_avail(tune.t_minarmem))
916 		return (-1);
917 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)
918 		return (-1);	/* no point in starting new processes */
919 
920 	pp = curproc;
921 	cp = kmem_cache_alloc(process_cache, KM_SLEEP);
922 	bzero(cp, sizeof (proc_t));
923 
924 	/*
925 	 * Make proc entry for child process
926 	 */
927 	mutex_init(&cp->p_splock, NULL, MUTEX_DEFAULT, NULL);
928 	mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL);
929 	mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL);
930 #if defined(__x86)
931 	mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL);
932 #endif
933 	mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL);
934 	cp->p_stat = SIDL;
935 	cp->p_mstart = gethrtime();
936 	/*
937 	 * p_zone must be set before we call pid_allocate since the process
938 	 * will be visible after that and code such as prfind_zone will
939 	 * look at the p_zone field.
940 	 */
941 	cp->p_zone = pp->p_zone;
942 
943 	if ((newpid = pid_allocate(cp, PID_ALLOC_PROC)) == -1) {
944 		if (nproc == v.v_proc) {
945 			CPU_STATS_ADDQ(CPU, sys, procovf, 1);
946 			cmn_err(CE_WARN, "out of processes");
947 		}
948 		goto bad;
949 	}
950 
951 	/*
952 	 * If not privileged make sure that this user hasn't exceeded
953 	 * v.v_maxup processes, and that users collectively haven't
954 	 * exceeded v.v_maxupttl processes.
955 	 */
956 	mutex_enter(&pidlock);
957 	ASSERT(nproc < v.v_proc);	/* otherwise how'd we get our pid? */
958 	cr = CRED();
959 	ruid = crgetruid(cr);
960 	zoneid = crgetzoneid(cr);
961 	if (nproc >= v.v_maxup && 	/* short-circuit; usually false */
962 	    (nproc >= v.v_maxupttl ||
963 	    upcount_get(ruid, zoneid) >= v.v_maxup) &&
964 	    secpolicy_newproc(cr) != 0) {
965 		mutex_exit(&pidlock);
966 		zcmn_err(zoneid, CE_NOTE,
967 		    "out of per-user processes for uid %d", ruid);
968 		goto bad;
969 	}
970 
971 	/*
972 	 * Everything is cool, put the new proc on the active process list.
973 	 * It is already on the pid list and in /proc.
974 	 * Increment the per uid process count (upcount).
975 	 */
976 	nproc++;
977 	upcount_inc(ruid, zoneid);
978 
979 	cp->p_next = practive;
980 	practive->p_prev = cp;
981 	practive = cp;
982 
983 	cp->p_ignore = pp->p_ignore;
984 	cp->p_siginfo = pp->p_siginfo;
985 	cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD);
986 	cp->p_sessp = pp->p_sessp;
987 	sess_hold(pp);
988 	cp->p_exec = pp->p_exec;
989 	cp->p_execdir = pp->p_execdir;
990 	cp->p_brand = pp->p_brand;
991 	if (PROC_IS_BRANDED(pp))
992 		BROP(pp)->b_copy_procdata(cp, pp);
993 
994 	cp->p_bssbase = pp->p_bssbase;
995 	cp->p_brkbase = pp->p_brkbase;
996 	cp->p_brksize = pp->p_brksize;
997 	cp->p_brkpageszc = pp->p_brkpageszc;
998 	cp->p_stksize = pp->p_stksize;
999 	cp->p_stkpageszc = pp->p_stkpageszc;
1000 	cp->p_stkprot = pp->p_stkprot;
1001 	cp->p_datprot = pp->p_datprot;
1002 	cp->p_usrstack = pp->p_usrstack;
1003 	cp->p_model = pp->p_model;
1004 	cp->p_ppid = pp->p_pid;
1005 	cp->p_ancpid = pp->p_pid;
1006 	cp->p_portcnt = pp->p_portcnt;
1007 
1008 	/*
1009 	 * Initialize watchpoint structures
1010 	 */
1011 	avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area),
1012 	    offsetof(struct watched_area, wa_link));
1013 
1014 	/*
1015 	 * Initialize immediate resource control values.
1016 	 */
1017 	cp->p_stk_ctl = pp->p_stk_ctl;
1018 	cp->p_fsz_ctl = pp->p_fsz_ctl;
1019 	cp->p_vmem_ctl = pp->p_vmem_ctl;
1020 	cp->p_fno_ctl = pp->p_fno_ctl;
1021 
1022 	/*
1023 	 * Link up to parent-child-sibling chain.  No need to lock
1024 	 * in general since only a call to freeproc() (done by the
1025 	 * same parent as newproc()) diddles with the child chain.
1026 	 */
1027 	cp->p_sibling = pp->p_child;
1028 	if (pp->p_child)
1029 		pp->p_child->p_psibling = cp;
1030 
1031 	cp->p_parent = pp;
1032 	pp->p_child = cp;
1033 
1034 	cp->p_child_ns = NULL;
1035 	cp->p_sibling_ns = NULL;
1036 
1037 	cp->p_nextorph = pp->p_orphan;
1038 	cp->p_nextofkin = pp;
1039 	pp->p_orphan = cp;
1040 
1041 	/*
1042 	 * Inherit profiling state; do not inherit REALPROF profiling state.
1043 	 */
1044 	cp->p_prof = pp->p_prof;
1045 	cp->p_rprof_cyclic = CYCLIC_NONE;
1046 
1047 	/*
1048 	 * Inherit pool pointer from the parent.  Kernel processes are
1049 	 * always bound to the default pool.
1050 	 */
1051 	mutex_enter(&pp->p_lock);
1052 	if (kernel) {
1053 		cp->p_pool = pool_default;
1054 		cp->p_flag |= SSYS;
1055 	} else {
1056 		cp->p_pool = pp->p_pool;
1057 	}
1058 	atomic_add_32(&cp->p_pool->pool_ref, 1);
1059 	mutex_exit(&pp->p_lock);
1060 
1061 	/*
1062 	 * Add the child process to the current task.  Kernel processes
1063 	 * are always attached to task0.
1064 	 */
1065 	mutex_enter(&cp->p_lock);
1066 	if (kernel)
1067 		task_attach(task0p, cp);
1068 	else
1069 		task_attach(pp->p_task, cp);
1070 	mutex_exit(&cp->p_lock);
1071 	mutex_exit(&pidlock);
1072 
1073 	avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t),
1074 	    offsetof(contract_t, ct_ctlist));
1075 
1076 	/*
1077 	 * Duplicate any audit information kept in the process table
1078 	 */
1079 #ifdef C2_AUDIT
1080 	if (audit_active)	/* copy audit data to cp */
1081 		audit_newproc(cp);
1082 #endif
1083 
1084 	crhold(cp->p_cred = cr);
1085 
1086 	/*
1087 	 * Bump up the counts on the file structures pointed at by the
1088 	 * parent's file table since the child will point at them too.
1089 	 */
1090 	fcnt_add(P_FINFO(pp), 1);
1091 
1092 	VN_HOLD(PTOU(pp)->u_cdir);
1093 	if (PTOU(pp)->u_rdir)
1094 		VN_HOLD(PTOU(pp)->u_rdir);
1095 	if (PTOU(pp)->u_cwd)
1096 		refstr_hold(PTOU(pp)->u_cwd);
1097 
1098 	/*
1099 	 * copy the parent's uarea.
1100 	 */
1101 	uarea = PTOU(cp);
1102 	bcopy(PTOU(pp), uarea, sizeof (*uarea));
1103 	flist_fork(P_FINFO(pp), P_FINFO(cp));
1104 
1105 	gethrestime(&uarea->u_start);
1106 	uarea->u_ticks = lbolt;
1107 	uarea->u_mem = rm_asrss(pp->p_as);
1108 	uarea->u_acflag = AFORK;
1109 
1110 	/*
1111 	 * If inherit-on-fork, copy /proc tracing flags to child.
1112 	 */
1113 	if ((pp->p_proc_flag & P_PR_FORK) != 0) {
1114 		cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK);
1115 		cp->p_sigmask = pp->p_sigmask;
1116 		cp->p_fltmask = pp->p_fltmask;
1117 	} else {
1118 		sigemptyset(&cp->p_sigmask);
1119 		premptyset(&cp->p_fltmask);
1120 		uarea->u_systrap = 0;
1121 		premptyset(&uarea->u_entrymask);
1122 		premptyset(&uarea->u_exitmask);
1123 	}
1124 	/*
1125 	 * If microstate accounting is being inherited, mark child
1126 	 */
1127 	if ((pp->p_flag & SMSFORK) != 0)
1128 		cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT);
1129 
1130 	/*
1131 	 * Inherit fixalignment flag from the parent
1132 	 */
1133 	cp->p_fixalignment = pp->p_fixalignment;
1134 
1135 	if (cp->p_exec)
1136 		VN_HOLD(cp->p_exec);
1137 	if (cp->p_execdir)
1138 		VN_HOLD(cp->p_execdir);
1139 	*cpp = cp;
1140 	return (0);
1141 
1142 bad:
1143 	ASSERT(MUTEX_NOT_HELD(&pidlock));
1144 
1145 	mutex_destroy(&cp->p_crlock);
1146 	mutex_destroy(&cp->p_pflock);
1147 #if defined(__x86)
1148 	mutex_destroy(&cp->p_ldtlock);
1149 #endif
1150 	if (newpid != -1) {
1151 		proc_entry_free(cp->p_pidp);
1152 		(void) pid_rele(cp->p_pidp);
1153 	}
1154 	kmem_cache_free(process_cache, cp);
1155 
1156 	/*
1157 	 * We most likely got into this situation because some process is
1158 	 * forking out of control.  As punishment, put it to sleep for a
1159 	 * bit so it can't eat the machine alive.  Sleep interval is chosen
1160 	 * to allow no more than one fork failure per cpu per clock tick
1161 	 * on average (yes, I just made this up).  This has two desirable
1162 	 * properties: (1) it sets a constant limit on the fork failure
1163 	 * rate, and (2) the busier the system is, the harsher the penalty
1164 	 * for abusing it becomes.
1165 	 */
1166 	INCR_COUNT(&fork_fail_pending, &pidlock);
1167 	delay(fork_fail_pending / ncpus + 1);
1168 	DECR_COUNT(&fork_fail_pending, &pidlock);
1169 
1170 	return (-1); /* out of memory or proc slots */
1171 }
1172 
1173 /*
1174  * Release virtual memory.
1175  * In the case of vfork(), the child was given exclusive access to its
1176  * parent's address space.  The parent is waiting in vfwait() for the
1177  * child to release its exclusive claim via relvm().
1178  */
1179 void
1180 relvm()
1181 {
1182 	proc_t *p = curproc;
1183 
1184 	ASSERT((unsigned)p->p_lwpcnt <= 1);
1185 
1186 	prrelvm();	/* inform /proc */
1187 
1188 	if (p->p_flag & SVFORK) {
1189 		proc_t *pp = p->p_parent;
1190 		/*
1191 		 * The child process is either exec'ing or exit'ing.
1192 		 * The child is now separated from the parent's address
1193 		 * space.  The parent process is made dispatchable.
1194 		 *
1195 		 * This is a delicate locking maneuver, involving
1196 		 * both the parent's p_lock and the child's p_lock.
1197 		 * As soon as the SVFORK flag is turned off, the
1198 		 * parent is free to run, but it must not run until
1199 		 * we wake it up using its p_cv because it might
1200 		 * exit and we would be referencing invalid memory.
1201 		 * Therefore, we hold the parent with its p_lock
1202 		 * while protecting our p_flags with our own p_lock.
1203 		 */
1204 try_again:
1205 		mutex_enter(&p->p_lock);	/* grab child's lock first */
1206 		prbarrier(p);		/* make sure /proc is blocked out */
1207 		mutex_enter(&pp->p_lock);
1208 
1209 		/*
1210 		 * Check if parent is locked by /proc.
1211 		 */
1212 		if (pp->p_proc_flag & P_PR_LOCK) {
1213 			/*
1214 			 * Delay until /proc is done with the parent.
1215 			 * We must drop our (the child's) p->p_lock, wait
1216 			 * via prbarrier() on the parent, then start over.
1217 			 */
1218 			mutex_exit(&p->p_lock);
1219 			prbarrier(pp);
1220 			mutex_exit(&pp->p_lock);
1221 			goto try_again;
1222 		}
1223 		p->p_flag &= ~SVFORK;
1224 		kpreempt_disable();
1225 		p->p_as = &kas;
1226 
1227 		/*
1228 		 * notify hat of change in thread's address space
1229 		 */
1230 		hat_thread_exit(curthread);
1231 		kpreempt_enable();
1232 
1233 		/*
1234 		 * child sizes are copied back to parent because
1235 		 * child may have grown.
1236 		 */
1237 		pp->p_brkbase = p->p_brkbase;
1238 		pp->p_brksize = p->p_brksize;
1239 		pp->p_stksize = p->p_stksize;
1240 		/*
1241 		 * The parent is no longer waiting for the vfork()d child.
1242 		 * Restore the parent's watched pages, if any.  This is
1243 		 * safe because we know the parent is not locked by /proc
1244 		 */
1245 		pp->p_flag &= ~SVFWAIT;
1246 		if (avl_numnodes(&pp->p_wpage) != 0) {
1247 			pp->p_as->a_wpage = pp->p_wpage;
1248 			avl_create(&pp->p_wpage, wp_compare,
1249 			    sizeof (struct watched_page),
1250 			    offsetof(struct watched_page, wp_link));
1251 		}
1252 		cv_signal(&pp->p_cv);
1253 		mutex_exit(&pp->p_lock);
1254 		mutex_exit(&p->p_lock);
1255 	} else {
1256 		if (p->p_as != &kas) {
1257 			struct as *as;
1258 
1259 			if (p->p_segacct)
1260 				shmexit(p);
1261 
1262 			/*
1263 			 * We grab p_lock for the benefit of /proc
1264 			 */
1265 			kpreempt_disable();
1266 			mutex_enter(&p->p_lock);
1267 			prbarrier(p);	/* make sure /proc is blocked out */
1268 			as = p->p_as;
1269 			p->p_as = &kas;
1270 			mutex_exit(&p->p_lock);
1271 
1272 			/*
1273 			 * notify hat of change in thread's address space
1274 			 */
1275 			hat_thread_exit(curthread);
1276 			kpreempt_enable();
1277 
1278 			as_free(as);
1279 		}
1280 	}
1281 }
1282 
1283 /*
1284  * Wait for child to exec or exit.
1285  * Called by parent of vfork'ed process.
1286  * See important comments in relvm(), above.
1287  */
1288 void
1289 vfwait(pid_t pid)
1290 {
1291 	int signalled = 0;
1292 	proc_t *pp = ttoproc(curthread);
1293 	proc_t *cp;
1294 
1295 	/*
1296 	 * Wait for child to exec or exit.
1297 	 */
1298 	for (;;) {
1299 		mutex_enter(&pidlock);
1300 		cp = prfind(pid);
1301 		if (cp == NULL || cp->p_parent != pp) {
1302 			/*
1303 			 * Child has exit()ed.
1304 			 */
1305 			mutex_exit(&pidlock);
1306 			break;
1307 		}
1308 		/*
1309 		 * Grab the child's p_lock before releasing pidlock.
1310 		 * Otherwise, the child could exit and we would be
1311 		 * referencing invalid memory.
1312 		 */
1313 		mutex_enter(&cp->p_lock);
1314 		mutex_exit(&pidlock);
1315 		if (!(cp->p_flag & SVFORK)) {
1316 			/*
1317 			 * Child has exec()ed or is exit()ing.
1318 			 */
1319 			mutex_exit(&cp->p_lock);
1320 			break;
1321 		}
1322 		mutex_enter(&pp->p_lock);
1323 		mutex_exit(&cp->p_lock);
1324 		/*
1325 		 * We might be waked up spuriously from the cv_wait().
1326 		 * We have to do the whole operation over again to be
1327 		 * sure the child's SVFORK flag really is turned off.
1328 		 * We cannot make reference to the child because it can
1329 		 * exit before we return and we would be referencing
1330 		 * invalid memory.
1331 		 *
1332 		 * Because this is potentially a very long-term wait,
1333 		 * we call cv_wait_sig() (for its jobcontrol and /proc
1334 		 * side-effects) unless there is a current signal, in
1335 		 * which case we use cv_wait() because we cannot return
1336 		 * from this function until the child has released the
1337 		 * address space.  Calling cv_wait_sig() with a current
1338 		 * signal would lead to an indefinite loop here because
1339 		 * cv_wait_sig() returns immediately in this case.
1340 		 */
1341 		if (signalled)
1342 			cv_wait(&pp->p_cv, &pp->p_lock);
1343 		else
1344 			signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock);
1345 		mutex_exit(&pp->p_lock);
1346 	}
1347 
1348 	/* restore watchpoints to parent */
1349 	if (pr_watch_active(pp)) {
1350 		struct as *as = pp->p_as;
1351 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1352 		as_setwatch(as);
1353 		AS_LOCK_EXIT(as, &as->a_lock);
1354 	}
1355 
1356 	mutex_enter(&pp->p_lock);
1357 	prbarrier(pp);	/* barrier against /proc locking */
1358 	continuelwps(pp);
1359 	mutex_exit(&pp->p_lock);
1360 }
1361