leave curproc set until last sleep, then clear before state is invalid
[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 *
967a7187 7 * @(#)kern_exit.c 7.36 (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);
fe3cec6e
MK
86
87 /* The next two chunks should probably be moved to vmspace_exit. */
9db58063 88#ifdef SYSVSHM
8429d022 89 if (p->p_vmspace->vm_shm)
9db58063
KM
90 shmexit(p);
91#endif
fe3cec6e
MK
92 /*
93 * Release user portion of address space.
94 * This releases references to vnodes,
95 * which could cause I/O if the file has been unlinked.
96 * Need to do this early enough that we can still sleep.
97 * Can't free the entire vmspace as the kernel stack
98 * may be mapped within that space also.
99 */
100 if (p->p_vmspace->vm_refcnt == 1)
101 (void) vm_map_remove(&p->p_vmspace->vm_map, VM_MIN_ADDRESS,
102 VM_MAXUSER_ADDRESS);
8429d022
MK
103
104 if (p->p_pid == 1)
105 panic("init died");
1ad494e6 106 if (SESS_LEADER(p)) {
a38b908e
MT
107 register struct session *sp = p->p_session;
108
109 if (sp->s_ttyvp) {
110 /*
111 * Controlling process.
e149007b
MK
112 * Signal foreground pgrp,
113 * drain controlling terminal
114 * and revoke access to controlling terminal.
a38b908e 115 */
e149007b
MK
116 if (sp->s_ttyp->t_session == sp) {
117 if (sp->s_ttyp->t_pgrp)
118 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
119 (void) ttywait(sp->s_ttyp);
120 vgoneall(sp->s_ttyvp);
121 }
a38b908e
MT
122 vrele(sp->s_ttyvp);
123 sp->s_ttyvp = NULL;
61fa8c82
MT
124 /*
125 * s_ttyp is not zero'd; we use this to indicate
126 * that the session once had a controlling terminal.
a38b908e 127 * (for logging and informational purposes)
61fa8c82 128 */
1ad494e6 129 }
e149007b 130 sp->s_leader = NULL;
1ad494e6 131 }
8429d022
MK
132 fixjobc(p, p->p_pgrp, 0);
133 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
d120e510 134 (void) acct(p);
1ad494e6
MK
135#ifdef KTRACE
136 /*
137 * release trace file
138 */
139 if (p->p_tracep)
c4ec2128 140 vrele(p->p_tracep);
a8560fa0 141#endif
967a7187
MK
142 /*
143 * Clear curproc after we've done all operations
144 * that could block, and before tearing down
145 * the rest of the process state.
146 */
147 curproc = NULL;
148 if (--p->p_limit->p_refcnt == 0)
149 FREE(p->p_limit, M_SUBPROC);
8429d022
MK
150
151 /*
152 * Remove proc from allproc queue and pidhash chain.
153 * Place onto zombproc. Unlink from parent's child list.
154 */
155 if (*p->p_prev = p->p_nxt)
1d348849 156 p->p_nxt->p_prev = p->p_prev;
8429d022 157 if (p->p_nxt = zombproc)
1d348849
MK
158 p->p_nxt->p_prev = &p->p_nxt;
159 p->p_prev = &zombproc;
160 zombproc = p;
a8560fa0 161 p->p_stat = SZOMB;
4a438310
MK
162 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
163 if (*pp == p) {
164 *pp = p->p_hash;
165 goto done;
166 }
167 panic("exit");
168done:
8429d022 169
1d348849 170 if (p->p_cptr) /* only need this if any child is S_ZOMB */
8429d022 171 wakeup((caddr_t) initproc);
1d348849
MK
172 for (q = p->p_cptr; q != NULL; q = nq) {
173 nq = q->p_osptr;
174 if (nq != NULL)
175 nq->p_ysptr = NULL;
8429d022
MK
176 if (initproc->p_cptr)
177 initproc->p_cptr->p_ysptr = q;
178 q->p_osptr = initproc->p_cptr;
1d348849 179 q->p_ysptr = NULL;
8429d022 180 initproc->p_cptr = q;
a8560fa0 181
8429d022 182 q->p_pptr = initproc;
1d348849
MK
183 /*
184 * Traced processes are killed
185 * since their existence means someone is screwing up.
1d348849
MK
186 */
187 if (q->p_flag&STRC) {
188 q->p_flag &= ~STRC;
189 psignal(q, SIGKILL);
a8560fa0 190 }
1d348849 191 }
bdf8c113 192 p->p_cptr = NULL;
8429d022
MK
193
194 /*
195 * Save exit status and final rusage info,
196 * adding in child rusage info and self times.
197 */
198 p->p_xstat = rv;
199 *p->p_ru = p->p_stats->p_ru;
200 p->p_ru->ru_stime = p->p_stime;
201 p->p_ru->ru_utime = p->p_utime;
202 ruadd(p->p_ru, &p->p_stats->p_cru);
203
204 /*
205 * Notify parent that we're gone.
206 */
a8560fa0
SL
207 psignal(p->p_pptr, SIGCHLD);
208 wakeup((caddr_t)p->p_pptr);
fb1db32c 209#if defined(tahoe)
e149007b
MK
210 /* move this to cpu_exit */
211 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
fb1db32c 212#endif
8429d022 213 /*
e149007b
MK
214 * Finally, call machine-dependent code to release the remaining
215 * resources including address space, the kernel stack and pcb.
216 * The address space is released by "vmspace_free(p->p_vmspace)";
217 * This is machine-dependent, as we may have to change stacks
218 * or ensure that the current one isn't reallocated before we
219 * finish. cpu_exit will end with a call to swtch(), finishing
220 * our execution (pun intended).
8429d022 221 */
e149007b 222 cpu_exit(p);
8429d022 223 /* NOTREACHED */
a8560fa0
SL
224}
225
1ad494e6 226#ifdef COMPAT_43
1bfd8b20
KM
227owait(p, uap, retval)
228 struct proc *p;
229 register struct args {
1ad494e6 230 int pid;
a2528931 231 int *status;
1ad494e6
MK
232 int options;
233 struct rusage *rusage;
a2528931 234 int compat;
1bfd8b20
KM
235 } *uap;
236 int *retval;
237{
a8560fa0 238
8429d022 239 if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
c35e8b3f 240 uap->options = 0;
1ad494e6
MK
241 uap->rusage = 0;
242 } else {
8429d022
MK
243 uap->options = p->p_regs[R0];
244 uap->rusage = (struct rusage *)p->p_regs[R1];
a8560fa0 245 }
1ad494e6
MK
246 uap->pid = WAIT_ANY;
247 uap->status = 0;
a2528931 248 uap->compat = 1;
d9c2f47f 249 return (wait1(p, uap, retval));
1ad494e6
MK
250}
251
1bfd8b20
KM
252wait4(p, uap, retval)
253 struct proc *p;
254 struct args {
a2528931
MK
255 int pid;
256 int *status;
257 int options;
258 struct rusage *rusage;
259 int compat;
1bfd8b20
KM
260 } *uap;
261 int *retval;
262{
a2528931
MK
263
264 uap->compat = 0;
d9c2f47f 265 return (wait1(p, uap, retval));
a8560fa0 266}
a2528931
MK
267#else
268#define wait1 wait4
1ad494e6 269#endif
a8560fa0 270
a8560fa0 271/*
8429d022
MK
272 * Wait: check child processes to see if any have exited,
273 * stopped under trace, or (optionally) stopped by a signal.
274 * Pass back status and deallocate exited child's proc structure.
a8560fa0 275 */
1bfd8b20
KM
276wait1(q, uap, retval)
277 register struct proc *q;
278 register struct args {
1ad494e6 279 int pid;
a2528931 280 int *status;
1ad494e6
MK
281 int options;
282 struct rusage *rusage;
a2528931
MK
283#ifdef COMPAT_43
284 int compat;
285#endif
1bfd8b20
KM
286 } *uap;
287 int retval[];
288{
8429d022 289 register int nfound;
1bfd8b20 290 register struct proc *p;
a2528931 291 int status, error;
a8560fa0 292
1ad494e6
MK
293 if (uap->pid == 0)
294 uap->pid = -q->p_pgid;
c35e8b3f 295#ifdef notyet
a2528931 296 if (uap->options &~ (WUNTRACED|WNOHANG))
d9c2f47f 297 return (EINVAL);
c35e8b3f 298#endif
1ad494e6 299loop:
8429d022 300 nfound = 0;
1d348849 301 for (p = q->p_cptr; p; p = p->p_osptr) {
1ad494e6
MK
302 if (uap->pid != WAIT_ANY &&
303 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
304 continue;
8429d022 305 nfound++;
a8560fa0 306 if (p->p_stat == SZOMB) {
1bfd8b20 307 retval[0] = p->p_pid;
1ad494e6 308#ifdef COMPAT_43
a2528931 309 if (uap->compat)
1bfd8b20 310 retval[1] = p->p_xstat;
1ad494e6
MK
311 else
312#endif
313 if (uap->status) {
a2528931
MK
314 status = p->p_xstat; /* convert to int */
315 if (error = copyout((caddr_t)&status,
1ad494e6 316 (caddr_t)uap->status, sizeof(status)))
d9c2f47f 317 return (error);
5e63dd78 318 }
a2528931
MK
319 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
320 (caddr_t)uap->rusage, sizeof (struct rusage))))
d9c2f47f 321 return (error);
1ad494e6 322 p->p_xstat = 0;
8429d022 323 ruadd(&q->p_stats->p_cru, p->p_ru);
1ad494e6 324 FREE(p->p_ru, M_ZOMBIE);
fe3cec6e
MK
325 if (--p->p_cred->p_refcnt == 0) {
326 crfree(p->p_cred->pc_ucred);
327 FREE(p->p_cred, M_SUBPROC);
328 }
8429d022
MK
329
330 /*
331 * Finally finished with old proc entry.
332 * Unlink it from its process group and free it.
333 */
334 leavepgrp(p);
1d348849
MK
335 if (*p->p_prev = p->p_nxt) /* off zombproc */
336 p->p_nxt->p_prev = p->p_prev;
a8560fa0
SL
337 if (q = p->p_ysptr)
338 q->p_osptr = p->p_osptr;
339 if (q = p->p_osptr)
340 q->p_ysptr = p->p_ysptr;
341 if ((q = p->p_pptr)->p_cptr == p)
342 q->p_cptr = p->p_osptr;
4021f565 343
fe3cec6e
MK
344 /*
345 * Give machine-dependent layer a chance
346 * to free anything that cpu_exit couldn't
347 * release while still running in process context.
348 */
349 cpu_wait(p);
8429d022
MK
350 FREE(p, M_PROC);
351 nprocs--;
d9c2f47f 352 return (0);
a8560fa0 353 }
1ad494e6
MK
354 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
355 (p->p_flag & STRC || uap->options & WUNTRACED)) {
a8560fa0 356 p->p_flag |= SWTED;
1bfd8b20 357 retval[0] = p->p_pid;
1ad494e6 358#ifdef COMPAT_43
85d629e3 359 if (uap->compat) {
19dec745 360 retval[1] = W_STOPCODE(p->p_xstat);
85d629e3
KM
361 error = 0;
362 } else
1ad494e6
MK
363#endif
364 if (uap->status) {
19dec745 365 status = W_STOPCODE(p->p_xstat);
a2528931 366 error = copyout((caddr_t)&status,
1ad494e6 367 (caddr_t)uap->status, sizeof(status));
a2528931
MK
368 } else
369 error = 0;
d9c2f47f 370 return (error);
a8560fa0
SL
371 }
372 }
8429d022 373 if (nfound == 0)
d9c2f47f 374 return (ECHILD);
1ad494e6 375 if (uap->options & WNOHANG) {
1bfd8b20 376 retval[0] = 0;
d9c2f47f 377 return (0);
a8560fa0 378 }
1bfd8b20 379 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
d9c2f47f 380 return (error);
a8560fa0
SL
381 goto loop;
382}