xref: /freebsd/sys/kern/kern_fork.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
39  * $Id: kern_fork.c,v 1.25 1996/08/22 03:50:18 julian Exp $
40  */
41 
42 #include "opt_ktrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53 #include <sys/acct.h>
54 #include <sys/ktrace.h>
55 #include <sys/unistd.h>
56 
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/lock.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_inherit.h>
64 
65 static int fork1 __P((struct proc *p, int flags, int *retval));
66 
67 /*
68  * These are the stuctures used to create a callout list for things to do
69  * when forking a process
70  */
71 typedef struct fork_list_element {
72 	struct fork_list_element *next;
73 	forklist_fn function;
74 } *fle_p;
75 
76 static fle_p	fork_list;
77 
78 #ifndef _SYS_SYSPROTO_H_
79 struct fork_args {
80 	int     dummy;
81 };
82 #endif
83 
84 /* ARGSUSED */
85 int
86 fork(p, uap, retval)
87 	struct proc *p;
88 	struct fork_args *uap;
89 	int retval[];
90 {
91 	return (fork1(p, (RFFDG|RFPROC), retval));
92 }
93 
94 /* ARGSUSED */
95 int
96 vfork(p, uap, retval)
97 	struct proc *p;
98 	struct vfork_args *uap;
99 	int retval[];
100 {
101 	return (fork1(p, (RFFDG|RFPROC|RFPPWAIT), retval));
102 }
103 
104 /* ARGSUSED */
105 int
106 rfork(p, uap, retval)
107 	struct proc *p;
108 	struct rfork_args *uap;
109 	int retval[];
110 {
111 	return (fork1(p, uap->flags, retval));
112 }
113 
114 
115 int	nprocs = 1;		/* process 0 */
116 
117 static int
118 fork1(p1, flags, retval)
119 	register struct proc *p1;
120 	int flags;
121 	int retval[];
122 {
123 	register struct proc *p2, *pptr;
124 	register uid_t uid;
125 	struct proc *newproc;
126 	int count;
127 	static int nextpid, pidchecked = 0;
128 	fle_p ep ;
129 
130 	ep = fork_list;
131 	if ((flags & RFPROC) == 0)
132 		return (EINVAL);
133 	if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
134 		return (EINVAL);
135 
136 	/*
137 	 * Although process entries are dynamically created, we still keep
138 	 * a global limit on the maximum number we will create.  Don't allow
139 	 * a nonprivileged user to use the last process; don't let root
140 	 * exceed the limit. The variable nprocs is the current number of
141 	 * processes, maxproc is the limit.
142 	 */
143 	uid = p1->p_cred->p_ruid;
144 	if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
145 		tablefull("proc");
146 		return (EAGAIN);
147 	}
148 	/*
149 	 * Increment the nprocs resource before blocking can occur.  There
150 	 * are hard-limits as to the number of processes that can run.
151 	 */
152 	nprocs++;
153 
154 	/*
155 	 * Increment the count of procs running with this uid. Don't allow
156 	 * a nonprivileged user to exceed their current limit.
157 	 */
158 	count = chgproccnt(uid, 1);
159 	if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
160 		(void)chgproccnt(uid, -1);
161 		/*
162 		 * Back out the process count
163 		 */
164 		nprocs--;
165 		return (EAGAIN);
166 	}
167 
168 	/* Allocate new proc. */
169 	MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK);
170 
171 	/*
172 	 * Find an unused process ID.  We remember a range of unused IDs
173 	 * ready to use (from nextpid+1 through pidchecked-1).
174 	 */
175 	nextpid++;
176 retry:
177 	/*
178 	 * If the process ID prototype has wrapped around,
179 	 * restart somewhat above 0, as the low-numbered procs
180 	 * tend to include daemons that don't exit.
181 	 */
182 	if (nextpid >= PID_MAX) {
183 		nextpid = 100;
184 		pidchecked = 0;
185 	}
186 	if (nextpid >= pidchecked) {
187 		int doingzomb = 0;
188 
189 		pidchecked = PID_MAX;
190 		/*
191 		 * Scan the active and zombie procs to check whether this pid
192 		 * is in use.  Remember the lowest pid that's greater
193 		 * than nextpid, so we can avoid checking for a while.
194 		 */
195 		p2 = allproc.lh_first;
196 again:
197 		for (; p2 != 0; p2 = p2->p_list.le_next) {
198 			while (p2->p_pid == nextpid ||
199 			    p2->p_pgrp->pg_id == nextpid) {
200 				nextpid++;
201 				if (nextpid >= pidchecked)
202 					goto retry;
203 			}
204 			if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
205 				pidchecked = p2->p_pid;
206 			if (p2->p_pgrp->pg_id > nextpid &&
207 			    pidchecked > p2->p_pgrp->pg_id)
208 				pidchecked = p2->p_pgrp->pg_id;
209 		}
210 		if (!doingzomb) {
211 			doingzomb = 1;
212 			p2 = zombproc.lh_first;
213 			goto again;
214 		}
215 	}
216 
217 	p2 = newproc;
218 	p2->p_stat = SIDL;			/* protect against others */
219 	p2->p_pid = nextpid;
220 	LIST_INSERT_HEAD(&allproc, p2, p_list);
221 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
222 
223 	/*
224 	 * Make a proc table entry for the new process.
225 	 * Start by zeroing the section of proc that is zero-initialized,
226 	 * then copy the section that is copied directly from the parent.
227 	 */
228 	bzero(&p2->p_startzero,
229 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
230 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
231 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
232 
233 	/*
234 	 * XXX: this should be done as part of the startzero above
235 	 */
236 	p2->p_vmspace = 0;		/* XXX */
237 
238 	/*
239 	 * Duplicate sub-structures as needed.
240 	 * Increase reference counts on shared objects.
241 	 * The p_stats and p_sigacts substructs are set in vm_fork.
242 	 */
243 	p2->p_flag = P_INMEM;
244 	if (p1->p_flag & P_PROFIL)
245 		startprofclock(p2);
246 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
247 	    M_SUBPROC, M_WAITOK);
248 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
249 	p2->p_cred->p_refcnt = 1;
250 	crhold(p1->p_ucred);
251 
252 	/* bump references to the text vnode (for procfs) */
253 	p2->p_textvp = p1->p_textvp;
254 	if (p2->p_textvp)
255 		VREF(p2->p_textvp);
256 
257 	if (flags & RFCFDG)
258 		p2->p_fd = fdinit(p1);
259 	else if (flags & RFFDG)
260 		p2->p_fd = fdcopy(p1);
261 	else
262 		p2->p_fd = fdshare(p1);
263 
264 	/*
265 	 * If p_limit is still copy-on-write, bump refcnt,
266 	 * otherwise get a copy that won't be modified.
267 	 * (If PL_SHAREMOD is clear, the structure is shared
268 	 * copy-on-write.)
269 	 */
270 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
271 		p2->p_limit = limcopy(p1->p_limit);
272 	else {
273 		p2->p_limit = p1->p_limit;
274 		p2->p_limit->p_refcnt++;
275 	}
276 
277 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
278 		p2->p_flag |= P_CONTROLT;
279 	if (flags & RFPPWAIT)
280 		p2->p_flag |= P_PPWAIT;
281 	LIST_INSERT_AFTER(p1, p2, p_pglist);
282 
283 	/*
284 	 * Attach the new process to its parent.
285 	 *
286 	 * If RFNOWAIT is set, the newly created process becomes a child
287 	 * of init.  This effectively disassociates the child from the
288 	 * parent.
289 	 */
290 	if (flags & RFNOWAIT)
291 		pptr = initproc;
292 	else
293 		pptr = p1;
294 	p2->p_pptr = pptr;
295 	LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling);
296 	LIST_INIT(&p2->p_children);
297 
298 #ifdef KTRACE
299 	/*
300 	 * Copy traceflag and tracefile if enabled.
301 	 * If not inherited, these were zeroed above.
302 	 */
303 	if (p1->p_traceflag&KTRFAC_INHERIT) {
304 		p2->p_traceflag = p1->p_traceflag;
305 		if ((p2->p_tracep = p1->p_tracep) != NULL)
306 			VREF(p2->p_tracep);
307 	}
308 #endif
309 
310 	/*
311 	 * set priority of child to be that of parent
312 	 */
313 	p2->p_estcpu = p1->p_estcpu;
314 
315 	/*
316 	 * This begins the section where we must prevent the parent
317 	 * from being swapped.
318 	 */
319 	p1->p_flag |= P_NOSWAP;
320 
321 	/*
322 	 * share as much address space as possible
323 	 * XXX this should probably go in vm_fork()
324 	 */
325 	if (flags & RFMEM)
326 		(void) vm_map_inherit(&p1->p_vmspace->vm_map,
327 		    VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS - MAXSSIZ,
328 		    VM_INHERIT_SHARE);
329 
330 	/*
331 	 * Set return values for child before vm_fork,
332 	 * so they can be copied to child stack.
333 	 * We return parent pid, and mark as child in retval[1].
334 	 * NOTE: the kernel stack may be at a different location in the child
335 	 * process, and thus addresses of automatic variables (including retval)
336 	 * may be invalid after vm_fork returns in the child process.
337 	 */
338 	retval[0] = p1->p_pid;
339 	retval[1] = 1;
340 	if (vm_fork(p1, p2)) {
341 		/*
342 		 * Child process.  Set start time and get to work.
343 		 */
344 		microtime(&runtime);
345 		p2->p_stats->p_start = runtime;
346 		p2->p_acflag = AFORK;
347 		return (0);
348 	}
349 
350 	/*
351 	 * Both processes are set up, now check if any LKMs want
352 	 * to adjust anything.
353 	 *   What if they have an error? XXX
354 	 */
355 	while (ep) {
356 		(*ep->function)(p1, p2, flags);
357 		ep = ep->next;
358 	}
359 
360 	/*
361 	 * Make child runnable and add to run queue.
362 	 */
363 	(void) splhigh();
364 	p2->p_stat = SRUN;
365 	setrunqueue(p2);
366 	(void) spl0();
367 
368 	/*
369 	 * Now can be swapped.
370 	 */
371 	p1->p_flag &= ~P_NOSWAP;
372 
373 	/*
374 	 * Preserve synchronization semantics of vfork.  If waiting for
375 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
376 	 * proc (in case of exit).
377 	 */
378 	while (p2->p_flag & P_PPWAIT)
379 		tsleep(p1, PWAIT, "ppwait", 0);
380 
381 	/*
382 	 * Return child pid to parent process,
383 	 * marking us as parent via retval[1].
384 	 */
385 	retval[0] = p2->p_pid;
386 	retval[1] = 0;
387 	return (0);
388 }
389 
390 /*
391  * The next two functionms are general routines to handle adding/deleting
392  * items on the fork callout list.
393  *
394  * at_fork():
395  * Take the arguments given and put them onto the fork callout list,
396  * However first make sure that it's not already there.
397  * Returns 0 on success or a standard error number.
398  */
399 int
400 at_fork(forklist_fn function)
401 {
402 	fle_p ep;
403 
404 	/* let the programmer know if he's been stupid */
405 	if (rm_at_fork(function))
406 		printf("fork callout entry already present\n");
407 	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
408 	if (ep == NULL)
409 		return (ENOMEM);
410 	ep->next = fork_list;
411 	ep->function = function;
412 	fork_list = ep;
413 	return (0);
414 }
415 
416 /*
417  * Scan the exit callout list for the given items and remove them.
418  * Returns the number of items removed.
419  * Theoretically this value can only be 0 or 1.
420  */
421 int
422 rm_at_fork(forklist_fn function)
423 {
424 	fle_p *epp, ep;
425 	int count;
426 
427 	count= 0;
428 	epp = &fork_list;
429 	ep = *epp;
430 	while (ep) {
431 		if (ep->function == function) {
432 			*epp = ep->next;
433 			free(ep, M_TEMP);
434 			count++;
435 		} else {
436 			epp = &ep->next;
437 		}
438 		ep = *epp;
439 	}
440 	return (count);
441 }
442 
443 
444