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