1/* 2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ 29/* 30 * Copyright (c) 1982, 1986, 1989, 1991, 1993 31 * The Regents of the University of California. All rights reserved. 32 * (c) UNIX System Laboratories, Inc. 33 * All or some portions of this file are derived from material licensed 34 * to the University of California by American Telephone and Telegraph 35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 36 * the permission of UNIX System Laboratories, Inc. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 67 */ 68/* 69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce 70 * support for mandatory and extensible security protections. This notice 71 * is included in support of clause 2.2 (b) of the Apple Public License, 72 * Version 2.0. 73 */ 74 75#include <machine/reg.h> 76#include <machine/psl.h> 77 78#include "compat_43.h" 79 80#include <sys/param.h> 81#include <sys/systm.h> 82#include <sys/ioctl.h> 83#include <sys/proc_internal.h> 84#include <sys/proc.h> 85#include <sys/kauth.h> 86#include <sys/tty.h> 87#include <sys/time.h> 88#include <sys/resource.h> 89#include <sys/kernel.h> 90#include <sys/wait.h> 91#include <sys/file_internal.h> 92#include <sys/vnode_internal.h> 93#include <sys/syslog.h> 94#include <sys/malloc.h> 95#include <sys/resourcevar.h> 96#include <sys/ptrace.h> 97#include <sys/user.h> 98#include <sys/aio_kern.h> 99#include <sys/sysproto.h> 100#include <sys/signalvar.h> 101#include <sys/kdebug.h> 102#include <sys/filedesc.h> /* fdfree */ 103#if SYSV_SHM 104#include <sys/shm_internal.h> /* shmexit */ 105#endif 106#include <sys/acct.h> /* acct_process */ 107 108#include <security/audit/audit.h> 109#include <bsm/audit_kevents.h> 110 111#include <mach/mach_types.h> 112 113#include <kern/kern_types.h> 114#include <kern/kalloc.h> 115#include <kern/task.h> 116#include <kern/thread.h> 117#include <kern/thread_call.h> 118#include <kern/sched_prim.h> 119#include <kern/assert.h> 120#include <sys/codesign.h> 121 122#if VM_PRESSURE_EVENTS 123#include <kern/vm_pressure.h> 124#endif 125 126#if CONFIG_MEMORYSTATUS 127#include <sys/kern_memorystatus.h> 128#endif 129 130#if CONFIG_DTRACE 131/* Do not include dtrace.h, it redefines kmem_[alloc/free] */ 132extern void (*dtrace_fasttrap_exit_ptr)(proc_t); 133extern void (*dtrace_helpers_cleanup)(proc_t); 134extern void dtrace_lazy_dofs_destroy(proc_t); 135 136#include <sys/dtrace_ptss.h> 137#endif 138 139#if CONFIG_MACF 140#include <security/mac.h> 141#include <sys/syscall.h> 142#endif 143 144#include <mach/mach_types.h> 145#include <mach/task.h> 146#include <mach/thread_act.h> 147 148#include <vm/vm_protos.h> 149 150#include <sys/sdt.h> 151 152extern char init_task_failure_data[]; 153void proc_prepareexit(proc_t p, int rv, boolean_t perf_notify); 154void vfork_exit(proc_t p, int rv); 155void vproc_exit(proc_t p); 156__private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p); 157__private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p); 158static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock); 159 160/* 161 * Things which should have prototypes in headers, but don't 162 */ 163void proc_exit(proc_t p); 164int wait1continue(int result); 165int waitidcontinue(int result); 166kern_return_t sys_perf_notify(thread_t thread, int pid); 167kern_return_t task_exception_notify(exception_type_t exception, 168 mach_exception_data_type_t code, mach_exception_data_type_t subcode); 169void delay(int); 170void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor); 171 172/* 173 * NOTE: Source and target may *NOT* overlap! 174 * XXX Should share code with bsd/dev/ppc/unix_signal.c 175 */ 176void 177siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out) 178{ 179 out->si_signo = in->si_signo; 180 out->si_errno = in->si_errno; 181 out->si_code = in->si_code; 182 out->si_pid = in->si_pid; 183 out->si_uid = in->si_uid; 184 out->si_status = in->si_status; 185 out->si_addr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_addr); 186 /* following cast works for sival_int because of padding */ 187 out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_value.sival_ptr); 188 out->si_band = in->si_band; /* range reduction */ 189} 190 191void 192siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out) 193{ 194 out->si_signo = in->si_signo; 195 out->si_errno = in->si_errno; 196 out->si_code = in->si_code; 197 out->si_pid = in->si_pid; 198 out->si_uid = in->si_uid; 199 out->si_status = in->si_status; 200 out->si_addr = in->si_addr; 201 /* following cast works for sival_int because of padding */ 202 out->si_value.sival_ptr = in->si_value.sival_ptr; 203 out->si_band = in->si_band; /* range reduction */ 204} 205 206static int 207copyoutsiginfo(user_siginfo_t *native, boolean_t is64, user_addr_t uaddr) 208{ 209 if (is64) { 210 user64_siginfo_t sinfo64; 211 212 bzero(&sinfo64, sizeof (sinfo64)); 213 siginfo_user_to_user64(native, &sinfo64); 214 return (copyout(&sinfo64, uaddr, sizeof (sinfo64))); 215 } else { 216 user32_siginfo_t sinfo32; 217 218 bzero(&sinfo32, sizeof (sinfo32)); 219 siginfo_user_to_user32(native, &sinfo32); 220 return (copyout(&sinfo32, uaddr, sizeof (sinfo32))); 221 } 222} 223 224/* 225 * exit -- 226 * Death of process. 227 */ 228void 229exit(proc_t p, struct exit_args *uap, int *retval) 230{ 231 exit1(p, W_EXITCODE(uap->rval, 0), retval); 232 233 thread_exception_return(); 234 /* NOTREACHED */ 235 while (TRUE) 236 thread_block(THREAD_CONTINUE_NULL); 237 /* NOTREACHED */ 238} 239 240/* 241 * Exit: deallocate address space and other resources, change proc state 242 * to zombie, and unlink proc from allproc and parent's lists. Save exit 243 * status and rusage for wait(). Check for child processes and orphan them. 244 */ 245int 246exit1(proc_t p, int rv, int *retval) 247{ 248 return exit1_internal(p, rv, retval, TRUE, TRUE, 0); 249} 250 251int 252exit1_internal(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify, 253 int jetsam_flags) 254{ 255 thread_t self = current_thread(); 256 struct task *task = p->task; 257 struct uthread *ut; 258 int error = 0; 259 260 /* 261 * If a thread in this task has already 262 * called exit(), then halt any others 263 * right here. 264 */ 265 266 ut = get_bsdthread_info(self); 267 if (ut->uu_flag & UT_VFORK) { 268 if (!thread_can_terminate) { 269 return EINVAL; 270 } 271 272 vfork_exit(p, rv); 273 vfork_return(p , retval, p->p_pid); 274 unix_syscall_return(0); 275 /* NOT REACHED */ 276 } 277 278 /* 279 * The parameter list of audit_syscall_exit() was augmented to 280 * take the Darwin syscall number as the first parameter, 281 * which is currently required by mac_audit_postselect(). 282 */ 283 284 /* 285 * The BSM token contains two components: an exit status as passed 286 * to exit(), and a return value to indicate what sort of exit it 287 * was. The exit status is WEXITSTATUS(rv), but it's not clear 288 * what the return value is. 289 */ 290 AUDIT_ARG(exit, WEXITSTATUS(rv), 0); 291 AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */ 292 293 DTRACE_PROC1(exit, int, CLD_EXITED); 294 295 /* mark process is going to exit and pull out of DBG/disk throttle */ 296 /* TODO: This should be done after becoming exit thread */ 297 proc_set_task_policy(p->task, THREAD_NULL, TASK_POLICY_ATTRIBUTE, 298 TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE); 299 300 proc_lock(p); 301 error = proc_transstart(p, 1, ((jetsam_flags & P_JETSAM_VNODE) ? 1 : 0)); 302 if (error == EDEADLK) { 303 /* Temp: If deadlock error, then it implies multithreaded exec is 304 * in progress. Instread of letting exit continue and 305 * corrupting the freed memory, let the exit thread 306 * return. This will save corruption in remote case. 307 */ 308 proc_unlock(p); 309 if (current_proc() == p){ 310 if (p->exit_thread == self) 311 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n", 312 p->p_comm, p->p_pid); 313 thread_exception_return(); 314 } else { 315 /* external termination like jetsam */ 316 return(error); 317 } 318 } 319 320 while (p->exit_thread != self) { 321 if (sig_try_locked(p) <= 0) { 322 proc_transend(p, 1); 323 if (get_threadtask(self) != task) { 324 proc_unlock(p); 325 return(0); 326 } 327 proc_unlock(p); 328 329 thread_terminate(self); 330 if (!thread_can_terminate) { 331 return 0; 332 } 333 334 thread_exception_return(); 335 /* NOTREACHED */ 336 } 337 sig_lock_to_exit(p); 338 } 339 if (p == initproc && current_proc() == p) { 340 proc_unlock(p); 341 printf("pid 1 exited (signal %d, exit %d)", 342 WTERMSIG(rv), WEXITSTATUS(rv)); 343#if (DEVELOPMENT || DEBUG) 344 int err; 345 /* 346 * For debugging purposes, generate a core file of initproc before 347 * panicking. Leave at least 300 MB free on the root volume, and ignore 348 * the process's corefile ulimit. 349 */ 350 if ((err = coredump(p, 300, 1)) != 0) { 351 printf("Failed to generate initproc core file: error %d", err); 352 } else { 353 printf("Generated initproc core file"); 354 sync(p, (void *)NULL, (int *)NULL); 355 } 356#endif 357 panic("%s died\nState at Last Exception:\n\n%s", 358 (p->p_comm[0] != '\0' ? 359 p->p_comm : 360 "launchd"), 361 init_task_failure_data); 362 } 363 364 p->p_lflag |= P_LEXIT; 365 p->p_xstat = rv; 366 p->p_lflag |= jetsam_flags; 367 368 proc_transend(p, 1); 369 proc_unlock(p); 370 371 proc_prepareexit(p, rv, perf_notify); 372 373 /* Last thread to terminate will call proc_exit() */ 374 task_terminate_internal(task); 375 376 return(0); 377} 378 379void 380proc_prepareexit(proc_t p, int rv, boolean_t perf_notify) 381{ 382 mach_exception_data_type_t code, subcode; 383 struct uthread *ut; 384 thread_t self = current_thread(); 385 ut = get_bsdthread_info(self); 386 struct rusage_superset *rup; 387 388 /* If a core should be generated, notify crash reporter */ 389 if (hassigprop(WTERMSIG(rv), SA_CORE) || ((p->p_csflags & CS_KILLED) != 0)) { 390 /* 391 * Workaround for processes checking up on PT_DENY_ATTACH: 392 * should be backed out post-Leopard (details in 5431025). 393 */ 394 if ((SIGSEGV == WTERMSIG(rv)) && 395 (p->p_pptr->p_lflag & P_LNOATTACH)) { 396 goto skipcheck; 397 } 398 399 /* 400 * Crash Reporter looks for the signal value, original exception 401 * type, and low 20 bits of the original code in code[0] 402 * (8, 4, and 20 bits respectively). code[1] is unmodified. 403 */ 404 code = ((WTERMSIG(rv) & 0xff) << 24) | 405 ((ut->uu_exception & 0x0f) << 20) | 406 ((int)ut->uu_code & 0xfffff); 407 subcode = ut->uu_subcode; 408 (void) task_exception_notify(EXC_CRASH, code, subcode); 409 } 410 411skipcheck: 412 /* Notify the perf server? */ 413 if (perf_notify) { 414 (void)sys_perf_notify(self, p->p_pid); 415 } 416 417 /* 418 * Before this process becomes a zombie, stash resource usage 419 * stats in the proc for external observers to query 420 * via proc_pid_rusage(). 421 * 422 * If the zombie allocation fails, just punt the stats. 423 */ 424 MALLOC_ZONE(rup, struct rusage_superset *, 425 sizeof (*rup), M_ZOMBIE, M_WAITOK); 426 if (rup != NULL) { 427 gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT); 428 rup->ri.ri_phys_footprint = 0; 429 rup->ri.ri_proc_exit_abstime = mach_absolute_time(); 430 431 /* 432 * Make the rusage_info visible to external observers 433 * only after it has been completely filled in. 434 */ 435 p->p_ru = rup; 436 } 437 438 /* 439 * Remove proc from allproc queue and from pidhash chain. 440 * Need to do this before we do anything that can block. 441 * Not doing causes things like mount() find this on allproc 442 * in partially cleaned state. 443 */ 444 445 proc_list_lock(); 446 447#if CONFIG_MEMORYSTATUS 448 memorystatus_remove(p, TRUE); 449#endif 450 451 LIST_REMOVE(p, p_list); 452 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */ 453 /* will not be visible via proc_find */ 454 p->p_listflag |= P_LIST_EXITED; 455 456 proc_list_unlock(); 457 458 459#ifdef PGINPROF 460 vmsizmon(); 461#endif 462 /* 463 * If parent is waiting for us to exit or exec, 464 * P_LPPWAIT is set; we will wakeup the parent below. 465 */ 466 proc_lock(p); 467 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT); 468 p->p_sigignore = ~(sigcantmask); 469 ut->uu_siglist = 0; 470 proc_unlock(p); 471} 472 473void 474proc_exit(proc_t p) 475{ 476 proc_t q; 477 proc_t pp; 478 struct task *task = p->task; 479 vnode_t tvp = NULLVP; 480 struct pgrp * pg; 481 struct session *sessp; 482 struct uthread * uth; 483 pid_t pid; 484 int exitval; 485 int knote_hint; 486 487 uth = current_uthread(); 488 489 proc_lock(p); 490 proc_transstart(p, 1, 0); 491 if( !(p->p_lflag & P_LEXIT)) { 492 /* 493 * This can happen if a thread_terminate() occurs 494 * in a single-threaded process. 495 */ 496 p->p_lflag |= P_LEXIT; 497 proc_transend(p, 1); 498 proc_unlock(p); 499 proc_prepareexit(p, 0, TRUE); 500 (void) task_terminate_internal(task); 501 proc_lock(p); 502 } else { 503 proc_transend(p, 1); 504 } 505 506 p->p_lflag |= P_LPEXIT; 507 508 /* 509 * Other kernel threads may be in the middle of signalling this process. 510 * Wait for those threads to wrap it up before making the process 511 * disappear on them. 512 */ 513 if ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 0)) { 514 p->p_sigwaitcnt++; 515 while ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 1)) 516 msleep(&p->p_sigmask, &p->p_mlock, PWAIT, "proc_sigdrain", NULL); 517 p->p_sigwaitcnt--; 518 } 519 520 proc_unlock(p); 521 pid = p->p_pid; 522 exitval = p->p_xstat; 523 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, 524 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START, 525 pid, exitval, 0, 0, 0); 526 527#if CONFIG_DTRACE 528 /* 529 * Free any outstanding lazy dof entries. It is imperative we 530 * always call dtrace_lazy_dofs_destroy, rather than null check 531 * and call if !NULL. If we NULL test, during lazy dof faulting 532 * we can race with the faulting code and proceed from here to 533 * beyond the helpers cleanup. The lazy dof faulting will then 534 * install new helpers which will never be cleaned up, and leak. 535 */ 536 dtrace_lazy_dofs_destroy(p); 537 538 /* 539 * Clean up any DTrace helper actions or probes for the process. 540 */ 541 if (p->p_dtrace_helpers != NULL) { 542 (*dtrace_helpers_cleanup)(p); 543 } 544 545 /* 546 * Clean up any DTrace probes associated with this process. 547 */ 548 /* 549 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(), 550 * call this after dtrace_helpers_cleanup() 551 */ 552 proc_lock(p); 553 if (p->p_dtrace_probes && dtrace_fasttrap_exit_ptr) { 554 (*dtrace_fasttrap_exit_ptr)(p); 555 } 556 proc_unlock(p); 557#endif 558 559 nspace_proc_exit(p); 560 561#if VM_PRESSURE_EVENTS 562 vm_pressure_proc_cleanup(p); 563#endif 564 565 /* 566 * need to cancel async IO requests that can be cancelled and wait for those 567 * already active. MAY BLOCK! 568 */ 569 570 proc_refdrain(p); 571 572 /* if any pending cpu limits action, clear it */ 573 task_clear_cpuusage(p->task, TRUE); 574 575 workqueue_mark_exiting(p); 576 workqueue_exit(p); 577 578 _aio_exit( p ); 579 580 /* 581 * Close open files and release open-file table. 582 * This may block! 583 */ 584 fdfree(p); 585 586 if (uth->uu_lowpri_window) { 587 /* 588 * task is marked as a low priority I/O type 589 * and the I/O we issued while in flushing files on close 590 * collided with normal I/O operations... 591 * no need to throttle this thread since its going away 592 * but we do need to update our bookeeping w/r to throttled threads 593 */ 594 throttle_lowpri_io(0); 595 } 596 597#if SYSV_SHM 598 /* Close ref SYSV Shared memory*/ 599 if (p->vm_shm) 600 shmexit(p); 601#endif 602#if SYSV_SEM 603 /* Release SYSV semaphores */ 604 semexit(p); 605#endif 606 607#if PSYNCH 608 pth_proc_hashdelete(p); 609#endif /* PSYNCH */ 610 611 sessp = proc_session(p); 612 if (SESS_LEADER(p, sessp)) { 613 614 if (sessp->s_ttyvp != NULLVP) { 615 struct vnode *ttyvp; 616 int ttyvid; 617 int cttyflag = 0; 618 struct vfs_context context; 619 struct tty *tp; 620 621 /* 622 * Controlling process. 623 * Signal foreground pgrp, 624 * drain controlling terminal 625 * and revoke access to controlling terminal. 626 */ 627 session_lock(sessp); 628 tp = SESSION_TP(sessp); 629 if ((tp != TTY_NULL) && (tp->t_session == sessp)) { 630 session_unlock(sessp); 631 632 /* 633 * We're going to SIGHUP the foreground process 634 * group. It can't change from this point on 635 * until the revoke is complete. 636 * The process group changes under both the tty 637 * lock and proc_list_lock but we need only one 638 */ 639 tty_lock(tp); 640 ttysetpgrphup(tp); 641 tty_unlock(tp); 642 643 tty_pgsignal(tp, SIGHUP, 1); 644 645 session_lock(sessp); 646 tp = SESSION_TP(sessp); 647 } 648 cttyflag = sessp->s_flags & S_CTTYREF; 649 sessp->s_flags &= ~S_CTTYREF; 650 ttyvp = sessp->s_ttyvp; 651 ttyvid = sessp->s_ttyvid; 652 sessp->s_ttyvp = NULLVP; 653 sessp->s_ttyvid = 0; 654 sessp->s_ttyp = TTY_NULL; 655 sessp->s_ttypgrpid = NO_PID; 656 session_unlock(sessp); 657 658 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) { 659 if (tp != TTY_NULL) { 660 tty_lock(tp); 661 (void) ttywait(tp); 662 tty_unlock(tp); 663 } 664 context.vc_thread = proc_thread(p); /* XXX */ 665 context.vc_ucred = kauth_cred_proc_ref(p); 666 VNOP_REVOKE(ttyvp, REVOKEALL, &context); 667 if (cttyflag) { 668 /* 669 * Release the extra usecount taken in cttyopen. 670 * usecount should be released after VNOP_REVOKE is called. 671 * This usecount was taken to ensure that 672 * the VNOP_REVOKE results in a close to 673 * the tty since cttyclose is a no-op. 674 */ 675 vnode_rele(ttyvp); 676 } 677 vnode_put(ttyvp); 678 kauth_cred_unref(&context.vc_ucred); 679 ttyvp = NULLVP; 680 } 681 if (tp) { 682 /* 683 * This is cleared even if not set. This is also done in 684 * spec_close to ensure that the flag is cleared. 685 */ 686 tty_lock(tp); 687 ttyclrpgrphup(tp); 688 tty_unlock(tp); 689 690 ttyfree(tp); 691 } 692 } 693 session_lock(sessp); 694 sessp->s_leader = NULL; 695 session_unlock(sessp); 696 } 697 session_rele(sessp); 698 699 pg = proc_pgrp(p); 700 fixjobc(p, pg, 0); 701 pg_rele(pg); 702 703 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 704 (void)acct_process(p); 705 706 proc_list_lock(); 707 708 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) { 709 p->p_listflag &= ~P_LIST_EXITCOUNT; 710 proc_shutdown_exitcount--; 711 if (proc_shutdown_exitcount == 0) 712 wakeup(&proc_shutdown_exitcount); 713 } 714 715 /* wait till parentrefs are dropped and grant no more */ 716 proc_childdrainstart(p); 717 while ((q = p->p_children.lh_first) != NULL) { 718 int reparentedtoinit = (q->p_listflag & P_LIST_DEADPARENT) ? 1 : 0; 719 if (q->p_stat == SZOMB) { 720 if (p != q->p_pptr) 721 panic("parent child linkage broken"); 722 /* check for sysctl zomb lookup */ 723 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { 724 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); 725 } 726 q->p_listflag |= P_LIST_WAITING; 727 /* 728 * This is a named reference and it is not granted 729 * if the reap is already in progress. So we get 730 * the reference here exclusively and their can be 731 * no waiters. So there is no need for a wakeup 732 * after we are done. Also the reap frees the structure 733 * and the proc struct cannot be used for wakeups as well. 734 * It is safe to use q here as this is system reap 735 */ 736 (void)reap_child_locked(p, q, 1, reparentedtoinit, 1, 0); 737 } else { 738 /* 739 * Traced processes are killed 740 * since their existence means someone is messing up. 741 */ 742 if (q->p_lflag & P_LTRACED) { 743 struct proc *opp; 744 745 /* 746 * Take a reference on the child process to 747 * ensure it doesn't exit and disappear between 748 * the time we drop the list_lock and attempt 749 * to acquire its proc_lock. 750 */ 751 if (proc_ref_locked(q) != q) 752 continue; 753 754 proc_list_unlock(); 755 756 opp = proc_find(q->p_oppid); 757 if (opp != PROC_NULL) { 758 proc_list_lock(); 759 q->p_oppid = 0; 760 proc_list_unlock(); 761 proc_reparentlocked(q, opp, 0, 0); 762 proc_rele(opp); 763 } else { 764 /* original parent exited while traced */ 765 proc_list_lock(); 766 q->p_listflag |= P_LIST_DEADPARENT; 767 q->p_oppid = 0; 768 proc_list_unlock(); 769 proc_reparentlocked(q, initproc, 0, 0); 770 } 771 772 proc_lock(q); 773 q->p_lflag &= ~P_LTRACED; 774 775 if (q->sigwait_thread) { 776 thread_t thread = q->sigwait_thread; 777 778 proc_unlock(q); 779 /* 780 * The sigwait_thread could be stopped at a 781 * breakpoint. Wake it up to kill. 782 * Need to do this as it could be a thread which is not 783 * the first thread in the task. So any attempts to kill 784 * the process would result into a deadlock on q->sigwait. 785 */ 786 thread_resume(thread); 787 clear_wait(thread, THREAD_INTERRUPTED); 788 threadsignal(thread, SIGKILL, 0); 789 } else { 790 proc_unlock(q); 791 } 792 793 psignal(q, SIGKILL); 794 proc_list_lock(); 795 proc_rele_locked(q); 796 } else { 797 q->p_listflag |= P_LIST_DEADPARENT; 798 proc_reparentlocked(q, initproc, 0, 1); 799 } 800 } 801 } 802 803 proc_childdrainend(p); 804 proc_list_unlock(); 805 806 /* 807 * Release reference to text vnode 808 */ 809 tvp = p->p_textvp; 810 p->p_textvp = NULL; 811 if (tvp != NULLVP) { 812 vnode_rele(tvp); 813 } 814 815 /* 816 * Save exit status and final rusage info, adding in child rusage 817 * info and self times. If we were unable to allocate a zombie 818 * structure, this information is lost. 819 */ 820 if (p->p_ru != NULL) { 821 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, NULL); 822 p->p_ru->ru = p->p_stats->p_ru; 823 824 ruadd(&(p->p_ru->ru), &p->p_stats->p_cru); 825 } 826 827 /* 828 * Free up profiling buffers. 829 */ 830 { 831 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn; 832 833 p1 = p0->pr_next; 834 p0->pr_next = NULL; 835 p0->pr_scale = 0; 836 837 for (; p1 != NULL; p1 = pn) { 838 pn = p1->pr_next; 839 kfree(p1, sizeof *p1); 840 } 841 } 842 843 proc_spinlock(p); 844 if (thread_call_cancel(p->p_rcall)) 845 p->p_ractive--; 846 847 while (p->p_ractive > 0) { 848 proc_spinunlock(p); 849 850 delay(1); 851 852 proc_spinlock(p); 853 } 854 proc_spinunlock(p); 855 856 thread_call_free(p->p_rcall); 857 p->p_rcall = NULL; 858 859 /* 860 * Other substructures are freed from wait(). 861 */ 862 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS); 863 p->p_stats = NULL; 864 865 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS); 866 p->p_sigacts = NULL; 867 868 proc_limitdrop(p, 1); 869 p->p_limit = NULL; 870 871 vm_purgeable_disown(p->task); 872 873 /* 874 * Finish up by terminating the task 875 * and halt this thread (only if a 876 * member of the task exiting). 877 */ 878 p->task = TASK_NULL; 879 set_bsdtask_info(task, NULL); 880 881 knote_hint = NOTE_EXIT | (p->p_xstat & 0xffff); 882 proc_knote(p, knote_hint); 883 884 /* mark the thread as the one that is doing proc_exit 885 * no need to hold proc lock in uthread_free 886 */ 887 uth->uu_flag |= UT_PROCEXIT; 888 /* 889 * Notify parent that we're gone. 890 */ 891 pp = proc_parent(p); 892 if (pp->p_flag & P_NOCLDWAIT) { 893 894 if (p->p_ru != NULL) { 895 proc_lock(pp); 896#if 3839178 897 /* 898 * If the parent is ignoring SIGCHLD, then POSIX requires 899 * us to not add the resource usage to the parent process - 900 * we are only going to hand it off to init to get reaped. 901 * We should contest the standard in this case on the basis 902 * of RLIMIT_CPU. 903 */ 904#else /* !3839178 */ 905 /* 906 * Add child resource usage to parent before giving 907 * zombie to init. If we were unable to allocate a 908 * zombie structure, this information is lost. 909 */ 910 ruadd(&pp->p_stats->p_cru, &p->p_ru->ru); 911#endif /* !3839178 */ 912 update_rusage_info_child(&pp->p_stats->ri_child, &p->p_ru->ri); 913 proc_unlock(pp); 914 } 915 916 /* kernel can reap this one, no need to move it to launchd */ 917 proc_list_lock(); 918 p->p_listflag |= P_LIST_DEADPARENT; 919 proc_list_unlock(); 920 } 921 if ((p->p_listflag & P_LIST_DEADPARENT) == 0 || p->p_oppid) { 922 if (pp != initproc) { 923 proc_lock(pp); 924 pp->si_pid = p->p_pid; 925 pp->si_status = p->p_xstat; 926 pp->si_code = CLD_EXITED; 927 /* 928 * p_ucred usage is safe as it is an exiting process 929 * and reference is dropped in reap 930 */ 931 pp->si_uid = kauth_cred_getruid(p->p_ucred); 932 proc_unlock(pp); 933 } 934 /* mark as a zombie */ 935 /* No need to take proc lock as all refs are drained and 936 * no one except parent (reaping ) can look at this. 937 * The write is to an int and is coherent. Also parent is 938 * keyed off of list lock for reaping 939 */ 940 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, 941 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END, 942 pid, exitval, 0, 0, 0); 943 p->p_stat = SZOMB; 944 /* 945 * The current process can be reaped so, no one 946 * can depend on this 947 */ 948 949 psignal(pp, SIGCHLD); 950 951 /* and now wakeup the parent */ 952 proc_list_lock(); 953 wakeup((caddr_t)pp); 954 proc_list_unlock(); 955 } else { 956 /* should be fine as parent proc would be initproc */ 957 /* mark as a zombie */ 958 /* No need to take proc lock as all refs are drained and 959 * no one except parent (reaping ) can look at this. 960 * The write is to an int and is coherent. Also parent is 961 * keyed off of list lock for reaping 962 */ 963 proc_list_lock(); 964 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, 965 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END, 966 pid, exitval, 0, 0, 0); 967 /* check for sysctl zomb lookup */ 968 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { 969 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); 970 } 971 /* safe to use p as this is a system reap */ 972 p->p_stat = SZOMB; 973 p->p_listflag |= P_LIST_WAITING; 974 975 /* 976 * This is a named reference and it is not granted 977 * if the reap is already in progress. So we get 978 * the reference here exclusively and their can be 979 * no waiters. So there is no need for a wakeup 980 * after we are done. AlsO the reap frees the structure 981 * and the proc struct cannot be used for wakeups as well. 982 * It is safe to use p here as this is system reap 983 */ 984 (void)reap_child_locked(pp, p, 1, 0, 1, 1); 985 /* list lock dropped by reap_child_locked */ 986 } 987 if (uth->uu_lowpri_window) { 988 /* 989 * task is marked as a low priority I/O type and we've 990 * somehow picked up another throttle during exit processing... 991 * no need to throttle this thread since its going away 992 * but we do need to update our bookeeping w/r to throttled threads 993 */ 994 throttle_lowpri_io(0); 995 } 996 997 proc_rele(pp); 998 999} 1000 1001 1002/* 1003 * reap_child_locked 1004 * 1005 * Description: Given a process from which all status information needed 1006 * has already been extracted, if the process is a ptrace 1007 * attach process, detach it and give it back to its real 1008 * parent, else recover all resources remaining associated 1009 * with it. 1010 * 1011 * Parameters: proc_t parent Parent of process being reaped 1012 * proc_t child Process to reap 1013 * 1014 * Returns: 0 Process was not reaped because it 1015 * came from an attach 1016 * 1 Process was reaped 1017 */ 1018static int 1019reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock) 1020{ 1021 proc_t trace_parent = PROC_NULL; /* Traced parent process, if tracing */ 1022 1023 if (locked == 1) 1024 proc_list_unlock(); 1025 1026 /* 1027 * If we got the child via a ptrace 'attach', 1028 * we need to give it back to the old parent. 1029 * 1030 * Exception: someone who has been reparented to launchd before being 1031 * ptraced can simply be reaped, refer to radar 5677288 1032 * p_oppid -> ptraced 1033 * trace_parent == initproc -> away from launchd 1034 * reparentedtoinit -> came to launchd by reparenting 1035 */ 1036 if (child->p_oppid) { 1037 int knote_hint; 1038 pid_t oppid; 1039 1040 proc_lock(child); 1041 oppid = child->p_oppid; 1042 child->p_oppid = 0; 1043 knote_hint = NOTE_EXIT | (child->p_xstat & 0xffff); 1044 proc_unlock(child); 1045 1046 if ((trace_parent = proc_find(oppid)) 1047 && !((trace_parent == initproc) && reparentedtoinit)) { 1048 1049 if (trace_parent != initproc) { 1050 /* 1051 * proc internal fileds and p_ucred usage safe 1052 * here as child is dead and is not reaped or 1053 * reparented yet 1054 */ 1055 proc_lock(trace_parent); 1056 trace_parent->si_pid = child->p_pid; 1057 trace_parent->si_status = child->p_xstat; 1058 trace_parent->si_code = CLD_CONTINUED; 1059 trace_parent->si_uid = kauth_cred_getruid(child->p_ucred); 1060 proc_unlock(trace_parent); 1061 } 1062 proc_reparentlocked(child, trace_parent, 1, 0); 1063 1064 /* resend knote to original parent (and others) after reparenting */ 1065 proc_knote(child, knote_hint); 1066 1067 psignal(trace_parent, SIGCHLD); 1068 proc_list_lock(); 1069 wakeup((caddr_t)trace_parent); 1070 child->p_listflag &= ~P_LIST_WAITING; 1071 wakeup(&child->p_stat); 1072 proc_list_unlock(); 1073 proc_rele(trace_parent); 1074 if ((locked == 1) && (droplock == 0)) 1075 proc_list_lock(); 1076 return (0); 1077 } 1078 1079 /* 1080 * If we can't reparent (e.g. the original parent exited while child was being debugged, or 1081 * original parent is the same as the debugger currently exiting), we still need to satisfy 1082 * the knote lifecycle for other observers on the system. While the debugger was attached, 1083 * the NOTE_EXIT would not have been broadcast during initial child termination. 1084 */ 1085 proc_knote(child, knote_hint); 1086 1087 if (trace_parent != PROC_NULL) { 1088 proc_rele(trace_parent); 1089 } 1090 } 1091 1092#pragma clang diagnostic push 1093#pragma clang diagnostic ignored "-Wdeprecated-declarations" 1094 proc_knote(child, NOTE_REAP); 1095#pragma clang diagnostic pop 1096 1097 proc_knote_drain(child); 1098 1099 child->p_xstat = 0; 1100 if (child->p_ru) { 1101 proc_lock(parent); 1102#if 3839178 1103 /* 1104 * If the parent is ignoring SIGCHLD, then POSIX requires 1105 * us to not add the resource usage to the parent process - 1106 * we are only going to hand it off to init to get reaped. 1107 * We should contest the standard in this case on the basis 1108 * of RLIMIT_CPU. 1109 */ 1110 if (!(parent->p_flag & P_NOCLDWAIT)) 1111#endif /* 3839178 */ 1112 ruadd(&parent->p_stats->p_cru, &child->p_ru->ru); 1113 update_rusage_info_child(&parent->p_stats->ri_child, &child->p_ru->ri); 1114 proc_unlock(parent); 1115 FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE); 1116 child->p_ru = NULL; 1117 } else { 1118 printf("Warning : lost p_ru for %s\n", child->p_comm); 1119 } 1120 1121 AUDIT_SESSION_PROCEXIT(child); 1122 1123 /* 1124 * Decrement the count of procs running with this uid. 1125 * p_ucred usage is safe here as it is an exited process. 1126 * and refernce is dropped after these calls down below 1127 * (locking protection is provided by list lock held in chgproccnt) 1128 */ 1129 (void)chgproccnt(kauth_cred_getruid(child->p_ucred), -1); 1130 1131#if CONFIG_LCTX 1132 ALLLCTX_LOCK; 1133 leavelctx(child); 1134 ALLLCTX_UNLOCK; 1135#endif 1136 1137 /* 1138 * Free up credentials. 1139 */ 1140 if (IS_VALID_CRED(child->p_ucred)) { 1141 kauth_cred_unref(&child->p_ucred); 1142 } 1143 1144 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */ 1145 1146 /* 1147 * Finally finished with old proc entry. 1148 * Unlink it from its process group and free it. 1149 */ 1150 leavepgrp(child); 1151 1152 proc_list_lock(); 1153 LIST_REMOVE(child, p_list); /* off zombproc */ 1154 parent->p_childrencnt--; 1155 LIST_REMOVE(child, p_sibling); 1156 /* If there are no more children wakeup parent */ 1157 if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children))) 1158 wakeup((caddr_t)parent); /* with list lock held */ 1159 child->p_listflag &= ~P_LIST_WAITING; 1160 wakeup(&child->p_stat); 1161 1162 /* Take it out of process hash */ 1163 LIST_REMOVE(child, p_hash); 1164 child->p_listflag &= ~P_LIST_INHASH; 1165 proc_checkdeadrefs(child); 1166 nprocs--; 1167 1168 if (deadparent) { 1169 /* 1170 * If a child zombie is being reaped because its parent 1171 * is exiting, make sure we update the list flag 1172 */ 1173 child->p_listflag |= P_LIST_DEADPARENT; 1174 } 1175 1176 proc_list_unlock(); 1177 1178#if CONFIG_FINE_LOCK_GROUPS 1179 lck_mtx_destroy(&child->p_mlock, proc_mlock_grp); 1180 lck_mtx_destroy(&child->p_fdmlock, proc_fdmlock_grp); 1181#if CONFIG_DTRACE 1182 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp); 1183#endif 1184 lck_spin_destroy(&child->p_slock, proc_slock_grp); 1185#else /* CONFIG_FINE_LOCK_GROUPS */ 1186 lck_mtx_destroy(&child->p_mlock, proc_lck_grp); 1187 lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp); 1188#if CONFIG_DTRACE 1189 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp); 1190#endif 1191 lck_spin_destroy(&child->p_slock, proc_lck_grp); 1192#endif /* CONFIG_FINE_LOCK_GROUPS */ 1193 workqueue_destroy_lock(child); 1194 1195 FREE_ZONE(child, sizeof *child, M_PROC); 1196 if ((locked == 1) && (droplock == 0)) 1197 proc_list_lock(); 1198 1199 return (1); 1200} 1201 1202 1203int 1204wait1continue(int result) 1205{ 1206 proc_t p; 1207 thread_t thread; 1208 uthread_t uth; 1209 struct _wait4_data *wait4_data; 1210 struct wait4_nocancel_args *uap; 1211 int *retval; 1212 1213 if (result) 1214 return(result); 1215 1216 p = current_proc(); 1217 thread = current_thread(); 1218 uth = (struct uthread *)get_bsdthread_info(thread); 1219 1220 wait4_data = &uth->uu_kevent.uu_wait4_data; 1221 uap = wait4_data->args; 1222 retval = wait4_data->retval; 1223 return(wait4_nocancel(p, uap, retval)); 1224} 1225 1226int 1227wait4(proc_t q, struct wait4_args *uap, int32_t *retval) 1228{ 1229 __pthread_testcancel(1); 1230 return(wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval)); 1231} 1232 1233int 1234wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval) 1235{ 1236 int nfound; 1237 int sibling_count; 1238 proc_t p; 1239 int status, error; 1240 uthread_t uth; 1241 struct _wait4_data *wait4_data; 1242 1243 AUDIT_ARG(pid, uap->pid); 1244 1245 if (uap->pid == 0) 1246 uap->pid = -q->p_pgrpid; 1247 1248loop: 1249 proc_list_lock(); 1250loop1: 1251 nfound = 0; 1252 sibling_count = 0; 1253 1254 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 1255 if ( p->p_sibling.le_next != 0 ) 1256 sibling_count++; 1257 if (uap->pid != WAIT_ANY && 1258 p->p_pid != uap->pid && 1259 p->p_pgrpid != -(uap->pid)) 1260 continue; 1261 1262 nfound++; 1263 1264 /* XXX This is racy because we don't get the lock!!!! */ 1265 1266 if (p->p_listflag & P_LIST_WAITING) { 1267 (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); 1268 goto loop1; 1269 } 1270 p->p_listflag |= P_LIST_WAITING; /* only allow single thread to wait() */ 1271 1272 1273 if (p->p_stat == SZOMB) { 1274 int reparentedtoinit = (p->p_listflag & P_LIST_DEADPARENT) ? 1 : 0; 1275 1276 proc_list_unlock(); 1277#if CONFIG_MACF 1278 if ((error = mac_proc_check_wait(q, p)) != 0) 1279 goto out; 1280#endif 1281 retval[0] = p->p_pid; 1282 if (uap->status) { 1283 /* Legacy apps expect only 8 bits of status */ 1284 status = 0xffff & p->p_xstat; /* convert to int */ 1285 error = copyout((caddr_t)&status, 1286 uap->status, 1287 sizeof(status)); 1288 if (error) 1289 goto out; 1290 } 1291 if (uap->rusage) { 1292 if (p->p_ru == NULL) { 1293 error = ENOMEM; 1294 } else { 1295 if (IS_64BIT_PROCESS(q)) { 1296 struct user64_rusage my_rusage; 1297 munge_user64_rusage(&p->p_ru->ru, &my_rusage); 1298 error = copyout((caddr_t)&my_rusage, 1299 uap->rusage, 1300 sizeof (my_rusage)); 1301 } 1302 else { 1303 struct user32_rusage my_rusage; 1304 munge_user32_rusage(&p->p_ru->ru, &my_rusage); 1305 error = copyout((caddr_t)&my_rusage, 1306 uap->rusage, 1307 sizeof (my_rusage)); 1308 } 1309 } 1310 /* information unavailable? */ 1311 if (error) 1312 goto out; 1313 } 1314 1315 /* Conformance change for 6577252. 1316 * When SIGCHLD is blocked and wait() returns because the status 1317 * of a child process is available and there are no other 1318 * children processes, then any pending SIGCHLD signal is cleared. 1319 */ 1320 if ( sibling_count == 0 ) { 1321 int mask = sigmask(SIGCHLD); 1322 uth = current_uthread(); 1323 1324 if ( (uth->uu_sigmask & mask) != 0 ) { 1325 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD. 1326 * This locking looks funny but it is protecting access to the 1327 * thread via p_uthlist. 1328 */ 1329 proc_lock(q); 1330 uth->uu_siglist &= ~mask; /* clear pending signal */ 1331 proc_unlock(q); 1332 } 1333 } 1334 1335 /* Clean up */ 1336 (void)reap_child_locked(q, p, 0, reparentedtoinit, 0, 0); 1337 1338 return (0); 1339 } 1340 if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 && 1341 (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) { 1342 proc_list_unlock(); 1343#if CONFIG_MACF 1344 if ((error = mac_proc_check_wait(q, p)) != 0) 1345 goto out; 1346#endif 1347 proc_lock(p); 1348 p->p_lflag |= P_LWAITED; 1349 proc_unlock(p); 1350 retval[0] = p->p_pid; 1351 if (uap->status) { 1352 status = W_STOPCODE(p->p_xstat); 1353 error = copyout((caddr_t)&status, 1354 uap->status, 1355 sizeof(status)); 1356 } else 1357 error = 0; 1358 goto out; 1359 } 1360 /* 1361 * If we are waiting for continued processses, and this 1362 * process was continued 1363 */ 1364 if ((uap->options & WCONTINUED) && 1365 (p->p_flag & P_CONTINUED)) { 1366 proc_list_unlock(); 1367#if CONFIG_MACF 1368 if ((error = mac_proc_check_wait(q, p)) != 0) 1369 goto out; 1370#endif 1371 1372 /* Prevent other process for waiting for this event */ 1373 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag); 1374 retval[0] = p->p_pid; 1375 if (uap->status) { 1376 status = W_STOPCODE(SIGCONT); 1377 error = copyout((caddr_t)&status, 1378 uap->status, 1379 sizeof(status)); 1380 } else 1381 error = 0; 1382 goto out; 1383 } 1384 p->p_listflag &= ~P_LIST_WAITING; 1385 wakeup(&p->p_stat); 1386 } 1387 /* list lock is held when we get here any which way */ 1388 if (nfound == 0) { 1389 proc_list_unlock(); 1390 return (ECHILD); 1391 } 1392 1393 if (uap->options & WNOHANG) { 1394 retval[0] = 0; 1395 proc_list_unlock(); 1396 return (0); 1397 } 1398 1399 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */ 1400 uth = current_uthread(); 1401 wait4_data = &uth->uu_kevent.uu_wait4_data; 1402 wait4_data->args = uap; 1403 wait4_data->retval = retval; 1404 1405 if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue))) 1406 return (error); 1407 1408 goto loop; 1409out: 1410 proc_list_lock(); 1411 p->p_listflag &= ~P_LIST_WAITING; 1412 wakeup(&p->p_stat); 1413 proc_list_unlock(); 1414 return (error); 1415} 1416 1417#if DEBUG 1418#define ASSERT_LCK_MTX_OWNED(lock) \ 1419 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED) 1420#else 1421#define ASSERT_LCK_MTX_OWNED(lock) /* nothing */ 1422#endif 1423 1424int 1425waitidcontinue(int result) 1426{ 1427 proc_t p; 1428 thread_t thread; 1429 uthread_t uth; 1430 struct _waitid_data *waitid_data; 1431 struct waitid_nocancel_args *uap; 1432 int *retval; 1433 1434 if (result) 1435 return (result); 1436 1437 p = current_proc(); 1438 thread = current_thread(); 1439 uth = (struct uthread *)get_bsdthread_info(thread); 1440 1441 waitid_data = &uth->uu_kevent.uu_waitid_data; 1442 uap = waitid_data->args; 1443 retval = waitid_data->retval; 1444 return(waitid_nocancel(p, uap, retval)); 1445} 1446 1447/* 1448 * Description: Suspend the calling thread until one child of the process 1449 * containing the calling thread changes state. 1450 * 1451 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL 1452 * uap->id pid_t or gid_t or ignored 1453 * uap->infop Address of siginfo_t struct in 1454 * user space into which to return status 1455 * uap->options flag values 1456 * 1457 * Returns: 0 Success 1458 * !0 Error returning status to user space 1459 */ 1460int 1461waitid(proc_t q, struct waitid_args *uap, int32_t *retval) 1462{ 1463 __pthread_testcancel(1); 1464 return (waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval)); 1465} 1466 1467int 1468waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap, 1469 __unused int32_t *retval) 1470{ 1471 user_siginfo_t siginfo; /* siginfo data to return to caller */ 1472 boolean_t caller64 = IS_64BIT_PROCESS(q); 1473 int nfound; 1474 proc_t p; 1475 int error; 1476 uthread_t uth; 1477 struct _waitid_data *waitid_data; 1478 1479 if (uap->options == 0 || 1480 (uap->options & ~(WNOHANG|WNOWAIT|WCONTINUED|WSTOPPED|WEXITED))) 1481 return (EINVAL); /* bits set that aren't recognized */ 1482 1483 switch (uap->idtype) { 1484 case P_PID: /* child with process ID equal to... */ 1485 case P_PGID: /* child with process group ID equal to... */ 1486 if (((int)uap->id) < 0) 1487 return (EINVAL); 1488 break; 1489 case P_ALL: /* any child */ 1490 break; 1491 } 1492 1493loop: 1494 proc_list_lock(); 1495loop1: 1496 nfound = 0; 1497 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 1498 1499 switch (uap->idtype) { 1500 case P_PID: /* child with process ID equal to... */ 1501 if (p->p_pid != (pid_t)uap->id) 1502 continue; 1503 break; 1504 case P_PGID: /* child with process group ID equal to... */ 1505 if (p->p_pgrpid != (pid_t)uap->id) 1506 continue; 1507 break; 1508 case P_ALL: /* any child */ 1509 break; 1510 } 1511 1512 /* XXX This is racy because we don't get the lock!!!! */ 1513 1514 /* 1515 * Wait collision; go to sleep and restart; used to maintain 1516 * the single return for waited process guarantee. 1517 */ 1518 if (p->p_listflag & P_LIST_WAITING) { 1519 (void) msleep(&p->p_stat, proc_list_mlock, 1520 PWAIT, "waitidcoll", 0); 1521 goto loop1; 1522 } 1523 p->p_listflag |= P_LIST_WAITING; /* mark busy */ 1524 1525 nfound++; 1526 1527 bzero(&siginfo, sizeof (siginfo)); 1528 1529 switch (p->p_stat) { 1530 case SZOMB: /* Exited */ 1531 if (!(uap->options & WEXITED)) 1532 break; 1533 proc_list_unlock(); 1534#if CONFIG_MACF 1535 if ((error = mac_proc_check_wait(q, p)) != 0) 1536 goto out; 1537#endif 1538 siginfo.si_signo = SIGCHLD; 1539 siginfo.si_pid = p->p_pid; 1540 siginfo.si_status = WEXITSTATUS(p->p_xstat); 1541 if (WIFSIGNALED(p->p_xstat)) { 1542 siginfo.si_code = WCOREDUMP(p->p_xstat) ? 1543 CLD_DUMPED : CLD_KILLED; 1544 } else 1545 siginfo.si_code = CLD_EXITED; 1546 1547 if ((error = copyoutsiginfo(&siginfo, 1548 caller64, uap->infop)) != 0) 1549 goto out; 1550 1551 /* Prevent other process for waiting for this event? */ 1552 if (!(uap->options & WNOWAIT)) { 1553 (void) reap_child_locked(q, p, 0, 0, 0, 0); 1554 return (0); 1555 } 1556 goto out; 1557 1558 case SSTOP: /* Stopped */ 1559 /* 1560 * If we are not interested in stopped processes, then 1561 * ignore this one. 1562 */ 1563 if (!(uap->options & WSTOPPED)) 1564 break; 1565 1566 /* 1567 * If someone has already waited it, we lost a race 1568 * to be the one to return status. 1569 */ 1570 if ((p->p_lflag & P_LWAITED) != 0) 1571 break; 1572 proc_list_unlock(); 1573#if CONFIG_MACF 1574 if ((error = mac_proc_check_wait(q, p)) != 0) 1575 goto out; 1576#endif 1577 siginfo.si_signo = SIGCHLD; 1578 siginfo.si_pid = p->p_pid; 1579 siginfo.si_status = p->p_xstat; /* signal number */ 1580 siginfo.si_code = CLD_STOPPED; 1581 1582 if ((error = copyoutsiginfo(&siginfo, 1583 caller64, uap->infop)) != 0) 1584 goto out; 1585 1586 /* Prevent other process for waiting for this event? */ 1587 if (!(uap->options & WNOWAIT)) { 1588 proc_lock(p); 1589 p->p_lflag |= P_LWAITED; 1590 proc_unlock(p); 1591 } 1592 goto out; 1593 1594 default: /* All other states => Continued */ 1595 if (!(uap->options & WCONTINUED)) 1596 break; 1597 1598 /* 1599 * If the flag isn't set, then this process has not 1600 * been stopped and continued, or the status has 1601 * already been reaped by another caller of waitid(). 1602 */ 1603 if ((p->p_flag & P_CONTINUED) == 0) 1604 break; 1605 proc_list_unlock(); 1606#if CONFIG_MACF 1607 if ((error = mac_proc_check_wait(q, p)) != 0) 1608 goto out; 1609#endif 1610 siginfo.si_signo = SIGCHLD; 1611 siginfo.si_code = CLD_CONTINUED; 1612 proc_lock(p); 1613 siginfo.si_pid = p->p_contproc; 1614 siginfo.si_status = p->p_xstat; 1615 proc_unlock(p); 1616 1617 if ((error = copyoutsiginfo(&siginfo, 1618 caller64, uap->infop)) != 0) 1619 goto out; 1620 1621 /* Prevent other process for waiting for this event? */ 1622 if (!(uap->options & WNOWAIT)) { 1623 OSBitAndAtomic(~((uint32_t)P_CONTINUED), 1624 &p->p_flag); 1625 } 1626 goto out; 1627 } 1628 ASSERT_LCK_MTX_OWNED(proc_list_mlock); 1629 1630 /* Not a process we are interested in; go on to next child */ 1631 1632 p->p_listflag &= ~P_LIST_WAITING; 1633 wakeup(&p->p_stat); 1634 } 1635 ASSERT_LCK_MTX_OWNED(proc_list_mlock); 1636 1637 /* No child processes that could possibly satisfy the request? */ 1638 1639 if (nfound == 0) { 1640 proc_list_unlock(); 1641 return (ECHILD); 1642 } 1643 1644 if (uap->options & WNOHANG) { 1645 proc_list_unlock(); 1646#if CONFIG_MACF 1647 if ((error = mac_proc_check_wait(q, p)) != 0) 1648 return (error); 1649#endif 1650 /* 1651 * The state of the siginfo structure in this case 1652 * is undefined. Some implementations bzero it, some 1653 * (like here) leave it untouched for efficiency. 1654 * 1655 * Thus the most portable check for "no matching pid with 1656 * WNOHANG" is to store a zero into si_pid before 1657 * invocation, then check for a non-zero value afterwards. 1658 */ 1659 return (0); 1660 } 1661 1662 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */ 1663 uth = current_uthread(); 1664 waitid_data = &uth->uu_kevent.uu_waitid_data; 1665 waitid_data->args = uap; 1666 waitid_data->retval = retval; 1667 1668 if ((error = msleep0(q, proc_list_mlock, 1669 PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)) != 0) 1670 return (error); 1671 1672 goto loop; 1673out: 1674 proc_list_lock(); 1675 p->p_listflag &= ~P_LIST_WAITING; 1676 wakeup(&p->p_stat); 1677 proc_list_unlock(); 1678 return (error); 1679} 1680 1681/* 1682 * make process 'parent' the new parent of process 'child'. 1683 */ 1684void 1685proc_reparentlocked(proc_t child, proc_t parent, int cansignal, int locked) 1686{ 1687 proc_t oldparent = PROC_NULL; 1688 1689 if (child->p_pptr == parent) 1690 return; 1691 1692 if (locked == 0) 1693 proc_list_lock(); 1694 1695 oldparent = child->p_pptr; 1696#if __PROC_INTERNAL_DEBUG 1697 if (oldparent == PROC_NULL) 1698 panic("proc_reparent: process %p does not have a parent\n", child); 1699#endif 1700 1701 LIST_REMOVE(child, p_sibling); 1702#if __PROC_INTERNAL_DEBUG 1703 if (oldparent->p_childrencnt == 0) 1704 panic("process children count already 0\n"); 1705#endif 1706 oldparent->p_childrencnt--; 1707#if __PROC_INTERNAL_DEBUG1 1708 if (oldparent->p_childrencnt < 0) 1709 panic("process children count -ve\n"); 1710#endif 1711 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1712 parent->p_childrencnt++; 1713 child->p_pptr = parent; 1714 child->p_ppid = parent->p_pid; 1715 1716 proc_list_unlock(); 1717 1718 if ((cansignal != 0) && (initproc == parent) && (child->p_stat == SZOMB)) 1719 psignal(initproc, SIGCHLD); 1720 if (locked == 1) 1721 proc_list_lock(); 1722} 1723 1724/* 1725 * Exit: deallocate address space and other resources, change proc state 1726 * to zombie, and unlink proc from allproc and parent's lists. Save exit 1727 * status and rusage for wait(). Check for child processes and orphan them. 1728 */ 1729 1730void 1731vfork_exit(proc_t p, int rv) 1732{ 1733 vfork_exit_internal(p, rv, 0); 1734} 1735 1736void 1737vfork_exit_internal(proc_t p, int rv, int forceexit) 1738{ 1739 thread_t self = current_thread(); 1740#ifdef FIXME 1741 struct task *task = p->task; 1742#endif 1743 struct uthread *ut; 1744 1745 /* 1746 * If a thread in this task has already 1747 * called exit(), then halt any others 1748 * right here. 1749 */ 1750 1751 ut = get_bsdthread_info(self); 1752 1753 1754 proc_lock(p); 1755 if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) { 1756 /* 1757 * This happens when a parent exits/killed and vfork is in progress 1758 * other threads. But shutdown code for ex has already called exit1() 1759 */ 1760 proc_unlock(p); 1761 return; 1762 } 1763 p->p_lflag |= (P_LEXIT | P_LPEXIT); 1764 proc_unlock(p); 1765 1766 if (forceexit == 0) { 1767 /* 1768 * parent of a vfork child has already called exit() and the 1769 * thread that has vfork in proress terminates. So there is no 1770 * separate address space here and it has already been marked for 1771 * termination. This was never covered before and could cause problems 1772 * if we block here for outside code. 1773 */ 1774 /* Notify the perf server */ 1775 (void)sys_perf_notify(self, p->p_pid); 1776 } 1777 1778 /* 1779 * Remove proc from allproc queue and from pidhash chain. 1780 * Need to do this before we do anything that can block. 1781 * Not doing causes things like mount() find this on allproc 1782 * in partially cleaned state. 1783 */ 1784 1785 proc_list_lock(); 1786 1787#if CONFIG_MEMORYSTATUS 1788 memorystatus_remove(p, TRUE); 1789#endif 1790 1791 LIST_REMOVE(p, p_list); 1792 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */ 1793 /* will not be visible via proc_find */ 1794 p->p_listflag |= P_LIST_EXITED; 1795 1796 proc_list_unlock(); 1797 1798 proc_lock(p); 1799 p->p_xstat = rv; 1800 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT); 1801 p->p_sigignore = ~0; 1802 proc_unlock(p); 1803 1804 proc_spinlock(p); 1805 if (thread_call_cancel(p->p_rcall)) 1806 p->p_ractive--; 1807 1808 while (p->p_ractive > 0) { 1809 proc_spinunlock(p); 1810 1811 delay(1); 1812 1813 proc_spinlock(p); 1814 } 1815 proc_spinunlock(p); 1816 1817 thread_call_free(p->p_rcall); 1818 p->p_rcall = NULL; 1819 1820 ut->uu_siglist = 0; 1821 1822 vproc_exit(p); 1823} 1824 1825void 1826vproc_exit(proc_t p) 1827{ 1828 proc_t q; 1829 proc_t pp; 1830 1831 vnode_t tvp; 1832#ifdef FIXME 1833 struct task *task = p->task; 1834#endif 1835 struct pgrp * pg; 1836 struct session *sessp; 1837 struct rusage_superset *rup; 1838 1839 /* XXX Zombie allocation may fail, in which case stats get lost */ 1840 MALLOC_ZONE(rup, struct rusage_superset *, 1841 sizeof (*rup), M_ZOMBIE, M_WAITOK); 1842 1843 proc_refdrain(p); 1844 1845 /* 1846 * Close open files and release open-file table. 1847 * This may block! 1848 */ 1849 fdfree(p); 1850 1851 sessp = proc_session(p); 1852 if (SESS_LEADER(p, sessp)) { 1853 1854 if (sessp->s_ttyvp != NULLVP) { 1855 struct vnode *ttyvp; 1856 int ttyvid; 1857 int cttyflag = 0; 1858 struct vfs_context context; 1859 struct tty *tp; 1860 1861 /* 1862 * Controlling process. 1863 * Signal foreground pgrp, 1864 * drain controlling terminal 1865 * and revoke access to controlling terminal. 1866 */ 1867 session_lock(sessp); 1868 tp = SESSION_TP(sessp); 1869 if ((tp != TTY_NULL) && (tp->t_session == sessp)) { 1870 session_unlock(sessp); 1871 1872 /* 1873 * We're going to SIGHUP the foreground process 1874 * group. It can't change from this point on 1875 * until the revoke is complete. 1876 * The process group changes under both the tty 1877 * lock and proc_list_lock but we need only one 1878 */ 1879 tty_lock(tp); 1880 ttysetpgrphup(tp); 1881 tty_unlock(tp); 1882 1883 tty_pgsignal(tp, SIGHUP, 1); 1884 1885 session_lock(sessp); 1886 tp = SESSION_TP(sessp); 1887 } 1888 cttyflag = sessp->s_flags & S_CTTYREF; 1889 sessp->s_flags &= ~S_CTTYREF; 1890 ttyvp = sessp->s_ttyvp; 1891 ttyvid = sessp->s_ttyvid; 1892 sessp->s_ttyvp = NULL; 1893 sessp->s_ttyvid = 0; 1894 sessp->s_ttyp = TTY_NULL; 1895 sessp->s_ttypgrpid = NO_PID; 1896 session_unlock(sessp); 1897 1898 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) { 1899 if (tp != TTY_NULL) { 1900 tty_lock(tp); 1901 (void) ttywait(tp); 1902 tty_unlock(tp); 1903 } 1904 context.vc_thread = proc_thread(p); /* XXX */ 1905 context.vc_ucred = kauth_cred_proc_ref(p); 1906 VNOP_REVOKE(ttyvp, REVOKEALL, &context); 1907 if (cttyflag) { 1908 /* 1909 * Release the extra usecount taken in cttyopen. 1910 * usecount should be released after VNOP_REVOKE is called. 1911 * This usecount was taken to ensure that 1912 * the VNOP_REVOKE results in a close to 1913 * the tty since cttyclose is a no-op. 1914 */ 1915 vnode_rele(ttyvp); 1916 } 1917 vnode_put(ttyvp); 1918 kauth_cred_unref(&context.vc_ucred); 1919 ttyvp = NULLVP; 1920 } 1921 if (tp) { 1922 /* 1923 * This is cleared even if not set. This is also done in 1924 * spec_close to ensure that the flag is cleared. 1925 */ 1926 tty_lock(tp); 1927 ttyclrpgrphup(tp); 1928 tty_unlock(tp); 1929 1930 ttyfree(tp); 1931 } 1932 } 1933 session_lock(sessp); 1934 sessp->s_leader = NULL; 1935 session_unlock(sessp); 1936 } 1937 session_rele(sessp); 1938 1939 pg = proc_pgrp(p); 1940 fixjobc(p, pg, 0); 1941 pg_rele(pg); 1942 1943 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 1944 1945 proc_list_lock(); 1946 proc_childdrainstart(p); 1947 while ((q = p->p_children.lh_first) != NULL) { 1948 if (q->p_stat == SZOMB) { 1949 if (p != q->p_pptr) 1950 panic("parent child linkage broken"); 1951 /* check for lookups by zomb sysctl */ 1952 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { 1953 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); 1954 } 1955 q->p_listflag |= P_LIST_WAITING; 1956 /* 1957 * This is a named reference and it is not granted 1958 * if the reap is already in progress. So we get 1959 * the reference here exclusively and their can be 1960 * no waiters. So there is no need for a wakeup 1961 * after we are done. AlsO the reap frees the structure 1962 * and the proc struct cannot be used for wakeups as well. 1963 * It is safe to use q here as this is system reap 1964 */ 1965 (void)reap_child_locked(p, q, 1, 0, 1, 0); 1966 } else { 1967 /* 1968 * Traced processes are killed 1969 * since their existence means someone is messing up. 1970 */ 1971 if (q->p_lflag & P_LTRACED) { 1972 struct proc *opp; 1973 1974 proc_list_unlock(); 1975 1976 opp = proc_find(q->p_oppid); 1977 if (opp != PROC_NULL) { 1978 proc_list_lock(); 1979 q->p_oppid = 0; 1980 proc_list_unlock(); 1981 proc_reparentlocked(q, opp, 0, 0); 1982 proc_rele(opp); 1983 } else { 1984 /* original parent exited while traced */ 1985 proc_list_lock(); 1986 q->p_listflag |= P_LIST_DEADPARENT; 1987 q->p_oppid = 0; 1988 proc_list_unlock(); 1989 proc_reparentlocked(q, initproc, 0, 0); 1990 } 1991 1992 proc_lock(q); 1993 q->p_lflag &= ~P_LTRACED; 1994 1995 if (q->sigwait_thread) { 1996 thread_t thread = q->sigwait_thread; 1997 1998 proc_unlock(q); 1999 /* 2000 * The sigwait_thread could be stopped at a 2001 * breakpoint. Wake it up to kill. 2002 * Need to do this as it could be a thread which is not 2003 * the first thread in the task. So any attempts to kill 2004 * the process would result into a deadlock on q->sigwait. 2005 */ 2006 thread_resume(thread); 2007 clear_wait(thread, THREAD_INTERRUPTED); 2008 threadsignal(thread, SIGKILL, 0); 2009 } else { 2010 proc_unlock(q); 2011 } 2012 2013 psignal(q, SIGKILL); 2014 proc_list_lock(); 2015 } else { 2016 q->p_listflag |= P_LIST_DEADPARENT; 2017 proc_reparentlocked(q, initproc, 0, 1); 2018 } 2019 } 2020 } 2021 2022 proc_childdrainend(p); 2023 proc_list_unlock(); 2024 2025 /* 2026 * Release reference to text vnode 2027 */ 2028 tvp = p->p_textvp; 2029 p->p_textvp = NULL; 2030 if (tvp != NULLVP) { 2031 vnode_rele(tvp); 2032 } 2033 2034 /* 2035 * Save exit status and final rusage info, adding in child rusage 2036 * info and self times. If we were unable to allocate a zombie 2037 * structure, this information is lost. 2038 */ 2039 if (rup != NULL) { 2040 rup->ru = p->p_stats->p_ru; 2041 timerclear(&rup->ru.ru_utime); 2042 timerclear(&rup->ru.ru_stime); 2043 2044#ifdef FIXME 2045 if (task) { 2046 mach_task_basic_info_data_t tinfo; 2047 task_thread_times_info_data_t ttimesinfo; 2048 int task_info_stuff, task_ttimes_stuff; 2049 struct timeval ut,st; 2050 2051 task_info_stuff = MACH_TASK_BASIC_INFO_COUNT; 2052 task_info(task, MACH_TASK_BASIC_INFO, 2053 &tinfo, &task_info_stuff); 2054 p->p_ru->ru.ru_utime.tv_sec = tinfo.user_time.seconds; 2055 p->p_ru->ru.ru_utime.tv_usec = tinfo.user_time.microseconds; 2056 p->p_ru->ru.ru_stime.tv_sec = tinfo.system_time.seconds; 2057 p->p_ru->ru.ru_stime.tv_usec = tinfo.system_time.microseconds; 2058 2059 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT; 2060 task_info(task, TASK_THREAD_TIMES_INFO, 2061 &ttimesinfo, &task_ttimes_stuff); 2062 2063 ut.tv_sec = ttimesinfo.user_time.seconds; 2064 ut.tv_usec = ttimesinfo.user_time.microseconds; 2065 st.tv_sec = ttimesinfo.system_time.seconds; 2066 st.tv_usec = ttimesinfo.system_time.microseconds; 2067 timeradd(&ut,&p->p_ru->ru.ru_utime,&p->p_ru->ru.ru_utime); 2068 timeradd(&st,&p->p_ru->ru.ru_stime,&p->p_ru->ru.ru_stime); 2069 } 2070#endif /* FIXME */ 2071 2072 ruadd(&rup->ru, &p->p_stats->p_cru); 2073 2074 gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT); 2075 rup->ri.ri_phys_footprint = 0; 2076 rup->ri.ri_proc_exit_abstime = mach_absolute_time(); 2077 2078 /* 2079 * Now that we have filled in the rusage info, make it 2080 * visible to an external observer via proc_pid_rusage(). 2081 */ 2082 p->p_ru = rup; 2083 } 2084 2085 /* 2086 * Free up profiling buffers. 2087 */ 2088 { 2089 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn; 2090 2091 p1 = p0->pr_next; 2092 p0->pr_next = NULL; 2093 p0->pr_scale = 0; 2094 2095 for (; p1 != NULL; p1 = pn) { 2096 pn = p1->pr_next; 2097 kfree(p1, sizeof *p1); 2098 } 2099 } 2100 2101#if PSYNCH 2102 pth_proc_hashdelete(p); 2103#endif /* PSYNCH */ 2104 2105 /* 2106 * Other substructures are freed from wait(). 2107 */ 2108 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS); 2109 p->p_stats = NULL; 2110 2111 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS); 2112 p->p_sigacts = NULL; 2113 2114 proc_limitdrop(p, 1); 2115 p->p_limit = NULL; 2116 2117 /* 2118 * Finish up by terminating the task 2119 * and halt this thread (only if a 2120 * member of the task exiting). 2121 */ 2122 p->task = TASK_NULL; 2123 2124 /* 2125 * Notify parent that we're gone. 2126 */ 2127 pp = proc_parent(p); 2128 if ((p->p_listflag & P_LIST_DEADPARENT) == 0) { 2129 if (pp != initproc) { 2130 proc_lock(pp); 2131 pp->si_pid = p->p_pid; 2132 pp->si_status = p->p_xstat; 2133 pp->si_code = CLD_EXITED; 2134 /* 2135 * p_ucred usage is safe as it is an exiting process 2136 * and reference is dropped in reap 2137 */ 2138 pp->si_uid = kauth_cred_getruid(p->p_ucred); 2139 proc_unlock(pp); 2140 } 2141 /* mark as a zombie */ 2142 /* mark as a zombie */ 2143 /* No need to take proc lock as all refs are drained and 2144 * no one except parent (reaping ) can look at this. 2145 * The write is to an int and is coherent. Also parent is 2146 * keyed off of list lock for reaping 2147 */ 2148 p->p_stat = SZOMB; 2149 2150 psignal(pp, SIGCHLD); 2151 2152 /* and now wakeup the parent */ 2153 proc_list_lock(); 2154 wakeup((caddr_t)pp); 2155 proc_list_unlock(); 2156 } else { 2157 proc_list_lock(); 2158 /* check for lookups by zomb sysctl */ 2159 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { 2160 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); 2161 } 2162 p->p_stat = SZOMB; 2163 p->p_listflag |= P_LIST_WAITING; 2164 2165 /* 2166 * This is a named reference and it is not granted 2167 * if the reap is already in progress. So we get 2168 * the reference here exclusively and their can be 2169 * no waiters. So there is no need for a wakeup 2170 * after we are done. AlsO the reap frees the structure 2171 * and the proc struct cannot be used for wakeups as well. 2172 * It is safe to use p here as this is system reap 2173 */ 2174 (void)reap_child_locked(pp, p, 0, 0, 1, 1); 2175 /* list lock dropped by reap_child_locked */ 2176 } 2177 proc_rele(pp); 2178} 2179 2180 2181/* 2182 * munge_rusage 2183 * LP64 support - long is 64 bits if we are dealing with a 64 bit user 2184 * process. We munge the kernel version of rusage into the 2185 * 64 bit version. 2186 */ 2187__private_extern__ void 2188munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p) 2189{ 2190 /* timeval changes size, so utime and stime need special handling */ 2191 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec; 2192 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec; 2193 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec; 2194 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec; 2195 /* 2196 * everything else can be a direct assign, since there is no loss 2197 * of precision implied boing 32->64. 2198 */ 2199 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss; 2200 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss; 2201 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss; 2202 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss; 2203 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt; 2204 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt; 2205 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap; 2206 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock; 2207 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock; 2208 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd; 2209 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv; 2210 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals; 2211 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw; 2212 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw; 2213} 2214 2215/* For a 64-bit kernel and 32-bit userspace, munging may be needed */ 2216__private_extern__ void 2217munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p) 2218{ 2219 /* timeval changes size, so utime and stime need special handling */ 2220 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec; 2221 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec; 2222 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec; 2223 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec; 2224 /* 2225 * everything else can be a direct assign. We currently ignore 2226 * the loss of precision 2227 */ 2228 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss; 2229 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss; 2230 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss; 2231 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss; 2232 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt; 2233 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt; 2234 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap; 2235 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock; 2236 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock; 2237 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd; 2238 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv; 2239 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals; 2240 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw; 2241 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw; 2242} 2243