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 funsetownlst(&p->p_sigiolst); 270 271 /* 272 * If this process has an nlminfo data area (for lockd), release it 273 */ 274 if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 275 (*nlminfo_release_p)(p); 276 277 /* 278 * Close open files and release open-file table. 279 * This may block! 280 */ 281 fdfree(td); 282 283 /* 284 * If this thread tickled GEOM, we need to wait for the giggling to 285 * stop before we return to userland 286 */ 287 if (td->td_pflags & TDP_GEOM) 288 g_waitidle(); 289 290 /* 291 * Remove ourself from our leader's peer list and wake our leader. 292 */ 293 mtx_lock(&ppeers_lock); 294 if (p->p_leader->p_peers) { 295 q = p->p_leader; 296 while (q->p_peers != p) 297 q = q->p_peers; 298 q->p_peers = p->p_peers; 299 wakeup(p->p_leader); 300 } 301 mtx_unlock(&ppeers_lock); 302 303 /* The next two chunks should probably be moved to vmspace_exit. */ 304 vm = p->p_vmspace; 305 /* 306 * Release user portion of address space. 307 * This releases references to vnodes, 308 * which could cause I/O if the file has been unlinked. 309 * Need to do this early enough that we can still sleep. 310 * Can't free the entire vmspace as the kernel stack 311 * may be mapped within that space also. 312 * 313 * Processes sharing the same vmspace may exit in one order, and 314 * get cleaned up by vmspace_exit() in a different order. The 315 * last exiting process to reach this point releases as much of 316 * the environment as it can, and the last process cleaned up 317 * by vmspace_exit() (which decrements exitingcnt) cleans up the 318 * remainder. 319 */ 320 atomic_add_int(&vm->vm_exitingcnt, 1); 321 do 322 refcnt = vm->vm_refcnt; 323 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 324 if (refcnt == 1) { 325 shmexit(vm); 326 pmap_remove_pages(vmspace_pmap(vm)); 327 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map), 328 vm_map_max(&vm->vm_map)); 329 } 330 331 sx_xlock(&proctree_lock); 332 if (SESS_LEADER(p)) { 333 struct session *sp; 334 335 sp = p->p_session; 336 if (sp->s_ttyvp) { 337 locked = VFS_LOCK_GIANT(sp->s_ttyvp->v_mount); 338 /* 339 * Controlling process. 340 * Signal foreground pgrp, 341 * drain controlling terminal 342 * and revoke access to controlling terminal. 343 */ 344 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 345 tp = sp->s_ttyp; 346 if (sp->s_ttyp->t_pgrp) { 347 PGRP_LOCK(sp->s_ttyp->t_pgrp); 348 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 349 PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 350 } 351 /* XXX tp should be locked. */ 352 sx_xunlock(&proctree_lock); 353 (void) ttywait(tp); 354 sx_xlock(&proctree_lock); 355 /* 356 * The tty could have been revoked 357 * if we blocked. 358 */ 359 if (sp->s_ttyvp) { 360 ttyvp = sp->s_ttyvp; 361 SESS_LOCK(p->p_session); 362 sp->s_ttyvp = NULL; 363 SESS_UNLOCK(p->p_session); 364 sx_xunlock(&proctree_lock); 365 VOP_LOCK(ttyvp, LK_EXCLUSIVE, td); 366 VOP_REVOKE(ttyvp, REVOKEALL); 367 vput(ttyvp); 368 sx_xlock(&proctree_lock); 369 } 370 } 371 if (sp->s_ttyvp) { 372 ttyvp = sp->s_ttyvp; 373 SESS_LOCK(p->p_session); 374 sp->s_ttyvp = NULL; 375 SESS_UNLOCK(p->p_session); 376 vrele(ttyvp); 377 } 378 /* 379 * s_ttyp is not zero'd; we use this to indicate 380 * that the session once had a controlling terminal. 381 * (for logging and informational purposes) 382 */ 383 VFS_UNLOCK_GIANT(locked); 384 } 385 SESS_LOCK(p->p_session); 386 sp->s_leader = NULL; 387 SESS_UNLOCK(p->p_session); 388 } 389 fixjobc(p, p->p_pgrp, 0); 390 sx_xunlock(&proctree_lock); 391 (void)acct_process(td); 392 #ifdef KTRACE 393 /* 394 * Drain any pending records on the thread and release the trace 395 * file. It might be better if drain-and-clear were atomic. 396 */ 397 ktrprocexit(td); 398 PROC_LOCK(p); 399 mtx_lock(&ktrace_mtx); 400 p->p_traceflag = 0; /* don't trace the vrele() */ 401 tracevp = p->p_tracevp; 402 p->p_tracevp = NULL; 403 tracecred = p->p_tracecred; 404 p->p_tracecred = NULL; 405 mtx_unlock(&ktrace_mtx); 406 PROC_UNLOCK(p); 407 if (tracevp != NULL) { 408 locked = VFS_LOCK_GIANT(tracevp->v_mount); 409 vrele(tracevp); 410 VFS_UNLOCK_GIANT(locked); 411 } 412 if (tracecred != NULL) 413 crfree(tracecred); 414 #endif 415 /* 416 * Release reference to text vnode 417 */ 418 if ((vtmp = p->p_textvp) != NULL) { 419 p->p_textvp = NULL; 420 locked = VFS_LOCK_GIANT(vtmp->v_mount); 421 vrele(vtmp); 422 VFS_UNLOCK_GIANT(locked); 423 } 424 425 /* 426 * Release our limits structure. 427 */ 428 PROC_LOCK(p); 429 plim = p->p_limit; 430 p->p_limit = NULL; 431 PROC_UNLOCK(p); 432 lim_free(plim); 433 434 /* 435 * Remove proc from allproc queue and pidhash chain. 436 * Place onto zombproc. Unlink from parent's child list. 437 */ 438 sx_xlock(&allproc_lock); 439 LIST_REMOVE(p, p_list); 440 LIST_INSERT_HEAD(&zombproc, p, p_list); 441 LIST_REMOVE(p, p_hash); 442 sx_xunlock(&allproc_lock); 443 444 /* 445 * Reparent all of our children to init. 446 */ 447 sx_xlock(&proctree_lock); 448 q = LIST_FIRST(&p->p_children); 449 if (q != NULL) /* only need this if any child is S_ZOMB */ 450 wakeup(initproc); 451 for (; q != NULL; q = nq) { 452 nq = LIST_NEXT(q, p_sibling); 453 PROC_LOCK(q); 454 proc_reparent(q, initproc); 455 q->p_sigparent = SIGCHLD; 456 /* 457 * Traced processes are killed 458 * since their existence means someone is screwing up. 459 */ 460 if (q->p_flag & P_TRACED) { 461 q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 462 psignal(q, SIGKILL); 463 } 464 PROC_UNLOCK(q); 465 } 466 467 /* 468 * Save exit status and finalize rusage info except for times, 469 * adding in child rusage info later when our time is locked. 470 */ 471 PROC_LOCK(p); 472 p->p_xstat = rv; 473 p->p_xthread = td; 474 p->p_stats->p_ru.ru_nvcsw++; 475 *p->p_ru = p->p_stats->p_ru; 476 477 /* 478 * Notify interested parties of our demise. 479 */ 480 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 481 482 /* 483 * Just delete all entries in the p_klist. At this point we won't 484 * report any more events, and there are nasty race conditions that 485 * can beat us if we don't. 486 */ 487 knlist_clear(&p->p_klist, 1); 488 489 /* 490 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 491 * flag set, or if the handler is set to SIG_IGN, notify process 492 * 1 instead (and hope it will handle this situation). 493 */ 494 PROC_LOCK(p->p_pptr); 495 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 496 if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 497 struct proc *pp; 498 499 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 500 pp = p->p_pptr; 501 PROC_UNLOCK(pp); 502 proc_reparent(p, initproc); 503 p->p_sigparent = SIGCHLD; 504 PROC_LOCK(p->p_pptr); 505 /* 506 * If this was the last child of our parent, notify 507 * parent, so in case he was wait(2)ing, he will 508 * continue. 509 */ 510 if (LIST_EMPTY(&pp->p_children)) 511 wakeup(pp); 512 } else 513 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 514 515 if (p->p_pptr == initproc) 516 psignal(p->p_pptr, SIGCHLD); 517 else if (p->p_sigparent != 0) { 518 if (p->p_sigparent == SIGCHLD) 519 childproc_exited(p); 520 else /* LINUX thread */ 521 psignal(p->p_pptr, p->p_sigparent); 522 } 523 PROC_UNLOCK(p->p_pptr); 524 PROC_UNLOCK(p); 525 526 /* 527 * Finally, call machine-dependent code to release the remaining 528 * resources including address space. 529 * The address space is released by "vmspace_exitfree(p)" in 530 * vm_waitproc(). 531 */ 532 cpu_exit(td); 533 534 WITNESS_WARN(WARN_PANIC, &proctree_lock.sx_object, 535 "process (pid %d) exiting", p->p_pid); 536 537 PROC_LOCK(p); 538 PROC_LOCK(p->p_pptr); 539 sx_xunlock(&proctree_lock); 540 541 /* 542 * We have to wait until after acquiring all locks before 543 * changing p_state. We need to avoid all possible context 544 * switches (including ones from blocking on a mutex) while 545 * marked as a zombie. We also have to set the zombie state 546 * before we release the parent process' proc lock to avoid 547 * a lost wakeup. So, we first call wakeup, then we grab the 548 * sched lock, update the state, and release the parent process' 549 * proc lock. 550 */ 551 wakeup(p->p_pptr); 552 mtx_lock_spin(&sched_lock); 553 p->p_state = PRS_ZOMBIE; 554 PROC_UNLOCK(p->p_pptr); 555 556 sched_exit(p->p_pptr, td); 557 558 /* 559 * Hopefully no one will try to deliver a signal to the process this 560 * late in the game. 561 */ 562 knlist_destroy(&p->p_klist); 563 564 /* 565 * Make sure the scheduler takes this thread out of its tables etc. 566 * This will also release this thread's reference to the ucred. 567 * Other thread parts to release include pcb bits and such. 568 */ 569 thread_exit(); 570 } 571 572 573 #ifndef _SYS_SYSPROTO_H_ 574 struct abort2_args { 575 char *why; 576 int nargs; 577 void **args; 578 }; 579 #endif 580 581 /* 582 * MPSAFE. 583 */ 584 int 585 abort2(struct thread *td, struct abort2_args *uap) 586 { 587 struct proc *p = td->td_proc; 588 struct sbuf *sb; 589 void *uargs[16]; 590 int error, i, sig; 591 592 error = 0; /* satisfy compiler */ 593 594 /* 595 * Do it right now so we can log either proper call of abort2(), or 596 * note, that invalid argument was passed. 512 is big enough to 597 * handle 16 arguments' descriptions with additional comments. 598 */ 599 sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 600 sbuf_clear(sb); 601 sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 602 p->p_comm, p->p_pid, td->td_ucred->cr_uid); 603 /* 604 * Since we can't return from abort2(), send SIGKILL in cases, where 605 * abort2() was called improperly 606 */ 607 sig = SIGKILL; 608 /* Prevent from DoSes from user-space. */ 609 if (uap->nargs < 0 || uap->nargs > 16) 610 goto out; 611 if (uap->args == NULL) 612 goto out; 613 error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 614 if (error != 0) 615 goto out; 616 /* 617 * Limit size of 'reason' string to 128. Will fit even when 618 * maximal number of arguments was chosen to be logged. 619 */ 620 if (uap->why != NULL) { 621 error = sbuf_copyin(sb, uap->why, 128); 622 if (error < 0) 623 goto out; 624 } else { 625 sbuf_printf(sb, "(null)"); 626 } 627 if (uap->nargs) { 628 sbuf_printf(sb, "("); 629 for (i = 0;i < uap->nargs; i++) 630 sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 631 sbuf_printf(sb, ")"); 632 } 633 /* 634 * Final stage: arguments were proper, string has been 635 * successfully copied from userspace, and copying pointers 636 * from user-space succeed. 637 */ 638 sig = SIGABRT; 639 out: 640 if (sig == SIGKILL) { 641 sbuf_trim(sb); 642 sbuf_printf(sb, " (Reason text inaccessible)"); 643 } 644 sbuf_cat(sb, "\n"); 645 sbuf_finish(sb); 646 log(LOG_INFO, "%s", sbuf_data(sb)); 647 sbuf_delete(sb); 648 exit1(td, W_EXITCODE(0, sig)); 649 return (0); 650 } 651 652 653 #ifdef COMPAT_43 654 /* 655 * The dirty work is handled by kern_wait(). 656 * 657 * MPSAFE. 658 */ 659 int 660 owait(struct thread *td, struct owait_args *uap __unused) 661 { 662 int error, status; 663 664 error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 665 if (error == 0) 666 td->td_retval[1] = status; 667 return (error); 668 } 669 #endif /* COMPAT_43 */ 670 671 /* 672 * The dirty work is handled by kern_wait(). 673 * 674 * MPSAFE. 675 */ 676 int 677 wait4(struct thread *td, struct wait_args *uap) 678 { 679 struct rusage ru, *rup; 680 int error, status; 681 682 if (uap->rusage != NULL) 683 rup = &ru; 684 else 685 rup = NULL; 686 error = kern_wait(td, uap->pid, &status, uap->options, rup); 687 if (uap->status != NULL && error == 0) 688 error = copyout(&status, uap->status, sizeof(status)); 689 if (uap->rusage != NULL && error == 0) 690 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 691 return (error); 692 } 693 694 int 695 kern_wait(struct thread *td, pid_t pid, int *status, int options, 696 struct rusage *rusage) 697 { 698 struct proc *p, *q, *t; 699 int error, nfound; 700 701 AUDIT_ARG(pid, pid); 702 703 q = td->td_proc; 704 if (pid == 0) { 705 PROC_LOCK(q); 706 pid = -q->p_pgid; 707 PROC_UNLOCK(q); 708 } 709 if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 710 return (EINVAL); 711 loop: 712 if (q->p_flag & P_STATCHILD) { 713 PROC_LOCK(q); 714 q->p_flag &= ~P_STATCHILD; 715 PROC_UNLOCK(q); 716 } 717 nfound = 0; 718 sx_xlock(&proctree_lock); 719 LIST_FOREACH(p, &q->p_children, p_sibling) { 720 PROC_LOCK(p); 721 if (pid != WAIT_ANY && 722 p->p_pid != pid && p->p_pgid != -pid) { 723 PROC_UNLOCK(p); 724 continue; 725 } 726 if (p_canwait(td, p)) { 727 PROC_UNLOCK(p); 728 continue; 729 } 730 731 /* 732 * This special case handles a kthread spawned by linux_clone 733 * (see linux_misc.c). The linux_wait4 and linux_waitpid 734 * functions need to be able to distinguish between waiting 735 * on a process and waiting on a thread. It is a thread if 736 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 737 * signifies we want to wait for threads and not processes. 738 */ 739 if ((p->p_sigparent != SIGCHLD) ^ 740 ((options & WLINUXCLONE) != 0)) { 741 PROC_UNLOCK(p); 742 continue; 743 } 744 745 nfound++; 746 if (p->p_state == PRS_ZOMBIE) { 747 748 /* 749 * It is possible that the last thread of this 750 * process is still running on another CPU 751 * in thread_exit() after having dropped the process 752 * lock via PROC_UNLOCK() but before it has completed 753 * cpu_throw(). In that case, the other thread must 754 * still hold sched_lock, so simply by acquiring 755 * sched_lock once we will wait long enough for the 756 * thread to exit in that case. 757 */ 758 mtx_lock_spin(&sched_lock); 759 mtx_unlock_spin(&sched_lock); 760 761 td->td_retval[0] = p->p_pid; 762 if (status) 763 *status = p->p_xstat; /* convert to int */ 764 if (rusage) { 765 *rusage = *p->p_ru; 766 calcru(p, &rusage->ru_utime, &rusage->ru_stime); 767 } 768 769 PROC_LOCK(q); 770 sigqueue_take(p->p_ksi); 771 PROC_UNLOCK(q); 772 773 /* 774 * If we got the child via a ptrace 'attach', 775 * we need to give it back to the old parent. 776 */ 777 PROC_UNLOCK(p); 778 if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 779 PROC_LOCK(p); 780 p->p_oppid = 0; 781 proc_reparent(p, t); 782 PROC_UNLOCK(p); 783 tdsignal(t, NULL, SIGCHLD, p->p_ksi); 784 wakeup(t); 785 PROC_UNLOCK(t); 786 sx_xunlock(&proctree_lock); 787 return (0); 788 } 789 790 /* 791 * Remove other references to this process to ensure 792 * we have an exclusive reference. 793 */ 794 sx_xlock(&allproc_lock); 795 LIST_REMOVE(p, p_list); /* off zombproc */ 796 sx_xunlock(&allproc_lock); 797 LIST_REMOVE(p, p_sibling); 798 leavepgrp(p); 799 sx_xunlock(&proctree_lock); 800 801 /* 802 * As a side effect of this lock, we know that 803 * all other writes to this proc are visible now, so 804 * no more locking is needed for p. 805 */ 806 PROC_LOCK(p); 807 p->p_xstat = 0; /* XXX: why? */ 808 PROC_UNLOCK(p); 809 PROC_LOCK(q); 810 ruadd(&q->p_stats->p_cru, &q->p_crux, p->p_ru, 811 &p->p_rux); 812 PROC_UNLOCK(q); 813 FREE(p->p_ru, M_ZOMBIE); 814 p->p_ru = NULL; 815 816 /* 817 * Decrement the count of procs running with this uid. 818 */ 819 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 820 821 /* 822 * Free credentials, arguments, and sigacts. 823 */ 824 crfree(p->p_ucred); 825 p->p_ucred = NULL; 826 pargs_drop(p->p_args); 827 p->p_args = NULL; 828 sigacts_free(p->p_sigacts); 829 p->p_sigacts = NULL; 830 831 /* 832 * Do any thread-system specific cleanups. 833 */ 834 thread_wait(p); 835 836 /* 837 * Give vm and machine-dependent layer a chance 838 * to free anything that cpu_exit couldn't 839 * release while still running in process context. 840 */ 841 vm_waitproc(p); 842 #ifdef MAC 843 mac_destroy_proc(p); 844 #endif 845 #ifdef AUDIT 846 audit_proc_free(p); 847 #endif 848 KASSERT(FIRST_THREAD_IN_PROC(p), 849 ("kern_wait: no residual thread!")); 850 uma_zfree(proc_zone, p); 851 sx_xlock(&allproc_lock); 852 nprocs--; 853 sx_xunlock(&allproc_lock); 854 return (0); 855 } 856 mtx_lock_spin(&sched_lock); 857 if ((p->p_flag & P_STOPPED_SIG) && 858 (p->p_suspcount == p->p_numthreads) && 859 (p->p_flag & P_WAITED) == 0 && 860 (p->p_flag & P_TRACED || options & WUNTRACED)) { 861 mtx_unlock_spin(&sched_lock); 862 p->p_flag |= P_WAITED; 863 sx_xunlock(&proctree_lock); 864 td->td_retval[0] = p->p_pid; 865 if (status) 866 *status = W_STOPCODE(p->p_xstat); 867 PROC_UNLOCK(p); 868 869 PROC_LOCK(q); 870 sigqueue_take(p->p_ksi); 871 PROC_UNLOCK(q); 872 873 return (0); 874 } 875 mtx_unlock_spin(&sched_lock); 876 if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 877 sx_xunlock(&proctree_lock); 878 td->td_retval[0] = p->p_pid; 879 p->p_flag &= ~P_CONTINUED; 880 PROC_UNLOCK(p); 881 882 PROC_LOCK(q); 883 sigqueue_take(p->p_ksi); 884 PROC_UNLOCK(q); 885 886 if (status) 887 *status = SIGCONT; 888 return (0); 889 } 890 PROC_UNLOCK(p); 891 } 892 if (nfound == 0) { 893 sx_xunlock(&proctree_lock); 894 return (ECHILD); 895 } 896 if (options & WNOHANG) { 897 sx_xunlock(&proctree_lock); 898 td->td_retval[0] = 0; 899 return (0); 900 } 901 PROC_LOCK(q); 902 sx_xunlock(&proctree_lock); 903 if (q->p_flag & P_STATCHILD) { 904 q->p_flag &= ~P_STATCHILD; 905 error = 0; 906 } else 907 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 908 PROC_UNLOCK(q); 909 if (error) 910 return (error); 911 goto loop; 912 } 913 914 /* 915 * Make process 'parent' the new parent of process 'child'. 916 * Must be called with an exclusive hold of proctree lock. 917 */ 918 void 919 proc_reparent(struct proc *child, struct proc *parent) 920 { 921 922 sx_assert(&proctree_lock, SX_XLOCKED); 923 PROC_LOCK_ASSERT(child, MA_OWNED); 924 if (child->p_pptr == parent) 925 return; 926 927 LIST_REMOVE(child, p_sibling); 928 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 929 child->p_pptr = parent; 930 } 931