xref: /openbsd-src/sys/kern/kern_exit.c (revision 1fc27e414118cd8922c6b93fbaeb7a5246bfd593)
1 /*	$OpenBSD: kern_exit.c,v 1.21 2000/02/21 20:00:09 art Exp $	*/
2 /*	$NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/map.h>
47 #include <sys/ioctl.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/time.h>
51 #include <sys/resource.h>
52 #include <sys/kernel.h>
53 #include <sys/proc.h>
54 #include <sys/buf.h>
55 #include <sys/wait.h>
56 #include <sys/file.h>
57 #include <sys/vnode.h>
58 #include <sys/syslog.h>
59 #include <sys/malloc.h>
60 #include <sys/resourcevar.h>
61 #include <sys/ptrace.h>
62 #include <sys/acct.h>
63 #include <sys/filedesc.h>
64 #include <sys/signalvar.h>
65 #include <sys/sched.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 
121 	if (p->p_flag & P_PROFIL)
122 		stopprofclock(p);
123 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
124 		M_ZOMBIE, M_WAITOK);
125 	/*
126 	 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
127 	 * wake up the parent early to avoid deadlock.
128 	 */
129 	p->p_flag |= P_WEXIT;
130 	p->p_flag &= ~P_TRACED;
131 	if (p->p_flag & P_PPWAIT) {
132 		p->p_flag &= ~P_PPWAIT;
133 		wakeup((caddr_t)p->p_pptr);
134 	}
135 	p->p_sigignore = ~0;
136 	p->p_siglist = 0;
137 	untimeout(realitexpire, (caddr_t)p);
138 
139 	/*
140 	 * Close open files and release open-file table.
141 	 * This may block!
142 	 */
143 	fdfree(p);
144 
145 	/* The next three chunks should probably be moved to vmspace_exit. */
146 	vm = p->p_vmspace;
147 #ifdef SYSVSHM
148 	if (vm->vm_shm && vm->vm_refcnt == 1)
149 		shmexit(vm);
150 #endif
151 #ifdef SYSVSEM
152 	semexit(p);
153 #endif
154 	/*
155 	 * Release user portion of address space.
156 	 * This releases references to vnodes,
157 	 * which could cause I/O if the file has been unlinked.
158 	 * Need to do this early enough that we can still sleep.
159 	 * Can't free the entire vmspace as the kernel stack
160 	 * may be mapped within that space also.
161 	 */
162 #if defined(UVM)
163 	if (vm->vm_refcnt == 1)
164 		(void) uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
165 		    VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
166 #else
167 	if (vm->vm_refcnt == 1)
168 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
169 		    VM_MAXUSER_ADDRESS);
170 #endif
171 
172 	if (SESS_LEADER(p)) {
173 		register struct session *sp = p->p_session;
174 
175 		if (sp->s_ttyvp) {
176 			/*
177 			 * Controlling process.
178 			 * Signal foreground pgrp,
179 			 * drain controlling terminal
180 			 * and revoke access to controlling terminal.
181 			 */
182 			if (sp->s_ttyp->t_session == sp) {
183 				if (sp->s_ttyp->t_pgrp)
184 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
185 				(void) ttywait(sp->s_ttyp);
186 				/*
187 				 * The tty could have been revoked
188 				 * if we blocked.
189 				 */
190 				if (sp->s_ttyvp)
191 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
192 			}
193 			if (sp->s_ttyvp)
194 				vrele(sp->s_ttyvp);
195 			sp->s_ttyvp = NULL;
196 			/*
197 			 * s_ttyp is not zero'd; we use this to indicate
198 			 * that the session once had a controlling terminal.
199 			 * (for logging and informational purposes)
200 			 */
201 		}
202 		sp->s_leader = NULL;
203 	}
204 	fixjobc(p, p->p_pgrp, 0);
205 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
206 	(void)acct_process(p);
207 #ifdef KTRACE
208 	/*
209 	 * release trace file
210 	 */
211 	p->p_traceflag = 0;	/* don't trace the vrele() */
212 	if (p->p_tracep)
213 		vrele(p->p_tracep);
214 #endif
215 	/*
216 	 * Remove proc from allproc queue and pidhash chain.
217 	 * Place onto zombproc.  Unlink from parent's child list.
218 	 */
219 	LIST_REMOVE(p, p_list);
220 	LIST_INSERT_HEAD(&zombproc, p, p_list);
221 	p->p_stat = SZOMB;
222 
223 	LIST_REMOVE(p, p_hash);
224 
225 	q = p->p_children.lh_first;
226 	if (q)		/* only need this if any child is S_ZOMB */
227 		wakeup((caddr_t)initproc);
228 	for (; q != 0; q = nq) {
229 		nq = q->p_sibling.le_next;
230 		proc_reparent(q, initproc);
231 		/*
232 		 * Traced processes are killed
233 		 * since their existence means someone is screwing up.
234 		 */
235 		if (q->p_flag & P_TRACED) {
236 			q->p_flag &= ~P_TRACED;
237 			psignal(q, SIGKILL);
238 		}
239 	}
240 
241 	/*
242 	 * Save exit status and final rusage info, adding in child rusage
243 	 * info and self times.
244 	 */
245 	p->p_xstat = rv;
246 	*p->p_ru = p->p_stats->p_ru;
247 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
248 	ruadd(p->p_ru, &p->p_stats->p_cru);
249 
250 	/*
251 	 * clear %cpu usage during swap
252 	 */
253 	p->p_pctcpu = 0;
254 
255 	/*
256 	 * Notify parent that we're gone.  If we have P_NOZOMBIE or parent has
257 	 * the P_NOCLDWAIT flag set, notify process 1 instead (and hope it
258 	 * will handle this situation).
259 	 */
260 	if ((p->p_flag & P_NOZOMBIE) || (p->p_pptr->p_flag & P_NOCLDWAIT)) {
261 		struct proc *pp = p->p_pptr;
262 		proc_reparent(p, initproc);
263 		/*
264 		 * If this was the last child of our parent, notify
265 		 * parent, so in case he was wait(2)ing, he will
266 		 * continue.
267 		 */
268 		if (pp->p_children.lh_first == NULL)
269 			wakeup((caddr_t)pp);
270 	}
271 	psignal(p->p_pptr, SIGCHLD);
272 	wakeup((caddr_t)p->p_pptr);
273 
274 	/*
275 	 * Notify procfs debugger
276 	 */
277 	if (p->p_flag & P_FSTRACE)
278 		wakeup((caddr_t)p);
279 #if defined(tahoe)
280 	/* move this to cpu_exit */
281 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
282 #endif
283 	/*
284 	 * Clear curproc after we've done all operations
285 	 * that could block, and before tearing down the rest
286 	 * of the process state that might be used from clock, etc.
287 	 * Also, can't clear curproc while we're still runnable,
288 	 * as we're not on a run queue (we are current, just not
289 	 * a proper proc any longer!).
290 	 *
291 	 * Other substructures are freed from wait().
292 	 */
293 	curproc = NULL;
294 	if (--p->p_limit->p_refcnt == 0)
295 		FREE(p->p_limit, M_SUBPROC);
296 
297 	/*
298 	 * Finally, call machine-dependent code to release the remaining
299 	 * resources including address space, the kernel stack and pcb.
300 	 * The address space is released by "vmspace_free(p->p_vmspace)";
301 	 * This is machine-dependent, as we may have to change stacks
302 	 * or ensure that the current one isn't reallocated before we
303 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
304 	 * our execution (pun intended).
305 	 */
306 	cpu_exit(p);
307 }
308 
309 int
310 sys_wait4(q, v, retval)
311 	register struct proc *q;
312 	void *v;
313 	register_t *retval;
314 {
315 	register struct sys_wait4_args /* {
316 		syscallarg(int) pid;
317 		syscallarg(int *) status;
318 		syscallarg(int) options;
319 		syscallarg(struct rusage *) rusage;
320 	} */ *uap = v;
321 	register int nfound;
322 	register struct proc *p, *t;
323 	int status, error;
324 
325 #ifdef COMPAT_09
326 	SCARG(uap, pid) = (short)SCARG(uap, pid);
327 #endif
328 
329 	if (SCARG(uap, pid) == 0)
330 		SCARG(uap, pid) = -q->p_pgid;
331 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
332 		return (EINVAL);
333 
334 loop:
335 	nfound = 0;
336 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
337 		if ((p->p_flag & P_NOZOMBIE) ||
338 		    (SCARG(uap, pid) != WAIT_ANY &&
339 		    p->p_pid != SCARG(uap, pid) &&
340 		    p->p_pgid != -SCARG(uap, pid)))
341 			continue;
342 		nfound++;
343 		if (p->p_stat == SZOMB) {
344 			retval[0] = p->p_pid;
345 
346 			if (SCARG(uap, status)) {
347 				status = p->p_xstat;	/* convert to int */
348 				error = copyout((caddr_t)&status,
349 						(caddr_t)SCARG(uap, status),
350 						sizeof(status));
351 				if (error)
352 					return (error);
353 			}
354 			if (SCARG(uap, rusage) &&
355 			    (error = copyout((caddr_t)p->p_ru,
356 			    (caddr_t)SCARG(uap, rusage),
357 			    sizeof (struct rusage))))
358 				return (error);
359 			/*
360 			 * If we got the child via a ptrace 'attach',
361 			 * we need to give it back to the old parent.
362 			 */
363 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
364 				p->p_oppid = 0;
365 				proc_reparent(p, t);
366 				psignal(t, SIGCHLD);
367 				wakeup((caddr_t)t);
368 				return (0);
369 			}
370 
371 			scheduler_wait_hook(curproc, p);
372 			p->p_xstat = 0;
373 			ruadd(&q->p_stats->p_cru, p->p_ru);
374 			FREE(p->p_ru, M_ZOMBIE);
375 
376 			/*
377 			 * Finally finished with old proc entry.
378 			 * Unlink it from its process group and free it.
379 			 */
380 			leavepgrp(p);
381 			LIST_REMOVE(p, p_list);	/* off zombproc */
382 			LIST_REMOVE(p, p_sibling);
383 
384 			/*
385 			 * Decrement the count of procs running with this uid.
386 			 */
387 			(void)chgproccnt(p->p_cred->p_ruid, -1);
388 
389 			/*
390 			 * Free up credentials.
391 			 */
392 			if (--p->p_cred->p_refcnt == 0) {
393 				crfree(p->p_cred->pc_ucred);
394 				FREE(p->p_cred, M_SUBPROC);
395 			}
396 
397 			/*
398 			 * Release reference to text vnode
399 			 */
400 			if (p->p_textvp)
401 				vrele(p->p_textvp);
402 
403 			/*
404 			 * Give machine-dependent layer a chance
405 			 * to free anything that cpu_exit couldn't
406 			 * release while still running in process context.
407 			 */
408 			cpu_wait(p);
409 			FREE(p, M_PROC);
410 			nprocs--;
411 			return (0);
412 		}
413 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
414 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
415 			p->p_flag |= P_WAITED;
416 			retval[0] = p->p_pid;
417 
418 			if (SCARG(uap, status)) {
419 				status = W_STOPCODE(p->p_xstat);
420 				error = copyout((caddr_t)&status,
421 				    (caddr_t)SCARG(uap, status),
422 				    sizeof(status));
423 			} else
424 				error = 0;
425 			return (error);
426 		}
427 	}
428 	if (nfound == 0)
429 		return (ECHILD);
430 	if (SCARG(uap, options) & WNOHANG) {
431 		retval[0] = 0;
432 		return (0);
433 	}
434 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
435 		return (error);
436 	goto loop;
437 }
438 
439 /*
440  * make process 'parent' the new parent of process 'child'.
441  */
442 void
443 proc_reparent(child, parent)
444 	register struct proc *child;
445 	register struct proc *parent;
446 {
447 
448 	if (child->p_pptr == parent)
449 		return;
450 
451 	LIST_REMOVE(child, p_sibling);
452 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
453 	child->p_pptr = parent;
454 }
455