xref: /csrg-svn/sys/kern/kern_exit.c (revision 13102)
1*13102Ssam /*	kern_exit.c	4.4	83/06/14	*/
212790Ssam 
312790Ssam #include "../machine/reg.h"
412790Ssam #include "../machine/psl.h"
512790Ssam 
612790Ssam #include "../h/param.h"
712790Ssam #include "../h/systm.h"
812790Ssam #include "../h/map.h"
912790Ssam #include "../h/dir.h"
1012790Ssam #include "../h/user.h"
1112790Ssam #include "../h/kernel.h"
1212790Ssam #include "../h/proc.h"
1312790Ssam #include "../h/buf.h"
1412790Ssam #include "../h/wait.h"
1512790Ssam #include "../h/vm.h"
1612790Ssam #include "../h/file.h"
1712790Ssam #include "../h/mbuf.h"
1812790Ssam #include "../h/inode.h"
1912790Ssam 
2012790Ssam /*
2112790Ssam  * Exit system call: pass back caller's arg
2212790Ssam  */
2312790Ssam rexit()
2412790Ssam {
2512790Ssam 	register struct a {
2612790Ssam 		int	rval;
2712790Ssam 	} *uap;
2812790Ssam 
2912790Ssam 	uap = (struct a *)u.u_ap;
3012790Ssam 	exit((uap->rval & 0377) << 8);
3112790Ssam }
3212790Ssam 
3312790Ssam /*
3412790Ssam  * Release resources.
3512790Ssam  * Save u. area for parent to look at.
3612790Ssam  * Enter zombie state.
3712790Ssam  * Wake up parent and init processes,
3812790Ssam  * and dispose of children.
3912790Ssam  */
4012790Ssam exit(rv)
4112790Ssam 	int rv;
4212790Ssam {
4312790Ssam 	register int i;
4412790Ssam 	register struct proc *p, *q;
4512790Ssam 	register int x;
4612790Ssam 	struct mbuf *m = m_getclr(M_WAIT, MT_ZOMBIE);
4712790Ssam 
4812790Ssam #ifdef PGINPROF
4912790Ssam 	vmsizmon();
5012790Ssam #endif
5112790Ssam 	p = u.u_procp;
5212790Ssam 	p->p_flag &= ~(STRC|SULOCK);
5312790Ssam 	p->p_flag |= SWEXIT;
5412882Ssam 	p->p_sigignore = ~0;
5512790Ssam 	p->p_cpticks = 0;
5612790Ssam 	p->p_pctcpu = 0;
5712882Ssam 	for (i = 0; i < NSIG; i++)
5812790Ssam 		u.u_signal[i] = SIG_IGN;
5912790Ssam 	untimeout(realitexpire, (caddr_t)p);
6012790Ssam 	/*
6112790Ssam 	 * Release virtual memory.  If we resulted from
6212790Ssam 	 * a vfork(), instead give the resources back to
6312790Ssam 	 * the parent.
6412790Ssam 	 */
6512790Ssam 	if ((p->p_flag & SVFORK) == 0)
6612790Ssam 		vrelvm();
6712790Ssam 	else {
6812790Ssam 		p->p_flag &= ~SVFORK;
6912790Ssam 		wakeup((caddr_t)p);
7012790Ssam 		while ((p->p_flag & SVFDONE) == 0)
7112790Ssam 			sleep((caddr_t)p, PZERO - 1);
7212790Ssam 		p->p_flag &= ~SVFDONE;
7312790Ssam 	}
7412790Ssam 	for (i = 0; i < NOFILE; i++) {
7512790Ssam 		struct file *f;
7612790Ssam 		int po;
7712790Ssam 
7812790Ssam 		f = u.u_ofile[i];
7912790Ssam 		u.u_ofile[i] = NULL;
8012790Ssam 		po = u.u_pofile[i];
8112790Ssam 		u.u_pofile[i] = 0;
82*13102Ssam 		closef(f);
8312790Ssam 	}
8412790Ssam 	ilock(u.u_cdir);
8512790Ssam 	iput(u.u_cdir);
8612790Ssam 	if (u.u_rdir) {
8712790Ssam 		ilock(u.u_rdir);
8812790Ssam 		iput(u.u_rdir);
8912790Ssam 	}
9012790Ssam 	u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
9112790Ssam 	acct();
9212790Ssam #ifdef QUOTA
9312790Ssam 	qclean();
9412790Ssam #endif
9512790Ssam #ifdef sun
9612790Ssam 	ctxfree(u.u_procp);
9712790Ssam #endif
9812790Ssam 	vrelpt(u.u_procp);
9912790Ssam 	vrelu(u.u_procp, 0);
10012790Ssam 	(void) spl5();		/* hack for mem alloc race XXX */
10112790Ssam 	multprog--;
10212790Ssam 	p->p_stat = SZOMB;
10312790Ssam 	noproc = 1;
10412790Ssam 	i = PIDHASH(p->p_pid);
10512790Ssam 	x = p - proc;
10612790Ssam 	if (pidhash[i] == x)
10712790Ssam 		pidhash[i] = p->p_idhash;
10812790Ssam 	else {
10912790Ssam 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
11012790Ssam 			if (proc[i].p_idhash == x) {
11112790Ssam 				proc[i].p_idhash = p->p_idhash;
11212790Ssam 				goto done;
11312790Ssam 			}
11412790Ssam 		panic("exit");
11512790Ssam 	}
11612790Ssam 	if (p->p_pid == 1)
11712790Ssam 		panic("init died");
11812790Ssam done:
11912790Ssam 	p->p_xstat = rv;
12012790Ssam if (m == 0)
12112790Ssam panic("exit: m_getclr");
12212790Ssam 	p->p_ru = mtod(m, struct rusage *);
12312790Ssam 	*p->p_ru = u.u_ru;
12412790Ssam 	ruadd(p->p_ru, &u.u_cru);
12512790Ssam 	for (q = proc; q < procNPROC; q++)
12612790Ssam 		if (q->p_pptr == p) {
12712790Ssam 			if (q->p_osptr)
12812790Ssam 				q->p_osptr->p_ysptr = q->p_ysptr;
12912790Ssam 			if (q->p_ysptr)
13012790Ssam 				q->p_ysptr->p_osptr = q->p_osptr;
13112790Ssam 			if (proc[1].p_cptr)
13212790Ssam 				proc[1].p_cptr->p_ysptr = q;
13312790Ssam 			q->p_osptr = proc[1].p_cptr;
13412790Ssam 			q->p_ysptr = NULL;
13512790Ssam 			proc[1].p_cptr = q;
13612790Ssam 
13712790Ssam 			q->p_pptr = &proc[1];
13812790Ssam 			q->p_ppid = 1;
13912790Ssam 			wakeup((caddr_t)&proc[1]);
14012790Ssam 			/*
14112790Ssam 			 * Traced processes are killed
14212790Ssam 			 * since their existence means someone is screwing up.
14312790Ssam 			 * Stopped processes are sent a hangup and a continue.
14412790Ssam 			 * This is designed to be ``safe'' for setuid
14512790Ssam 			 * processes since they must be willing to tolerate
14612790Ssam 			 * hangups anyways.
14712790Ssam 			 */
14812790Ssam 			if (q->p_flag&STRC) {
14912790Ssam 				q->p_flag &= ~STRC;
15012790Ssam 				psignal(q, SIGKILL);
15112790Ssam 			} else if (q->p_stat == SSTOP) {
15212790Ssam 				psignal(q, SIGHUP);
15312790Ssam 				psignal(q, SIGCONT);
15412790Ssam 			}
15512790Ssam 			/*
15612790Ssam 			 * Protect this process from future
15712790Ssam 			 * tty signals, clear TSTP/TTIN/TTOU if pending.
15812790Ssam 			 */
15912790Ssam 			(void) spgrp(q, -1);
16012790Ssam 		}
16112790Ssam 	psignal(p->p_pptr, SIGCHLD);
16212790Ssam 	wakeup((caddr_t)p->p_pptr);
16312790Ssam 	swtch();
16412790Ssam }
16512790Ssam 
16612790Ssam wait()
16712790Ssam {
16812790Ssam 	struct rusage ru, *rup;
16912790Ssam 
17012790Ssam 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
17112790Ssam 		u.u_error = wait1(0, (struct rusage *)0);
17212790Ssam 		return;
17312790Ssam 	}
17412790Ssam 	rup = (struct rusage *)u.u_ar0[R1];
17512790Ssam 	u.u_error = wait1(u.u_ar0[R0], &ru);
17612790Ssam 	if (u.u_error)
17712790Ssam 		return;
17812790Ssam 	(void) copyout((caddr_t)&ru, (caddr_t)rup, sizeof (struct rusage));
17912790Ssam }
18012790Ssam 
18112790Ssam /*
18212790Ssam  * Wait system call.
18312790Ssam  * Search for a terminated (zombie) child,
18412790Ssam  * finally lay it to rest, and collect its status.
18512790Ssam  * Look also for stopped (traced) children,
18612790Ssam  * and pass back status from them.
18712790Ssam  */
18812790Ssam wait1(options, ru)
18912790Ssam 	register int options;
19012790Ssam 	struct rusage *ru;
19112790Ssam {
19212790Ssam 	register f;
19312790Ssam 	register struct proc *p, *q;
19412790Ssam 
19512790Ssam 	f = 0;
19612790Ssam loop:
19712790Ssam 	for (p = proc; p < procNPROC; p++)
19812790Ssam 	if (p->p_pptr == u.u_procp) {
19912790Ssam 		f++;
20012790Ssam 		if (p->p_stat == SZOMB) {
20112790Ssam 			u.u_r.r_val1 = p->p_pid;
20212790Ssam 			u.u_r.r_val2 = p->p_xstat;
20312790Ssam 			p->p_xstat = 0;
20412790Ssam 			if (ru)
20512790Ssam 				*ru = *p->p_ru;
20612790Ssam 			ruadd(&u.u_cru, p->p_ru);
20712790Ssam 			(void) m_free(dtom(p->p_ru));
20812790Ssam 			p->p_ru = 0;
20912790Ssam 			p->p_stat = NULL;
21012790Ssam 			p->p_pid = 0;
21112790Ssam 			p->p_ppid = 0;
21212790Ssam 			if (q = p->p_ysptr)
21312790Ssam 				q->p_osptr = p->p_osptr;
21412790Ssam 			if (q = p->p_osptr)
21512790Ssam 				q->p_ysptr = p->p_ysptr;
21612790Ssam 			if ((q = p->p_pptr)->p_cptr == p)
21712790Ssam 				q->p_cptr = p->p_osptr;
21812790Ssam 			p->p_pptr = 0;
21912790Ssam 			p->p_ysptr = 0;
22012790Ssam 			p->p_osptr = 0;
22112790Ssam 			p->p_cptr = 0;
22212790Ssam 			p->p_sig = 0;
22312882Ssam 			p->p_sigcatch = 0;
22412882Ssam 			p->p_sigignore = 0;
22512882Ssam 			p->p_sigmask = 0;
22612790Ssam 			p->p_pgrp = 0;
22712790Ssam 			p->p_flag = 0;
22812790Ssam 			p->p_wchan = 0;
22912790Ssam 			p->p_cursig = 0;
23012790Ssam 			return (0);
23112790Ssam 		}
23212790Ssam 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
23312790Ssam 		    (p->p_flag&STRC || options&WUNTRACED)) {
23412790Ssam 			p->p_flag |= SWTED;
23512790Ssam 			u.u_r.r_val1 = p->p_pid;
23612790Ssam 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
23712790Ssam 			return (0);
23812790Ssam 		}
23912790Ssam 	}
24012882Ssam 	if (f == 0)
24112790Ssam 		return (ECHILD);
24212790Ssam 	if (options&WNOHANG) {
24312790Ssam 		u.u_r.r_val1 = 0;
24412790Ssam 		return (0);
24512790Ssam 	}
24612882Ssam 	if ((u.u_procp->p_flag&SOUSIG) == 0 && setjmp(&u.u_qsave)) {
24712790Ssam 		u.u_eosys = RESTARTSYS;
24812790Ssam 		return (0);
24912790Ssam 	}
25012790Ssam 	sleep((caddr_t)u.u_procp, PWAIT);
25112790Ssam 	goto loop;
25212790Ssam }
253