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