2011-08-23 17:01:25 +04:00
|
|
|
/* $NetBSD: kern_fork.c,v 1.185 2011/08/23 13:01:25 christos Exp $ */
|
2001-07-01 22:06:11 +04:00
|
|
|
|
|
|
|
/*-
|
2008-03-22 00:54:58 +03:00
|
|
|
* Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
|
2001-07-01 22:06:11 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
2007-12-04 19:56:16 +03:00
|
|
|
* NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
|
2005-02-27 00:34:55 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2001-07-01 22:06:11 +04:00
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2005-02-27 00:34:55 +03:00
|
|
|
*
|
2001-07-01 22:06:11 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2005-02-27 00:34:55 +03:00
|
|
|
*/
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
/*
|
1994-05-19 09:57:44 +04:00
|
|
|
* Copyright (c) 1982, 1986, 1989, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1994-05-17 08:21:49 +04:00
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-17 08:21:49 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
|
1994-05-17 08:21:49 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2011-08-23 17:01:25 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.185 2011/08/23 13:01:25 christos Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1998-06-26 01:17:15 +04:00
|
|
|
#include "opt_ktrace.h"
|
1998-02-10 17:08:44 +03:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/kernel.h>
|
1998-08-02 08:41:32 +04:00
|
|
|
#include <sys/pool.h>
|
1996-02-09 21:59:18 +03:00
|
|
|
#include <sys/mount.h>
|
1994-05-17 08:21:49 +04:00
|
|
|
#include <sys/proc.h>
|
2002-08-28 11:16:33 +04:00
|
|
|
#include <sys/ras.h>
|
1994-05-17 08:21:49 +04:00
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/acct.h>
|
|
|
|
#include <sys/ktrace.h>
|
1998-01-04 06:52:02 +03:00
|
|
|
#include <sys/vmmeter.h>
|
1999-02-23 05:57:18 +03:00
|
|
|
#include <sys/sched.h>
|
1999-05-01 01:23:49 +04:00
|
|
|
#include <sys/signalvar.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2007-12-04 19:56:16 +03:00
|
|
|
#include <sys/atomic.h>
|
1996-02-09 21:59:18 +03:00
|
|
|
#include <sys/syscallargs.h>
|
2008-10-11 17:40:57 +04:00
|
|
|
#include <sys/uidinfo.h>
|
DTrace: Add an SDT (Statically Defined Tracing) provider framework, and
implement most of the proc provider. Adds proc:::create, exec,
exec_success, exec_faillure, signal_send, signal_discard, signal_handle,
lwp_create, lwp_start, lwp_exit.
2010-03-02 00:10:13 +03:00
|
|
|
#include <sys/sdt.h>
|
1996-02-09 21:59:18 +03:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
DTrace: Add an SDT (Statically Defined Tracing) provider framework, and
implement most of the proc provider. Adds proc:::create, exec,
exec_success, exec_faillure, signal_send, signal_discard, signal_handle,
lwp_create, lwp_start, lwp_exit.
2010-03-02 00:10:13 +03:00
|
|
|
/*
|
|
|
|
* DTrace SDT provider definitions
|
|
|
|
*/
|
|
|
|
SDT_PROBE_DEFINE(proc,,,create,
|
|
|
|
"struct proc *", NULL, /* new process */
|
|
|
|
"struct proc *", NULL, /* parent process */
|
|
|
|
"int", NULL, /* flags */
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
|
2007-12-04 19:56:16 +03:00
|
|
|
u_int nprocs = 1; /* process 0 */
|
1995-12-09 07:23:07 +03:00
|
|
|
|
2002-12-11 21:09:07 +03:00
|
|
|
/*
|
|
|
|
* Number of ticks to sleep if fork() would fail due to process hitting
|
|
|
|
* limits. Exported in miliseconds to userland via sysctl.
|
|
|
|
*/
|
|
|
|
int forkfsleep = 0;
|
|
|
|
|
1996-02-04 05:15:01 +03:00
|
|
|
/*ARGSUSED*/
|
1995-12-09 07:23:07 +03:00
|
|
|
int
|
2007-12-21 02:02:38 +03:00
|
|
|
sys_fork(struct lwp *l, const void *v, register_t *retval)
|
1994-05-17 08:21:49 +04:00
|
|
|
{
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
return (fork1(l, 0, SIGCHLD, NULL, 0, NULL, NULL, retval, NULL));
|
1994-05-17 08:21:49 +04:00
|
|
|
}
|
|
|
|
|
1998-01-04 06:52:02 +03:00
|
|
|
/*
|
|
|
|
* vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
|
|
|
|
* Address space is not shared, but parent is blocked until child exit.
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
/*ARGSUSED*/
|
1995-12-09 07:23:07 +03:00
|
|
|
int
|
2007-12-21 02:02:38 +03:00
|
|
|
sys_vfork(struct lwp *l, const void *v, register_t *retval)
|
1994-05-17 08:21:49 +04:00
|
|
|
{
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
return (fork1(l, FORK_PPWAIT, SIGCHLD, NULL, 0, NULL, NULL,
|
2000-05-28 09:48:59 +04:00
|
|
|
retval, NULL));
|
1994-05-17 08:21:49 +04:00
|
|
|
}
|
|
|
|
|
1998-01-04 06:52:02 +03:00
|
|
|
/*
|
|
|
|
* New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
|
|
|
|
* semantics. Address space is shared, and parent is blocked until child exit.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
1995-12-09 07:23:07 +03:00
|
|
|
int
|
2007-12-21 02:02:38 +03:00
|
|
|
sys___vfork14(struct lwp *l, const void *v, register_t *retval)
|
1998-01-04 06:52:02 +03:00
|
|
|
{
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
return (fork1(l, FORK_PPWAIT|FORK_SHAREVM, SIGCHLD, NULL, 0,
|
2000-05-28 09:48:59 +04:00
|
|
|
NULL, NULL, retval, NULL));
|
1998-01-04 06:52:02 +03:00
|
|
|
}
|
|
|
|
|
2001-07-01 22:06:11 +04:00
|
|
|
/*
|
|
|
|
* Linux-compatible __clone(2) system call.
|
|
|
|
*/
|
|
|
|
int
|
2007-12-21 02:02:38 +03:00
|
|
|
sys___clone(struct lwp *l, const struct sys___clone_args *uap, register_t *retval)
|
2001-07-01 22:06:11 +04:00
|
|
|
{
|
2007-12-21 02:02:38 +03:00
|
|
|
/* {
|
2001-07-01 22:06:11 +04:00
|
|
|
syscallarg(int) flags;
|
|
|
|
syscallarg(void *) stack;
|
2007-12-21 02:02:38 +03:00
|
|
|
} */
|
2001-07-01 22:06:11 +04:00
|
|
|
int flags, sig;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't support the CLONE_PID or CLONE_PTRACE flags.
|
|
|
|
*/
|
|
|
|
if (SCARG(uap, flags) & (CLONE_PID|CLONE_PTRACE))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2004-08-08 15:02:10 +04:00
|
|
|
/*
|
|
|
|
* Linux enforces CLONE_VM with CLONE_SIGHAND, do same.
|
|
|
|
*/
|
|
|
|
if (SCARG(uap, flags) & CLONE_SIGHAND
|
|
|
|
&& (SCARG(uap, flags) & CLONE_VM) == 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2001-07-08 03:33:54 +04:00
|
|
|
flags = 0;
|
|
|
|
|
2001-07-01 22:06:11 +04:00
|
|
|
if (SCARG(uap, flags) & CLONE_VM)
|
|
|
|
flags |= FORK_SHAREVM;
|
|
|
|
if (SCARG(uap, flags) & CLONE_FS)
|
|
|
|
flags |= FORK_SHARECWD;
|
|
|
|
if (SCARG(uap, flags) & CLONE_FILES)
|
|
|
|
flags |= FORK_SHAREFILES;
|
|
|
|
if (SCARG(uap, flags) & CLONE_SIGHAND)
|
|
|
|
flags |= FORK_SHARESIGS;
|
|
|
|
if (SCARG(uap, flags) & CLONE_VFORK)
|
|
|
|
flags |= FORK_PPWAIT;
|
|
|
|
|
|
|
|
sig = SCARG(uap, flags) & CLONE_CSIGNAL;
|
|
|
|
if (sig < 0 || sig >= _NSIG)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that the Linux API does not provide a portable way of
|
|
|
|
* specifying the stack area; the caller must know if the stack
|
|
|
|
* grows up or down. So, we pass a stack size of 0, so that the
|
|
|
|
* code that makes this adjustment is a noop.
|
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
return (fork1(l, flags, sig, SCARG(uap, stack), 0,
|
2001-07-01 22:06:11 +04:00
|
|
|
NULL, NULL, retval, NULL));
|
|
|
|
}
|
|
|
|
|
2002-12-05 19:24:46 +03:00
|
|
|
/* print the 'table full' message once per 10 seconds */
|
|
|
|
struct timeval fork_tfmrate = { 10, 0 };
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* General fork call. Note that another LWP in the process may call exec()
|
|
|
|
* or exit() while we are forking. It's safe to continue here, because
|
|
|
|
* neither operation will complete until all LWPs have exited the process.
|
|
|
|
*/
|
1998-01-04 06:52:02 +03:00
|
|
|
int
|
2003-01-18 13:06:22 +03:00
|
|
|
fork1(struct lwp *l1, int flags, int exitsig, void *stack, size_t stacksize,
|
2000-08-01 08:57:28 +04:00
|
|
|
void (*func)(void *), void *arg, register_t *retval,
|
|
|
|
struct proc **rnewprocp)
|
1994-05-17 08:21:49 +04:00
|
|
|
{
|
2003-11-13 00:07:37 +03:00
|
|
|
struct proc *p1, *p2, *parent;
|
2007-09-29 16:22:30 +04:00
|
|
|
struct plimit *p1_lim;
|
2001-02-27 00:09:57 +03:00
|
|
|
uid_t uid;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l2;
|
2007-02-10 00:55:00 +03:00
|
|
|
int count;
|
2001-02-27 00:09:57 +03:00
|
|
|
vaddr_t uaddr;
|
2007-12-04 19:56:16 +03:00
|
|
|
int tnprocs;
|
2008-01-08 02:51:06 +03:00
|
|
|
int error = 0;
|
1994-05-17 08:21:49 +04:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
p1 = l1->l_proc;
|
2008-04-24 22:39:20 +04:00
|
|
|
uid = kauth_cred_getuid(l1->l_cred);
|
2007-12-04 19:56:16 +03:00
|
|
|
tnprocs = atomic_inc_uint_nv(&nprocs);
|
2008-01-08 02:51:06 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Although process entries are dynamically created, we still keep
|
|
|
|
* a global limit on the maximum number we will create.
|
|
|
|
*/
|
|
|
|
if (__predict_false(tnprocs >= maxproc))
|
|
|
|
error = -1;
|
|
|
|
else
|
2008-01-28 23:09:06 +03:00
|
|
|
error = kauth_authorize_process(l1->l_cred,
|
2008-01-08 02:51:06 +03:00
|
|
|
KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL);
|
|
|
|
|
|
|
|
if (error) {
|
2002-12-05 19:24:46 +03:00
|
|
|
static struct timeval lasttfm;
|
2007-12-04 19:56:16 +03:00
|
|
|
atomic_dec_uint(&nprocs);
|
2002-12-05 19:24:46 +03:00
|
|
|
if (ratecheck(&lasttfm, &fork_tfmrate))
|
|
|
|
tablefull("proc", "increase kern.maxproc or NPROC");
|
2002-12-11 21:09:07 +03:00
|
|
|
if (forkfsleep)
|
2008-05-27 21:49:07 +04:00
|
|
|
kpause("forkmx", false, forkfsleep, NULL);
|
1994-05-17 08:21:49 +04:00
|
|
|
return (EAGAIN);
|
|
|
|
}
|
1994-08-30 10:16:25 +04:00
|
|
|
|
1994-05-19 09:57:44 +04:00
|
|
|
/*
|
2007-12-04 01:37:34 +03:00
|
|
|
* Enforce limits.
|
1994-05-19 09:57:44 +04:00
|
|
|
*/
|
|
|
|
count = chgproccnt(uid, 1);
|
2009-03-25 00:00:05 +03:00
|
|
|
if (kauth_authorize_generic(l1->l_cred, KAUTH_GENERIC_ISSUSER, NULL) !=
|
|
|
|
0 && __predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) {
|
1994-05-19 09:57:44 +04:00
|
|
|
(void)chgproccnt(uid, -1);
|
2007-12-04 19:56:16 +03:00
|
|
|
atomic_dec_uint(&nprocs);
|
2002-12-11 21:09:07 +03:00
|
|
|
if (forkfsleep)
|
2008-05-27 21:49:07 +04:00
|
|
|
kpause("forkulim", false, forkfsleep, NULL);
|
1994-05-17 08:21:49 +04:00
|
|
|
return (EAGAIN);
|
1994-05-19 09:57:44 +04:00
|
|
|
}
|
|
|
|
|
1998-04-09 04:23:38 +04:00
|
|
|
/*
|
|
|
|
* Allocate virtual address space for the U-area now, while it
|
|
|
|
* is still easy to abort the fork operation if we're out of
|
2009-10-22 01:11:57 +04:00
|
|
|
* kernel virtual address space.
|
1998-04-09 04:23:38 +04:00
|
|
|
*/
|
2009-10-22 01:11:57 +04:00
|
|
|
uaddr = uvm_uarea_alloc();
|
2000-05-08 23:59:21 +04:00
|
|
|
if (__predict_false(uaddr == 0)) {
|
1998-04-09 04:23:38 +04:00
|
|
|
(void)chgproccnt(uid, -1);
|
2007-12-04 19:56:16 +03:00
|
|
|
atomic_dec_uint(&nprocs);
|
1998-04-09 04:23:38 +04:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are now committed to the fork. From here on, we may
|
|
|
|
* block on resources, but resource allocation may NOT fail.
|
|
|
|
*/
|
|
|
|
|
1994-05-19 09:57:44 +04:00
|
|
|
/* Allocate new proc. */
|
2003-03-19 14:36:32 +03:00
|
|
|
p2 = proc_alloc();
|
1998-09-09 03:50:13 +04:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
/*
|
|
|
|
* Make a proc table entry for the new process.
|
|
|
|
* Start by zeroing the section of proc that is zero-initialized,
|
|
|
|
* then copy the section that is copied directly from the parent.
|
|
|
|
*/
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(&p2->p_startzero, 0,
|
2007-03-04 08:59:00 +03:00
|
|
|
(unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero));
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memcpy(&p2->p_startcopy, &p1->p_startcopy,
|
2007-03-04 08:59:00 +03:00
|
|
|
(unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy));
|
1994-05-17 08:21:49 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
CIRCLEQ_INIT(&p2->p_sigpend.sp_info);
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
LIST_INIT(&p2->p_lwps);
|
2007-02-10 00:55:00 +03:00
|
|
|
LIST_INIT(&p2->p_sigwaiters);
|
2000-05-31 09:02:31 +04:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
/*
|
|
|
|
* Duplicate sub-structures as needed.
|
|
|
|
* Increase reference counts on shared objects.
|
2005-05-17 23:22:19 +04:00
|
|
|
* Inherit flags we want to keep. The flags related to SIGCHLD
|
|
|
|
* handling are important in order to keep a consistent behaviour
|
2011-01-19 02:56:48 +03:00
|
|
|
* for the child after the fork. If we are a 32-bit process, the
|
|
|
|
* child will be too.
|
1994-05-17 08:21:49 +04:00
|
|
|
*/
|
2011-01-19 02:56:48 +03:00
|
|
|
p2->p_flag =
|
|
|
|
p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32);
|
1994-08-30 10:16:25 +04:00
|
|
|
p2->p_emul = p1->p_emul;
|
2001-12-08 03:35:25 +03:00
|
|
|
p2->p_execsw = p1->p_execsw;
|
2001-01-10 02:36:50 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (flags & FORK_SYSTEM) {
|
|
|
|
/*
|
|
|
|
* Mark it as a system process. Set P_NOCLDWAIT so that
|
|
|
|
* children are reparented to init(8) when they exit.
|
|
|
|
* init(8) can easily wait them out for us.
|
|
|
|
*/
|
2007-02-18 01:31:36 +03:00
|
|
|
p2->p_flag |= (PK_SYSTEM | PK_NOCLDWAIT);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2006-05-15 01:15:11 +04:00
|
|
|
|
2007-12-05 10:06:50 +03:00
|
|
|
mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_init(&p2->p_reflock);
|
2007-02-10 00:55:00 +03:00
|
|
|
cv_init(&p2->p_waitcv, "wait");
|
|
|
|
cv_init(&p2->p_lwpcv, "lwpwait");
|
|
|
|
|
2008-04-24 22:39:20 +04:00
|
|
|
/*
|
|
|
|
* Share a lock between the processes if they are to share signal
|
|
|
|
* state: we must synchronize access to it.
|
|
|
|
*/
|
|
|
|
if (flags & FORK_SHARESIGS) {
|
|
|
|
p2->p_lock = p1->p_lock;
|
|
|
|
mutex_obj_hold(p1->p_lock);
|
|
|
|
} else
|
|
|
|
p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
|
2007-01-15 20:45:32 +03:00
|
|
|
kauth_proc_fork(p1, p2);
|
1994-05-17 08:21:49 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
p2->p_raslist = NULL;
|
2002-08-28 11:16:33 +04:00
|
|
|
#if defined(__HAVE_RAS)
|
|
|
|
ras_fork(p1, p2);
|
|
|
|
#endif
|
|
|
|
|
1994-05-19 09:57:44 +04:00
|
|
|
/* bump references to the text vnode (for procfs) */
|
|
|
|
p2->p_textvp = p1->p_textvp;
|
|
|
|
if (p2->p_textvp)
|
2010-01-08 14:35:07 +03:00
|
|
|
vref(p2->p_textvp);
|
1994-05-17 08:21:49 +04:00
|
|
|
|
1999-05-01 01:39:51 +04:00
|
|
|
if (flags & FORK_SHAREFILES)
|
2008-03-22 00:54:58 +03:00
|
|
|
fd_share(p2);
|
2002-08-06 17:58:08 +04:00
|
|
|
else if (flags & FORK_CLEANFILES)
|
2008-03-22 00:54:58 +03:00
|
|
|
p2->p_fd = fd_init(NULL);
|
1999-05-01 01:39:51 +04:00
|
|
|
else
|
2008-03-22 00:54:58 +03:00
|
|
|
p2->p_fd = fd_copy();
|
1999-05-01 01:39:51 +04:00
|
|
|
|
2011-04-25 00:17:53 +04:00
|
|
|
/* XXX racy */
|
|
|
|
p2->p_mqueue_cnt = p1->p_mqueue_cnt;
|
|
|
|
|
1999-05-01 01:39:51 +04:00
|
|
|
if (flags & FORK_SHARECWD)
|
2008-03-22 00:54:58 +03:00
|
|
|
cwdshare(p2);
|
1999-05-01 01:39:51 +04:00
|
|
|
else
|
2008-03-22 00:54:58 +03:00
|
|
|
p2->p_cwdi = cwdinit();
|
1999-04-30 22:42:58 +04:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
/*
|
2011-05-01 05:15:18 +04:00
|
|
|
* Note: p_limit (rlimit stuff) is copy-on-write, so normally
|
|
|
|
* we just need increase pl_refcnt.
|
1994-05-17 08:21:49 +04:00
|
|
|
*/
|
2007-09-29 16:22:30 +04:00
|
|
|
p1_lim = p1->p_limit;
|
2011-05-01 05:15:18 +04:00
|
|
|
if (!p1_lim->pl_writeable) {
|
2007-09-29 16:22:30 +04:00
|
|
|
lim_addref(p1_lim);
|
|
|
|
p2->p_limit = p1_lim;
|
2011-05-01 05:15:18 +04:00
|
|
|
} else {
|
|
|
|
p2->p_limit = lim_copy(p1_lim);
|
1994-05-17 08:21:49 +04:00
|
|
|
}
|
|
|
|
|
2008-06-16 13:51:14 +04:00
|
|
|
p2->p_lflag = ((flags & FORK_PPWAIT) ? PL_PPWAIT : 0);
|
|
|
|
p2->p_sflag = 0;
|
2007-02-10 00:55:00 +03:00
|
|
|
p2->p_slflag = 0;
|
2003-11-13 00:07:37 +03:00
|
|
|
parent = (flags & FORK_NOWAIT) ? initproc : p1;
|
|
|
|
p2->p_pptr = parent;
|
2008-06-02 20:18:09 +04:00
|
|
|
p2->p_ppid = parent->p_pid;
|
1994-08-30 07:04:28 +04:00
|
|
|
LIST_INIT(&p2->p_children);
|
|
|
|
|
2007-04-30 18:44:28 +04:00
|
|
|
p2->p_aio = NULL;
|
2003-03-19 14:36:32 +03:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
#ifdef KTRACE
|
|
|
|
/*
|
|
|
|
* Copy traceflag and tracefile if enabled.
|
|
|
|
* If not inherited, these were zeroed above.
|
|
|
|
*/
|
2001-01-10 02:36:50 +03:00
|
|
|
if (p1->p_traceflag & KTRFAC_INHERIT) {
|
2007-08-15 16:07:23 +04:00
|
|
|
mutex_enter(&ktrace_lock);
|
1994-05-17 08:21:49 +04:00
|
|
|
p2->p_traceflag = p1->p_traceflag;
|
|
|
|
if ((p2->p_tracep = p1->p_tracep) != NULL)
|
1998-05-02 22:33:19 +04:00
|
|
|
ktradref(p2);
|
2007-08-15 16:07:23 +04:00
|
|
|
mutex_exit(&ktrace_lock);
|
1994-05-17 08:21:49 +04:00
|
|
|
}
|
|
|
|
#endif
|
2001-01-10 02:36:50 +03:00
|
|
|
|
1999-05-01 01:23:49 +04:00
|
|
|
/*
|
|
|
|
* Create signal actions for the child process.
|
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
p2->p_sigacts = sigactsinit(p1, flags & FORK_SHARESIGS);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p1->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
p2->p_sflag |=
|
|
|
|
(p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP));
|
2007-05-17 18:51:11 +04:00
|
|
|
sched_proc_fork(p1, p2);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p1->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
p2->p_stflag = p1->p_stflag;
|
1999-05-01 01:23:49 +04:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
/*
|
2005-02-27 00:34:55 +03:00
|
|
|
* p_stats.
|
2003-01-18 13:06:22 +03:00
|
|
|
* Copy parts of p_stats, and zero out the rest.
|
|
|
|
*/
|
|
|
|
p2->p_stats = pstatscopy(p1->p_stats);
|
|
|
|
|
2000-11-07 15:41:52 +03:00
|
|
|
/*
|
2010-07-07 05:30:32 +04:00
|
|
|
* Set up the new process address space.
|
2000-11-07 15:41:52 +03:00
|
|
|
*/
|
2007-02-22 09:34:42 +03:00
|
|
|
uvm_proc_fork(p1, p2, (flags & FORK_SHAREVM) ? true : false);
|
1995-12-09 07:23:07 +03:00
|
|
|
|
|
|
|
/*
|
2003-03-19 14:36:32 +03:00
|
|
|
* Finish creating the child process.
|
|
|
|
* It will return through a different path later.
|
1995-12-09 07:23:07 +03:00
|
|
|
*/
|
2009-10-22 01:11:57 +04:00
|
|
|
lwp_create(l1, p2, uaddr, (flags & FORK_PPWAIT) ? LWP_VFORK : 0,
|
2008-06-02 17:58:07 +04:00
|
|
|
stack, stacksize, (func != NULL) ? func : child_return, arg, &l2,
|
2007-11-06 03:42:39 +03:00
|
|
|
l1->l_class);
|
2011-08-23 17:01:25 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Inherit l_private from the parent.
|
|
|
|
* Note that we cannot use lwp_setprivate() here since that
|
|
|
|
* also sets the CPU TLS register, which is incorrect if the
|
|
|
|
* process has changed that without letting the kernel know.
|
|
|
|
*/
|
|
|
|
l2->l_private = l1->l_private;
|
1994-05-17 08:21:49 +04:00
|
|
|
|
2010-07-07 05:30:32 +04:00
|
|
|
/*
|
|
|
|
* If emulation has a process fork hook, call it now.
|
|
|
|
*/
|
|
|
|
if (p2->p_emul->e_proc_fork)
|
|
|
|
(*p2->p_emul->e_proc_fork)(p2, l1, flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ...and finally, any other random fork hooks that subsystems
|
|
|
|
* might have registered.
|
|
|
|
*/
|
|
|
|
doforkhooks(p2, p1);
|
|
|
|
|
|
|
|
SDT_PROBE(proc,,,create, p2, p1, flags, 0, 0);
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* It's now safe for the scheduler and other processes to see the
|
|
|
|
* child process.
|
|
|
|
*/
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_enter(proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT)
|
|
|
|
p2->p_lflag |= PL_CONTROLT;
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&parent->p_children, p2, p_sibling);
|
2002-10-21 21:37:53 +04:00
|
|
|
p2->p_exitsig = exitsig; /* signal for parent on exit */
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
LIST_INSERT_AFTER(p1, p2, p_pglist);
|
2007-12-03 23:26:24 +03:00
|
|
|
LIST_INSERT_HEAD(&allproc, p2, p_list);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-24 21:30:07 +03:00
|
|
|
p2->p_trace_enabled = trace_is_enabled(p2);
|
2002-11-13 03:51:02 +03:00
|
|
|
#ifdef __HAVE_SYSCALL_INTERN
|
|
|
|
(*p2->p_emul->e_syscall_intern)(p2);
|
|
|
|
#endif
|
|
|
|
|
1998-01-04 06:52:02 +03:00
|
|
|
/*
|
|
|
|
* Update stats now that we know the fork was successful.
|
|
|
|
*/
|
1998-02-05 10:59:28 +03:00
|
|
|
uvmexp.forks++;
|
|
|
|
if (flags & FORK_PPWAIT)
|
|
|
|
uvmexp.forks_ppwait++;
|
|
|
|
if (flags & FORK_SHAREVM)
|
|
|
|
uvmexp.forks_sharevm++;
|
1998-01-04 06:52:02 +03:00
|
|
|
|
1998-01-05 08:16:26 +03:00
|
|
|
/*
|
|
|
|
* Pass a pointer to the new process to the caller.
|
|
|
|
*/
|
|
|
|
if (rnewprocp != NULL)
|
|
|
|
*rnewprocp = p2;
|
|
|
|
|
2007-08-15 16:07:23 +04:00
|
|
|
if (ktrpoint(KTR_EMUL))
|
2004-02-13 02:47:21 +03:00
|
|
|
p2->p_traceflag |= KTRFAC_TRC_EMUL;
|
2000-12-10 14:41:20 +03:00
|
|
|
|
2008-04-24 19:35:27 +04:00
|
|
|
/*
|
|
|
|
* Notify any interested parties about the new process.
|
|
|
|
*/
|
|
|
|
if (!SLIST_EMPTY(&p1->p_klist)) {
|
|
|
|
mutex_exit(proc_lock);
|
|
|
|
KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
|
|
|
|
mutex_enter(proc_lock);
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* Make child runnable, set start time, and add to run queue except
|
|
|
|
* if the parent requested the child to start in SSTOP state.
|
|
|
|
*/
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p2->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-27 18:18:51 +04:00
|
|
|
/*
|
|
|
|
* Start profiling.
|
|
|
|
*/
|
|
|
|
if ((p2->p_stflag & PST_PROFIL) != 0) {
|
|
|
|
mutex_spin_enter(&p2->p_stmutex);
|
|
|
|
startprofclock(p2);
|
|
|
|
mutex_spin_exit(&p2->p_stmutex);
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
getmicrotime(&p2->p_stats->p_start);
|
|
|
|
p2->p_acflag = AFORK;
|
2008-05-27 18:18:51 +04:00
|
|
|
lwp_lock(l2);
|
2010-06-13 08:13:31 +04:00
|
|
|
KASSERT(p2->p_nrlwps == 1);
|
2007-02-10 00:55:00 +03:00
|
|
|
if (p2->p_sflag & PS_STOPFORK) {
|
2011-05-14 22:50:07 +04:00
|
|
|
struct schedstate_percpu *spc = &l2->l_cpu->ci_schedstate;
|
2007-02-10 00:55:00 +03:00
|
|
|
p2->p_nrlwps = 0;
|
|
|
|
p2->p_stat = SSTOP;
|
|
|
|
p2->p_waited = 0;
|
|
|
|
p1->p_nstopchild++;
|
|
|
|
l2->l_stat = LSSTOP;
|
2011-05-14 22:50:07 +04:00
|
|
|
KASSERT(l2->l_wchan == NULL);
|
|
|
|
lwp_unlock_to(l2, spc->spc_lwplock);
|
2007-02-10 00:55:00 +03:00
|
|
|
} else {
|
|
|
|
p2->p_nrlwps = 1;
|
|
|
|
p2->p_stat = SACTIVE;
|
|
|
|
l2->l_stat = LSRUN;
|
2007-05-17 18:51:11 +04:00
|
|
|
sched_enqueue(l2, false);
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_unlock(l2);
|
|
|
|
}
|
2008-05-31 17:04:14 +04:00
|
|
|
mutex_exit(p2->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
/*
|
1994-05-19 09:57:44 +04:00
|
|
|
* Preserve synchronization semantics of vfork. If waiting for
|
2008-06-16 13:51:14 +04:00
|
|
|
* child to exec or exit, set PL_PPWAIT on child, and sleep on our
|
1994-05-19 09:57:44 +04:00
|
|
|
* proc (in case of exit).
|
1994-05-17 08:21:49 +04:00
|
|
|
*/
|
2008-06-16 13:51:14 +04:00
|
|
|
while (p2->p_lflag & PL_PPWAIT)
|
|
|
|
cv_wait(&p1->p_waitcv, proc_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-31 17:04:14 +04:00
|
|
|
mutex_exit(proc_lock);
|
1994-05-17 08:21:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return child pid to parent process,
|
|
|
|
* marking us as parent via retval[1].
|
|
|
|
*/
|
1998-01-07 00:15:41 +03:00
|
|
|
if (retval != NULL) {
|
|
|
|
retval[0] = p2->p_pid;
|
|
|
|
retval[1] = 0;
|
|
|
|
}
|
2000-11-07 15:31:17 +03:00
|
|
|
|
1994-05-17 08:21:49 +04:00
|
|
|
return (0);
|
|
|
|
}
|