xref: /freebsd/sys/kern/kern_procctl.c (revision 85e449cfcfdf3a3ea99bae7fed9f3cd436eb21ec)
1 /*-
2  * Copyright (c) 2014 John Baldwin
3  * Copyright (c) 2014, 2016 The FreeBSD Foundation
4  *
5  * Portions of this software were developed by Konstantin Belousov
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_ktrace.h"
31 
32 #include <sys/param.h>
33 #include <sys/_unrhdr.h>
34 #include <sys/systm.h>
35 #include <sys/capsicum.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mman.h>
39 #include <sys/mutex.h>
40 #include <sys/priv.h>
41 #include <sys/proc.h>
42 #include <sys/procctl.h>
43 #include <sys/sx.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysproto.h>
46 #include <sys/wait.h>
47 
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
52 #include <vm/uma.h>
53 
54 static int
55 protect_setchild(struct thread *td, struct proc *p, int flags)
56 {
57 
58 	PROC_LOCK_ASSERT(p, MA_OWNED);
59 	if (p->p_flag & P_SYSTEM || p_cansched(td, p) != 0)
60 		return (0);
61 	if (flags & PPROT_SET) {
62 		p->p_flag |= P_PROTECTED;
63 		if (flags & PPROT_INHERIT)
64 			p->p_flag2 |= P2_INHERIT_PROTECTED;
65 	} else {
66 		p->p_flag &= ~P_PROTECTED;
67 		p->p_flag2 &= ~P2_INHERIT_PROTECTED;
68 	}
69 	return (1);
70 }
71 
72 static int
73 protect_setchildren(struct thread *td, struct proc *top, int flags)
74 {
75 	struct proc *p;
76 	int ret;
77 
78 	p = top;
79 	ret = 0;
80 	sx_assert(&proctree_lock, SX_LOCKED);
81 	for (;;) {
82 		ret |= protect_setchild(td, p, flags);
83 		PROC_UNLOCK(p);
84 		/*
85 		 * If this process has children, descend to them next,
86 		 * otherwise do any siblings, and if done with this level,
87 		 * follow back up the tree (but not past top).
88 		 */
89 		if (!LIST_EMPTY(&p->p_children))
90 			p = LIST_FIRST(&p->p_children);
91 		else for (;;) {
92 			if (p == top) {
93 				PROC_LOCK(p);
94 				return (ret);
95 			}
96 			if (LIST_NEXT(p, p_sibling)) {
97 				p = LIST_NEXT(p, p_sibling);
98 				break;
99 			}
100 			p = p->p_pptr;
101 		}
102 		PROC_LOCK(p);
103 	}
104 }
105 
106 static int
107 protect_set(struct thread *td, struct proc *p, void *data)
108 {
109 	int error, flags, ret;
110 
111 	flags = *(int *)data;
112 	switch (PPROT_OP(flags)) {
113 	case PPROT_SET:
114 	case PPROT_CLEAR:
115 		break;
116 	default:
117 		return (EINVAL);
118 	}
119 
120 	if ((PPROT_FLAGS(flags) & ~(PPROT_DESCEND | PPROT_INHERIT)) != 0)
121 		return (EINVAL);
122 
123 	error = priv_check(td, PRIV_VM_MADV_PROTECT);
124 	if (error)
125 		return (error);
126 
127 	if (flags & PPROT_DESCEND)
128 		ret = protect_setchildren(td, p, flags);
129 	else
130 		ret = protect_setchild(td, p, flags);
131 	if (ret == 0)
132 		return (EPERM);
133 	return (0);
134 }
135 
136 static struct proc *
137 get_reaper_or_p(struct proc *p)
138 {
139 	sx_assert(&proctree_lock, SX_LOCKED);
140 	return ((p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p);
141 }
142 
143 static int
144 reap_acquire(struct thread *td, struct proc *p, void *data __unused)
145 {
146 
147 	sx_assert(&proctree_lock, SX_XLOCKED);
148 	if (p != td->td_proc)
149 		return (EPERM);
150 	if ((p->p_treeflag & P_TREE_REAPER) != 0)
151 		return (EBUSY);
152 	p->p_treeflag |= P_TREE_REAPER;
153 	/*
154 	 * We do not reattach existing children and the whole tree
155 	 * under them to us, since p->p_reaper already seen them.
156 	 */
157 	return (0);
158 }
159 
160 static int
161 reap_release(struct thread *td, struct proc *p, void *data __unused)
162 {
163 
164 	sx_assert(&proctree_lock, SX_XLOCKED);
165 	if (p != td->td_proc)
166 		return (EPERM);
167 	if (p == initproc)
168 		return (EINVAL);
169 	if ((p->p_treeflag & P_TREE_REAPER) == 0)
170 		return (EINVAL);
171 	reaper_abandon_children(p, false);
172 	return (0);
173 }
174 
175 static int
176 reap_status(struct thread *td, struct proc *p, void *data)
177 {
178 	struct proc *reap, *p2, *first_p;
179 	struct procctl_reaper_status *rs;
180 
181 	rs = data;
182 	sx_assert(&proctree_lock, SX_LOCKED);
183 	reap = get_reaper_or_p(p);
184 	if (reap == p)
185 		rs->rs_flags |= REAPER_STATUS_OWNED;
186 	if (reap == initproc)
187 		rs->rs_flags |= REAPER_STATUS_REALINIT;
188 	rs->rs_reaper = reap->p_pid;
189 	rs->rs_descendants = 0;
190 	rs->rs_children = 0;
191 	if (!LIST_EMPTY(&reap->p_reaplist)) {
192 		first_p = LIST_FIRST(&reap->p_children);
193 		if (first_p == NULL)
194 			first_p = LIST_FIRST(&reap->p_reaplist);
195 		rs->rs_pid = first_p->p_pid;
196 		LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
197 			if (proc_realparent(p2) == reap)
198 				rs->rs_children++;
199 			rs->rs_descendants++;
200 		}
201 	} else {
202 		rs->rs_pid = -1;
203 	}
204 	return (0);
205 }
206 
207 static int
208 reap_getpids_count(struct proc **reapp, struct proc *p,
209     const struct procctl_reaper_pids *rp)
210 {
211 	struct proc *reap, *p2;
212 	int n;
213 
214 	sx_assert(&proctree_lock, SX_LOCKED);
215 
216 	reap = get_reaper_or_p(p);
217 	n = 0;
218 	LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling)
219 		n++;
220 	if (rp->rp_count < n)
221 		n = rp->rp_count;
222 	*reapp = reap;
223 	return (n);
224 }
225 
226 static int
227 reap_getpids(struct thread *td, struct proc *p, void *data)
228 {
229 	struct proc *reap, *p2;
230 	struct procctl_reaper_pidinfo *pi, *pip;
231 	struct procctl_reaper_pids *rp;
232 	u_int i, n, n1;
233 	int error;
234 
235 	rp = data;
236 	sx_assert(&proctree_lock, SX_LOCKED);
237 	PROC_UNLOCK(p);
238 	i = 0;
239 	for (;;) {
240 		n1 = reap_getpids_count(&reap, p, rp);
241 		sx_unlock(&proctree_lock);
242 		pi = mallocarray(n1, sizeof(*pi), M_TEMP, M_WAITOK);
243 		sx_slock(&proctree_lock);
244 		n = reap_getpids_count(&reap, p, rp);
245 		if (n <= n1)
246 			break;
247 		free(pi, M_TEMP);
248 	}
249 
250 	LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
251 		if (i == n)
252 			break;
253 		pip = &pi[i];
254 		bzero(pip, sizeof(*pip));
255 		pip->pi_pid = p2->p_pid;
256 		pip->pi_subtree = p2->p_reapsubtree;
257 		pip->pi_flags = REAPER_PIDINFO_VALID;
258 		if (proc_realparent(p2) == reap)
259 			pip->pi_flags |= REAPER_PIDINFO_CHILD;
260 		if ((p2->p_treeflag & P_TREE_REAPER) != 0)
261 			pip->pi_flags |= REAPER_PIDINFO_REAPER;
262 		if ((p2->p_flag & P_STOPPED) != 0)
263 			pip->pi_flags |= REAPER_PIDINFO_STOPPED;
264 		if (p2->p_state == PRS_ZOMBIE)
265 			pip->pi_flags |= REAPER_PIDINFO_ZOMBIE;
266 		else if ((p2->p_flag & P_WEXIT) != 0)
267 			pip->pi_flags |= REAPER_PIDINFO_EXITING;
268 		i++;
269 	}
270 	sx_sunlock(&proctree_lock);
271 	error = copyout(pi, rp->rp_pids, i * sizeof(*pi));
272 	free(pi, M_TEMP);
273 	sx_slock(&proctree_lock);
274 	PROC_LOCK(p);
275 	return (error);
276 }
277 
278 struct reap_kill_proc_work {
279 	struct ucred *cr;
280 	struct proc *target;
281 	ksiginfo_t *ksi;
282 	struct procctl_reaper_kill *rk;
283 	int *error;
284 };
285 
286 static void
287 reap_kill_proc_locked(struct reap_kill_proc_work *w)
288 {
289 	int error;
290 
291 	PROC_LOCK_ASSERT(w->target, MA_OWNED);
292 	PROC_ASSERT_HELD(w->target);
293 
294 	error = cr_cansignal(w->cr, w->target, w->rk->rk_sig);
295 	if (error != 0) {
296 		/*
297 		 * Hide ESRCH errors to ensure that this function
298 		 * cannot be used as an oracle for process visibility.
299 		 */
300 		if (error != ESRCH && *w->error == 0) {
301 			w->rk->rk_fpid = w->target->p_pid;
302 			*w->error = error;
303 		}
304 		return;
305 	}
306 
307 	(void)pksignal(w->target, w->rk->rk_sig, w->ksi);
308 	w->rk->rk_killed++;
309 	*w->error = error;
310 }
311 
312 static void
313 reap_kill_proc(struct reap_kill_proc_work *w, bool *proctree_dropped)
314 {
315 	struct pgrp *pgrp;
316 	int xlocked;
317 
318 	sx_assert(&proctree_lock, SX_LOCKED);
319 	xlocked = sx_xlocked(&proctree_lock);
320 	PROC_LOCK_ASSERT(w->target, MA_OWNED);
321 	PROC_ASSERT_HELD(w->target);
322 
323 	/* Sync with forks. */
324 	for (;;) {
325 		/*
326 		 * Short-circuit handling of the exiting process, do
327 		 * not wait for it to single-thread (hold prevents it
328 		 * from exiting further).  This avoids
329 		 * locking pg_killsx for it, and reduces the
330 		 * proctree_lock contention.
331 		 */
332 		if ((w->target->p_flag2 & P2_WEXIT) != 0)
333 			return;
334 
335 		pgrp = w->target->p_pgrp;
336 		if (pgrp == NULL || sx_try_xlock(&pgrp->pg_killsx))
337 			break;
338 
339 		PROC_UNLOCK(w->target);
340 		sx_unlock(&proctree_lock);
341 		/* This is safe because pgrp zone is nofree. */
342 		sx_xlock(&pgrp->pg_killsx);
343 		sx_xunlock(&pgrp->pg_killsx);
344 		*proctree_dropped = true;
345 		if (xlocked)
346 			sx_xlock(&proctree_lock);
347 		else
348 			sx_slock(&proctree_lock);
349 		PROC_LOCK(w->target);
350 	}
351 
352 	reap_kill_proc_locked(w);
353 
354 	if (pgrp != NULL)
355 		sx_xunlock(&pgrp->pg_killsx);
356 }
357 
358 struct reap_kill_tracker {
359 	struct proc *parent;
360 	TAILQ_ENTRY(reap_kill_tracker) link;
361 };
362 
363 TAILQ_HEAD(reap_kill_tracker_head, reap_kill_tracker);
364 
365 static void
366 reap_kill_sched(struct reap_kill_tracker_head *tracker, struct proc *p2)
367 {
368 	struct reap_kill_tracker *t;
369 
370 	PROC_TREE_REF(p2);
371 	t = malloc(sizeof(struct reap_kill_tracker), M_TEMP, M_WAITOK);
372 	t->parent = p2;
373 	TAILQ_INSERT_TAIL(tracker, t, link);
374 }
375 
376 static void
377 reap_kill_sched_free(struct reap_kill_tracker *t)
378 {
379 	PROC_TREE_UNREF(t->parent);
380 	free(t, M_TEMP);
381 }
382 
383 static void
384 reap_kill_children(struct thread *td, struct proc *reaper,
385     struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error)
386 {
387 	struct proc *p2;
388 	int error1;
389 
390 	LIST_FOREACH(p2, &reaper->p_children, p_sibling) {
391 		PROC_LOCK(p2);
392 		if ((p2->p_flag2 & P2_WEXIT) == 0) {
393 			error1 = p_cansignal(td, p2, rk->rk_sig);
394 			if (error1 != 0) {
395 				if (*error == ESRCH) {
396 					rk->rk_fpid = p2->p_pid;
397 					*error = error1;
398 				}
399 
400 				/*
401 				 * Do not end the loop on error,
402 				 * signal everything we can.
403 				 */
404 			} else {
405 				(void)pksignal(p2, rk->rk_sig, ksi);
406 				rk->rk_killed++;
407 			}
408 		}
409 		PROC_UNLOCK(p2);
410 	}
411 }
412 
413 static bool
414 reap_kill_subtree_once(struct thread *td, struct proc *p, struct proc **reaperp,
415     struct unrhdr *pids, struct reap_kill_proc_work *w)
416 {
417 	struct reap_kill_tracker_head tracker;
418 	struct reap_kill_tracker *t;
419 	struct proc *p2, *reaper, *old_reaper;
420 	bool proctree_dropped, res;
421 
422 	res = false;
423 	TAILQ_INIT(&tracker);
424 	reaper = *reaperp;
425 	reap_kill_sched(&tracker, reaper);
426 	while ((t = TAILQ_FIRST(&tracker)) != NULL) {
427 		TAILQ_REMOVE(&tracker, t, link);
428 
429 again:
430 		/*
431 		 * Since reap_kill_proc() drops proctree_lock sx, it
432 		 * is possible that the tracked reaper is no longer.
433 		 * In this case the subtree is reparented to the new
434 		 * reaper, which should handle it.
435 		 */
436 		if ((t->parent->p_treeflag & P_TREE_REAPER) == 0) {
437 			reap_kill_sched_free(t);
438 			res = true;
439 			continue;
440 		}
441 
442 		LIST_FOREACH(p2, &t->parent->p_reaplist, p_reapsibling) {
443 			if (t->parent == reaper &&
444 			    (w->rk->rk_flags & REAPER_KILL_SUBTREE) != 0 &&
445 			    p2->p_reapsubtree != w->rk->rk_subtree)
446 				continue;
447 			if ((p2->p_treeflag & P_TREE_REAPER) != 0)
448 				reap_kill_sched(&tracker, p2);
449 
450 			/*
451 			 * Handle possible pid reuse.  If we recorded
452 			 * p2 as killed but its p_flag2 does not
453 			 * confirm it, that means that the process
454 			 * terminated and its id was reused by other
455 			 * process in the reaper subtree.
456 			 *
457 			 * Unlocked read of p2->p_flag2 is fine, it is
458 			 * our thread that set the tested flag.
459 			 */
460 			if (alloc_unr_specific(pids, p2->p_pid) != p2->p_pid &&
461 			    (atomic_load_int(&p2->p_flag2) &
462 			    (P2_REAPKILLED | P2_WEXIT)) != 0)
463 				continue;
464 
465 			proctree_dropped = false;
466 			PROC_LOCK(p2);
467 			if ((p2->p_flag2 & P2_WEXIT) == 0) {
468 				_PHOLD(p2);
469 
470 				/*
471 				 * sapblk ensures that only one thread
472 				 * in the system sets this flag.
473 				 */
474 				p2->p_flag2 |= P2_REAPKILLED;
475 
476 				w->target = p2;
477 				reap_kill_proc(w, &proctree_dropped);
478 				_PRELE(p2);
479 			}
480 			PROC_UNLOCK(p2);
481 			res = true;
482 			if (proctree_dropped) {
483 				old_reaper = reaper;
484 				reaper = get_reaper_or_p(p);
485 				if (old_reaper != reaper) {
486 					*reaperp = reaper;
487 					PROC_TREE_REF(reaper);
488 					PROC_TREE_UNREF(old_reaper);
489 					reap_kill_sched(&tracker, reaper);
490 					/*
491 					 * Already scheduled kill
492 					 * actions should be kept on
493 					 * the schedule, the processes
494 					 * are inherited by the new
495 					 * reaper.
496 					 */
497 				}
498 				goto again;
499 			}
500 		}
501 		reap_kill_sched_free(t);
502 	}
503 	return (res);
504 }
505 
506 static void
507 reap_kill_subtree(struct thread *td, struct proc *p, struct proc **reaperp,
508     struct reap_kill_proc_work *w)
509 {
510 	struct unrhdr pids;
511 	void *ihandle;
512 	struct proc *p2;
513 	int pid;
514 
515 	/*
516 	 * pids records processes which were already signalled, to
517 	 * avoid doubling signals to them if iteration needs to be
518 	 * repeated.
519 	 */
520 	init_unrhdr(&pids, 1, PID_MAX, UNR_NO_MTX);
521 	PROC_LOCK(td->td_proc);
522 	if ((td->td_proc->p_flag2 & P2_WEXIT) != 0) {
523 		PROC_UNLOCK(td->td_proc);
524 		goto out;
525 	}
526 	PROC_UNLOCK(td->td_proc);
527 	while (reap_kill_subtree_once(td, p, reaperp, &pids, w))
528 	       ;
529 
530 	ihandle = create_iter_unr(&pids);
531 	while ((pid = next_iter_unr(ihandle)) != -1) {
532 		p2 = pfind(pid);
533 		if (p2 != NULL) {
534 			p2->p_flag2 &= ~P2_REAPKILLED;
535 			PROC_UNLOCK(p2);
536 		}
537 	}
538 	free_iter_unr(ihandle);
539 
540 out:
541 	clean_unrhdr(&pids);
542 	clear_unrhdr(&pids);
543 }
544 
545 static bool
546 reap_kill_sapblk(struct thread *td __unused, void *data)
547 {
548 	struct procctl_reaper_kill *rk;
549 
550 	rk = data;
551 	return ((rk->rk_flags & REAPER_KILL_CHILDREN) == 0);
552 }
553 
554 static int
555 reap_kill(struct thread *td, struct proc *p, void *data)
556 {
557 	struct reap_kill_proc_work w;
558 	struct proc *reaper;
559 	ksiginfo_t ksi;
560 	struct procctl_reaper_kill *rk;
561 	int error;
562 
563 	rk = data;
564 	sx_assert(&proctree_lock, SX_LOCKED);
565 	if (CAP_TRACING(td))
566 		ktrcapfail(CAPFAIL_SIGNAL, &rk->rk_sig);
567 	if (IN_CAPABILITY_MODE(td))
568 		return (ECAPMODE);
569 	if (rk->rk_sig <= 0 || rk->rk_sig > _SIG_MAXSIG ||
570 	    (rk->rk_flags & ~(REAPER_KILL_CHILDREN |
571 	    REAPER_KILL_SUBTREE)) != 0 || (rk->rk_flags &
572 	    (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE)) ==
573 	    (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE))
574 		return (EINVAL);
575 	PROC_UNLOCK(p);
576 	reaper = get_reaper_or_p(p);
577 
578 	ksiginfo_init(&ksi);
579 	ksi.ksi_signo = rk->rk_sig;
580 	ksi.ksi_code = SI_USER;
581 	ksi.ksi_pid = td->td_proc->p_pid;
582 	ksi.ksi_uid = td->td_ucred->cr_ruid;
583 	error = ESRCH;
584 	rk->rk_killed = 0;
585 	rk->rk_fpid = -1;
586 	if ((rk->rk_flags & REAPER_KILL_CHILDREN) != 0) {
587 		reap_kill_children(td, reaper, rk, &ksi, &error);
588 	} else {
589 		w.cr = crhold(td->td_ucred);
590 		w.ksi = &ksi;
591 		w.rk = rk;
592 		w.error = &error;
593 		PROC_TREE_REF(reaper);
594 		reap_kill_subtree(td, p, &reaper, &w);
595 		PROC_TREE_UNREF(reaper);
596 		crfree(w.cr);
597 	}
598 	PROC_LOCK(p);
599 	return (error);
600 }
601 
602 static int
603 trace_ctl(struct thread *td, struct proc *p, void *data)
604 {
605 	int state;
606 
607 	PROC_LOCK_ASSERT(p, MA_OWNED);
608 	state = *(int *)data;
609 
610 	/*
611 	 * Ktrace changes p_traceflag from or to zero under the
612 	 * process lock, so the test does not need to acquire ktrace
613 	 * mutex.
614 	 */
615 	if ((p->p_flag & P_TRACED) != 0 || p->p_traceflag != 0)
616 		return (EBUSY);
617 
618 	switch (state) {
619 	case PROC_TRACE_CTL_ENABLE:
620 		if (td->td_proc != p)
621 			return (EPERM);
622 		p->p_flag2 &= ~(P2_NOTRACE | P2_NOTRACE_EXEC);
623 		break;
624 	case PROC_TRACE_CTL_DISABLE_EXEC:
625 		p->p_flag2 |= P2_NOTRACE_EXEC | P2_NOTRACE;
626 		break;
627 	case PROC_TRACE_CTL_DISABLE:
628 		if ((p->p_flag2 & P2_NOTRACE_EXEC) != 0) {
629 			KASSERT((p->p_flag2 & P2_NOTRACE) != 0,
630 			    ("dandling P2_NOTRACE_EXEC"));
631 			if (td->td_proc != p)
632 				return (EPERM);
633 			p->p_flag2 &= ~P2_NOTRACE_EXEC;
634 		} else {
635 			p->p_flag2 |= P2_NOTRACE;
636 		}
637 		break;
638 	default:
639 		return (EINVAL);
640 	}
641 	return (0);
642 }
643 
644 static int
645 trace_status(struct thread *td, struct proc *p, void *data)
646 {
647 	int *status;
648 
649 	status = data;
650 	if ((p->p_flag2 & P2_NOTRACE) != 0) {
651 		KASSERT((p->p_flag & P_TRACED) == 0,
652 		    ("%d traced but tracing disabled", p->p_pid));
653 		*status = -1;
654 	} else if ((p->p_flag & P_TRACED) != 0) {
655 		*status = p->p_pptr->p_pid;
656 	} else {
657 		*status = 0;
658 	}
659 	return (0);
660 }
661 
662 static int
663 trapcap_ctl(struct thread *td, struct proc *p, void *data)
664 {
665 	int state;
666 
667 	PROC_LOCK_ASSERT(p, MA_OWNED);
668 	state = *(int *)data;
669 
670 	switch (state) {
671 	case PROC_TRAPCAP_CTL_ENABLE:
672 		p->p_flag2 |= P2_TRAPCAP;
673 		break;
674 	case PROC_TRAPCAP_CTL_DISABLE:
675 		p->p_flag2 &= ~P2_TRAPCAP;
676 		break;
677 	default:
678 		return (EINVAL);
679 	}
680 	return (0);
681 }
682 
683 static int
684 trapcap_status(struct thread *td, struct proc *p, void *data)
685 {
686 	int *status;
687 
688 	status = data;
689 	*status = (p->p_flag2 & P2_TRAPCAP) != 0 ? PROC_TRAPCAP_CTL_ENABLE :
690 	    PROC_TRAPCAP_CTL_DISABLE;
691 	return (0);
692 }
693 
694 static int
695 no_new_privs_ctl(struct thread *td, struct proc *p, void *data)
696 {
697 	int state;
698 
699 	PROC_LOCK_ASSERT(p, MA_OWNED);
700 	state = *(int *)data;
701 
702 	if (state != PROC_NO_NEW_PRIVS_ENABLE)
703 		return (EINVAL);
704 	p->p_flag2 |= P2_NO_NEW_PRIVS;
705 	return (0);
706 }
707 
708 static int
709 no_new_privs_status(struct thread *td, struct proc *p, void *data)
710 {
711 
712 	*(int *)data = (p->p_flag2 & P2_NO_NEW_PRIVS) != 0 ?
713 	    PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
714 	return (0);
715 }
716 
717 static int
718 protmax_ctl(struct thread *td, struct proc *p, void *data)
719 {
720 	int state;
721 
722 	PROC_LOCK_ASSERT(p, MA_OWNED);
723 	state = *(int *)data;
724 
725 	switch (state) {
726 	case PROC_PROTMAX_FORCE_ENABLE:
727 		p->p_flag2 &= ~P2_PROTMAX_DISABLE;
728 		p->p_flag2 |= P2_PROTMAX_ENABLE;
729 		break;
730 	case PROC_PROTMAX_FORCE_DISABLE:
731 		p->p_flag2 |= P2_PROTMAX_DISABLE;
732 		p->p_flag2 &= ~P2_PROTMAX_ENABLE;
733 		break;
734 	case PROC_PROTMAX_NOFORCE:
735 		p->p_flag2 &= ~(P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE);
736 		break;
737 	default:
738 		return (EINVAL);
739 	}
740 	return (0);
741 }
742 
743 static int
744 protmax_status(struct thread *td, struct proc *p, void *data)
745 {
746 	int d;
747 
748 	switch (p->p_flag2 & (P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE)) {
749 	case 0:
750 		d = PROC_PROTMAX_NOFORCE;
751 		break;
752 	case P2_PROTMAX_ENABLE:
753 		d = PROC_PROTMAX_FORCE_ENABLE;
754 		break;
755 	case P2_PROTMAX_DISABLE:
756 		d = PROC_PROTMAX_FORCE_DISABLE;
757 		break;
758 	}
759 	if (kern_mmap_maxprot(p, PROT_READ) == PROT_READ)
760 		d |= PROC_PROTMAX_ACTIVE;
761 	*(int *)data = d;
762 	return (0);
763 }
764 
765 static int
766 aslr_ctl(struct thread *td, struct proc *p, void *data)
767 {
768 	int state;
769 
770 	PROC_LOCK_ASSERT(p, MA_OWNED);
771 	state = *(int *)data;
772 
773 	switch (state) {
774 	case PROC_ASLR_FORCE_ENABLE:
775 		p->p_flag2 &= ~P2_ASLR_DISABLE;
776 		p->p_flag2 |= P2_ASLR_ENABLE;
777 		break;
778 	case PROC_ASLR_FORCE_DISABLE:
779 		p->p_flag2 |= P2_ASLR_DISABLE;
780 		p->p_flag2 &= ~P2_ASLR_ENABLE;
781 		break;
782 	case PROC_ASLR_NOFORCE:
783 		p->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE);
784 		break;
785 	default:
786 		return (EINVAL);
787 	}
788 	return (0);
789 }
790 
791 static int
792 aslr_status(struct thread *td, struct proc *p, void *data)
793 {
794 	struct vmspace *vm;
795 	int d;
796 
797 	switch (p->p_flag2 & (P2_ASLR_ENABLE | P2_ASLR_DISABLE)) {
798 	case 0:
799 		d = PROC_ASLR_NOFORCE;
800 		break;
801 	case P2_ASLR_ENABLE:
802 		d = PROC_ASLR_FORCE_ENABLE;
803 		break;
804 	case P2_ASLR_DISABLE:
805 		d = PROC_ASLR_FORCE_DISABLE;
806 		break;
807 	}
808 	PROC_UNLOCK(p);
809 	vm = vmspace_acquire_ref(p);
810 	if (vm != NULL) {
811 		if ((vm->vm_map.flags & MAP_ASLR) != 0)
812 			d |= PROC_ASLR_ACTIVE;
813 		vmspace_free(vm);
814 	}
815 	*(int *)data = d;
816 	PROC_LOCK(p);
817 	return (0);
818 }
819 
820 static int
821 stackgap_ctl(struct thread *td, struct proc *p, void *data)
822 {
823 	int state;
824 
825 	PROC_LOCK_ASSERT(p, MA_OWNED);
826 	state = *(int *)data;
827 
828 	if ((state & ~(PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE |
829 	    PROC_STACKGAP_ENABLE_EXEC | PROC_STACKGAP_DISABLE_EXEC)) != 0)
830 		return (EINVAL);
831 	switch (state & (PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE)) {
832 	case PROC_STACKGAP_ENABLE:
833 		if ((p->p_flag2 & P2_STKGAP_DISABLE) != 0)
834 			return (EINVAL);
835 		break;
836 	case PROC_STACKGAP_DISABLE:
837 		p->p_flag2 |= P2_STKGAP_DISABLE;
838 		break;
839 	case 0:
840 		break;
841 	default:
842 		return (EINVAL);
843 	}
844 	switch (state & (PROC_STACKGAP_ENABLE_EXEC |
845 	    PROC_STACKGAP_DISABLE_EXEC)) {
846 	case PROC_STACKGAP_ENABLE_EXEC:
847 		p->p_flag2 &= ~P2_STKGAP_DISABLE_EXEC;
848 		break;
849 	case PROC_STACKGAP_DISABLE_EXEC:
850 		p->p_flag2 |= P2_STKGAP_DISABLE_EXEC;
851 		break;
852 	case 0:
853 		break;
854 	default:
855 		return (EINVAL);
856 	}
857 	return (0);
858 }
859 
860 static int
861 stackgap_status(struct thread *td, struct proc *p, void *data)
862 {
863 	int d;
864 
865 	PROC_LOCK_ASSERT(p, MA_OWNED);
866 
867 	d = (p->p_flag2 & P2_STKGAP_DISABLE) != 0 ? PROC_STACKGAP_DISABLE :
868 	    PROC_STACKGAP_ENABLE;
869 	d |= (p->p_flag2 & P2_STKGAP_DISABLE_EXEC) != 0 ?
870 	    PROC_STACKGAP_DISABLE_EXEC : PROC_STACKGAP_ENABLE_EXEC;
871 	*(int *)data = d;
872 	return (0);
873 }
874 
875 static int
876 wxmap_ctl(struct thread *td, struct proc *p, void *data)
877 {
878 	struct vmspace *vm;
879 	vm_map_t map;
880 	int state;
881 
882 	PROC_LOCK_ASSERT(p, MA_OWNED);
883 	state = *(int *)data;
884 
885 	switch (state) {
886 	case PROC_WX_MAPPINGS_PERMIT:
887 		p->p_flag2 |= P2_WXORX_DISABLE;
888 		PROC_UNLOCK(p);
889 		vm = vmspace_acquire_ref(p);
890 		if (vm != NULL) {
891 			map = &vm->vm_map;
892 			vm_map_lock(map);
893 			map->flags &= ~MAP_WXORX;
894 			vm_map_unlock(map);
895 			vmspace_free(vm);
896 		}
897 		PROC_LOCK(p);
898 		break;
899 	case PROC_WX_MAPPINGS_DISALLOW_EXEC:
900 		p->p_flag2 |= P2_WXORX_ENABLE_EXEC;
901 		break;
902 	default:
903 		return (EINVAL);
904 	}
905 
906 	return (0);
907 }
908 
909 static int
910 wxmap_status(struct thread *td, struct proc *p, void *data)
911 {
912 	struct vmspace *vm;
913 	int d;
914 
915 	PROC_LOCK_ASSERT(p, MA_OWNED);
916 
917 	d = 0;
918 	if ((p->p_flag2 & P2_WXORX_DISABLE) != 0)
919 		d |= PROC_WX_MAPPINGS_PERMIT;
920 	if ((p->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
921 		d |= PROC_WX_MAPPINGS_DISALLOW_EXEC;
922 	PROC_UNLOCK(p);
923 	vm = vmspace_acquire_ref(p);
924 	if (vm != NULL) {
925 		if ((vm->vm_map.flags & MAP_WXORX) != 0)
926 			d |= PROC_WXORX_ENFORCE;
927 		vmspace_free(vm);
928 	}
929 	*(int *)data = d;
930 	PROC_LOCK(p);
931 	return (0);
932 }
933 
934 static int
935 pdeathsig_ctl(struct thread *td, struct proc *p, void *data)
936 {
937 	int signum;
938 
939 	signum = *(int *)data;
940 	if (p != td->td_proc || (signum != 0 && !_SIG_VALID(signum)))
941 		return (EINVAL);
942 	p->p_pdeathsig = signum;
943 	return (0);
944 }
945 
946 static int
947 pdeathsig_status(struct thread *td, struct proc *p, void *data)
948 {
949 	if (p != td->td_proc)
950 		return (EINVAL);
951 	*(int *)data = p->p_pdeathsig;
952 	return (0);
953 }
954 
955 static int
956 logsigexit_ctl(struct thread *td, struct proc *p, void *data)
957 {
958 	int state;
959 
960 	PROC_LOCK_ASSERT(p, MA_OWNED);
961 	state = *(int *)data;
962 
963 	switch (state) {
964 	case PROC_LOGSIGEXIT_CTL_NOFORCE:
965 		p->p_flag2 &= ~(P2_LOGSIGEXIT_CTL | P2_LOGSIGEXIT_ENABLE);
966 		break;
967 	case PROC_LOGSIGEXIT_CTL_FORCE_ENABLE:
968 		p->p_flag2 |= P2_LOGSIGEXIT_CTL | P2_LOGSIGEXIT_ENABLE;
969 		break;
970 	case PROC_LOGSIGEXIT_CTL_FORCE_DISABLE:
971 		p->p_flag2 |= P2_LOGSIGEXIT_CTL;
972 		p->p_flag2 &= ~P2_LOGSIGEXIT_ENABLE;
973 		break;
974 	default:
975 		return (EINVAL);
976 	}
977 	return (0);
978 }
979 
980 static int
981 logsigexit_status(struct thread *td, struct proc *p, void *data)
982 {
983 	int state;
984 
985 	if ((p->p_flag2 & P2_LOGSIGEXIT_CTL) == 0)
986 		state = PROC_LOGSIGEXIT_CTL_NOFORCE;
987 	else if ((p->p_flag2 & P2_LOGSIGEXIT_ENABLE) != 0)
988 		state = PROC_LOGSIGEXIT_CTL_FORCE_ENABLE;
989 	else
990 		state = PROC_LOGSIGEXIT_CTL_FORCE_DISABLE;
991 	*(int *)data = state;
992 	return (0);
993 }
994 
995 enum {
996 	PCTL_SLOCKED,
997 	PCTL_XLOCKED,
998 	PCTL_UNLOCKED,
999 };
1000 
1001 struct procctl_cmd_info {
1002 	int lock_tree;
1003 	bool one_proc : 1;
1004 	bool esrch_is_einval : 1;
1005 	bool copyout_on_error : 1;
1006 	bool no_nonnull_data : 1;
1007 	bool need_candebug : 1;
1008 	int copyin_sz;
1009 	int copyout_sz;
1010 	int (*exec)(struct thread *, struct proc *, void *);
1011 	bool (*sapblk)(struct thread *, void *);
1012 };
1013 static const struct procctl_cmd_info procctl_cmds_info[] = {
1014 	[PROC_SPROTECT] =
1015 	    { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1016 	      .esrch_is_einval = false, .no_nonnull_data = false,
1017 	      .need_candebug = false,
1018 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1019 	      .exec = protect_set, .copyout_on_error = false, },
1020 	[PROC_REAP_ACQUIRE] =
1021 	    { .lock_tree = PCTL_XLOCKED, .one_proc = true,
1022 	      .esrch_is_einval = false, .no_nonnull_data = true,
1023 	      .need_candebug = false,
1024 	      .copyin_sz = 0, .copyout_sz = 0,
1025 	      .exec = reap_acquire, .copyout_on_error = false, },
1026 	[PROC_REAP_RELEASE] =
1027 	    { .lock_tree = PCTL_XLOCKED, .one_proc = true,
1028 	      .esrch_is_einval = false, .no_nonnull_data = true,
1029 	      .need_candebug = false,
1030 	      .copyin_sz = 0, .copyout_sz = 0,
1031 	      .exec = reap_release, .copyout_on_error = false, },
1032 	[PROC_REAP_STATUS] =
1033 	    { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1034 	      .esrch_is_einval = false, .no_nonnull_data = false,
1035 	      .need_candebug = false,
1036 	      .copyin_sz = 0,
1037 	      .copyout_sz = sizeof(struct procctl_reaper_status),
1038 	      .exec = reap_status, .copyout_on_error = false, },
1039 	[PROC_REAP_GETPIDS] =
1040 	    { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1041 	      .esrch_is_einval = false, .no_nonnull_data = false,
1042 	      .need_candebug = false,
1043 	      .copyin_sz = sizeof(struct procctl_reaper_pids),
1044 	      .copyout_sz = 0,
1045 	      .exec = reap_getpids, .copyout_on_error = false, },
1046 	[PROC_REAP_KILL] =
1047 	    { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1048 	      .esrch_is_einval = false, .no_nonnull_data = false,
1049 	      .need_candebug = false,
1050 	      .copyin_sz = sizeof(struct procctl_reaper_kill),
1051 	      .copyout_sz = sizeof(struct procctl_reaper_kill),
1052 	      .exec = reap_kill, .copyout_on_error = true,
1053 	      .sapblk = reap_kill_sapblk, },
1054 	[PROC_TRACE_CTL] =
1055 	    { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1056 	      .esrch_is_einval = false, .no_nonnull_data = false,
1057 	      .need_candebug = true,
1058 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1059 	      .exec = trace_ctl, .copyout_on_error = false, },
1060 	[PROC_TRACE_STATUS] =
1061 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1062 	      .esrch_is_einval = false, .no_nonnull_data = false,
1063 	      .need_candebug = false,
1064 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1065 	      .exec = trace_status, .copyout_on_error = false, },
1066 	[PROC_TRAPCAP_CTL] =
1067 	    { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1068 	      .esrch_is_einval = false, .no_nonnull_data = false,
1069 	      .need_candebug = true,
1070 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1071 	      .exec = trapcap_ctl, .copyout_on_error = false, },
1072 	[PROC_TRAPCAP_STATUS] =
1073 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1074 	      .esrch_is_einval = false, .no_nonnull_data = false,
1075 	      .need_candebug = false,
1076 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1077 	      .exec = trapcap_status, .copyout_on_error = false, },
1078 	[PROC_PDEATHSIG_CTL] =
1079 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1080 	      .esrch_is_einval = true, .no_nonnull_data = false,
1081 	      .need_candebug = false,
1082 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1083 	      .exec = pdeathsig_ctl, .copyout_on_error = false, },
1084 	[PROC_PDEATHSIG_STATUS] =
1085 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1086 	      .esrch_is_einval = true, .no_nonnull_data = false,
1087 	      .need_candebug = false,
1088 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1089 	      .exec = pdeathsig_status, .copyout_on_error = false, },
1090 	[PROC_ASLR_CTL] =
1091 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1092 	      .esrch_is_einval = false, .no_nonnull_data = false,
1093 	      .need_candebug = true,
1094 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1095 	      .exec = aslr_ctl, .copyout_on_error = false, },
1096 	[PROC_ASLR_STATUS] =
1097 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1098 	      .esrch_is_einval = false, .no_nonnull_data = false,
1099 	      .need_candebug = false,
1100 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1101 	      .exec = aslr_status, .copyout_on_error = false, },
1102 	[PROC_PROTMAX_CTL] =
1103 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1104 	      .esrch_is_einval = false, .no_nonnull_data = false,
1105 	      .need_candebug = true,
1106 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1107 	      .exec = protmax_ctl, .copyout_on_error = false, },
1108 	[PROC_PROTMAX_STATUS] =
1109 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1110 	      .esrch_is_einval = false, .no_nonnull_data = false,
1111 	      .need_candebug = false,
1112 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1113 	      .exec = protmax_status, .copyout_on_error = false, },
1114 	[PROC_STACKGAP_CTL] =
1115 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1116 	      .esrch_is_einval = false, .no_nonnull_data = false,
1117 	      .need_candebug = true,
1118 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1119 	      .exec = stackgap_ctl, .copyout_on_error = false, },
1120 	[PROC_STACKGAP_STATUS] =
1121 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1122 	      .esrch_is_einval = false, .no_nonnull_data = false,
1123 	      .need_candebug = false,
1124 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1125 	      .exec = stackgap_status, .copyout_on_error = false, },
1126 	[PROC_NO_NEW_PRIVS_CTL] =
1127 	    { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1128 	      .esrch_is_einval = false, .no_nonnull_data = false,
1129 	      .need_candebug = true,
1130 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1131 	      .exec = no_new_privs_ctl, .copyout_on_error = false, },
1132 	[PROC_NO_NEW_PRIVS_STATUS] =
1133 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1134 	      .esrch_is_einval = false, .no_nonnull_data = false,
1135 	      .need_candebug = false,
1136 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1137 	      .exec = no_new_privs_status, .copyout_on_error = false, },
1138 	[PROC_WXMAP_CTL] =
1139 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1140 	      .esrch_is_einval = false, .no_nonnull_data = false,
1141 	      .need_candebug = true,
1142 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1143 	      .exec = wxmap_ctl, .copyout_on_error = false, },
1144 	[PROC_WXMAP_STATUS] =
1145 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1146 	      .esrch_is_einval = false, .no_nonnull_data = false,
1147 	      .need_candebug = false,
1148 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1149 	      .exec = wxmap_status, .copyout_on_error = false, },
1150 	[PROC_LOGSIGEXIT_CTL] =
1151 	    { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1152 	      .esrch_is_einval = false, .no_nonnull_data = false,
1153 	      .need_candebug = true,
1154 	      .copyin_sz = sizeof(int), .copyout_sz = 0,
1155 	      .exec = logsigexit_ctl, .copyout_on_error = false, },
1156 	[PROC_LOGSIGEXIT_STATUS] =
1157 	    { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1158 	      .esrch_is_einval = false, .no_nonnull_data = false,
1159 	      .need_candebug = false,
1160 	      .copyin_sz = 0, .copyout_sz = sizeof(int),
1161 	      .exec = logsigexit_status, .copyout_on_error = false, },
1162 };
1163 
1164 int
1165 sys_procctl(struct thread *td, struct procctl_args *uap)
1166 {
1167 	union {
1168 		struct procctl_reaper_status rs;
1169 		struct procctl_reaper_pids rp;
1170 		struct procctl_reaper_kill rk;
1171 		int flags;
1172 	} x;
1173 	const struct procctl_cmd_info *cmd_info;
1174 	int error, error1;
1175 
1176 	if (uap->com >= PROC_PROCCTL_MD_MIN)
1177 		return (cpu_procctl(td, uap->idtype, uap->id,
1178 		    uap->com, uap->data));
1179 	if (uap->com <= 0 || uap->com >= nitems(procctl_cmds_info))
1180 		return (EINVAL);
1181 	cmd_info = &procctl_cmds_info[uap->com];
1182 	bzero(&x, sizeof(x));
1183 
1184 	if (cmd_info->copyin_sz > 0) {
1185 		error = copyin(uap->data, &x, cmd_info->copyin_sz);
1186 		if (error != 0)
1187 			return (error);
1188 	} else if (cmd_info->no_nonnull_data && uap->data != NULL) {
1189 		return (EINVAL);
1190 	}
1191 
1192 	error = kern_procctl(td, uap->idtype, uap->id, uap->com, &x);
1193 
1194 	if (cmd_info->copyout_sz > 0 && (error == 0 ||
1195 	    cmd_info->copyout_on_error)) {
1196 		error1 = copyout(&x, uap->data, cmd_info->copyout_sz);
1197 		if (error == 0)
1198 			error = error1;
1199 	}
1200 	return (error);
1201 }
1202 
1203 static int
1204 kern_procctl_single(struct thread *td, struct proc *p, int com, void *data)
1205 {
1206 	int error;
1207 
1208 	PROC_LOCK_ASSERT(p, MA_OWNED);
1209 	if ((p->p_flag & P_WEXIT) != 0)
1210 		return (ESRCH);
1211 	_PHOLD(p);
1212 	error = procctl_cmds_info[com].exec(td, p, data);
1213 	_PRELE(p);
1214 	return (error);
1215 }
1216 
1217 int
1218 kern_procctl(struct thread *td, idtype_t idtype, id_t id, int com, void *data)
1219 {
1220 	struct pgrp *pg;
1221 	struct proc *p;
1222 	const struct procctl_cmd_info *cmd_info;
1223 	int error, first_error, ok;
1224 	bool sapblk;
1225 
1226 	MPASS(com > 0 && com < nitems(procctl_cmds_info));
1227 	cmd_info = &procctl_cmds_info[com];
1228 	if (idtype != P_PID && cmd_info->one_proc)
1229 		return (EINVAL);
1230 
1231 	sapblk = false;
1232 	if (cmd_info->sapblk != NULL) {
1233 		sapblk = cmd_info->sapblk(td, data);
1234 		if (sapblk && !stop_all_proc_block())
1235 			return (ERESTART);
1236 	}
1237 
1238 	switch (cmd_info->lock_tree) {
1239 	case PCTL_XLOCKED:
1240 		sx_xlock(&proctree_lock);
1241 		break;
1242 	case PCTL_SLOCKED:
1243 		sx_slock(&proctree_lock);
1244 		break;
1245 	default:
1246 		break;
1247 	}
1248 
1249 	switch (idtype) {
1250 	case P_PID:
1251 		if (id == 0) {
1252 			p = td->td_proc;
1253 			error = 0;
1254 			PROC_LOCK(p);
1255 		} else {
1256 			p = pfind(id);
1257 			if (p == NULL) {
1258 				error = cmd_info->esrch_is_einval ?
1259 				    EINVAL : ESRCH;
1260 				break;
1261 			}
1262 			error = cmd_info->need_candebug ? p_candebug(td, p) :
1263 			    p_cansee(td, p);
1264 		}
1265 		if (error == 0)
1266 			error = kern_procctl_single(td, p, com, data);
1267 		PROC_UNLOCK(p);
1268 		break;
1269 	case P_PGID:
1270 		/*
1271 		 * Attempt to apply the operation to all members of the
1272 		 * group.  Ignore processes in the group that can't be
1273 		 * seen.  Ignore errors so long as at least one process is
1274 		 * able to complete the request successfully.
1275 		 */
1276 		pg = pgfind(id);
1277 		if (pg == NULL) {
1278 			error = ESRCH;
1279 			break;
1280 		}
1281 		PGRP_UNLOCK(pg);
1282 		ok = 0;
1283 		first_error = 0;
1284 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1285 			PROC_LOCK(p);
1286 			if (p->p_state == PRS_NEW ||
1287 			    p->p_state == PRS_ZOMBIE ||
1288 			    (cmd_info->need_candebug ? p_candebug(td, p) :
1289 			    p_cansee(td, p)) != 0) {
1290 				PROC_UNLOCK(p);
1291 				continue;
1292 			}
1293 			error = kern_procctl_single(td, p, com, data);
1294 			PROC_UNLOCK(p);
1295 			if (error == 0)
1296 				ok = 1;
1297 			else if (first_error == 0)
1298 				first_error = error;
1299 		}
1300 		if (ok)
1301 			error = 0;
1302 		else if (first_error != 0)
1303 			error = first_error;
1304 		else
1305 			/*
1306 			 * Was not able to see any processes in the
1307 			 * process group.
1308 			 */
1309 			error = ESRCH;
1310 		break;
1311 	default:
1312 		error = EINVAL;
1313 		break;
1314 	}
1315 
1316 	switch (cmd_info->lock_tree) {
1317 	case PCTL_XLOCKED:
1318 		sx_xunlock(&proctree_lock);
1319 		break;
1320 	case PCTL_SLOCKED:
1321 		sx_sunlock(&proctree_lock);
1322 		break;
1323 	default:
1324 		break;
1325 	}
1326 	if (sapblk)
1327 		stop_all_proc_unblock();
1328 	return (error);
1329 }
1330