kern_fork.c revision 1.60 1 /* $NetBSD: kern_fork.c,v 1.60 1999/07/22 18:28:30 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
41 */
42
43 #include "opt_ktrace.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/map.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/pool.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
56 #include <sys/file.h>
57 #include <sys/acct.h>
58 #include <sys/ktrace.h>
59 #include <sys/vmmeter.h>
60 #include <sys/sched.h>
61 #include <sys/signalvar.h>
62
63 #include <sys/syscallargs.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_kern.h>
67
68 #include <uvm/uvm_extern.h>
69
70 int nprocs = 1; /* process 0 */
71
72 /*ARGSUSED*/
73 int
74 sys_fork(p, v, retval)
75 struct proc *p;
76 void *v;
77 register_t *retval;
78 {
79
80 return (fork1(p, 0, SIGCHLD, NULL, 0, retval, NULL));
81 }
82
83 /*
84 * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
85 * Address space is not shared, but parent is blocked until child exit.
86 */
87 /*ARGSUSED*/
88 int
89 sys_vfork(p, v, retval)
90 struct proc *p;
91 void *v;
92 register_t *retval;
93 {
94
95 return (fork1(p, FORK_PPWAIT, SIGCHLD, NULL, 0, retval, NULL));
96 }
97
98 /*
99 * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
100 * semantics. Address space is shared, and parent is blocked until child exit.
101 */
102 /*ARGSUSED*/
103 int
104 sys___vfork14(p, v, retval)
105 struct proc *p;
106 void *v;
107 register_t *retval;
108 {
109
110 return (fork1(p, FORK_PPWAIT|FORK_SHAREVM, SIGCHLD, NULL, 0,
111 retval, NULL));
112 }
113
114 int
115 fork1(p1, flags, exitsig, stack, stacksize, retval, rnewprocp)
116 register struct proc *p1;
117 int flags;
118 int exitsig;
119 void *stack;
120 size_t stacksize;
121 register_t *retval;
122 struct proc **rnewprocp;
123 {
124 register struct proc *p2;
125 register uid_t uid;
126 struct proc *newproc;
127 int count, s;
128 vaddr_t uaddr;
129 static int nextpid, pidchecked = 0;
130
131 /*
132 * Although process entries are dynamically created, we still keep
133 * a global limit on the maximum number we will create. Don't allow
134 * a nonprivileged user to use the last process; don't let root
135 * exceed the limit. The variable nprocs is the current number of
136 * processes, maxproc is the limit.
137 */
138 uid = p1->p_cred->p_ruid;
139 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
140 tablefull("proc");
141 return (EAGAIN);
142 }
143
144 /*
145 * Increment the count of procs running with this uid. Don't allow
146 * a nonprivileged user to exceed their current limit.
147 */
148 count = chgproccnt(uid, 1);
149 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
150 (void)chgproccnt(uid, -1);
151 return (EAGAIN);
152 }
153
154 /*
155 * Allocate virtual address space for the U-area now, while it
156 * is still easy to abort the fork operation if we're out of
157 * kernel virtual address space. The actual U-area pages will
158 * be allocated and wired in vm_fork().
159 */
160 uaddr = uvm_km_valloc(kernel_map, USPACE);
161 if (uaddr == 0) {
162 (void)chgproccnt(uid, -1);
163 return (ENOMEM);
164 }
165
166 /*
167 * We are now committed to the fork. From here on, we may
168 * block on resources, but resource allocation may NOT fail.
169 */
170
171 /* Allocate new proc. */
172 newproc = pool_get(&proc_pool, PR_WAITOK);
173
174 /*
175 * BEGIN PID ALLOCATION. (Lock PID allocation variables eventually).
176 */
177
178 /*
179 * Find an unused process ID. We remember a range of unused IDs
180 * ready to use (from nextpid+1 through pidchecked-1).
181 */
182 nextpid++;
183 retry:
184 /*
185 * If the process ID prototype has wrapped around,
186 * restart somewhat above 0, as the low-numbered procs
187 * tend to include daemons that don't exit.
188 */
189 if (nextpid >= PID_MAX) {
190 nextpid = 100;
191 pidchecked = 0;
192 }
193 if (nextpid >= pidchecked) {
194 const struct proclist_desc *pd;
195
196 pidchecked = PID_MAX;
197 /*
198 * Scan the process lists to check whether this pid
199 * is in use. Remember the lowest pid that's greater
200 * than nextpid, so we can avoid checking for a while.
201 */
202 pd = proclists;
203 again:
204 for (p2 = LIST_FIRST(pd->pd_list); p2 != 0;
205 p2 = LIST_NEXT(p2, p_list)) {
206 while (p2->p_pid == nextpid ||
207 p2->p_pgrp->pg_id == nextpid ||
208 p2->p_session->s_sid == nextpid) {
209 nextpid++;
210 if (nextpid >= pidchecked)
211 goto retry;
212 }
213 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
214 pidchecked = p2->p_pid;
215
216 if (p2->p_pgrp->pg_id > nextpid &&
217 pidchecked > p2->p_pgrp->pg_id)
218 pidchecked = p2->p_pgrp->pg_id;
219
220 if (p2->p_session->s_sid > nextpid &&
221 pidchecked > p2->p_session->s_sid)
222 pidchecked = p2->p_session->s_sid;
223 }
224
225 /*
226 * If there's another list, scan it. If we have checked
227 * them all, we've found one!
228 */
229 pd++;
230 if (pd->pd_list != NULL)
231 goto again;
232 }
233
234 nprocs++;
235 p2 = newproc;
236
237 /* Record the pid we've allocated. */
238 p2->p_pid = nextpid;
239
240 /* Record the signal to be delivered to the parent on exit. */
241 p2->p_exitsig = exitsig;
242
243 /*
244 * Put the proc on allproc before unlocking PID allocation
245 * so that waiters won't grab it as soon as we unlock.
246 */
247
248 p2->p_stat = SIDL; /* protect against others */
249 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */
250
251 LIST_INSERT_HEAD(&allproc, p2, p_list);
252
253 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
254
255 /*
256 * END PID ALLOCATION. (Unlock PID allocation variables).
257 */
258
259 /*
260 * Make a proc table entry for the new process.
261 * Start by zeroing the section of proc that is zero-initialized,
262 * then copy the section that is copied directly from the parent.
263 */
264 memset(&p2->p_startzero, 0,
265 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
266 memcpy(&p2->p_startcopy, &p1->p_startcopy,
267 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
268
269 /*
270 * Duplicate sub-structures as needed.
271 * Increase reference counts on shared objects.
272 * The p_stats and p_sigacts substructs are set in vm_fork.
273 */
274 p2->p_flag = P_INMEM | (p1->p_flag & P_SUGID);
275 p2->p_emul = p1->p_emul;
276 if (p1->p_flag & P_PROFIL)
277 startprofclock(p2);
278 p2->p_cred = pool_get(&pcred_pool, PR_WAITOK);
279 memcpy(p2->p_cred, p1->p_cred, sizeof(*p2->p_cred));
280 p2->p_cred->p_refcnt = 1;
281 crhold(p1->p_ucred);
282
283 /* bump references to the text vnode (for procfs) */
284 p2->p_textvp = p1->p_textvp;
285 if (p2->p_textvp)
286 VREF(p2->p_textvp);
287
288 if (flags & FORK_SHAREFILES)
289 fdshare(p1, p2);
290 else
291 p2->p_fd = fdcopy(p1);
292
293 if (flags & FORK_SHARECWD)
294 cwdshare(p1, p2);
295 else
296 p2->p_cwdi = cwdinit(p1);
297
298 /*
299 * If p_limit is still copy-on-write, bump refcnt,
300 * otherwise get a copy that won't be modified.
301 * (If PL_SHAREMOD is clear, the structure is shared
302 * copy-on-write.)
303 */
304 if (p1->p_limit->p_lflags & PL_SHAREMOD)
305 p2->p_limit = limcopy(p1->p_limit);
306 else {
307 p2->p_limit = p1->p_limit;
308 p2->p_limit->p_refcnt++;
309 }
310
311 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
312 p2->p_flag |= P_CONTROLT;
313 if (flags & FORK_PPWAIT)
314 p2->p_flag |= P_PPWAIT;
315 LIST_INSERT_AFTER(p1, p2, p_pglist);
316 p2->p_pptr = p1;
317 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
318 LIST_INIT(&p2->p_children);
319
320 #ifdef KTRACE
321 /*
322 * Copy traceflag and tracefile if enabled.
323 * If not inherited, these were zeroed above.
324 */
325 if (p1->p_traceflag&KTRFAC_INHERIT) {
326 p2->p_traceflag = p1->p_traceflag;
327 if ((p2->p_tracep = p1->p_tracep) != NULL)
328 ktradref(p2);
329 }
330 #endif
331 scheduler_fork_hook(p1, p2);
332
333 /*
334 * Create signal actions for the child process.
335 */
336 if (flags & FORK_SHARESIGS)
337 sigactsshare(p1, p2);
338 else
339 p2->p_sigacts = sigactsinit(p1);
340
341 /*
342 * This begins the section where we must prevent the parent
343 * from being swapped.
344 */
345 PHOLD(p1);
346
347 /*
348 * Finish creating the child process. It will return through a
349 * different path later.
350 */
351 p2->p_addr = (struct user *)uaddr;
352 uvm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE,
353 stack, stacksize);
354
355 /*
356 * Make child runnable, set start time, and add to run queue.
357 */
358 s = splstatclock();
359 p2->p_stats->p_start = time;
360 p2->p_acflag = AFORK;
361 p2->p_stat = SRUN;
362 setrunqueue(p2);
363 splx(s);
364
365 /*
366 * Now can be swapped.
367 */
368 PRELE(p1);
369
370 /*
371 * Update stats now that we know the fork was successful.
372 */
373 uvmexp.forks++;
374 if (flags & FORK_PPWAIT)
375 uvmexp.forks_ppwait++;
376 if (flags & FORK_SHAREVM)
377 uvmexp.forks_sharevm++;
378
379 /*
380 * Pass a pointer to the new process to the caller.
381 */
382 if (rnewprocp != NULL)
383 *rnewprocp = p2;
384
385 /*
386 * Preserve synchronization semantics of vfork. If waiting for
387 * child to exec or exit, set P_PPWAIT on child, and sleep on our
388 * proc (in case of exit).
389 */
390 if (flags & FORK_PPWAIT)
391 while (p2->p_flag & P_PPWAIT)
392 tsleep(p1, PWAIT, "ppwait", 0);
393
394 /*
395 * Return child pid to parent process,
396 * marking us as parent via retval[1].
397 */
398 if (retval != NULL) {
399 retval[0] = p2->p_pid;
400 retval[1] = 0;
401 }
402 return (0);
403 }
404