]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_exit.c
385c637111c6f63c7db3480eb1b08e7cf1e964d0
[apple/xnu.git] / bsd / kern / kern_exit.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
61 */
62
63 #include <machine/reg.h>
64 #include <machine/psl.h>
65
66 #include "compat_43.h"
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/ioctl.h>
71 #include <sys/proc.h>
72 #include <sys/tty.h>
73 #include <sys/time.h>
74 #include <sys/resource.h>
75 #include <sys/kernel.h>
76 #include <sys/buf.h>
77 #include <sys/wait.h>
78 #include <sys/file.h>
79 #include <sys/vnode.h>
80 #include <sys/syslog.h>
81 #include <sys/malloc.h>
82 #include <sys/resourcevar.h>
83 #include <sys/ptrace.h>
84 #include <sys/user.h>
85
86 #include <mach/mach_types.h>
87 #include <kern/thread.h>
88 #include <kern/thread_act.h>
89 #include <kern/assert.h>
90
91 extern char init_task_failure_data[];
92 void exit1 __P((struct proc *, int));
93
94 /*
95 * exit --
96 * Death of process.
97 */
98 struct exit_args {
99 int rval;
100 };
101 void
102 exit(p, uap, retval)
103 struct proc *p;
104 struct exit_args *uap;
105 int *retval;
106 {
107 exit1(p, W_EXITCODE(uap->rval, 0));
108
109 /* drop funnel befewo we return */
110 thread_funnel_set(kernel_flock, FALSE);
111 thread_exception_return();
112 /* NOTREACHED */
113 while (TRUE)
114 thread_block(0);
115 /* NOTREACHED */
116 }
117
118 /*
119 * Exit: deallocate address space and other resources, change proc state
120 * to zombie, and unlink proc from allproc and parent's lists. Save exit
121 * status and rusage for wait(). Check for child processes and orphan them.
122 */
123 void
124 exit1(p, rv)
125 register struct proc *p;
126 int rv;
127 {
128 register struct proc *q, *nq;
129 thread_t self = current_thread();
130 thread_act_t th_act_self = current_act();
131 struct task *task = p->task;
132 register int i,s;
133 struct uthread *ut;
134
135 /*
136 * If a thread in this task has already
137 * called exit(), then halt any others
138 * right here.
139 */
140 signal_lock(p);
141 while (p->exit_thread != self) {
142 if (sig_try_locked(p) <= 0) {
143 if (get_threadtask(th_act_self) != task) {
144 signal_unlock(p);
145 return;
146 }
147 signal_unlock(p);
148 thread_terminate(th_act_self);
149 thread_funnel_set(kernel_flock, FALSE);
150 thread_exception_return();
151 /* NOTREACHED */
152 }
153 sig_lock_to_exit(p);
154 }
155 signal_unlock(p);
156 if (p->p_pid == 1) {
157 printf("pid 1 exited (signal %d, exit %d)",
158 WTERMSIG(rv), WEXITSTATUS(rv));
159 panic("init died\nState at Last Exception:\n\n%s",
160 init_task_failure_data);
161 }
162
163 s = splsched();
164 p->p_flag |= P_WEXIT;
165 splx(s);
166 proc_prepareexit(p);
167 p->p_xstat = rv;
168
169 /* task terminate will call proc_terminate and that cleans it up */
170 task_terminate_internal(task);
171
172 /*
173 * we come back and returns to AST which
174 * should cleanup the rest
175 */
176 #if 0
177 if (task == current_task()) {
178 thread_exception_return();
179 /*NOTREACHED*/
180 }
181
182 while (task == current_task()) {
183 thread_terminate_self();
184 /*NOTREACHED*/
185 }
186 #endif
187 }
188
189 void
190 proc_prepareexit(struct proc *p)
191 {
192 int s;
193 struct uthread *ut;
194 thread_t self = current_thread();
195 thread_act_t th_act_self = current_act();
196
197
198 /*
199 * Remove proc from allproc queue and from pidhash chain.
200 * Need to do this before we do anything that can block.
201 * Not doing causes things like mount() find this on allproc
202 * in partially cleaned state.
203 */
204 LIST_REMOVE(p, p_list);
205 LIST_REMOVE(p, p_hash);
206
207 #ifdef PGINPROF
208 vmsizmon();
209 #endif
210 /*
211 * If parent is waiting for us to exit or exec,
212 * P_PPWAIT is set; we will wakeup the parent below.
213 */
214 p->p_flag &= ~(P_TRACED | P_PPWAIT);
215 p->p_sigignore = ~0;
216 p->p_siglist = 0;
217 ut = get_bsdthread_info(th_act_self);
218 ut->uu_sig = 0;
219 untimeout(realitexpire, (caddr_t)p);
220
221 }
222
223 void
224 proc_exit(struct proc *p)
225 {
226 register struct proc *q, *nq;
227 thread_t self = current_thread();
228 thread_act_t th_act_self = current_act();
229 struct task *task = p->task;
230 register int i,s;
231 struct uthread *ut;
232 boolean_t funnel_state;
233
234 /* This can happen if thread_terminate of the single thread
235 * process
236 */
237
238 funnel_state = thread_funnel_set(kernel_flock, TRUE);
239 if( !(p->p_flag & P_WEXIT)) {
240 s = splsched();
241 p->p_flag |= P_WEXIT;
242 splx(s);
243 proc_prepareexit(p);
244 }
245
246 MALLOC_ZONE(p->p_ru, struct rusage *,
247 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
248
249 /*
250 * Close open files and release open-file table.
251 * This may block!
252 */
253 fdfree(p);
254
255 /* Close ref SYSV Shared memory*/
256 if (p->vm_shm)
257 shmexit(p);
258
259 if (SESS_LEADER(p)) {
260 register struct session *sp = p->p_session;
261
262 if (sp->s_ttyvp) {
263 /*
264 * Controlling process.
265 * Signal foreground pgrp,
266 * drain controlling terminal
267 * and revoke access to controlling terminal.
268 */
269 if (sp->s_ttyp->t_session == sp) {
270 if (sp->s_ttyp->t_pgrp)
271 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
272 (void) ttywait(sp->s_ttyp);
273 /*
274 * The tty could have been revoked
275 * if we blocked.
276 */
277 if (sp->s_ttyvp)
278 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
279 }
280 if (sp->s_ttyvp)
281 vrele(sp->s_ttyvp);
282 sp->s_ttyvp = NULL;
283 /*
284 * s_ttyp is not zero'd; we use this to indicate
285 * that the session once had a controlling terminal.
286 * (for logging and informational purposes)
287 */
288 }
289 sp->s_leader = NULL;
290 }
291
292 fixjobc(p, p->p_pgrp, 0);
293 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
294 #if KTRACE
295 /*
296 * release trace file
297 */
298 p->p_traceflag = 0; /* don't trace the vrele() */
299 if (p->p_tracep)
300 vrele(p->p_tracep);
301 #endif
302
303
304 q = p->p_children.lh_first;
305 if (q) /* only need this if any child is S_ZOMB */
306 wakeup((caddr_t) initproc);
307 for (; q != 0; q = nq) {
308 nq = q->p_sibling.le_next;
309 proc_reparent(q, initproc);
310 /*
311 * Traced processes are killed
312 * since their existence means someone is messing up.
313 */
314 if (q->p_flag & P_TRACED) {
315 q->p_flag &= ~P_TRACED;
316 if (q->sigwait_thread) {
317 thread_t sig_shuttle = getshuttle_thread(q->sigwait_thread);
318 /*
319 * The sigwait_thread could be stopped at a
320 * breakpoint. Wake it up to kill.
321 * Need to do this as it could be a thread which is not
322 * the first thread in the task. So any attempts to kill
323 * the process would result into a deadlock on q->sigwait.
324 */
325 thread_resume((struct thread *)q->sigwait_thread);
326 clear_wait(sig_shuttle, THREAD_INTERRUPTED);
327 threadsignal(q->sigwait_thread, SIGKILL, 0);
328 }
329 psignal(q, SIGKILL);
330 }
331 }
332
333
334 /*
335 * Save exit status and final rusage info, adding in child rusage
336 * info and self times.
337 */
338 *p->p_ru = p->p_stats->p_ru;
339
340 timerclear(&p->p_ru->ru_utime);
341 timerclear(&p->p_ru->ru_stime);
342
343 if (task) {
344 task_basic_info_data_t tinfo;
345 task_thread_times_info_data_t ttimesinfo;
346 int task_info_stuff, task_ttimes_stuff;
347 struct timeval ut,st;
348
349 task_info_stuff = TASK_BASIC_INFO_COUNT;
350 task_info(task, TASK_BASIC_INFO,
351 &tinfo, &task_info_stuff);
352 p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
353 p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
354 p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
355 p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
356
357 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
358 task_info(task, TASK_THREAD_TIMES_INFO,
359 &ttimesinfo, &task_ttimes_stuff);
360
361 ut.tv_sec = ttimesinfo.user_time.seconds;
362 ut.tv_usec = ttimesinfo.user_time.microseconds;
363 st.tv_sec = ttimesinfo.system_time.seconds;
364 st.tv_usec = ttimesinfo.system_time.microseconds;
365 timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
366 timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
367 }
368
369
370 ruadd(p->p_ru, &p->p_stats->p_cru);
371
372 /*
373 * Free up profiling buffers.
374 */
375 {
376 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
377
378 p1 = p0->pr_next;
379 p0->pr_next = NULL;
380 p0->pr_scale = 0;
381
382 for (; p1 != NULL; p1 = pn) {
383 pn = p1->pr_next;
384 kfree((vm_offset_t)p1, sizeof *p1);
385 }
386 }
387
388 /*
389 * Other substructures are freed from wait().
390 */
391 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
392 p->p_stats = NULL;
393
394 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
395 p->p_sigacts = NULL;
396
397 if (--p->p_limit->p_refcnt == 0)
398 FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
399 p->p_limit = NULL;
400
401 /*
402 * Finish up by terminating the task
403 * and halt this thread (only if a
404 * member of the task exiting).
405 */
406 p->task = TASK_NULL;
407 //task->proc = NULL;
408 set_bsdtask_info(task, NULL);
409
410 /*
411 * Notify parent that we're gone.
412 */
413 psignal(p->p_pptr, SIGCHLD);
414
415 /* Place onto zombproc. */
416 LIST_INSERT_HEAD(&zombproc, p, p_list);
417 p->p_stat = SZOMB;
418
419 /* and now wakeup the parent */
420 wakeup((caddr_t)p->p_pptr);
421
422 (void) thread_funnel_set(kernel_flock, funnel_state);
423 }
424
425
426 struct wait4_args {
427 int pid;
428 int *status;
429 int options;
430 struct rusage *rusage;
431 };
432
433 #if COMPAT_43
434 int
435 owait(p, uap, retval)
436 struct proc *p;
437 void *uap;
438 int *retval;
439 {
440 struct wait4_args *a;
441
442 a = (struct wait4_args *)get_bsduthreadarg(current_act());
443
444 a->options = 0;
445 a->rusage = NULL;
446 a->pid = WAIT_ANY;
447 a->status = NULL;
448 return (wait1(p, a, retval, 1));
449 }
450
451 int
452 wait4(p, uap, retval)
453 struct proc *p;
454 struct wait4_args *uap;
455 int *retval;
456 {
457
458 return (wait1(p, uap, retval, 0));
459 }
460
461 struct owait3_args {
462 int *status;
463 int options;
464 struct rusage *rusage;
465 };
466
467 int
468 owait3(p, uap, retval)
469 struct proc *p;
470 struct owait3_args *uap;
471 int *retval;
472 {
473 struct wait4_args *a;
474
475 a = (struct wait4_args *)get_bsduthreadarg(current_act);
476
477 a->rusage = uap->rusage;
478 a->options = uap->options;
479 a->status = uap->status;
480 a->pid = WAIT_ANY;
481
482 return (wait1(p, a, retval, 1));
483 }
484
485 #else
486 #define wait1 wait4
487 #endif
488
489 int
490 wait1continue(result)
491 {
492 void *vt;
493 thread_act_t thread;
494 struct uthread *ut;
495 int *retval;
496 struct proc *p;
497
498 p = get_bsdtask_info(current_task());
499 p->p_flag &= ~P_WAITING;
500
501 if (result != 0) {
502 return(result);
503 }
504
505 thread = current_act();
506 ut = get_bsdthread_info(thread);
507 vt = get_bsduthreadarg(thread);
508 retval = get_bsduthreadrval(thread);
509 wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0);
510 }
511
512 int
513 wait1(q, uap, retval, compat)
514 register struct proc *q;
515 register struct wait4_args *uap;
516 register_t *retval;
517 #if COMPAT_43
518 int compat;
519 #endif
520 {
521 register int nfound;
522 register struct proc *p, *t;
523 int status, error;
524
525
526 #if 0
527 /* since we are funneled we don't need to do this atomically, yet */
528 if (q->p_flag & P_WAITING) {
529 return(EINVAL);
530 }
531 q->p_flag |= P_WAITING; /* only allow single thread to wait() */
532 #endif
533
534 if (uap->pid == 0)
535 uap->pid = -q->p_pgid;
536
537 loop:
538 nfound = 0;
539 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
540 if (uap->pid != WAIT_ANY &&
541 p->p_pid != uap->pid &&
542 p->p_pgid != -(uap->pid))
543 continue;
544 nfound++;
545 if (p->p_stat == SZOMB) {
546 retval[0] = p->p_pid;
547 #if COMPAT_43
548 if (compat)
549 retval[1] = p->p_xstat;
550 else
551 #endif
552 if (uap->status) {
553 status = p->p_xstat; /* convert to int */
554 if (error = copyout((caddr_t)&status,
555 (caddr_t)uap->status,
556 sizeof(status))) {
557 q->p_flag &= ~P_WAITING;
558 return (error);
559 }
560 }
561 if (uap->rusage &&
562 (error = copyout((caddr_t)p->p_ru,
563 (caddr_t)uap->rusage,
564 sizeof (struct rusage)))) {
565 q->p_flag &= ~P_WAITING;
566 return (error);
567 }
568 /*
569 * If we got the child via a ptrace 'attach',
570 * we need to give it back to the old parent.
571 */
572 if (p->p_oppid && (t = pfind(p->p_oppid))) {
573 p->p_oppid = 0;
574 proc_reparent(p, t);
575 psignal(t, SIGCHLD);
576 wakeup((caddr_t)t);
577 q->p_flag &= ~P_WAITING;
578 return (0);
579 }
580 p->p_xstat = 0;
581 if (p->p_ru) {
582 ruadd(&q->p_stats->p_cru, p->p_ru);
583 FREE_ZONE(p->p_ru, sizeof *p->p_ru, M_ZOMBIE);
584 p->p_ru = NULL;
585 } else {
586 printf("Warning : lost p_ru for %s\n", p->p_comm);
587 }
588
589 /*
590 * Decrement the count of procs running with this uid.
591 */
592 (void)chgproccnt(p->p_cred->p_ruid, -1);
593
594 /*
595 * Free up credentials.
596 */
597 if (--p->p_cred->p_refcnt == 0) {
598 struct ucred *ucr = p->p_ucred;
599 struct pcred *pcr;
600
601 if (ucr != NOCRED) {
602 p->p_ucred = NOCRED;
603 crfree(ucr);
604 }
605 pcr = p->p_cred;
606 p->p_cred = NULL;
607 FREE_ZONE(pcr, sizeof *pcr, M_SUBPROC);
608 }
609
610 /*
611 * Release reference to text vnode
612 */
613 if (p->p_textvp)
614 vrele(p->p_textvp);
615
616 /*
617 * Finally finished with old proc entry.
618 * Unlink it from its process group and free it.
619 */
620 leavepgrp(p);
621 LIST_REMOVE(p, p_list); /* off zombproc */
622 LIST_REMOVE(p, p_sibling);
623 FREE_ZONE(p, sizeof *p, M_PROC);
624 nprocs--;
625 q->p_flag &= ~P_WAITING;
626 return (0);
627 }
628 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
629 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
630 p->p_flag |= P_WAITED;
631 retval[0] = p->p_pid;
632 #if COMPAT_43
633 if (compat) {
634 retval[1] = W_STOPCODE(p->p_xstat);
635 error = 0;
636 } else
637 #endif
638 if (uap->status) {
639 status = W_STOPCODE(p->p_xstat);
640 error = copyout((caddr_t)&status,
641 (caddr_t)uap->status,
642 sizeof(status));
643 } else
644 error = 0;
645 q->p_flag &= ~P_WAITING;
646 return (error);
647 }
648 }
649 if (nfound == 0) {
650 q->p_flag &= ~P_WAITING;
651 return (ECHILD);
652 }
653 if (uap->options & WNOHANG) {
654 retval[0] = 0;
655 q->p_flag &= ~P_WAITING;
656 return (0);
657 }
658
659 if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue)) {
660 q->p_flag &= ~P_WAITING;
661 return (error);
662 }
663 goto loop;
664 }
665
666 /*
667 * make process 'parent' the new parent of process 'child'.
668 */
669 void
670 proc_reparent(child, parent)
671 register struct proc *child;
672 register struct proc *parent;
673 {
674
675 if (child->p_pptr == parent)
676 return;
677
678 LIST_REMOVE(child, p_sibling);
679 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
680 child->p_pptr = parent;
681 }
682
683 kern_return_t
684 init_process(void)
685 /*
686 * Make the current process an "init" process, meaning
687 * that it doesn't have a parent, and that it won't be
688 * gunned down by kill(-1, 0).
689 */
690 {
691 register struct proc *p = current_proc();
692
693 if (suser(p->p_ucred, &p->p_acflag))
694 return(KERN_NO_ACCESS);
695
696 if (p->p_pid != 1 && p->p_pgid != p->p_pid)
697 enterpgrp(p, p->p_pid, 0);
698 p->p_flag |= P_SYSTEM;
699
700 /*
701 * Take us out of the sibling chain, and
702 * out of our parent's child chain.
703 */
704 LIST_REMOVE(p, p_sibling);
705 p->p_sibling.le_prev = NULL;
706 p->p_sibling.le_next = NULL;
707 p->p_pptr = kernproc;
708
709 return(KERN_SUCCESS);
710 }
711
712 void
713 process_terminate_self(void)
714 {
715 struct proc *p = current_proc();
716
717 if (p != NULL) {
718 exit1(p, W_EXITCODE(0, SIGKILL));
719 /*NOTREACHED*/
720 }
721 }