kern_exit.c revision 275615
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: head/sys/kern/kern_exit.c 275615 2014-12-08 16:02:02Z kib $"); 39 40#include "opt_compat.h" 41#include "opt_ktrace.h" 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/sysproto.h> 46#include <sys/capsicum.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/procdesc.h> 54#include <sys/pioctl.h> 55#include <sys/jail.h> 56#include <sys/tty.h> 57#include <sys/wait.h> 58#include <sys/vmmeter.h> 59#include <sys/vnode.h> 60#include <sys/racct.h> 61#include <sys/resourcevar.h> 62#include <sys/sbuf.h> 63#include <sys/signalvar.h> 64#include <sys/sched.h> 65#include <sys/sx.h> 66#include <sys/syscallsubr.h> 67#include <sys/syslog.h> 68#include <sys/ptrace.h> 69#include <sys/acct.h> /* for acct_process() function prototype */ 70#include <sys/filedesc.h> 71#include <sys/sdt.h> 72#include <sys/shm.h> 73#include <sys/sem.h> 74#ifdef KTRACE 75#include <sys/ktrace.h> 76#endif 77 78#include <security/audit/audit.h> 79#include <security/mac/mac_framework.h> 80 81#include <vm/vm.h> 82#include <vm/vm_extern.h> 83#include <vm/vm_param.h> 84#include <vm/pmap.h> 85#include <vm/vm_map.h> 86#include <vm/vm_page.h> 87#include <vm/uma.h> 88 89#ifdef KDTRACE_HOOKS 90#include <sys/dtrace_bsd.h> 91dtrace_execexit_func_t dtrace_fasttrap_exit; 92#endif 93 94SDT_PROVIDER_DECLARE(proc); 95SDT_PROBE_DEFINE1(proc, kernel, , exit, "int"); 96 97/* Hook for NFS teardown procedure. */ 98void (*nlminfo_release_p)(struct proc *p); 99 100struct proc * 101proc_realparent(struct proc *child) 102{ 103 struct proc *p, *parent; 104 105 sx_assert(&proctree_lock, SX_LOCKED); 106 if ((child->p_treeflag & P_TREE_ORPHANED) == 0) { 107 if (child->p_oppid == 0 || 108 child->p_pptr->p_pid == child->p_oppid) 109 parent = child->p_pptr; 110 else 111 parent = initproc; 112 return (parent); 113 } 114 for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) { 115 /* Cannot use LIST_PREV(), since the list head is not known. */ 116 p = __containerof(p->p_orphan.le_prev, struct proc, 117 p_orphan.le_next); 118 KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0, 119 ("missing P_ORPHAN %p", p)); 120 } 121 parent = __containerof(p->p_orphan.le_prev, struct proc, 122 p_orphans.lh_first); 123 return (parent); 124} 125 126static void 127clear_orphan(struct proc *p) 128{ 129 struct proc *p1; 130 131 sx_assert(&proctree_lock, SA_XLOCKED); 132 if ((p->p_treeflag & P_TREE_ORPHANED) == 0) 133 return; 134 if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) { 135 p1 = LIST_NEXT(p, p_orphan); 136 if (p1 != NULL) 137 p1->p_treeflag |= P_TREE_FIRST_ORPHAN; 138 p->p_treeflag &= ~P_TREE_FIRST_ORPHAN; 139 } 140 LIST_REMOVE(p, p_orphan); 141 p->p_treeflag &= ~P_TREE_ORPHANED; 142} 143 144/* 145 * exit -- death of process. 146 */ 147void 148sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 149{ 150 151 exit1(td, W_EXITCODE(uap->rval, 0)); 152 /* NOTREACHED */ 153} 154 155/* 156 * Exit: deallocate address space and other resources, change proc state to 157 * zombie, and unlink proc from allproc and parent's lists. Save exit status 158 * and rusage for wait(). Check for child processes and orphan them. 159 */ 160void 161exit1(struct thread *td, int rv) 162{ 163 struct proc *p, *nq, *q, *t; 164 struct thread *tdt; 165 struct vnode *ttyvp = NULL; 166 167 mtx_assert(&Giant, MA_NOTOWNED); 168 169 p = td->td_proc; 170 /* 171 * XXX in case we're rebooting we just let init die in order to 172 * work around an unsolved stack overflow seen very late during 173 * shutdown on sparc64 when the gmirror worker process exists. 174 */ 175 if (p == initproc && rebooting == 0) { 176 printf("init died (signal %d, exit %d)\n", 177 WTERMSIG(rv), WEXITSTATUS(rv)); 178 panic("Going nowhere without my init!"); 179 } 180 181 /* 182 * MUST abort all other threads before proceeding past here. 183 */ 184 PROC_LOCK(p); 185 /* 186 * First check if some other thread or external request got 187 * here before us. If so, act appropriately: exit or suspend. 188 * We must ensure that stop requests are handled before we set 189 * P_WEXIT. 190 */ 191 thread_suspend_check(0); 192 while (p->p_flag & P_HADTHREADS) { 193 /* 194 * Kill off the other threads. This requires 195 * some co-operation from other parts of the kernel 196 * so it may not be instantaneous. With this state set 197 * any thread entering the kernel from userspace will 198 * thread_exit() in trap(). Any thread attempting to 199 * sleep will return immediately with EINTR or EWOULDBLOCK 200 * which will hopefully force them to back out to userland 201 * freeing resources as they go. Any thread attempting 202 * to return to userland will thread_exit() from userret(). 203 * thread_exit() will unsuspend us when the last of the 204 * other threads exits. 205 * If there is already a thread singler after resumption, 206 * calling thread_single will fail; in that case, we just 207 * re-check all suspension request, the thread should 208 * either be suspended there or exit. 209 */ 210 if (!thread_single(SINGLE_EXIT)) 211 /* 212 * All other activity in this process is now 213 * stopped. Threading support has been turned 214 * off. 215 */ 216 break; 217 /* 218 * Recheck for new stop or suspend requests which 219 * might appear while process lock was dropped in 220 * thread_single(). 221 */ 222 thread_suspend_check(0); 223 } 224 KASSERT(p->p_numthreads == 1, 225 ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 226 racct_sub(p, RACCT_NTHR, 1); 227 /* 228 * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 229 * on our vmspace, so we should block below until they have 230 * released their reference to us. Note that if they have 231 * requested S_EXIT stops we will block here until they ack 232 * via PIOCCONT. 233 */ 234 _STOPEVENT(p, S_EXIT, rv); 235 236 /* 237 * Ignore any pending request to stop due to a stop signal. 238 * Once P_WEXIT is set, future requests will be ignored as 239 * well. 240 */ 241 p->p_flag &= ~P_STOPPED_SIG; 242 KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 243 244 /* 245 * Note that we are exiting and do another wakeup of anyone in 246 * PIOCWAIT in case they aren't listening for S_EXIT stops or 247 * decided to wait again after we told them we are exiting. 248 */ 249 p->p_flag |= P_WEXIT; 250 wakeup(&p->p_stype); 251 252 /* 253 * Wait for any processes that have a hold on our vmspace to 254 * release their reference. 255 */ 256 while (p->p_lock > 0) 257 msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 258 259 p->p_xstat = rv; /* Let event handler change exit status */ 260 PROC_UNLOCK(p); 261 /* Drain the limit callout while we don't have the proc locked */ 262 callout_drain(&p->p_limco); 263 264#ifdef AUDIT 265 /* 266 * The Sun BSM exit token contains two components: an exit status as 267 * passed to exit(), and a return value to indicate what sort of exit 268 * it was. The exit status is WEXITSTATUS(rv), but it's not clear 269 * what the return value is. 270 */ 271 AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0); 272 AUDIT_SYSCALL_EXIT(0, td); 273#endif 274 275 /* Are we a task leader with peers? */ 276 if (p->p_peers != NULL && p == p->p_leader) { 277 mtx_lock(&ppeers_lock); 278 q = p->p_peers; 279 while (q != NULL) { 280 PROC_LOCK(q); 281 kern_psignal(q, SIGKILL); 282 PROC_UNLOCK(q); 283 q = q->p_peers; 284 } 285 while (p->p_peers != NULL) 286 msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 287 mtx_unlock(&ppeers_lock); 288 } 289 290 /* 291 * Check if any loadable modules need anything done at process exit. 292 * E.g. SYSV IPC stuff 293 * XXX what if one of these generates an error? 294 */ 295 EVENTHANDLER_INVOKE(process_exit, p); 296 297 /* 298 * If parent is waiting for us to exit or exec, 299 * P_PPWAIT is set; we will wakeup the parent below. 300 */ 301 PROC_LOCK(p); 302 rv = p->p_xstat; /* Event handler could change exit status */ 303 stopprofclock(p); 304 p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 305 306 /* 307 * Stop the real interval timer. If the handler is currently 308 * executing, prevent it from rearming itself and let it finish. 309 */ 310 if (timevalisset(&p->p_realtimer.it_value) && 311 callout_stop(&p->p_itcallout) == 0) { 312 timevalclear(&p->p_realtimer.it_interval); 313 msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 314 KASSERT(!timevalisset(&p->p_realtimer.it_value), 315 ("realtime timer is still armed")); 316 } 317 PROC_UNLOCK(p); 318 319 /* 320 * Reset any sigio structures pointing to us as a result of 321 * F_SETOWN with our pid. 322 */ 323 funsetownlst(&p->p_sigiolst); 324 325 /* 326 * If this process has an nlminfo data area (for lockd), release it 327 */ 328 if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 329 (*nlminfo_release_p)(p); 330 331 /* 332 * Close open files and release open-file table. 333 * This may block! 334 */ 335 fdescfree(td); 336 337 /* 338 * If this thread tickled GEOM, we need to wait for the giggling to 339 * stop before we return to userland 340 */ 341 if (td->td_pflags & TDP_GEOM) 342 g_waitidle(); 343 344 /* 345 * Remove ourself from our leader's peer list and wake our leader. 346 */ 347 if (p->p_leader->p_peers != NULL) { 348 mtx_lock(&ppeers_lock); 349 if (p->p_leader->p_peers != NULL) { 350 q = p->p_leader; 351 while (q->p_peers != p) 352 q = q->p_peers; 353 q->p_peers = p->p_peers; 354 wakeup(p->p_leader); 355 } 356 mtx_unlock(&ppeers_lock); 357 } 358 359 vmspace_exit(td); 360 361 sx_xlock(&proctree_lock); 362 if (SESS_LEADER(p)) { 363 struct session *sp = p->p_session; 364 struct tty *tp; 365 366 /* 367 * s_ttyp is not zero'd; we use this to indicate that 368 * the session once had a controlling terminal. (for 369 * logging and informational purposes) 370 */ 371 SESS_LOCK(sp); 372 ttyvp = sp->s_ttyvp; 373 tp = sp->s_ttyp; 374 sp->s_ttyvp = NULL; 375 sp->s_ttydp = NULL; 376 sp->s_leader = NULL; 377 SESS_UNLOCK(sp); 378 379 /* 380 * Signal foreground pgrp and revoke access to 381 * controlling terminal if it has not been revoked 382 * already. 383 * 384 * Because the TTY may have been revoked in the mean 385 * time and could already have a new session associated 386 * with it, make sure we don't send a SIGHUP to a 387 * foreground process group that does not belong to this 388 * session. 389 */ 390 391 if (tp != NULL) { 392 tty_lock(tp); 393 if (tp->t_session == sp) 394 tty_signal_pgrp(tp, SIGHUP); 395 tty_unlock(tp); 396 } 397 398 if (ttyvp != NULL) { 399 sx_xunlock(&proctree_lock); 400 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) { 401 VOP_REVOKE(ttyvp, REVOKEALL); 402 VOP_UNLOCK(ttyvp, 0); 403 } 404 sx_xlock(&proctree_lock); 405 } 406 } 407 fixjobc(p, p->p_pgrp, 0); 408 sx_xunlock(&proctree_lock); 409 (void)acct_process(td); 410 411 /* Release the TTY now we've unlocked everything. */ 412 if (ttyvp != NULL) 413 vrele(ttyvp); 414#ifdef KTRACE 415 ktrprocexit(td); 416#endif 417 /* 418 * Release reference to text vnode 419 */ 420 if (p->p_textvp != NULL) { 421 vrele(p->p_textvp); 422 p->p_textvp = NULL; 423 } 424 425 /* 426 * Release our limits structure. 427 */ 428 lim_free(p->p_limit); 429 p->p_limit = NULL; 430 431 tidhash_remove(td); 432 433 /* 434 * Remove proc from allproc queue and pidhash chain. 435 * Place onto zombproc. Unlink from parent's child list. 436 */ 437 sx_xlock(&allproc_lock); 438 LIST_REMOVE(p, p_list); 439 LIST_INSERT_HEAD(&zombproc, p, p_list); 440 LIST_REMOVE(p, p_hash); 441 sx_xunlock(&allproc_lock); 442 443 /* 444 * Call machine-dependent code to release any 445 * machine-dependent resources other than the address space. 446 * The address space is released by "vmspace_exitfree(p)" in 447 * vm_waitproc(). 448 */ 449 cpu_exit(td); 450 451 WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 452 453 /* 454 * Reparent all children processes: 455 * - traced ones to the original parent (or init if we are that parent) 456 * - the rest to init 457 */ 458 sx_xlock(&proctree_lock); 459 q = LIST_FIRST(&p->p_children); 460 if (q != NULL) /* only need this if any child is S_ZOMB */ 461 wakeup(initproc); 462 for (; q != NULL; q = nq) { 463 nq = LIST_NEXT(q, p_sibling); 464 PROC_LOCK(q); 465 q->p_sigparent = SIGCHLD; 466 467 if (!(q->p_flag & P_TRACED)) { 468 proc_reparent(q, initproc); 469 } else { 470 /* 471 * Traced processes are killed since their existence 472 * means someone is screwing up. 473 */ 474 t = proc_realparent(q); 475 if (t == p) { 476 proc_reparent(q, initproc); 477 } else { 478 PROC_LOCK(t); 479 proc_reparent(q, t); 480 PROC_UNLOCK(t); 481 } 482 /* 483 * Since q was found on our children list, the 484 * proc_reparent() call moved q to the orphan 485 * list due to present P_TRACED flag. Clear 486 * orphan link for q now while q is locked. 487 */ 488 clear_orphan(q); 489 q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 490 FOREACH_THREAD_IN_PROC(q, tdt) 491 tdt->td_dbgflags &= ~TDB_SUSPEND; 492 kern_psignal(q, SIGKILL); 493 } 494 PROC_UNLOCK(q); 495 } 496 497 /* 498 * Also get rid of our orphans. 499 */ 500 while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 501 PROC_LOCK(q); 502 clear_orphan(q); 503 PROC_UNLOCK(q); 504 } 505 506 /* Save exit status. */ 507 PROC_LOCK(p); 508 p->p_xthread = td; 509 510 /* Tell the prison that we are gone. */ 511 prison_proc_free(p->p_ucred->cr_prison); 512 513#ifdef KDTRACE_HOOKS 514 /* 515 * Tell the DTrace fasttrap provider about the exit if it 516 * has declared an interest. 517 */ 518 if (dtrace_fasttrap_exit) 519 dtrace_fasttrap_exit(p); 520#endif 521 522 /* 523 * Notify interested parties of our demise. 524 */ 525 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 526 527#ifdef KDTRACE_HOOKS 528 int reason = CLD_EXITED; 529 if (WCOREDUMP(rv)) 530 reason = CLD_DUMPED; 531 else if (WIFSIGNALED(rv)) 532 reason = CLD_KILLED; 533 SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 534#endif 535 536 /* 537 * Just delete all entries in the p_klist. At this point we won't 538 * report any more events, and there are nasty race conditions that 539 * can beat us if we don't. 540 */ 541 knlist_clear(&p->p_klist, 1); 542 543 /* 544 * If this is a process with a descriptor, we may not need to deliver 545 * a signal to the parent. proctree_lock is held over 546 * procdesc_exit() to serialize concurrent calls to close() and 547 * exit(). 548 */ 549 if (p->p_procdesc == NULL || procdesc_exit(p)) { 550 /* 551 * Notify parent that we're gone. If parent has the 552 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 553 * notify process 1 instead (and hope it will handle this 554 * situation). 555 */ 556 PROC_LOCK(p->p_pptr); 557 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 558 if (p->p_pptr->p_sigacts->ps_flag & 559 (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 560 struct proc *pp; 561 562 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 563 pp = p->p_pptr; 564 PROC_UNLOCK(pp); 565 proc_reparent(p, initproc); 566 p->p_sigparent = SIGCHLD; 567 PROC_LOCK(p->p_pptr); 568 569 /* 570 * Notify parent, so in case he was wait(2)ing or 571 * executing waitpid(2) with our pid, he will 572 * continue. 573 */ 574 wakeup(pp); 575 } else 576 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 577 578 if (p->p_pptr == initproc) 579 kern_psignal(p->p_pptr, SIGCHLD); 580 else if (p->p_sigparent != 0) { 581 if (p->p_sigparent == SIGCHLD) 582 childproc_exited(p); 583 else /* LINUX thread */ 584 kern_psignal(p->p_pptr, p->p_sigparent); 585 } 586 } else 587 PROC_LOCK(p->p_pptr); 588 sx_xunlock(&proctree_lock); 589 590 /* 591 * The state PRS_ZOMBIE prevents other proesses from sending 592 * signal to the process, to avoid memory leak, we free memory 593 * for signal queue at the time when the state is set. 594 */ 595 sigqueue_flush(&p->p_sigqueue); 596 sigqueue_flush(&td->td_sigqueue); 597 598 /* 599 * We have to wait until after acquiring all locks before 600 * changing p_state. We need to avoid all possible context 601 * switches (including ones from blocking on a mutex) while 602 * marked as a zombie. We also have to set the zombie state 603 * before we release the parent process' proc lock to avoid 604 * a lost wakeup. So, we first call wakeup, then we grab the 605 * sched lock, update the state, and release the parent process' 606 * proc lock. 607 */ 608 wakeup(p->p_pptr); 609 cv_broadcast(&p->p_pwait); 610 sched_exit(p->p_pptr, td); 611 PROC_SLOCK(p); 612 p->p_state = PRS_ZOMBIE; 613 PROC_UNLOCK(p->p_pptr); 614 615 /* 616 * Hopefully no one will try to deliver a signal to the process this 617 * late in the game. 618 */ 619 knlist_destroy(&p->p_klist); 620 621 /* 622 * Save our children's rusage information in our exit rusage. 623 */ 624 PROC_STATLOCK(p); 625 ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 626 PROC_STATUNLOCK(p); 627 628 /* 629 * Make sure the scheduler takes this thread out of its tables etc. 630 * This will also release this thread's reference to the ucred. 631 * Other thread parts to release include pcb bits and such. 632 */ 633 thread_exit(); 634} 635 636 637#ifndef _SYS_SYSPROTO_H_ 638struct abort2_args { 639 char *why; 640 int nargs; 641 void **args; 642}; 643#endif 644 645int 646sys_abort2(struct thread *td, struct abort2_args *uap) 647{ 648 struct proc *p = td->td_proc; 649 struct sbuf *sb; 650 void *uargs[16]; 651 int error, i, sig; 652 653 /* 654 * Do it right now so we can log either proper call of abort2(), or 655 * note, that invalid argument was passed. 512 is big enough to 656 * handle 16 arguments' descriptions with additional comments. 657 */ 658 sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 659 sbuf_clear(sb); 660 sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 661 p->p_comm, p->p_pid, td->td_ucred->cr_uid); 662 /* 663 * Since we can't return from abort2(), send SIGKILL in cases, where 664 * abort2() was called improperly 665 */ 666 sig = SIGKILL; 667 /* Prevent from DoSes from user-space. */ 668 if (uap->nargs < 0 || uap->nargs > 16) 669 goto out; 670 if (uap->nargs > 0) { 671 if (uap->args == NULL) 672 goto out; 673 error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 674 if (error != 0) 675 goto out; 676 } 677 /* 678 * Limit size of 'reason' string to 128. Will fit even when 679 * maximal number of arguments was chosen to be logged. 680 */ 681 if (uap->why != NULL) { 682 error = sbuf_copyin(sb, uap->why, 128); 683 if (error < 0) 684 goto out; 685 } else { 686 sbuf_printf(sb, "(null)"); 687 } 688 if (uap->nargs > 0) { 689 sbuf_printf(sb, "("); 690 for (i = 0;i < uap->nargs; i++) 691 sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 692 sbuf_printf(sb, ")"); 693 } 694 /* 695 * Final stage: arguments were proper, string has been 696 * successfully copied from userspace, and copying pointers 697 * from user-space succeed. 698 */ 699 sig = SIGABRT; 700out: 701 if (sig == SIGKILL) { 702 sbuf_trim(sb); 703 sbuf_printf(sb, " (Reason text inaccessible)"); 704 } 705 sbuf_cat(sb, "\n"); 706 sbuf_finish(sb); 707 log(LOG_INFO, "%s", sbuf_data(sb)); 708 sbuf_delete(sb); 709 exit1(td, W_EXITCODE(0, sig)); 710 return (0); 711} 712 713 714#ifdef COMPAT_43 715/* 716 * The dirty work is handled by kern_wait(). 717 */ 718int 719owait(struct thread *td, struct owait_args *uap __unused) 720{ 721 int error, status; 722 723 error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 724 if (error == 0) 725 td->td_retval[1] = status; 726 return (error); 727} 728#endif /* COMPAT_43 */ 729 730/* 731 * The dirty work is handled by kern_wait(). 732 */ 733int 734sys_wait4(struct thread *td, struct wait4_args *uap) 735{ 736 struct rusage ru, *rup; 737 int error, status; 738 739 if (uap->rusage != NULL) 740 rup = &ru; 741 else 742 rup = NULL; 743 error = kern_wait(td, uap->pid, &status, uap->options, rup); 744 if (uap->status != NULL && error == 0) 745 error = copyout(&status, uap->status, sizeof(status)); 746 if (uap->rusage != NULL && error == 0) 747 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 748 return (error); 749} 750 751int 752sys_wait6(struct thread *td, struct wait6_args *uap) 753{ 754 struct __wrusage wru, *wrup; 755 siginfo_t si, *sip; 756 idtype_t idtype; 757 id_t id; 758 int error, status; 759 760 idtype = uap->idtype; 761 id = uap->id; 762 763 if (uap->wrusage != NULL) 764 wrup = &wru; 765 else 766 wrup = NULL; 767 768 if (uap->info != NULL) { 769 sip = &si; 770 bzero(sip, sizeof(*sip)); 771 } else 772 sip = NULL; 773 774 /* 775 * We expect all callers of wait6() to know about WEXITED and 776 * WTRAPPED. 777 */ 778 error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip); 779 780 if (uap->status != NULL && error == 0) 781 error = copyout(&status, uap->status, sizeof(status)); 782 if (uap->wrusage != NULL && error == 0) 783 error = copyout(&wru, uap->wrusage, sizeof(wru)); 784 if (uap->info != NULL && error == 0) 785 error = copyout(&si, uap->info, sizeof(si)); 786 return (error); 787} 788 789/* 790 * Reap the remains of a zombie process and optionally return status and 791 * rusage. Asserts and will release both the proctree_lock and the process 792 * lock as part of its work. 793 */ 794void 795proc_reap(struct thread *td, struct proc *p, int *status, int options) 796{ 797 struct proc *q, *t; 798 799 sx_assert(&proctree_lock, SA_XLOCKED); 800 PROC_LOCK_ASSERT(p, MA_OWNED); 801 PROC_SLOCK_ASSERT(p, MA_OWNED); 802 KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 803 804 q = td->td_proc; 805 806 PROC_SUNLOCK(p); 807 td->td_retval[0] = p->p_pid; 808 if (status) 809 *status = p->p_xstat; /* convert to int */ 810 if (options & WNOWAIT) { 811 /* 812 * Only poll, returning the status. Caller does not wish to 813 * release the proc struct just yet. 814 */ 815 PROC_UNLOCK(p); 816 sx_xunlock(&proctree_lock); 817 return; 818 } 819 820 PROC_LOCK(q); 821 sigqueue_take(p->p_ksi); 822 PROC_UNLOCK(q); 823 PROC_UNLOCK(p); 824 825 /* 826 * If we got the child via a ptrace 'attach', we need to give it back 827 * to the old parent. 828 */ 829 if (p->p_oppid != 0) { 830 t = proc_realparent(p); 831 PROC_LOCK(t); 832 PROC_LOCK(p); 833 proc_reparent(p, t); 834 p->p_oppid = 0; 835 PROC_UNLOCK(p); 836 pksignal(t, SIGCHLD, p->p_ksi); 837 wakeup(t); 838 cv_broadcast(&p->p_pwait); 839 PROC_UNLOCK(t); 840 sx_xunlock(&proctree_lock); 841 return; 842 } 843 844 /* 845 * Remove other references to this process to ensure we have an 846 * exclusive reference. 847 */ 848 sx_xlock(&allproc_lock); 849 LIST_REMOVE(p, p_list); /* off zombproc */ 850 sx_xunlock(&allproc_lock); 851 LIST_REMOVE(p, p_sibling); 852 PROC_LOCK(p); 853 clear_orphan(p); 854 PROC_UNLOCK(p); 855 leavepgrp(p); 856 if (p->p_procdesc != NULL) 857 procdesc_reap(p); 858 sx_xunlock(&proctree_lock); 859 860 /* 861 * As a side effect of this lock, we know that all other writes to 862 * this proc are visible now, so no more locking is needed for p. 863 */ 864 PROC_LOCK(p); 865 p->p_xstat = 0; /* XXX: why? */ 866 PROC_UNLOCK(p); 867 PROC_LOCK(q); 868 ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 869 PROC_UNLOCK(q); 870 871 /* 872 * Decrement the count of procs running with this uid. 873 */ 874 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 875 876 /* 877 * Destroy resource accounting information associated with the process. 878 */ 879#ifdef RACCT 880 PROC_LOCK(p); 881 racct_sub(p, RACCT_NPROC, 1); 882 PROC_UNLOCK(p); 883#endif 884 racct_proc_exit(p); 885 886 /* 887 * Free credentials, arguments, and sigacts. 888 */ 889 crfree(p->p_ucred); 890 p->p_ucred = NULL; 891 pargs_drop(p->p_args); 892 p->p_args = NULL; 893 sigacts_free(p->p_sigacts); 894 p->p_sigacts = NULL; 895 896 /* 897 * Do any thread-system specific cleanups. 898 */ 899 thread_wait(p); 900 901 /* 902 * Give vm and machine-dependent layer a chance to free anything that 903 * cpu_exit couldn't release while still running in process context. 904 */ 905 vm_waitproc(p); 906#ifdef MAC 907 mac_proc_destroy(p); 908#endif 909 KASSERT(FIRST_THREAD_IN_PROC(p), 910 ("proc_reap: no residual thread!")); 911 uma_zfree(proc_zone, p); 912 sx_xlock(&allproc_lock); 913 nprocs--; 914 sx_xunlock(&allproc_lock); 915} 916 917static int 918proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, 919 int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo) 920{ 921 struct proc *q; 922 struct rusage *rup; 923 924 sx_assert(&proctree_lock, SA_XLOCKED); 925 926 q = td->td_proc; 927 PROC_LOCK(p); 928 929 switch (idtype) { 930 case P_ALL: 931 break; 932 case P_PID: 933 if (p->p_pid != (pid_t)id) { 934 PROC_UNLOCK(p); 935 return (0); 936 } 937 break; 938 case P_PGID: 939 if (p->p_pgid != (pid_t)id) { 940 PROC_UNLOCK(p); 941 return (0); 942 } 943 break; 944 case P_SID: 945 if (p->p_session->s_sid != (pid_t)id) { 946 PROC_UNLOCK(p); 947 return (0); 948 } 949 break; 950 case P_UID: 951 if (p->p_ucred->cr_uid != (uid_t)id) { 952 PROC_UNLOCK(p); 953 return (0); 954 } 955 break; 956 case P_GID: 957 if (p->p_ucred->cr_gid != (gid_t)id) { 958 PROC_UNLOCK(p); 959 return (0); 960 } 961 break; 962 case P_JAILID: 963 if (p->p_ucred->cr_prison->pr_id != (int)id) { 964 PROC_UNLOCK(p); 965 return (0); 966 } 967 break; 968 /* 969 * It seems that the thread structures get zeroed out 970 * at process exit. This makes it impossible to 971 * support P_SETID, P_CID or P_CPUID. 972 */ 973 default: 974 PROC_UNLOCK(p); 975 return (0); 976 } 977 978 if (p_canwait(td, p)) { 979 PROC_UNLOCK(p); 980 return (0); 981 } 982 983 if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) { 984 PROC_UNLOCK(p); 985 return (0); 986 } 987 988 /* 989 * This special case handles a kthread spawned by linux_clone 990 * (see linux_misc.c). The linux_wait4 and linux_waitpid 991 * functions need to be able to distinguish between waiting 992 * on a process and waiting on a thread. It is a thread if 993 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 994 * signifies we want to wait for threads and not processes. 995 */ 996 if ((p->p_sigparent != SIGCHLD) ^ 997 ((options & WLINUXCLONE) != 0)) { 998 PROC_UNLOCK(p); 999 return (0); 1000 } 1001 1002 if (siginfo != NULL) { 1003 bzero(siginfo, sizeof(*siginfo)); 1004 siginfo->si_errno = 0; 1005 1006 /* 1007 * SUSv4 requires that the si_signo value is always 1008 * SIGCHLD. Obey it despite the rfork(2) interface 1009 * allows to request other signal for child exit 1010 * notification. 1011 */ 1012 siginfo->si_signo = SIGCHLD; 1013 1014 /* 1015 * This is still a rough estimate. We will fix the 1016 * cases TRAPPED, STOPPED, and CONTINUED later. 1017 */ 1018 if (WCOREDUMP(p->p_xstat)) { 1019 siginfo->si_code = CLD_DUMPED; 1020 siginfo->si_status = WTERMSIG(p->p_xstat); 1021 } else if (WIFSIGNALED(p->p_xstat)) { 1022 siginfo->si_code = CLD_KILLED; 1023 siginfo->si_status = WTERMSIG(p->p_xstat); 1024 } else { 1025 siginfo->si_code = CLD_EXITED; 1026 siginfo->si_status = WEXITSTATUS(p->p_xstat); 1027 } 1028 1029 siginfo->si_pid = p->p_pid; 1030 siginfo->si_uid = p->p_ucred->cr_uid; 1031 1032 /* 1033 * The si_addr field would be useful additional 1034 * detail, but apparently the PC value may be lost 1035 * when we reach this point. bzero() above sets 1036 * siginfo->si_addr to NULL. 1037 */ 1038 } 1039 1040 /* 1041 * There should be no reason to limit resources usage info to 1042 * exited processes only. A snapshot about any resources used 1043 * by a stopped process may be exactly what is needed. 1044 */ 1045 if (wrusage != NULL) { 1046 rup = &wrusage->wru_self; 1047 *rup = p->p_ru; 1048 PROC_STATLOCK(p); 1049 calcru(p, &rup->ru_utime, &rup->ru_stime); 1050 PROC_STATUNLOCK(p); 1051 1052 rup = &wrusage->wru_children; 1053 *rup = p->p_stats->p_cru; 1054 calccru(p, &rup->ru_utime, &rup->ru_stime); 1055 } 1056 1057 if (p->p_state == PRS_ZOMBIE) { 1058 PROC_SLOCK(p); 1059 proc_reap(td, p, status, options); 1060 return (-1); 1061 } 1062 PROC_UNLOCK(p); 1063 return (1); 1064} 1065 1066int 1067kern_wait(struct thread *td, pid_t pid, int *status, int options, 1068 struct rusage *rusage) 1069{ 1070 struct __wrusage wru, *wrup; 1071 idtype_t idtype; 1072 id_t id; 1073 int ret; 1074 1075 /* 1076 * Translate the special pid values into the (idtype, pid) 1077 * pair for kern_wait6. The WAIT_MYPGRP case is handled by 1078 * kern_wait6() on its own. 1079 */ 1080 if (pid == WAIT_ANY) { 1081 idtype = P_ALL; 1082 id = 0; 1083 } else if (pid < 0) { 1084 idtype = P_PGID; 1085 id = (id_t)-pid; 1086 } else { 1087 idtype = P_PID; 1088 id = (id_t)pid; 1089 } 1090 1091 if (rusage != NULL) 1092 wrup = &wru; 1093 else 1094 wrup = NULL; 1095 1096 /* 1097 * For backward compatibility we implicitly add flags WEXITED 1098 * and WTRAPPED here. 1099 */ 1100 options |= WEXITED | WTRAPPED; 1101 ret = kern_wait6(td, idtype, id, status, options, wrup, NULL); 1102 if (rusage != NULL) 1103 *rusage = wru.wru_self; 1104 return (ret); 1105} 1106 1107int 1108kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, 1109 int options, struct __wrusage *wrusage, siginfo_t *siginfo) 1110{ 1111 struct proc *p, *q; 1112 int error, nfound, ret; 1113 1114 AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */ 1115 AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */ 1116 AUDIT_ARG_VALUE(options); 1117 1118 q = td->td_proc; 1119 1120 if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1121 PROC_LOCK(q); 1122 id = (id_t)q->p_pgid; 1123 PROC_UNLOCK(q); 1124 idtype = P_PGID; 1125 } 1126 1127 /* If we don't know the option, just return. */ 1128 if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT | 1129 WEXITED | WTRAPPED | WLINUXCLONE)) != 0) 1130 return (EINVAL); 1131 if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) { 1132 /* 1133 * We will be unable to find any matching processes, 1134 * because there are no known events to look for. 1135 * Prefer to return error instead of blocking 1136 * indefinitely. 1137 */ 1138 return (EINVAL); 1139 } 1140 1141loop: 1142 if (q->p_flag & P_STATCHILD) { 1143 PROC_LOCK(q); 1144 q->p_flag &= ~P_STATCHILD; 1145 PROC_UNLOCK(q); 1146 } 1147 nfound = 0; 1148 sx_xlock(&proctree_lock); 1149 LIST_FOREACH(p, &q->p_children, p_sibling) { 1150 ret = proc_to_reap(td, p, idtype, id, status, options, 1151 wrusage, siginfo); 1152 if (ret == 0) 1153 continue; 1154 else if (ret == 1) 1155 nfound++; 1156 else 1157 return (0); 1158 1159 PROC_LOCK(p); 1160 PROC_SLOCK(p); 1161 1162 if ((options & WTRAPPED) != 0 && 1163 (p->p_flag & P_TRACED) != 0 && 1164 (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 && 1165 (p->p_suspcount == p->p_numthreads) && 1166 ((p->p_flag & P_WAITED) == 0)) { 1167 PROC_SUNLOCK(p); 1168 if ((options & WNOWAIT) == 0) 1169 p->p_flag |= P_WAITED; 1170 sx_xunlock(&proctree_lock); 1171 td->td_retval[0] = p->p_pid; 1172 1173 if (status != NULL) 1174 *status = W_STOPCODE(p->p_xstat); 1175 if (siginfo != NULL) { 1176 siginfo->si_status = p->p_xstat; 1177 siginfo->si_code = CLD_TRAPPED; 1178 } 1179 if ((options & WNOWAIT) == 0) { 1180 PROC_LOCK(q); 1181 sigqueue_take(p->p_ksi); 1182 PROC_UNLOCK(q); 1183 } 1184 1185 PROC_UNLOCK(p); 1186 return (0); 1187 } 1188 if ((options & WUNTRACED) != 0 && 1189 (p->p_flag & P_STOPPED_SIG) != 0 && 1190 (p->p_suspcount == p->p_numthreads) && 1191 ((p->p_flag & P_WAITED) == 0)) { 1192 PROC_SUNLOCK(p); 1193 if ((options & WNOWAIT) == 0) 1194 p->p_flag |= P_WAITED; 1195 sx_xunlock(&proctree_lock); 1196 td->td_retval[0] = p->p_pid; 1197 1198 if (status != NULL) 1199 *status = W_STOPCODE(p->p_xstat); 1200 if (siginfo != NULL) { 1201 siginfo->si_status = p->p_xstat; 1202 siginfo->si_code = CLD_STOPPED; 1203 } 1204 if ((options & WNOWAIT) == 0) { 1205 PROC_LOCK(q); 1206 sigqueue_take(p->p_ksi); 1207 PROC_UNLOCK(q); 1208 } 1209 1210 PROC_UNLOCK(p); 1211 return (0); 1212 } 1213 PROC_SUNLOCK(p); 1214 if ((options & WCONTINUED) != 0 && 1215 (p->p_flag & P_CONTINUED) != 0) { 1216 sx_xunlock(&proctree_lock); 1217 td->td_retval[0] = p->p_pid; 1218 if ((options & WNOWAIT) == 0) { 1219 p->p_flag &= ~P_CONTINUED; 1220 PROC_LOCK(q); 1221 sigqueue_take(p->p_ksi); 1222 PROC_UNLOCK(q); 1223 } 1224 PROC_UNLOCK(p); 1225 1226 if (status != NULL) 1227 *status = SIGCONT; 1228 if (siginfo != NULL) { 1229 siginfo->si_status = SIGCONT; 1230 siginfo->si_code = CLD_CONTINUED; 1231 } 1232 return (0); 1233 } 1234 PROC_UNLOCK(p); 1235 } 1236 1237 /* 1238 * Look in the orphans list too, to allow the parent to 1239 * collect it's child exit status even if child is being 1240 * debugged. 1241 * 1242 * Debugger detaches from the parent upon successful 1243 * switch-over from parent to child. At this point due to 1244 * re-parenting the parent loses the child to debugger and a 1245 * wait4(2) call would report that it has no children to wait 1246 * for. By maintaining a list of orphans we allow the parent 1247 * to successfully wait until the child becomes a zombie. 1248 */ 1249 LIST_FOREACH(p, &q->p_orphans, p_orphan) { 1250 ret = proc_to_reap(td, p, idtype, id, status, options, 1251 wrusage, siginfo); 1252 if (ret == 0) 1253 continue; 1254 else if (ret == 1) 1255 nfound++; 1256 else 1257 return (0); 1258 } 1259 if (nfound == 0) { 1260 sx_xunlock(&proctree_lock); 1261 return (ECHILD); 1262 } 1263 if (options & WNOHANG) { 1264 sx_xunlock(&proctree_lock); 1265 td->td_retval[0] = 0; 1266 return (0); 1267 } 1268 PROC_LOCK(q); 1269 sx_xunlock(&proctree_lock); 1270 if (q->p_flag & P_STATCHILD) { 1271 q->p_flag &= ~P_STATCHILD; 1272 error = 0; 1273 } else 1274 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 1275 PROC_UNLOCK(q); 1276 if (error) 1277 return (error); 1278 goto loop; 1279} 1280 1281/* 1282 * Make process 'parent' the new parent of process 'child'. 1283 * Must be called with an exclusive hold of proctree lock. 1284 */ 1285void 1286proc_reparent(struct proc *child, struct proc *parent) 1287{ 1288 1289 sx_assert(&proctree_lock, SX_XLOCKED); 1290 PROC_LOCK_ASSERT(child, MA_OWNED); 1291 if (child->p_pptr == parent) 1292 return; 1293 1294 PROC_LOCK(child->p_pptr); 1295 sigqueue_take(child->p_ksi); 1296 PROC_UNLOCK(child->p_pptr); 1297 LIST_REMOVE(child, p_sibling); 1298 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1299 1300 clear_orphan(child); 1301 if (child->p_flag & P_TRACED) { 1302 if (LIST_EMPTY(&child->p_pptr->p_orphans)) { 1303 child->p_treeflag |= P_TREE_FIRST_ORPHAN; 1304 LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, 1305 p_orphan); 1306 } else { 1307 LIST_INSERT_AFTER(LIST_FIRST(&child->p_pptr->p_orphans), 1308 child, p_orphan); 1309 } 1310 child->p_treeflag |= P_TREE_ORPHANED; 1311 } 1312 1313 child->p_pptr = parent; 1314} 1315