xref: /freebsd/sys/kern/kern_exit.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
39  * $Id: kern_exit.c,v 1.70 1998/12/19 02:55:33 julian Exp $
40  */
41 
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/pioctl.h>
52 #include <sys/tty.h>
53 #include <sys/wait.h>
54 #include <sys/vnode.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/ptrace.h>
58 #include <sys/acct.h>		/* for acct_process() function prototype */
59 #include <sys/filedesc.h>
60 #include <sys/shm.h>
61 #include <sys/sem.h>
62 #include <sys/aio.h>
63 
64 #ifdef COMPAT_43
65 #include <machine/reg.h>
66 #include <machine/psl.h>
67 #endif
68 #include <machine/limits.h>	/* for UCHAR_MAX = typeof(p_priority)_MAX */
69 
70 #include <vm/vm.h>
71 #include <vm/vm_param.h>
72 #include <sys/lock.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_zone.h>
76 #ifdef COMPAT_LINUX_THREADS
77 #include <sys/user.h>
78 #endif
79 
80 static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
81 
82 static int wait1 __P((struct proc *, struct wait_args *, int));
83 
84 /*
85  * callout list for things to do at exit time
86  */
87 typedef struct exit_list_element {
88 	struct exit_list_element *next;
89 	exitlist_fn function;
90 } *ele_p;
91 
92 static ele_p exit_list;
93 
94 /*
95  * exit --
96  *	Death of process.
97  */
98 void
99 exit(p, uap)
100 	struct proc *p;
101 	struct rexit_args /* {
102 		int	rval;
103 	} */ *uap;
104 {
105 
106 	exit1(p, W_EXITCODE(uap->rval, 0));
107 	/* NOTREACHED */
108 }
109 
110 /*
111  * Exit: deallocate address space and other resources, change proc state
112  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
113  * status and rusage for wait().  Check for child processes and orphan them.
114  */
115 void
116 exit1(p, rv)
117 	register struct proc *p;
118 	int rv;
119 {
120 	register struct proc *q, *nq;
121 	register struct vmspace *vm;
122 	ele_p ep = exit_list;
123 
124 	if (p->p_pid == 1) {
125 		printf("init died (signal %d, exit %d)\n",
126 		    WTERMSIG(rv), WEXITSTATUS(rv));
127 		panic("Going nowhere without my init!");
128 	}
129 
130 	aio_proc_rundown(p);
131 
132 	/* are we a task leader? */
133 	if(p == p->p_leader) {
134         	struct kill_args killArgs;
135 		killArgs.signum = SIGKILL;
136 		q = p->p_peers;
137 		while(q) {
138 			killArgs.pid = q->p_pid;
139 			/*
140 		         * The interface for kill is better
141 			 * than the internal signal
142 			 */
143 			kill(p, &killArgs);
144 			nq = q;
145 			q = q->p_peers;
146 			/*
147 			 * orphan the threads so we don't mess up
148 			 * when they call exit
149 			 */
150 			nq->p_peers = 0;
151 			nq->p_leader = nq;
152 		}
153 
154 	/* otherwise are we a peer? */
155 	} else if(p->p_peers) {
156 		q = p->p_leader;
157 		while(q->p_peers != p)
158 			q = q->p_peers;
159 		q->p_peers = p->p_peers;
160 	}
161 
162 #ifdef PGINPROF
163 	vmsizmon();
164 #endif
165 	STOPEVENT(p, S_EXIT, rv);
166 
167 	/*
168 	 * Check if any LKMs need anything done at process exit.
169 	 * e.g. SYSV IPC stuff
170 	 * XXX what if one of these generates an error?
171 	 */
172 	while (ep) {
173 		(*ep->function)(p);
174 		ep = ep->next;
175 	}
176 
177 	if (p->p_flag & P_PROFIL)
178 		stopprofclock(p);
179 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
180 		M_ZOMBIE, M_WAITOK);
181 	/*
182 	 * If parent is waiting for us to exit or exec,
183 	 * P_PPWAIT is set; we will wakeup the parent below.
184 	 */
185 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
186 	p->p_flag |= P_WEXIT;
187 #ifndef COMPAT_LINUX_THREADS
188 	p->p_sigignore = ~0;
189 #endif /* COMPAT_LINUX_THREADS */
190 	p->p_siglist = 0;
191 	if (timevalisset(&p->p_realtimer.it_value))
192 		untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
193 
194 	/*
195 	 * Reset any sigio structures pointing to us as a result of
196 	 * F_SETOWN with our pid.
197 	 */
198 	funsetownlst(&p->p_sigiolst);
199 
200 	/*
201 	 * Close open files and release open-file table.
202 	 * This may block!
203 	 */
204 	fdfree(p);
205 
206 	/*
207 	 * XXX Shutdown SYSV semaphores
208 	 */
209 	semexit(p);
210 
211 	/* The next two chunks should probably be moved to vmspace_exit. */
212 	vm = p->p_vmspace;
213 	/*
214 	 * Release user portion of address space.
215 	 * This releases references to vnodes,
216 	 * which could cause I/O if the file has been unlinked.
217 	 * Need to do this early enough that we can still sleep.
218 	 * Can't free the entire vmspace as the kernel stack
219 	 * may be mapped within that space also.
220 	 */
221 	if (vm->vm_refcnt == 1) {
222 		if (vm->vm_shm)
223 			shmexit(p);
224 		pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS,
225 		    VM_MAXUSER_ADDRESS);
226 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
227 		    VM_MAXUSER_ADDRESS);
228 	}
229 
230 	if (SESS_LEADER(p)) {
231 		register struct session *sp = p->p_session;
232 
233 		if (sp->s_ttyvp) {
234 			/*
235 			 * Controlling process.
236 			 * Signal foreground pgrp,
237 			 * drain controlling terminal
238 			 * and revoke access to controlling terminal.
239 			 */
240 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
241 				if (sp->s_ttyp->t_pgrp)
242 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
243 				(void) ttywait(sp->s_ttyp);
244 				/*
245 				 * The tty could have been revoked
246 				 * if we blocked.
247 				 */
248 				if (sp->s_ttyvp)
249 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
250 			}
251 			if (sp->s_ttyvp)
252 				vrele(sp->s_ttyvp);
253 			sp->s_ttyvp = NULL;
254 			/*
255 			 * s_ttyp is not zero'd; we use this to indicate
256 			 * that the session once had a controlling terminal.
257 			 * (for logging and informational purposes)
258 			 */
259 		}
260 		sp->s_leader = NULL;
261 	}
262 	fixjobc(p, p->p_pgrp, 0);
263 	(void)acct_process(p);
264 #ifdef KTRACE
265 	/*
266 	 * release trace file
267 	 */
268 	p->p_traceflag = 0;	/* don't trace the vrele() */
269 	if (p->p_tracep)
270 		vrele(p->p_tracep);
271 #endif
272 	/*
273 	 * Remove proc from allproc queue and pidhash chain.
274 	 * Place onto zombproc.  Unlink from parent's child list.
275 	 */
276 	LIST_REMOVE(p, p_list);
277 	LIST_INSERT_HEAD(&zombproc, p, p_list);
278 	p->p_stat = SZOMB;
279 
280 	LIST_REMOVE(p, p_hash);
281 
282 	q = p->p_children.lh_first;
283 	if (q)		/* only need this if any child is S_ZOMB */
284 		wakeup((caddr_t) initproc);
285 	for (; q != 0; q = nq) {
286 		nq = q->p_sibling.le_next;
287 		LIST_REMOVE(q, p_sibling);
288 		LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
289 		q->p_pptr = initproc;
290 #ifdef COMPAT_LINUX_THREADS
291 		q->p_sigparent = 0;
292 #endif /* COMPAT_LINUX_THREADS */
293 		/*
294 		 * Traced processes are killed
295 		 * since their existence means someone is screwing up.
296 		 */
297 		if (q->p_flag & P_TRACED) {
298 			q->p_flag &= ~P_TRACED;
299 			psignal(q, SIGKILL);
300 		}
301 	}
302 
303 	/*
304 	 * Save exit status and final rusage info, adding in child rusage
305 	 * info and self times.
306 	 */
307 	p->p_xstat = rv;
308 	*p->p_ru = p->p_stats->p_ru;
309 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
310 	ruadd(p->p_ru, &p->p_stats->p_cru);
311 
312 	/*
313 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
314 	 * flag set, notify process 1 instead (and hope it will handle
315 	 * this situation).
316 	 */
317 #ifndef COMPAT_LINUX_THREADS
318 	if (p->p_pptr->p_flag & P_NOCLDWAIT) {
319 #else
320 	if (p->p_pptr->p_procsig->ps_flag & P_NOCLDWAIT) {
321 #endif /* COMPAT_LINUX_THREADS */
322 		struct proc *pp = p->p_pptr;
323 		proc_reparent(p, initproc);
324 		/*
325 		 * If this was the last child of our parent, notify
326 		 * parent, so in case he was wait(2)ing, he will
327 		 * continue.
328 		 */
329 		if (LIST_EMPTY(&pp->p_children))
330 			wakeup((caddr_t)pp);
331 	}
332 
333 #ifndef COMPAT_LINUX_THREADS
334 	psignal(p->p_pptr, SIGCHLD);
335 #else
336 	if (p->p_sigparent && p->p_pptr != initproc) {
337 	        psignal(p->p_pptr, p->p_sigparent);
338 	} else {
339 	        psignal(p->p_pptr, SIGCHLD);
340 	}
341 #endif /* COMPAT_LINUX_THREADS */
342 	wakeup((caddr_t)p->p_pptr);
343 #if defined(tahoe)
344 	/* move this to cpu_exit */
345 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
346 #endif
347 	/*
348 	 * Clear curproc after we've done all operations
349 	 * that could block, and before tearing down the rest
350 	 * of the process state that might be used from clock, etc.
351 	 * Also, can't clear curproc while we're still runnable,
352 	 * as we're not on a run queue (we are current, just not
353 	 * a proper proc any longer!).
354 	 *
355 	 * Other substructures are freed from wait().
356 	 */
357 	curproc = NULL;
358 	if (--p->p_limit->p_refcnt == 0) {
359 		FREE(p->p_limit, M_SUBPROC);
360 		p->p_limit = NULL;
361 	}
362 
363 	/*
364 	 * Finally, call machine-dependent code to release the remaining
365 	 * resources including address space, the kernel stack and pcb.
366 	 * The address space is released by "vmspace_free(p->p_vmspace)";
367 	 * This is machine-dependent, as we may have to change stacks
368 	 * or ensure that the current one isn't reallocated before we
369 	 * finish.  cpu_exit will end with a call to cpu_switch(), finishing
370 	 * our execution (pun intended).
371 	 */
372 	cpu_exit(p);
373 }
374 
375 #ifdef COMPAT_43
376 #if defined(hp300) || defined(luna68k)
377 #include <machine/frame.h>
378 #define GETPS(rp)	((struct frame *)(rp))->f_sr
379 #else
380 #define GETPS(rp)	(rp)[PS]
381 #endif
382 
383 int
384 owait(p, uap)
385 	struct proc *p;
386 	register struct owait_args /* {
387 		int     dummy;
388 	} */ *uap;
389 {
390 	struct wait_args w;
391 
392 #ifdef PSL_ALLCC
393 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
394 		w.options = 0;
395 		w.rusage = NULL;
396 	} else {
397 		w.options = p->p_md.md_regs[R0];
398 		w.rusage = (struct rusage *)p->p_md.md_regs[R1];
399 	}
400 #else
401 	w.options = 0;
402 	w.rusage = NULL;
403 #endif
404 	w.pid = WAIT_ANY;
405 	w.status = NULL;
406 	return (wait1(p, &w, 1));
407 }
408 #endif /* COMPAT_43 */
409 
410 int
411 wait4(p, uap)
412 	struct proc *p;
413 	struct wait_args *uap;
414 {
415 
416 	return (wait1(p, uap, 0));
417 }
418 
419 static int
420 wait1(q, uap, compat)
421 	register struct proc *q;
422 	register struct wait_args /* {
423 		int pid;
424 		int *status;
425 		int options;
426 		struct rusage *rusage;
427 	} */ *uap;
428 	int compat;
429 {
430 	register int nfound;
431 	register struct proc *p, *t;
432 	int status, error;
433 
434 	if (uap->pid == 0)
435 		uap->pid = -q->p_pgid;
436 	if (uap->options &~ (WUNTRACED|WNOHANG))
437 		return (EINVAL);
438 loop:
439 	nfound = 0;
440 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
441 		if (uap->pid != WAIT_ANY &&
442 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
443 			continue;
444 		nfound++;
445 		if (p->p_stat == SZOMB) {
446 			/* charge childs scheduling cpu usage to parent */
447 			if (curproc->p_pid != 1) {
448 				curproc->p_estcpu = min(curproc->p_estcpu +
449 				    p->p_estcpu, UCHAR_MAX);
450 			}
451 
452 			q->p_retval[0] = p->p_pid;
453 #ifdef COMPAT_43
454 			if (compat)
455 				q->p_retval[1] = p->p_xstat;
456 			else
457 #endif
458 			if (uap->status) {
459 				status = p->p_xstat;	/* convert to int */
460 				if ((error = copyout((caddr_t)&status,
461 				    (caddr_t)uap->status, sizeof(status))))
462 					return (error);
463 			}
464 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
465 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
466 				return (error);
467 			/*
468 			 * If we got the child via a ptrace 'attach',
469 			 * we need to give it back to the old parent.
470 			 */
471 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
472 				p->p_oppid = 0;
473 				proc_reparent(p, t);
474 				psignal(t, SIGCHLD);
475 				wakeup((caddr_t)t);
476 				return (0);
477 			}
478 			p->p_xstat = 0;
479 			ruadd(&q->p_stats->p_cru, p->p_ru);
480 			FREE(p->p_ru, M_ZOMBIE);
481 			p->p_ru = NULL;
482 
483 			/*
484 			 * Decrement the count of procs running with this uid.
485 			 */
486 			(void)chgproccnt(p->p_cred->p_ruid, -1);
487 
488 			/*
489 			 * Release reference to text vnode
490 			 */
491 			if (p->p_textvp)
492 				vrele(p->p_textvp);
493 
494 			/*
495 			 * Free up credentials.
496 			 */
497 			if (--p->p_cred->p_refcnt == 0) {
498 				crfree(p->p_cred->pc_ucred);
499 				FREE(p->p_cred, M_SUBPROC);
500 				p->p_cred = NULL;
501 			}
502 
503 			/*
504 			 * Finally finished with old proc entry.
505 			 * Unlink it from its process group and free it.
506 			 */
507 			leavepgrp(p);
508 			LIST_REMOVE(p, p_list);	/* off zombproc */
509 			LIST_REMOVE(p, p_sibling);
510 
511 #ifdef COMPAT_LINUX_THREADS
512 			if (--p->p_procsig->ps_refcnt == 0) {
513 				if (p->p_sigacts != &p->p_addr->u_sigacts)
514 					FREE(p->p_sigacts, M_SUBPROC);
515 			        FREE(p->p_procsig, M_SUBPROC);
516 				p->p_procsig = NULL;
517 			}
518 #endif /* COMPAT_LINUX_THREADS */
519 			/*
520 			 * Give machine-dependent layer a chance
521 			 * to free anything that cpu_exit couldn't
522 			 * release while still running in process context.
523 			 */
524 			cpu_wait(p);
525 			zfree(proc_zone, p);
526 			nprocs--;
527 			return (0);
528 		}
529 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
530 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
531 			p->p_flag |= P_WAITED;
532 			q->p_retval[0] = p->p_pid;
533 #ifdef COMPAT_43
534 			if (compat) {
535 				q->p_retval[1] = W_STOPCODE(p->p_xstat);
536 				error = 0;
537 			} else
538 #endif
539 			if (uap->status) {
540 				status = W_STOPCODE(p->p_xstat);
541 				error = copyout((caddr_t)&status,
542 					(caddr_t)uap->status, sizeof(status));
543 			} else
544 				error = 0;
545 			return (error);
546 		}
547 	}
548 	if (nfound == 0)
549 		return (ECHILD);
550 	if (uap->options & WNOHANG) {
551 		q->p_retval[0] = 0;
552 		return (0);
553 	}
554 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
555 		return (error);
556 	goto loop;
557 }
558 
559 /*
560  * make process 'parent' the new parent of process 'child'.
561  */
562 void
563 proc_reparent(child, parent)
564 	register struct proc *child;
565 	register struct proc *parent;
566 {
567 
568 	if (child->p_pptr == parent)
569 		return;
570 
571 	LIST_REMOVE(child, p_sibling);
572 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
573 	child->p_pptr = parent;
574 }
575 
576 /*
577  * The next two functions are to handle adding/deleting items on the
578  * exit callout list
579  *
580  * at_exit():
581  * Take the arguments given and put them onto the exit callout list,
582  * However first make sure that it's not already there.
583  * returns 0 on success.
584  */
585 int
586 at_exit(function)
587 	exitlist_fn function;
588 {
589 	ele_p ep;
590 
591 	/* Be noisy if the programmer has lost track of things */
592 	if (rm_at_exit(function))
593 		printf("exit callout entry already present\n");
594 	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
595 	if (ep == NULL)
596 		return (ENOMEM);
597 	ep->next = exit_list;
598 	ep->function = function;
599 	exit_list = ep;
600 	return (0);
601 }
602 /*
603  * Scan the exit callout list for the given items and remove them.
604  * Returns the number of items removed.
605  * Logically this can only be 0 or 1.
606  */
607 int
608 rm_at_exit(function)
609 	exitlist_fn function;
610 {
611 	ele_p *epp, ep;
612 	int count;
613 
614 	count = 0;
615 	epp = &exit_list;
616 	ep = *epp;
617 	while (ep) {
618 		if (ep->function == function) {
619 			*epp = ep->next;
620 			free(ep, M_TEMP);
621 			count++;
622 		} else {
623 			epp = &ep->next;
624 		}
625 		ep = *epp;
626 	}
627 	return (count);
628 }
629 
630 #ifdef COMPAT_LINUX_THREADS
631 void check_sigacts (void)
632 {
633 	struct proc *p = curproc;
634 	struct sigacts *pss;
635 	int s;
636 
637 	if (p->p_procsig->ps_refcnt == 1 &&
638 	    p->p_sigacts != &p->p_addr->u_sigacts) {
639 		pss = p->p_sigacts;
640 		s = splhigh();
641 		p->p_addr->u_sigacts = *pss;
642 		p->p_sigacts = &p->p_addr->u_sigacts;
643 		splx(s);
644 		FREE(pss, M_SUBPROC);
645 	}
646 }
647 #endif
648