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