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