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