xref: /netbsd-src/sys/kern/kern_exit.c (revision 705ee976cfe4e85b18732519865b1f7b6cbb2322)
1 /*	$NetBSD: kern_exit.c,v 1.28 1994/10/30 21:47:39 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 <sys/mount.h>
63 #include <sys/syscallargs.h>
64 
65 #include <machine/cpu.h>
66 #ifdef COMPAT_43
67 #include <machine/reg.h>
68 #include <machine/psl.h>
69 #endif
70 
71 #include <vm/vm.h>
72 #include <vm/vm_kern.h>
73 
74 __dead void cpu_exit __P((struct proc *)) __attribute__((noreturn));
75 __dead void exit1 __P((struct proc *, int)) __attribute__((noreturn));
76 
77 /*
78  * exit --
79  *	Death of process.
80  */
81 __dead void
82 exit(p, uap, retval)
83 	struct proc *p;
84 	struct exit_args /* {
85 		syscallarg(int) rval;
86 	} */ *uap;
87 	int *retval;
88 {
89 
90 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
91 	/* NOTREACHED */
92 }
93 
94 /*
95  * Exit: deallocate address space and other resources, change proc state
96  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
97  * status and rusage for wait().  Check for child processes and orphan them.
98  */
99 __dead void
100 exit1(p, rv)
101 	register struct proc *p;
102 	int rv;
103 {
104 	register struct proc *q, *nq;
105 	register struct proc **pp;
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 compat_43_wait(p, uap, retval)
282 	struct proc *p;
283 	void *uap;
284 	int *retval;
285 {
286 	struct wait4_args /* {
287 		syscallarg(int) pid;
288 		syscallarg(int *) status;
289 		syscallarg(int) options;
290 		syscallarg(struct rusage *) rusage;
291 	} */ a;
292 
293 #ifdef PSL_ALLCC
294 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
295 		SCARG(&a, options) = 0;
296 		SCARG(&a, rusage) = NULL;
297 	} else {
298 		SCARG(&a, options) = p->p_md.md_regs[R0];
299 		SCARG(&a, rusage) = (struct rusage *)p->p_md.md_regs[R1];
300 	}
301 #else
302 	SCARG(&a, options) = 0;
303 	SCARG(&a, rusage) = NULL;
304 #endif
305 	SCARG(&a, pid) = WAIT_ANY;
306 	SCARG(&a, status) = NULL;
307 	return (wait1(p, &a, retval, 1));
308 }
309 
310 wait4(p, uap, retval)
311 	struct proc *p;
312 	struct wait4_args /* {
313 		syscallarg(int) pid;
314 		syscallarg(int *) status;
315 		syscallarg(int) options;
316 		syscallarg(struct rusage *) rusage;
317 	} */ *uap;
318 	int *retval;
319 {
320 
321 	return (wait1(p, uap, retval, 0));
322 }
323 #else
324 #define	wait1	wait4
325 #endif
326 
327 int
328 wait1(q, uap, retval, compat)
329 	register struct proc *q;
330 	register struct wait4_args /* {
331 		syscallarg(int) pid;
332 		syscallarg(int *) status;
333 		syscallarg(int) options;
334 		syscallarg(struct rusage *) rusage;
335 	} */ *uap;
336 	register_t *retval;
337 #ifdef COMPAT_43
338 	int compat;
339 #endif
340 {
341 	register int nfound;
342 	register struct proc *p, *t;
343 	int status, error;
344 
345 #ifdef COMPAT_09
346 	SCARG(uap, pid) = (short)SCARG(uap, pid);
347 #endif
348 
349 	if (SCARG(uap, pid) == 0)
350 		SCARG(uap, pid) = -q->p_pgid;
351 #ifdef notyet
352 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
353 		return (EINVAL);
354 #endif
355 loop:
356 	nfound = 0;
357 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
358 		if (SCARG(uap, pid) != WAIT_ANY &&
359 		    p->p_pid != SCARG(uap, pid) &&
360 		    p->p_pgid != -SCARG(uap, pid))
361 			continue;
362 		nfound++;
363 		if (p->p_stat == SZOMB) {
364 			retval[0] = p->p_pid;
365 #ifdef COMPAT_43
366 			if (compat)
367 				retval[1] = p->p_xstat;
368 			else
369 #endif
370 			if (SCARG(uap, status)) {
371 				status = p->p_xstat;	/* convert to int */
372 				if (error = copyout((caddr_t)&status,
373 				    (caddr_t)SCARG(uap, status),
374 				    sizeof(status)))
375 					return (error);
376 			}
377 			if (SCARG(uap, rusage) &&
378 			    (error = copyout((caddr_t)p->p_ru,
379 			    (caddr_t)SCARG(uap, rusage),
380 			    sizeof (struct rusage))))
381 				return (error);
382 			/*
383 			 * If we got the child via a ptrace 'attach',
384 			 * we need to give it back to the old parent.
385 			 */
386 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
387 				p->p_oppid = 0;
388 				proc_reparent(p, t);
389 				psignal(t, SIGCHLD);
390 				wakeup((caddr_t)t);
391 				return (0);
392 			}
393 			p->p_xstat = 0;
394 			ruadd(&q->p_stats->p_cru, p->p_ru);
395 			FREE(p->p_ru, M_ZOMBIE);
396 
397 			/*
398 			 * Decrement the count of procs running with this uid.
399 			 */
400 			(void)chgproccnt(p->p_cred->p_ruid, -1);
401 
402 			/*
403 			 * Free up credentials.
404 			 */
405 			if (--p->p_cred->p_refcnt == 0) {
406 				crfree(p->p_cred->pc_ucred);
407 				FREE(p->p_cred, M_SUBPROC);
408 			}
409 
410 			/*
411 			 * Release reference to text vnode
412 			 */
413 			if (p->p_textvp)
414 				vrele(p->p_textvp);
415 
416 			/*
417 			 * Finally finished with old proc entry.
418 			 * Unlink it from its process group and free it.
419 			 */
420 			leavepgrp(p);
421 			LIST_REMOVE(p, p_list);	/* off zombproc */
422 			LIST_REMOVE(p, p_sibling);
423 
424 			/*
425 			 * Give machine-dependent layer a chance
426 			 * to free anything that cpu_exit couldn't
427 			 * release while still running in process context.
428 			 */
429 			cpu_wait(p);
430 			FREE(p, M_PROC);
431 			nprocs--;
432 			return (0);
433 		}
434 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
435 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
436 			p->p_flag |= P_WAITED;
437 			retval[0] = p->p_pid;
438 #ifdef COMPAT_43
439 			if (compat) {
440 				retval[1] = W_STOPCODE(p->p_xstat);
441 				error = 0;
442 			} else
443 #endif
444 			if (SCARG(uap, status)) {
445 				status = W_STOPCODE(p->p_xstat);
446 				error = copyout((caddr_t)&status,
447 				    (caddr_t)SCARG(uap, status),
448 				    sizeof(status));
449 			} else
450 				error = 0;
451 			return (error);
452 		}
453 	}
454 	if (nfound == 0)
455 		return (ECHILD);
456 	if (SCARG(uap, options) & WNOHANG) {
457 		retval[0] = 0;
458 		return (0);
459 	}
460 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
461 		return (error);
462 	goto loop;
463 }
464 
465 /*
466  * make process 'parent' the new parent of process 'child'.
467  */
468 void
469 proc_reparent(child, parent)
470 	register struct proc *child;
471 	register struct proc *parent;
472 {
473 
474 	if (child->p_pptr == parent)
475 		return;
476 
477 	LIST_REMOVE(child, p_sibling);
478 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
479 	child->p_pptr = parent;
480 }
481