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