xref: /freebsd/sys/kern/kern_fork.c (revision c6362551506242d9b1d848bfb5bb483e0e695acf)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
47df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
49c76e95c3SPeter Wemm #include <sys/sysctl.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
54df8bae1dSRodney W. Grimes #include <sys/acct.h>
55df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
56b71fec07SBruce Evans #include <sys/unistd.h>
5775c13541SPoul-Henning Kamp #include <sys/jail.h>
58df8bae1dSRodney W. Grimes 
59d93f860cSPoul-Henning Kamp #include <vm/vm.h>
60996c772fSJohn Dyson #include <sys/lock.h>
61dabee6feSPeter Wemm #include <vm/pmap.h>
62dabee6feSPeter Wemm #include <vm/vm_map.h>
63efeaf95aSDavid Greenman #include <vm/vm_extern.h>
642d8acc0fSJohn Dyson #include <vm/vm_zone.h>
65d93f860cSPoul-Henning Kamp 
66dc9c271aSJulian Elischer #include <sys/user.h>
6788c5ea45SJulian Elischer 
6893efcae8SPoul-Henning Kamp static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback");
6993efcae8SPoul-Henning Kamp 
70be67169aSBruce Evans static int	fast_vfork = 1;
71c76e95c3SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, fast_vfork, CTLFLAG_RW, &fast_vfork, 0, "");
72c76e95c3SPeter Wemm 
73fed06968SJulian Elischer /*
74e0d898b4SJulian Elischer  * These are the stuctures used to create a callout list for things to do
75e0d898b4SJulian Elischer  * when forking a process
76fed06968SJulian Elischer  */
7793efcae8SPoul-Henning Kamp struct forklist {
78fed06968SJulian Elischer 	forklist_fn function;
79e3975643SJake Burkholder 	TAILQ_ENTRY(forklist) next;
8093efcae8SPoul-Henning Kamp };
81fed06968SJulian Elischer 
82e3975643SJake Burkholder TAILQ_HEAD(forklist_head, forklist);
8393efcae8SPoul-Henning Kamp static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list);
84fed06968SJulian Elischer 
85d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
86ad7507e2SSteven Wallace struct fork_args {
87ad7507e2SSteven Wallace 	int     dummy;
88ad7507e2SSteven Wallace };
89d2d3e875SBruce Evans #endif
90ad7507e2SSteven Wallace 
91df8bae1dSRodney W. Grimes /* ARGSUSED */
9226f9a767SRodney W. Grimes int
93cb226aaaSPoul-Henning Kamp fork(p, uap)
94df8bae1dSRodney W. Grimes 	struct proc *p;
95df8bae1dSRodney W. Grimes 	struct fork_args *uap;
96df8bae1dSRodney W. Grimes {
97df8abd0bSPeter Wemm 	int error;
98df8abd0bSPeter Wemm 	struct proc *p2;
99be67169aSBruce Evans 
100df8abd0bSPeter Wemm 	error = fork1(p, RFFDG | RFPROC, &p2);
101df8abd0bSPeter Wemm 	if (error == 0) {
102df8abd0bSPeter Wemm 		p->p_retval[0] = p2->p_pid;
103df8abd0bSPeter Wemm 		p->p_retval[1] = 0;
104df8abd0bSPeter Wemm 	}
105df8abd0bSPeter Wemm 	return error;
106df8bae1dSRodney W. Grimes }
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes /* ARGSUSED */
10926f9a767SRodney W. Grimes int
110cb226aaaSPoul-Henning Kamp vfork(p, uap)
111df8bae1dSRodney W. Grimes 	struct proc *p;
112dabee6feSPeter Wemm 	struct vfork_args *uap;
113df8bae1dSRodney W. Grimes {
114df8abd0bSPeter Wemm 	int error;
115df8abd0bSPeter Wemm 	struct proc *p2;
116be67169aSBruce Evans 
117df8abd0bSPeter Wemm 	error = fork1(p, RFFDG | RFPROC | RFPPWAIT | RFMEM, &p2);
118df8abd0bSPeter Wemm 	if (error == 0) {
119df8abd0bSPeter Wemm 		p->p_retval[0] = p2->p_pid;
120df8abd0bSPeter Wemm 		p->p_retval[1] = 0;
121df8abd0bSPeter Wemm 	}
122df8abd0bSPeter Wemm 	return error;
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes 
125dabee6feSPeter Wemm int
126cb226aaaSPoul-Henning Kamp rfork(p, uap)
127dabee6feSPeter Wemm 	struct proc *p;
128dabee6feSPeter Wemm 	struct rfork_args *uap;
129dabee6feSPeter Wemm {
130df8abd0bSPeter Wemm 	int error;
131df8abd0bSPeter Wemm 	struct proc *p2;
132be67169aSBruce Evans 
133df8abd0bSPeter Wemm 	error = fork1(p, uap->flags, &p2);
134df8abd0bSPeter Wemm 	if (error == 0) {
1351943af61SPeter Wemm 		p->p_retval[0] = p2 ? p2->p_pid : 0;
136df8abd0bSPeter Wemm 		p->p_retval[1] = 0;
137df8abd0bSPeter Wemm 	}
138df8abd0bSPeter Wemm 	return error;
139dabee6feSPeter Wemm }
140dabee6feSPeter Wemm 
141dabee6feSPeter Wemm 
142df8bae1dSRodney W. Grimes int	nprocs = 1;		/* process 0 */
14351068190SWolfram Schneider static int nextpid = 0;
144df8bae1dSRodney W. Grimes 
145bb6a234eSPeter Wemm /*
146bb6a234eSPeter Wemm  * Random component to nextpid generation.  We mix in a random factor to make
147bb6a234eSPeter Wemm  * it a little harder to predict.  We sanity check the modulus value to avoid
148bb6a234eSPeter Wemm  * doing it in critical paths.  Don't let it be too small or we pointlessly
149bb6a234eSPeter Wemm  * waste randomness entropy, and don't let it be impossibly large.  Using a
150bb6a234eSPeter Wemm  * modulus that is too big causes a LOT more process table scans and slows
151bb6a234eSPeter Wemm  * down fork processing as the pidchecked caching is defeated.
152bb6a234eSPeter Wemm  */
153ee3fd601SDan Moschuk static int randompid = 0;
154bb6a234eSPeter Wemm 
155bb6a234eSPeter Wemm static int
156bb6a234eSPeter Wemm sysctl_kern_randompid SYSCTL_HANDLER_ARGS
157bb6a234eSPeter Wemm {
158bb6a234eSPeter Wemm 		int error, pid;
159bb6a234eSPeter Wemm 
160bb6a234eSPeter Wemm 		pid = randompid;
161bb6a234eSPeter Wemm 		error = sysctl_handle_int(oidp, &pid, 0, req);
162bb6a234eSPeter Wemm 		if (error || !req->newptr)
163bb6a234eSPeter Wemm 			return (error);
164bb6a234eSPeter Wemm 		if (pid < 0 || pid > PID_MAX - 100)	/* out of range */
165bb6a234eSPeter Wemm 			pid = PID_MAX - 100;
166bb6a234eSPeter Wemm 		else if (pid < 2)			/* NOP */
167bb6a234eSPeter Wemm 			pid = 0;
168bb6a234eSPeter Wemm 		else if (pid < 100)			/* Make it reasonable */
169bb6a234eSPeter Wemm 			pid = 100;
170bb6a234eSPeter Wemm 		randompid = pid;
171bb6a234eSPeter Wemm 		return (error);
172bb6a234eSPeter Wemm }
173bb6a234eSPeter Wemm 
174bb6a234eSPeter Wemm SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
175bb6a234eSPeter Wemm     0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
176ee3fd601SDan Moschuk 
17774b2192aSJohn Dyson int
178df8abd0bSPeter Wemm fork1(p1, flags, procp)
179df8abd0bSPeter Wemm 	struct proc *p1;
1800e3eb7eeSSujal Patel 	int flags;
181df8abd0bSPeter Wemm 	struct proc **procp;
182df8bae1dSRodney W. Grimes {
183df8abd0bSPeter Wemm 	struct proc *p2, *pptr;
184df8abd0bSPeter Wemm 	uid_t uid;
185df8bae1dSRodney W. Grimes 	struct proc *newproc;
186c6362551SAlfred Perlstein 	int ok;
18751068190SWolfram Schneider 	static int pidchecked = 0;
18893efcae8SPoul-Henning Kamp 	struct forklist *ep;
1895856e12eSJohn Dyson 
1900e3eb7eeSSujal Patel 	if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
191dabee6feSPeter Wemm 		return (EINVAL);
192df8bae1dSRodney W. Grimes 
193df8bae1dSRodney W. Grimes 	/*
1945856e12eSJohn Dyson 	 * Here we don't create a new process, but we divorce
1955856e12eSJohn Dyson 	 * certain parts of a process from itself.
1965856e12eSJohn Dyson 	 */
1975856e12eSJohn Dyson 	if ((flags & RFPROC) == 0) {
1985856e12eSJohn Dyson 
19991c28bfdSLuoqi Chen 		vm_fork(p1, 0, flags);
2005856e12eSJohn Dyson 
2015856e12eSJohn Dyson 		/*
2025856e12eSJohn Dyson 		 * Close all file descriptors.
2035856e12eSJohn Dyson 		 */
2045856e12eSJohn Dyson 		if (flags & RFCFDG) {
2055856e12eSJohn Dyson 			struct filedesc *fdtmp;
2065856e12eSJohn Dyson 			fdtmp = fdinit(p1);
2075856e12eSJohn Dyson 			fdfree(p1);
2085856e12eSJohn Dyson 			p1->p_fd = fdtmp;
2095856e12eSJohn Dyson 		}
2105856e12eSJohn Dyson 
2115856e12eSJohn Dyson 		/*
2125856e12eSJohn Dyson 		 * Unshare file descriptors (from parent.)
2135856e12eSJohn Dyson 		 */
2145856e12eSJohn Dyson 		if (flags & RFFDG) {
2155856e12eSJohn Dyson 			if (p1->p_fd->fd_refcnt > 1) {
2165856e12eSJohn Dyson 				struct filedesc *newfd;
2175856e12eSJohn Dyson 				newfd = fdcopy(p1);
2185856e12eSJohn Dyson 				fdfree(p1);
2195856e12eSJohn Dyson 				p1->p_fd = newfd;
2205856e12eSJohn Dyson 			}
2215856e12eSJohn Dyson 		}
2221943af61SPeter Wemm 		*procp = NULL;
2235856e12eSJohn Dyson 		return (0);
2245856e12eSJohn Dyson 	}
2255856e12eSJohn Dyson 
2265856e12eSJohn Dyson 	/*
227df8bae1dSRodney W. Grimes 	 * Although process entries are dynamically created, we still keep
228df8bae1dSRodney W. Grimes 	 * a global limit on the maximum number we will create.  Don't allow
229df8bae1dSRodney W. Grimes 	 * a nonprivileged user to use the last process; don't let root
230df8bae1dSRodney W. Grimes 	 * exceed the limit. The variable nprocs is the current number of
231df8bae1dSRodney W. Grimes 	 * processes, maxproc is the limit.
232df8bae1dSRodney W. Grimes 	 */
233df8bae1dSRodney W. Grimes 	uid = p1->p_cred->p_ruid;
234df8bae1dSRodney W. Grimes 	if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
235df8bae1dSRodney W. Grimes 		tablefull("proc");
236df8bae1dSRodney W. Grimes 		return (EAGAIN);
237df8bae1dSRodney W. Grimes 	}
238df8bae1dSRodney W. Grimes 	/*
239ef5dc8a9SJohn Dyson 	 * Increment the nprocs resource before blocking can occur.  There
240ef5dc8a9SJohn Dyson 	 * are hard-limits as to the number of processes that can run.
241ef5dc8a9SJohn Dyson 	 */
242ef5dc8a9SJohn Dyson 	nprocs++;
243ef5dc8a9SJohn Dyson 
244ef5dc8a9SJohn Dyson 	/*
245df8bae1dSRodney W. Grimes 	 * Increment the count of procs running with this uid. Don't allow
246df8bae1dSRodney W. Grimes 	 * a nonprivileged user to exceed their current limit.
247df8bae1dSRodney W. Grimes 	 */
248c6362551SAlfred Perlstein 	ok = chgproccnt(uid, 1, p1->p_rlimit[RLIMIT_NPROC].rlim_cur);
249c6362551SAlfred Perlstein 	if (uid != 0 && !ok) {
250ef5dc8a9SJohn Dyson 		/*
251ef5dc8a9SJohn Dyson 		 * Back out the process count
252ef5dc8a9SJohn Dyson 		 */
253ef5dc8a9SJohn Dyson 		nprocs--;
254df8bae1dSRodney W. Grimes 		return (EAGAIN);
255df8bae1dSRodney W. Grimes 	}
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes 	/* Allocate new proc. */
2582d8acc0fSJohn Dyson 	newproc = zalloc(proc_zone);
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes 	/*
2612c1011f7SJohn Dyson 	 * Setup linkage for kernel based threading
2622c1011f7SJohn Dyson 	 */
2632c1011f7SJohn Dyson 	if((flags & RFTHREAD) != 0) {
2642c1011f7SJohn Dyson 		newproc->p_peers = p1->p_peers;
2652c1011f7SJohn Dyson 		p1->p_peers = newproc;
2662c1011f7SJohn Dyson 		newproc->p_leader = p1->p_leader;
2672c1011f7SJohn Dyson 	} else {
2682c1011f7SJohn Dyson 		newproc->p_peers = 0;
2692c1011f7SJohn Dyson 		newproc->p_leader = newproc;
2702c1011f7SJohn Dyson 	}
2712c1011f7SJohn Dyson 
2722c1011f7SJohn Dyson 	newproc->p_wakeup = 0;
2732c1011f7SJohn Dyson 
274d4da2dbaSAlan Cox 	newproc->p_vmspace = NULL;
275d4da2dbaSAlan Cox 
2762c1011f7SJohn Dyson 	/*
277df8bae1dSRodney W. Grimes 	 * Find an unused process ID.  We remember a range of unused IDs
278df8bae1dSRodney W. Grimes 	 * ready to use (from nextpid+1 through pidchecked-1).
279df8bae1dSRodney W. Grimes 	 */
280df8bae1dSRodney W. Grimes 	nextpid++;
281bb6a234eSPeter Wemm 	if (randompid)
282bb6a234eSPeter Wemm 		nextpid += arc4random() % randompid;
283df8bae1dSRodney W. Grimes retry:
284df8bae1dSRodney W. Grimes 	/*
285df8bae1dSRodney W. Grimes 	 * If the process ID prototype has wrapped around,
286df8bae1dSRodney W. Grimes 	 * restart somewhat above 0, as the low-numbered procs
287df8bae1dSRodney W. Grimes 	 * tend to include daemons that don't exit.
288df8bae1dSRodney W. Grimes 	 */
289bb6a234eSPeter Wemm 	if (nextpid >= PID_MAX) {
290bb6a234eSPeter Wemm 		nextpid = nextpid % PID_MAX;
291bb6a234eSPeter Wemm 		if (nextpid < 100)
292bb6a234eSPeter Wemm 			nextpid += 100;
293df8bae1dSRodney W. Grimes 		pidchecked = 0;
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 	if (nextpid >= pidchecked) {
296df8bae1dSRodney W. Grimes 		int doingzomb = 0;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 		pidchecked = PID_MAX;
299df8bae1dSRodney W. Grimes 		/*
300df8bae1dSRodney W. Grimes 		 * Scan the active and zombie procs to check whether this pid
301df8bae1dSRodney W. Grimes 		 * is in use.  Remember the lowest pid that's greater
302df8bae1dSRodney W. Grimes 		 * than nextpid, so we can avoid checking for a while.
303df8bae1dSRodney W. Grimes 		 */
3042e3c8fcbSPoul-Henning Kamp 		p2 = LIST_FIRST(&allproc);
305df8bae1dSRodney W. Grimes again:
3062e3c8fcbSPoul-Henning Kamp 		for (; p2 != 0; p2 = LIST_NEXT(p2, p_list)) {
307df8bae1dSRodney W. Grimes 			while (p2->p_pid == nextpid ||
308643a8daaSDon Lewis 			    p2->p_pgrp->pg_id == nextpid ||
309643a8daaSDon Lewis 			    p2->p_session->s_sid == nextpid) {
310df8bae1dSRodney W. Grimes 				nextpid++;
311df8bae1dSRodney W. Grimes 				if (nextpid >= pidchecked)
312df8bae1dSRodney W. Grimes 					goto retry;
313df8bae1dSRodney W. Grimes 			}
314df8bae1dSRodney W. Grimes 			if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
315df8bae1dSRodney W. Grimes 				pidchecked = p2->p_pid;
316df8bae1dSRodney W. Grimes 			if (p2->p_pgrp->pg_id > nextpid &&
317df8bae1dSRodney W. Grimes 			    pidchecked > p2->p_pgrp->pg_id)
318df8bae1dSRodney W. Grimes 				pidchecked = p2->p_pgrp->pg_id;
319643a8daaSDon Lewis 			if (p2->p_session->s_sid > nextpid &&
320643a8daaSDon Lewis 			    pidchecked > p2->p_session->s_sid)
321643a8daaSDon Lewis 				pidchecked = p2->p_session->s_sid;
322df8bae1dSRodney W. Grimes 		}
323df8bae1dSRodney W. Grimes 		if (!doingzomb) {
324df8bae1dSRodney W. Grimes 			doingzomb = 1;
3252e3c8fcbSPoul-Henning Kamp 			p2 = LIST_FIRST(&zombproc);
326df8bae1dSRodney W. Grimes 			goto again;
327df8bae1dSRodney W. Grimes 		}
328df8bae1dSRodney W. Grimes 	}
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	p2 = newproc;
331b75356e1SJeffrey Hsu 	p2->p_stat = SIDL;			/* protect against others */
332b75356e1SJeffrey Hsu 	p2->p_pid = nextpid;
333b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&allproc, p2, p_list);
334b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
337df8bae1dSRodney W. Grimes 	 * Make a proc table entry for the new process.
338df8bae1dSRodney W. Grimes 	 * Start by zeroing the section of proc that is zero-initialized,
339df8bae1dSRodney W. Grimes 	 * then copy the section that is copied directly from the parent.
340df8bae1dSRodney W. Grimes 	 */
341df8bae1dSRodney W. Grimes 	bzero(&p2->p_startzero,
342df8bae1dSRodney W. Grimes 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
343df8bae1dSRodney W. Grimes 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
344df8bae1dSRodney W. Grimes 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
345df8bae1dSRodney W. Grimes 
3462244ea07SJohn Dyson 	p2->p_aioinfo = NULL;
3472244ea07SJohn Dyson 
348df8bae1dSRodney W. Grimes 	/*
349df8bae1dSRodney W. Grimes 	 * Duplicate sub-structures as needed.
350df8bae1dSRodney W. Grimes 	 * Increase reference counts on shared objects.
351df8bae1dSRodney W. Grimes 	 * The p_stats and p_sigacts substructs are set in vm_fork.
352df8bae1dSRodney W. Grimes 	 */
353df8bae1dSRodney W. Grimes 	p2->p_flag = P_INMEM;
354df8bae1dSRodney W. Grimes 	if (p1->p_flag & P_PROFIL)
355df8bae1dSRodney W. Grimes 		startprofclock(p2);
356df8bae1dSRodney W. Grimes 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
357df8bae1dSRodney W. Grimes 	    M_SUBPROC, M_WAITOK);
358df8bae1dSRodney W. Grimes 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
359df8bae1dSRodney W. Grimes 	p2->p_cred->p_refcnt = 1;
360df8bae1dSRodney W. Grimes 	crhold(p1->p_ucred);
361df8bae1dSRodney W. Grimes 
36275c13541SPoul-Henning Kamp 	if (p2->p_prison) {
36375c13541SPoul-Henning Kamp 		p2->p_prison->pr_ref++;
36475c13541SPoul-Henning Kamp 		p2->p_flag |= P_JAILED;
36575c13541SPoul-Henning Kamp 	}
36675c13541SPoul-Henning Kamp 
367b9df5231SPoul-Henning Kamp 	if (p2->p_args)
368b9df5231SPoul-Henning Kamp 		p2->p_args->ar_ref++;
369b9df5231SPoul-Henning Kamp 
3706626c604SJulian Elischer 	if (flags & RFSIGSHARE) {
371dc9c271aSJulian Elischer 		p2->p_procsig = p1->p_procsig;
3726626c604SJulian Elischer 		p2->p_procsig->ps_refcnt++;
373dc9c271aSJulian Elischer 		if (p1->p_sigacts == &p1->p_addr->u_sigacts) {
374dc9c271aSJulian Elischer 			struct sigacts *newsigacts;
375dc9c271aSJulian Elischer 			int s;
376dc9c271aSJulian Elischer 
377dc9c271aSJulian Elischer 			/* Create the shared sigacts structure */
378df8abd0bSPeter Wemm 			MALLOC(newsigacts, struct sigacts *,
379df8abd0bSPeter Wemm 			    sizeof(struct sigacts), M_SUBPROC, M_WAITOK);
380dc9c271aSJulian Elischer 			s = splhigh();
381df8abd0bSPeter Wemm 			/*
382df8abd0bSPeter Wemm 			 * Set p_sigacts to the new shared structure.
383df8abd0bSPeter Wemm 			 * Note that this is updating p1->p_sigacts at the
384df8abd0bSPeter Wemm 			 * same time, since p_sigacts is just a pointer to
385df8abd0bSPeter Wemm 			 * the shared p_procsig->ps_sigacts.
386dc9c271aSJulian Elischer 			 */
387dc9c271aSJulian Elischer 			p2->p_sigacts  = newsigacts;
388df8abd0bSPeter Wemm 			bcopy(&p1->p_addr->u_sigacts, p2->p_sigacts,
389df8abd0bSPeter Wemm 			    sizeof(*p2->p_sigacts));
390dc9c271aSJulian Elischer 			*p2->p_sigacts = p1->p_addr->u_sigacts;
391dc9c271aSJulian Elischer 			splx(s);
392dc9c271aSJulian Elischer 		}
3936626c604SJulian Elischer 	} else {
394dc9c271aSJulian Elischer 		MALLOC(p2->p_procsig, struct procsig *, sizeof(struct procsig),
395dc9c271aSJulian Elischer 		    M_SUBPROC, M_WAITOK);
396df8abd0bSPeter Wemm 		bcopy(p1->p_procsig, p2->p_procsig, sizeof(*p2->p_procsig));
397dc9c271aSJulian Elischer 		p2->p_procsig->ps_refcnt = 1;
398df8abd0bSPeter Wemm 		p2->p_sigacts = NULL;	/* finished in vm_fork() */
3996626c604SJulian Elischer 	}
4004ac9ae70SJulian Elischer 	if (flags & RFLINUXTHPN)
4016626c604SJulian Elischer 	        p2->p_sigparent = SIGUSR1;
4024ac9ae70SJulian Elischer 	else
4034ac9ae70SJulian Elischer 	        p2->p_sigparent = SIGCHLD;
40488c5ea45SJulian Elischer 
405df8bae1dSRodney W. Grimes 	/* bump references to the text vnode (for procfs) */
406df8bae1dSRodney W. Grimes 	p2->p_textvp = p1->p_textvp;
407df8bae1dSRodney W. Grimes 	if (p2->p_textvp)
408df8bae1dSRodney W. Grimes 		VREF(p2->p_textvp);
409df8bae1dSRodney W. Grimes 
4100e3eb7eeSSujal Patel 	if (flags & RFCFDG)
411dabee6feSPeter Wemm 		p2->p_fd = fdinit(p1);
4120e3eb7eeSSujal Patel 	else if (flags & RFFDG)
413df8bae1dSRodney W. Grimes 		p2->p_fd = fdcopy(p1);
414dabee6feSPeter Wemm 	else
415dabee6feSPeter Wemm 		p2->p_fd = fdshare(p1);
416dabee6feSPeter Wemm 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * If p_limit is still copy-on-write, bump refcnt,
419df8bae1dSRodney W. Grimes 	 * otherwise get a copy that won't be modified.
420df8bae1dSRodney W. Grimes 	 * (If PL_SHAREMOD is clear, the structure is shared
421df8bae1dSRodney W. Grimes 	 * copy-on-write.)
422df8bae1dSRodney W. Grimes 	 */
423df8bae1dSRodney W. Grimes 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
424df8bae1dSRodney W. Grimes 		p2->p_limit = limcopy(p1->p_limit);
425df8bae1dSRodney W. Grimes 	else {
426df8bae1dSRodney W. Grimes 		p2->p_limit = p1->p_limit;
427df8bae1dSRodney W. Grimes 		p2->p_limit->p_refcnt++;
428df8bae1dSRodney W. Grimes 	}
429df8bae1dSRodney W. Grimes 
43070e534e7SDavid Greenman 	/*
431be67169aSBruce Evans 	 * Preserve some more flags in subprocess.  P_PROFIL has already
432be67169aSBruce Evans 	 * been preserved.
43370e534e7SDavid Greenman 	 */
43470e534e7SDavid Greenman 	p2->p_flag |= p1->p_flag & P_SUGID;
435df8bae1dSRodney W. Grimes 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
436df8bae1dSRodney W. Grimes 		p2->p_flag |= P_CONTROLT;
4370e3eb7eeSSujal Patel 	if (flags & RFPPWAIT)
438df8bae1dSRodney W. Grimes 		p2->p_flag |= P_PPWAIT;
439be67169aSBruce Evans 
440b75356e1SJeffrey Hsu 	LIST_INSERT_AFTER(p1, p2, p_pglist);
4410e3eb7eeSSujal Patel 
4420e3eb7eeSSujal Patel 	/*
4430e3eb7eeSSujal Patel 	 * Attach the new process to its parent.
4440e3eb7eeSSujal Patel 	 *
4450e3eb7eeSSujal Patel 	 * If RFNOWAIT is set, the newly created process becomes a child
4460e3eb7eeSSujal Patel 	 * of init.  This effectively disassociates the child from the
4470e3eb7eeSSujal Patel 	 * parent.
4480e3eb7eeSSujal Patel 	 */
4490e3eb7eeSSujal Patel 	if (flags & RFNOWAIT)
4500e3eb7eeSSujal Patel 		pptr = initproc;
4510e3eb7eeSSujal Patel 	else
4520e3eb7eeSSujal Patel 		pptr = p1;
4530e3eb7eeSSujal Patel 	p2->p_pptr = pptr;
4540e3eb7eeSSujal Patel 	LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling);
455b75356e1SJeffrey Hsu 	LIST_INIT(&p2->p_children);
456b75356e1SJeffrey Hsu 
457df8bae1dSRodney W. Grimes #ifdef KTRACE
458df8bae1dSRodney W. Grimes 	/*
459df8bae1dSRodney W. Grimes 	 * Copy traceflag and tracefile if enabled.
460df8bae1dSRodney W. Grimes 	 * If not inherited, these were zeroed above.
461df8bae1dSRodney W. Grimes 	 */
462df8bae1dSRodney W. Grimes 	if (p1->p_traceflag&KTRFAC_INHERIT) {
463df8bae1dSRodney W. Grimes 		p2->p_traceflag = p1->p_traceflag;
464df8bae1dSRodney W. Grimes 		if ((p2->p_tracep = p1->p_tracep) != NULL)
465df8bae1dSRodney W. Grimes 			VREF(p2->p_tracep);
466df8bae1dSRodney W. Grimes 	}
467df8bae1dSRodney W. Grimes #endif
468df8bae1dSRodney W. Grimes 
469df8bae1dSRodney W. Grimes 	/*
4700d2afceeSDavid Greenman 	 * set priority of child to be that of parent
4710d2afceeSDavid Greenman 	 */
4720d2afceeSDavid Greenman 	p2->p_estcpu = p1->p_estcpu;
4730d2afceeSDavid Greenman 
4740d2afceeSDavid Greenman 	/*
475df8bae1dSRodney W. Grimes 	 * This begins the section where we must prevent the parent
476df8bae1dSRodney W. Grimes 	 * from being swapped.
477df8bae1dSRodney W. Grimes 	 */
478af8ad83eSPeter Wemm 	PHOLD(p1);
4790d2afceeSDavid Greenman 
480df8bae1dSRodney W. Grimes 	/*
481a2a1c95cSPeter Wemm 	 * Finish creating the child process.  It will return via a different
482a2a1c95cSPeter Wemm 	 * execution path later.  (ie: directly into user mode)
483dabee6feSPeter Wemm 	 */
484a2a1c95cSPeter Wemm 	vm_fork(p1, p2, flags);
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 	/*
487e9189611SPeter Wemm 	 * Both processes are set up, now check if any loadable modules want
488e0d898b4SJulian Elischer 	 * to adjust anything.
489fed06968SJulian Elischer 	 *   What if they have an error? XXX
490fed06968SJulian Elischer 	 */
49193efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &fork_list, next) {
492fed06968SJulian Elischer 		(*ep->function)(p1, p2, flags);
493fed06968SJulian Elischer 	}
494fed06968SJulian Elischer 
495fed06968SJulian Elischer 	/*
496df8bae1dSRodney W. Grimes 	 * Make child runnable and add to run queue.
497df8bae1dSRodney W. Grimes 	 */
498a2a1c95cSPeter Wemm 	microtime(&(p2->p_stats->p_start));
499a2a1c95cSPeter Wemm 	p2->p_acflag = AFORK;
500df8bae1dSRodney W. Grimes 	(void) splhigh();
501df8bae1dSRodney W. Grimes 	p2->p_stat = SRUN;
502df8bae1dSRodney W. Grimes 	setrunqueue(p2);
503df8bae1dSRodney W. Grimes 	(void) spl0();
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 	/*
506df8bae1dSRodney W. Grimes 	 * Now can be swapped.
507df8bae1dSRodney W. Grimes 	 */
508af8ad83eSPeter Wemm 	PRELE(p1);
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 	/*
511cb679c38SJonathan Lemon 	 * tell any interested parties about the new process
512cb679c38SJonathan Lemon 	 */
513cb679c38SJonathan Lemon 	KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
514cb679c38SJonathan Lemon 
515cb679c38SJonathan Lemon 	/*
516df8bae1dSRodney W. Grimes 	 * Preserve synchronization semantics of vfork.  If waiting for
517df8bae1dSRodney W. Grimes 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
518df8bae1dSRodney W. Grimes 	 * proc (in case of exit).
519df8bae1dSRodney W. Grimes 	 */
520df8bae1dSRodney W. Grimes 	while (p2->p_flag & P_PPWAIT)
521df8bae1dSRodney W. Grimes 		tsleep(p1, PWAIT, "ppwait", 0);
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 	/*
524df8abd0bSPeter Wemm 	 * Return child proc pointer to parent.
525df8bae1dSRodney W. Grimes 	 */
526df8abd0bSPeter Wemm 	*procp = p2;
527df8bae1dSRodney W. Grimes 	return (0);
528df8bae1dSRodney W. Grimes }
529fed06968SJulian Elischer 
530e0d898b4SJulian Elischer /*
531e0d898b4SJulian Elischer  * The next two functionms are general routines to handle adding/deleting
532e0d898b4SJulian Elischer  * items on the fork callout list.
533e0d898b4SJulian Elischer  *
534e0d898b4SJulian Elischer  * at_fork():
535e0d898b4SJulian Elischer  * Take the arguments given and put them onto the fork callout list,
536fed06968SJulian Elischer  * However first make sure that it's not already there.
537e0d898b4SJulian Elischer  * Returns 0 on success or a standard error number.
538fed06968SJulian Elischer  */
53993efcae8SPoul-Henning Kamp 
540fed06968SJulian Elischer int
541eb776aeaSBruce Evans at_fork(function)
542eb776aeaSBruce Evans 	forklist_fn function;
543fed06968SJulian Elischer {
54493efcae8SPoul-Henning Kamp 	struct forklist *ep;
545e0d898b4SJulian Elischer 
54693efcae8SPoul-Henning Kamp #ifdef INVARIANTS
547e0d898b4SJulian Elischer 	/* let the programmer know if he's been stupid */
548e0d898b4SJulian Elischer 	if (rm_at_fork(function))
54993efcae8SPoul-Henning Kamp 		printf("WARNING: fork callout entry (%p) already present\n",
55093efcae8SPoul-Henning Kamp 		    function);
55193efcae8SPoul-Henning Kamp #endif
55293efcae8SPoul-Henning Kamp 	ep = malloc(sizeof(*ep), M_ATFORK, M_NOWAIT);
553e0d898b4SJulian Elischer 	if (ep == NULL)
554e0d898b4SJulian Elischer 		return (ENOMEM);
555fed06968SJulian Elischer 	ep->function = function;
55693efcae8SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&fork_list, ep, next);
557e0d898b4SJulian Elischer 	return (0);
558fed06968SJulian Elischer }
559e0d898b4SJulian Elischer 
560fed06968SJulian Elischer /*
56193efcae8SPoul-Henning Kamp  * Scan the exit callout list for the given item and remove it..
56293efcae8SPoul-Henning Kamp  * Returns the number of items removed (0 or 1)
563fed06968SJulian Elischer  */
56493efcae8SPoul-Henning Kamp 
565fed06968SJulian Elischer int
566eb776aeaSBruce Evans rm_at_fork(function)
567eb776aeaSBruce Evans 	forklist_fn function;
568fed06968SJulian Elischer {
56993efcae8SPoul-Henning Kamp 	struct forklist *ep;
570fed06968SJulian Elischer 
57193efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &fork_list, next) {
572fed06968SJulian Elischer 		if (ep->function == function) {
57393efcae8SPoul-Henning Kamp 			TAILQ_REMOVE(&fork_list, ep, next);
57493efcae8SPoul-Henning Kamp 			free(ep, M_ATFORK);
57593efcae8SPoul-Henning Kamp 			return(1);
576fed06968SJulian Elischer 		}
577fed06968SJulian Elischer 	}
57893efcae8SPoul-Henning Kamp 	return (0);
579fed06968SJulian Elischer }
580