kern_exit.c revision 1.24 1 /* $NetBSD: kern_exit.c,v 1.24 1994/06/29 06:32:26 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/map.h>
46 #include <sys/ioctl.h>
47 #include <sys/proc.h>
48 #include <sys/tty.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/buf.h>
54 #include <sys/wait.h>
55 #include <sys/file.h>
56 #include <sys/vnode.h>
57 #include <sys/syslog.h>
58 #include <sys/malloc.h>
59 #include <sys/resourcevar.h>
60 #include <sys/ptrace.h>
61
62 #include <machine/cpu.h>
63 #ifdef COMPAT_43
64 #include <machine/reg.h>
65 #include <machine/psl.h>
66 #endif
67
68 #include <vm/vm.h>
69 #include <vm/vm_kern.h>
70
71 __dead void cpu_exit __P((struct proc *));
72 __dead void exit1 __P((struct proc *, int));
73
74 /*
75 * exit --
76 * Death of process.
77 */
78 struct exit_args {
79 int rval;
80 };
81 __dead void
82 exit(p, uap, retval)
83 struct proc *p;
84 struct exit_args *uap;
85 int *retval;
86 {
87
88 exit1(p, W_EXITCODE(uap->rval, 0));
89 /* NOTREACHED */
90 }
91
92 /*
93 * Exit: deallocate address space and other resources, change proc state
94 * to zombie, and unlink proc from allproc and parent's lists. Save exit
95 * status and rusage for wait(). Check for child processes and orphan them.
96 */
97 __dead void
98 exit1(p, rv)
99 register struct proc *p;
100 int rv;
101 {
102 register struct proc *q, *nq;
103 register struct proc **pp;
104 register struct vmspace *vm;
105
106 if (p->p_pid == 1)
107 panic("init died (signal %d, exit %d)",
108 WTERMSIG(rv), WEXITSTATUS(rv));
109 #ifdef PGINPROF
110 vmsizmon();
111 #endif
112 if (p->p_flag & P_PROFIL)
113 stopprofclock(p);
114 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
115 M_ZOMBIE, M_WAITOK);
116 /*
117 * If parent is waiting for us to exit or exec,
118 * P_PPWAIT is set; we will wakeup the parent below.
119 */
120 p->p_flag &= ~(P_TRACED | P_PPWAIT);
121 p->p_flag |= P_WEXIT;
122 p->p_sigignore = ~0;
123 p->p_siglist = 0;
124 untimeout(realitexpire, (caddr_t)p);
125
126 /*
127 * Close open files and release open-file table.
128 * This may block!
129 */
130 fdfree(p);
131
132 /* The next three chunks should probably be moved to vmspace_exit. */
133 vm = p->p_vmspace;
134 #ifdef SYSVSHM
135 if (vm->vm_shm)
136 shmexit(p);
137 #endif
138 #ifdef SYSVSEM
139 semexit(p);
140 #endif
141 /*
142 * Release user portion of address space.
143 * This releases references to vnodes,
144 * which could cause I/O if the file has been unlinked.
145 * Need to do this early enough that we can still sleep.
146 * Can't free the entire vmspace as the kernel stack
147 * may be mapped within that space also.
148 */
149 if (vm->vm_refcnt == 1)
150 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
151 VM_MAXUSER_ADDRESS);
152
153 if (SESS_LEADER(p)) {
154 register struct session *sp = p->p_session;
155
156 if (sp->s_ttyvp) {
157 /*
158 * Controlling process.
159 * Signal foreground pgrp,
160 * drain controlling terminal
161 * and revoke access to controlling terminal.
162 */
163 if (sp->s_ttyp->t_session == sp) {
164 if (sp->s_ttyp->t_pgrp)
165 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
166 (void) ttywait(sp->s_ttyp);
167 /*
168 * The tty could have been revoked
169 * if we blocked.
170 */
171 if (sp->s_ttyvp)
172 vgoneall(sp->s_ttyvp);
173 }
174 if (sp->s_ttyvp)
175 vrele(sp->s_ttyvp);
176 sp->s_ttyvp = NULL;
177 /*
178 * s_ttyp is not zero'd; we use this to indicate
179 * that the session once had a controlling terminal.
180 * (for logging and informational purposes)
181 */
182 }
183 sp->s_leader = NULL;
184 }
185 fixjobc(p, p->p_pgrp, 0);
186 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
187 (void)acct_process(p);
188 #ifdef KTRACE
189 /*
190 * release trace file
191 */
192 p->p_traceflag = 0; /* don't trace the vrele() */
193 if (p->p_tracep)
194 vrele(p->p_tracep);
195 #endif
196 /*
197 * Remove proc from allproc queue and pidhash chain.
198 * Place onto zombproc. Unlink from parent's child list.
199 */
200 if (*p->p_prev = p->p_next)
201 p->p_next->p_prev = p->p_prev;
202 if (p->p_next = zombproc)
203 p->p_next->p_prev = &p->p_next;
204 p->p_prev = &zombproc;
205 zombproc = p;
206 p->p_stat = SZOMB;
207
208 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
209 if (*pp == p) {
210 *pp = p->p_hash;
211 goto done;
212 }
213 panic("exit");
214 done:
215
216 if (p->p_cptr) /* only need this if any child is S_ZOMB */
217 wakeup((caddr_t) initproc);
218 for (q = p->p_cptr; q != NULL; q = nq) {
219 nq = q->p_osptr;
220 if (nq != NULL)
221 nq->p_ysptr = NULL;
222 if (initproc->p_cptr)
223 initproc->p_cptr->p_ysptr = q;
224 q->p_osptr = initproc->p_cptr;
225 q->p_ysptr = NULL;
226 initproc->p_cptr = q;
227
228 q->p_pptr = initproc;
229 /*
230 * Traced processes are killed
231 * since their existence means someone is screwing up.
232 */
233 if (q->p_flag & P_TRACED) {
234 q->p_flag &= ~P_TRACED;
235 psignal(q, SIGKILL);
236 }
237 }
238 p->p_cptr = NULL;
239
240 /*
241 * Save exit status and final rusage info, adding in child rusage
242 * info and self times.
243 */
244 p->p_xstat = rv;
245 *p->p_ru = p->p_stats->p_ru;
246 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
247 ruadd(p->p_ru, &p->p_stats->p_cru);
248
249 /*
250 * Notify parent that we're gone.
251 */
252 psignal(p->p_pptr, SIGCHLD);
253 wakeup((caddr_t)p->p_pptr);
254 /*
255 * Notify procfs debugger
256 */
257 if (p->p_flag & P_FSTRACE)
258 wakeup((caddr_t)p);
259 #if defined(tahoe)
260 /* move this to cpu_exit */
261 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
262 #endif
263 /*
264 * Clear curproc after we've done all operations
265 * that could block, and before tearing down the rest
266 * of the process state that might be used from clock, etc.
267 * Also, can't clear curproc while we're still runnable,
268 * as we're not on a run queue (we are current, just not
269 * a proper proc any longer!).
270 *
271 * Other substructures are freed from wait().
272 */
273 curproc = NULL;
274 if (--p->p_limit->p_refcnt == 0)
275 FREE(p->p_limit, M_SUBPROC);
276
277 /*
278 * Finally, call machine-dependent code to release the remaining
279 * resources including address space, the kernel stack and pcb.
280 * The address space is released by "vmspace_free(p->p_vmspace)";
281 * This is machine-dependent, as we may have to change stacks
282 * or ensure that the current one isn't reallocated before we
283 * finish. cpu_exit will end with a call to cpu_swtch(), finishing
284 * our execution (pun intended).
285 */
286 cpu_exit(p);
287 }
288
289 struct wait_args {
290 int pid;
291 int *status;
292 int options;
293 struct rusage *rusage;
294 #ifdef COMPAT_43
295 int compat; /* pseudo */
296 #endif
297 };
298
299 #ifdef COMPAT_43
300 #ifdef m68k
301 #include <machine/frame.h>
302 #define GETPS(rp) ((struct frame *)(rp))->f_sr
303 #else
304 #define GETPS(rp) (rp)[PS]
305 #endif
306
307 owait(p, uap, retval)
308 struct proc *p;
309 register struct wait_args *uap;
310 int *retval;
311 {
312
313 #ifdef PSL_ALLCC
314 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
315 uap->options = 0;
316 uap->rusage = NULL;
317 } else {
318 uap->options = p->p_md.md_regs[R0];
319 uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
320 }
321 #else
322 uap->options = 0;
323 uap->rusage = NULL;
324 #endif
325 uap->pid = WAIT_ANY;
326 uap->status = NULL;
327 uap->compat = 1;
328 return (wait1(p, uap, retval));
329 }
330
331 wait4(p, uap, retval)
332 struct proc *p;
333 struct wait_args *uap;
334 int *retval;
335 {
336
337 uap->compat = 0;
338 return (wait1(p, uap, retval));
339 }
340 #else
341 #define wait1 wait4
342 #endif
343
344 int
345 wait1(q, uap, retval)
346 register struct proc *q;
347 register struct wait_args *uap;
348 int retval[];
349 {
350 register int nfound;
351 register struct proc *p, *t;
352 int status, error;
353
354 #ifdef COMPAT_09
355 uap->pid = (short) uap->pid;
356 #endif
357
358 if (uap->pid == 0)
359 uap->pid = -q->p_pgid;
360 #ifdef notyet
361 if (uap->options &~ (WUNTRACED|WNOHANG))
362 return (EINVAL);
363 #endif
364 loop:
365 nfound = 0;
366 for (p = q->p_cptr; p; p = p->p_osptr) {
367 if (uap->pid != WAIT_ANY &&
368 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
369 continue;
370 nfound++;
371 if (p->p_stat == SZOMB) {
372 retval[0] = p->p_pid;
373 #ifdef COMPAT_43
374 if (uap->compat)
375 retval[1] = p->p_xstat;
376 else
377 #endif
378 if (uap->status) {
379 status = p->p_xstat; /* convert to int */
380 if (error = copyout((caddr_t)&status,
381 (caddr_t)uap->status, sizeof(status)))
382 return (error);
383 }
384 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
385 (caddr_t)uap->rusage, sizeof (struct rusage))))
386 return (error);
387 /*
388 * If we got the child via a ptrace 'attach',
389 * we need to give it back to the old parent.
390 */
391 if (p->p_oppid && (t = pfind(p->p_oppid))) {
392 p->p_oppid = 0;
393 proc_reparent(p, t);
394 psignal(t, SIGCHLD);
395 wakeup((caddr_t)t);
396 return (0);
397 }
398 p->p_xstat = 0;
399 ruadd(&q->p_stats->p_cru, p->p_ru);
400 FREE(p->p_ru, M_ZOMBIE);
401
402 /*
403 * Decrement the count of procs running with this uid.
404 */
405 (void)chgproccnt(p->p_cred->p_ruid, -1);
406
407 /*
408 * Free up credentials.
409 */
410 if (--p->p_cred->p_refcnt == 0) {
411 crfree(p->p_cred->pc_ucred);
412 FREE(p->p_cred, M_SUBPROC);
413 }
414
415 /*
416 * Release reference to text vnode
417 */
418 if (p->p_textvp)
419 vrele(p->p_textvp);
420
421 /*
422 * Finally finished with old proc entry.
423 * Unlink it from its process group and free it.
424 */
425 leavepgrp(p);
426 if (*p->p_prev = p->p_next) /* off zombproc */
427 p->p_next->p_prev = p->p_prev;
428 if (q = p->p_ysptr)
429 q->p_osptr = p->p_osptr;
430 if (q = p->p_osptr)
431 q->p_ysptr = p->p_ysptr;
432 if ((q = p->p_pptr)->p_cptr == p)
433 q->p_cptr = p->p_osptr;
434
435 /*
436 * Give machine-dependent layer a chance
437 * to free anything that cpu_exit couldn't
438 * release while still running in process context.
439 */
440 cpu_wait(p);
441 FREE(p, M_PROC);
442 nprocs--;
443 return (0);
444 }
445 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
446 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
447 p->p_flag |= P_WAITED;
448 retval[0] = p->p_pid;
449 #ifdef COMPAT_43
450 if (uap->compat) {
451 retval[1] = W_STOPCODE(p->p_xstat);
452 error = 0;
453 } else
454 #endif
455 if (uap->status) {
456 status = W_STOPCODE(p->p_xstat);
457 error = copyout((caddr_t)&status,
458 (caddr_t)uap->status, sizeof(status));
459 } else
460 error = 0;
461 return (error);
462 }
463 }
464 if (nfound == 0)
465 return (ECHILD);
466 if (uap->options & WNOHANG) {
467 retval[0] = 0;
468 return (0);
469 }
470 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
471 return (error);
472 goto loop;
473 }
474
475 /*
476 * make process 'parent' the new parent of process 'child'.
477 */
478 void
479 proc_reparent(child, parent)
480 register struct proc *child;
481 register struct proc *parent;
482 {
483 register struct proc *o;
484 register struct proc *y;
485
486 if (child->p_pptr == parent)
487 return;
488
489 /* fix up the child linkage for the old parent */
490 o = child->p_osptr;
491 y = child->p_ysptr;
492 if (y)
493 y->p_osptr = o;
494 if (o)
495 o->p_ysptr = y;
496 if (child->p_pptr->p_cptr == child)
497 child->p_pptr->p_cptr = o;
498
499 /* fix up child linkage for new parent */
500 o = parent->p_cptr;
501 if (o)
502 o->p_ysptr = child;
503 child->p_osptr = o;
504 child->p_ysptr = NULL;
505 parent->p_cptr = child;
506 child->p_pptr = parent;
507 }
508