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_stat = SIDL; /* protect against others */ 376 p2->p_pid = trypid; 377 LIST_INSERT_HEAD(&allproc, p2, p_list); 378 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 379 ALLPROC_LOCK(AP_RELEASE); 380 381 /* 382 * Make a proc table entry for the new process. 383 * Start by zeroing the section of proc that is zero-initialized, 384 * then copy the section that is copied directly from the parent. 385 */ 386 bzero(&p2->p_startzero, 387 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 388 PROC_LOCK(p1); 389 bcopy(&p1->p_startcopy, &p2->p_startcopy, 390 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 391 PROC_UNLOCK(p1); 392 393 mtx_init(&p2->p_mtx, "process lock", MTX_DEF); 394 PROC_LOCK(p2); 395 396 /* 397 * Duplicate sub-structures as needed. 398 * Increase reference counts on shared objects. 399 * The p_stats and p_sigacts substructs are set in vm_fork. 400 */ 401 p2->p_flag = 0; 402 mtx_lock_spin(&sched_lock); 403 p2->p_sflag = PS_INMEM; 404 if (p1->p_sflag & PS_PROFIL) 405 startprofclock(p2); 406 mtx_unlock_spin(&sched_lock); 407 /* 408 * We start off holding one spinlock after fork: sched_lock. 409 */ 410 p2->p_spinlocks = 1; 411 PROC_UNLOCK(p2); 412 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred), 413 M_SUBPROC, M_WAITOK); 414 PROC_LOCK(p2); 415 PROC_LOCK(p1); 416 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); 417 p2->p_cred->p_refcnt = 1; 418 crhold(p1->p_ucred); 419 uihold(p1->p_cred->p_uidinfo); 420 421 if (p2->p_args) 422 p2->p_args->ar_ref++; 423 424 if (flags & RFSIGSHARE) { 425 p2->p_procsig = p1->p_procsig; 426 p2->p_procsig->ps_refcnt++; 427 if (p1->p_sigacts == &p1->p_addr->u_sigacts) { 428 struct sigacts *newsigacts; 429 430 PROC_UNLOCK(p1); 431 PROC_UNLOCK(p2); 432 /* Create the shared sigacts structure */ 433 MALLOC(newsigacts, struct sigacts *, 434 sizeof(struct sigacts), M_SUBPROC, M_WAITOK); 435 PROC_LOCK(p2); 436 PROC_LOCK(p1); 437 /* 438 * Set p_sigacts to the new shared structure. 439 * Note that this is updating p1->p_sigacts at the 440 * same time, since p_sigacts is just a pointer to 441 * the shared p_procsig->ps_sigacts. 442 */ 443 p2->p_sigacts = newsigacts; 444 bcopy(&p1->p_addr->u_sigacts, p2->p_sigacts, 445 sizeof(*p2->p_sigacts)); 446 *p2->p_sigacts = p1->p_addr->u_sigacts; 447 } 448 } else { 449 PROC_UNLOCK(p1); 450 PROC_UNLOCK(p2); 451 MALLOC(p2->p_procsig, struct procsig *, sizeof(struct procsig), 452 M_SUBPROC, M_WAITOK); 453 PROC_LOCK(p2); 454 PROC_LOCK(p1); 455 bcopy(p1->p_procsig, p2->p_procsig, sizeof(*p2->p_procsig)); 456 p2->p_procsig->ps_refcnt = 1; 457 p2->p_sigacts = NULL; /* finished in vm_fork() */ 458 } 459 if (flags & RFLINUXTHPN) 460 p2->p_sigparent = SIGUSR1; 461 else 462 p2->p_sigparent = SIGCHLD; 463 464 /* bump references to the text vnode (for procfs) */ 465 p2->p_textvp = p1->p_textvp; 466 PROC_UNLOCK(p1); 467 PROC_UNLOCK(p2); 468 if (p2->p_textvp) 469 VREF(p2->p_textvp); 470 471 if (flags & RFCFDG) 472 fd = fdinit(p1); 473 else if (flags & RFFDG) 474 fd = fdcopy(p1); 475 else 476 fd = fdshare(p1); 477 PROC_LOCK(p2); 478 p2->p_fd = fd; 479 480 /* 481 * If p_limit is still copy-on-write, bump refcnt, 482 * otherwise get a copy that won't be modified. 483 * (If PL_SHAREMOD is clear, the structure is shared 484 * copy-on-write.) 485 */ 486 PROC_LOCK(p1); 487 if (p1->p_limit->p_lflags & PL_SHAREMOD) 488 p2->p_limit = limcopy(p1->p_limit); 489 else { 490 p2->p_limit = p1->p_limit; 491 p2->p_limit->p_refcnt++; 492 } 493 494 /* 495 * Preserve some more flags in subprocess. PS_PROFIL has already 496 * been preserved. 497 */ 498 p2->p_flag |= p1->p_flag & P_SUGID; 499 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 500 p2->p_flag |= P_CONTROLT; 501 if (flags & RFPPWAIT) 502 p2->p_flag |= P_PPWAIT; 503 504 LIST_INSERT_AFTER(p1, p2, p_pglist); 505 PROC_UNLOCK(p1); 506 PROC_UNLOCK(p2); 507 508 /* 509 * Attach the new process to its parent. 510 * 511 * If RFNOWAIT is set, the newly created process becomes a child 512 * of init. This effectively disassociates the child from the 513 * parent. 514 */ 515 if (flags & RFNOWAIT) 516 pptr = initproc; 517 else 518 pptr = p1; 519 PROCTREE_LOCK(PT_EXCLUSIVE); 520 PROC_LOCK(p2); 521 p2->p_pptr = pptr; 522 PROC_UNLOCK(p2); 523 LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling); 524 PROCTREE_LOCK(PT_RELEASE); 525 PROC_LOCK(p2); 526 LIST_INIT(&p2->p_children); 527 LIST_INIT(&p2->p_heldmtx); 528 LIST_INIT(&p2->p_contested); 529 530 callout_init(&p2->p_itcallout, 0); 531 callout_init(&p2->p_slpcallout, 1); 532 533 PROC_LOCK(p1); 534 #ifdef KTRACE 535 /* 536 * Copy traceflag and tracefile if enabled. 537 * If not inherited, these were zeroed above. 538 */ 539 if (p1->p_traceflag & KTRFAC_INHERIT) { 540 p2->p_traceflag = p1->p_traceflag; 541 if ((p2->p_tracep = p1->p_tracep) != NULL) { 542 PROC_UNLOCK(p1); 543 PROC_UNLOCK(p2); 544 VREF(p2->p_tracep); 545 PROC_LOCK(p2); 546 PROC_LOCK(p1); 547 } 548 } 549 #endif 550 551 /* 552 * set priority of child to be that of parent 553 */ 554 mtx_lock_spin(&sched_lock); 555 p2->p_estcpu = p1->p_estcpu; 556 mtx_unlock_spin(&sched_lock); 557 558 /* 559 * This begins the section where we must prevent the parent 560 * from being swapped. 561 */ 562 _PHOLD(p1); 563 PROC_UNLOCK(p1); 564 PROC_UNLOCK(p2); 565 566 /* 567 * Finish creating the child process. It will return via a different 568 * execution path later. (ie: directly into user mode) 569 */ 570 vm_fork(p1, p2, flags); 571 572 if (flags == (RFFDG | RFPROC)) { 573 cnt.v_forks++; 574 cnt.v_forkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 575 } else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) { 576 cnt.v_vforks++; 577 cnt.v_vforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 578 } else if (p1 == &proc0) { 579 cnt.v_kthreads++; 580 cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 581 } else { 582 cnt.v_rforks++; 583 cnt.v_rforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 584 } 585 586 /* 587 * Both processes are set up, now check if any loadable modules want 588 * to adjust anything. 589 * What if they have an error? XXX 590 */ 591 sx_slock(&fork_list_lock); 592 TAILQ_FOREACH(ep, &fork_list, next) { 593 (*ep->function)(p1, p2, flags); 594 } 595 sx_sunlock(&fork_list_lock); 596 597 /* 598 * If RFSTOPPED not requested, make child runnable and add to 599 * run queue. 600 */ 601 microtime(&(p2->p_stats->p_start)); 602 p2->p_acflag = AFORK; 603 if ((flags & RFSTOPPED) == 0) { 604 mtx_lock_spin(&sched_lock); 605 p2->p_stat = SRUN; 606 setrunqueue(p2); 607 mtx_unlock_spin(&sched_lock); 608 } 609 610 /* 611 * Now can be swapped. 612 */ 613 PROC_LOCK(p1); 614 _PRELE(p1); 615 616 /* 617 * tell any interested parties about the new process 618 */ 619 KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); 620 PROC_UNLOCK(p1); 621 622 /* 623 * Preserve synchronization semantics of vfork. If waiting for 624 * child to exec or exit, set P_PPWAIT on child, and sleep on our 625 * proc (in case of exit). 626 */ 627 PROC_LOCK(p2); 628 while (p2->p_flag & P_PPWAIT) 629 msleep(p1, &p2->p_mtx, PWAIT, "ppwait", 0); 630 PROC_UNLOCK(p2); 631 632 /* 633 * Return child proc pointer to parent. 634 */ 635 *procp = p2; 636 return (0); 637 } 638 639 /* 640 * The next two functionms are general routines to handle adding/deleting 641 * items on the fork callout list. 642 * 643 * at_fork(): 644 * Take the arguments given and put them onto the fork callout list, 645 * However first make sure that it's not already there. 646 * Returns 0 on success or a standard error number. 647 */ 648 649 int 650 at_fork(function) 651 forklist_fn function; 652 { 653 struct forklist *ep; 654 655 #ifdef INVARIANTS 656 /* let the programmer know if he's been stupid */ 657 if (rm_at_fork(function)) 658 printf("WARNING: fork callout entry (%p) already present\n", 659 function); 660 #endif 661 ep = malloc(sizeof(*ep), M_ATFORK, M_NOWAIT); 662 if (ep == NULL) 663 return (ENOMEM); 664 ep->function = function; 665 sx_xlock(&fork_list_lock); 666 TAILQ_INSERT_TAIL(&fork_list, ep, next); 667 sx_xunlock(&fork_list_lock); 668 return (0); 669 } 670 671 /* 672 * Scan the exit callout list for the given item and remove it.. 673 * Returns the number of items removed (0 or 1) 674 */ 675 676 int 677 rm_at_fork(function) 678 forklist_fn function; 679 { 680 struct forklist *ep; 681 682 sx_xlock(&fork_list_lock); 683 TAILQ_FOREACH(ep, &fork_list, next) { 684 if (ep->function == function) { 685 TAILQ_REMOVE(&fork_list, ep, next); 686 sx_xunlock(&fork_list_lock); 687 free(ep, M_ATFORK); 688 return(1); 689 } 690 } 691 sx_xunlock(&fork_list_lock); 692 return (0); 693 } 694 695 /* 696 * Handle the return of a child process from fork1(). This function 697 * is called from the MD fork_trampoline() entry point. 698 */ 699 void 700 fork_exit(callout, arg, frame) 701 void (*callout)(void *, struct trapframe *); 702 void *arg; 703 struct trapframe *frame; 704 { 705 struct proc *p; 706 707 p = curproc; 708 709 /* 710 * Setup the sched_lock state so that we can release it. 711 */ 712 sched_lock.mtx_lock = (uintptr_t)p; 713 sched_lock.mtx_recurse = 0; 714 /* 715 * XXX: We really shouldn't have to do this. 716 */ 717 mtx_intr_enable(&sched_lock); 718 mtx_unlock_spin(&sched_lock); 719 720 #ifdef SMP 721 if (PCPU_GET(switchtime.tv_sec) == 0) 722 microuptime(PCPU_PTR(switchtime)); 723 PCPU_SET(switchticks, ticks); 724 #endif 725 726 /* 727 * cpu_set_fork_handler intercepts this function call to 728 * have this call a non-return function to stay in kernel mode. 729 * initproc has its own fork handler, but it does return. 730 */ 731 KASSERT(callout != NULL, ("NULL callout in fork_exit")); 732 callout(arg, frame); 733 734 /* 735 * Check if a kernel thread misbehaved and returned from its main 736 * function. 737 */ 738 PROC_LOCK(p); 739 if (p->p_flag & P_KTHREAD) { 740 PROC_UNLOCK(p); 741 mtx_lock(&Giant); 742 printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n", 743 p->p_comm, p->p_pid); 744 kthread_exit(0); 745 } 746 PROC_UNLOCK(p); 747 mtx_assert(&Giant, MA_NOTOWNED); 748 } 749 750 /* 751 * Simplified back end of syscall(), used when returning from fork() 752 * directly into user mode. Giant is not held on entry, and must not 753 * be held on return. This function is passed in to fork_exit() as the 754 * first parameter and is called when returning to a new userland process. 755 */ 756 void 757 fork_return(p, frame) 758 struct proc *p; 759 struct trapframe *frame; 760 { 761 762 userret(p, frame, 0); 763 #ifdef KTRACE 764 if (KTRPOINT(p, KTR_SYSRET)) { 765 if (!mtx_owned(&Giant)) 766 mtx_lock(&Giant); 767 ktrsysret(p->p_tracep, SYS_fork, 0, 0); 768 } 769 #endif 770 if (mtx_owned(&Giant)) 771 mtx_unlock(&Giant); 772 mtx_assert(&Giant, MA_NOTOWNED); 773 } 774