xref: /original-bsd/sys/kern/kern_exit.c (revision f737e041)
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.7 (Berkeley) 02/12/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 				/*
136 				 * The tty could have been revoked
137 				 * if we blocked.
138 				 */
139 				if (sp->s_ttyvp)
140 					vgoneall(sp->s_ttyvp);
141 			}
142 			if (sp->s_ttyvp)
143 				vrele(sp->s_ttyvp);
144 			sp->s_ttyvp = NULL;
145 			/*
146 			 * s_ttyp is not zero'd; we use this to indicate
147 			 * that the session once had a controlling terminal.
148 			 * (for logging and informational purposes)
149 			 */
150 		}
151 		sp->s_leader = NULL;
152 	}
153 	fixjobc(p, p->p_pgrp, 0);
154 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
155 	(void)acct_process(p);
156 #ifdef KTRACE
157 	/*
158 	 * release trace file
159 	 */
160 	p->p_traceflag = 0;	/* don't trace the vrele() */
161 	if (p->p_tracep)
162 		vrele(p->p_tracep);
163 #endif
164 	/*
165 	 * Remove proc from allproc queue and pidhash chain.
166 	 * Place onto zombproc.  Unlink from parent's child list.
167 	 */
168 	if (*p->p_prev = p->p_next)
169 		p->p_next->p_prev = p->p_prev;
170 	if (p->p_next = zombproc)
171 		p->p_next->p_prev = &p->p_next;
172 	p->p_prev = &zombproc;
173 	zombproc = p;
174 	p->p_stat = SZOMB;
175 
176 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
177 		if (*pp == p) {
178 			*pp = p->p_hash;
179 			goto done;
180 		}
181 	panic("exit");
182 done:
183 
184 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
185 		wakeup((caddr_t) initproc);
186 	for (q = p->p_cptr; q != NULL; q = nq) {
187 		nq = q->p_osptr;
188 		if (nq != NULL)
189 			nq->p_ysptr = NULL;
190 		if (initproc->p_cptr)
191 			initproc->p_cptr->p_ysptr = q;
192 		q->p_osptr = initproc->p_cptr;
193 		q->p_ysptr = NULL;
194 		initproc->p_cptr = q;
195 
196 		q->p_pptr = initproc;
197 		/*
198 		 * Traced processes are killed
199 		 * since their existence means someone is screwing up.
200 		 */
201 		if (q->p_flag & P_TRACED) {
202 			q->p_flag &= ~P_TRACED;
203 			psignal(q, SIGKILL);
204 		}
205 	}
206 	p->p_cptr = NULL;
207 
208 	/*
209 	 * Save exit status and final rusage info, adding in child rusage
210 	 * info and self times.
211 	 */
212 	p->p_xstat = rv;
213 	*p->p_ru = p->p_stats->p_ru;
214 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
215 	ruadd(p->p_ru, &p->p_stats->p_cru);
216 
217 	/*
218 	 * Notify parent that we're gone.
219 	 */
220 	psignal(p->p_pptr, SIGCHLD);
221 	wakeup((caddr_t)p->p_pptr);
222 #if defined(tahoe)
223 	/* move this to cpu_exit */
224 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
225 #endif
226 	/*
227 	 * Clear curproc after we've done all operations
228 	 * that could block, and before tearing down the rest
229 	 * of the process state that might be used from clock, etc.
230 	 * Also, can't clear curproc while we're still runnable,
231 	 * as we're not on a run queue (we are current, just not
232 	 * a proper proc any longer!).
233 	 *
234 	 * Other substructures are freed from wait().
235 	 */
236 	curproc = NULL;
237 	if (--p->p_limit->p_refcnt == 0)
238 		FREE(p->p_limit, M_SUBPROC);
239 
240 	/*
241 	 * Finally, call machine-dependent code to release the remaining
242 	 * resources including address space, the kernel stack and pcb.
243 	 * The address space is released by "vmspace_free(p->p_vmspace)";
244 	 * This is machine-dependent, as we may have to change stacks
245 	 * or ensure that the current one isn't reallocated before we
246 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
247 	 * our execution (pun intended).
248 	 */
249 	cpu_exit(p);
250 }
251 
252 struct wait_args {
253 	int	pid;
254 	int	*status;
255 	int	options;
256 	struct	rusage *rusage;
257 #ifdef COMPAT_43
258 	int	compat;		/* pseudo */
259 #endif
260 };
261 
262 #ifdef COMPAT_43
263 #if defined(hp300) || defined(luna68k)
264 #include <machine/frame.h>
265 #define GETPS(rp)	((struct frame *)(rp))->f_sr
266 #else
267 #define GETPS(rp)	(rp)[PS]
268 #endif
269 
270 owait(p, uap, retval)
271 	struct proc *p;
272 	register struct wait_args *uap;
273 	int *retval;
274 {
275 
276 #ifdef PSL_ALLCC
277 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
278 		uap->options = 0;
279 		uap->rusage = NULL;
280 	} else {
281 		uap->options = p->p_md.md_regs[R0];
282 		uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
283 	}
284 #else
285 	uap->options = 0;
286 	uap->rusage = NULL;
287 #endif
288 	uap->pid = WAIT_ANY;
289 	uap->status = NULL;
290 	uap->compat = 1;
291 	return (wait1(p, uap, retval));
292 }
293 
294 wait4(p, uap, retval)
295 	struct proc *p;
296 	struct wait_args *uap;
297 	int *retval;
298 {
299 
300 	uap->compat = 0;
301 	return (wait1(p, uap, retval));
302 }
303 #else
304 #define	wait1	wait4
305 #endif
306 
307 int
308 wait1(q, uap, retval)
309 	register struct proc *q;
310 	register struct wait_args *uap;
311 	int retval[];
312 {
313 	register int nfound;
314 	register struct proc *p, *t;
315 	int status, error;
316 
317 	if (uap->pid == 0)
318 		uap->pid = -q->p_pgid;
319 #ifdef notyet
320 	if (uap->options &~ (WUNTRACED|WNOHANG))
321 		return (EINVAL);
322 #endif
323 loop:
324 	nfound = 0;
325 	for (p = q->p_cptr; p; p = p->p_osptr) {
326 		if (uap->pid != WAIT_ANY &&
327 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
328 			continue;
329 		nfound++;
330 		if (p->p_stat == SZOMB) {
331 			retval[0] = p->p_pid;
332 #ifdef COMPAT_43
333 			if (uap->compat)
334 				retval[1] = p->p_xstat;
335 			else
336 #endif
337 			if (uap->status) {
338 				status = p->p_xstat;	/* convert to int */
339 				if (error = copyout((caddr_t)&status,
340 				    (caddr_t)uap->status, sizeof(status)))
341 					return (error);
342 			}
343 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
344 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
345 				return (error);
346 			/*
347 			 * If we got the child via a ptrace 'attach',
348 			 * we need to give it back to the old parent.
349 			 */
350 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
351 				p->p_oppid = 0;
352 				proc_reparent(p, t);
353 				psignal(t, SIGCHLD);
354 				wakeup((caddr_t)t);
355 				return (0);
356 			}
357 			p->p_xstat = 0;
358 			ruadd(&q->p_stats->p_cru, p->p_ru);
359 			FREE(p->p_ru, M_ZOMBIE);
360 
361 			/*
362 			 * Decrement the count of procs running with this uid.
363 			 */
364 			(void)chgproccnt(p->p_cred->p_ruid, -1);
365 
366 			/*
367 			 * Free up credentials.
368 			 */
369 			if (--p->p_cred->p_refcnt == 0) {
370 				crfree(p->p_cred->pc_ucred);
371 				FREE(p->p_cred, M_SUBPROC);
372 			}
373 
374 			/*
375 			 * Release reference to text vnode
376 			 */
377 			if (p->p_textvp)
378 				vrele(p->p_textvp);
379 
380 			/*
381 			 * Finally finished with old proc entry.
382 			 * Unlink it from its process group and free it.
383 			 */
384 			leavepgrp(p);
385 			if (*p->p_prev = p->p_next)	/* off zombproc */
386 				p->p_next->p_prev = p->p_prev;
387 			if (q = p->p_ysptr)
388 				q->p_osptr = p->p_osptr;
389 			if (q = p->p_osptr)
390 				q->p_ysptr = p->p_ysptr;
391 			if ((q = p->p_pptr)->p_cptr == p)
392 				q->p_cptr = p->p_osptr;
393 
394 			/*
395 			 * Give machine-dependent layer a chance
396 			 * to free anything that cpu_exit couldn't
397 			 * release while still running in process context.
398 			 */
399 			cpu_wait(p);
400 			FREE(p, M_PROC);
401 			nprocs--;
402 			return (0);
403 		}
404 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
405 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
406 			p->p_flag |= P_WAITED;
407 			retval[0] = p->p_pid;
408 #ifdef COMPAT_43
409 			if (uap->compat) {
410 				retval[1] = W_STOPCODE(p->p_xstat);
411 				error = 0;
412 			} else
413 #endif
414 			if (uap->status) {
415 				status = W_STOPCODE(p->p_xstat);
416 				error = copyout((caddr_t)&status,
417 					(caddr_t)uap->status, sizeof(status));
418 			} else
419 				error = 0;
420 			return (error);
421 		}
422 	}
423 	if (nfound == 0)
424 		return (ECHILD);
425 	if (uap->options & WNOHANG) {
426 		retval[0] = 0;
427 		return (0);
428 	}
429 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
430 		return (error);
431 	goto loop;
432 }
433 
434 /*
435  * make process 'parent' the new parent of process 'child'.
436  */
437 void
438 proc_reparent(child, parent)
439 	register struct proc *child;
440 	register struct proc *parent;
441 {
442 	register struct proc *o;
443 	register struct proc *y;
444 
445 	if (child->p_pptr == parent)
446 		return;
447 
448 	/* fix up the child linkage for the old parent */
449 	o = child->p_osptr;
450 	y = child->p_ysptr;
451 	if (y)
452 		y->p_osptr = o;
453 	if (o)
454 		o->p_ysptr = y;
455 	if (child->p_pptr->p_cptr == child)
456 		child->p_pptr->p_cptr = o;
457 
458 	/* fix up child linkage for new parent */
459 	o = parent->p_cptr;
460 	if (o)
461 		o->p_ysptr = child;
462 	child->p_osptr = o;
463 	child->p_ysptr = NULL;
464 	parent->p_cptr = child;
465 	child->p_pptr = parent;
466 }
467