1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include "opt_ddb.h"
34 #include "opt_ktrace.h"
35 #include "opt_kstack_pages.h"
36 #include "opt_stack.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bitstring.h>
41 #include <sys/conf.h>
42 #include <sys/elf.h>
43 #include <sys/eventhandler.h>
44 #include <sys/exec.h>
45 #include <sys/fcntl.h>
46 #include <sys/ipc.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/lock.h>
51 #include <sys/loginclass.h>
52 #include <sys/malloc.h>
53 #include <sys/mman.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/namei.h>
57 #include <sys/proc.h>
58 #include <sys/ptrace.h>
59 #include <sys/refcount.h>
60 #include <sys/resourcevar.h>
61 #include <sys/rwlock.h>
62 #include <sys/sbuf.h>
63 #include <sys/sysent.h>
64 #include <sys/sched.h>
65 #include <sys/shm.h>
66 #include <sys/smp.h>
67 #include <sys/stack.h>
68 #include <sys/stat.h>
69 #include <sys/dtrace_bsd.h>
70 #include <sys/sysctl.h>
71 #include <sys/filedesc.h>
72 #include <sys/tty.h>
73 #include <sys/signalvar.h>
74 #include <sys/sdt.h>
75 #include <sys/sx.h>
76 #include <sys/user.h>
77 #include <sys/vnode.h>
78 #include <sys/wait.h>
79 #ifdef KTRACE
80 #include <sys/ktrace.h>
81 #endif
82
83 #ifdef DDB
84 #include <ddb/ddb.h>
85 #endif
86
87 #include <vm/vm.h>
88 #include <vm/vm_param.h>
89 #include <vm/vm_extern.h>
90 #include <vm/pmap.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_radix.h>
96 #include <vm/uma.h>
97
98 #include <fs/devfs/devfs.h>
99
100 #ifdef COMPAT_FREEBSD32
101 #include <compat/freebsd32/freebsd32.h>
102 #include <compat/freebsd32/freebsd32_util.h>
103 #endif
104
105 SDT_PROVIDER_DEFINE(proc);
106
107 MALLOC_DEFINE(M_SESSION, "session", "session header");
108 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
109 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
110
111 static void doenterpgrp(struct proc *, struct pgrp *);
112 static void orphanpg(struct pgrp *pg);
113 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
114 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
115 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
116 int preferthread);
117 static void pgdelete(struct pgrp *);
118 static int pgrp_init(void *mem, int size, int flags);
119 static int proc_ctor(void *mem, int size, void *arg, int flags);
120 static void proc_dtor(void *mem, int size, void *arg);
121 static int proc_init(void *mem, int size, int flags);
122 static void proc_fini(void *mem, int size);
123 static void pargs_free(struct pargs *pa);
124
125 /*
126 * Other process lists
127 */
128 struct pidhashhead *pidhashtbl = NULL;
129 struct sx *pidhashtbl_lock;
130 u_long pidhash;
131 u_long pidhashlock;
132 struct pgrphashhead *pgrphashtbl;
133 u_long pgrphash;
134 struct proclist allproc = LIST_HEAD_INITIALIZER(allproc);
135 struct sx __exclusive_cache_line allproc_lock;
136 struct sx __exclusive_cache_line proctree_lock;
137 struct mtx __exclusive_cache_line ppeers_lock;
138 struct mtx __exclusive_cache_line procid_lock;
139 uma_zone_t proc_zone;
140 uma_zone_t pgrp_zone;
141
142 /*
143 * The offset of various fields in struct proc and struct thread.
144 * These are used by kernel debuggers to enumerate kernel threads and
145 * processes.
146 */
147 const int proc_off_p_pid = offsetof(struct proc, p_pid);
148 const int proc_off_p_comm = offsetof(struct proc, p_comm);
149 const int proc_off_p_list = offsetof(struct proc, p_list);
150 const int proc_off_p_hash = offsetof(struct proc, p_hash);
151 const int proc_off_p_threads = offsetof(struct proc, p_threads);
152 const int thread_off_td_tid = offsetof(struct thread, td_tid);
153 const int thread_off_td_name = offsetof(struct thread, td_name);
154 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
155 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
156 const int thread_off_td_plist = offsetof(struct thread, td_plist);
157
158 EVENTHANDLER_LIST_DEFINE(process_ctor);
159 EVENTHANDLER_LIST_DEFINE(process_dtor);
160 EVENTHANDLER_LIST_DEFINE(process_init);
161 EVENTHANDLER_LIST_DEFINE(process_fini);
162 EVENTHANDLER_LIST_DEFINE(process_exit);
163 EVENTHANDLER_LIST_DEFINE(process_fork);
164 EVENTHANDLER_LIST_DEFINE(process_exec);
165
166 int kstack_pages = KSTACK_PAGES;
167 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
168 &kstack_pages, 0,
169 "Kernel stack size in pages");
170 static int vmmap_skip_res_cnt = 0;
171 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
172 &vmmap_skip_res_cnt, 0,
173 "Skip calculation of the pages resident count in kern.proc.vmmap");
174
175 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
176 #ifdef COMPAT_FREEBSD32
177 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
178 #endif
179
180 /*
181 * Initialize global process hashing structures.
182 */
183 void
procinit(void)184 procinit(void)
185 {
186 u_long i;
187
188 sx_init(&allproc_lock, "allproc");
189 sx_init(&proctree_lock, "proctree");
190 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
191 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
192 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
193 pidhashlock = (pidhash + 1) / 64;
194 if (pidhashlock > 0)
195 pidhashlock--;
196 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
197 M_PROC, M_WAITOK | M_ZERO);
198 for (i = 0; i < pidhashlock + 1; i++)
199 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
200 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
201 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
202 proc_ctor, proc_dtor, proc_init, proc_fini,
203 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
204 pgrp_zone = uma_zcreate("PGRP", sizeof(struct pgrp), NULL, NULL,
205 pgrp_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
206 uihashinit();
207 }
208
209 /*
210 * Prepare a proc for use.
211 */
212 static int
proc_ctor(void * mem,int size,void * arg,int flags)213 proc_ctor(void *mem, int size, void *arg, int flags)
214 {
215 struct proc *p;
216 struct thread *td;
217
218 p = (struct proc *)mem;
219 #ifdef KDTRACE_HOOKS
220 kdtrace_proc_ctor(p);
221 #endif
222 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
223 td = FIRST_THREAD_IN_PROC(p);
224 if (td != NULL) {
225 /* Make sure all thread constructors are executed */
226 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
227 }
228 return (0);
229 }
230
231 /*
232 * Reclaim a proc after use.
233 */
234 static void
proc_dtor(void * mem,int size,void * arg)235 proc_dtor(void *mem, int size, void *arg)
236 {
237 struct proc *p;
238 struct thread *td;
239
240 p = mem;
241 td = FIRST_THREAD_IN_PROC(p);
242 if (td != NULL) {
243 KASSERT(p->p_numthreads == 1,
244 ("too many threads in exiting process"));
245
246 /* Free all OSD associated to this thread. */
247 osd_thread_exit(td);
248 ast_kclear(td);
249
250 /* Make sure all thread destructors are executed */
251 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
252 }
253 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
254 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
255 #ifdef KDTRACE_HOOKS
256 kdtrace_proc_dtor(p);
257 #endif
258 KASSERT(p->p_ksi == NULL || !KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
259 }
260
261 /*
262 * Initialize type-stable parts of a proc (when newly created).
263 */
264 static int
proc_init(void * mem,int size,int flags)265 proc_init(void *mem, int size, int flags)
266 {
267 struct proc *p;
268
269 p = (struct proc *)mem;
270 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
271 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
272 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
273 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
274 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
275 cv_init(&p->p_pwait, "ppwait");
276 TAILQ_INIT(&p->p_threads); /* all threads in proc */
277 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
278 p->p_stats = pstats_alloc();
279 p->p_pgrp = NULL;
280 TAILQ_INIT(&p->p_kqtim_stop);
281 STAILQ_INIT(&p->p_ktr);
282 return (0);
283 }
284
285 /*
286 * UMA should ensure that this function is never called.
287 * Freeing a proc structure would violate type stability.
288 */
289 static void
proc_fini(void * mem,int size)290 proc_fini(void *mem, int size)
291 {
292 #ifdef notnow
293 struct proc *p;
294
295 p = (struct proc *)mem;
296 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
297 pstats_free(p->p_stats);
298 thread_free(FIRST_THREAD_IN_PROC(p));
299 mtx_destroy(&p->p_mtx);
300 if (p->p_ksi != NULL)
301 ksiginfo_free(p->p_ksi);
302 #else
303 panic("proc reclaimed");
304 #endif
305 }
306
307 static int
pgrp_init(void * mem,int size,int flags)308 pgrp_init(void *mem, int size, int flags)
309 {
310 struct pgrp *pg;
311
312 pg = mem;
313 mtx_init(&pg->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
314 sx_init(&pg->pg_killsx, "killpg racer");
315 return (0);
316 }
317
318 /*
319 * PID space management.
320 *
321 * These bitmaps are used by fork_findpid.
322 */
323 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
324 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
325 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
326 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
327
328 static bitstr_t *proc_id_array[] = {
329 proc_id_pidmap,
330 proc_id_grpidmap,
331 proc_id_sessidmap,
332 proc_id_reapmap,
333 };
334
335 void
proc_id_set(int type,pid_t id)336 proc_id_set(int type, pid_t id)
337 {
338
339 KASSERT(type >= 0 && type < nitems(proc_id_array),
340 ("invalid type %d\n", type));
341 mtx_lock(&procid_lock);
342 KASSERT(bit_test(proc_id_array[type], id) == 0,
343 ("bit %d already set in %d\n", id, type));
344 bit_set(proc_id_array[type], id);
345 mtx_unlock(&procid_lock);
346 }
347
348 void
proc_id_set_cond(int type,pid_t id)349 proc_id_set_cond(int type, pid_t id)
350 {
351
352 KASSERT(type >= 0 && type < nitems(proc_id_array),
353 ("invalid type %d\n", type));
354 if (bit_test(proc_id_array[type], id))
355 return;
356 mtx_lock(&procid_lock);
357 bit_set(proc_id_array[type], id);
358 mtx_unlock(&procid_lock);
359 }
360
361 void
proc_id_clear(int type,pid_t id)362 proc_id_clear(int type, pid_t id)
363 {
364
365 KASSERT(type >= 0 && type < nitems(proc_id_array),
366 ("invalid type %d\n", type));
367 mtx_lock(&procid_lock);
368 KASSERT(bit_test(proc_id_array[type], id) != 0,
369 ("bit %d not set in %d\n", id, type));
370 bit_clear(proc_id_array[type], id);
371 mtx_unlock(&procid_lock);
372 }
373
374 /*
375 * Is p an inferior of the current process?
376 */
377 int
inferior(struct proc * p)378 inferior(struct proc *p)
379 {
380
381 sx_assert(&proctree_lock, SX_LOCKED);
382 PROC_LOCK_ASSERT(p, MA_OWNED);
383 for (; p != curproc; p = proc_realparent(p)) {
384 if (p->p_pid == 0)
385 return (0);
386 }
387 return (1);
388 }
389
390 /*
391 * Shared lock all the pid hash lists.
392 */
393 void
pidhash_slockall(void)394 pidhash_slockall(void)
395 {
396 u_long i;
397
398 for (i = 0; i < pidhashlock + 1; i++)
399 sx_slock(&pidhashtbl_lock[i]);
400 }
401
402 /*
403 * Shared unlock all the pid hash lists.
404 */
405 void
pidhash_sunlockall(void)406 pidhash_sunlockall(void)
407 {
408 u_long i;
409
410 for (i = 0; i < pidhashlock + 1; i++)
411 sx_sunlock(&pidhashtbl_lock[i]);
412 }
413
414 /*
415 * Similar to pfind(), this function locate a process by number.
416 */
417 struct proc *
pfind_any_locked(pid_t pid)418 pfind_any_locked(pid_t pid)
419 {
420 struct proc *p;
421
422 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
423 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
424 if (p->p_pid == pid) {
425 PROC_LOCK(p);
426 if (p->p_state == PRS_NEW) {
427 PROC_UNLOCK(p);
428 p = NULL;
429 }
430 break;
431 }
432 }
433 return (p);
434 }
435
436 /*
437 * Locate a process by number.
438 *
439 * By not returning processes in the PRS_NEW state, we allow callers to avoid
440 * testing for that condition to avoid dereferencing p_ucred, et al.
441 */
442 static __always_inline struct proc *
_pfind(pid_t pid,bool zombie)443 _pfind(pid_t pid, bool zombie)
444 {
445 struct proc *p;
446
447 p = curproc;
448 if (p->p_pid == pid) {
449 PROC_LOCK(p);
450 return (p);
451 }
452 sx_slock(PIDHASHLOCK(pid));
453 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
454 if (p->p_pid == pid) {
455 PROC_LOCK(p);
456 if (p->p_state == PRS_NEW ||
457 (!zombie && p->p_state == PRS_ZOMBIE)) {
458 PROC_UNLOCK(p);
459 p = NULL;
460 }
461 break;
462 }
463 }
464 sx_sunlock(PIDHASHLOCK(pid));
465 return (p);
466 }
467
468 struct proc *
pfind(pid_t pid)469 pfind(pid_t pid)
470 {
471
472 return (_pfind(pid, false));
473 }
474
475 /*
476 * Same as pfind but allow zombies.
477 */
478 struct proc *
pfind_any(pid_t pid)479 pfind_any(pid_t pid)
480 {
481
482 return (_pfind(pid, true));
483 }
484
485 /*
486 * Locate a process group by number.
487 * The caller must hold proctree_lock.
488 */
489 struct pgrp *
pgfind(pid_t pgid)490 pgfind(pid_t pgid)
491 {
492 struct pgrp *pgrp;
493
494 sx_assert(&proctree_lock, SX_LOCKED);
495
496 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
497 if (pgrp->pg_id == pgid) {
498 PGRP_LOCK(pgrp);
499 return (pgrp);
500 }
501 }
502 return (NULL);
503 }
504
505 /*
506 * Locate process and do additional manipulations, depending on flags.
507 */
508 int
pget(pid_t pid,int flags,struct proc ** pp)509 pget(pid_t pid, int flags, struct proc **pp)
510 {
511 struct proc *p;
512 struct thread *td1;
513 int error;
514
515 p = curproc;
516 if (p->p_pid == pid) {
517 PROC_LOCK(p);
518 } else {
519 p = NULL;
520 if (pid <= PID_MAX) {
521 if ((flags & PGET_NOTWEXIT) == 0)
522 p = pfind_any(pid);
523 else
524 p = pfind(pid);
525 } else if ((flags & PGET_NOTID) == 0) {
526 td1 = tdfind(pid, -1);
527 if (td1 != NULL)
528 p = td1->td_proc;
529 }
530 if (p == NULL)
531 return (ESRCH);
532 if ((flags & PGET_CANSEE) != 0) {
533 error = p_cansee(curthread, p);
534 if (error != 0)
535 goto errout;
536 }
537 }
538 if ((flags & PGET_CANDEBUG) != 0) {
539 error = p_candebug(curthread, p);
540 if (error != 0)
541 goto errout;
542 }
543 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
544 error = EPERM;
545 goto errout;
546 }
547 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
548 error = ESRCH;
549 goto errout;
550 }
551 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
552 /*
553 * XXXRW: Not clear ESRCH is the right error during proc
554 * execve().
555 */
556 error = ESRCH;
557 goto errout;
558 }
559 if ((flags & PGET_HOLD) != 0) {
560 _PHOLD(p);
561 PROC_UNLOCK(p);
562 }
563 *pp = p;
564 return (0);
565 errout:
566 PROC_UNLOCK(p);
567 return (error);
568 }
569
570 /*
571 * Create a new process group.
572 * pgid must be equal to the pid of p.
573 * Begin a new session if required.
574 */
575 int
enterpgrp(struct proc * p,pid_t pgid,struct pgrp * pgrp,struct session * sess)576 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
577 {
578 struct pgrp *old_pgrp;
579
580 sx_assert(&proctree_lock, SX_XLOCKED);
581
582 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
583 KASSERT(p->p_pid == pgid,
584 ("enterpgrp: new pgrp and pid != pgid"));
585 KASSERT(pgfind(pgid) == NULL,
586 ("enterpgrp: pgrp with pgid exists"));
587 KASSERT(!SESS_LEADER(p),
588 ("enterpgrp: session leader attempted setpgrp"));
589
590 old_pgrp = p->p_pgrp;
591 if (!sx_try_xlock(&old_pgrp->pg_killsx)) {
592 sx_xunlock(&proctree_lock);
593 sx_xlock(&old_pgrp->pg_killsx);
594 sx_xunlock(&old_pgrp->pg_killsx);
595 return (ERESTART);
596 }
597 MPASS(old_pgrp == p->p_pgrp);
598
599 if (sess != NULL) {
600 /*
601 * new session
602 */
603 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
604 PROC_LOCK(p);
605 p->p_flag &= ~P_CONTROLT;
606 PROC_UNLOCK(p);
607 PGRP_LOCK(pgrp);
608 sess->s_leader = p;
609 sess->s_sid = p->p_pid;
610 proc_id_set(PROC_ID_SESSION, p->p_pid);
611 refcount_init(&sess->s_count, 1);
612 sess->s_ttyvp = NULL;
613 sess->s_ttydp = NULL;
614 sess->s_ttyp = NULL;
615 bcopy(p->p_session->s_login, sess->s_login,
616 sizeof(sess->s_login));
617 pgrp->pg_session = sess;
618 KASSERT(p == curproc,
619 ("enterpgrp: mksession and p != curproc"));
620 } else {
621 pgrp->pg_session = p->p_session;
622 sess_hold(pgrp->pg_session);
623 PGRP_LOCK(pgrp);
624 }
625 pgrp->pg_id = pgid;
626 proc_id_set(PROC_ID_GROUP, p->p_pid);
627 LIST_INIT(&pgrp->pg_members);
628 pgrp->pg_flags = 0;
629
630 /*
631 * As we have an exclusive lock of proctree_lock,
632 * this should not deadlock.
633 */
634 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
635 SLIST_INIT(&pgrp->pg_sigiolst);
636 PGRP_UNLOCK(pgrp);
637
638 doenterpgrp(p, pgrp);
639
640 sx_xunlock(&old_pgrp->pg_killsx);
641 return (0);
642 }
643
644 /*
645 * Move p to an existing process group
646 */
647 int
enterthispgrp(struct proc * p,struct pgrp * pgrp)648 enterthispgrp(struct proc *p, struct pgrp *pgrp)
649 {
650 struct pgrp *old_pgrp;
651
652 sx_assert(&proctree_lock, SX_XLOCKED);
653 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
654 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
655 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
656 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
657 KASSERT(pgrp->pg_session == p->p_session,
658 ("%s: pgrp's session %p, p->p_session %p proc %p\n",
659 __func__, pgrp->pg_session, p->p_session, p));
660 KASSERT(pgrp != p->p_pgrp,
661 ("%s: p %p belongs to pgrp %p", __func__, p, pgrp));
662
663 old_pgrp = p->p_pgrp;
664 if (!sx_try_xlock(&old_pgrp->pg_killsx)) {
665 sx_xunlock(&proctree_lock);
666 sx_xlock(&old_pgrp->pg_killsx);
667 sx_xunlock(&old_pgrp->pg_killsx);
668 return (ERESTART);
669 }
670 MPASS(old_pgrp == p->p_pgrp);
671 if (!sx_try_xlock(&pgrp->pg_killsx)) {
672 sx_xunlock(&old_pgrp->pg_killsx);
673 sx_xunlock(&proctree_lock);
674 sx_xlock(&pgrp->pg_killsx);
675 sx_xunlock(&pgrp->pg_killsx);
676 return (ERESTART);
677 }
678
679 doenterpgrp(p, pgrp);
680
681 sx_xunlock(&pgrp->pg_killsx);
682 sx_xunlock(&old_pgrp->pg_killsx);
683 return (0);
684 }
685
686 /*
687 * If true, any child of q which belongs to group pgrp, qualifies the
688 * process group pgrp as not orphaned.
689 */
690 static bool
isjobproc(struct proc * q,struct pgrp * pgrp)691 isjobproc(struct proc *q, struct pgrp *pgrp)
692 {
693 sx_assert(&proctree_lock, SX_LOCKED);
694
695 return (q->p_pgrp != pgrp &&
696 q->p_pgrp->pg_session == pgrp->pg_session);
697 }
698
699 static struct proc *
jobc_reaper(struct proc * p)700 jobc_reaper(struct proc *p)
701 {
702 struct proc *pp;
703
704 sx_assert(&proctree_lock, SA_LOCKED);
705
706 for (pp = p;;) {
707 pp = pp->p_reaper;
708 if (pp->p_reaper == pp ||
709 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
710 return (pp);
711 }
712 }
713
714 static struct proc *
jobc_parent(struct proc * p,struct proc * p_exiting)715 jobc_parent(struct proc *p, struct proc *p_exiting)
716 {
717 struct proc *pp;
718
719 sx_assert(&proctree_lock, SA_LOCKED);
720
721 pp = proc_realparent(p);
722 if (pp->p_pptr == NULL || pp == p_exiting ||
723 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
724 return (pp);
725 return (jobc_reaper(pp));
726 }
727
728 int
pgrp_calc_jobc(struct pgrp * pgrp)729 pgrp_calc_jobc(struct pgrp *pgrp)
730 {
731 struct proc *q;
732 int cnt;
733
734 #ifdef INVARIANTS
735 if (!mtx_owned(&pgrp->pg_mtx))
736 sx_assert(&proctree_lock, SA_LOCKED);
737 #endif
738
739 cnt = 0;
740 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
741 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
742 q->p_pptr == NULL)
743 continue;
744 if (isjobproc(jobc_parent(q, NULL), pgrp))
745 cnt++;
746 }
747 return (cnt);
748 }
749
750 /*
751 * Move p to a process group
752 */
753 static void
doenterpgrp(struct proc * p,struct pgrp * pgrp)754 doenterpgrp(struct proc *p, struct pgrp *pgrp)
755 {
756 struct pgrp *savepgrp;
757 struct proc *pp;
758
759 sx_assert(&proctree_lock, SX_XLOCKED);
760 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
761 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
762 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
763 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
764
765 savepgrp = p->p_pgrp;
766 pp = jobc_parent(p, NULL);
767
768 PGRP_LOCK(pgrp);
769 PGRP_LOCK(savepgrp);
770 if (isjobproc(pp, savepgrp) && pgrp_calc_jobc(savepgrp) == 1)
771 orphanpg(savepgrp);
772 PROC_LOCK(p);
773 LIST_REMOVE(p, p_pglist);
774 p->p_pgrp = pgrp;
775 PROC_UNLOCK(p);
776 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
777 if (isjobproc(pp, pgrp))
778 pgrp->pg_flags &= ~PGRP_ORPHANED;
779 PGRP_UNLOCK(savepgrp);
780 PGRP_UNLOCK(pgrp);
781 if (LIST_EMPTY(&savepgrp->pg_members))
782 pgdelete(savepgrp);
783 }
784
785 /*
786 * remove process from process group
787 */
788 int
leavepgrp(struct proc * p)789 leavepgrp(struct proc *p)
790 {
791 struct pgrp *savepgrp;
792
793 sx_assert(&proctree_lock, SX_XLOCKED);
794 savepgrp = p->p_pgrp;
795 PGRP_LOCK(savepgrp);
796 PROC_LOCK(p);
797 LIST_REMOVE(p, p_pglist);
798 p->p_pgrp = NULL;
799 PROC_UNLOCK(p);
800 PGRP_UNLOCK(savepgrp);
801 if (LIST_EMPTY(&savepgrp->pg_members))
802 pgdelete(savepgrp);
803 return (0);
804 }
805
806 /*
807 * delete a process group
808 */
809 static void
pgdelete(struct pgrp * pgrp)810 pgdelete(struct pgrp *pgrp)
811 {
812 struct session *savesess;
813 struct tty *tp;
814
815 sx_assert(&proctree_lock, SX_XLOCKED);
816 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
817 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
818
819 /*
820 * Reset any sigio structures pointing to us as a result of
821 * F_SETOWN with our pgid. The proctree lock ensures that
822 * new sigio structures will not be added after this point.
823 */
824 funsetownlst(&pgrp->pg_sigiolst);
825
826 PGRP_LOCK(pgrp);
827 tp = pgrp->pg_session->s_ttyp;
828 LIST_REMOVE(pgrp, pg_hash);
829 savesess = pgrp->pg_session;
830 PGRP_UNLOCK(pgrp);
831
832 /* Remove the reference to the pgrp before deallocating it. */
833 if (tp != NULL) {
834 tty_lock(tp);
835 tty_rel_pgrp(tp, pgrp);
836 }
837
838 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
839 uma_zfree(pgrp_zone, pgrp);
840 sess_release(savesess);
841 }
842
843
844 static void
fixjobc_kill(struct proc * p)845 fixjobc_kill(struct proc *p)
846 {
847 struct proc *q;
848 struct pgrp *pgrp;
849
850 sx_assert(&proctree_lock, SX_LOCKED);
851 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
852 pgrp = p->p_pgrp;
853 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
854 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
855
856 /*
857 * p no longer affects process group orphanage for children.
858 * It is marked by the flag because p is only physically
859 * removed from its process group on wait(2).
860 */
861 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
862 p->p_treeflag |= P_TREE_GRPEXITED;
863
864 /*
865 * Check if exiting p orphans its own group.
866 */
867 pgrp = p->p_pgrp;
868 if (isjobproc(jobc_parent(p, NULL), pgrp)) {
869 PGRP_LOCK(pgrp);
870 if (pgrp_calc_jobc(pgrp) == 0)
871 orphanpg(pgrp);
872 PGRP_UNLOCK(pgrp);
873 }
874
875 /*
876 * Check this process' children to see whether they qualify
877 * their process groups after reparenting to reaper.
878 */
879 LIST_FOREACH(q, &p->p_children, p_sibling) {
880 pgrp = q->p_pgrp;
881 PGRP_LOCK(pgrp);
882 if (pgrp_calc_jobc(pgrp) == 0) {
883 /*
884 * We want to handle exactly the children that
885 * has p as realparent. Then, when calculating
886 * jobc_parent for children, we should ignore
887 * P_TREE_GRPEXITED flag already set on p.
888 */
889 if (jobc_parent(q, p) == p && isjobproc(p, pgrp))
890 orphanpg(pgrp);
891 } else
892 pgrp->pg_flags &= ~PGRP_ORPHANED;
893 PGRP_UNLOCK(pgrp);
894 }
895 LIST_FOREACH(q, &p->p_orphans, p_orphan) {
896 pgrp = q->p_pgrp;
897 PGRP_LOCK(pgrp);
898 if (pgrp_calc_jobc(pgrp) == 0) {
899 if (isjobproc(p, pgrp))
900 orphanpg(pgrp);
901 } else
902 pgrp->pg_flags &= ~PGRP_ORPHANED;
903 PGRP_UNLOCK(pgrp);
904 }
905 }
906
907 void
killjobc(void)908 killjobc(void)
909 {
910 struct session *sp;
911 struct tty *tp;
912 struct proc *p;
913 struct vnode *ttyvp;
914
915 p = curproc;
916 MPASS(p->p_flag & P_WEXIT);
917 sx_assert(&proctree_lock, SX_LOCKED);
918
919 if (SESS_LEADER(p)) {
920 sp = p->p_session;
921
922 /*
923 * s_ttyp is not zero'd; we use this to indicate that
924 * the session once had a controlling terminal. (for
925 * logging and informational purposes)
926 */
927 SESS_LOCK(sp);
928 ttyvp = sp->s_ttyvp;
929 tp = sp->s_ttyp;
930 sp->s_ttyvp = NULL;
931 sp->s_ttydp = NULL;
932 sp->s_leader = NULL;
933 SESS_UNLOCK(sp);
934
935 /*
936 * Signal foreground pgrp and revoke access to
937 * controlling terminal if it has not been revoked
938 * already.
939 *
940 * Because the TTY may have been revoked in the mean
941 * time and could already have a new session associated
942 * with it, make sure we don't send a SIGHUP to a
943 * foreground process group that does not belong to this
944 * session.
945 */
946
947 if (tp != NULL) {
948 tty_lock(tp);
949 if (tp->t_session == sp)
950 tty_signal_pgrp(tp, SIGHUP);
951 tty_unlock(tp);
952 }
953
954 if (ttyvp != NULL) {
955 sx_xunlock(&proctree_lock);
956 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
957 VOP_REVOKE(ttyvp, REVOKEALL);
958 VOP_UNLOCK(ttyvp);
959 }
960 devfs_ctty_unref(ttyvp);
961 sx_xlock(&proctree_lock);
962 }
963 }
964 fixjobc_kill(p);
965 }
966
967 /*
968 * A process group has become orphaned, mark it as such for signal
969 * delivery code. If there are any stopped processes in the group,
970 * hang-up all process in that group.
971 */
972 static void
orphanpg(struct pgrp * pg)973 orphanpg(struct pgrp *pg)
974 {
975 struct proc *p;
976
977 PGRP_LOCK_ASSERT(pg, MA_OWNED);
978
979 pg->pg_flags |= PGRP_ORPHANED;
980
981 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
982 PROC_LOCK(p);
983 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
984 PROC_UNLOCK(p);
985 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
986 PROC_LOCK(p);
987 kern_psignal(p, SIGHUP);
988 kern_psignal(p, SIGCONT);
989 PROC_UNLOCK(p);
990 }
991 return;
992 }
993 PROC_UNLOCK(p);
994 }
995 }
996
997 void
sess_hold(struct session * s)998 sess_hold(struct session *s)
999 {
1000
1001 refcount_acquire(&s->s_count);
1002 }
1003
1004 void
sess_release(struct session * s)1005 sess_release(struct session *s)
1006 {
1007
1008 if (refcount_release(&s->s_count)) {
1009 if (s->s_ttyp != NULL) {
1010 tty_lock(s->s_ttyp);
1011 tty_rel_sess(s->s_ttyp, s);
1012 }
1013 proc_id_clear(PROC_ID_SESSION, s->s_sid);
1014 mtx_destroy(&s->s_mtx);
1015 free(s, M_SESSION);
1016 }
1017 }
1018
1019 #ifdef DDB
1020
1021 static void
db_print_pgrp_one(struct pgrp * pgrp,struct proc * p)1022 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
1023 {
1024 db_printf(
1025 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
1026 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
1027 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
1028 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
1029 }
1030
DB_SHOW_COMMAND_FLAGS(pgrpdump,pgrpdump,DB_CMD_MEMSAFE)1031 DB_SHOW_COMMAND_FLAGS(pgrpdump, pgrpdump, DB_CMD_MEMSAFE)
1032 {
1033 struct pgrp *pgrp;
1034 struct proc *p;
1035 int i;
1036
1037 for (i = 0; i <= pgrphash; i++) {
1038 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1039 db_printf("indx %d\n", i);
1040 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1041 db_printf(
1042 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1043 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1044 pgrp->pg_session->s_count,
1045 LIST_FIRST(&pgrp->pg_members));
1046 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1047 db_print_pgrp_one(pgrp, p);
1048 }
1049 }
1050 }
1051 }
1052 #endif /* DDB */
1053
1054 /*
1055 * Calculate the kinfo_proc members which contain process-wide
1056 * informations.
1057 * Must be called with the target process locked.
1058 */
1059 static void
fill_kinfo_aggregate(struct proc * p,struct kinfo_proc * kp)1060 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1061 {
1062 struct thread *td;
1063
1064 PROC_LOCK_ASSERT(p, MA_OWNED);
1065
1066 kp->ki_estcpu = 0;
1067 kp->ki_pctcpu = 0;
1068 FOREACH_THREAD_IN_PROC(p, td) {
1069 thread_lock(td);
1070 kp->ki_pctcpu += sched_pctcpu(td);
1071 kp->ki_estcpu += sched_estcpu(td);
1072 thread_unlock(td);
1073 }
1074 }
1075
1076 /*
1077 * Fill in any information that is common to all threads in the process.
1078 * Must be called with the target process locked.
1079 */
1080 static void
fill_kinfo_proc_only(struct proc * p,struct kinfo_proc * kp)1081 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1082 {
1083 struct thread *td0;
1084 struct ucred *cred;
1085 struct sigacts *ps;
1086 struct timeval boottime;
1087
1088 PROC_LOCK_ASSERT(p, MA_OWNED);
1089
1090 kp->ki_structsize = sizeof(*kp);
1091 kp->ki_paddr = p;
1092 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1093 kp->ki_args = p->p_args;
1094 kp->ki_textvp = p->p_textvp;
1095 #ifdef KTRACE
1096 kp->ki_tracep = ktr_get_tracevp(p, false);
1097 kp->ki_traceflag = p->p_traceflag;
1098 #endif
1099 kp->ki_fd = p->p_fd;
1100 kp->ki_pd = p->p_pd;
1101 kp->ki_vmspace = p->p_vmspace;
1102 kp->ki_flag = p->p_flag;
1103 kp->ki_flag2 = p->p_flag2;
1104 cred = p->p_ucred;
1105 if (cred) {
1106 kp->ki_uid = cred->cr_uid;
1107 kp->ki_ruid = cred->cr_ruid;
1108 kp->ki_svuid = cred->cr_svuid;
1109 kp->ki_cr_flags = 0;
1110 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1111 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1112 /* XXX bde doesn't like KI_NGROUPS */
1113 if (1 + cred->cr_ngroups > KI_NGROUPS) {
1114 kp->ki_ngroups = KI_NGROUPS;
1115 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1116 } else
1117 kp->ki_ngroups = 1 + cred->cr_ngroups;
1118 kp->ki_groups[0] = cred->cr_gid;
1119 bcopy(cred->cr_groups, kp->ki_groups + 1,
1120 (kp->ki_ngroups - 1) * sizeof(gid_t));
1121 kp->ki_rgid = cred->cr_rgid;
1122 kp->ki_svgid = cred->cr_svgid;
1123 /* If jailed(cred), emulate the old P_JAILED flag. */
1124 if (jailed(cred)) {
1125 kp->ki_flag |= P_JAILED;
1126 /* If inside the jail, use 0 as a jail ID. */
1127 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1128 kp->ki_jid = cred->cr_prison->pr_id;
1129 }
1130 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1131 sizeof(kp->ki_loginclass));
1132 }
1133 ps = p->p_sigacts;
1134 if (ps) {
1135 mtx_lock(&ps->ps_mtx);
1136 kp->ki_sigignore = ps->ps_sigignore;
1137 kp->ki_sigcatch = ps->ps_sigcatch;
1138 mtx_unlock(&ps->ps_mtx);
1139 }
1140 if (p->p_state != PRS_NEW &&
1141 p->p_state != PRS_ZOMBIE &&
1142 p->p_vmspace != NULL) {
1143 struct vmspace *vm = p->p_vmspace;
1144
1145 kp->ki_size = vm->vm_map.size;
1146 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1147 FOREACH_THREAD_IN_PROC(p, td0)
1148 kp->ki_rssize += td0->td_kstack_pages;
1149 kp->ki_swrss = vm->vm_swrss;
1150 kp->ki_tsize = vm->vm_tsize;
1151 kp->ki_dsize = vm->vm_dsize;
1152 kp->ki_ssize = vm->vm_ssize;
1153 } else if (p->p_state == PRS_ZOMBIE)
1154 kp->ki_stat = SZOMB;
1155 kp->ki_sflag = PS_INMEM;
1156 /* Calculate legacy swtime as seconds since 'swtick'. */
1157 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1158 kp->ki_pid = p->p_pid;
1159 kp->ki_nice = p->p_nice;
1160 kp->ki_fibnum = p->p_fibnum;
1161 kp->ki_start = p->p_stats->p_start;
1162 getboottime(&boottime);
1163 timevaladd(&kp->ki_start, &boottime);
1164 PROC_STATLOCK(p);
1165 rufetch(p, &kp->ki_rusage);
1166 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1167 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1168 PROC_STATUNLOCK(p);
1169 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1170 /* Some callers want child times in a single value. */
1171 kp->ki_childtime = kp->ki_childstime;
1172 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1173
1174 FOREACH_THREAD_IN_PROC(p, td0)
1175 kp->ki_cow += td0->td_cow;
1176
1177 if (p->p_comm[0] != '\0')
1178 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1179 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1180 p->p_sysent->sv_name[0] != '\0')
1181 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1182 kp->ki_siglist = p->p_siglist;
1183 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1184 kp->ki_acflag = p->p_acflag;
1185 kp->ki_lock = p->p_lock;
1186 if (p->p_pptr) {
1187 kp->ki_ppid = p->p_oppid;
1188 if (p->p_flag & P_TRACED)
1189 kp->ki_tracer = p->p_pptr->p_pid;
1190 }
1191 }
1192
1193 /*
1194 * Fill job-related process information.
1195 */
1196 static void
fill_kinfo_proc_pgrp(struct proc * p,struct kinfo_proc * kp)1197 fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp)
1198 {
1199 struct tty *tp;
1200 struct session *sp;
1201 struct pgrp *pgrp;
1202
1203 sx_assert(&proctree_lock, SA_LOCKED);
1204 PROC_LOCK_ASSERT(p, MA_OWNED);
1205
1206 pgrp = p->p_pgrp;
1207 if (pgrp == NULL)
1208 return;
1209
1210 kp->ki_pgid = pgrp->pg_id;
1211 kp->ki_jobc = pgrp_calc_jobc(pgrp);
1212
1213 sp = pgrp->pg_session;
1214 tp = NULL;
1215
1216 if (sp != NULL) {
1217 kp->ki_sid = sp->s_sid;
1218 SESS_LOCK(sp);
1219 strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login));
1220 if (sp->s_ttyvp)
1221 kp->ki_kiflag |= KI_CTTY;
1222 if (SESS_LEADER(p))
1223 kp->ki_kiflag |= KI_SLEADER;
1224 tp = sp->s_ttyp;
1225 SESS_UNLOCK(sp);
1226 }
1227
1228 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1229 kp->ki_tdev = tty_udev(tp);
1230 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1231 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1232 if (tp->t_session)
1233 kp->ki_tsid = tp->t_session->s_sid;
1234 } else {
1235 kp->ki_tdev = NODEV;
1236 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1237 }
1238 kp->ki_reaper = p->p_reaper->p_pid;
1239 kp->ki_reapsubtree = p->p_reapsubtree;
1240 }
1241
1242 /*
1243 * Fill in information that is thread specific. Must be called with
1244 * target process locked. If 'preferthread' is set, overwrite certain
1245 * process-related fields that are maintained for both threads and
1246 * processes.
1247 */
1248 static void
fill_kinfo_thread(struct thread * td,struct kinfo_proc * kp,int preferthread)1249 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1250 {
1251 struct proc *p;
1252
1253 p = td->td_proc;
1254 kp->ki_tdaddr = td;
1255 PROC_LOCK_ASSERT(p, MA_OWNED);
1256
1257 if (preferthread)
1258 PROC_STATLOCK(p);
1259 thread_lock(td);
1260 if (td->td_wmesg != NULL)
1261 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1262 else
1263 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1264 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1265 sizeof(kp->ki_tdname)) {
1266 strlcpy(kp->ki_moretdname,
1267 td->td_name + sizeof(kp->ki_tdname) - 1,
1268 sizeof(kp->ki_moretdname));
1269 } else {
1270 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1271 }
1272 if (TD_ON_LOCK(td)) {
1273 kp->ki_kiflag |= KI_LOCKBLOCK;
1274 strlcpy(kp->ki_lockname, td->td_lockname,
1275 sizeof(kp->ki_lockname));
1276 } else {
1277 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1278 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1279 }
1280
1281 if (p->p_state == PRS_NORMAL) { /* approximate. */
1282 if (TD_ON_RUNQ(td) ||
1283 TD_CAN_RUN(td) ||
1284 TD_IS_RUNNING(td)) {
1285 kp->ki_stat = SRUN;
1286 } else if (P_SHOULDSTOP(p)) {
1287 kp->ki_stat = SSTOP;
1288 } else if (TD_IS_SLEEPING(td)) {
1289 kp->ki_stat = SSLEEP;
1290 } else if (TD_ON_LOCK(td)) {
1291 kp->ki_stat = SLOCK;
1292 } else {
1293 kp->ki_stat = SWAIT;
1294 }
1295 } else if (p->p_state == PRS_ZOMBIE) {
1296 kp->ki_stat = SZOMB;
1297 } else {
1298 kp->ki_stat = SIDL;
1299 }
1300
1301 /* Things in the thread */
1302 kp->ki_wchan = td->td_wchan;
1303 kp->ki_pri.pri_level = td->td_priority;
1304 kp->ki_pri.pri_native = td->td_base_pri;
1305
1306 /*
1307 * Note: legacy fields; clamp at the old NOCPU value and/or
1308 * the maximum u_char CPU value.
1309 */
1310 if (td->td_lastcpu == NOCPU)
1311 kp->ki_lastcpu_old = NOCPU_OLD;
1312 else if (td->td_lastcpu > MAXCPU_OLD)
1313 kp->ki_lastcpu_old = MAXCPU_OLD;
1314 else
1315 kp->ki_lastcpu_old = td->td_lastcpu;
1316
1317 if (td->td_oncpu == NOCPU)
1318 kp->ki_oncpu_old = NOCPU_OLD;
1319 else if (td->td_oncpu > MAXCPU_OLD)
1320 kp->ki_oncpu_old = MAXCPU_OLD;
1321 else
1322 kp->ki_oncpu_old = td->td_oncpu;
1323
1324 kp->ki_lastcpu = td->td_lastcpu;
1325 kp->ki_oncpu = td->td_oncpu;
1326 kp->ki_tdflags = td->td_flags;
1327 kp->ki_tid = td->td_tid;
1328 kp->ki_numthreads = p->p_numthreads;
1329 kp->ki_pcb = td->td_pcb;
1330 kp->ki_kstack = td->td_kstack;
1331 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1332 kp->ki_pri.pri_class = td->td_pri_class;
1333 kp->ki_pri.pri_user = td->td_user_pri;
1334
1335 if (preferthread) {
1336 rufetchtd(td, &kp->ki_rusage);
1337 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1338 kp->ki_pctcpu = sched_pctcpu(td);
1339 kp->ki_estcpu = sched_estcpu(td);
1340 kp->ki_cow = td->td_cow;
1341 }
1342
1343 /* We can't get this anymore but ps etc never used it anyway. */
1344 kp->ki_rqindex = 0;
1345
1346 if (preferthread)
1347 kp->ki_siglist = td->td_siglist;
1348 kp->ki_sigmask = td->td_sigmask;
1349 thread_unlock(td);
1350 if (preferthread)
1351 PROC_STATUNLOCK(p);
1352
1353 if ((td->td_pflags & TDP2_UEXTERR) != 0)
1354 kp->ki_uerrmsg = td->td_exterr_ptr;
1355 }
1356
1357 /*
1358 * Fill in a kinfo_proc structure for the specified process.
1359 * Must be called with the target process locked.
1360 */
1361 void
fill_kinfo_proc(struct proc * p,struct kinfo_proc * kp)1362 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1363 {
1364 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1365
1366 bzero(kp, sizeof(*kp));
1367
1368 fill_kinfo_proc_pgrp(p,kp);
1369 fill_kinfo_proc_only(p, kp);
1370 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1371 fill_kinfo_aggregate(p, kp);
1372 }
1373
1374 struct pstats *
pstats_alloc(void)1375 pstats_alloc(void)
1376 {
1377
1378 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1379 }
1380
1381 /*
1382 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1383 */
1384 void
pstats_fork(struct pstats * src,struct pstats * dst)1385 pstats_fork(struct pstats *src, struct pstats *dst)
1386 {
1387
1388 bzero(&dst->pstat_startzero,
1389 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1390 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1391 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1392 }
1393
1394 void
pstats_free(struct pstats * ps)1395 pstats_free(struct pstats *ps)
1396 {
1397
1398 free(ps, M_SUBPROC);
1399 }
1400
1401 #ifdef COMPAT_FREEBSD32
1402
1403 /*
1404 * This function is typically used to copy out the kernel address, so
1405 * it can be replaced by assignment of zero.
1406 */
1407 static inline uint32_t
ptr32_trim(const void * ptr)1408 ptr32_trim(const void *ptr)
1409 {
1410 uintptr_t uptr;
1411
1412 uptr = (uintptr_t)ptr;
1413 return ((uptr > UINT_MAX) ? 0 : uptr);
1414 }
1415
1416 #define PTRTRIM_CP(src,dst,fld) \
1417 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1418
1419 static void
freebsd32_kinfo_proc_out(const struct kinfo_proc * ki,struct kinfo_proc32 * ki32)1420 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1421 {
1422 int i;
1423
1424 bzero(ki32, sizeof(struct kinfo_proc32));
1425 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1426 CP(*ki, *ki32, ki_layout);
1427 PTRTRIM_CP(*ki, *ki32, ki_args);
1428 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1429 PTRTRIM_CP(*ki, *ki32, ki_addr);
1430 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1431 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1432 PTRTRIM_CP(*ki, *ki32, ki_fd);
1433 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1434 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1435 CP(*ki, *ki32, ki_pid);
1436 CP(*ki, *ki32, ki_ppid);
1437 CP(*ki, *ki32, ki_pgid);
1438 CP(*ki, *ki32, ki_tpgid);
1439 CP(*ki, *ki32, ki_sid);
1440 CP(*ki, *ki32, ki_tsid);
1441 CP(*ki, *ki32, ki_jobc);
1442 FU64_CP(*ki, *ki32, ki_tdev);
1443 CP(*ki, *ki32, ki_tdev_freebsd11);
1444 CP(*ki, *ki32, ki_siglist);
1445 CP(*ki, *ki32, ki_sigmask);
1446 CP(*ki, *ki32, ki_sigignore);
1447 CP(*ki, *ki32, ki_sigcatch);
1448 CP(*ki, *ki32, ki_uid);
1449 CP(*ki, *ki32, ki_ruid);
1450 CP(*ki, *ki32, ki_svuid);
1451 CP(*ki, *ki32, ki_rgid);
1452 CP(*ki, *ki32, ki_svgid);
1453 CP(*ki, *ki32, ki_ngroups);
1454 for (i = 0; i < KI_NGROUPS; i++)
1455 CP(*ki, *ki32, ki_groups[i]);
1456 CP(*ki, *ki32, ki_size);
1457 CP(*ki, *ki32, ki_rssize);
1458 CP(*ki, *ki32, ki_swrss);
1459 CP(*ki, *ki32, ki_tsize);
1460 CP(*ki, *ki32, ki_dsize);
1461 CP(*ki, *ki32, ki_ssize);
1462 CP(*ki, *ki32, ki_xstat);
1463 CP(*ki, *ki32, ki_acflag);
1464 CP(*ki, *ki32, ki_pctcpu);
1465 CP(*ki, *ki32, ki_estcpu);
1466 CP(*ki, *ki32, ki_slptime);
1467 CP(*ki, *ki32, ki_swtime);
1468 CP(*ki, *ki32, ki_cow);
1469 FU64_CP(*ki, *ki32, ki_runtime);
1470 TV_CP(*ki, *ki32, ki_start);
1471 TV_CP(*ki, *ki32, ki_childtime);
1472 CP(*ki, *ki32, ki_flag);
1473 CP(*ki, *ki32, ki_kiflag);
1474 CP(*ki, *ki32, ki_traceflag);
1475 CP(*ki, *ki32, ki_stat);
1476 CP(*ki, *ki32, ki_nice);
1477 CP(*ki, *ki32, ki_lock);
1478 CP(*ki, *ki32, ki_rqindex);
1479 CP(*ki, *ki32, ki_oncpu);
1480 CP(*ki, *ki32, ki_lastcpu);
1481
1482 /* XXX TODO: wrap cpu value as appropriate */
1483 CP(*ki, *ki32, ki_oncpu_old);
1484 CP(*ki, *ki32, ki_lastcpu_old);
1485
1486 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1487 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1488 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1489 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1490 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1491 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1492 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1493 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1494 CP(*ki, *ki32, ki_tracer);
1495 CP(*ki, *ki32, ki_flag2);
1496 CP(*ki, *ki32, ki_fibnum);
1497 CP(*ki, *ki32, ki_cr_flags);
1498 CP(*ki, *ki32, ki_jid);
1499 CP(*ki, *ki32, ki_reaper);
1500 CP(*ki, *ki32, ki_reapsubtree);
1501 CP(*ki, *ki32, ki_numthreads);
1502 CP(*ki, *ki32, ki_tid);
1503 CP(*ki, *ki32, ki_pri);
1504 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1505 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1506 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1507 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1508 PTRTRIM_CP(*ki, *ki32, ki_udata);
1509 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1510 PTRTRIM_CP(*ki, *ki32, ki_pd);
1511 CP(*ki, *ki32, ki_sflag);
1512 CP(*ki, *ki32, ki_tdflags);
1513 PTRTRIM_CP(*ki, *ki32, ki_uerrmsg);
1514 }
1515 #endif
1516
1517 static ssize_t
kern_proc_out_size(struct proc * p,int flags)1518 kern_proc_out_size(struct proc *p, int flags)
1519 {
1520 ssize_t size = 0;
1521
1522 PROC_LOCK_ASSERT(p, MA_OWNED);
1523
1524 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1525 #ifdef COMPAT_FREEBSD32
1526 if ((flags & KERN_PROC_MASK32) != 0) {
1527 size += sizeof(struct kinfo_proc32);
1528 } else
1529 #endif
1530 size += sizeof(struct kinfo_proc);
1531 } else {
1532 #ifdef COMPAT_FREEBSD32
1533 if ((flags & KERN_PROC_MASK32) != 0)
1534 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1535 else
1536 #endif
1537 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1538 }
1539 PROC_UNLOCK(p);
1540 return (size);
1541 }
1542
1543 int
kern_proc_out(struct proc * p,struct sbuf * sb,int flags)1544 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1545 {
1546 struct thread *td;
1547 struct kinfo_proc ki;
1548 #ifdef COMPAT_FREEBSD32
1549 struct kinfo_proc32 ki32;
1550 #endif
1551 int error;
1552
1553 PROC_LOCK_ASSERT(p, MA_OWNED);
1554 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1555
1556 error = 0;
1557 fill_kinfo_proc(p, &ki);
1558 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1559 #ifdef COMPAT_FREEBSD32
1560 if ((flags & KERN_PROC_MASK32) != 0) {
1561 freebsd32_kinfo_proc_out(&ki, &ki32);
1562 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1563 error = ENOMEM;
1564 } else
1565 #endif
1566 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1567 error = ENOMEM;
1568 } else {
1569 FOREACH_THREAD_IN_PROC(p, td) {
1570 fill_kinfo_thread(td, &ki, 1);
1571 #ifdef COMPAT_FREEBSD32
1572 if ((flags & KERN_PROC_MASK32) != 0) {
1573 freebsd32_kinfo_proc_out(&ki, &ki32);
1574 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1575 error = ENOMEM;
1576 } else
1577 #endif
1578 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1579 error = ENOMEM;
1580 if (error != 0)
1581 break;
1582 }
1583 }
1584 PROC_UNLOCK(p);
1585 return (error);
1586 }
1587
1588 static int
sysctl_out_proc(struct proc * p,struct sysctl_req * req,int flags)1589 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1590 {
1591 struct sbuf sb;
1592 struct kinfo_proc ki;
1593 int error, error2;
1594
1595 if (req->oldptr == NULL)
1596 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1597
1598 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1599 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1600 error = kern_proc_out(p, &sb, flags);
1601 error2 = sbuf_finish(&sb);
1602 sbuf_delete(&sb);
1603 if (error != 0)
1604 return (error);
1605 else if (error2 != 0)
1606 return (error2);
1607 return (0);
1608 }
1609
1610 int
proc_iterate(int (* cb)(struct proc *,void *),void * cbarg)1611 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1612 {
1613 struct proc *p;
1614 int error, i, j;
1615
1616 for (i = 0; i < pidhashlock + 1; i++) {
1617 sx_slock(&proctree_lock);
1618 sx_slock(&pidhashtbl_lock[i]);
1619 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1620 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1621 if (p->p_state == PRS_NEW)
1622 continue;
1623 error = cb(p, cbarg);
1624 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1625 if (error != 0) {
1626 sx_sunlock(&pidhashtbl_lock[i]);
1627 sx_sunlock(&proctree_lock);
1628 return (error);
1629 }
1630 }
1631 }
1632 sx_sunlock(&pidhashtbl_lock[i]);
1633 sx_sunlock(&proctree_lock);
1634 }
1635 return (0);
1636 }
1637
1638 struct kern_proc_out_args {
1639 struct sysctl_req *req;
1640 int flags;
1641 int oid_number;
1642 int *name;
1643 };
1644
1645 static int
sysctl_kern_proc_iterate(struct proc * p,void * origarg)1646 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1647 {
1648 struct kern_proc_out_args *arg = origarg;
1649 int *name = arg->name;
1650 int oid_number = arg->oid_number;
1651 int flags = arg->flags;
1652 struct sysctl_req *req = arg->req;
1653 int error = 0;
1654
1655 PROC_LOCK(p);
1656
1657 KASSERT(p->p_ucred != NULL,
1658 ("process credential is NULL for non-NEW proc"));
1659 /*
1660 * Show a user only appropriate processes.
1661 */
1662 if (p_cansee(curthread, p))
1663 goto skip;
1664 /*
1665 * TODO - make more efficient (see notes below).
1666 * do by session.
1667 */
1668 switch (oid_number) {
1669 case KERN_PROC_GID:
1670 if (p->p_ucred->cr_gid != (gid_t)name[0])
1671 goto skip;
1672 break;
1673
1674 case KERN_PROC_PGRP:
1675 /* could do this by traversing pgrp */
1676 if (p->p_pgrp == NULL ||
1677 p->p_pgrp->pg_id != (pid_t)name[0])
1678 goto skip;
1679 break;
1680
1681 case KERN_PROC_RGID:
1682 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1683 goto skip;
1684 break;
1685
1686 case KERN_PROC_SESSION:
1687 if (p->p_session == NULL ||
1688 p->p_session->s_sid != (pid_t)name[0])
1689 goto skip;
1690 break;
1691
1692 case KERN_PROC_TTY:
1693 if ((p->p_flag & P_CONTROLT) == 0 ||
1694 p->p_session == NULL)
1695 goto skip;
1696 /* XXX proctree_lock */
1697 SESS_LOCK(p->p_session);
1698 if (p->p_session->s_ttyp == NULL ||
1699 tty_udev(p->p_session->s_ttyp) !=
1700 (dev_t)name[0]) {
1701 SESS_UNLOCK(p->p_session);
1702 goto skip;
1703 }
1704 SESS_UNLOCK(p->p_session);
1705 break;
1706
1707 case KERN_PROC_UID:
1708 if (p->p_ucred->cr_uid != (uid_t)name[0])
1709 goto skip;
1710 break;
1711
1712 case KERN_PROC_RUID:
1713 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1714 goto skip;
1715 break;
1716
1717 case KERN_PROC_PROC:
1718 break;
1719
1720 default:
1721 break;
1722 }
1723 error = sysctl_out_proc(p, req, flags);
1724 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1725 return (error);
1726 skip:
1727 PROC_UNLOCK(p);
1728 return (0);
1729 }
1730
1731 static int
sysctl_kern_proc(SYSCTL_HANDLER_ARGS)1732 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1733 {
1734 struct kern_proc_out_args iterarg;
1735 int *name = (int *)arg1;
1736 u_int namelen = arg2;
1737 struct proc *p;
1738 int flags, oid_number;
1739 int error = 0;
1740
1741 oid_number = oidp->oid_number;
1742 if (oid_number != KERN_PROC_ALL &&
1743 (oid_number & KERN_PROC_INC_THREAD) == 0)
1744 flags = KERN_PROC_NOTHREADS;
1745 else {
1746 flags = 0;
1747 oid_number &= ~KERN_PROC_INC_THREAD;
1748 }
1749 #ifdef COMPAT_FREEBSD32
1750 if (req->flags & SCTL_MASK32)
1751 flags |= KERN_PROC_MASK32;
1752 #endif
1753 if (oid_number == KERN_PROC_PID) {
1754 if (namelen != 1)
1755 return (EINVAL);
1756 error = sysctl_wire_old_buffer(req, 0);
1757 if (error)
1758 return (error);
1759 sx_slock(&proctree_lock);
1760 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1761 if (error == 0)
1762 error = sysctl_out_proc(p, req, flags);
1763 sx_sunlock(&proctree_lock);
1764 return (error);
1765 }
1766
1767 switch (oid_number) {
1768 case KERN_PROC_ALL:
1769 if (namelen != 0)
1770 return (EINVAL);
1771 break;
1772 case KERN_PROC_PROC:
1773 if (namelen != 0 && namelen != 1)
1774 return (EINVAL);
1775 break;
1776 default:
1777 if (namelen != 1)
1778 return (EINVAL);
1779 break;
1780 }
1781
1782 if (req->oldptr == NULL) {
1783 /* overestimate by 5 procs */
1784 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1785 if (error)
1786 return (error);
1787 } else {
1788 error = sysctl_wire_old_buffer(req, 0);
1789 if (error != 0)
1790 return (error);
1791 }
1792 iterarg.flags = flags;
1793 iterarg.oid_number = oid_number;
1794 iterarg.req = req;
1795 iterarg.name = name;
1796 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1797 return (error);
1798 }
1799
1800 struct pargs *
pargs_alloc(int len)1801 pargs_alloc(int len)
1802 {
1803 struct pargs *pa;
1804
1805 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1806 M_WAITOK);
1807 refcount_init(&pa->ar_ref, 1);
1808 pa->ar_length = len;
1809 return (pa);
1810 }
1811
1812 static void
pargs_free(struct pargs * pa)1813 pargs_free(struct pargs *pa)
1814 {
1815
1816 free(pa, M_PARGS);
1817 }
1818
1819 void
pargs_hold(struct pargs * pa)1820 pargs_hold(struct pargs *pa)
1821 {
1822
1823 if (pa == NULL)
1824 return;
1825 refcount_acquire(&pa->ar_ref);
1826 }
1827
1828 void
pargs_drop(struct pargs * pa)1829 pargs_drop(struct pargs *pa)
1830 {
1831
1832 if (pa == NULL)
1833 return;
1834 if (refcount_release(&pa->ar_ref))
1835 pargs_free(pa);
1836 }
1837
1838 static int
proc_read_string(struct thread * td,struct proc * p,const char * sptr,char * buf,size_t len)1839 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1840 size_t len)
1841 {
1842 ssize_t n;
1843
1844 /*
1845 * This may return a short read if the string is shorter than the chunk
1846 * and is aligned at the end of the page, and the following page is not
1847 * mapped.
1848 */
1849 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1850 if (n <= 0)
1851 return (ENOMEM);
1852 return (0);
1853 }
1854
1855 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1856
1857 enum proc_vector_type {
1858 PROC_ARG,
1859 PROC_ENV,
1860 PROC_AUX,
1861 };
1862
1863 #ifdef COMPAT_FREEBSD32
1864 static int
get_proc_vector32(struct thread * td,struct proc * p,char *** proc_vectorp,size_t * vsizep,enum proc_vector_type type)1865 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1866 size_t *vsizep, enum proc_vector_type type)
1867 {
1868 struct freebsd32_ps_strings pss;
1869 Elf32_Auxinfo aux;
1870 vm_offset_t vptr, ptr;
1871 uint32_t *proc_vector32;
1872 char **proc_vector;
1873 size_t vsize, size;
1874 int i, error;
1875
1876 error = 0;
1877 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1878 sizeof(pss))
1879 return (ENOMEM);
1880 switch (type) {
1881 case PROC_ARG:
1882 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1883 vsize = pss.ps_nargvstr;
1884 if (vsize > ARG_MAX)
1885 return (ENOEXEC);
1886 size = vsize * sizeof(int32_t);
1887 break;
1888 case PROC_ENV:
1889 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1890 vsize = pss.ps_nenvstr;
1891 if (vsize > ARG_MAX)
1892 return (ENOEXEC);
1893 size = vsize * sizeof(int32_t);
1894 break;
1895 case PROC_AUX:
1896 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1897 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1898 if (vptr % 4 != 0)
1899 return (ENOEXEC);
1900 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1901 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1902 sizeof(aux))
1903 return (ENOMEM);
1904 if (aux.a_type == AT_NULL)
1905 break;
1906 ptr += sizeof(aux);
1907 }
1908 if (aux.a_type != AT_NULL)
1909 return (ENOEXEC);
1910 vsize = i + 1;
1911 size = vsize * sizeof(aux);
1912 break;
1913 default:
1914 KASSERT(0, ("Wrong proc vector type: %d", type));
1915 return (EINVAL);
1916 }
1917 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1918 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1919 error = ENOMEM;
1920 goto done;
1921 }
1922 if (type == PROC_AUX) {
1923 *proc_vectorp = (char **)proc_vector32;
1924 *vsizep = vsize;
1925 return (0);
1926 }
1927 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1928 for (i = 0; i < (int)vsize; i++)
1929 proc_vector[i] = PTRIN(proc_vector32[i]);
1930 *proc_vectorp = proc_vector;
1931 *vsizep = vsize;
1932 done:
1933 free(proc_vector32, M_TEMP);
1934 return (error);
1935 }
1936 #endif
1937
1938 static int
get_proc_vector(struct thread * td,struct proc * p,char *** proc_vectorp,size_t * vsizep,enum proc_vector_type type)1939 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1940 size_t *vsizep, enum proc_vector_type type)
1941 {
1942 struct ps_strings pss;
1943 Elf_Auxinfo aux;
1944 vm_offset_t vptr, ptr;
1945 char **proc_vector;
1946 size_t vsize, size;
1947 int i;
1948
1949 #ifdef COMPAT_FREEBSD32
1950 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1951 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1952 #endif
1953 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1954 sizeof(pss))
1955 return (ENOMEM);
1956 switch (type) {
1957 case PROC_ARG:
1958 vptr = (vm_offset_t)pss.ps_argvstr;
1959 vsize = pss.ps_nargvstr;
1960 if (vsize > ARG_MAX)
1961 return (ENOEXEC);
1962 size = vsize * sizeof(char *);
1963 break;
1964 case PROC_ENV:
1965 vptr = (vm_offset_t)pss.ps_envstr;
1966 vsize = pss.ps_nenvstr;
1967 if (vsize > ARG_MAX)
1968 return (ENOEXEC);
1969 size = vsize * sizeof(char *);
1970 break;
1971 case PROC_AUX:
1972 /*
1973 * The aux array is just above env array on the stack. Check
1974 * that the address is naturally aligned.
1975 */
1976 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1977 * sizeof(char *);
1978 #if __ELF_WORD_SIZE == 64
1979 if (vptr % sizeof(uint64_t) != 0)
1980 #else
1981 if (vptr % sizeof(uint32_t) != 0)
1982 #endif
1983 return (ENOEXEC);
1984 /*
1985 * We count the array size reading the aux vectors from the
1986 * stack until AT_NULL vector is returned. So (to keep the code
1987 * simple) we read the process stack twice: the first time here
1988 * to find the size and the second time when copying the vectors
1989 * to the allocated proc_vector.
1990 */
1991 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1992 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1993 sizeof(aux))
1994 return (ENOMEM);
1995 if (aux.a_type == AT_NULL)
1996 break;
1997 ptr += sizeof(aux);
1998 }
1999 /*
2000 * If the PROC_AUXV_MAX entries are iterated over, and we have
2001 * not reached AT_NULL, it is most likely we are reading wrong
2002 * data: either the process doesn't have auxv array or data has
2003 * been modified. Return the error in this case.
2004 */
2005 if (aux.a_type != AT_NULL)
2006 return (ENOEXEC);
2007 vsize = i + 1;
2008 size = vsize * sizeof(aux);
2009 break;
2010 default:
2011 KASSERT(0, ("Wrong proc vector type: %d", type));
2012 return (EINVAL); /* In case we are built without INVARIANTS. */
2013 }
2014 proc_vector = malloc(size, M_TEMP, M_WAITOK);
2015 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
2016 free(proc_vector, M_TEMP);
2017 return (ENOMEM);
2018 }
2019 *proc_vectorp = proc_vector;
2020 *vsizep = vsize;
2021
2022 return (0);
2023 }
2024
2025 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
2026
2027 static int
get_ps_strings(struct thread * td,struct proc * p,struct sbuf * sb,enum proc_vector_type type)2028 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
2029 enum proc_vector_type type)
2030 {
2031 size_t done, len, nchr, vsize;
2032 int error, i;
2033 char **proc_vector, *sptr;
2034 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2035
2036 PROC_ASSERT_HELD(p);
2037
2038 /*
2039 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2040 */
2041 nchr = 2 * (PATH_MAX + ARG_MAX);
2042
2043 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2044 if (error != 0)
2045 return (error);
2046 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2047 /*
2048 * The program may have scribbled into its argv array, e.g. to
2049 * remove some arguments. If that has happened, break out
2050 * before trying to read from NULL.
2051 */
2052 if (proc_vector[i] == NULL)
2053 break;
2054 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2055 error = proc_read_string(td, p, sptr, pss_string,
2056 sizeof(pss_string));
2057 if (error != 0)
2058 goto done;
2059 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2060 if (done + len >= nchr)
2061 len = nchr - done - 1;
2062 sbuf_bcat(sb, pss_string, len);
2063 if (len != GET_PS_STRINGS_CHUNK_SZ)
2064 break;
2065 done += GET_PS_STRINGS_CHUNK_SZ;
2066 }
2067 sbuf_bcat(sb, "", 1);
2068 done += len + 1;
2069 }
2070 done:
2071 free(proc_vector, M_TEMP);
2072 return (error);
2073 }
2074
2075 int
proc_getargv(struct thread * td,struct proc * p,struct sbuf * sb)2076 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2077 {
2078
2079 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2080 }
2081
2082 int
proc_getenvv(struct thread * td,struct proc * p,struct sbuf * sb)2083 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2084 {
2085
2086 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2087 }
2088
2089 int
proc_getauxv(struct thread * td,struct proc * p,struct sbuf * sb)2090 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2091 {
2092 size_t vsize, size;
2093 char **auxv;
2094 int error;
2095
2096 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2097 if (error == 0) {
2098 #ifdef COMPAT_FREEBSD32
2099 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2100 size = vsize * sizeof(Elf32_Auxinfo);
2101 else
2102 #endif
2103 size = vsize * sizeof(Elf_Auxinfo);
2104 if (sbuf_bcat(sb, auxv, size) != 0)
2105 error = ENOMEM;
2106 free(auxv, M_TEMP);
2107 }
2108 return (error);
2109 }
2110
2111 /*
2112 * This sysctl allows a process to retrieve the argument list or process
2113 * title for another process without groping around in the address space
2114 * of the other process. It also allow a process to set its own "process
2115 * title to a string of its own choice.
2116 */
2117 static int
sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)2118 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2119 {
2120 int *name = (int *)arg1;
2121 u_int namelen = arg2;
2122 struct pargs *newpa, *pa;
2123 struct proc *p;
2124 struct sbuf sb;
2125 int flags, error = 0, error2;
2126 pid_t pid;
2127
2128 if (namelen != 1)
2129 return (EINVAL);
2130
2131 p = curproc;
2132 pid = (pid_t)name[0];
2133 if (pid == -1) {
2134 pid = p->p_pid;
2135 }
2136
2137 /*
2138 * If the query is for this process and it is single-threaded, there
2139 * is nobody to modify pargs, thus we can just read.
2140 */
2141 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2142 (pa = p->p_args) != NULL)
2143 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2144
2145 flags = PGET_CANSEE;
2146 if (req->newptr != NULL)
2147 flags |= PGET_ISCURRENT;
2148 error = pget(pid, flags, &p);
2149 if (error)
2150 return (error);
2151
2152 pa = p->p_args;
2153 if (pa != NULL) {
2154 pargs_hold(pa);
2155 PROC_UNLOCK(p);
2156 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2157 pargs_drop(pa);
2158 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2159 _PHOLD(p);
2160 PROC_UNLOCK(p);
2161 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2162 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2163 error = proc_getargv(curthread, p, &sb);
2164 error2 = sbuf_finish(&sb);
2165 PRELE(p);
2166 sbuf_delete(&sb);
2167 if (error == 0 && error2 != 0)
2168 error = error2;
2169 } else {
2170 PROC_UNLOCK(p);
2171 }
2172 if (error != 0 || req->newptr == NULL)
2173 return (error);
2174
2175 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2176 return (ENOMEM);
2177
2178 if (req->newlen == 0) {
2179 /*
2180 * Clear the argument pointer, so that we'll fetch arguments
2181 * with proc_getargv() until further notice.
2182 */
2183 newpa = NULL;
2184 } else {
2185 newpa = pargs_alloc(req->newlen);
2186 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2187 if (error != 0) {
2188 pargs_free(newpa);
2189 return (error);
2190 }
2191 }
2192 PROC_LOCK(p);
2193 pa = p->p_args;
2194 p->p_args = newpa;
2195 PROC_UNLOCK(p);
2196 pargs_drop(pa);
2197 return (0);
2198 }
2199
2200 /*
2201 * This sysctl allows a process to retrieve environment of another process.
2202 */
2203 static int
sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)2204 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2205 {
2206 int *name = (int *)arg1;
2207 u_int namelen = arg2;
2208 struct proc *p;
2209 struct sbuf sb;
2210 int error, error2;
2211
2212 if (namelen != 1)
2213 return (EINVAL);
2214
2215 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2216 if (error != 0)
2217 return (error);
2218 if ((p->p_flag & P_SYSTEM) != 0) {
2219 PRELE(p);
2220 return (0);
2221 }
2222
2223 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2224 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2225 error = proc_getenvv(curthread, p, &sb);
2226 error2 = sbuf_finish(&sb);
2227 PRELE(p);
2228 sbuf_delete(&sb);
2229 return (error != 0 ? error : error2);
2230 }
2231
2232 /*
2233 * This sysctl allows a process to retrieve ELF auxiliary vector of
2234 * another process.
2235 */
2236 static int
sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)2237 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2238 {
2239 int *name = (int *)arg1;
2240 u_int namelen = arg2;
2241 struct proc *p;
2242 struct sbuf sb;
2243 int error, error2;
2244
2245 if (namelen != 1)
2246 return (EINVAL);
2247
2248 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2249 if (error != 0)
2250 return (error);
2251 if ((p->p_flag & P_SYSTEM) != 0) {
2252 PRELE(p);
2253 return (0);
2254 }
2255 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2256 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2257 error = proc_getauxv(curthread, p, &sb);
2258 error2 = sbuf_finish(&sb);
2259 PRELE(p);
2260 sbuf_delete(&sb);
2261 return (error != 0 ? error : error2);
2262 }
2263
2264 /*
2265 * Look up the canonical executable path running in the specified process.
2266 * It tries to return the same hardlink name as was used for execve(2).
2267 * This allows the programs that modify their behavior based on their progname,
2268 * to operate correctly.
2269 *
2270 * Result is returned in retbuf, it must not be freed, similar to vn_fullpath()
2271 * calling conventions.
2272 * binname is a pointer to temporary string buffer of length MAXPATHLEN,
2273 * allocated and freed by caller.
2274 * freebuf should be freed by caller, from the M_TEMP malloc type.
2275 */
2276 int
proc_get_binpath(struct proc * p,char * binname,char ** retbuf,char ** freebuf)2277 proc_get_binpath(struct proc *p, char *binname, char **retbuf,
2278 char **freebuf)
2279 {
2280 struct nameidata nd;
2281 struct vnode *vp, *dvp;
2282 size_t freepath_size;
2283 int error;
2284 bool do_fullpath;
2285
2286 PROC_LOCK_ASSERT(p, MA_OWNED);
2287
2288 vp = p->p_textvp;
2289 if (vp == NULL) {
2290 PROC_UNLOCK(p);
2291 *retbuf = "";
2292 *freebuf = NULL;
2293 return (0);
2294 }
2295 vref(vp);
2296 dvp = p->p_textdvp;
2297 if (dvp != NULL)
2298 vref(dvp);
2299 if (p->p_binname != NULL)
2300 strlcpy(binname, p->p_binname, MAXPATHLEN);
2301 PROC_UNLOCK(p);
2302
2303 do_fullpath = true;
2304 *freebuf = NULL;
2305 if (dvp != NULL && binname[0] != '\0') {
2306 freepath_size = MAXPATHLEN;
2307 if (vn_fullpath_hardlink(vp, dvp, binname, strlen(binname),
2308 retbuf, freebuf, &freepath_size) == 0) {
2309 /*
2310 * Recheck the looked up path. The binary
2311 * might have been renamed or replaced, in
2312 * which case we should not report old name.
2313 */
2314 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, *retbuf);
2315 error = namei(&nd);
2316 if (error == 0) {
2317 if (nd.ni_vp == vp)
2318 do_fullpath = false;
2319 vrele(nd.ni_vp);
2320 NDFREE_PNBUF(&nd);
2321 }
2322 }
2323 }
2324 if (do_fullpath) {
2325 free(*freebuf, M_TEMP);
2326 *freebuf = NULL;
2327 error = vn_fullpath(vp, retbuf, freebuf);
2328 }
2329 vrele(vp);
2330 if (dvp != NULL)
2331 vrele(dvp);
2332 return (error);
2333 }
2334
2335 /*
2336 * This sysctl allows a process to retrieve the path of the executable for
2337 * itself or another process.
2338 */
2339 static int
sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)2340 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2341 {
2342 pid_t *pidp = (pid_t *)arg1;
2343 unsigned int arglen = arg2;
2344 struct proc *p;
2345 char *retbuf, *freebuf, *binname;
2346 int error;
2347
2348 if (arglen != 1)
2349 return (EINVAL);
2350 binname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2351 binname[0] = '\0';
2352 if (*pidp == -1) { /* -1 means this process */
2353 error = 0;
2354 p = req->td->td_proc;
2355 PROC_LOCK(p);
2356 } else {
2357 error = pget(*pidp, PGET_CANSEE, &p);
2358 }
2359
2360 if (error == 0)
2361 error = proc_get_binpath(p, binname, &retbuf, &freebuf);
2362 free(binname, M_TEMP);
2363 if (error != 0)
2364 return (error);
2365 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2366 free(freebuf, M_TEMP);
2367 return (error);
2368 }
2369
2370 static int
sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)2371 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2372 {
2373 struct proc *p;
2374 char *sv_name;
2375 int *name;
2376 int namelen;
2377 int error;
2378
2379 namelen = arg2;
2380 if (namelen != 1)
2381 return (EINVAL);
2382
2383 name = (int *)arg1;
2384 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2385 if (error != 0)
2386 return (error);
2387 sv_name = p->p_sysent->sv_name;
2388 PROC_UNLOCK(p);
2389 return (sysctl_handle_string(oidp, sv_name, 0, req));
2390 }
2391
2392 #ifdef KINFO_OVMENTRY_SIZE
2393 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2394 #endif
2395
2396 #ifdef COMPAT_FREEBSD7
2397 static int
sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)2398 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2399 {
2400 vm_map_entry_t entry, tmp_entry;
2401 unsigned int last_timestamp, namelen;
2402 char *fullpath, *freepath;
2403 struct kinfo_ovmentry *kve;
2404 struct vattr va;
2405 struct ucred *cred;
2406 int error, *name;
2407 struct vnode *vp;
2408 struct proc *p;
2409 vm_map_t map;
2410 struct vmspace *vm;
2411
2412 namelen = arg2;
2413 if (namelen != 1)
2414 return (EINVAL);
2415
2416 name = (int *)arg1;
2417 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2418 if (error != 0)
2419 return (error);
2420 vm = vmspace_acquire_ref(p);
2421 if (vm == NULL) {
2422 PRELE(p);
2423 return (ESRCH);
2424 }
2425 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2426
2427 map = &vm->vm_map;
2428 vm_map_lock_read(map);
2429 VM_MAP_ENTRY_FOREACH(entry, map) {
2430 vm_object_t obj, tobj, lobj;
2431 vm_offset_t addr;
2432
2433 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2434 continue;
2435
2436 bzero(kve, sizeof(*kve));
2437 kve->kve_structsize = sizeof(*kve);
2438
2439 kve->kve_private_resident = 0;
2440 obj = entry->object.vm_object;
2441 if (obj != NULL) {
2442 VM_OBJECT_RLOCK(obj);
2443 if (obj->shadow_count == 1)
2444 kve->kve_private_resident =
2445 obj->resident_page_count;
2446 }
2447 kve->kve_resident = 0;
2448 addr = entry->start;
2449 while (addr < entry->end) {
2450 if (pmap_extract(map->pmap, addr))
2451 kve->kve_resident++;
2452 addr += PAGE_SIZE;
2453 }
2454
2455 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2456 if (tobj != obj) {
2457 VM_OBJECT_RLOCK(tobj);
2458 kve->kve_offset += tobj->backing_object_offset;
2459 }
2460 if (lobj != obj)
2461 VM_OBJECT_RUNLOCK(lobj);
2462 lobj = tobj;
2463 }
2464
2465 kve->kve_start = (void*)entry->start;
2466 kve->kve_end = (void*)entry->end;
2467 kve->kve_offset += (off_t)entry->offset;
2468
2469 if (entry->protection & VM_PROT_READ)
2470 kve->kve_protection |= KVME_PROT_READ;
2471 if (entry->protection & VM_PROT_WRITE)
2472 kve->kve_protection |= KVME_PROT_WRITE;
2473 if (entry->protection & VM_PROT_EXECUTE)
2474 kve->kve_protection |= KVME_PROT_EXEC;
2475
2476 if (entry->eflags & MAP_ENTRY_COW)
2477 kve->kve_flags |= KVME_FLAG_COW;
2478 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2479 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2480 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2481 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2482
2483 last_timestamp = map->timestamp;
2484 vm_map_unlock_read(map);
2485
2486 kve->kve_fileid = 0;
2487 kve->kve_fsid = 0;
2488 freepath = NULL;
2489 fullpath = "";
2490 if (lobj) {
2491 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2492 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2493 kve->kve_type = KVME_TYPE_UNKNOWN;
2494 if (vp != NULL)
2495 vref(vp);
2496 if (lobj != obj)
2497 VM_OBJECT_RUNLOCK(lobj);
2498
2499 kve->kve_ref_count = obj->ref_count;
2500 kve->kve_shadow_count = obj->shadow_count;
2501 VM_OBJECT_RUNLOCK(obj);
2502 if (vp != NULL) {
2503 vn_fullpath(vp, &fullpath, &freepath);
2504 cred = curthread->td_ucred;
2505 vn_lock(vp, LK_SHARED | LK_RETRY);
2506 if (VOP_GETATTR(vp, &va, cred) == 0) {
2507 kve->kve_fileid = va.va_fileid;
2508 /* truncate */
2509 kve->kve_fsid = va.va_fsid;
2510 }
2511 vput(vp);
2512 }
2513 } else {
2514 kve->kve_type = KVME_TYPE_NONE;
2515 kve->kve_ref_count = 0;
2516 kve->kve_shadow_count = 0;
2517 }
2518
2519 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2520 if (freepath != NULL)
2521 free(freepath, M_TEMP);
2522
2523 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2524 vm_map_lock_read(map);
2525 if (error)
2526 break;
2527 if (last_timestamp != map->timestamp) {
2528 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2529 entry = tmp_entry;
2530 }
2531 }
2532 vm_map_unlock_read(map);
2533 vmspace_free(vm);
2534 PRELE(p);
2535 free(kve, M_TEMP);
2536 return (error);
2537 }
2538 #endif /* COMPAT_FREEBSD7 */
2539
2540 #ifdef KINFO_VMENTRY_SIZE
2541 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2542 #endif
2543
2544 void
kern_proc_vmmap_resident(vm_map_t map,vm_map_entry_t entry,int * resident_count,bool * super)2545 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2546 int *resident_count, bool *super)
2547 {
2548 vm_object_t obj, tobj;
2549 vm_page_t m, m_adv;
2550 vm_offset_t addr;
2551 vm_paddr_t pa;
2552 vm_pindex_t pi, pi_adv, pindex;
2553 int incore;
2554
2555 *super = false;
2556 *resident_count = 0;
2557 if (vmmap_skip_res_cnt)
2558 return;
2559
2560 pa = 0;
2561 obj = entry->object.vm_object;
2562 addr = entry->start;
2563 m_adv = NULL;
2564 pi = OFF_TO_IDX(entry->offset);
2565 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2566 if (m_adv != NULL) {
2567 m = m_adv;
2568 } else {
2569 pi_adv = atop(entry->end - addr);
2570 pindex = pi;
2571 for (tobj = obj;; tobj = tobj->backing_object) {
2572 m = vm_radix_lookup_ge(&tobj->rtree, pindex);
2573 if (m != NULL) {
2574 if (m->pindex == pindex)
2575 break;
2576 if (pi_adv > m->pindex - pindex) {
2577 pi_adv = m->pindex - pindex;
2578 m_adv = m;
2579 }
2580 }
2581 if (tobj->backing_object == NULL)
2582 goto next;
2583 pindex += OFF_TO_IDX(tobj->
2584 backing_object_offset);
2585 }
2586 }
2587 m_adv = NULL;
2588 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2589 (addr & (pagesizes[1] - 1)) == 0 && (incore =
2590 pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2591 *super = true;
2592 /*
2593 * The virtual page might be smaller than the physical
2594 * page, so we use the page size reported by the pmap
2595 * rather than m->psind.
2596 */
2597 pi_adv = atop(pagesizes[incore >> MINCORE_PSIND_SHIFT]);
2598 } else {
2599 /*
2600 * We do not test the found page on validity.
2601 * Either the page is busy and being paged in,
2602 * or it was invalidated. The first case
2603 * should be counted as resident, the second
2604 * is not so clear; we do account both.
2605 */
2606 pi_adv = 1;
2607 }
2608 *resident_count += pi_adv;
2609 next:;
2610 }
2611 }
2612
2613 /*
2614 * Must be called with the process locked and will return unlocked.
2615 */
2616 int
kern_proc_vmmap_out(struct proc * p,struct sbuf * sb,ssize_t maxlen,int flags)2617 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2618 {
2619 vm_map_entry_t entry, tmp_entry;
2620 struct vattr va;
2621 vm_map_t map;
2622 vm_object_t lobj, nobj, obj, tobj;
2623 char *fullpath, *freepath;
2624 struct kinfo_vmentry *kve;
2625 struct ucred *cred;
2626 struct vnode *vp;
2627 struct vmspace *vm;
2628 vm_offset_t addr;
2629 unsigned int last_timestamp;
2630 int error;
2631 key_t key;
2632 unsigned short seq;
2633 bool guard, super;
2634
2635 PROC_LOCK_ASSERT(p, MA_OWNED);
2636
2637 _PHOLD(p);
2638 PROC_UNLOCK(p);
2639 vm = vmspace_acquire_ref(p);
2640 if (vm == NULL) {
2641 PRELE(p);
2642 return (ESRCH);
2643 }
2644 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2645
2646 error = 0;
2647 map = &vm->vm_map;
2648 vm_map_lock_read(map);
2649 VM_MAP_ENTRY_FOREACH(entry, map) {
2650 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2651 continue;
2652
2653 addr = entry->end;
2654 bzero(kve, sizeof(*kve));
2655 obj = entry->object.vm_object;
2656 if (obj != NULL) {
2657 if ((obj->flags & OBJ_ANON) != 0)
2658 kve->kve_obj = (uintptr_t)obj;
2659
2660 for (tobj = obj; tobj != NULL;
2661 tobj = tobj->backing_object) {
2662 VM_OBJECT_RLOCK(tobj);
2663 kve->kve_offset += tobj->backing_object_offset;
2664 lobj = tobj;
2665 }
2666 if (obj->backing_object == NULL)
2667 kve->kve_private_resident =
2668 obj->resident_page_count;
2669 kern_proc_vmmap_resident(map, entry,
2670 &kve->kve_resident, &super);
2671 if (super)
2672 kve->kve_flags |= KVME_FLAG_SUPER;
2673 for (tobj = obj; tobj != NULL; tobj = nobj) {
2674 nobj = tobj->backing_object;
2675 if (tobj != obj && tobj != lobj)
2676 VM_OBJECT_RUNLOCK(tobj);
2677 }
2678 } else {
2679 lobj = NULL;
2680 }
2681
2682 kve->kve_start = entry->start;
2683 kve->kve_end = entry->end;
2684 kve->kve_offset += entry->offset;
2685
2686 if (entry->protection & VM_PROT_READ)
2687 kve->kve_protection |= KVME_PROT_READ;
2688 if (entry->protection & VM_PROT_WRITE)
2689 kve->kve_protection |= KVME_PROT_WRITE;
2690 if (entry->protection & VM_PROT_EXECUTE)
2691 kve->kve_protection |= KVME_PROT_EXEC;
2692 if (entry->max_protection & VM_PROT_READ)
2693 kve->kve_protection |= KVME_MAX_PROT_READ;
2694 if (entry->max_protection & VM_PROT_WRITE)
2695 kve->kve_protection |= KVME_MAX_PROT_WRITE;
2696 if (entry->max_protection & VM_PROT_EXECUTE)
2697 kve->kve_protection |= KVME_MAX_PROT_EXEC;
2698
2699 if (entry->eflags & MAP_ENTRY_COW)
2700 kve->kve_flags |= KVME_FLAG_COW;
2701 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2702 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2703 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2704 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2705 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2706 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2707 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2708 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2709
2710 guard = (entry->eflags & MAP_ENTRY_GUARD) != 0;
2711
2712 last_timestamp = map->timestamp;
2713 vm_map_unlock_read(map);
2714
2715 freepath = NULL;
2716 fullpath = "";
2717 if (lobj != NULL) {
2718 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2719 if (vp != NULL)
2720 vref(vp);
2721 if (lobj != obj)
2722 VM_OBJECT_RUNLOCK(lobj);
2723
2724 kve->kve_ref_count = obj->ref_count;
2725 kve->kve_shadow_count = obj->shadow_count;
2726 if (obj->type == OBJT_DEVICE ||
2727 obj->type == OBJT_MGTDEVICE) {
2728 cdev_pager_get_path(obj, kve->kve_path,
2729 sizeof(kve->kve_path));
2730 }
2731 VM_OBJECT_RUNLOCK(obj);
2732 if ((lobj->flags & OBJ_SYSVSHM) != 0) {
2733 kve->kve_flags |= KVME_FLAG_SYSVSHM;
2734 shmobjinfo(lobj, &key, &seq);
2735 kve->kve_vn_fileid = key;
2736 kve->kve_vn_fsid_freebsd11 = seq;
2737 }
2738 if ((lobj->flags & OBJ_POSIXSHM) != 0) {
2739 kve->kve_flags |= KVME_FLAG_POSIXSHM;
2740 shm_get_path(lobj, kve->kve_path,
2741 sizeof(kve->kve_path));
2742 }
2743 if (vp != NULL) {
2744 vn_fullpath(vp, &fullpath, &freepath);
2745 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2746 cred = curthread->td_ucred;
2747 vn_lock(vp, LK_SHARED | LK_RETRY);
2748 if (VOP_GETATTR(vp, &va, cred) == 0) {
2749 kve->kve_vn_fileid = va.va_fileid;
2750 kve->kve_vn_fsid = va.va_fsid;
2751 kve->kve_vn_fsid_freebsd11 =
2752 kve->kve_vn_fsid; /* truncate */
2753 kve->kve_vn_mode =
2754 MAKEIMODE(va.va_type, va.va_mode);
2755 kve->kve_vn_size = va.va_size;
2756 kve->kve_vn_rdev = va.va_rdev;
2757 kve->kve_vn_rdev_freebsd11 =
2758 kve->kve_vn_rdev; /* truncate */
2759 kve->kve_status = KF_ATTR_VALID;
2760 }
2761 vput(vp);
2762 strlcpy(kve->kve_path, fullpath, sizeof(
2763 kve->kve_path));
2764 free(freepath, M_TEMP);
2765 }
2766 } else {
2767 kve->kve_type = guard ? KVME_TYPE_GUARD :
2768 KVME_TYPE_NONE;
2769 kve->kve_ref_count = 0;
2770 kve->kve_shadow_count = 0;
2771 }
2772
2773 /* Pack record size down */
2774 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2775 kve->kve_structsize =
2776 offsetof(struct kinfo_vmentry, kve_path) +
2777 strlen(kve->kve_path) + 1;
2778 else
2779 kve->kve_structsize = sizeof(*kve);
2780 kve->kve_structsize = roundup(kve->kve_structsize,
2781 sizeof(uint64_t));
2782
2783 /* Halt filling and truncate rather than exceeding maxlen */
2784 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2785 error = 0;
2786 vm_map_lock_read(map);
2787 break;
2788 } else if (maxlen != -1)
2789 maxlen -= kve->kve_structsize;
2790
2791 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2792 error = ENOMEM;
2793 vm_map_lock_read(map);
2794 if (error != 0)
2795 break;
2796 if (last_timestamp != map->timestamp) {
2797 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2798 entry = tmp_entry;
2799 }
2800 }
2801 vm_map_unlock_read(map);
2802 vmspace_free(vm);
2803 PRELE(p);
2804 free(kve, M_TEMP);
2805 return (error);
2806 }
2807
2808 static int
sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)2809 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2810 {
2811 struct proc *p;
2812 struct sbuf sb;
2813 u_int namelen;
2814 int error, error2, *name;
2815
2816 namelen = arg2;
2817 if (namelen != 1)
2818 return (EINVAL);
2819
2820 name = (int *)arg1;
2821 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2822 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2823 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2824 if (error != 0) {
2825 sbuf_delete(&sb);
2826 return (error);
2827 }
2828 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2829 error2 = sbuf_finish(&sb);
2830 sbuf_delete(&sb);
2831 return (error != 0 ? error : error2);
2832 }
2833
2834 #if defined(STACK) || defined(DDB)
2835 static int
sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)2836 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2837 {
2838 struct kinfo_kstack *kkstp;
2839 int error, i, *name, numthreads;
2840 lwpid_t *lwpidarray;
2841 struct thread *td;
2842 struct stack *st;
2843 struct sbuf sb;
2844 struct proc *p;
2845 u_int namelen;
2846
2847 namelen = arg2;
2848 if (namelen != 1)
2849 return (EINVAL);
2850
2851 name = (int *)arg1;
2852 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2853 if (error != 0)
2854 return (error);
2855
2856 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2857 st = stack_create(M_WAITOK);
2858
2859 lwpidarray = NULL;
2860 PROC_LOCK(p);
2861 do {
2862 if (lwpidarray != NULL) {
2863 free(lwpidarray, M_TEMP);
2864 lwpidarray = NULL;
2865 }
2866 numthreads = p->p_numthreads;
2867 PROC_UNLOCK(p);
2868 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2869 M_WAITOK | M_ZERO);
2870 PROC_LOCK(p);
2871 } while (numthreads < p->p_numthreads);
2872
2873 /*
2874 * XXXRW: During the below loop, execve(2) and countless other sorts
2875 * of changes could have taken place. Should we check to see if the
2876 * vmspace has been replaced, or the like, in order to prevent
2877 * giving a snapshot that spans, say, execve(2), with some threads
2878 * before and some after? Among other things, the credentials could
2879 * have changed, in which case the right to extract debug info might
2880 * no longer be assured.
2881 */
2882 i = 0;
2883 FOREACH_THREAD_IN_PROC(p, td) {
2884 KASSERT(i < numthreads,
2885 ("sysctl_kern_proc_kstack: numthreads"));
2886 lwpidarray[i] = td->td_tid;
2887 i++;
2888 }
2889 PROC_UNLOCK(p);
2890 numthreads = i;
2891 for (i = 0; i < numthreads; i++) {
2892 td = tdfind(lwpidarray[i], p->p_pid);
2893 if (td == NULL) {
2894 continue;
2895 }
2896 bzero(kkstp, sizeof(*kkstp));
2897 (void)sbuf_new(&sb, kkstp->kkst_trace,
2898 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2899 thread_lock(td);
2900 kkstp->kkst_tid = td->td_tid;
2901 if (stack_save_td(st, td) == 0)
2902 kkstp->kkst_state = KKST_STATE_STACKOK;
2903 else
2904 kkstp->kkst_state = KKST_STATE_RUNNING;
2905 thread_unlock(td);
2906 PROC_UNLOCK(p);
2907 stack_sbuf_print(&sb, st);
2908 sbuf_finish(&sb);
2909 sbuf_delete(&sb);
2910 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2911 if (error)
2912 break;
2913 }
2914 PRELE(p);
2915 if (lwpidarray != NULL)
2916 free(lwpidarray, M_TEMP);
2917 stack_destroy(st);
2918 free(kkstp, M_TEMP);
2919 return (error);
2920 }
2921 #endif
2922
2923 /*
2924 * This sysctl allows a process to retrieve the full list of groups from
2925 * itself or another process.
2926 */
2927 static int
sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)2928 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2929 {
2930 pid_t *pidp = (pid_t *)arg1;
2931 unsigned int arglen = arg2;
2932 struct proc *p;
2933 struct ucred *cred;
2934 int error;
2935
2936 if (arglen != 1)
2937 return (EINVAL);
2938 if (*pidp == -1) { /* -1 means this process */
2939 p = req->td->td_proc;
2940 PROC_LOCK(p);
2941 } else {
2942 error = pget(*pidp, PGET_CANSEE, &p);
2943 if (error != 0)
2944 return (error);
2945 }
2946
2947 cred = crhold(p->p_ucred);
2948 PROC_UNLOCK(p);
2949
2950 error = SYSCTL_OUT(req, &cred->cr_gid, sizeof(gid_t));
2951 if (error == 0)
2952 error = SYSCTL_OUT(req, cred->cr_groups,
2953 cred->cr_ngroups * sizeof(gid_t));
2954
2955 crfree(cred);
2956 return (error);
2957 }
2958
2959 /*
2960 * This sysctl allows a process to retrieve or/and set the resource limit for
2961 * another process.
2962 */
2963 static int
sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)2964 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2965 {
2966 int *name = (int *)arg1;
2967 u_int namelen = arg2;
2968 struct rlimit rlim;
2969 struct proc *p;
2970 u_int which;
2971 int flags, error;
2972
2973 if (namelen != 2)
2974 return (EINVAL);
2975
2976 which = (u_int)name[1];
2977 if (which >= RLIM_NLIMITS)
2978 return (EINVAL);
2979
2980 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2981 return (EINVAL);
2982
2983 flags = PGET_HOLD | PGET_NOTWEXIT;
2984 if (req->newptr != NULL)
2985 flags |= PGET_CANDEBUG;
2986 else
2987 flags |= PGET_CANSEE;
2988 error = pget((pid_t)name[0], flags, &p);
2989 if (error != 0)
2990 return (error);
2991
2992 /*
2993 * Retrieve limit.
2994 */
2995 if (req->oldptr != NULL) {
2996 PROC_LOCK(p);
2997 lim_rlimit_proc(p, which, &rlim);
2998 PROC_UNLOCK(p);
2999 }
3000 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
3001 if (error != 0)
3002 goto errout;
3003
3004 /*
3005 * Set limit.
3006 */
3007 if (req->newptr != NULL) {
3008 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
3009 if (error == 0)
3010 error = kern_proc_setrlimit(curthread, p, which, &rlim);
3011 }
3012
3013 errout:
3014 PRELE(p);
3015 return (error);
3016 }
3017
3018 /*
3019 * This sysctl allows a process to retrieve ps_strings structure location of
3020 * another process.
3021 */
3022 static int
sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)3023 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
3024 {
3025 int *name = (int *)arg1;
3026 u_int namelen = arg2;
3027 struct proc *p;
3028 vm_offset_t ps_strings;
3029 int error;
3030 #ifdef COMPAT_FREEBSD32
3031 uint32_t ps_strings32;
3032 #endif
3033
3034 if (namelen != 1)
3035 return (EINVAL);
3036
3037 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3038 if (error != 0)
3039 return (error);
3040 #ifdef COMPAT_FREEBSD32
3041 if ((req->flags & SCTL_MASK32) != 0) {
3042 /*
3043 * We return 0 if the 32 bit emulation request is for a 64 bit
3044 * process.
3045 */
3046 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
3047 PTROUT(PROC_PS_STRINGS(p)) : 0;
3048 PROC_UNLOCK(p);
3049 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
3050 return (error);
3051 }
3052 #endif
3053 ps_strings = PROC_PS_STRINGS(p);
3054 PROC_UNLOCK(p);
3055 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
3056 return (error);
3057 }
3058
3059 /*
3060 * This sysctl allows a process to retrieve umask of another process.
3061 */
3062 static int
sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)3063 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
3064 {
3065 int *name = (int *)arg1;
3066 u_int namelen = arg2;
3067 struct proc *p;
3068 int error;
3069 u_short cmask;
3070 pid_t pid;
3071
3072 if (namelen != 1)
3073 return (EINVAL);
3074
3075 pid = (pid_t)name[0];
3076 p = curproc;
3077 if (pid == p->p_pid || pid == 0) {
3078 cmask = p->p_pd->pd_cmask;
3079 goto out;
3080 }
3081
3082 error = pget(pid, PGET_WANTREAD, &p);
3083 if (error != 0)
3084 return (error);
3085
3086 cmask = p->p_pd->pd_cmask;
3087 PRELE(p);
3088 out:
3089 error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
3090 return (error);
3091 }
3092
3093 /*
3094 * This sysctl allows a process to set and retrieve binary osreldate of
3095 * another process.
3096 */
3097 static int
sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)3098 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3099 {
3100 int *name = (int *)arg1;
3101 u_int namelen = arg2;
3102 struct proc *p;
3103 int flags, error, osrel;
3104
3105 if (namelen != 1)
3106 return (EINVAL);
3107
3108 if (req->newptr != NULL && req->newlen != sizeof(osrel))
3109 return (EINVAL);
3110
3111 flags = PGET_HOLD | PGET_NOTWEXIT;
3112 if (req->newptr != NULL)
3113 flags |= PGET_CANDEBUG;
3114 else
3115 flags |= PGET_CANSEE;
3116 error = pget((pid_t)name[0], flags, &p);
3117 if (error != 0)
3118 return (error);
3119
3120 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3121 if (error != 0)
3122 goto errout;
3123
3124 if (req->newptr != NULL) {
3125 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3126 if (error != 0)
3127 goto errout;
3128 if (osrel < 0) {
3129 error = EINVAL;
3130 goto errout;
3131 }
3132 p->p_osrel = osrel;
3133 }
3134 errout:
3135 PRELE(p);
3136 return (error);
3137 }
3138
3139 static int
sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)3140 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
3141 {
3142 int *name = (int *)arg1;
3143 u_int namelen = arg2;
3144 struct proc *p;
3145 struct kinfo_sigtramp kst;
3146 const struct sysentvec *sv;
3147 int error;
3148 #ifdef COMPAT_FREEBSD32
3149 struct kinfo_sigtramp32 kst32;
3150 #endif
3151
3152 if (namelen != 1)
3153 return (EINVAL);
3154
3155 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3156 if (error != 0)
3157 return (error);
3158 sv = p->p_sysent;
3159 #ifdef COMPAT_FREEBSD32
3160 if ((req->flags & SCTL_MASK32) != 0) {
3161 bzero(&kst32, sizeof(kst32));
3162 if (SV_PROC_FLAG(p, SV_ILP32)) {
3163 if (PROC_HAS_SHP(p)) {
3164 kst32.ksigtramp_start = PROC_SIGCODE(p);
3165 kst32.ksigtramp_end = kst32.ksigtramp_start +
3166 ((sv->sv_flags & SV_DSO_SIG) == 0 ?
3167 *sv->sv_szsigcode :
3168 (uintptr_t)sv->sv_szsigcode);
3169 } else {
3170 kst32.ksigtramp_start = PROC_PS_STRINGS(p) -
3171 *sv->sv_szsigcode;
3172 kst32.ksigtramp_end = PROC_PS_STRINGS(p);
3173 }
3174 }
3175 PROC_UNLOCK(p);
3176 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3177 return (error);
3178 }
3179 #endif
3180 bzero(&kst, sizeof(kst));
3181 if (PROC_HAS_SHP(p)) {
3182 kst.ksigtramp_start = (char *)PROC_SIGCODE(p);
3183 kst.ksigtramp_end = (char *)kst.ksigtramp_start +
3184 ((sv->sv_flags & SV_DSO_SIG) == 0 ? *sv->sv_szsigcode :
3185 (uintptr_t)sv->sv_szsigcode);
3186 } else {
3187 kst.ksigtramp_start = (char *)PROC_PS_STRINGS(p) -
3188 *sv->sv_szsigcode;
3189 kst.ksigtramp_end = (char *)PROC_PS_STRINGS(p);
3190 }
3191 PROC_UNLOCK(p);
3192 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3193 return (error);
3194 }
3195
3196 static int
sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)3197 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3198 {
3199 int *name = (int *)arg1;
3200 u_int namelen = arg2;
3201 pid_t pid;
3202 struct proc *p;
3203 struct thread *td1;
3204 uintptr_t addr;
3205 #ifdef COMPAT_FREEBSD32
3206 uint32_t addr32;
3207 #endif
3208 int error;
3209
3210 if (namelen != 1 || req->newptr != NULL)
3211 return (EINVAL);
3212
3213 pid = (pid_t)name[0];
3214 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3215 if (error != 0)
3216 return (error);
3217
3218 PROC_LOCK(p);
3219 #ifdef COMPAT_FREEBSD32
3220 if (SV_CURPROC_FLAG(SV_ILP32)) {
3221 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3222 error = EINVAL;
3223 goto errlocked;
3224 }
3225 }
3226 #endif
3227 if (pid <= PID_MAX) {
3228 td1 = FIRST_THREAD_IN_PROC(p);
3229 } else {
3230 FOREACH_THREAD_IN_PROC(p, td1) {
3231 if (td1->td_tid == pid)
3232 break;
3233 }
3234 }
3235 if (td1 == NULL) {
3236 error = ESRCH;
3237 goto errlocked;
3238 }
3239 /*
3240 * The access to the private thread flags. It is fine as far
3241 * as no out-of-thin-air values are read from td_pflags, and
3242 * usermode read of the td_sigblock_ptr is racy inherently,
3243 * since target process might have already changed it
3244 * meantime.
3245 */
3246 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3247 addr = (uintptr_t)td1->td_sigblock_ptr;
3248 else
3249 error = ENOTTY;
3250
3251 errlocked:
3252 _PRELE(p);
3253 PROC_UNLOCK(p);
3254 if (error != 0)
3255 return (error);
3256
3257 #ifdef COMPAT_FREEBSD32
3258 if (SV_CURPROC_FLAG(SV_ILP32)) {
3259 addr32 = addr;
3260 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3261 } else
3262 #endif
3263 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3264 return (error);
3265 }
3266
3267 static int
sysctl_kern_proc_vm_layout(SYSCTL_HANDLER_ARGS)3268 sysctl_kern_proc_vm_layout(SYSCTL_HANDLER_ARGS)
3269 {
3270 struct kinfo_vm_layout kvm;
3271 struct proc *p;
3272 struct vmspace *vmspace;
3273 int error, *name;
3274
3275 name = (int *)arg1;
3276 if ((u_int)arg2 != 1)
3277 return (EINVAL);
3278
3279 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3280 if (error != 0)
3281 return (error);
3282 #ifdef COMPAT_FREEBSD32
3283 if (SV_CURPROC_FLAG(SV_ILP32)) {
3284 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3285 PROC_UNLOCK(p);
3286 return (EINVAL);
3287 }
3288 }
3289 #endif
3290 vmspace = vmspace_acquire_ref(p);
3291 PROC_UNLOCK(p);
3292
3293 memset(&kvm, 0, sizeof(kvm));
3294 kvm.kvm_min_user_addr = vm_map_min(&vmspace->vm_map);
3295 kvm.kvm_max_user_addr = vm_map_max(&vmspace->vm_map);
3296 kvm.kvm_text_addr = (uintptr_t)vmspace->vm_taddr;
3297 kvm.kvm_text_size = vmspace->vm_tsize;
3298 kvm.kvm_data_addr = (uintptr_t)vmspace->vm_daddr;
3299 kvm.kvm_data_size = vmspace->vm_dsize;
3300 kvm.kvm_stack_addr = (uintptr_t)vmspace->vm_maxsaddr;
3301 kvm.kvm_stack_size = vmspace->vm_ssize;
3302 kvm.kvm_shp_addr = vmspace->vm_shp_base;
3303 kvm.kvm_shp_size = p->p_sysent->sv_shared_page_len;
3304 if ((vmspace->vm_map.flags & MAP_WIREFUTURE) != 0)
3305 kvm.kvm_map_flags |= KMAP_FLAG_WIREFUTURE;
3306 if ((vmspace->vm_map.flags & MAP_ASLR) != 0)
3307 kvm.kvm_map_flags |= KMAP_FLAG_ASLR;
3308 if ((vmspace->vm_map.flags & MAP_ASLR_IGNSTART) != 0)
3309 kvm.kvm_map_flags |= KMAP_FLAG_ASLR_IGNSTART;
3310 if ((vmspace->vm_map.flags & MAP_WXORX) != 0)
3311 kvm.kvm_map_flags |= KMAP_FLAG_WXORX;
3312 if ((vmspace->vm_map.flags & MAP_ASLR_STACK) != 0)
3313 kvm.kvm_map_flags |= KMAP_FLAG_ASLR_STACK;
3314 if (vmspace->vm_shp_base != p->p_sysent->sv_shared_page_base &&
3315 PROC_HAS_SHP(p))
3316 kvm.kvm_map_flags |= KMAP_FLAG_ASLR_SHARED_PAGE;
3317
3318 #ifdef COMPAT_FREEBSD32
3319 if (SV_CURPROC_FLAG(SV_ILP32)) {
3320 struct kinfo_vm_layout32 kvm32;
3321
3322 memset(&kvm32, 0, sizeof(kvm32));
3323 kvm32.kvm_min_user_addr = (uint32_t)kvm.kvm_min_user_addr;
3324 kvm32.kvm_max_user_addr = (uint32_t)kvm.kvm_max_user_addr;
3325 kvm32.kvm_text_addr = (uint32_t)kvm.kvm_text_addr;
3326 kvm32.kvm_text_size = (uint32_t)kvm.kvm_text_size;
3327 kvm32.kvm_data_addr = (uint32_t)kvm.kvm_data_addr;
3328 kvm32.kvm_data_size = (uint32_t)kvm.kvm_data_size;
3329 kvm32.kvm_stack_addr = (uint32_t)kvm.kvm_stack_addr;
3330 kvm32.kvm_stack_size = (uint32_t)kvm.kvm_stack_size;
3331 kvm32.kvm_shp_addr = (uint32_t)kvm.kvm_shp_addr;
3332 kvm32.kvm_shp_size = (uint32_t)kvm.kvm_shp_size;
3333 kvm32.kvm_map_flags = kvm.kvm_map_flags;
3334 error = SYSCTL_OUT(req, &kvm32, sizeof(kvm32));
3335 goto out;
3336 }
3337 #endif
3338
3339 error = SYSCTL_OUT(req, &kvm, sizeof(kvm));
3340 #ifdef COMPAT_FREEBSD32
3341 out:
3342 #endif
3343 vmspace_free(vmspace);
3344 return (error);
3345 }
3346
3347 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3348 "Process table");
3349
3350 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3351 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3352 "Return entire process table");
3353
3354 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3355 sysctl_kern_proc, "Process table");
3356
3357 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3358 sysctl_kern_proc, "Process table");
3359
3360 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3361 sysctl_kern_proc, "Process table");
3362
3363 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3364 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3365
3366 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3367 sysctl_kern_proc, "Process table");
3368
3369 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3370 sysctl_kern_proc, "Process table");
3371
3372 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3373 sysctl_kern_proc, "Process table");
3374
3375 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3376 sysctl_kern_proc, "Process table");
3377
3378 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3379 sysctl_kern_proc, "Return process table, no threads");
3380
3381 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3382 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3383 sysctl_kern_proc_args, "Process argument list");
3384
3385 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3386 sysctl_kern_proc_env, "Process environment");
3387
3388 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3389 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3390
3391 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3392 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3393
3394 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3395 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3396 "Process syscall vector name (ABI type)");
3397
3398 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3399 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3400
3401 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3402 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3403
3404 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3405 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3406
3407 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3408 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3409
3410 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3411 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3412
3413 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3414 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3415
3416 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3417 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3418
3419 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3420 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3421
3422 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3423 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3424 "Return process table, including threads");
3425
3426 #ifdef COMPAT_FREEBSD7
3427 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3428 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3429 #endif
3430
3431 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3432 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3433
3434 #if defined(STACK) || defined(DDB)
3435 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3436 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3437 #endif
3438
3439 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3440 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3441
3442 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3443 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3444 "Process resource limits");
3445
3446 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3447 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3448 "Process ps_strings location");
3449
3450 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3451 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3452
3453 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3454 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3455 "Process binary osreldate");
3456
3457 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3458 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3459 "Process signal trampoline location");
3460
3461 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3462 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3463 "Thread sigfastblock address");
3464
3465 static SYSCTL_NODE(_kern_proc, KERN_PROC_VM_LAYOUT, vm_layout, CTLFLAG_RD |
3466 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_vm_layout,
3467 "Process virtual address space layout info");
3468
3469 static struct sx stop_all_proc_blocker;
3470 SX_SYSINIT(stop_all_proc_blocker, &stop_all_proc_blocker, "sapblk");
3471
3472 bool
stop_all_proc_block(void)3473 stop_all_proc_block(void)
3474 {
3475 return (sx_xlock_sig(&stop_all_proc_blocker) == 0);
3476 }
3477
3478 void
stop_all_proc_unblock(void)3479 stop_all_proc_unblock(void)
3480 {
3481 sx_xunlock(&stop_all_proc_blocker);
3482 }
3483
3484 int allproc_gen;
3485
3486 /*
3487 * stop_all_proc() purpose is to stop all process which have usermode,
3488 * except current process for obvious reasons. This makes it somewhat
3489 * unreliable when invoked from multithreaded process. The service
3490 * must not be user-callable anyway.
3491 */
3492 void
stop_all_proc(void)3493 stop_all_proc(void)
3494 {
3495 struct proc *cp, *p;
3496 int r, gen;
3497 bool restart, seen_stopped, seen_exiting, stopped_some;
3498
3499 if (!stop_all_proc_block())
3500 return;
3501
3502 cp = curproc;
3503 allproc_loop:
3504 sx_xlock(&allproc_lock);
3505 gen = allproc_gen;
3506 seen_exiting = seen_stopped = stopped_some = restart = false;
3507 LIST_REMOVE(cp, p_list);
3508 LIST_INSERT_HEAD(&allproc, cp, p_list);
3509 for (;;) {
3510 p = LIST_NEXT(cp, p_list);
3511 if (p == NULL)
3512 break;
3513 LIST_REMOVE(cp, p_list);
3514 LIST_INSERT_AFTER(p, cp, p_list);
3515 PROC_LOCK(p);
3516 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP |
3517 P_STOPPED_SIG)) != 0) {
3518 PROC_UNLOCK(p);
3519 continue;
3520 }
3521 if ((p->p_flag2 & P2_WEXIT) != 0) {
3522 seen_exiting = true;
3523 PROC_UNLOCK(p);
3524 continue;
3525 }
3526 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3527 /*
3528 * Stopped processes are tolerated when there
3529 * are no other processes which might continue
3530 * them. P_STOPPED_SINGLE but not
3531 * P_TOTAL_STOP process still has at least one
3532 * thread running.
3533 */
3534 seen_stopped = true;
3535 PROC_UNLOCK(p);
3536 continue;
3537 }
3538 if ((p->p_flag & P_TRACED) != 0) {
3539 /*
3540 * thread_single() below cannot stop traced p,
3541 * so skip it. OTOH, we cannot require
3542 * restart because debugger might be either
3543 * already stopped or traced as well.
3544 */
3545 PROC_UNLOCK(p);
3546 continue;
3547 }
3548 sx_xunlock(&allproc_lock);
3549 _PHOLD(p);
3550 r = thread_single(p, SINGLE_ALLPROC);
3551 if (r != 0)
3552 restart = true;
3553 else
3554 stopped_some = true;
3555 _PRELE(p);
3556 PROC_UNLOCK(p);
3557 sx_xlock(&allproc_lock);
3558 }
3559 /* Catch forked children we did not see in iteration. */
3560 if (gen != allproc_gen)
3561 restart = true;
3562 sx_xunlock(&allproc_lock);
3563 if (restart || stopped_some || seen_exiting || seen_stopped) {
3564 kern_yield(PRI_USER);
3565 goto allproc_loop;
3566 }
3567 }
3568
3569 void
resume_all_proc(void)3570 resume_all_proc(void)
3571 {
3572 struct proc *cp, *p;
3573
3574 cp = curproc;
3575 sx_xlock(&allproc_lock);
3576 again:
3577 LIST_REMOVE(cp, p_list);
3578 LIST_INSERT_HEAD(&allproc, cp, p_list);
3579 for (;;) {
3580 p = LIST_NEXT(cp, p_list);
3581 if (p == NULL)
3582 break;
3583 LIST_REMOVE(cp, p_list);
3584 LIST_INSERT_AFTER(p, cp, p_list);
3585 PROC_LOCK(p);
3586 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3587 sx_xunlock(&allproc_lock);
3588 _PHOLD(p);
3589 thread_single_end(p, SINGLE_ALLPROC);
3590 _PRELE(p);
3591 PROC_UNLOCK(p);
3592 sx_xlock(&allproc_lock);
3593 } else {
3594 PROC_UNLOCK(p);
3595 }
3596 }
3597 /* Did the loop above missed any stopped process ? */
3598 FOREACH_PROC_IN_SYSTEM(p) {
3599 /* No need for proc lock. */
3600 if ((p->p_flag & P_TOTAL_STOP) != 0)
3601 goto again;
3602 }
3603 sx_xunlock(&allproc_lock);
3604
3605 stop_all_proc_unblock();
3606 }
3607
3608 /* #define TOTAL_STOP_DEBUG 1 */
3609 #ifdef TOTAL_STOP_DEBUG
3610 volatile static int ap_resume;
3611 #include <sys/mount.h>
3612
3613 static int
sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)3614 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3615 {
3616 int error, val;
3617
3618 val = 0;
3619 ap_resume = 0;
3620 error = sysctl_handle_int(oidp, &val, 0, req);
3621 if (error != 0 || req->newptr == NULL)
3622 return (error);
3623 if (val != 0) {
3624 stop_all_proc();
3625 syncer_suspend();
3626 while (ap_resume == 0)
3627 ;
3628 syncer_resume();
3629 resume_all_proc();
3630 }
3631 return (0);
3632 }
3633
3634 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3635 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3636 sysctl_debug_stop_all_proc, "I",
3637 "");
3638 #endif
3639