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