kern_fork.c revision 1.1.1.3 1 /*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/map.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/vnode.h>
50 #include <sys/file.h>
51 #include <sys/acct.h>
52 #include <sys/ktrace.h>
53
54 /* ARGSUSED */
55 fork(p, uap, retval)
56 struct proc *p;
57 void *uap;
58 register_t *retval;
59 {
60
61 return (fork1(p, 0, retval));
62 }
63
64 /* ARGSUSED */
65 vfork(p, uap, retval)
66 struct proc *p;
67 void *uap;
68 register_t *retval;
69 {
70
71 return (fork1(p, 1, retval));
72 }
73
74 int nprocs = 1; /* process 0 */
75
76 fork1(p1, isvfork, retval)
77 register struct proc *p1;
78 int isvfork;
79 register_t *retval;
80 {
81 register struct proc *p2;
82 register uid_t uid;
83 struct proc *newproc;
84 struct proc **hash;
85 int count;
86 static int nextpid, pidchecked = 0;
87
88 /*
89 * Although process entries are dynamically created, we still keep
90 * a global limit on the maximum number we will create. Don't allow
91 * a nonprivileged user to use the last process; don't let root
92 * exceed the limit. The variable nprocs is the current number of
93 * processes, maxproc is the limit.
94 */
95 uid = p1->p_cred->p_ruid;
96 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
97 tablefull("proc");
98 return (EAGAIN);
99 }
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 = allproc.lh_first;
139 again:
140 for (; p2 != 0; p2 = p2->p_list.le_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.lh_first;
156 goto again;
157 }
158 }
159
160 nprocs++;
161 p2 = newproc;
162 p2->p_stat = SIDL; /* protect against others */
163 p2->p_pid = nextpid;
164 LIST_INSERT_HEAD(&allproc, p2, p_list);
165 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */
166 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
167
168 /*
169 * Make a proc table entry for the new process.
170 * Start by zeroing the section of proc that is zero-initialized,
171 * then copy the section that is copied directly from the parent.
172 */
173 bzero(&p2->p_startzero,
174 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
175 bcopy(&p1->p_startcopy, &p2->p_startcopy,
176 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
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 p2->p_flag = P_INMEM;
184 if (p1->p_flag & P_PROFIL)
185 startprofclock(p2);
186 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
187 M_SUBPROC, M_WAITOK);
188 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
189 p2->p_cred->p_refcnt = 1;
190 crhold(p1->p_ucred);
191
192 /* bump references to the text vnode (for procfs) */
193 p2->p_textvp = p1->p_textvp;
194 if (p2->p_textvp)
195 VREF(p2->p_textvp);
196
197 p2->p_fd = fdcopy(p1);
198 /*
199 * If p_limit is still copy-on-write, bump refcnt,
200 * otherwise get a copy that won't be modified.
201 * (If PL_SHAREMOD is clear, the structure is shared
202 * copy-on-write.)
203 */
204 if (p1->p_limit->p_lflags & PL_SHAREMOD)
205 p2->p_limit = limcopy(p1->p_limit);
206 else {
207 p2->p_limit = p1->p_limit;
208 p2->p_limit->p_refcnt++;
209 }
210
211 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
212 p2->p_flag |= P_CONTROLT;
213 if (isvfork)
214 p2->p_flag |= P_PPWAIT;
215 LIST_INSERT_AFTER(p1, p2, p_pglist);
216 p2->p_pptr = p1;
217 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
218 LIST_INIT(&p2->p_children);
219
220 #ifdef KTRACE
221 /*
222 * Copy traceflag and tracefile if enabled.
223 * If not inherited, these were zeroed above.
224 */
225 if (p1->p_traceflag&KTRFAC_INHERIT) {
226 p2->p_traceflag = p1->p_traceflag;
227 if ((p2->p_tracep = p1->p_tracep) != NULL)
228 VREF(p2->p_tracep);
229 }
230 #endif
231
232 /*
233 * This begins the section where we must prevent the parent
234 * from being swapped.
235 */
236 p1->p_flag |= P_NOSWAP;
237 /*
238 * Set return values for child before vm_fork,
239 * so they can be copied to child stack.
240 * We return parent pid, and mark as child in retval[1].
241 * NOTE: the kernel stack may be at a different location in the child
242 * process, and thus addresses of automatic variables (including retval)
243 * may be invalid after vm_fork returns in the child process.
244 */
245 retval[0] = p1->p_pid;
246 retval[1] = 1;
247 if (vm_fork(p1, p2, isvfork)) {
248 /*
249 * Child process. Set start time and get to work.
250 */
251 (void) splclock();
252 p2->p_stats->p_start = time;
253 (void) spl0();
254 p2->p_acflag = AFORK;
255 return (0);
256 }
257
258 /*
259 * Make child runnable and add to run queue.
260 */
261 (void) splhigh();
262 p2->p_stat = SRUN;
263 setrunqueue(p2);
264 (void) spl0();
265
266 /*
267 * Now can be swapped.
268 */
269 p1->p_flag &= ~P_NOSWAP;
270
271 /*
272 * Preserve synchronization semantics of vfork. If waiting for
273 * child to exec or exit, set P_PPWAIT on child, and sleep on our
274 * proc (in case of exit).
275 */
276 if (isvfork)
277 while (p2->p_flag & P_PPWAIT)
278 tsleep(p1, PWAIT, "ppwait", 0);
279
280 /*
281 * Return child pid to parent process,
282 * marking us as parent via retval[1].
283 */
284 retval[0] = p2->p_pid;
285 retval[1] = 0;
286 return (0);
287 }
288