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