add hashinit to size and allocate hash tables
[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 *
9e97623a 7 * @(#)kern_exit.c 7.48 (Berkeley) %G%
da7c5cc6 8 */
a8560fa0 9
94368568
JB
10#include "param.h"
11#include "systm.h"
12#include "map.h"
1bfd8b20 13#include "ioctl.h"
063b2136 14#include "proc.h"
1bfd8b20 15#include "tty.h"
8429d022
MK
16#include "time.h"
17#include "resource.h"
94368568 18#include "kernel.h"
94368568
JB
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"
efe68e1f 26#include "ptrace.h"
a8560fa0 27
e149007b 28#include "machine/cpu.h"
1ad494e6 29#ifdef COMPAT_43
8429d022 30#include "machine/reg.h"
d301d150 31#include "machine/psl.h"
1ad494e6
MK
32#endif
33
9a5334ea 34#include "vm/vm.h"
8429d022 35#include "vm/vm_kern.h"
9db58063 36
a8560fa0
SL
37/*
38 * Exit system call: pass back caller's arg
39 */
9e97623a
CT
40struct rexit_args {
41 int rval;
42};
1bfd8b20
KM
43/* ARGSUSED */
44rexit(p, uap, retval)
45 struct proc *p;
9e97623a 46 struct rexit_args *uap;
1bfd8b20
KM
47 int *retval;
48{
a8560fa0 49
5360383b
MK
50 exit(p, W_EXITCODE(uap->rval, 0));
51 /* NOTREACHED */
a8560fa0
SL
52}
53
54/*
8429d022
MK
55 * Exit: deallocate address space and other resources,
56 * change proc state to zombie, and unlink proc from allproc
57 * and parent's lists. Save exit status and rusage for wait().
58 * Check for child processes and orphan them.
a8560fa0 59 */
1bfd8b20 60exit(p, rv)
8429d022 61 register struct proc *p;
c35e8b3f 62 int rv;
a8560fa0 63{
1bfd8b20 64 register struct proc *q, *nq;
4a438310 65 register struct proc **pp;
7322266f 66 register struct vmspace *vm;
8429d022 67 int s;
a8560fa0 68
efe68e1f
KM
69 if (p->p_pid == 1)
70 panic("init died (signal %d, exit %d)",
71 WTERMSIG(rv), WEXITSTATUS(rv));
a8560fa0
SL
72#ifdef PGINPROF
73 vmsizmon();
74#endif
e0ef57b1
KM
75 if (p->p_flag & SPROFIL)
76 stopprofclock(p);
61d22ffc
KM
77 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
78 M_ZOMBIE, M_WAITOK);
8429d022
MK
79 /*
80 * If parent is waiting for us to exit or exec,
81 * SPPWAIT is set; we will wakeup the parent below.
82 */
9a5334ea 83 p->p_flag &= ~(STRC|SPPWAIT);
a8560fa0 84 p->p_flag |= SWEXIT;
dd012d1e 85 p->p_sigignore = ~0;
a2528931 86 p->p_sig = 0;
a8560fa0 87 untimeout(realitexpire, (caddr_t)p);
8429d022
MK
88
89 /*
90 * Close open files and release open-file table.
91 * This may block!
92 */
93 fdfree(p);
fe3cec6e
MK
94
95 /* The next two chunks should probably be moved to vmspace_exit. */
7322266f 96 vm = p->p_vmspace;
9db58063 97#ifdef SYSVSHM
7322266f 98 if (vm->vm_shm)
9db58063
KM
99 shmexit(p);
100#endif
fe3cec6e
MK
101 /*
102 * Release user portion of address space.
103 * This releases references to vnodes,
104 * which could cause I/O if the file has been unlinked.
105 * Need to do this early enough that we can still sleep.
106 * Can't free the entire vmspace as the kernel stack
107 * may be mapped within that space also.
108 */
7322266f
KM
109 if (vm->vm_refcnt == 1)
110 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
fe3cec6e 111 VM_MAXUSER_ADDRESS);
8429d022 112
1ad494e6 113 if (SESS_LEADER(p)) {
a38b908e
MT
114 register struct session *sp = p->p_session;
115
116 if (sp->s_ttyvp) {
117 /*
118 * Controlling process.
e149007b
MK
119 * Signal foreground pgrp,
120 * drain controlling terminal
121 * and revoke access to controlling terminal.
a38b908e 122 */
e149007b
MK
123 if (sp->s_ttyp->t_session == sp) {
124 if (sp->s_ttyp->t_pgrp)
125 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
126 (void) ttywait(sp->s_ttyp);
127 vgoneall(sp->s_ttyvp);
128 }
a38b908e
MT
129 vrele(sp->s_ttyvp);
130 sp->s_ttyvp = NULL;
61fa8c82
MT
131 /*
132 * s_ttyp is not zero'd; we use this to indicate
133 * that the session once had a controlling terminal.
a38b908e 134 * (for logging and informational purposes)
61fa8c82 135 */
1ad494e6 136 }
e149007b 137 sp->s_leader = NULL;
1ad494e6 138 }
8429d022
MK
139 fixjobc(p, p->p_pgrp, 0);
140 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
d120e510 141 (void) acct(p);
1ad494e6
MK
142#ifdef KTRACE
143 /*
144 * release trace file
145 */
d06df067 146 p->p_traceflag = 0; /* don't trace the vrele() */
1ad494e6 147 if (p->p_tracep)
c4ec2128 148 vrele(p->p_tracep);
a8560fa0 149#endif
8429d022
MK
150 /*
151 * Remove proc from allproc queue and pidhash chain.
152 * Place onto zombproc. Unlink from parent's child list.
153 */
154 if (*p->p_prev = p->p_nxt)
1d348849 155 p->p_nxt->p_prev = p->p_prev;
8429d022 156 if (p->p_nxt = zombproc)
1d348849
MK
157 p->p_nxt->p_prev = &p->p_nxt;
158 p->p_prev = &zombproc;
159 zombproc = p;
a8560fa0 160 p->p_stat = SZOMB;
de8789fe 161
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 /*
509987d3 195 * Save exit status and final rusage info, adding in child rusage
f3e27f1d 196 * info and self times.
8429d022
MK
197 */
198 p->p_xstat = rv;
199 *p->p_ru = p->p_stats->p_ru;
509987d3 200 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
8429d022
MK
201 ruadd(p->p_ru, &p->p_stats->p_cru);
202
203 /*
204 * Notify parent that we're gone.
205 */
a8560fa0
SL
206 psignal(p->p_pptr, SIGCHLD);
207 wakeup((caddr_t)p->p_pptr);
fb1db32c 208#if defined(tahoe)
e149007b
MK
209 /* move this to cpu_exit */
210 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
fb1db32c 211#endif
de8789fe
KM
212 /*
213 * Clear curproc after we've done all operations
214 * that could block, and before tearing down the rest
215 * of the process state that might be used from clock, etc.
216 * Also, can't clear curproc while we're still runnable,
217 * as we're not on a run queue (we are current, just not
218 * a proper proc any longer!).
219 *
220 * Other substructures are freed from wait().
221 */
222 curproc = NULL;
223 if (--p->p_limit->p_refcnt == 0)
224 FREE(p->p_limit, M_SUBPROC);
225
8429d022 226 /*
e149007b
MK
227 * Finally, call machine-dependent code to release the remaining
228 * resources including address space, the kernel stack and pcb.
229 * The address space is released by "vmspace_free(p->p_vmspace)";
230 * This is machine-dependent, as we may have to change stacks
231 * or ensure that the current one isn't reallocated before we
509987d3 232 * finish. cpu_exit will end with a call to cpu_swtch(), finishing
e149007b 233 * our execution (pun intended).
8429d022 234 */
e149007b 235 cpu_exit(p);
8429d022 236 /* NOTREACHED */
a8560fa0
SL
237}
238
9e97623a
CT
239struct wait_args {
240 int pid;
241 int *status;
242 int options;
243 struct rusage *rusage;
244#ifdef COMPAT_43
245 int compat; /* pseudo */
246#endif
247};
248
1ad494e6 249#ifdef COMPAT_43
1bfd8b20
KM
250owait(p, uap, retval)
251 struct proc *p;
9e97623a 252 register struct wait_args *uap;
1bfd8b20
KM
253 int *retval;
254{
a8560fa0 255
ba0aea50 256#ifdef PSL_ALLCC
5d398c16 257 if ((p->p_md.md_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
c35e8b3f 258 uap->options = 0;
9e97623a 259 uap->rusage = NULL;
1ad494e6 260 } else {
5d398c16
KM
261 uap->options = p->p_md.md_regs[R0];
262 uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
a8560fa0 263 }
ba0aea50
RC
264#else
265 uap->options = 0;
9e97623a 266 uap->rusage = NULL;
ba0aea50 267#endif
1ad494e6 268 uap->pid = WAIT_ANY;
9e97623a 269 uap->status = NULL;
a2528931 270 uap->compat = 1;
d9c2f47f 271 return (wait1(p, uap, retval));
1ad494e6
MK
272}
273
1bfd8b20
KM
274wait4(p, uap, retval)
275 struct proc *p;
9e97623a 276 struct wait_args *uap;
1bfd8b20
KM
277 int *retval;
278{
a2528931
MK
279
280 uap->compat = 0;
d9c2f47f 281 return (wait1(p, uap, retval));
a8560fa0 282}
a2528931
MK
283#else
284#define wait1 wait4
1ad494e6 285#endif
a8560fa0 286
a8560fa0 287/*
8429d022
MK
288 * Wait: check child processes to see if any have exited,
289 * stopped under trace, or (optionally) stopped by a signal.
290 * Pass back status and deallocate exited child's proc structure.
a8560fa0 291 */
1bfd8b20
KM
292wait1(q, uap, retval)
293 register struct proc *q;
9e97623a 294 register struct wait_args *uap;
1bfd8b20
KM
295 int retval[];
296{
8429d022 297 register int nfound;
efe68e1f 298 register struct proc *p, *t;
a2528931 299 int status, error;
a8560fa0 300
1ad494e6
MK
301 if (uap->pid == 0)
302 uap->pid = -q->p_pgid;
c35e8b3f 303#ifdef notyet
a2528931 304 if (uap->options &~ (WUNTRACED|WNOHANG))
d9c2f47f 305 return (EINVAL);
c35e8b3f 306#endif
1ad494e6 307loop:
8429d022 308 nfound = 0;
1d348849 309 for (p = q->p_cptr; p; p = p->p_osptr) {
1ad494e6
MK
310 if (uap->pid != WAIT_ANY &&
311 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
312 continue;
8429d022 313 nfound++;
a8560fa0 314 if (p->p_stat == SZOMB) {
1bfd8b20 315 retval[0] = p->p_pid;
1ad494e6 316#ifdef COMPAT_43
a2528931 317 if (uap->compat)
1bfd8b20 318 retval[1] = p->p_xstat;
1ad494e6
MK
319 else
320#endif
321 if (uap->status) {
a2528931
MK
322 status = p->p_xstat; /* convert to int */
323 if (error = copyout((caddr_t)&status,
1ad494e6 324 (caddr_t)uap->status, sizeof(status)))
d9c2f47f 325 return (error);
5e63dd78 326 }
a2528931
MK
327 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
328 (caddr_t)uap->rusage, sizeof (struct rusage))))
d9c2f47f 329 return (error);
efe68e1f
KM
330 /*
331 * If we got the child via a ptrace 'attach',
332 * we need to give it back to the old parent.
333 */
334 if (p->p_oppid && (t = pfind(p->p_oppid))) {
335 p->p_oppid = 0;
336 proc_reparent(p, t);
337 psignal(t, SIGCHLD);
338 wakeup((caddr_t)t);
339 return (0);
340 }
1ad494e6 341 p->p_xstat = 0;
8429d022 342 ruadd(&q->p_stats->p_cru, p->p_ru);
1ad494e6 343 FREE(p->p_ru, M_ZOMBIE);
fe3cec6e
MK
344 if (--p->p_cred->p_refcnt == 0) {
345 crfree(p->p_cred->pc_ucred);
346 FREE(p->p_cred, M_SUBPROC);
347 }
8429d022
MK
348
349 /*
350 * Finally finished with old proc entry.
351 * Unlink it from its process group and free it.
352 */
353 leavepgrp(p);
1d348849
MK
354 if (*p->p_prev = p->p_nxt) /* off zombproc */
355 p->p_nxt->p_prev = p->p_prev;
a8560fa0
SL
356 if (q = p->p_ysptr)
357 q->p_osptr = p->p_osptr;
358 if (q = p->p_osptr)
359 q->p_ysptr = p->p_ysptr;
360 if ((q = p->p_pptr)->p_cptr == p)
361 q->p_cptr = p->p_osptr;
4021f565 362
fe3cec6e
MK
363 /*
364 * Give machine-dependent layer a chance
365 * to free anything that cpu_exit couldn't
366 * release while still running in process context.
367 */
368 cpu_wait(p);
8429d022
MK
369 FREE(p, M_PROC);
370 nprocs--;
d9c2f47f 371 return (0);
a8560fa0 372 }
1ad494e6
MK
373 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
374 (p->p_flag & STRC || uap->options & WUNTRACED)) {
a8560fa0 375 p->p_flag |= SWTED;
1bfd8b20 376 retval[0] = p->p_pid;
1ad494e6 377#ifdef COMPAT_43
85d629e3 378 if (uap->compat) {
19dec745 379 retval[1] = W_STOPCODE(p->p_xstat);
85d629e3
KM
380 error = 0;
381 } else
1ad494e6
MK
382#endif
383 if (uap->status) {
19dec745 384 status = W_STOPCODE(p->p_xstat);
a2528931 385 error = copyout((caddr_t)&status,
ba0aea50 386 (caddr_t)uap->status, sizeof(status));
a2528931
MK
387 } else
388 error = 0;
d9c2f47f 389 return (error);
a8560fa0
SL
390 }
391 }
8429d022 392 if (nfound == 0)
d9c2f47f 393 return (ECHILD);
1ad494e6 394 if (uap->options & WNOHANG) {
1bfd8b20 395 retval[0] = 0;
d9c2f47f 396 return (0);
a8560fa0 397 }
1bfd8b20 398 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
d9c2f47f 399 return (error);
a8560fa0
SL
400 goto loop;
401}
efe68e1f
KM
402
403/*
404 * make process 'parent' the new parent of process 'child'.
405 */
406void
407proc_reparent(child, parent)
408 register struct proc *child;
409 register struct proc *parent;
410{
411 register struct proc *o;
412 register struct proc *y;
413
414 if (child->p_pptr == parent)
415 return;
416
417 /* fix up the child linkage for the old parent */
418 o = child->p_osptr;
419 y = child->p_ysptr;
420 if (y)
421 y->p_osptr = o;
422 if (o)
423 o->p_ysptr = y;
424 if (child->p_pptr->p_cptr == child)
425 child->p_pptr->p_cptr = o;
426
427 /* fix up child linkage for new parent */
428 o = parent->p_cptr;
429 if (o)
430 o->p_ysptr = child;
431 child->p_osptr = o;
432 child->p_ysptr = NULL;
433 parent->p_cptr = child;
434 child->p_pptr = parent;
435}