1 /* $OpenBSD: kern_exit.c,v 1.188 2020/03/18 15:48:21 visa Exp $ */ 2 /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/ioctl.h> 43 #include <sys/proc.h> 44 #include <sys/tty.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 #include <sys/wait.h> 50 #include <sys/vnode.h> 51 #include <sys/syslog.h> 52 #include <sys/malloc.h> 53 #include <sys/resourcevar.h> 54 #include <sys/ptrace.h> 55 #include <sys/acct.h> 56 #include <sys/filedesc.h> 57 #include <sys/signalvar.h> 58 #include <sys/sched.h> 59 #include <sys/ktrace.h> 60 #include <sys/pool.h> 61 #include <sys/mutex.h> 62 #include <sys/pledge.h> 63 #ifdef SYSVSEM 64 #include <sys/sem.h> 65 #endif 66 #include <sys/witness.h> 67 68 #include <sys/mount.h> 69 #include <sys/syscallargs.h> 70 71 #include <uvm/uvm_extern.h> 72 73 #include "kcov.h" 74 #if NKCOV > 0 75 #include <sys/kcov.h> 76 #endif 77 78 void proc_finish_wait(struct proc *, struct proc *); 79 void process_clear_orphan(struct process *); 80 void process_zap(struct process *); 81 void proc_free(struct proc *); 82 void unveil_destroy(struct process *ps); 83 84 /* 85 * exit -- 86 * Death of process. 87 */ 88 int 89 sys_exit(struct proc *p, void *v, register_t *retval) 90 { 91 struct sys_exit_args /* { 92 syscallarg(int) rval; 93 } */ *uap = v; 94 95 exit1(p, SCARG(uap, rval), 0, EXIT_NORMAL); 96 /* NOTREACHED */ 97 return (0); 98 } 99 100 int 101 sys___threxit(struct proc *p, void *v, register_t *retval) 102 { 103 struct sys___threxit_args /* { 104 syscallarg(pid_t *) notdead; 105 } */ *uap = v; 106 107 if (SCARG(uap, notdead) != NULL) { 108 pid_t zero = 0; 109 if (copyout(&zero, SCARG(uap, notdead), sizeof(zero))) 110 psignal(p, SIGSEGV); 111 } 112 exit1(p, 0, 0, EXIT_THREAD); 113 114 return (0); 115 } 116 117 /* 118 * Exit: deallocate address space and other resources, change proc state 119 * to zombie, and unlink proc from allproc and parent's lists. Save exit 120 * status and rusage for wait(). Check for child processes and orphan them. 121 */ 122 void 123 exit1(struct proc *p, int xexit, int xsig, int flags) 124 { 125 struct process *pr, *qr, *nqr; 126 struct rusage *rup; 127 128 atomic_setbits_int(&p->p_flag, P_WEXIT); 129 130 pr = p->p_p; 131 132 /* single-threaded? */ 133 if (!P_HASSIBLING(p)) { 134 flags = EXIT_NORMAL; 135 } else { 136 /* nope, multi-threaded */ 137 if (flags == EXIT_NORMAL) 138 single_thread_set(p, SINGLE_EXIT, 0); 139 else if (flags == EXIT_THREAD) 140 single_thread_check(p, 0); 141 } 142 143 if (flags == EXIT_NORMAL) { 144 if (pr->ps_pid == 1) 145 panic("init died (signal %d, exit %d)", xsig, xexit); 146 147 atomic_setbits_int(&pr->ps_flags, PS_EXITING); 148 pr->ps_xexit = xexit; 149 pr->ps_xsig = xsig; 150 151 /* 152 * If parent is waiting for us to exit or exec, PS_PPWAIT 153 * is set; we wake up the parent early to avoid deadlock. 154 */ 155 if (pr->ps_flags & PS_PPWAIT) { 156 atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT); 157 atomic_clearbits_int(&pr->ps_pptr->ps_flags, 158 PS_ISPWAIT); 159 wakeup(pr->ps_pptr); 160 } 161 } 162 163 /* unlink ourselves from the active threads */ 164 TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link); 165 if ((p->p_flag & P_THREAD) == 0) { 166 /* main thread gotta wait because it has the pid, et al */ 167 while (pr->ps_refcnt > 1) 168 tsleep_nsec(&pr->ps_threads, PWAIT, "thrdeath", INFSLP); 169 if (pr->ps_flags & PS_PROFIL) 170 stopprofclock(pr); 171 } 172 173 rup = pr->ps_ru; 174 if (rup == NULL) { 175 rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO); 176 if (pr->ps_ru == NULL) { 177 pr->ps_ru = rup; 178 } else { 179 pool_put(&rusage_pool, rup); 180 rup = pr->ps_ru; 181 } 182 } 183 p->p_siglist = 0; 184 if ((p->p_flag & P_THREAD) == 0) 185 pr->ps_siglist = 0; 186 187 #if NKCOV > 0 188 kcov_exit(p); 189 #endif 190 191 if ((p->p_flag & P_THREAD) == 0) { 192 sigio_freelist(&pr->ps_sigiolst); 193 194 /* close open files and release open-file table */ 195 fdfree(p); 196 197 timeout_del(&pr->ps_realit_to); 198 timeout_del(&pr->ps_rucheck_to); 199 #ifdef SYSVSEM 200 semexit(pr); 201 #endif 202 killjobc(pr); 203 #ifdef ACCOUNTING 204 acct_process(p); 205 #endif 206 207 #ifdef KTRACE 208 /* release trace file */ 209 if (pr->ps_tracevp) 210 ktrcleartrace(pr); 211 #endif 212 213 unveil_destroy(pr); 214 215 /* 216 * If parent has the SAS_NOCLDWAIT flag set, we're not 217 * going to become a zombie. 218 */ 219 if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT) 220 atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE); 221 } 222 223 p->p_fd = NULL; /* zap the thread's copy */ 224 225 /* 226 * Remove proc from pidhash chain and allproc so looking 227 * it up won't work. We will put the proc on the 228 * deadproc list later (using the p_hash member), and 229 * wake up the reaper when we do. If this is the last 230 * thread of a process that isn't PS_NOZOMBIE, we'll put 231 * the process on the zombprocess list below. 232 */ 233 /* 234 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 235 */ 236 p->p_stat = SDEAD; 237 238 LIST_REMOVE(p, p_hash); 239 LIST_REMOVE(p, p_list); 240 241 if ((p->p_flag & P_THREAD) == 0) { 242 LIST_REMOVE(pr, ps_hash); 243 LIST_REMOVE(pr, ps_list); 244 245 if ((pr->ps_flags & PS_NOZOMBIE) == 0) 246 LIST_INSERT_HEAD(&zombprocess, pr, ps_list); 247 else { 248 /* 249 * Not going to be a zombie, so it's now off all 250 * the lists scanned by ispidtaken(), so block 251 * fast reuse of the pid now. 252 */ 253 freepid(pr->ps_pid); 254 } 255 256 /* 257 * Reparent children to their original parent, in case 258 * they were being traced, or to init(8). 259 */ 260 qr = LIST_FIRST(&pr->ps_children); 261 if (qr) /* only need this if any child is S_ZOMB */ 262 wakeup(initprocess); 263 for (; qr != 0; qr = nqr) { 264 nqr = LIST_NEXT(qr, ps_sibling); 265 /* 266 * Traced processes are killed since their 267 * existence means someone is screwing up. 268 */ 269 if (qr->ps_flags & PS_TRACED && 270 !(qr->ps_flags & PS_EXITING)) { 271 process_untrace(qr); 272 273 /* 274 * If single threading is active, 275 * direct the signal to the active 276 * thread to avoid deadlock. 277 */ 278 if (qr->ps_single) 279 ptsignal(qr->ps_single, SIGKILL, 280 STHREAD); 281 else 282 prsignal(qr, SIGKILL); 283 } else { 284 process_reparent(qr, initprocess); 285 } 286 } 287 288 /* 289 * Make sure orphans won't remember the exiting process. 290 */ 291 while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) { 292 KASSERT(qr->ps_oppid == pr->ps_pid); 293 qr->ps_oppid = 0; 294 process_clear_orphan(qr); 295 } 296 } 297 298 /* add thread's accumulated rusage into the process's total */ 299 ruadd(rup, &p->p_ru); 300 tuagg(pr, p); 301 302 /* 303 * clear %cpu usage during swap 304 */ 305 p->p_pctcpu = 0; 306 307 if ((p->p_flag & P_THREAD) == 0) { 308 /* 309 * Final thread has died, so add on our children's rusage 310 * and calculate the total times 311 */ 312 calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL); 313 ruadd(rup, &pr->ps_cru); 314 315 /* notify interested parties of our demise and clean up */ 316 knote_processexit(p); 317 318 /* 319 * Notify parent that we're gone. If we're not going to 320 * become a zombie, reparent to process 1 (init) so that 321 * we can wake our original parent to possibly unblock 322 * wait4() to return ECHILD. 323 */ 324 if (pr->ps_flags & PS_NOZOMBIE) { 325 struct process *ppr = pr->ps_pptr; 326 process_reparent(pr, initprocess); 327 wakeup(ppr); 328 } 329 330 /* 331 * Release the process's signal state. 332 */ 333 sigactsfree(pr); 334 } 335 336 /* just a thread? detach it from its process */ 337 if (p->p_flag & P_THREAD) { 338 /* scheduler_wait_hook(pr->ps_mainproc, p); XXX */ 339 if (--pr->ps_refcnt == 1) 340 wakeup(&pr->ps_threads); 341 KASSERT(pr->ps_refcnt > 0); 342 } 343 344 /* Release the thread's read reference of resource limit structure. */ 345 if (p->p_limit != NULL) { 346 struct plimit *limit; 347 348 limit = p->p_limit; 349 p->p_limit = NULL; 350 lim_free(limit); 351 } 352 353 /* 354 * Other substructures are freed from reaper and wait(). 355 */ 356 357 /* 358 * Finally, call machine-dependent code to switch to a new 359 * context (possibly the idle context). Once we are no longer 360 * using the dead process's vmspace and stack, exit2() will be 361 * called to schedule those resources to be released by the 362 * reaper thread. 363 * 364 * Note that cpu_exit() will end with a call equivalent to 365 * cpu_switch(), finishing our execution (pun intended). 366 */ 367 uvmexp.swtch++; 368 cpu_exit(p); 369 panic("cpu_exit returned"); 370 } 371 372 /* 373 * Locking of this proclist is special; it's accessed in a 374 * critical section of process exit, and thus locking it can't 375 * modify interrupt state. We use a simple spin lock for this 376 * proclist. We use the p_hash member to linkup to deadproc. 377 */ 378 struct mutex deadproc_mutex = 379 MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS); 380 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 381 382 /* 383 * We are called from cpu_exit() once it is safe to schedule the 384 * dead process's resources to be freed. 385 * 386 * NOTE: One must be careful with locking in this routine. It's 387 * called from a critical section in machine-dependent code, so 388 * we should refrain from changing any interrupt state. 389 * 390 * We lock the deadproc list, place the proc on that list (using 391 * the p_hash member), and wake up the reaper. 392 */ 393 void 394 exit2(struct proc *p) 395 { 396 mtx_enter(&deadproc_mutex); 397 LIST_INSERT_HEAD(&deadproc, p, p_hash); 398 mtx_leave(&deadproc_mutex); 399 400 wakeup(&deadproc); 401 } 402 403 void 404 proc_free(struct proc *p) 405 { 406 crfree(p->p_ucred); 407 pool_put(&proc_pool, p); 408 nthreads--; 409 } 410 411 /* 412 * Process reaper. This is run by a kernel thread to free the resources 413 * of a dead process. Once the resources are free, the process becomes 414 * a zombie, and the parent is allowed to read the undead's status. 415 */ 416 void 417 reaper(void *arg) 418 { 419 struct proc *p; 420 421 KERNEL_UNLOCK(); 422 423 SCHED_ASSERT_UNLOCKED(); 424 425 for (;;) { 426 mtx_enter(&deadproc_mutex); 427 while ((p = LIST_FIRST(&deadproc)) == NULL) 428 msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper", 429 INFSLP); 430 431 /* Remove us from the deadproc list. */ 432 LIST_REMOVE(p, p_hash); 433 mtx_leave(&deadproc_mutex); 434 435 WITNESS_THREAD_EXIT(p); 436 437 KERNEL_LOCK(); 438 439 /* 440 * Free the VM resources we're still holding on to. 441 * We must do this from a valid thread because doing 442 * so may block. 443 */ 444 uvm_uarea_free(p); 445 p->p_vmspace = NULL; /* zap the thread's copy */ 446 447 if (p->p_flag & P_THREAD) { 448 /* Just a thread */ 449 proc_free(p); 450 } else { 451 struct process *pr = p->p_p; 452 453 /* Release the rest of the process's vmspace */ 454 uvm_exit(pr); 455 456 if ((pr->ps_flags & PS_NOZOMBIE) == 0) { 457 /* Process is now a true zombie. */ 458 atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE); 459 prsignal(pr->ps_pptr, SIGCHLD); 460 461 /* Wake up the parent so it can get exit status. */ 462 wakeup(pr->ps_pptr); 463 } else { 464 /* No one will wait for us. Just zap the process now */ 465 process_zap(pr); 466 } 467 } 468 469 KERNEL_UNLOCK(); 470 } 471 } 472 473 int 474 sys_wait4(struct proc *q, void *v, register_t *retval) 475 { 476 struct sys_wait4_args /* { 477 syscallarg(pid_t) pid; 478 syscallarg(int *) status; 479 syscallarg(int) options; 480 syscallarg(struct rusage *) rusage; 481 } */ *uap = v; 482 struct rusage ru; 483 int status, error; 484 485 error = dowait4(q, SCARG(uap, pid), 486 SCARG(uap, status) ? &status : NULL, 487 SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval); 488 if (error == 0 && retval[0] > 0 && SCARG(uap, status)) { 489 error = copyout(&status, SCARG(uap, status), sizeof(status)); 490 } 491 if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) { 492 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 493 #ifdef KTRACE 494 if (error == 0 && KTRPOINT(q, KTR_STRUCT)) 495 ktrrusage(q, &ru); 496 #endif 497 } 498 return (error); 499 } 500 501 int 502 dowait4(struct proc *q, pid_t pid, int *statusp, int options, 503 struct rusage *rusage, register_t *retval) 504 { 505 int nfound; 506 struct process *pr; 507 struct proc *p; 508 int error; 509 510 if (pid == 0) 511 pid = -q->p_p->ps_pgid; 512 if (options &~ (WUNTRACED|WNOHANG|WCONTINUED)) 513 return (EINVAL); 514 515 loop: 516 nfound = 0; 517 LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) { 518 if ((pr->ps_flags & PS_NOZOMBIE) || 519 (pid != WAIT_ANY && 520 pr->ps_pid != pid && 521 pr->ps_pgid != -pid)) 522 continue; 523 524 p = pr->ps_mainproc; 525 526 nfound++; 527 if (pr->ps_flags & PS_ZOMBIE) { 528 retval[0] = pr->ps_pid; 529 530 if (statusp != NULL) 531 *statusp = W_EXITCODE(pr->ps_xexit, 532 pr->ps_xsig); 533 if (rusage != NULL) 534 memcpy(rusage, pr->ps_ru, sizeof(*rusage)); 535 proc_finish_wait(q, p); 536 return (0); 537 } 538 if (pr->ps_flags & PS_TRACED && 539 (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single && 540 pr->ps_single->p_stat == SSTOP && 541 (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) { 542 if (single_thread_wait(pr, 0)) 543 goto loop; 544 545 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 546 retval[0] = pr->ps_pid; 547 548 if (statusp != NULL) 549 *statusp = W_STOPCODE(pr->ps_xsig); 550 if (rusage != NULL) 551 memset(rusage, 0, sizeof(*rusage)); 552 return (0); 553 } 554 if (p->p_stat == SSTOP && 555 (pr->ps_flags & PS_WAITED) == 0 && 556 (p->p_flag & P_SUSPSINGLE) == 0 && 557 (pr->ps_flags & PS_TRACED || 558 options & WUNTRACED)) { 559 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 560 retval[0] = pr->ps_pid; 561 562 if (statusp != NULL) 563 *statusp = W_STOPCODE(pr->ps_xsig); 564 if (rusage != NULL) 565 memset(rusage, 0, sizeof(*rusage)); 566 return (0); 567 } 568 if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 569 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 570 retval[0] = pr->ps_pid; 571 572 if (statusp != NULL) 573 *statusp = _WCONTINUED; 574 if (rusage != NULL) 575 memset(rusage, 0, sizeof(*rusage)); 576 return (0); 577 } 578 } 579 /* 580 * Look in the orphans list too, to allow the parent to 581 * collect it's child exit status even if child is being 582 * debugged. 583 * 584 * Debugger detaches from the parent upon successful 585 * switch-over from parent to child. At this point due to 586 * re-parenting the parent loses the child to debugger and a 587 * wait4(2) call would report that it has no children to wait 588 * for. By maintaining a list of orphans we allow the parent 589 * to successfully wait until the child becomes a zombie. 590 */ 591 if (nfound == 0) { 592 LIST_FOREACH(pr, &q->p_p->ps_orphans, ps_orphan) { 593 if ((pr->ps_flags & PS_NOZOMBIE) || 594 (pid != WAIT_ANY && 595 pr->ps_pid != pid && 596 pr->ps_pgid != -pid)) 597 continue; 598 nfound++; 599 break; 600 } 601 } 602 if (nfound == 0) 603 return (ECHILD); 604 if (options & WNOHANG) { 605 retval[0] = 0; 606 return (0); 607 } 608 if ((error = tsleep_nsec(q->p_p, PWAIT | PCATCH, "wait", INFSLP)) != 0) 609 return (error); 610 goto loop; 611 } 612 613 void 614 proc_finish_wait(struct proc *waiter, struct proc *p) 615 { 616 struct process *pr, *tr; 617 struct rusage *rup; 618 619 /* 620 * If we got the child via a ptrace 'attach', 621 * we need to give it back to the old parent. 622 */ 623 pr = p->p_p; 624 if (pr->ps_oppid != 0 && (pr->ps_oppid != pr->ps_pptr->ps_pid) && 625 (tr = prfind(pr->ps_oppid))) { 626 pr->ps_oppid = 0; 627 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 628 process_reparent(pr, tr); 629 prsignal(tr, SIGCHLD); 630 wakeup(tr); 631 } else { 632 scheduler_wait_hook(waiter, p); 633 rup = &waiter->p_p->ps_cru; 634 ruadd(rup, pr->ps_ru); 635 LIST_REMOVE(pr, ps_list); /* off zombprocess */ 636 freepid(pr->ps_pid); 637 process_zap(pr); 638 } 639 } 640 641 /* 642 * give process back to original parent or init(8) 643 */ 644 void 645 process_untrace(struct process *pr) 646 { 647 struct process *ppr = NULL; 648 649 KASSERT(pr->ps_flags & PS_TRACED); 650 651 if (pr->ps_oppid != 0 && 652 (pr->ps_oppid != pr->ps_pptr->ps_pid)) 653 ppr = prfind(pr->ps_oppid); 654 655 /* not being traced any more */ 656 pr->ps_oppid = 0; 657 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 658 process_reparent(pr, ppr ? ppr : initprocess); 659 } 660 661 void 662 process_clear_orphan(struct process *pr) 663 { 664 if (pr->ps_flags & PS_ORPHAN) { 665 LIST_REMOVE(pr, ps_orphan); 666 atomic_clearbits_int(&pr->ps_flags, PS_ORPHAN); 667 } 668 } 669 670 /* 671 * make process 'parent' the new parent of process 'child'. 672 */ 673 void 674 process_reparent(struct process *child, struct process *parent) 675 { 676 677 if (child->ps_pptr == parent) 678 return; 679 680 KASSERT(child->ps_oppid == 0 || 681 child->ps_oppid == child->ps_pptr->ps_pid); 682 683 LIST_REMOVE(child, ps_sibling); 684 LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling); 685 686 process_clear_orphan(child); 687 if (child->ps_flags & PS_TRACED) { 688 atomic_setbits_int(&child->ps_flags, PS_ORPHAN); 689 LIST_INSERT_HEAD(&child->ps_pptr->ps_orphans, child, ps_orphan); 690 } 691 692 child->ps_pptr = parent; 693 } 694 695 void 696 process_zap(struct process *pr) 697 { 698 struct vnode *otvp; 699 struct proc *p = pr->ps_mainproc; 700 701 /* 702 * Finally finished with old proc entry. 703 * Unlink it from its process group and free it. 704 */ 705 leavepgrp(pr); 706 LIST_REMOVE(pr, ps_sibling); 707 process_clear_orphan(pr); 708 709 /* 710 * Decrement the count of procs running with this uid. 711 */ 712 (void)chgproccnt(pr->ps_ucred->cr_ruid, -1); 713 714 /* 715 * Release reference to text vnode 716 */ 717 otvp = pr->ps_textvp; 718 pr->ps_textvp = NULL; 719 if (otvp) 720 vrele(otvp); 721 722 KASSERT(pr->ps_refcnt == 1); 723 if (pr->ps_ptstat != NULL) 724 free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat)); 725 pool_put(&rusage_pool, pr->ps_ru); 726 KASSERT(TAILQ_EMPTY(&pr->ps_threads)); 727 lim_free(pr->ps_limit); 728 crfree(pr->ps_ucred); 729 pool_put(&process_pool, pr); 730 nprocesses--; 731 732 proc_free(p); 733 } 734