xref: /original-bsd/sys/kern/kern_fork.c (revision 333da485)
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  * %sccs.include.redist.c%
11  *
12  *	@(#)kern_fork.c	8.5 (Berkeley) 01/21/94
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/map.h>
18 #include <sys/filedesc.h>
19 #include <sys/kernel.h>
20 #include <sys/malloc.h>
21 #include <sys/proc.h>
22 #include <sys/resourcevar.h>
23 #include <sys/vnode.h>
24 #include <sys/file.h>
25 #include <sys/acct.h>
26 #include <sys/ktrace.h>
27 
28 struct fork_args {
29 	int	dummy;
30 };
31 /* ARGSUSED */
32 fork(p, uap, retval)
33 	struct proc *p;
34 	struct fork_args *uap;
35 	int retval[];
36 {
37 
38 	return (fork1(p, 0, retval));
39 }
40 
41 /* ARGSUSED */
42 vfork(p, uap, retval)
43 	struct proc *p;
44 	struct fork_args *uap;
45 	int retval[];
46 {
47 
48 	return (fork1(p, 1, retval));
49 }
50 
51 int	nprocs = 1;		/* process 0 */
52 
53 fork1(p1, isvfork, retval)
54 	register struct proc *p1;
55 	int isvfork, retval[];
56 {
57 	register struct proc *p2;
58 	register uid_t uid;
59 	struct proc *newproc;
60 	struct proc **hash;
61 	int count;
62 	static int nextpid, pidchecked = 0;
63 
64 	/*
65 	 * Although process entries are dynamically created, we still keep
66 	 * a global limit on the maximum number we will create.  Don't allow
67 	 * a nonprivileged user to bring the system within one of the global
68 	 * limit; don't let root exceed the limit. The variable nprocs is
69 	 * the current number of processes, maxproc is the limit.
70 	 */
71 	uid = p1->p_cred->p_ruid;
72 	if (nprocs >= maxproc || uid == 0 && nprocs >= maxproc + 1) {
73 		tablefull("proc");
74 		return (EAGAIN);
75 	}
76 	/*
77 	 * Increment the count of procs running with this uid. Don't allow
78 	 * a nonprivileged user to exceed their current limit.
79 	 */
80 	count = chgproccnt(uid, 1);
81 	if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
82 		(void)chgproccnt(uid, -1);
83 		return (EAGAIN);
84 	}
85 
86 	/* Allocate new proc. */
87 	MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK);
88 
89 	/*
90 	 * Find an unused process ID.  We remember a range of unused IDs
91 	 * ready to use (from nextpid+1 through pidchecked-1).
92 	 */
93 	nextpid++;
94 retry:
95 	/*
96 	 * If the process ID prototype has wrapped around,
97 	 * restart somewhat above 0, as the low-numbered procs
98 	 * tend to include daemons that don't exit.
99 	 */
100 	if (nextpid >= PID_MAX) {
101 		nextpid = 100;
102 		pidchecked = 0;
103 	}
104 	if (nextpid >= pidchecked) {
105 		int doingzomb = 0;
106 
107 		pidchecked = PID_MAX;
108 		/*
109 		 * Scan the active and zombie procs to check whether this pid
110 		 * is in use.  Remember the lowest pid that's greater
111 		 * than nextpid, so we can avoid checking for a while.
112 		 */
113 		p2 = (struct proc *)allproc;
114 again:
115 		for (; p2 != NULL; p2 = p2->p_next) {
116 			while (p2->p_pid == nextpid ||
117 			    p2->p_pgrp->pg_id == nextpid) {
118 				nextpid++;
119 				if (nextpid >= pidchecked)
120 					goto retry;
121 			}
122 			if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
123 				pidchecked = p2->p_pid;
124 			if (p2->p_pgrp->pg_id > nextpid &&
125 			    pidchecked > p2->p_pgrp->pg_id)
126 				pidchecked = p2->p_pgrp->pg_id;
127 		}
128 		if (!doingzomb) {
129 			doingzomb = 1;
130 			p2 = zombproc;
131 			goto again;
132 		}
133 	}
134 
135 
136 	/*
137 	 * Link onto allproc (this should probably be delayed).
138 	 * Heavy use of volatile here to prevent the compiler from
139 	 * rearranging code.  Yes, it *is* terribly ugly, but at least
140 	 * it works.
141 	 */
142 	nprocs++;
143 	p2 = newproc;
144 #define	Vp2 ((volatile struct proc *)p2)
145 	Vp2->p_stat = SIDL;			/* protect against others */
146 	Vp2->p_pid = nextpid;
147 	/*
148 	 * This is really:
149 	 *	p2->p_next = allproc;
150 	 *	allproc->p_prev = &p2->p_next;
151 	 *	p2->p_prev = &allproc;
152 	 *	allproc = p2;
153 	 * The assignment via allproc is legal since it is never NULL.
154 	 */
155 	*(volatile struct proc **)&Vp2->p_next = allproc;
156 	*(volatile struct proc ***)&allproc->p_prev =
157 	    (volatile struct proc **)&Vp2->p_next;
158 	*(volatile struct proc ***)&Vp2->p_prev = &allproc;
159 	allproc = Vp2;
160 #undef Vp2
161 	p2->p_forw = p2->p_back = NULL;		/* shouldn't be necessary */
162 
163 	/* Insert on the hash chain. */
164 	hash = &pidhash[PIDHASH(p2->p_pid)];
165 	p2->p_hash = *hash;
166 	*hash = p2;
167 
168 	/*
169 	 * Make a proc table entry for the new process.
170 	 * Start by zeroing the section of proc that is zero-initialized,
171 	 * then copy the section that is copied directly from the parent.
172 	 */
173 	bzero(&p2->p_startzero,
174 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
175 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
176 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
177 
178 	/*
179 	 * Duplicate sub-structures as needed.
180 	 * Increase reference counts on shared objects.
181 	 * The p_stats and p_sigacts substructs are set in vm_fork.
182 	 */
183 	p2->p_flag = P_INMEM;
184 	if (p1->p_flag & P_PROFIL)
185 		startprofclock(p2);
186 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
187 	    M_SUBPROC, M_WAITOK);
188 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
189 	p2->p_cred->p_refcnt = 1;
190 	crhold(p1->p_ucred);
191 
192 	/* bump references to the text vnode (for procfs) */
193 	p2->p_textvp = p1->p_textvp;
194 	if (p2->p_textvp)
195 		VREF(p2->p_textvp);
196 
197 	p2->p_fd = fdcopy(p1);
198 	/*
199 	 * If p_limit is still copy-on-write, bump refcnt,
200 	 * otherwise get a copy that won't be modified.
201 	 * (If PL_SHAREMOD is clear, the structure is shared
202 	 * copy-on-write.)
203 	 */
204 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
205 		p2->p_limit = limcopy(p1->p_limit);
206 	else {
207 		p2->p_limit = p1->p_limit;
208 		p2->p_limit->p_refcnt++;
209 	}
210 
211 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
212 		p2->p_flag |= P_CONTROLT;
213 	if (isvfork)
214 		p2->p_flag |= P_PPWAIT;
215 	p2->p_pgrpnxt = p1->p_pgrpnxt;
216 	p1->p_pgrpnxt = p2;
217 	p2->p_pptr = p1;
218 	p2->p_osptr = p1->p_cptr;
219 	if (p1->p_cptr)
220 		p1->p_cptr->p_ysptr = p2;
221 	p1->p_cptr = p2;
222 #ifdef KTRACE
223 	/*
224 	 * Copy traceflag and tracefile if enabled.
225 	 * If not inherited, these were zeroed above.
226 	 */
227 	if (p1->p_traceflag&KTRFAC_INHERIT) {
228 		p2->p_traceflag = p1->p_traceflag;
229 		if ((p2->p_tracep = p1->p_tracep) != NULL)
230 			VREF(p2->p_tracep);
231 	}
232 #endif
233 
234 	/*
235 	 * This begins the section where we must prevent the parent
236 	 * from being swapped.
237 	 */
238 	p1->p_flag |= P_NOSWAP;
239 	/*
240 	 * Set return values for child before vm_fork,
241 	 * so they can be copied to child stack.
242 	 * We return parent pid, and mark as child in retval[1].
243 	 * NOTE: the kernel stack may be at a different location in the child
244 	 * process, and thus addresses of automatic variables (including retval)
245 	 * may be invalid after vm_fork returns in the child process.
246 	 */
247 	retval[0] = p1->p_pid;
248 	retval[1] = 1;
249 	if (vm_fork(p1, p2, isvfork)) {
250 		/*
251 		 * Child process.  Set start time and get to work.
252 		 */
253 		(void) splclock();
254 		p2->p_stats->p_start = time;
255 		(void) spl0();
256 		p2->p_acflag = AFORK;
257 		return (0);
258 	}
259 
260 	/*
261 	 * Make child runnable and add to run queue.
262 	 */
263 	(void) splhigh();
264 	p2->p_stat = SRUN;
265 	setrunqueue(p2);
266 	(void) spl0();
267 
268 	/*
269 	 * Now can be swapped.
270 	 */
271 	p1->p_flag &= ~P_NOSWAP;
272 
273 	/*
274 	 * Preserve synchronization semantics of vfork.  If waiting for
275 	 * child to exec or exit, set P_PPWAIT on child, and sleep on our
276 	 * proc (in case of exit).
277 	 */
278 	if (isvfork)
279 		while (p2->p_flag & P_PPWAIT)
280 			tsleep(p1, PWAIT, "ppwait", 0);
281 
282 	/*
283 	 * Return child pid to parent process,
284 	 * marking us as parent via retval[1].
285 	 */
286 	retval[0] = p2->p_pid;
287 	retval[1] = 0;
288 	return (0);
289 }
290