kern_exit.c revision 1.30 1 /* $NetBSD: kern_exit.c,v 1.30 1995/06/24 20:33:59 christos Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/map.h>
46 #include <sys/ioctl.h>
47 #include <sys/proc.h>
48 #include <sys/tty.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/buf.h>
54 #include <sys/wait.h>
55 #include <sys/file.h>
56 #include <sys/vnode.h>
57 #include <sys/syslog.h>
58 #include <sys/malloc.h>
59 #include <sys/resourcevar.h>
60 #include <sys/ptrace.h>
61 #include <sys/acct.h>
62
63 #include <sys/mount.h>
64 #include <sys/syscallargs.h>
65
66 #include <machine/cpu.h>
67
68 #include <vm/vm.h>
69 #include <vm/vm_kern.h>
70
71 __dead void cpu_exit __P((struct proc *)) __attribute__((noreturn));
72 __dead void exit1 __P((struct proc *, int)) __attribute__((noreturn));
73
74 /*
75 * exit --
76 * Death of process.
77 */
78 __dead void
79 exit(p, uap, retval)
80 struct proc *p;
81 struct exit_args /* {
82 syscallarg(int) rval;
83 } */ *uap;
84 int *retval;
85 {
86
87 exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
88 /* NOTREACHED */
89 }
90
91 /*
92 * Exit: deallocate address space and other resources, change proc state
93 * to zombie, and unlink proc from allproc and parent's lists. Save exit
94 * status and rusage for wait(). Check for child processes and orphan them.
95 */
96 __dead void
97 exit1(p, rv)
98 register struct proc *p;
99 int rv;
100 {
101 register struct proc *q, *nq;
102 register struct vmspace *vm;
103
104 if (p->p_pid == 1)
105 panic("init died (signal %d, exit %d)",
106 WTERMSIG(rv), WEXITSTATUS(rv));
107 #ifdef PGINPROF
108 vmsizmon();
109 #endif
110 if (p->p_flag & P_PROFIL)
111 stopprofclock(p);
112 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
113 M_ZOMBIE, M_WAITOK);
114 /*
115 * If parent is waiting for us to exit or exec,
116 * P_PPWAIT is set; we will wakeup the parent below.
117 */
118 p->p_flag &= ~(P_TRACED | P_PPWAIT);
119 p->p_flag |= P_WEXIT;
120 p->p_sigignore = ~0;
121 p->p_siglist = 0;
122 untimeout(realitexpire, (caddr_t)p);
123
124 /*
125 * Close open files and release open-file table.
126 * This may block!
127 */
128 fdfree(p);
129
130 /* The next three chunks should probably be moved to vmspace_exit. */
131 vm = p->p_vmspace;
132 #ifdef SYSVSHM
133 if (vm->vm_shm)
134 shmexit(p);
135 #endif
136 #ifdef SYSVSEM
137 semexit(p);
138 #endif
139 /*
140 * Release user portion of address space.
141 * This releases references to vnodes,
142 * which could cause I/O if the file has been unlinked.
143 * Need to do this early enough that we can still sleep.
144 * Can't free the entire vmspace as the kernel stack
145 * may be mapped within that space also.
146 */
147 if (vm->vm_refcnt == 1)
148 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
149 VM_MAXUSER_ADDRESS);
150
151 if (SESS_LEADER(p)) {
152 register struct session *sp = p->p_session;
153
154 if (sp->s_ttyvp) {
155 /*
156 * Controlling process.
157 * Signal foreground pgrp,
158 * drain controlling terminal
159 * and revoke access to controlling terminal.
160 */
161 if (sp->s_ttyp->t_session == sp) {
162 if (sp->s_ttyp->t_pgrp)
163 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
164 (void) ttywait(sp->s_ttyp);
165 /*
166 * The tty could have been revoked
167 * if we blocked.
168 */
169 if (sp->s_ttyvp)
170 vgoneall(sp->s_ttyvp);
171 }
172 if (sp->s_ttyvp)
173 vrele(sp->s_ttyvp);
174 sp->s_ttyvp = NULL;
175 /*
176 * s_ttyp is not zero'd; we use this to indicate
177 * that the session once had a controlling terminal.
178 * (for logging and informational purposes)
179 */
180 }
181 sp->s_leader = NULL;
182 }
183 fixjobc(p, p->p_pgrp, 0);
184 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
185 (void)acct_process(p);
186 #ifdef KTRACE
187 /*
188 * release trace file
189 */
190 p->p_traceflag = 0; /* don't trace the vrele() */
191 if (p->p_tracep)
192 vrele(p->p_tracep);
193 #endif
194 /*
195 * Remove proc from allproc queue and pidhash chain.
196 * Place onto zombproc. Unlink from parent's child list.
197 */
198 LIST_REMOVE(p, p_list);
199 LIST_INSERT_HEAD(&zombproc, p, p_list);
200 p->p_stat = SZOMB;
201
202 LIST_REMOVE(p, p_hash);
203
204 q = p->p_children.lh_first;
205 if (q) /* only need this if any child is S_ZOMB */
206 wakeup((caddr_t) initproc);
207 for (; q != 0; q = nq) {
208 nq = q->p_sibling.le_next;
209 proc_reparent(q, initproc);
210 /*
211 * Traced processes are killed
212 * since their existence means someone is screwing up.
213 */
214 if (q->p_flag & P_TRACED) {
215 q->p_flag &= ~P_TRACED;
216 psignal(q, SIGKILL);
217 }
218 }
219
220 /*
221 * Save exit status and final rusage info, adding in child rusage
222 * info and self times.
223 */
224 p->p_xstat = rv;
225 *p->p_ru = p->p_stats->p_ru;
226 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
227 ruadd(p->p_ru, &p->p_stats->p_cru);
228
229 /*
230 * Notify parent that we're gone.
231 */
232 psignal(p->p_pptr, SIGCHLD);
233 wakeup((caddr_t)p->p_pptr);
234 /*
235 * Notify procfs debugger
236 */
237 if (p->p_flag & P_FSTRACE)
238 wakeup((caddr_t)p);
239 #if defined(tahoe)
240 /* move this to cpu_exit */
241 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
242 #endif
243 /*
244 * Clear curproc after we've done all operations
245 * that could block, and before tearing down the rest
246 * of the process state that might be used from clock, etc.
247 * Also, can't clear curproc while we're still runnable,
248 * as we're not on a run queue (we are current, just not
249 * a proper proc any longer!).
250 *
251 * Other substructures are freed from wait().
252 */
253 curproc = NULL;
254 if (--p->p_limit->p_refcnt == 0)
255 FREE(p->p_limit, M_SUBPROC);
256
257 /*
258 * Finally, call machine-dependent code to release the remaining
259 * resources including address space, the kernel stack and pcb.
260 * The address space is released by "vmspace_free(p->p_vmspace)";
261 * This is machine-dependent, as we may have to change stacks
262 * or ensure that the current one isn't reallocated before we
263 * finish. cpu_exit will end with a call to cpu_swtch(), finishing
264 * our execution (pun intended).
265 */
266 cpu_exit(p);
267 }
268
269 int
270 wait4(q, uap, retval, compat)
271 register struct proc *q;
272 register struct wait4_args /* {
273 syscallarg(int) pid;
274 syscallarg(int *) status;
275 syscallarg(int) options;
276 syscallarg(struct rusage *) rusage;
277 } */ *uap;
278 register_t *retval;
279 {
280 register int nfound;
281 register struct proc *p, *t;
282 int status, error;
283
284 #ifdef COMPAT_09
285 SCARG(uap, pid) = (short)SCARG(uap, pid);
286 #endif
287
288 if (SCARG(uap, pid) == 0)
289 SCARG(uap, pid) = -q->p_pgid;
290 #ifdef notyet
291 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
292 return (EINVAL);
293 #endif
294 loop:
295 nfound = 0;
296 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
297 if (SCARG(uap, pid) != WAIT_ANY &&
298 p->p_pid != SCARG(uap, pid) &&
299 p->p_pgid != -SCARG(uap, pid))
300 continue;
301 nfound++;
302 if (p->p_stat == SZOMB) {
303 retval[0] = p->p_pid;
304
305 if (SCARG(uap, status)) {
306 status = p->p_xstat; /* convert to int */
307 if (error = copyout((caddr_t)&status,
308 (caddr_t)SCARG(uap, status),
309 sizeof(status)))
310 return (error);
311 }
312 if (SCARG(uap, rusage) &&
313 (error = copyout((caddr_t)p->p_ru,
314 (caddr_t)SCARG(uap, rusage),
315 sizeof (struct rusage))))
316 return (error);
317 /*
318 * If we got the child via a ptrace 'attach',
319 * we need to give it back to the old parent.
320 */
321 if (p->p_oppid && (t = pfind(p->p_oppid))) {
322 p->p_oppid = 0;
323 proc_reparent(p, t);
324 psignal(t, SIGCHLD);
325 wakeup((caddr_t)t);
326 return (0);
327 }
328 p->p_xstat = 0;
329 ruadd(&q->p_stats->p_cru, p->p_ru);
330 FREE(p->p_ru, M_ZOMBIE);
331
332 /*
333 * Decrement the count of procs running with this uid.
334 */
335 (void)chgproccnt(p->p_cred->p_ruid, -1);
336
337 /*
338 * Free up credentials.
339 */
340 if (--p->p_cred->p_refcnt == 0) {
341 crfree(p->p_cred->pc_ucred);
342 FREE(p->p_cred, M_SUBPROC);
343 }
344
345 /*
346 * Release reference to text vnode
347 */
348 if (p->p_textvp)
349 vrele(p->p_textvp);
350
351 /*
352 * Finally finished with old proc entry.
353 * Unlink it from its process group and free it.
354 */
355 leavepgrp(p);
356 LIST_REMOVE(p, p_list); /* off zombproc */
357 LIST_REMOVE(p, p_sibling);
358
359 /*
360 * Give machine-dependent layer a chance
361 * to free anything that cpu_exit couldn't
362 * release while still running in process context.
363 */
364 cpu_wait(p);
365 FREE(p, M_PROC);
366 nprocs--;
367 return (0);
368 }
369 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
370 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
371 p->p_flag |= P_WAITED;
372 retval[0] = p->p_pid;
373
374 if (SCARG(uap, status)) {
375 status = W_STOPCODE(p->p_xstat);
376 error = copyout((caddr_t)&status,
377 (caddr_t)SCARG(uap, status),
378 sizeof(status));
379 } else
380 error = 0;
381 return (error);
382 }
383 }
384 if (nfound == 0)
385 return (ECHILD);
386 if (SCARG(uap, options) & WNOHANG) {
387 retval[0] = 0;
388 return (0);
389 }
390 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
391 return (error);
392 goto loop;
393 }
394
395 /*
396 * make process 'parent' the new parent of process 'child'.
397 */
398 void
399 proc_reparent(child, parent)
400 register struct proc *child;
401 register struct proc *parent;
402 {
403
404 if (child->p_pptr == parent)
405 return;
406
407 LIST_REMOVE(child, p_sibling);
408 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
409 child->p_pptr = parent;
410 }
411