1 /* $NetBSD: kern_fork.c,v 1.174 2009/10/21 21:12:06 rmind Exp $ */
4 * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.174 2009/10/21 21:12:06 rmind Exp $");
72 #include "opt_ktrace.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/filedesc.h>
77 #include <sys/kernel.h>
79 #include <sys/mount.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vnode.h>
86 #include <sys/ktrace.h>
87 #include <sys/vmmeter.h>
88 #include <sys/sched.h>
89 #include <sys/signalvar.h>
90 #include <sys/kauth.h>
91 #include <sys/atomic.h>
92 #include <sys/syscallargs.h>
93 #include <sys/uidinfo.h>
95 #include <uvm/uvm_extern.h>
97 u_int nprocs
= 1; /* process 0 */
100 * Number of ticks to sleep if fork() would fail due to process hitting
101 * limits. Exported in miliseconds to userland via sysctl.
107 sys_fork(struct lwp
*l
, const void *v
, register_t
*retval
)
110 return (fork1(l
, 0, SIGCHLD
, NULL
, 0, NULL
, NULL
, retval
, NULL
));
114 * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
115 * Address space is not shared, but parent is blocked until child exit.
119 sys_vfork(struct lwp
*l
, const void *v
, register_t
*retval
)
122 return (fork1(l
, FORK_PPWAIT
, SIGCHLD
, NULL
, 0, NULL
, NULL
,
127 * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
128 * semantics. Address space is shared, and parent is blocked until child exit.
132 sys___vfork14(struct lwp
*l
, const void *v
, register_t
*retval
)
135 return (fork1(l
, FORK_PPWAIT
|FORK_SHAREVM
, SIGCHLD
, NULL
, 0,
136 NULL
, NULL
, retval
, NULL
));
140 * Linux-compatible __clone(2) system call.
143 sys___clone(struct lwp
*l
, const struct sys___clone_args
*uap
, register_t
*retval
)
146 syscallarg(int) flags;
147 syscallarg(void *) stack;
152 * We don't support the CLONE_PID or CLONE_PTRACE flags.
154 if (SCARG(uap
, flags
) & (CLONE_PID
|CLONE_PTRACE
))
158 * Linux enforces CLONE_VM with CLONE_SIGHAND, do same.
160 if (SCARG(uap
, flags
) & CLONE_SIGHAND
161 && (SCARG(uap
, flags
) & CLONE_VM
) == 0)
166 if (SCARG(uap
, flags
) & CLONE_VM
)
167 flags
|= FORK_SHAREVM
;
168 if (SCARG(uap
, flags
) & CLONE_FS
)
169 flags
|= FORK_SHARECWD
;
170 if (SCARG(uap
, flags
) & CLONE_FILES
)
171 flags
|= FORK_SHAREFILES
;
172 if (SCARG(uap
, flags
) & CLONE_SIGHAND
)
173 flags
|= FORK_SHARESIGS
;
174 if (SCARG(uap
, flags
) & CLONE_VFORK
)
175 flags
|= FORK_PPWAIT
;
177 sig
= SCARG(uap
, flags
) & CLONE_CSIGNAL
;
178 if (sig
< 0 || sig
>= _NSIG
)
182 * Note that the Linux API does not provide a portable way of
183 * specifying the stack area; the caller must know if the stack
184 * grows up or down. So, we pass a stack size of 0, so that the
185 * code that makes this adjustment is a noop.
187 return (fork1(l
, flags
, sig
, SCARG(uap
, stack
), 0,
188 NULL
, NULL
, retval
, NULL
));
191 /* print the 'table full' message once per 10 seconds */
192 struct timeval fork_tfmrate
= { 10, 0 };
195 * General fork call. Note that another LWP in the process may call exec()
196 * or exit() while we are forking. It's safe to continue here, because
197 * neither operation will complete until all LWPs have exited the process.
200 fork1(struct lwp
*l1
, int flags
, int exitsig
, void *stack
, size_t stacksize
,
201 void (*func
)(void *), void *arg
, register_t
*retval
,
202 struct proc
**rnewprocp
)
204 struct proc
*p1
, *p2
, *parent
;
205 struct plimit
*p1_lim
;
215 uid
= kauth_cred_getuid(l1
->l_cred
);
216 tnprocs
= atomic_inc_uint_nv(&nprocs
);
219 * Although process entries are dynamically created, we still keep
220 * a global limit on the maximum number we will create.
222 if (__predict_false(tnprocs
>= maxproc
))
225 error
= kauth_authorize_process(l1
->l_cred
,
226 KAUTH_PROCESS_FORK
, p1
, KAUTH_ARG(tnprocs
), NULL
, NULL
);
229 static struct timeval lasttfm
;
230 atomic_dec_uint(&nprocs
);
231 if (ratecheck(&lasttfm
, &fork_tfmrate
))
232 tablefull("proc", "increase kern.maxproc or NPROC");
234 kpause("forkmx", false, forkfsleep
, NULL
);
241 count
= chgproccnt(uid
, 1);
242 if (kauth_authorize_generic(l1
->l_cred
, KAUTH_GENERIC_ISSUSER
, NULL
) !=
243 0 && __predict_false(count
> p1
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
)) {
244 (void)chgproccnt(uid
, -1);
245 atomic_dec_uint(&nprocs
);
247 kpause("forkulim", false, forkfsleep
, NULL
);
252 * Allocate virtual address space for the U-area now, while it
253 * is still easy to abort the fork operation if we're out of
254 * kernel virtual address space.
256 uaddr
= uvm_uarea_alloc();
257 if (__predict_false(uaddr
== 0)) {
258 (void)chgproccnt(uid
, -1);
259 atomic_dec_uint(&nprocs
);
264 * We are now committed to the fork. From here on, we may
265 * block on resources, but resource allocation may NOT fail.
268 /* Allocate new proc. */
272 * Make a proc table entry for the new process.
273 * Start by zeroing the section of proc that is zero-initialized,
274 * then copy the section that is copied directly from the parent.
276 memset(&p2
->p_startzero
, 0,
277 (unsigned) ((char *)&p2
->p_endzero
- (char *)&p2
->p_startzero
));
278 memcpy(&p2
->p_startcopy
, &p1
->p_startcopy
,
279 (unsigned) ((char *)&p2
->p_endcopy
- (char *)&p2
->p_startcopy
));
281 CIRCLEQ_INIT(&p2
->p_sigpend
.sp_info
);
283 LIST_INIT(&p2
->p_lwps
);
284 LIST_INIT(&p2
->p_sigwaiters
);
287 * Duplicate sub-structures as needed.
288 * Increase reference counts on shared objects.
289 * Inherit flags we want to keep. The flags related to SIGCHLD
290 * handling are important in order to keep a consistent behaviour
291 * for the child after the fork.
293 p2
->p_flag
= p1
->p_flag
& (PK_SUGID
| PK_NOCLDWAIT
| PK_CLDSIGIGN
);
294 p2
->p_emul
= p1
->p_emul
;
295 p2
->p_execsw
= p1
->p_execsw
;
297 if (flags
& FORK_SYSTEM
) {
299 * Mark it as a system process. Set P_NOCLDWAIT so that
300 * children are reparented to init(8) when they exit.
301 * init(8) can easily wait them out for us.
303 p2
->p_flag
|= (PK_SYSTEM
| PK_NOCLDWAIT
);
306 mutex_init(&p2
->p_stmutex
, MUTEX_DEFAULT
, IPL_HIGH
);
307 mutex_init(&p2
->p_auxlock
, MUTEX_DEFAULT
, IPL_NONE
);
308 rw_init(&p2
->p_reflock
);
309 cv_init(&p2
->p_waitcv
, "wait");
310 cv_init(&p2
->p_lwpcv
, "lwpwait");
313 * Share a lock between the processes if they are to share signal
314 * state: we must synchronize access to it.
316 if (flags
& FORK_SHARESIGS
) {
317 p2
->p_lock
= p1
->p_lock
;
318 mutex_obj_hold(p1
->p_lock
);
320 p2
->p_lock
= mutex_obj_alloc(MUTEX_DEFAULT
, IPL_NONE
);
322 kauth_proc_fork(p1
, p2
);
324 p2
->p_raslist
= NULL
;
325 #if defined(__HAVE_RAS)
329 /* bump references to the text vnode (for procfs) */
330 p2
->p_textvp
= p1
->p_textvp
;
334 if (flags
& FORK_SHAREFILES
)
336 else if (flags
& FORK_CLEANFILES
)
337 p2
->p_fd
= fd_init(NULL
);
339 p2
->p_fd
= fd_copy();
341 if (flags
& FORK_SHARECWD
)
344 p2
->p_cwdi
= cwdinit();
347 * p_limit (rlimit stuff) is usually copy-on-write, so we just need
349 * However in some cases (see compat irix, and plausibly from clone)
350 * the parent and child share limits - in which case nothing else
351 * must have a copy of the limits (PL_SHAREMOD is set).
353 if (__predict_false(flags
& FORK_SHARELIMIT
))
354 lim_privatise(p1
, 1);
355 p1_lim
= p1
->p_limit
;
356 if (p1_lim
->pl_flags
& PL_WRITEABLE
&& !(flags
& FORK_SHARELIMIT
))
357 p2
->p_limit
= lim_copy(p1_lim
);
360 p2
->p_limit
= p1_lim
;
363 p2
->p_lflag
= ((flags
& FORK_PPWAIT
) ? PL_PPWAIT
: 0);
366 parent
= (flags
& FORK_NOWAIT
) ? initproc
: p1
;
368 p2
->p_ppid
= parent
->p_pid
;
369 LIST_INIT(&p2
->p_children
);
375 * Copy traceflag and tracefile if enabled.
376 * If not inherited, these were zeroed above.
378 if (p1
->p_traceflag
& KTRFAC_INHERIT
) {
379 mutex_enter(&ktrace_lock
);
380 p2
->p_traceflag
= p1
->p_traceflag
;
381 if ((p2
->p_tracep
= p1
->p_tracep
) != NULL
)
383 mutex_exit(&ktrace_lock
);
388 * Create signal actions for the child process.
390 p2
->p_sigacts
= sigactsinit(p1
, flags
& FORK_SHARESIGS
);
391 mutex_enter(p1
->p_lock
);
393 (p1
->p_sflag
& (PS_STOPFORK
| PS_STOPEXEC
| PS_NOCLDSTOP
));
394 sched_proc_fork(p1
, p2
);
395 mutex_exit(p1
->p_lock
);
397 p2
->p_stflag
= p1
->p_stflag
;
401 * Copy parts of p_stats, and zero out the rest.
403 p2
->p_stats
= pstatscopy(p1
->p_stats
);
406 * If emulation has process fork hook, call it now.
408 if (p2
->p_emul
->e_proc_fork
)
409 (*p2
->p_emul
->e_proc_fork
)(p2
, p1
, flags
);
412 * ...and finally, any other random fork hooks that subsystems
413 * might have registered.
417 uvm_proc_fork(p1
, p2
, (flags
& FORK_SHAREVM
) ? true : false);
420 * Finish creating the child process.
421 * It will return through a different path later.
423 lwp_create(l1
, p2
, uaddr
, (flags
& FORK_PPWAIT
) ? LWP_VFORK
: 0,
424 stack
, stacksize
, (func
!= NULL
) ? func
: child_return
, arg
, &l2
,
428 * It's now safe for the scheduler and other processes to see the
431 mutex_enter(proc_lock
);
433 if (p1
->p_session
->s_ttyvp
!= NULL
&& p1
->p_lflag
& PL_CONTROLT
)
434 p2
->p_lflag
|= PL_CONTROLT
;
436 LIST_INSERT_HEAD(&parent
->p_children
, p2
, p_sibling
);
437 p2
->p_exitsig
= exitsig
; /* signal for parent on exit */
439 LIST_INSERT_AFTER(p1
, p2
, p_pglist
);
440 LIST_INSERT_HEAD(&allproc
, p2
, p_list
);
442 p2
->p_trace_enabled
= trace_is_enabled(p2
);
443 #ifdef __HAVE_SYSCALL_INTERN
444 (*p2
->p_emul
->e_syscall_intern
)(p2
);
448 * Update stats now that we know the fork was successful.
451 if (flags
& FORK_PPWAIT
)
452 uvmexp
.forks_ppwait
++;
453 if (flags
& FORK_SHAREVM
)
454 uvmexp
.forks_sharevm
++;
457 * Pass a pointer to the new process to the caller.
459 if (rnewprocp
!= NULL
)
462 if (ktrpoint(KTR_EMUL
))
463 p2
->p_traceflag
|= KTRFAC_TRC_EMUL
;
466 * Notify any interested parties about the new process.
468 if (!SLIST_EMPTY(&p1
->p_klist
)) {
469 mutex_exit(proc_lock
);
470 KNOTE(&p1
->p_klist
, NOTE_FORK
| p2
->p_pid
);
471 mutex_enter(proc_lock
);
475 * Make child runnable, set start time, and add to run queue except
476 * if the parent requested the child to start in SSTOP state.
478 tmp
= (p2
->p_userret
!= NULL
? LW_WUSERRET
: 0);
479 mutex_enter(p2
->p_lock
);
484 if ((p2
->p_stflag
& PST_PROFIL
) != 0) {
485 mutex_spin_enter(&p2
->p_stmutex
);
487 mutex_spin_exit(&p2
->p_stmutex
);
490 getmicrotime(&p2
->p_stats
->p_start
);
491 p2
->p_acflag
= AFORK
;
493 if (p2
->p_sflag
& PS_STOPFORK
) {
503 p2
->p_stat
= SACTIVE
;
506 sched_enqueue(l2
, false);
510 mutex_exit(p2
->p_lock
);
513 * Preserve synchronization semantics of vfork. If waiting for
514 * child to exec or exit, set PL_PPWAIT on child, and sleep on our
515 * proc (in case of exit).
517 while (p2
->p_lflag
& PL_PPWAIT
)
518 cv_wait(&p1
->p_waitcv
, proc_lock
);
520 mutex_exit(proc_lock
);
523 * Return child pid to parent process,
524 * marking us as parent via retval[1].
526 if (retval
!= NULL
) {
527 retval
[0] = p2
->p_pid
;