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 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_compat.h" 41 #include "opt_ktrace.h" 42 #include "opt_mac.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/sysproto.h> 47 #include <sys/eventhandler.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/sbuf.h> 60 #include <sys/signalvar.h> 61 #include <sys/sched.h> 62 #include <sys/sx.h> 63 #include <sys/syscallsubr.h> 64 #include <sys/syslog.h> 65 #include <sys/ptrace.h> 66 #include <sys/acct.h> /* for acct_process() function prototype */ 67 #include <sys/filedesc.h> 68 #include <sys/mac.h> 69 #include <sys/shm.h> 70 #include <sys/sem.h> 71 #ifdef KTRACE 72 #include <sys/ktrace.h> 73 #endif 74 75 #include <security/audit/audit.h> 76 77 #include <vm/vm.h> 78 #include <vm/vm_extern.h> 79 #include <vm/vm_param.h> 80 #include <vm/pmap.h> 81 #include <vm/vm_map.h> 82 #include <vm/vm_page.h> 83 #include <vm/uma.h> 84 85 /* Required to be non-static for SysVR4 emulator */ 86 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 87 88 /* Hook for NFS teardown procedure. */ 89 void (*nlminfo_release_p)(struct proc *p); 90 91 /* 92 * exit -- 93 * Death of process. 94 * 95 * MPSAFE 96 */ 97 void 98 sys_exit(struct thread *td, struct sys_exit_args *uap) 99 { 100 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 struct plimit *plim; 123 int locked, refcnt; 124 125 /* 126 * Drop Giant if caller has it. Eventually we should warn about 127 * being called with Giant held. 128 */ 129 while (mtx_owned(&Giant)) 130 mtx_unlock(&Giant); 131 132 p = td->td_proc; 133 if (p == initproc) { 134 printf("init died (signal %d, exit %d)\n", 135 WTERMSIG(rv), WEXITSTATUS(rv)); 136 panic("Going nowhere without my init!"); 137 } 138 139 /* 140 * MUST abort all other threads before proceeding past here. 141 */ 142 PROC_LOCK(p); 143 if (p->p_flag & P_HADTHREADS) { 144 retry: 145 /* 146 * First check if some other thread got here before us.. 147 * if so, act apropriatly, (exit or suspend); 148 */ 149 thread_suspend_check(0); 150 151 /* 152 * Kill off the other threads. This requires 153 * some co-operation from other parts of the kernel 154 * so it may not be instantaneous. With this state set 155 * any thread entering the kernel from userspace will 156 * thread_exit() in trap(). Any thread attempting to 157 * sleep will return immediately with EINTR or EWOULDBLOCK 158 * which will hopefully force them to back out to userland 159 * freeing resources as they go. Any thread attempting 160 * to return to userland will thread_exit() from userret(). 161 * thread_exit() will unsuspend us when the last of the 162 * other threads exits. 163 * If there is already a thread singler after resumption, 164 * calling thread_single will fail; in that case, we just 165 * re-check all suspension request, the thread should 166 * either be suspended there or exit. 167 */ 168 if (thread_single(SINGLE_EXIT)) 169 goto retry; 170 171 /* 172 * All other activity in this process is now stopped. 173 * Threading support has been turned off. 174 */ 175 } 176 177 /* 178 * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 179 * on our vmspace, so we should block below until they have 180 * released their reference to us. Note that if they have 181 * requested S_EXIT stops we will block here until they ack 182 * via PIOCCONT. 183 */ 184 _STOPEVENT(p, S_EXIT, rv); 185 186 /* 187 * Note that we are exiting and do another wakeup of anyone in 188 * PIOCWAIT in case they aren't listening for S_EXIT stops or 189 * decided to wait again after we told them we are exiting. 190 */ 191 p->p_flag |= P_WEXIT; 192 wakeup(&p->p_stype); 193 194 /* 195 * Wait for any processes that have a hold on our vmspace to 196 * release their reference. 197 */ 198 while (p->p_lock > 0) 199 msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 200 201 PROC_LOCK(p->p_pptr); 202 sigqueue_take(p->p_ksi); 203 PROC_UNLOCK(p->p_pptr); 204 205 PROC_UNLOCK(p); 206 207 #ifdef AUDIT 208 /* 209 * The Sun BSM exit token contains two components: an exit status as 210 * passed to exit(), and a return value to indicate what sort of exit 211 * it was. The exit status is WEXITSTATUS(rv), but it's not clear 212 * what the return value is. 213 */ 214 AUDIT_ARG(exit, WEXITSTATUS(rv), 0); 215 AUDIT_SYSCALL_EXIT(0, td); 216 #endif 217 218 /* Are we a task leader? */ 219 if (p == p->p_leader) { 220 mtx_lock(&ppeers_lock); 221 q = p->p_peers; 222 while (q != NULL) { 223 PROC_LOCK(q); 224 psignal(q, SIGKILL); 225 PROC_UNLOCK(q); 226 q = q->p_peers; 227 } 228 while (p->p_peers != NULL) 229 msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 230 mtx_unlock(&ppeers_lock); 231 } 232 233 /* 234 * Check if any loadable modules need anything done at process exit. 235 * E.g. SYSV IPC stuff 236 * XXX what if one of these generates an error? 237 */ 238 EVENTHANDLER_INVOKE(process_exit, p); 239 240 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 241 M_ZOMBIE, M_WAITOK); 242 /* 243 * If parent is waiting for us to exit or exec, 244 * P_PPWAIT is set; we will wakeup the parent below. 245 */ 246 PROC_LOCK(p); 247 stopprofclock(p); 248 p->p_flag &= ~(P_TRACED | P_PPWAIT); 249 250 /* 251 * Stop the real interval timer. If the handler is currently 252 * executing, prevent it from rearming itself and let it finish. 253 */ 254 if (timevalisset(&p->p_realtimer.it_value) && 255 callout_stop(&p->p_itcallout) == 0) { 256 timevalclear(&p->p_realtimer.it_interval); 257 msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 258 KASSERT(!timevalisset(&p->p_realtimer.it_value), 259 ("realtime timer is still armed")); 260 } 261 sigqueue_flush(&p->p_sigqueue); 262 sigqueue_flush(&td->td_sigqueue); 263 PROC_UNLOCK(p); 264 265 /* 266 * Reset any sigio structures pointing to us as a result of 267 * F_SETOWN with our pid. 268 */ 269 mtx_lock(&Giant); /* XXX: not sure if needed */ 270 funsetownlst(&p->p_sigiolst); 271 mtx_unlock(&Giant); 272 273 /* 274 * If this process has an nlminfo data area (for lockd), release it 275 */ 276 if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 277 (*nlminfo_release_p)(p); 278 279 /* 280 * Close open files and release open-file table. 281 * This may block! 282 */ 283 fdfree(td); 284 285 /* 286 * If this thread tickled GEOM, we need to wait for the giggling to 287 * stop before we return to userland 288 */ 289 if (td->td_pflags & TDP_GEOM) 290 g_waitidle(); 291 292 /* 293 * Remove ourself from our leader's peer list and wake our leader. 294 */ 295 mtx_lock(&ppeers_lock); 296 if (p->p_leader->p_peers) { 297 q = p->p_leader; 298 while (q->p_peers != p) 299 q = q->p_peers; 300 q->p_peers = p->p_peers; 301 wakeup(p->p_leader); 302 } 303 mtx_unlock(&ppeers_lock); 304 305 /* The next two chunks should probably be moved to vmspace_exit. */ 306 vm = p->p_vmspace; 307 /* 308 * Release user portion of address space. 309 * This releases references to vnodes, 310 * which could cause I/O if the file has been unlinked. 311 * Need to do this early enough that we can still sleep. 312 * Can't free the entire vmspace as the kernel stack 313 * may be mapped within that space also. 314 * 315 * Processes sharing the same vmspace may exit in one order, and 316 * get cleaned up by vmspace_exit() in a different order. The 317 * last exiting process to reach this point releases as much of 318 * the environment as it can, and the last process cleaned up 319 * by vmspace_exit() (which decrements exitingcnt) cleans up the 320 * remainder. 321 */ 322 atomic_add_int(&vm->vm_exitingcnt, 1); 323 do 324 refcnt = vm->vm_refcnt; 325 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 326 if (refcnt == 1) { 327 shmexit(vm); 328 pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map), 329 vm_map_max(&vm->vm_map)); 330 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map), 331 vm_map_max(&vm->vm_map)); 332 } 333 334 sx_xlock(&proctree_lock); 335 if (SESS_LEADER(p)) { 336 struct session *sp; 337 338 sp = p->p_session; 339 if (sp->s_ttyvp) { 340 locked = VFS_LOCK_GIANT(sp->s_ttyvp->v_mount); 341 /* 342 * Controlling process. 343 * Signal foreground pgrp, 344 * drain controlling terminal 345 * and revoke access to controlling terminal. 346 */ 347 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 348 tp = sp->s_ttyp; 349 if (sp->s_ttyp->t_pgrp) { 350 PGRP_LOCK(sp->s_ttyp->t_pgrp); 351 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 352 PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 353 } 354 /* XXX tp should be locked. */ 355 sx_xunlock(&proctree_lock); 356 (void) ttywait(tp); 357 sx_xlock(&proctree_lock); 358 /* 359 * The tty could have been revoked 360 * if we blocked. 361 */ 362 if (sp->s_ttyvp) { 363 ttyvp = sp->s_ttyvp; 364 SESS_LOCK(p->p_session); 365 sp->s_ttyvp = NULL; 366 SESS_UNLOCK(p->p_session); 367 sx_xunlock(&proctree_lock); 368 VOP_LOCK(ttyvp, LK_EXCLUSIVE, td); 369 VOP_REVOKE(ttyvp, REVOKEALL); 370 vput(ttyvp); 371 sx_xlock(&proctree_lock); 372 } 373 } 374 if (sp->s_ttyvp) { 375 ttyvp = sp->s_ttyvp; 376 SESS_LOCK(p->p_session); 377 sp->s_ttyvp = NULL; 378 SESS_UNLOCK(p->p_session); 379 vrele(ttyvp); 380 } 381 /* 382 * s_ttyp is not zero'd; we use this to indicate 383 * that the session once had a controlling terminal. 384 * (for logging and informational purposes) 385 */ 386 VFS_UNLOCK_GIANT(locked); 387 } 388 SESS_LOCK(p->p_session); 389 sp->s_leader = NULL; 390 SESS_UNLOCK(p->p_session); 391 } 392 fixjobc(p, p->p_pgrp, 0); 393 sx_xunlock(&proctree_lock); 394 (void)acct_process(td); 395 #ifdef KTRACE 396 /* 397 * Drain any pending records on the thread and release the trace 398 * file. It might be better if drain-and-clear were atomic. 399 */ 400 ktrprocexit(td); 401 PROC_LOCK(p); 402 mtx_lock(&ktrace_mtx); 403 p->p_traceflag = 0; /* don't trace the vrele() */ 404 tracevp = p->p_tracevp; 405 p->p_tracevp = NULL; 406 tracecred = p->p_tracecred; 407 p->p_tracecred = NULL; 408 mtx_unlock(&ktrace_mtx); 409 PROC_UNLOCK(p); 410 if (tracevp != NULL) { 411 locked = VFS_LOCK_GIANT(tracevp->v_mount); 412 vrele(tracevp); 413 VFS_UNLOCK_GIANT(locked); 414 } 415 if (tracecred != NULL) 416 crfree(tracecred); 417 #endif 418 /* 419 * Release reference to text vnode 420 */ 421 if ((vtmp = p->p_textvp) != NULL) { 422 p->p_textvp = NULL; 423 locked = VFS_LOCK_GIANT(vtmp->v_mount); 424 vrele(vtmp); 425 VFS_UNLOCK_GIANT(locked); 426 } 427 428 /* 429 * Release our limits structure. 430 */ 431 PROC_LOCK(p); 432 plim = p->p_limit; 433 p->p_limit = NULL; 434 PROC_UNLOCK(p); 435 lim_free(plim); 436 437 /* 438 * Remove proc from allproc queue and pidhash chain. 439 * Place onto zombproc. Unlink from parent's child list. 440 */ 441 sx_xlock(&allproc_lock); 442 LIST_REMOVE(p, p_list); 443 LIST_INSERT_HEAD(&zombproc, p, p_list); 444 LIST_REMOVE(p, p_hash); 445 sx_xunlock(&allproc_lock); 446 447 /* 448 * Reparent all of our children to init. 449 */ 450 sx_xlock(&proctree_lock); 451 q = LIST_FIRST(&p->p_children); 452 if (q != NULL) /* only need this if any child is S_ZOMB */ 453 wakeup(initproc); 454 for (; q != NULL; q = nq) { 455 nq = LIST_NEXT(q, p_sibling); 456 PROC_LOCK(q); 457 proc_reparent(q, initproc); 458 q->p_sigparent = SIGCHLD; 459 /* 460 * Traced processes are killed 461 * since their existence means someone is screwing up. 462 */ 463 if (q->p_flag & P_TRACED) { 464 q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 465 psignal(q, SIGKILL); 466 } 467 PROC_UNLOCK(q); 468 } 469 470 /* 471 * Save exit status and finalize rusage info except for times, 472 * adding in child rusage info later when our time is locked. 473 */ 474 PROC_LOCK(p); 475 p->p_xstat = rv; 476 p->p_xthread = td; 477 p->p_stats->p_ru.ru_nvcsw++; 478 *p->p_ru = p->p_stats->p_ru; 479 480 /* 481 * Notify interested parties of our demise. 482 */ 483 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 484 485 /* 486 * Just delete all entries in the p_klist. At this point we won't 487 * report any more events, and there are nasty race conditions that 488 * can beat us if we don't. 489 */ 490 knlist_clear(&p->p_klist, 1); 491 492 /* 493 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 494 * flag set, or if the handler is set to SIG_IGN, notify process 495 * 1 instead (and hope it will handle this situation). 496 */ 497 PROC_LOCK(p->p_pptr); 498 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 499 if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 500 struct proc *pp; 501 502 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 503 pp = p->p_pptr; 504 PROC_UNLOCK(pp); 505 proc_reparent(p, initproc); 506 p->p_sigparent = SIGCHLD; 507 PROC_LOCK(p->p_pptr); 508 /* 509 * If this was the last child of our parent, notify 510 * parent, so in case he was wait(2)ing, he will 511 * continue. 512 */ 513 if (LIST_EMPTY(&pp->p_children)) 514 wakeup(pp); 515 } else 516 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 517 518 if (p->p_pptr == initproc) 519 psignal(p->p_pptr, SIGCHLD); 520 else if (p->p_sigparent != 0) { 521 if (p->p_sigparent == SIGCHLD) 522 childproc_exited(p); 523 else /* LINUX thread */ 524 psignal(p->p_pptr, p->p_sigparent); 525 } 526 PROC_UNLOCK(p->p_pptr); 527 PROC_UNLOCK(p); 528 529 /* 530 * Finally, call machine-dependent code to release the remaining 531 * resources including address space. 532 * The address space is released by "vmspace_exitfree(p)" in 533 * vm_waitproc(). 534 */ 535 cpu_exit(td); 536 537 WITNESS_WARN(WARN_PANIC, &proctree_lock.sx_object, 538 "process (pid %d) exiting", p->p_pid); 539 540 PROC_LOCK(p); 541 PROC_LOCK(p->p_pptr); 542 sx_xunlock(&proctree_lock); 543 544 /* 545 * We have to wait until after acquiring all locks before 546 * changing p_state. We need to avoid all possible context 547 * switches (including ones from blocking on a mutex) while 548 * marked as a zombie. We also have to set the zombie state 549 * before we release the parent process' proc lock to avoid 550 * a lost wakeup. So, we first call wakeup, then we grab the 551 * sched lock, update the state, and release the parent process' 552 * proc lock. 553 */ 554 wakeup(p->p_pptr); 555 mtx_lock_spin(&sched_lock); 556 p->p_state = PRS_ZOMBIE; 557 PROC_UNLOCK(p->p_pptr); 558 559 sched_exit(p->p_pptr, td); 560 561 /* 562 * Hopefully no one will try to deliver a signal to the process this 563 * late in the game. 564 */ 565 knlist_destroy(&p->p_klist); 566 567 /* 568 * Make sure the scheduler takes this thread out of its tables etc. 569 * This will also release this thread's reference to the ucred. 570 * Other thread parts to release include pcb bits and such. 571 */ 572 thread_exit(); 573 } 574 575 576 #ifndef _SYS_SYSPROTO_H_ 577 struct abort2_args { 578 char *why; 579 int nargs; 580 void **args; 581 }; 582 #endif 583 584 /* 585 * MPSAFE. 586 */ 587 int 588 abort2(struct thread *td, struct abort2_args *uap) 589 { 590 struct proc *p = td->td_proc; 591 struct sbuf *sb; 592 void *uargs[16]; 593 int error, i, sig; 594 595 error = 0; /* satisfy compiler */ 596 597 /* 598 * Do it right now so we can log either proper call of abort2(), or 599 * note, that invalid argument was passed. 512 is big enough to 600 * handle 16 arguments' descriptions with additional comments. 601 */ 602 sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 603 sbuf_clear(sb); 604 sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 605 p->p_comm, p->p_pid, td->td_ucred->cr_uid); 606 /* 607 * Since we can't return from abort2(), send SIGKILL in cases, where 608 * abort2() was called improperly 609 */ 610 sig = SIGKILL; 611 /* Prevent from DoSes from user-space. */ 612 if (uap->nargs < 0 || uap->nargs > 16) 613 goto out; 614 if (uap->args == NULL) 615 goto out; 616 error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 617 if (error != 0) 618 goto out; 619 /* 620 * Limit size of 'reason' string to 128. Will fit even when 621 * maximal number of arguments was chosen to be logged. 622 */ 623 if (uap->why != NULL) { 624 error = sbuf_copyin(sb, uap->why, 128); 625 if (error < 0) 626 goto out; 627 } else { 628 sbuf_printf(sb, "(null)"); 629 } 630 if (uap->nargs) { 631 sbuf_printf(sb, "("); 632 for (i = 0;i < uap->nargs; i++) 633 sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 634 sbuf_printf(sb, ")"); 635 } 636 /* 637 * Final stage: arguments were proper, string has been 638 * successfully copied from userspace, and copying pointers 639 * from user-space succeed. 640 */ 641 sig = SIGABRT; 642 out: 643 if (sig == SIGKILL) { 644 sbuf_trim(sb); 645 sbuf_printf(sb, " (Reason text inaccessible)"); 646 } 647 sbuf_cat(sb, "\n"); 648 sbuf_finish(sb); 649 log(LOG_INFO, "%s", sbuf_data(sb)); 650 sbuf_delete(sb); 651 exit1(td, W_EXITCODE(0, sig)); 652 return (0); 653 } 654 655 656 #ifdef COMPAT_43 657 /* 658 * The dirty work is handled by kern_wait(). 659 * 660 * MPSAFE. 661 */ 662 int 663 owait(struct thread *td, struct owait_args *uap __unused) 664 { 665 int error, status; 666 667 error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 668 if (error == 0) 669 td->td_retval[1] = status; 670 return (error); 671 } 672 #endif /* COMPAT_43 */ 673 674 /* 675 * The dirty work is handled by kern_wait(). 676 * 677 * MPSAFE. 678 */ 679 int 680 wait4(struct thread *td, struct wait_args *uap) 681 { 682 struct rusage ru, *rup; 683 int error, status; 684 685 if (uap->rusage != NULL) 686 rup = &ru; 687 else 688 rup = NULL; 689 error = kern_wait(td, uap->pid, &status, uap->options, rup); 690 if (uap->status != NULL && error == 0) 691 error = copyout(&status, uap->status, sizeof(status)); 692 if (uap->rusage != NULL && error == 0) 693 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 694 return (error); 695 } 696 697 int 698 kern_wait(struct thread *td, pid_t pid, int *status, int options, 699 struct rusage *rusage) 700 { 701 struct proc *p, *q, *t; 702 int error, nfound; 703 704 AUDIT_ARG(pid, pid); 705 706 q = td->td_proc; 707 if (pid == 0) { 708 PROC_LOCK(q); 709 pid = -q->p_pgid; 710 PROC_UNLOCK(q); 711 } 712 if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 713 return (EINVAL); 714 loop: 715 if (q->p_flag & P_STATCHILD) { 716 PROC_LOCK(q); 717 q->p_flag &= ~P_STATCHILD; 718 PROC_UNLOCK(q); 719 } 720 nfound = 0; 721 sx_xlock(&proctree_lock); 722 LIST_FOREACH(p, &q->p_children, p_sibling) { 723 PROC_LOCK(p); 724 if (pid != WAIT_ANY && 725 p->p_pid != pid && p->p_pgid != -pid) { 726 PROC_UNLOCK(p); 727 continue; 728 } 729 if (p_canwait(td, p)) { 730 PROC_UNLOCK(p); 731 continue; 732 } 733 734 /* 735 * This special case handles a kthread spawned by linux_clone 736 * (see linux_misc.c). The linux_wait4 and linux_waitpid 737 * functions need to be able to distinguish between waiting 738 * on a process and waiting on a thread. It is a thread if 739 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 740 * signifies we want to wait for threads and not processes. 741 */ 742 if ((p->p_sigparent != SIGCHLD) ^ 743 ((options & WLINUXCLONE) != 0)) { 744 PROC_UNLOCK(p); 745 continue; 746 } 747 748 nfound++; 749 if (p->p_state == PRS_ZOMBIE) { 750 751 /* 752 * It is possible that the last thread of this 753 * process is still running on another CPU 754 * in thread_exit() after having dropped the process 755 * lock via PROC_UNLOCK() but before it has completed 756 * cpu_throw(). In that case, the other thread must 757 * still hold sched_lock, so simply by acquiring 758 * sched_lock once we will wait long enough for the 759 * thread to exit in that case. 760 */ 761 mtx_lock_spin(&sched_lock); 762 mtx_unlock_spin(&sched_lock); 763 764 td->td_retval[0] = p->p_pid; 765 if (status) 766 *status = p->p_xstat; /* convert to int */ 767 if (rusage) { 768 *rusage = *p->p_ru; 769 calcru(p, &rusage->ru_utime, &rusage->ru_stime); 770 } 771 772 PROC_LOCK(q); 773 sigqueue_take(p->p_ksi); 774 PROC_UNLOCK(q); 775 776 /* 777 * If we got the child via a ptrace 'attach', 778 * we need to give it back to the old parent. 779 */ 780 PROC_UNLOCK(p); 781 if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 782 PROC_LOCK(p); 783 p->p_oppid = 0; 784 proc_reparent(p, t); 785 PROC_UNLOCK(p); 786 tdsignal(t, NULL, SIGCHLD, p->p_ksi); 787 wakeup(t); 788 PROC_UNLOCK(t); 789 sx_xunlock(&proctree_lock); 790 return (0); 791 } 792 793 /* 794 * Remove other references to this process to ensure 795 * we have an exclusive reference. 796 */ 797 sx_xlock(&allproc_lock); 798 LIST_REMOVE(p, p_list); /* off zombproc */ 799 sx_xunlock(&allproc_lock); 800 LIST_REMOVE(p, p_sibling); 801 leavepgrp(p); 802 sx_xunlock(&proctree_lock); 803 804 /* 805 * As a side effect of this lock, we know that 806 * all other writes to this proc are visible now, so 807 * no more locking is needed for p. 808 */ 809 PROC_LOCK(p); 810 p->p_xstat = 0; /* XXX: why? */ 811 PROC_UNLOCK(p); 812 PROC_LOCK(q); 813 ruadd(&q->p_stats->p_cru, &q->p_crux, p->p_ru, 814 &p->p_rux); 815 PROC_UNLOCK(q); 816 FREE(p->p_ru, M_ZOMBIE); 817 p->p_ru = NULL; 818 819 /* 820 * Decrement the count of procs running with this uid. 821 */ 822 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 823 824 /* 825 * Free credentials, arguments, and sigacts. 826 */ 827 crfree(p->p_ucred); 828 p->p_ucred = NULL; 829 pargs_drop(p->p_args); 830 p->p_args = NULL; 831 sigacts_free(p->p_sigacts); 832 p->p_sigacts = NULL; 833 834 /* 835 * Do any thread-system specific cleanups. 836 */ 837 thread_wait(p); 838 839 /* 840 * Give vm and machine-dependent layer a chance 841 * to free anything that cpu_exit couldn't 842 * release while still running in process context. 843 */ 844 vm_waitproc(p); 845 #ifdef MAC 846 mac_destroy_proc(p); 847 #endif 848 #ifdef AUDIT 849 audit_proc_free(p); 850 #endif 851 KASSERT(FIRST_THREAD_IN_PROC(p), 852 ("kern_wait: no residual thread!")); 853 uma_zfree(proc_zone, p); 854 sx_xlock(&allproc_lock); 855 nprocs--; 856 sx_xunlock(&allproc_lock); 857 return (0); 858 } 859 mtx_lock_spin(&sched_lock); 860 if ((p->p_flag & P_STOPPED_SIG) && 861 (p->p_suspcount == p->p_numthreads) && 862 (p->p_flag & P_WAITED) == 0 && 863 (p->p_flag & P_TRACED || options & WUNTRACED)) { 864 mtx_unlock_spin(&sched_lock); 865 p->p_flag |= P_WAITED; 866 sx_xunlock(&proctree_lock); 867 td->td_retval[0] = p->p_pid; 868 if (status) 869 *status = W_STOPCODE(p->p_xstat); 870 PROC_UNLOCK(p); 871 872 PROC_LOCK(q); 873 sigqueue_take(p->p_ksi); 874 PROC_UNLOCK(q); 875 876 return (0); 877 } 878 mtx_unlock_spin(&sched_lock); 879 if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 880 sx_xunlock(&proctree_lock); 881 td->td_retval[0] = p->p_pid; 882 p->p_flag &= ~P_CONTINUED; 883 PROC_UNLOCK(p); 884 885 PROC_LOCK(q); 886 sigqueue_take(p->p_ksi); 887 PROC_UNLOCK(q); 888 889 if (status) 890 *status = SIGCONT; 891 return (0); 892 } 893 PROC_UNLOCK(p); 894 } 895 if (nfound == 0) { 896 sx_xunlock(&proctree_lock); 897 return (ECHILD); 898 } 899 if (options & WNOHANG) { 900 sx_xunlock(&proctree_lock); 901 td->td_retval[0] = 0; 902 return (0); 903 } 904 PROC_LOCK(q); 905 sx_xunlock(&proctree_lock); 906 if (q->p_flag & P_STATCHILD) { 907 q->p_flag &= ~P_STATCHILD; 908 error = 0; 909 } else 910 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 911 PROC_UNLOCK(q); 912 if (error) 913 return (error); 914 goto loop; 915 } 916 917 /* 918 * Make process 'parent' the new parent of process 'child'. 919 * Must be called with an exclusive hold of proctree lock. 920 */ 921 void 922 proc_reparent(struct proc *child, struct proc *parent) 923 { 924 925 sx_assert(&proctree_lock, SX_XLOCKED); 926 PROC_LOCK_ASSERT(child, MA_OWNED); 927 if (child->p_pptr == parent) 928 return; 929 930 LIST_REMOVE(child, p_sibling); 931 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 932 child->p_pptr = parent; 933 } 934