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