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