do not return from sigsuspend prematurely
[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 *
817f339a 7 * @(#)kern_exit.c 7.52 (Berkeley) %G%
da7c5cc6 8 */
a8560fa0 9
38a01dbe
KB
10#include <sys/param.h>
11#include <sys/systm.h>
12#include <sys/map.h>
13#include <sys/ioctl.h>
14#include <sys/proc.h>
15#include <sys/tty.h>
16#include <sys/time.h>
17#include <sys/resource.h>
18#include <sys/kernel.h>
19#include <sys/buf.h>
20#include <sys/wait.h>
21#include <sys/file.h>
22#include <sys/vnode.h>
23#include <sys/syslog.h>
24#include <sys/malloc.h>
25#include <sys/resourcevar.h>
26#include <sys/ptrace.h>
a8560fa0 27
38a01dbe 28#include <machine/cpu.h>
1ad494e6 29#ifdef COMPAT_43
38a01dbe
KB
30#include <machine/reg.h>
31#include <machine/psl.h>
1ad494e6
MK
32#endif
33
38a01dbe
KB
34#include <vm/vm.h>
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
817f339a 50 exit1(p, W_EXITCODE(uap->rval, 0));
5360383b 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 */
817f339a 60exit1(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
ef07a225
MH
250#ifdef hp300
251#include <machine/frame.h>
252#define GETPS(rp) ((struct frame *)(rp))->f_sr
253#else
254#define GETPS(rp) (rp)[PS]
255#endif
256
1bfd8b20
KM
257owait(p, uap, retval)
258 struct proc *p;
9e97623a 259 register struct wait_args *uap;
1bfd8b20
KM
260 int *retval;
261{
a8560fa0 262
ba0aea50 263#ifdef PSL_ALLCC
ef07a225 264 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
c35e8b3f 265 uap->options = 0;
9e97623a 266 uap->rusage = NULL;
1ad494e6 267 } else {
5d398c16
KM
268 uap->options = p->p_md.md_regs[R0];
269 uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
a8560fa0 270 }
ba0aea50
RC
271#else
272 uap->options = 0;
9e97623a 273 uap->rusage = NULL;
ba0aea50 274#endif
1ad494e6 275 uap->pid = WAIT_ANY;
9e97623a 276 uap->status = NULL;
a2528931 277 uap->compat = 1;
d9c2f47f 278 return (wait1(p, uap, retval));
1ad494e6
MK
279}
280
1bfd8b20
KM
281wait4(p, uap, retval)
282 struct proc *p;
9e97623a 283 struct wait_args *uap;
1bfd8b20
KM
284 int *retval;
285{
a2528931
MK
286
287 uap->compat = 0;
d9c2f47f 288 return (wait1(p, uap, retval));
a8560fa0 289}
a2528931
MK
290#else
291#define wait1 wait4
1ad494e6 292#endif
a8560fa0 293
a8560fa0 294/*
8429d022
MK
295 * Wait: check child processes to see if any have exited,
296 * stopped under trace, or (optionally) stopped by a signal.
297 * Pass back status and deallocate exited child's proc structure.
a8560fa0 298 */
1bfd8b20
KM
299wait1(q, uap, retval)
300 register struct proc *q;
9e97623a 301 register struct wait_args *uap;
1bfd8b20
KM
302 int retval[];
303{
8429d022 304 register int nfound;
efe68e1f 305 register struct proc *p, *t;
a2528931 306 int status, error;
a8560fa0 307
1ad494e6
MK
308 if (uap->pid == 0)
309 uap->pid = -q->p_pgid;
c35e8b3f 310#ifdef notyet
a2528931 311 if (uap->options &~ (WUNTRACED|WNOHANG))
d9c2f47f 312 return (EINVAL);
c35e8b3f 313#endif
1ad494e6 314loop:
8429d022 315 nfound = 0;
1d348849 316 for (p = q->p_cptr; p; p = p->p_osptr) {
1ad494e6
MK
317 if (uap->pid != WAIT_ANY &&
318 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
319 continue;
8429d022 320 nfound++;
a8560fa0 321 if (p->p_stat == SZOMB) {
1bfd8b20 322 retval[0] = p->p_pid;
1ad494e6 323#ifdef COMPAT_43
a2528931 324 if (uap->compat)
1bfd8b20 325 retval[1] = p->p_xstat;
1ad494e6
MK
326 else
327#endif
328 if (uap->status) {
a2528931
MK
329 status = p->p_xstat; /* convert to int */
330 if (error = copyout((caddr_t)&status,
1ad494e6 331 (caddr_t)uap->status, sizeof(status)))
d9c2f47f 332 return (error);
5e63dd78 333 }
a2528931
MK
334 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
335 (caddr_t)uap->rusage, sizeof (struct rusage))))
d9c2f47f 336 return (error);
efe68e1f
KM
337 /*
338 * If we got the child via a ptrace 'attach',
339 * we need to give it back to the old parent.
340 */
341 if (p->p_oppid && (t = pfind(p->p_oppid))) {
342 p->p_oppid = 0;
343 proc_reparent(p, t);
344 psignal(t, SIGCHLD);
345 wakeup((caddr_t)t);
346 return (0);
347 }
1ad494e6 348 p->p_xstat = 0;
8429d022 349 ruadd(&q->p_stats->p_cru, p->p_ru);
1ad494e6 350 FREE(p->p_ru, M_ZOMBIE);
db8fc89d
KM
351
352 /*
353 * Decrement the count of procs running with this uid.
354 */
355 (void)chgproccnt(p->p_cred->p_ruid, -1);
356
357 /*
358 * Free up credentials.
359 */
fe3cec6e
MK
360 if (--p->p_cred->p_refcnt == 0) {
361 crfree(p->p_cred->pc_ucred);
362 FREE(p->p_cred, M_SUBPROC);
363 }
8429d022
MK
364
365 /*
366 * Finally finished with old proc entry.
367 * Unlink it from its process group and free it.
368 */
369 leavepgrp(p);
1d348849
MK
370 if (*p->p_prev = p->p_nxt) /* off zombproc */
371 p->p_nxt->p_prev = p->p_prev;
a8560fa0
SL
372 if (q = p->p_ysptr)
373 q->p_osptr = p->p_osptr;
374 if (q = p->p_osptr)
375 q->p_ysptr = p->p_ysptr;
376 if ((q = p->p_pptr)->p_cptr == p)
377 q->p_cptr = p->p_osptr;
4021f565 378
fe3cec6e
MK
379 /*
380 * Give machine-dependent layer a chance
381 * to free anything that cpu_exit couldn't
382 * release while still running in process context.
383 */
384 cpu_wait(p);
8429d022
MK
385 FREE(p, M_PROC);
386 nprocs--;
d9c2f47f 387 return (0);
a8560fa0 388 }
1ad494e6
MK
389 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
390 (p->p_flag & STRC || uap->options & WUNTRACED)) {
a8560fa0 391 p->p_flag |= SWTED;
1bfd8b20 392 retval[0] = p->p_pid;
1ad494e6 393#ifdef COMPAT_43
85d629e3 394 if (uap->compat) {
19dec745 395 retval[1] = W_STOPCODE(p->p_xstat);
85d629e3
KM
396 error = 0;
397 } else
1ad494e6
MK
398#endif
399 if (uap->status) {
19dec745 400 status = W_STOPCODE(p->p_xstat);
a2528931 401 error = copyout((caddr_t)&status,
ba0aea50 402 (caddr_t)uap->status, sizeof(status));
a2528931
MK
403 } else
404 error = 0;
d9c2f47f 405 return (error);
a8560fa0
SL
406 }
407 }
8429d022 408 if (nfound == 0)
d9c2f47f 409 return (ECHILD);
1ad494e6 410 if (uap->options & WNOHANG) {
1bfd8b20 411 retval[0] = 0;
d9c2f47f 412 return (0);
a8560fa0 413 }
1bfd8b20 414 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
d9c2f47f 415 return (error);
a8560fa0
SL
416 goto loop;
417}
efe68e1f
KM
418
419/*
420 * make process 'parent' the new parent of process 'child'.
421 */
422void
423proc_reparent(child, parent)
424 register struct proc *child;
425 register struct proc *parent;
426{
427 register struct proc *o;
428 register struct proc *y;
429
430 if (child->p_pptr == parent)
431 return;
432
433 /* fix up the child linkage for the old parent */
434 o = child->p_osptr;
435 y = child->p_ysptr;
436 if (y)
437 y->p_osptr = o;
438 if (o)
439 o->p_ysptr = y;
440 if (child->p_pptr->p_cptr == child)
441 child->p_pptr->p_cptr = o;
442
443 /* fix up child linkage for new parent */
444 o = parent->p_cptr;
445 if (o)
446 o->p_ysptr = child;
447 child->p_osptr = o;
448 child->p_ysptr = NULL;
449 parent->p_cptr = child;
450 child->p_pptr = parent;
451}