xref: /csrg-svn/sys/kern/kern_exit.c (revision 12790)
1 /*	kern_exit.c	4.1	83/05/27	*/
2 
3 #include "../machine/reg.h"
4 #include "../machine/psl.h"
5 
6 #include "../h/param.h"
7 #include "../h/systm.h"
8 #include "../h/map.h"
9 #include "../h/dir.h"
10 #include "../h/user.h"
11 #include "../h/kernel.h"
12 #include "../h/proc.h"
13 #include "../h/buf.h"
14 #include "../h/wait.h"
15 #include "../h/vm.h"
16 #include "../h/file.h"
17 #include "../h/mbuf.h"
18 #include "../h/inode.h"
19 
20 /*
21  * Exit system call: pass back caller's arg
22  */
23 rexit()
24 {
25 	register struct a {
26 		int	rval;
27 	} *uap;
28 
29 	uap = (struct a *)u.u_ap;
30 	exit((uap->rval & 0377) << 8);
31 }
32 
33 /*
34  * Release resources.
35  * Save u. area for parent to look at.
36  * Enter zombie state.
37  * Wake up parent and init processes,
38  * and dispose of children.
39  */
40 exit(rv)
41 	int rv;
42 {
43 	register int i;
44 	register struct proc *p, *q;
45 	register int x;
46 	struct mbuf *m = m_getclr(M_WAIT, MT_ZOMBIE);
47 
48 #ifdef PGINPROF
49 	vmsizmon();
50 #endif
51 	p = u.u_procp;
52 	p->p_flag &= ~(STRC|SULOCK);
53 	p->p_flag |= SWEXIT;
54 	(void) spl6();
55 	/* we know SIG_IGN is 1 */
56 	p->p_siga0 = ~0;
57 	p->p_siga1 = 0;
58 	(void) spl0();
59 	p->p_cpticks = 0;
60 	p->p_pctcpu = 0;
61 	for (i=0; i<NSIG; i++)
62 		u.u_signal[i] = SIG_IGN;
63 	untimeout(realitexpire, (caddr_t)p);
64 	/*
65 	 * Release virtual memory.  If we resulted from
66 	 * a vfork(), instead give the resources back to
67 	 * the parent.
68 	 */
69 	if ((p->p_flag & SVFORK) == 0)
70 		vrelvm();
71 	else {
72 		p->p_flag &= ~SVFORK;
73 		wakeup((caddr_t)p);
74 		while ((p->p_flag & SVFDONE) == 0)
75 			sleep((caddr_t)p, PZERO - 1);
76 		p->p_flag &= ~SVFDONE;
77 	}
78 	for (i = 0; i < NOFILE; i++) {
79 		struct file *f;
80 		int po;
81 
82 		f = u.u_ofile[i];
83 		u.u_ofile[i] = NULL;
84 		po = u.u_pofile[i];
85 		u.u_pofile[i] = 0;
86 		closef(f, po);
87 	}
88 	ilock(u.u_cdir);
89 	iput(u.u_cdir);
90 	if (u.u_rdir) {
91 		ilock(u.u_rdir);
92 		iput(u.u_rdir);
93 	}
94 	u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
95 	acct();
96 #ifdef QUOTA
97 	qclean();
98 #endif
99 #ifdef sun
100 	ctxfree(u.u_procp);
101 #endif
102 	vrelpt(u.u_procp);
103 	vrelu(u.u_procp, 0);
104 	(void) spl5();		/* hack for mem alloc race XXX */
105 	multprog--;
106 	p->p_stat = SZOMB;
107 	noproc = 1;
108 	i = PIDHASH(p->p_pid);
109 	x = p - proc;
110 	if (pidhash[i] == x)
111 		pidhash[i] = p->p_idhash;
112 	else {
113 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
114 			if (proc[i].p_idhash == x) {
115 				proc[i].p_idhash = p->p_idhash;
116 				goto done;
117 			}
118 		panic("exit");
119 	}
120 	if (p->p_pid == 1)
121 		panic("init died");
122 done:
123 	p->p_xstat = rv;
124 if (m == 0)
125 panic("exit: m_getclr");
126 	p->p_ru = mtod(m, struct rusage *);
127 	*p->p_ru = u.u_ru;
128 	ruadd(p->p_ru, &u.u_cru);
129 	for (q = proc; q < procNPROC; q++)
130 		if (q->p_pptr == p) {
131 			if (q->p_osptr)
132 				q->p_osptr->p_ysptr = q->p_ysptr;
133 			if (q->p_ysptr)
134 				q->p_ysptr->p_osptr = q->p_osptr;
135 			if (proc[1].p_cptr)
136 				proc[1].p_cptr->p_ysptr = q;
137 			q->p_osptr = proc[1].p_cptr;
138 			q->p_ysptr = NULL;
139 			proc[1].p_cptr = q;
140 
141 			q->p_pptr = &proc[1];
142 			q->p_ppid = 1;
143 			wakeup((caddr_t)&proc[1]);
144 			/*
145 			 * Traced processes are killed
146 			 * since their existence means someone is screwing up.
147 			 * Stopped processes are sent a hangup and a continue.
148 			 * This is designed to be ``safe'' for setuid
149 			 * processes since they must be willing to tolerate
150 			 * hangups anyways.
151 			 */
152 			if (q->p_flag&STRC) {
153 				q->p_flag &= ~STRC;
154 				psignal(q, SIGKILL);
155 			} else if (q->p_stat == SSTOP) {
156 				psignal(q, SIGHUP);
157 				psignal(q, SIGCONT);
158 			}
159 			/*
160 			 * Protect this process from future
161 			 * tty signals, clear TSTP/TTIN/TTOU if pending.
162 			 */
163 			(void) spgrp(q, -1);
164 		}
165 	psignal(p->p_pptr, SIGCHLD);
166 	wakeup((caddr_t)p->p_pptr);
167 	swtch();
168 }
169 
170 wait()
171 {
172 	struct rusage ru, *rup;
173 
174 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
175 		u.u_error = wait1(0, (struct rusage *)0);
176 		return;
177 	}
178 	rup = (struct rusage *)u.u_ar0[R1];
179 	u.u_error = wait1(u.u_ar0[R0], &ru);
180 	if (u.u_error)
181 		return;
182 	(void) copyout((caddr_t)&ru, (caddr_t)rup, sizeof (struct rusage));
183 }
184 
185 #ifndef NOCOMPAT
186 #include "../h/vtimes.h"
187 
188 owait()
189 {
190 	struct rusage ru;
191 	struct vtimes *vtp, avt;
192 
193 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
194 		u.u_error = wait1(0, (struct rusage *)0);
195 		return;
196 	}
197 	vtp = (struct vtimes *)u.u_ar0[R1];
198 	u.u_error = wait1(u.u_ar0[R0], &ru);
199 	if (u.u_error)
200 		return;
201 	getvtimes(&ru, &avt);
202 	(void) copyout((caddr_t)&avt, (caddr_t)vtp, sizeof (struct vtimes));
203 }
204 #endif
205 
206 /*
207  * Wait system call.
208  * Search for a terminated (zombie) child,
209  * finally lay it to rest, and collect its status.
210  * Look also for stopped (traced) children,
211  * and pass back status from them.
212  */
213 wait1(options, ru)
214 	register int options;
215 	struct rusage *ru;
216 {
217 	register f;
218 	register struct proc *p, *q;
219 
220 	f = 0;
221 loop:
222 	for (p = proc; p < procNPROC; p++)
223 	if (p->p_pptr == u.u_procp) {
224 		f++;
225 		if (p->p_stat == SZOMB) {
226 			u.u_r.r_val1 = p->p_pid;
227 			u.u_r.r_val2 = p->p_xstat;
228 			p->p_xstat = 0;
229 			if (ru)
230 				*ru = *p->p_ru;
231 			ruadd(&u.u_cru, p->p_ru);
232 			(void) m_free(dtom(p->p_ru));
233 			p->p_ru = 0;
234 			p->p_stat = NULL;
235 			p->p_pid = 0;
236 			p->p_ppid = 0;
237 			if (q = p->p_ysptr)
238 				q->p_osptr = p->p_osptr;
239 			if (q = p->p_osptr)
240 				q->p_ysptr = p->p_ysptr;
241 			if ((q = p->p_pptr)->p_cptr == p)
242 				q->p_cptr = p->p_osptr;
243 			p->p_pptr = 0;
244 			p->p_ysptr = 0;
245 			p->p_osptr = 0;
246 			p->p_cptr = 0;
247 			p->p_sig = 0;
248 			p->p_siga0 = 0;
249 			p->p_siga1 = 0;
250 			p->p_pgrp = 0;
251 			p->p_flag = 0;
252 			p->p_wchan = 0;
253 			p->p_cursig = 0;
254 			return (0);
255 		}
256 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
257 		    (p->p_flag&STRC || options&WUNTRACED)) {
258 			p->p_flag |= SWTED;
259 			u.u_r.r_val1 = p->p_pid;
260 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
261 			return (0);
262 		}
263 	}
264 	if (f == 0) {
265 		return (ECHILD);
266 	}
267 	if (options&WNOHANG) {
268 		u.u_r.r_val1 = 0;
269 		return (0);
270 	}
271 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) {
272 		u.u_eosys = RESTARTSYS;
273 		return (0);
274 	}
275 	sleep((caddr_t)u.u_procp, PWAIT);
276 	goto loop;
277 }
278