1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/sysmacros.h> 36 #include <sys/signal.h> 37 #include <sys/cred.h> 38 #include <sys/policy.h> 39 #include <sys/user.h> 40 #include <sys/systm.h> 41 #include <sys/cpuvar.h> 42 #include <sys/vfs.h> 43 #include <sys/vnode.h> 44 #include <sys/file.h> 45 #include <sys/errno.h> 46 #include <sys/time.h> 47 #include <sys/proc.h> 48 #include <sys/cmn_err.h> 49 #include <sys/acct.h> 50 #include <sys/tuneable.h> 51 #include <sys/class.h> 52 #include <sys/kmem.h> 53 #include <sys/session.h> 54 #include <sys/ucontext.h> 55 #include <sys/stack.h> 56 #include <sys/procfs.h> 57 #include <sys/prsystm.h> 58 #include <sys/vmsystm.h> 59 #include <sys/vtrace.h> 60 #include <sys/debug.h> 61 #include <sys/shm_impl.h> 62 #include <sys/door_data.h> 63 #include <vm/as.h> 64 #include <vm/rm.h> 65 #include <c2/audit.h> 66 #include <sys/var.h> 67 #include <sys/schedctl.h> 68 #include <sys/utrap.h> 69 #include <sys/task.h> 70 #include <sys/resource.h> 71 #include <sys/cyclic.h> 72 #include <sys/lgrp.h> 73 #include <sys/rctl.h> 74 #include <sys/contract_impl.h> 75 #include <sys/contract/process_impl.h> 76 #include <sys/list.h> 77 #include <sys/dtrace.h> 78 #include <sys/pool.h> 79 #include <sys/zone.h> 80 #include <sys/sdt.h> 81 #include <sys/class.h> 82 #include <sys/corectl.h> 83 #include <sys/brand.h> 84 85 static int64_t cfork(int, int); 86 static int getproc(proc_t **, int); 87 static void fork_fail(proc_t *); 88 static void forklwp_fail(proc_t *); 89 90 int fork_fail_pending; 91 92 extern struct kmem_cache *process_cache; 93 94 /* 95 * forkall system call. 96 */ 97 int64_t 98 forkall(void) 99 { 100 return (cfork(0, 0)); 101 } 102 103 /* 104 * The parent is stopped until the child invokes relvm(). 105 */ 106 int64_t 107 vfork(void) 108 { 109 curthread->t_post_sys = 1; /* so vfwait() will be called */ 110 return (cfork(1, 1)); 111 } 112 113 /* 114 * fork1 system call 115 */ 116 int64_t 117 fork1(void) 118 { 119 return (cfork(0, 1)); 120 } 121 122 /* ARGSUSED */ 123 static int64_t 124 cfork(int isvfork, int isfork1) 125 { 126 proc_t *p = ttoproc(curthread); 127 struct as *as; 128 proc_t *cp, **orphpp; 129 klwp_t *clone; 130 kthread_t *t; 131 task_t *tk; 132 rval_t r; 133 int error; 134 int i; 135 rctl_set_t *dup_set; 136 rctl_alloc_gp_t *dup_gp; 137 rctl_entity_p_t e; 138 lwpdir_t *ldp; 139 lwpent_t *lep; 140 lwpent_t *clep; 141 142 /* 143 * fork is not supported for the /proc agent lwp. 144 */ 145 if (curthread == p->p_agenttp) { 146 error = ENOTSUP; 147 goto forkerr; 148 } 149 150 if ((error = secpolicy_basic_fork(CRED())) != 0) 151 goto forkerr; 152 153 /* 154 * If the calling lwp is doing a fork1() then the 155 * other lwps in this process are not duplicated and 156 * don't need to be held where their kernel stacks can be 157 * cloned. If doing forkall(), the process is held with 158 * SHOLDFORK, so that the lwps are at a point where their 159 * stacks can be copied which is on entry or exit from 160 * the kernel. 161 */ 162 if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) { 163 aston(curthread); 164 error = EINTR; 165 goto forkerr; 166 } 167 168 #if defined(__sparc) 169 /* 170 * Ensure that the user stack is fully constructed 171 * before creating the child process structure. 172 */ 173 (void) flush_user_windows_to_stack(NULL); 174 #endif 175 176 mutex_enter(&p->p_lock); 177 /* 178 * If this is vfork(), cancel any suspend request we might 179 * have gotten from some other thread via lwp_suspend(). 180 * Otherwise we could end up with a deadlock on return 181 * from the vfork() in both the parent and the child. 182 */ 183 if (isvfork) 184 curthread->t_proc_flag &= ~TP_HOLDLWP; 185 /* 186 * Prevent our resource set associations from being changed during fork. 187 */ 188 pool_barrier_enter(); 189 mutex_exit(&p->p_lock); 190 191 /* 192 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes. 193 */ 194 if (getproc(&cp, 0) < 0) { 195 mutex_enter(&p->p_lock); 196 pool_barrier_exit(); 197 continuelwps(p); 198 mutex_exit(&p->p_lock); 199 error = EAGAIN; 200 goto forkerr; 201 } 202 203 TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p); 204 205 /* 206 * Assign an address space to child 207 */ 208 if (isvfork) { 209 /* 210 * Clear any watched areas and remember the 211 * watched pages for restoring in vfwait(). 212 */ 213 as = p->p_as; 214 if (avl_numnodes(&as->a_wpage) != 0) { 215 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 216 as_clearwatch(as); 217 p->p_wpage = as->a_wpage; 218 avl_create(&as->a_wpage, wp_compare, 219 sizeof (struct watched_page), 220 offsetof(struct watched_page, wp_link)); 221 AS_LOCK_EXIT(as, &as->a_lock); 222 } 223 cp->p_as = as; 224 cp->p_flag |= SVFORK; 225 } else { 226 /* 227 * We need to hold P_PR_LOCK until the address space has 228 * been duplicated and we've had a chance to remove from the 229 * child any DTrace probes that were in the parent. Holding 230 * P_PR_LOCK prevents any new probes from being added and any 231 * extant probes from being removed. 232 */ 233 mutex_enter(&p->p_lock); 234 sprlock_proc(p); 235 p->p_flag |= SFORKING; 236 mutex_exit(&p->p_lock); 237 238 error = as_dup(p->p_as, &cp->p_as); 239 if (error != 0) { 240 fork_fail(cp); 241 mutex_enter(&pidlock); 242 orphpp = &p->p_orphan; 243 while (*orphpp != cp) 244 orphpp = &(*orphpp)->p_nextorph; 245 *orphpp = cp->p_nextorph; 246 if (p->p_child == cp) 247 p->p_child = cp->p_sibling; 248 if (cp->p_sibling) 249 cp->p_sibling->p_psibling = cp->p_psibling; 250 if (cp->p_psibling) 251 cp->p_psibling->p_sibling = cp->p_sibling; 252 mutex_enter(&cp->p_lock); 253 tk = cp->p_task; 254 task_detach(cp); 255 ASSERT(cp->p_pool->pool_ref > 0); 256 atomic_add_32(&cp->p_pool->pool_ref, -1); 257 mutex_exit(&cp->p_lock); 258 pid_exit(cp); 259 mutex_exit(&pidlock); 260 task_rele(tk); 261 262 mutex_enter(&p->p_lock); 263 p->p_flag &= ~SFORKING; 264 pool_barrier_exit(); 265 continuelwps(p); 266 sprunlock(p); 267 /* 268 * Preserve ENOMEM error condition but 269 * map all others to EAGAIN. 270 */ 271 error = (error == ENOMEM) ? ENOMEM : EAGAIN; 272 goto forkerr; 273 } 274 cp->p_as->a_proc = cp; 275 276 /* Duplicate parent's shared memory */ 277 if (p->p_segacct) 278 shmfork(p, cp); 279 280 /* 281 * Remove all DTrace tracepoints from the child process. We 282 * need to do this _before_ duplicating USDT providers since 283 * any associated probes may be immediately enabled. 284 */ 285 if (p->p_dtrace_count > 0) 286 dtrace_fasttrap_fork(p, cp); 287 288 /* 289 * Duplicate any helper actions and providers. The SFORKING 290 * we set above informs the code to enable USDT probes that 291 * sprlock() may fail because the child is being forked. 292 */ 293 if (p->p_dtrace_helpers != NULL) { 294 ASSERT(dtrace_helpers_fork != NULL); 295 (*dtrace_helpers_fork)(p, cp); 296 } 297 298 mutex_enter(&p->p_lock); 299 p->p_flag &= ~SFORKING; 300 sprunlock(p); 301 } 302 303 /* 304 * Duplicate parent's resource controls. 305 */ 306 dup_set = rctl_set_create(); 307 for (;;) { 308 dup_gp = rctl_set_dup_prealloc(p->p_rctls); 309 mutex_enter(&p->p_rctls->rcs_lock); 310 if (rctl_set_dup_ready(p->p_rctls, dup_gp)) 311 break; 312 mutex_exit(&p->p_rctls->rcs_lock); 313 rctl_prealloc_destroy(dup_gp); 314 } 315 e.rcep_p.proc = cp; 316 e.rcep_t = RCENTITY_PROCESS; 317 cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp, 318 RCD_DUP | RCD_CALLBACK); 319 mutex_exit(&p->p_rctls->rcs_lock); 320 321 rctl_prealloc_destroy(dup_gp); 322 323 /* 324 * Allocate the child's lwp directory and lwpid hash table. 325 */ 326 if (isfork1) 327 cp->p_lwpdir_sz = 2; 328 else 329 cp->p_lwpdir_sz = p->p_lwpdir_sz; 330 cp->p_lwpdir = cp->p_lwpfree = ldp = 331 kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP); 332 for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++) 333 ldp->ld_next = ldp + 1; 334 cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2; 335 cp->p_tidhash = 336 kmem_zalloc(cp->p_tidhash_sz * sizeof (lwpdir_t *), KM_SLEEP); 337 338 /* 339 * Duplicate parent's lwps. 340 * Mutual exclusion is not needed because the process is 341 * in the hold state and only the current lwp is running. 342 */ 343 klgrpset_clear(cp->p_lgrpset); 344 if (isfork1) { 345 clone = forklwp(ttolwp(curthread), cp, curthread->t_tid); 346 if (clone == NULL) 347 goto forklwperr; 348 /* 349 * Inherit only the lwp_wait()able flag, 350 * Daemon threads should not call fork1(), but oh well... 351 */ 352 lwptot(clone)->t_proc_flag |= 353 (curthread->t_proc_flag & TP_TWAIT); 354 } else { 355 /* this is forkall(), no one can be in lwp_wait() */ 356 ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0); 357 /* for each entry in the parent's lwp directory... */ 358 for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) { 359 klwp_t *clwp; 360 kthread_t *ct; 361 362 if ((lep = ldp->ld_entry) == NULL) 363 continue; 364 365 if ((t = lep->le_thread) != NULL) { 366 clwp = forklwp(ttolwp(t), cp, t->t_tid); 367 if (clwp == NULL) 368 goto forklwperr; 369 ct = lwptot(clwp); 370 /* 371 * Inherit lwp_wait()able and daemon flags. 372 */ 373 ct->t_proc_flag |= 374 (t->t_proc_flag & (TP_TWAIT|TP_DAEMON)); 375 /* 376 * Keep track of the clone of curthread to 377 * post return values through lwp_setrval(). 378 * Mark other threads for special treatment 379 * by lwp_rtt() / post_syscall(). 380 */ 381 if (t == curthread) 382 clone = clwp; 383 else 384 ct->t_flag |= T_FORKALL; 385 } else { 386 /* 387 * Replicate zombie lwps in the child. 388 */ 389 clep = kmem_zalloc(sizeof (*clep), KM_SLEEP); 390 clep->le_lwpid = lep->le_lwpid; 391 clep->le_start = lep->le_start; 392 lwp_hash_in(cp, clep); 393 } 394 } 395 } 396 397 /* 398 * Put new process in the parent's process contract, or put it 399 * in a new one if there is an active process template. Send a 400 * fork event (if requested) to whatever contract the child is 401 * a member of. Fails if the parent has been SIGKILLed. 402 */ 403 if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL) 404 goto forklwperr; 405 406 /* 407 * No fork failures occur beyond this point. 408 */ 409 410 cp->p_lwpid = p->p_lwpid; 411 if (!isfork1) { 412 cp->p_lwpdaemon = p->p_lwpdaemon; 413 cp->p_zombcnt = p->p_zombcnt; 414 /* 415 * If the parent's lwp ids have wrapped around, so have the 416 * child's. 417 */ 418 cp->p_flag |= p->p_flag & SLWPWRAP; 419 } 420 421 mutex_enter(&p->p_lock); 422 corectl_path_hold(cp->p_corefile = p->p_corefile); 423 corectl_content_hold(cp->p_content = p->p_content); 424 mutex_exit(&p->p_lock); 425 426 /* 427 * Duplicate process context ops, if any. 428 */ 429 if (p->p_pctx) 430 forkpctx(p, cp); 431 432 #ifdef __sparc 433 utrap_dup(p, cp); 434 #endif 435 /* 436 * If the child process has been marked to stop on exit 437 * from this fork, arrange for all other lwps to stop in 438 * sympathy with the active lwp. 439 */ 440 if (PTOU(cp)->u_systrap && 441 prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) { 442 mutex_enter(&cp->p_lock); 443 t = cp->p_tlist; 444 do { 445 t->t_proc_flag |= TP_PRSTOP; 446 aston(t); /* so TP_PRSTOP will be seen */ 447 } while ((t = t->t_forw) != cp->p_tlist); 448 mutex_exit(&cp->p_lock); 449 } 450 /* 451 * If the parent process has been marked to stop on exit 452 * from this fork, and its asynchronous-stop flag has not 453 * been set, arrange for all other lwps to stop before 454 * they return back to user level. 455 */ 456 if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap && 457 prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) { 458 mutex_enter(&p->p_lock); 459 t = p->p_tlist; 460 do { 461 t->t_proc_flag |= TP_PRSTOP; 462 aston(t); /* so TP_PRSTOP will be seen */ 463 } while ((t = t->t_forw) != p->p_tlist); 464 mutex_exit(&p->p_lock); 465 } 466 467 if (PROC_IS_BRANDED(p)) 468 BROP(p)->b_lwp_setrval(clone, p->p_pid, 1); 469 else 470 lwp_setrval(clone, p->p_pid, 1); 471 472 /* set return values for parent */ 473 r.r_val1 = (int)cp->p_pid; 474 r.r_val2 = 0; 475 476 /* 477 * pool_barrier_exit() can now be called because the child process has: 478 * - all identifying features cloned or set (p_pid, p_task, p_pool) 479 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset) 480 * - any other fields set which are used in resource set binding. 481 */ 482 mutex_enter(&p->p_lock); 483 pool_barrier_exit(); 484 mutex_exit(&p->p_lock); 485 486 mutex_enter(&pidlock); 487 mutex_enter(&cp->p_lock); 488 489 /* 490 * Now that there are lwps and threads attached, add the new 491 * process to the process group. 492 */ 493 pgjoin(cp, p->p_pgidp); 494 cp->p_stat = SRUN; 495 /* 496 * We are now done with all the lwps in the child process. 497 */ 498 t = cp->p_tlist; 499 do { 500 /* 501 * Set the lwp_suspend()ed lwps running. 502 * They will suspend properly at syscall exit. 503 */ 504 if (t->t_proc_flag & TP_HOLDLWP) 505 lwp_create_done(t); 506 else { 507 /* set TS_CREATE to allow continuelwps() to work */ 508 thread_lock(t); 509 ASSERT(t->t_state == TS_STOPPED && 510 !(t->t_schedflag & (TS_CREATE|TS_CSTART))); 511 t->t_schedflag |= TS_CREATE; 512 thread_unlock(t); 513 } 514 } while ((t = t->t_forw) != cp->p_tlist); 515 mutex_exit(&cp->p_lock); 516 517 if (isvfork) { 518 CPU_STATS_ADDQ(CPU, sys, sysvfork, 1); 519 mutex_enter(&p->p_lock); 520 p->p_flag |= SVFWAIT; 521 DTRACE_PROC1(create, proc_t *, cp); 522 cv_broadcast(&pr_pid_cv[p->p_slot]); /* inform /proc */ 523 mutex_exit(&p->p_lock); 524 /* 525 * Grab child's p_lock before dropping pidlock to ensure 526 * the process will not disappear before we set it running. 527 */ 528 mutex_enter(&cp->p_lock); 529 mutex_exit(&pidlock); 530 sigdefault(cp); 531 continuelwps(cp); 532 mutex_exit(&cp->p_lock); 533 } else { 534 CPU_STATS_ADDQ(CPU, sys, sysfork, 1); 535 DTRACE_PROC1(create, proc_t *, cp); 536 /* 537 * It is CL_FORKRET's job to drop pidlock. 538 * If we do it here, the process could be set running 539 * and disappear before CL_FORKRET() is called. 540 */ 541 CL_FORKRET(curthread, cp->p_tlist); 542 ASSERT(MUTEX_NOT_HELD(&pidlock)); 543 } 544 545 return (r.r_vals); 546 547 forklwperr: 548 if (isvfork) { 549 if (avl_numnodes(&p->p_wpage) != 0) { 550 /* restore watchpoints to parent */ 551 as = p->p_as; 552 AS_LOCK_ENTER(as, &as->a_lock, 553 RW_WRITER); 554 as->a_wpage = p->p_wpage; 555 avl_create(&p->p_wpage, wp_compare, 556 sizeof (struct watched_page), 557 offsetof(struct watched_page, wp_link)); 558 as_setwatch(as); 559 AS_LOCK_EXIT(as, &as->a_lock); 560 } 561 } else { 562 if (cp->p_segacct) 563 shmexit(cp); 564 as = cp->p_as; 565 cp->p_as = &kas; 566 as_free(as); 567 } 568 569 if (cp->p_lwpdir) { 570 for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++) 571 if ((lep = ldp->ld_entry) != NULL) 572 kmem_free(lep, sizeof (*lep)); 573 kmem_free(cp->p_lwpdir, 574 cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir)); 575 } 576 cp->p_lwpdir = NULL; 577 cp->p_lwpfree = NULL; 578 cp->p_lwpdir_sz = 0; 579 580 if (cp->p_tidhash) 581 kmem_free(cp->p_tidhash, 582 cp->p_tidhash_sz * sizeof (*cp->p_tidhash)); 583 cp->p_tidhash = NULL; 584 cp->p_tidhash_sz = 0; 585 586 forklwp_fail(cp); 587 fork_fail(cp); 588 rctl_set_free(cp->p_rctls); 589 mutex_enter(&pidlock); 590 591 /* 592 * Detach failed child from task. 593 */ 594 mutex_enter(&cp->p_lock); 595 tk = cp->p_task; 596 task_detach(cp); 597 ASSERT(cp->p_pool->pool_ref > 0); 598 atomic_add_32(&cp->p_pool->pool_ref, -1); 599 mutex_exit(&cp->p_lock); 600 601 orphpp = &p->p_orphan; 602 while (*orphpp != cp) 603 orphpp = &(*orphpp)->p_nextorph; 604 *orphpp = cp->p_nextorph; 605 if (p->p_child == cp) 606 p->p_child = cp->p_sibling; 607 if (cp->p_sibling) 608 cp->p_sibling->p_psibling = cp->p_psibling; 609 if (cp->p_psibling) 610 cp->p_psibling->p_sibling = cp->p_sibling; 611 pid_exit(cp); 612 mutex_exit(&pidlock); 613 614 task_rele(tk); 615 616 mutex_enter(&p->p_lock); 617 pool_barrier_exit(); 618 continuelwps(p); 619 mutex_exit(&p->p_lock); 620 error = EAGAIN; 621 forkerr: 622 return ((int64_t)set_errno(error)); 623 } 624 625 /* 626 * Free allocated resources from getproc() if a fork failed. 627 */ 628 static void 629 fork_fail(proc_t *cp) 630 { 631 uf_info_t *fip = P_FINFO(cp); 632 633 fcnt_add(fip, -1); 634 sigdelq(cp, NULL, 0); 635 636 mutex_enter(&pidlock); 637 upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred)); 638 mutex_exit(&pidlock); 639 640 /* 641 * single threaded, so no locking needed here 642 */ 643 crfree(cp->p_cred); 644 645 kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t)); 646 647 VN_RELE(u.u_cdir); 648 if (u.u_rdir) 649 VN_RELE(u.u_rdir); 650 if (cp->p_exec) 651 VN_RELE(cp->p_exec); 652 if (cp->p_execdir) 653 VN_RELE(cp->p_execdir); 654 if (u.u_cwd) 655 refstr_rele(u.u_cwd); 656 } 657 658 /* 659 * Clean up the lwps already created for this child process. 660 * The fork failed while duplicating all the lwps of the parent 661 * and those lwps already created must be freed. 662 * This process is invisible to the rest of the system, 663 * so we don't need to hold p->p_lock to protect the list. 664 */ 665 static void 666 forklwp_fail(proc_t *p) 667 { 668 kthread_t *t; 669 task_t *tk; 670 671 while ((t = p->p_tlist) != NULL) { 672 /* 673 * First remove the lwp from the process's p_tlist. 674 */ 675 if (t != t->t_forw) 676 p->p_tlist = t->t_forw; 677 else 678 p->p_tlist = NULL; 679 p->p_lwpcnt--; 680 t->t_forw->t_back = t->t_back; 681 t->t_back->t_forw = t->t_forw; 682 683 tk = p->p_task; 684 mutex_enter(&p->p_zone->zone_nlwps_lock); 685 tk->tk_nlwps--; 686 tk->tk_proj->kpj_nlwps--; 687 p->p_zone->zone_nlwps--; 688 mutex_exit(&p->p_zone->zone_nlwps_lock); 689 690 ASSERT(t->t_schedctl == NULL); 691 692 if (t->t_door != NULL) { 693 kmem_free(t->t_door, sizeof (door_data_t)); 694 t->t_door = NULL; 695 } 696 lwp_ctmpl_clear(ttolwp(t)); 697 698 /* 699 * Remove the thread from the all threads list. 700 * We need to hold pidlock for this. 701 */ 702 mutex_enter(&pidlock); 703 t->t_next->t_prev = t->t_prev; 704 t->t_prev->t_next = t->t_next; 705 CL_EXIT(t); /* tell the scheduler that we're exiting */ 706 cv_broadcast(&t->t_joincv); /* tell anyone in thread_join */ 707 mutex_exit(&pidlock); 708 709 /* 710 * Let the lgroup load averages know that this thread isn't 711 * going to show up (i.e. un-do what was done on behalf of 712 * this thread by the earlier lgrp_move_thread()). 713 */ 714 kpreempt_disable(); 715 lgrp_move_thread(t, NULL, 1); 716 kpreempt_enable(); 717 718 /* 719 * The thread was created TS_STOPPED. 720 * We change it to TS_FREE to avoid an 721 * ASSERT() panic in thread_free(). 722 */ 723 t->t_state = TS_FREE; 724 thread_rele(t); 725 thread_free(t); 726 } 727 } 728 729 extern struct as kas; 730 731 /* 732 * fork a kernel process. 733 */ 734 int 735 newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct) 736 { 737 proc_t *p; 738 struct user *up; 739 klwp_t *lwp; 740 cont_process_t *ctp = NULL; 741 rctl_entity_p_t e; 742 743 ASSERT(!(cid == syscid && ct != NULL)); 744 if (cid == syscid) { 745 rctl_alloc_gp_t *init_gp; 746 rctl_set_t *init_set; 747 748 if (getproc(&p, 1) < 0) 749 return (EAGAIN); 750 751 p->p_flag |= SNOWAIT; 752 p->p_exec = NULL; 753 p->p_execdir = NULL; 754 755 init_set = rctl_set_create(); 756 init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS); 757 758 /* 759 * kernel processes do not inherit /proc tracing flags. 760 */ 761 sigemptyset(&p->p_sigmask); 762 premptyset(&p->p_fltmask); 763 up = PTOU(p); 764 up->u_systrap = 0; 765 premptyset(&(up->u_entrymask)); 766 premptyset(&(up->u_exitmask)); 767 mutex_enter(&p->p_lock); 768 e.rcep_p.proc = p; 769 e.rcep_t = RCENTITY_PROCESS; 770 p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set, 771 init_gp); 772 mutex_exit(&p->p_lock); 773 774 rctl_prealloc_destroy(init_gp); 775 } else { 776 rctl_alloc_gp_t *init_gp, *default_gp; 777 rctl_set_t *init_set; 778 task_t *tk, *tk_old; 779 780 if (getproc(&p, 0) < 0) 781 return (EAGAIN); 782 /* 783 * init creates a new task, distinct from the task 784 * containing kernel "processes". 785 */ 786 tk = task_create(0, p->p_zone); 787 mutex_enter(&tk->tk_zone->zone_nlwps_lock); 788 tk->tk_proj->kpj_ntasks++; 789 mutex_exit(&tk->tk_zone->zone_nlwps_lock); 790 791 default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS); 792 init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS); 793 init_set = rctl_set_create(); 794 795 mutex_enter(&pidlock); 796 mutex_enter(&p->p_lock); 797 tk_old = p->p_task; /* switch to new task */ 798 799 task_detach(p); 800 task_begin(tk, p); 801 mutex_exit(&pidlock); 802 803 e.rcep_p.proc = p; 804 e.rcep_t = RCENTITY_PROCESS; 805 p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set, 806 init_gp); 807 rctlproc_default_init(p, default_gp); 808 mutex_exit(&p->p_lock); 809 810 task_rele(tk_old); 811 rctl_prealloc_destroy(default_gp); 812 rctl_prealloc_destroy(init_gp); 813 } 814 815 p->p_as = &kas; 816 817 if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri, 818 &curthread->t_hold, cid, 1)) == NULL) { 819 task_t *tk; 820 fork_fail(p); 821 mutex_enter(&pidlock); 822 mutex_enter(&p->p_lock); 823 tk = p->p_task; 824 task_detach(p); 825 ASSERT(p->p_pool->pool_ref > 0); 826 atomic_add_32(&p->p_pool->pool_ref, -1); 827 mutex_exit(&p->p_lock); 828 pid_exit(p); 829 mutex_exit(&pidlock); 830 task_rele(tk); 831 832 return (EAGAIN); 833 } 834 835 if (cid != syscid) { 836 ctp = contract_process_fork(sys_process_tmpl, p, curproc, 837 B_FALSE); 838 ASSERT(ctp != NULL); 839 if (ct != NULL) 840 *ct = &ctp->conp_contract; 841 } 842 843 p->p_lwpid = 1; 844 mutex_enter(&pidlock); 845 pgjoin(p, curproc->p_pgidp); 846 p->p_stat = SRUN; 847 mutex_enter(&p->p_lock); 848 lwptot(lwp)->t_proc_flag &= ~TP_HOLDLWP; 849 lwp_create_done(lwptot(lwp)); 850 mutex_exit(&p->p_lock); 851 mutex_exit(&pidlock); 852 return (0); 853 } 854 855 /* 856 * create a child proc struct. 857 */ 858 static int 859 getproc(proc_t **cpp, int kernel) 860 { 861 proc_t *pp, *cp; 862 pid_t newpid; 863 struct user *uarea; 864 extern uint_t nproc; 865 struct cred *cr; 866 uid_t ruid; 867 zoneid_t zoneid; 868 869 if (!page_mem_avail(tune.t_minarmem)) 870 return (-1); 871 if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) 872 return (-1); /* no point in starting new processes */ 873 874 pp = curproc; 875 cp = kmem_cache_alloc(process_cache, KM_SLEEP); 876 bzero(cp, sizeof (proc_t)); 877 878 /* 879 * Make proc entry for child process 880 */ 881 mutex_init(&cp->p_splock, NULL, MUTEX_DEFAULT, NULL); 882 mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL); 883 mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL); 884 #if defined(__x86) 885 mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL); 886 #endif 887 mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL); 888 cp->p_stat = SIDL; 889 cp->p_mstart = gethrtime(); 890 891 if ((newpid = pid_allocate(cp, PID_ALLOC_PROC)) == -1) { 892 if (nproc == v.v_proc) { 893 CPU_STATS_ADDQ(CPU, sys, procovf, 1); 894 cmn_err(CE_WARN, "out of processes"); 895 } 896 goto bad; 897 } 898 899 /* 900 * If not privileged make sure that this user hasn't exceeded 901 * v.v_maxup processes, and that users collectively haven't 902 * exceeded v.v_maxupttl processes. 903 */ 904 mutex_enter(&pidlock); 905 ASSERT(nproc < v.v_proc); /* otherwise how'd we get our pid? */ 906 cr = CRED(); 907 ruid = crgetruid(cr); 908 zoneid = crgetzoneid(cr); 909 if (nproc >= v.v_maxup && /* short-circuit; usually false */ 910 (nproc >= v.v_maxupttl || 911 upcount_get(ruid, zoneid) >= v.v_maxup) && 912 secpolicy_newproc(cr) != 0) { 913 mutex_exit(&pidlock); 914 zcmn_err(zoneid, CE_NOTE, 915 "out of per-user processes for uid %d", ruid); 916 goto bad; 917 } 918 919 /* 920 * Everything is cool, put the new proc on the active process list. 921 * It is already on the pid list and in /proc. 922 * Increment the per uid process count (upcount). 923 */ 924 nproc++; 925 upcount_inc(ruid, zoneid); 926 927 cp->p_next = practive; 928 practive->p_prev = cp; 929 practive = cp; 930 931 cp->p_ignore = pp->p_ignore; 932 cp->p_siginfo = pp->p_siginfo; 933 cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD); 934 cp->p_sessp = pp->p_sessp; 935 sess_hold(pp); 936 cp->p_exec = pp->p_exec; 937 cp->p_execdir = pp->p_execdir; 938 cp->p_zone = pp->p_zone; 939 cp->p_brand = pp->p_brand; 940 if (PROC_IS_BRANDED(pp)) 941 BROP(pp)->b_copy_procdata(cp, pp); 942 943 cp->p_bssbase = pp->p_bssbase; 944 cp->p_brkbase = pp->p_brkbase; 945 cp->p_brksize = pp->p_brksize; 946 cp->p_brkpageszc = pp->p_brkpageszc; 947 cp->p_stksize = pp->p_stksize; 948 cp->p_stkpageszc = pp->p_stkpageszc; 949 cp->p_stkprot = pp->p_stkprot; 950 cp->p_datprot = pp->p_datprot; 951 cp->p_usrstack = pp->p_usrstack; 952 cp->p_model = pp->p_model; 953 cp->p_ppid = pp->p_pid; 954 cp->p_ancpid = pp->p_pid; 955 cp->p_portcnt = pp->p_portcnt; 956 957 /* 958 * Initialize watchpoint structures 959 */ 960 avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area), 961 offsetof(struct watched_area, wa_link)); 962 963 /* 964 * Initialize immediate resource control values. 965 */ 966 cp->p_stk_ctl = pp->p_stk_ctl; 967 cp->p_fsz_ctl = pp->p_fsz_ctl; 968 cp->p_vmem_ctl = pp->p_vmem_ctl; 969 cp->p_fno_ctl = pp->p_fno_ctl; 970 971 /* 972 * Link up to parent-child-sibling chain. No need to lock 973 * in general since only a call to freeproc() (done by the 974 * same parent as newproc()) diddles with the child chain. 975 */ 976 cp->p_sibling = pp->p_child; 977 if (pp->p_child) 978 pp->p_child->p_psibling = cp; 979 980 cp->p_parent = pp; 981 pp->p_child = cp; 982 983 cp->p_child_ns = NULL; 984 cp->p_sibling_ns = NULL; 985 986 cp->p_nextorph = pp->p_orphan; 987 cp->p_nextofkin = pp; 988 pp->p_orphan = cp; 989 990 /* 991 * Inherit profiling state; do not inherit REALPROF profiling state. 992 */ 993 cp->p_prof = pp->p_prof; 994 cp->p_rprof_cyclic = CYCLIC_NONE; 995 996 /* 997 * Inherit pool pointer from the parent. Kernel processes are 998 * always bound to the default pool. 999 */ 1000 mutex_enter(&pp->p_lock); 1001 if (kernel) { 1002 cp->p_pool = pool_default; 1003 cp->p_flag |= SSYS; 1004 } else { 1005 cp->p_pool = pp->p_pool; 1006 } 1007 atomic_add_32(&cp->p_pool->pool_ref, 1); 1008 mutex_exit(&pp->p_lock); 1009 1010 /* 1011 * Add the child process to the current task. Kernel processes 1012 * are always attached to task0. 1013 */ 1014 mutex_enter(&cp->p_lock); 1015 if (kernel) 1016 task_attach(task0p, cp); 1017 else 1018 task_attach(pp->p_task, cp); 1019 mutex_exit(&cp->p_lock); 1020 mutex_exit(&pidlock); 1021 1022 avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t), 1023 offsetof(contract_t, ct_ctlist)); 1024 1025 /* 1026 * Duplicate any audit information kept in the process table 1027 */ 1028 #ifdef C2_AUDIT 1029 if (audit_active) /* copy audit data to cp */ 1030 audit_newproc(cp); 1031 #endif 1032 1033 crhold(cp->p_cred = cr); 1034 1035 /* 1036 * Bump up the counts on the file structures pointed at by the 1037 * parent's file table since the child will point at them too. 1038 */ 1039 fcnt_add(P_FINFO(pp), 1); 1040 1041 VN_HOLD(u.u_cdir); 1042 if (u.u_rdir) 1043 VN_HOLD(u.u_rdir); 1044 if (u.u_cwd) 1045 refstr_hold(u.u_cwd); 1046 1047 /* 1048 * copy the parent's uarea. 1049 */ 1050 uarea = PTOU(cp); 1051 bcopy(PTOU(pp), uarea, sizeof (user_t)); 1052 flist_fork(P_FINFO(pp), P_FINFO(cp)); 1053 1054 gethrestime(&uarea->u_start); 1055 uarea->u_ticks = lbolt; 1056 uarea->u_mem = rm_asrss(pp->p_as); 1057 uarea->u_acflag = AFORK; 1058 1059 /* 1060 * If inherit-on-fork, copy /proc tracing flags to child. 1061 */ 1062 if ((pp->p_proc_flag & P_PR_FORK) != 0) { 1063 cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK); 1064 cp->p_sigmask = pp->p_sigmask; 1065 cp->p_fltmask = pp->p_fltmask; 1066 } else { 1067 sigemptyset(&cp->p_sigmask); 1068 premptyset(&cp->p_fltmask); 1069 uarea->u_systrap = 0; 1070 premptyset(&uarea->u_entrymask); 1071 premptyset(&uarea->u_exitmask); 1072 } 1073 /* 1074 * If microstate accounting is being inherited, mark child 1075 */ 1076 if ((pp->p_flag & SMSFORK) != 0) 1077 cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT); 1078 1079 /* 1080 * Inherit fixalignment flag from the parent 1081 */ 1082 cp->p_fixalignment = pp->p_fixalignment; 1083 1084 if (cp->p_exec) 1085 VN_HOLD(cp->p_exec); 1086 if (cp->p_execdir) 1087 VN_HOLD(cp->p_execdir); 1088 *cpp = cp; 1089 return (0); 1090 1091 bad: 1092 ASSERT(MUTEX_NOT_HELD(&pidlock)); 1093 1094 mutex_destroy(&cp->p_crlock); 1095 mutex_destroy(&cp->p_pflock); 1096 #if defined(__x86) 1097 mutex_destroy(&cp->p_ldtlock); 1098 #endif 1099 if (newpid != -1) { 1100 proc_entry_free(cp->p_pidp); 1101 (void) pid_rele(cp->p_pidp); 1102 } 1103 kmem_cache_free(process_cache, cp); 1104 1105 /* 1106 * We most likely got into this situation because some process is 1107 * forking out of control. As punishment, put it to sleep for a 1108 * bit so it can't eat the machine alive. Sleep interval is chosen 1109 * to allow no more than one fork failure per cpu per clock tick 1110 * on average (yes, I just made this up). This has two desirable 1111 * properties: (1) it sets a constant limit on the fork failure 1112 * rate, and (2) the busier the system is, the harsher the penalty 1113 * for abusing it becomes. 1114 */ 1115 INCR_COUNT(&fork_fail_pending, &pidlock); 1116 delay(fork_fail_pending / ncpus + 1); 1117 DECR_COUNT(&fork_fail_pending, &pidlock); 1118 1119 return (-1); /* out of memory or proc slots */ 1120 } 1121 1122 /* 1123 * Release virtual memory. 1124 * In the case of vfork(), the child was given exclusive access to its 1125 * parent's address space. The parent is waiting in vfwait() for the 1126 * child to release its exclusive claim via relvm(). 1127 */ 1128 void 1129 relvm() 1130 { 1131 proc_t *p = curproc; 1132 1133 ASSERT((unsigned)p->p_lwpcnt <= 1); 1134 1135 prrelvm(); /* inform /proc */ 1136 1137 if (p->p_flag & SVFORK) { 1138 proc_t *pp = p->p_parent; 1139 /* 1140 * The child process is either exec'ing or exit'ing. 1141 * The child is now separated from the parent's address 1142 * space. The parent process is made dispatchable. 1143 * 1144 * This is a delicate locking maneuver, involving 1145 * both the parent's p_lock and the child's p_lock. 1146 * As soon as the SVFORK flag is turned off, the 1147 * parent is free to run, but it must not run until 1148 * we wake it up using its p_cv because it might 1149 * exit and we would be referencing invalid memory. 1150 * Therefore, we hold the parent with its p_lock 1151 * while protecting our p_flags with our own p_lock. 1152 */ 1153 try_again: 1154 mutex_enter(&p->p_lock); /* grab child's lock first */ 1155 prbarrier(p); /* make sure /proc is blocked out */ 1156 mutex_enter(&pp->p_lock); 1157 1158 /* 1159 * Check if parent is locked by /proc. 1160 */ 1161 if (pp->p_proc_flag & P_PR_LOCK) { 1162 /* 1163 * Delay until /proc is done with the parent. 1164 * We must drop our (the child's) p->p_lock, wait 1165 * via prbarrier() on the parent, then start over. 1166 */ 1167 mutex_exit(&p->p_lock); 1168 prbarrier(pp); 1169 mutex_exit(&pp->p_lock); 1170 goto try_again; 1171 } 1172 p->p_flag &= ~SVFORK; 1173 kpreempt_disable(); 1174 p->p_as = &kas; 1175 1176 /* 1177 * notify hat of change in thread's address space 1178 */ 1179 hat_thread_exit(curthread); 1180 kpreempt_enable(); 1181 1182 /* 1183 * child sizes are copied back to parent because 1184 * child may have grown. 1185 */ 1186 pp->p_brkbase = p->p_brkbase; 1187 pp->p_brksize = p->p_brksize; 1188 pp->p_stksize = p->p_stksize; 1189 /* 1190 * The parent is no longer waiting for the vfork()d child. 1191 * Restore the parent's watched pages, if any. This is 1192 * safe because we know the parent is not locked by /proc 1193 */ 1194 pp->p_flag &= ~SVFWAIT; 1195 if (avl_numnodes(&pp->p_wpage) != 0) { 1196 pp->p_as->a_wpage = pp->p_wpage; 1197 avl_create(&pp->p_wpage, wp_compare, 1198 sizeof (struct watched_page), 1199 offsetof(struct watched_page, wp_link)); 1200 } 1201 cv_signal(&pp->p_cv); 1202 mutex_exit(&pp->p_lock); 1203 mutex_exit(&p->p_lock); 1204 } else { 1205 if (p->p_as != &kas) { 1206 struct as *as; 1207 1208 if (p->p_segacct) 1209 shmexit(p); 1210 1211 /* 1212 * We grab p_lock for the benefit of /proc 1213 */ 1214 kpreempt_disable(); 1215 mutex_enter(&p->p_lock); 1216 prbarrier(p); /* make sure /proc is blocked out */ 1217 as = p->p_as; 1218 p->p_as = &kas; 1219 mutex_exit(&p->p_lock); 1220 1221 /* 1222 * notify hat of change in thread's address space 1223 */ 1224 hat_thread_exit(curthread); 1225 kpreempt_enable(); 1226 1227 as_free(as); 1228 } 1229 } 1230 } 1231 1232 /* 1233 * Wait for child to exec or exit. 1234 * Called by parent of vfork'ed process. 1235 * See important comments in relvm(), above. 1236 */ 1237 void 1238 vfwait(pid_t pid) 1239 { 1240 int signalled = 0; 1241 proc_t *pp = ttoproc(curthread); 1242 proc_t *cp; 1243 1244 /* 1245 * Wait for child to exec or exit. 1246 */ 1247 for (;;) { 1248 mutex_enter(&pidlock); 1249 cp = prfind(pid); 1250 if (cp == NULL || cp->p_parent != pp) { 1251 /* 1252 * Child has exit()ed. 1253 */ 1254 mutex_exit(&pidlock); 1255 break; 1256 } 1257 /* 1258 * Grab the child's p_lock before releasing pidlock. 1259 * Otherwise, the child could exit and we would be 1260 * referencing invalid memory. 1261 */ 1262 mutex_enter(&cp->p_lock); 1263 mutex_exit(&pidlock); 1264 if (!(cp->p_flag & SVFORK)) { 1265 /* 1266 * Child has exec()ed or is exit()ing. 1267 */ 1268 mutex_exit(&cp->p_lock); 1269 break; 1270 } 1271 mutex_enter(&pp->p_lock); 1272 mutex_exit(&cp->p_lock); 1273 /* 1274 * We might be waked up spuriously from the cv_wait(). 1275 * We have to do the whole operation over again to be 1276 * sure the child's SVFORK flag really is turned off. 1277 * We cannot make reference to the child because it can 1278 * exit before we return and we would be referencing 1279 * invalid memory. 1280 * 1281 * Because this is potentially a very long-term wait, 1282 * we call cv_wait_sig() (for its jobcontrol and /proc 1283 * side-effects) unless there is a current signal, in 1284 * which case we use cv_wait() because we cannot return 1285 * from this function until the child has released the 1286 * address space. Calling cv_wait_sig() with a current 1287 * signal would lead to an indefinite loop here because 1288 * cv_wait_sig() returns immediately in this case. 1289 */ 1290 if (signalled) 1291 cv_wait(&pp->p_cv, &pp->p_lock); 1292 else 1293 signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock); 1294 mutex_exit(&pp->p_lock); 1295 } 1296 1297 /* restore watchpoints to parent */ 1298 if (pr_watch_active(pp)) { 1299 struct as *as = pp->p_as; 1300 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1301 as_setwatch(as); 1302 AS_LOCK_EXIT(as, &as->a_lock); 1303 } 1304 1305 mutex_enter(&pp->p_lock); 1306 prbarrier(pp); /* barrier against /proc locking */ 1307 continuelwps(pp); 1308 mutex_exit(&pp->p_lock); 1309 } 1310