]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_exit.c
6d04dd6a9d576e2bcb685a7c26df64fdefbc8c60
[apple/xnu.git] / bsd / kern / kern_exit.c
1 /*
2 * Copyright (c) 2000-2007 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 CONFIG_DTRACE
123 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
124 extern void (*dtrace_fasttrap_exit_ptr)(proc_t);
125 extern void (*dtrace_helpers_cleanup)(proc_t);
126 extern void dtrace_lazy_dofs_destroy(proc_t);
127
128 #include <sys/dtrace_ptss.h>
129 #endif
130
131 #if CONFIG_MACF
132 #include <security/mac.h>
133 #include <sys/syscall.h>
134 #endif
135
136 #include <mach/mach_types.h>
137 #include <mach/task.h>
138 #include <mach/thread_act.h>
139
140 #include <sys/sdt.h>
141
142 extern char init_task_failure_data[];
143 void proc_prepareexit(proc_t p, int rv);
144 void vfork_exit(proc_t p, int rv);
145 void vproc_exit(proc_t p);
146 __private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
147 __private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
148 static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int locked, int droplock);
149
150 /*
151 * Things which should have prototypes in headers, but don't
152 */
153 void *get_bsduthreadarg(thread_t);
154 void proc_exit(proc_t p);
155 int wait1continue(int result);
156 int waitidcontinue(int result);
157 int *get_bsduthreadrval(thread_t);
158 kern_return_t sys_perf_notify(thread_t thread, int pid);
159 kern_return_t abnormal_exit_notify(mach_exception_data_type_t code,
160 mach_exception_data_type_t subcode);
161 void delay(int);
162
163 /*
164 * NOTE: Source and target may *NOT* overlap!
165 * XXX Should share code with bsd/dev/ppc/unix_signal.c
166 */
167 static void
168 siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
169 {
170 out->si_signo = in->si_signo;
171 out->si_errno = in->si_errno;
172 out->si_code = in->si_code;
173 out->si_pid = in->si_pid;
174 out->si_uid = in->si_uid;
175 out->si_status = in->si_status;
176 out->si_addr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_addr);
177 /* following cast works for sival_int because of padding */
178 out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_value.sival_ptr);
179 out->si_band = in->si_band; /* range reduction */
180 }
181
182 static void
183 siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
184 {
185 out->si_signo = in->si_signo;
186 out->si_errno = in->si_errno;
187 out->si_code = in->si_code;
188 out->si_pid = in->si_pid;
189 out->si_uid = in->si_uid;
190 out->si_status = in->si_status;
191 out->si_addr = in->si_addr;
192 /* following cast works for sival_int because of padding */
193 out->si_value.sival_ptr = in->si_value.sival_ptr;
194 out->si_band = in->si_band; /* range reduction */
195 }
196
197 /*
198 * exit --
199 * Death of process.
200 */
201 void
202 exit(proc_t p, struct exit_args *uap, int *retval)
203 {
204 exit1(p, W_EXITCODE(uap->rval, 0), retval);
205
206 /* drop funnel before we return */
207 thread_exception_return();
208 /* NOTREACHED */
209 while (TRUE)
210 thread_block(THREAD_CONTINUE_NULL);
211 /* NOTREACHED */
212 }
213
214 /*
215 * Exit: deallocate address space and other resources, change proc state
216 * to zombie, and unlink proc from allproc and parent's lists. Save exit
217 * status and rusage for wait(). Check for child processes and orphan them.
218 */
219 int
220 exit1(proc_t p, int rv, int *retval)
221 {
222 thread_t self = current_thread();
223 struct task *task = p->task;
224 struct uthread *ut;
225
226 /*
227 * If a thread in this task has already
228 * called exit(), then halt any others
229 * right here.
230 */
231
232 ut = get_bsdthread_info(self);
233 if (ut->uu_flag & UT_VFORK) {
234 vfork_exit(p, rv);
235 vfork_return(p , retval, p->p_pid);
236 unix_syscall_return(0);
237 /* NOT REACHED */
238 }
239
240 /*
241 * The parameter list of audit_syscall_exit() was augmented to
242 * take the Darwin syscall number as the first parameter,
243 * which is currently required by mac_audit_postselect().
244 */
245
246 /*
247 * The BSM token contains two components: an exit status as passed
248 * to exit(), and a return value to indicate what sort of exit it
249 * was. The exit status is WEXITSTATUS(rv), but it's not clear
250 * what the return value is.
251 */
252 AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
253 AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
254
255 DTRACE_PROC1(exit, int, CLD_EXITED);
256
257 proc_lock(p);
258 proc_transstart(p, 1);
259 while (p->exit_thread != self) {
260 if (sig_try_locked(p) <= 0) {
261 proc_transend(p, 1);
262 if (get_threadtask(self) != task) {
263 proc_unlock(p);
264 return(0);
265 }
266 proc_unlock(p);
267 thread_terminate(self);
268 thread_exception_return();
269 /* NOTREACHED */
270 }
271 sig_lock_to_exit(p);
272 }
273 if (p == initproc) {
274 proc_unlock(p);
275 printf("pid 1 exited (signal %d, exit %d)",
276 WTERMSIG(rv), WEXITSTATUS(rv));
277 panic("%s died\nState at Last Exception:\n\n%s",
278 (p->p_comm[0] != '\0' ?
279 p->p_comm :
280 "launchd"),
281 init_task_failure_data);
282 }
283
284 p->p_lflag |= P_LEXIT;
285 p->p_xstat = rv;
286
287 proc_transend(p, 1);
288 proc_unlock(p);
289
290 proc_prepareexit(p, rv);
291
292 /* Last thread to terminate will call proc_exit() */
293 task_terminate_internal(task);
294
295 return(0);
296 }
297
298 void
299 proc_prepareexit(proc_t p, int rv)
300 {
301 mach_exception_data_type_t code, subcode;
302 struct uthread *ut;
303 thread_t self = current_thread();
304 ut = get_bsdthread_info(self);
305
306 /* If a core should be generated, notify crash reporter */
307 if (hassigprop(WTERMSIG(rv), SA_CORE) || ((p->p_csflags & CS_KILLED) != 0)) {
308 /*
309 * Workaround for processes checking up on PT_DENY_ATTACH:
310 * should be backed out post-Leopard (details in 5431025).
311 */
312 if ((SIGSEGV == WTERMSIG(rv)) &&
313 (p->p_pptr->p_lflag & P_LNOATTACH)) {
314 goto skipcheck;
315 }
316
317 /*
318 * Crash Reporter looks for the signal value, original exception
319 * type, and low 20 bits of the original code in code[0]
320 * (8, 4, and 20 bits respectively). code[1] is unmodified.
321 */
322 code = ((WTERMSIG(rv) & 0xff) << 24) |
323 ((ut->uu_exception & 0x0f) << 20) |
324 ((int)ut->uu_code & 0xfffff);
325 subcode = ut->uu_subcode;
326 (void) abnormal_exit_notify(code, subcode);
327 }
328
329 skipcheck:
330 /* Notify the perf server */
331 (void)sys_perf_notify(self, p->p_pid);
332
333 /*
334 * Remove proc from allproc queue and from pidhash chain.
335 * Need to do this before we do anything that can block.
336 * Not doing causes things like mount() find this on allproc
337 * in partially cleaned state.
338 */
339
340 proc_list_lock();
341
342 LIST_REMOVE(p, p_list);
343 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
344 /* will not be visible via proc_find */
345 p->p_listflag |= P_LIST_EXITED;
346
347 proc_list_unlock();
348
349
350 #ifdef PGINPROF
351 vmsizmon();
352 #endif
353 /*
354 * If parent is waiting for us to exit or exec,
355 * P_LPPWAIT is set; we will wakeup the parent below.
356 */
357 proc_lock(p);
358 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
359 p->p_sigignore = ~(sigcantmask);
360 ut->uu_siglist = 0;
361 proc_unlock(p);
362 }
363
364 void
365 proc_exit(proc_t p)
366 {
367 proc_t q;
368 proc_t pp;
369 struct task *task = p->task;
370 vnode_t tvp = NULLVP;
371 struct pgrp * pg;
372 struct session *sessp;
373 struct uthread * uth;
374 pid_t pid;
375 int exitval;
376
377 uth = (struct uthread *)get_bsdthread_info(current_thread());
378
379 proc_lock(p);
380 proc_transstart(p, 1);
381 if( !(p->p_lflag & P_LEXIT)) {
382 /*
383 * This can happen if a thread_terminate() occurs
384 * in a single-threaded process.
385 */
386 p->p_lflag |= P_LEXIT;
387 proc_transend(p, 1);
388 proc_unlock(p);
389 proc_prepareexit(p, 0);
390 (void) task_terminate_internal(task);
391 proc_lock(p);
392 } else {
393 proc_transend(p, 1);
394 }
395
396 p->p_lflag |= P_LPEXIT;
397
398 /*
399 * Other kernel threads may be in the middle of signalling this process.
400 * Wait for those threads to wrap it up before making the process
401 * disappear on them.
402 */
403 if ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 0)) {
404 p->p_sigwaitcnt++;
405 while ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 1))
406 msleep(&p->p_sigmask, &p->p_mlock, PWAIT, "proc_sigdrain", NULL);
407 p->p_sigwaitcnt--;
408 }
409
410 proc_unlock(p);
411 pid = p->p_pid;
412 exitval = p->p_xstat;
413 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START,
414 pid, exitval, 0, 0, 0);
415
416 #if CONFIG_DTRACE
417 /*
418 * Free any outstanding lazy dof entries. It is imperative we
419 * always call dtrace_lazy_dofs_destroy, rather than null check
420 * and call if !NULL. If we NULL test, during lazy dof faulting
421 * we can race with the faulting code and proceed from here to
422 * beyond the helpers cleanup. The lazy dof faulting will then
423 * install new helpers which will never be cleaned up, and leak.
424 */
425 dtrace_lazy_dofs_destroy(p);
426
427 /*
428 * Clean up any DTrace helper actions or probes for the process.
429 */
430 if (p->p_dtrace_helpers != NULL) {
431 (*dtrace_helpers_cleanup)(p);
432 }
433
434 /*
435 * Clean up any DTrace probes associated with this process.
436 */
437 /*
438 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
439 * call this after dtrace_helpers_cleanup()
440 */
441 proc_lock(p);
442 if (p->p_dtrace_probes && dtrace_fasttrap_exit_ptr) {
443 (*dtrace_fasttrap_exit_ptr)(p);
444 }
445 proc_unlock(p);
446 #endif
447
448 /* XXX Zombie allocation may fail, in which case stats get lost */
449 MALLOC_ZONE(p->p_ru, struct rusage *,
450 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
451
452 nspace_proc_exit(p);
453
454 /*
455 * need to cancel async IO requests that can be cancelled and wait for those
456 * already active. MAY BLOCK!
457 */
458
459 proc_refdrain(p);
460
461 workqueue_mark_exiting(p);
462 workqueue_exit(p);
463
464 _aio_exit( p );
465
466 /*
467 * Close open files and release open-file table.
468 * This may block!
469 */
470 fdfree(p);
471
472 if (uth->uu_lowpri_window) {
473 /*
474 * task is marked as a low priority I/O type
475 * and the I/O we issued while in flushing files on close
476 * collided with normal I/O operations...
477 * no need to throttle this thread since its going away
478 * but we do need to update our bookeeping w/r to throttled threads
479 */
480 throttle_lowpri_io(FALSE);
481 }
482
483 #if SYSV_SHM
484 /* Close ref SYSV Shared memory*/
485 if (p->vm_shm)
486 shmexit(p);
487 #endif
488 #if SYSV_SEM
489 /* Release SYSV semaphores */
490 semexit(p);
491 #endif
492
493 #if PSYNCH
494 pth_proc_hashdelete(p);
495 #endif /* PSYNCH */
496
497 sessp = proc_session(p);
498 if (SESS_LEADER(p, sessp)) {
499
500 if (sessp->s_ttyvp != NULLVP) {
501 struct vnode *ttyvp;
502 int ttyvid;
503 struct vfs_context context;
504 struct tty * tp;
505
506
507 /*
508 * Controlling process.
509 * Signal foreground pgrp,
510 * drain controlling terminal
511 * and revoke access to controlling terminal.
512 */
513 tp = SESSION_TP(sessp);
514
515 if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
516 tty_pgsignal(tp, SIGHUP, 1);
517
518 session_lock(sessp);
519 /* reget potentially tp due to revocation */
520 tp = SESSION_TP(sessp);
521 ttyvp = sessp->s_ttyvp;
522 ttyvid = sessp->s_ttyvid;
523 sessp->s_ttyvp = NULLVP;
524 sessp->s_ttyvid = 0;
525 sessp->s_ttyp = TTY_NULL;
526 sessp->s_ttypgrpid = NO_PID;
527 session_unlock(sessp);
528
529 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
530
531 if (tp != TTY_NULL) {
532 tty_lock(tp);
533 (void) ttywait(tp);
534 tty_unlock(tp);
535 }
536 context.vc_thread = proc_thread(p); /* XXX */
537 context.vc_ucred = kauth_cred_proc_ref(p);
538 VNOP_REVOKE(ttyvp, REVOKEALL, &context);
539 vnode_put(ttyvp);
540 kauth_cred_unref(&context.vc_ucred);
541 }
542 } else {
543 session_lock(sessp);
544 /* reget potentially tp due to revocation */
545 tp = SESSION_TP(sessp);
546 ttyvp = sessp->s_ttyvp;
547 sessp->s_ttyvp = NULLVP;
548 sessp->s_ttyvid = 0;
549 sessp->s_ttyp = TTY_NULL;
550 sessp->s_ttypgrpid = NO_PID;
551 session_unlock(sessp);
552 }
553 if (ttyvp)
554 vnode_rele(ttyvp);
555 /*
556 * s_ttyp is not zero'd; we use this to indicate
557 * that the session once had a controlling terminal.
558 * (for logging and informational purposes)
559 */
560 }
561
562 session_lock(sessp);
563 sessp->s_leader = NULL;
564 session_unlock(sessp);
565 }
566 session_rele(sessp);
567
568 pg = proc_pgrp(p);
569 fixjobc(p, pg, 0);
570 pg_rele(pg);
571
572 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
573 (void)acct_process(p);
574
575 proc_list_lock();
576
577 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
578 p->p_listflag &= ~P_LIST_EXITCOUNT;
579 proc_shutdown_exitcount--;
580 if (proc_shutdown_exitcount == 0)
581 wakeup(&proc_shutdown_exitcount);
582 }
583
584 /* wait till parentrefs are dropped and grant no more */
585 proc_childdrainstart(p);
586 while ((q = p->p_children.lh_first) != NULL) {
587 q->p_listflag |= P_LIST_DEADPARENT;
588 if (q->p_stat == SZOMB) {
589 if (p != q->p_pptr)
590 panic("parent child linkage broken");
591 /* check for sysctl zomb lookup */
592 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
593 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
594 }
595 q->p_listflag |= P_LIST_WAITING;
596 /*
597 * This is a named reference and it is not granted
598 * if the reap is already in progress. So we get
599 * the reference here exclusively and their can be
600 * no waiters. So there is no need for a wakeup
601 * after we are done. Also the reap frees the structure
602 * and the proc struct cannot be used for wakeups as well.
603 * It is safe to use q here as this is system reap
604 */
605 (void)reap_child_locked(p, q, 1, 1, 0);
606 } else {
607 proc_reparentlocked(q, initproc, 0, 1);
608 /*
609 * Traced processes are killed
610 * since their existence means someone is messing up.
611 */
612 if (q->p_lflag & P_LTRACED) {
613 /*
614 * Take a reference on the child process to
615 * ensure it doesn't exit and disappear between
616 * the time we drop the list_lock and attempt
617 * to acquire its proc_lock.
618 */
619 if (proc_ref_locked(q) != q)
620 continue;
621
622 proc_list_unlock();
623 proc_lock(q);
624 q->p_lflag &= ~P_LTRACED;
625 if (q->sigwait_thread) {
626 thread_t thread = q->sigwait_thread;
627
628 proc_unlock(q);
629 /*
630 * The sigwait_thread could be stopped at a
631 * breakpoint. Wake it up to kill.
632 * Need to do this as it could be a thread which is not
633 * the first thread in the task. So any attempts to kill
634 * the process would result into a deadlock on q->sigwait.
635 */
636 thread_resume(thread);
637 clear_wait(thread, THREAD_INTERRUPTED);
638 threadsignal(thread, SIGKILL, 0);
639 } else {
640 proc_unlock(q);
641 }
642
643 psignal(q, SIGKILL);
644 proc_list_lock();
645 proc_rele_locked(q);
646 }
647 }
648 }
649
650 proc_childdrainend(p);
651 proc_list_unlock();
652
653 /*
654 * Release reference to text vnode
655 */
656 tvp = p->p_textvp;
657 p->p_textvp = NULL;
658 if (tvp != NULLVP) {
659 vnode_rele(tvp);
660 }
661
662 /*
663 * Save exit status and final rusage info, adding in child rusage
664 * info and self times. If we were unable to allocate a zombie
665 * structure, this information is lost.
666 */
667 /* No need for locking here as no one than this thread can access this */
668 if (p->p_ru != NULL) {
669 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, NULL);
670 *p->p_ru = p->p_stats->p_ru;
671
672 ruadd(p->p_ru, &p->p_stats->p_cru);
673 }
674
675 /*
676 * Free up profiling buffers.
677 */
678 {
679 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
680
681 p1 = p0->pr_next;
682 p0->pr_next = NULL;
683 p0->pr_scale = 0;
684
685 for (; p1 != NULL; p1 = pn) {
686 pn = p1->pr_next;
687 kfree(p1, sizeof *p1);
688 }
689 }
690
691 proc_spinlock(p);
692 if (thread_call_cancel(p->p_rcall))
693 p->p_ractive--;
694
695 while (p->p_ractive > 0) {
696 proc_spinunlock(p);
697
698 delay(1);
699
700 proc_spinlock(p);
701 }
702 proc_spinunlock(p);
703
704 thread_call_free(p->p_rcall);
705 p->p_rcall = NULL;
706
707 /*
708 * Other substructures are freed from wait().
709 */
710 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
711 p->p_stats = NULL;
712
713 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
714 p->p_sigacts = NULL;
715
716 proc_limitdrop(p, 1);
717 p->p_limit = NULL;
718
719
720 /*
721 * Finish up by terminating the task
722 * and halt this thread (only if a
723 * member of the task exiting).
724 */
725 p->task = TASK_NULL;
726 set_bsdtask_info(task, NULL);
727
728 /* exit status will be seen by parent process */
729 proc_knote(p, NOTE_EXIT | (p->p_xstat & 0xffff));
730
731 /* mark the thread as the one that is doing proc_exit
732 * no need to hold proc lock in uthread_free
733 */
734 uth->uu_flag |= UT_PROCEXIT;
735 /*
736 * Notify parent that we're gone.
737 */
738 pp = proc_parent(p);
739 if (pp->p_flag & P_NOCLDWAIT) {
740
741 #if 3839178
742 /*
743 * If the parent is ignoring SIGCHLD, then POSIX requires
744 * us to not add the resource usage to the parent process -
745 * we are only going to hand it off to init to get reaped.
746 * We should contest the standard in this case on the basis
747 * of RLIMIT_CPU.
748 */
749 #else /* !3839178 */
750 /*
751 * Add child resource usage to parent before giving
752 * zombie to init. If we were unable to allocate a
753 * zombie structure, this information is lost.
754 */
755 if (p->p_ru != NULL) {
756 proc_lock(pp);
757 ruadd(&pp->p_stats->p_cru, p->p_ru);
758 proc_unlock(pp);
759 }
760 #endif /* !3839178 */
761
762 /* kernel can reap this one, no need to move it to launchd */
763 proc_list_lock();
764 p->p_listflag |= P_LIST_DEADPARENT;
765 proc_list_unlock();
766 }
767 if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
768 if (pp != initproc) {
769 proc_lock(pp);
770 pp->si_pid = p->p_pid;
771 pp->si_status = p->p_xstat;
772 pp->si_code = CLD_EXITED;
773 /*
774 * p_ucred usage is safe as it is an exiting process
775 * and reference is dropped in reap
776 */
777 pp->si_uid = kauth_cred_getruid(p->p_ucred);
778 proc_unlock(pp);
779 }
780 /* mark as a zombie */
781 /* No need to take proc lock as all refs are drained and
782 * no one except parent (reaping ) can look at this.
783 * The write is to an int and is coherent. Also parent is
784 * keyed off of list lock for reaping
785 */
786 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
787 pid, exitval, 0, 0, 0);
788 p->p_stat = SZOMB;
789 /*
790 * The current process can be reaped so, no one
791 * can depend on this
792 */
793
794 psignal(pp, SIGCHLD);
795
796 /* and now wakeup the parent */
797 proc_list_lock();
798 wakeup((caddr_t)pp);
799 proc_list_unlock();
800 } else {
801 /* should be fine as parent proc would be initproc */
802 /* mark as a zombie */
803 /* No need to take proc lock as all refs are drained and
804 * no one except parent (reaping ) can look at this.
805 * The write is to an int and is coherent. Also parent is
806 * keyed off of list lock for reaping
807 */
808 proc_list_lock();
809 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
810 pid, exitval, 0, 0, 0);
811 /* check for sysctl zomb lookup */
812 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
813 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
814 }
815 /* safe to use p as this is a system reap */
816 p->p_stat = SZOMB;
817 p->p_listflag |= P_LIST_WAITING;
818
819 /*
820 * This is a named reference and it is not granted
821 * if the reap is already in progress. So we get
822 * the reference here exclusively and their can be
823 * no waiters. So there is no need for a wakeup
824 * after we are done. AlsO the reap frees the structure
825 * and the proc struct cannot be used for wakeups as well.
826 * It is safe to use p here as this is system reap
827 */
828 (void)reap_child_locked(pp, p, 1, 1, 1);
829 /* list lock dropped by reap_child_locked */
830 }
831 if (uth->uu_lowpri_window) {
832 /*
833 * task is marked as a low priority I/O type and we've
834 * somehow picked up another throttle during exit processing...
835 * no need to throttle this thread since its going away
836 * but we do need to update our bookeeping w/r to throttled threads
837 */
838 throttle_lowpri_io(FALSE);
839 }
840
841 proc_rele(pp);
842
843 }
844
845
846 /*
847 * reap_child_locked
848 *
849 * Description: Given a process from which all status information needed
850 * has already been extracted, if the process is a ptrace
851 * attach process, detach it and give it back to its real
852 * parent, else recover all resources remaining associated
853 * with it.
854 *
855 * Parameters: proc_t parent Parent of process being reaped
856 * proc_t child Process to reap
857 *
858 * Returns: 0 Process was not reaped because it
859 * came from an attach
860 * 1 Process was reaped
861 */
862 static int
863 reap_child_locked(proc_t parent, proc_t child, int deadparent, int locked, int droplock)
864 {
865 proc_t trace_parent = PROC_NULL; /* Traced parent process, if tracing */
866
867 if (locked == 1)
868 proc_list_unlock();
869
870 /*
871 * If we got the child via a ptrace 'attach',
872 * we need to give it back to the old parent.
873 *
874 * Exception: someone who has been reparented to launchd before being
875 * ptraced can simply be reaped, refer to radar 5677288
876 * p_oppid -> ptraced
877 * trace_parent == initproc -> away from launchd
878 * P_LIST_DEADPARENT -> came to launchd by reparenting
879 */
880 if (child->p_oppid && (trace_parent = proc_find(child->p_oppid))
881 && !((trace_parent == initproc) && (child->p_lflag & P_LIST_DEADPARENT))) {
882 proc_lock(child);
883 child->p_oppid = 0;
884 proc_unlock(child);
885 if (trace_parent != initproc) {
886 /*
887 * proc internal fileds and p_ucred usage safe
888 * here as child is dead and is not reaped or
889 * reparented yet
890 */
891 proc_lock(trace_parent);
892 trace_parent->si_pid = child->p_pid;
893 trace_parent->si_status = child->p_xstat;
894 trace_parent->si_code = CLD_CONTINUED;
895 trace_parent->si_uid = kauth_cred_getruid(child->p_ucred);
896 proc_unlock(trace_parent);
897 }
898 proc_reparentlocked(child, trace_parent, 1, 0);
899 psignal(trace_parent, SIGCHLD);
900 proc_list_lock();
901 wakeup((caddr_t)trace_parent);
902 child->p_listflag &= ~P_LIST_WAITING;
903 wakeup(&child->p_stat);
904 proc_list_unlock();
905 proc_rele(trace_parent);
906 if ((locked == 1) && (droplock == 0))
907 proc_list_lock();
908 return (0);
909 }
910
911 if (trace_parent != PROC_NULL) {
912 proc_rele(trace_parent);
913 }
914
915 proc_knote(child, NOTE_REAP);
916 proc_knote_drain(child);
917
918 child->p_xstat = 0;
919 if (child->p_ru) {
920 proc_lock(parent);
921 #if 3839178
922 /*
923 * If the parent is ignoring SIGCHLD, then POSIX requires
924 * us to not add the resource usage to the parent process -
925 * we are only going to hand it off to init to get reaped.
926 * We should contest the standard in this case on the basis
927 * of RLIMIT_CPU.
928 */
929 if (!(parent->p_flag & P_NOCLDWAIT))
930 #endif /* 3839178 */
931 ruadd(&parent->p_stats->p_cru, child->p_ru);
932 proc_unlock(parent);
933 FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE);
934 child->p_ru = NULL;
935 } else {
936 printf("Warning : lost p_ru for %s\n", child->p_comm);
937 }
938
939 AUDIT_SESSION_PROCEXIT(child);
940
941 /*
942 * Decrement the count of procs running with this uid.
943 * p_ucred usage is safe here as it is an exited process.
944 * and refernce is dropped after these calls down below
945 * (locking protection is provided by list lock held in chgproccnt)
946 */
947 (void)chgproccnt(kauth_cred_getruid(child->p_ucred), -1);
948
949 #if CONFIG_LCTX
950 ALLLCTX_LOCK;
951 leavelctx(child);
952 ALLLCTX_UNLOCK;
953 #endif
954
955 /*
956 * Free up credentials.
957 */
958 if (IS_VALID_CRED(child->p_ucred)) {
959 kauth_cred_unref(&child->p_ucred);
960 }
961
962 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
963
964 /*
965 * Finally finished with old proc entry.
966 * Unlink it from its process group and free it.
967 */
968 leavepgrp(child);
969
970 proc_list_lock();
971 LIST_REMOVE(child, p_list); /* off zombproc */
972 parent->p_childrencnt--;
973 LIST_REMOVE(child, p_sibling);
974 /* If there are no more children wakeup parent */
975 if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children)))
976 wakeup((caddr_t)parent); /* with list lock held */
977 child->p_listflag &= ~P_LIST_WAITING;
978 wakeup(&child->p_stat);
979
980 /* Take it out of process hash */
981 LIST_REMOVE(child, p_hash);
982 child->p_listflag &= ~P_LIST_INHASH;
983 proc_checkdeadrefs(child);
984 nprocs--;
985
986 proc_list_unlock();
987
988 #if CONFIG_FINE_LOCK_GROUPS
989 lck_mtx_destroy(&child->p_mlock, proc_mlock_grp);
990 lck_mtx_destroy(&child->p_fdmlock, proc_fdmlock_grp);
991 #if CONFIG_DTRACE
992 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
993 #endif
994 lck_spin_destroy(&child->p_slock, proc_slock_grp);
995 #else /* CONFIG_FINE_LOCK_GROUPS */
996 lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
997 lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
998 #if CONFIG_DTRACE
999 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
1000 #endif
1001 lck_spin_destroy(&child->p_slock, proc_lck_grp);
1002 #endif /* CONFIG_FINE_LOCK_GROUPS */
1003 workqueue_destroy_lock(child);
1004
1005 FREE_ZONE(child, sizeof *child, M_PROC);
1006 if ((locked == 1) && (droplock == 0))
1007 proc_list_lock();
1008
1009 return (1);
1010 }
1011
1012
1013 int
1014 wait1continue(int result)
1015 {
1016 void *vt;
1017 thread_t thread;
1018 int *retval;
1019 proc_t p;
1020
1021 if (result)
1022 return(result);
1023
1024 p = current_proc();
1025 thread = current_thread();
1026 vt = get_bsduthreadarg(thread);
1027 retval = get_bsduthreadrval(thread);
1028 return(wait4(p, (struct wait4_args *)vt, retval));
1029 }
1030
1031 int
1032 wait4(proc_t q, struct wait4_args *uap, int32_t *retval)
1033 {
1034 __pthread_testcancel(1);
1035 return(wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval));
1036 }
1037
1038 int
1039 wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval)
1040 {
1041 int nfound;
1042 int sibling_count;
1043 proc_t p;
1044 int status, error;
1045
1046 AUDIT_ARG(pid, uap->pid);
1047
1048 if (uap->pid == 0)
1049 uap->pid = -q->p_pgrpid;
1050
1051 loop:
1052 proc_list_lock();
1053 loop1:
1054 nfound = 0;
1055 sibling_count = 0;
1056
1057 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
1058 if ( p->p_sibling.le_next != 0 )
1059 sibling_count++;
1060 if (uap->pid != WAIT_ANY &&
1061 p->p_pid != uap->pid &&
1062 p->p_pgrpid != -(uap->pid))
1063 continue;
1064
1065 nfound++;
1066
1067 /* XXX This is racy because we don't get the lock!!!! */
1068
1069 if (p->p_listflag & P_LIST_WAITING) {
1070 (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1071 goto loop1;
1072 }
1073 p->p_listflag |= P_LIST_WAITING; /* only allow single thread to wait() */
1074
1075
1076 if (p->p_stat == SZOMB) {
1077 proc_list_unlock();
1078 #if CONFIG_MACF
1079 if ((error = mac_proc_check_wait(q, p)) != 0)
1080 goto out;
1081 #endif
1082 retval[0] = p->p_pid;
1083 if (uap->status) {
1084 /* Legacy apps expect only 8 bits of status */
1085 status = 0xffff & p->p_xstat; /* convert to int */
1086 error = copyout((caddr_t)&status,
1087 uap->status,
1088 sizeof(status));
1089 if (error)
1090 goto out;
1091 }
1092 if (uap->rusage) {
1093 if (p->p_ru == NULL) {
1094 error = ENOMEM;
1095 } else {
1096 if (IS_64BIT_PROCESS(q)) {
1097 struct user64_rusage my_rusage;
1098 munge_user64_rusage(p->p_ru, &my_rusage);
1099 error = copyout((caddr_t)&my_rusage,
1100 uap->rusage,
1101 sizeof (my_rusage));
1102 }
1103 else {
1104 struct user32_rusage my_rusage;
1105 munge_user32_rusage(p->p_ru, &my_rusage);
1106 error = copyout((caddr_t)&my_rusage,
1107 uap->rusage,
1108 sizeof (my_rusage));
1109 }
1110 }
1111 /* information unavailable? */
1112 if (error)
1113 goto out;
1114 }
1115
1116 /* Conformance change for 6577252.
1117 * When SIGCHLD is blocked and wait() returns because the status
1118 * of a child process is available and there are no other
1119 * children processes, then any pending SIGCHLD signal is cleared.
1120 */
1121 if ( sibling_count == 0 ) {
1122 int mask = sigmask(SIGCHLD);
1123 uthread_t uth = (struct uthread *)get_bsdthread_info(current_thread());
1124
1125 if ( (uth->uu_sigmask & mask) != 0 ) {
1126 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1127 * This locking looks funny but it is protecting access to the
1128 * thread via p_uthlist.
1129 */
1130 proc_lock(q);
1131 uth->uu_siglist &= ~mask; /* clear pending signal */
1132 proc_unlock(q);
1133 }
1134 }
1135
1136 /* Clean up */
1137 (void)reap_child_locked(q, p, 0, 0, 0);
1138
1139 return (0);
1140 }
1141 if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 &&
1142 (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) {
1143 proc_list_unlock();
1144 #if CONFIG_MACF
1145 if ((error = mac_proc_check_wait(q, p)) != 0)
1146 goto out;
1147 #endif
1148 proc_lock(p);
1149 p->p_lflag |= P_LWAITED;
1150 proc_unlock(p);
1151 retval[0] = p->p_pid;
1152 if (uap->status) {
1153 status = W_STOPCODE(p->p_xstat);
1154 error = copyout((caddr_t)&status,
1155 uap->status,
1156 sizeof(status));
1157 } else
1158 error = 0;
1159 goto out;
1160 }
1161 /*
1162 * If we are waiting for continued processses, and this
1163 * process was continued
1164 */
1165 if ((uap->options & WCONTINUED) &&
1166 (p->p_flag & P_CONTINUED)) {
1167 proc_list_unlock();
1168 #if CONFIG_MACF
1169 if ((error = mac_proc_check_wait(q, p)) != 0)
1170 goto out;
1171 #endif
1172
1173 /* Prevent other process for waiting for this event */
1174 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
1175 retval[0] = p->p_pid;
1176 if (uap->status) {
1177 status = W_STOPCODE(SIGCONT);
1178 error = copyout((caddr_t)&status,
1179 uap->status,
1180 sizeof(status));
1181 } else
1182 error = 0;
1183 goto out;
1184 }
1185 p->p_listflag &= ~P_LIST_WAITING;
1186 wakeup(&p->p_stat);
1187 }
1188 /* list lock is held when we get here any which way */
1189 if (nfound == 0) {
1190 proc_list_unlock();
1191 return (ECHILD);
1192 }
1193
1194 if (uap->options & WNOHANG) {
1195 retval[0] = 0;
1196 proc_list_unlock();
1197 return (0);
1198 }
1199
1200 if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue)))
1201 return (error);
1202
1203 goto loop;
1204 out:
1205 proc_list_lock();
1206 p->p_listflag &= ~P_LIST_WAITING;
1207 wakeup(&p->p_stat);
1208 proc_list_unlock();
1209 return (error);
1210 }
1211
1212
1213 int
1214 waitidcontinue(int result)
1215 {
1216 void *vt;
1217 thread_t thread;
1218 int *retval;
1219
1220 if (result)
1221 return(result);
1222
1223 thread = current_thread();
1224 vt = get_bsduthreadarg(thread);
1225 retval = get_bsduthreadrval(thread);
1226 return(waitid(current_proc(), (struct waitid_args *)vt, retval));
1227 }
1228
1229 /*
1230 * Description: Suspend the calling thread until one child of the process
1231 * containing the calling thread changes state.
1232 *
1233 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1234 * uap->id pid_t or gid_t or ignored
1235 * uap->infop Address of signinfo_t struct in
1236 * user space into which to return status
1237 * uap->options flag values
1238 *
1239 * Returns: 0 Success
1240 * !0 Error returning status to user space
1241 */
1242 int
1243 waitid(proc_t q, struct waitid_args *uap, int32_t *retval)
1244 {
1245 __pthread_testcancel(1);
1246 return(waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval));
1247 }
1248
1249 int
1250 waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap, __unused int32_t *retval)
1251 {
1252 user_siginfo_t collect64; /* siginfo data to return to caller */
1253
1254 int nfound;
1255 proc_t p;
1256 int error;
1257
1258 /*
1259 * Forced validation of options for T.waitpid 21; should be a TSD!
1260 * This will pass the test, but note that we have more bits than the
1261 * standard specifies that we will allow in, in this case. The test
1262 * passes because they light all the bits, not just the ones we allow,
1263 * and so the following check returns EINVAL like the test wants.
1264 */
1265 if (((uap->options & (WNOHANG|WNOWAIT|WCONTINUED|WUNTRACED|WSTOPPED|WEXITED)) != uap->options) ||
1266 (uap->options == 0))
1267 return (EINVAL); /* bits set that aren't recognized */
1268
1269 /*
1270 * Overly critical options checking, per POSIX
1271 */
1272 switch(uap->idtype) {
1273 case P_PID: /* child with process ID equal to... */
1274 case P_PGID: /* child with process group ID equal to... */
1275 if (((int)uap->id) < 0)
1276 return (EINVAL);
1277 break;
1278 case P_ALL: /* any child */
1279 break;
1280 }
1281
1282 loop:
1283 proc_list_lock();
1284 loop1:
1285 nfound = 0;
1286 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
1287 switch(uap->idtype) {
1288 case P_PID: /* child with process ID equal to... */
1289 if (p->p_pid != (pid_t)uap->id)
1290 continue;
1291 break;
1292 case P_PGID: /* child with process group ID equal to... */
1293 if (p->p_pgrpid != (pid_t)uap->id)
1294 continue;
1295 break;
1296 case P_ALL: /* any child */
1297 break;
1298 }
1299
1300 /* XXX This is racy because we don't get the lock!!!! */
1301
1302 /*
1303 * Wait collision; go to sleep and restart; used to maintain
1304 * the single return for waited process guarantee.
1305 */
1306 if (p->p_listflag & P_LIST_WAITING) {
1307 (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitidcoll", 0);
1308 goto loop1;
1309 }
1310 p->p_listflag |= P_LIST_WAITING; /* mark busy */
1311
1312 nfound++;
1313
1314 /*
1315 * Types of processes we are interested in
1316 *
1317 * XXX Don't know what to do for WCONTINUED?!?
1318 */
1319 switch(p->p_stat) {
1320 case SZOMB: /* Exited */
1321 if (!(uap->options & WEXITED))
1322 break;
1323
1324 /* drop the lock and the thread is going to return */
1325 proc_list_unlock();
1326
1327 /* Collect "siginfo" information for caller */
1328 collect64.si_signo = SIGCHLD;
1329 collect64.si_code = 0;
1330 collect64.si_errno = 0;
1331 collect64.si_pid = 0;
1332 collect64.si_uid = 0;
1333 collect64.si_addr = 0;
1334 collect64.si_status = WEXITSTATUS(p->p_xstat);
1335 collect64.si_band = 0;
1336
1337 if (IS_64BIT_PROCESS(p)) {
1338 user64_siginfo_t sinfo64;
1339
1340 siginfo_user_to_user64(&collect64, &sinfo64);
1341
1342 error = copyout((caddr_t)&sinfo64,
1343 uap->infop,
1344 sizeof(sinfo64));
1345 } else {
1346 user32_siginfo_t sinfo32;
1347
1348 siginfo_user_to_user32(&collect64, &sinfo32);
1349
1350 error = copyout((caddr_t)&sinfo32,
1351 uap->infop,
1352 sizeof(sinfo32));
1353 }
1354 /* information unavailable? */
1355 if (error)
1356 goto out;
1357
1358 /* Prevent other process for waiting for this event? */
1359 if (!(uap->options & WNOWAIT)) {
1360 /* Clean up */
1361 (void)reap_child_locked(q, p, 0, 0, 0);
1362 } else {
1363 proc_list_lock();
1364 p->p_listflag &= ~P_LIST_WAITING;
1365 proc_list_unlock();
1366 }
1367
1368 return (0);
1369
1370 case SSTOP: /* Stopped */
1371 /*
1372 * If we are not interested in stopped processes, then
1373 * ignore this one.
1374 */
1375 if (!(uap->options & WSTOPPED))
1376 break;
1377
1378 /*
1379 * If someone has already waited it, we lost a race
1380 * to be the one to return status.
1381 */
1382 if ((p->p_lflag & P_LWAITED) != 0)
1383 break;
1384
1385 /* drop the lock and the thread is going to return */
1386 proc_list_unlock();
1387
1388 /* Collect "siginfo" information for caller */
1389 collect64.si_signo = SIGCHLD;
1390 collect64.si_code = 0;
1391 collect64.si_errno = 0;
1392 collect64.si_pid = 0;
1393 collect64.si_uid = 0;
1394 collect64.si_addr = 0;
1395 proc_lock(p);
1396 collect64.si_status = p->p_xstat;
1397 proc_unlock(p);
1398 collect64.si_band = 0;
1399
1400 if (IS_64BIT_PROCESS(p)) {
1401 user64_siginfo_t sinfo64;
1402
1403 siginfo_user_to_user64(&collect64, &sinfo64);
1404
1405 error = copyout((caddr_t)&sinfo64,
1406 uap->infop,
1407 sizeof(sinfo64));
1408 } else {
1409 user32_siginfo_t sinfo32;
1410
1411 siginfo_user_to_user32(&collect64, &sinfo32);
1412
1413 error = copyout((caddr_t)&sinfo32,
1414 uap->infop,
1415 sizeof(sinfo32));
1416 }
1417 /* information unavailable? */
1418 if (error)
1419 goto out;
1420
1421 /* Prevent other process for waiting for this event? */
1422 if (!(uap->options & WNOWAIT)) {
1423 proc_lock(p);
1424 p->p_lflag |= P_LWAITED;
1425 proc_unlock(p);
1426 }
1427
1428 error = 0;
1429 goto out;
1430
1431 default: /* All others */
1432 /* ...meaning Continued */
1433 if (!(uap->options & WCONTINUED))
1434 break;
1435
1436 /*
1437 * If the flag isn't set, then this process has not
1438 * been stopped and continued, or the status has
1439 * already been reaped by another caller of waitid().
1440 */
1441 if ((p->p_flag & P_CONTINUED) == 0)
1442 break;
1443
1444 /* drop the lock and the thread is going to return */
1445 proc_list_unlock();
1446
1447 /* Collect "siginfo" information for caller */
1448 proc_lock(p);
1449 collect64.si_signo = SIGCHLD;
1450 collect64.si_code = CLD_CONTINUED;
1451 collect64.si_errno = 0;
1452 collect64.si_pid = p->p_contproc;
1453 collect64.si_uid = 0;
1454 collect64.si_addr = 0;
1455 collect64.si_status = p->p_xstat;
1456 collect64.si_band = 0;
1457 proc_unlock(p);
1458
1459 if (IS_64BIT_PROCESS(p)) {
1460 user64_siginfo_t sinfo64;
1461
1462 siginfo_user_to_user64(&collect64, &sinfo64);
1463
1464 error = copyout((caddr_t)&sinfo64,
1465 uap->infop,
1466 sizeof(sinfo64));
1467 } else {
1468 user32_siginfo_t sinfo32;
1469
1470 siginfo_user_to_user32(&collect64, &sinfo32);
1471
1472 error = copyout((caddr_t)&sinfo32,
1473 uap->infop,
1474 sizeof(sinfo32));
1475 }
1476 /* information unavailable? */
1477 if (error)
1478 goto out;
1479
1480 /* Prevent other process for waiting for this event? */
1481 if (!(uap->options & WNOWAIT)) {
1482 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
1483 }
1484
1485 error = 0;
1486 goto out;
1487 }
1488 /* LIST LOCK IS HELD HERE */
1489 /* Not a process we are interested in; go on to next child */
1490
1491 p->p_listflag &= ~P_LIST_WAITING;
1492 wakeup(&p->p_stat);
1493 }
1494
1495 /* list lock is always held */
1496 /* No child processes that could possibly satisfy the request? */
1497 if (nfound == 0) {
1498 proc_list_unlock();
1499 return (ECHILD);
1500 }
1501
1502 if (uap->options & WNOHANG) {
1503 proc_list_unlock();
1504 return (0);
1505 }
1506
1507 if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)))
1508 return (error);
1509
1510 goto loop;
1511 out:
1512 proc_list_lock();
1513 p->p_listflag &= ~P_LIST_WAITING;
1514 wakeup(&p->p_stat);
1515 proc_list_unlock();
1516 return (error);
1517 }
1518
1519 /*
1520 * make process 'parent' the new parent of process 'child'.
1521 */
1522 void
1523 proc_reparentlocked(proc_t child, proc_t parent, int cansignal, int locked)
1524 {
1525 proc_t oldparent = PROC_NULL;
1526
1527 if (child->p_pptr == parent)
1528 return;
1529
1530 if (locked == 0)
1531 proc_list_lock();
1532
1533 oldparent = child->p_pptr;
1534 #if __PROC_INTERNAL_DEBUG
1535 if (oldparent == PROC_NULL)
1536 panic("proc_reparent: process %p does not have a parent\n", child);
1537 #endif
1538
1539 LIST_REMOVE(child, p_sibling);
1540 #if __PROC_INTERNAL_DEBUG
1541 if (oldparent->p_childrencnt == 0)
1542 panic("process children count already 0\n");
1543 #endif
1544 oldparent->p_childrencnt--;
1545 #if __PROC_INTERNAL_DEBUG1
1546 if (oldparent->p_childrencnt < 0)
1547 panic("process children count -ve\n");
1548 #endif
1549 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1550 parent->p_childrencnt++;
1551 child->p_pptr = parent;
1552 child->p_ppid = parent->p_pid;
1553
1554 proc_list_unlock();
1555
1556 if ((cansignal != 0) && (initproc == parent) && (child->p_stat == SZOMB))
1557 psignal(initproc, SIGCHLD);
1558 if (locked == 1)
1559 proc_list_lock();
1560 }
1561
1562 /*
1563 * Exit: deallocate address space and other resources, change proc state
1564 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1565 * status and rusage for wait(). Check for child processes and orphan them.
1566 */
1567
1568 void
1569 vfork_exit(proc_t p, int rv)
1570 {
1571 vfork_exit_internal(p, rv, 0);
1572 }
1573
1574 void
1575 vfork_exit_internal(proc_t p, int rv, int forceexit)
1576 {
1577 thread_t self = current_thread();
1578 #ifdef FIXME
1579 struct task *task = p->task;
1580 #endif
1581 struct uthread *ut;
1582
1583 /*
1584 * If a thread in this task has already
1585 * called exit(), then halt any others
1586 * right here.
1587 */
1588
1589 ut = get_bsdthread_info(self);
1590
1591
1592 proc_lock(p);
1593 if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) {
1594 /*
1595 * This happens when a parent exits/killed and vfork is in progress
1596 * other threads. But shutdown code for ex has already called exit1()
1597 */
1598 proc_unlock(p);
1599 return;
1600 }
1601 p->p_lflag |= (P_LEXIT | P_LPEXIT);
1602 proc_unlock(p);
1603
1604 if (forceexit == 0) {
1605 /*
1606 * parent of a vfork child has already called exit() and the
1607 * thread that has vfork in proress terminates. So there is no
1608 * separate address space here and it has already been marked for
1609 * termination. This was never covered before and could cause problems
1610 * if we block here for outside code.
1611 */
1612 /* Notify the perf server */
1613 (void)sys_perf_notify(self, p->p_pid);
1614 }
1615
1616 /*
1617 * Remove proc from allproc queue and from pidhash chain.
1618 * Need to do this before we do anything that can block.
1619 * Not doing causes things like mount() find this on allproc
1620 * in partially cleaned state.
1621 */
1622
1623 proc_list_lock();
1624
1625 LIST_REMOVE(p, p_list);
1626 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
1627 /* will not be visible via proc_find */
1628 p->p_listflag |= P_LIST_EXITED;
1629
1630 proc_list_unlock();
1631
1632 proc_lock(p);
1633 p->p_xstat = rv;
1634 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
1635 p->p_sigignore = ~0;
1636 proc_unlock(p);
1637
1638 proc_spinlock(p);
1639 if (thread_call_cancel(p->p_rcall))
1640 p->p_ractive--;
1641
1642 while (p->p_ractive > 0) {
1643 proc_spinunlock(p);
1644
1645 delay(1);
1646
1647 proc_spinlock(p);
1648 }
1649 proc_spinunlock(p);
1650
1651 thread_call_free(p->p_rcall);
1652 p->p_rcall = NULL;
1653
1654 ut->uu_siglist = 0;
1655
1656 vproc_exit(p);
1657 }
1658
1659 void
1660 vproc_exit(proc_t p)
1661 {
1662 proc_t q;
1663 proc_t pp;
1664
1665 vnode_t tvp;
1666 #ifdef FIXME
1667 struct task *task = p->task;
1668 #endif
1669 struct pgrp * pg;
1670 struct session *sessp;
1671
1672 /* XXX Zombie allocation may fail, in which case stats get lost */
1673 MALLOC_ZONE(p->p_ru, struct rusage *,
1674 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
1675
1676
1677 proc_refdrain(p);
1678
1679 /*
1680 * Close open files and release open-file table.
1681 * This may block!
1682 */
1683 fdfree(p);
1684
1685 sessp = proc_session(p);
1686 if (SESS_LEADER(p, sessp)) {
1687
1688 if (sessp->s_ttyvp != NULLVP) {
1689 struct vnode *ttyvp;
1690 int ttyvid;
1691 struct vfs_context context;
1692 struct tty * tp;
1693
1694 /*
1695 * Controlling process.
1696 * Signal foreground pgrp,
1697 * drain controlling terminal
1698 * and revoke access to controlling terminal.
1699 */
1700 tp = SESSION_TP(sessp);
1701
1702 if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
1703 tty_pgsignal(tp, SIGHUP, 1);
1704 tty_lock(tp);
1705 (void) ttywait(tp);
1706 tty_unlock(tp);
1707 /*
1708 * The tty could have been revoked
1709 * if we blocked.
1710 */
1711
1712 session_lock(sessp);
1713 /* reget in case of race */
1714 tp = SESSION_TP(sessp);
1715 ttyvp = sessp->s_ttyvp;
1716 ttyvid = sessp->s_ttyvid;
1717 sessp->s_ttyvp = NULL;
1718 sessp->s_ttyvid = 0;
1719 sessp->s_ttyp = TTY_NULL;
1720 sessp->s_ttypgrpid = NO_PID;
1721 session_unlock(sessp);
1722
1723 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
1724 context.vc_thread = proc_thread(p); /* XXX */
1725 context.vc_ucred = kauth_cred_proc_ref(p);
1726 VNOP_REVOKE(ttyvp, REVOKEALL, &context);
1727 vnode_put(ttyvp);
1728 kauth_cred_unref(&context.vc_ucred);
1729 }
1730 } else {
1731 session_lock(sessp);
1732 ttyvp = sessp->s_ttyvp;
1733 sessp->s_ttyvp = NULL;
1734 sessp->s_ttyvid = 0;
1735 sessp->s_ttyp = TTY_NULL;
1736 sessp->s_ttypgrpid = NO_PID;
1737 session_unlock(sessp);
1738 }
1739 if (ttyvp)
1740 vnode_rele(ttyvp);
1741 /*
1742 * s_ttyp is not zero'd; we use this to indicate
1743 * that the session once had a controlling terminal.
1744 * (for logging and informational purposes)
1745 */
1746 }
1747
1748 session_lock(sessp);
1749 sessp->s_leader = NULL;
1750 session_unlock(sessp);
1751 }
1752 session_rele(sessp);
1753
1754 pg = proc_pgrp(p);
1755 fixjobc(p, pg, 0);
1756 pg_rele(pg);
1757
1758 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
1759
1760 proc_list_lock();
1761 proc_childdrainstart(p);
1762 while ((q = p->p_children.lh_first) != NULL) {
1763 q->p_listflag |= P_LIST_DEADPARENT;
1764 if (q->p_stat == SZOMB) {
1765 if (p != q->p_pptr)
1766 panic("parent child linkage broken");
1767 /* check for lookups by zomb sysctl */
1768 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
1769 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1770 }
1771 q->p_listflag |= P_LIST_WAITING;
1772 /*
1773 * This is a named reference and it is not granted
1774 * if the reap is already in progress. So we get
1775 * the reference here exclusively and their can be
1776 * no waiters. So there is no need for a wakeup
1777 * after we are done. AlsO the reap frees the structure
1778 * and the proc struct cannot be used for wakeups as well.
1779 * It is safe to use q here as this is system reap
1780 */
1781 (void)reap_child_locked(p, q, 1, 1, 0);
1782 } else {
1783 proc_reparentlocked(q, initproc, 0, 1);
1784 /*
1785 * Traced processes are killed
1786 * since their existence means someone is messing up.
1787 */
1788 if (q->p_lflag & P_LTRACED) {
1789 proc_list_unlock();
1790 proc_lock(q);
1791 q->p_lflag &= ~P_LTRACED;
1792 if (q->sigwait_thread) {
1793 thread_t thread = q->sigwait_thread;
1794
1795 proc_unlock(q);
1796 /*
1797 * The sigwait_thread could be stopped at a
1798 * breakpoint. Wake it up to kill.
1799 * Need to do this as it could be a thread which is not
1800 * the first thread in the task. So any attempts to kill
1801 * the process would result into a deadlock on q->sigwait.
1802 */
1803 thread_resume(thread);
1804 clear_wait(thread, THREAD_INTERRUPTED);
1805 threadsignal(thread, SIGKILL, 0);
1806 } else {
1807 proc_unlock(q);
1808 }
1809
1810 psignal(q, SIGKILL);
1811 proc_list_lock();
1812 }
1813 }
1814 }
1815
1816 proc_childdrainend(p);
1817 proc_list_unlock();
1818
1819 /*
1820 * Release reference to text vnode
1821 */
1822 tvp = p->p_textvp;
1823 p->p_textvp = NULL;
1824 if (tvp != NULLVP) {
1825 vnode_rele(tvp);
1826 }
1827
1828 /*
1829 * Save exit status and final rusage info, adding in child rusage
1830 * info and self times. If we were unable to allocate a zombie
1831 * structure, this information is lost.
1832 */
1833 /* No need for locking here as no one than this thread can access this */
1834 if (p->p_ru != NULL) {
1835 *p->p_ru = p->p_stats->p_ru;
1836 timerclear(&p->p_ru->ru_utime);
1837 timerclear(&p->p_ru->ru_stime);
1838
1839 #ifdef FIXME
1840 if (task) {
1841 task_basic_info_data_t tinfo;
1842 task_thread_times_info_data_t ttimesinfo;
1843 int task_info_stuff, task_ttimes_stuff;
1844 struct timeval ut,st;
1845
1846 task_info_stuff = TASK_BASIC_INFO_COUNT;
1847 task_info(task, TASK_BASIC_INFO,
1848 &tinfo, &task_info_stuff);
1849 p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
1850 p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
1851 p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
1852 p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
1853
1854 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
1855 task_info(task, TASK_THREAD_TIMES_INFO,
1856 &ttimesinfo, &task_ttimes_stuff);
1857
1858 ut.tv_sec = ttimesinfo.user_time.seconds;
1859 ut.tv_usec = ttimesinfo.user_time.microseconds;
1860 st.tv_sec = ttimesinfo.system_time.seconds;
1861 st.tv_usec = ttimesinfo.system_time.microseconds;
1862 timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
1863 timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
1864 }
1865 #endif /* FIXME */
1866
1867 ruadd(p->p_ru, &p->p_stats->p_cru);
1868 }
1869
1870 /*
1871 * Free up profiling buffers.
1872 */
1873 {
1874 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
1875
1876 p1 = p0->pr_next;
1877 p0->pr_next = NULL;
1878 p0->pr_scale = 0;
1879
1880 for (; p1 != NULL; p1 = pn) {
1881 pn = p1->pr_next;
1882 kfree(p1, sizeof *p1);
1883 }
1884 }
1885
1886 #if PSYNCH
1887 pth_proc_hashdelete(p);
1888 #endif /* PSYNCH */
1889
1890 /*
1891 * Other substructures are freed from wait().
1892 */
1893 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
1894 p->p_stats = NULL;
1895
1896 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
1897 p->p_sigacts = NULL;
1898
1899 proc_limitdrop(p, 1);
1900 p->p_limit = NULL;
1901
1902 /*
1903 * Finish up by terminating the task
1904 * and halt this thread (only if a
1905 * member of the task exiting).
1906 */
1907 p->task = TASK_NULL;
1908
1909 /*
1910 * Notify parent that we're gone.
1911 */
1912 pp = proc_parent(p);
1913 if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
1914 if (pp != initproc) {
1915 proc_lock(pp);
1916 pp->si_pid = p->p_pid;
1917 pp->si_status = p->p_xstat;
1918 pp->si_code = CLD_EXITED;
1919 /*
1920 * p_ucred usage is safe as it is an exiting process
1921 * and reference is dropped in reap
1922 */
1923 pp->si_uid = kauth_cred_getruid(p->p_ucred);
1924 proc_unlock(pp);
1925 }
1926 /* mark as a zombie */
1927 /* mark as a zombie */
1928 /* No need to take proc lock as all refs are drained and
1929 * no one except parent (reaping ) can look at this.
1930 * The write is to an int and is coherent. Also parent is
1931 * keyed off of list lock for reaping
1932 */
1933 p->p_stat = SZOMB;
1934
1935 psignal(pp, SIGCHLD);
1936
1937 /* and now wakeup the parent */
1938 proc_list_lock();
1939 wakeup((caddr_t)pp);
1940 proc_list_unlock();
1941 } else {
1942 proc_list_lock();
1943 /* check for lookups by zomb sysctl */
1944 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
1945 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1946 }
1947 p->p_stat = SZOMB;
1948 p->p_listflag |= P_LIST_WAITING;
1949
1950 /*
1951 * This is a named reference and it is not granted
1952 * if the reap is already in progress. So we get
1953 * the reference here exclusively and their can be
1954 * no waiters. So there is no need for a wakeup
1955 * after we are done. AlsO the reap frees the structure
1956 * and the proc struct cannot be used for wakeups as well.
1957 * It is safe to use p here as this is system reap
1958 */
1959 (void)reap_child_locked(pp, p, 0, 1, 1);
1960 /* list lock dropped by reap_child_locked */
1961 }
1962 proc_rele(pp);
1963 }
1964
1965
1966 /*
1967 * munge_rusage
1968 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1969 * process. We munge the kernel version of rusage into the
1970 * 64 bit version.
1971 */
1972 __private_extern__ void
1973 munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p)
1974 {
1975 /* timeval changes size, so utime and stime need special handling */
1976 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
1977 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
1978 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
1979 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
1980 /*
1981 * everything else can be a direct assign, since there is no loss
1982 * of precision implied boing 32->64.
1983 */
1984 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
1985 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
1986 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
1987 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
1988 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
1989 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
1990 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
1991 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
1992 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
1993 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
1994 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
1995 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
1996 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
1997 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
1998 }
1999
2000 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2001 __private_extern__ void
2002 munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p)
2003 {
2004 /* timeval changes size, so utime and stime need special handling */
2005 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
2006 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
2007 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
2008 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
2009 /*
2010 * everything else can be a direct assign. We currently ignore
2011 * the loss of precision
2012 */
2013 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
2014 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
2015 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
2016 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
2017 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
2018 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
2019 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
2020 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
2021 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
2022 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
2023 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
2024 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
2025 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
2026 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
2027 }