386BSD 0.0 development
[unix-history] / usr / src / sys.386bsd / kern / kern_fork.c
CommitLineData
d3687174
WJ
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_fork.c 7.29 (Berkeley) 5/15/91
34 */
35
36#include "param.h"
37#include "systm.h"
38#include "filedesc.h"
39#include "kernel.h"
40#include "malloc.h"
41#include "proc.h"
42#include "resourcevar.h"
43#include "vnode.h"
44#include "file.h"
45#include "acct.h"
46#include "ktrace.h"
47
48/* ARGSUSED */
49fork(p, uap, retval)
50 struct proc *p;
51 void *uap;
52 int retval[];
53{
54
55 return (fork1(p, 0, retval));
56}
57
58/* ARGSUSED */
59vfork(p, uap, retval)
60 struct proc *p;
61 void *uap;
62 int retval[];
63{
64
65 return (fork1(p, 1, retval));
66}
67
68int nprocs = 1; /* process 0 */
69
70fork1(p1, isvfork, retval)
71 register struct proc *p1;
72 int isvfork, retval[];
73{
74 register struct proc *p2;
75 register int count, uid;
76 static int nextpid, pidchecked = 0;
77
78 count = 0;
79 if ((uid = p1->p_ucred->cr_uid) != 0) {
80 for (p2 = allproc; p2; p2 = p2->p_nxt)
81 if (p2->p_ucred->cr_uid == uid)
82 count++;
83 for (p2 = zombproc; p2; p2 = p2->p_nxt)
84 if (p2->p_ucred->cr_uid == uid)
85 count++;
86 }
87 /*
88 * Although process entries are dynamically entries,
89 * we still keep a global limit on the maximum number
90 * we will create. Don't allow a nonprivileged user
91 * to exceed its current limit or to bring us within one
92 * of the global limit; don't let root exceed the limit.
93 * nprocs is the current number of processes,
94 * maxproc is the limit.
95 */
96 if (nprocs >= maxproc || uid == 0 && nprocs >= maxproc + 1) {
97 tablefull("proc");
98 return (EAGAIN);
99 }
100 if (count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)
101 return (EAGAIN);
102
103 /*
104 * Find an unused process ID.
105 * We remember a range of unused IDs ready to use
106 * (from nextpid+1 through pidchecked-1).
107 */
108 nextpid++;
109retry:
110 /*
111 * If the process ID prototype has wrapped around,
112 * restart somewhat above 0, as the low-numbered procs
113 * tend to include daemons that don't exit.
114 */
115 if (nextpid >= PID_MAX) {
116 nextpid = 100;
117 pidchecked = 0;
118 }
119 if (nextpid >= pidchecked) {
120 int doingzomb = 0;
121
122 pidchecked = PID_MAX;
123 /*
124 * Scan the active and zombie procs to check whether this pid
125 * is in use. Remember the lowest pid that's greater
126 * than nextpid, so we can avoid checking for a while.
127 */
128 p2 = allproc;
129again:
130 for (; p2 != NULL; p2 = p2->p_nxt) {
131 if (p2->p_pid == nextpid ||
132 p2->p_pgrp->pg_id == nextpid) {
133 nextpid++;
134 if (nextpid >= pidchecked)
135 goto retry;
136 }
137 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
138 pidchecked = p2->p_pid;
139 if (p2->p_pgrp->pg_id > nextpid &&
140 pidchecked > p2->p_pgrp->pg_id)
141 pidchecked = p2->p_pgrp->pg_id;
142 }
143 if (!doingzomb) {
144 doingzomb = 1;
145 p2 = zombproc;
146 goto again;
147 }
148 }
149
150
151 /*
152 * Allocate new proc.
153 * Link onto allproc (this should probably be delayed).
154 */
155 MALLOC(p2, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK);
156 nprocs++;
157 p2->p_nxt = allproc;
158 p2->p_nxt->p_prev = &p2->p_nxt; /* allproc is never NULL */
159 p2->p_prev = &allproc;
160 allproc = p2;
161 p2->p_link = NULL; /* shouldn't be necessary */
162 p2->p_rlink = NULL; /* shouldn't be necessary */
163
164 /*
165 * Make a proc table entry for the new process.
166 * Start by zeroing the section of proc that is zero-initialized,
167 * then copy the section that is copied directly from the parent.
168 */
169 bzero(&p2->p_startzero,
170 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
171 bcopy(&p1->p_startcopy, &p2->p_startcopy,
172 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
173 p2->p_spare[0] = 0; /* XXX - should be in zero range */
174 p2->p_spare[1] = 0; /* XXX - should be in zero range */
175 p2->p_spare[2] = 0; /* XXX - should be in zero range */
176 p2->p_spare[3] = 0; /* XXX - should be in zero range */
177
178 /*
179 * Duplicate sub-structures as needed.
180 * Increase reference counts on shared objects.
181 * The p_stats and p_sigacts substructs are set in vm_fork.
182 */
183 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
184 M_SUBPROC, M_WAITOK);
185 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
186 p2->p_cred->p_refcnt = 1;
187 crhold(p1->p_ucred);
188
189 p2->p_fd = fdcopy(p1);
190 /*
191 * If p_limit is still copy-on-write, bump refcnt,
192 * otherwise get a copy that won't be modified.
193 * (If PL_SHAREMOD is clear, the structure is shared
194 * copy-on-write.)
195 */
196 if (p1->p_limit->p_lflags & PL_SHAREMOD)
197 p2->p_limit = limcopy(p1->p_limit);
198 else {
199 p2->p_limit = p1->p_limit;
200 p2->p_limit->p_refcnt++;
201 }
202
203 p2->p_flag = SLOAD | (p1->p_flag & SHPUX);
204 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & SCTTY)
205 p2->p_flag |= SCTTY;
206 if (isvfork)
207 p2->p_flag |= SPPWAIT;
208 p2->p_stat = SIDL;
209 p2->p_pid = nextpid;
210 {
211 struct proc **hash = &pidhash[PIDHASH(p2->p_pid)];
212
213 p2->p_hash = *hash;
214 *hash = p2;
215 }
216 p2->p_pgrpnxt = p1->p_pgrpnxt;
217 p1->p_pgrpnxt = p2;
218 p2->p_pptr = p1;
219 p2->p_osptr = p1->p_cptr;
220 if (p1->p_cptr)
221 p1->p_cptr->p_ysptr = p2;
222 p1->p_cptr = p2;
223#ifdef KTRACE
224 /*
225 * Copy traceflag and tracefile if enabled.
226 * If not inherited, these were zeroed above.
227 */
228 if (p1->p_traceflag&KTRFAC_INHERIT) {
229 p2->p_traceflag = p1->p_traceflag;
230 if ((p2->p_tracep = p1->p_tracep) != NULL)
231 VREF(p2->p_tracep);
232 }
233#endif
234
235#if defined(tahoe)
236 p2->p_vmspace->p_ckey = p1->p_vmspace->p_ckey; /* XXX move this */
237#endif
238
239 /*
240 * This begins the section where we must prevent the parent
241 * from being swapped.
242 */
243 p1->p_flag |= SKEEP;
244 /*
245 * Set return values for child before vm_fork,
246 * so they can be copied to child stack.
247 * We return parent pid, and mark as child in retval[1].
248 * NOTE: the kernel stack may be at a different location in the child
249 * process, and thus addresses of automatic variables (including retval)
250 * may be invalid after vm_fork returns in the child process.
251 */
252 retval[0] = p1->p_pid;
253 retval[1] = 1;
254 if (vm_fork(p1, p2, isvfork)) {
255 /*
256 * Child process. Set start time and get to work.
257 */
258 (void) splclock();
259 p2->p_stats->p_start = time;
260 (void) spl0();
261 p2->p_acflag = AFORK;
262 return (0);
263 }
264
265 /*
266 * Make child runnable and add to run queue.
267 */
268 (void) splhigh();
269 p2->p_stat = SRUN;
270 setrq(p2);
271 (void) spl0();
272
273 /*
274 * Now can be swapped.
275 */
276 p1->p_flag &= ~SKEEP;
277
278 /*
279 * Preserve synchronization semantics of vfork.
280 * If waiting for child to exec or exit, set SPPWAIT
281 * on child, and sleep on our proc (in case of exit).
282 */
283 if (isvfork)
284 while (p2->p_flag & SPPWAIT)
285 tsleep((caddr_t)p1, PWAIT, "ppwait", 0);
286
287 /*
288 * Return child pid to parent process,
289 * marking us as parent via retval[1].
290 */
291 retval[0] = p2->p_pid;
292 retval[1] = 0;
293 return (0);
294}