fix olstat to give the correct symlink info
[unix-history] / usr / src / sys / kern / kern_exit.c
CommitLineData
da7c5cc6 1/*
2e40cc0a
KB
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
adb35f79
KB
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
da7c5cc6 9 *
dbf0c423 10 * %sccs.include.redist.c%
c4ec2128 11 *
3ef0a118 12 * @(#)kern_exit.c 8.8 (Berkeley) %G%
da7c5cc6 13 */
a8560fa0 14
38a01dbe
KB
15#include <sys/param.h>
16#include <sys/systm.h>
17#include <sys/map.h>
18#include <sys/ioctl.h>
19#include <sys/proc.h>
20#include <sys/tty.h>
21#include <sys/time.h>
22#include <sys/resource.h>
23#include <sys/kernel.h>
24#include <sys/buf.h>
25#include <sys/wait.h>
26#include <sys/file.h>
27#include <sys/vnode.h>
28#include <sys/syslog.h>
29#include <sys/malloc.h>
30#include <sys/resourcevar.h>
31#include <sys/ptrace.h>
a8560fa0 32
38a01dbe 33#include <machine/cpu.h>
1ad494e6 34#ifdef COMPAT_43
38a01dbe
KB
35#include <machine/reg.h>
36#include <machine/psl.h>
1ad494e6
MK
37#endif
38
38a01dbe
KB
39#include <vm/vm.h>
40#include <vm/vm_kern.h>
9db58063 41
8c065ade
KB
42__dead void cpu_exit __P((struct proc *));
43__dead void exit1 __P((struct proc *, int));
44
a8560fa0 45/*
1732aa9d
KB
46 * exit --
47 * Death of process.
a8560fa0 48 */
9e97623a
CT
49struct rexit_args {
50 int rval;
51};
8c065ade 52__dead void
1732aa9d 53exit(p, uap, retval)
1bfd8b20 54 struct proc *p;
9e97623a 55 struct rexit_args *uap;
1bfd8b20
KM
56 int *retval;
57{
a8560fa0 58
817f339a 59 exit1(p, W_EXITCODE(uap->rval, 0));
5360383b 60 /* NOTREACHED */
a8560fa0
SL
61}
62
63/*
1732aa9d
KB
64 * Exit: deallocate address space and other resources, change proc state
65 * to zombie, and unlink proc from allproc and parent's lists. Save exit
66 * status and rusage for wait(). Check for child processes and orphan them.
a8560fa0 67 */
8c065ade 68__dead void
817f339a 69exit1(p, rv)
8429d022 70 register struct proc *p;
c35e8b3f 71 int rv;
a8560fa0 72{
1bfd8b20 73 register struct proc *q, *nq;
4a438310 74 register struct proc **pp;
7322266f 75 register struct vmspace *vm;
a8560fa0 76
efe68e1f
KM
77 if (p->p_pid == 1)
78 panic("init died (signal %d, exit %d)",
79 WTERMSIG(rv), WEXITSTATUS(rv));
a8560fa0
SL
80#ifdef PGINPROF
81 vmsizmon();
82#endif
cf5ef508 83 if (p->p_flag & P_PROFIL)
e0ef57b1 84 stopprofclock(p);
61d22ffc
KM
85 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
86 M_ZOMBIE, M_WAITOK);
8429d022
MK
87 /*
88 * If parent is waiting for us to exit or exec,
cf5ef508 89 * P_PPWAIT is set; we will wakeup the parent below.
8429d022 90 */
cf5ef508
KB
91 p->p_flag &= ~(P_TRACED | P_PPWAIT);
92 p->p_flag |= P_WEXIT;
dd012d1e 93 p->p_sigignore = ~0;
cf5ef508 94 p->p_siglist = 0;
a8560fa0 95 untimeout(realitexpire, (caddr_t)p);
8429d022
MK
96
97 /*
98 * Close open files and release open-file table.
99 * This may block!
100 */
101 fdfree(p);
fe3cec6e
MK
102
103 /* The next two chunks should probably be moved to vmspace_exit. */
7322266f 104 vm = p->p_vmspace;
9db58063 105#ifdef SYSVSHM
7322266f 106 if (vm->vm_shm)
9db58063
KM
107 shmexit(p);
108#endif
fe3cec6e
MK
109 /*
110 * Release user portion of address space.
111 * This releases references to vnodes,
112 * which could cause I/O if the file has been unlinked.
113 * Need to do this early enough that we can still sleep.
114 * Can't free the entire vmspace as the kernel stack
115 * may be mapped within that space also.
116 */
7322266f
KM
117 if (vm->vm_refcnt == 1)
118 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
fe3cec6e 119 VM_MAXUSER_ADDRESS);
8429d022 120
1ad494e6 121 if (SESS_LEADER(p)) {
a38b908e
MT
122 register struct session *sp = p->p_session;
123
124 if (sp->s_ttyvp) {
125 /*
126 * Controlling process.
e149007b
MK
127 * Signal foreground pgrp,
128 * drain controlling terminal
129 * and revoke access to controlling terminal.
a38b908e 130 */
e149007b
MK
131 if (sp->s_ttyp->t_session == sp) {
132 if (sp->s_ttyp->t_pgrp)
133 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
134 (void) ttywait(sp->s_ttyp);
1fa9a14f
MK
135 /*
136 * The tty could have been revoked
137 * if we blocked.
138 */
139 if (sp->s_ttyvp)
140 vgoneall(sp->s_ttyvp);
e149007b 141 }
1fa9a14f
MK
142 if (sp->s_ttyvp)
143 vrele(sp->s_ttyvp);
a38b908e 144 sp->s_ttyvp = NULL;
61fa8c82
MT
145 /*
146 * s_ttyp is not zero'd; we use this to indicate
147 * that the session once had a controlling terminal.
a38b908e 148 * (for logging and informational purposes)
61fa8c82 149 */
1ad494e6 150 }
e149007b 151 sp->s_leader = NULL;
1ad494e6 152 }
8429d022
MK
153 fixjobc(p, p->p_pgrp, 0);
154 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
52ec1332 155 (void)acct_process(p);
1ad494e6
MK
156#ifdef KTRACE
157 /*
158 * release trace file
159 */
d06df067 160 p->p_traceflag = 0; /* don't trace the vrele() */
1ad494e6 161 if (p->p_tracep)
c4ec2128 162 vrele(p->p_tracep);
a8560fa0 163#endif
8429d022
MK
164 /*
165 * Remove proc from allproc queue and pidhash chain.
166 * Place onto zombproc. Unlink from parent's child list.
167 */
3ef0a118
KM
168 LIST_REMOVE(p, p_list);
169 LIST_INSERT_HEAD(&zombproc, p, p_list);
a8560fa0 170 p->p_stat = SZOMB;
de8789fe 171
3ef0a118 172 LIST_REMOVE(p, p_hash);
8429d022 173
3ef0a118
KM
174 q = p->p_children.lh_first;
175 if (q) /* only need this if any child is S_ZOMB */
8429d022 176 wakeup((caddr_t) initproc);
3ef0a118
KM
177 for (; q != 0; q = nq) {
178 nq = q->p_sibling.le_next;
179 LIST_REMOVE(q, p_sibling);
180 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
8429d022 181 q->p_pptr = initproc;
1d348849
MK
182 /*
183 * Traced processes are killed
184 * since their existence means someone is screwing up.
1d348849 185 */
cf5ef508
KB
186 if (q->p_flag & P_TRACED) {
187 q->p_flag &= ~P_TRACED;
1d348849 188 psignal(q, SIGKILL);
a8560fa0 189 }
1d348849 190 }
8429d022
MK
191
192 /*
509987d3 193 * Save exit status and final rusage info, adding in child rusage
f3e27f1d 194 * info and self times.
8429d022
MK
195 */
196 p->p_xstat = rv;
197 *p->p_ru = p->p_stats->p_ru;
509987d3 198 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
8429d022
MK
199 ruadd(p->p_ru, &p->p_stats->p_cru);
200
201 /*
202 * Notify parent that we're gone.
203 */
a8560fa0
SL
204 psignal(p->p_pptr, SIGCHLD);
205 wakeup((caddr_t)p->p_pptr);
fb1db32c 206#if defined(tahoe)
e149007b
MK
207 /* move this to cpu_exit */
208 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
fb1db32c 209#endif
de8789fe
KM
210 /*
211 * Clear curproc after we've done all operations
212 * that could block, and before tearing down the rest
213 * of the process state that might be used from clock, etc.
214 * Also, can't clear curproc while we're still runnable,
215 * as we're not on a run queue (we are current, just not
216 * a proper proc any longer!).
217 *
218 * Other substructures are freed from wait().
219 */
220 curproc = NULL;
221 if (--p->p_limit->p_refcnt == 0)
222 FREE(p->p_limit, M_SUBPROC);
223
8429d022 224 /*
e149007b
MK
225 * Finally, call machine-dependent code to release the remaining
226 * resources including address space, the kernel stack and pcb.
227 * The address space is released by "vmspace_free(p->p_vmspace)";
228 * This is machine-dependent, as we may have to change stacks
229 * or ensure that the current one isn't reallocated before we
509987d3 230 * finish. cpu_exit will end with a call to cpu_swtch(), finishing
e149007b 231 * our execution (pun intended).
8429d022 232 */
e149007b 233 cpu_exit(p);
a8560fa0
SL
234}
235
9e97623a
CT
236struct wait_args {
237 int pid;
238 int *status;
239 int options;
240 struct rusage *rusage;
241#ifdef COMPAT_43
242 int compat; /* pseudo */
243#endif
244};
245
1ad494e6 246#ifdef COMPAT_43
2a81c17c 247#if defined(hp300) || defined(luna68k)
ef07a225
MH
248#include <machine/frame.h>
249#define GETPS(rp) ((struct frame *)(rp))->f_sr
250#else
251#define GETPS(rp) (rp)[PS]
252#endif
253
1bfd8b20
KM
254owait(p, uap, retval)
255 struct proc *p;
9e97623a 256 register struct wait_args *uap;
1bfd8b20
KM
257 int *retval;
258{
a8560fa0 259
ba0aea50 260#ifdef PSL_ALLCC
ef07a225 261 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
c35e8b3f 262 uap->options = 0;
9e97623a 263 uap->rusage = NULL;
1ad494e6 264 } else {
5d398c16
KM
265 uap->options = p->p_md.md_regs[R0];
266 uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
a8560fa0 267 }
ba0aea50
RC
268#else
269 uap->options = 0;
9e97623a 270 uap->rusage = NULL;
ba0aea50 271#endif
1ad494e6 272 uap->pid = WAIT_ANY;
9e97623a 273 uap->status = NULL;
a2528931 274 uap->compat = 1;
d9c2f47f 275 return (wait1(p, uap, retval));
1ad494e6
MK
276}
277
1bfd8b20
KM
278wait4(p, uap, retval)
279 struct proc *p;
9e97623a 280 struct wait_args *uap;
1bfd8b20
KM
281 int *retval;
282{
a2528931
MK
283
284 uap->compat = 0;
d9c2f47f 285 return (wait1(p, uap, retval));
a8560fa0 286}
a2528931
MK
287#else
288#define wait1 wait4
1ad494e6 289#endif
a8560fa0 290
cb84e0ab 291int
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;
3ef0a118 309 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
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);
db8fc89d
KM
344
345 /*
346 * Decrement the count of procs running with this uid.
347 */
348 (void)chgproccnt(p->p_cred->p_ruid, -1);
349
350 /*
351 * Free up credentials.
352 */
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 357
859437ef
JSP
358 /*
359 * Release reference to text vnode
360 */
361 if (p->p_textvp)
362 vrele(p->p_textvp);
363
8429d022
MK
364 /*
365 * Finally finished with old proc entry.
366 * Unlink it from its process group and free it.
367 */
368 leavepgrp(p);
3ef0a118
KM
369 LIST_REMOVE(p, p_list); /* off zombproc */
370 LIST_REMOVE(p, p_sibling);
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 }
cf5ef508
KB
382 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
383 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
384 p->p_flag |= P_WAITED;
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{
efe68e1f
KM
420
421 if (child->p_pptr == parent)
422 return;
423
3ef0a118
KM
424 LIST_REMOVE(child, p_sibling);
425 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
efe68e1f
KM
426 child->p_pptr = parent;
427}