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 | * |
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 | * | |
8b4bf5b8 | 17 | * @(#)kern_exit.c 7.11 (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" |
a2528931 | 31 | #include "ioctl.h" |
1ad494e6 | 32 | #include "tty.h" |
5e63dd78 | 33 | #include "syslog.h" |
61d22ffc | 34 | #include "malloc.h" |
a8560fa0 | 35 | |
d301d150 | 36 | #include "machine/reg.h" |
1ad494e6 | 37 | #ifdef COMPAT_43 |
d301d150 | 38 | #include "machine/psl.h" |
1ad494e6 MK |
39 | #endif |
40 | ||
a8560fa0 SL |
41 | /* |
42 | * Exit system call: pass back caller's arg | |
43 | */ | |
44 | rexit() | |
45 | { | |
a2528931 | 46 | struct a { |
a8560fa0 SL |
47 | int rval; |
48 | } *uap; | |
49 | ||
50 | uap = (struct a *)u.u_ap; | |
a2528931 | 51 | exit(W_EXITCODE(uap->rval, 0)); |
a8560fa0 SL |
52 | } |
53 | ||
54 | /* | |
55 | * Release resources. | |
56 | * Save u. area for parent to look at. | |
57 | * Enter zombie state. | |
58 | * Wake up parent and init processes, | |
59 | * and dispose of children. | |
60 | */ | |
61 | exit(rv) | |
c35e8b3f | 62 | int rv; |
a8560fa0 SL |
63 | { |
64 | register int i; | |
1d348849 | 65 | register struct proc *p, *q, *nq; |
a8560fa0 | 66 | register int x; |
a8560fa0 SL |
67 | |
68 | #ifdef PGINPROF | |
69 | vmsizmon(); | |
70 | #endif | |
71 | p = u.u_procp; | |
61d22ffc KM |
72 | MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), |
73 | M_ZOMBIE, M_WAITOK); | |
a8560fa0 SL |
74 | p->p_flag &= ~(STRC|SULOCK); |
75 | p->p_flag |= SWEXIT; | |
dd012d1e | 76 | p->p_sigignore = ~0; |
a2528931 | 77 | p->p_sig = 0; |
a8560fa0 SL |
78 | p->p_cpticks = 0; |
79 | p->p_pctcpu = 0; | |
dd012d1e | 80 | for (i = 0; i < NSIG; i++) |
a8560fa0 SL |
81 | u.u_signal[i] = SIG_IGN; |
82 | untimeout(realitexpire, (caddr_t)p); | |
83 | /* | |
84 | * Release virtual memory. If we resulted from | |
85 | * a vfork(), instead give the resources back to | |
86 | * the parent. | |
87 | */ | |
88 | if ((p->p_flag & SVFORK) == 0) | |
89 | vrelvm(); | |
90 | else { | |
91 | p->p_flag &= ~SVFORK; | |
92 | wakeup((caddr_t)p); | |
93 | while ((p->p_flag & SVFDONE) == 0) | |
94 | sleep((caddr_t)p, PZERO - 1); | |
95 | p->p_flag &= ~SVFDONE; | |
96 | } | |
48faa1e9 | 97 | for (i = 0; i <= u.u_lastfile; i++) { |
a8560fa0 | 98 | struct file *f; |
a8560fa0 SL |
99 | |
100 | f = u.u_ofile[i]; | |
48faa1e9 MK |
101 | if (f) { |
102 | u.u_ofile[i] = NULL; | |
103 | u.u_pofile[i] = 0; | |
22e4e5b9 | 104 | (void) closef(f); |
48faa1e9 | 105 | } |
a8560fa0 | 106 | } |
1ad494e6 MK |
107 | if (SESS_LEADER(p)) { |
108 | p->p_session->s_leader = 0; | |
8b4bf5b8 MT |
109 | if (p->p_session->s_ttyvp) { |
110 | vgone(p->p_session->s_ttyvp); | |
111 | vrele(p->p_session->s_ttyvp); | |
112 | p->p_session->s_ttyvp = NULL; | |
113 | p->p_session->s_ttyp = NULL; | |
1ad494e6 MK |
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) { | |
a2528931 | 167 | printf("Can't exec init (errno %d)\n", WEXITSTATUS(rv)); |
48faa1e9 MK |
168 | for (;;) |
169 | ; | |
170 | } else | |
171 | panic("init died"); | |
172 | } | |
a8560fa0 SL |
173 | done: |
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 |
228 | owait() | |
a8560fa0 | 229 | { |
a2528931 | 230 | register struct a { |
1ad494e6 | 231 | int pid; |
a2528931 | 232 | int *status; |
1ad494e6 MK |
233 | int options; |
234 | struct rusage *rusage; | |
a2528931 | 235 | int compat; |
1ad494e6 | 236 | } *uap = (struct a *)u.u_ap; |
a8560fa0 SL |
237 | |
238 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { | |
c35e8b3f | 239 | uap->options = 0; |
1ad494e6 MK |
240 | uap->rusage = 0; |
241 | } else { | |
c35e8b3f | 242 | uap->options = u.u_ar0[R0]; |
1ad494e6 | 243 | uap->rusage = (struct rusage *)u.u_ar0[R1]; |
a8560fa0 | 244 | } |
1ad494e6 MK |
245 | uap->pid = WAIT_ANY; |
246 | uap->status = 0; | |
a2528931 MK |
247 | uap->compat = 1; |
248 | u.u_error = wait1(); | |
1ad494e6 MK |
249 | } |
250 | ||
251 | wait4() | |
252 | { | |
a2528931 MK |
253 | register struct a { |
254 | int pid; | |
255 | int *status; | |
256 | int options; | |
257 | struct rusage *rusage; | |
258 | int compat; | |
259 | } *uap = (struct a *)u.u_ap; | |
260 | ||
261 | uap->compat = 0; | |
262 | u.u_error = wait1(); | |
a8560fa0 | 263 | } |
a2528931 MK |
264 | #else |
265 | #define wait1 wait4 | |
1ad494e6 | 266 | #endif |
a8560fa0 | 267 | |
a8560fa0 SL |
268 | /* |
269 | * Wait system call. | |
270 | * Search for a terminated (zombie) child, | |
271 | * finally lay it to rest, and collect its status. | |
272 | * Look also for stopped (traced) children, | |
273 | * and pass back status from them. | |
274 | */ | |
a2528931 | 275 | wait1() |
a8560fa0 | 276 | { |
1ad494e6 MK |
277 | register struct a { |
278 | int pid; | |
a2528931 | 279 | int *status; |
1ad494e6 MK |
280 | int options; |
281 | struct rusage *rusage; | |
a2528931 MK |
282 | #ifdef COMPAT_43 |
283 | int compat; | |
284 | #endif | |
1ad494e6 | 285 | } *uap = (struct a *)u.u_ap; |
a8560fa0 SL |
286 | register f; |
287 | register struct proc *p, *q; | |
a2528931 | 288 | int status, error; |
a8560fa0 | 289 | |
1d348849 | 290 | q = u.u_procp; |
1ad494e6 MK |
291 | if (uap->pid == 0) |
292 | uap->pid = -q->p_pgid; | |
c35e8b3f | 293 | #ifdef notyet |
a2528931 MK |
294 | if (uap->options &~ (WUNTRACED|WNOHANG)) |
295 | return (EINVAL); | |
c35e8b3f | 296 | #endif |
1ad494e6 | 297 | loop: |
c35e8b3f | 298 | f = 0; |
1d348849 | 299 | for (p = q->p_cptr; p; p = p->p_osptr) { |
1ad494e6 MK |
300 | if (uap->pid != WAIT_ANY && |
301 | p->p_pid != uap->pid && p->p_pgid != -uap->pid) | |
302 | continue; | |
a8560fa0 SL |
303 | f++; |
304 | if (p->p_stat == SZOMB) { | |
305 | u.u_r.r_val1 = p->p_pid; | |
1ad494e6 | 306 | #ifdef COMPAT_43 |
a2528931 | 307 | if (uap->compat) |
1ad494e6 MK |
308 | u.u_r.r_val2 = p->p_xstat; |
309 | else | |
310 | #endif | |
311 | if (uap->status) { | |
a2528931 MK |
312 | status = p->p_xstat; /* convert to int */ |
313 | if (error = copyout((caddr_t)&status, | |
1ad494e6 | 314 | (caddr_t)uap->status, sizeof(status))) |
a2528931 | 315 | return (error); |
5e63dd78 | 316 | } |
a2528931 MK |
317 | if (uap->rusage && (error = copyout((caddr_t)p->p_ru, |
318 | (caddr_t)uap->rusage, sizeof (struct rusage)))) | |
319 | return (error); | |
320 | pgrm(p); /* off pgrp */ | |
1ad494e6 | 321 | p->p_xstat = 0; |
1ad494e6 MK |
322 | ruadd(&u.u_cru, p->p_ru); |
323 | FREE(p->p_ru, M_ZOMBIE); | |
324 | p->p_ru = 0; | |
a8560fa0 SL |
325 | p->p_stat = NULL; |
326 | p->p_pid = 0; | |
327 | p->p_ppid = 0; | |
1d348849 MK |
328 | if (*p->p_prev = p->p_nxt) /* off zombproc */ |
329 | p->p_nxt->p_prev = p->p_prev; | |
330 | p->p_nxt = freeproc; /* onto freeproc */ | |
331 | freeproc = p; | |
a8560fa0 SL |
332 | if (q = p->p_ysptr) |
333 | q->p_osptr = p->p_osptr; | |
334 | if (q = p->p_osptr) | |
335 | q->p_ysptr = p->p_ysptr; | |
336 | if ((q = p->p_pptr)->p_cptr == p) | |
337 | q->p_cptr = p->p_osptr; | |
338 | p->p_pptr = 0; | |
339 | p->p_ysptr = 0; | |
340 | p->p_osptr = 0; | |
341 | p->p_cptr = 0; | |
342 | p->p_sig = 0; | |
dd012d1e SL |
343 | p->p_sigcatch = 0; |
344 | p->p_sigignore = 0; | |
345 | p->p_sigmask = 0; | |
1ad494e6 | 346 | /*p->p_pgrp = 0;*/ |
a8560fa0 SL |
347 | p->p_flag = 0; |
348 | p->p_wchan = 0; | |
349 | p->p_cursig = 0; | |
a2528931 | 350 | return (0); |
a8560fa0 | 351 | } |
1ad494e6 MK |
352 | if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && |
353 | (p->p_flag & STRC || uap->options & WUNTRACED)) { | |
a8560fa0 SL |
354 | p->p_flag |= SWTED; |
355 | u.u_r.r_val1 = p->p_pid; | |
1ad494e6 | 356 | #ifdef COMPAT_43 |
a2528931 MK |
357 | if (uap->compat) |
358 | u.u_r.r_val2 = W_STOPCODE(p->p_cursig); | |
1ad494e6 MK |
359 | else |
360 | #endif | |
361 | if (uap->status) { | |
a2528931 MK |
362 | status = W_STOPCODE(p->p_cursig); |
363 | error = copyout((caddr_t)&status, | |
1ad494e6 | 364 | (caddr_t)uap->status, sizeof(status)); |
a2528931 MK |
365 | } else |
366 | error = 0; | |
367 | return (error); | |
a8560fa0 SL |
368 | } |
369 | } | |
a2528931 MK |
370 | if (f == 0) |
371 | return (ECHILD); | |
1ad494e6 | 372 | if (uap->options & WNOHANG) { |
a8560fa0 | 373 | u.u_r.r_val1 = 0; |
a2528931 | 374 | return (0); |
a8560fa0 | 375 | } |
c35e8b3f | 376 | if (setjmp(&u.u_qsave)) { |
fe7f81f4 | 377 | p = u.u_procp; |
a2528931 MK |
378 | if ((u.u_sigintr & sigmask(p->p_cursig)) != 0) |
379 | return (EINTR); | |
a8560fa0 | 380 | u.u_eosys = RESTARTSYS; |
a2528931 | 381 | return (0); |
a8560fa0 SL |
382 | } |
383 | sleep((caddr_t)u.u_procp, PWAIT); | |
384 | goto loop; | |
385 | } |