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