Commit | Line | Data |
---|---|---|
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 | * |
8429d022 | 7 | * @(#)kern_exit.c 7.31 (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" | |
8429d022 MK |
15 | #include "time.h" |
16 | #include "resource.h" | |
d9c2f47f | 17 | #include "user.h" |
94368568 JB |
18 | #include "kernel.h" |
19 | #include "proc.h" | |
20 | #include "buf.h" | |
21 | #include "wait.h" | |
94368568 | 22 | #include "file.h" |
c4ec2128 | 23 | #include "vnode.h" |
5e63dd78 | 24 | #include "syslog.h" |
61d22ffc | 25 | #include "malloc.h" |
a8560fa0 | 26 | |
1ad494e6 | 27 | #ifdef COMPAT_43 |
8429d022 | 28 | #include "machine/reg.h" |
d301d150 | 29 | #include "machine/psl.h" |
1ad494e6 MK |
30 | #endif |
31 | ||
8429d022 MK |
32 | #include "vm/vm_param.h" |
33 | #include "vm/vm_map.h" | |
34 | #include "vm/vm_kern.h" | |
9db58063 | 35 | |
a8560fa0 SL |
36 | /* |
37 | * Exit system call: pass back caller's arg | |
38 | */ | |
1bfd8b20 KM |
39 | /* ARGSUSED */ |
40 | rexit(p, uap, retval) | |
41 | struct proc *p; | |
42 | struct args { | |
a8560fa0 SL |
43 | int rval; |
44 | } *uap; | |
1bfd8b20 KM |
45 | int *retval; |
46 | { | |
a8560fa0 | 47 | |
5360383b MK |
48 | exit(p, W_EXITCODE(uap->rval, 0)); |
49 | /* NOTREACHED */ | |
a8560fa0 SL |
50 | } |
51 | ||
52 | /* | |
8429d022 MK |
53 | * Exit: deallocate address space and other resources, |
54 | * change proc state to zombie, and unlink proc from allproc | |
55 | * and parent's lists. Save exit status and rusage for wait(). | |
56 | * Check for child processes and orphan them. | |
a8560fa0 | 57 | */ |
1bfd8b20 | 58 | exit(p, rv) |
8429d022 | 59 | register struct proc *p; |
c35e8b3f | 60 | int rv; |
a8560fa0 | 61 | { |
1bfd8b20 | 62 | register struct proc *q, *nq; |
4a438310 | 63 | register struct proc **pp; |
8429d022 | 64 | int s; |
a8560fa0 SL |
65 | |
66 | #ifdef PGINPROF | |
67 | vmsizmon(); | |
68 | #endif | |
61d22ffc KM |
69 | MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), |
70 | M_ZOMBIE, M_WAITOK); | |
8429d022 MK |
71 | /* |
72 | * If parent is waiting for us to exit or exec, | |
73 | * SPPWAIT is set; we will wakeup the parent below. | |
74 | */ | |
75 | p->p_flag &= ~(STRC|SULOCK|SPPWAIT); | |
a8560fa0 | 76 | p->p_flag |= SWEXIT; |
dd012d1e | 77 | p->p_sigignore = ~0; |
a2528931 | 78 | p->p_sig = 0; |
a8560fa0 | 79 | untimeout(realitexpire, (caddr_t)p); |
8429d022 MK |
80 | |
81 | /* | |
82 | * Close open files and release open-file table. | |
83 | * This may block! | |
84 | */ | |
85 | fdfree(p); | |
86 | p->p_fd = 0; | |
9db58063 | 87 | #ifdef SYSVSHM |
8429d022 | 88 | if (p->p_vmspace->vm_shm) |
9db58063 KM |
89 | shmexit(p); |
90 | #endif | |
8429d022 MK |
91 | vmspace_free(p->p_vmspace); |
92 | p->p_vmspace = 0; | |
93 | ||
94 | if (p->p_pid == 1) | |
95 | panic("init died"); | |
1ad494e6 | 96 | if (SESS_LEADER(p)) { |
a38b908e MT |
97 | register struct session *sp = p->p_session; |
98 | ||
99 | if (sp->s_ttyvp) { | |
100 | /* | |
101 | * Controlling process. | |
102 | * Signal foreground pgrp and revoke access | |
103 | * to controlling terminal. | |
104 | */ | |
105 | if (sp->s_ttyp->t_pgrp) | |
773142c4 | 106 | pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); |
a38b908e MT |
107 | vgoneall(sp->s_ttyvp); |
108 | vrele(sp->s_ttyvp); | |
109 | sp->s_ttyvp = NULL; | |
61fa8c82 MT |
110 | /* |
111 | * s_ttyp is not zero'd; we use this to indicate | |
112 | * that the session once had a controlling terminal. | |
a38b908e | 113 | * (for logging and informational purposes) |
61fa8c82 | 114 | */ |
1ad494e6 | 115 | } |
a38b908e | 116 | sp->s_leader = 0; |
1ad494e6 | 117 | } |
8429d022 MK |
118 | fixjobc(p, p->p_pgrp, 0); |
119 | p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; | |
d120e510 | 120 | (void) acct(p); |
8429d022 MK |
121 | if (--p->p_limit->p_refcnt == 0) |
122 | FREE(p->p_limit, M_SUBPROC); | |
123 | if (--p->p_cred->p_refcnt == 0) { | |
124 | crfree(p->p_cred->pc_ucred); | |
125 | FREE(p->p_cred, M_SUBPROC); | |
126 | } | |
1ad494e6 MK |
127 | #ifdef KTRACE |
128 | /* | |
129 | * release trace file | |
130 | */ | |
131 | if (p->p_tracep) | |
c4ec2128 | 132 | vrele(p->p_tracep); |
a8560fa0 | 133 | #endif |
8429d022 MK |
134 | |
135 | /* | |
136 | * Remove proc from allproc queue and pidhash chain. | |
137 | * Place onto zombproc. Unlink from parent's child list. | |
138 | */ | |
139 | if (*p->p_prev = p->p_nxt) | |
1d348849 | 140 | p->p_nxt->p_prev = p->p_prev; |
8429d022 | 141 | if (p->p_nxt = zombproc) |
1d348849 MK |
142 | p->p_nxt->p_prev = &p->p_nxt; |
143 | p->p_prev = &zombproc; | |
144 | zombproc = p; | |
a8560fa0 SL |
145 | p->p_stat = SZOMB; |
146 | noproc = 1; | |
4a438310 MK |
147 | for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) |
148 | if (*pp == p) { | |
149 | *pp = p->p_hash; | |
150 | goto done; | |
151 | } | |
152 | panic("exit"); | |
153 | done: | |
8429d022 | 154 | |
1d348849 | 155 | if (p->p_cptr) /* only need this if any child is S_ZOMB */ |
8429d022 | 156 | wakeup((caddr_t) initproc); |
1d348849 MK |
157 | for (q = p->p_cptr; q != NULL; q = nq) { |
158 | nq = q->p_osptr; | |
159 | if (nq != NULL) | |
160 | nq->p_ysptr = NULL; | |
8429d022 MK |
161 | if (initproc->p_cptr) |
162 | initproc->p_cptr->p_ysptr = q; | |
163 | q->p_osptr = initproc->p_cptr; | |
1d348849 | 164 | q->p_ysptr = NULL; |
8429d022 | 165 | initproc->p_cptr = q; |
a8560fa0 | 166 | |
8429d022 | 167 | q->p_pptr = initproc; |
1d348849 MK |
168 | /* |
169 | * Traced processes are killed | |
170 | * since their existence means someone is screwing up. | |
1d348849 MK |
171 | */ |
172 | if (q->p_flag&STRC) { | |
173 | q->p_flag &= ~STRC; | |
174 | psignal(q, SIGKILL); | |
a8560fa0 | 175 | } |
1d348849 | 176 | } |
bdf8c113 | 177 | p->p_cptr = NULL; |
8429d022 MK |
178 | |
179 | /* | |
180 | * Save exit status and final rusage info, | |
181 | * adding in child rusage info and self times. | |
182 | */ | |
183 | p->p_xstat = rv; | |
184 | *p->p_ru = p->p_stats->p_ru; | |
185 | p->p_ru->ru_stime = p->p_stime; | |
186 | p->p_ru->ru_utime = p->p_utime; | |
187 | ruadd(p->p_ru, &p->p_stats->p_cru); | |
188 | ||
189 | /* | |
190 | * Notify parent that we're gone. | |
191 | */ | |
a8560fa0 SL |
192 | psignal(p->p_pptr, SIGCHLD); |
193 | wakeup((caddr_t)p->p_pptr); | |
fb1db32c | 194 | #if defined(tahoe) |
fb1db32c MK |
195 | u.u_pcb.pcb_savacc.faddr = (float *)NULL; |
196 | #endif | |
8429d022 MK |
197 | /* |
198 | * Free the memory for the user structure and kernel stack. | |
199 | * As we continue using it until the swtch completes | |
200 | * (or switches to an interrupt stack), we need to block | |
201 | * memory allocation by raising priority until we are gone. | |
202 | */ | |
203 | (void) splimp(); | |
204 | /* I don't think this will cause a sleep/realloc anywhere... */ | |
205 | kmem_free(kernel_map, (vm_offset_t)p->p_addr, | |
206 | round_page(ctob(UPAGES))); | |
a8560fa0 | 207 | swtch(); |
8429d022 | 208 | /* NOTREACHED */ |
a8560fa0 SL |
209 | } |
210 | ||
1ad494e6 | 211 | #ifdef COMPAT_43 |
1bfd8b20 KM |
212 | owait(p, uap, retval) |
213 | struct proc *p; | |
214 | register struct args { | |
1ad494e6 | 215 | int pid; |
a2528931 | 216 | int *status; |
1ad494e6 MK |
217 | int options; |
218 | struct rusage *rusage; | |
a2528931 | 219 | int compat; |
1bfd8b20 KM |
220 | } *uap; |
221 | int *retval; | |
222 | { | |
a8560fa0 | 223 | |
8429d022 | 224 | if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) { |
c35e8b3f | 225 | uap->options = 0; |
1ad494e6 MK |
226 | uap->rusage = 0; |
227 | } else { | |
8429d022 MK |
228 | uap->options = p->p_regs[R0]; |
229 | uap->rusage = (struct rusage *)p->p_regs[R1]; | |
a8560fa0 | 230 | } |
1ad494e6 MK |
231 | uap->pid = WAIT_ANY; |
232 | uap->status = 0; | |
a2528931 | 233 | uap->compat = 1; |
d9c2f47f | 234 | return (wait1(p, uap, retval)); |
1ad494e6 MK |
235 | } |
236 | ||
1bfd8b20 KM |
237 | wait4(p, uap, retval) |
238 | struct proc *p; | |
239 | struct args { | |
a2528931 MK |
240 | int pid; |
241 | int *status; | |
242 | int options; | |
243 | struct rusage *rusage; | |
244 | int compat; | |
1bfd8b20 KM |
245 | } *uap; |
246 | int *retval; | |
247 | { | |
a2528931 MK |
248 | |
249 | uap->compat = 0; | |
d9c2f47f | 250 | return (wait1(p, uap, retval)); |
a8560fa0 | 251 | } |
a2528931 MK |
252 | #else |
253 | #define wait1 wait4 | |
1ad494e6 | 254 | #endif |
a8560fa0 | 255 | |
a8560fa0 | 256 | /* |
8429d022 MK |
257 | * Wait: check child processes to see if any have exited, |
258 | * stopped under trace, or (optionally) stopped by a signal. | |
259 | * Pass back status and deallocate exited child's proc structure. | |
a8560fa0 | 260 | */ |
1bfd8b20 KM |
261 | wait1(q, uap, retval) |
262 | register struct proc *q; | |
263 | register struct args { | |
1ad494e6 | 264 | int pid; |
a2528931 | 265 | int *status; |
1ad494e6 MK |
266 | int options; |
267 | struct rusage *rusage; | |
a2528931 MK |
268 | #ifdef COMPAT_43 |
269 | int compat; | |
270 | #endif | |
1bfd8b20 KM |
271 | } *uap; |
272 | int retval[]; | |
273 | { | |
8429d022 | 274 | register int nfound; |
1bfd8b20 | 275 | register struct proc *p; |
a2528931 | 276 | int status, error; |
a8560fa0 | 277 | |
1ad494e6 MK |
278 | if (uap->pid == 0) |
279 | uap->pid = -q->p_pgid; | |
c35e8b3f | 280 | #ifdef notyet |
a2528931 | 281 | if (uap->options &~ (WUNTRACED|WNOHANG)) |
d9c2f47f | 282 | return (EINVAL); |
c35e8b3f | 283 | #endif |
1ad494e6 | 284 | loop: |
8429d022 | 285 | nfound = 0; |
1d348849 | 286 | for (p = q->p_cptr; p; p = p->p_osptr) { |
1ad494e6 MK |
287 | if (uap->pid != WAIT_ANY && |
288 | p->p_pid != uap->pid && p->p_pgid != -uap->pid) | |
289 | continue; | |
8429d022 | 290 | nfound++; |
a8560fa0 | 291 | if (p->p_stat == SZOMB) { |
1bfd8b20 | 292 | retval[0] = p->p_pid; |
1ad494e6 | 293 | #ifdef COMPAT_43 |
a2528931 | 294 | if (uap->compat) |
1bfd8b20 | 295 | retval[1] = p->p_xstat; |
1ad494e6 MK |
296 | else |
297 | #endif | |
298 | if (uap->status) { | |
a2528931 MK |
299 | status = p->p_xstat; /* convert to int */ |
300 | if (error = copyout((caddr_t)&status, | |
1ad494e6 | 301 | (caddr_t)uap->status, sizeof(status))) |
d9c2f47f | 302 | return (error); |
5e63dd78 | 303 | } |
a2528931 MK |
304 | if (uap->rusage && (error = copyout((caddr_t)p->p_ru, |
305 | (caddr_t)uap->rusage, sizeof (struct rusage)))) | |
d9c2f47f | 306 | return (error); |
1ad494e6 | 307 | p->p_xstat = 0; |
8429d022 | 308 | ruadd(&q->p_stats->p_cru, p->p_ru); |
1ad494e6 | 309 | FREE(p->p_ru, M_ZOMBIE); |
8429d022 MK |
310 | |
311 | /* | |
312 | * Finally finished with old proc entry. | |
313 | * Unlink it from its process group and free it. | |
314 | */ | |
315 | leavepgrp(p); | |
1d348849 MK |
316 | if (*p->p_prev = p->p_nxt) /* off zombproc */ |
317 | p->p_nxt->p_prev = p->p_prev; | |
a8560fa0 SL |
318 | if (q = p->p_ysptr) |
319 | q->p_osptr = p->p_osptr; | |
320 | if (q = p->p_osptr) | |
321 | q->p_ysptr = p->p_ysptr; | |
322 | if ((q = p->p_pptr)->p_cptr == p) | |
323 | q->p_cptr = p->p_osptr; | |
8429d022 MK |
324 | FREE(p, M_PROC); |
325 | nprocs--; | |
d9c2f47f | 326 | return (0); |
a8560fa0 | 327 | } |
1ad494e6 MK |
328 | if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && |
329 | (p->p_flag & STRC || uap->options & WUNTRACED)) { | |
a8560fa0 | 330 | p->p_flag |= SWTED; |
1bfd8b20 | 331 | retval[0] = p->p_pid; |
1ad494e6 | 332 | #ifdef COMPAT_43 |
85d629e3 | 333 | if (uap->compat) { |
19dec745 | 334 | retval[1] = W_STOPCODE(p->p_xstat); |
85d629e3 KM |
335 | error = 0; |
336 | } else | |
1ad494e6 MK |
337 | #endif |
338 | if (uap->status) { | |
19dec745 | 339 | status = W_STOPCODE(p->p_xstat); |
a2528931 | 340 | error = copyout((caddr_t)&status, |
1ad494e6 | 341 | (caddr_t)uap->status, sizeof(status)); |
a2528931 MK |
342 | } else |
343 | error = 0; | |
d9c2f47f | 344 | return (error); |
a8560fa0 SL |
345 | } |
346 | } | |
8429d022 | 347 | if (nfound == 0) |
d9c2f47f | 348 | return (ECHILD); |
1ad494e6 | 349 | if (uap->options & WNOHANG) { |
1bfd8b20 | 350 | retval[0] = 0; |
d9c2f47f | 351 | return (0); |
a8560fa0 | 352 | } |
1bfd8b20 | 353 | if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) |
d9c2f47f | 354 | return (error); |
a8560fa0 SL |
355 | goto loop; |
356 | } |