Home | History | Annotate | Line # | Download | only in kern
kern_fork.c revision 1.188
      1 /*	$NetBSD: kern_fork.c,v 1.188 2012/03/02 21:23:05 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)kern_fork.c	8.8 (Berkeley) 2/14/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.188 2012/03/02 21:23:05 rmind Exp $");
     71 
     72 #include "opt_ktrace.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/filedesc.h>
     77 #include <sys/kernel.h>
     78 #include <sys/pool.h>
     79 #include <sys/mount.h>
     80 #include <sys/proc.h>
     81 #include <sys/ras.h>
     82 #include <sys/resourcevar.h>
     83 #include <sys/vnode.h>
     84 #include <sys/file.h>
     85 #include <sys/acct.h>
     86 #include <sys/ktrace.h>
     87 #include <sys/sched.h>
     88 #include <sys/signalvar.h>
     89 #include <sys/kauth.h>
     90 #include <sys/atomic.h>
     91 #include <sys/syscallargs.h>
     92 #include <sys/uidinfo.h>
     93 #include <sys/sdt.h>
     94 #include <sys/ptrace.h>
     95 
     96 #include <uvm/uvm_extern.h>
     97 
     98 /*
     99  * DTrace SDT provider definitions
    100  */
    101 SDT_PROBE_DEFINE(proc,,,create,
    102 	    "struct proc *", NULL,	/* new process */
    103 	    "struct proc *", NULL,	/* parent process */
    104 	    "int", NULL,		/* flags */
    105 	    NULL, NULL, NULL, NULL);
    106 
    107 u_int	nprocs __cacheline_aligned = 1;		/* process 0 */
    108 
    109 /*
    110  * Number of ticks to sleep if fork() would fail due to process hitting
    111  * limits. Exported in miliseconds to userland via sysctl.
    112  */
    113 int	forkfsleep = 0;
    114 
    115 int
    116 sys_fork(struct lwp *l, const void *v, register_t *retval)
    117 {
    118 
    119 	return fork1(l, 0, SIGCHLD, NULL, 0, NULL, NULL, retval, NULL);
    120 }
    121 
    122 /*
    123  * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
    124  * Address space is not shared, but parent is blocked until child exit.
    125  */
    126 int
    127 sys_vfork(struct lwp *l, const void *v, register_t *retval)
    128 {
    129 
    130 	return fork1(l, FORK_PPWAIT, SIGCHLD, NULL, 0, NULL, NULL,
    131 	    retval, NULL);
    132 }
    133 
    134 /*
    135  * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
    136  * semantics.  Address space is shared, and parent is blocked until child exit.
    137  */
    138 int
    139 sys___vfork14(struct lwp *l, const void *v, register_t *retval)
    140 {
    141 
    142 	return fork1(l, FORK_PPWAIT|FORK_SHAREVM, SIGCHLD, NULL, 0,
    143 	    NULL, NULL, retval, NULL);
    144 }
    145 
    146 /*
    147  * Linux-compatible __clone(2) system call.
    148  */
    149 int
    150 sys___clone(struct lwp *l, const struct sys___clone_args *uap,
    151     register_t *retval)
    152 {
    153 	/* {
    154 		syscallarg(int) flags;
    155 		syscallarg(void *) stack;
    156 	} */
    157 	int flags, sig;
    158 
    159 	/*
    160 	 * We don't support the CLONE_PID or CLONE_PTRACE flags.
    161 	 */
    162 	if (SCARG(uap, flags) & (CLONE_PID|CLONE_PTRACE))
    163 		return EINVAL;
    164 
    165 	/*
    166 	 * Linux enforces CLONE_VM with CLONE_SIGHAND, do same.
    167 	 */
    168 	if (SCARG(uap, flags) & CLONE_SIGHAND
    169 	    && (SCARG(uap, flags) & CLONE_VM) == 0)
    170 		return EINVAL;
    171 
    172 	flags = 0;
    173 
    174 	if (SCARG(uap, flags) & CLONE_VM)
    175 		flags |= FORK_SHAREVM;
    176 	if (SCARG(uap, flags) & CLONE_FS)
    177 		flags |= FORK_SHARECWD;
    178 	if (SCARG(uap, flags) & CLONE_FILES)
    179 		flags |= FORK_SHAREFILES;
    180 	if (SCARG(uap, flags) & CLONE_SIGHAND)
    181 		flags |= FORK_SHARESIGS;
    182 	if (SCARG(uap, flags) & CLONE_VFORK)
    183 		flags |= FORK_PPWAIT;
    184 
    185 	sig = SCARG(uap, flags) & CLONE_CSIGNAL;
    186 	if (sig < 0 || sig >= _NSIG)
    187 		return EINVAL;
    188 
    189 	/*
    190 	 * Note that the Linux API does not provide a portable way of
    191 	 * specifying the stack area; the caller must know if the stack
    192 	 * grows up or down.  So, we pass a stack size of 0, so that the
    193 	 * code that makes this adjustment is a noop.
    194 	 */
    195 	return fork1(l, flags, sig, SCARG(uap, stack), 0,
    196 	    NULL, NULL, retval, NULL);
    197 }
    198 
    199 /*
    200  * Print the 'table full' message once per 10 seconds.
    201  */
    202 static struct timeval fork_tfmrate = { 10, 0 };
    203 
    204 /*
    205  * General fork call.  Note that another LWP in the process may call exec()
    206  * or exit() while we are forking.  It's safe to continue here, because
    207  * neither operation will complete until all LWPs have exited the process.
    208  */
    209 int
    210 fork1(struct lwp *l1, int flags, int exitsig, void *stack, size_t stacksize,
    211     void (*func)(void *), void *arg, register_t *retval,
    212     struct proc **rnewprocp)
    213 {
    214 	struct proc	*p1, *p2, *parent;
    215 	struct plimit   *p1_lim;
    216 	uid_t		uid;
    217 	struct lwp	*l2;
    218 	int		count;
    219 	vaddr_t		uaddr;
    220 	int		tnprocs;
    221 	int		tracefork;
    222 	int		error = 0;
    223 
    224 	p1 = l1->l_proc;
    225 	uid = kauth_cred_getuid(l1->l_cred);
    226 	tnprocs = atomic_inc_uint_nv(&nprocs);
    227 
    228 	/*
    229 	 * Although process entries are dynamically created, we still keep
    230 	 * a global limit on the maximum number we will create.
    231 	 */
    232 	if (__predict_false(tnprocs >= maxproc))
    233 		error = -1;
    234 	else
    235 		error = kauth_authorize_process(l1->l_cred,
    236 		    KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL);
    237 
    238 	if (error) {
    239 		static struct timeval lasttfm;
    240 		atomic_dec_uint(&nprocs);
    241 		if (ratecheck(&lasttfm, &fork_tfmrate))
    242 			tablefull("proc", "increase kern.maxproc or NPROC");
    243 		if (forkfsleep)
    244 			kpause("forkmx", false, forkfsleep, NULL);
    245 		return EAGAIN;
    246 	}
    247 
    248 	/*
    249 	 * Enforce limits.
    250 	 */
    251 	count = chgproccnt(uid, 1);
    252 	if (kauth_authorize_generic(l1->l_cred, KAUTH_GENERIC_ISSUSER, NULL) !=
    253 	    0 && __predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) {
    254 		(void)chgproccnt(uid, -1);
    255 		atomic_dec_uint(&nprocs);
    256 		if (forkfsleep)
    257 			kpause("forkulim", false, forkfsleep, NULL);
    258 		return EAGAIN;
    259 	}
    260 
    261 	/*
    262 	 * Allocate virtual address space for the U-area now, while it
    263 	 * is still easy to abort the fork operation if we're out of
    264 	 * kernel virtual address space.
    265 	 */
    266 	uaddr = uvm_uarea_alloc();
    267 	if (__predict_false(uaddr == 0)) {
    268 		(void)chgproccnt(uid, -1);
    269 		atomic_dec_uint(&nprocs);
    270 		return ENOMEM;
    271 	}
    272 
    273 	/*
    274 	 * We are now committed to the fork.  From here on, we may
    275 	 * block on resources, but resource allocation may NOT fail.
    276 	 */
    277 
    278 	/* Allocate new proc. */
    279 	p2 = proc_alloc();
    280 
    281 	/*
    282 	 * Make a proc table entry for the new process.
    283 	 * Start by zeroing the section of proc that is zero-initialized,
    284 	 * then copy the section that is copied directly from the parent.
    285 	 */
    286 	memset(&p2->p_startzero, 0,
    287 	    (unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero));
    288 	memcpy(&p2->p_startcopy, &p1->p_startcopy,
    289 	    (unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy));
    290 
    291 	CIRCLEQ_INIT(&p2->p_sigpend.sp_info);
    292 
    293 	LIST_INIT(&p2->p_lwps);
    294 	LIST_INIT(&p2->p_sigwaiters);
    295 
    296 	/*
    297 	 * Duplicate sub-structures as needed.
    298 	 * Increase reference counts on shared objects.
    299 	 * Inherit flags we want to keep.  The flags related to SIGCHLD
    300 	 * handling are important in order to keep a consistent behaviour
    301 	 * for the child after the fork.  If we are a 32-bit process, the
    302 	 * child will be too.
    303 	 */
    304 	p2->p_flag =
    305 	    p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32);
    306 	p2->p_emul = p1->p_emul;
    307 	p2->p_execsw = p1->p_execsw;
    308 
    309 	if (flags & FORK_SYSTEM) {
    310 		/*
    311 		 * Mark it as a system process.  Set P_NOCLDWAIT so that
    312 		 * children are reparented to init(8) when they exit.
    313 		 * init(8) can easily wait them out for us.
    314 		 */
    315 		p2->p_flag |= (PK_SYSTEM | PK_NOCLDWAIT);
    316 	}
    317 
    318 	mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    319 	mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    320 	rw_init(&p2->p_reflock);
    321 	cv_init(&p2->p_waitcv, "wait");
    322 	cv_init(&p2->p_lwpcv, "lwpwait");
    323 
    324 	/*
    325 	 * Share a lock between the processes if they are to share signal
    326 	 * state: we must synchronize access to it.
    327 	 */
    328 	if (flags & FORK_SHARESIGS) {
    329 		p2->p_lock = p1->p_lock;
    330 		mutex_obj_hold(p1->p_lock);
    331 	} else
    332 		p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    333 
    334 	kauth_proc_fork(p1, p2);
    335 
    336 	p2->p_raslist = NULL;
    337 #if defined(__HAVE_RAS)
    338 	ras_fork(p1, p2);
    339 #endif
    340 
    341 	/* bump references to the text vnode (for procfs) */
    342 	p2->p_textvp = p1->p_textvp;
    343 	if (p2->p_textvp)
    344 		vref(p2->p_textvp);
    345 
    346 	if (flags & FORK_SHAREFILES)
    347 		fd_share(p2);
    348 	else if (flags & FORK_CLEANFILES)
    349 		p2->p_fd = fd_init(NULL);
    350 	else
    351 		p2->p_fd = fd_copy();
    352 
    353 	/* XXX racy */
    354 	p2->p_mqueue_cnt = p1->p_mqueue_cnt;
    355 
    356 	if (flags & FORK_SHARECWD)
    357 		cwdshare(p2);
    358 	else
    359 		p2->p_cwdi = cwdinit();
    360 
    361 	/*
    362 	 * Note: p_limit (rlimit stuff) is copy-on-write, so normally
    363 	 * we just need increase pl_refcnt.
    364 	 */
    365 	p1_lim = p1->p_limit;
    366 	if (!p1_lim->pl_writeable) {
    367 		lim_addref(p1_lim);
    368 		p2->p_limit = p1_lim;
    369 	} else {
    370 		p2->p_limit = lim_copy(p1_lim);
    371 	}
    372 
    373 	p2->p_lflag = ((flags & FORK_PPWAIT) ? PL_PPWAIT : 0);
    374 	p2->p_sflag = 0;
    375 	p2->p_slflag = 0;
    376 	parent = (flags & FORK_NOWAIT) ? initproc : p1;
    377 	p2->p_pptr = parent;
    378 	p2->p_ppid = parent->p_pid;
    379 	LIST_INIT(&p2->p_children);
    380 
    381 	p2->p_aio = NULL;
    382 
    383 #ifdef KTRACE
    384 	/*
    385 	 * Copy traceflag and tracefile if enabled.
    386 	 * If not inherited, these were zeroed above.
    387 	 */
    388 	if (p1->p_traceflag & KTRFAC_INHERIT) {
    389 		mutex_enter(&ktrace_lock);
    390 		p2->p_traceflag = p1->p_traceflag;
    391 		if ((p2->p_tracep = p1->p_tracep) != NULL)
    392 			ktradref(p2);
    393 		mutex_exit(&ktrace_lock);
    394 	}
    395 #endif
    396 
    397 	/*
    398 	 * Create signal actions for the child process.
    399 	 */
    400 	p2->p_sigacts = sigactsinit(p1, flags & FORK_SHARESIGS);
    401 	mutex_enter(p1->p_lock);
    402 	p2->p_sflag |=
    403 	    (p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP));
    404 	sched_proc_fork(p1, p2);
    405 	mutex_exit(p1->p_lock);
    406 
    407 	p2->p_stflag = p1->p_stflag;
    408 
    409 	/*
    410 	 * p_stats.
    411 	 * Copy parts of p_stats, and zero out the rest.
    412 	 */
    413 	p2->p_stats = pstatscopy(p1->p_stats);
    414 
    415 	/*
    416 	 * Set up the new process address space.
    417 	 */
    418 	uvm_proc_fork(p1, p2, (flags & FORK_SHAREVM) ? true : false);
    419 
    420 	/*
    421 	 * Finish creating the child process.
    422 	 * It will return through a different path later.
    423 	 */
    424 	lwp_create(l1, p2, uaddr, (flags & FORK_PPWAIT) ? LWP_VFORK : 0,
    425 	    stack, stacksize, (func != NULL) ? func : child_return, arg, &l2,
    426 	    l1->l_class);
    427 
    428 	/*
    429 	 * Inherit l_private from the parent.
    430 	 * Note that we cannot use lwp_setprivate() here since that
    431 	 * also sets the CPU TLS register, which is incorrect if the
    432 	 * process has changed that without letting the kernel know.
    433 	 */
    434 	l2->l_private = l1->l_private;
    435 
    436 	/*
    437 	 * If emulation has a process fork hook, call it now.
    438 	 */
    439 	if (p2->p_emul->e_proc_fork)
    440 		(*p2->p_emul->e_proc_fork)(p2, l1, flags);
    441 
    442 	/*
    443 	 * ...and finally, any other random fork hooks that subsystems
    444 	 * might have registered.
    445 	 */
    446 	doforkhooks(p2, p1);
    447 
    448 	SDT_PROBE(proc,,,create, p2, p1, flags, 0, 0);
    449 
    450 	/*
    451 	 * It's now safe for the scheduler and other processes to see the
    452 	 * child process.
    453 	 */
    454 	mutex_enter(proc_lock);
    455 
    456 	if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT)
    457 		p2->p_lflag |= PL_CONTROLT;
    458 
    459 	LIST_INSERT_HEAD(&parent->p_children, p2, p_sibling);
    460 	p2->p_exitsig = exitsig;		/* signal for parent on exit */
    461 
    462 	/*
    463 	 * We don't want to tracefork vfork()ed processes because they
    464 	 * will not receive the SIGTRAP until it is too late.
    465 	 */
    466 	tracefork = (p1->p_slflag & (PSL_TRACEFORK|PSL_TRACED)) ==
    467 	    (PSL_TRACEFORK|PSL_TRACED) && (flags && FORK_PPWAIT) == 0;
    468 	if (tracefork) {
    469 		p2->p_slflag |= PSL_TRACED;
    470 		p2->p_opptr = p2->p_pptr;
    471 		if (p2->p_pptr != p1->p_pptr) {
    472 			struct proc *parent1 = p2->p_pptr;
    473 
    474 			if (parent1->p_lock < p2->p_lock) {
    475 				if (!mutex_tryenter(parent1->p_lock)) {
    476 					mutex_exit(p2->p_lock);
    477 					mutex_enter(parent1->p_lock);
    478 				}
    479 			} else if (parent1->p_lock > p2->p_lock) {
    480 				mutex_enter(parent1->p_lock);
    481 			}
    482 			parent1->p_slflag |= PSL_CHTRACED;
    483 			proc_reparent(p2, p1->p_pptr);
    484 			if (parent1->p_lock != p2->p_lock)
    485 				mutex_exit(parent1->p_lock);
    486 		}
    487 
    488 		/*
    489 		 * Set ptrace status.
    490 		 */
    491 		p1->p_fpid = p2->p_pid;
    492 		p2->p_fpid = p1->p_pid;
    493 	}
    494 
    495 	LIST_INSERT_AFTER(p1, p2, p_pglist);
    496 	LIST_INSERT_HEAD(&allproc, p2, p_list);
    497 
    498 	p2->p_trace_enabled = trace_is_enabled(p2);
    499 #ifdef __HAVE_SYSCALL_INTERN
    500 	(*p2->p_emul->e_syscall_intern)(p2);
    501 #endif
    502 
    503 	/*
    504 	 * Update stats now that we know the fork was successful.
    505 	 */
    506 	uvmexp.forks++;
    507 	if (flags & FORK_PPWAIT)
    508 		uvmexp.forks_ppwait++;
    509 	if (flags & FORK_SHAREVM)
    510 		uvmexp.forks_sharevm++;
    511 
    512 	/*
    513 	 * Pass a pointer to the new process to the caller.
    514 	 */
    515 	if (rnewprocp != NULL)
    516 		*rnewprocp = p2;
    517 
    518 	if (ktrpoint(KTR_EMUL))
    519 		p2->p_traceflag |= KTRFAC_TRC_EMUL;
    520 
    521 	/*
    522 	 * Notify any interested parties about the new process.
    523 	 */
    524 	if (!SLIST_EMPTY(&p1->p_klist)) {
    525 		mutex_exit(proc_lock);
    526 		KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
    527 		mutex_enter(proc_lock);
    528 	}
    529 
    530 	/*
    531 	 * Make child runnable, set start time, and add to run queue except
    532 	 * if the parent requested the child to start in SSTOP state.
    533 	 */
    534 	mutex_enter(p2->p_lock);
    535 
    536 	/*
    537 	 * Start profiling.
    538 	 */
    539 	if ((p2->p_stflag & PST_PROFIL) != 0) {
    540 		mutex_spin_enter(&p2->p_stmutex);
    541 		startprofclock(p2);
    542 		mutex_spin_exit(&p2->p_stmutex);
    543 	}
    544 
    545 	getmicrotime(&p2->p_stats->p_start);
    546 	p2->p_acflag = AFORK;
    547 	lwp_lock(l2);
    548 	KASSERT(p2->p_nrlwps == 1);
    549 	if (p2->p_sflag & PS_STOPFORK) {
    550 		struct schedstate_percpu *spc = &l2->l_cpu->ci_schedstate;
    551 		p2->p_nrlwps = 0;
    552 		p2->p_stat = SSTOP;
    553 		p2->p_waited = 0;
    554 		p1->p_nstopchild++;
    555 		l2->l_stat = LSSTOP;
    556 		KASSERT(l2->l_wchan == NULL);
    557 		lwp_unlock_to(l2, spc->spc_lwplock);
    558 	} else {
    559 		p2->p_nrlwps = 1;
    560 		p2->p_stat = SACTIVE;
    561 		l2->l_stat = LSRUN;
    562 		sched_enqueue(l2, false);
    563 		lwp_unlock(l2);
    564 	}
    565 	mutex_exit(p2->p_lock);
    566 
    567 	/*
    568 	 * Preserve synchronization semantics of vfork.  If waiting for
    569 	 * child to exec or exit, set PL_PPWAIT on child, and sleep on our
    570 	 * proc (in case of exit).
    571 	 */
    572 	while (p2->p_lflag & PL_PPWAIT)
    573 		cv_wait(&p1->p_waitcv, proc_lock);
    574 
    575 	/*
    576 	 * Let the parent know that we are tracing its child.
    577 	 */
    578 	if (tracefork) {
    579 		ksiginfo_t ksi;
    580 
    581 		KSI_INIT_EMPTY(&ksi);
    582 		ksi.ksi_signo = SIGTRAP;
    583 		ksi.ksi_lid = l1->l_lid;
    584 		kpsignal(p1, &ksi, NULL);
    585 	}
    586 
    587 	mutex_exit(proc_lock);
    588 
    589 	/*
    590 	 * Return child pid to parent process,
    591 	 * marking us as parent via retval[1].
    592 	 */
    593 	if (retval != NULL) {
    594 		retval[0] = p2->p_pid;
    595 		retval[1] = 0;
    596 	}
    597 
    598 	return 0;
    599 }
    600