xref: /openbsd-src/sys/kern/kern_fork.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: kern_fork.c,v 1.63 2003/09/23 20:26:18 millert Exp $	*/
2 /*	$NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/filedesc.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
50 #include <sys/file.h>
51 #include <sys/acct.h>
52 #include <sys/ktrace.h>
53 #include <sys/sched.h>
54 #include <dev/rndvar.h>
55 #include <sys/pool.h>
56 #include <sys/mman.h>
57 
58 #include <sys/syscallargs.h>
59 
60 #include "systrace.h"
61 #include <dev/systrace.h>
62 
63 #include <uvm/uvm_extern.h>
64 #include <uvm/uvm_map.h>
65 
66 int	nprocs = 1;		/* process 0 */
67 int	randompid;		/* when set to 1, pid's go random */
68 pid_t	lastpid;
69 struct	forkstat forkstat;
70 
71 int pidtaken(pid_t);
72 
73 /*ARGSUSED*/
74 int
75 sys_fork(struct proc *p, void *v, register_t *retval)
76 {
77 	return (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, NULL, NULL, retval));
78 }
79 
80 /*ARGSUSED*/
81 int
82 sys_vfork(struct proc *p, void *v, register_t *retval)
83 {
84 	return (fork1(p, SIGCHLD, FORK_VFORK|FORK_PPWAIT, NULL, 0, NULL,
85 	    NULL, retval));
86 }
87 
88 int
89 sys_rfork(struct proc *p, void *v, register_t *retval)
90 {
91 	struct sys_rfork_args /* {
92 		syscallarg(int) flags;
93 	} */ *uap = v;
94 
95 	int rforkflags;
96 	int flags;
97 
98 	flags = FORK_RFORK;
99 	rforkflags = SCARG(uap, flags);
100 
101 	if ((rforkflags & RFPROC) == 0)
102 		return (EINVAL);
103 
104 	switch(rforkflags & (RFFDG|RFCFDG)) {
105 	case (RFFDG|RFCFDG):
106 		return EINVAL;
107 	case RFCFDG:
108 		flags |= FORK_CLEANFILES;
109 		break;
110 	case RFFDG:
111 		break;
112 	default:
113 		flags |= FORK_SHAREFILES;
114 		break;
115 	}
116 
117 	if (rforkflags & RFNOWAIT)
118 		flags |= FORK_NOZOMBIE;
119 
120 	if (rforkflags & RFMEM)
121 		flags |= FORK_VMNOSTACK;
122 
123 	return (fork1(p, SIGCHLD, flags, NULL, 0, NULL, NULL, retval));
124 }
125 
126 /* print the 'table full' message once per 10 seconds */
127 struct timeval fork_tfmrate = { 10, 0 };
128 
129 int
130 fork1(struct proc *p1, int exitsig, int flags, void *stack, size_t stacksize,
131     void (*func)(void *), void *arg, register_t *retval)
132 {
133 	struct proc *p2;
134 	uid_t uid;
135 	struct vmspace *vm;
136 	int count;
137 	vaddr_t uaddr;
138 	int s;
139 	extern void endtsleep(void *);
140 	extern void realitexpire(void *);
141 
142 	/*
143 	 * Although process entries are dynamically created, we still keep
144 	 * a global limit on the maximum number we will create. We reserve
145 	 * the last 5 processes to root. The variable nprocs is the current
146 	 * number of processes, maxproc is the limit.
147 	 */
148 	uid = p1->p_cred->p_ruid;
149 	if ((nprocs >= maxproc - 5 && uid != 0) || nprocs >= maxproc) {
150 		static struct timeval lasttfm;
151 
152 		if (ratecheck(&lasttfm, &fork_tfmrate))
153 			tablefull("proc");
154 		return (EAGAIN);
155 	}
156 	nprocs++;
157 
158 	/*
159 	 * Increment the count of procs running with this uid. Don't allow
160 	 * a nonprivileged user to exceed their current limit.
161 	 */
162 	count = chgproccnt(uid, 1);
163 	if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
164 		(void)chgproccnt(uid, -1);
165 		nprocs--;
166 		return (EAGAIN);
167 	}
168 
169 	/*
170 	 * Allocate a pcb and kernel stack for the process
171 	 */
172 	uaddr = uvm_km_valloc(kernel_map, USPACE);
173 	if (uaddr == 0) {
174 		chgproccnt(uid, -1);
175 		nprocs--;
176 		return (ENOMEM);
177 	}
178 
179 	/*
180 	 * From now on, we're committed to the fork and cannot fail.
181 	 */
182 
183 	/* Allocate new proc. */
184 	p2 = pool_get(&proc_pool, PR_WAITOK);
185 
186 	p2->p_stat = SIDL;			/* protect against others */
187 	p2->p_exitsig = exitsig;
188 	p2->p_forw = p2->p_back = NULL;
189 
190 	/*
191 	 * Make a proc table entry for the new process.
192 	 * Start by zeroing the section of proc that is zero-initialized,
193 	 * then copy the section that is copied directly from the parent.
194 	 */
195 	bzero(&p2->p_startzero,
196 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
197 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
198 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
199 
200 	/*
201 	 * Initialize the timeouts.
202 	 */
203 	timeout_set(&p2->p_sleep_to, endtsleep, p2);
204 	timeout_set(&p2->p_realit_to, realitexpire, p2);
205 
206 	/*
207 	 * Duplicate sub-structures as needed.
208 	 * Increase reference counts on shared objects.
209 	 * The p_stats and p_sigacts substructs are set in vm_fork.
210 	 */
211 	p2->p_flag = P_INMEM;
212 	p2->p_emul = p1->p_emul;
213 	if (p1->p_flag & P_PROFIL)
214 		startprofclock(p2);
215 	p2->p_flag |= (p1->p_flag & (P_SUGID | P_SUGIDEXEC));
216 	p2->p_cred = pool_get(&pcred_pool, PR_WAITOK);
217 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
218 	p2->p_cred->p_refcnt = 1;
219 	crhold(p1->p_ucred);
220 
221 	/* bump references to the text vnode (for procfs) */
222 	p2->p_textvp = p1->p_textvp;
223 	if (p2->p_textvp)
224 		VREF(p2->p_textvp);
225 
226 	if (flags & FORK_CLEANFILES)
227 		p2->p_fd = fdinit(p1);
228 	else if (flags & FORK_SHAREFILES)
229 		p2->p_fd = fdshare(p1);
230 	else
231 		p2->p_fd = fdcopy(p1);
232 
233 	/*
234 	 * If p_limit is still copy-on-write, bump refcnt,
235 	 * otherwise get a copy that won't be modified.
236 	 * (If PL_SHAREMOD is clear, the structure is shared
237 	 * copy-on-write.)
238 	 */
239 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
240 		p2->p_limit = limcopy(p1->p_limit);
241 	else {
242 		p2->p_limit = p1->p_limit;
243 		p2->p_limit->p_refcnt++;
244 	}
245 
246 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
247 		p2->p_flag |= P_CONTROLT;
248 	if (flags & FORK_PPWAIT)
249 		p2->p_flag |= P_PPWAIT;
250 	LIST_INSERT_AFTER(p1, p2, p_pglist);
251 	p2->p_pptr = p1;
252 	if (flags & FORK_NOZOMBIE)
253 		p2->p_flag |= P_NOZOMBIE;
254 	LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
255 	LIST_INIT(&p2->p_children);
256 
257 #ifdef KTRACE
258 	/*
259 	 * Copy traceflag and tracefile if enabled.
260 	 * If not inherited, these were zeroed above.
261 	 */
262 	if (p1->p_traceflag & KTRFAC_INHERIT) {
263 		p2->p_traceflag = p1->p_traceflag;
264 		if ((p2->p_tracep = p1->p_tracep) != NULL)
265 			VREF(p2->p_tracep);
266 	}
267 #endif
268 
269 	/*
270 	 * set priority of child to be that of parent
271 	 * XXX should move p_estcpu into the region of struct proc which gets
272 	 * copied.
273 	 */
274 	scheduler_fork_hook(p1, p2);
275 
276 	/*
277 	 * Create signal actions for the child process.
278 	 */
279 	if (flags & FORK_SIGHAND)
280 		sigactsshare(p1, p2);
281 	else
282 		p2->p_sigacts = sigactsinit(p1);
283 
284 	/*
285 	 * If emulation has process fork hook, call it now.
286 	 */
287 	if (p2->p_emul->e_proc_fork)
288 		(*p2->p_emul->e_proc_fork)(p2, p1);
289 	/*
290 	 * This begins the section where we must prevent the parent
291 	 * from being swapped.
292 	 */
293 	PHOLD(p1);
294 
295 	if (flags & FORK_VMNOSTACK) {
296 		/* share everything, but ... */
297 		uvm_map_inherit(&p1->p_vmspace->vm_map,
298 		    VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS,
299 		    MAP_INHERIT_SHARE);
300 		/* ... don't share stack */
301 #ifdef MACHINE_STACK_GROWS_UP
302 		uvm_map_inherit(&p1->p_vmspace->vm_map,
303 		    USRSTACK, USRSTACK + MAXSSIZ,
304 		    MAP_INHERIT_COPY);
305 #else
306 		uvm_map_inherit(&p1->p_vmspace->vm_map,
307 		    USRSTACK - MAXSSIZ, USRSTACK,
308 		    MAP_INHERIT_COPY);
309 #endif
310 	}
311 
312 	p2->p_addr = (struct user *)uaddr;
313 
314 	/*
315 	 * Finish creating the child process.  It will return through a
316 	 * different path later.
317 	 */
318 	uvm_fork(p1, p2, ((flags & FORK_SHAREVM) ? TRUE : FALSE), stack,
319 	    stacksize, func ? func : child_return, arg ? arg : p2);
320 
321 	vm = p2->p_vmspace;
322 
323 	if (flags & FORK_FORK) {
324 		forkstat.cntfork++;
325 		forkstat.sizfork += vm->vm_dsize + vm->vm_ssize;
326 	} else if (flags & FORK_VFORK) {
327 		forkstat.cntvfork++;
328 		forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize;
329 	} else if (flags & FORK_RFORK) {
330 		forkstat.cntrfork++;
331 		forkstat.sizrfork += vm->vm_dsize + vm->vm_ssize;
332 	} else {
333 		forkstat.cntkthread++;
334 		forkstat.sizkthread += vm->vm_dsize + vm->vm_ssize;
335 	}
336 
337 	/* Find an unused pid satisfying 1 <= lastpid <= PID_MAX */
338 	do {
339 		lastpid = 1 + (randompid ? arc4random() : lastpid) % PID_MAX;
340 	} while (pidtaken(lastpid));
341 	p2->p_pid = lastpid;
342 
343 	LIST_INSERT_HEAD(&allproc, p2, p_list);
344 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
345 
346 #if NSYSTRACE > 0
347 	if (ISSET(p1->p_flag, P_SYSTRACE))
348 		systrace_fork(p1, p2);
349 #endif
350 
351 	/*
352 	 * Make child runnable, set start time, and add to run queue.
353 	 */
354 	s = splstatclock();
355 	p2->p_stats->p_start = time;
356 	p2->p_acflag = AFORK;
357 	p2->p_stat = SRUN;
358 	setrunqueue(p2);
359 	splx(s);
360 
361 	/*
362 	 * Now can be swapped.
363 	 */
364 	PRELE(p1);
365 
366 	uvmexp.forks++;
367 	if (flags & FORK_PPWAIT)
368 		uvmexp.forks_ppwait++;
369 	if (flags & FORK_SHAREVM)
370 		uvmexp.forks_sharevm++;
371 
372 	/*
373 	 * tell any interested parties about the new process
374 	 */
375 	KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
376 
377 	/*
378 	 * Preserve synchronization semantics of vfork.  If waiting for
379 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
380 	 * proc (in case of exit).
381 	 */
382 	if (flags & FORK_PPWAIT)
383 		while (p2->p_flag & P_PPWAIT)
384 			tsleep(p1, PWAIT, "ppwait", 0);
385 
386 	/*
387 	 * Return child pid to parent process,
388 	 * marking us as parent via retval[1].
389 	 */
390 	retval[0] = p2->p_pid;
391 	retval[1] = 0;
392 	return (0);
393 }
394 
395 /*
396  * Checks for current use of a pid, either as a pid or pgid.
397  */
398 int
399 pidtaken(pid_t pid)
400 {
401 	struct proc *p;
402 
403 	if (pfind(pid) != NULL)
404 		return (1);
405 	if (pgfind(pid) != NULL)
406 		return (1);
407 	LIST_FOREACH(p, &zombproc, p_list)
408 		if (p->p_pid == pid || p->p_pgid == pid)
409 			return (1);
410 	return (0);
411 }
412