generalize the buffer pool so that NFS can become a client
[unix-history] / usr / src / sys / kern / kern_exit.c
CommitLineData
da7c5cc6 1/*
c4ec2128
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
c4ec2128
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * @(#)kern_exit.c 7.7 (Berkeley) %G%
da7c5cc6 18 */
a8560fa0 19
94368568
JB
20#include "param.h"
21#include "systm.h"
22#include "map.h"
94368568
JB
23#include "user.h"
24#include "kernel.h"
25#include "proc.h"
26#include "buf.h"
27#include "wait.h"
28#include "vm.h"
29#include "file.h"
c4ec2128 30#include "vnode.h"
1ad494e6 31#include "tty.h"
5e63dd78 32#include "syslog.h"
61d22ffc 33#include "malloc.h"
a8560fa0 34
d301d150 35#include "machine/reg.h"
1ad494e6 36#ifdef COMPAT_43
d301d150 37#include "machine/psl.h"
1ad494e6
MK
38#endif
39
a8560fa0
SL
40/*
41 * Exit system call: pass back caller's arg
42 */
43rexit()
44{
45 register struct a {
46 int rval;
47 } *uap;
1ad494e6 48 union wait status;
a8560fa0
SL
49
50 uap = (struct a *)u.u_ap;
1ad494e6
MK
51 status.w_status = 0;
52 status.w_retcode = uap->rval;
53 exit(status.w_status);
a8560fa0
SL
54}
55
56/*
57 * Release resources.
58 * Save u. area for parent to look at.
59 * Enter zombie state.
60 * Wake up parent and init processes,
61 * and dispose of children.
62 */
63exit(rv)
1ad494e6 64 int rv; /* should be union wait (XXX) */
a8560fa0
SL
65{
66 register int i;
1d348849 67 register struct proc *p, *q, *nq;
a8560fa0 68 register int x;
a8560fa0
SL
69
70#ifdef PGINPROF
71 vmsizmon();
72#endif
73 p = u.u_procp;
61d22ffc
KM
74 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
75 M_ZOMBIE, M_WAITOK);
a8560fa0
SL
76 p->p_flag &= ~(STRC|SULOCK);
77 p->p_flag |= SWEXIT;
dd012d1e 78 p->p_sigignore = ~0;
a8560fa0
SL
79 p->p_cpticks = 0;
80 p->p_pctcpu = 0;
dd012d1e 81 for (i = 0; i < NSIG; i++)
a8560fa0
SL
82 u.u_signal[i] = SIG_IGN;
83 untimeout(realitexpire, (caddr_t)p);
84 /*
85 * Release virtual memory. If we resulted from
86 * a vfork(), instead give the resources back to
87 * the parent.
88 */
89 if ((p->p_flag & SVFORK) == 0)
90 vrelvm();
91 else {
92 p->p_flag &= ~SVFORK;
93 wakeup((caddr_t)p);
94 while ((p->p_flag & SVFDONE) == 0)
95 sleep((caddr_t)p, PZERO - 1);
96 p->p_flag &= ~SVFDONE;
97 }
48faa1e9 98 for (i = 0; i <= u.u_lastfile; i++) {
a8560fa0 99 struct file *f;
a8560fa0
SL
100
101 f = u.u_ofile[i];
48faa1e9
MK
102 if (f) {
103 u.u_ofile[i] = NULL;
104 u.u_pofile[i] = 0;
105 closef(f);
106 }
a8560fa0 107 }
1ad494e6
MK
108 if (SESS_LEADER(p)) {
109 p->p_session->s_leader = 0;
110 /* TODO: vhangup(); */
111 if (u.u_ttyp) {
112 u.u_ttyp->t_session = 0;
113 u.u_ttyp->t_pgid = 0;
114 }
115 }
c4ec2128
KM
116 VOP_LOCK(u.u_cdir);
117 vput(u.u_cdir);
a8560fa0 118 if (u.u_rdir) {
c4ec2128
KM
119 VOP_LOCK(u.u_rdir);
120 vput(u.u_rdir);
a8560fa0
SL
121 }
122 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
123 acct();
124#ifdef QUOTA
125 qclean();
1ad494e6 126#endif
c4ec2128 127 crfree(u.u_cred);
1ad494e6
MK
128#ifdef KTRACE
129 /*
130 * release trace file
131 */
132 if (p->p_tracep)
c4ec2128 133 vrele(p->p_tracep);
a8560fa0 134#endif
1ad494e6 135 /*
db8289b8
MK
136 /*
137 * Freeing the user structure and kernel stack
138 * for the current process: have to run a bit longer
139 * using the pages which are about to be freed...
140 * vrelu will block memory allocation by raising ipl.
141 */
a8560fa0 142 vrelu(u.u_procp, 0);
db8289b8 143 vrelpt(u.u_procp);
1d348849
MK
144 if (*p->p_prev = p->p_nxt) /* off allproc queue */
145 p->p_nxt->p_prev = p->p_prev;
146 if (p->p_nxt = zombproc) /* onto zombproc */
147 p->p_nxt->p_prev = &p->p_nxt;
148 p->p_prev = &zombproc;
149 zombproc = p;
a8560fa0
SL
150 multprog--;
151 p->p_stat = SZOMB;
152 noproc = 1;
153 i = PIDHASH(p->p_pid);
154 x = p - proc;
155 if (pidhash[i] == x)
156 pidhash[i] = p->p_idhash;
157 else {
158 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
159 if (proc[i].p_idhash == x) {
160 proc[i].p_idhash = p->p_idhash;
161 goto done;
162 }
163 panic("exit");
164 }
48faa1e9
MK
165 if (p->p_pid == 1) {
166 if (p->p_dsize == 0) {
1ad494e6 167 printf("Can't exec init (errno %d)\n", rv >> 8);
48faa1e9
MK
168 for (;;)
169 ;
170 } else
171 panic("init died");
172 }
a8560fa0
SL
173done:
174 p->p_xstat = rv;
48faa1e9
MK
175 *p->p_ru = u.u_ru;
176 ruadd(p->p_ru, &u.u_cru);
1d348849
MK
177 if (p->p_cptr) /* only need this if any child is S_ZOMB */
178 wakeup((caddr_t)&proc[1]);
1ad494e6
MK
179 if (PGRP_JOBC(p))
180 p->p_pgrp->pg_jobc--;
1d348849 181 for (q = p->p_cptr; q != NULL; q = nq) {
1ad494e6
MK
182 if (PGRP_JOBC(q))
183 q->p_pgrp->pg_jobc--;
1d348849
MK
184 nq = q->p_osptr;
185 if (nq != NULL)
186 nq->p_ysptr = NULL;
187 if (proc[1].p_cptr)
188 proc[1].p_cptr->p_ysptr = q;
189 q->p_osptr = proc[1].p_cptr;
190 q->p_ysptr = NULL;
191 proc[1].p_cptr = q;
a8560fa0 192
1d348849
MK
193 q->p_pptr = &proc[1];
194 q->p_ppid = 1;
195 /*
196 * Traced processes are killed
197 * since their existence means someone is screwing up.
198 * Stopped processes are sent a hangup and a continue.
199 * This is designed to be ``safe'' for setuid
200 * processes since they must be willing to tolerate
201 * hangups anyways.
202 */
203 if (q->p_flag&STRC) {
204 q->p_flag &= ~STRC;
205 psignal(q, SIGKILL);
206 } else if (q->p_stat == SSTOP) {
207 psignal(q, SIGHUP);
208 psignal(q, SIGCONT);
a8560fa0 209 }
1d348849
MK
210 /*
211 * Protect this process from future
212 * tty signals, clear TSTP/TTIN/TTOU if pending.
213 */
f6ab5a0c 214 (void) spgrp(q);
1d348849 215 }
bdf8c113 216 p->p_cptr = NULL;
a8560fa0
SL
217 psignal(p->p_pptr, SIGCHLD);
218 wakeup((caddr_t)p->p_pptr);
fb1db32c
MK
219#if defined(tahoe)
220 dkeyrelease(p->p_dkey), p->p_dkey = 0;
221 ckeyrelease(p->p_ckey), p->p_ckey = 0;
222 u.u_pcb.pcb_savacc.faddr = (float *)NULL;
223#endif
a8560fa0
SL
224 swtch();
225}
226
1ad494e6
MK
227#ifdef COMPAT_43
228owait()
a8560fa0 229{
1ad494e6
MK
230 struct a {
231 int pid;
232 union wait *status;
233 int options;
234 struct rusage *rusage;
235 } *uap = (struct a *)u.u_ap;
a8560fa0
SL
236
237 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
1ad494e6
MK
238 uap->options = WSIGRESTART;
239 uap->rusage = 0;
240 } else {
241 uap->options = u.u_ar0[R0] | WSIGRESTART;
242 uap->rusage = (struct rusage *)u.u_ar0[R1];
a8560fa0 243 }
1ad494e6
MK
244 uap->pid = WAIT_ANY;
245 uap->status = 0;
246 wait1(1);
247}
248
249wait4()
250{
251 wait1(0);
a8560fa0 252}
1ad494e6 253#endif
a8560fa0 254
a8560fa0
SL
255/*
256 * Wait system call.
257 * Search for a terminated (zombie) child,
258 * finally lay it to rest, and collect its status.
259 * Look also for stopped (traced) children,
260 * and pass back status from them.
261 */
1ad494e6
MK
262#ifdef COMPAT_43
263wait1(compat)
264 int compat;
265#else
266wait4()
267#endif
a8560fa0 268{
1ad494e6
MK
269 register struct a {
270 int pid;
271 union wait *status;
272 int options;
273 struct rusage *rusage;
274 } *uap = (struct a *)u.u_ap;
a8560fa0
SL
275 register f;
276 register struct proc *p, *q;
1ad494e6 277 union wait status;
a8560fa0
SL
278
279 f = 0;
1d348849 280 q = u.u_procp;
1ad494e6
MK
281 if (uap->pid == 0)
282 uap->pid = -q->p_pgid;
283 if (uap->options &~ (WUNTRACED|WNOHANG|WSIGRESTART)) {
284 u.u_error = EINVAL;
285 return;
286 }
287loop:
1d348849 288 for (p = q->p_cptr; p; p = p->p_osptr) {
1ad494e6
MK
289 if (uap->pid != WAIT_ANY &&
290 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
291 continue;
a8560fa0
SL
292 f++;
293 if (p->p_stat == SZOMB) {
1ad494e6 294 pgrm(p); /* off pgrp */
a8560fa0 295 u.u_r.r_val1 = p->p_pid;
1ad494e6
MK
296#ifdef COMPAT_43
297 if (compat)
298 u.u_r.r_val2 = p->p_xstat;
299 else
300#endif
301 if (uap->status) {
302 status.w_status = p->p_xstat;
303 if (u.u_error = copyout((caddr_t)&status,
304 (caddr_t)uap->status, sizeof(status)))
305 return;
5e63dd78 306 }
1ad494e6
MK
307 p->p_xstat = 0;
308 if (uap->rusage)
309 u.u_error = copyout((caddr_t)p->p_ru,
310 (caddr_t)uap->rusage,
311 sizeof (struct rusage));
312 ruadd(&u.u_cru, p->p_ru);
313 FREE(p->p_ru, M_ZOMBIE);
314 p->p_ru = 0;
a8560fa0
SL
315 p->p_stat = NULL;
316 p->p_pid = 0;
317 p->p_ppid = 0;
1d348849
MK
318 if (*p->p_prev = p->p_nxt) /* off zombproc */
319 p->p_nxt->p_prev = p->p_prev;
320 p->p_nxt = freeproc; /* onto freeproc */
321 freeproc = p;
a8560fa0
SL
322 if (q = p->p_ysptr)
323 q->p_osptr = p->p_osptr;
324 if (q = p->p_osptr)
325 q->p_ysptr = p->p_ysptr;
326 if ((q = p->p_pptr)->p_cptr == p)
327 q->p_cptr = p->p_osptr;
328 p->p_pptr = 0;
329 p->p_ysptr = 0;
330 p->p_osptr = 0;
331 p->p_cptr = 0;
332 p->p_sig = 0;
dd012d1e
SL
333 p->p_sigcatch = 0;
334 p->p_sigignore = 0;
335 p->p_sigmask = 0;
1ad494e6 336 /*p->p_pgrp = 0;*/
a8560fa0
SL
337 p->p_flag = 0;
338 p->p_wchan = 0;
339 p->p_cursig = 0;
1ad494e6 340 return;
a8560fa0 341 }
1ad494e6
MK
342 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
343 (p->p_flag & STRC || uap->options & WUNTRACED)) {
a8560fa0
SL
344 p->p_flag |= SWTED;
345 u.u_r.r_val1 = p->p_pid;
1ad494e6
MK
346#ifdef COMPAT_43
347 if (compat)
348 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
349 else
350#endif
351 if (uap->status) {
352 status.w_status = 0;
353 status.w_stopval = WSTOPPED;
354 status.w_stopsig = p->p_cursig;
355 u.u_error = copyout((caddr_t)&status,
356 (caddr_t)uap->status, sizeof(status));
357 }
358 return;
a8560fa0
SL
359 }
360 }
1ad494e6
MK
361 if (f == 0) {
362 u.u_error = ECHILD;
363 return;
364 }
365 if (uap->options & WNOHANG) {
a8560fa0 366 u.u_r.r_val1 = 0;
1ad494e6 367 return;
a8560fa0 368 }
1ad494e6 369 if (uap->options & WSIGRESTART && setjmp(&u.u_qsave)) {
fe7f81f4 370 p = u.u_procp;
1ad494e6
MK
371 if ((u.u_sigintr & sigmask(p->p_cursig)) != 0) {
372 u.u_error = EINTR;
373 return;
374 }
a8560fa0 375 u.u_eosys = RESTARTSYS;
1ad494e6 376 return;
a8560fa0
SL
377 }
378 sleep((caddr_t)u.u_procp, PWAIT);
379 goto loop;
380}