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