1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include "opt_ktrace.h" 38 #include "opt_kstack_pages.h" 39 40 #define EXTERR_CATEGORY EXTERR_CAT_FORK 41 #include <sys/systm.h> 42 #include <sys/acct.h> 43 #include <sys/bitstring.h> 44 #include <sys/capsicum.h> 45 #include <sys/eventhandler.h> 46 #include <sys/exterrvar.h> 47 #include <sys/fcntl.h> 48 #include <sys/filedesc.h> 49 #include <sys/jail.h> 50 #include <sys/kernel.h> 51 #include <sys/kthread.h> 52 #include <sys/ktr.h> 53 #include <sys/ktrace.h> 54 #include <sys/sysctl.h> 55 #include <sys/lock.h> 56 #include <sys/malloc.h> 57 #include <sys/msan.h> 58 #include <sys/mutex.h> 59 #include <sys/priv.h> 60 #include <sys/proc.h> 61 #include <sys/procdesc.h> 62 #include <sys/ptrace.h> 63 #include <sys/racct.h> 64 #include <sys/resourcevar.h> 65 #include <sys/sched.h> 66 #include <sys/sdt.h> 67 #include <sys/signalvar.h> 68 #include <sys/sx.h> 69 #include <sys/syscall.h> 70 #include <sys/syscallsubr.h> 71 #include <sys/sysent.h> 72 #include <sys/sysproto.h> 73 #include <sys/vmmeter.h> 74 #include <sys/vnode.h> 75 #include <sys/unistd.h> 76 77 #include <security/audit/audit.h> 78 #include <security/mac/mac_framework.h> 79 80 #include <vm/vm.h> 81 #include <vm/pmap.h> 82 #include <vm/vm_map.h> 83 #include <vm/vm_extern.h> 84 #include <vm/uma.h> 85 86 #ifdef KDTRACE_HOOKS 87 #include <sys/dtrace_bsd.h> 88 dtrace_fork_func_t dtrace_fasttrap_fork; 89 #endif 90 91 SDT_PROVIDER_DECLARE(proc); 92 SDT_PROBE_DEFINE3(proc, , , create, "struct proc *", "struct proc *", "int"); 93 94 static bool pdfork_implicit_nowaitpid; 95 SYSCTL_BOOL(_kern, OID_AUTO, pdfork_implicit_nowaitpid, CTLFLAG_RWTUN, 96 &pdfork_implicit_nowaitpid, 0, 97 "PD_NOWAITPID is assumed to be always set"); 98 99 #ifndef _SYS_SYSPROTO_H_ 100 struct fork_args { 101 int dummy; 102 }; 103 #endif 104 105 /* ARGSUSED */ 106 int 107 sys_fork(struct thread *td, struct fork_args *uap) 108 { 109 struct fork_req fr; 110 int error, pid; 111 112 bzero(&fr, sizeof(fr)); 113 fr.fr_flags = RFFDG | RFPROC; 114 fr.fr_pidp = &pid; 115 error = fork1(td, &fr); 116 if (error == 0) { 117 td->td_retval[0] = pid; 118 td->td_retval[1] = 0; 119 } 120 return (error); 121 } 122 123 /* ARGUSED */ 124 int 125 sys_pdfork(struct thread *td, struct pdfork_args *uap) 126 { 127 struct fork_req fr; 128 struct filecaps fcaps; 129 int error, fd, pid; 130 131 bzero(&fr, sizeof(fr)); 132 fr.fr_flags = RFFDG | RFPROC | RFPROCDESC; 133 fr.fr_pidp = &pid; 134 fr.fr_pd_fd = &fd; 135 fr.fr_pd_flags = uap->flags; 136 filecaps_fill(&fcaps); 137 if ((uap->flags & PD_PTRACE_CAP) == 0) 138 cap_rights_clear(&fcaps.fc_rights, CAP_PTRACE); 139 fr.fr_pd_fcaps = &fcaps; 140 AUDIT_ARG_FFLAGS(uap->flags); 141 /* 142 * It is necessary to return fd by reference because 0 is a valid file 143 * descriptor number, and the child needs to be able to distinguish 144 * itself from the parent using the return value. 145 */ 146 error = fork1(td, &fr); 147 if (error == 0) { 148 td->td_retval[0] = pid; 149 td->td_retval[1] = 0; 150 error = copyout(&fd, uap->fdp, sizeof(fd)); 151 } 152 return (error); 153 } 154 155 /* ARGSUSED */ 156 int 157 sys_vfork(struct thread *td, struct vfork_args *uap) 158 { 159 struct fork_req fr; 160 int error, pid; 161 162 bzero(&fr, sizeof(fr)); 163 fr.fr_flags = RFFDG | RFPROC | RFPPWAIT | RFMEM; 164 fr.fr_pidp = &pid; 165 error = fork1(td, &fr); 166 if (error == 0) { 167 td->td_retval[0] = pid; 168 td->td_retval[1] = 0; 169 } 170 return (error); 171 } 172 173 int 174 sys_rfork(struct thread *td, struct rfork_args *uap) 175 { 176 struct fork_req fr; 177 int error, pid; 178 179 /* Don't allow kernel-only flags. */ 180 if ((uap->flags & RFKERNELONLY) != 0) 181 return (EXTERROR(EINVAL, "Kernel-only flags %#jx", uap->flags)); 182 /* RFSPAWN must not appear with others */ 183 if ((uap->flags & RFSPAWN) != 0 && uap->flags != RFSPAWN) 184 return (EXTERROR(EINVAL, "RFSPAWN must be the only flag %#jx", 185 uap->flags)); 186 187 AUDIT_ARG_FFLAGS(uap->flags); 188 bzero(&fr, sizeof(fr)); 189 if ((uap->flags & RFSPAWN) != 0) { 190 fr.fr_flags = RFFDG | RFPROC | RFPPWAIT | RFMEM; 191 fr.fr_flags2 = FR2_DROPSIG_CAUGHT; 192 } else { 193 fr.fr_flags = uap->flags; 194 } 195 fr.fr_pidp = &pid; 196 error = fork1(td, &fr); 197 if (error == 0) { 198 td->td_retval[0] = pid; 199 td->td_retval[1] = 0; 200 } 201 return (error); 202 } 203 204 int 205 sys_pdrfork(struct thread *td, struct pdrfork_args *uap) 206 { 207 struct fork_req fr; 208 struct filecaps fcaps; 209 int error, fd, pid; 210 211 bzero(&fr, sizeof(fr)); 212 fd = -1; 213 214 AUDIT_ARG_FFLAGS(uap->pdflags); 215 AUDIT_ARG_CMD(uap->rfflags); 216 217 if ((uap->rfflags & (RFSTOPPED | RFHIGHPID)) != 0) 218 return (EXTERROR(EINVAL, 219 "Kernel-only flags %#jx", uap->rfflags)); 220 221 /* RFSPAWN must not appear with others */ 222 if ((uap->rfflags & RFSPAWN) != 0) { 223 if (uap->rfflags != RFSPAWN) 224 return (EXTERROR(EINVAL, 225 "RFSPAWN must be the only flag %#jx", 226 uap->rfflags)); 227 fr.fr_flags = RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPROCDESC; 228 fr.fr_flags2 = FR2_DROPSIG_CAUGHT; 229 } else { 230 if ((uap->rfflags & (RFPROC | RFPROCDESC)) != 231 (RFPROC | RFPROCDESC)) { 232 return (EXTERROR(EINVAL, 233 "RFPROC|RFPROCDESC required %#jx", uap->rfflags)); 234 } 235 fr.fr_flags = uap->rfflags; 236 } 237 238 fr.fr_pidp = &pid; 239 fr.fr_pd_fd = &fd; 240 fr.fr_pd_flags = uap->pdflags; 241 filecaps_fill(&fcaps); 242 if ((uap->pdflags & PD_PTRACE_CAP) == 0) 243 cap_rights_clear(&fcaps.fc_rights, CAP_PTRACE); 244 fr.fr_pd_fcaps = &fcaps; 245 error = fork1(td, &fr); 246 if (error == 0) { 247 td->td_retval[0] = pid; 248 td->td_retval[1] = 0; 249 if ((fr.fr_flags & (RFPROC | RFPROCDESC)) == 250 (RFPROC | RFPROCDESC) || uap->rfflags == RFSPAWN) 251 error = copyout(&fd, uap->fdp, sizeof(fd)); 252 } 253 return (error); 254 } 255 256 int __exclusive_cache_line nprocs = 1; /* process 0 */ 257 int lastpid = 0; 258 SYSCTL_INT(_kern, OID_AUTO, lastpid, CTLFLAG_RD, &lastpid, 0, 259 "Last used PID"); 260 261 /* 262 * Random component to lastpid generation. We mix in a random factor to make 263 * it a little harder to predict. We sanity check the modulus value to avoid 264 * doing it in critical paths. Don't let it be too small or we pointlessly 265 * waste randomness entropy, and don't let it be impossibly large. Using a 266 * modulus that is too big causes a LOT more process table scans and slows 267 * down fork processing as the pidchecked caching is defeated. 268 */ 269 static int randompid = 0; 270 271 static int 272 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) 273 { 274 int error, pid; 275 276 error = sysctl_wire_old_buffer(req, sizeof(int)); 277 if (error != 0) 278 return(error); 279 sx_xlock(&allproc_lock); 280 pid = randompid; 281 error = sysctl_handle_int(oidp, &pid, 0, req); 282 if (error == 0 && req->newptr != NULL) { 283 if (pid == 0) 284 randompid = 0; 285 else if (pid == 1) 286 /* generate a random PID modulus between 100 and 1123 */ 287 randompid = 100 + arc4random() % 1024; 288 else if (pid < 0 || pid > pid_max - 100) 289 /* out of range */ 290 randompid = pid_max - 100; 291 else if (pid < 100) 292 /* Make it reasonable */ 293 randompid = 100; 294 else 295 randompid = pid; 296 } 297 sx_xunlock(&allproc_lock); 298 return (error); 299 } 300 301 SYSCTL_PROC(_kern, OID_AUTO, randompid, 302 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0, 303 sysctl_kern_randompid, "I", 304 "Random PID modulus. Special values: 0: disable, 1: choose random value"); 305 306 extern bitstr_t proc_id_pidmap; 307 extern bitstr_t proc_id_grpidmap; 308 extern bitstr_t proc_id_sessidmap; 309 extern bitstr_t proc_id_reapmap; 310 311 /* 312 * Find an unused process ID 313 * 314 * If RFHIGHPID is set (used during system boot), do not allocate 315 * low-numbered pids. 316 */ 317 static int 318 fork_findpid(int flags) 319 { 320 pid_t result; 321 int trypid, random; 322 323 /* 324 * Avoid calling arc4random with procid_lock held. 325 */ 326 random = 0; 327 if (__predict_false(randompid)) 328 random = arc4random() % randompid; 329 330 mtx_lock(&procid_lock); 331 332 trypid = lastpid + 1; 333 if (flags & RFHIGHPID) { 334 if (trypid < 10) 335 trypid = 10; 336 } else { 337 trypid += random; 338 } 339 retry: 340 if (trypid >= pid_max) 341 trypid = 2; 342 343 bit_ffc_at(&proc_id_pidmap, trypid, pid_max, &result); 344 if (result == -1) { 345 KASSERT(trypid != 2, ("unexpectedly ran out of IDs")); 346 trypid = 2; 347 goto retry; 348 } 349 if (bit_test(&proc_id_grpidmap, result) || 350 bit_test(&proc_id_sessidmap, result) || 351 bit_test(&proc_id_reapmap, result)) { 352 trypid = result + 1; 353 goto retry; 354 } 355 356 /* 357 * RFHIGHPID does not mess with the lastpid counter during boot. 358 */ 359 if ((flags & RFHIGHPID) == 0) 360 lastpid = result; 361 362 bit_set(&proc_id_pidmap, result); 363 mtx_unlock(&procid_lock); 364 365 return (result); 366 } 367 368 static int 369 fork_norfproc(struct thread *td, int flags) 370 { 371 struct proc *p1; 372 int error; 373 374 KASSERT((flags & RFPROC) == 0, 375 ("fork_norfproc called with RFPROC set")); 376 p1 = td->td_proc; 377 378 /* 379 * Quiesce other threads if necessary. If RFMEM is not specified we 380 * must ensure that other threads do not concurrently create a second 381 * process sharing the vmspace, see vmspace_unshare(). 382 */ 383 if ((p1->p_flag & (P_HADTHREADS | P_SYSTEM)) == P_HADTHREADS && 384 ((flags & (RFCFDG | RFFDG)) != 0 || (flags & RFMEM) == 0)) { 385 PROC_LOCK(p1); 386 if (thread_single(p1, SINGLE_BOUNDARY)) { 387 PROC_UNLOCK(p1); 388 return (ERESTART); 389 } 390 PROC_UNLOCK(p1); 391 } 392 393 error = vm_forkproc(td, NULL, NULL, NULL, flags); 394 if (error != 0) 395 goto fail; 396 397 /* 398 * Close all file descriptors. 399 */ 400 if ((flags & RFCFDG) != 0) { 401 struct filedesc *fdtmp; 402 struct pwddesc *pdtmp; 403 404 pdtmp = pdinit(td->td_proc->p_pd, false); 405 fdtmp = fdinit(); 406 pdescfree(td); 407 fdescfree(td); 408 p1->p_fd = fdtmp; 409 p1->p_pd = pdtmp; 410 } 411 412 /* 413 * Unshare file descriptors (from parent). 414 */ 415 if ((flags & RFFDG) != 0) { 416 fdunshare(td); 417 pdunshare(td); 418 } 419 420 fail: 421 if ((p1->p_flag & (P_HADTHREADS | P_SYSTEM)) == P_HADTHREADS && 422 ((flags & (RFCFDG | RFFDG)) != 0 || (flags & RFMEM) == 0)) { 423 PROC_LOCK(p1); 424 thread_single_end(p1, SINGLE_BOUNDARY); 425 PROC_UNLOCK(p1); 426 } 427 return (error); 428 } 429 430 static void 431 do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread *td2, 432 struct vmspace *vm2, struct file *fp_procdesc) 433 { 434 struct proc *p1, *pptr; 435 struct filedesc *fd; 436 struct filedesc_to_leader *fdtol; 437 struct pwddesc *pd; 438 struct sigacts *newsigacts; 439 440 p1 = td->td_proc; 441 442 PROC_LOCK(p1); 443 bcopy(&p1->p_startcopy, &p2->p_startcopy, 444 __rangeof(struct proc, p_startcopy, p_endcopy)); 445 pargs_hold(p2->p_args); 446 PROC_UNLOCK(p1); 447 448 bzero(&p2->p_startzero, 449 __rangeof(struct proc, p_startzero, p_endzero)); 450 451 /* Tell the prison that we exist. */ 452 prison_proc_hold(p2->p_ucred->cr_prison); 453 454 p2->p_state = PRS_NEW; /* protect against others */ 455 p2->p_pid = fork_findpid(fr->fr_flags); 456 AUDIT_ARG_PID(p2->p_pid); 457 TSFORK(p2->p_pid, p1->p_pid); 458 459 sx_xlock(&allproc_lock); 460 LIST_INSERT_HEAD(&allproc, p2, p_list); 461 allproc_gen++; 462 prison_proc_link(p2->p_ucred->cr_prison, p2); 463 sx_xunlock(&allproc_lock); 464 465 sx_xlock(PIDHASHLOCK(p2->p_pid)); 466 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 467 sx_xunlock(PIDHASHLOCK(p2->p_pid)); 468 469 tidhash_add(td2); 470 471 /* 472 * Malloc things while we don't hold any locks. 473 */ 474 if (fr->fr_flags & RFSIGSHARE) 475 newsigacts = NULL; 476 else 477 newsigacts = sigacts_alloc(); 478 479 /* 480 * Copy filedesc. 481 */ 482 if (fr->fr_flags & RFCFDG) { 483 pd = pdinit(p1->p_pd, false); 484 fd = fdinit(); 485 fdtol = NULL; 486 } else if (fr->fr_flags & RFFDG) { 487 if (fr->fr_flags2 & FR2_SHARE_PATHS) 488 pd = pdshare(p1->p_pd); 489 else 490 pd = pdcopy(p1->p_pd); 491 fd = fdcopy(p1->p_fd, p2); 492 fdtol = NULL; 493 } else { 494 if (fr->fr_flags2 & FR2_SHARE_PATHS) 495 pd = pdcopy(p1->p_pd); 496 else 497 pd = pdshare(p1->p_pd); 498 fd = fdshare(p1->p_fd); 499 if (p1->p_fdtol == NULL) 500 p1->p_fdtol = filedesc_to_leader_alloc(NULL, NULL, 501 p1->p_leader); 502 if ((fr->fr_flags & RFTHREAD) != 0) { 503 /* 504 * Shared file descriptor table, and shared 505 * process leaders. 506 */ 507 fdtol = filedesc_to_leader_share(p1->p_fdtol, p1->p_fd); 508 } else { 509 /* 510 * Shared file descriptor table, and different 511 * process leaders. 512 */ 513 fdtol = filedesc_to_leader_alloc(p1->p_fdtol, 514 p1->p_fd, p2); 515 } 516 } 517 /* 518 * Make a proc table entry for the new process. 519 * Start by zeroing the section of proc that is zero-initialized, 520 * then copy the section that is copied directly from the parent. 521 */ 522 523 PROC_LOCK(p2); 524 PROC_LOCK(p1); 525 526 bzero(&td2->td_startzero, 527 __rangeof(struct thread, td_startzero, td_endzero)); 528 529 bcopy(&td->td_startcopy, &td2->td_startcopy, 530 __rangeof(struct thread, td_startcopy, td_endcopy)); 531 532 bcopy(&p2->p_comm, &td2->td_name, sizeof(td2->td_name)); 533 td2->td_sigstk = td->td_sigstk; 534 td2->td_flags = TDF_INMEM; 535 td2->td_lend_user_pri = PRI_MAX; 536 537 #ifdef VIMAGE 538 td2->td_vnet = NULL; 539 td2->td_vnet_lpush = NULL; 540 #endif 541 542 /* 543 * Allow the scheduler to initialize the child. 544 */ 545 thread_lock(td); 546 sched_fork(td, td2); 547 /* 548 * Request AST to check for TDP_RFPPWAIT. Do it here 549 * to avoid calling thread_lock() again. 550 */ 551 if ((fr->fr_flags & RFPPWAIT) != 0) 552 ast_sched_locked(td, TDA_VFORK); 553 thread_unlock(td); 554 555 /* 556 * Duplicate sub-structures as needed. 557 * Increase reference counts on shared objects. 558 */ 559 p2->p_flag = P_INMEM; 560 p2->p_flag2 = p1->p_flag2 & (P2_ASLR_DISABLE | P2_ASLR_ENABLE | 561 P2_ASLR_IGNSTART | P2_NOTRACE | P2_NOTRACE_EXEC | 562 P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE | P2_TRAPCAP | 563 P2_STKGAP_DISABLE | P2_STKGAP_DISABLE_EXEC | P2_NO_NEW_PRIVS | 564 P2_WXORX_DISABLE | P2_WXORX_ENABLE_EXEC | P2_LOGSIGEXIT_CTL | 565 P2_LOGSIGEXIT_ENABLE); 566 if ((fr->fr_flags & RFPROCDESC) != 0) { 567 p2->p_zombieref = PZOMBIEREF_PROCDESC; 568 if (((fr->fr_pd_flags & PD_NOWAITPID) == 0 && 569 !pdfork_implicit_nowaitpid) && 570 (fr->fr_flags & RFNOWAIT) == 0) 571 p2->p_zombieref |= (PZOMBIEREF_PARENT | 572 PZOMBIEREF_NEEDPARENT); 573 } else { 574 p2->p_zombieref = PZOMBIEREF_PARENT | PZOMBIEREF_NEEDPARENT; 575 } 576 p2->p_swtick = ticks; 577 if (p1->p_flag & P_PROFIL) 578 startprofclock(p2); 579 580 if (fr->fr_flags & RFSIGSHARE) { 581 p2->p_sigacts = sigacts_hold(p1->p_sigacts); 582 } else { 583 sigacts_copy(newsigacts, p1->p_sigacts); 584 p2->p_sigacts = newsigacts; 585 if ((fr->fr_flags2 & (FR2_DROPSIG_CAUGHT | FR2_KPROC)) != 0) { 586 mtx_lock(&p2->p_sigacts->ps_mtx); 587 if ((fr->fr_flags2 & FR2_DROPSIG_CAUGHT) != 0) 588 sig_drop_caught(p2); 589 if ((fr->fr_flags2 & FR2_KPROC) != 0) 590 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT; 591 mtx_unlock(&p2->p_sigacts->ps_mtx); 592 } 593 } 594 595 if (fr->fr_flags & RFTSIGZMB) 596 p2->p_sigparent = RFTSIGNUM(fr->fr_flags); 597 else if (fr->fr_flags & RFLINUXTHPN) 598 p2->p_sigparent = SIGUSR1; 599 else 600 p2->p_sigparent = SIGCHLD; 601 602 if ((fr->fr_flags2 & FR2_KPROC) != 0) { 603 p2->p_flag |= P_SYSTEM | P_KPROC; 604 td2->td_pflags |= TDP_KTHREAD; 605 } 606 607 p2->p_textvp = p1->p_textvp; 608 p2->p_textdvp = p1->p_textdvp; 609 p2->p_fd = fd; 610 p2->p_fdtol = fdtol; 611 p2->p_pd = pd; 612 613 if (p1->p_flag2 & P2_INHERIT_PROTECTED) { 614 p2->p_flag |= P_PROTECTED; 615 p2->p_flag2 |= P2_INHERIT_PROTECTED; 616 } 617 618 /* 619 * p_limit is copy-on-write. Bump its refcount. 620 */ 621 lim_fork(p1, p2); 622 623 thread_cow_get_proc(td2, p2); 624 625 pstats_fork(p1->p_stats, p2->p_stats); 626 627 PROC_UNLOCK(p1); 628 PROC_UNLOCK(p2); 629 630 /* 631 * Bump references to the text vnode and directory, and copy 632 * the hardlink name. 633 */ 634 if (p2->p_textvp != NULL) 635 vrefact(p2->p_textvp); 636 if (p2->p_textdvp != NULL) 637 vrefact(p2->p_textdvp); 638 p2->p_binname = p1->p_binname == NULL ? NULL : 639 strdup(p1->p_binname, M_PARGS); 640 641 /* 642 * Set up linkage for kernel based threading. 643 */ 644 if ((fr->fr_flags & RFTHREAD) != 0) { 645 mtx_lock(&ppeers_lock); 646 p2->p_peers = p1->p_peers; 647 p1->p_peers = p2; 648 p2->p_leader = p1->p_leader; 649 mtx_unlock(&ppeers_lock); 650 PROC_LOCK(p1->p_leader); 651 if ((p1->p_leader->p_flag & P_WEXIT) != 0) { 652 PROC_UNLOCK(p1->p_leader); 653 /* 654 * The task leader is exiting, so process p1 is 655 * going to be killed shortly. Since p1 obviously 656 * isn't dead yet, we know that the leader is either 657 * sending SIGKILL's to all the processes in this 658 * task or is sleeping waiting for all the peers to 659 * exit. We let p1 complete the fork, but we need 660 * to go ahead and kill the new process p2 since 661 * the task leader may not get a chance to send 662 * SIGKILL to it. We leave it on the list so that 663 * the task leader will wait for this new process 664 * to commit suicide. 665 */ 666 PROC_LOCK(p2); 667 kern_psignal(p2, SIGKILL); 668 PROC_UNLOCK(p2); 669 } else 670 PROC_UNLOCK(p1->p_leader); 671 } else { 672 p2->p_peers = NULL; 673 p2->p_leader = p2; 674 } 675 676 sx_xlock(&proctree_lock); 677 PGRP_LOCK(p1->p_pgrp); 678 PROC_LOCK(p2); 679 PROC_LOCK(p1); 680 681 /* 682 * Preserve some more flags in subprocess. P_PROFIL has already 683 * been preserved. 684 */ 685 p2->p_flag |= p1->p_flag & P_SUGID; 686 td2->td_pflags |= td->td_pflags & (TDP_ALTSTACK | TDP_SIGFASTBLOCK); 687 td2->td_pflags2 |= td->td_pflags2 & TDP2_UEXTERR; 688 if (p1->p_flag & P_CONTROLT) { 689 SESS_LOCK(p1->p_session); 690 if (p1->p_session->s_ttyvp != NULL) 691 p2->p_flag |= P_CONTROLT; 692 SESS_UNLOCK(p1->p_session); 693 } 694 if (fr->fr_flags & RFPPWAIT) 695 p2->p_flag |= P_PPWAIT; 696 697 p2->p_pgrp = p1->p_pgrp; 698 LIST_INSERT_AFTER(p1, p2, p_pglist); 699 PGRP_UNLOCK(p1->p_pgrp); 700 LIST_INIT(&p2->p_children); 701 LIST_INIT(&p2->p_orphans); 702 703 callout_init_mtx(&p2->p_itcallout, &p2->p_mtx, 0); 704 705 PROC_UNLOCK(p1); 706 707 /* 708 * Attach the new process to its parent. 709 * 710 * If RFNOWAIT is set, the newly created process becomes a child 711 * of init. This effectively disassociates the child from the 712 * parent. 713 */ 714 if ((fr->fr_flags & RFNOWAIT) != 0) { 715 pptr = p1->p_reaper; 716 p2->p_reaper = pptr; 717 } else { 718 p2->p_reaper = (p1->p_treeflag & P_TREE_REAPER) != 0 ? 719 p1 : p1->p_reaper; 720 pptr = p1; 721 } 722 p2->p_pptr = pptr; 723 p2->p_oppid = pptr->p_pid; 724 LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling); 725 LIST_INIT(&p2->p_reaplist); 726 LIST_INSERT_HEAD(&p2->p_reaper->p_reaplist, p2, p_reapsibling); 727 if (p2->p_reaper == p1 && p1 != initproc) { 728 p2->p_reapsubtree = p2->p_pid; 729 proc_id_set_cond(PROC_ID_REAP, p2->p_pid); 730 } else { 731 /* 732 * Explicitly copy this field under the proctree lock, as it 733 * might have changed since the bulk copying of the parent's 734 * fields. 735 */ 736 p2->p_reapsubtree = p1->p_reapsubtree; 737 } 738 sx_xunlock(&proctree_lock); 739 740 /* Inform accounting that we have forked. */ 741 p2->p_acflag = AFORK; 742 PROC_UNLOCK(p2); 743 744 #ifdef KTRACE 745 ktrprocfork(p1, p2); 746 #endif 747 748 /* 749 * Finish creating the child process. It will return via a different 750 * execution path later. (ie: directly into user mode) 751 */ 752 vm_forkproc(td, p2, td2, vm2, fr->fr_flags); 753 754 if (fr->fr_flags == (RFFDG | RFPROC)) { 755 VM_CNT_INC(v_forks); 756 VM_CNT_ADD(v_forkpages, p2->p_vmspace->vm_dsize + 757 p2->p_vmspace->vm_ssize); 758 } else if (fr->fr_flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) { 759 VM_CNT_INC(v_vforks); 760 VM_CNT_ADD(v_vforkpages, p2->p_vmspace->vm_dsize + 761 p2->p_vmspace->vm_ssize); 762 } else if (p1 == &proc0) { 763 VM_CNT_INC(v_kthreads); 764 VM_CNT_ADD(v_kthreadpages, p2->p_vmspace->vm_dsize + 765 p2->p_vmspace->vm_ssize); 766 } else { 767 VM_CNT_INC(v_rforks); 768 VM_CNT_ADD(v_rforkpages, p2->p_vmspace->vm_dsize + 769 p2->p_vmspace->vm_ssize); 770 } 771 772 /* 773 * Associate the process descriptor with the process before anything 774 * can happen that might cause that process to need the descriptor. 775 * However, don't do this until after fork(2) can no longer fail. 776 */ 777 if (fr->fr_flags & RFPROCDESC) 778 procdesc_new(p2, fr->fr_pd_flags); 779 780 /* 781 * Both processes are set up, now check if any loadable modules want 782 * to adjust anything. 783 */ 784 EVENTHANDLER_DIRECT_INVOKE(process_fork, p1, p2, fr->fr_flags); 785 786 /* 787 * Set the child start time and mark the process as being complete. 788 */ 789 PROC_LOCK(p2); 790 PROC_LOCK(p1); 791 microuptime(&p2->p_stats->p_start); 792 PROC_SLOCK(p2); 793 p2->p_state = PRS_NORMAL; 794 PROC_SUNLOCK(p2); 795 796 #ifdef KDTRACE_HOOKS 797 /* 798 * Tell the DTrace fasttrap provider about the new process so that any 799 * tracepoints inherited from the parent can be removed. We have to do 800 * this only after p_state is PRS_NORMAL since the fasttrap module will 801 * use pfind() later on. 802 */ 803 if ((fr->fr_flags & RFMEM) == 0 && dtrace_fasttrap_fork) 804 dtrace_fasttrap_fork(p1, p2); 805 #endif 806 if (fr->fr_flags & RFPPWAIT) { 807 td->td_pflags |= TDP_RFPPWAIT; 808 td->td_rfppwait_p = p2; 809 td->td_dbgflags |= TDB_VFORK; 810 } 811 PROC_UNLOCK(p2); 812 813 /* 814 * Tell any interested parties about the new process. 815 */ 816 knote_fork(p1->p_klist, p2->p_pid); 817 818 PROC_UNLOCK(p1); 819 SDT_PROBE3(proc, , , create, p2, p1, fr->fr_flags); 820 821 if (fr->fr_flags & RFPROCDESC) { 822 procdesc_finit(p2->p_procdesc, fp_procdesc); 823 fdrop(fp_procdesc, td); 824 } 825 826 /* 827 * Speculative check for PTRACE_FORK. PTRACE_FORK is not 828 * synced with forks in progress so it is OK if we miss it 829 * if being set atm. 830 */ 831 if ((p1->p_ptevents & PTRACE_FORK) != 0) { 832 sx_xlock(&proctree_lock); 833 PROC_LOCK(p2); 834 835 /* 836 * p1->p_ptevents & p1->p_pptr are protected by both 837 * process and proctree locks for modifications, 838 * so owning proctree_lock allows the race-free read. 839 */ 840 if ((p1->p_ptevents & PTRACE_FORK) != 0) { 841 /* 842 * Arrange for debugger to receive the fork event. 843 * 844 * We can report PL_FLAG_FORKED regardless of 845 * P_FOLLOWFORK settings, but it does not make a sense 846 * for runaway child. 847 */ 848 td->td_dbgflags |= TDB_FORK; 849 td->td_dbg_forked = p2->p_pid; 850 td2->td_dbgflags |= TDB_STOPATFORK; 851 proc_set_traced(p2, true); 852 CTR2(KTR_PTRACE, 853 "do_fork: attaching to new child pid %d: oppid %d", 854 p2->p_pid, p2->p_oppid); 855 proc_reparent(p2, p1->p_pptr, false); 856 } 857 PROC_UNLOCK(p2); 858 sx_xunlock(&proctree_lock); 859 } 860 861 /* 862 * Activate procdesc NOTE_FORK after we attached the debugger 863 * to the child. This guarantees that a debugger which does 864 * kevent() on the process descriptor to get notifications of 865 * fork events, can properly observe the child right after the 866 * notification fired. 867 */ 868 procdesc_fork(p1, p2->p_pid); 869 870 racct_proc_fork_done(p2); 871 872 if ((fr->fr_flags & RFSTOPPED) == 0) { 873 if (fr->fr_pidp != NULL) 874 *fr->fr_pidp = p2->p_pid; 875 /* 876 * If RFSTOPPED not requested, make child runnable and 877 * add to run queue. 878 */ 879 thread_lock(td2); 880 TD_SET_CAN_RUN(td2); 881 sched_add(td2, SRQ_BORING); 882 } else { 883 *fr->fr_procp = p2; 884 } 885 } 886 887 static void 888 ast_vfork(struct thread *td, int tda __unused) 889 { 890 struct proc *p, *p2; 891 892 MPASS(td->td_pflags & TDP_RFPPWAIT); 893 894 p = td->td_proc; 895 /* 896 * Preserve synchronization semantics of vfork. If 897 * waiting for child to exec or exit, fork set 898 * P_PPWAIT on child, and there we sleep on our proc 899 * (in case of exit). 900 * 901 * Do it after the ptracestop() above is finished, to 902 * not block our debugger until child execs or exits 903 * to finish vfork wait. 904 */ 905 td->td_pflags &= ~TDP_RFPPWAIT; 906 p2 = td->td_rfppwait_p; 907 again: 908 PROC_LOCK(p2); 909 while (p2->p_flag & P_PPWAIT) { 910 PROC_LOCK(p); 911 if (thread_suspend_check_needed()) { 912 PROC_UNLOCK(p2); 913 thread_suspend_check(0); 914 PROC_UNLOCK(p); 915 goto again; 916 } else { 917 PROC_UNLOCK(p); 918 } 919 cv_timedwait(&p2->p_pwait, &p2->p_mtx, hz); 920 } 921 PROC_UNLOCK(p2); 922 923 if (td->td_dbgflags & TDB_VFORK) { 924 PROC_LOCK(p); 925 if (p->p_ptevents & PTRACE_VFORK) 926 ptracestop(td, SIGTRAP, NULL); 927 td->td_dbgflags &= ~TDB_VFORK; 928 PROC_UNLOCK(p); 929 } 930 } 931 932 int 933 fork1(struct thread *td, struct fork_req *fr) 934 { 935 struct proc *p1, *newproc; 936 struct thread *td2; 937 struct vmspace *vm2; 938 struct ucred *cred; 939 struct file *fp_procdesc; 940 struct pgrp *pg; 941 vm_ooffset_t mem_charged; 942 int error, nprocs_new; 943 static int curfail; 944 static struct timeval lastfail; 945 int flags, pages; 946 bool killsx_locked, singlethreaded; 947 948 flags = fr->fr_flags; 949 pages = fr->fr_pages; 950 951 if ((flags & RFSTOPPED) != 0) 952 MPASS(fr->fr_procp != NULL && fr->fr_pidp == NULL); 953 else 954 MPASS(fr->fr_procp == NULL); 955 956 if ((flags & ~(RFFLAGS | RFTSIGFLAGS(RFTSIGMASK))) != 0) 957 return (EXTERROR(EINVAL, 958 "Undef or unimplemented flags %#jx", flags)); 959 960 if ((flags & RFTSIGFLAGS(RFTSIGMASK)) != 0 && (flags & RFTSIGZMB) == 0) 961 return (EXTERROR(EINVAL, 962 "Signal value requires RFTSIGZMB", flags)); 963 964 if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) 965 return (EXTERROR(EINVAL, "Can not copy and clear")); 966 967 if ((flags & RFTSIGZMB) != 0 && (u_int)RFTSIGNUM(flags) > _SIG_MAXSIG) 968 return (EXTERROR(EINVAL, "Invalid signal", RFTSIGNUM(flags))); 969 970 if ((flags & RFPROCDESC) != 0) { 971 if ((flags & RFPROC) == 0) 972 return (EXTERROR(EINVAL, 973 "Can not not create a process yet get a process descriptor")); 974 975 if (fr->fr_pd_fd == NULL) 976 return (EXTERROR(EINVAL, 977 "Must provide a place to put a procdesc if creating one")); 978 979 if ((fr->fr_pd_flags & ~PD_ALLOWED_AT_FORK) != 0) 980 return (EXTERROR(EINVAL, 981 "Invalid pdflags at fork %#jx", fr->fr_pd_flags)); 982 } 983 984 p1 = td->td_proc; 985 986 /* 987 * Here we don't create a new process, but we divorce 988 * certain parts of a process from itself. 989 */ 990 if ((flags & RFPROC) == 0) { 991 if (fr->fr_procp != NULL) 992 *fr->fr_procp = NULL; 993 else if (fr->fr_pidp != NULL) 994 *fr->fr_pidp = 0; 995 return (fork_norfproc(td, flags)); 996 } 997 998 fp_procdesc = NULL; 999 newproc = NULL; 1000 vm2 = NULL; 1001 killsx_locked = false; 1002 singlethreaded = false; 1003 1004 /* 1005 * Increment the nprocs resource before allocations occur. 1006 * Although process entries are dynamically created, we still 1007 * keep a global limit on the maximum number we will 1008 * create. There are hard-limits as to the number of processes 1009 * that can run, established by the KVA and memory usage for 1010 * the process data. 1011 * 1012 * Don't allow a nonprivileged user to use the last ten 1013 * processes; don't let root exceed the limit. 1014 */ 1015 nprocs_new = atomic_fetchadd_int(&nprocs, 1) + 1; 1016 if (nprocs_new >= maxproc - 10) { 1017 if (priv_check_cred(td->td_ucred, PRIV_MAXPROC) != 0 || 1018 nprocs_new >= maxproc) { 1019 error = EAGAIN; 1020 sx_xlock(&allproc_lock); 1021 if (ppsratecheck(&lastfail, &curfail, 1)) { 1022 printf("maxproc limit exceeded by uid %u " 1023 "(pid %d); see tuning(7) and " 1024 "login.conf(5)\n", 1025 td->td_ucred->cr_ruid, p1->p_pid); 1026 } 1027 sx_xunlock(&allproc_lock); 1028 goto fail2; 1029 } 1030 } 1031 1032 /* 1033 * If we are possibly multi-threaded, and there is a process 1034 * sending a signal to our group right now, ensure that our 1035 * other threads cannot be chosen for the signal queueing. 1036 * Otherwise, this might delay signal action, and make the new 1037 * child escape the signaling. 1038 */ 1039 pg = p1->p_pgrp; 1040 if (p1->p_numthreads > 1) { 1041 if (sx_try_slock(&pg->pg_killsx) != 0) { 1042 killsx_locked = true; 1043 } else { 1044 PROC_LOCK(p1); 1045 if (thread_single(p1, SINGLE_BOUNDARY)) { 1046 PROC_UNLOCK(p1); 1047 error = ERESTART; 1048 goto fail2; 1049 } 1050 PROC_UNLOCK(p1); 1051 singlethreaded = true; 1052 } 1053 } 1054 1055 /* 1056 * Atomically check for signals and block processes from sending 1057 * a signal to our process group until the child is visible. 1058 */ 1059 if (!killsx_locked && sx_slock_sig(&pg->pg_killsx) != 0) { 1060 error = ERESTART; 1061 goto fail2; 1062 } 1063 if (__predict_false(p1->p_pgrp != pg || sig_intr() != 0)) { 1064 /* 1065 * Either the process was moved to other process 1066 * group, or there is pending signal. sx_slock_sig() 1067 * does not check for signals if not sleeping for the 1068 * lock. 1069 */ 1070 sx_sunlock(&pg->pg_killsx); 1071 killsx_locked = false; 1072 error = ERESTART; 1073 goto fail2; 1074 } else { 1075 killsx_locked = true; 1076 } 1077 1078 /* 1079 * If required, create a process descriptor in the parent first; we 1080 * will abandon it if something goes wrong. We don't finit() until 1081 * later. 1082 */ 1083 if (flags & RFPROCDESC) { 1084 error = procdesc_falloc(td, &fp_procdesc, fr->fr_pd_fd, 1085 fr->fr_pd_flags, fr->fr_pd_fcaps); 1086 if (error != 0) 1087 goto fail2; 1088 fr->fr_pd_fcaps = NULL; 1089 AUDIT_ARG_FD(*fr->fr_pd_fd); 1090 } 1091 1092 mem_charged = 0; 1093 if (pages == 0) 1094 pages = kstack_pages; 1095 /* Allocate new proc. */ 1096 newproc = uma_zalloc(proc_zone, M_WAITOK); 1097 PROC_TREE_REF(newproc); 1098 td2 = FIRST_THREAD_IN_PROC(newproc); 1099 if (td2 == NULL) { 1100 td2 = thread_alloc(pages); 1101 if (td2 == NULL) { 1102 error = ENOMEM; 1103 goto fail2; 1104 } 1105 proc_linkup(newproc, td2); 1106 } else { 1107 error = thread_recycle(td2, pages); 1108 if (error != 0) 1109 goto fail2; 1110 } 1111 1112 if ((flags & RFMEM) == 0) { 1113 vm2 = vmspace_fork(p1->p_vmspace, &mem_charged); 1114 if (vm2 == NULL) { 1115 error = ENOMEM; 1116 goto fail2; 1117 } 1118 if (!swap_reserve(mem_charged)) { 1119 /* 1120 * The swap reservation failed. The accounting 1121 * from the entries of the copied vm2 will be 1122 * subtracted in vmspace_free(), so force the 1123 * reservation there. 1124 */ 1125 swap_reserve_force(mem_charged); 1126 error = ENOMEM; 1127 goto fail2; 1128 } 1129 } else 1130 vm2 = NULL; 1131 1132 /* 1133 * XXX: This is ugly; when we copy resource usage, we need to bump 1134 * per-cred resource counters. 1135 */ 1136 newproc->p_ucred = crcowget(td->td_ucred); 1137 1138 /* 1139 * Initialize resource accounting for the child process. 1140 */ 1141 error = racct_proc_fork(p1, newproc); 1142 if (error != 0) { 1143 error = EAGAIN; 1144 goto fail1; 1145 } 1146 1147 #ifdef MAC 1148 mac_proc_init(newproc); 1149 #endif 1150 1151 /* 1152 * Increment the count of procs running with this uid. Don't allow 1153 * a nonprivileged user to exceed their current limit. 1154 */ 1155 cred = td->td_ucred; 1156 if (!chgproccnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPROC))) { 1157 if (priv_check_cred(cred, PRIV_PROC_LIMIT) != 0) 1158 goto fail0; 1159 chgproccnt(cred->cr_ruidinfo, 1, 0); 1160 } 1161 1162 newproc->p_klist = knlist_alloc(&newproc->p_mtx); 1163 1164 do_fork(td, fr, newproc, td2, vm2, fp_procdesc); 1165 error = 0; 1166 goto cleanup; 1167 fail0: 1168 error = EAGAIN; 1169 #ifdef MAC 1170 mac_proc_destroy(newproc); 1171 #endif 1172 racct_proc_exit(newproc); 1173 fail1: 1174 proc_unset_cred(newproc, false); 1175 fail2: 1176 if (vm2 != NULL) 1177 vmspace_free(vm2); 1178 if (newproc != NULL) 1179 PROC_TREE_UNREF(newproc); 1180 if ((flags & RFPROCDESC) != 0 && fp_procdesc != NULL) { 1181 fdclose(td, fp_procdesc, *fr->fr_pd_fd); 1182 fdrop(fp_procdesc, td); 1183 } 1184 if (fr->fr_pd_fcaps != NULL) 1185 filecaps_free(fr->fr_pd_fcaps); 1186 atomic_add_int(&nprocs, -1); 1187 cleanup: 1188 if (killsx_locked) 1189 sx_sunlock(&pg->pg_killsx); 1190 if (singlethreaded) { 1191 PROC_LOCK(p1); 1192 thread_single_end(p1, SINGLE_BOUNDARY); 1193 PROC_UNLOCK(p1); 1194 } 1195 if (error != 0) 1196 pause("fork", hz / 2); 1197 return (error); 1198 } 1199 1200 /* 1201 * Handle the return of a child process from fork1(). This function 1202 * is called from the MD fork_trampoline() entry point. 1203 */ 1204 void 1205 fork_exit(void (*callout)(void *, struct trapframe *), void *arg, 1206 struct trapframe *frame) 1207 { 1208 struct proc *p; 1209 struct thread *td; 1210 struct thread *dtd; 1211 1212 kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED); 1213 1214 td = curthread; 1215 p = td->td_proc; 1216 KASSERT(p->p_state == PRS_NORMAL, ("executing process is still new")); 1217 1218 CTR4(KTR_PROC, "fork_exit: new thread %p (td_sched %p, pid %d, %s)", 1219 td, td_get_sched(td), p->p_pid, td->td_name); 1220 1221 sched_fork_exit(td); 1222 1223 /* 1224 * Processes normally resume in mi_switch() after being 1225 * cpu_switch()'ed to, but when children start up they arrive here 1226 * instead, so we must do much the same things as mi_switch() would. 1227 */ 1228 if ((dtd = PCPU_GET(deadthread))) { 1229 PCPU_SET(deadthread, NULL); 1230 thread_stash(dtd); 1231 } 1232 thread_unlock(td); 1233 1234 /* 1235 * cpu_fork_kthread_handler intercepts this function call to 1236 * have this call a non-return function to stay in kernel mode. 1237 * initproc has its own fork handler, but it does return. 1238 */ 1239 KASSERT(callout != NULL, ("NULL callout in fork_exit")); 1240 callout(arg, frame); 1241 1242 /* 1243 * Check if a kernel thread misbehaved and returned from its main 1244 * function. 1245 */ 1246 if (p->p_flag & P_KPROC) { 1247 printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n", 1248 td->td_name, p->p_pid); 1249 kthread_exit(); 1250 } 1251 mtx_assert(&Giant, MA_NOTOWNED); 1252 1253 /* 1254 * Now going to return to userland. 1255 */ 1256 1257 if (p->p_sysent->sv_schedtail != NULL) 1258 (p->p_sysent->sv_schedtail)(td); 1259 1260 userret(td, frame); 1261 } 1262 1263 /* 1264 * Simplified back end of syscall(), used when returning from fork() 1265 * directly into user mode. This function is passed in to fork_exit() 1266 * as the first parameter and is called when returning to a new 1267 * userland process. 1268 */ 1269 void 1270 fork_return(struct thread *td, struct trapframe *frame) 1271 { 1272 struct proc *p; 1273 1274 p = td->td_proc; 1275 if (td->td_dbgflags & TDB_STOPATFORK) { 1276 PROC_LOCK(p); 1277 if ((p->p_flag & P_TRACED) != 0) { 1278 /* 1279 * Inform the debugger if one is still present. 1280 */ 1281 td->td_dbgflags |= TDB_CHILD | TDB_SCX | TDB_FSTP; 1282 ptracestop(td, SIGSTOP, NULL); 1283 td->td_dbgflags &= ~(TDB_CHILD | TDB_SCX); 1284 } else { 1285 /* 1286 * ... otherwise clear the request. 1287 */ 1288 td->td_dbgflags &= ~TDB_STOPATFORK; 1289 } 1290 PROC_UNLOCK(p); 1291 } else if (p->p_flag & P_TRACED) { 1292 /* 1293 * This is the start of a new thread in a traced 1294 * process. Report a system call exit event. 1295 */ 1296 PROC_LOCK(p); 1297 td->td_dbgflags |= TDB_SCX; 1298 if ((p->p_ptevents & PTRACE_SCX) != 0 || 1299 (td->td_dbgflags & TDB_BORN) != 0) 1300 ptracestop(td, SIGTRAP, NULL); 1301 td->td_dbgflags &= ~(TDB_SCX | TDB_BORN); 1302 PROC_UNLOCK(p); 1303 } 1304 1305 /* 1306 * If the prison was killed mid-fork, die along with it. 1307 */ 1308 if (!prison_isalive(td->td_ucred->cr_prison)) 1309 kern_exit(td, 0, SIGKILL); 1310 1311 #ifdef KTRACE 1312 if (KTRPOINT(td, KTR_SYSRET)) 1313 ktrsysret(td->td_sa.code, 0, 0); 1314 #endif 1315 } 1316 1317 static void 1318 fork_init(void *arg __unused) 1319 { 1320 ast_register(TDA_VFORK, ASTR_ASTF_REQUIRED | ASTR_TDP, TDP_RFPPWAIT, 1321 ast_vfork); 1322 } 1323 SYSINIT(fork, SI_SUB_INTRINSIC, SI_ORDER_ANY, fork_init, NULL); 1324