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