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