xref: /netbsd-src/sys/kern/kern_exit.c (revision 9573504567626934c7ee01c7dce0c4bb1dfe7403)
1 /*	$NetBSD: kern_exit.c,v 1.34 1995/12/09 04:09:32 mycroft 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 
68 #include <vm/vm.h>
69 #include <vm/vm_kern.h>
70 
71 void cpu_exit __P((struct proc *));	/* XXX MOVE ME */
72 void exit1 __P((struct proc *, int));
73 
74 /*
75  * exit --
76  *	Death of process.
77  */
78 int
79 sys_exit(p, v, retval)
80 	struct proc *p;
81 	void *v;
82 	register_t *retval;
83 {
84 	struct sys_exit_args /* {
85 		syscallarg(int) rval;
86 	} */ *uap = v;
87 
88 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
89 	/* NOTREACHED */
90 	return (0);
91 }
92 
93 /*
94  * Exit: deallocate address space and other resources, change proc state
95  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
96  * status and rusage for wait().  Check for child processes and orphan them.
97  */
98 void
99 exit1(p, rv)
100 	register struct proc *p;
101 	int rv;
102 {
103 	register struct proc *q, *nq;
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 	LIST_REMOVE(p, p_list);
201 	LIST_INSERT_HEAD(&zombproc, p, p_list);
202 	p->p_stat = SZOMB;
203 
204 	LIST_REMOVE(p, p_hash);
205 
206 	q = p->p_children.lh_first;
207 	if (q)		/* only need this if any child is S_ZOMB */
208 		wakeup((caddr_t) initproc);
209 	for (; q != 0; q = nq) {
210 		nq = q->p_sibling.le_next;
211 		proc_reparent(q, initproc);
212 		/*
213 		 * Traced processes are killed
214 		 * since their existence means someone is screwing up.
215 		 */
216 		if (q->p_flag & P_TRACED) {
217 			q->p_flag &= ~P_TRACED;
218 			psignal(q, SIGKILL);
219 		}
220 	}
221 
222 	/*
223 	 * Save exit status and final rusage info, adding in child rusage
224 	 * info and self times.
225 	 */
226 	p->p_xstat = rv;
227 	*p->p_ru = p->p_stats->p_ru;
228 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
229 	ruadd(p->p_ru, &p->p_stats->p_cru);
230 
231 	/*
232 	 * Notify parent that we're gone.
233 	 */
234 	psignal(p->p_pptr, SIGCHLD);
235 	wakeup((caddr_t)p->p_pptr);
236 	/*
237 	 * Notify procfs debugger
238 	 */
239 	if (p->p_flag & P_FSTRACE)
240 		wakeup((caddr_t)p);
241 #if defined(tahoe)
242 	/* move this to cpu_exit */
243 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
244 #endif
245 	/*
246 	 * Clear curproc after we've done all operations
247 	 * that could block, and before tearing down the rest
248 	 * of the process state that might be used from clock, etc.
249 	 * Also, can't clear curproc while we're still runnable,
250 	 * as we're not on a run queue (we are current, just not
251 	 * a proper proc any longer!).
252 	 *
253 	 * Other substructures are freed from wait().
254 	 */
255 	curproc = NULL;
256 	limfree(p->p_limit);
257 
258 	/*
259 	 * Finally, call machine-dependent code to release the remaining
260 	 * resources including address space, the kernel stack and pcb.
261 	 * The address space is released by "vmspace_free(p->p_vmspace)";
262 	 * This is machine-dependent, as we may have to change stacks
263 	 * or ensure that the current one isn't reallocated before we
264 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
265 	 * our execution (pun intended).
266 	 */
267 	cpu_exit(p);
268 }
269 
270 int
271 sys_wait4(q, v, retval)
272 	register struct proc *q;
273 	void *v;
274 	register_t *retval;
275 {
276 	register struct sys_wait4_args /* {
277 		syscallarg(int) pid;
278 		syscallarg(int *) status;
279 		syscallarg(int) options;
280 		syscallarg(struct rusage *) rusage;
281 	} */ *uap = v;
282 	register int nfound;
283 	register struct proc *p, *t;
284 	int status, error;
285 
286 #ifdef COMPAT_09
287 	SCARG(uap, pid) = (short)SCARG(uap, pid);
288 #endif
289 
290 	if (SCARG(uap, pid) == 0)
291 		SCARG(uap, pid) = -q->p_pgid;
292 #ifdef notyet
293 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
294 		return (EINVAL);
295 #endif
296 loop:
297 	nfound = 0;
298 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
299 		if (SCARG(uap, pid) != WAIT_ANY &&
300 		    p->p_pid != SCARG(uap, pid) &&
301 		    p->p_pgid != -SCARG(uap, pid))
302 			continue;
303 		nfound++;
304 		if (p->p_stat == SZOMB) {
305 			retval[0] = p->p_pid;
306 
307 			if (SCARG(uap, status)) {
308 				status = p->p_xstat;	/* convert to int */
309 				if (error = copyout((caddr_t)&status,
310 				    (caddr_t)SCARG(uap, status),
311 				    sizeof(status)))
312 					return (error);
313 			}
314 			if (SCARG(uap, rusage) &&
315 			    (error = copyout((caddr_t)p->p_ru,
316 			    (caddr_t)SCARG(uap, rusage),
317 			    sizeof (struct rusage))))
318 				return (error);
319 			/*
320 			 * If we got the child via a ptrace 'attach',
321 			 * we need to give it back to the old parent.
322 			 */
323 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
324 				p->p_oppid = 0;
325 				proc_reparent(p, t);
326 				psignal(t, SIGCHLD);
327 				wakeup((caddr_t)t);
328 				return (0);
329 			}
330 			p->p_xstat = 0;
331 			ruadd(&q->p_stats->p_cru, p->p_ru);
332 			FREE(p->p_ru, M_ZOMBIE);
333 
334 			/*
335 			 * Decrement the count of procs running with this uid.
336 			 */
337 			(void)chgproccnt(p->p_cred->p_ruid, -1);
338 
339 			/*
340 			 * Free up credentials.
341 			 */
342 			if (--p->p_cred->p_refcnt == 0) {
343 				crfree(p->p_cred->pc_ucred);
344 				FREE(p->p_cred, M_SUBPROC);
345 			}
346 
347 			/*
348 			 * Release reference to text vnode
349 			 */
350 			if (p->p_textvp)
351 				vrele(p->p_textvp);
352 
353 			/*
354 			 * Finally finished with old proc entry.
355 			 * Unlink it from its process group and free it.
356 			 */
357 			leavepgrp(p);
358 			LIST_REMOVE(p, p_list);	/* off zombproc */
359 			LIST_REMOVE(p, p_sibling);
360 
361 			/*
362 			 * Give machine-dependent layer a chance
363 			 * to free anything that cpu_exit couldn't
364 			 * release while still running in process context.
365 			 */
366 			cpu_wait(p);
367 			FREE(p, M_PROC);
368 			nprocs--;
369 			return (0);
370 		}
371 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
372 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
373 			p->p_flag |= P_WAITED;
374 			retval[0] = p->p_pid;
375 
376 			if (SCARG(uap, status)) {
377 				status = W_STOPCODE(p->p_xstat);
378 				error = copyout((caddr_t)&status,
379 				    (caddr_t)SCARG(uap, status),
380 				    sizeof(status));
381 			} else
382 				error = 0;
383 			return (error);
384 		}
385 	}
386 	if (nfound == 0)
387 		return (ECHILD);
388 	if (SCARG(uap, options) & WNOHANG) {
389 		retval[0] = 0;
390 		return (0);
391 	}
392 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
393 		return (error);
394 	goto loop;
395 }
396 
397 /*
398  * make process 'parent' the new parent of process 'child'.
399  */
400 void
401 proc_reparent(child, parent)
402 	register struct proc *child;
403 	register struct proc *parent;
404 {
405 
406 	if (child->p_pptr == parent)
407 		return;
408 
409 	LIST_REMOVE(child, p_sibling);
410 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
411 	child->p_pptr = parent;
412 }
413