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