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