Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
c4ec2128 KM |
2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
3 | * All rights reserved. | |
da7c5cc6 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
c4ec2128 | 6 | * |
9db58063 | 7 | * @(#)kern_exit.c 7.29 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
a8560fa0 | 9 | |
94368568 JB |
10 | #include "param.h" |
11 | #include "systm.h" | |
12 | #include "map.h" | |
1bfd8b20 KM |
13 | #include "ioctl.h" |
14 | #include "tty.h" | |
d9c2f47f | 15 | #include "user.h" |
94368568 JB |
16 | #include "kernel.h" |
17 | #include "proc.h" | |
18 | #include "buf.h" | |
19 | #include "wait.h" | |
94368568 | 20 | #include "file.h" |
c4ec2128 | 21 | #include "vnode.h" |
5e63dd78 | 22 | #include "syslog.h" |
61d22ffc | 23 | #include "malloc.h" |
a8560fa0 | 24 | |
d301d150 | 25 | #include "machine/reg.h" |
1ad494e6 | 26 | #ifdef COMPAT_43 |
d301d150 | 27 | #include "machine/psl.h" |
1ad494e6 MK |
28 | #endif |
29 | ||
9db58063 KM |
30 | #include "../vm/vm_param.h" |
31 | #include "../vm/vm_map.h" | |
32 | #include "../vm/vm_kern.h" | |
33 | ||
a8560fa0 SL |
34 | /* |
35 | * Exit system call: pass back caller's arg | |
36 | */ | |
1bfd8b20 KM |
37 | /* ARGSUSED */ |
38 | rexit(p, uap, retval) | |
39 | struct proc *p; | |
40 | struct args { | |
a8560fa0 SL |
41 | int rval; |
42 | } *uap; | |
1bfd8b20 KM |
43 | int *retval; |
44 | { | |
a8560fa0 | 45 | |
5360383b MK |
46 | exit(p, W_EXITCODE(uap->rval, 0)); |
47 | /* NOTREACHED */ | |
a8560fa0 SL |
48 | } |
49 | ||
50 | /* | |
51 | * Release resources. | |
52 | * Save u. area for parent to look at. | |
53 | * Enter zombie state. | |
54 | * Wake up parent and init processes, | |
55 | * and dispose of children. | |
56 | */ | |
1bfd8b20 KM |
57 | exit(p, rv) |
58 | struct proc *p; | |
c35e8b3f | 59 | int rv; |
a8560fa0 SL |
60 | { |
61 | register int i; | |
1bfd8b20 | 62 | register struct proc *q, *nq; |
4a438310 | 63 | register struct proc **pp; |
a8560fa0 SL |
64 | |
65 | #ifdef PGINPROF | |
66 | vmsizmon(); | |
67 | #endif | |
61d22ffc KM |
68 | MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), |
69 | M_ZOMBIE, M_WAITOK); | |
a8560fa0 SL |
70 | p->p_flag &= ~(STRC|SULOCK); |
71 | p->p_flag |= SWEXIT; | |
dd012d1e | 72 | p->p_sigignore = ~0; |
a2528931 | 73 | p->p_sig = 0; |
a8560fa0 SL |
74 | p->p_cpticks = 0; |
75 | p->p_pctcpu = 0; | |
dd012d1e | 76 | for (i = 0; i < NSIG; i++) |
a8560fa0 SL |
77 | u.u_signal[i] = SIG_IGN; |
78 | untimeout(realitexpire, (caddr_t)p); | |
9db58063 KM |
79 | #ifdef SYSVSHM |
80 | if (p->p_shm) | |
81 | shmexit(p); | |
82 | #endif | |
83 | vm_map_deallocate(p->p_map); | |
84 | p->p_map = VM_MAP_NULL; | |
a8560fa0 | 85 | /* |
9db58063 | 86 | * XXX preserve synchronization semantics of vfork |
a8560fa0 | 87 | */ |
9db58063 | 88 | if (p->p_flag & SVFORK) { |
a8560fa0 SL |
89 | p->p_flag &= ~SVFORK; |
90 | wakeup((caddr_t)p); | |
91 | while ((p->p_flag & SVFDONE) == 0) | |
92 | sleep((caddr_t)p, PZERO - 1); | |
93 | p->p_flag &= ~SVFDONE; | |
94 | } | |
48faa1e9 | 95 | for (i = 0; i <= u.u_lastfile; i++) { |
a8560fa0 | 96 | struct file *f; |
a8560fa0 SL |
97 | |
98 | f = u.u_ofile[i]; | |
48faa1e9 MK |
99 | if (f) { |
100 | u.u_ofile[i] = NULL; | |
101 | u.u_pofile[i] = 0; | |
22e4e5b9 | 102 | (void) closef(f); |
48faa1e9 | 103 | } |
a8560fa0 | 104 | } |
1ad494e6 | 105 | if (SESS_LEADER(p)) { |
a38b908e MT |
106 | register struct session *sp = p->p_session; |
107 | ||
108 | if (sp->s_ttyvp) { | |
109 | /* | |
110 | * Controlling process. | |
111 | * Signal foreground pgrp and revoke access | |
112 | * to controlling terminal. | |
113 | */ | |
114 | if (sp->s_ttyp->t_pgrp) | |
773142c4 | 115 | pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); |
a38b908e MT |
116 | vgoneall(sp->s_ttyvp); |
117 | vrele(sp->s_ttyvp); | |
118 | sp->s_ttyvp = NULL; | |
61fa8c82 MT |
119 | /* |
120 | * s_ttyp is not zero'd; we use this to indicate | |
121 | * that the session once had a controlling terminal. | |
a38b908e | 122 | * (for logging and informational purposes) |
61fa8c82 | 123 | */ |
1ad494e6 | 124 | } |
a38b908e | 125 | sp->s_leader = 0; |
1ad494e6 | 126 | } |
c4ec2128 KM |
127 | VOP_LOCK(u.u_cdir); |
128 | vput(u.u_cdir); | |
a8560fa0 | 129 | if (u.u_rdir) { |
c4ec2128 KM |
130 | VOP_LOCK(u.u_rdir); |
131 | vput(u.u_rdir); | |
a8560fa0 SL |
132 | } |
133 | u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; | |
d120e510 | 134 | (void) acct(p); |
c4ec2128 | 135 | crfree(u.u_cred); |
1ad494e6 MK |
136 | #ifdef KTRACE |
137 | /* | |
138 | * release trace file | |
139 | */ | |
140 | if (p->p_tracep) | |
c4ec2128 | 141 | vrele(p->p_tracep); |
a8560fa0 | 142 | #endif |
9db58063 KM |
143 | splimp(); |
144 | /* I don't think this will cause a sleep/realloc anywhere... */ | |
145 | kmem_free(kernel_map, (vm_offset_t)p->p_addr, | |
146 | round_page(ctob(UPAGES))); | |
1d348849 MK |
147 | if (*p->p_prev = p->p_nxt) /* off allproc queue */ |
148 | p->p_nxt->p_prev = p->p_prev; | |
149 | if (p->p_nxt = zombproc) /* onto zombproc */ | |
150 | p->p_nxt->p_prev = &p->p_nxt; | |
151 | p->p_prev = &zombproc; | |
152 | zombproc = p; | |
a8560fa0 SL |
153 | p->p_stat = SZOMB; |
154 | noproc = 1; | |
4a438310 MK |
155 | for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) |
156 | if (*pp == p) { | |
157 | *pp = p->p_hash; | |
158 | goto done; | |
159 | } | |
160 | panic("exit"); | |
161 | done: | |
9db58063 KM |
162 | if (p->p_pid == 1) |
163 | panic("init died"); | |
a8560fa0 | 164 | p->p_xstat = rv; |
48faa1e9 | 165 | *p->p_ru = u.u_ru; |
61fa8c82 MT |
166 | i = splclock(); |
167 | p->p_ru->ru_stime = p->p_stime; | |
168 | p->p_ru->ru_utime = p->p_utime; | |
169 | splx(i); | |
48faa1e9 | 170 | ruadd(p->p_ru, &u.u_cru); |
1d348849 MK |
171 | if (p->p_cptr) /* only need this if any child is S_ZOMB */ |
172 | wakeup((caddr_t)&proc[1]); | |
2dccc728 | 173 | fixjobc(p, p->p_pgrp, 0); |
1d348849 MK |
174 | for (q = p->p_cptr; q != NULL; q = nq) { |
175 | nq = q->p_osptr; | |
176 | if (nq != NULL) | |
177 | nq->p_ysptr = NULL; | |
178 | if (proc[1].p_cptr) | |
179 | proc[1].p_cptr->p_ysptr = q; | |
180 | q->p_osptr = proc[1].p_cptr; | |
181 | q->p_ysptr = NULL; | |
182 | proc[1].p_cptr = q; | |
a8560fa0 | 183 | |
1d348849 MK |
184 | q->p_pptr = &proc[1]; |
185 | q->p_ppid = 1; | |
186 | /* | |
187 | * Traced processes are killed | |
188 | * since their existence means someone is screwing up. | |
1d348849 MK |
189 | */ |
190 | if (q->p_flag&STRC) { | |
191 | q->p_flag &= ~STRC; | |
192 | psignal(q, SIGKILL); | |
a8560fa0 | 193 | } |
1d348849 | 194 | } |
bdf8c113 | 195 | p->p_cptr = NULL; |
a8560fa0 SL |
196 | psignal(p->p_pptr, SIGCHLD); |
197 | wakeup((caddr_t)p->p_pptr); | |
fb1db32c MK |
198 | #if defined(tahoe) |
199 | dkeyrelease(p->p_dkey), p->p_dkey = 0; | |
200 | ckeyrelease(p->p_ckey), p->p_ckey = 0; | |
201 | u.u_pcb.pcb_savacc.faddr = (float *)NULL; | |
202 | #endif | |
a8560fa0 SL |
203 | swtch(); |
204 | } | |
205 | ||
1ad494e6 | 206 | #ifdef COMPAT_43 |
1bfd8b20 KM |
207 | owait(p, uap, retval) |
208 | struct proc *p; | |
209 | register struct args { | |
1ad494e6 | 210 | int pid; |
a2528931 | 211 | int *status; |
1ad494e6 MK |
212 | int options; |
213 | struct rusage *rusage; | |
a2528931 | 214 | int compat; |
1bfd8b20 KM |
215 | } *uap; |
216 | int *retval; | |
217 | { | |
a8560fa0 SL |
218 | |
219 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { | |
c35e8b3f | 220 | uap->options = 0; |
1ad494e6 MK |
221 | uap->rusage = 0; |
222 | } else { | |
c35e8b3f | 223 | uap->options = u.u_ar0[R0]; |
1ad494e6 | 224 | uap->rusage = (struct rusage *)u.u_ar0[R1]; |
a8560fa0 | 225 | } |
1ad494e6 MK |
226 | uap->pid = WAIT_ANY; |
227 | uap->status = 0; | |
a2528931 | 228 | uap->compat = 1; |
d9c2f47f | 229 | return (wait1(p, uap, retval)); |
1ad494e6 MK |
230 | } |
231 | ||
1bfd8b20 KM |
232 | wait4(p, uap, retval) |
233 | struct proc *p; | |
234 | struct args { | |
a2528931 MK |
235 | int pid; |
236 | int *status; | |
237 | int options; | |
238 | struct rusage *rusage; | |
239 | int compat; | |
1bfd8b20 KM |
240 | } *uap; |
241 | int *retval; | |
242 | { | |
a2528931 MK |
243 | |
244 | uap->compat = 0; | |
d9c2f47f | 245 | return (wait1(p, uap, retval)); |
a8560fa0 | 246 | } |
a2528931 MK |
247 | #else |
248 | #define wait1 wait4 | |
1ad494e6 | 249 | #endif |
a8560fa0 | 250 | |
a8560fa0 SL |
251 | /* |
252 | * Wait system call. | |
253 | * Search for a terminated (zombie) child, | |
254 | * finally lay it to rest, and collect its status. | |
255 | * Look also for stopped (traced) children, | |
256 | * and pass back status from them. | |
257 | */ | |
1bfd8b20 KM |
258 | wait1(q, uap, retval) |
259 | register struct proc *q; | |
260 | register struct args { | |
1ad494e6 | 261 | int pid; |
a2528931 | 262 | int *status; |
1ad494e6 MK |
263 | int options; |
264 | struct rusage *rusage; | |
a2528931 MK |
265 | #ifdef COMPAT_43 |
266 | int compat; | |
267 | #endif | |
1bfd8b20 KM |
268 | } *uap; |
269 | int retval[]; | |
270 | { | |
271 | register int f; | |
272 | register struct proc *p; | |
a2528931 | 273 | int status, error; |
a8560fa0 | 274 | |
1ad494e6 MK |
275 | if (uap->pid == 0) |
276 | uap->pid = -q->p_pgid; | |
c35e8b3f | 277 | #ifdef notyet |
a2528931 | 278 | if (uap->options &~ (WUNTRACED|WNOHANG)) |
d9c2f47f | 279 | return (EINVAL); |
c35e8b3f | 280 | #endif |
1ad494e6 | 281 | loop: |
c35e8b3f | 282 | f = 0; |
1d348849 | 283 | for (p = q->p_cptr; p; p = p->p_osptr) { |
1ad494e6 MK |
284 | if (uap->pid != WAIT_ANY && |
285 | p->p_pid != uap->pid && p->p_pgid != -uap->pid) | |
286 | continue; | |
a8560fa0 SL |
287 | f++; |
288 | if (p->p_stat == SZOMB) { | |
1bfd8b20 | 289 | retval[0] = p->p_pid; |
1ad494e6 | 290 | #ifdef COMPAT_43 |
a2528931 | 291 | if (uap->compat) |
1bfd8b20 | 292 | retval[1] = p->p_xstat; |
1ad494e6 MK |
293 | else |
294 | #endif | |
295 | if (uap->status) { | |
a2528931 MK |
296 | status = p->p_xstat; /* convert to int */ |
297 | if (error = copyout((caddr_t)&status, | |
1ad494e6 | 298 | (caddr_t)uap->status, sizeof(status))) |
d9c2f47f | 299 | return (error); |
5e63dd78 | 300 | } |
a2528931 MK |
301 | if (uap->rusage && (error = copyout((caddr_t)p->p_ru, |
302 | (caddr_t)uap->rusage, sizeof (struct rusage)))) | |
d9c2f47f | 303 | return (error); |
a2528931 | 304 | pgrm(p); /* off pgrp */ |
1ad494e6 | 305 | p->p_xstat = 0; |
1ad494e6 MK |
306 | ruadd(&u.u_cru, p->p_ru); |
307 | FREE(p->p_ru, M_ZOMBIE); | |
308 | p->p_ru = 0; | |
a8560fa0 SL |
309 | p->p_stat = NULL; |
310 | p->p_pid = 0; | |
311 | p->p_ppid = 0; | |
1d348849 MK |
312 | if (*p->p_prev = p->p_nxt) /* off zombproc */ |
313 | p->p_nxt->p_prev = p->p_prev; | |
314 | p->p_nxt = freeproc; /* onto freeproc */ | |
315 | freeproc = p; | |
a8560fa0 SL |
316 | if (q = p->p_ysptr) |
317 | q->p_osptr = p->p_osptr; | |
318 | if (q = p->p_osptr) | |
319 | q->p_ysptr = p->p_ysptr; | |
320 | if ((q = p->p_pptr)->p_cptr == p) | |
321 | q->p_cptr = p->p_osptr; | |
322 | p->p_pptr = 0; | |
323 | p->p_ysptr = 0; | |
324 | p->p_osptr = 0; | |
325 | p->p_cptr = 0; | |
326 | p->p_sig = 0; | |
dd012d1e SL |
327 | p->p_sigcatch = 0; |
328 | p->p_sigignore = 0; | |
329 | p->p_sigmask = 0; | |
1ad494e6 | 330 | /*p->p_pgrp = 0;*/ |
a8560fa0 SL |
331 | p->p_flag = 0; |
332 | p->p_wchan = 0; | |
d9c2f47f | 333 | return (0); |
a8560fa0 | 334 | } |
1ad494e6 MK |
335 | if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && |
336 | (p->p_flag & STRC || uap->options & WUNTRACED)) { | |
a8560fa0 | 337 | p->p_flag |= SWTED; |
1bfd8b20 | 338 | retval[0] = p->p_pid; |
1ad494e6 | 339 | #ifdef COMPAT_43 |
85d629e3 | 340 | if (uap->compat) { |
19dec745 | 341 | retval[1] = W_STOPCODE(p->p_xstat); |
85d629e3 KM |
342 | error = 0; |
343 | } else | |
1ad494e6 MK |
344 | #endif |
345 | if (uap->status) { | |
19dec745 | 346 | status = W_STOPCODE(p->p_xstat); |
a2528931 | 347 | error = copyout((caddr_t)&status, |
1ad494e6 | 348 | (caddr_t)uap->status, sizeof(status)); |
a2528931 MK |
349 | } else |
350 | error = 0; | |
d9c2f47f | 351 | return (error); |
a8560fa0 SL |
352 | } |
353 | } | |
a2528931 | 354 | if (f == 0) |
d9c2f47f | 355 | return (ECHILD); |
1ad494e6 | 356 | if (uap->options & WNOHANG) { |
1bfd8b20 | 357 | retval[0] = 0; |
d9c2f47f | 358 | return (0); |
a8560fa0 | 359 | } |
1bfd8b20 | 360 | if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) |
d9c2f47f | 361 | return (error); |
a8560fa0 SL |
362 | goto loop; |
363 | } |