kern_fork.c revision 1.19 1 /* $NetBSD: kern_fork.c,v 1.19 1994/06/29 06:32:28 cgd 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/proc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/vnode.h>
52 #include <sys/file.h>
53 #include <sys/acct.h>
54 #include <sys/ktrace.h>
55
56 /* ARGSUSED */
57 fork(p, uap, retval)
58 struct proc *p;
59 void *uap;
60 int retval[];
61 {
62
63 return (fork1(p, 0, retval));
64 }
65
66 /* ARGSUSED */
67 vfork(p, uap, retval)
68 struct proc *p;
69 void *uap;
70 int retval[];
71 {
72
73 return (fork1(p, 1, retval));
74 }
75
76 int nprocs = 1; /* process 0 */
77
78 fork1(p1, isvfork, retval)
79 register struct proc *p1;
80 int isvfork, retval[];
81 {
82 register struct proc *p2;
83 register uid_t uid;
84 struct proc *newproc;
85 struct proc **hash;
86 int count;
87 static int nextpid, pidchecked = 0;
88
89 /*
90 * Although process entries are dynamically created, we still keep
91 * a global limit on the maximum number we will create. Don't allow
92 * a nonprivileged user to use the last process; don't let root
93 * exceed the limit. The variable nprocs is the current number of
94 * processes, maxproc is the limit.
95 */
96 uid = p1->p_cred->p_ruid;
97 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
98 tablefull("proc");
99 return (EAGAIN);
100 }
101 /*
102 * Increment the count of procs running with this uid. Don't allow
103 * a nonprivileged user to exceed their current limit.
104 */
105 count = chgproccnt(uid, 1);
106 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
107 (void)chgproccnt(uid, -1);
108 return (EAGAIN);
109 }
110
111 /* Allocate new proc. */
112 MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK);
113
114 /*
115 * Find an unused process ID. We remember a range of unused IDs
116 * ready to use (from nextpid+1 through pidchecked-1).
117 */
118 nextpid++;
119 retry:
120 /*
121 * If the process ID prototype has wrapped around,
122 * restart somewhat above 0, as the low-numbered procs
123 * tend to include daemons that don't exit.
124 */
125 if (nextpid >= PID_MAX) {
126 nextpid = 100;
127 pidchecked = 0;
128 }
129 if (nextpid >= pidchecked) {
130 int doingzomb = 0;
131
132 pidchecked = PID_MAX;
133 /*
134 * Scan the active and zombie procs to check whether this pid
135 * is in use. Remember the lowest pid that's greater
136 * than nextpid, so we can avoid checking for a while.
137 */
138 p2 = (struct proc *)allproc;
139 again:
140 for (; p2 != NULL; p2 = p2->p_next) {
141 while (p2->p_pid == nextpid ||
142 p2->p_pgrp->pg_id == nextpid) {
143 nextpid++;
144 if (nextpid >= pidchecked)
145 goto retry;
146 }
147 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
148 pidchecked = p2->p_pid;
149 if (p2->p_pgrp->pg_id > nextpid &&
150 pidchecked > p2->p_pgrp->pg_id)
151 pidchecked = p2->p_pgrp->pg_id;
152 }
153 if (!doingzomb) {
154 doingzomb = 1;
155 p2 = zombproc;
156 goto again;
157 }
158 }
159
160
161 /*
162 * Link onto allproc (this should probably be delayed).
163 * Heavy use of volatile here to prevent the compiler from
164 * rearranging code. Yes, it *is* terribly ugly, but at least
165 * it works.
166 */
167 nprocs++;
168 p2 = newproc;
169 #define Vp2 ((volatile struct proc *)p2)
170 Vp2->p_stat = SIDL; /* protect against others */
171 Vp2->p_pid = nextpid;
172 /*
173 * This is really:
174 * p2->p_next = allproc;
175 * allproc->p_prev = &p2->p_next;
176 * p2->p_prev = &allproc;
177 * allproc = p2;
178 * The assignment via allproc is legal since it is never NULL.
179 */
180 *(volatile struct proc **)&Vp2->p_next = allproc;
181 *(volatile struct proc ***)&allproc->p_prev =
182 (volatile struct proc **)&Vp2->p_next;
183 *(volatile struct proc ***)&Vp2->p_prev = &allproc;
184 allproc = Vp2;
185 #undef Vp2
186 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */
187
188 /* Insert on the hash chain. */
189 hash = &pidhash[PIDHASH(p2->p_pid)];
190 p2->p_hash = *hash;
191 *hash = p2;
192
193 /*
194 * Make a proc table entry for the new process.
195 * Start by zeroing the section of proc that is zero-initialized,
196 * then copy the section that is copied directly from the parent.
197 */
198 bzero(&p2->p_startzero,
199 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
200 bcopy(&p1->p_startcopy, &p2->p_startcopy,
201 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
202
203 /*
204 * Duplicate sub-structures as needed.
205 * Increase reference counts on shared objects.
206 * The p_stats and p_sigacts substructs are set in vm_fork.
207 */
208 p2->p_flag = P_INMEM;
209 if (p1->p_flag & P_PROFIL)
210 startprofclock(p2);
211 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
212 M_SUBPROC, M_WAITOK);
213 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
214 p2->p_cred->p_refcnt = 1;
215 crhold(p1->p_ucred);
216
217 /* bump references to the text vnode (for procfs) */
218 p2->p_textvp = p1->p_textvp;
219 if (p2->p_textvp)
220 VREF(p2->p_textvp);
221
222 p2->p_fd = fdcopy(p1);
223 /*
224 * If p_limit is still copy-on-write, bump refcnt,
225 * otherwise get a copy that won't be modified.
226 * (If PL_SHAREMOD is clear, the structure is shared
227 * copy-on-write.)
228 */
229 if (p1->p_limit->p_lflags & PL_SHAREMOD)
230 p2->p_limit = limcopy(p1->p_limit);
231 else {
232 p2->p_limit = p1->p_limit;
233 p2->p_limit->p_refcnt++;
234 }
235
236 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
237 p2->p_flag |= P_CONTROLT;
238 if (isvfork)
239 p2->p_flag |= P_PPWAIT;
240 p2->p_pgrpnxt = p1->p_pgrpnxt;
241 p1->p_pgrpnxt = p2;
242 p2->p_pptr = p1;
243 p2->p_osptr = p1->p_cptr;
244 if (p1->p_cptr)
245 p1->p_cptr->p_ysptr = p2;
246 p1->p_cptr = p2;
247 #ifdef KTRACE
248 /*
249 * Copy traceflag and tracefile if enabled.
250 * If not inherited, these were zeroed above.
251 */
252 if (p1->p_traceflag&KTRFAC_INHERIT) {
253 p2->p_traceflag = p1->p_traceflag;
254 if ((p2->p_tracep = p1->p_tracep) != NULL)
255 VREF(p2->p_tracep);
256 }
257 #endif
258
259 /*
260 * This begins the section where we must prevent the parent
261 * from being swapped.
262 */
263 p1->p_holdcnt++;
264 /*
265 * Set return values for child before vm_fork,
266 * so they can be copied to child stack.
267 * We return parent pid, and mark as child in retval[1].
268 * NOTE: the kernel stack may be at a different location in the child
269 * process, and thus addresses of automatic variables (including retval)
270 * may be invalid after vm_fork returns in the child process.
271 */
272 retval[0] = p1->p_pid;
273 retval[1] = 1;
274 if (vm_fork(p1, p2, isvfork)) {
275 /*
276 * Child process. Set start time and get to work.
277 */
278 (void) splclock();
279 p2->p_stats->p_start = time;
280 (void) spl0();
281 p2->p_acflag = AFORK;
282 return (0);
283 }
284
285 /*
286 * Make child runnable and add to run queue.
287 */
288 (void) splhigh();
289 p2->p_stat = SRUN;
290 setrunqueue(p2);
291 (void) spl0();
292
293 /*
294 * Now can be swapped.
295 */
296 p1->p_holdcnt--;
297
298 /*
299 * Preserve synchronization semantics of vfork. If waiting for
300 * child to exec or exit, set P_PPWAIT on child, and sleep on our
301 * proc (in case of exit).
302 */
303 if (isvfork)
304 while (p2->p_flag & P_PPWAIT)
305 tsleep(p1, PWAIT, "ppwait", 0);
306
307 /*
308 * Return child pid to parent process,
309 * marking us as parent via retval[1].
310 */
311 retval[0] = p2->p_pid;
312 retval[1] = 0;
313 return (0);
314 }
315