]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/kern_exit.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / kern / kern_exit.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
31/*
32 * Copyright (c) 1982, 1986, 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
69 */
70
71#include <machine/reg.h>
72#include <machine/psl.h>
73
74#include "compat_43.h"
75
76#include <sys/param.h>
77#include <sys/systm.h>
78#include <sys/ioctl.h>
79#include <sys/proc_internal.h>
80#include <sys/kauth.h>
81#include <sys/tty.h>
82#include <sys/time.h>
83#include <sys/resource.h>
84#include <sys/kernel.h>
85#include <sys/wait.h>
86#include <sys/file_internal.h>
87#include <sys/vnode_internal.h>
88#include <sys/syslog.h>
89#include <sys/malloc.h>
90#include <sys/resourcevar.h>
91#include <sys/ptrace.h>
92#include <sys/user.h>
93#include <sys/aio_kern.h>
94#include <sys/sysproto.h>
95#include <sys/signalvar.h>
96#include <sys/filedesc.h> /* fdfree */
97#include <sys/shm_internal.h> /* shmexit */
98#include <sys/acct.h> /* acct_process */
99#include <machine/spl.h>
100
101#include <bsm/audit_kernel.h>
102#include <bsm/audit_kevents.h>
103
104#include <mach/mach_types.h>
105
106#include <kern/kern_types.h>
107#include <kern/kalloc.h>
108#include <kern/task.h>
109#include <kern/thread.h>
110#include <kern/sched_prim.h>
111#include <kern/assert.h>
112#if KTRACE
113#include <sys/ktrace.h>
114#endif
115
116#include <mach/mach_types.h>
117#include <mach/task.h>
118#include <mach/thread_act.h>
119#include <mach/mach_traps.h> /* init_process */
120
121extern char init_task_failure_data[];
122int exit1(struct proc *, int, int *);
123void proc_prepareexit(struct proc *p);
124void vfork_exit(struct proc *p, int rv);
125void vproc_exit(struct proc *p);
126__private_extern__ void munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p);
127
128/*
129 * Things which should have prototypes in headers, but don't
130 */
131void unix_syscall_return(int);
132void *get_bsduthreadarg(thread_t);
133void proc_exit(struct proc *p);
134int wait1continue(int result);
135int waitidcontinue(int result);
136int *get_bsduthreadrval(thread_t);
137kern_return_t sys_perf_notify(struct task *task, exception_data_t code,
138 mach_msg_type_number_t codeCnt);
139
140/*
141 * NOTE: Source and target may *NOT* overlap!
142 * XXX Should share code with bsd/dev/ppc/unix_signal.c
143 */
144static void
145siginfo_64to32(user_siginfo_t *in, siginfo_t *out)
146{
147 out->si_signo = in->si_signo;
148 out->si_errno = in->si_errno;
149 out->si_code = in->si_code;
150 out->si_pid = in->si_pid;
151 out->si_uid = in->si_uid;
152 out->si_status = in->si_status;
153 out->si_addr = CAST_DOWN(void *,in->si_addr);
154 /* following cast works for sival_int because of padding */
155 out->si_value.sival_ptr = CAST_DOWN(void *,in->si_value.sival_ptr);
156 out->si_band = in->si_band; /* range reduction */
157 out->pad[0] = in->pad[0]; /* mcontext.ss.r1 */
158}
159
160/*
161 * exit --
162 * Death of process.
163 */
164void
165exit(struct proc *p, struct exit_args *uap, int *retval)
166{
167 exit1(p, W_EXITCODE(uap->rval, 0), retval);
168
169 /* drop funnel before we return */
170 thread_funnel_set(kernel_flock, FALSE);
171 thread_exception_return();
172 /* NOTREACHED */
173 while (TRUE)
174 thread_block(THREAD_CONTINUE_NULL);
175 /* NOTREACHED */
176}
177
178/*
179 * Exit: deallocate address space and other resources, change proc state
180 * to zombie, and unlink proc from allproc and parent's lists. Save exit
181 * status and rusage for wait(). Check for child processes and orphan them.
182 */
183int
184exit1(struct proc *p, int rv, int *retval)
185{
186 thread_t self = current_thread();
187 struct task *task = p->task;
188 register int s;
189 struct uthread *ut;
190
191 /*
192 * If a thread in this task has already
193 * called exit(), then halt any others
194 * right here.
195 */
196
197 ut = get_bsdthread_info(self);
198 if (ut->uu_flag & UT_VFORK) {
199 vfork_exit(p, rv);
200 vfork_return(self, p->p_pptr, p , retval);
201 unix_syscall_return(0);
202 /* NOT REACHED */
203 }
204 AUDIT_SYSCALL_EXIT(0, p, ut); /* Exit is always successfull */
205 signal_lock(p);
206 while (p->exit_thread != self) {
207 if (sig_try_locked(p) <= 0) {
208 if (get_threadtask(self) != task) {
209 signal_unlock(p);
210 return(0);
211 }
212 signal_unlock(p);
213 thread_terminate(self);
214 thread_funnel_set(kernel_flock, FALSE);
215 thread_exception_return();
216 /* NOTREACHED */
217 }
218 sig_lock_to_exit(p);
219 }
220 signal_unlock(p);
221 if (p->p_pid == 1) {
222 printf("pid 1 exited (signal %d, exit %d)",
223 WTERMSIG(rv), WEXITSTATUS(rv));
224 panic("init died\nState at Last Exception:\n\n%s",
225 init_task_failure_data);
226 }
227
228 s = splsched();
229 p->p_flag |= P_WEXIT;
230 splx(s);
231 proc_prepareexit(p);
232 p->p_xstat = rv;
233
234 /* task terminate will call proc_terminate and that cleans it up */
235 task_terminate_internal(task);
236
237 return(0);
238}
239
240void
241proc_prepareexit(struct proc *p)
242{
243 struct uthread *ut;
244 exception_data_t code[EXCEPTION_CODE_MAX];
245 thread_t self = current_thread();
246
247 code[0] = (exception_data_t)0xFF000001; /* Set terminate code */
248 code[1] = (exception_data_t)p->p_pid; /* Pass out the pid */
249 /* Notify the perf server */
250 (void)sys_perf_notify(p->task, (exception_data_t)&code, 2);
251
252 /*
253 * Remove proc from allproc queue and from pidhash chain.
254 * Need to do this before we do anything that can block.
255 * Not doing causes things like mount() find this on allproc
256 * in partially cleaned state.
257 */
258 LIST_REMOVE(p, p_list);
259 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
260 LIST_REMOVE(p, p_hash);
261
262#ifdef PGINPROF
263 vmsizmon();
264#endif
265 /*
266 * If parent is waiting for us to exit or exec,
267 * P_PPWAIT is set; we will wakeup the parent below.
268 */
269 p->p_flag &= ~(P_TRACED | P_PPWAIT);
270 p->p_sigignore = ~(sigcantmask);
271 p->p_siglist = 0;
272 ut = get_bsdthread_info(self);
273 ut->uu_siglist = 0;
274 untimeout(realitexpire, (caddr_t)p->p_pid);
275}
276
277void
278proc_exit(struct proc *p)
279{
280 register struct proc *q, *nq, *pp;
281 struct task *task = p->task;
282 register int s;
283 boolean_t funnel_state;
284
285 /* This can happen if thread_terminate of the single thread
286 * process
287 */
288
289 funnel_state = thread_funnel_set(kernel_flock, TRUE);
290 if( !(p->p_flag & P_WEXIT)) {
291 s = splsched();
292 p->p_flag |= P_WEXIT;
293 splx(s);
294 proc_prepareexit(p);
295 }
296
297 p->p_lflag |= P_LPEXIT;
298 /* XXX Zombie allocation may fail, in which case stats get lost */
299 MALLOC_ZONE(p->p_ru, struct rusage *,
300 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
301
302 /*
303 * need to cancel async IO requests that can be cancelled and wait for those
304 * already active. MAY BLOCK!
305 */
306
307 p->p_lflag |= P_LREFDRAIN;
308 while (p->p_internalref) {
309 p->p_lflag |= P_LREFDRAINWAIT;
310 msleep(&p->p_internalref, (lck_mtx_t *)0, 0, "proc_refdrain", 0) ;
311 }
312 p->p_lflag &= ~P_LREFDRAIN;
313 p->p_lflag |= P_LREFDEAD;
314
315 _aio_exit( p );
316
317 /*
318 * Close open files and release open-file table.
319 * This may block!
320 */
321 fdfree(p);
322
323 /* Close ref SYSV Shared memory*/
324 if (p->vm_shm)
325 shmexit(p);
326 /* Release SYSV semaphores */
327 semexit(p);
328
329 if (SESS_LEADER(p)) {
330 register struct session *sp = p->p_session;
331
332 if (sp->s_ttyvp) {
333 struct vnode *ttyvp;
334 struct vfs_context context;
335
336 /*
337 * Controlling process.
338 * Signal foreground pgrp,
339 * drain controlling terminal
340 * and revoke access to controlling terminal.
341 */
342 if (sp->s_ttyp->t_session == sp) {
343 if (sp->s_ttyp->t_pgrp)
344 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
345 (void) ttywait(sp->s_ttyp);
346 /*
347 * The tty could have been revoked
348 * if we blocked.
349 */
350 context.vc_proc = p;
351 context.vc_ucred = p->p_ucred;
352 if (sp->s_ttyvp)
353 VNOP_REVOKE(sp->s_ttyvp, REVOKEALL, &context);
354 }
355 ttyvp = sp->s_ttyvp;
356 sp->s_ttyvp = NULL;
357 if (ttyvp) {
358 vnode_rele(ttyvp);
359 }
360 /*
361 * s_ttyp is not zero'd; we use this to indicate
362 * that the session once had a controlling terminal.
363 * (for logging and informational purposes)
364 */
365 }
366 sp->s_leader = NULL;
367 }
368
369 fixjobc(p, p->p_pgrp, 0);
370 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
371 (void)acct_process(p);
372
373#if KTRACE
374 /*
375 * release trace file
376 */
377 p->p_traceflag = 0; /* don't trace the vnode_put() */
378 if (p->p_tracep) {
379 struct vnode *tvp = p->p_tracep;
380 p->p_tracep = NULL;
381 vnode_rele(tvp);
382 }
383#endif
384
385 while (q = p->p_children.lh_first) {
386 proc_reparent(q, initproc);
387 /*
388 * Traced processes are killed
389 * since their existence means someone is messing up.
390 */
391 if (q->p_flag & P_TRACED) {
392 q->p_flag &= ~P_TRACED;
393 if (q->sigwait_thread) {
394 /*
395 * The sigwait_thread could be stopped at a
396 * breakpoint. Wake it up to kill.
397 * Need to do this as it could be a thread which is not
398 * the first thread in the task. So any attempts to kill
399 * the process would result into a deadlock on q->sigwait.
400 */
401 thread_resume((thread_t)q->sigwait_thread);
402 clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
403 threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
404 }
405 psignal(q, SIGKILL);
406 }
407 }
408
409 /*
410 * Save exit status and final rusage info, adding in child rusage
411 * info and self times. If we were unable to allocate a zombie
412 * structure, this information is lost.
413 */
414 if (p->p_ru != NULL) {
415 *p->p_ru = p->p_stats->p_ru;
416
417 timerclear(&p->p_ru->ru_utime);
418 timerclear(&p->p_ru->ru_stime);
419
420 if (task) {
421 task_basic_info_data_t tinfo;
422 task_thread_times_info_data_t ttimesinfo;
423 int task_info_stuff, task_ttimes_stuff;
424 struct timeval ut,st;
425
426 task_info_stuff = TASK_BASIC_INFO_COUNT;
427 task_info(task, TASK_BASIC_INFO,
428 (task_info_t)&tinfo, &task_info_stuff);
429 p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
430 p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
431 p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
432 p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
433
434 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
435 task_info(task, TASK_THREAD_TIMES_INFO,
436 (task_info_t)&ttimesinfo, &task_ttimes_stuff);
437
438 ut.tv_sec = ttimesinfo.user_time.seconds;
439 ut.tv_usec = ttimesinfo.user_time.microseconds;
440 st.tv_sec = ttimesinfo.system_time.seconds;
441 st.tv_usec = ttimesinfo.system_time.microseconds;
442 timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
443 timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
444 }
445
446 ruadd(p->p_ru, &p->p_stats->p_cru);
447 }
448
449 /*
450 * Free up profiling buffers.
451 */
452 {
453 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
454
455 p1 = p0->pr_next;
456 p0->pr_next = NULL;
457 p0->pr_scale = 0;
458
459 for (; p1 != NULL; p1 = pn) {
460 pn = p1->pr_next;
461 kfree(p1, sizeof *p1);
462 }
463 }
464
465 /*
466 * Other substructures are freed from wait().
467 */
468 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
469 p->p_stats = NULL;
470
471 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
472 p->p_sigacts = NULL;
473
474 if (--p->p_limit->p_refcnt == 0)
475 FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
476 p->p_limit = NULL;
477
478 /*
479 * Finish up by terminating the task
480 * and halt this thread (only if a
481 * member of the task exiting).
482 */
483 p->task = TASK_NULL;
484 //task->proc = NULL;
485 set_bsdtask_info(task, NULL);
486
487 KNOTE(&p->p_klist, NOTE_EXIT);
488
489 /*
490 * Notify parent that we're gone.
491 */
492 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
493 struct proc *opp = p->p_pptr;
494
495 /*
496 * Add child resource usage to parent before giving
497 * zombie to init. If we were unable to allocate a
498 * zombie structure, this information is lost.
499 */
500 if (p->p_ru != NULL)
501 ruadd(&p->p_pptr->p_stats->p_cru, p->p_ru);
502
503 proc_reparent(p, initproc);
504 /* If there are no more children wakeup parent */
505 if (LIST_EMPTY(&opp->p_children))
506 wakeup((caddr_t)opp);
507 }
508 /* should be fine as parent proc would be initproc */
509 pp = p->p_pptr;
510 if (pp != initproc) {
511 pp->si_pid = p->p_pid;
512 pp->si_status = p->p_xstat;
513 pp->si_code = CLD_EXITED;
514 pp->si_uid = p->p_ucred->cr_ruid;
515 }
516 /* mark as a zombie */
517 p->p_stat = SZOMB;
518
519 psignal(pp, SIGCHLD);
520
521 /* and now wakeup the parent */
522 wakeup((caddr_t)p->p_pptr);
523
524 (void) thread_funnel_set(kernel_flock, funnel_state);
525}
526
527
528/*
529 * reap_child_process
530 *
531 * Description: Given a process from which all status information needed
532 * has already been extracted, if the process is a ptrace
533 * attach process, detach it and give it back to its real
534 * parent, else recover all resources remaining associated
535 * with it.
536 *
537 * Parameters: struct proc *parent Parent of process being reaped
538 * struct proc *child Process to reap
539 *
540 * Returns: 0 Process was not reaped because it
541 * came from an attach
542 * 1 Process was reaped
543 */
544static int
545reap_child_process(struct proc *parent, struct proc *child)
546{
547 struct proc *trace_parent; /* Traced parent process, if tracing */
548 struct vnode *tvp; /* Traced vnode pointer, if used */
549
550 /*
551 * If we got the child via a ptrace 'attach',
552 * we need to give it back to the old parent.
553 */
554 if (child->p_oppid && (trace_parent = pfind(child->p_oppid))) {
555 child->p_oppid = 0;
556 proc_reparent(child, trace_parent);
557 if (trace_parent != initproc) {
558 trace_parent->si_pid = child->p_pid;
559 trace_parent->si_status = child->p_xstat;
560 trace_parent->si_code = CLD_CONTINUED;
561 trace_parent->si_uid = child->p_ucred->cr_ruid;
562 }
563 psignal(trace_parent, SIGCHLD);
564 wakeup((caddr_t)trace_parent);
565 return (0);
566 }
567 child->p_xstat = 0;
568 if (child->p_ru) {
569 ruadd(&parent->p_stats->p_cru, child->p_ru);
570 FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE);
571 child->p_ru = NULL;
572 } else {
573 printf("Warning : lost p_ru for %s\n", child->p_comm);
574 }
575
576 /*
577 * Decrement the count of procs running with this uid.
578 */
579 (void)chgproccnt(child->p_ucred->cr_ruid, -1);
580
581 /*
582 * Free up credentials.
583 */
584 if (child->p_ucred != NOCRED) {
585 kauth_cred_t ucr = child->p_ucred;
586 child->p_ucred = NOCRED;
587 kauth_cred_rele(ucr);
588 }
589
590 /*
591 * Release reference to text vnode
592 */
593 tvp = child->p_textvp;
594 child->p_textvp = NULL;
595 if (tvp) {
596 vnode_rele(tvp);
597 }
598 /*
599 * Finally finished with old proc entry.
600 * Unlink it from its process group and free it.
601 */
602 leavepgrp(child);
603 LIST_REMOVE(child, p_list); /* off zombproc */
604 LIST_REMOVE(child, p_sibling);
605 child->p_lflag &= ~P_LWAITING;
606 wakeup(&child->p_stat);
607
608 lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
609 lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
610 FREE_ZONE(child, sizeof *child, M_PROC);
611 nprocs--;
612 return (1);
613}
614
615
616int
617wait1continue(int result)
618{
619 void *vt;
620 thread_t thread;
621 int *retval;
622 struct proc *p;
623
624 if (result)
625 return(result);
626
627 p = current_proc();
628 thread = current_thread();
629 vt = get_bsduthreadarg(thread);
630 retval = get_bsduthreadrval(thread);
631 return(wait4((struct proc *)p, (struct wait4_args *)vt, retval));
632}
633
634int
635wait4(struct proc *q, struct wait4_args *uap, register_t *retval)
636{
637 register int nfound;
638 register struct proc *p;
639 int status, error;
640
641 if (uap->pid == 0)
642 uap->pid = -q->p_pgid;
643
644loop:
645 nfound = 0;
646 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
647 if (uap->pid != WAIT_ANY &&
648 p->p_pid != uap->pid &&
649 p->p_pgid != -(uap->pid))
650 continue;
651 nfound++;
652
653 /* XXX This is racy because we don't get the lock!!!! */
654
655 if (p->p_lflag & P_LWAITING) {
656 (void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0);
657 goto loop;
658 }
659 p->p_lflag |= P_LWAITING; /* only allow single thread to wait() */
660
661 if (p->p_stat == SZOMB) {
662 retval[0] = p->p_pid;
663 if (uap->status) {
664 status = p->p_xstat; /* convert to int */
665 error = copyout((caddr_t)&status,
666 uap->status,
667 sizeof(status));
668 if (error) {
669 p->p_lflag &= ~P_LWAITING;
670 wakeup(&p->p_stat);
671 return (error);
672 }
673 }
674 if (uap->rusage) {
675 if (p->p_ru == NULL) {
676 error = ENOMEM;
677 } else {
678 if (IS_64BIT_PROCESS(q)) {
679 struct user_rusage my_rusage;
680 munge_rusage(p->p_ru, &my_rusage);
681 error = copyout((caddr_t)&my_rusage,
682 uap->rusage,
683 sizeof (my_rusage));
684 }
685 else {
686 error = copyout((caddr_t)p->p_ru,
687 uap->rusage,
688 sizeof (struct rusage));
689 }
690 }
691 /* information unavailable? */
692 if (error) {
693 p->p_lflag &= ~P_LWAITING;
694 wakeup(&p->p_stat);
695 return (error);
696 }
697 }
698
699 /* Clean up */
700 if (!reap_child_process(q, p)) {
701 p->p_lflag &= ~P_LWAITING;
702 wakeup(&p->p_stat);
703 }
704
705 return (0);
706 }
707 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
708 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
709 p->p_flag |= P_WAITED;
710 retval[0] = p->p_pid;
711 if (uap->status) {
712 status = W_STOPCODE(p->p_xstat);
713 error = copyout((caddr_t)&status,
714 uap->status,
715 sizeof(status));
716 } else
717 error = 0;
718 p->p_lflag &= ~P_LWAITING;
719 wakeup(&p->p_stat);
720 return (error);
721 }
722 p->p_lflag &= ~P_LWAITING;
723 wakeup(&p->p_stat);
724 }
725 if (nfound == 0)
726 return (ECHILD);
727
728 if (uap->options & WNOHANG) {
729 retval[0] = 0;
730 return (0);
731 }
732
733 if ((error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue)))
734 return (error);
735
736 goto loop;
737}
738
739
740int
741waitidcontinue(int result)
742{
743 void *vt;
744 thread_t thread;
745 int *retval;
746 struct proc *p;
747
748 if (result)
749 return(result);
750
751 p = current_proc();
752 thread = current_thread();
753 vt = get_bsduthreadarg(thread);
754 retval = get_bsduthreadrval(thread);
755 return(waitid((struct proc *)p, (struct waitid_args *)vt, retval));
756}
757
758/*
759 * Description: Suspend the calling thread until one child of the process
760 * containing the calling thread changes state.
761 *
762 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
763 * uap->id pid_t or gid_t or ignored
764 * uap->infop Address of signinfo_t struct in
765 * user space into which to return status
766 * uap->options flag values
767 *
768 * Returns: 0 Success
769 * !0 Error returning status to user space
770 */
771int
772waitid(struct proc *q, struct waitid_args *uap, register_t *retval)
773{
774 user_siginfo_t collect64; /* siginfo data to return to caller */
775
776 register int nfound;
777 register struct proc *p;
778 int error;
779
780loop:
781 nfound = 0;
782 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
783 switch(uap->idtype) {
784 case P_PID: /* child with process ID equal to... */
785 if (p->p_pid != (pid_t)uap->id)
786 continue;
787 break;
788 case P_PGID: /* child with process group ID equal to... */
789 if (p->p_pgid != (pid_t)uap->id)
790 continue;
791 break;
792 case P_ALL: /* any child */
793 break;
794 }
795
796 /* XXX This is racy because we don't get the lock!!!! */
797
798 /*
799 * Wait collision; go to sleep and restart; used to maintain
800 * the single return for waited process guarantee.
801 */
802 if (p->p_lflag & P_LWAITING) {
803 (void)tsleep(&p->p_stat, PWAIT, "waitidcoll", 0);
804 goto loop;
805 }
806 p->p_lflag |= P_LWAITING; /* mark busy */
807
808 nfound++;
809
810 /*
811 * Types of processes we are interested in
812 *
813 * XXX Don't know what to do for WCONTINUED?!?
814 */
815 switch(p->p_stat) {
816 case SZOMB: /* Exited */
817 if (!(uap->options & WEXITED))
818 break;
819
820 /* Collect "siginfo" information for caller */
821 collect64.si_signo = 0;
822 collect64.si_code = 0;
823 collect64.si_errno = 0;
824 collect64.si_pid = 0;
825 collect64.si_uid = 0;
826 collect64.si_addr = 0;
827 collect64.si_status = p->p_xstat;
828 collect64.si_band = 0;
829
830 if (IS_64BIT_PROCESS(p)) {
831 error = copyout((caddr_t)&collect64,
832 uap->infop,
833 sizeof(collect64));
834 } else {
835 siginfo_t collect;
836 siginfo_64to32(&collect64,&collect);
837 error = copyout((caddr_t)&collect,
838 uap->infop,
839 sizeof(collect));
840 }
841 /* information unavailable? */
842 if (error) {
843 p->p_lflag &= ~P_LWAITING;
844 wakeup(&p->p_stat);
845 return (error);
846 }
847
848 /* Prevent other process for waiting for this event? */
849 if (!(uap->options & WNOWAIT)) {
850 /* Clean up */
851 if (!reap_child_process(q, p)) {
852 p->p_lflag &= ~P_LWAITING;
853 wakeup(&p->p_stat);
854 }
855 }
856
857 return (0);
858
859 case SSTOP: /* Stopped */
860 /*
861 * If we are not interested in stopped processes, then
862 * ignore this one.
863 */
864 if (!(uap->options & WSTOPPED))
865 break;
866
867 /*
868 * If someone has already waited it, we lost a race
869 * to be the one to return status.
870 */
871 if ((p->p_flag & P_WAITED) != 0)
872 break;
873
874 /*
875 * If this is not a traced process, and they haven't
876 * indicated an interest in untraced processes, then
877 * ignore this one.
878 */
879 if (!(p->p_flag & P_TRACED) && !(uap->options & WUNTRACED))
880 break;
881
882 /* Collect "siginfo" information for caller */
883 collect64.si_signo = 0;
884 collect64.si_code = 0;
885 collect64.si_errno = 0;
886 collect64.si_pid = 0;
887 collect64.si_uid = 0;
888 collect64.si_addr = 0;
889 collect64.si_status = p->p_xstat;
890 collect64.si_band = 0;
891
892 if (IS_64BIT_PROCESS(p)) {
893 error = copyout((caddr_t)&collect64,
894 uap->infop,
895 sizeof(collect64));
896 } else {
897 siginfo_t collect;
898 siginfo_64to32(&collect64,&collect);
899 error = copyout((caddr_t)&collect,
900 uap->infop,
901 sizeof(collect));
902 }
903 /* information unavailable? */
904 if (error) {
905 p->p_lflag &= ~P_LWAITING;
906 wakeup(&p->p_stat);
907 return (error);
908 }
909
910 /* Prevent other process for waiting for this event? */
911 if (!(uap->options & WNOWAIT)) {
912 p->p_flag |= P_WAITED;
913 }
914
915 p->p_lflag &= ~P_LWAITING;
916 wakeup(&p->p_stat);
917 return (0);
918
919 default: /* All others */
920 /* ...meaning Continued */
921 if (!(uap->options & WCONTINUED))
922 break;
923
924 /*
925 * If the flag isn't set, then this process has not
926 * been stopped and continued, or the status has
927 * already been reaped by another caller of waitid().
928 */
929 if ((p->p_flag & P_CONTINUED) == 0)
930 break;
931
932 /* Collect "siginfo" information for caller */
933 collect64.si_signo = 0;
934 collect64.si_code = 0;
935 collect64.si_errno = 0;
936 collect64.si_pid = 0;
937 collect64.si_uid = 0;
938 collect64.si_addr = 0;
939 collect64.si_status = p->p_xstat;
940 collect64.si_band = 0;
941
942 if (IS_64BIT_PROCESS(p)) {
943 error = copyout((caddr_t)&collect64,
944 uap->infop,
945 sizeof(collect64));
946 } else {
947 siginfo_t collect;
948 siginfo_64to32(&collect64,&collect);
949 error = copyout((caddr_t)&collect,
950 uap->infop,
951 sizeof(collect));
952 }
953 /* information unavailable? */
954 if (error) {
955 p->p_lflag &= ~P_LWAITING;
956 wakeup(&p->p_stat);
957 return (error);
958 }
959
960 /* Prevent other process for waiting for this event? */
961 if (!(uap->options & WNOWAIT)) {
962 p->p_flag &= ~P_CONTINUED;
963 }
964
965 p->p_lflag &= ~P_LWAITING;
966 wakeup(&p->p_stat);
967 return (0);
968
969 break;
970 }
971
972
973 /* Not a process we are interested in; go on to next child */
974 p->p_lflag &= ~P_LWAITING;
975 wakeup(&p->p_stat);
976 }
977
978 /* No child processes that could possibly satisfy the request? */
979 if (nfound == 0)
980 return (ECHILD);
981
982 if (uap->options & WNOHANG) {
983 retval[0] = 0;
984 return (0);
985 }
986
987 if ((error = tsleep0((caddr_t)q, PWAIT | PCATCH, "waitid", 0, waitidcontinue)))
988 return (error);
989
990 goto loop;
991}
992
993/*
994 * make process 'parent' the new parent of process 'child'.
995 */
996void
997proc_reparent(struct proc *child, struct proc *parent)
998{
999
1000 if (child->p_pptr == parent)
1001 return;
1002
1003 LIST_REMOVE(child, p_sibling);
1004 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1005 child->p_pptr = parent;
1006
1007 if (initproc == parent && child->p_stat == SZOMB)
1008 psignal(initproc, SIGCHLD);
1009}
1010
1011/*
1012 * Make the current process an "init" process, meaning
1013 * that it doesn't have a parent, and that it won't be
1014 * gunned down by kill(-1, 0).
1015 */
1016kern_return_t
1017init_process(__unused struct init_process_args *args)
1018{
1019 register struct proc *p = current_proc();
1020
1021 AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS);
1022 if (suser(kauth_cred_get(), &p->p_acflag)) {
1023 AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS);
1024 return(KERN_NO_ACCESS);
1025 }
1026
1027 if (p->p_pid != 1 && p->p_pgid != p->p_pid)
1028 enterpgrp(p, p->p_pid, 0);
1029 p->p_flag |= P_SYSTEM;
1030
1031 /*
1032 * Take us out of the sibling chain, and
1033 * out of our parent's child chain.
1034 */
1035 LIST_REMOVE(p, p_sibling);
1036 p->p_sibling.le_prev = NULL;
1037 p->p_sibling.le_next = NULL;
1038 p->p_pptr = kernproc;
1039
1040 AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS);
1041 return(KERN_SUCCESS);
1042}
1043
1044
1045/*
1046 * Exit: deallocate address space and other resources, change proc state
1047 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1048 * status and rusage for wait(). Check for child processes and orphan them.
1049 */
1050
1051void
1052vfork_exit(struct proc *p, int rv)
1053{
1054 thread_t self = current_thread();
1055#ifdef FIXME
1056 struct task *task = p->task;
1057#endif
1058 register int s;
1059 struct uthread *ut;
1060 exception_data_t code[EXCEPTION_CODE_MAX];
1061
1062 /*
1063 * If a thread in this task has already
1064 * called exit(), then halt any others
1065 * right here.
1066 */
1067
1068 ut = get_bsdthread_info(self);
1069#ifdef FIXME
1070 signal_lock(p);
1071 while (p->exit_thread != self) {
1072 if (sig_try_locked(p) <= 0) {
1073 if (get_threadtask(self) != task) {
1074 signal_unlock(p);
1075 return;
1076 }
1077 signal_unlock(p);
1078 thread_terminate(self);
1079 thread_funnel_set(kernel_flock, FALSE);
1080 thread_exception_return();
1081 /* NOTREACHED */
1082 }
1083 sig_lock_to_exit(p);
1084 }
1085 signal_unlock(p);
1086 if (p->p_pid == 1) {
1087 printf("pid 1 exited (signal %d, exit %d)",
1088 WTERMSIG(rv), WEXITSTATUS(rv));
1089panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data);
1090 }
1091#endif /* FIXME */
1092
1093 s = splsched();
1094 p->p_flag |= P_WEXIT;
1095 p->p_lflag |= P_LPEXIT;
1096 splx(s);
1097
1098 code[0] = (exception_data_t)0xFF000001; /* Set terminate code */
1099 code[1] = (exception_data_t)p->p_pid; /* Pass out the pid */
1100 /* Notify the perf server */
1101 (void)sys_perf_notify(p->task, (exception_data_t)&code, 2);
1102
1103 /*
1104 * Remove proc from allproc queue and from pidhash chain.
1105 * Need to do this before we do anything that can block.
1106 * Not doing causes things like mount() find this on allproc
1107 * in partially cleaned state.
1108 */
1109 LIST_REMOVE(p, p_list);
1110 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
1111 LIST_REMOVE(p, p_hash);
1112 /*
1113 * If parent is waiting for us to exit or exec,
1114 * P_PPWAIT is set; we will wakeup the parent below.
1115 */
1116 p->p_flag &= ~(P_TRACED | P_PPWAIT);
1117 p->p_sigignore = ~0;
1118 p->p_siglist = 0;
1119
1120 ut->uu_siglist = 0;
1121 untimeout(realitexpire, (caddr_t)p->p_pid);
1122
1123 p->p_xstat = rv;
1124
1125 vproc_exit(p);
1126}
1127
1128void
1129vproc_exit(struct proc *p)
1130{
1131 register struct proc *q, *nq, *pp;
1132#ifdef FIXME
1133 struct task *task = p->task;
1134#endif
1135
1136 /* XXX Zombie allocation may fail, in which case stats get lost */
1137 MALLOC_ZONE(p->p_ru, struct rusage *,
1138 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
1139
1140 /*
1141 * Close open files and release open-file table.
1142 * This may block!
1143 */
1144 fdfree(p);
1145
1146 if (SESS_LEADER(p)) {
1147 register struct session *sp = p->p_session;
1148
1149 if (sp->s_ttyvp) {
1150 struct vnode *ttyvp;
1151 struct vfs_context context;
1152
1153 /*
1154 * Controlling process.
1155 * Signal foreground pgrp,
1156 * drain controlling terminal
1157 * and revoke access to controlling terminal.
1158 */
1159 if (sp->s_ttyp->t_session == sp) {
1160 if (sp->s_ttyp->t_pgrp)
1161 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1162 (void) ttywait(sp->s_ttyp);
1163 /*
1164 * The tty could have been revoked
1165 * if we blocked.
1166 */
1167 context.vc_proc = p;
1168 context.vc_ucred = p->p_ucred;
1169 if (sp->s_ttyvp)
1170 VNOP_REVOKE(sp->s_ttyvp, REVOKEALL, &context);
1171 }
1172 ttyvp = sp->s_ttyvp;
1173 sp->s_ttyvp = NULL;
1174 if (ttyvp) {
1175 vnode_rele(ttyvp);
1176 }
1177 /*
1178 * s_ttyp is not zero'd; we use this to indicate
1179 * that the session once had a controlling terminal.
1180 * (for logging and informational purposes)
1181 */
1182 }
1183 sp->s_leader = NULL;
1184 }
1185
1186 fixjobc(p, p->p_pgrp, 0);
1187 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
1188
1189#if KTRACE
1190 /*
1191 * release trace file
1192 */
1193 p->p_traceflag = 0; /* don't trace the vnode_rele() */
1194 if (p->p_tracep) {
1195 struct vnode *tvp = p->p_tracep;
1196 p->p_tracep = NULL;
1197 vnode_rele(tvp);
1198 }
1199#endif
1200
1201 while (q = p->p_children.lh_first) {
1202 proc_reparent(q, initproc);
1203 /*
1204 * Traced processes are killed
1205 * since their existence means someone is messing up.
1206 */
1207 if (q->p_flag & P_TRACED) {
1208 q->p_flag &= ~P_TRACED;
1209 if (q->sigwait_thread) {
1210 /*
1211 * The sigwait_thread could be stopped at a
1212 * breakpoint. Wake it up to kill.
1213 * Need to do this as it could be a thread which is not
1214 * the first thread in the task. So any attempts to kill
1215 * the process would result into a deadlock on q->sigwait.
1216 */
1217 thread_resume((thread_t)q->sigwait_thread);
1218 clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
1219 threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
1220 }
1221 psignal(q, SIGKILL);
1222 }
1223 }
1224
1225 /*
1226 * Save exit status and final rusage info, adding in child rusage
1227 * info and self times. If we were unable to allocate a zombie
1228 * structure, this information is lost.
1229 */
1230 if (p->p_ru != NULL) {
1231 *p->p_ru = p->p_stats->p_ru;
1232 timerclear(&p->p_ru->ru_utime);
1233 timerclear(&p->p_ru->ru_stime);
1234
1235#ifdef FIXME
1236 if (task) {
1237 task_basic_info_data_t tinfo;
1238 task_thread_times_info_data_t ttimesinfo;
1239 int task_info_stuff, task_ttimes_stuff;
1240 struct timeval ut,st;
1241
1242 task_info_stuff = TASK_BASIC_INFO_COUNT;
1243 task_info(task, TASK_BASIC_INFO,
1244 &tinfo, &task_info_stuff);
1245 p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
1246 p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
1247 p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
1248 p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
1249
1250 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
1251 task_info(task, TASK_THREAD_TIMES_INFO,
1252 &ttimesinfo, &task_ttimes_stuff);
1253
1254 ut.tv_sec = ttimesinfo.user_time.seconds;
1255 ut.tv_usec = ttimesinfo.user_time.microseconds;
1256 st.tv_sec = ttimesinfo.system_time.seconds;
1257 st.tv_usec = ttimesinfo.system_time.microseconds;
1258 timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
1259 timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
1260 }
1261#endif /* FIXME */
1262
1263 ruadd(p->p_ru, &p->p_stats->p_cru);
1264 }
1265
1266 /*
1267 * Free up profiling buffers.
1268 */
1269 {
1270 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
1271
1272 p1 = p0->pr_next;
1273 p0->pr_next = NULL;
1274 p0->pr_scale = 0;
1275
1276 for (; p1 != NULL; p1 = pn) {
1277 pn = p1->pr_next;
1278 kfree(p1, sizeof *p1);
1279 }
1280 }
1281
1282 /*
1283 * Other substructures are freed from wait().
1284 */
1285 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
1286 p->p_stats = NULL;
1287
1288 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
1289 p->p_sigacts = NULL;
1290
1291 if (--p->p_limit->p_refcnt == 0)
1292 FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
1293 p->p_limit = NULL;
1294
1295 /*
1296 * Finish up by terminating the task
1297 * and halt this thread (only if a
1298 * member of the task exiting).
1299 */
1300 p->task = TASK_NULL;
1301
1302 /*
1303 * Notify parent that we're gone.
1304 */
1305 pp = p->p_pptr;
1306 if (pp != initproc) {
1307 pp->si_pid = p->p_pid;
1308 pp->si_status = p->p_xstat;
1309 pp->si_code = CLD_EXITED;
1310 pp->si_uid = p->p_ucred->cr_ruid;
1311 }
1312 /* mark as a zombie */
1313 p->p_stat = SZOMB;
1314
1315 psignal(p->p_pptr, SIGCHLD);
1316
1317 /* and now wakeup the parent */
1318 wakeup((caddr_t)p->p_pptr);
1319}
1320
1321
1322/*
1323 * munge_rusage
1324 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1325 * process. We munge the kernel (32 bit) version of rusage into the
1326 * 64 bit version.
1327 */
1328__private_extern__ void
1329munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p)
1330{
1331 /* timeval changes size, so utime and stime need special handling */
1332 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
1333 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
1334 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
1335 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
1336 /*
1337 * everything else can be a direct assign, since there is no loss
1338 * of precision implied boing 32->64.
1339 */
1340 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
1341 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
1342 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
1343 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
1344 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
1345 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
1346 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
1347 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
1348 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
1349 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
1350 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
1351 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
1352 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
1353 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
1354}