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 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/sysproto.h> 47 #include <sys/filedesc.h> 48 #include <sys/kernel.h> 49 #include <sys/sysctl.h> 50 #include <sys/malloc.h> 51 #include <sys/mutex.h> 52 #include <sys/proc.h> 53 #include <sys/resourcevar.h> 54 #include <sys/syscall.h> 55 #include <sys/vnode.h> 56 #include <sys/acct.h> 57 #include <sys/ktr.h> 58 #include <sys/ktrace.h> 59 #include <sys/kthread.h> 60 #include <sys/unistd.h> 61 #include <sys/jail.h> 62 #include <sys/sx.h> 63 64 #include <vm/vm.h> 65 #include <sys/lock.h> 66 #include <vm/pmap.h> 67 #include <vm/vm_map.h> 68 #include <vm/vm_extern.h> 69 #include <vm/vm_zone.h> 70 71 #include <sys/vmmeter.h> 72 #include <sys/user.h> 73 74 static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback"); 75 76 static int fast_vfork = 1; 77 SYSCTL_INT(_kern, OID_AUTO, fast_vfork, CTLFLAG_RW, &fast_vfork, 0, 78 "flag to indicate whether we have a fast vfork()"); 79 80 /* 81 * These are the stuctures used to create a callout list for things to do 82 * when forking a process 83 */ 84 struct forklist { 85 forklist_fn function; 86 TAILQ_ENTRY(forklist) next; 87 }; 88 89 static struct sx fork_list_lock; 90 91 TAILQ_HEAD(forklist_head, forklist); 92 static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list); 93 94 #ifndef _SYS_SYSPROTO_H_ 95 struct fork_args { 96 int dummy; 97 }; 98 #endif 99 100 static void 101 init_fork_list(void *data __unused) 102 { 103 104 sx_init(&fork_list_lock, "fork list"); 105 } 106 SYSINIT(fork_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_fork_list, NULL); 107 108 /* ARGSUSED */ 109 int 110 fork(p, uap) 111 struct proc *p; 112 struct fork_args *uap; 113 { 114 int error; 115 struct proc *p2; 116 117 error = fork1(p, RFFDG | RFPROC, &p2); 118 if (error == 0) { 119 p->p_retval[0] = p2->p_pid; 120 p->p_retval[1] = 0; 121 } 122 return error; 123 } 124 125 /* ARGSUSED */ 126 int 127 vfork(p, uap) 128 struct proc *p; 129 struct vfork_args *uap; 130 { 131 int error; 132 struct proc *p2; 133 134 error = fork1(p, RFFDG | RFPROC | RFPPWAIT | RFMEM, &p2); 135 if (error == 0) { 136 p->p_retval[0] = p2->p_pid; 137 p->p_retval[1] = 0; 138 } 139 return error; 140 } 141 142 int 143 rfork(p, uap) 144 struct proc *p; 145 struct rfork_args *uap; 146 { 147 int error; 148 struct proc *p2; 149 150 /* mask kernel only flags out of the user flags */ 151 error = fork1(p, uap->flags & ~RFKERNELONLY, &p2); 152 if (error == 0) { 153 p->p_retval[0] = p2 ? p2->p_pid : 0; 154 p->p_retval[1] = 0; 155 } 156 return error; 157 } 158 159 160 int nprocs = 1; /* process 0 */ 161 static int nextpid = 0; 162 SYSCTL_INT(_kern, OID_AUTO, lastpid, CTLFLAG_RD, &nextpid, 0, 163 "Last used PID"); 164 165 /* 166 * Random component to nextpid generation. We mix in a random factor to make 167 * it a little harder to predict. We sanity check the modulus value to avoid 168 * doing it in critical paths. Don't let it be too small or we pointlessly 169 * waste randomness entropy, and don't let it be impossibly large. Using a 170 * modulus that is too big causes a LOT more process table scans and slows 171 * down fork processing as the pidchecked caching is defeated. 172 */ 173 static int randompid = 0; 174 175 static int 176 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) 177 { 178 int error, pid; 179 180 pid = randompid; 181 error = sysctl_handle_int(oidp, &pid, 0, req); 182 if (error || !req->newptr) 183 return (error); 184 if (pid < 0 || pid > PID_MAX - 100) /* out of range */ 185 pid = PID_MAX - 100; 186 else if (pid < 2) /* NOP */ 187 pid = 0; 188 else if (pid < 100) /* Make it reasonable */ 189 pid = 100; 190 randompid = pid; 191 return (error); 192 } 193 194 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, 195 0, 0, sysctl_kern_randompid, "I", "Random PID modulus"); 196 197 int 198 fork1(p1, flags, procp) 199 struct proc *p1; /* parent proc */ 200 int flags; 201 struct proc **procp; /* child proc */ 202 { 203 struct proc *p2, *pptr; 204 uid_t uid; 205 struct proc *newproc; 206 int trypid; 207 int ok; 208 static int pidchecked = 0; 209 struct forklist *ep; 210 struct filedesc *fd; 211 212 /* Can't copy and clear */ 213 if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG)) 214 return (EINVAL); 215 216 /* 217 * Here we don't create a new process, but we divorce 218 * certain parts of a process from itself. 219 */ 220 if ((flags & RFPROC) == 0) { 221 222 vm_fork(p1, 0, flags); 223 224 /* 225 * Close all file descriptors. 226 */ 227 if (flags & RFCFDG) { 228 struct filedesc *fdtmp; 229 fdtmp = fdinit(p1); 230 PROC_LOCK(p1); 231 fdfree(p1); 232 p1->p_fd = fdtmp; 233 PROC_UNLOCK(p1); 234 } 235 236 /* 237 * Unshare file descriptors (from parent.) 238 */ 239 if (flags & RFFDG) { 240 if (p1->p_fd->fd_refcnt > 1) { 241 struct filedesc *newfd; 242 newfd = fdcopy(p1); 243 PROC_LOCK(p1); 244 fdfree(p1); 245 p1->p_fd = newfd; 246 PROC_UNLOCK(p1); 247 } 248 } 249 *procp = NULL; 250 return (0); 251 } 252 253 /* 254 * Although process entries are dynamically created, we still keep 255 * a global limit on the maximum number we will create. Don't allow 256 * a nonprivileged user to use the last process; don't let root 257 * exceed the limit. The variable nprocs is the current number of 258 * processes, maxproc is the limit. 259 */ 260 uid = p1->p_cred->p_ruid; 261 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { 262 tablefull("proc"); 263 return (EAGAIN); 264 } 265 /* 266 * Increment the nprocs resource before blocking can occur. There 267 * are hard-limits as to the number of processes that can run. 268 */ 269 nprocs++; 270 271 /* 272 * Increment the count of procs running with this uid. Don't allow 273 * a nonprivileged user to exceed their current limit. 274 */ 275 ok = chgproccnt(p1->p_cred->p_uidinfo, 1, 276 (uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0); 277 if (!ok) { 278 /* 279 * Back out the process count 280 */ 281 nprocs--; 282 return (EAGAIN); 283 } 284 285 /* Allocate new proc. */ 286 newproc = zalloc(proc_zone); 287 288 /* 289 * Setup linkage for kernel based threading 290 */ 291 if((flags & RFTHREAD) != 0) { 292 newproc->p_peers = p1->p_peers; 293 p1->p_peers = newproc; 294 newproc->p_leader = p1->p_leader; 295 } else { 296 newproc->p_peers = NULL; 297 newproc->p_leader = newproc; 298 } 299 300 newproc->p_vmspace = NULL; 301 302 /* 303 * Find an unused process ID. We remember a range of unused IDs 304 * ready to use (from nextpid+1 through pidchecked-1). 305 * 306 * If RFHIGHPID is set (used during system boot), do not allocate 307 * low-numbered pids. 308 */ 309 ALLPROC_LOCK(AP_EXCLUSIVE); 310 trypid = nextpid + 1; 311 if (flags & RFHIGHPID) { 312 if (trypid < 10) { 313 trypid = 10; 314 } 315 } else { 316 if (randompid) 317 trypid += arc4random() % randompid; 318 } 319 retry: 320 /* 321 * If the process ID prototype has wrapped around, 322 * restart somewhat above 0, as the low-numbered procs 323 * tend to include daemons that don't exit. 324 */ 325 if (trypid >= PID_MAX) { 326 trypid = trypid % PID_MAX; 327 if (trypid < 100) 328 trypid += 100; 329 pidchecked = 0; 330 } 331 if (trypid >= pidchecked) { 332 int doingzomb = 0; 333 334 pidchecked = PID_MAX; 335 /* 336 * Scan the active and zombie procs to check whether this pid 337 * is in use. Remember the lowest pid that's greater 338 * than trypid, so we can avoid checking for a while. 339 */ 340 p2 = LIST_FIRST(&allproc); 341 again: 342 for (; p2 != NULL; p2 = LIST_NEXT(p2, p_list)) { 343 while (p2->p_pid == trypid || 344 p2->p_pgrp->pg_id == trypid || 345 p2->p_session->s_sid == trypid) { 346 trypid++; 347 if (trypid >= pidchecked) 348 goto retry; 349 } 350 if (p2->p_pid > trypid && pidchecked > p2->p_pid) 351 pidchecked = p2->p_pid; 352 if (p2->p_pgrp->pg_id > trypid && 353 pidchecked > p2->p_pgrp->pg_id) 354 pidchecked = p2->p_pgrp->pg_id; 355 if (p2->p_session->s_sid > trypid && 356 pidchecked > p2->p_session->s_sid) 357 pidchecked = p2->p_session->s_sid; 358 } 359 if (!doingzomb) { 360 doingzomb = 1; 361 p2 = LIST_FIRST(&zombproc); 362 goto again; 363 } 364 } 365 366 /* 367 * RFHIGHPID does not mess with the nextpid counter during boot. 368 */ 369 if (flags & RFHIGHPID) 370 pidchecked = 0; 371 else 372 nextpid = trypid; 373 374 p2 = newproc; 375 p2->p_intr_nesting_level = 0; 376 p2->p_stat = SIDL; /* protect against others */ 377 p2->p_pid = trypid; 378 LIST_INSERT_HEAD(&allproc, p2, p_list); 379 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 380 ALLPROC_LOCK(AP_RELEASE); 381 382 /* 383 * Make a proc table entry for the new process. 384 * Start by zeroing the section of proc that is zero-initialized, 385 * then copy the section that is copied directly from the parent. 386 */ 387 bzero(&p2->p_startzero, 388 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 389 PROC_LOCK(p1); 390 bcopy(&p1->p_startcopy, &p2->p_startcopy, 391 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 392 PROC_UNLOCK(p1); 393 394 mtx_init(&p2->p_mtx, "process lock", MTX_DEF); 395 PROC_LOCK(p2); 396 p2->p_aioinfo = NULL; 397 398 /* 399 * Duplicate sub-structures as needed. 400 * Increase reference counts on shared objects. 401 * The p_stats and p_sigacts substructs are set in vm_fork. 402 */ 403 p2->p_flag = 0; 404 mtx_lock_spin(&sched_lock); 405 p2->p_sflag = PS_INMEM; 406 if (p1->p_sflag & PS_PROFIL) 407 startprofclock(p2); 408 mtx_unlock_spin(&sched_lock); 409 /* 410 * We start off holding one spinlock after fork: sched_lock. 411 */ 412 p2->p_spinlocks = 1; 413 PROC_UNLOCK(p2); 414 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred), 415 M_SUBPROC, M_WAITOK); 416 PROC_LOCK(p2); 417 PROC_LOCK(p1); 418 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); 419 p2->p_cred->p_refcnt = 1; 420 crhold(p1->p_ucred); 421 uihold(p1->p_cred->p_uidinfo); 422 423 if (p2->p_args) 424 p2->p_args->ar_ref++; 425 426 if (flags & RFSIGSHARE) { 427 p2->p_procsig = p1->p_procsig; 428 p2->p_procsig->ps_refcnt++; 429 if (p1->p_sigacts == &p1->p_addr->u_sigacts) { 430 struct sigacts *newsigacts; 431 432 PROC_UNLOCK(p1); 433 PROC_UNLOCK(p2); 434 /* Create the shared sigacts structure */ 435 MALLOC(newsigacts, struct sigacts *, 436 sizeof(struct sigacts), M_SUBPROC, M_WAITOK); 437 PROC_LOCK(p2); 438 PROC_LOCK(p1); 439 /* 440 * Set p_sigacts to the new shared structure. 441 * Note that this is updating p1->p_sigacts at the 442 * same time, since p_sigacts is just a pointer to 443 * the shared p_procsig->ps_sigacts. 444 */ 445 p2->p_sigacts = newsigacts; 446 bcopy(&p1->p_addr->u_sigacts, p2->p_sigacts, 447 sizeof(*p2->p_sigacts)); 448 *p2->p_sigacts = p1->p_addr->u_sigacts; 449 } 450 } else { 451 PROC_UNLOCK(p1); 452 PROC_UNLOCK(p2); 453 MALLOC(p2->p_procsig, struct procsig *, sizeof(struct procsig), 454 M_SUBPROC, M_WAITOK); 455 PROC_LOCK(p2); 456 PROC_LOCK(p1); 457 bcopy(p1->p_procsig, p2->p_procsig, sizeof(*p2->p_procsig)); 458 p2->p_procsig->ps_refcnt = 1; 459 p2->p_sigacts = NULL; /* finished in vm_fork() */ 460 } 461 if (flags & RFLINUXTHPN) 462 p2->p_sigparent = SIGUSR1; 463 else 464 p2->p_sigparent = SIGCHLD; 465 466 /* bump references to the text vnode (for procfs) */ 467 p2->p_textvp = p1->p_textvp; 468 PROC_UNLOCK(p1); 469 PROC_UNLOCK(p2); 470 if (p2->p_textvp) 471 VREF(p2->p_textvp); 472 473 if (flags & RFCFDG) 474 fd = fdinit(p1); 475 else if (flags & RFFDG) 476 fd = fdcopy(p1); 477 else 478 fd = fdshare(p1); 479 PROC_LOCK(p2); 480 p2->p_fd = fd; 481 482 /* 483 * If p_limit is still copy-on-write, bump refcnt, 484 * otherwise get a copy that won't be modified. 485 * (If PL_SHAREMOD is clear, the structure is shared 486 * copy-on-write.) 487 */ 488 PROC_LOCK(p1); 489 if (p1->p_limit->p_lflags & PL_SHAREMOD) 490 p2->p_limit = limcopy(p1->p_limit); 491 else { 492 p2->p_limit = p1->p_limit; 493 p2->p_limit->p_refcnt++; 494 } 495 496 /* 497 * Preserve some more flags in subprocess. PS_PROFIL has already 498 * been preserved. 499 */ 500 p2->p_flag |= p1->p_flag & P_SUGID; 501 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 502 p2->p_flag |= P_CONTROLT; 503 if (flags & RFPPWAIT) 504 p2->p_flag |= P_PPWAIT; 505 506 LIST_INSERT_AFTER(p1, p2, p_pglist); 507 PROC_UNLOCK(p1); 508 PROC_UNLOCK(p2); 509 510 /* 511 * Attach the new process to its parent. 512 * 513 * If RFNOWAIT is set, the newly created process becomes a child 514 * of init. This effectively disassociates the child from the 515 * parent. 516 */ 517 if (flags & RFNOWAIT) 518 pptr = initproc; 519 else 520 pptr = p1; 521 PROCTREE_LOCK(PT_EXCLUSIVE); 522 PROC_LOCK(p2); 523 p2->p_pptr = pptr; 524 PROC_UNLOCK(p2); 525 LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling); 526 PROCTREE_LOCK(PT_RELEASE); 527 PROC_LOCK(p2); 528 LIST_INIT(&p2->p_children); 529 LIST_INIT(&p2->p_heldmtx); 530 LIST_INIT(&p2->p_contested); 531 532 callout_init(&p2->p_itcallout, 0); 533 callout_init(&p2->p_slpcallout, 1); 534 535 PROC_LOCK(p1); 536 #ifdef KTRACE 537 /* 538 * Copy traceflag and tracefile if enabled. 539 * If not inherited, these were zeroed above. 540 */ 541 if (p1->p_traceflag & KTRFAC_INHERIT) { 542 p2->p_traceflag = p1->p_traceflag; 543 if ((p2->p_tracep = p1->p_tracep) != NULL) { 544 PROC_UNLOCK(p1); 545 PROC_UNLOCK(p2); 546 VREF(p2->p_tracep); 547 PROC_LOCK(p2); 548 PROC_LOCK(p1); 549 } 550 } 551 #endif 552 553 /* 554 * set priority of child to be that of parent 555 */ 556 mtx_lock_spin(&sched_lock); 557 p2->p_estcpu = p1->p_estcpu; 558 mtx_unlock_spin(&sched_lock); 559 560 /* 561 * This begins the section where we must prevent the parent 562 * from being swapped. 563 */ 564 _PHOLD(p1); 565 PROC_UNLOCK(p1); 566 PROC_UNLOCK(p2); 567 568 /* 569 * Finish creating the child process. It will return via a different 570 * execution path later. (ie: directly into user mode) 571 */ 572 vm_fork(p1, p2, flags); 573 574 if (flags == (RFFDG | RFPROC)) { 575 cnt.v_forks++; 576 cnt.v_forkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 577 } else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) { 578 cnt.v_vforks++; 579 cnt.v_vforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 580 } else if (p1 == &proc0) { 581 cnt.v_kthreads++; 582 cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 583 } else { 584 cnt.v_rforks++; 585 cnt.v_rforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 586 } 587 588 /* 589 * Both processes are set up, now check if any loadable modules want 590 * to adjust anything. 591 * What if they have an error? XXX 592 */ 593 sx_slock(&fork_list_lock); 594 TAILQ_FOREACH(ep, &fork_list, next) { 595 (*ep->function)(p1, p2, flags); 596 } 597 sx_sunlock(&fork_list_lock); 598 599 /* 600 * If RFSTOPPED not requested, make child runnable and add to 601 * run queue. 602 */ 603 microtime(&(p2->p_stats->p_start)); 604 p2->p_acflag = AFORK; 605 if ((flags & RFSTOPPED) == 0) { 606 mtx_lock_spin(&sched_lock); 607 p2->p_stat = SRUN; 608 setrunqueue(p2); 609 mtx_unlock_spin(&sched_lock); 610 } 611 612 /* 613 * Now can be swapped. 614 */ 615 PROC_LOCK(p1); 616 _PRELE(p1); 617 618 /* 619 * tell any interested parties about the new process 620 */ 621 KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); 622 PROC_UNLOCK(p1); 623 624 /* 625 * Preserve synchronization semantics of vfork. If waiting for 626 * child to exec or exit, set P_PPWAIT on child, and sleep on our 627 * proc (in case of exit). 628 */ 629 PROC_LOCK(p2); 630 while (p2->p_flag & P_PPWAIT) 631 msleep(p1, &p2->p_mtx, PWAIT, "ppwait", 0); 632 PROC_UNLOCK(p2); 633 634 /* 635 * Return child proc pointer to parent. 636 */ 637 *procp = p2; 638 return (0); 639 } 640 641 /* 642 * The next two functionms are general routines to handle adding/deleting 643 * items on the fork callout list. 644 * 645 * at_fork(): 646 * Take the arguments given and put them onto the fork callout list, 647 * However first make sure that it's not already there. 648 * Returns 0 on success or a standard error number. 649 */ 650 651 int 652 at_fork(function) 653 forklist_fn function; 654 { 655 struct forklist *ep; 656 657 #ifdef INVARIANTS 658 /* let the programmer know if he's been stupid */ 659 if (rm_at_fork(function)) 660 printf("WARNING: fork callout entry (%p) already present\n", 661 function); 662 #endif 663 ep = malloc(sizeof(*ep), M_ATFORK, M_NOWAIT); 664 if (ep == NULL) 665 return (ENOMEM); 666 ep->function = function; 667 sx_xlock(&fork_list_lock); 668 TAILQ_INSERT_TAIL(&fork_list, ep, next); 669 sx_xunlock(&fork_list_lock); 670 return (0); 671 } 672 673 /* 674 * Scan the exit callout list for the given item and remove it.. 675 * Returns the number of items removed (0 or 1) 676 */ 677 678 int 679 rm_at_fork(function) 680 forklist_fn function; 681 { 682 struct forklist *ep; 683 684 sx_xlock(&fork_list_lock); 685 TAILQ_FOREACH(ep, &fork_list, next) { 686 if (ep->function == function) { 687 TAILQ_REMOVE(&fork_list, ep, next); 688 sx_xunlock(&fork_list_lock); 689 free(ep, M_ATFORK); 690 return(1); 691 } 692 } 693 sx_xunlock(&fork_list_lock); 694 return (0); 695 } 696 697 /* 698 * Handle the return of a child process from fork1(). This function 699 * is called from the MD fork_trampoline() entry point. 700 */ 701 void 702 fork_exit(callout, arg, frame) 703 void (*callout)(void *, struct trapframe *); 704 void *arg; 705 struct trapframe *frame; 706 { 707 struct proc *p; 708 709 p = curproc; 710 711 /* 712 * Setup the sched_lock state so that we can release it. 713 */ 714 sched_lock.mtx_lock = (uintptr_t)p; 715 sched_lock.mtx_recurse = 0; 716 /* 717 * XXX: We really shouldn't have to do this. 718 */ 719 mtx_intr_enable(&sched_lock); 720 mtx_unlock_spin(&sched_lock); 721 722 #ifdef SMP 723 if (PCPU_GET(switchtime.tv_sec) == 0) 724 microuptime(PCPU_PTR(switchtime)); 725 PCPU_SET(switchticks, ticks); 726 #endif 727 728 /* 729 * cpu_set_fork_handler intercepts this function call to 730 * have this call a non-return function to stay in kernel mode. 731 * initproc has its own fork handler, but it does return. 732 */ 733 KASSERT(callout != NULL, ("NULL callout in fork_exit")); 734 callout(arg, frame); 735 736 /* 737 * Check if a kernel thread misbehaved and returned from its main 738 * function. 739 */ 740 PROC_LOCK(p); 741 if (p->p_flag & P_KTHREAD) { 742 PROC_UNLOCK(p); 743 mtx_lock(&Giant); 744 printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n", 745 p->p_comm, p->p_pid); 746 kthread_exit(0); 747 } 748 PROC_UNLOCK(p); 749 mtx_assert(&Giant, MA_NOTOWNED); 750 } 751 752 /* 753 * Simplified back end of syscall(), used when returning from fork() 754 * directly into user mode. Giant is not held on entry, and must not 755 * be held on return. This function is passed in to fork_exit() as the 756 * first parameter and is called when returning to a new userland process. 757 */ 758 void 759 fork_return(p, frame) 760 struct proc *p; 761 struct trapframe *frame; 762 { 763 764 userret(p, frame, 0); 765 #ifdef KTRACE 766 if (KTRPOINT(p, KTR_SYSRET)) { 767 if (!mtx_owned(&Giant)) 768 mtx_lock(&Giant); 769 ktrsysret(p->p_tracep, SYS_fork, 0, 0); 770 } 771 #endif 772 if (mtx_owned(&Giant)) 773 mtx_unlock(&Giant); 774 mtx_assert(&Giant, MA_NOTOWNED); 775 } 776