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