kern_fork.c revision 1.39 1 /* $NetBSD: kern_fork.c,v 1.39 1998/02/14 00:37:31 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.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 p2->p_session->s_sid == nextpid) {
180 nextpid++;
181 if (nextpid >= pidchecked)
182 goto retry;
183 }
184 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
185 pidchecked = p2->p_pid;
186
187 if (p2->p_pgrp->pg_id > nextpid &&
188 pidchecked > p2->p_pgrp->pg_id)
189 pidchecked = p2->p_pgrp->pg_id;
190
191 if (p2->p_session->s_sid > nextpid &&
192 pidchecked > p2->p_session->s_sid)
193 pidchecked = p2->p_session->s_sid;
194 }
195 if (!doingzomb) {
196 doingzomb = 1;
197 p2 = zombproc.lh_first;
198 goto again;
199 }
200 }
201
202 nprocs++;
203 p2 = newproc;
204 p2->p_stat = SIDL; /* protect against others */
205 p2->p_pid = nextpid;
206 LIST_INSERT_HEAD(&allproc, p2, p_list);
207 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */
208 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
209
210 /*
211 * Make a proc table entry for the new process.
212 * Start by zeroing the section of proc that is zero-initialized,
213 * then copy the section that is copied directly from the parent.
214 */
215 bzero(&p2->p_startzero,
216 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
217 bcopy(&p1->p_startcopy, &p2->p_startcopy,
218 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
219
220 /*
221 * Duplicate sub-structures as needed.
222 * Increase reference counts on shared objects.
223 * The p_stats and p_sigacts substructs are set in vm_fork.
224 */
225 p2->p_flag = P_INMEM | (p1->p_flag & P_SUGID);
226 p2->p_emul = p1->p_emul;
227 if (p1->p_flag & P_PROFIL)
228 startprofclock(p2);
229 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
230 M_SUBPROC, M_WAITOK);
231 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
232 p2->p_cred->p_refcnt = 1;
233 crhold(p1->p_ucred);
234
235 /* bump references to the text vnode (for procfs) */
236 p2->p_textvp = p1->p_textvp;
237 if (p2->p_textvp)
238 VREF(p2->p_textvp);
239
240 p2->p_fd = fdcopy(p1);
241 /*
242 * If p_limit is still copy-on-write, bump refcnt,
243 * otherwise get a copy that won't be modified.
244 * (If PL_SHAREMOD is clear, the structure is shared
245 * copy-on-write.)
246 */
247 if (p1->p_limit->p_lflags & PL_SHAREMOD)
248 p2->p_limit = limcopy(p1->p_limit);
249 else {
250 p2->p_limit = p1->p_limit;
251 p2->p_limit->p_refcnt++;
252 }
253
254 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
255 p2->p_flag |= P_CONTROLT;
256 if (flags & FORK_PPWAIT)
257 p2->p_flag |= P_PPWAIT;
258 LIST_INSERT_AFTER(p1, p2, p_pglist);
259 p2->p_pptr = p1;
260 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
261 LIST_INIT(&p2->p_children);
262
263 #ifdef KTRACE
264 /*
265 * Copy traceflag and tracefile if enabled.
266 * If not inherited, these were zeroed above.
267 */
268 if (p1->p_traceflag&KTRFAC_INHERIT) {
269 p2->p_traceflag = p1->p_traceflag;
270 if ((p2->p_tracep = p1->p_tracep) != NULL)
271 VREF(p2->p_tracep);
272 }
273 #endif
274
275 /*
276 * This begins the section where we must prevent the parent
277 * from being swapped.
278 */
279 PHOLD(p1);
280
281 /*
282 * Finish creating the child process. It will return through a
283 * different path later.
284 */
285 #if defined(UVM)
286 uvm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE);
287 #else
288 vm_fork(p1, p2, (flags & FORK_SHAREVM) ? TRUE : FALSE);
289 #endif
290
291 /*
292 * Make child runnable, set start time, and add to run queue.
293 */
294 (void) splstatclock();
295 p2->p_stats->p_start = time;
296 p2->p_acflag = AFORK;
297 p2->p_stat = SRUN;
298 setrunqueue(p2);
299 (void) spl0();
300
301 /*
302 * Now can be swapped.
303 */
304 PRELE(p1);
305
306 /*
307 * Update stats now that we know the fork was successful.
308 */
309 #if defined(UVM)
310 uvmexp.forks++;
311 if (flags & FORK_PPWAIT)
312 uvmexp.forks_ppwait++;
313 if (flags & FORK_SHAREVM)
314 uvmexp.forks_sharevm++;
315 #else
316 cnt.v_forks++;
317 if (flags & FORK_PPWAIT)
318 cnt.v_forks_ppwait++;
319 if (flags & FORK_SHAREVM)
320 cnt.v_forks_sharevm++;
321 #endif
322
323 /*
324 * Pass a pointer to the new process to the caller.
325 */
326 if (rnewprocp != NULL)
327 *rnewprocp = p2;
328
329 /*
330 * Preserve synchronization semantics of vfork. If waiting for
331 * child to exec or exit, set P_PPWAIT on child, and sleep on our
332 * proc (in case of exit).
333 */
334 if (flags & FORK_PPWAIT)
335 while (p2->p_flag & P_PPWAIT)
336 tsleep(p1, PWAIT, "ppwait", 0);
337
338 /*
339 * Return child pid to parent process,
340 * marking us as parent via retval[1].
341 */
342 if (retval != NULL) {
343 retval[0] = p2->p_pid;
344 retval[1] = 0;
345 }
346 return (0);
347 }
348