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

Cache object: acf239933f8b1daeb8a2faad38b3b606


[ 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.