xref: /openbsd-src/sys/kern/kern_exit.c (revision fe1f7dfa9438a02ac3cf846b995d81cf559db7f5)
1 /*	$OpenBSD: kern_exit.c,v 1.177 2019/06/13 21:19:28 mpi Exp $	*/
2 /*	$NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/ioctl.h>
43 #include <sys/proc.h>
44 #include <sys/tty.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/wait.h>
50 #include <sys/vnode.h>
51 #include <sys/syslog.h>
52 #include <sys/malloc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/ptrace.h>
55 #include <sys/acct.h>
56 #include <sys/filedesc.h>
57 #include <sys/signalvar.h>
58 #include <sys/sched.h>
59 #include <sys/ktrace.h>
60 #include <sys/pool.h>
61 #include <sys/mutex.h>
62 #include <sys/pledge.h>
63 #ifdef SYSVSEM
64 #include <sys/sem.h>
65 #endif
66 #include <sys/witness.h>
67 
68 #include <sys/mount.h>
69 #include <sys/syscallargs.h>
70 
71 #include <uvm/uvm_extern.h>
72 
73 #include "kcov.h"
74 #if NKCOV > 0
75 #include <sys/kcov.h>
76 #endif
77 
78 void	proc_finish_wait(struct proc *, struct proc *);
79 void	process_zap(struct process *);
80 void	proc_free(struct proc *);
81 void	unveil_destroy(struct process *ps);
82 
83 /*
84  * exit --
85  *	Death of process.
86  */
87 int
88 sys_exit(struct proc *p, void *v, register_t *retval)
89 {
90 	struct sys_exit_args /* {
91 		syscallarg(int) rval;
92 	} */ *uap = v;
93 
94 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL);
95 	/* NOTREACHED */
96 	return (0);
97 }
98 
99 int
100 sys___threxit(struct proc *p, void *v, register_t *retval)
101 {
102 	struct sys___threxit_args /* {
103 		syscallarg(pid_t *) notdead;
104 	} */ *uap = v;
105 
106 	if (SCARG(uap, notdead) != NULL) {
107 		pid_t zero = 0;
108 		if (copyout(&zero, SCARG(uap, notdead), sizeof(zero)))
109 			psignal(p, SIGSEGV);
110 	}
111 	exit1(p, 0, EXIT_THREAD);
112 
113 	return (0);
114 }
115 
116 /*
117  * Exit: deallocate address space and other resources, change proc state
118  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
119  * status and rusage for wait().  Check for child processes and orphan them.
120  */
121 void
122 exit1(struct proc *p, int rv, int flags)
123 {
124 	struct process *pr, *qr, *nqr;
125 	struct rusage *rup;
126 
127 	atomic_setbits_int(&p->p_flag, P_WEXIT);
128 
129 	pr = p->p_p;
130 
131 	/* single-threaded? */
132 	if (!P_HASSIBLING(p)) {
133 		flags = EXIT_NORMAL;
134 	} else {
135 		/* nope, multi-threaded */
136 		if (flags == EXIT_NORMAL)
137 			single_thread_set(p, SINGLE_EXIT, 0);
138 		else if (flags == EXIT_THREAD)
139 			single_thread_check(p, 0);
140 	}
141 
142 	if (flags == EXIT_NORMAL) {
143 		if (pr->ps_pid == 1)
144 			panic("init died (signal %d, exit %d)",
145 			    WTERMSIG(rv), WEXITSTATUS(rv));
146 
147 		atomic_setbits_int(&pr->ps_flags, PS_EXITING);
148 		pr->ps_mainproc->p_xstat = rv;
149 
150 		/*
151 		 * If parent is waiting for us to exit or exec, PS_PPWAIT
152 		 * is set; we wake up the parent early to avoid deadlock.
153 		 */
154 		if (pr->ps_flags & PS_PPWAIT) {
155 			atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
156 			atomic_clearbits_int(&pr->ps_pptr->ps_flags,
157 			    PS_ISPWAIT);
158 			wakeup(pr->ps_pptr);
159 		}
160 	}
161 
162 	/* unlink ourselves from the active threads */
163 	TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link);
164 	if ((p->p_flag & P_THREAD) == 0) {
165 		/* main thread gotta wait because it has the pid, et al */
166 		while (pr->ps_refcnt > 1)
167 			tsleep(&pr->ps_threads, PWAIT, "thrdeath", 0);
168 		if (pr->ps_flags & PS_PROFIL)
169 			stopprofclock(pr);
170 	}
171 
172 	rup = pr->ps_ru;
173 	if (rup == NULL) {
174 		rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO);
175 		if (pr->ps_ru == NULL) {
176 			pr->ps_ru = rup;
177 		} else {
178 			pool_put(&rusage_pool, rup);
179 			rup = pr->ps_ru;
180 		}
181 	}
182 	p->p_siglist = 0;
183 	if ((p->p_flag & P_THREAD) == 0)
184 		pr->ps_siglist = 0;
185 
186 #if NKCOV > 0
187 	kcov_exit(p);
188 #endif
189 
190 	if ((p->p_flag & P_THREAD) == 0) {
191 		sigio_freelist(&pr->ps_sigiolst);
192 
193 		/* close open files and release open-file table */
194 		fdfree(p);
195 
196 		timeout_del(&pr->ps_realit_to);
197 		timeout_del(&pr->ps_rucheck_to);
198 #ifdef SYSVSEM
199 		semexit(pr);
200 #endif
201 		killjobc(pr);
202 #ifdef ACCOUNTING
203 		acct_process(p);
204 #endif
205 
206 #ifdef KTRACE
207 		/* release trace file */
208 		if (pr->ps_tracevp)
209 			ktrcleartrace(pr);
210 #endif
211 
212 		unveil_destroy(pr);
213 
214 		/*
215 		 * If parent has the SAS_NOCLDWAIT flag set, we're not
216 		 * going to become a zombie.
217 		 */
218 		if (pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDWAIT)
219 			atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
220 	}
221 
222 	p->p_fd = NULL;		/* zap the thread's copy */
223 
224         /*
225 	 * Remove proc from pidhash chain and allproc so looking
226 	 * it up won't work.  We will put the proc on the
227 	 * deadproc list later (using the p_hash member), and
228 	 * wake up the reaper when we do.  If this is the last
229 	 * thread of a process that isn't PS_NOZOMBIE, we'll put
230 	 * the process on the zombprocess list below.
231 	 */
232 	/*
233 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
234 	 */
235 	p->p_stat = SDEAD;
236 
237 	LIST_REMOVE(p, p_hash);
238 	LIST_REMOVE(p, p_list);
239 
240 	if ((p->p_flag & P_THREAD) == 0) {
241 		LIST_REMOVE(pr, ps_hash);
242 		LIST_REMOVE(pr, ps_list);
243 
244 		if ((pr->ps_flags & PS_NOZOMBIE) == 0)
245 			LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
246 		else {
247 			/*
248 			 * Not going to be a zombie, so it's now off all
249 			 * the lists scanned by ispidtaken(), so block
250 			 * fast reuse of the pid now.
251 			 */
252 			freepid(pr->ps_pid);
253 		}
254 
255 		/*
256 		 * Give orphaned children to init(8).
257 		 */
258 		qr = LIST_FIRST(&pr->ps_children);
259 		if (qr)		/* only need this if any child is S_ZOMB */
260 			wakeup(initprocess);
261 		for (; qr != 0; qr = nqr) {
262 			nqr = LIST_NEXT(qr, ps_sibling);
263 			proc_reparent(qr, initprocess);
264 			/*
265 			 * Traced processes are killed since their
266 			 * existence means someone is screwing up.
267 			 */
268 			if (qr->ps_flags & PS_TRACED &&
269 			    !(qr->ps_flags & PS_EXITING)) {
270 				atomic_clearbits_int(&qr->ps_flags, PS_TRACED);
271 				/*
272 				 * If single threading is active,
273 				 * direct the signal to the active
274 				 * thread to avoid deadlock.
275 				 */
276 				if (qr->ps_single)
277 					ptsignal(qr->ps_single, SIGKILL,
278 					    STHREAD);
279 				else
280 					prsignal(qr, SIGKILL);
281 			}
282 		}
283 	}
284 
285 	/* add thread's accumulated rusage into the process's total */
286 	ruadd(rup, &p->p_ru);
287 	tuagg(pr, p);
288 
289 	/*
290 	 * clear %cpu usage during swap
291 	 */
292 	p->p_pctcpu = 0;
293 
294 	if ((p->p_flag & P_THREAD) == 0) {
295 		/*
296 		 * Final thread has died, so add on our children's rusage
297 		 * and calculate the total times
298 		 */
299 		calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
300 		ruadd(rup, &pr->ps_cru);
301 
302 		/* notify interested parties of our demise and clean up */
303 		knote_processexit(p);
304 
305 		/*
306 		 * Notify parent that we're gone.  If we're not going to
307 		 * become a zombie, reparent to process 1 (init) so that
308 		 * we can wake our original parent to possibly unblock
309 		 * wait4() to return ECHILD.
310 		 */
311 		if (pr->ps_flags & PS_NOZOMBIE) {
312 			struct process *ppr = pr->ps_pptr;
313 			proc_reparent(pr, initprocess);
314 			wakeup(ppr);
315 		}
316 
317 		/*
318 		 * Release the process's signal state.
319 		 */
320 		sigactsfree(pr);
321 	}
322 
323 	/* just a thread? detach it from its process */
324 	if (p->p_flag & P_THREAD) {
325 		/* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
326 		if (--pr->ps_refcnt == 1)
327 			wakeup(&pr->ps_threads);
328 		KASSERT(pr->ps_refcnt > 0);
329 	}
330 
331 	/*
332 	 * Other substructures are freed from reaper and wait().
333 	 */
334 
335 	/*
336 	 * Finally, call machine-dependent code to switch to a new
337 	 * context (possibly the idle context).  Once we are no longer
338 	 * using the dead process's vmspace and stack, exit2() will be
339 	 * called to schedule those resources to be released by the
340 	 * reaper thread.
341 	 *
342 	 * Note that cpu_exit() will end with a call equivalent to
343 	 * cpu_switch(), finishing our execution (pun intended).
344 	 */
345 	uvmexp.swtch++;
346 	cpu_exit(p);
347 	panic("cpu_exit returned");
348 }
349 
350 /*
351  * Locking of this proclist is special; it's accessed in a
352  * critical section of process exit, and thus locking it can't
353  * modify interrupt state.  We use a simple spin lock for this
354  * proclist.  We use the p_hash member to linkup to deadproc.
355  */
356 struct mutex deadproc_mutex =
357     MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS);
358 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
359 
360 /*
361  * We are called from cpu_exit() once it is safe to schedule the
362  * dead process's resources to be freed.
363  *
364  * NOTE: One must be careful with locking in this routine.  It's
365  * called from a critical section in machine-dependent code, so
366  * we should refrain from changing any interrupt state.
367  *
368  * We lock the deadproc list, place the proc on that list (using
369  * the p_hash member), and wake up the reaper.
370  */
371 void
372 exit2(struct proc *p)
373 {
374 	mtx_enter(&deadproc_mutex);
375 	LIST_INSERT_HEAD(&deadproc, p, p_hash);
376 	mtx_leave(&deadproc_mutex);
377 
378 	wakeup(&deadproc);
379 }
380 
381 void
382 proc_free(struct proc *p)
383 {
384 	crfree(p->p_ucred);
385 	pool_put(&proc_pool, p);
386 	nthreads--;
387 }
388 
389 /*
390  * Process reaper.  This is run by a kernel thread to free the resources
391  * of a dead process.  Once the resources are free, the process becomes
392  * a zombie, and the parent is allowed to read the undead's status.
393  */
394 void
395 reaper(void *arg)
396 {
397 	struct proc *p;
398 
399 	KERNEL_UNLOCK();
400 
401 	SCHED_ASSERT_UNLOCKED();
402 
403 	for (;;) {
404 		mtx_enter(&deadproc_mutex);
405 		while ((p = LIST_FIRST(&deadproc)) == NULL)
406 			msleep(&deadproc, &deadproc_mutex, PVM, "reaper", 0);
407 
408 		/* Remove us from the deadproc list. */
409 		LIST_REMOVE(p, p_hash);
410 		mtx_leave(&deadproc_mutex);
411 
412 		WITNESS_THREAD_EXIT(p);
413 
414 		KERNEL_LOCK();
415 
416 		/*
417 		 * Free the VM resources we're still holding on to.
418 		 * We must do this from a valid thread because doing
419 		 * so may block.
420 		 */
421 		uvm_uarea_free(p);
422 		p->p_vmspace = NULL;		/* zap the thread's copy */
423 
424 		if (p->p_flag & P_THREAD) {
425 			/* Just a thread */
426 			proc_free(p);
427 		} else {
428 			struct process *pr = p->p_p;
429 
430 			/* Release the rest of the process's vmspace */
431 			uvm_exit(pr);
432 
433 			if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
434 				/* Process is now a true zombie. */
435 				atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
436 				prsignal(pr->ps_pptr, SIGCHLD);
437 
438 				/* Wake up the parent so it can get exit status. */
439 				wakeup(pr->ps_pptr);
440 			} else {
441 				/* No one will wait for us. Just zap the process now */
442 				process_zap(pr);
443 			}
444 		}
445 
446 		KERNEL_UNLOCK();
447 	}
448 }
449 
450 int
451 sys_wait4(struct proc *q, void *v, register_t *retval)
452 {
453 	struct sys_wait4_args /* {
454 		syscallarg(pid_t) pid;
455 		syscallarg(int *) status;
456 		syscallarg(int) options;
457 		syscallarg(struct rusage *) rusage;
458 	} */ *uap = v;
459 	struct rusage ru;
460 	int status, error;
461 
462 	error = dowait4(q, SCARG(uap, pid),
463 	    SCARG(uap, status) ? &status : NULL,
464 	    SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval);
465 	if (error == 0 && retval[0] > 0 && SCARG(uap, status)) {
466 		error = copyout(&status, SCARG(uap, status), sizeof(status));
467 	}
468 	if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) {
469 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
470 #ifdef KTRACE
471 		if (error == 0 && KTRPOINT(q, KTR_STRUCT))
472 			ktrrusage(q, &ru);
473 #endif
474 	}
475 	return (error);
476 }
477 
478 int
479 dowait4(struct proc *q, pid_t pid, int *statusp, int options,
480     struct rusage *rusage, register_t *retval)
481 {
482 	int nfound;
483 	struct process *pr;
484 	struct proc *p;
485 	int error;
486 
487 	if (pid == 0)
488 		pid = -q->p_p->ps_pgid;
489 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED))
490 		return (EINVAL);
491 
492 loop:
493 	nfound = 0;
494 	LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) {
495 		if ((pr->ps_flags & PS_NOZOMBIE) ||
496 		    (pid != WAIT_ANY &&
497 		    pr->ps_pid != pid &&
498 		    pr->ps_pgid != -pid))
499 			continue;
500 
501 		p = pr->ps_mainproc;
502 
503 		nfound++;
504 		if (pr->ps_flags & PS_ZOMBIE) {
505 			retval[0] = pr->ps_pid;
506 
507 			if (statusp != NULL)
508 				*statusp = p->p_xstat;	/* convert to int */
509 			if (rusage != NULL)
510 				memcpy(rusage, pr->ps_ru, sizeof(*rusage));
511 			proc_finish_wait(q, p);
512 			return (0);
513 		}
514 		if (pr->ps_flags & PS_TRACED &&
515 		    (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single &&
516 		    pr->ps_single->p_stat == SSTOP &&
517 		    (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) {
518 			single_thread_wait(pr);
519 
520 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
521 			retval[0] = pr->ps_pid;
522 
523 			if (statusp != NULL)
524 				*statusp = W_STOPCODE(pr->ps_single->p_xstat);
525 			if (rusage != NULL)
526 				memset(rusage, 0, sizeof(*rusage));
527 			return (0);
528 		}
529 		if (p->p_stat == SSTOP &&
530 		    (pr->ps_flags & PS_WAITED) == 0 &&
531 		    (p->p_flag & P_SUSPSINGLE) == 0 &&
532 		    (pr->ps_flags & PS_TRACED ||
533 		    options & WUNTRACED)) {
534 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
535 			retval[0] = pr->ps_pid;
536 
537 			if (statusp != NULL)
538 				*statusp = W_STOPCODE(p->p_xstat);
539 			if (rusage != NULL)
540 				memset(rusage, 0, sizeof(*rusage));
541 			return (0);
542 		}
543 		if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
544 			atomic_clearbits_int(&p->p_flag, P_CONTINUED);
545 			retval[0] = pr->ps_pid;
546 
547 			if (statusp != NULL)
548 				*statusp = _WCONTINUED;
549 			if (rusage != NULL)
550 				memset(rusage, 0, sizeof(*rusage));
551 			return (0);
552 		}
553 	}
554 	if (nfound == 0)
555 		return (ECHILD);
556 	if (options & WNOHANG) {
557 		retval[0] = 0;
558 		return (0);
559 	}
560 	if ((error = tsleep(q->p_p, PWAIT | PCATCH, "wait", 0)) != 0)
561 		return (error);
562 	goto loop;
563 }
564 
565 void
566 proc_finish_wait(struct proc *waiter, struct proc *p)
567 {
568 	struct process *pr, *tr;
569 	struct rusage *rup;
570 
571 	/*
572 	 * If we got the child via a ptrace 'attach',
573 	 * we need to give it back to the old parent.
574 	 */
575 	pr = p->p_p;
576 	if (pr->ps_oppid && (tr = prfind(pr->ps_oppid))) {
577 		atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
578 		pr->ps_oppid = 0;
579 		proc_reparent(pr, tr);
580 		prsignal(tr, SIGCHLD);
581 		wakeup(tr);
582 	} else {
583 		scheduler_wait_hook(waiter, p);
584 		p->p_xstat = 0;
585 		rup = &waiter->p_p->ps_cru;
586 		ruadd(rup, pr->ps_ru);
587 		LIST_REMOVE(pr, ps_list);	/* off zombprocess */
588 		freepid(pr->ps_pid);
589 		process_zap(pr);
590 	}
591 }
592 
593 /*
594  * make process 'parent' the new parent of process 'child'.
595  */
596 void
597 proc_reparent(struct process *child, struct process *parent)
598 {
599 
600 	if (child->ps_pptr == parent)
601 		return;
602 
603 	LIST_REMOVE(child, ps_sibling);
604 	LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling);
605 	child->ps_pptr = parent;
606 }
607 
608 void
609 process_zap(struct process *pr)
610 {
611 	struct vnode *otvp;
612 	struct proc *p = pr->ps_mainproc;
613 
614 	/*
615 	 * Finally finished with old proc entry.
616 	 * Unlink it from its process group and free it.
617 	 */
618 	leavepgrp(pr);
619 	LIST_REMOVE(pr, ps_sibling);
620 
621 	/*
622 	 * Decrement the count of procs running with this uid.
623 	 */
624 	(void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
625 
626 	/*
627 	 * Release reference to text vnode
628 	 */
629 	otvp = pr->ps_textvp;
630 	pr->ps_textvp = NULL;
631 	if (otvp)
632 		vrele(otvp);
633 
634 	KASSERT(pr->ps_refcnt == 1);
635 	if (pr->ps_ptstat != NULL)
636 		free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat));
637 	pool_put(&rusage_pool, pr->ps_ru);
638 	KASSERT(TAILQ_EMPTY(&pr->ps_threads));
639 	limfree(pr->ps_limit);
640 	crfree(pr->ps_ucred);
641 	pool_put(&process_pool, pr);
642 	nprocesses--;
643 
644 	proc_free(p);
645 }
646