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