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 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD$");
39
40#include "opt_compat.h"
41#include "opt_kdtrace.h"
42#include "opt_ktrace.h"
43#include "opt_procdesc.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/capability.h>
49#include <sys/eventhandler.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <sys/proc.h>
55#include <sys/procdesc.h>
56#include <sys/pioctl.h>
57#include <sys/jail.h>
58#include <sys/tty.h>
59#include <sys/wait.h>
60#include <sys/vmmeter.h>
61#include <sys/vnode.h>
62#include <sys/racct.h>
63#include <sys/resourcevar.h>
64#include <sys/sbuf.h>
65#include <sys/signalvar.h>
66#include <sys/sched.h>
67#include <sys/sx.h>
68#include <sys/syscallsubr.h>
69#include <sys/syslog.h>
70#include <sys/ptrace.h>
71#include <sys/acct.h>		/* for acct_process() function prototype */
72#include <sys/filedesc.h>
73#include <sys/sdt.h>
74#include <sys/shm.h>
75#include <sys/sem.h>
76#ifdef KTRACE
77#include <sys/ktrace.h>
78#endif
79
80#include <security/audit/audit.h>
81#include <security/mac/mac_framework.h>
82
83#include <vm/vm.h>
84#include <vm/vm_extern.h>
85#include <vm/vm_param.h>
86#include <vm/pmap.h>
87#include <vm/vm_map.h>
88#include <vm/vm_page.h>
89#include <vm/uma.h>
90
91#ifdef KDTRACE_HOOKS
92#include <sys/dtrace_bsd.h>
93dtrace_execexit_func_t	dtrace_fasttrap_exit;
94#endif
95
96SDT_PROVIDER_DECLARE(proc);
97SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
98
99/* Hook for NFS teardown procedure. */
100void (*nlminfo_release_p)(struct proc *p);
101
102static void
103clear_orphan(struct proc *p)
104{
105
106	PROC_LOCK_ASSERT(p, MA_OWNED);
107
108	if (p->p_flag & P_ORPHAN) {
109		LIST_REMOVE(p, p_orphan);
110		p->p_flag &= ~P_ORPHAN;
111	}
112}
113
114/*
115 * exit -- death of process.
116 */
117void
118sys_sys_exit(struct thread *td, struct sys_exit_args *uap)
119{
120
121	exit1(td, W_EXITCODE(uap->rval, 0));
122	/* NOTREACHED */
123}
124
125/*
126 * Exit: deallocate address space and other resources, change proc state to
127 * zombie, and unlink proc from allproc and parent's lists.  Save exit status
128 * and rusage for wait().  Check for child processes and orphan them.
129 */
130void
131exit1(struct thread *td, int rv)
132{
133	struct proc *p, *nq, *q;
134	struct vnode *vtmp;
135	struct vnode *ttyvp = NULL;
136	struct plimit *plim;
137	int locked;
138
139	mtx_assert(&Giant, MA_NOTOWNED);
140
141	p = td->td_proc;
142	/*
143	 * XXX in case we're rebooting we just let init die in order to
144	 * work around an unsolved stack overflow seen very late during
145	 * shutdown on sparc64 when the gmirror worker process exists.
146	 */
147	if (p == initproc && rebooting == 0) {
148		printf("init died (signal %d, exit %d)\n",
149		    WTERMSIG(rv), WEXITSTATUS(rv));
150		panic("Going nowhere without my init!");
151	}
152
153	/*
154	 * MUST abort all other threads before proceeding past here.
155	 */
156	PROC_LOCK(p);
157	while (p->p_flag & P_HADTHREADS) {
158		/*
159		 * First check if some other thread got here before us..
160		 * if so, act apropriatly, (exit or suspend);
161		 */
162		thread_suspend_check(0);
163
164		/*
165		 * Kill off the other threads. This requires
166		 * some co-operation from other parts of the kernel
167		 * so it may not be instantaneous.  With this state set
168		 * any thread entering the kernel from userspace will
169		 * thread_exit() in trap().  Any thread attempting to
170		 * sleep will return immediately with EINTR or EWOULDBLOCK
171		 * which will hopefully force them to back out to userland
172		 * freeing resources as they go.  Any thread attempting
173		 * to return to userland will thread_exit() from userret().
174		 * thread_exit() will unsuspend us when the last of the
175		 * other threads exits.
176		 * If there is already a thread singler after resumption,
177		 * calling thread_single will fail; in that case, we just
178		 * re-check all suspension request, the thread should
179		 * either be suspended there or exit.
180		 */
181		if (! thread_single(SINGLE_EXIT))
182			break;
183
184		/*
185		 * All other activity in this process is now stopped.
186		 * Threading support has been turned off.
187		 */
188	}
189	KASSERT(p->p_numthreads == 1,
190	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
191	racct_sub(p, RACCT_NTHR, 1);
192	/*
193	 * Wakeup anyone in procfs' PIOCWAIT.  They should have a hold
194	 * on our vmspace, so we should block below until they have
195	 * released their reference to us.  Note that if they have
196	 * requested S_EXIT stops we will block here until they ack
197	 * via PIOCCONT.
198	 */
199	_STOPEVENT(p, S_EXIT, rv);
200
201	/*
202	 * Ignore any pending request to stop due to a stop signal.
203	 * Once P_WEXIT is set, future requests will be ignored as
204	 * well.
205	 */
206	p->p_flag &= ~P_STOPPED_SIG;
207	KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped"));
208
209	/*
210	 * Note that we are exiting and do another wakeup of anyone in
211	 * PIOCWAIT in case they aren't listening for S_EXIT stops or
212	 * decided to wait again after we told them we are exiting.
213	 */
214	p->p_flag |= P_WEXIT;
215	wakeup(&p->p_stype);
216
217	/*
218	 * Wait for any processes that have a hold on our vmspace to
219	 * release their reference.
220	 */
221	while (p->p_lock > 0)
222		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
223
224	p->p_xstat = rv;	/* Let event handler change exit status */
225	PROC_UNLOCK(p);
226	/* Drain the limit callout while we don't have the proc locked */
227	callout_drain(&p->p_limco);
228
229#ifdef AUDIT
230	/*
231	 * The Sun BSM exit token contains two components: an exit status as
232	 * passed to exit(), and a return value to indicate what sort of exit
233	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
234	 * what the return value is.
235	 */
236	AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0);
237	AUDIT_SYSCALL_EXIT(0, td);
238#endif
239
240	/* Are we a task leader? */
241	if (p == p->p_leader) {
242		mtx_lock(&ppeers_lock);
243		q = p->p_peers;
244		while (q != NULL) {
245			PROC_LOCK(q);
246			kern_psignal(q, SIGKILL);
247			PROC_UNLOCK(q);
248			q = q->p_peers;
249		}
250		while (p->p_peers != NULL)
251			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
252		mtx_unlock(&ppeers_lock);
253	}
254
255	/*
256	 * Check if any loadable modules need anything done at process exit.
257	 * E.g. SYSV IPC stuff
258	 * XXX what if one of these generates an error?
259	 */
260	EVENTHANDLER_INVOKE(process_exit, p);
261
262	/*
263	 * If parent is waiting for us to exit or exec,
264	 * P_PPWAIT is set; we will wakeup the parent below.
265	 */
266	PROC_LOCK(p);
267	rv = p->p_xstat;	/* Event handler could change exit status */
268	stopprofclock(p);
269	p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
270
271	/*
272	 * Stop the real interval timer.  If the handler is currently
273	 * executing, prevent it from rearming itself and let it finish.
274	 */
275	if (timevalisset(&p->p_realtimer.it_value) &&
276	    callout_stop(&p->p_itcallout) == 0) {
277		timevalclear(&p->p_realtimer.it_interval);
278		msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
279		KASSERT(!timevalisset(&p->p_realtimer.it_value),
280		    ("realtime timer is still armed"));
281	}
282	PROC_UNLOCK(p);
283
284	/*
285	 * Reset any sigio structures pointing to us as a result of
286	 * F_SETOWN with our pid.
287	 */
288	funsetownlst(&p->p_sigiolst);
289
290	/*
291	 * If this process has an nlminfo data area (for lockd), release it
292	 */
293	if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
294		(*nlminfo_release_p)(p);
295
296	/*
297	 * Close open files and release open-file table.
298	 * This may block!
299	 */
300	fdfree(td);
301
302	/*
303	 * If this thread tickled GEOM, we need to wait for the giggling to
304	 * stop before we return to userland
305	 */
306	if (td->td_pflags & TDP_GEOM)
307		g_waitidle();
308
309	/*
310	 * Remove ourself from our leader's peer list and wake our leader.
311	 */
312	mtx_lock(&ppeers_lock);
313	if (p->p_leader->p_peers) {
314		q = p->p_leader;
315		while (q->p_peers != p)
316			q = q->p_peers;
317		q->p_peers = p->p_peers;
318		wakeup(p->p_leader);
319	}
320	mtx_unlock(&ppeers_lock);
321
322	vmspace_exit(td);
323
324	sx_xlock(&proctree_lock);
325	if (SESS_LEADER(p)) {
326		struct session *sp = p->p_session;
327		struct tty *tp;
328
329		/*
330		 * s_ttyp is not zero'd; we use this to indicate that
331		 * the session once had a controlling terminal. (for
332		 * logging and informational purposes)
333		 */
334		SESS_LOCK(sp);
335		ttyvp = sp->s_ttyvp;
336		tp = sp->s_ttyp;
337		sp->s_ttyvp = NULL;
338		sp->s_ttydp = NULL;
339		sp->s_leader = NULL;
340		SESS_UNLOCK(sp);
341
342		/*
343		 * Signal foreground pgrp and revoke access to
344		 * controlling terminal if it has not been revoked
345		 * already.
346		 *
347		 * Because the TTY may have been revoked in the mean
348		 * time and could already have a new session associated
349		 * with it, make sure we don't send a SIGHUP to a
350		 * foreground process group that does not belong to this
351		 * session.
352		 */
353
354		if (tp != NULL) {
355			tty_lock(tp);
356			if (tp->t_session == sp)
357				tty_signal_pgrp(tp, SIGHUP);
358			tty_unlock(tp);
359		}
360
361		if (ttyvp != NULL) {
362			sx_xunlock(&proctree_lock);
363			if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
364				VOP_REVOKE(ttyvp, REVOKEALL);
365				VOP_UNLOCK(ttyvp, 0);
366			}
367			sx_xlock(&proctree_lock);
368		}
369	}
370	fixjobc(p, p->p_pgrp, 0);
371	sx_xunlock(&proctree_lock);
372	(void)acct_process(td);
373
374	/* Release the TTY now we've unlocked everything. */
375	if (ttyvp != NULL)
376		vrele(ttyvp);
377#ifdef KTRACE
378	ktrprocexit(td);
379#endif
380	/*
381	 * Release reference to text vnode
382	 */
383	if ((vtmp = p->p_textvp) != NULL) {
384		p->p_textvp = NULL;
385		locked = VFS_LOCK_GIANT(vtmp->v_mount);
386		vrele(vtmp);
387		VFS_UNLOCK_GIANT(locked);
388	}
389
390	/*
391	 * Release our limits structure.
392	 */
393	PROC_LOCK(p);
394	plim = p->p_limit;
395	p->p_limit = NULL;
396	PROC_UNLOCK(p);
397	lim_free(plim);
398
399	tidhash_remove(td);
400
401	/*
402	 * Remove proc from allproc queue and pidhash chain.
403	 * Place onto zombproc.  Unlink from parent's child list.
404	 */
405	sx_xlock(&allproc_lock);
406	LIST_REMOVE(p, p_list);
407	LIST_INSERT_HEAD(&zombproc, p, p_list);
408	LIST_REMOVE(p, p_hash);
409	sx_xunlock(&allproc_lock);
410
411	/*
412	 * Call machine-dependent code to release any
413	 * machine-dependent resources other than the address space.
414	 * The address space is released by "vmspace_exitfree(p)" in
415	 * vm_waitproc().
416	 */
417	cpu_exit(td);
418
419	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
420
421	/*
422	 * Reparent all of our children to init.
423	 */
424	sx_xlock(&proctree_lock);
425	q = LIST_FIRST(&p->p_children);
426	if (q != NULL)		/* only need this if any child is S_ZOMB */
427		wakeup(initproc);
428	for (; q != NULL; q = nq) {
429		nq = LIST_NEXT(q, p_sibling);
430		PROC_LOCK(q);
431		proc_reparent(q, initproc);
432		q->p_sigparent = SIGCHLD;
433		/*
434		 * Traced processes are killed
435		 * since their existence means someone is screwing up.
436		 */
437		if (q->p_flag & P_TRACED) {
438			struct thread *temp;
439
440			/*
441			 * Since q was found on our children list, the
442			 * proc_reparent() call moved q to the orphan
443			 * list due to present P_TRACED flag. Clear
444			 * orphan link for q now while q is locked.
445			 */
446			clear_orphan(q);
447			q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
448			FOREACH_THREAD_IN_PROC(q, temp)
449				temp->td_dbgflags &= ~TDB_SUSPEND;
450			kern_psignal(q, SIGKILL);
451		}
452		PROC_UNLOCK(q);
453	}
454
455	/*
456	 * Also get rid of our orphans.
457	 */
458	while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
459		PROC_LOCK(q);
460		clear_orphan(q);
461		PROC_UNLOCK(q);
462	}
463
464	/* Save exit status. */
465	PROC_LOCK(p);
466	p->p_xthread = td;
467
468	/* Tell the prison that we are gone. */
469	prison_proc_free(p->p_ucred->cr_prison);
470
471#ifdef KDTRACE_HOOKS
472	/*
473	 * Tell the DTrace fasttrap provider about the exit if it
474	 * has declared an interest.
475	 */
476	if (dtrace_fasttrap_exit)
477		dtrace_fasttrap_exit(p);
478#endif
479
480	/*
481	 * Notify interested parties of our demise.
482	 */
483	KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
484
485#ifdef KDTRACE_HOOKS
486	int reason = CLD_EXITED;
487	if (WCOREDUMP(rv))
488		reason = CLD_DUMPED;
489	else if (WIFSIGNALED(rv))
490		reason = CLD_KILLED;
491	SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0);
492#endif
493
494	/*
495	 * Just delete all entries in the p_klist. At this point we won't
496	 * report any more events, and there are nasty race conditions that
497	 * can beat us if we don't.
498	 */
499	knlist_clear(&p->p_klist, 1);
500
501	/*
502	 * If this is a process with a descriptor, we may not need to deliver
503	 * a signal to the parent.  proctree_lock is held over
504	 * procdesc_exit() to serialize concurrent calls to close() and
505	 * exit().
506	 */
507#ifdef PROCDESC
508	if (p->p_procdesc == NULL || procdesc_exit(p)) {
509#endif
510		/*
511		 * Notify parent that we're gone.  If parent has the
512		 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN,
513		 * notify process 1 instead (and hope it will handle this
514		 * situation).
515		 */
516		PROC_LOCK(p->p_pptr);
517		mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
518		if (p->p_pptr->p_sigacts->ps_flag &
519		    (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
520			struct proc *pp;
521
522			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
523			pp = p->p_pptr;
524			PROC_UNLOCK(pp);
525			proc_reparent(p, initproc);
526			p->p_sigparent = SIGCHLD;
527			PROC_LOCK(p->p_pptr);
528
529			/*
530			 * Notify parent, so in case he was wait(2)ing or
531			 * executing waitpid(2) with our pid, he will
532			 * continue.
533			 */
534			wakeup(pp);
535		} else
536			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
537
538		if (p->p_pptr == initproc)
539			kern_psignal(p->p_pptr, SIGCHLD);
540		else if (p->p_sigparent != 0) {
541			if (p->p_sigparent == SIGCHLD)
542				childproc_exited(p);
543			else	/* LINUX thread */
544				kern_psignal(p->p_pptr, p->p_sigparent);
545		}
546#ifdef PROCDESC
547	} else
548		PROC_LOCK(p->p_pptr);
549#endif
550	sx_xunlock(&proctree_lock);
551
552	/*
553	 * The state PRS_ZOMBIE prevents other proesses from sending
554	 * signal to the process, to avoid memory leak, we free memory
555	 * for signal queue at the time when the state is set.
556	 */
557	sigqueue_flush(&p->p_sigqueue);
558	sigqueue_flush(&td->td_sigqueue);
559
560	/*
561	 * We have to wait until after acquiring all locks before
562	 * changing p_state.  We need to avoid all possible context
563	 * switches (including ones from blocking on a mutex) while
564	 * marked as a zombie.  We also have to set the zombie state
565	 * before we release the parent process' proc lock to avoid
566	 * a lost wakeup.  So, we first call wakeup, then we grab the
567	 * sched lock, update the state, and release the parent process'
568	 * proc lock.
569	 */
570	wakeup(p->p_pptr);
571	cv_broadcast(&p->p_pwait);
572	sched_exit(p->p_pptr, td);
573	PROC_SLOCK(p);
574	p->p_state = PRS_ZOMBIE;
575	PROC_UNLOCK(p->p_pptr);
576
577	/*
578	 * Hopefully no one will try to deliver a signal to the process this
579	 * late in the game.
580	 */
581	knlist_destroy(&p->p_klist);
582
583	/*
584	 * Save our children's rusage information in our exit rusage.
585	 */
586	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
587
588	/*
589	 * Make sure the scheduler takes this thread out of its tables etc.
590	 * This will also release this thread's reference to the ucred.
591	 * Other thread parts to release include pcb bits and such.
592	 */
593	thread_exit();
594}
595
596
597#ifndef _SYS_SYSPROTO_H_
598struct abort2_args {
599	char *why;
600	int nargs;
601	void **args;
602};
603#endif
604
605int
606sys_abort2(struct thread *td, struct abort2_args *uap)
607{
608	struct proc *p = td->td_proc;
609	struct sbuf *sb;
610	void *uargs[16];
611	int error, i, sig;
612
613	/*
614	 * Do it right now so we can log either proper call of abort2(), or
615	 * note, that invalid argument was passed. 512 is big enough to
616	 * handle 16 arguments' descriptions with additional comments.
617	 */
618	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
619	sbuf_clear(sb);
620	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
621	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
622	/*
623	 * Since we can't return from abort2(), send SIGKILL in cases, where
624	 * abort2() was called improperly
625	 */
626	sig = SIGKILL;
627	/* Prevent from DoSes from user-space. */
628	if (uap->nargs < 0 || uap->nargs > 16)
629		goto out;
630	if (uap->nargs > 0) {
631		if (uap->args == NULL)
632			goto out;
633		error = copyin(uap->args, uargs, uap->nargs * sizeof(void *));
634		if (error != 0)
635			goto out;
636	}
637	/*
638	 * Limit size of 'reason' string to 128. Will fit even when
639	 * maximal number of arguments was chosen to be logged.
640	 */
641	if (uap->why != NULL) {
642		error = sbuf_copyin(sb, uap->why, 128);
643		if (error < 0)
644			goto out;
645	} else {
646		sbuf_printf(sb, "(null)");
647	}
648	if (uap->nargs > 0) {
649		sbuf_printf(sb, "(");
650		for (i = 0;i < uap->nargs; i++)
651			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
652		sbuf_printf(sb, ")");
653	}
654	/*
655	 * Final stage: arguments were proper, string has been
656	 * successfully copied from userspace, and copying pointers
657	 * from user-space succeed.
658	 */
659	sig = SIGABRT;
660out:
661	if (sig == SIGKILL) {
662		sbuf_trim(sb);
663		sbuf_printf(sb, " (Reason text inaccessible)");
664	}
665	sbuf_cat(sb, "\n");
666	sbuf_finish(sb);
667	log(LOG_INFO, "%s", sbuf_data(sb));
668	sbuf_delete(sb);
669	exit1(td, W_EXITCODE(0, sig));
670	return (0);
671}
672
673
674#ifdef COMPAT_43
675/*
676 * The dirty work is handled by kern_wait().
677 */
678int
679owait(struct thread *td, struct owait_args *uap __unused)
680{
681	int error, status;
682
683	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
684	if (error == 0)
685		td->td_retval[1] = status;
686	return (error);
687}
688#endif /* COMPAT_43 */
689
690/*
691 * The dirty work is handled by kern_wait().
692 */
693int
694sys_wait4(struct thread *td, struct wait4_args *uap)
695{
696	struct rusage ru, *rup;
697	int error, status;
698
699	if (uap->rusage != NULL)
700		rup = &ru;
701	else
702		rup = NULL;
703	error = kern_wait(td, uap->pid, &status, uap->options, rup);
704	if (uap->status != NULL && error == 0)
705		error = copyout(&status, uap->status, sizeof(status));
706	if (uap->rusage != NULL && error == 0)
707		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
708	return (error);
709}
710
711int
712sys_wait6(struct thread *td, struct wait6_args *uap)
713{
714	struct __wrusage wru, *wrup;
715	siginfo_t si, *sip;
716	idtype_t idtype;
717	id_t id;
718	int error, status;
719
720	idtype = uap->idtype;
721	id = uap->id;
722
723	if (uap->wrusage != NULL)
724		wrup = &wru;
725	else
726		wrup = NULL;
727
728	if (uap->info != NULL) {
729		sip = &si;
730		bzero(sip, sizeof(*sip));
731	} else
732		sip = NULL;
733
734	/*
735	 *  We expect all callers of wait6() to know about WEXITED and
736	 *  WTRAPPED.
737	 */
738	error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
739
740	if (uap->status != NULL && error == 0)
741		error = copyout(&status, uap->status, sizeof(status));
742	if (uap->wrusage != NULL && error == 0)
743		error = copyout(&wru, uap->wrusage, sizeof(wru));
744	if (uap->info != NULL && error == 0)
745		error = copyout(&si, uap->info, sizeof(si));
746	return (error);
747}
748
749/*
750 * Reap the remains of a zombie process and optionally return status and
751 * rusage.  Asserts and will release both the proctree_lock and the process
752 * lock as part of its work.
753 */
754void
755proc_reap(struct thread *td, struct proc *p, int *status, int options)
756{
757	struct proc *q, *t;
758
759	sx_assert(&proctree_lock, SA_XLOCKED);
760	PROC_LOCK_ASSERT(p, MA_OWNED);
761	PROC_SLOCK_ASSERT(p, MA_OWNED);
762	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
763
764	q = td->td_proc;
765
766	PROC_SUNLOCK(p);
767	td->td_retval[0] = p->p_pid;
768	if (status)
769		*status = p->p_xstat;	/* convert to int */
770	if (options & WNOWAIT) {
771		/*
772		 *  Only poll, returning the status.  Caller does not wish to
773		 * release the proc struct just yet.
774		 */
775		PROC_UNLOCK(p);
776		sx_xunlock(&proctree_lock);
777		return;
778	}
779
780	PROC_LOCK(q);
781	sigqueue_take(p->p_ksi);
782	PROC_UNLOCK(q);
783	PROC_UNLOCK(p);
784
785	/*
786	 * If we got the child via a ptrace 'attach', we need to give it back
787	 * to the old parent.
788	 */
789	if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
790		PROC_LOCK(p);
791		proc_reparent(p, t);
792		p->p_oppid = 0;
793		PROC_UNLOCK(p);
794		pksignal(t, SIGCHLD, p->p_ksi);
795		wakeup(t);
796		cv_broadcast(&p->p_pwait);
797		PROC_UNLOCK(t);
798		sx_xunlock(&proctree_lock);
799		return;
800	}
801
802	/*
803	 * Remove other references to this process to ensure we have an
804	 * exclusive reference.
805	 */
806	sx_xlock(&allproc_lock);
807	LIST_REMOVE(p, p_list);	/* off zombproc */
808	sx_xunlock(&allproc_lock);
809	LIST_REMOVE(p, p_sibling);
810	PROC_LOCK(p);
811	clear_orphan(p);
812	PROC_UNLOCK(p);
813	leavepgrp(p);
814#ifdef PROCDESC
815	if (p->p_procdesc != NULL)
816		procdesc_reap(p);
817#endif
818	sx_xunlock(&proctree_lock);
819
820	/*
821	 * As a side effect of this lock, we know that all other writes to
822	 * this proc are visible now, so no more locking is needed for p.
823	 */
824	PROC_LOCK(p);
825	p->p_xstat = 0;		/* XXX: why? */
826	PROC_UNLOCK(p);
827	PROC_LOCK(q);
828	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
829	PROC_UNLOCK(q);
830
831	/*
832	 * Decrement the count of procs running with this uid.
833	 */
834	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
835
836	/*
837	 * Destroy resource accounting information associated with the process.
838	 */
839#ifdef RACCT
840	PROC_LOCK(p);
841	racct_sub(p, RACCT_NPROC, 1);
842	PROC_UNLOCK(p);
843#endif
844	racct_proc_exit(p);
845
846	/*
847	 * Free credentials, arguments, and sigacts.
848	 */
849	crfree(p->p_ucred);
850	p->p_ucred = NULL;
851	pargs_drop(p->p_args);
852	p->p_args = NULL;
853	sigacts_free(p->p_sigacts);
854	p->p_sigacts = NULL;
855
856	/*
857	 * Do any thread-system specific cleanups.
858	 */
859	thread_wait(p);
860
861	/*
862	 * Give vm and machine-dependent layer a chance to free anything that
863	 * cpu_exit couldn't release while still running in process context.
864	 */
865	vm_waitproc(p);
866#ifdef MAC
867	mac_proc_destroy(p);
868#endif
869	KASSERT(FIRST_THREAD_IN_PROC(p),
870	    ("proc_reap: no residual thread!"));
871	uma_zfree(proc_zone, p);
872	sx_xlock(&allproc_lock);
873	nprocs--;
874	sx_xunlock(&allproc_lock);
875}
876
877static int
878proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
879    int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo)
880{
881	struct proc *q;
882	struct rusage *rup;
883
884	sx_assert(&proctree_lock, SA_XLOCKED);
885
886	q = td->td_proc;
887	PROC_LOCK(p);
888
889	switch (idtype) {
890	case P_ALL:
891		break;
892	case P_PID:
893		if (p->p_pid != (pid_t)id) {
894			PROC_UNLOCK(p);
895			return (0);
896		}
897		break;
898	case P_PGID:
899		if (p->p_pgid != (pid_t)id) {
900			PROC_UNLOCK(p);
901			return (0);
902		}
903		break;
904	case P_SID:
905		if (p->p_session->s_sid != (pid_t)id) {
906			PROC_UNLOCK(p);
907			return (0);
908		}
909		break;
910	case P_UID:
911		if (p->p_ucred->cr_uid != (uid_t)id) {
912			PROC_UNLOCK(p);
913			return (0);
914		}
915		break;
916	case P_GID:
917		if (p->p_ucred->cr_gid != (gid_t)id) {
918			PROC_UNLOCK(p);
919			return (0);
920		}
921		break;
922	case P_JAILID:
923		if (p->p_ucred->cr_prison == NULL ||
924		    (p->p_ucred->cr_prison->pr_id != (int)id)) {
925			PROC_UNLOCK(p);
926			return (0);
927		}
928		break;
929	/*
930	 * It seems that the thread structures get zeroed out
931	 * at process exit.  This makes it impossible to
932	 * support P_SETID, P_CID or P_CPUID.
933	 */
934	default:
935		PROC_UNLOCK(p);
936		return (0);
937	}
938
939	if (p_canwait(td, p)) {
940		PROC_UNLOCK(p);
941		return (0);
942	}
943
944	if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
945		PROC_UNLOCK(p);
946		return (0);
947	}
948
949	/*
950	 * This special case handles a kthread spawned by linux_clone
951	 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
952	 * functions need to be able to distinguish between waiting
953	 * on a process and waiting on a thread.  It is a thread if
954	 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
955	 * signifies we want to wait for threads and not processes.
956	 */
957	if ((p->p_sigparent != SIGCHLD) ^
958	    ((options & WLINUXCLONE) != 0)) {
959		PROC_UNLOCK(p);
960		return (0);
961	}
962
963	PROC_SLOCK(p);
964
965	if (siginfo != NULL) {
966		bzero(siginfo, sizeof(*siginfo));
967		siginfo->si_errno = 0;
968
969		/*
970		 * SUSv4 requires that the si_signo value is always
971		 * SIGCHLD. Obey it despite the rfork(2) interface
972		 * allows to request other signal for child exit
973		 * notification.
974		 */
975		siginfo->si_signo = SIGCHLD;
976
977		/*
978		 *  This is still a rough estimate.  We will fix the
979		 *  cases TRAPPED, STOPPED, and CONTINUED later.
980		 */
981		if (WCOREDUMP(p->p_xstat)) {
982			siginfo->si_code = CLD_DUMPED;
983			siginfo->si_status = WTERMSIG(p->p_xstat);
984		} else if (WIFSIGNALED(p->p_xstat)) {
985			siginfo->si_code = CLD_KILLED;
986			siginfo->si_status = WTERMSIG(p->p_xstat);
987		} else {
988			siginfo->si_code = CLD_EXITED;
989			siginfo->si_status = WEXITSTATUS(p->p_xstat);
990		}
991
992		siginfo->si_pid = p->p_pid;
993		siginfo->si_uid = p->p_ucred->cr_uid;
994
995		/*
996		 * The si_addr field would be useful additional
997		 * detail, but apparently the PC value may be lost
998		 * when we reach this point.  bzero() above sets
999		 * siginfo->si_addr to NULL.
1000		 */
1001	}
1002
1003	/*
1004	 * There should be no reason to limit resources usage info to
1005	 * exited processes only.  A snapshot about any resources used
1006	 * by a stopped process may be exactly what is needed.
1007	 */
1008	if (wrusage != NULL) {
1009		rup = &wrusage->wru_self;
1010		*rup = p->p_ru;
1011		calcru(p, &rup->ru_utime, &rup->ru_stime);
1012
1013		rup = &wrusage->wru_children;
1014		*rup = p->p_stats->p_cru;
1015		calccru(p, &rup->ru_utime, &rup->ru_stime);
1016	}
1017
1018	if (p->p_state == PRS_ZOMBIE) {
1019		proc_reap(td, p, status, options);
1020		return (-1);
1021	}
1022	PROC_SUNLOCK(p);
1023	PROC_UNLOCK(p);
1024	return (1);
1025}
1026
1027int
1028kern_wait(struct thread *td, pid_t pid, int *status, int options,
1029    struct rusage *rusage)
1030{
1031	struct __wrusage wru, *wrup;
1032	idtype_t idtype;
1033	id_t id;
1034	int ret;
1035
1036	/*
1037	 * Translate the special pid values into the (idtype, pid)
1038	 * pair for kern_wait6.  The WAIT_MYPGRP case is handled by
1039	 * kern_wait6() on its own.
1040	 */
1041	if (pid == WAIT_ANY) {
1042		idtype = P_ALL;
1043		id = 0;
1044	} else if (pid < 0) {
1045		idtype = P_PGID;
1046		id = (id_t)-pid;
1047	} else {
1048		idtype = P_PID;
1049		id = (id_t)pid;
1050	}
1051
1052	if (rusage != NULL)
1053		wrup = &wru;
1054	else
1055		wrup = NULL;
1056
1057	/*
1058	 * For backward compatibility we implicitly add flags WEXITED
1059	 * and WTRAPPED here.
1060	 */
1061	options |= WEXITED | WTRAPPED;
1062	ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1063	if (rusage != NULL)
1064		*rusage = wru.wru_self;
1065	return (ret);
1066}
1067
1068int
1069kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1070    int options, struct __wrusage *wrusage, siginfo_t *siginfo)
1071{
1072	struct proc *p, *q;
1073	int error, nfound, ret;
1074
1075	AUDIT_ARG_VALUE((int)idtype);	/* XXX - This is likely wrong! */
1076	AUDIT_ARG_PID((pid_t)id);	/* XXX - This may be wrong! */
1077	AUDIT_ARG_VALUE(options);
1078
1079	q = td->td_proc;
1080
1081	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1082		PROC_LOCK(q);
1083		id = (id_t)q->p_pgid;
1084		PROC_UNLOCK(q);
1085		idtype = P_PGID;
1086	}
1087
1088	/* If we don't know the option, just return. */
1089	if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1090	    WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
1091		return (EINVAL);
1092	if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1093		/*
1094		 * We will be unable to find any matching processes,
1095		 * because there are no known events to look for.
1096		 * Prefer to return error instead of blocking
1097		 * indefinitely.
1098		 */
1099		return (EINVAL);
1100	}
1101
1102loop:
1103	if (q->p_flag & P_STATCHILD) {
1104		PROC_LOCK(q);
1105		q->p_flag &= ~P_STATCHILD;
1106		PROC_UNLOCK(q);
1107	}
1108	nfound = 0;
1109	sx_xlock(&proctree_lock);
1110	LIST_FOREACH(p, &q->p_children, p_sibling) {
1111		ret = proc_to_reap(td, p, idtype, id, status, options,
1112		    wrusage, siginfo);
1113		if (ret == 0)
1114			continue;
1115		else if (ret == 1)
1116			nfound++;
1117		else
1118			return (0);
1119
1120		PROC_LOCK(p);
1121		PROC_SLOCK(p);
1122
1123		if ((options & WTRAPPED) != 0 &&
1124		    (p->p_flag & P_TRACED) != 0 &&
1125		    (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 &&
1126		    (p->p_suspcount == p->p_numthreads) &&
1127		    ((p->p_flag & P_WAITED) == 0)) {
1128			PROC_SUNLOCK(p);
1129			if ((options & WNOWAIT) == 0)
1130				p->p_flag |= P_WAITED;
1131			sx_xunlock(&proctree_lock);
1132			td->td_retval[0] = p->p_pid;
1133
1134			if (status != NULL)
1135				*status = W_STOPCODE(p->p_xstat);
1136			if (siginfo != NULL) {
1137				siginfo->si_status = p->p_xstat;
1138				siginfo->si_code = CLD_TRAPPED;
1139			}
1140			if ((options & WNOWAIT) == 0) {
1141				PROC_LOCK(q);
1142				sigqueue_take(p->p_ksi);
1143				PROC_UNLOCK(q);
1144			}
1145
1146			PROC_UNLOCK(p);
1147			return (0);
1148		}
1149		if ((options & WUNTRACED) != 0 &&
1150		    (p->p_flag & P_STOPPED_SIG) != 0 &&
1151		    (p->p_suspcount == p->p_numthreads) &&
1152		    ((p->p_flag & P_WAITED) == 0)) {
1153			PROC_SUNLOCK(p);
1154			if ((options & WNOWAIT) == 0)
1155				p->p_flag |= P_WAITED;
1156			sx_xunlock(&proctree_lock);
1157			td->td_retval[0] = p->p_pid;
1158
1159			if (status != NULL)
1160				*status = W_STOPCODE(p->p_xstat);
1161			if (siginfo != NULL) {
1162				siginfo->si_status = p->p_xstat;
1163				siginfo->si_code = CLD_STOPPED;
1164			}
1165			if ((options & WNOWAIT) == 0) {
1166				PROC_LOCK(q);
1167				sigqueue_take(p->p_ksi);
1168				PROC_UNLOCK(q);
1169			}
1170
1171			PROC_UNLOCK(p);
1172			return (0);
1173		}
1174		PROC_SUNLOCK(p);
1175		if ((options & WCONTINUED) != 0 &&
1176		    (p->p_flag & P_CONTINUED) != 0) {
1177			sx_xunlock(&proctree_lock);
1178			td->td_retval[0] = p->p_pid;
1179			if ((options & WNOWAIT) == 0) {
1180				p->p_flag &= ~P_CONTINUED;
1181				PROC_LOCK(q);
1182				sigqueue_take(p->p_ksi);
1183				PROC_UNLOCK(q);
1184			}
1185			PROC_UNLOCK(p);
1186
1187			if (status != NULL)
1188				*status = SIGCONT;
1189			if (siginfo != NULL) {
1190				siginfo->si_status = SIGCONT;
1191				siginfo->si_code = CLD_CONTINUED;
1192			}
1193			return (0);
1194		}
1195		PROC_UNLOCK(p);
1196	}
1197
1198	/*
1199	 * Look in the orphans list too, to allow the parent to
1200	 * collect it's child exit status even if child is being
1201	 * debugged.
1202	 *
1203	 * Debugger detaches from the parent upon successful
1204	 * switch-over from parent to child.  At this point due to
1205	 * re-parenting the parent loses the child to debugger and a
1206	 * wait4(2) call would report that it has no children to wait
1207	 * for.  By maintaining a list of orphans we allow the parent
1208	 * to successfully wait until the child becomes a zombie.
1209	 */
1210	LIST_FOREACH(p, &q->p_orphans, p_orphan) {
1211		ret = proc_to_reap(td, p, idtype, id, status, options,
1212		    wrusage, siginfo);
1213		if (ret == 0)
1214			continue;
1215		else if (ret == 1)
1216			nfound++;
1217		else
1218			return (0);
1219	}
1220	if (nfound == 0) {
1221		sx_xunlock(&proctree_lock);
1222		return (ECHILD);
1223	}
1224	if (options & WNOHANG) {
1225		sx_xunlock(&proctree_lock);
1226		td->td_retval[0] = 0;
1227		return (0);
1228	}
1229	PROC_LOCK(q);
1230	sx_xunlock(&proctree_lock);
1231	if (q->p_flag & P_STATCHILD) {
1232		q->p_flag &= ~P_STATCHILD;
1233		error = 0;
1234	} else
1235		error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
1236	PROC_UNLOCK(q);
1237	if (error)
1238		return (error);
1239	goto loop;
1240}
1241
1242/*
1243 * Make process 'parent' the new parent of process 'child'.
1244 * Must be called with an exclusive hold of proctree lock.
1245 */
1246void
1247proc_reparent(struct proc *child, struct proc *parent)
1248{
1249
1250	sx_assert(&proctree_lock, SX_XLOCKED);
1251	PROC_LOCK_ASSERT(child, MA_OWNED);
1252	if (child->p_pptr == parent)
1253		return;
1254
1255	PROC_LOCK(child->p_pptr);
1256	sigqueue_take(child->p_ksi);
1257	PROC_UNLOCK(child->p_pptr);
1258	LIST_REMOVE(child, p_sibling);
1259	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1260
1261	clear_orphan(child);
1262	if (child->p_flag & P_TRACED) {
1263		LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, p_orphan);
1264		child->p_flag |= P_ORPHAN;
1265	}
1266
1267	child->p_pptr = parent;
1268}
1269