xref: /original-bsd/sys/kern/kern_exit.c (revision 333da485)
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  * %sccs.include.redist.c%
11  *
12  *	@(#)kern_exit.c	8.6 (Berkeley) 01/21/94
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/map.h>
18 #include <sys/ioctl.h>
19 #include <sys/proc.h>
20 #include <sys/tty.h>
21 #include <sys/time.h>
22 #include <sys/resource.h>
23 #include <sys/kernel.h>
24 #include <sys/buf.h>
25 #include <sys/wait.h>
26 #include <sys/file.h>
27 #include <sys/vnode.h>
28 #include <sys/syslog.h>
29 #include <sys/malloc.h>
30 #include <sys/resourcevar.h>
31 #include <sys/ptrace.h>
32 
33 #include <machine/cpu.h>
34 #ifdef COMPAT_43
35 #include <machine/reg.h>
36 #include <machine/psl.h>
37 #endif
38 
39 #include <vm/vm.h>
40 #include <vm/vm_kern.h>
41 
42 __dead void cpu_exit __P((struct proc *));
43 __dead void exit1 __P((struct proc *, int));
44 
45 /*
46  * exit --
47  *	Death of process.
48  */
49 struct rexit_args {
50 	int	rval;
51 };
52 __dead void
53 exit(p, uap, retval)
54 	struct proc *p;
55 	struct rexit_args *uap;
56 	int *retval;
57 {
58 
59 	exit1(p, W_EXITCODE(uap->rval, 0));
60 	/* NOTREACHED */
61 }
62 
63 /*
64  * Exit: deallocate address space and other resources, change proc state
65  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
66  * status and rusage for wait().  Check for child processes and orphan them.
67  */
68 __dead void
69 exit1(p, rv)
70 	register struct proc *p;
71 	int rv;
72 {
73 	register struct proc *q, *nq;
74 	register struct proc **pp;
75 	register struct vmspace *vm;
76 
77 	if (p->p_pid == 1)
78 		panic("init died (signal %d, exit %d)",
79 		    WTERMSIG(rv), WEXITSTATUS(rv));
80 #ifdef PGINPROF
81 	vmsizmon();
82 #endif
83 	if (p->p_flag & P_PROFIL)
84 		stopprofclock(p);
85 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
86 		M_ZOMBIE, M_WAITOK);
87 	/*
88 	 * If parent is waiting for us to exit or exec,
89 	 * P_PPWAIT is set; we will wakeup the parent below.
90 	 */
91 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
92 	p->p_flag |= P_WEXIT;
93 	p->p_sigignore = ~0;
94 	p->p_siglist = 0;
95 	untimeout(realitexpire, (caddr_t)p);
96 
97 	/*
98 	 * Close open files and release open-file table.
99 	 * This may block!
100 	 */
101 	fdfree(p);
102 
103 	/* The next two chunks should probably be moved to vmspace_exit. */
104 	vm = p->p_vmspace;
105 #ifdef SYSVSHM
106 	if (vm->vm_shm)
107 		shmexit(p);
108 #endif
109 	/*
110 	 * Release user portion of address space.
111 	 * This releases references to vnodes,
112 	 * which could cause I/O if the file has been unlinked.
113 	 * Need to do this early enough that we can still sleep.
114 	 * Can't free the entire vmspace as the kernel stack
115 	 * may be mapped within that space also.
116 	 */
117 	if (vm->vm_refcnt == 1)
118 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
119 		    VM_MAXUSER_ADDRESS);
120 
121 	if (SESS_LEADER(p)) {
122 		register struct session *sp = p->p_session;
123 
124 		if (sp->s_ttyvp) {
125 			/*
126 			 * Controlling process.
127 			 * Signal foreground pgrp,
128 			 * drain controlling terminal
129 			 * and revoke access to controlling terminal.
130 			 */
131 			if (sp->s_ttyp->t_session == sp) {
132 				if (sp->s_ttyp->t_pgrp)
133 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
134 				(void) ttywait(sp->s_ttyp);
135 				vgoneall(sp->s_ttyvp);
136 			}
137 			vrele(sp->s_ttyvp);
138 			sp->s_ttyvp = NULL;
139 			/*
140 			 * s_ttyp is not zero'd; we use this to indicate
141 			 * that the session once had a controlling terminal.
142 			 * (for logging and informational purposes)
143 			 */
144 		}
145 		sp->s_leader = NULL;
146 	}
147 	fixjobc(p, p->p_pgrp, 0);
148 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
149 	(void)acct_process(p);
150 #ifdef KTRACE
151 	/*
152 	 * release trace file
153 	 */
154 	p->p_traceflag = 0;	/* don't trace the vrele() */
155 	if (p->p_tracep)
156 		vrele(p->p_tracep);
157 #endif
158 	/*
159 	 * Remove proc from allproc queue and pidhash chain.
160 	 * Place onto zombproc.  Unlink from parent's child list.
161 	 */
162 	if (*p->p_prev = p->p_next)
163 		p->p_next->p_prev = p->p_prev;
164 	if (p->p_next = zombproc)
165 		p->p_next->p_prev = &p->p_next;
166 	p->p_prev = &zombproc;
167 	zombproc = p;
168 	p->p_stat = SZOMB;
169 
170 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
171 		if (*pp == p) {
172 			*pp = p->p_hash;
173 			goto done;
174 		}
175 	panic("exit");
176 done:
177 
178 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
179 		wakeup((caddr_t) initproc);
180 	for (q = p->p_cptr; q != NULL; q = nq) {
181 		nq = q->p_osptr;
182 		if (nq != NULL)
183 			nq->p_ysptr = NULL;
184 		if (initproc->p_cptr)
185 			initproc->p_cptr->p_ysptr = q;
186 		q->p_osptr = initproc->p_cptr;
187 		q->p_ysptr = NULL;
188 		initproc->p_cptr = q;
189 
190 		q->p_pptr = initproc;
191 		/*
192 		 * Traced processes are killed
193 		 * since their existence means someone is screwing up.
194 		 */
195 		if (q->p_flag & P_TRACED) {
196 			q->p_flag &= ~P_TRACED;
197 			psignal(q, SIGKILL);
198 		}
199 	}
200 	p->p_cptr = NULL;
201 
202 	/*
203 	 * Save exit status and final rusage info, adding in child rusage
204 	 * info and self times.
205 	 */
206 	p->p_xstat = rv;
207 	*p->p_ru = p->p_stats->p_ru;
208 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
209 	ruadd(p->p_ru, &p->p_stats->p_cru);
210 
211 	/*
212 	 * Notify parent that we're gone.
213 	 */
214 	psignal(p->p_pptr, SIGCHLD);
215 	wakeup((caddr_t)p->p_pptr);
216 #if defined(tahoe)
217 	/* move this to cpu_exit */
218 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
219 #endif
220 	/*
221 	 * Clear curproc after we've done all operations
222 	 * that could block, and before tearing down the rest
223 	 * of the process state that might be used from clock, etc.
224 	 * Also, can't clear curproc while we're still runnable,
225 	 * as we're not on a run queue (we are current, just not
226 	 * a proper proc any longer!).
227 	 *
228 	 * Other substructures are freed from wait().
229 	 */
230 	curproc = NULL;
231 	if (--p->p_limit->p_refcnt == 0)
232 		FREE(p->p_limit, M_SUBPROC);
233 
234 	/*
235 	 * Finally, call machine-dependent code to release the remaining
236 	 * resources including address space, the kernel stack and pcb.
237 	 * The address space is released by "vmspace_free(p->p_vmspace)";
238 	 * This is machine-dependent, as we may have to change stacks
239 	 * or ensure that the current one isn't reallocated before we
240 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
241 	 * our execution (pun intended).
242 	 */
243 	cpu_exit(p);
244 }
245 
246 struct wait_args {
247 	int	pid;
248 	int	*status;
249 	int	options;
250 	struct	rusage *rusage;
251 #ifdef COMPAT_43
252 	int	compat;		/* pseudo */
253 #endif
254 };
255 
256 #ifdef COMPAT_43
257 #if defined(hp300) || defined(luna68k)
258 #include <machine/frame.h>
259 #define GETPS(rp)	((struct frame *)(rp))->f_sr
260 #else
261 #define GETPS(rp)	(rp)[PS]
262 #endif
263 
264 owait(p, uap, retval)
265 	struct proc *p;
266 	register struct wait_args *uap;
267 	int *retval;
268 {
269 
270 #ifdef PSL_ALLCC
271 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
272 		uap->options = 0;
273 		uap->rusage = NULL;
274 	} else {
275 		uap->options = p->p_md.md_regs[R0];
276 		uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
277 	}
278 #else
279 	uap->options = 0;
280 	uap->rusage = NULL;
281 #endif
282 	uap->pid = WAIT_ANY;
283 	uap->status = NULL;
284 	uap->compat = 1;
285 	return (wait1(p, uap, retval));
286 }
287 
288 wait4(p, uap, retval)
289 	struct proc *p;
290 	struct wait_args *uap;
291 	int *retval;
292 {
293 
294 	uap->compat = 0;
295 	return (wait1(p, uap, retval));
296 }
297 #else
298 #define	wait1	wait4
299 #endif
300 
301 int
302 wait1(q, uap, retval)
303 	register struct proc *q;
304 	register struct wait_args *uap;
305 	int retval[];
306 {
307 	register int nfound;
308 	register struct proc *p, *t;
309 	int status, error;
310 
311 	if (uap->pid == 0)
312 		uap->pid = -q->p_pgid;
313 #ifdef notyet
314 	if (uap->options &~ (WUNTRACED|WNOHANG))
315 		return (EINVAL);
316 #endif
317 loop:
318 	nfound = 0;
319 	for (p = q->p_cptr; p; p = p->p_osptr) {
320 		if (uap->pid != WAIT_ANY &&
321 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
322 			continue;
323 		nfound++;
324 		if (p->p_stat == SZOMB) {
325 			retval[0] = p->p_pid;
326 #ifdef COMPAT_43
327 			if (uap->compat)
328 				retval[1] = p->p_xstat;
329 			else
330 #endif
331 			if (uap->status) {
332 				status = p->p_xstat;	/* convert to int */
333 				if (error = copyout((caddr_t)&status,
334 				    (caddr_t)uap->status, sizeof(status)))
335 					return (error);
336 			}
337 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
338 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
339 				return (error);
340 			/*
341 			 * If we got the child via a ptrace 'attach',
342 			 * we need to give it back to the old parent.
343 			 */
344 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
345 				p->p_oppid = 0;
346 				proc_reparent(p, t);
347 				psignal(t, SIGCHLD);
348 				wakeup((caddr_t)t);
349 				return (0);
350 			}
351 			p->p_xstat = 0;
352 			ruadd(&q->p_stats->p_cru, p->p_ru);
353 			FREE(p->p_ru, M_ZOMBIE);
354 
355 			/*
356 			 * Decrement the count of procs running with this uid.
357 			 */
358 			(void)chgproccnt(p->p_cred->p_ruid, -1);
359 
360 			/*
361 			 * Free up credentials.
362 			 */
363 			if (--p->p_cred->p_refcnt == 0) {
364 				crfree(p->p_cred->pc_ucred);
365 				FREE(p->p_cred, M_SUBPROC);
366 			}
367 
368 			/*
369 			 * Release reference to text vnode
370 			 */
371 			if (p->p_textvp)
372 				vrele(p->p_textvp);
373 
374 			/*
375 			 * Finally finished with old proc entry.
376 			 * Unlink it from its process group and free it.
377 			 */
378 			leavepgrp(p);
379 			if (*p->p_prev = p->p_next)	/* off zombproc */
380 				p->p_next->p_prev = p->p_prev;
381 			if (q = p->p_ysptr)
382 				q->p_osptr = p->p_osptr;
383 			if (q = p->p_osptr)
384 				q->p_ysptr = p->p_ysptr;
385 			if ((q = p->p_pptr)->p_cptr == p)
386 				q->p_cptr = p->p_osptr;
387 
388 			/*
389 			 * Give machine-dependent layer a chance
390 			 * to free anything that cpu_exit couldn't
391 			 * release while still running in process context.
392 			 */
393 			cpu_wait(p);
394 			FREE(p, M_PROC);
395 			nprocs--;
396 			return (0);
397 		}
398 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
399 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
400 			p->p_flag |= P_WAITED;
401 			retval[0] = p->p_pid;
402 #ifdef COMPAT_43
403 			if (uap->compat) {
404 				retval[1] = W_STOPCODE(p->p_xstat);
405 				error = 0;
406 			} else
407 #endif
408 			if (uap->status) {
409 				status = W_STOPCODE(p->p_xstat);
410 				error = copyout((caddr_t)&status,
411 					(caddr_t)uap->status, sizeof(status));
412 			} else
413 				error = 0;
414 			return (error);
415 		}
416 	}
417 	if (nfound == 0)
418 		return (ECHILD);
419 	if (uap->options & WNOHANG) {
420 		retval[0] = 0;
421 		return (0);
422 	}
423 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
424 		return (error);
425 	goto loop;
426 }
427 
428 /*
429  * make process 'parent' the new parent of process 'child'.
430  */
431 void
432 proc_reparent(child, parent)
433 	register struct proc *child;
434 	register struct proc *parent;
435 {
436 	register struct proc *o;
437 	register struct proc *y;
438 
439 	if (child->p_pptr == parent)
440 		return;
441 
442 	/* fix up the child linkage for the old parent */
443 	o = child->p_osptr;
444 	y = child->p_ysptr;
445 	if (y)
446 		y->p_osptr = o;
447 	if (o)
448 		o->p_ysptr = y;
449 	if (child->p_pptr->p_cptr == child)
450 		child->p_pptr->p_cptr = o;
451 
452 	/* fix up child linkage for new parent */
453 	o = parent->p_cptr;
454 	if (o)
455 		o->p_ysptr = child;
456 	child->p_osptr = o;
457 	child->p_ysptr = NULL;
458 	parent->p_cptr = child;
459 	child->p_pptr = parent;
460 }
461