1 /* $OpenBSD: kern_fork.c,v 1.59 2002/10/31 01:33:27 art Exp $ */ 2 /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.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/signalvar.h> 53 #include <sys/vnode.h> 54 #include <sys/file.h> 55 #include <sys/acct.h> 56 #include <sys/ktrace.h> 57 #include <sys/sched.h> 58 #include <dev/rndvar.h> 59 #include <sys/pool.h> 60 #include <sys/mman.h> 61 62 #include <sys/syscallargs.h> 63 64 #include "systrace.h" 65 #include <dev/systrace.h> 66 67 #include <uvm/uvm_extern.h> 68 #include <uvm/uvm_map.h> 69 70 int nprocs = 1; /* process 0 */ 71 int randompid; /* when set to 1, pid's go random */ 72 pid_t lastpid; 73 struct forkstat forkstat; 74 75 int pidtaken(pid_t); 76 77 /*ARGSUSED*/ 78 int 79 sys_fork(struct proc *p, void *v, register_t *retval) 80 { 81 return (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, NULL, NULL, retval)); 82 } 83 84 /*ARGSUSED*/ 85 int 86 sys_vfork(struct proc *p, void *v, register_t *retval) 87 { 88 return (fork1(p, SIGCHLD, FORK_VFORK|FORK_PPWAIT, NULL, 0, NULL, 89 NULL, retval)); 90 } 91 92 int 93 sys_rfork(struct proc *p, void *v, register_t *retval) 94 { 95 struct sys_rfork_args /* { 96 syscallarg(int) flags; 97 } */ *uap = v; 98 99 int rforkflags; 100 int flags; 101 102 flags = FORK_RFORK; 103 rforkflags = SCARG(uap, flags); 104 105 if ((rforkflags & RFPROC) == 0) 106 return (EINVAL); 107 108 switch(rforkflags & (RFFDG|RFCFDG)) { 109 case (RFFDG|RFCFDG): 110 return EINVAL; 111 case RFCFDG: 112 flags |= FORK_CLEANFILES; 113 break; 114 case RFFDG: 115 break; 116 default: 117 flags |= FORK_SHAREFILES; 118 break; 119 } 120 121 if (rforkflags & RFNOWAIT) 122 flags |= FORK_NOZOMBIE; 123 124 if (rforkflags & RFMEM) 125 flags |= FORK_VMNOSTACK; 126 127 return (fork1(p, SIGCHLD, flags, NULL, 0, NULL, NULL, retval)); 128 } 129 130 int 131 fork1(struct proc *p1, int exitsig, int flags, void *stack, size_t stacksize, 132 void (*func)(void *), void *arg, register_t *retval) 133 { 134 struct proc *p2; 135 uid_t uid; 136 struct vmspace *vm; 137 int count; 138 vaddr_t uaddr; 139 int s; 140 extern void endtsleep(void *); 141 extern void realitexpire(void *); 142 143 /* 144 * Although process entries are dynamically created, we still keep 145 * a global limit on the maximum number we will create. We reserve 146 * the last 5 processes to root. The variable nprocs is the current 147 * number of processes, maxproc is the limit. 148 */ 149 uid = p1->p_cred->p_ruid; 150 if ((nprocs >= maxproc - 5 && uid != 0) || nprocs >= maxproc) { 151 tablefull("proc"); 152 return (EAGAIN); 153 } 154 nprocs++; 155 156 /* 157 * Increment the count of procs running with this uid. Don't allow 158 * a nonprivileged user to exceed their current limit. 159 */ 160 count = chgproccnt(uid, 1); 161 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { 162 (void)chgproccnt(uid, -1); 163 nprocs--; 164 return (EAGAIN); 165 } 166 167 /* 168 * Allocate a pcb and kernel stack for the process 169 */ 170 uaddr = uvm_km_valloc(kernel_map, USPACE); 171 if (uaddr == 0) { 172 chgproccnt(uid, -1); 173 nprocs--; 174 return (ENOMEM); 175 } 176 177 /* 178 * From now on, we're comitted to the fork and cannot fail. 179 */ 180 181 /* Allocate new proc. */ 182 p2 = pool_get(&proc_pool, PR_WAITOK); 183 184 p2->p_stat = SIDL; /* protect against others */ 185 p2->p_exitsig = exitsig; 186 p2->p_forw = p2->p_back = NULL; 187 188 /* 189 * Make a proc table entry for the new process. 190 * Start by zeroing the section of proc that is zero-initialized, 191 * then copy the section that is copied directly from the parent. 192 */ 193 bzero(&p2->p_startzero, 194 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 195 bcopy(&p1->p_startcopy, &p2->p_startcopy, 196 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 197 198 /* 199 * Initialize the timeouts. 200 */ 201 timeout_set(&p2->p_sleep_to, endtsleep, p2); 202 timeout_set(&p2->p_realit_to, realitexpire, p2); 203 204 /* 205 * Duplicate sub-structures as needed. 206 * Increase reference counts on shared objects. 207 * The p_stats and p_sigacts substructs are set in vm_fork. 208 */ 209 p2->p_flag = P_INMEM; 210 p2->p_emul = p1->p_emul; 211 if (p1->p_flag & P_PROFIL) 212 startprofclock(p2); 213 p2->p_flag |= (p1->p_flag & (P_SUGID | P_SUGIDEXEC)); 214 p2->p_cred = pool_get(&pcred_pool, PR_WAITOK); 215 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); 216 p2->p_cred->p_refcnt = 1; 217 crhold(p1->p_ucred); 218 219 /* bump references to the text vnode (for procfs) */ 220 p2->p_textvp = p1->p_textvp; 221 if (p2->p_textvp) 222 VREF(p2->p_textvp); 223 224 if (flags & FORK_CLEANFILES) 225 p2->p_fd = fdinit(p1); 226 else if (flags & FORK_SHAREFILES) 227 p2->p_fd = fdshare(p1); 228 else 229 p2->p_fd = fdcopy(p1); 230 231 /* 232 * If p_limit is still copy-on-write, bump refcnt, 233 * otherwise get a copy that won't be modified. 234 * (If PL_SHAREMOD is clear, the structure is shared 235 * copy-on-write.) 236 */ 237 if (p1->p_limit->p_lflags & PL_SHAREMOD) 238 p2->p_limit = limcopy(p1->p_limit); 239 else { 240 p2->p_limit = p1->p_limit; 241 p2->p_limit->p_refcnt++; 242 } 243 244 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 245 p2->p_flag |= P_CONTROLT; 246 if (flags & FORK_PPWAIT) 247 p2->p_flag |= P_PPWAIT; 248 LIST_INSERT_AFTER(p1, p2, p_pglist); 249 p2->p_pptr = p1; 250 if (flags & FORK_NOZOMBIE) 251 p2->p_flag |= P_NOZOMBIE; 252 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling); 253 LIST_INIT(&p2->p_children); 254 255 #ifdef KTRACE 256 /* 257 * Copy traceflag and tracefile if enabled. 258 * If not inherited, these were zeroed above. 259 */ 260 if (p1->p_traceflag & KTRFAC_INHERIT) { 261 p2->p_traceflag = p1->p_traceflag; 262 if ((p2->p_tracep = p1->p_tracep) != NULL) 263 VREF(p2->p_tracep); 264 } 265 #endif 266 267 /* 268 * set priority of child to be that of parent 269 * XXX should move p_estcpu into the region of struct proc which gets 270 * copied. 271 */ 272 scheduler_fork_hook(p1, p2); 273 274 /* 275 * Create signal actions for the child process. 276 */ 277 if (flags & FORK_SIGHAND) 278 sigactsshare(p1, p2); 279 else 280 p2->p_sigacts = sigactsinit(p1); 281 282 /* 283 * This begins the section where we must prevent the parent 284 * from being swapped. 285 */ 286 PHOLD(p1); 287 288 if (flags & FORK_VMNOSTACK) { 289 /* share everything, but ... */ 290 uvm_map_inherit(&p1->p_vmspace->vm_map, 291 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS, 292 MAP_INHERIT_SHARE); 293 /* ... don't share stack */ 294 #ifdef MACHINE_STACK_GROWS_UP 295 uvm_map_inherit(&p1->p_vmspace->vm_map, 296 USRSTACK, USRSTACK + MAXSSIZ, 297 MAP_INHERIT_COPY); 298 #else 299 uvm_map_inherit(&p1->p_vmspace->vm_map, 300 USRSTACK - MAXSSIZ, USRSTACK, 301 MAP_INHERIT_COPY); 302 #endif 303 } 304 305 p2->p_addr = (struct user *)uaddr; 306 307 /* 308 * Finish creating the child process. It will return through a 309 * different path later. 310 */ 311 uvm_fork(p1, p2, ((flags & FORK_SHAREVM) ? TRUE : FALSE), stack, 312 stacksize, func ? func : child_return, arg ? arg : p2); 313 314 vm = p2->p_vmspace; 315 316 if (flags & FORK_FORK) { 317 forkstat.cntfork++; 318 forkstat.sizfork += vm->vm_dsize + vm->vm_ssize; 319 } else if (flags & FORK_VFORK) { 320 forkstat.cntvfork++; 321 forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize; 322 } else if (flags & FORK_RFORK) { 323 forkstat.cntrfork++; 324 forkstat.sizrfork += vm->vm_dsize + vm->vm_ssize; 325 } else { 326 forkstat.cntkthread++; 327 forkstat.sizkthread += vm->vm_dsize + vm->vm_ssize; 328 } 329 330 /* Find an unused pid satisfying 1 <= lastpid <= PID_MAX */ 331 do { 332 lastpid = 1 + (randompid ? arc4random() : lastpid) % PID_MAX; 333 } while (pidtaken(lastpid)); 334 p2->p_pid = lastpid; 335 336 LIST_INSERT_HEAD(&allproc, p2, p_list); 337 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 338 339 #if NSYSTRACE > 0 340 if (ISSET(p1->p_flag, P_SYSTRACE)) 341 systrace_fork(p1, p2); 342 #endif 343 344 /* 345 * Make child runnable, set start time, and add to run queue. 346 */ 347 s = splstatclock(); 348 p2->p_stats->p_start = time; 349 p2->p_acflag = AFORK; 350 p2->p_stat = SRUN; 351 setrunqueue(p2); 352 splx(s); 353 354 /* 355 * Now can be swapped. 356 */ 357 PRELE(p1); 358 359 uvmexp.forks++; 360 if (flags & FORK_PPWAIT) 361 uvmexp.forks_ppwait++; 362 if (flags & FORK_SHAREVM) 363 uvmexp.forks_sharevm++; 364 365 /* 366 * tell any interested parties about the new process 367 */ 368 KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); 369 370 /* 371 * Preserve synchronization semantics of vfork. If waiting for 372 * child to exec or exit, set P_PPWAIT on child, and sleep on our 373 * proc (in case of exit). 374 */ 375 if (flags & FORK_PPWAIT) 376 while (p2->p_flag & P_PPWAIT) 377 tsleep(p1, PWAIT, "ppwait", 0); 378 379 /* 380 * Return child pid to parent process, 381 * marking us as parent via retval[1]. 382 */ 383 retval[0] = p2->p_pid; 384 retval[1] = 0; 385 return (0); 386 } 387 388 /* 389 * Checks for current use of a pid, either as a pid or pgid. 390 */ 391 int 392 pidtaken(pid_t pid) 393 { 394 struct proc *p; 395 396 if (pfind(pid) != NULL) 397 return (1); 398 if (pgfind(pid) != NULL) 399 return (1); 400 LIST_FOREACH(p, &zombproc, p_list) 401 if (p->p_pid == pid || p->p_pgid == pid) 402 return (1); 403 return (0); 404 } 405