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