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