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