xref: /netbsd-src/sys/kern/kern_exit.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: kern_exit.c,v 1.29 1994/12/24 15:07: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 #include <sys/acct.h>
62 
63 #include <sys/mount.h>
64 #include <sys/syscallargs.h>
65 
66 #include <machine/cpu.h>
67 #ifdef COMPAT_43
68 #include <machine/reg.h>
69 #include <machine/psl.h>
70 #endif
71 
72 #include <vm/vm.h>
73 #include <vm/vm_kern.h>
74 
75 __dead void cpu_exit __P((struct proc *)) __attribute__((noreturn));
76 __dead void exit1 __P((struct proc *, int)) __attribute__((noreturn));
77 
78 /*
79  * exit --
80  *	Death of process.
81  */
82 __dead void
83 exit(p, uap, retval)
84 	struct proc *p;
85 	struct exit_args /* {
86 		syscallarg(int) rval;
87 	} */ *uap;
88 	int *retval;
89 {
90 
91 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
92 	/* NOTREACHED */
93 }
94 
95 /*
96  * Exit: deallocate address space and other resources, change proc state
97  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
98  * status and rusage for wait().  Check for child processes and orphan them.
99  */
100 __dead void
101 exit1(p, rv)
102 	register struct proc *p;
103 	int rv;
104 {
105 	register struct proc *q, *nq;
106 	register struct vmspace *vm;
107 
108 	if (p->p_pid == 1)
109 		panic("init died (signal %d, exit %d)",
110 		    WTERMSIG(rv), WEXITSTATUS(rv));
111 #ifdef PGINPROF
112 	vmsizmon();
113 #endif
114 	if (p->p_flag & P_PROFIL)
115 		stopprofclock(p);
116 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
117 		M_ZOMBIE, M_WAITOK);
118 	/*
119 	 * If parent is waiting for us to exit or exec,
120 	 * P_PPWAIT is set; we will wakeup the parent below.
121 	 */
122 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
123 	p->p_flag |= P_WEXIT;
124 	p->p_sigignore = ~0;
125 	p->p_siglist = 0;
126 	untimeout(realitexpire, (caddr_t)p);
127 
128 	/*
129 	 * Close open files and release open-file table.
130 	 * This may block!
131 	 */
132 	fdfree(p);
133 
134 	/* The next three chunks should probably be moved to vmspace_exit. */
135 	vm = p->p_vmspace;
136 #ifdef SYSVSHM
137 	if (vm->vm_shm)
138 		shmexit(p);
139 #endif
140 #ifdef SYSVSEM
141 	semexit(p);
142 #endif
143 	/*
144 	 * Release user portion of address space.
145 	 * This releases references to vnodes,
146 	 * which could cause I/O if the file has been unlinked.
147 	 * Need to do this early enough that we can still sleep.
148 	 * Can't free the entire vmspace as the kernel stack
149 	 * may be mapped within that space also.
150 	 */
151 	if (vm->vm_refcnt == 1)
152 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
153 		    VM_MAXUSER_ADDRESS);
154 
155 	if (SESS_LEADER(p)) {
156 		register struct session *sp = p->p_session;
157 
158 		if (sp->s_ttyvp) {
159 			/*
160 			 * Controlling process.
161 			 * Signal foreground pgrp,
162 			 * drain controlling terminal
163 			 * and revoke access to controlling terminal.
164 			 */
165 			if (sp->s_ttyp->t_session == sp) {
166 				if (sp->s_ttyp->t_pgrp)
167 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
168 				(void) ttywait(sp->s_ttyp);
169 				/*
170 				 * The tty could have been revoked
171 				 * if we blocked.
172 				 */
173 				if (sp->s_ttyvp)
174 					vgoneall(sp->s_ttyvp);
175 			}
176 			if (sp->s_ttyvp)
177 				vrele(sp->s_ttyvp);
178 			sp->s_ttyvp = NULL;
179 			/*
180 			 * s_ttyp is not zero'd; we use this to indicate
181 			 * that the session once had a controlling terminal.
182 			 * (for logging and informational purposes)
183 			 */
184 		}
185 		sp->s_leader = NULL;
186 	}
187 	fixjobc(p, p->p_pgrp, 0);
188 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
189 	(void)acct_process(p);
190 #ifdef KTRACE
191 	/*
192 	 * release trace file
193 	 */
194 	p->p_traceflag = 0;	/* don't trace the vrele() */
195 	if (p->p_tracep)
196 		vrele(p->p_tracep);
197 #endif
198 	/*
199 	 * Remove proc from allproc queue and pidhash chain.
200 	 * Place onto zombproc.  Unlink from parent's child list.
201 	 */
202 	LIST_REMOVE(p, p_list);
203 	LIST_INSERT_HEAD(&zombproc, p, p_list);
204 	p->p_stat = SZOMB;
205 
206 	LIST_REMOVE(p, p_hash);
207 
208 	q = p->p_children.lh_first;
209 	if (q)		/* only need this if any child is S_ZOMB */
210 		wakeup((caddr_t) initproc);
211 	for (; q != 0; q = nq) {
212 		nq = q->p_sibling.le_next;
213 		proc_reparent(q, initproc);
214 		/*
215 		 * Traced processes are killed
216 		 * since their existence means someone is screwing up.
217 		 */
218 		if (q->p_flag & P_TRACED) {
219 			q->p_flag &= ~P_TRACED;
220 			psignal(q, SIGKILL);
221 		}
222 	}
223 
224 	/*
225 	 * Save exit status and final rusage info, adding in child rusage
226 	 * info and self times.
227 	 */
228 	p->p_xstat = rv;
229 	*p->p_ru = p->p_stats->p_ru;
230 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
231 	ruadd(p->p_ru, &p->p_stats->p_cru);
232 
233 	/*
234 	 * Notify parent that we're gone.
235 	 */
236 	psignal(p->p_pptr, SIGCHLD);
237 	wakeup((caddr_t)p->p_pptr);
238 	/*
239 	 * Notify procfs debugger
240 	 */
241 	if (p->p_flag & P_FSTRACE)
242 		wakeup((caddr_t)p);
243 #if defined(tahoe)
244 	/* move this to cpu_exit */
245 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
246 #endif
247 	/*
248 	 * Clear curproc after we've done all operations
249 	 * that could block, and before tearing down the rest
250 	 * of the process state that might be used from clock, etc.
251 	 * Also, can't clear curproc while we're still runnable,
252 	 * as we're not on a run queue (we are current, just not
253 	 * a proper proc any longer!).
254 	 *
255 	 * Other substructures are freed from wait().
256 	 */
257 	curproc = NULL;
258 	if (--p->p_limit->p_refcnt == 0)
259 		FREE(p->p_limit, M_SUBPROC);
260 
261 	/*
262 	 * Finally, call machine-dependent code to release the remaining
263 	 * resources including address space, the kernel stack and pcb.
264 	 * The address space is released by "vmspace_free(p->p_vmspace)";
265 	 * This is machine-dependent, as we may have to change stacks
266 	 * or ensure that the current one isn't reallocated before we
267 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
268 	 * our execution (pun intended).
269 	 */
270 	cpu_exit(p);
271 }
272 
273 #ifdef COMPAT_43
274 #ifdef m68k
275 #include <machine/frame.h>
276 #define GETPS(rp)	((struct frame *)(rp))->f_sr
277 #else
278 #define GETPS(rp)	(rp)[PS]
279 #endif
280 
281 int
282 compat_43_wait(p, uap, retval)
283 	struct proc *p;
284 	void *uap;
285 	int *retval;
286 {
287 	struct wait4_args /* {
288 		syscallarg(int) pid;
289 		syscallarg(int *) status;
290 		syscallarg(int) options;
291 		syscallarg(struct rusage *) rusage;
292 	} */ a;
293 
294 #ifdef PSL_ALLCC
295 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
296 		SCARG(&a, options) = 0;
297 		SCARG(&a, rusage) = NULL;
298 	} else {
299 		SCARG(&a, options) = p->p_md.md_regs[R0];
300 		SCARG(&a, rusage) = (struct rusage *)p->p_md.md_regs[R1];
301 	}
302 #else
303 	SCARG(&a, options) = 0;
304 	SCARG(&a, rusage) = NULL;
305 #endif
306 	SCARG(&a, pid) = WAIT_ANY;
307 	SCARG(&a, status) = NULL;
308 	return (wait1(p, &a, retval, 1));
309 }
310 
311 int
312 wait4(p, uap, retval)
313 	struct proc *p;
314 	struct wait4_args /* {
315 		syscallarg(int) pid;
316 		syscallarg(int *) status;
317 		syscallarg(int) options;
318 		syscallarg(struct rusage *) rusage;
319 	} */ *uap;
320 	int *retval;
321 {
322 
323 	return (wait1(p, uap, retval, 0));
324 }
325 #else
326 #define	wait1	wait4
327 #endif
328 
329 int
330 wait1(q, uap, retval, compat)
331 	register struct proc *q;
332 	register struct wait4_args /* {
333 		syscallarg(int) pid;
334 		syscallarg(int *) status;
335 		syscallarg(int) options;
336 		syscallarg(struct rusage *) rusage;
337 	} */ *uap;
338 	register_t *retval;
339 #ifdef COMPAT_43
340 	int compat;
341 #endif
342 {
343 	register int nfound;
344 	register struct proc *p, *t;
345 	int status, error;
346 
347 #ifdef COMPAT_09
348 	SCARG(uap, pid) = (short)SCARG(uap, pid);
349 #endif
350 
351 	if (SCARG(uap, pid) == 0)
352 		SCARG(uap, pid) = -q->p_pgid;
353 #ifdef notyet
354 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
355 		return (EINVAL);
356 #endif
357 loop:
358 	nfound = 0;
359 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
360 		if (SCARG(uap, pid) != WAIT_ANY &&
361 		    p->p_pid != SCARG(uap, pid) &&
362 		    p->p_pgid != -SCARG(uap, pid))
363 			continue;
364 		nfound++;
365 		if (p->p_stat == SZOMB) {
366 			retval[0] = p->p_pid;
367 #ifdef COMPAT_43
368 			if (compat)
369 				retval[1] = p->p_xstat;
370 			else
371 #endif
372 			if (SCARG(uap, status)) {
373 				status = p->p_xstat;	/* convert to int */
374 				if (error = copyout((caddr_t)&status,
375 				    (caddr_t)SCARG(uap, status),
376 				    sizeof(status)))
377 					return (error);
378 			}
379 			if (SCARG(uap, rusage) &&
380 			    (error = copyout((caddr_t)p->p_ru,
381 			    (caddr_t)SCARG(uap, rusage),
382 			    sizeof (struct rusage))))
383 				return (error);
384 			/*
385 			 * If we got the child via a ptrace 'attach',
386 			 * we need to give it back to the old parent.
387 			 */
388 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
389 				p->p_oppid = 0;
390 				proc_reparent(p, t);
391 				psignal(t, SIGCHLD);
392 				wakeup((caddr_t)t);
393 				return (0);
394 			}
395 			p->p_xstat = 0;
396 			ruadd(&q->p_stats->p_cru, p->p_ru);
397 			FREE(p->p_ru, M_ZOMBIE);
398 
399 			/*
400 			 * Decrement the count of procs running with this uid.
401 			 */
402 			(void)chgproccnt(p->p_cred->p_ruid, -1);
403 
404 			/*
405 			 * Free up credentials.
406 			 */
407 			if (--p->p_cred->p_refcnt == 0) {
408 				crfree(p->p_cred->pc_ucred);
409 				FREE(p->p_cred, M_SUBPROC);
410 			}
411 
412 			/*
413 			 * Release reference to text vnode
414 			 */
415 			if (p->p_textvp)
416 				vrele(p->p_textvp);
417 
418 			/*
419 			 * Finally finished with old proc entry.
420 			 * Unlink it from its process group and free it.
421 			 */
422 			leavepgrp(p);
423 			LIST_REMOVE(p, p_list);	/* off zombproc */
424 			LIST_REMOVE(p, p_sibling);
425 
426 			/*
427 			 * Give machine-dependent layer a chance
428 			 * to free anything that cpu_exit couldn't
429 			 * release while still running in process context.
430 			 */
431 			cpu_wait(p);
432 			FREE(p, M_PROC);
433 			nprocs--;
434 			return (0);
435 		}
436 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
437 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
438 			p->p_flag |= P_WAITED;
439 			retval[0] = p->p_pid;
440 #ifdef COMPAT_43
441 			if (compat) {
442 				retval[1] = W_STOPCODE(p->p_xstat);
443 				error = 0;
444 			} else
445 #endif
446 			if (SCARG(uap, status)) {
447 				status = W_STOPCODE(p->p_xstat);
448 				error = copyout((caddr_t)&status,
449 				    (caddr_t)SCARG(uap, status),
450 				    sizeof(status));
451 			} else
452 				error = 0;
453 			return (error);
454 		}
455 	}
456 	if (nfound == 0)
457 		return (ECHILD);
458 	if (SCARG(uap, options) & WNOHANG) {
459 		retval[0] = 0;
460 		return (0);
461 	}
462 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
463 		return (error);
464 	goto loop;
465 }
466 
467 /*
468  * make process 'parent' the new parent of process 'child'.
469  */
470 void
471 proc_reparent(child, parent)
472 	register struct proc *child;
473 	register struct proc *parent;
474 {
475 
476 	if (child->p_pptr == parent)
477 		return;
478 
479 	LIST_REMOVE(child, p_sibling);
480 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
481 	child->p_pptr = parent;
482 }
483