xref: /netbsd-src/sys/kern/kern_fork.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: kern_fork.c,v 1.53 1999/02/23 02:57:18 ross Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)kern_fork.c	8.8 (Berkeley) 2/14/95
41  */
42 
43 #include "opt_ktrace.h"
44 #include "opt_uvm.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/map.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/pool.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
57 #include <sys/file.h>
58 #include <sys/acct.h>
59 #include <sys/ktrace.h>
60 #include <sys/vmmeter.h>
61 #include <sys/sched.h>
62 
63 #include <sys/syscallargs.h>
64 
65 #include <vm/vm.h>
66 #include <vm/vm_kern.h>
67 
68 #if defined(UVM)
69 #include <uvm/uvm_extern.h>
70 #endif
71 
72 int	nprocs = 1;		/* process 0 */
73 
74 /*ARGSUSED*/
75 int
76 sys_fork(p, v, retval)
77 	struct proc *p;
78 	void *v;
79 	register_t *retval;
80 {
81 
82 	return (fork1(p, 0, retval, NULL));
83 }
84 
85 /*
86  * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
87  * Address space is not shared, but parent is blocked until child exit.
88  */
89 /*ARGSUSED*/
90 int
91 sys_vfork(p, v, retval)
92 	struct proc *p;
93 	void *v;
94 	register_t *retval;
95 {
96 
97 	return (fork1(p, FORK_PPWAIT, retval, NULL));
98 }
99 
100 /*
101  * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
102  * semantics.  Address space is shared, and parent is blocked until child exit.
103  */
104 /*ARGSUSED*/
105 int
106 sys___vfork14(p, v, retval)
107 	struct proc *p;
108 	void *v;
109 	register_t *retval;
110 {
111 
112 	return (fork1(p, FORK_PPWAIT|FORK_SHAREVM, retval, NULL));
113 }
114 
115 int
116 fork1(p1, flags, retval, rnewprocp)
117 	register struct proc *p1;
118 	int flags;
119 	register_t *retval;
120 	struct proc **rnewprocp;
121 {
122 	register struct proc *p2;
123 	register uid_t uid;
124 	struct proc *newproc;
125 	int count, s;
126 	vaddr_t uaddr;
127 	static int nextpid, pidchecked = 0;
128 
129 	/*
130 	 * Although process entries are dynamically created, we still keep
131 	 * a global limit on the maximum number we will create.  Don't allow
132 	 * a nonprivileged user to use the last process; don't let root
133 	 * exceed the limit. The variable nprocs is the current number of
134 	 * processes, maxproc is the limit.
135 	 */
136 	uid = p1->p_cred->p_ruid;
137 	if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
138 		tablefull("proc");
139 		return (EAGAIN);
140 	}
141 
142 	/*
143 	 * Increment the count of procs running with this uid. Don't allow
144 	 * a nonprivileged user to exceed their current limit.
145 	 */
146 	count = chgproccnt(uid, 1);
147 	if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
148 		(void)chgproccnt(uid, -1);
149 		return (EAGAIN);
150 	}
151 
152 	/*
153 	 * Allocate virtual address space for the U-area now, while it
154 	 * is still easy to abort the fork operation if we're out of
155 	 * kernel virtual address space.  The actual U-area pages will
156 	 * be allocated and wired in vm_fork().
157 	 */
158 #if defined(UVM)
159 	uaddr = uvm_km_valloc(kernel_map, USPACE);
160 #else
161 	uaddr = kmem_alloc_pageable(kernel_map, USPACE);
162 #endif
163 	if (uaddr == 0) {
164 		(void)chgproccnt(uid, -1);
165 		return (ENOMEM);
166 	}
167 
168 	/*
169 	 * We are now committed to the fork.  From here on, we may
170 	 * block on resources, but resource allocation may NOT fail.
171 	 */
172 
173 	/* Allocate new proc. */
174 	newproc = pool_get(&proc_pool, PR_WAITOK);
175 
176 	/*
177 	 * BEGIN PID ALLOCATION.  (Lock PID allocation variables eventually).
178 	 */
179 
180 	/*
181 	 * Find an unused process ID.  We remember a range of unused IDs
182 	 * ready to use (from nextpid+1 through pidchecked-1).
183 	 */
184 	nextpid++;
185 retry:
186 	/*
187 	 * If the process ID prototype has wrapped around,
188 	 * restart somewhat above 0, as the low-numbered procs
189 	 * tend to include daemons that don't exit.
190 	 */
191 	if (nextpid >= PID_MAX) {
192 		nextpid = 100;
193 		pidchecked = 0;
194 	}
195 	if (nextpid >= pidchecked) {
196 		const struct proclist_desc *pd;
197 
198 		pidchecked = PID_MAX;
199 		/*
200 		 * Scan the process lists to check whether this pid
201 		 * is in use.  Remember the lowest pid that's greater
202 		 * than nextpid, so we can avoid checking for a while.
203 		 */
204 		pd = proclists;
205 again:
206 		for (p2 = LIST_FIRST(pd->pd_list); p2 != 0;
207 		     p2 = LIST_NEXT(p2, p_list)) {
208 			while (p2->p_pid == nextpid ||
209 			    p2->p_pgrp->pg_id == nextpid ||
210 			    p2->p_session->s_sid == nextpid) {
211 				nextpid++;
212 				if (nextpid >= pidchecked)
213 					goto retry;
214 			}
215 			if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
216 				pidchecked = p2->p_pid;
217 
218 			if (p2->p_pgrp->pg_id > nextpid &&
219 			    pidchecked > p2->p_pgrp->pg_id)
220 				pidchecked = p2->p_pgrp->pg_id;
221 
222 			if (p2->p_session->s_sid > nextpid &&
223 			    pidchecked > p2->p_session->s_sid)
224 				pidchecked = p2->p_session->s_sid;
225 		}
226 
227 		/*
228 		 * If there's another list, scan it.  If we have checked
229 		 * them all, we've found one!
230 		 */
231 		pd++;
232 		if (pd->pd_list != NULL)
233 			goto again;
234 	}
235 
236 	nprocs++;
237 	p2 = newproc;
238 
239 	/* Record the pid we've allocated. */
240 	p2->p_pid = nextpid;
241 
242 	/*
243 	 * Put the proc on allproc before unlocking PID allocation
244 	 * so that waiters won't grab it as soon as we unlock.
245 	 */
246 	LIST_INSERT_HEAD(&allproc, p2, p_list);
247 
248 	/*
249 	 * END PID ALLOCATION.  (Unlock PID allocation variables).
250 	 */
251 
252 	p2->p_stat = SIDL;			/* protect against others */
253 	p2->p_forw = p2->p_back = NULL;		/* shouldn't be necessary */
254 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
255 
256 	/*
257 	 * Make a proc table entry for the new process.
258 	 * Start by zeroing the section of proc that is zero-initialized,
259 	 * then copy the section that is copied directly from the parent.
260 	 */
261 	memset(&p2->p_startzero, 0,
262 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
263 	memcpy(&p2->p_startcopy, &p1->p_startcopy,
264 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
265 
266 	/*
267 	 * Duplicate sub-structures as needed.
268 	 * Increase reference counts on shared objects.
269 	 * The p_stats and p_sigacts substructs are set in vm_fork.
270 	 */
271 	p2->p_flag = P_INMEM | (p1->p_flag & P_SUGID);
272 	p2->p_emul = p1->p_emul;
273 	if (p1->p_flag & P_PROFIL)
274 		startprofclock(p2);
275 	p2->p_cred = pool_get(&pcred_pool, PR_WAITOK);
276 	memcpy(p2->p_cred, p1->p_cred, sizeof(*p2->p_cred));
277 	p2->p_cred->p_refcnt = 1;
278 	crhold(p1->p_ucred);
279 
280 	/* bump references to the text vnode (for procfs) */
281 	p2->p_textvp = p1->p_textvp;
282 	if (p2->p_textvp)
283 		VREF(p2->p_textvp);
284 
285 	p2->p_fd = fdcopy(p1);
286 	/*
287 	 * If p_limit is still copy-on-write, bump refcnt,
288 	 * otherwise get a copy that won't be modified.
289 	 * (If PL_SHAREMOD is clear, the structure is shared
290 	 * copy-on-write.)
291 	 */
292 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
293 		p2->p_limit = limcopy(p1->p_limit);
294 	else {
295 		p2->p_limit = p1->p_limit;
296 		p2->p_limit->p_refcnt++;
297 	}
298 
299 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
300 		p2->p_flag |= P_CONTROLT;
301 	if (flags & FORK_PPWAIT)
302 		p2->p_flag |= P_PPWAIT;
303 	LIST_INSERT_AFTER(p1, p2, p_pglist);
304 	p2->p_pptr = p1;
305 	LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
306 	LIST_INIT(&p2->p_children);
307 
308 #ifdef KTRACE
309 	/*
310 	 * Copy traceflag and tracefile if enabled.
311 	 * If not inherited, these were zeroed above.
312 	 */
313 	if (p1->p_traceflag&KTRFAC_INHERIT) {
314 		p2->p_traceflag = p1->p_traceflag;
315 		if ((p2->p_tracep = p1->p_tracep) != NULL)
316 			ktradref(p2);
317 	}
318 #endif
319 	scheduler_fork_hook(p1, p2);
320 
321 	/*
322 	 * This begins the section where we must prevent the parent
323 	 * from being swapped.
324 	 */
325 	PHOLD(p1);
326 
327 	/*
328 	 * Finish creating the child process.  It will return through a
329 	 * different path later.
330 	 */
331 	p2->p_addr = (struct user *)uaddr;
332 #if defined(UVM)
333 	uvm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE);
334 #else
335 	vm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE);
336 #endif
337 
338 	/*
339 	 * Make child runnable, set start time, and add to run queue.
340 	 */
341 	s = splstatclock();
342 	p2->p_stats->p_start = time;
343 	p2->p_acflag = AFORK;
344 	p2->p_stat = SRUN;
345 	setrunqueue(p2);
346 	splx(s);
347 
348 	/*
349 	 * Now can be swapped.
350 	 */
351 	PRELE(p1);
352 
353 	/*
354 	 * Update stats now that we know the fork was successful.
355 	 */
356 #if defined(UVM)
357 	uvmexp.forks++;
358 	if (flags & FORK_PPWAIT)
359 		uvmexp.forks_ppwait++;
360 	if (flags & FORK_SHAREVM)
361 		uvmexp.forks_sharevm++;
362 #else
363 	cnt.v_forks++;
364 	if (flags & FORK_PPWAIT)
365 		cnt.v_forks_ppwait++;
366 	if (flags & FORK_SHAREVM)
367 		cnt.v_forks_sharevm++;
368 #endif
369 
370 	/*
371 	 * Pass a pointer to the new process to the caller.
372 	 */
373 	if (rnewprocp != NULL)
374 		*rnewprocp = p2;
375 
376 	/*
377 	 * Preserve synchronization semantics of vfork.  If waiting for
378 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
379 	 * proc (in case of exit).
380 	 */
381 	if (flags & FORK_PPWAIT)
382 		while (p2->p_flag & P_PPWAIT)
383 			tsleep(p1, PWAIT, "ppwait", 0);
384 
385 	/*
386 	 * Return child pid to parent process,
387 	 * marking us as parent via retval[1].
388 	 */
389 	if (retval != NULL) {
390 		retval[0] = p2->p_pid;
391 		retval[1] = 0;
392 	}
393 	return (0);
394 }
395