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 * MUST abort all other threads before proceeding past here. 133 */ 134 PROC_LOCK(p); 135 if (p->p_flag & P_THREADED || 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 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 SIGEMPTYSET(td->td_siglist); 221 if (timevalisset(&p->p_realtimer.it_value)) 222 callout_stop(&p->p_itcallout); 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 funsetownlst(&p->p_sigiolst); 230 231 /* 232 * Close open files and release open-file table. 233 * This may block! 234 */ 235 fdfree(td); 236 237 /* 238 * Remove ourself from our leader's peer list and wake our leader. 239 */ 240 mtx_lock(&ppeers_lock); 241 if (p->p_leader->p_peers) { 242 q = p->p_leader; 243 while (q->p_peers != p) 244 q = q->p_peers; 245 q->p_peers = p->p_peers; 246 wakeup(p->p_leader); 247 } 248 mtx_unlock(&ppeers_lock); 249 250 /* The next two chunks should probably be moved to vmspace_exit. */ 251 vm = p->p_vmspace; 252 /* 253 * Release user portion of address space. 254 * This releases references to vnodes, 255 * which could cause I/O if the file has been unlinked. 256 * Need to do this early enough that we can still sleep. 257 * Can't free the entire vmspace as the kernel stack 258 * may be mapped within that space also. 259 * 260 * Processes sharing the same vmspace may exit in one order, and 261 * get cleaned up by vmspace_exit() in a different order. The 262 * last exiting process to reach this point releases as much of 263 * the environment as it can, and the last process cleaned up 264 * by vmspace_exit() (which decrements exitingcnt) cleans up the 265 * remainder. 266 */ 267 ++vm->vm_exitingcnt; 268 if (--vm->vm_refcnt == 0) { 269 shmexit(vm); 270 vm_page_lock_queues(); 271 pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map), 272 vm_map_max(&vm->vm_map)); 273 vm_page_unlock_queues(); 274 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map), 275 vm_map_max(&vm->vm_map)); 276 } 277 278 sx_xlock(&proctree_lock); 279 if (SESS_LEADER(p)) { 280 struct session *sp; 281 282 sp = p->p_session; 283 if (sp->s_ttyvp) { 284 /* 285 * Controlling process. 286 * Signal foreground pgrp, 287 * drain controlling terminal 288 * and revoke access to controlling terminal. 289 */ 290 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 291 tp = sp->s_ttyp; 292 if (sp->s_ttyp->t_pgrp) { 293 PGRP_LOCK(sp->s_ttyp->t_pgrp); 294 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 295 PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 296 } 297 /* XXX tp should be locked. */ 298 sx_xunlock(&proctree_lock); 299 (void) ttywait(tp); 300 sx_xlock(&proctree_lock); 301 /* 302 * The tty could have been revoked 303 * if we blocked. 304 */ 305 if (sp->s_ttyvp) { 306 ttyvp = sp->s_ttyvp; 307 SESS_LOCK(p->p_session); 308 sp->s_ttyvp = NULL; 309 SESS_UNLOCK(p->p_session); 310 sx_xunlock(&proctree_lock); 311 VOP_REVOKE(ttyvp, REVOKEALL); 312 vrele(ttyvp); 313 sx_xlock(&proctree_lock); 314 } 315 } 316 if (sp->s_ttyvp) { 317 ttyvp = sp->s_ttyvp; 318 SESS_LOCK(p->p_session); 319 sp->s_ttyvp = NULL; 320 SESS_UNLOCK(p->p_session); 321 vrele(ttyvp); 322 } 323 /* 324 * s_ttyp is not zero'd; we use this to indicate 325 * that the session once had a controlling terminal. 326 * (for logging and informational purposes) 327 */ 328 } 329 SESS_LOCK(p->p_session); 330 sp->s_leader = NULL; 331 SESS_UNLOCK(p->p_session); 332 } 333 fixjobc(p, p->p_pgrp, 0); 334 sx_xunlock(&proctree_lock); 335 (void)acct_process(td); 336 #ifdef KTRACE 337 /* 338 * release trace file 339 */ 340 PROC_LOCK(p); 341 mtx_lock(&ktrace_mtx); 342 p->p_traceflag = 0; /* don't trace the vrele() */ 343 tracevp = p->p_tracevp; 344 p->p_tracevp = NULL; 345 tracecred = p->p_tracecred; 346 p->p_tracecred = NULL; 347 mtx_unlock(&ktrace_mtx); 348 PROC_UNLOCK(p); 349 if (tracevp != NULL) 350 vrele(tracevp); 351 if (tracecred != NULL) 352 crfree(tracecred); 353 #endif 354 /* 355 * Release reference to text vnode 356 */ 357 if ((vtmp = p->p_textvp) != NULL) { 358 p->p_textvp = NULL; 359 vrele(vtmp); 360 } 361 362 /* 363 * Release our limits structure. 364 */ 365 mtx_assert(&Giant, MA_OWNED); 366 if (--p->p_limit->p_refcnt == 0) { 367 FREE(p->p_limit, M_SUBPROC); 368 p->p_limit = NULL; 369 } 370 371 /* 372 * Release this thread's reference to the ucred. The actual proc 373 * reference will stay around until the proc is harvested by 374 * wait(). At this point the ucred is immutable (no other threads 375 * from this proc are around that can change it) so we leave the 376 * per-thread ucred pointer intact in case it is needed although 377 * in theory nothing should be using it at this point. 378 */ 379 crfree(td->td_ucred); 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; 406 psignal(q, SIGKILL); 407 } 408 PROC_UNLOCK(q); 409 } 410 411 /* 412 * Save exit status and final rusage info, adding in child rusage 413 * info and self times. 414 */ 415 PROC_LOCK(p); 416 p->p_xstat = rv; 417 *p->p_ru = p->p_stats->p_ru; 418 mtx_lock_spin(&sched_lock); 419 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 420 mtx_unlock_spin(&sched_lock); 421 ruadd(p->p_ru, &p->p_stats->p_cru); 422 423 /* 424 * Notify interested parties of our demise. 425 */ 426 KNOTE(&p->p_klist, NOTE_EXIT); 427 428 /* 429 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 430 * flag set, or if the handler is set to SIG_IGN, notify process 431 * 1 instead (and hope it will handle this situation). 432 */ 433 PROC_LOCK(p->p_pptr); 434 if (p->p_pptr->p_procsig->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 435 struct proc *pp; 436 437 pp = p->p_pptr; 438 PROC_UNLOCK(pp); 439 proc_reparent(p, initproc); 440 PROC_LOCK(p->p_pptr); 441 /* 442 * If this was the last child of our parent, notify 443 * parent, so in case he was wait(2)ing, he will 444 * continue. 445 */ 446 if (LIST_EMPTY(&pp->p_children)) 447 wakeup(pp); 448 } 449 450 if (p->p_sigparent && p->p_pptr != initproc) 451 psignal(p->p_pptr, p->p_sigparent); 452 else 453 psignal(p->p_pptr, SIGCHLD); 454 PROC_UNLOCK(p->p_pptr); 455 456 /* 457 * If this is a kthread, then wakeup anyone waiting for it to exit. 458 */ 459 if (p->p_flag & P_KTHREAD) 460 wakeup(p); 461 PROC_UNLOCK(p); 462 463 /* 464 * Finally, call machine-dependent code to release the remaining 465 * resources including address space. 466 * The address space is released by "vmspace_exitfree(p)" in 467 * vm_waitproc(). 468 */ 469 cpu_exit(td); 470 471 PROC_LOCK(p); 472 PROC_LOCK(p->p_pptr); 473 sx_xunlock(&proctree_lock); 474 mtx_lock_spin(&sched_lock); 475 476 while (mtx_owned(&Giant)) 477 mtx_unlock(&Giant); 478 479 /* 480 * We have to wait until after acquiring all locks before 481 * changing p_state. If we block on a mutex then we will be 482 * back at SRUN when we resume and our parent will never 483 * harvest us. 484 */ 485 p->p_state = PRS_ZOMBIE; 486 487 wakeup(p->p_pptr); 488 PROC_UNLOCK(p->p_pptr); 489 cnt.v_swtch++; 490 binuptime(PCPU_PTR(switchtime)); 491 PCPU_SET(switchticks, ticks); 492 493 cpu_sched_exit(td); /* XXXKSE check if this should be in thread_exit */ 494 /* 495 * Allow the scheduler to adjust the priority of the 496 * parent when a kseg is exiting. 497 */ 498 if (p->p_pid != 1) 499 sched_exit(p->p_pptr, p); 500 501 /* 502 * Make sure the scheduler takes this thread out of its tables etc. 503 * This will also release this thread's reference to the ucred. 504 * Other thread parts to release include pcb bits and such. 505 */ 506 thread_exit(); 507 } 508 509 #ifdef COMPAT_43 510 /* 511 * MPSAFE. The dirty work is handled by wait1(). 512 */ 513 int 514 owait(struct thread *td, struct owait_args *uap __unused) 515 { 516 struct wait_args w; 517 518 w.options = 0; 519 w.rusage = NULL; 520 w.pid = WAIT_ANY; 521 w.status = NULL; 522 return (wait1(td, &w, 1)); 523 } 524 #endif /* COMPAT_43 */ 525 526 /* 527 * MPSAFE. The dirty work is handled by wait1(). 528 */ 529 int 530 wait4(struct thread *td, struct wait_args *uap) 531 { 532 533 return (wait1(td, uap, 0)); 534 } 535 536 /* 537 * MPSAFE 538 */ 539 static int 540 wait1(struct thread *td, struct wait_args *uap, int compat) 541 { 542 struct rusage ru; 543 int nfound; 544 struct proc *p, *q, *t; 545 int status, error; 546 547 q = td->td_proc; 548 if (uap->pid == 0) { 549 PROC_LOCK(q); 550 uap->pid = -q->p_pgid; 551 PROC_UNLOCK(q); 552 } 553 if (uap->options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 554 return (EINVAL); 555 mtx_lock(&Giant); 556 loop: 557 nfound = 0; 558 sx_xlock(&proctree_lock); 559 LIST_FOREACH(p, &q->p_children, p_sibling) { 560 PROC_LOCK(p); 561 if (uap->pid != WAIT_ANY && 562 p->p_pid != uap->pid && p->p_pgid != -uap->pid) { 563 PROC_UNLOCK(p); 564 continue; 565 } 566 567 /* 568 * This special case handles a kthread spawned by linux_clone 569 * (see linux_misc.c). The linux_wait4 and linux_waitpid 570 * functions need to be able to distinguish between waiting 571 * on a process and waiting on a thread. It is a thread if 572 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 573 * signifies we want to wait for threads and not processes. 574 */ 575 if ((p->p_sigparent != SIGCHLD) ^ 576 ((uap->options & WLINUXCLONE) != 0)) { 577 PROC_UNLOCK(p); 578 continue; 579 } 580 581 nfound++; 582 if (p->p_state == PRS_ZOMBIE) { 583 td->td_retval[0] = p->p_pid; 584 #ifdef COMPAT_43 585 if (compat) 586 td->td_retval[1] = p->p_xstat; 587 else 588 #endif 589 if (uap->status) { 590 status = p->p_xstat; /* convert to int */ 591 PROC_UNLOCK(p); 592 if ((error = copyout(&status, 593 uap->status, sizeof(status)))) { 594 sx_xunlock(&proctree_lock); 595 mtx_unlock(&Giant); 596 return (error); 597 } 598 PROC_LOCK(p); 599 } 600 if (uap->rusage) { 601 bcopy(p->p_ru, &ru, sizeof(ru)); 602 PROC_UNLOCK(p); 603 if ((error = copyout(&ru, 604 uap->rusage, sizeof (struct rusage)))) { 605 sx_xunlock(&proctree_lock); 606 mtx_unlock(&Giant); 607 return (error); 608 } 609 } else 610 PROC_UNLOCK(p); 611 /* 612 * If we got the child via a ptrace 'attach', 613 * we need to give it back to the old parent. 614 */ 615 if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 616 PROC_LOCK(p); 617 p->p_oppid = 0; 618 proc_reparent(p, t); 619 PROC_UNLOCK(p); 620 psignal(t, SIGCHLD); 621 wakeup(t); 622 PROC_UNLOCK(t); 623 sx_xunlock(&proctree_lock); 624 mtx_unlock(&Giant); 625 return (0); 626 } 627 628 /* 629 * Remove other references to this process to ensure 630 * we have an exclusive reference. 631 */ 632 sx_xlock(&allproc_lock); 633 LIST_REMOVE(p, p_list); /* off zombproc */ 634 sx_xunlock(&allproc_lock); 635 LIST_REMOVE(p, p_sibling); 636 leavepgrp(p); 637 sx_xunlock(&proctree_lock); 638 639 /* 640 * As a side effect of this lock, we know that 641 * all other writes to this proc are visible now, so 642 * no more locking is needed for p. 643 */ 644 PROC_LOCK(p); 645 p->p_xstat = 0; /* XXX: why? */ 646 PROC_UNLOCK(p); 647 PROC_LOCK(q); 648 ruadd(&q->p_stats->p_cru, p->p_ru); 649 PROC_UNLOCK(q); 650 FREE(p->p_ru, M_ZOMBIE); 651 p->p_ru = NULL; 652 653 /* 654 * Decrement the count of procs running with this uid. 655 */ 656 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 657 658 /* 659 * Free up credentials. 660 */ 661 crfree(p->p_ucred); 662 p->p_ucred = NULL; /* XXX: why? */ 663 664 /* 665 * Remove unused arguments 666 */ 667 pargs_drop(p->p_args); 668 p->p_args = NULL; 669 670 if (--p->p_procsig->ps_refcnt == 0) { 671 if (p->p_sigacts != &p->p_uarea->u_sigacts) 672 FREE(p->p_sigacts, M_SUBPROC); 673 FREE(p->p_procsig, M_SUBPROC); 674 p->p_procsig = NULL; 675 } 676 677 /* 678 * do any thread-system specific cleanups 679 */ 680 thread_wait(p); 681 682 /* 683 * Give vm and machine-dependent layer a chance 684 * to free anything that cpu_exit couldn't 685 * release while still running in process context. 686 */ 687 vm_waitproc(p); 688 #ifdef MAC 689 mac_destroy_proc(p); 690 #endif 691 KASSERT(FIRST_THREAD_IN_PROC(p), 692 ("wait1: no residual thread!")); 693 uma_zfree(proc_zone, p); 694 sx_xlock(&allproc_lock); 695 nprocs--; 696 sx_xunlock(&allproc_lock); 697 mtx_unlock(&Giant); 698 return (0); 699 } 700 mtx_lock_spin(&sched_lock); 701 if (P_SHOULDSTOP(p) && (p->p_suspcount == p->p_numthreads) && 702 ((p->p_flag & P_WAITED) == 0) && 703 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 704 mtx_unlock_spin(&sched_lock); 705 p->p_flag |= P_WAITED; 706 sx_xunlock(&proctree_lock); 707 td->td_retval[0] = p->p_pid; 708 #ifdef COMPAT_43 709 if (compat) { 710 td->td_retval[1] = W_STOPCODE(p->p_xstat); 711 PROC_UNLOCK(p); 712 error = 0; 713 } else 714 #endif 715 if (uap->status) { 716 status = W_STOPCODE(p->p_xstat); 717 PROC_UNLOCK(p); 718 error = copyout(&status, 719 uap->status, sizeof(status)); 720 } else { 721 PROC_UNLOCK(p); 722 error = 0; 723 } 724 mtx_unlock(&Giant); 725 return (error); 726 } 727 mtx_unlock_spin(&sched_lock); 728 if (uap->options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 729 sx_xunlock(&proctree_lock); 730 td->td_retval[0] = p->p_pid; 731 p->p_flag &= ~P_CONTINUED; 732 PROC_UNLOCK(p); 733 734 if (uap->status) { 735 status = SIGCONT; 736 error = copyout(&status, 737 uap->status, sizeof(status)); 738 } else 739 error = 0; 740 741 mtx_unlock(&Giant); 742 return (error); 743 } 744 PROC_UNLOCK(p); 745 } 746 if (nfound == 0) { 747 sx_xunlock(&proctree_lock); 748 mtx_unlock(&Giant); 749 return (ECHILD); 750 } 751 if (uap->options & WNOHANG) { 752 sx_xunlock(&proctree_lock); 753 td->td_retval[0] = 0; 754 mtx_unlock(&Giant); 755 return (0); 756 } 757 PROC_LOCK(q); 758 sx_xunlock(&proctree_lock); 759 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 760 PROC_UNLOCK(q); 761 if (error) { 762 mtx_unlock(&Giant); 763 return (error); 764 } 765 goto loop; 766 } 767 768 /* 769 * Make process 'parent' the new parent of process 'child'. 770 * Must be called with an exclusive hold of proctree lock. 771 */ 772 void 773 proc_reparent(struct proc *child, struct proc *parent) 774 { 775 776 sx_assert(&proctree_lock, SX_XLOCKED); 777 PROC_LOCK_ASSERT(child, MA_OWNED); 778 if (child->p_pptr == parent) 779 return; 780 781 LIST_REMOVE(child, p_sibling); 782 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 783 child->p_pptr = parent; 784 } 785