kern_exit.c revision 1.55 1 /* $NetBSD: kern_exit.c,v 1.55 1998/09/01 01:02:33 thorpej 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.10 (Berkeley) 2/23/95
41 */
42
43 #include "opt_ktrace.h"
44 #include "opt_uvm.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/map.h>
49 #include <sys/ioctl.h>
50 #include <sys/proc.h>
51 #include <sys/tty.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <sys/kernel.h>
55 #include <sys/ktrace.h>
56 #include <sys/proc.h>
57 #include <sys/buf.h>
58 #include <sys/wait.h>
59 #include <sys/file.h>
60 #include <sys/vnode.h>
61 #include <sys/syslog.h>
62 #include <sys/malloc.h>
63 #include <sys/pool.h>
64 #include <sys/resourcevar.h>
65 #include <sys/ptrace.h>
66 #include <sys/acct.h>
67 #include <sys/filedesc.h>
68 #include <sys/signalvar.h>
69 #ifdef SYSVSHM
70 #include <sys/shm.h>
71 #endif
72 #ifdef SYSVSEM
73 #include <sys/sem.h>
74 #endif
75
76 #include <sys/mount.h>
77 #include <sys/syscallargs.h>
78
79 #include <machine/cpu.h>
80
81 #include <vm/vm.h>
82 #include <vm/vm_kern.h>
83
84 #if defined(UVM)
85 #include <uvm/uvm_extern.h>
86 #endif
87
88 /*
89 * exit --
90 * Death of process.
91 */
92 int
93 sys_exit(p, v, retval)
94 struct proc *p;
95 void *v;
96 register_t *retval;
97 {
98 struct sys_exit_args /* {
99 syscallarg(int) rval;
100 } */ *uap = v;
101
102 exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
103 /* NOTREACHED */
104 return (0);
105 }
106
107 /*
108 * Exit: deallocate address space and other resources, change proc state
109 * to zombie, and unlink proc from allproc and parent's lists. Save exit
110 * status and rusage for wait(). Check for child processes and orphan them.
111 */
112 void
113 exit1(p, rv)
114 register struct proc *p;
115 int rv;
116 {
117 register struct proc *q, *nq;
118 register struct vmspace *vm;
119
120 if (p->p_pid == 1)
121 panic("init died (signal %d, exit %d)",
122 WTERMSIG(rv), WEXITSTATUS(rv));
123 #ifdef PGINPROF
124 vmsizmon();
125 #endif
126 if (p->p_flag & P_PROFIL)
127 stopprofclock(p);
128 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
129 /*
130 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
131 * wake up the parent early to avoid deadlock.
132 */
133 p->p_flag |= P_WEXIT;
134 if (p->p_flag & P_PPWAIT) {
135 p->p_flag &= ~P_PPWAIT;
136 wakeup((caddr_t)p->p_pptr);
137 }
138 p->p_sigignore = ~0;
139 p->p_siglist = 0;
140 untimeout(realitexpire, (caddr_t)p);
141
142 /*
143 * Close open files and release open-file table.
144 * This may block!
145 */
146 fdfree(p);
147
148 /* The next three chunks should probably be moved to vmspace_exit. */
149 vm = p->p_vmspace;
150 #ifdef SYSVSHM
151 if (vm->vm_shm && vm->vm_refcnt == 1)
152 shmexit(vm);
153 #endif
154 #ifdef SYSVSEM
155 semexit(p);
156 #endif
157 /*
158 * Release user portion of address space.
159 * This releases references to vnodes,
160 * which could cause I/O if the file has been unlinked.
161 * Need to do this early enough that we can still sleep.
162 * Can't free the entire vmspace as the kernel stack
163 * may be mapped within that space also.
164 */
165 #if defined(UVM)
166 if (vm->vm_refcnt == 1)
167 (void) uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
168 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
169 #else
170 if (vm->vm_refcnt == 1)
171 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
172 VM_MAXUSER_ADDRESS);
173 #endif
174
175 if (SESS_LEADER(p)) {
176 register struct session *sp = p->p_session;
177
178 if (sp->s_ttyvp) {
179 /*
180 * Controlling process.
181 * Signal foreground pgrp,
182 * drain controlling terminal
183 * and revoke access to controlling terminal.
184 */
185 if (sp->s_ttyp->t_session == sp) {
186 if (sp->s_ttyp->t_pgrp)
187 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
188 (void) ttywait(sp->s_ttyp);
189 /*
190 * The tty could have been revoked
191 * if we blocked.
192 */
193 if (sp->s_ttyvp)
194 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
195 }
196 if (sp->s_ttyvp)
197 vrele(sp->s_ttyvp);
198 sp->s_ttyvp = NULL;
199 /*
200 * s_ttyp is not zero'd; we use this to indicate
201 * that the session once had a controlling terminal.
202 * (for logging and informational purposes)
203 */
204 }
205 sp->s_leader = NULL;
206 }
207 fixjobc(p, p->p_pgrp, 0);
208 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
209 (void)acct_process(p);
210 #ifdef KTRACE
211 /*
212 * release trace file
213 */
214 ktrderef(p);
215 #endif
216 /*
217 * Remove proc from allproc queue and pidhash chain.
218 * Place onto zombproc. Unlink from parent's child list.
219 */
220 LIST_REMOVE(p, p_list);
221 LIST_INSERT_HEAD(&zombproc, p, p_list);
222 p->p_stat = SZOMB;
223
224 LIST_REMOVE(p, p_hash);
225
226 q = p->p_children.lh_first;
227 if (q) /* only need this if any child is S_ZOMB */
228 wakeup((caddr_t)initproc);
229 for (; q != 0; q = nq) {
230 nq = q->p_sibling.le_next;
231 proc_reparent(q, initproc);
232 /*
233 * Traced processes are killed
234 * since their existence means someone is screwing up.
235 */
236 if (q->p_flag & P_TRACED) {
237 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
238 psignal(q, SIGKILL);
239 }
240 }
241
242 /*
243 * Save exit status and final rusage info, adding in child rusage
244 * info and self times.
245 */
246 p->p_xstat = rv;
247 *p->p_ru = p->p_stats->p_ru;
248 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
249 ruadd(p->p_ru, &p->p_stats->p_cru);
250
251 /*
252 * Notify parent that we're gone.
253 */
254 if ((p->p_flag & P_FSTRACE) == 0)
255 psignal(p->p_pptr, SIGCHLD);
256 wakeup((caddr_t)p->p_pptr);
257
258 /*
259 * Clear curproc after we've done all operations
260 * that could block, and before tearing down the rest
261 * of the process state that might be used from clock, etc.
262 * Also, can't clear curproc while we're still runnable,
263 * as we're not on a run queue (we are current, just not
264 * a proper proc any longer!).
265 *
266 * Other substructures are freed from wait().
267 */
268 curproc = NULL;
269 if (--p->p_limit->p_refcnt == 0)
270 pool_put(&plimit_pool, p->p_limit);
271
272 /*
273 * Finally, call machine-dependent code to release the remaining
274 * resources including address space, the kernel stack and pcb.
275 * The address space is released by "vmspace_free(p->p_vmspace)";
276 * This is machine-dependent, as we may have to change stacks
277 * or ensure that the current one isn't reallocated before we
278 * finish. cpu_exit will end with a call to cpu_swtch(), finishing
279 * our execution (pun intended).
280 */
281 cpu_exit(p);
282 }
283
284 int
285 sys_wait4(q, v, retval)
286 register struct proc *q;
287 void *v;
288 register_t *retval;
289 {
290 register struct sys_wait4_args /* {
291 syscallarg(int) pid;
292 syscallarg(int *) status;
293 syscallarg(int) options;
294 syscallarg(struct rusage *) rusage;
295 } */ *uap = v;
296 register int nfound;
297 register struct proc *p, *t;
298 int status, error;
299
300 if (SCARG(uap, pid) == 0)
301 SCARG(uap, pid) = -q->p_pgid;
302 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
303 return (EINVAL);
304
305 loop:
306 nfound = 0;
307 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
308 if (SCARG(uap, pid) != WAIT_ANY &&
309 p->p_pid != SCARG(uap, pid) &&
310 p->p_pgid != -SCARG(uap, pid))
311 continue;
312 nfound++;
313 if (p->p_stat == SZOMB) {
314 retval[0] = p->p_pid;
315
316 if (SCARG(uap, status)) {
317 status = p->p_xstat; /* convert to int */
318 error = copyout((caddr_t)&status,
319 (caddr_t)SCARG(uap, status),
320 sizeof(status));
321 if (error)
322 return (error);
323 }
324 if (SCARG(uap, rusage) &&
325 (error = copyout((caddr_t)p->p_ru,
326 (caddr_t)SCARG(uap, rusage),
327 sizeof(struct rusage))))
328 return (error);
329 /*
330 * If we got the child via ptrace(2) or procfs, and
331 * the parent is different (meaning the process was
332 * attached, rather than run as a child), then we need
333 * to give it back to the old parent, and send the
334 * parent a SIGCHLD. The rest of the cleanup will be
335 * done when the old parent waits on the child.
336 */
337 if ((p->p_flag & P_TRACED) &&
338 p->p_oppid != p->p_pptr->p_pid) {
339 t = pfind(p->p_oppid);
340 proc_reparent(p, t ? t : initproc);
341 p->p_oppid = 0;
342 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
343 psignal(p->p_pptr, SIGCHLD);
344 wakeup((caddr_t)p->p_pptr);
345 return (0);
346 }
347 p->p_xstat = 0;
348 ruadd(&q->p_stats->p_cru, p->p_ru);
349 pool_put(&rusage_pool, p->p_ru);
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 * Decrement the count of procs running with this uid.
361 */
362 (void)chgproccnt(p->p_cred->p_ruid, -1);
363
364 /*
365 * Free up credentials.
366 */
367 if (--p->p_cred->p_refcnt == 0) {
368 crfree(p->p_cred->pc_ucred);
369 pool_put(&pcred_pool, p->p_cred);
370 }
371
372 /*
373 * Release reference to text vnode
374 */
375 if (p->p_textvp)
376 vrele(p->p_textvp);
377
378 /*
379 * Give machine-dependent layer a chance
380 * to free anything that cpu_exit couldn't
381 * release while still running in process context.
382 */
383 cpu_wait(p);
384 pool_put(&proc_pool, p);
385 nprocs--;
386 return (0);
387 }
388 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
389 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
390 p->p_flag |= P_WAITED;
391 retval[0] = p->p_pid;
392
393 if (SCARG(uap, status)) {
394 status = W_STOPCODE(p->p_xstat);
395 error = copyout((caddr_t)&status,
396 (caddr_t)SCARG(uap, status),
397 sizeof(status));
398 } else
399 error = 0;
400 return (error);
401 }
402 }
403 if (nfound == 0)
404 return (ECHILD);
405 if (SCARG(uap, options) & WNOHANG) {
406 retval[0] = 0;
407 return (0);
408 }
409 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
410 return (error);
411 goto loop;
412 }
413
414 /*
415 * make process 'parent' the new parent of process 'child'.
416 */
417 void
418 proc_reparent(child, parent)
419 register struct proc *child;
420 register struct proc *parent;
421 {
422
423 if (child->p_pptr == parent)
424 return;
425
426 LIST_REMOVE(child, p_sibling);
427 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
428 child->p_pptr = parent;
429 }
430