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