1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 39 * $FreeBSD$ 40 */ 41 42 #include "opt_ktrace.h" 43 #include "opt_mac.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/sysproto.h> 48 #include <sys/eventhandler.h> 49 #include <sys/filedesc.h> 50 #include <sys/kernel.h> 51 #include <sys/sysctl.h> 52 #include <sys/lock.h> 53 #include <sys/malloc.h> 54 #include <sys/mutex.h> 55 #include <sys/proc.h> 56 #include <sys/pioctl.h> 57 #include <sys/resourcevar.h> 58 #include <sys/sched.h> 59 #include <sys/syscall.h> 60 #include <sys/vnode.h> 61 #include <sys/acct.h> 62 #include <sys/mac.h> 63 #include <sys/ktr.h> 64 #include <sys/ktrace.h> 65 #include <sys/kthread.h> 66 #include <sys/unistd.h> 67 #include <sys/jail.h> 68 #include <sys/sx.h> 69 70 #include <vm/vm.h> 71 #include <vm/pmap.h> 72 #include <vm/vm_map.h> 73 #include <vm/vm_extern.h> 74 #include <vm/uma.h> 75 76 #include <sys/vmmeter.h> 77 #include <sys/user.h> 78 #include <machine/critical.h> 79 80 #ifndef _SYS_SYSPROTO_H_ 81 struct fork_args { 82 int dummy; 83 }; 84 #endif 85 86 static int forksleep; /* Place for fork1() to sleep on. */ 87 88 /* 89 * MPSAFE 90 */ 91 /* ARGSUSED */ 92 int 93 fork(td, uap) 94 struct thread *td; 95 struct fork_args *uap; 96 { 97 int error; 98 struct proc *p2; 99 100 error = fork1(td, RFFDG | RFPROC, 0, &p2); 101 if (error == 0) { 102 td->td_retval[0] = p2->p_pid; 103 td->td_retval[1] = 0; 104 } 105 return error; 106 } 107 108 /* 109 * MPSAFE 110 */ 111 /* ARGSUSED */ 112 int 113 vfork(td, uap) 114 struct thread *td; 115 struct vfork_args *uap; 116 { 117 int error; 118 struct proc *p2; 119 120 error = fork1(td, RFFDG | RFPROC | RFPPWAIT | RFMEM, 0, &p2); 121 if (error == 0) { 122 td->td_retval[0] = p2->p_pid; 123 td->td_retval[1] = 0; 124 } 125 return error; 126 } 127 128 /* 129 * MPSAFE 130 */ 131 int 132 rfork(td, uap) 133 struct thread *td; 134 struct rfork_args *uap; 135 { 136 int error; 137 struct proc *p2; 138 139 /* Don't allow kernel only flags. */ 140 if ((uap->flags & RFKERNELONLY) != 0) 141 return (EINVAL); 142 error = fork1(td, uap->flags, 0, &p2); 143 if (error == 0) { 144 td->td_retval[0] = p2 ? p2->p_pid : 0; 145 td->td_retval[1] = 0; 146 } 147 return error; 148 } 149 150 151 int nprocs = 1; /* process 0 */ 152 int lastpid = 0; 153 SYSCTL_INT(_kern, OID_AUTO, lastpid, CTLFLAG_RD, &lastpid, 0, 154 "Last used PID"); 155 156 /* 157 * Random component to lastpid generation. We mix in a random factor to make 158 * it a little harder to predict. We sanity check the modulus value to avoid 159 * doing it in critical paths. Don't let it be too small or we pointlessly 160 * waste randomness entropy, and don't let it be impossibly large. Using a 161 * modulus that is too big causes a LOT more process table scans and slows 162 * down fork processing as the pidchecked caching is defeated. 163 */ 164 static int randompid = 0; 165 166 static int 167 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) 168 { 169 int error, pid; 170 171 sysctl_wire_old_buffer(req, sizeof(int)); 172 sx_xlock(&allproc_lock); 173 pid = randompid; 174 error = sysctl_handle_int(oidp, &pid, 0, req); 175 if (error == 0 && req->newptr != NULL) { 176 if (pid < 0 || pid > PID_MAX - 100) /* out of range */ 177 pid = PID_MAX - 100; 178 else if (pid < 2) /* NOP */ 179 pid = 0; 180 else if (pid < 100) /* Make it reasonable */ 181 pid = 100; 182 randompid = pid; 183 } 184 sx_xunlock(&allproc_lock); 185 return (error); 186 } 187 188 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, 189 0, 0, sysctl_kern_randompid, "I", "Random PID modulus"); 190 191 int 192 fork1(td, flags, pages, procp) 193 struct thread *td; /* parent proc */ 194 int flags; 195 int pages; 196 struct proc **procp; /* child proc */ 197 { 198 struct proc *p2, *pptr; 199 uid_t uid; 200 struct proc *newproc; 201 int trypid; 202 int ok; 203 static int pidchecked = 0; 204 struct filedesc *fd; 205 struct filedesc_to_leader *fdtol; 206 struct proc *p1 = td->td_proc; 207 struct thread *td2; 208 struct kse *ke2; 209 struct ksegrp *kg2; 210 struct sigacts *newsigacts; 211 int error; 212 213 /* Can't copy and clear */ 214 if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG)) 215 return (EINVAL); 216 217 mtx_lock(&Giant); 218 /* 219 * Here we don't create a new process, but we divorce 220 * certain parts of a process from itself. 221 */ 222 if ((flags & RFPROC) == 0) { 223 vm_forkproc(td, NULL, NULL, flags); 224 225 /* 226 * Close all file descriptors. 227 */ 228 if (flags & RFCFDG) { 229 struct filedesc *fdtmp; 230 fdtmp = fdinit(td->td_proc->p_fd); 231 fdfree(td); 232 p1->p_fd = fdtmp; 233 } 234 235 /* 236 * Unshare file descriptors (from parent.) 237 */ 238 if (flags & RFFDG) { 239 FILEDESC_LOCK(p1->p_fd); 240 if (p1->p_fd->fd_refcnt > 1) { 241 struct filedesc *newfd; 242 243 newfd = fdcopy(td->td_proc->p_fd); 244 FILEDESC_UNLOCK(p1->p_fd); 245 fdfree(td); 246 p1->p_fd = newfd; 247 } else 248 FILEDESC_UNLOCK(p1->p_fd); 249 } 250 mtx_unlock(&Giant); 251 *procp = NULL; 252 return (0); 253 } 254 255 /* 256 * Note 1:1 allows for forking with one thread coming out on the 257 * other side with the expectation that the process is about to 258 * exec. 259 */ 260 if (p1->p_flag & P_THREADED) { 261 /* 262 * Idle the other threads for a second. 263 * Since the user space is copied, it must remain stable. 264 * In addition, all threads (from the user perspective) 265 * need to either be suspended or in the kernel, 266 * where they will try restart in the parent and will 267 * be aborted in the child. 268 */ 269 PROC_LOCK(p1); 270 if (thread_single(SINGLE_NO_EXIT)) { 271 /* Abort.. someone else is single threading before us */ 272 PROC_UNLOCK(p1); 273 mtx_unlock(&Giant); 274 return (ERESTART); 275 } 276 PROC_UNLOCK(p1); 277 /* 278 * All other activity in this process 279 * is now suspended at the user boundary, 280 * (or other safe places if we think of any). 281 */ 282 } 283 284 /* Allocate new proc. */ 285 newproc = uma_zalloc(proc_zone, M_WAITOK); 286 #ifdef MAC 287 mac_init_proc(newproc); 288 #endif 289 290 /* 291 * Although process entries are dynamically created, we still keep 292 * a global limit on the maximum number we will create. Don't allow 293 * a nonprivileged user to use the last ten processes; don't let root 294 * exceed the limit. The variable nprocs is the current number of 295 * processes, maxproc is the limit. 296 */ 297 sx_xlock(&allproc_lock); 298 uid = td->td_ucred->cr_ruid; 299 if ((nprocs >= maxproc - 10 && uid != 0) || nprocs >= maxproc) { 300 error = EAGAIN; 301 goto fail; 302 } 303 304 /* 305 * Increment the count of procs running with this uid. Don't allow 306 * a nonprivileged user to exceed their current limit. 307 */ 308 PROC_LOCK(p1); 309 ok = chgproccnt(td->td_ucred->cr_ruidinfo, 1, 310 (uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0); 311 PROC_UNLOCK(p1); 312 if (!ok) { 313 error = EAGAIN; 314 goto fail; 315 } 316 317 /* 318 * Increment the nprocs resource before blocking can occur. There 319 * are hard-limits as to the number of processes that can run. 320 */ 321 nprocs++; 322 323 /* 324 * Find an unused process ID. We remember a range of unused IDs 325 * ready to use (from lastpid+1 through pidchecked-1). 326 * 327 * If RFHIGHPID is set (used during system boot), do not allocate 328 * low-numbered pids. 329 */ 330 trypid = lastpid + 1; 331 if (flags & RFHIGHPID) { 332 if (trypid < 10) { 333 trypid = 10; 334 } 335 } else { 336 if (randompid) 337 trypid += arc4random() % randompid; 338 } 339 retry: 340 /* 341 * If the process ID prototype has wrapped around, 342 * restart somewhat above 0, as the low-numbered procs 343 * tend to include daemons that don't exit. 344 */ 345 if (trypid >= PID_MAX) { 346 trypid = trypid % PID_MAX; 347 if (trypid < 100) 348 trypid += 100; 349 pidchecked = 0; 350 } 351 if (trypid >= pidchecked) { 352 int doingzomb = 0; 353 354 pidchecked = PID_MAX; 355 /* 356 * Scan the active and zombie procs to check whether this pid 357 * is in use. Remember the lowest pid that's greater 358 * than trypid, so we can avoid checking for a while. 359 */ 360 p2 = LIST_FIRST(&allproc); 361 again: 362 for (; p2 != NULL; p2 = LIST_NEXT(p2, p_list)) { 363 PROC_LOCK(p2); 364 while (p2->p_pid == trypid || 365 p2->p_pgrp->pg_id == trypid || 366 p2->p_session->s_sid == trypid) { 367 trypid++; 368 if (trypid >= pidchecked) { 369 PROC_UNLOCK(p2); 370 goto retry; 371 } 372 } 373 if (p2->p_pid > trypid && pidchecked > p2->p_pid) 374 pidchecked = p2->p_pid; 375 if (p2->p_pgrp->pg_id > trypid && 376 pidchecked > p2->p_pgrp->pg_id) 377 pidchecked = p2->p_pgrp->pg_id; 378 if (p2->p_session->s_sid > trypid && 379 pidchecked > p2->p_session->s_sid) 380 pidchecked = p2->p_session->s_sid; 381 PROC_UNLOCK(p2); 382 } 383 if (!doingzomb) { 384 doingzomb = 1; 385 p2 = LIST_FIRST(&zombproc); 386 goto again; 387 } 388 } 389 390 /* 391 * RFHIGHPID does not mess with the lastpid counter during boot. 392 */ 393 if (flags & RFHIGHPID) 394 pidchecked = 0; 395 else 396 lastpid = trypid; 397 398 p2 = newproc; 399 p2->p_state = PRS_NEW; /* protect against others */ 400 p2->p_pid = trypid; 401 LIST_INSERT_HEAD(&allproc, p2, p_list); 402 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 403 sx_xunlock(&allproc_lock); 404 405 /* 406 * Malloc things while we don't hold any locks. 407 */ 408 if (flags & RFSIGSHARE) 409 newsigacts = NULL; 410 else 411 newsigacts = sigacts_alloc(); 412 413 /* 414 * Copy filedesc. 415 */ 416 if (flags & RFCFDG) { 417 fd = fdinit(td->td_proc->p_fd); 418 fdtol = NULL; 419 } else if (flags & RFFDG) { 420 FILEDESC_LOCK(p1->p_fd); 421 fd = fdcopy(td->td_proc->p_fd); 422 FILEDESC_UNLOCK(p1->p_fd); 423 fdtol = NULL; 424 } else { 425 fd = fdshare(p1->p_fd); 426 if (p1->p_fdtol == NULL) 427 p1->p_fdtol = 428 filedesc_to_leader_alloc(NULL, 429 NULL, 430 p1->p_leader); 431 if ((flags & RFTHREAD) != 0) { 432 /* 433 * Shared file descriptor table and 434 * shared process leaders. 435 */ 436 fdtol = p1->p_fdtol; 437 FILEDESC_LOCK(p1->p_fd); 438 fdtol->fdl_refcount++; 439 FILEDESC_UNLOCK(p1->p_fd); 440 } else { 441 /* 442 * Shared file descriptor table, and 443 * different process leaders 444 */ 445 fdtol = filedesc_to_leader_alloc(p1->p_fdtol, 446 p1->p_fd, 447 p2); 448 } 449 } 450 /* 451 * Make a proc table entry for the new process. 452 * Start by zeroing the section of proc that is zero-initialized, 453 * then copy the section that is copied directly from the parent. 454 */ 455 td2 = FIRST_THREAD_IN_PROC(p2); 456 kg2 = FIRST_KSEGRP_IN_PROC(p2); 457 ke2 = FIRST_KSE_IN_KSEGRP(kg2); 458 459 /* Allocate and switch to an alternate kstack if specified */ 460 if (pages != 0) 461 pmap_new_altkstack(td2, pages); 462 463 PROC_LOCK(p2); 464 PROC_LOCK(p1); 465 466 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 467 468 bzero(&p2->p_startzero, 469 (unsigned) RANGEOF(struct proc, p_startzero, p_endzero)); 470 bzero(&ke2->ke_startzero, 471 (unsigned) RANGEOF(struct kse, ke_startzero, ke_endzero)); 472 bzero(&td2->td_startzero, 473 (unsigned) RANGEOF(struct thread, td_startzero, td_endzero)); 474 bzero(&kg2->kg_startzero, 475 (unsigned) RANGEOF(struct ksegrp, kg_startzero, kg_endzero)); 476 477 bcopy(&p1->p_startcopy, &p2->p_startcopy, 478 (unsigned) RANGEOF(struct proc, p_startcopy, p_endcopy)); 479 bcopy(&td->td_startcopy, &td2->td_startcopy, 480 (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 481 bcopy(&td->td_ksegrp->kg_startcopy, &kg2->kg_startcopy, 482 (unsigned) RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy)); 483 #undef RANGEOF 484 485 /* Set up the thread as an active thread (as if runnable). */ 486 ke2->ke_state = KES_THREAD; 487 ke2->ke_thread = td2; 488 td2->td_kse = ke2; 489 490 /* 491 * Duplicate sub-structures as needed. 492 * Increase reference counts on shared objects. 493 * The p_stats substruct is set in vm_forkproc. 494 */ 495 p2->p_flag = 0; 496 if (p1->p_flag & P_PROFIL) 497 startprofclock(p2); 498 mtx_lock_spin(&sched_lock); 499 p2->p_sflag = PS_INMEM; 500 /* 501 * Allow the scheduler to adjust the priority of the child and 502 * parent while we hold the sched_lock. 503 */ 504 sched_fork(p1, p2); 505 506 mtx_unlock_spin(&sched_lock); 507 p2->p_ucred = crhold(td->td_ucred); 508 td2->td_ucred = crhold(p2->p_ucred); /* XXXKSE */ 509 510 pargs_hold(p2->p_args); 511 512 if (flags & RFSIGSHARE) { 513 p2->p_sigacts = sigacts_hold(p1->p_sigacts); 514 } else { 515 sigacts_copy(newsigacts, p1->p_sigacts); 516 p2->p_sigacts = newsigacts; 517 } 518 if (flags & RFLINUXTHPN) 519 p2->p_sigparent = SIGUSR1; 520 else 521 p2->p_sigparent = SIGCHLD; 522 523 /* Bump references to the text vnode (for procfs) */ 524 p2->p_textvp = p1->p_textvp; 525 if (p2->p_textvp) 526 VREF(p2->p_textvp); 527 p2->p_fd = fd; 528 p2->p_fdtol = fdtol; 529 PROC_UNLOCK(p1); 530 PROC_UNLOCK(p2); 531 532 /* 533 * p_limit is copy-on-write, bump refcnt, 534 */ 535 p2->p_limit = p1->p_limit; 536 p2->p_limit->p_refcnt++; 537 538 /* 539 * Setup linkage for kernel based threading 540 */ 541 if((flags & RFTHREAD) != 0) { 542 mtx_lock(&ppeers_lock); 543 p2->p_peers = p1->p_peers; 544 p1->p_peers = p2; 545 p2->p_leader = p1->p_leader; 546 mtx_unlock(&ppeers_lock); 547 PROC_LOCK(p1->p_leader); 548 if ((p1->p_leader->p_flag & P_WEXIT) != 0) { 549 PROC_UNLOCK(p1->p_leader); 550 /* 551 * The task leader is exiting, so process p1 is 552 * going to be killed shortly. Since p1 obviously 553 * isn't dead yet, we know that the leader is either 554 * sending SIGKILL's to all the processes in this 555 * task or is sleeping waiting for all the peers to 556 * exit. We let p1 complete the fork, but we need 557 * to go ahead and kill the new process p2 since 558 * the task leader may not get a chance to send 559 * SIGKILL to it. We leave it on the list so that 560 * the task leader will wait for this new process 561 * to commit suicide. 562 */ 563 PROC_LOCK(p2); 564 psignal(p2, SIGKILL); 565 PROC_UNLOCK(p2); 566 } else 567 PROC_UNLOCK(p1->p_leader); 568 } else { 569 p2->p_peers = NULL; 570 p2->p_leader = p2; 571 } 572 573 sx_xlock(&proctree_lock); 574 PGRP_LOCK(p1->p_pgrp); 575 PROC_LOCK(p2); 576 PROC_LOCK(p1); 577 578 /* 579 * Preserve some more flags in subprocess. P_PROFIL has already 580 * been preserved. 581 */ 582 p2->p_flag |= p1->p_flag & (P_SUGID | P_ALTSTACK); 583 SESS_LOCK(p1->p_session); 584 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 585 p2->p_flag |= P_CONTROLT; 586 SESS_UNLOCK(p1->p_session); 587 if (flags & RFPPWAIT) 588 p2->p_flag |= P_PPWAIT; 589 590 LIST_INSERT_AFTER(p1, p2, p_pglist); 591 PGRP_UNLOCK(p1->p_pgrp); 592 LIST_INIT(&p2->p_children); 593 594 callout_init(&p2->p_itcallout, 1); 595 596 #ifdef KTRACE 597 /* 598 * Copy traceflag and tracefile if enabled. 599 */ 600 mtx_lock(&ktrace_mtx); 601 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode")); 602 if (p1->p_traceflag & KTRFAC_INHERIT) { 603 p2->p_traceflag = p1->p_traceflag; 604 if ((p2->p_tracevp = p1->p_tracevp) != NULL) { 605 VREF(p2->p_tracevp); 606 KASSERT(p1->p_tracecred != NULL, 607 ("ktrace vnode with no cred")); 608 p2->p_tracecred = crhold(p1->p_tracecred); 609 } 610 } 611 mtx_unlock(&ktrace_mtx); 612 #endif 613 614 /* 615 * If PF_FORK is set, the child process inherits the 616 * procfs ioctl flags from its parent. 617 */ 618 if (p1->p_pfsflags & PF_FORK) { 619 p2->p_stops = p1->p_stops; 620 p2->p_pfsflags = p1->p_pfsflags; 621 } 622 623 /* 624 * This begins the section where we must prevent the parent 625 * from being swapped. 626 */ 627 _PHOLD(p1); 628 PROC_UNLOCK(p1); 629 630 /* 631 * Attach the new process to its parent. 632 * 633 * If RFNOWAIT is set, the newly created process becomes a child 634 * of init. This effectively disassociates the child from the 635 * parent. 636 */ 637 if (flags & RFNOWAIT) 638 pptr = initproc; 639 else 640 pptr = p1; 641 p2->p_pptr = pptr; 642 LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling); 643 sx_xunlock(&proctree_lock); 644 645 /* Inform accounting that we have forked. */ 646 p2->p_acflag = AFORK; 647 PROC_UNLOCK(p2); 648 649 /* 650 * Finish creating the child process. It will return via a different 651 * execution path later. (ie: directly into user mode) 652 */ 653 vm_forkproc(td, p2, td2, flags); 654 655 if (flags == (RFFDG | RFPROC)) { 656 cnt.v_forks++; 657 cnt.v_forkpages += p2->p_vmspace->vm_dsize + 658 p2->p_vmspace->vm_ssize; 659 } else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) { 660 cnt.v_vforks++; 661 cnt.v_vforkpages += p2->p_vmspace->vm_dsize + 662 p2->p_vmspace->vm_ssize; 663 } else if (p1 == &proc0) { 664 cnt.v_kthreads++; 665 cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + 666 p2->p_vmspace->vm_ssize; 667 } else { 668 cnt.v_rforks++; 669 cnt.v_rforkpages += p2->p_vmspace->vm_dsize + 670 p2->p_vmspace->vm_ssize; 671 } 672 673 /* 674 * Both processes are set up, now check if any loadable modules want 675 * to adjust anything. 676 * What if they have an error? XXX 677 */ 678 EVENTHANDLER_INVOKE(process_fork, p1, p2, flags); 679 680 /* 681 * If RFSTOPPED not requested, make child runnable and add to 682 * run queue. 683 */ 684 microuptime(&p2->p_stats->p_start); 685 if ((flags & RFSTOPPED) == 0) { 686 mtx_lock_spin(&sched_lock); 687 p2->p_state = PRS_NORMAL; 688 TD_SET_CAN_RUN(td2); 689 setrunqueue(td2); 690 mtx_unlock_spin(&sched_lock); 691 } 692 693 /* 694 * Now can be swapped. 695 */ 696 PROC_LOCK(p1); 697 _PRELE(p1); 698 699 /* 700 * tell any interested parties about the new process 701 */ 702 KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); 703 PROC_UNLOCK(p1); 704 705 /* 706 * Preserve synchronization semantics of vfork. If waiting for 707 * child to exec or exit, set P_PPWAIT on child, and sleep on our 708 * proc (in case of exit). 709 */ 710 PROC_LOCK(p2); 711 while (p2->p_flag & P_PPWAIT) 712 msleep(p1, &p2->p_mtx, PWAIT, "ppwait", 0); 713 PROC_UNLOCK(p2); 714 715 /* 716 * If other threads are waiting, let them continue now 717 */ 718 if (p1->p_flag & P_THREADED) { 719 PROC_LOCK(p1); 720 thread_single_end(); 721 PROC_UNLOCK(p1); 722 } 723 724 /* 725 * Return child proc pointer to parent. 726 */ 727 mtx_unlock(&Giant); 728 *procp = p2; 729 return (0); 730 fail: 731 sx_xunlock(&allproc_lock); 732 uma_zfree(proc_zone, newproc); 733 if (p1->p_flag & P_THREADED) { 734 PROC_LOCK(p1); 735 thread_single_end(); 736 PROC_UNLOCK(p1); 737 } 738 tsleep(&forksleep, PUSER, "fork", hz / 2); 739 mtx_unlock(&Giant); 740 return (error); 741 } 742 743 /* 744 * Handle the return of a child process from fork1(). This function 745 * is called from the MD fork_trampoline() entry point. 746 */ 747 void 748 fork_exit(callout, arg, frame) 749 void (*callout)(void *, struct trapframe *); 750 void *arg; 751 struct trapframe *frame; 752 { 753 struct thread *td; 754 struct proc *p; 755 756 if ((td = PCPU_GET(deadthread))) { 757 PCPU_SET(deadthread, NULL); 758 thread_stash(td); 759 } 760 td = curthread; 761 p = td->td_proc; 762 td->td_oncpu = PCPU_GET(cpuid); 763 p->p_state = PRS_NORMAL; 764 /* 765 * Finish setting up thread glue. We need to initialize 766 * the thread into a td_critnest=1 state. Some platforms 767 * may have already partially or fully initialized td_critnest 768 * and/or td_md.md_savecrit (when applciable). 769 * 770 * see <arch>/<arch>/critical.c 771 */ 772 sched_lock.mtx_lock = (uintptr_t)td; 773 sched_lock.mtx_recurse = 0; 774 cpu_critical_fork_exit(); 775 CTR3(KTR_PROC, "fork_exit: new thread %p (pid %d, %s)", td, p->p_pid, 776 p->p_comm); 777 if (PCPU_GET(switchtime.sec) == 0) 778 binuptime(PCPU_PTR(switchtime)); 779 PCPU_SET(switchticks, ticks); 780 mtx_unlock_spin(&sched_lock); 781 782 /* 783 * cpu_set_fork_handler intercepts this function call to 784 * have this call a non-return function to stay in kernel mode. 785 * initproc has its own fork handler, but it does return. 786 */ 787 KASSERT(callout != NULL, ("NULL callout in fork_exit")); 788 callout(arg, frame); 789 790 /* 791 * Check if a kernel thread misbehaved and returned from its main 792 * function. 793 */ 794 PROC_LOCK(p); 795 if (p->p_flag & P_KTHREAD) { 796 PROC_UNLOCK(p); 797 mtx_lock(&Giant); 798 printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n", 799 p->p_comm, p->p_pid); 800 kthread_exit(0); 801 } 802 PROC_UNLOCK(p); 803 #ifdef DIAGNOSTIC 804 cred_free_thread(td); 805 #endif 806 mtx_assert(&Giant, MA_NOTOWNED); 807 } 808 809 /* 810 * Simplified back end of syscall(), used when returning from fork() 811 * directly into user mode. Giant is not held on entry, and must not 812 * be held on return. This function is passed in to fork_exit() as the 813 * first parameter and is called when returning to a new userland process. 814 */ 815 void 816 fork_return(td, frame) 817 struct thread *td; 818 struct trapframe *frame; 819 { 820 821 userret(td, frame, 0); 822 #ifdef KTRACE 823 if (KTRPOINT(td, KTR_SYSRET)) 824 ktrsysret(SYS_fork, 0, 0); 825 #endif 826 mtx_assert(&Giant, MA_NOTOWNED); 827 } 828