VROOT flag is set in union_allocvp
[unix-history] / usr / src / sys / kern / kern_fork.c
CommitLineData
da7c5cc6 1/*
436acd38
KB
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
adb35f79
KB
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.
da7c5cc6 9 *
dbf0c423 10 * %sccs.include.redist.c%
c4ec2128 11 *
449a479b 12 * @(#)kern_fork.c 8.6 (Berkeley) %G%
da7c5cc6 13 */
50108d5c 14
31c4861a
KM
15#include <sys/param.h>
16#include <sys/systm.h>
17#include <sys/map.h>
18#include <sys/filedesc.h>
19#include <sys/kernel.h>
20#include <sys/malloc.h>
21#include <sys/proc.h>
22#include <sys/resourcevar.h>
23#include <sys/vnode.h>
24#include <sys/file.h>
25#include <sys/acct.h>
26#include <sys/ktrace.h>
50108d5c 27
9e97623a
CT
28struct fork_args {
29 int dummy;
30};
c9714ae3
KM
31/* ARGSUSED */
32fork(p, uap, retval)
33 struct proc *p;
9e97623a 34 struct fork_args *uap;
c9714ae3 35 int retval[];
50108d5c
SL
36{
37
d9c2f47f 38 return (fork1(p, 0, retval));
50108d5c
SL
39}
40
c9714ae3
KM
41/* ARGSUSED */
42vfork(p, uap, retval)
43 struct proc *p;
9e97623a 44 struct fork_args *uap;
c9714ae3 45 int retval[];
50108d5c
SL
46{
47
d9c2f47f 48 return (fork1(p, 1, retval));
50108d5c
SL
49}
50
ae2e0122
MK
51int nprocs = 1; /* process 0 */
52
c9714ae3
KM
53fork1(p1, isvfork, retval)
54 register struct proc *p1;
55 int isvfork, retval[];
50108d5c 56{
c9714ae3 57 register struct proc *p2;
9c71e14c 58 register uid_t uid;
4bb226b7 59 struct proc *newproc;
141bed06 60 struct proc **hash;
9c71e14c 61 int count;
ae2e0122 62 static int nextpid, pidchecked = 0;
50108d5c 63
50108d5c 64 /*
141bed06
KM
65 * Although process entries are dynamically created, we still keep
66 * a global limit on the maximum number we will create. Don't allow
449a479b
KM
67 * a nonprivileged user to use the last process; don't let root
68 * exceed the limit. The variable nprocs is the current number of
69 * processes, maxproc is the limit.
50108d5c 70 */
9c71e14c 71 uid = p1->p_cred->p_ruid;
449a479b 72 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
50108d5c 73 tablefull("proc");
c9714ae3 74 return (EAGAIN);
50108d5c 75 }
9c71e14c
KM
76 /*
77 * Increment the count of procs running with this uid. Don't allow
78 * a nonprivileged user to exceed their current limit.
79 */
80 count = chgproccnt(uid, 1);
81 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
82 (void)chgproccnt(uid, -1);
ae2e0122 83 return (EAGAIN);
9c71e14c 84 }
50108d5c 85
141bed06 86 /* Allocate new proc. */
4bb226b7 87 MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK);
141bed06 88
50108d5c 89 /*
141bed06
KM
90 * Find an unused process ID. We remember a range of unused IDs
91 * ready to use (from nextpid+1 through pidchecked-1).
50108d5c 92 */
ae2e0122 93 nextpid++;
1d348849 94retry:
ae2e0122
MK
95 /*
96 * If the process ID prototype has wrapped around,
97 * restart somewhat above 0, as the low-numbered procs
98 * tend to include daemons that don't exit.
99 */
100 if (nextpid >= PID_MAX) {
101 nextpid = 100;
1d348849 102 pidchecked = 0;
50108d5c 103 }
ae2e0122 104 if (nextpid >= pidchecked) {
1d348849 105 int doingzomb = 0;
e9539440 106
27ed98f2 107 pidchecked = PID_MAX;
1d348849 108 /*
ae2e0122 109 * Scan the active and zombie procs to check whether this pid
1d348849 110 * is in use. Remember the lowest pid that's greater
ae2e0122 111 * than nextpid, so we can avoid checking for a while.
1d348849 112 */
80a8e5e6 113 p2 = (struct proc *)allproc;
1d348849 114again:
cf5ef508 115 for (; p2 != NULL; p2 = p2->p_next) {
141bed06 116 while (p2->p_pid == nextpid ||
ae2e0122
MK
117 p2->p_pgrp->pg_id == nextpid) {
118 nextpid++;
119 if (nextpid >= pidchecked)
1d348849
MK
120 goto retry;
121 }
ae2e0122
MK
122 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
123 pidchecked = p2->p_pid;
124 if (p2->p_pgrp->pg_id > nextpid &&
125 pidchecked > p2->p_pgrp->pg_id)
126 pidchecked = p2->p_pgrp->pg_id;
1d348849
MK
127 }
128 if (!doingzomb) {
129 doingzomb = 1;
ae2e0122 130 p2 = zombproc;
1d348849
MK
131 goto again;
132 }
50108d5c 133 }
50108d5c 134
ae2e0122 135
773b35ab
KM
136 /*
137 * Link onto allproc (this should probably be delayed).
138 * Heavy use of volatile here to prevent the compiler from
139 * rearranging code. Yes, it *is* terribly ugly, but at least
140 * it works.
141 */
ae2e0122 142 nprocs++;
4bb226b7 143 p2 = newproc;
773b35ab
KM
144#define Vp2 ((volatile struct proc *)p2)
145 Vp2->p_stat = SIDL; /* protect against others */
146 Vp2->p_pid = nextpid;
147 /*
148 * This is really:
cf5ef508
KB
149 * p2->p_next = allproc;
150 * allproc->p_prev = &p2->p_next;
773b35ab
KM
151 * p2->p_prev = &allproc;
152 * allproc = p2;
153 * The assignment via allproc is legal since it is never NULL.
154 */
cf5ef508 155 *(volatile struct proc **)&Vp2->p_next = allproc;
773b35ab 156 *(volatile struct proc ***)&allproc->p_prev =
cf5ef508 157 (volatile struct proc **)&Vp2->p_next;
773b35ab
KM
158 *(volatile struct proc ***)&Vp2->p_prev = &allproc;
159 allproc = Vp2;
160#undef Vp2
cf5ef508 161 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */
1d348849 162
141bed06
KM
163 /* Insert on the hash chain. */
164 hash = &pidhash[PIDHASH(p2->p_pid)];
165 p2->p_hash = *hash;
166 *hash = p2;
167
50108d5c
SL
168 /*
169 * Make a proc table entry for the new process.
ae2e0122
MK
170 * Start by zeroing the section of proc that is zero-initialized,
171 * then copy the section that is copied directly from the parent.
50108d5c 172 */
ae2e0122
MK
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.
6795ddce 181 * The p_stats and p_sigacts substructs are set in vm_fork.
ae2e0122 182 */
cf5ef508
KB
183 p2->p_flag = P_INMEM;
184 if (p1->p_flag & P_PROFIL)
44d9bc96 185 startprofclock(p2);
ae2e0122
MK
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));
53e7449e 189 p2->p_cred->p_refcnt = 1;
ae2e0122
MK
190 crhold(p1->p_ucred);
191
859437ef
JSP
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
ae2e0122 197 p2->p_fd = fdcopy(p1);
ae2e0122
MK
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++;
98111078 209 }
ae2e0122 210
cf5ef508
KB
211 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
212 p2->p_flag |= P_CONTROLT;
ae2e0122 213 if (isvfork)
cf5ef508 214 p2->p_flag |= P_PPWAIT;
ae2e0122
MK
215 p2->p_pgrpnxt = p1->p_pgrpnxt;
216 p1->p_pgrpnxt = p2;
217 p2->p_pptr = p1;
218 p2->p_osptr = p1->p_cptr;
219 if (p1->p_cptr)
220 p1->p_cptr->p_ysptr = p2;
221 p1->p_cptr = p2;
222#ifdef KTRACE
50108d5c 223 /*
ae2e0122
MK
224 * Copy traceflag and tracefile if enabled.
225 * If not inherited, these were zeroed above.
50108d5c 226 */
ae2e0122
MK
227 if (p1->p_traceflag&KTRFAC_INHERIT) {
228 p2->p_traceflag = p1->p_traceflag;
229 if ((p2->p_tracep = p1->p_tracep) != NULL)
230 VREF(p2->p_tracep);
231 }
232#endif
233
50108d5c 234 /*
50108d5c
SL
235 * This begins the section where we must prevent the parent
236 * from being swapped.
237 */
cf5ef508 238 p1->p_flag |= P_NOSWAP;
6795ddce
MK
239 /*
240 * Set return values for child before vm_fork,
241 * so they can be copied to child stack.
242 * We return parent pid, and mark as child in retval[1].
f6fca708
MK
243 * NOTE: the kernel stack may be at a different location in the child
244 * process, and thus addresses of automatic variables (including retval)
245 * may be invalid after vm_fork returns in the child process.
6795ddce
MK
246 */
247 retval[0] = p1->p_pid;
248 retval[1] = 1;
ae2e0122
MK
249 if (vm_fork(p1, p2, isvfork)) {
250 /*
6795ddce 251 * Child process. Set start time and get to work.
ae2e0122 252 */
d848aba6 253 (void) splclock();
ae2e0122 254 p2->p_stats->p_start = time;
d848aba6 255 (void) spl0();
ae2e0122
MK
256 p2->p_acflag = AFORK;
257 return (0);
d848aba6 258 }
50108d5c
SL
259
260 /*
261 * Make child runnable and add to run queue.
262 */
ae2e0122
MK
263 (void) splhigh();
264 p2->p_stat = SRUN;
cb84e0ab 265 setrunqueue(p2);
50108d5c
SL
266 (void) spl0();
267
50108d5c
SL
268 /*
269 * Now can be swapped.
270 */
cf5ef508 271 p1->p_flag &= ~P_NOSWAP;
50108d5c
SL
272
273 /*
cf5ef508
KB
274 * Preserve synchronization semantics of vfork. If waiting for
275 * child to exec or exit, set P_PPWAIT on child, and sleep on our
276 * proc (in case of exit).
50108d5c 277 */
ae2e0122 278 if (isvfork)
cf5ef508
KB
279 while (p2->p_flag & P_PPWAIT)
280 tsleep(p1, PWAIT, "ppwait", 0);
50108d5c
SL
281
282 /*
6795ddce
MK
283 * Return child pid to parent process,
284 * marking us as parent via retval[1].
50108d5c 285 */
ae2e0122 286 retval[0] = p2->p_pid;
6795ddce 287 retval[1] = 0;
50108d5c
SL
288 return (0);
289}