]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_exit.c
84c51a09c7dbb7db0435ce8c41eda14f8ebdf9a5
[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 int exit1 __P((struct proc *, int, 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), retval);
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 int
124 exit1(p, rv, retval)
125 register struct proc *p;
126 int rv;
127 int * retval;
128 {
129 register struct proc *q, *nq;
130 thread_t self = current_thread();
131 thread_act_t th_act_self = current_act();
132 struct task *task = p->task;
133 register int i,s;
134 struct uthread *ut;
135
136 /*
137 * If a thread in this task has already
138 * called exit(), then halt any others
139 * right here.
140 */
141
142 ut = get_bsdthread_info(th_act_self);
143 if (ut->uu_flag & P_VFORK) {
144 vfork_exit(p, rv);
145 vfork_return(th_act_self, p->p_pptr, p , retval);
146 unix_syscall_return(0);
147 /* NOT REACHED */
148 }
149 signal_lock(p);
150 while (p->exit_thread != self) {
151 if (sig_try_locked(p) <= 0) {
152 if (get_threadtask(th_act_self) != task) {
153 signal_unlock(p);
154 return(0);
155 }
156 signal_unlock(p);
157 thread_terminate(th_act_self);
158 thread_funnel_set(kernel_flock, FALSE);
159 thread_exception_return();
160 /* NOTREACHED */
161 }
162 sig_lock_to_exit(p);
163 }
164 signal_unlock(p);
165 if (p->p_pid == 1) {
166 printf("pid 1 exited (signal %d, exit %d)",
167 WTERMSIG(rv), WEXITSTATUS(rv));
168 panic("init died\nState at Last Exception:\n\n%s",
169 init_task_failure_data);
170 }
171
172 s = splsched();
173 p->p_flag |= P_WEXIT;
174 splx(s);
175 proc_prepareexit(p);
176 p->p_xstat = rv;
177
178 /* task terminate will call proc_terminate and that cleans it up */
179 task_terminate_internal(task);
180
181 /*
182 * we come back and returns to AST which
183 * should cleanup the rest
184 */
185 #if 0
186 if (task == current_task()) {
187 thread_exception_return();
188 /*NOTREACHED*/
189 }
190
191 while (task == current_task()) {
192 thread_terminate_self();
193 /*NOTREACHED*/
194 }
195 #endif
196 return(0);
197 }
198
199 void
200 proc_prepareexit(struct proc *p)
201 {
202 int s;
203 struct uthread *ut;
204 thread_t self = current_thread();
205 thread_act_t th_act_self = current_act();
206
207 /*
208 * Remove proc from allproc queue and from pidhash chain.
209 * Need to do this before we do anything that can block.
210 * Not doing causes things like mount() find this on allproc
211 * in partially cleaned state.
212 */
213 LIST_REMOVE(p, p_list);
214 LIST_REMOVE(p, p_hash);
215
216 #ifdef PGINPROF
217 vmsizmon();
218 #endif
219 /*
220 * If parent is waiting for us to exit or exec,
221 * P_PPWAIT is set; we will wakeup the parent below.
222 */
223 p->p_flag &= ~(P_TRACED | P_PPWAIT);
224 p->p_sigignore = ~0;
225 p->p_siglist = 0;
226 ut = get_bsdthread_info(th_act_self);
227 ut->uu_sig = 0;
228 untimeout(realitexpire, (caddr_t)p);
229 }
230
231 void
232 proc_exit(struct proc *p)
233 {
234 register struct proc *q, *nq;
235 thread_t self = current_thread();
236 thread_act_t th_act_self = current_act();
237 struct task *task = p->task;
238 register int i,s;
239 boolean_t funnel_state;
240
241 /* This can happen if thread_terminate of the single thread
242 * process
243 */
244
245 funnel_state = thread_funnel_set(kernel_flock, TRUE);
246 if( !(p->p_flag & P_WEXIT)) {
247 s = splsched();
248 p->p_flag |= P_WEXIT;
249 splx(s);
250 proc_prepareexit(p);
251 }
252
253 MALLOC_ZONE(p->p_ru, struct rusage *,
254 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
255
256 /*
257 * Close open files and release open-file table.
258 * This may block!
259 */
260 fdfree(p);
261
262 /* Close ref SYSV Shared memory*/
263 if (p->vm_shm)
264 shmexit(p);
265
266 if (SESS_LEADER(p)) {
267 register struct session *sp = p->p_session;
268
269 if (sp->s_ttyvp) {
270 /*
271 * Controlling process.
272 * Signal foreground pgrp,
273 * drain controlling terminal
274 * and revoke access to controlling terminal.
275 */
276 if (sp->s_ttyp->t_session == sp) {
277 if (sp->s_ttyp->t_pgrp)
278 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
279 (void) ttywait(sp->s_ttyp);
280 /*
281 * The tty could have been revoked
282 * if we blocked.
283 */
284 if (sp->s_ttyvp)
285 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
286 }
287 if (sp->s_ttyvp)
288 vrele(sp->s_ttyvp);
289 sp->s_ttyvp = NULL;
290 /*
291 * s_ttyp is not zero'd; we use this to indicate
292 * that the session once had a controlling terminal.
293 * (for logging and informational purposes)
294 */
295 }
296 sp->s_leader = NULL;
297 }
298
299 fixjobc(p, p->p_pgrp, 0);
300 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
301 #if KTRACE
302 /*
303 * release trace file
304 */
305 p->p_traceflag = 0; /* don't trace the vrele() */
306 if (p->p_tracep)
307 vrele(p->p_tracep);
308 #endif
309
310
311 q = p->p_children.lh_first;
312 if (q) /* only need this if any child is S_ZOMB */
313 wakeup((caddr_t) initproc);
314 for (; q != 0; q = nq) {
315 nq = q->p_sibling.le_next;
316 proc_reparent(q, initproc);
317 /*
318 * Traced processes are killed
319 * since their existence means someone is messing up.
320 */
321 if (q->p_flag & P_TRACED) {
322 q->p_flag &= ~P_TRACED;
323 if (q->sigwait_thread) {
324 thread_t sig_shuttle = getshuttle_thread(q->sigwait_thread);
325 /*
326 * The sigwait_thread could be stopped at a
327 * breakpoint. Wake it up to kill.
328 * Need to do this as it could be a thread which is not
329 * the first thread in the task. So any attempts to kill
330 * the process would result into a deadlock on q->sigwait.
331 */
332 thread_resume((struct thread *)q->sigwait_thread);
333 clear_wait(sig_shuttle, THREAD_INTERRUPTED);
334 threadsignal(q->sigwait_thread, SIGKILL, 0);
335 }
336 psignal(q, SIGKILL);
337 }
338 }
339
340 /*
341 * Save exit status and final rusage info, adding in child rusage
342 * info and self times.
343 */
344 *p->p_ru = p->p_stats->p_ru;
345
346 timerclear(&p->p_ru->ru_utime);
347 timerclear(&p->p_ru->ru_stime);
348
349 if (task) {
350 task_basic_info_data_t tinfo;
351 task_thread_times_info_data_t ttimesinfo;
352 int task_info_stuff, task_ttimes_stuff;
353 struct timeval ut,st;
354
355 task_info_stuff = TASK_BASIC_INFO_COUNT;
356 task_info(task, TASK_BASIC_INFO,
357 &tinfo, &task_info_stuff);
358 p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
359 p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
360 p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
361 p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
362
363 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
364 task_info(task, TASK_THREAD_TIMES_INFO,
365 &ttimesinfo, &task_ttimes_stuff);
366
367 ut.tv_sec = ttimesinfo.user_time.seconds;
368 ut.tv_usec = ttimesinfo.user_time.microseconds;
369 st.tv_sec = ttimesinfo.system_time.seconds;
370 st.tv_usec = ttimesinfo.system_time.microseconds;
371 timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
372 timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
373 }
374
375 ruadd(p->p_ru, &p->p_stats->p_cru);
376
377 /*
378 * Free up profiling buffers.
379 */
380 {
381 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
382
383 p1 = p0->pr_next;
384 p0->pr_next = NULL;
385 p0->pr_scale = 0;
386
387 for (; p1 != NULL; p1 = pn) {
388 pn = p1->pr_next;
389 kfree((vm_offset_t)p1, sizeof *p1);
390 }
391 }
392
393 /*
394 * Other substructures are freed from wait().
395 */
396 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
397 p->p_stats = NULL;
398
399 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
400 p->p_sigacts = NULL;
401
402 if (--p->p_limit->p_refcnt == 0)
403 FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
404 p->p_limit = NULL;
405
406 /*
407 * Finish up by terminating the task
408 * and halt this thread (only if a
409 * member of the task exiting).
410 */
411 p->task = TASK_NULL;
412 //task->proc = NULL;
413 set_bsdtask_info(task, NULL);
414
415 /*
416 * Notify parent that we're gone.
417 */
418 psignal(p->p_pptr, SIGCHLD);
419
420 /* Place onto zombproc. */
421 LIST_INSERT_HEAD(&zombproc, p, p_list);
422 p->p_stat = SZOMB;
423
424 /* and now wakeup the parent */
425 wakeup((caddr_t)p->p_pptr);
426
427 (void) thread_funnel_set(kernel_flock, funnel_state);
428 }
429
430
431 struct wait4_args {
432 int pid;
433 int *status;
434 int options;
435 struct rusage *rusage;
436 };
437
438 #if COMPAT_43
439 int
440 owait(p, uap, retval)
441 struct proc *p;
442 void *uap;
443 int *retval;
444 {
445 struct wait4_args *a;
446
447 a = (struct wait4_args *)get_bsduthreadarg(current_act());
448
449 a->options = 0;
450 a->rusage = NULL;
451 a->pid = WAIT_ANY;
452 a->status = NULL;
453 return (wait1(p, a, retval, 1));
454 }
455
456 int
457 wait4(p, uap, retval)
458 struct proc *p;
459 struct wait4_args *uap;
460 int *retval;
461 {
462 return (wait1(p, uap, retval, 0));
463 }
464
465 struct owait3_args {
466 int *status;
467 int options;
468 struct rusage *rusage;
469 };
470
471 int
472 owait3(p, uap, retval)
473 struct proc *p;
474 struct owait3_args *uap;
475 int *retval;
476 {
477 struct wait4_args *a;
478
479 a = (struct wait4_args *)get_bsduthreadarg(current_act);
480
481 a->rusage = uap->rusage;
482 a->options = uap->options;
483 a->status = uap->status;
484 a->pid = WAIT_ANY;
485
486 return (wait1(p, a, retval, 1));
487 }
488
489 #else
490 #define wait1 wait4
491 #endif
492
493 int
494 wait1continue(result)
495 {
496 void *vt;
497 thread_act_t thread;
498 int *retval;
499 struct proc *p;
500
501 if (result)
502 return(result);
503
504 p = current_proc();
505 thread = current_act();
506 vt = get_bsduthreadarg(thread);
507 retval = get_bsduthreadrval(thread);
508 wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0);
509 }
510
511 int
512 wait1(q, uap, retval, compat)
513 register struct proc *q;
514 register struct wait4_args *uap;
515 register_t *retval;
516 #if COMPAT_43
517 int compat;
518 #endif
519 {
520 register int nfound;
521 register struct proc *p, *t;
522 int status, error;
523
524 retry:
525 if (uap->pid == 0)
526 uap->pid = -q->p_pgid;
527
528 loop:
529 nfound = 0;
530 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
531 if (uap->pid != WAIT_ANY &&
532 p->p_pid != uap->pid &&
533 p->p_pgid != -(uap->pid))
534 continue;
535 nfound++;
536 if (p->p_flag & P_WAITING) {
537 (void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0);
538 goto loop;
539 }
540 p->p_flag |= P_WAITING; /* only allow single thread to wait() */
541
542 if (p->p_stat == SZOMB) {
543 retval[0] = p->p_pid;
544 #if COMPAT_43
545 if (compat)
546 retval[1] = p->p_xstat;
547 else
548 #endif
549 if (uap->status) {
550 status = p->p_xstat; /* convert to int */
551 if (error = copyout((caddr_t)&status,
552 (caddr_t)uap->status,
553 sizeof(status))) {
554 p->p_flag &= ~P_WAITING;
555 wakeup(&p->p_stat);
556 return (error);
557 }
558 }
559 if (uap->rusage &&
560 (error = copyout((caddr_t)p->p_ru,
561 (caddr_t)uap->rusage,
562 sizeof (struct rusage)))) {
563 p->p_flag &= ~P_WAITING;
564 wakeup(&p->p_stat);
565 return (error);
566 }
567 /*
568 * If we got the child via a ptrace 'attach',
569 * we need to give it back to the old parent.
570 */
571 if (p->p_oppid && (t = pfind(p->p_oppid))) {
572 p->p_oppid = 0;
573 proc_reparent(p, t);
574 psignal(t, SIGCHLD);
575 wakeup((caddr_t)t);
576 p->p_flag &= ~P_WAITING;
577 wakeup(&p->p_stat);
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 p->p_flag &= ~P_WAITING;
624 FREE_ZONE(p, sizeof *p, M_PROC);
625 nprocs--;
626 wakeup(&p->p_stat);
627 return (0);
628 }
629 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
630 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
631 p->p_flag |= P_WAITED;
632 retval[0] = p->p_pid;
633 #if COMPAT_43
634 if (compat) {
635 retval[1] = W_STOPCODE(p->p_xstat);
636 error = 0;
637 } else
638 #endif
639 if (uap->status) {
640 status = W_STOPCODE(p->p_xstat);
641 error = copyout((caddr_t)&status,
642 (caddr_t)uap->status,
643 sizeof(status));
644 } else
645 error = 0;
646 p->p_flag &= ~P_WAITING;
647 wakeup(&p->p_stat);
648 return (error);
649 }
650 p->p_flag &= ~P_WAITING;
651 wakeup(&p->p_stat);
652 }
653 if (nfound == 0)
654 return (ECHILD);
655
656 if (uap->options & WNOHANG) {
657 retval[0] = 0;
658 return (0);
659 }
660
661 if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue))
662 return (error);
663
664 goto loop;
665 }
666
667 /*
668 * make process 'parent' the new parent of process 'child'.
669 */
670 void
671 proc_reparent(child, parent)
672 register struct proc *child;
673 register struct proc *parent;
674 {
675
676 if (child->p_pptr == parent)
677 return;
678
679 LIST_REMOVE(child, p_sibling);
680 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
681 child->p_pptr = parent;
682 }
683
684 /*
685 * Make the current process an "init" process, meaning
686 * that it doesn't have a parent, and that it won't be
687 * gunned down by kill(-1, 0).
688 */
689 kern_return_t
690 init_process(void)
691 {
692 register struct proc *p = current_proc();
693
694 if (suser(p->p_ucred, &p->p_acflag))
695 return(KERN_NO_ACCESS);
696
697 if (p->p_pid != 1 && p->p_pgid != p->p_pid)
698 enterpgrp(p, p->p_pid, 0);
699 p->p_flag |= P_SYSTEM;
700
701 /*
702 * Take us out of the sibling chain, and
703 * out of our parent's child chain.
704 */
705 LIST_REMOVE(p, p_sibling);
706 p->p_sibling.le_prev = NULL;
707 p->p_sibling.le_next = NULL;
708 p->p_pptr = kernproc;
709
710 return(KERN_SUCCESS);
711 }
712
713 void
714 process_terminate_self(void)
715 {
716 struct proc *p = current_proc();
717
718 if (p != NULL) {
719 exit1(p, W_EXITCODE(0, SIGKILL), (int *)NULL);
720 /*NOTREACHED*/
721 }
722 }
723
724 /*
725 * Exit: deallocate address space and other resources, change proc state
726 * to zombie, and unlink proc from allproc and parent's lists. Save exit
727 * status and rusage for wait(). Check for child processes and orphan them.
728 */
729
730 void
731 vfork_exit(p, rv)
732 register struct proc *p;
733 int rv;
734 {
735 register struct proc *q, *nq;
736 thread_t self = current_thread();
737 thread_act_t th_act_self = current_act();
738 struct task *task = p->task;
739 register int i,s;
740 struct uthread *ut;
741
742 /*
743 * If a thread in this task has already
744 * called exit(), then halt any others
745 * right here.
746 */
747
748 ut = get_bsdthread_info(th_act_self);
749 #ifdef FIXME
750 signal_lock(p);
751 while (p->exit_thread != self) {
752 if (sig_try_locked(p) <= 0) {
753 if (get_threadtask(th_act_self) != task) {
754 signal_unlock(p);
755 return;
756 }
757 signal_unlock(p);
758 thread_terminate(th_act_self);
759 thread_funnel_set(kernel_flock, FALSE);
760 thread_exception_return();
761 /* NOTREACHED */
762 }
763 sig_lock_to_exit(p);
764 }
765 signal_unlock(p);
766 if (p->p_pid == 1) {
767 printf("pid 1 exited (signal %d, exit %d)",
768 WTERMSIG(rv), WEXITSTATUS(rv));
769 panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data);
770 }
771 #endif /* FIXME */
772
773 s = splsched();
774 p->p_flag |= P_WEXIT;
775 splx(s);
776 /*
777 * Remove proc from allproc queue and from pidhash chain.
778 * Need to do this before we do anything that can block.
779 * Not doing causes things like mount() find this on allproc
780 * in partially cleaned state.
781 */
782 LIST_REMOVE(p, p_list);
783 LIST_REMOVE(p, p_hash);
784 /*
785 * If parent is waiting for us to exit or exec,
786 * P_PPWAIT is set; we will wakeup the parent below.
787 */
788 p->p_flag &= ~(P_TRACED | P_PPWAIT);
789 p->p_sigignore = ~0;
790 p->p_siglist = 0;
791
792 ut->uu_sig = 0;
793 untimeout(realitexpire, (caddr_t)p);
794
795 p->p_xstat = rv;
796
797 vproc_exit(p);
798 }
799
800 void
801 vproc_exit(struct proc *p)
802 {
803 register struct proc *q, *nq;
804 thread_t self = current_thread();
805 thread_act_t th_act_self = current_act();
806 struct task *task = p->task;
807 register int i,s;
808 boolean_t funnel_state;
809
810 MALLOC_ZONE(p->p_ru, struct rusage *,
811 sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
812
813 /*
814 * Close open files and release open-file table.
815 * This may block!
816 */
817 fdfree(p);
818
819 /* Close ref SYSV Shared memory*/
820 if (p->vm_shm)
821 shmexit(p);
822
823 if (SESS_LEADER(p)) {
824 register struct session *sp = p->p_session;
825
826 if (sp->s_ttyvp) {
827 /*
828 * Controlling process.
829 * Signal foreground pgrp,
830 * drain controlling terminal
831 * and revoke access to controlling terminal.
832 */
833 if (sp->s_ttyp->t_session == sp) {
834 if (sp->s_ttyp->t_pgrp)
835 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
836 (void) ttywait(sp->s_ttyp);
837 /*
838 * The tty could have been revoked
839 * if we blocked.
840 */
841 if (sp->s_ttyvp)
842 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
843 }
844 if (sp->s_ttyvp)
845 vrele(sp->s_ttyvp);
846 sp->s_ttyvp = NULL;
847 /*
848 * s_ttyp is not zero'd; we use this to indicate
849 * that the session once had a controlling terminal.
850 * (for logging and informational purposes)
851 */
852 }
853 sp->s_leader = NULL;
854 }
855
856 fixjobc(p, p->p_pgrp, 0);
857 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
858 #if KTRACE
859 /*
860 * release trace file
861 */
862 p->p_traceflag = 0; /* don't trace the vrele() */
863 if (p->p_tracep)
864 vrele(p->p_tracep);
865 #endif
866
867 q = p->p_children.lh_first;
868 if (q) /* only need this if any child is S_ZOMB */
869 wakeup((caddr_t) initproc);
870 for (; q != 0; q = nq) {
871 nq = q->p_sibling.le_next;
872 proc_reparent(q, initproc);
873 /*
874 * Traced processes are killed
875 * since their existence means someone is messing up.
876 */
877 if (q->p_flag & P_TRACED) {
878 q->p_flag &= ~P_TRACED;
879 if (q->sigwait_thread) {
880 thread_t sig_shuttle = getshuttle_thread(q->sigwait_thread);
881 /*
882 * The sigwait_thread could be stopped at a
883 * breakpoint. Wake it up to kill.
884 * Need to do this as it could be a thread which is not
885 * the first thread in the task. So any attempts to kill
886 * the process would result into a deadlock on q->sigwait.
887 */
888 thread_resume((struct thread *)q->sigwait_thread);
889 clear_wait(sig_shuttle, THREAD_INTERRUPTED);
890 threadsignal(q->sigwait_thread, SIGKILL, 0);
891 }
892 psignal(q, SIGKILL);
893 }
894 }
895
896 /*
897 * Save exit status and final rusage info, adding in child rusage
898 * info and self times.
899 */
900 *p->p_ru = p->p_stats->p_ru;
901
902 timerclear(&p->p_ru->ru_utime);
903 timerclear(&p->p_ru->ru_stime);
904
905 #ifdef FIXME
906 if (task) {
907 task_basic_info_data_t tinfo;
908 task_thread_times_info_data_t ttimesinfo;
909 int task_info_stuff, task_ttimes_stuff;
910 struct timeval ut,st;
911
912 task_info_stuff = TASK_BASIC_INFO_COUNT;
913 task_info(task, TASK_BASIC_INFO,
914 &tinfo, &task_info_stuff);
915 p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
916 p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
917 p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
918 p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
919
920 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
921 task_info(task, TASK_THREAD_TIMES_INFO,
922 &ttimesinfo, &task_ttimes_stuff);
923
924 ut.tv_sec = ttimesinfo.user_time.seconds;
925 ut.tv_usec = ttimesinfo.user_time.microseconds;
926 st.tv_sec = ttimesinfo.system_time.seconds;
927 st.tv_usec = ttimesinfo.system_time.microseconds;
928 timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
929 timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
930 }
931 #endif /* FIXME */
932
933 ruadd(p->p_ru, &p->p_stats->p_cru);
934
935 /*
936 * Free up profiling buffers.
937 */
938 {
939 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
940
941 p1 = p0->pr_next;
942 p0->pr_next = NULL;
943 p0->pr_scale = 0;
944
945 for (; p1 != NULL; p1 = pn) {
946 pn = p1->pr_next;
947 kfree((vm_offset_t)p1, sizeof *p1);
948 }
949 }
950
951 /*
952 * Other substructures are freed from wait().
953 */
954 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
955 p->p_stats = NULL;
956
957 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
958 p->p_sigacts = NULL;
959
960 if (--p->p_limit->p_refcnt == 0)
961 FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
962 p->p_limit = NULL;
963
964 /*
965 * Finish up by terminating the task
966 * and halt this thread (only if a
967 * member of the task exiting).
968 */
969 p->task = TASK_NULL;
970
971 /*
972 * Notify parent that we're gone.
973 */
974 psignal(p->p_pptr, SIGCHLD);
975
976 /* Place onto zombproc. */
977 LIST_INSERT_HEAD(&zombproc, p, p_list);
978 p->p_stat = SZOMB;
979
980 /* and now wakeup the parent */
981 wakeup((caddr_t)p->p_pptr);
982 }