kern_fork.c revision 1.46 1 /* $NetBSD: kern_fork.c,v 1.46 1998/08/13 02:10:57 eeh 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 #include "opt_uvm.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/map.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/pool.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
57 #include <sys/file.h>
58 #include <sys/acct.h>
59 #include <sys/ktrace.h>
60 #include <sys/vmmeter.h>
61
62 #include <sys/syscallargs.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_kern.h>
66
67 #if defined(UVM)
68 #include <uvm/uvm_extern.h>
69 #endif
70
71 int nprocs = 1; /* process 0 */
72
73 /*ARGSUSED*/
74 int
75 sys_fork(p, v, retval)
76 struct proc *p;
77 void *v;
78 register_t *retval;
79 {
80
81 return (fork1(p, 0, retval, NULL));
82 }
83
84 /*
85 * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
86 * Address space is not shared, but parent is blocked until child exit.
87 */
88 /*ARGSUSED*/
89 int
90 sys_vfork(p, v, retval)
91 struct proc *p;
92 void *v;
93 register_t *retval;
94 {
95
96 return (fork1(p, FORK_PPWAIT, retval, NULL));
97 }
98
99 /*
100 * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
101 * semantics. Address space is shared, and parent is blocked until child exit.
102 */
103 /*ARGSUSED*/
104 int
105 sys___vfork14(p, v, retval)
106 struct proc *p;
107 void *v;
108 register_t *retval;
109 {
110
111 return (fork1(p, FORK_PPWAIT|FORK_SHAREVM, retval, NULL));
112 }
113
114 int
115 fork1(p1, flags, retval, rnewprocp)
116 register struct proc *p1;
117 int flags;
118 register_t *retval;
119 struct proc **rnewprocp;
120 {
121 register struct proc *p2;
122 register uid_t uid;
123 struct proc *newproc;
124 int count;
125 vaddr_t uaddr;
126 static int nextpid, pidchecked = 0;
127
128 /*
129 * Although process entries are dynamically created, we still keep
130 * a global limit on the maximum number we will create. Don't allow
131 * a nonprivileged user to use the last process; don't let root
132 * exceed the limit. The variable nprocs is the current number of
133 * processes, maxproc is the limit.
134 */
135 uid = p1->p_cred->p_ruid;
136 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
137 tablefull("proc");
138 return (EAGAIN);
139 }
140
141 /*
142 * Increment the count of procs running with this uid. Don't allow
143 * a nonprivileged user to exceed their current limit.
144 */
145 count = chgproccnt(uid, 1);
146 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
147 (void)chgproccnt(uid, -1);
148 return (EAGAIN);
149 }
150
151 /*
152 * Allocate virtual address space for the U-area now, while it
153 * is still easy to abort the fork operation if we're out of
154 * kernel virtual address space. The actual U-area pages will
155 * be allocated and wired in vm_fork().
156 */
157 #if defined(UVM)
158 uaddr = uvm_km_valloc(kernel_map, USPACE);
159 #else
160 uaddr = kmem_alloc_pageable(kernel_map, USPACE);
161 #endif
162 if (uaddr == 0) {
163 (void)chgproccnt(uid, -1);
164 return (ENOMEM);
165 }
166
167 /*
168 * We are now committed to the fork. From here on, we may
169 * block on resources, but resource allocation may NOT fail.
170 */
171
172 /* Allocate new proc. */
173 newproc = pool_get(&proc_pool, PR_WAITOK);
174
175 /*
176 * Find an unused process ID. We remember a range of unused IDs
177 * ready to use (from nextpid+1 through pidchecked-1).
178 */
179 nextpid++;
180 retry:
181 /*
182 * If the process ID prototype has wrapped around,
183 * restart somewhat above 0, as the low-numbered procs
184 * tend to include daemons that don't exit.
185 */
186 if (nextpid >= PID_MAX) {
187 nextpid = 100;
188 pidchecked = 0;
189 }
190 if (nextpid >= pidchecked) {
191 int doingzomb = 0;
192
193 pidchecked = PID_MAX;
194 /*
195 * Scan the active and zombie procs to check whether this pid
196 * is in use. Remember the lowest pid that's greater
197 * than nextpid, so we can avoid checking for a while.
198 */
199 p2 = allproc.lh_first;
200 again:
201 for (; p2 != 0; p2 = p2->p_list.le_next) {
202 while (p2->p_pid == nextpid ||
203 p2->p_pgrp->pg_id == nextpid ||
204 p2->p_session->s_sid == nextpid) {
205 nextpid++;
206 if (nextpid >= pidchecked)
207 goto retry;
208 }
209 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
210 pidchecked = p2->p_pid;
211
212 if (p2->p_pgrp->pg_id > nextpid &&
213 pidchecked > p2->p_pgrp->pg_id)
214 pidchecked = p2->p_pgrp->pg_id;
215
216 if (p2->p_session->s_sid > nextpid &&
217 pidchecked > p2->p_session->s_sid)
218 pidchecked = p2->p_session->s_sid;
219 }
220 if (!doingzomb) {
221 doingzomb = 1;
222 p2 = zombproc.lh_first;
223 goto again;
224 }
225 }
226
227 nprocs++;
228 p2 = newproc;
229 p2->p_stat = SIDL; /* protect against others */
230 p2->p_pid = nextpid;
231 LIST_INSERT_HEAD(&allproc, p2, p_list);
232 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */
233 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
234
235 /*
236 * Make a proc table entry for the new process.
237 * Start by zeroing the section of proc that is zero-initialized,
238 * then copy the section that is copied directly from the parent.
239 */
240 memset(&p2->p_startzero, 0,
241 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
242 memcpy(&p2->p_startcopy, &p1->p_startcopy,
243 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
244
245 /*
246 * Duplicate sub-structures as needed.
247 * Increase reference counts on shared objects.
248 * The p_stats and p_sigacts substructs are set in vm_fork.
249 */
250 p2->p_flag = P_INMEM | (p1->p_flag & P_SUGID);
251 p2->p_emul = p1->p_emul;
252 if (p1->p_flag & P_PROFIL)
253 startprofclock(p2);
254 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
255 M_SUBPROC, M_WAITOK);
256 memcpy(p2->p_cred, p1->p_cred, sizeof(*p2->p_cred));
257 p2->p_cred->p_refcnt = 1;
258 crhold(p1->p_ucred);
259
260 /* bump references to the text vnode (for procfs) */
261 p2->p_textvp = p1->p_textvp;
262 if (p2->p_textvp)
263 VREF(p2->p_textvp);
264
265 p2->p_fd = fdcopy(p1);
266 /*
267 * If p_limit is still copy-on-write, bump refcnt,
268 * otherwise get a copy that won't be modified.
269 * (If PL_SHAREMOD is clear, the structure is shared
270 * copy-on-write.)
271 */
272 if (p1->p_limit->p_lflags & PL_SHAREMOD)
273 p2->p_limit = limcopy(p1->p_limit);
274 else {
275 p2->p_limit = p1->p_limit;
276 p2->p_limit->p_refcnt++;
277 }
278
279 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
280 p2->p_flag |= P_CONTROLT;
281 if (flags & FORK_PPWAIT)
282 p2->p_flag |= P_PPWAIT;
283 LIST_INSERT_AFTER(p1, p2, p_pglist);
284 p2->p_pptr = p1;
285 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
286 LIST_INIT(&p2->p_children);
287
288 #ifdef KTRACE
289 /*
290 * Copy traceflag and tracefile if enabled.
291 * If not inherited, these were zeroed above.
292 */
293 if (p1->p_traceflag&KTRFAC_INHERIT) {
294 p2->p_traceflag = p1->p_traceflag;
295 if ((p2->p_tracep = p1->p_tracep) != NULL)
296 ktradref(p2);
297 }
298 #endif
299
300 /*
301 * This begins the section where we must prevent the parent
302 * from being swapped.
303 */
304 PHOLD(p1);
305
306 /*
307 * Finish creating the child process. It will return through a
308 * different path later.
309 */
310 p2->p_addr = (struct user *)uaddr;
311 #if defined(UVM)
312 uvm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE);
313 #else
314 vm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE);
315 #endif
316
317 /*
318 * Make child runnable, set start time, and add to run queue.
319 */
320 (void) splstatclock();
321 p2->p_stats->p_start = time;
322 p2->p_acflag = AFORK;
323 p2->p_stat = SRUN;
324 setrunqueue(p2);
325 (void) spl0();
326
327 /*
328 * Now can be swapped.
329 */
330 PRELE(p1);
331
332 /*
333 * Update stats now that we know the fork was successful.
334 */
335 #if defined(UVM)
336 uvmexp.forks++;
337 if (flags & FORK_PPWAIT)
338 uvmexp.forks_ppwait++;
339 if (flags & FORK_SHAREVM)
340 uvmexp.forks_sharevm++;
341 #else
342 cnt.v_forks++;
343 if (flags & FORK_PPWAIT)
344 cnt.v_forks_ppwait++;
345 if (flags & FORK_SHAREVM)
346 cnt.v_forks_sharevm++;
347 #endif
348
349 /*
350 * Pass a pointer to the new process to the caller.
351 */
352 if (rnewprocp != NULL)
353 *rnewprocp = p2;
354
355 /*
356 * Preserve synchronization semantics of vfork. If waiting for
357 * child to exec or exit, set P_PPWAIT on child, and sleep on our
358 * proc (in case of exit).
359 */
360 if (flags & FORK_PPWAIT)
361 while (p2->p_flag & P_PPWAIT)
362 tsleep(p1, PWAIT, "ppwait", 0);
363
364 /*
365 * Return child pid to parent process,
366 * marking us as parent via retval[1].
367 */
368 if (retval != NULL) {
369 retval[0] = p2->p_pid;
370 retval[1] = 0;
371 }
372 return (0);
373 }
374