mv vmspace_free and kmem_free of kernel stack to cpu_exit;
[unix-history] / usr / src / sys / kern / kern_exit.c
CommitLineData
da7c5cc6 1/*
8429d022 2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
c4ec2128 3 * All rights reserved.
da7c5cc6 4 *
dbf0c423 5 * %sccs.include.redist.c%
c4ec2128 6 *
e149007b 7 * @(#)kern_exit.c 7.33 (Berkeley) %G%
da7c5cc6 8 */
a8560fa0 9
94368568
JB
10#include "param.h"
11#include "systm.h"
12#include "map.h"
1bfd8b20
KM
13#include "ioctl.h"
14#include "tty.h"
8429d022
MK
15#include "time.h"
16#include "resource.h"
94368568
JB
17#include "kernel.h"
18#include "proc.h"
19#include "buf.h"
20#include "wait.h"
94368568 21#include "file.h"
c4ec2128 22#include "vnode.h"
5e63dd78 23#include "syslog.h"
61d22ffc 24#include "malloc.h"
9a5334ea 25#include "resourcevar.h"
a8560fa0 26
e149007b 27#include "machine/cpu.h"
1ad494e6 28#ifdef COMPAT_43
8429d022 29#include "machine/reg.h"
d301d150 30#include "machine/psl.h"
1ad494e6
MK
31#endif
32
9a5334ea 33#include "vm/vm.h"
8429d022 34#include "vm/vm_kern.h"
9db58063 35
a8560fa0
SL
36/*
37 * Exit system call: pass back caller's arg
38 */
1bfd8b20
KM
39/* ARGSUSED */
40rexit(p, uap, retval)
41 struct proc *p;
42 struct args {
a8560fa0
SL
43 int rval;
44 } *uap;
1bfd8b20
KM
45 int *retval;
46{
a8560fa0 47
5360383b
MK
48 exit(p, W_EXITCODE(uap->rval, 0));
49 /* NOTREACHED */
a8560fa0
SL
50}
51
52/*
8429d022
MK
53 * Exit: deallocate address space and other resources,
54 * change proc state to zombie, and unlink proc from allproc
55 * and parent's lists. Save exit status and rusage for wait().
56 * Check for child processes and orphan them.
a8560fa0 57 */
1bfd8b20 58exit(p, rv)
8429d022 59 register struct proc *p;
c35e8b3f 60 int rv;
a8560fa0 61{
1bfd8b20 62 register struct proc *q, *nq;
4a438310 63 register struct proc **pp;
8429d022 64 int s;
a8560fa0
SL
65
66#ifdef PGINPROF
67 vmsizmon();
68#endif
61d22ffc
KM
69 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
70 M_ZOMBIE, M_WAITOK);
8429d022
MK
71 /*
72 * If parent is waiting for us to exit or exec,
73 * SPPWAIT is set; we will wakeup the parent below.
74 */
9a5334ea 75 p->p_flag &= ~(STRC|SPPWAIT);
a8560fa0 76 p->p_flag |= SWEXIT;
dd012d1e 77 p->p_sigignore = ~0;
a2528931 78 p->p_sig = 0;
a8560fa0 79 untimeout(realitexpire, (caddr_t)p);
8429d022
MK
80
81 /*
82 * Close open files and release open-file table.
83 * This may block!
84 */
85 fdfree(p);
9db58063 86#ifdef SYSVSHM
8429d022 87 if (p->p_vmspace->vm_shm)
9db58063
KM
88 shmexit(p);
89#endif
8429d022
MK
90
91 if (p->p_pid == 1)
92 panic("init died");
1ad494e6 93 if (SESS_LEADER(p)) {
a38b908e
MT
94 register struct session *sp = p->p_session;
95
96 if (sp->s_ttyvp) {
97 /*
98 * Controlling process.
e149007b
MK
99 * Signal foreground pgrp,
100 * drain controlling terminal
101 * and revoke access to controlling terminal.
a38b908e 102 */
e149007b
MK
103 if (sp->s_ttyp->t_session == sp) {
104 if (sp->s_ttyp->t_pgrp)
105 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
106 (void) ttywait(sp->s_ttyp);
107 vgoneall(sp->s_ttyvp);
108 }
a38b908e
MT
109 vrele(sp->s_ttyvp);
110 sp->s_ttyvp = NULL;
61fa8c82
MT
111 /*
112 * s_ttyp is not zero'd; we use this to indicate
113 * that the session once had a controlling terminal.
a38b908e 114 * (for logging and informational purposes)
61fa8c82 115 */
1ad494e6 116 }
e149007b 117 sp->s_leader = NULL;
1ad494e6 118 }
8429d022
MK
119 fixjobc(p, p->p_pgrp, 0);
120 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
d120e510 121 (void) acct(p);
8429d022
MK
122 if (--p->p_limit->p_refcnt == 0)
123 FREE(p->p_limit, M_SUBPROC);
124 if (--p->p_cred->p_refcnt == 0) {
125 crfree(p->p_cred->pc_ucred);
126 FREE(p->p_cred, M_SUBPROC);
127 }
1ad494e6
MK
128#ifdef KTRACE
129 /*
130 * release trace file
131 */
132 if (p->p_tracep)
c4ec2128 133 vrele(p->p_tracep);
a8560fa0 134#endif
8429d022
MK
135
136 /*
137 * Remove proc from allproc queue and pidhash chain.
138 * Place onto zombproc. Unlink from parent's child list.
139 */
140 if (*p->p_prev = p->p_nxt)
1d348849 141 p->p_nxt->p_prev = p->p_prev;
8429d022 142 if (p->p_nxt = zombproc)
1d348849
MK
143 p->p_nxt->p_prev = &p->p_nxt;
144 p->p_prev = &zombproc;
145 zombproc = p;
a8560fa0 146 p->p_stat = SZOMB;
9a5334ea 147 curproc = NULL;
4a438310
MK
148 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
149 if (*pp == p) {
150 *pp = p->p_hash;
151 goto done;
152 }
153 panic("exit");
154done:
8429d022 155
1d348849 156 if (p->p_cptr) /* only need this if any child is S_ZOMB */
8429d022 157 wakeup((caddr_t) initproc);
1d348849
MK
158 for (q = p->p_cptr; q != NULL; q = nq) {
159 nq = q->p_osptr;
160 if (nq != NULL)
161 nq->p_ysptr = NULL;
8429d022
MK
162 if (initproc->p_cptr)
163 initproc->p_cptr->p_ysptr = q;
164 q->p_osptr = initproc->p_cptr;
1d348849 165 q->p_ysptr = NULL;
8429d022 166 initproc->p_cptr = q;
a8560fa0 167
8429d022 168 q->p_pptr = initproc;
1d348849
MK
169 /*
170 * Traced processes are killed
171 * since their existence means someone is screwing up.
1d348849
MK
172 */
173 if (q->p_flag&STRC) {
174 q->p_flag &= ~STRC;
175 psignal(q, SIGKILL);
a8560fa0 176 }
1d348849 177 }
bdf8c113 178 p->p_cptr = NULL;
8429d022
MK
179
180 /*
181 * Save exit status and final rusage info,
182 * adding in child rusage info and self times.
183 */
184 p->p_xstat = rv;
185 *p->p_ru = p->p_stats->p_ru;
186 p->p_ru->ru_stime = p->p_stime;
187 p->p_ru->ru_utime = p->p_utime;
188 ruadd(p->p_ru, &p->p_stats->p_cru);
189
190 /*
191 * Notify parent that we're gone.
192 */
a8560fa0
SL
193 psignal(p->p_pptr, SIGCHLD);
194 wakeup((caddr_t)p->p_pptr);
fb1db32c 195#if defined(tahoe)
e149007b
MK
196 /* move this to cpu_exit */
197 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
fb1db32c 198#endif
8429d022 199 /*
e149007b
MK
200 * Finally, call machine-dependent code to release the remaining
201 * resources including address space, the kernel stack and pcb.
202 * The address space is released by "vmspace_free(p->p_vmspace)";
203 * This is machine-dependent, as we may have to change stacks
204 * or ensure that the current one isn't reallocated before we
205 * finish. cpu_exit will end with a call to swtch(), finishing
206 * our execution (pun intended).
8429d022 207 */
e149007b 208 cpu_exit(p);
8429d022 209 /* NOTREACHED */
a8560fa0
SL
210}
211
1ad494e6 212#ifdef COMPAT_43
1bfd8b20
KM
213owait(p, uap, retval)
214 struct proc *p;
215 register struct args {
1ad494e6 216 int pid;
a2528931 217 int *status;
1ad494e6
MK
218 int options;
219 struct rusage *rusage;
a2528931 220 int compat;
1bfd8b20
KM
221 } *uap;
222 int *retval;
223{
a8560fa0 224
8429d022 225 if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
c35e8b3f 226 uap->options = 0;
1ad494e6
MK
227 uap->rusage = 0;
228 } else {
8429d022
MK
229 uap->options = p->p_regs[R0];
230 uap->rusage = (struct rusage *)p->p_regs[R1];
a8560fa0 231 }
1ad494e6
MK
232 uap->pid = WAIT_ANY;
233 uap->status = 0;
a2528931 234 uap->compat = 1;
d9c2f47f 235 return (wait1(p, uap, retval));
1ad494e6
MK
236}
237
1bfd8b20
KM
238wait4(p, uap, retval)
239 struct proc *p;
240 struct args {
a2528931
MK
241 int pid;
242 int *status;
243 int options;
244 struct rusage *rusage;
245 int compat;
1bfd8b20
KM
246 } *uap;
247 int *retval;
248{
a2528931
MK
249
250 uap->compat = 0;
d9c2f47f 251 return (wait1(p, uap, retval));
a8560fa0 252}
a2528931
MK
253#else
254#define wait1 wait4
1ad494e6 255#endif
a8560fa0 256
a8560fa0 257/*
8429d022
MK
258 * Wait: check child processes to see if any have exited,
259 * stopped under trace, or (optionally) stopped by a signal.
260 * Pass back status and deallocate exited child's proc structure.
a8560fa0 261 */
1bfd8b20
KM
262wait1(q, uap, retval)
263 register struct proc *q;
264 register struct args {
1ad494e6 265 int pid;
a2528931 266 int *status;
1ad494e6
MK
267 int options;
268 struct rusage *rusage;
a2528931
MK
269#ifdef COMPAT_43
270 int compat;
271#endif
1bfd8b20
KM
272 } *uap;
273 int retval[];
274{
8429d022 275 register int nfound;
1bfd8b20 276 register struct proc *p;
a2528931 277 int status, error;
a8560fa0 278
1ad494e6
MK
279 if (uap->pid == 0)
280 uap->pid = -q->p_pgid;
c35e8b3f 281#ifdef notyet
a2528931 282 if (uap->options &~ (WUNTRACED|WNOHANG))
d9c2f47f 283 return (EINVAL);
c35e8b3f 284#endif
1ad494e6 285loop:
8429d022 286 nfound = 0;
1d348849 287 for (p = q->p_cptr; p; p = p->p_osptr) {
1ad494e6
MK
288 if (uap->pid != WAIT_ANY &&
289 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
290 continue;
8429d022 291 nfound++;
a8560fa0 292 if (p->p_stat == SZOMB) {
1bfd8b20 293 retval[0] = p->p_pid;
1ad494e6 294#ifdef COMPAT_43
a2528931 295 if (uap->compat)
1bfd8b20 296 retval[1] = p->p_xstat;
1ad494e6
MK
297 else
298#endif
299 if (uap->status) {
a2528931
MK
300 status = p->p_xstat; /* convert to int */
301 if (error = copyout((caddr_t)&status,
1ad494e6 302 (caddr_t)uap->status, sizeof(status)))
d9c2f47f 303 return (error);
5e63dd78 304 }
a2528931
MK
305 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
306 (caddr_t)uap->rusage, sizeof (struct rusage))))
d9c2f47f 307 return (error);
1ad494e6 308 p->p_xstat = 0;
8429d022 309 ruadd(&q->p_stats->p_cru, p->p_ru);
1ad494e6 310 FREE(p->p_ru, M_ZOMBIE);
8429d022
MK
311
312 /*
313 * Finally finished with old proc entry.
314 * Unlink it from its process group and free it.
315 */
316 leavepgrp(p);
1d348849
MK
317 if (*p->p_prev = p->p_nxt) /* off zombproc */
318 p->p_nxt->p_prev = p->p_prev;
a8560fa0
SL
319 if (q = p->p_ysptr)
320 q->p_osptr = p->p_osptr;
321 if (q = p->p_osptr)
322 q->p_ysptr = p->p_ysptr;
323 if ((q = p->p_pptr)->p_cptr == p)
324 q->p_cptr = p->p_osptr;
8429d022
MK
325 FREE(p, M_PROC);
326 nprocs--;
d9c2f47f 327 return (0);
a8560fa0 328 }
1ad494e6
MK
329 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
330 (p->p_flag & STRC || uap->options & WUNTRACED)) {
a8560fa0 331 p->p_flag |= SWTED;
1bfd8b20 332 retval[0] = p->p_pid;
1ad494e6 333#ifdef COMPAT_43
85d629e3 334 if (uap->compat) {
19dec745 335 retval[1] = W_STOPCODE(p->p_xstat);
85d629e3
KM
336 error = 0;
337 } else
1ad494e6
MK
338#endif
339 if (uap->status) {
19dec745 340 status = W_STOPCODE(p->p_xstat);
a2528931 341 error = copyout((caddr_t)&status,
1ad494e6 342 (caddr_t)uap->status, sizeof(status));
a2528931
MK
343 } else
344 error = 0;
d9c2f47f 345 return (error);
a8560fa0
SL
346 }
347 }
8429d022 348 if (nfound == 0)
d9c2f47f 349 return (ECHILD);
1ad494e6 350 if (uap->options & WNOHANG) {
1bfd8b20 351 retval[0] = 0;
d9c2f47f 352 return (0);
a8560fa0 353 }
1bfd8b20 354 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
d9c2f47f 355 return (error);
a8560fa0
SL
356 goto loop;
357}