The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_proc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6a1cf7f519b11feb3bce49c09e3eeda6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.