1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_compat.h" 45 #include "opt_ktrace.h" 46 #include "opt_mac.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/sysproto.h> 51 #include <sys/eventhandler.h> 52 #include <sys/kernel.h> 53 #include <sys/malloc.h> 54 #include <sys/lock.h> 55 #include <sys/mutex.h> 56 #include <sys/proc.h> 57 #include <sys/pioctl.h> 58 #include <sys/tty.h> 59 #include <sys/wait.h> 60 #include <sys/vmmeter.h> 61 #include <sys/vnode.h> 62 #include <sys/resourcevar.h> 63 #include <sys/signalvar.h> 64 #include <sys/sched.h> 65 #include <sys/sx.h> 66 #include <sys/ptrace.h> 67 #include <sys/acct.h> /* for acct_process() function prototype */ 68 #include <sys/filedesc.h> 69 #include <sys/mac.h> 70 #include <sys/shm.h> 71 #include <sys/sem.h> 72 #ifdef KTRACE 73 #include <sys/ktrace.h> 74 #endif 75 76 #include <vm/vm.h> 77 #include <vm/vm_extern.h> 78 #include <vm/vm_param.h> 79 #include <vm/pmap.h> 80 #include <vm/vm_map.h> 81 #include <vm/vm_page.h> 82 #include <vm/uma.h> 83 #include <sys/user.h> 84 85 /* Required to be non-static for SysVR4 emulator */ 86 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 87 88 /* 89 * exit -- 90 * Death of process. 91 * 92 * MPSAFE 93 */ 94 void 95 sys_exit(struct thread *td, struct sys_exit_args *uap) 96 { 97 98 exit1(td, W_EXITCODE(uap->rval, 0)); 99 /* NOTREACHED */ 100 } 101 102 /* 103 * Exit: deallocate address space and other resources, change proc state 104 * to zombie, and unlink proc from allproc and parent's lists. Save exit 105 * status and rusage for wait(). Check for child processes and orphan them. 106 */ 107 void 108 exit1(struct thread *td, int rv) 109 { 110 struct proc *p, *nq, *q; 111 struct tty *tp; 112 struct vnode *ttyvp; 113 struct vmspace *vm; 114 struct vnode *vtmp; 115 #ifdef KTRACE 116 struct vnode *tracevp; 117 struct ucred *tracecred; 118 #endif 119 struct plimit *plim; 120 121 /* 122 * Drop Giant if caller has it. Eventually we should warn about 123 * being called with Giant held. 124 */ 125 while (mtx_owned(&Giant)) 126 mtx_unlock(&Giant); 127 128 p = td->td_proc; 129 if (p == initproc) { 130 printf("init died (signal %d, exit %d)\n", 131 WTERMSIG(rv), WEXITSTATUS(rv)); 132 panic("Going nowhere without my init!"); 133 } 134 135 /* 136 * MUST abort all other threads before proceeding past here. 137 */ 138 PROC_LOCK(p); 139 if (p->p_flag & P_SA || p->p_numthreads > 1) { 140 /* 141 * First check if some other thread got here before us.. 142 * if so, act apropriatly, (exit or suspend); 143 */ 144 thread_suspend_check(0); 145 146 /* 147 * Kill off the other threads. This requires 148 * Some co-operation from other parts of the kernel 149 * so it may not be instant. 150 * With this state set: 151 * Any thread entering the kernel from userspace will 152 * thread_exit() in trap(). Any thread attempting to 153 * sleep will return immediatly 154 * with EINTR or EWOULDBLOCK, which will hopefully force them 155 * to back out to userland, freeing resources as they go, and 156 * anything attempting to return to userland will thread_exit() 157 * from userret(). thread_exit() will unsuspend us 158 * when the last other thread exits. 159 */ 160 if (thread_single(SINGLE_EXIT)) 161 panic ("Exit: Single threading fouled up"); 162 /* 163 * All other activity in this process is now stopped. 164 * Remove excess KSEs and KSEGRPS. XXXKSE (when we have them) 165 * ... 166 * Turn off threading support. 167 */ 168 p->p_flag &= ~P_SA; 169 thread_single_end(); /* Don't need this any more. */ 170 } 171 /* 172 * With this state set: 173 * Any thread entering the kernel from userspace will thread_exit() 174 * in trap(). Any thread attempting to sleep will return immediatly 175 * with EINTR or EWOULDBLOCK, which will hopefully force them 176 * to back out to userland, freeing resources as they go, and 177 * anything attempting to return to userland will thread_exit() 178 * from userret(). thread_exit() will do a wakeup on p->p_numthreads 179 * if it transitions to 1. 180 */ 181 182 p->p_flag |= P_WEXIT; 183 PROC_UNLOCK(p); 184 185 /* Are we a task leader? */ 186 if (p == p->p_leader) { 187 mtx_lock(&ppeers_lock); 188 q = p->p_peers; 189 while (q != NULL) { 190 PROC_LOCK(q); 191 psignal(q, SIGKILL); 192 PROC_UNLOCK(q); 193 q = q->p_peers; 194 } 195 while (p->p_peers != NULL) 196 msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 197 mtx_unlock(&ppeers_lock); 198 } 199 200 #ifdef PGINPROF 201 mtx_lock(&Giant); 202 vmsizmon(); 203 mtx_unlock(&Giant); 204 #endif 205 PROC_LOCK(p); 206 _STOPEVENT(p, S_EXIT, rv); 207 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */ 208 PROC_UNLOCK(p); 209 210 /* 211 * Check if any loadable modules need anything done at process exit. 212 * e.g. SYSV IPC stuff 213 * XXX what if one of these generates an error? 214 */ 215 EVENTHANDLER_INVOKE(process_exit, p); 216 217 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 218 M_ZOMBIE, M_WAITOK); 219 /* 220 * If parent is waiting for us to exit or exec, 221 * P_PPWAIT is set; we will wakeup the parent below. 222 */ 223 PROC_LOCK(p); 224 stopprofclock(p); 225 p->p_flag &= ~(P_TRACED | P_PPWAIT); 226 SIGEMPTYSET(p->p_siglist); 227 SIGEMPTYSET(td->td_siglist); 228 229 /* 230 * Stop the real interval timer. If the handler is currently 231 * executing, prevent it from rearming itself and let it finish. 232 */ 233 if (timevalisset(&p->p_realtimer.it_value) && 234 callout_stop(&p->p_itcallout) == 0) { 235 timevalclear(&p->p_realtimer.it_interval); 236 msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 237 KASSERT(!timevalisset(&p->p_realtimer.it_value), 238 ("realtime timer is still armed")); 239 } 240 PROC_UNLOCK(p); 241 242 /* 243 * Reset any sigio structures pointing to us as a result of 244 * F_SETOWN with our pid. 245 */ 246 mtx_lock(&Giant); /* XXX: not sure if needed */ 247 funsetownlst(&p->p_sigiolst); 248 249 /* 250 * Close open files and release open-file table. 251 * This may block! 252 */ 253 fdfree(td); 254 mtx_unlock(&Giant); 255 256 /* 257 * Remove ourself from our leader's peer list and wake our leader. 258 */ 259 mtx_lock(&ppeers_lock); 260 if (p->p_leader->p_peers) { 261 q = p->p_leader; 262 while (q->p_peers != p) 263 q = q->p_peers; 264 q->p_peers = p->p_peers; 265 wakeup(p->p_leader); 266 } 267 mtx_unlock(&ppeers_lock); 268 269 mtx_lock(&Giant); 270 /* The next two chunks should probably be moved to vmspace_exit. */ 271 vm = p->p_vmspace; 272 /* 273 * Release user portion of address space. 274 * This releases references to vnodes, 275 * which could cause I/O if the file has been unlinked. 276 * Need to do this early enough that we can still sleep. 277 * Can't free the entire vmspace as the kernel stack 278 * may be mapped within that space also. 279 * 280 * Processes sharing the same vmspace may exit in one order, and 281 * get cleaned up by vmspace_exit() in a different order. The 282 * last exiting process to reach this point releases as much of 283 * the environment as it can, and the last process cleaned up 284 * by vmspace_exit() (which decrements exitingcnt) cleans up the 285 * remainder. 286 */ 287 ++vm->vm_exitingcnt; 288 if (--vm->vm_refcnt == 0) { 289 shmexit(vm); 290 vm_page_lock_queues(); 291 pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map), 292 vm_map_max(&vm->vm_map)); 293 vm_page_unlock_queues(); 294 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map), 295 vm_map_max(&vm->vm_map)); 296 } 297 298 sx_xlock(&proctree_lock); 299 if (SESS_LEADER(p)) { 300 struct session *sp; 301 302 sp = p->p_session; 303 if (sp->s_ttyvp) { 304 /* 305 * Controlling process. 306 * Signal foreground pgrp, 307 * drain controlling terminal 308 * and revoke access to controlling terminal. 309 */ 310 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 311 tp = sp->s_ttyp; 312 if (sp->s_ttyp->t_pgrp) { 313 PGRP_LOCK(sp->s_ttyp->t_pgrp); 314 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 315 PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 316 } 317 /* XXX tp should be locked. */ 318 sx_xunlock(&proctree_lock); 319 (void) ttywait(tp); 320 sx_xlock(&proctree_lock); 321 /* 322 * The tty could have been revoked 323 * if we blocked. 324 */ 325 if (sp->s_ttyvp) { 326 ttyvp = sp->s_ttyvp; 327 SESS_LOCK(p->p_session); 328 sp->s_ttyvp = NULL; 329 SESS_UNLOCK(p->p_session); 330 sx_xunlock(&proctree_lock); 331 VOP_REVOKE(ttyvp, REVOKEALL); 332 vrele(ttyvp); 333 sx_xlock(&proctree_lock); 334 } 335 } 336 if (sp->s_ttyvp) { 337 ttyvp = sp->s_ttyvp; 338 SESS_LOCK(p->p_session); 339 sp->s_ttyvp = NULL; 340 SESS_UNLOCK(p->p_session); 341 vrele(ttyvp); 342 } 343 /* 344 * s_ttyp is not zero'd; we use this to indicate 345 * that the session once had a controlling terminal. 346 * (for logging and informational purposes) 347 */ 348 } 349 SESS_LOCK(p->p_session); 350 sp->s_leader = NULL; 351 SESS_UNLOCK(p->p_session); 352 } 353 fixjobc(p, p->p_pgrp, 0); 354 sx_xunlock(&proctree_lock); 355 (void)acct_process(td); 356 mtx_unlock(&Giant); 357 #ifdef KTRACE 358 /* 359 * release trace file 360 */ 361 PROC_LOCK(p); 362 mtx_lock(&ktrace_mtx); 363 p->p_traceflag = 0; /* don't trace the vrele() */ 364 tracevp = p->p_tracevp; 365 p->p_tracevp = NULL; 366 tracecred = p->p_tracecred; 367 p->p_tracecred = NULL; 368 mtx_unlock(&ktrace_mtx); 369 PROC_UNLOCK(p); 370 if (tracevp != NULL) { 371 mtx_lock(&Giant); 372 vrele(tracevp); 373 mtx_unlock(&Giant); 374 } 375 if (tracecred != NULL) 376 crfree(tracecred); 377 #endif 378 /* 379 * Release reference to text vnode 380 */ 381 if ((vtmp = p->p_textvp) != NULL) { 382 p->p_textvp = NULL; 383 mtx_lock(&Giant); 384 vrele(vtmp); 385 mtx_unlock(&Giant); 386 } 387 388 /* 389 * Release our limits structure. 390 */ 391 PROC_LOCK(p); 392 plim = p->p_limit; 393 p->p_limit = NULL; 394 PROC_UNLOCK(p); 395 lim_free(plim); 396 397 /* 398 * Release this thread's reference to the ucred. The actual proc 399 * reference will stay around until the proc is harvested by 400 * wait(). At this point the ucred is immutable (no other threads 401 * from this proc are around that can change it) so we leave the 402 * per-thread ucred pointer intact in case it is needed although 403 * in theory nothing should be using it at this point. 404 */ 405 crfree(td->td_ucred); 406 407 /* 408 * Remove proc from allproc queue and pidhash chain. 409 * Place onto zombproc. Unlink from parent's child list. 410 */ 411 sx_xlock(&allproc_lock); 412 LIST_REMOVE(p, p_list); 413 LIST_INSERT_HEAD(&zombproc, p, p_list); 414 LIST_REMOVE(p, p_hash); 415 sx_xunlock(&allproc_lock); 416 417 sx_xlock(&proctree_lock); 418 q = LIST_FIRST(&p->p_children); 419 if (q != NULL) /* only need this if any child is S_ZOMB */ 420 wakeup(initproc); 421 for (; q != NULL; q = nq) { 422 nq = LIST_NEXT(q, p_sibling); 423 PROC_LOCK(q); 424 proc_reparent(q, initproc); 425 q->p_sigparent = SIGCHLD; 426 /* 427 * Traced processes are killed 428 * since their existence means someone is screwing up. 429 */ 430 if (q->p_flag & P_TRACED) { 431 q->p_flag &= ~P_TRACED; 432 psignal(q, SIGKILL); 433 } 434 PROC_UNLOCK(q); 435 } 436 437 /* 438 * Save exit status and final rusage info, adding in child rusage 439 * info and self times. 440 */ 441 mtx_lock(&Giant); 442 PROC_LOCK(p); 443 p->p_xstat = rv; 444 *p->p_ru = p->p_stats->p_ru; 445 mtx_lock_spin(&sched_lock); 446 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 447 mtx_unlock_spin(&sched_lock); 448 ruadd(p->p_ru, &p->p_stats->p_cru); 449 450 /* 451 * Notify interested parties of our demise. 452 */ 453 KNOTE(&p->p_klist, NOTE_EXIT); 454 mtx_unlock(&Giant); 455 /* 456 * Just delete all entries in the p_klist. At this point we won't 457 * report any more events, and there are nasty race conditions that 458 * can beat us if we don't. 459 */ 460 while (SLIST_FIRST(&p->p_klist)) 461 SLIST_REMOVE_HEAD(&p->p_klist, kn_selnext); 462 463 /* 464 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 465 * flag set, or if the handler is set to SIG_IGN, notify process 466 * 1 instead (and hope it will handle this situation). 467 */ 468 PROC_LOCK(p->p_pptr); 469 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 470 if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 471 struct proc *pp; 472 473 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 474 pp = p->p_pptr; 475 PROC_UNLOCK(pp); 476 proc_reparent(p, initproc); 477 p->p_sigparent = SIGCHLD; 478 PROC_LOCK(p->p_pptr); 479 /* 480 * If this was the last child of our parent, notify 481 * parent, so in case he was wait(2)ing, he will 482 * continue. 483 */ 484 if (LIST_EMPTY(&pp->p_children)) 485 wakeup(pp); 486 } else 487 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 488 489 if (p->p_pptr == initproc) 490 psignal(p->p_pptr, SIGCHLD); 491 else if (p->p_sigparent != 0) 492 psignal(p->p_pptr, p->p_sigparent); 493 PROC_UNLOCK(p->p_pptr); 494 495 /* 496 * If this is a kthread, then wakeup anyone waiting for it to exit. 497 */ 498 if (p->p_flag & P_KTHREAD) 499 wakeup(p); 500 PROC_UNLOCK(p); 501 502 /* 503 * Finally, call machine-dependent code to release the remaining 504 * resources including address space. 505 * The address space is released by "vmspace_exitfree(p)" in 506 * vm_waitproc(). 507 */ 508 cpu_exit(td); 509 510 PROC_LOCK(p); 511 PROC_LOCK(p->p_pptr); 512 sx_xunlock(&proctree_lock); 513 514 while (mtx_owned(&Giant)) 515 mtx_unlock(&Giant); 516 517 /* 518 * We have to wait until after acquiring all locks before 519 * changing p_state. We need to avoid any possibly context 520 * switches while marked as a zombie including blocking on 521 * a mutex. 522 */ 523 mtx_lock_spin(&sched_lock); 524 p->p_state = PRS_ZOMBIE; 525 critical_enter(); 526 mtx_unlock_spin(&sched_lock); 527 528 wakeup(p->p_pptr); 529 PROC_UNLOCK(p->p_pptr); 530 531 mtx_lock_spin(&sched_lock); 532 critical_exit(); 533 cnt.v_swtch++; 534 binuptime(PCPU_PTR(switchtime)); 535 PCPU_SET(switchticks, ticks); 536 537 cpu_sched_exit(td); /* XXXKSE check if this should be in thread_exit */ 538 /* 539 * Allow the scheduler to adjust the priority of the 540 * parent when a kseg is exiting. 541 */ 542 if (p->p_pid != 1) 543 sched_exit(p->p_pptr, p); 544 545 /* 546 * Make sure the scheduler takes this thread out of its tables etc. 547 * This will also release this thread's reference to the ucred. 548 * Other thread parts to release include pcb bits and such. 549 */ 550 thread_exit(); 551 } 552 553 #ifdef COMPAT_43 554 /* 555 * MPSAFE. The dirty work is handled by kern_wait(). 556 */ 557 int 558 owait(struct thread *td, struct owait_args *uap __unused) 559 { 560 int error, status; 561 562 error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 563 if (error == 0) 564 td->td_retval[1] = status; 565 return (error); 566 } 567 #endif /* COMPAT_43 */ 568 569 /* 570 * MPSAFE. The dirty work is handled by kern_wait(). 571 */ 572 int 573 wait4(struct thread *td, struct wait_args *uap) 574 { 575 struct rusage ru; 576 int error, status; 577 578 error = kern_wait(td, uap->pid, &status, uap->options, &ru); 579 if (uap->status != NULL && error == 0) 580 error = copyout(&status, uap->status, sizeof(status)); 581 if (uap->rusage != NULL && error == 0) 582 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 583 return (error); 584 } 585 586 int 587 kern_wait(struct thread *td, pid_t pid, int *status, int options, struct rusage *rusage) 588 { 589 int nfound; 590 struct proc *p, *q, *t; 591 int error; 592 593 q = td->td_proc; 594 if (pid == 0) { 595 PROC_LOCK(q); 596 pid = -q->p_pgid; 597 PROC_UNLOCK(q); 598 } 599 if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 600 return (EINVAL); 601 loop: 602 nfound = 0; 603 sx_xlock(&proctree_lock); 604 LIST_FOREACH(p, &q->p_children, p_sibling) { 605 PROC_LOCK(p); 606 if (pid != WAIT_ANY && 607 p->p_pid != pid && p->p_pgid != -pid) { 608 PROC_UNLOCK(p); 609 continue; 610 } 611 612 /* 613 * This special case handles a kthread spawned by linux_clone 614 * (see linux_misc.c). The linux_wait4 and linux_waitpid 615 * functions need to be able to distinguish between waiting 616 * on a process and waiting on a thread. It is a thread if 617 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 618 * signifies we want to wait for threads and not processes. 619 */ 620 if ((p->p_sigparent != SIGCHLD) ^ 621 ((options & WLINUXCLONE) != 0)) { 622 PROC_UNLOCK(p); 623 continue; 624 } 625 626 nfound++; 627 if (p->p_state == PRS_ZOMBIE) { 628 td->td_retval[0] = p->p_pid; 629 if (status) 630 *status = p->p_xstat; /* convert to int */ 631 if (rusage) 632 bcopy(p->p_ru, rusage, sizeof(struct rusage)); 633 634 /* 635 * If we got the child via a ptrace 'attach', 636 * we need to give it back to the old parent. 637 */ 638 PROC_UNLOCK(p); 639 if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 640 PROC_LOCK(p); 641 p->p_oppid = 0; 642 proc_reparent(p, t); 643 PROC_UNLOCK(p); 644 psignal(t, SIGCHLD); 645 wakeup(t); 646 PROC_UNLOCK(t); 647 sx_xunlock(&proctree_lock); 648 return (0); 649 } 650 651 /* 652 * Remove other references to this process to ensure 653 * we have an exclusive reference. 654 */ 655 sx_xlock(&allproc_lock); 656 LIST_REMOVE(p, p_list); /* off zombproc */ 657 sx_xunlock(&allproc_lock); 658 LIST_REMOVE(p, p_sibling); 659 leavepgrp(p); 660 sx_xunlock(&proctree_lock); 661 662 /* 663 * As a side effect of this lock, we know that 664 * all other writes to this proc are visible now, so 665 * no more locking is needed for p. 666 */ 667 mtx_lock(&Giant); 668 PROC_LOCK(p); 669 p->p_xstat = 0; /* XXX: why? */ 670 PROC_UNLOCK(p); 671 PROC_LOCK(q); 672 ruadd(&q->p_stats->p_cru, p->p_ru); 673 PROC_UNLOCK(q); 674 FREE(p->p_ru, M_ZOMBIE); 675 p->p_ru = NULL; 676 mtx_unlock(&Giant); 677 678 /* 679 * Decrement the count of procs running with this uid. 680 */ 681 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 682 683 /* 684 * Free credentials, arguments, and sigacts 685 */ 686 crfree(p->p_ucred); 687 p->p_ucred = NULL; 688 pargs_drop(p->p_args); 689 p->p_args = NULL; 690 sigacts_free(p->p_sigacts); 691 p->p_sigacts = NULL; 692 693 /* 694 * do any thread-system specific cleanups 695 */ 696 thread_wait(p); 697 698 /* 699 * Give vm and machine-dependent layer a chance 700 * to free anything that cpu_exit couldn't 701 * release while still running in process context. 702 */ 703 mtx_lock(&Giant); 704 vm_waitproc(p); 705 mtx_unlock(&Giant); 706 #ifdef MAC 707 mac_destroy_proc(p); 708 #endif 709 KASSERT(FIRST_THREAD_IN_PROC(p), 710 ("kern_wait: no residual thread!")); 711 uma_zfree(proc_zone, p); 712 sx_xlock(&allproc_lock); 713 nprocs--; 714 sx_xunlock(&allproc_lock); 715 return (0); 716 } 717 mtx_lock_spin(&sched_lock); 718 if (P_SHOULDSTOP(p) && (p->p_suspcount == p->p_numthreads) && 719 ((p->p_flag & P_WAITED) == 0) && 720 (p->p_flag & P_TRACED || options & WUNTRACED)) { 721 mtx_unlock_spin(&sched_lock); 722 p->p_flag |= P_WAITED; 723 sx_xunlock(&proctree_lock); 724 td->td_retval[0] = p->p_pid; 725 if (status) 726 *status = W_STOPCODE(p->p_xstat); 727 PROC_UNLOCK(p); 728 return (0); 729 } 730 mtx_unlock_spin(&sched_lock); 731 if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 732 sx_xunlock(&proctree_lock); 733 td->td_retval[0] = p->p_pid; 734 p->p_flag &= ~P_CONTINUED; 735 PROC_UNLOCK(p); 736 737 if (status) 738 *status = SIGCONT; 739 return (0); 740 } 741 PROC_UNLOCK(p); 742 } 743 if (nfound == 0) { 744 sx_xunlock(&proctree_lock); 745 return (ECHILD); 746 } 747 if (options & WNOHANG) { 748 sx_xunlock(&proctree_lock); 749 td->td_retval[0] = 0; 750 return (0); 751 } 752 PROC_LOCK(q); 753 sx_xunlock(&proctree_lock); 754 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 755 PROC_UNLOCK(q); 756 if (error) 757 return (error); 758 goto loop; 759 } 760 761 /* 762 * Make process 'parent' the new parent of process 'child'. 763 * Must be called with an exclusive hold of proctree lock. 764 */ 765 void 766 proc_reparent(struct proc *child, struct proc *parent) 767 { 768 769 sx_assert(&proctree_lock, SX_XLOCKED); 770 PROC_LOCK_ASSERT(child, MA_OWNED); 771 if (child->p_pptr == parent) 772 return; 773 774 LIST_REMOVE(child, p_sibling); 775 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 776 child->p_pptr = parent; 777 } 778