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