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