1 /* $OpenBSD: kern_fork.c,v 1.165 2014/05/04 05:03:26 guenther 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.h> 66 67 #ifdef __HAVE_MD_TCB 68 # include <machine/tcb.h> 69 #endif 70 71 int nprocesses = 1; /* process 0 */ 72 int nthreads = 1; /* proc 0 */ 73 int randompid; /* when set to 1, pid's go random */ 74 struct forkstat forkstat; 75 76 void fork_return(void *); 77 void tfork_child_return(void *); 78 int pidtaken(pid_t); 79 80 void process_new(struct proc *, struct process *, int); 81 82 void 83 fork_return(void *arg) 84 { 85 struct proc *p = (struct proc *)arg; 86 87 if (p->p_p->ps_flags & PS_TRACED) 88 psignal(p, SIGTRAP); 89 90 child_return(p); 91 } 92 93 /*ARGSUSED*/ 94 int 95 sys_fork(struct proc *p, void *v, register_t *retval) 96 { 97 int flags; 98 99 flags = FORK_FORK; 100 if (p->p_p->ps_ptmask & PTRACE_FORK) 101 flags |= FORK_PTRACE; 102 return (fork1(p, flags, NULL, 0, fork_return, NULL, retval, NULL)); 103 } 104 105 /*ARGSUSED*/ 106 int 107 sys_vfork(struct proc *p, void *v, register_t *retval) 108 { 109 return (fork1(p, FORK_VFORK|FORK_PPWAIT, NULL, 0, NULL, 110 NULL, retval, NULL)); 111 } 112 113 int 114 sys___tfork(struct proc *p, void *v, register_t *retval) 115 { 116 struct sys___tfork_args /* { 117 syscallarg(const struct __tfork) *param; 118 syscallarg(size_t) psize; 119 } */ *uap = v; 120 size_t psize = SCARG(uap, psize); 121 struct __tfork param = { 0 }; 122 int flags; 123 int error; 124 125 if (psize == 0 || psize > sizeof(param)) 126 return (EINVAL); 127 if ((error = copyin(SCARG(uap, param), ¶m, psize))) 128 return (error); 129 #ifdef KTRACE 130 if (KTRPOINT(p, KTR_STRUCT)) 131 ktrstruct(p, "tfork", ¶m, sizeof(param)); 132 #endif 133 134 flags = FORK_TFORK | FORK_THREAD | FORK_SIGHAND | FORK_SHAREVM 135 | FORK_SHAREFILES; 136 137 return (fork1(p, flags, param.tf_stack, param.tf_tid, 138 tfork_child_return, param.tf_tcb, retval, NULL)); 139 } 140 141 void 142 tfork_child_return(void *arg) 143 { 144 struct proc *p = curproc; 145 146 TCB_SET(p, arg); 147 child_return(p); 148 } 149 150 /* 151 * Allocate and initialize a new process. 152 */ 153 void 154 process_new(struct proc *p, struct process *parent, int flags) 155 { 156 struct process *pr; 157 158 pr = pool_get(&process_pool, PR_WAITOK); 159 pr->ps_mainproc = p; 160 161 TAILQ_INIT(&pr->ps_threads); 162 TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); 163 pr->ps_pptr = parent; 164 LIST_INIT(&pr->ps_children); 165 pr->ps_refcnt = 1; 166 167 /* 168 * Make a process structure for the new process. 169 * Start by zeroing the section of proc that is zero-initialized, 170 * then copy the section that is copied directly from the parent. 171 */ 172 memset(&pr->ps_startzero, 0, 173 (caddr_t)&pr->ps_endzero - (caddr_t)&pr->ps_startzero); 174 memcpy(&pr->ps_startcopy, &parent->ps_startcopy, 175 (caddr_t)&pr->ps_endcopy - (caddr_t)&pr->ps_startcopy); 176 177 /* post-copy fixups */ 178 pr->ps_ucred = p->p_ucred; 179 crhold(pr->ps_ucred); 180 KASSERT(p->p_ucred->cr_ref >= 3); /* fork thr, new thr, new process */ 181 pr->ps_limit->p_refcnt++; 182 183 /* bump references to the text vnode (for procfs) */ 184 pr->ps_textvp = parent->ps_textvp; 185 if (pr->ps_textvp) 186 vref(pr->ps_textvp); 187 188 timeout_set(&pr->ps_realit_to, realitexpire, pr); 189 190 pr->ps_flags = parent->ps_flags & (PS_SUGID | PS_SUGIDEXEC); 191 if (parent->ps_session->s_ttyvp != NULL && 192 parent->ps_flags & PS_CONTROLT) 193 atomic_setbits_int(&pr->ps_flags, PS_CONTROLT); 194 195 p->p_p = pr; 196 197 /* 198 * Create signal actions for the child process. 199 */ 200 if (flags & FORK_SIGHAND) 201 pr->ps_sigacts = sigactsshare(parent); 202 else 203 pr->ps_sigacts = sigactsinit(parent); 204 205 if (parent->ps_flags & PS_PROFIL) 206 startprofclock(pr); 207 if ((flags & FORK_PTRACE) && (parent->ps_flags & PS_TRACED)) 208 atomic_setbits_int(&pr->ps_flags, PS_TRACED); 209 if (flags & FORK_NOZOMBIE) 210 atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE); 211 if (flags & FORK_SYSTEM) 212 atomic_setbits_int(&pr->ps_flags, PS_SYSTEM); 213 214 /* it's sufficiently inited to be globally visible */ 215 LIST_INSERT_HEAD(&allprocess, pr, ps_list); 216 } 217 218 /* print the 'table full' message once per 10 seconds */ 219 struct timeval fork_tfmrate = { 10, 0 }; 220 221 int 222 fork1(struct proc *curp, int flags, void *stack, pid_t *tidptr, 223 void (*func)(void *), void *arg, register_t *retval, 224 struct proc **rnewprocp) 225 { 226 struct process *curpr = curp->p_p; 227 struct process *pr; 228 struct proc *p; 229 uid_t uid; 230 struct vmspace *vm; 231 int count; 232 vaddr_t uaddr; 233 int s; 234 struct ptrace_state *newptstat = NULL; 235 #if NSYSTRACE > 0 236 void *newstrp = NULL; 237 #endif 238 239 /* sanity check some flag combinations */ 240 if (flags & FORK_THREAD) { 241 if ((flags & FORK_SIGHAND) == 0 || (flags & FORK_SYSTEM) != 0) 242 return (EINVAL); 243 } 244 if (flags & FORK_SIGHAND && (flags & FORK_SHAREVM) == 0) 245 return (EINVAL); 246 247 /* 248 * Although process entries are dynamically created, we still keep 249 * a global limit on the maximum number we will create. We reserve 250 * the last 5 processes to root. The variable nprocesses is the 251 * current number of processes, maxprocess is the limit. Similar 252 * rules for threads (struct proc): we reserve the last 5 to root; 253 * the variable nthreads is the current number of procs, maxthread is 254 * the limit. 255 */ 256 uid = curp->p_ucred->cr_ruid; 257 if ((nthreads >= maxthread - 5 && uid != 0) || nthreads >= maxthread) { 258 static struct timeval lasttfm; 259 260 if (ratecheck(&lasttfm, &fork_tfmrate)) 261 tablefull("proc"); 262 return (EAGAIN); 263 } 264 nthreads++; 265 266 if ((flags & FORK_THREAD) == 0) { 267 if ((nprocesses >= maxprocess - 5 && uid != 0) || 268 nprocesses >= maxprocess) { 269 static struct timeval lasttfm; 270 271 if (ratecheck(&lasttfm, &fork_tfmrate)) 272 tablefull("process"); 273 nthreads--; 274 return (EAGAIN); 275 } 276 nprocesses++; 277 278 /* 279 * Increment the count of processes running with 280 * this uid. Don't allow a nonprivileged user to 281 * exceed their current limit. 282 */ 283 count = chgproccnt(uid, 1); 284 if (uid != 0 && count > curp->p_rlimit[RLIMIT_NPROC].rlim_cur) { 285 (void)chgproccnt(uid, -1); 286 nprocesses--; 287 nthreads--; 288 return (EAGAIN); 289 } 290 } 291 292 uaddr = uvm_uarea_alloc(); 293 if (uaddr == 0) { 294 if ((flags & FORK_THREAD) == 0) { 295 (void)chgproccnt(uid, -1); 296 nprocesses--; 297 } 298 nthreads--; 299 return (ENOMEM); 300 } 301 302 /* 303 * From now on, we're committed to the fork and cannot fail. 304 */ 305 306 /* Allocate new proc. */ 307 p = pool_get(&proc_pool, PR_WAITOK); 308 309 p->p_stat = SIDL; /* protect against others */ 310 p->p_flag = 0; 311 312 /* 313 * Make a proc table entry for the new process. 314 * Start by zeroing the section of proc that is zero-initialized, 315 * then copy the section that is copied directly from the parent. 316 */ 317 memset(&p->p_startzero, 0, 318 (caddr_t)&p->p_endzero - (caddr_t)&p->p_startzero); 319 memcpy(&p->p_startcopy, &curp->p_startcopy, 320 (caddr_t)&p->p_endcopy - (caddr_t)&p->p_startcopy); 321 crhold(p->p_ucred); 322 323 /* 324 * Initialize the timeouts. 325 */ 326 timeout_set(&p->p_sleep_to, endtsleep, p); 327 328 if (flags & FORK_THREAD) { 329 atomic_setbits_int(&p->p_flag, P_THREAD); 330 p->p_p = pr = curpr; 331 pr->ps_refcnt++; 332 } else { 333 process_new(p, curpr, flags); 334 pr = p->p_p; 335 } 336 if (pr->ps_flags & PS_SYSTEM) 337 atomic_setbits_int(&p->p_flag, P_SYSTEM); 338 339 /* 340 * Duplicate sub-structures as needed. 341 * Increase reference counts on shared objects. 342 */ 343 if (flags & FORK_SHAREFILES) 344 p->p_fd = fdshare(curp); 345 else 346 p->p_fd = fdcopy(curp); 347 348 if (flags & FORK_PPWAIT) { 349 atomic_setbits_int(&pr->ps_flags, PS_PPWAIT); 350 atomic_setbits_int(&curpr->ps_flags, PS_ISPWAIT); 351 } 352 353 #ifdef KTRACE 354 /* 355 * Copy traceflag and tracefile if enabled. 356 * If not inherited, these were zeroed above. 357 */ 358 if ((flags & FORK_THREAD) == 0 && curpr->ps_traceflag & KTRFAC_INHERIT) 359 ktrsettrace(pr, curpr->ps_traceflag, curpr->ps_tracevp, 360 curpr->ps_tracecred); 361 #endif 362 363 /* 364 * set priority of child to be that of parent 365 * XXX should move p_estcpu into the region of struct proc which gets 366 * copied. 367 */ 368 scheduler_fork_hook(curp, p); 369 370 if (flags & FORK_THREAD) 371 sigstkinit(&p->p_sigstk); 372 373 /* 374 * If emulation has thread fork hook, call it now. 375 */ 376 if (pr->ps_emul->e_proc_fork) 377 (*pr->ps_emul->e_proc_fork)(p, curp); 378 379 p->p_addr = (struct user *)uaddr; 380 381 /* 382 * Finish creating the child process. It will return through a 383 * different path later. 384 */ 385 uvm_fork(curp, p, ((flags & FORK_SHAREVM) ? TRUE : FALSE), stack, 386 0, func ? func : child_return, arg ? arg : p); 387 388 vm = p->p_vmspace; 389 390 if (flags & FORK_FORK) { 391 forkstat.cntfork++; 392 forkstat.sizfork += vm->vm_dsize + vm->vm_ssize; 393 } else if (flags & FORK_VFORK) { 394 forkstat.cntvfork++; 395 forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize; 396 } else if (flags & FORK_TFORK) { 397 forkstat.cnttfork++; 398 } else { 399 forkstat.cntkthread++; 400 forkstat.sizkthread += vm->vm_dsize + vm->vm_ssize; 401 } 402 403 if (pr->ps_flags & PS_TRACED && flags & FORK_FORK) 404 newptstat = malloc(sizeof(*newptstat), M_SUBPROC, M_WAITOK); 405 #if NSYSTRACE > 0 406 if (ISSET(curp->p_flag, P_SYSTRACE)) 407 newstrp = systrace_getproc(); 408 #endif 409 410 p->p_pid = allocpid(); 411 412 LIST_INSERT_HEAD(&allproc, p, p_list); 413 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash); 414 if ((flags & FORK_THREAD) == 0) { 415 LIST_INSERT_AFTER(curpr, pr, ps_pglist); 416 LIST_INSERT_HEAD(&curpr->ps_children, pr, ps_sibling); 417 418 if (pr->ps_flags & PS_TRACED) { 419 pr->ps_oppid = curpr->ps_pid; 420 if (pr->ps_pptr != curpr->ps_pptr) 421 proc_reparent(pr, curpr->ps_pptr); 422 423 /* 424 * Set ptrace status. 425 */ 426 if (flags & FORK_FORK) { 427 pr->ps_ptstat = newptstat; 428 newptstat = NULL; 429 curpr->ps_ptstat->pe_report_event = PTRACE_FORK; 430 pr->ps_ptstat->pe_report_event = PTRACE_FORK; 431 curpr->ps_ptstat->pe_other_pid = pr->ps_pid; 432 pr->ps_ptstat->pe_other_pid = curpr->ps_pid; 433 } 434 } 435 } else { 436 TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); 437 /* 438 * if somebody else wants to take us to single threaded mode, 439 * count ourselves in. 440 */ 441 if (pr->ps_single) { 442 curpr->ps_singlecount++; 443 atomic_setbits_int(&p->p_flag, P_SUSPSINGLE); 444 } 445 } 446 447 #if NSYSTRACE > 0 448 if (newstrp) 449 systrace_fork(curp, p, newstrp); 450 #endif 451 452 if (tidptr != NULL) { 453 pid_t pid = p->p_pid + THREAD_PID_OFFSET; 454 455 if (copyout(&pid, tidptr, sizeof(pid))) 456 psignal(curp, SIGSEGV); 457 } 458 459 /* 460 * For new processes, set accounting bits 461 */ 462 if ((flags & FORK_THREAD) == 0) { 463 getnanotime(&pr->ps_start); 464 pr->ps_acflag = AFORK; 465 } 466 467 /* 468 * Make child runnable and add to run queue. 469 */ 470 if ((flags & FORK_IDLE) == 0) { 471 SCHED_LOCK(s); 472 p->p_stat = SRUN; 473 p->p_cpu = sched_choosecpu_fork(curp, flags); 474 setrunqueue(p); 475 SCHED_UNLOCK(s); 476 } else 477 p->p_cpu = arg; 478 479 if (newptstat) 480 free(newptstat, M_SUBPROC); 481 482 /* 483 * Notify any interested parties about the new process. 484 */ 485 if ((flags & FORK_THREAD) == 0) 486 KNOTE(&curpr->ps_klist, NOTE_FORK | p->p_pid); 487 488 /* 489 * Update stats now that we know the fork was successful. 490 */ 491 uvmexp.forks++; 492 if (flags & FORK_PPWAIT) 493 uvmexp.forks_ppwait++; 494 if (flags & FORK_SHAREVM) 495 uvmexp.forks_sharevm++; 496 497 /* 498 * Pass a pointer to the new process to the caller. 499 */ 500 if (rnewprocp != NULL) 501 *rnewprocp = p; 502 503 /* 504 * Preserve synchronization semantics of vfork. If waiting for 505 * child to exec or exit, set PS_PPWAIT on child and PS_ISPWAIT 506 * on ourselves, and sleep on our process for the latter flag 507 * to go away. 508 * XXX Need to stop other rthreads in the parent 509 */ 510 if (flags & FORK_PPWAIT) 511 while (curpr->ps_flags & PS_ISPWAIT) 512 tsleep(curpr, PWAIT, "ppwait", 0); 513 514 /* 515 * If we're tracing the child, alert the parent too. 516 */ 517 if ((flags & FORK_PTRACE) && (curpr->ps_flags & PS_TRACED)) 518 psignal(curp, SIGTRAP); 519 520 /* 521 * Return child pid to parent process, 522 * marking us as parent via retval[1]. 523 */ 524 if (retval != NULL) { 525 retval[0] = p->p_pid + 526 (flags & FORK_THREAD ? THREAD_PID_OFFSET : 0); 527 retval[1] = 0; 528 } 529 return (0); 530 } 531 532 /* 533 * Checks for current use of a pid, either as a pid or pgid. 534 */ 535 pid_t oldpids[100]; 536 int 537 ispidtaken(pid_t pid) 538 { 539 uint32_t i; 540 struct process *pr; 541 542 for (i = 0; i < nitems(oldpids); i++) 543 if (pid == oldpids[i]) 544 return (1); 545 546 if (pfind(pid) != NULL) 547 return (1); 548 if (pgfind(pid) != NULL) 549 return (1); 550 LIST_FOREACH(pr, &zombprocess, ps_list) { 551 if (pr->ps_pid == pid || 552 (pr->ps_pgrp && pr->ps_pgrp->pg_id == pid)) 553 return (1); 554 } 555 return (0); 556 } 557 558 /* Find an unused pid satisfying 1 <= lastpid <= PID_MAX */ 559 pid_t 560 allocpid(void) 561 { 562 static pid_t lastpid; 563 pid_t pid; 564 565 if (!randompid) { 566 /* only used early on for system processes */ 567 pid = ++lastpid; 568 } else { 569 do { 570 pid = 1 + arc4random_uniform(PID_MAX); 571 } while (ispidtaken(pid)); 572 } 573 574 return pid; 575 } 576 577 void 578 freepid(pid_t pid) 579 { 580 static uint32_t idx; 581 582 oldpids[idx++ % nitems(oldpids)] = pid; 583 } 584 585 #if defined(MULTIPROCESSOR) 586 /* 587 * XXX This is a slight hack to get newly-formed processes to 588 * XXX acquire the kernel lock as soon as they run. 589 */ 590 void 591 proc_trampoline_mp(void) 592 { 593 struct proc *p; 594 595 p = curproc; 596 597 SCHED_ASSERT_LOCKED(); 598 __mp_unlock(&sched_lock); 599 spl0(); 600 SCHED_ASSERT_UNLOCKED(); 601 KASSERT(__mp_lock_held(&kernel_lock) == 0); 602 603 KERNEL_LOCK(); 604 } 605 #endif 606