xref: /csrg-svn/sys/kern/kern_exit.c (revision 42916)
123369Smckusick /*
237728Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337728Smckusick  * All rights reserved.
423369Smckusick  *
537728Smckusick  * Redistribution and use in source and binary forms are permitted
637728Smckusick  * provided that the above copyright notice and this paragraph are
737728Smckusick  * duplicated in all such forms and that any documentation,
837728Smckusick  * advertising materials, and other materials related to such
937728Smckusick  * distribution and use acknowledge that the software was developed
1037728Smckusick  * by the University of California, Berkeley.  The name of the
1137728Smckusick  * University may not be used to endorse or promote products derived
1237728Smckusick  * from this software without specific prior written permission.
1337728Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437728Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537728Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637728Smckusick  *
17*42916Smarc  *	@(#)kern_exit.c	7.21 (Berkeley) 06/05/90
1823369Smckusick  */
1912790Ssam 
2017090Sbloom #include "param.h"
2117090Sbloom #include "systm.h"
2217090Sbloom #include "map.h"
2317090Sbloom #include "user.h"
2417090Sbloom #include "kernel.h"
2517090Sbloom #include "proc.h"
2617090Sbloom #include "buf.h"
2717090Sbloom #include "wait.h"
2817090Sbloom #include "vm.h"
2917090Sbloom #include "file.h"
3037728Smckusick #include "vnode.h"
3139410Skarels #include "ioctl.h"
3237328Skarels #include "tty.h"
3318638Ssam #include "syslog.h"
3432018Smckusick #include "malloc.h"
3512790Ssam 
3637520Smckusick #include "machine/reg.h"
3737328Skarels #ifdef COMPAT_43
3837520Smckusick #include "machine/psl.h"
3937328Skarels #endif
4037328Skarels 
4112790Ssam /*
4212790Ssam  * Exit system call: pass back caller's arg
4312790Ssam  */
4412790Ssam rexit()
4512790Ssam {
4639410Skarels 	struct a {
4712790Ssam 		int	rval;
4812790Ssam 	} *uap;
4912790Ssam 
5012790Ssam 	uap = (struct a *)u.u_ap;
5139410Skarels 	exit(W_EXITCODE(uap->rval, 0));
5212790Ssam }
5312790Ssam 
5412790Ssam /*
5512790Ssam  * Release resources.
5612790Ssam  * Save u. area for parent to look at.
5712790Ssam  * Enter zombie state.
5812790Ssam  * Wake up parent and init processes,
5912790Ssam  * and dispose of children.
6012790Ssam  */
6112790Ssam exit(rv)
6238930Skarels 	int rv;
6312790Ssam {
6412790Ssam 	register int i;
6516527Skarels 	register struct proc *p, *q, *nq;
6640670Skarels 	register struct proc **pp;
6712790Ssam 
6812790Ssam #ifdef PGINPROF
6912790Ssam 	vmsizmon();
7012790Ssam #endif
7112790Ssam 	p = u.u_procp;
7232018Smckusick 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
7332018Smckusick 		M_ZOMBIE, M_WAITOK);
7412790Ssam 	p->p_flag &= ~(STRC|SULOCK);
7512790Ssam 	p->p_flag |= SWEXIT;
7612882Ssam 	p->p_sigignore = ~0;
7739410Skarels 	p->p_sig = 0;
7812790Ssam 	p->p_cpticks = 0;
7912790Ssam 	p->p_pctcpu = 0;
8012882Ssam 	for (i = 0; i < NSIG; i++)
8112790Ssam 		u.u_signal[i] = SIG_IGN;
8212790Ssam 	untimeout(realitexpire, (caddr_t)p);
8312790Ssam 	/*
8412790Ssam 	 * Release virtual memory.  If we resulted from
8512790Ssam 	 * a vfork(), instead give the resources back to
8612790Ssam 	 * the parent.
8712790Ssam 	 */
8841569Smckusick 	if ((p->p_flag & SVFORK) == 0) {
8941569Smckusick #ifdef MAPMEM
9041569Smckusick 		if (u.u_mmap)
9141569Smckusick 			mmexit();
9241569Smckusick #endif
9312790Ssam 		vrelvm();
9441569Smckusick 	} else {
9512790Ssam 		p->p_flag &= ~SVFORK;
9612790Ssam 		wakeup((caddr_t)p);
9712790Ssam 		while ((p->p_flag & SVFDONE) == 0)
9812790Ssam 			sleep((caddr_t)p, PZERO - 1);
9912790Ssam 		p->p_flag &= ~SVFDONE;
10012790Ssam 	}
10121105Skarels 	for (i = 0; i <= u.u_lastfile; i++) {
10212790Ssam 		struct file *f;
10312790Ssam 
10412790Ssam 		f = u.u_ofile[i];
10521105Skarels 		if (f) {
10621105Skarels 			u.u_ofile[i] = NULL;
10721105Skarels 			u.u_pofile[i] = 0;
10839351Smckusick 			(void) closef(f);
10921105Skarels 		}
11012790Ssam 	}
11137328Skarels 	if (SESS_LEADER(p)) {
11242897Smarc 		register struct session *sp = p->p_session;
11342897Smarc 
11442897Smarc 		if (sp->s_ttyvp) {
11540809Smarc 			/*
11642897Smarc 			 * Controlling process.
11742897Smarc 			 * Signal foreground pgrp and revoke access
11842897Smarc 			 * to controlling terminal.
11942897Smarc 			 */
12042897Smarc 			if (sp->s_ttyp->t_pgrp)
121*42916Smarc 				pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
12242897Smarc 			vgoneall(sp->s_ttyvp);
12342897Smarc 			vrele(sp->s_ttyvp);
12442897Smarc 			sp->s_ttyvp = NULL;
12542897Smarc 			/*
12640809Smarc 			 * s_ttyp is not zero'd; we use this to indicate
12740809Smarc 			 * that the session once had a controlling terminal.
12842897Smarc 			 * (for logging and informational purposes)
12940809Smarc 			 */
13037328Skarels 		}
13142897Smarc 		sp->s_leader = 0;
13237328Skarels 	}
13337728Smckusick 	VOP_LOCK(u.u_cdir);
13437728Smckusick 	vput(u.u_cdir);
13512790Ssam 	if (u.u_rdir) {
13637728Smckusick 		VOP_LOCK(u.u_rdir);
13737728Smckusick 		vput(u.u_rdir);
13812790Ssam 	}
13912790Ssam 	u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
14012790Ssam 	acct();
14137728Smckusick 	crfree(u.u_cred);
14237328Skarels #ifdef KTRACE
14337328Skarels 	/*
14437328Skarels 	 * release trace file
14537328Skarels 	 */
14637328Skarels 	if (p->p_tracep)
14737728Smckusick 		vrele(p->p_tracep);
14837328Skarels #endif
14926823Skarels 	/*
15026823Skarels 	 * Freeing the user structure and kernel stack
15126823Skarels 	 * for the current process: have to run a bit longer
15226823Skarels 	 * using the pages which are about to be freed...
15326823Skarels 	 * vrelu will block memory allocation by raising ipl.
15426823Skarels 	 */
15526823Skarels 	vrelu(u.u_procp, 0);
15612790Ssam 	vrelpt(u.u_procp);
15716527Skarels 	if (*p->p_prev = p->p_nxt)		/* off allproc queue */
15816527Skarels 		p->p_nxt->p_prev = p->p_prev;
15916527Skarels 	if (p->p_nxt = zombproc)		/* onto zombproc */
16016527Skarels 		p->p_nxt->p_prev = &p->p_nxt;
16116527Skarels 	p->p_prev = &zombproc;
16216527Skarels 	zombproc = p;
16312790Ssam 	multprog--;
16412790Ssam 	p->p_stat = SZOMB;
16512790Ssam 	noproc = 1;
16640670Skarels 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
16740670Skarels 		if (*pp == p) {
16840670Skarels 			*pp = p->p_hash;
16940670Skarels 			goto done;
17040670Skarels 		}
17140670Skarels 	panic("exit");
17240670Skarels done:
17321105Skarels 	if (p->p_pid == 1) {
17421105Skarels 		if (p->p_dsize == 0) {
17539410Skarels 			printf("Can't exec init (errno %d)\n", WEXITSTATUS(rv));
17621105Skarels 			for (;;)
17721105Skarels 				;
17821105Skarels 		} else
17921105Skarels 			panic("init died");
18021105Skarels 	}
18112790Ssam 	p->p_xstat = rv;
18221105Skarels 	*p->p_ru = u.u_ru;
18340809Smarc 	i = splclock();
18440809Smarc 	p->p_ru->ru_stime = p->p_stime;
18540809Smarc 	p->p_ru->ru_utime = p->p_utime;
18640809Smarc 	splx(i);
18721105Skarels 	ruadd(p->p_ru, &u.u_cru);
18816527Skarels 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
18916527Skarels 		wakeup((caddr_t)&proc[1]);
19042897Smarc 	fixjobc(p, 0);
19116527Skarels 	for (q = p->p_cptr; q != NULL; q = nq) {
19216527Skarels 		nq = q->p_osptr;
19316527Skarels 		if (nq != NULL)
19416527Skarels 			nq->p_ysptr = NULL;
19516527Skarels 		if (proc[1].p_cptr)
19616527Skarels 			proc[1].p_cptr->p_ysptr = q;
19716527Skarels 		q->p_osptr = proc[1].p_cptr;
19816527Skarels 		q->p_ysptr = NULL;
19916527Skarels 		proc[1].p_cptr = q;
20012790Ssam 
20116527Skarels 		q->p_pptr = &proc[1];
20216527Skarels 		q->p_ppid = 1;
20316527Skarels 		/*
20416527Skarels 		 * Traced processes are killed
20516527Skarels 		 * since their existence means someone is screwing up.
20616527Skarels 		 */
20716527Skarels 		if (q->p_flag&STRC) {
20816527Skarels 			q->p_flag &= ~STRC;
20916527Skarels 			psignal(q, SIGKILL);
21012790Ssam 		}
21116527Skarels 	}
21217540Skarels 	p->p_cptr = NULL;
21312790Ssam 	psignal(p->p_pptr, SIGCHLD);
21412790Ssam 	wakeup((caddr_t)p->p_pptr);
21529946Skarels #if defined(tahoe)
21629946Skarels 	dkeyrelease(p->p_dkey), p->p_dkey = 0;
21729946Skarels 	ckeyrelease(p->p_ckey), p->p_ckey = 0;
21829946Skarels 	u.u_pcb.pcb_savacc.faddr = (float *)NULL;
21929946Skarels #endif
22012790Ssam 	swtch();
22112790Ssam }
22212790Ssam 
22337328Skarels #ifdef COMPAT_43
22437328Skarels owait()
22512790Ssam {
22639410Skarels 	register struct a {
22737328Skarels 		int	pid;
22839410Skarels 		int	*status;
22937328Skarels 		int	options;
23037328Skarels 		struct	rusage *rusage;
23139410Skarels 		int	compat;
23237328Skarels 	} *uap = (struct a *)u.u_ap;
23312790Ssam 
23412790Ssam 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
23538930Skarels 		uap->options = 0;
23637328Skarels 		uap->rusage = 0;
23737328Skarels 	} else {
23838930Skarels 		uap->options = u.u_ar0[R0];
23937328Skarels 		uap->rusage = (struct rusage *)u.u_ar0[R1];
24012790Ssam 	}
24137328Skarels 	uap->pid = WAIT_ANY;
24237328Skarels 	uap->status = 0;
24339410Skarels 	uap->compat = 1;
24439410Skarels 	u.u_error = wait1();
24512790Ssam }
24612790Ssam 
24737328Skarels wait4()
24837328Skarels {
24939410Skarels 	register struct a {
25039410Skarels 		int	pid;
25139410Skarels 		int	*status;
25239410Skarels 		int	options;
25339410Skarels 		struct	rusage *rusage;
25439410Skarels 		int	compat;
25539410Skarels 	} *uap = (struct a *)u.u_ap;
25639410Skarels 
25739410Skarels 	uap->compat = 0;
25839410Skarels 	u.u_error = wait1();
25937328Skarels }
26039410Skarels #else
26139410Skarels #define	wait1	wait4
26237328Skarels #endif
26337328Skarels 
26412790Ssam /*
26512790Ssam  * Wait system call.
26612790Ssam  * Search for a terminated (zombie) child,
26712790Ssam  * finally lay it to rest, and collect its status.
26812790Ssam  * Look also for stopped (traced) children,
26912790Ssam  * and pass back status from them.
27012790Ssam  */
27139410Skarels wait1()
27212790Ssam {
27337328Skarels 	register struct a {
27437328Skarels 		int	pid;
27539410Skarels 		int	*status;
27637328Skarels 		int	options;
27737328Skarels 		struct	rusage *rusage;
27839410Skarels #ifdef COMPAT_43
27939410Skarels 		int compat;
28039410Skarels #endif
28137328Skarels 	} *uap = (struct a *)u.u_ap;
28212790Ssam 	register f;
28312790Ssam 	register struct proc *p, *q;
28439410Skarels 	int status, error;
28512790Ssam 
28637328Skarels 	q = u.u_procp;
28737328Skarels 	if (uap->pid == 0)
28837328Skarels 		uap->pid = -q->p_pgid;
28938930Skarels #ifdef notyet
29039410Skarels 	if (uap->options &~ (WUNTRACED|WNOHANG))
29139410Skarels 		return (EINVAL);
29238930Skarels #endif
29312790Ssam loop:
29438930Skarels 	f = 0;
29516527Skarels 	for (p = q->p_cptr; p; p = p->p_osptr) {
29637328Skarels 		if (uap->pid != WAIT_ANY &&
29737328Skarels 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
29837328Skarels 			continue;
29912790Ssam 		f++;
30012790Ssam 		if (p->p_stat == SZOMB) {
30112790Ssam 			u.u_r.r_val1 = p->p_pid;
30237328Skarels #ifdef COMPAT_43
30339410Skarels 			if (uap->compat)
30437328Skarels 				u.u_r.r_val2 = p->p_xstat;
30537328Skarels 			else
30637328Skarels #endif
30737328Skarels 			if (uap->status) {
30839410Skarels 				status = p->p_xstat;	/* convert to int */
30939410Skarels 				if (error = copyout((caddr_t)&status,
31037328Skarels 				    (caddr_t)uap->status, sizeof(status)))
31139410Skarels 					return (error);
31237328Skarels 			}
31339410Skarels 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
31439410Skarels 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
31539410Skarels 				return (error);
31639410Skarels 			pgrm(p);			/* off pgrp */
31712790Ssam 			p->p_xstat = 0;
31837328Skarels 			ruadd(&u.u_cru, p->p_ru);
31937328Skarels 			FREE(p->p_ru, M_ZOMBIE);
32037328Skarels 			p->p_ru = 0;
32112790Ssam 			p->p_stat = NULL;
32212790Ssam 			p->p_pid = 0;
32312790Ssam 			p->p_ppid = 0;
32416527Skarels 			if (*p->p_prev = p->p_nxt)	/* off zombproc */
32516527Skarels 				p->p_nxt->p_prev = p->p_prev;
32616527Skarels 			p->p_nxt = freeproc;		/* onto freeproc */
32716527Skarels 			freeproc = p;
32812790Ssam 			if (q = p->p_ysptr)
32912790Ssam 				q->p_osptr = p->p_osptr;
33012790Ssam 			if (q = p->p_osptr)
33112790Ssam 				q->p_ysptr = p->p_ysptr;
33212790Ssam 			if ((q = p->p_pptr)->p_cptr == p)
33312790Ssam 				q->p_cptr = p->p_osptr;
33412790Ssam 			p->p_pptr = 0;
33512790Ssam 			p->p_ysptr = 0;
33612790Ssam 			p->p_osptr = 0;
33712790Ssam 			p->p_cptr = 0;
33812790Ssam 			p->p_sig = 0;
33912882Ssam 			p->p_sigcatch = 0;
34012882Ssam 			p->p_sigignore = 0;
34112882Ssam 			p->p_sigmask = 0;
34237328Skarels 			/*p->p_pgrp = 0;*/
34312790Ssam 			p->p_flag = 0;
34412790Ssam 			p->p_wchan = 0;
34512790Ssam 			p->p_cursig = 0;
34639410Skarels 			return (0);
34712790Ssam 		}
34837328Skarels 		if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
34937328Skarels 		    (p->p_flag & STRC || uap->options & WUNTRACED)) {
35012790Ssam 			p->p_flag |= SWTED;
35112790Ssam 			u.u_r.r_val1 = p->p_pid;
35237328Skarels #ifdef COMPAT_43
35339737Smckusick 			if (uap->compat) {
35439410Skarels 				u.u_r.r_val2 = W_STOPCODE(p->p_cursig);
35539737Smckusick 				error = 0;
35639737Smckusick 			} else
35737328Skarels #endif
35837328Skarels 			if (uap->status) {
35939410Skarels 				status = W_STOPCODE(p->p_cursig);
36039410Skarels 				error = copyout((caddr_t)&status,
36137328Skarels 				    (caddr_t)uap->status, sizeof(status));
36239410Skarels 			} else
36339410Skarels 				error = 0;
36439410Skarels 			return (error);
36512790Ssam 		}
36612790Ssam 	}
36739410Skarels 	if (f == 0)
36839410Skarels 		return (ECHILD);
36937328Skarels 	if (uap->options & WNOHANG) {
37012790Ssam 		u.u_r.r_val1 = 0;
37139410Skarels 		return (0);
37212790Ssam 	}
37340708Skarels 	if (error = tsleep((caddr_t)u.u_procp, PWAIT | PCATCH, "wait", 0))
37440708Skarels 		return (error);
37512790Ssam 	goto loop;
37612790Ssam }
377