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