reorganization to move ufsmount ops to be vnode ops;
[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 *
a0f03348 7 * @(#)kern_exit.c 7.38 (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
ba0aea50 239#ifdef PSL_ALLCC
8429d022 240 if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
c35e8b3f 241 uap->options = 0;
1ad494e6
MK
242 uap->rusage = 0;
243 } else {
8429d022
MK
244 uap->options = p->p_regs[R0];
245 uap->rusage = (struct rusage *)p->p_regs[R1];
a8560fa0 246 }
ba0aea50
RC
247#else
248 uap->options = 0;
249 uap->rusage = 0;
250#endif
1ad494e6
MK
251 uap->pid = WAIT_ANY;
252 uap->status = 0;
a2528931 253 uap->compat = 1;
d9c2f47f 254 return (wait1(p, uap, retval));
1ad494e6
MK
255}
256
1bfd8b20
KM
257wait4(p, uap, retval)
258 struct proc *p;
259 struct args {
a2528931
MK
260 int pid;
261 int *status;
262 int options;
263 struct rusage *rusage;
264 int compat;
1bfd8b20
KM
265 } *uap;
266 int *retval;
267{
a2528931
MK
268
269 uap->compat = 0;
d9c2f47f 270 return (wait1(p, uap, retval));
a8560fa0 271}
a2528931
MK
272#else
273#define wait1 wait4
1ad494e6 274#endif
a8560fa0 275
a8560fa0 276/*
8429d022
MK
277 * Wait: check child processes to see if any have exited,
278 * stopped under trace, or (optionally) stopped by a signal.
279 * Pass back status and deallocate exited child's proc structure.
a8560fa0 280 */
1bfd8b20
KM
281wait1(q, uap, retval)
282 register struct proc *q;
283 register struct args {
1ad494e6 284 int pid;
a2528931 285 int *status;
1ad494e6
MK
286 int options;
287 struct rusage *rusage;
a2528931
MK
288#ifdef COMPAT_43
289 int compat;
290#endif
1bfd8b20
KM
291 } *uap;
292 int retval[];
293{
8429d022 294 register int nfound;
1bfd8b20 295 register struct proc *p;
a2528931 296 int status, error;
a8560fa0 297
1ad494e6
MK
298 if (uap->pid == 0)
299 uap->pid = -q->p_pgid;
c35e8b3f 300#ifdef notyet
a2528931 301 if (uap->options &~ (WUNTRACED|WNOHANG))
d9c2f47f 302 return (EINVAL);
c35e8b3f 303#endif
1ad494e6 304loop:
8429d022 305 nfound = 0;
1d348849 306 for (p = q->p_cptr; p; p = p->p_osptr) {
1ad494e6
MK
307 if (uap->pid != WAIT_ANY &&
308 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
309 continue;
8429d022 310 nfound++;
a8560fa0 311 if (p->p_stat == SZOMB) {
1bfd8b20 312 retval[0] = p->p_pid;
1ad494e6 313#ifdef COMPAT_43
a2528931 314 if (uap->compat)
1bfd8b20 315 retval[1] = p->p_xstat;
1ad494e6
MK
316 else
317#endif
318 if (uap->status) {
a2528931
MK
319 status = p->p_xstat; /* convert to int */
320 if (error = copyout((caddr_t)&status,
1ad494e6 321 (caddr_t)uap->status, sizeof(status)))
d9c2f47f 322 return (error);
5e63dd78 323 }
a2528931
MK
324 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
325 (caddr_t)uap->rusage, sizeof (struct rusage))))
d9c2f47f 326 return (error);
1ad494e6 327 p->p_xstat = 0;
8429d022 328 ruadd(&q->p_stats->p_cru, p->p_ru);
1ad494e6 329 FREE(p->p_ru, M_ZOMBIE);
fe3cec6e
MK
330 if (--p->p_cred->p_refcnt == 0) {
331 crfree(p->p_cred->pc_ucred);
332 FREE(p->p_cred, M_SUBPROC);
333 }
8429d022
MK
334
335 /*
336 * Finally finished with old proc entry.
337 * Unlink it from its process group and free it.
338 */
339 leavepgrp(p);
1d348849
MK
340 if (*p->p_prev = p->p_nxt) /* off zombproc */
341 p->p_nxt->p_prev = p->p_prev;
a8560fa0
SL
342 if (q = p->p_ysptr)
343 q->p_osptr = p->p_osptr;
344 if (q = p->p_osptr)
345 q->p_ysptr = p->p_ysptr;
346 if ((q = p->p_pptr)->p_cptr == p)
347 q->p_cptr = p->p_osptr;
4021f565 348
fe3cec6e
MK
349 /*
350 * Give machine-dependent layer a chance
351 * to free anything that cpu_exit couldn't
352 * release while still running in process context.
353 */
354 cpu_wait(p);
8429d022
MK
355 FREE(p, M_PROC);
356 nprocs--;
d9c2f47f 357 return (0);
a8560fa0 358 }
1ad494e6
MK
359 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
360 (p->p_flag & STRC || uap->options & WUNTRACED)) {
a8560fa0 361 p->p_flag |= SWTED;
1bfd8b20 362 retval[0] = p->p_pid;
1ad494e6 363#ifdef COMPAT_43
85d629e3 364 if (uap->compat) {
19dec745 365 retval[1] = W_STOPCODE(p->p_xstat);
85d629e3
KM
366 error = 0;
367 } else
1ad494e6
MK
368#endif
369 if (uap->status) {
19dec745 370 status = W_STOPCODE(p->p_xstat);
a2528931 371 error = copyout((caddr_t)&status,
ba0aea50 372 (caddr_t)uap->status, sizeof(status));
a2528931
MK
373 } else
374 error = 0;
d9c2f47f 375 return (error);
a8560fa0
SL
376 }
377 }
8429d022 378 if (nfound == 0)
d9c2f47f 379 return (ECHILD);
1ad494e6 380 if (uap->options & WNOHANG) {
1bfd8b20 381 retval[0] = 0;
d9c2f47f 382 return (0);
a8560fa0 383 }
1bfd8b20 384 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
d9c2f47f 385 return (error);
a8560fa0
SL
386 goto loop;
387}