1 /* $OpenBSD: kern_fork.c,v 1.126 2011/06/06 17:05:46 deraadt 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. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/filedesc.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/mount.h> 46 #include <sys/proc.h> 47 #include <sys/exec.h> 48 #include <sys/resourcevar.h> 49 #include <sys/signalvar.h> 50 #include <sys/vnode.h> 51 #include <sys/file.h> 52 #include <sys/acct.h> 53 #include <sys/ktrace.h> 54 #include <sys/sched.h> 55 #include <dev/rndvar.h> 56 #include <sys/pool.h> 57 #include <sys/mman.h> 58 #include <sys/ptrace.h> 59 60 #include <sys/syscallargs.h> 61 62 #include "systrace.h" 63 #include <dev/systrace.h> 64 65 #include <uvm/uvm_extern.h> 66 #include <uvm/uvm_map.h> 67 68 int nprocs = 1; /* process 0 */ 69 int randompid; /* when set to 1, pid's go random */ 70 pid_t lastpid; 71 struct forkstat forkstat; 72 73 void fork_return(void *); 74 int pidtaken(pid_t); 75 76 void process_new(struct proc *, struct proc *); 77 78 void 79 fork_return(void *arg) 80 { 81 struct proc *p = (struct proc *)arg; 82 83 if (p->p_flag & P_TRACED) 84 psignal(p, SIGTRAP); 85 86 child_return(p); 87 } 88 89 /*ARGSUSED*/ 90 int 91 sys_fork(struct proc *p, void *v, register_t *retval) 92 { 93 int flags; 94 95 flags = FORK_FORK; 96 if (p->p_ptmask & PTRACE_FORK) 97 flags |= FORK_PTRACE; 98 return (fork1(p, SIGCHLD, flags, NULL, 0, 99 fork_return, NULL, retval, NULL)); 100 } 101 102 /*ARGSUSED*/ 103 int 104 sys_vfork(struct proc *p, void *v, register_t *retval) 105 { 106 return (fork1(p, SIGCHLD, FORK_VFORK|FORK_PPWAIT, NULL, 0, NULL, 107 NULL, retval, NULL)); 108 } 109 110 int 111 sys_rfork(struct proc *p, void *v, register_t *retval) 112 { 113 struct sys_rfork_args /* { 114 syscallarg(int) flags; 115 } */ *uap = v; 116 117 int rforkflags; 118 int flags; 119 120 flags = FORK_RFORK; 121 rforkflags = SCARG(uap, flags); 122 123 if ((rforkflags & RFPROC) == 0) 124 return (EINVAL); 125 126 switch(rforkflags & (RFFDG|RFCFDG)) { 127 case (RFFDG|RFCFDG): 128 return EINVAL; 129 case RFCFDG: 130 flags |= FORK_CLEANFILES; 131 break; 132 case RFFDG: 133 break; 134 default: 135 flags |= FORK_SHAREFILES; 136 break; 137 } 138 139 if (rforkflags & RFNOWAIT) 140 flags |= FORK_NOZOMBIE; 141 142 if (rforkflags & RFMEM) 143 flags |= FORK_SHAREVM; 144 145 if (rforkflags & RFTHREAD) 146 flags |= FORK_THREAD | FORK_SIGHAND | FORK_NOZOMBIE; 147 148 return (fork1(p, SIGCHLD, flags, NULL, 0, NULL, NULL, retval, NULL)); 149 } 150 151 /* 152 * Allocate and initialize a new process. 153 */ 154 void 155 process_new(struct proc *newproc, struct proc *parentproc) 156 { 157 struct process *pr, *parent; 158 159 pr = pool_get(&process_pool, PR_WAITOK); 160 pr->ps_mainproc = newproc; 161 parent = parentproc->p_p; 162 163 TAILQ_INIT(&pr->ps_threads); 164 TAILQ_INSERT_TAIL(&pr->ps_threads, newproc, p_thr_link); 165 LIST_INSERT_AFTER(parent, pr, ps_pglist); 166 pr->ps_pptr = parent; 167 LIST_INSERT_HEAD(&parent->ps_children, pr, ps_sibling); 168 LIST_INIT(&pr->ps_children); 169 pr->ps_refcnt = 1; 170 171 /* 172 * Make a process structure for the new process. 173 * Start by zeroing the section of proc that is zero-initialized, 174 * then copy the section that is copied directly from the parent. 175 */ 176 bzero(&pr->ps_startzero, 177 (unsigned) ((caddr_t)&pr->ps_endzero - (caddr_t)&pr->ps_startzero)); 178 bcopy(&parent->ps_startcopy, &pr->ps_startcopy, 179 (unsigned) ((caddr_t)&pr->ps_endcopy - (caddr_t)&pr->ps_startcopy)); 180 181 /* post-copy fixups */ 182 pr->ps_cred = pool_get(&pcred_pool, PR_WAITOK); 183 bcopy(parent->ps_cred, pr->ps_cred, sizeof(*pr->ps_cred)); 184 crhold(parent->ps_cred->pc_ucred); 185 pr->ps_limit->p_refcnt++; 186 187 pr->ps_flags = parent->ps_flags & (PS_SUGID | PS_SUGIDEXEC); 188 if (parent->ps_session->s_ttyvp != NULL && 189 parent->ps_flags & PS_CONTROLT) 190 atomic_setbits_int(&pr->ps_flags, PS_CONTROLT); 191 192 newproc->p_p = pr; 193 } 194 195 /* print the 'table full' message once per 10 seconds */ 196 struct timeval fork_tfmrate = { 10, 0 }; 197 198 int 199 fork1(struct proc *p1, int exitsig, int flags, void *stack, size_t stacksize, 200 void (*func)(void *), void *arg, register_t *retval, 201 struct proc **rnewprocp) 202 { 203 struct proc *p2; 204 uid_t uid; 205 struct vmspace *vm; 206 int count; 207 vaddr_t uaddr; 208 int s; 209 extern void endtsleep(void *); 210 extern void realitexpire(void *); 211 struct ptrace_state *newptstat = NULL; 212 #if NSYSTRACE > 0 213 void *newstrp = NULL; 214 #endif 215 216 /* sanity check some flag combinations */ 217 if (flags & FORK_THREAD) { 218 if (!rthreads_enabled) 219 return (ENOTSUP); 220 if ((flags & (FORK_SIGHAND | FORK_NOZOMBIE)) != 221 (FORK_SIGHAND | FORK_NOZOMBIE)) 222 return (EINVAL); 223 } 224 if (flags & FORK_SIGHAND && (flags & FORK_SHAREVM) == 0) 225 return (EINVAL); 226 227 /* 228 * Although process entries are dynamically created, we still keep 229 * a global limit on the maximum number we will create. We reserve 230 * the last 5 processes to root. The variable nprocs is the current 231 * number of processes, maxproc is the limit. 232 */ 233 uid = p1->p_cred->p_ruid; 234 if ((nprocs >= maxproc - 5 && uid != 0) || nprocs >= maxproc) { 235 static struct timeval lasttfm; 236 237 if (ratecheck(&lasttfm, &fork_tfmrate)) 238 tablefull("proc"); 239 return (EAGAIN); 240 } 241 nprocs++; 242 243 /* 244 * Increment the count of procs running with this uid. Don't allow 245 * a nonprivileged user to exceed their current limit. 246 */ 247 count = chgproccnt(uid, 1); 248 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { 249 (void)chgproccnt(uid, -1); 250 nprocs--; 251 return (EAGAIN); 252 } 253 254 uaddr = uvm_km_kmemalloc_pla(kernel_map, uvm.kernel_object, USPACE, 255 USPACE_ALIGN, UVM_KMF_ZERO, 256 no_constraint.ucr_low, no_constraint.ucr_high, 257 0, 0, USPACE/PAGE_SIZE); 258 if (uaddr == 0) { 259 chgproccnt(uid, -1); 260 nprocs--; 261 return (ENOMEM); 262 } 263 264 /* 265 * From now on, we're committed to the fork and cannot fail. 266 */ 267 268 /* Allocate new proc. */ 269 p2 = pool_get(&proc_pool, PR_WAITOK); 270 271 p2->p_stat = SIDL; /* protect against others */ 272 p2->p_exitsig = exitsig; 273 p2->p_flag = 0; 274 275 if (flags & FORK_THREAD) { 276 atomic_setbits_int(&p2->p_flag, P_THREAD); 277 p2->p_p = p1->p_p; 278 TAILQ_INSERT_TAIL(&p2->p_p->ps_threads, p2, p_thr_link); 279 p2->p_p->ps_refcnt++; 280 } else { 281 process_new(p2, p1); 282 } 283 284 /* 285 * Make a proc table entry for the new process. 286 * Start by zeroing the section of proc that is zero-initialized, 287 * then copy the section that is copied directly from the parent. 288 */ 289 bzero(&p2->p_startzero, 290 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 291 bcopy(&p1->p_startcopy, &p2->p_startcopy, 292 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 293 294 /* 295 * Initialize the timeouts. 296 */ 297 timeout_set(&p2->p_sleep_to, endtsleep, p2); 298 timeout_set(&p2->p_realit_to, realitexpire, p2); 299 300 /* 301 * Duplicate sub-structures as needed. 302 * Increase reference counts on shared objects. 303 * The p_stats and p_sigacts substructs are set in vm_fork. 304 */ 305 if (p1->p_flag & P_PROFIL) 306 startprofclock(p2); 307 if (flags & FORK_PTRACE) 308 atomic_setbits_int(&p2->p_flag, p1->p_flag & P_TRACED); 309 310 /* bump references to the text vnode (for procfs) */ 311 p2->p_textvp = p1->p_textvp; 312 if (p2->p_textvp) 313 vref(p2->p_textvp); 314 315 if (flags & FORK_CLEANFILES) 316 p2->p_fd = fdinit(p1); 317 else if (flags & FORK_SHAREFILES) 318 p2->p_fd = fdshare(p1); 319 else 320 p2->p_fd = fdcopy(p1); 321 322 if (flags & FORK_PPWAIT) { 323 atomic_setbits_int(&p2->p_p->ps_flags, PS_PPWAIT); 324 atomic_setbits_int(&p1->p_p->ps_flags, PS_ISPWAIT); 325 } 326 if (flags & FORK_NOZOMBIE) 327 atomic_setbits_int(&p2->p_flag, P_NOZOMBIE); 328 329 #ifdef KTRACE 330 /* 331 * Copy traceflag and tracefile if enabled. 332 * If not inherited, these were zeroed above. 333 */ 334 if (p1->p_traceflag & KTRFAC_INHERIT) { 335 p2->p_traceflag = p1->p_traceflag; 336 if ((p2->p_tracep = p1->p_tracep) != NULL) 337 vref(p2->p_tracep); 338 } 339 #endif 340 341 /* 342 * set priority of child to be that of parent 343 * XXX should move p_estcpu into the region of struct proc which gets 344 * copied. 345 */ 346 scheduler_fork_hook(p1, p2); 347 348 /* 349 * Create signal actions for the child process. 350 */ 351 if (flags & FORK_SIGHAND) 352 sigactsshare(p1, p2); 353 else 354 p2->p_sigacts = sigactsinit(p1); 355 356 /* 357 * If emulation has process fork hook, call it now. 358 */ 359 if (p2->p_emul->e_proc_fork) 360 (*p2->p_emul->e_proc_fork)(p2, p1); 361 362 p2->p_addr = (struct user *)uaddr; 363 364 /* 365 * Finish creating the child process. It will return through a 366 * different path later. 367 */ 368 uvm_fork(p1, p2, ((flags & FORK_SHAREVM) ? TRUE : FALSE), stack, 369 stacksize, func ? func : child_return, arg ? arg : p2); 370 371 timeout_set(&p2->p_stats->p_virt_to, virttimer_trampoline, p2); 372 timeout_set(&p2->p_stats->p_prof_to, proftimer_trampoline, p2); 373 374 vm = p2->p_vmspace; 375 376 if (flags & FORK_FORK) { 377 forkstat.cntfork++; 378 forkstat.sizfork += vm->vm_dsize + vm->vm_ssize; 379 } else if (flags & FORK_VFORK) { 380 forkstat.cntvfork++; 381 forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize; 382 } else if (flags & FORK_RFORK) { 383 forkstat.cntrfork++; 384 forkstat.sizrfork += vm->vm_dsize + vm->vm_ssize; 385 } else { 386 forkstat.cntkthread++; 387 forkstat.sizkthread += vm->vm_dsize + vm->vm_ssize; 388 } 389 390 if (p2->p_flag & P_TRACED && flags & FORK_FORK) 391 newptstat = malloc(sizeof(*newptstat), M_SUBPROC, M_WAITOK); 392 #if NSYSTRACE > 0 393 if (ISSET(p1->p_flag, P_SYSTRACE)) 394 newstrp = systrace_getproc(); 395 #endif 396 397 /* Find an unused pid satisfying 1 <= lastpid <= PID_MAX */ 398 do { 399 lastpid = 1 + (randompid ? arc4random() : lastpid) % PID_MAX; 400 } while (pidtaken(lastpid)); 401 p2->p_pid = lastpid; 402 403 LIST_INSERT_HEAD(&allproc, p2, p_list); 404 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 405 406 if (p2->p_flag & P_TRACED) { 407 p2->p_oppid = p1->p_pid; 408 if ((flags & FORK_THREAD) == 0 && 409 p2->p_p->ps_pptr != p1->p_p->ps_pptr) 410 proc_reparent(p2->p_p, p1->p_p->ps_pptr); 411 412 /* 413 * Set ptrace status. 414 */ 415 if (flags & FORK_FORK) { 416 p2->p_ptstat = newptstat; 417 newptstat = NULL; 418 p1->p_ptstat->pe_report_event = PTRACE_FORK; 419 p2->p_ptstat->pe_report_event = PTRACE_FORK; 420 p1->p_ptstat->pe_other_pid = p2->p_pid; 421 p2->p_ptstat->pe_other_pid = p1->p_pid; 422 } 423 } 424 425 #if NSYSTRACE > 0 426 if (newstrp) 427 systrace_fork(p1, p2, newstrp); 428 #endif 429 430 /* 431 * Make child runnable, set start time, and add to run queue. 432 */ 433 SCHED_LOCK(s); 434 getmicrotime(&p2->p_stats->p_start); 435 p2->p_acflag = AFORK; 436 p2->p_stat = SRUN; 437 p2->p_cpu = sched_choosecpu_fork(p1, flags); 438 setrunqueue(p2); 439 SCHED_UNLOCK(s); 440 441 if (newptstat) 442 free(newptstat, M_SUBPROC); 443 444 /* 445 * Notify any interested parties about the new process. 446 */ 447 if ((flags & FORK_THREAD) == 0) 448 KNOTE(&p1->p_p->ps_klist, NOTE_FORK | p2->p_pid); 449 450 /* 451 * Update stats now that we know the fork was successful. 452 */ 453 uvmexp.forks++; 454 if (flags & FORK_PPWAIT) 455 uvmexp.forks_ppwait++; 456 if (flags & FORK_SHAREVM) 457 uvmexp.forks_sharevm++; 458 459 /* 460 * Pass a pointer to the new process to the caller. 461 */ 462 if (rnewprocp != NULL) 463 *rnewprocp = p2; 464 465 /* 466 * Preserve synchronization semantics of vfork. If waiting for 467 * child to exec or exit, set PS_PPWAIT on child and PS_ISPWAIT 468 * on ourselves, and sleep on our process for the latter flag 469 * to go away. 470 * XXX Need to stop other rthreads in the parent 471 */ 472 if (flags & FORK_PPWAIT) 473 while (p1->p_p->ps_flags & PS_ISPWAIT) 474 tsleep(p1->p_p, PWAIT, "ppwait", 0); 475 476 /* 477 * If we're tracing the child, alert the parent too. 478 */ 479 if ((flags & FORK_PTRACE) && (p1->p_flag & P_TRACED)) 480 psignal(p1, SIGTRAP); 481 482 /* 483 * Return child pid to parent process, 484 * marking us as parent via retval[1]. 485 */ 486 if (retval != NULL) { 487 retval[0] = p2->p_pid + 488 (flags & FORK_THREAD ? THREAD_PID_OFFSET : 0); 489 retval[1] = 0; 490 } 491 return (0); 492 } 493 494 /* 495 * Checks for current use of a pid, either as a pid or pgid. 496 */ 497 int 498 pidtaken(pid_t pid) 499 { 500 struct proc *p; 501 502 if (pfind(pid) != NULL) 503 return (1); 504 if (pgfind(pid) != NULL) 505 return (1); 506 LIST_FOREACH(p, &zombproc, p_list) { 507 if (p->p_pid == pid || (p->p_p->ps_pgrp && p->p_p->ps_pgrp->pg_id == pid)) 508 return (1); 509 } 510 return (0); 511 } 512 513 #if defined(MULTIPROCESSOR) 514 /* 515 * XXX This is a slight hack to get newly-formed processes to 516 * XXX acquire the kernel lock as soon as they run. 517 */ 518 void 519 proc_trampoline_mp(void) 520 { 521 struct proc *p; 522 523 p = curproc; 524 525 SCHED_ASSERT_LOCKED(); 526 __mp_unlock(&sched_lock); 527 spl0(); 528 SCHED_ASSERT_UNLOCKED(); 529 KASSERT(__mp_lock_held(&kernel_lock) == 0); 530 531 KERNEL_PROC_LOCK(p); 532 } 533 #endif 534