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 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 */ 77 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 78 79 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback"); 80 81 static int wait1(struct thread *, struct wait_args *, int); 82 83 /* 84 * callout list for things to do at exit time 85 */ 86 struct exitlist { 87 exitlist_fn function; 88 TAILQ_ENTRY(exitlist) next; 89 }; 90 91 TAILQ_HEAD(exit_list_head, exitlist); 92 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list); 93 94 /* 95 * exit -- 96 * Death of process. 97 * 98 * MPSAFE 99 */ 100 void 101 sys_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 */ 118 void 119 exit1(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 PGRPSESS_XLOCK(); 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 PGRPSESS_XUNLOCK(); 255 (void) ttywait(tp); 256 PGRPSESS_XLOCK(); 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 PGRPSESS_XUNLOCK(); 267 VOP_REVOKE(ttyvp, REVOKEALL); 268 PGRPSESS_XLOCK(); 269 vrele(ttyvp); 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 PGRPSESS_XUNLOCK(); 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, notify process 1 instead (and hope it will handle 372 * this situation). 373 */ 374 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) { 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 */ 461 int 462 owait(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 */ 481 int 482 wait4(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 */ 493 static int 494 wait1(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 mtx_lock(&Giant); 510 q = td->td_proc; 511 if (uap->pid == 0) { 512 PROC_LOCK(q); 513 uap->pid = -q->p_pgid; 514 PROC_UNLOCK(q); 515 } 516 if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) { 517 error = EINVAL; 518 goto done2; 519 } 520 loop: 521 nfound = 0; 522 sx_slock(&proctree_lock); 523 LIST_FOREACH(p, &q->p_children, p_sibling) { 524 PROC_LOCK(p); 525 if (uap->pid != WAIT_ANY && 526 p->p_pid != uap->pid && p->p_pgid != -uap->pid) { 527 PROC_UNLOCK(p); 528 continue; 529 } 530 531 /* 532 * This special case handles a kthread spawned by linux_clone 533 * (see linux_misc.c). The linux_wait4 and linux_waitpid 534 * functions need to be able to distinguish between waiting 535 * on a process and waiting on a thread. It is a thread if 536 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 537 * signifies we want to wait for threads and not processes. 538 */ 539 if ((p->p_sigparent != SIGCHLD) ^ 540 ((uap->options & WLINUXCLONE) != 0)) { 541 PROC_UNLOCK(p); 542 continue; 543 } 544 545 nfound++; 546 mtx_lock_spin(&sched_lock); 547 if (p->p_stat == SZOMB) { 548 /* 549 * charge childs scheduling cpu usage to parent 550 * XXXKSE assume only one thread & kse & ksegrp 551 * keep estcpu in each ksegrp 552 * so charge it to the ksegrp that did the wait 553 * since process estcpu is sum of all ksegrps, 554 * this is strictly as expected. 555 * Assume that the child process aggregated all 556 * tke estcpu into the 'build-in' ksegrp. 557 * XXXKSE 558 */ 559 if (curthread->td_proc->p_pid != 1) { 560 curthread->td_ksegrp->kg_estcpu = 561 ESTCPULIM(curthread->td_ksegrp->kg_estcpu + 562 p->p_ksegrp.kg_estcpu); 563 } 564 565 mtx_unlock_spin(&sched_lock); 566 PROC_UNLOCK(p); 567 sx_sunlock(&proctree_lock); 568 569 td->td_retval[0] = p->p_pid; 570 #ifdef COMPAT_43 571 if (compat) 572 td->td_retval[1] = p->p_xstat; 573 else 574 #endif 575 if (uap->status) { 576 status = p->p_xstat; /* convert to int */ 577 if ((error = copyout((caddr_t)&status, 578 (caddr_t)uap->status, sizeof(status)))) { 579 goto done2; 580 } 581 } 582 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 583 (caddr_t)uap->rusage, sizeof (struct rusage)))) { 584 goto done2; 585 } 586 /* 587 * If we got the child via a ptrace 'attach', 588 * we need to give it back to the old parent. 589 */ 590 sx_xlock(&proctree_lock); 591 if (p->p_oppid) { 592 if ((t = pfind(p->p_oppid)) != NULL) { 593 PROC_LOCK(p); 594 p->p_oppid = 0; 595 proc_reparent(p, t); 596 PROC_UNLOCK(p); 597 psignal(t, SIGCHLD); 598 wakeup((caddr_t)t); 599 PROC_UNLOCK(t); 600 sx_xunlock(&proctree_lock); 601 error = 0; 602 goto done2; 603 } 604 } 605 sx_xunlock(&proctree_lock); 606 PROC_LOCK(p); 607 p->p_xstat = 0; 608 pa = p->p_args; 609 p->p_args = NULL; 610 PROC_UNLOCK(p); 611 ruadd(&q->p_stats->p_cru, p->p_ru); 612 FREE(p->p_ru, M_ZOMBIE); 613 p->p_ru = NULL; 614 615 /* 616 * Decrement the count of procs running with this uid. 617 */ 618 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 619 620 /* 621 * Finally finished with old proc entry. 622 * Unlink it from its process group and free it. 623 */ 624 leavepgrp(p); 625 626 sx_xlock(&allproc_lock); 627 LIST_REMOVE(p, p_list); /* off zombproc */ 628 sx_xunlock(&allproc_lock); 629 630 sx_xlock(&proctree_lock); 631 LIST_REMOVE(p, p_sibling); 632 sx_xunlock(&proctree_lock); 633 634 /* 635 * Free up credentials. 636 */ 637 crfree(p->p_ucred); 638 p->p_ucred = NULL; 639 640 /* 641 * Remove unused arguments 642 */ 643 pargs_drop(pa); 644 645 if (--p->p_procsig->ps_refcnt == 0) { 646 if (p->p_sigacts != &p->p_uarea->u_sigacts) 647 FREE(p->p_sigacts, M_SUBPROC); 648 FREE(p->p_procsig, M_SUBPROC); 649 p->p_procsig = NULL; 650 } 651 652 /* 653 * Give vm and machine-dependent layer a chance 654 * to free anything that cpu_exit couldn't 655 * release while still running in process context. 656 */ 657 vm_waitproc(p); 658 mtx_destroy(&p->p_mtx); 659 uma_zfree(proc_zone, p); 660 nprocs--; 661 error = 0; 662 goto done2; 663 } 664 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 665 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 666 mtx_unlock_spin(&sched_lock); 667 p->p_flag |= P_WAITED; 668 PROC_UNLOCK(p); 669 sx_sunlock(&proctree_lock); 670 td->td_retval[0] = p->p_pid; 671 #ifdef COMPAT_43 672 if (compat) { 673 td->td_retval[1] = W_STOPCODE(p->p_xstat); 674 error = 0; 675 } else 676 #endif 677 if (uap->status) { 678 status = W_STOPCODE(p->p_xstat); 679 error = copyout((caddr_t)&status, 680 (caddr_t)uap->status, sizeof(status)); 681 } else 682 error = 0; 683 goto done2; 684 } 685 mtx_unlock_spin(&sched_lock); 686 PROC_UNLOCK(p); 687 } 688 sx_sunlock(&proctree_lock); 689 if (nfound == 0) { 690 error = ECHILD; 691 goto done2; 692 } 693 if (uap->options & WNOHANG) { 694 td->td_retval[0] = 0; 695 error = 0; 696 goto done2; 697 } 698 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 699 goto done2; 700 goto loop; 701 done2: 702 mtx_unlock(&Giant); 703 return(error); 704 } 705 706 /* 707 * Make process 'parent' the new parent of process 'child'. 708 * Must be called with an exclusive hold of proctree lock. 709 */ 710 void 711 proc_reparent(child, parent) 712 register struct proc *child; 713 register struct proc *parent; 714 { 715 716 sx_assert(&proctree_lock, SX_XLOCKED); 717 PROC_LOCK_ASSERT(child, MA_OWNED); 718 if (child->p_pptr == parent) 719 return; 720 721 LIST_REMOVE(child, p_sibling); 722 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 723 child->p_pptr = parent; 724 } 725 726 /* 727 * The next two functions are to handle adding/deleting items on the 728 * exit callout list 729 * 730 * at_exit(): 731 * Take the arguments given and put them onto the exit callout list, 732 * However first make sure that it's not already there. 733 * returns 0 on success. 734 */ 735 736 int 737 at_exit(function) 738 exitlist_fn function; 739 { 740 struct exitlist *ep; 741 742 #ifdef INVARIANTS 743 /* Be noisy if the programmer has lost track of things */ 744 if (rm_at_exit(function)) 745 printf("WARNING: exit callout entry (%p) already present\n", 746 function); 747 #endif 748 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT); 749 if (ep == NULL) 750 return (ENOMEM); 751 ep->function = function; 752 TAILQ_INSERT_TAIL(&exit_list, ep, next); 753 return (0); 754 } 755 756 /* 757 * Scan the exit callout list for the given item and remove it. 758 * Returns the number of items removed (0 or 1) 759 */ 760 int 761 rm_at_exit(function) 762 exitlist_fn function; 763 { 764 struct exitlist *ep; 765 766 TAILQ_FOREACH(ep, &exit_list, next) { 767 if (ep->function == function) { 768 TAILQ_REMOVE(&exit_list, ep, next); 769 free(ep, M_ATEXIT); 770 return(1); 771 } 772 } 773 return (0); 774 } 775