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/syscallsubr.h> 63 #include <sys/ptrace.h> 64 #include <sys/acct.h> /* for acct_process() function prototype */ 65 #include <sys/filedesc.h> 66 #include <sys/mac.h> 67 #include <sys/shm.h> 68 #include <sys/sem.h> 69 #ifdef KTRACE 70 #include <sys/ktrace.h> 71 #endif 72 73 #include <vm/vm.h> 74 #include <vm/vm_extern.h> 75 #include <vm/vm_param.h> 76 #include <vm/pmap.h> 77 #include <vm/vm_map.h> 78 #include <vm/vm_page.h> 79 #include <vm/uma.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 bintime new_switchtime; 107 struct proc *p, *nq, *q; 108 struct tty *tp; 109 struct vnode *ttyvp; 110 struct vmspace *vm; 111 struct vnode *vtmp; 112 #ifdef KTRACE 113 struct vnode *tracevp; 114 struct ucred *tracecred; 115 #endif 116 struct plimit *plim; 117 int refcnt; 118 119 /* 120 * Drop Giant if caller has it. Eventually we should warn about 121 * being called with Giant held. 122 */ 123 while (mtx_owned(&Giant)) 124 mtx_unlock(&Giant); 125 126 p = td->td_proc; 127 if (p == initproc) { 128 printf("init died (signal %d, exit %d)\n", 129 WTERMSIG(rv), WEXITSTATUS(rv)); 130 panic("Going nowhere without my init!"); 131 } 132 133 /* 134 * MUST abort all other threads before proceeding past here. 135 */ 136 PROC_LOCK(p); 137 if (p->p_flag & P_HADTHREADS) { 138 retry: 139 /* 140 * First check if some other thread got here before us.. 141 * if so, act apropriatly, (exit or suspend); 142 */ 143 thread_suspend_check(0); 144 145 /* 146 * Kill off the other threads. This requires 147 * some co-operation from other parts of the kernel 148 * so it may not be instantaneous. With this state set 149 * any thread entering the kernel from userspace will 150 * thread_exit() in trap(). Any thread attempting to 151 * sleep will return immediately with EINTR or EWOULDBLOCK 152 * which will hopefully force them to back out to userland 153 * freeing resources as they go. Any thread attempting 154 * to return to userland will thread_exit() from userret(). 155 * thread_exit() will unsuspend us when the last of the 156 * other threads exits. 157 * If there is already a thread singler after resumption, 158 * calling thread_single will fail; in that case, we just 159 * re-check all suspension request, the thread should 160 * either be suspended there or exit. 161 */ 162 if (thread_single(SINGLE_EXIT)) 163 goto retry; 164 165 /* 166 * All other activity in this process is now stopped. 167 * Threading support has been turned off. 168 */ 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 PROC_LOCK(p); 190 _STOPEVENT(p, S_EXIT, rv); 191 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */ 192 PROC_UNLOCK(p); 193 194 /* 195 * Check if any loadable modules need anything done at process exit. 196 * E.g. SYSV IPC stuff 197 * XXX what if one of these generates an error? 198 */ 199 EVENTHANDLER_INVOKE(process_exit, p); 200 201 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 202 M_ZOMBIE, M_WAITOK); 203 /* 204 * If parent is waiting for us to exit or exec, 205 * P_PPWAIT is set; we will wakeup the parent below. 206 */ 207 PROC_LOCK(p); 208 stopprofclock(p); 209 p->p_flag &= ~(P_TRACED | P_PPWAIT); 210 SIGEMPTYSET(p->p_siglist); 211 SIGEMPTYSET(td->td_siglist); 212 213 /* 214 * Stop the real interval timer. If the handler is currently 215 * executing, prevent it from rearming itself and let it finish. 216 */ 217 if (timevalisset(&p->p_realtimer.it_value) && 218 callout_stop(&p->p_itcallout) == 0) { 219 timevalclear(&p->p_realtimer.it_interval); 220 msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 221 KASSERT(!timevalisset(&p->p_realtimer.it_value), 222 ("realtime timer is still armed")); 223 } 224 PROC_UNLOCK(p); 225 226 /* 227 * Reset any sigio structures pointing to us as a result of 228 * F_SETOWN with our pid. 229 */ 230 mtx_lock(&Giant); /* XXX: not sure if needed */ 231 funsetownlst(&p->p_sigiolst); 232 233 /* 234 * Close open files and release open-file table. 235 * This may block! 236 */ 237 fdfree(td); 238 mtx_unlock(&Giant); 239 240 /* 241 * If this thread tickled GEOM, we need to wait for the giggling to 242 * stop before we return to userland 243 */ 244 if (td->td_pflags & TDP_GEOM) 245 g_waitidle(); 246 247 /* 248 * Remove ourself from our leader's peer list and wake our leader. 249 */ 250 mtx_lock(&ppeers_lock); 251 if (p->p_leader->p_peers) { 252 q = p->p_leader; 253 while (q->p_peers != p) 254 q = q->p_peers; 255 q->p_peers = p->p_peers; 256 wakeup(p->p_leader); 257 } 258 mtx_unlock(&ppeers_lock); 259 260 /* The next two chunks should probably be moved to vmspace_exit. */ 261 vm = p->p_vmspace; 262 /* 263 * Release user portion of address space. 264 * This releases references to vnodes, 265 * which could cause I/O if the file has been unlinked. 266 * Need to do this early enough that we can still sleep. 267 * Can't free the entire vmspace as the kernel stack 268 * may be mapped within that space also. 269 * 270 * Processes sharing the same vmspace may exit in one order, and 271 * get cleaned up by vmspace_exit() in a different order. The 272 * last exiting process to reach this point releases as much of 273 * the environment as it can, and the last process cleaned up 274 * by vmspace_exit() (which decrements exitingcnt) cleans up the 275 * remainder. 276 */ 277 atomic_add_int(&vm->vm_exitingcnt, 1); 278 do 279 refcnt = vm->vm_refcnt; 280 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 281 if (refcnt == 1) { 282 shmexit(vm); 283 pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map), 284 vm_map_max(&vm->vm_map)); 285 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map), 286 vm_map_max(&vm->vm_map)); 287 } 288 289 mtx_lock(&Giant); 290 sx_xlock(&proctree_lock); 291 if (SESS_LEADER(p)) { 292 struct session *sp; 293 294 sp = p->p_session; 295 if (sp->s_ttyvp) { 296 /* 297 * Controlling process. 298 * Signal foreground pgrp, 299 * drain controlling terminal 300 * and revoke access to controlling terminal. 301 */ 302 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 303 tp = sp->s_ttyp; 304 if (sp->s_ttyp->t_pgrp) { 305 PGRP_LOCK(sp->s_ttyp->t_pgrp); 306 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 307 PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 308 } 309 /* XXX tp should be locked. */ 310 sx_xunlock(&proctree_lock); 311 (void) ttywait(tp); 312 sx_xlock(&proctree_lock); 313 /* 314 * The tty could have been revoked 315 * if we blocked. 316 */ 317 if (sp->s_ttyvp) { 318 ttyvp = sp->s_ttyvp; 319 SESS_LOCK(p->p_session); 320 sp->s_ttyvp = NULL; 321 SESS_UNLOCK(p->p_session); 322 sx_xunlock(&proctree_lock); 323 VOP_REVOKE(ttyvp, REVOKEALL); 324 vrele(ttyvp); 325 sx_xlock(&proctree_lock); 326 } 327 } 328 if (sp->s_ttyvp) { 329 ttyvp = sp->s_ttyvp; 330 SESS_LOCK(p->p_session); 331 sp->s_ttyvp = NULL; 332 SESS_UNLOCK(p->p_session); 333 vrele(ttyvp); 334 } 335 /* 336 * s_ttyp is not zero'd; we use this to indicate 337 * that the session once had a controlling terminal. 338 * (for logging and informational purposes) 339 */ 340 } 341 SESS_LOCK(p->p_session); 342 sp->s_leader = NULL; 343 SESS_UNLOCK(p->p_session); 344 } 345 fixjobc(p, p->p_pgrp, 0); 346 sx_xunlock(&proctree_lock); 347 (void)acct_process(td); 348 mtx_unlock(&Giant); 349 #ifdef KTRACE 350 /* 351 * release trace file 352 */ 353 PROC_LOCK(p); 354 mtx_lock(&ktrace_mtx); 355 p->p_traceflag = 0; /* don't trace the vrele() */ 356 tracevp = p->p_tracevp; 357 p->p_tracevp = NULL; 358 tracecred = p->p_tracecred; 359 p->p_tracecred = NULL; 360 mtx_unlock(&ktrace_mtx); 361 PROC_UNLOCK(p); 362 if (tracevp != NULL) { 363 mtx_lock(&Giant); 364 vrele(tracevp); 365 mtx_unlock(&Giant); 366 } 367 if (tracecred != NULL) 368 crfree(tracecred); 369 #endif 370 /* 371 * Release reference to text vnode 372 */ 373 if ((vtmp = p->p_textvp) != NULL) { 374 p->p_textvp = NULL; 375 mtx_lock(&Giant); 376 vrele(vtmp); 377 mtx_unlock(&Giant); 378 } 379 380 /* 381 * Release our limits structure. 382 */ 383 PROC_LOCK(p); 384 plim = p->p_limit; 385 p->p_limit = NULL; 386 PROC_UNLOCK(p); 387 lim_free(plim); 388 389 /* 390 * Remove proc from allproc queue and pidhash chain. 391 * Place onto zombproc. Unlink from parent's child list. 392 */ 393 sx_xlock(&allproc_lock); 394 LIST_REMOVE(p, p_list); 395 LIST_INSERT_HEAD(&zombproc, p, p_list); 396 LIST_REMOVE(p, p_hash); 397 sx_xunlock(&allproc_lock); 398 399 sx_xlock(&proctree_lock); 400 q = LIST_FIRST(&p->p_children); 401 if (q != NULL) /* only need this if any child is S_ZOMB */ 402 wakeup(initproc); 403 for (; q != NULL; q = nq) { 404 nq = LIST_NEXT(q, p_sibling); 405 PROC_LOCK(q); 406 proc_reparent(q, initproc); 407 q->p_sigparent = SIGCHLD; 408 /* 409 * Traced processes are killed 410 * since their existence means someone is screwing up. 411 */ 412 if (q->p_flag & P_TRACED) { 413 q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 414 psignal(q, SIGKILL); 415 } 416 PROC_UNLOCK(q); 417 } 418 419 /* 420 * Save exit status and finalize rusage info except for times, 421 * adding in child rusage info. 422 */ 423 PROC_LOCK(p); 424 p->p_xstat = rv; 425 p->p_xthread = td; 426 p->p_stats->p_ru.ru_nvcsw++; 427 *p->p_ru = p->p_stats->p_ru; 428 ruadd(p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 429 430 /* 431 * Notify interested parties of our demise. 432 */ 433 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 434 435 /* 436 * Just delete all entries in the p_klist. At this point we won't 437 * report any more events, and there are nasty race conditions that 438 * can beat us if we don't. 439 */ 440 knlist_clear(&p->p_klist, 1); 441 442 /* 443 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 444 * flag set, or if the handler is set to SIG_IGN, notify process 445 * 1 instead (and hope it will handle this situation). 446 */ 447 PROC_LOCK(p->p_pptr); 448 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 449 if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 450 struct proc *pp; 451 452 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 453 pp = p->p_pptr; 454 PROC_UNLOCK(pp); 455 proc_reparent(p, initproc); 456 p->p_sigparent = SIGCHLD; 457 PROC_LOCK(p->p_pptr); 458 /* 459 * If this was the last child of our parent, notify 460 * parent, so in case he was wait(2)ing, he will 461 * continue. 462 */ 463 if (LIST_EMPTY(&pp->p_children)) 464 wakeup(pp); 465 } else 466 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 467 468 if (p->p_pptr == initproc) 469 psignal(p->p_pptr, SIGCHLD); 470 else if (p->p_sigparent != 0) 471 psignal(p->p_pptr, p->p_sigparent); 472 PROC_UNLOCK(p->p_pptr); 473 474 /* 475 * If this is a kthread, then wakeup anyone waiting for it to exit. 476 */ 477 if (p->p_flag & P_KTHREAD) 478 wakeup(p); 479 PROC_UNLOCK(p); 480 481 /* 482 * Finally, call machine-dependent code to release the remaining 483 * resources including address space. 484 * The address space is released by "vmspace_exitfree(p)" in 485 * vm_waitproc(). 486 */ 487 cpu_exit(td); 488 489 PROC_LOCK(p); 490 PROC_LOCK(p->p_pptr); 491 sx_xunlock(&proctree_lock); 492 493 /* 494 * We have to wait until after acquiring all locks before 495 * changing p_state. We need to avoid all possible context 496 * switches (including ones from blocking on a mutex) while 497 * marked as a zombie. 498 */ 499 mtx_lock_spin(&sched_lock); 500 p->p_state = PRS_ZOMBIE; 501 502 critical_enter(); 503 mtx_unlock_spin(&sched_lock); 504 wakeup(p->p_pptr); 505 PROC_UNLOCK(p->p_pptr); 506 mtx_lock_spin(&sched_lock); 507 critical_exit(); 508 509 /* Do the same timestamp bookkeeping that mi_switch() would do. */ 510 binuptime(&new_switchtime); 511 bintime_add(&p->p_rux.rux_runtime, &new_switchtime); 512 bintime_sub(&p->p_rux.rux_runtime, PCPU_PTR(switchtime)); 513 PCPU_SET(switchtime, new_switchtime); 514 PCPU_SET(switchticks, ticks); 515 cnt.v_swtch++; 516 517 sched_exit(p->p_pptr, td); 518 519 /* 520 * Hopefully no one will try to deliver a signal to the process this 521 * late in the game. 522 */ 523 knlist_destroy(&p->p_klist); 524 525 /* 526 * Make sure the scheduler takes this thread out of its tables etc. 527 * This will also release this thread's reference to the ucred. 528 * Other thread parts to release include pcb bits and such. 529 */ 530 thread_exit(); 531 } 532 533 #ifdef COMPAT_43 534 /* 535 * The dirty work is handled by kern_wait(). 536 * 537 * MPSAFE. 538 */ 539 int 540 owait(struct thread *td, struct owait_args *uap __unused) 541 { 542 int error, status; 543 544 error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 545 if (error == 0) 546 td->td_retval[1] = status; 547 return (error); 548 } 549 #endif /* COMPAT_43 */ 550 551 /* 552 * The dirty work is handled by kern_wait(). 553 * 554 * MPSAFE. 555 */ 556 int 557 wait4(struct thread *td, struct wait_args *uap) 558 { 559 struct rusage ru, *rup; 560 int error, status; 561 562 if (uap->rusage != NULL) 563 rup = &ru; 564 else 565 rup = NULL; 566 error = kern_wait(td, uap->pid, &status, uap->options, rup); 567 if (uap->status != NULL && error == 0) 568 error = copyout(&status, uap->status, sizeof(status)); 569 if (uap->rusage != NULL && error == 0) 570 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 571 return (error); 572 } 573 574 int 575 kern_wait(struct thread *td, pid_t pid, int *status, int options, 576 struct rusage *rusage) 577 { 578 struct proc *p, *q, *t; 579 int error, nfound; 580 581 q = td->td_proc; 582 if (pid == 0) { 583 PROC_LOCK(q); 584 pid = -q->p_pgid; 585 PROC_UNLOCK(q); 586 } 587 if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 588 return (EINVAL); 589 loop: 590 nfound = 0; 591 sx_xlock(&proctree_lock); 592 LIST_FOREACH(p, &q->p_children, p_sibling) { 593 PROC_LOCK(p); 594 if (pid != WAIT_ANY && 595 p->p_pid != pid && p->p_pgid != -pid) { 596 PROC_UNLOCK(p); 597 continue; 598 } 599 600 /* 601 * This special case handles a kthread spawned by linux_clone 602 * (see linux_misc.c). The linux_wait4 and linux_waitpid 603 * functions need to be able to distinguish between waiting 604 * on a process and waiting on a thread. It is a thread if 605 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 606 * signifies we want to wait for threads and not processes. 607 */ 608 if ((p->p_sigparent != SIGCHLD) ^ 609 ((options & WLINUXCLONE) != 0)) { 610 PROC_UNLOCK(p); 611 continue; 612 } 613 614 nfound++; 615 if (p->p_state == PRS_ZOMBIE) { 616 td->td_retval[0] = p->p_pid; 617 if (status) 618 *status = p->p_xstat; /* convert to int */ 619 if (rusage) { 620 *rusage = *p->p_ru; 621 calcru(p, &rusage->ru_utime, &rusage->ru_stime); 622 } 623 624 /* 625 * If we got the child via a ptrace 'attach', 626 * we need to give it back to the old parent. 627 */ 628 PROC_UNLOCK(p); 629 if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 630 PROC_LOCK(p); 631 p->p_oppid = 0; 632 proc_reparent(p, t); 633 PROC_UNLOCK(p); 634 psignal(t, SIGCHLD); 635 wakeup(t); 636 PROC_UNLOCK(t); 637 sx_xunlock(&proctree_lock); 638 return (0); 639 } 640 641 /* 642 * Remove other references to this process to ensure 643 * we have an exclusive reference. 644 */ 645 sx_xlock(&allproc_lock); 646 LIST_REMOVE(p, p_list); /* off zombproc */ 647 sx_xunlock(&allproc_lock); 648 LIST_REMOVE(p, p_sibling); 649 leavepgrp(p); 650 sx_xunlock(&proctree_lock); 651 652 /* 653 * As a side effect of this lock, we know that 654 * all other writes to this proc are visible now, so 655 * no more locking is needed for p. 656 */ 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, &q->p_crux, p->p_ru, 662 &p->p_rux); 663 PROC_UNLOCK(q); 664 FREE(p->p_ru, M_ZOMBIE); 665 p->p_ru = NULL; 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 vm_waitproc(p); 693 #ifdef MAC 694 mac_destroy_proc(p); 695 #endif 696 KASSERT(FIRST_THREAD_IN_PROC(p), 697 ("kern_wait: no residual thread!")); 698 uma_zfree(proc_zone, p); 699 sx_xlock(&allproc_lock); 700 nprocs--; 701 sx_xunlock(&allproc_lock); 702 return (0); 703 } 704 mtx_lock_spin(&sched_lock); 705 if (P_SHOULDSTOP(p) && p->p_suspcount == p->p_numthreads && 706 (p->p_flag & P_WAITED) == 0 && 707 (p->p_flag & P_TRACED || options & WUNTRACED)) { 708 mtx_unlock_spin(&sched_lock); 709 p->p_flag |= P_WAITED; 710 sx_xunlock(&proctree_lock); 711 td->td_retval[0] = p->p_pid; 712 if (status) 713 *status = W_STOPCODE(p->p_xstat); 714 PROC_UNLOCK(p); 715 return (0); 716 } 717 mtx_unlock_spin(&sched_lock); 718 if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 719 sx_xunlock(&proctree_lock); 720 td->td_retval[0] = p->p_pid; 721 p->p_flag &= ~P_CONTINUED; 722 PROC_UNLOCK(p); 723 724 if (status) 725 *status = SIGCONT; 726 return (0); 727 } 728 PROC_UNLOCK(p); 729 } 730 if (nfound == 0) { 731 sx_xunlock(&proctree_lock); 732 return (ECHILD); 733 } 734 if (options & WNOHANG) { 735 sx_xunlock(&proctree_lock); 736 td->td_retval[0] = 0; 737 return (0); 738 } 739 PROC_LOCK(q); 740 sx_xunlock(&proctree_lock); 741 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 742 PROC_UNLOCK(q); 743 if (error) 744 return (error); 745 goto loop; 746 } 747 748 /* 749 * Make process 'parent' the new parent of process 'child'. 750 * Must be called with an exclusive hold of proctree lock. 751 */ 752 void 753 proc_reparent(struct proc *child, struct proc *parent) 754 { 755 756 sx_assert(&proctree_lock, SX_XLOCKED); 757 PROC_LOCK_ASSERT(child, MA_OWNED); 758 if (child->p_pptr == parent) 759 return; 760 761 LIST_REMOVE(child, p_sibling); 762 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 763 child->p_pptr = parent; 764 } 765