xref: /openbsd-src/sys/kern/kern_fork.c (revision 012ea299774669efba934275e67c4a60442e6b8f)
1 /*	$OpenBSD: kern_fork.c,v 1.69 2004/06/13 21:49:26 niklas 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/exec.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/file.h>
52 #include <sys/acct.h>
53 #include <sys/ktrace.h>
54 #include <sys/sched.h>
55 #include <dev/rndvar.h>
56 #include <sys/pool.h>
57 #include <sys/mman.h>
58 
59 #include <sys/syscallargs.h>
60 
61 #include "systrace.h"
62 #include <dev/systrace.h>
63 
64 #include <uvm/uvm_extern.h>
65 #include <uvm/uvm_map.h>
66 
67 int	nprocs = 1;		/* process 0 */
68 int	randompid;		/* when set to 1, pid's go random */
69 pid_t	lastpid;
70 struct	forkstat forkstat;
71 
72 int pidtaken(pid_t);
73 
74 /*ARGSUSED*/
75 int
76 sys_fork(struct proc *p, void *v, register_t *retval)
77 {
78 	return (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, NULL, NULL, retval));
79 }
80 
81 /*ARGSUSED*/
82 int
83 sys_vfork(struct proc *p, void *v, register_t *retval)
84 {
85 	return (fork1(p, SIGCHLD, FORK_VFORK|FORK_PPWAIT, NULL, 0, NULL,
86 	    NULL, retval));
87 }
88 
89 int
90 sys_rfork(struct proc *p, void *v, register_t *retval)
91 {
92 	struct sys_rfork_args /* {
93 		syscallarg(int) flags;
94 	} */ *uap = v;
95 
96 	int rforkflags;
97 	int flags;
98 
99 	flags = FORK_RFORK;
100 	rforkflags = SCARG(uap, flags);
101 
102 	if ((rforkflags & RFPROC) == 0)
103 		return (EINVAL);
104 
105 	switch(rforkflags & (RFFDG|RFCFDG)) {
106 	case (RFFDG|RFCFDG):
107 		return EINVAL;
108 	case RFCFDG:
109 		flags |= FORK_CLEANFILES;
110 		break;
111 	case RFFDG:
112 		break;
113 	default:
114 		flags |= FORK_SHAREFILES;
115 		break;
116 	}
117 
118 	if (rforkflags & RFNOWAIT)
119 		flags |= FORK_NOZOMBIE;
120 
121 	if (rforkflags & RFMEM)
122 		flags |= FORK_SHAREVM;
123 
124 	return (fork1(p, SIGCHLD, flags, NULL, 0, NULL, NULL, retval));
125 }
126 
127 /* print the 'table full' message once per 10 seconds */
128 struct timeval fork_tfmrate = { 10, 0 };
129 
130 int
131 fork1(struct proc *p1, int exitsig, int flags, void *stack, size_t stacksize,
132     void (*func)(void *), void *arg, register_t *retval)
133 {
134 	struct proc *p2;
135 	uid_t uid;
136 	struct vmspace *vm;
137 	int count;
138 	vaddr_t uaddr;
139 	int s;
140 	extern void endtsleep(void *);
141 	extern void realitexpire(void *);
142 
143 	/*
144 	 * Although process entries are dynamically created, we still keep
145 	 * a global limit on the maximum number we will create. We reserve
146 	 * the last 5 processes to root. The variable nprocs is the current
147 	 * number of processes, maxproc is the limit.
148 	 */
149 	uid = p1->p_cred->p_ruid;
150 	if ((nprocs >= maxproc - 5 && uid != 0) || nprocs >= maxproc) {
151 		static struct timeval lasttfm;
152 
153 		if (ratecheck(&lasttfm, &fork_tfmrate))
154 			tablefull("proc");
155 		return (EAGAIN);
156 	}
157 	nprocs++;
158 
159 	/*
160 	 * Increment the count of procs running with this uid. Don't allow
161 	 * a nonprivileged user to exceed their current limit.
162 	 */
163 	count = chgproccnt(uid, 1);
164 	if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
165 		(void)chgproccnt(uid, -1);
166 		nprocs--;
167 		return (EAGAIN);
168 	}
169 
170 	/*
171 	 * Allocate a pcb and kernel stack for the process
172 	 */
173 	uaddr = uvm_km_valloc(kernel_map, USPACE);
174 	if (uaddr == 0) {
175 		chgproccnt(uid, -1);
176 		nprocs--;
177 		return (ENOMEM);
178 	}
179 
180 	/*
181 	 * From now on, we're committed to the fork and cannot fail.
182 	 */
183 
184 	/* Allocate new proc. */
185 	p2 = pool_get(&proc_pool, PR_WAITOK);
186 
187 	p2->p_stat = SIDL;			/* protect against others */
188 	p2->p_exitsig = exitsig;
189 	p2->p_forw = p2->p_back = NULL;
190 
191 	/*
192 	 * Make a proc table entry for the new process.
193 	 * Start by zeroing the section of proc that is zero-initialized,
194 	 * then copy the section that is copied directly from the parent.
195 	 */
196 	bzero(&p2->p_startzero,
197 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
198 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
199 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
200 
201 	/*
202 	 * Initialize the timeouts.
203 	 */
204 	timeout_set(&p2->p_sleep_to, endtsleep, p2);
205 	timeout_set(&p2->p_realit_to, realitexpire, p2);
206 
207 #if defined(__HAVE_CPUINFO)
208 	p2->p_cpu = NULL;
209 #endif
210 
211 	/*
212 	 * Duplicate sub-structures as needed.
213 	 * Increase reference counts on shared objects.
214 	 * The p_stats and p_sigacts substructs are set in vm_fork.
215 	 */
216 	p2->p_flag = P_INMEM;
217 	p2->p_emul = p1->p_emul;
218 	if (p1->p_flag & P_PROFIL)
219 		startprofclock(p2);
220 	p2->p_flag |= (p1->p_flag & (P_SUGID | P_SUGIDEXEC));
221 	p2->p_cred = pool_get(&pcred_pool, PR_WAITOK);
222 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
223 	p2->p_cred->p_refcnt = 1;
224 	crhold(p1->p_ucred);
225 
226 	/* bump references to the text vnode (for procfs) */
227 	p2->p_textvp = p1->p_textvp;
228 	if (p2->p_textvp)
229 		VREF(p2->p_textvp);
230 
231 	if (flags & FORK_CLEANFILES)
232 		p2->p_fd = fdinit(p1);
233 	else if (flags & FORK_SHAREFILES)
234 		p2->p_fd = fdshare(p1);
235 	else
236 		p2->p_fd = fdcopy(p1);
237 
238 	/*
239 	 * If p_limit is still copy-on-write, bump refcnt,
240 	 * otherwise get a copy that won't be modified.
241 	 * (If PL_SHAREMOD is clear, the structure is shared
242 	 * copy-on-write.)
243 	 */
244 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
245 		p2->p_limit = limcopy(p1->p_limit);
246 	else {
247 		p2->p_limit = p1->p_limit;
248 		p2->p_limit->p_refcnt++;
249 	}
250 
251 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
252 		p2->p_flag |= P_CONTROLT;
253 	if (flags & FORK_PPWAIT)
254 		p2->p_flag |= P_PPWAIT;
255 	LIST_INSERT_AFTER(p1, p2, p_pglist);
256 	p2->p_pptr = p1;
257 	if (flags & FORK_NOZOMBIE)
258 		p2->p_flag |= P_NOZOMBIE;
259 	LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
260 	LIST_INIT(&p2->p_children);
261 
262 #ifdef KTRACE
263 	/*
264 	 * Copy traceflag and tracefile if enabled.
265 	 * If not inherited, these were zeroed above.
266 	 */
267 	if (p1->p_traceflag & KTRFAC_INHERIT) {
268 		p2->p_traceflag = p1->p_traceflag;
269 		if ((p2->p_tracep = p1->p_tracep) != NULL)
270 			VREF(p2->p_tracep);
271 	}
272 #endif
273 
274 	/*
275 	 * set priority of child to be that of parent
276 	 * XXX should move p_estcpu into the region of struct proc which gets
277 	 * copied.
278 	 */
279 	scheduler_fork_hook(p1, p2);
280 
281 	/*
282 	 * Create signal actions for the child process.
283 	 */
284 	if (flags & FORK_SIGHAND)
285 		sigactsshare(p1, p2);
286 	else
287 		p2->p_sigacts = sigactsinit(p1);
288 
289 	/*
290 	 * If emulation has process fork hook, call it now.
291 	 */
292 	if (p2->p_emul->e_proc_fork)
293 		(*p2->p_emul->e_proc_fork)(p2, p1);
294 	/*
295 	 * This begins the section where we must prevent the parent
296 	 * from being swapped.
297 	 */
298 	PHOLD(p1);
299 
300 	p2->p_addr = (struct user *)uaddr;
301 
302 	/*
303 	 * Finish creating the child process.  It will return through a
304 	 * different path later.
305 	 */
306 	uvm_fork(p1, p2, ((flags & FORK_SHAREVM) ? TRUE : FALSE), stack,
307 	    stacksize, func ? func : child_return, arg ? arg : p2);
308 
309 	vm = p2->p_vmspace;
310 
311 	if (flags & FORK_FORK) {
312 		forkstat.cntfork++;
313 		forkstat.sizfork += vm->vm_dsize + vm->vm_ssize;
314 	} else if (flags & FORK_VFORK) {
315 		forkstat.cntvfork++;
316 		forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize;
317 	} else if (flags & FORK_RFORK) {
318 		forkstat.cntrfork++;
319 		forkstat.sizrfork += vm->vm_dsize + vm->vm_ssize;
320 	} else {
321 		forkstat.cntkthread++;
322 		forkstat.sizkthread += vm->vm_dsize + vm->vm_ssize;
323 	}
324 
325 	/* Find an unused pid satisfying 1 <= lastpid <= PID_MAX */
326 	do {
327 		lastpid = 1 + (randompid ? arc4random() : lastpid) % PID_MAX;
328 	} while (pidtaken(lastpid));
329 	p2->p_pid = lastpid;
330 
331 	LIST_INSERT_HEAD(&allproc, p2, p_list);
332 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
333 
334 #if NSYSTRACE > 0
335 	if (ISSET(p1->p_flag, P_SYSTRACE))
336 		systrace_fork(p1, p2);
337 #endif
338 
339 	/*
340 	 * Make child runnable, set start time, and add to run queue.
341 	 */
342 	SCHED_LOCK(s);
343 	p2->p_stats->p_start = time;
344 	p2->p_acflag = AFORK;
345 	p2->p_stat = SRUN;
346 	setrunqueue(p2);
347 	SCHED_UNLOCK(s);
348 
349 	/*
350 	 * Now can be swapped.
351 	 */
352 	PRELE(p1);
353 
354 	uvmexp.forks++;
355 	if (flags & FORK_PPWAIT)
356 		uvmexp.forks_ppwait++;
357 	if (flags & FORK_SHAREVM)
358 		uvmexp.forks_sharevm++;
359 
360 	/*
361 	 * tell any interested parties about the new process
362 	 */
363 	KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
364 
365 	/*
366 	 * Preserve synchronization semantics of vfork.  If waiting for
367 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
368 	 * proc (in case of exit).
369 	 */
370 	if (flags & FORK_PPWAIT)
371 		while (p2->p_flag & P_PPWAIT)
372 			tsleep(p1, PWAIT, "ppwait", 0);
373 
374 	/*
375 	 * Return child pid to parent process,
376 	 * marking us as parent via retval[1].
377 	 */
378 	if (retval != NULL) {
379 		retval[0] = p2->p_pid;
380 		retval[1] = 0;
381 	}
382 	return (0);
383 }
384 
385 /*
386  * Checks for current use of a pid, either as a pid or pgid.
387  */
388 int
389 pidtaken(pid_t pid)
390 {
391 	struct proc *p;
392 
393 	if (pfind(pid) != NULL)
394 		return (1);
395 	if (pgfind(pid) != NULL)
396 		return (1);
397 	LIST_FOREACH(p, &zombproc, p_list)
398 		if (p->p_pid == pid || p->p_pgid == pid)
399 			return (1);
400 	return (0);
401 }
402 
403 #if defined(MULTIPROCESSOR)
404 /*
405  * XXX This is a slight hack to get newly-formed processes to
406  * XXX acquire the kernel lock as soon as they run.
407  */
408 void
409 proc_trampoline_mp(void)
410 {
411 	struct proc *p;
412 
413 	p = curproc;
414 
415 	SCHED_ASSERT_UNLOCKED();
416 	KERNEL_PROC_LOCK(p);
417 }
418 #endif
419