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