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/8.1/sys/kern/kern_proc.c 207917 2010-05-11 13:33:37Z kib $");
   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/kernel.h>
   45 #include <sys/limits.h>
   46 #include <sys/lock.h>
   47 #include <sys/malloc.h>
   48 #include <sys/mount.h>
   49 #include <sys/mutex.h>
   50 #include <sys/proc.h>
   51 #include <sys/refcount.h>
   52 #include <sys/sbuf.h>
   53 #include <sys/sysent.h>
   54 #include <sys/sched.h>
   55 #include <sys/smp.h>
   56 #include <sys/stack.h>
   57 #include <sys/sysctl.h>
   58 #include <sys/filedesc.h>
   59 #include <sys/tty.h>
   60 #include <sys/signalvar.h>
   61 #include <sys/sdt.h>
   62 #include <sys/sx.h>
   63 #include <sys/user.h>
   64 #include <sys/jail.h>
   65 #include <sys/vnode.h>
   66 #include <sys/eventhandler.h>
   67 #ifdef KTRACE
   68 #include <sys/uio.h>
   69 #include <sys/ktrace.h>
   70 #endif
   71 
   72 #ifdef DDB
   73 #include <ddb/ddb.h>
   74 #endif
   75 
   76 #include <vm/vm.h>
   77 #include <vm/vm_extern.h>
   78 #include <vm/pmap.h>
   79 #include <vm/vm_map.h>
   80 #include <vm/vm_object.h>
   81 #include <vm/uma.h>
   82 
   83 #ifdef COMPAT_FREEBSD32
   84 #include <compat/freebsd32/freebsd32.h>
   85 #include <compat/freebsd32/freebsd32_util.h>
   86 #endif
   87 
   88 SDT_PROVIDER_DEFINE(proc);
   89 SDT_PROBE_DEFINE(proc, kernel, ctor, entry);
   90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
   91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
   92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
   93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
   94 SDT_PROBE_DEFINE(proc, kernel, ctor, return);
   95 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
   96 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
   97 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
   98 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
   99 SDT_PROBE_DEFINE(proc, kernel, dtor, entry);
  100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
  101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
  102 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
  103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
  104 SDT_PROBE_DEFINE(proc, kernel, dtor, return);
  105 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
  106 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
  107 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
  108 SDT_PROBE_DEFINE(proc, kernel, init, entry);
  109 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
  110 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
  111 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
  112 SDT_PROBE_DEFINE(proc, kernel, init, return);
  113 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
  114 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
  115 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
  116 
  117 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
  118 MALLOC_DEFINE(M_SESSION, "session", "session header");
  119 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
  120 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
  121 
  122 static void doenterpgrp(struct proc *, struct pgrp *);
  123 static void orphanpg(struct pgrp *pg);
  124 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
  125 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
  126 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
  127     int preferthread);
  128 static void pgadjustjobc(struct pgrp *pgrp, int entering);
  129 static void pgdelete(struct pgrp *);
  130 static int proc_ctor(void *mem, int size, void *arg, int flags);
  131 static void proc_dtor(void *mem, int size, void *arg);
  132 static int proc_init(void *mem, int size, int flags);
  133 static void proc_fini(void *mem, int size);
  134 static void pargs_free(struct pargs *pa);
  135 
  136 /*
  137  * Other process lists
  138  */
  139 struct pidhashhead *pidhashtbl;
  140 u_long pidhash;
  141 struct pgrphashhead *pgrphashtbl;
  142 u_long pgrphash;
  143 struct proclist allproc;
  144 struct proclist zombproc;
  145 struct sx allproc_lock;
  146 struct sx proctree_lock;
  147 struct mtx ppeers_lock;
  148 uma_zone_t proc_zone;
  149 
  150 int kstack_pages = KSTACK_PAGES;
  151 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
  152 
  153 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
  154 #ifdef COMPAT_FREEBSD32
  155 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
  156 #endif
  157 
  158 /*
  159  * Initialize global process hashing structures.
  160  */
  161 void
  162 procinit()
  163 {
  164 
  165         sx_init(&allproc_lock, "allproc");
  166         sx_init(&proctree_lock, "proctree");
  167         mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
  168         LIST_INIT(&allproc);
  169         LIST_INIT(&zombproc);
  170         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
  171         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
  172         proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
  173             proc_ctor, proc_dtor, proc_init, proc_fini,
  174             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  175         uihashinit();
  176 }
  177 
  178 /*
  179  * Prepare a proc for use.
  180  */
  181 static int
  182 proc_ctor(void *mem, int size, void *arg, int flags)
  183 {
  184         struct proc *p;
  185 
  186         p = (struct proc *)mem;
  187         SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
  188         EVENTHANDLER_INVOKE(process_ctor, p);
  189         SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
  190         return (0);
  191 }
  192 
  193 /*
  194  * Reclaim a proc after use.
  195  */
  196 static void
  197 proc_dtor(void *mem, int size, void *arg)
  198 {
  199         struct proc *p;
  200         struct thread *td;
  201 
  202         /* INVARIANTS checks go here */
  203         p = (struct proc *)mem;
  204         td = FIRST_THREAD_IN_PROC(p);
  205         SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
  206         if (td != NULL) {
  207 #ifdef INVARIANTS
  208                 KASSERT((p->p_numthreads == 1),
  209                     ("bad number of threads in exiting process"));
  210                 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
  211 #endif
  212                 /* Free all OSD associated to this thread. */
  213                 osd_thread_exit(td);
  214         }
  215         EVENTHANDLER_INVOKE(process_dtor, p);
  216         if (p->p_ksi != NULL)
  217                 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
  218         SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
  219 }
  220 
  221 /*
  222  * Initialize type-stable parts of a proc (when newly created).
  223  */
  224 static int
  225 proc_init(void *mem, int size, int flags)
  226 {
  227         struct proc *p;
  228 
  229         p = (struct proc *)mem;
  230         SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
  231         p->p_sched = (struct p_sched *)&p[1];
  232         bzero(&p->p_mtx, sizeof(struct mtx));
  233         mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
  234         mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
  235         cv_init(&p->p_pwait, "ppwait");
  236         TAILQ_INIT(&p->p_threads);           /* all threads in proc */
  237         EVENTHANDLER_INVOKE(process_init, p);
  238         p->p_stats = pstats_alloc();
  239         SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
  240         return (0);
  241 }
  242 
  243 /*
  244  * UMA should ensure that this function is never called.
  245  * Freeing a proc structure would violate type stability.
  246  */
  247 static void
  248 proc_fini(void *mem, int size)
  249 {
  250 #ifdef notnow
  251         struct proc *p;
  252 
  253         p = (struct proc *)mem;
  254         EVENTHANDLER_INVOKE(process_fini, p);
  255         pstats_free(p->p_stats);
  256         thread_free(FIRST_THREAD_IN_PROC(p));
  257         mtx_destroy(&p->p_mtx);
  258         if (p->p_ksi != NULL)
  259                 ksiginfo_free(p->p_ksi);
  260 #else
  261         panic("proc reclaimed");
  262 #endif
  263 }
  264 
  265 /*
  266  * Is p an inferior of the current process?
  267  */
  268 int
  269 inferior(p)
  270         register struct proc *p;
  271 {
  272 
  273         sx_assert(&proctree_lock, SX_LOCKED);
  274         for (; p != curproc; p = p->p_pptr)
  275                 if (p->p_pid == 0)
  276                         return (0);
  277         return (1);
  278 }
  279 
  280 /*
  281  * Locate a process by number; return only "live" processes -- i.e., neither
  282  * zombies nor newly born but incompletely initialized processes.  By not
  283  * returning processes in the PRS_NEW state, we allow callers to avoid
  284  * testing for that condition to avoid dereferencing p_ucred, et al.
  285  */
  286 struct proc *
  287 pfind(pid)
  288         register pid_t pid;
  289 {
  290         register struct proc *p;
  291 
  292         sx_slock(&allproc_lock);
  293         LIST_FOREACH(p, PIDHASH(pid), p_hash)
  294                 if (p->p_pid == pid) {
  295                         if (p->p_state == PRS_NEW) {
  296                                 p = NULL;
  297                                 break;
  298                         }
  299                         PROC_LOCK(p);
  300                         break;
  301                 }
  302         sx_sunlock(&allproc_lock);
  303         return (p);
  304 }
  305 
  306 /*
  307  * Locate a process group by number.
  308  * The caller must hold proctree_lock.
  309  */
  310 struct pgrp *
  311 pgfind(pgid)
  312         register pid_t pgid;
  313 {
  314         register struct pgrp *pgrp;
  315 
  316         sx_assert(&proctree_lock, SX_LOCKED);
  317 
  318         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
  319                 if (pgrp->pg_id == pgid) {
  320                         PGRP_LOCK(pgrp);
  321                         return (pgrp);
  322                 }
  323         }
  324         return (NULL);
  325 }
  326 
  327 /*
  328  * Create a new process group.
  329  * pgid must be equal to the pid of p.
  330  * Begin a new session if required.
  331  */
  332 int
  333 enterpgrp(p, pgid, pgrp, sess)
  334         register struct proc *p;
  335         pid_t pgid;
  336         struct pgrp *pgrp;
  337         struct session *sess;
  338 {
  339         struct pgrp *pgrp2;
  340 
  341         sx_assert(&proctree_lock, SX_XLOCKED);
  342 
  343         KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
  344         KASSERT(p->p_pid == pgid,
  345             ("enterpgrp: new pgrp and pid != pgid"));
  346 
  347         pgrp2 = pgfind(pgid);
  348 
  349         KASSERT(pgrp2 == NULL,
  350             ("enterpgrp: pgrp with pgid exists"));
  351         KASSERT(!SESS_LEADER(p),
  352             ("enterpgrp: session leader attempted setpgrp"));
  353 
  354         mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
  355 
  356         if (sess != NULL) {
  357                 /*
  358                  * new session
  359                  */
  360                 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
  361                 PROC_LOCK(p);
  362                 p->p_flag &= ~P_CONTROLT;
  363                 PROC_UNLOCK(p);
  364                 PGRP_LOCK(pgrp);
  365                 sess->s_leader = p;
  366                 sess->s_sid = p->p_pid;
  367                 refcount_init(&sess->s_count, 1);
  368                 sess->s_ttyvp = NULL;
  369                 sess->s_ttyp = NULL;
  370                 bcopy(p->p_session->s_login, sess->s_login,
  371                             sizeof(sess->s_login));
  372                 pgrp->pg_session = sess;
  373                 KASSERT(p == curproc,
  374                     ("enterpgrp: mksession and p != curproc"));
  375         } else {
  376                 pgrp->pg_session = p->p_session;
  377                 sess_hold(pgrp->pg_session);
  378                 PGRP_LOCK(pgrp);
  379         }
  380         pgrp->pg_id = pgid;
  381         LIST_INIT(&pgrp->pg_members);
  382 
  383         /*
  384          * As we have an exclusive lock of proctree_lock,
  385          * this should not deadlock.
  386          */
  387         LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
  388         pgrp->pg_jobc = 0;
  389         SLIST_INIT(&pgrp->pg_sigiolst);
  390         PGRP_UNLOCK(pgrp);
  391 
  392         doenterpgrp(p, pgrp);
  393 
  394         return (0);
  395 }
  396 
  397 /*
  398  * Move p to an existing process group
  399  */
  400 int
  401 enterthispgrp(p, pgrp)
  402         register struct proc *p;
  403         struct pgrp *pgrp;
  404 {
  405 
  406         sx_assert(&proctree_lock, SX_XLOCKED);
  407         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  408         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  409         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
  410         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
  411         KASSERT(pgrp->pg_session == p->p_session,
  412                 ("%s: pgrp's session %p, p->p_session %p.\n",
  413                 __func__,
  414                 pgrp->pg_session,
  415                 p->p_session));
  416         KASSERT(pgrp != p->p_pgrp,
  417                 ("%s: p belongs to pgrp.", __func__));
  418 
  419         doenterpgrp(p, pgrp);
  420 
  421         return (0);
  422 }
  423 
  424 /*
  425  * Move p to a process group
  426  */
  427 static void
  428 doenterpgrp(p, pgrp)
  429         struct proc *p;
  430         struct pgrp *pgrp;
  431 {
  432         struct pgrp *savepgrp;
  433 
  434         sx_assert(&proctree_lock, SX_XLOCKED);
  435         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  436         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  437         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
  438         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
  439 
  440         savepgrp = p->p_pgrp;
  441 
  442         /*
  443          * Adjust eligibility of affected pgrps to participate in job control.
  444          * Increment eligibility counts before decrementing, otherwise we
  445          * could reach 0 spuriously during the first call.
  446          */
  447         fixjobc(p, pgrp, 1);
  448         fixjobc(p, p->p_pgrp, 0);
  449 
  450         PGRP_LOCK(pgrp);
  451         PGRP_LOCK(savepgrp);
  452         PROC_LOCK(p);
  453         LIST_REMOVE(p, p_pglist);
  454         p->p_pgrp = pgrp;
  455         PROC_UNLOCK(p);
  456         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
  457         PGRP_UNLOCK(savepgrp);
  458         PGRP_UNLOCK(pgrp);
  459         if (LIST_EMPTY(&savepgrp->pg_members))
  460                 pgdelete(savepgrp);
  461 }
  462 
  463 /*
  464  * remove process from process group
  465  */
  466 int
  467 leavepgrp(p)
  468         register struct proc *p;
  469 {
  470         struct pgrp *savepgrp;
  471 
  472         sx_assert(&proctree_lock, SX_XLOCKED);
  473         savepgrp = p->p_pgrp;
  474         PGRP_LOCK(savepgrp);
  475         PROC_LOCK(p);
  476         LIST_REMOVE(p, p_pglist);
  477         p->p_pgrp = NULL;
  478         PROC_UNLOCK(p);
  479         PGRP_UNLOCK(savepgrp);
  480         if (LIST_EMPTY(&savepgrp->pg_members))
  481                 pgdelete(savepgrp);
  482         return (0);
  483 }
  484 
  485 /*
  486  * delete a process group
  487  */
  488 static void
  489 pgdelete(pgrp)
  490         register struct pgrp *pgrp;
  491 {
  492         struct session *savesess;
  493         struct tty *tp;
  494 
  495         sx_assert(&proctree_lock, SX_XLOCKED);
  496         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  497         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  498 
  499         /*
  500          * Reset any sigio structures pointing to us as a result of
  501          * F_SETOWN with our pgid.
  502          */
  503         funsetownlst(&pgrp->pg_sigiolst);
  504 
  505         PGRP_LOCK(pgrp);
  506         tp = pgrp->pg_session->s_ttyp;
  507         LIST_REMOVE(pgrp, pg_hash);
  508         savesess = pgrp->pg_session;
  509         PGRP_UNLOCK(pgrp);
  510 
  511         /* Remove the reference to the pgrp before deallocating it. */
  512         if (tp != NULL) {
  513                 tty_lock(tp);
  514                 tty_rel_pgrp(tp, pgrp);
  515         }
  516 
  517         mtx_destroy(&pgrp->pg_mtx);
  518         free(pgrp, M_PGRP);
  519         sess_release(savesess);
  520 }
  521 
  522 static void
  523 pgadjustjobc(pgrp, entering)
  524         struct pgrp *pgrp;
  525         int entering;
  526 {
  527 
  528         PGRP_LOCK(pgrp);
  529         if (entering)
  530                 pgrp->pg_jobc++;
  531         else {
  532                 --pgrp->pg_jobc;
  533                 if (pgrp->pg_jobc == 0)
  534                         orphanpg(pgrp);
  535         }
  536         PGRP_UNLOCK(pgrp);
  537 }
  538 
  539 /*
  540  * Adjust pgrp jobc counters when specified process changes process group.
  541  * We count the number of processes in each process group that "qualify"
  542  * the group for terminal job control (those with a parent in a different
  543  * process group of the same session).  If that count reaches zero, the
  544  * process group becomes orphaned.  Check both the specified process'
  545  * process group and that of its children.
  546  * entering == 0 => p is leaving specified group.
  547  * entering == 1 => p is entering specified group.
  548  */
  549 void
  550 fixjobc(p, pgrp, entering)
  551         register struct proc *p;
  552         register struct pgrp *pgrp;
  553         int entering;
  554 {
  555         register struct pgrp *hispgrp;
  556         register struct session *mysession;
  557 
  558         sx_assert(&proctree_lock, SX_LOCKED);
  559         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  560         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  561         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  562 
  563         /*
  564          * Check p's parent to see whether p qualifies its own process
  565          * group; if so, adjust count for p's process group.
  566          */
  567         mysession = pgrp->pg_session;
  568         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
  569             hispgrp->pg_session == mysession)
  570                 pgadjustjobc(pgrp, entering);
  571 
  572         /*
  573          * Check this process' children to see whether they qualify
  574          * their process groups; if so, adjust counts for children's
  575          * process groups.
  576          */
  577         LIST_FOREACH(p, &p->p_children, p_sibling) {
  578                 hispgrp = p->p_pgrp;
  579                 if (hispgrp == pgrp ||
  580                     hispgrp->pg_session != mysession)
  581                         continue;
  582                 PROC_LOCK(p);
  583                 if (p->p_state == PRS_ZOMBIE) {
  584                         PROC_UNLOCK(p);
  585                         continue;
  586                 }
  587                 PROC_UNLOCK(p);
  588                 pgadjustjobc(hispgrp, entering);
  589         }
  590 }
  591 
  592 /*
  593  * A process group has become orphaned;
  594  * if there are any stopped processes in the group,
  595  * hang-up all process in that group.
  596  */
  597 static void
  598 orphanpg(pg)
  599         struct pgrp *pg;
  600 {
  601         register struct proc *p;
  602 
  603         PGRP_LOCK_ASSERT(pg, MA_OWNED);
  604 
  605         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  606                 PROC_LOCK(p);
  607                 if (P_SHOULDSTOP(p)) {
  608                         PROC_UNLOCK(p);
  609                         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  610                                 PROC_LOCK(p);
  611                                 psignal(p, SIGHUP);
  612                                 psignal(p, SIGCONT);
  613                                 PROC_UNLOCK(p);
  614                         }
  615                         return;
  616                 }
  617                 PROC_UNLOCK(p);
  618         }
  619 }
  620 
  621 void
  622 sess_hold(struct session *s)
  623 {
  624 
  625         refcount_acquire(&s->s_count);
  626 }
  627 
  628 void
  629 sess_release(struct session *s)
  630 {
  631 
  632         if (refcount_release(&s->s_count)) {
  633                 if (s->s_ttyp != NULL) {
  634                         tty_lock(s->s_ttyp);
  635                         tty_rel_sess(s->s_ttyp, s);
  636                 }
  637                 mtx_destroy(&s->s_mtx);
  638                 free(s, M_SESSION);
  639         }
  640 }
  641 
  642 #include "opt_ddb.h"
  643 #ifdef DDB
  644 #include <ddb/ddb.h>
  645 
  646 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
  647 {
  648         register struct pgrp *pgrp;
  649         register struct proc *p;
  650         register int i;
  651 
  652         for (i = 0; i <= pgrphash; i++) {
  653                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
  654                         printf("\tindx %d\n", i);
  655                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
  656                                 printf(
  657                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
  658                                     (void *)pgrp, (long)pgrp->pg_id,
  659                                     (void *)pgrp->pg_session,
  660                                     pgrp->pg_session->s_count,
  661                                     (void *)LIST_FIRST(&pgrp->pg_members));
  662                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
  663                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
  664                                             (long)p->p_pid, (void *)p,
  665                                             (void *)p->p_pgrp);
  666                                 }
  667                         }
  668                 }
  669         }
  670 }
  671 #endif /* DDB */
  672 
  673 /*
  674  * Calculate the kinfo_proc members which contain process-wide
  675  * informations.
  676  * Must be called with the target process locked.
  677  */
  678 static void
  679 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
  680 {
  681         struct thread *td;
  682 
  683         PROC_LOCK_ASSERT(p, MA_OWNED);
  684 
  685         kp->ki_estcpu = 0;
  686         kp->ki_pctcpu = 0;
  687         FOREACH_THREAD_IN_PROC(p, td) {
  688                 thread_lock(td);
  689                 kp->ki_pctcpu += sched_pctcpu(td);
  690                 kp->ki_estcpu += td->td_estcpu;
  691                 thread_unlock(td);
  692         }
  693 }
  694 
  695 /*
  696  * Clear kinfo_proc and fill in any information that is common
  697  * to all threads in the process.
  698  * Must be called with the target process locked.
  699  */
  700 static void
  701 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
  702 {
  703         struct thread *td0;
  704         struct tty *tp;
  705         struct session *sp;
  706         struct ucred *cred;
  707         struct sigacts *ps;
  708 
  709         PROC_LOCK_ASSERT(p, MA_OWNED);
  710         bzero(kp, sizeof(*kp));
  711 
  712         kp->ki_structsize = sizeof(*kp);
  713         kp->ki_paddr = p;
  714         kp->ki_addr =/* p->p_addr; */0; /* XXX */
  715         kp->ki_args = p->p_args;
  716         kp->ki_textvp = p->p_textvp;
  717 #ifdef KTRACE
  718         kp->ki_tracep = p->p_tracevp;
  719         mtx_lock(&ktrace_mtx);
  720         kp->ki_traceflag = p->p_traceflag;
  721         mtx_unlock(&ktrace_mtx);
  722 #endif
  723         kp->ki_fd = p->p_fd;
  724         kp->ki_vmspace = p->p_vmspace;
  725         kp->ki_flag = p->p_flag;
  726         cred = p->p_ucred;
  727         if (cred) {
  728                 kp->ki_uid = cred->cr_uid;
  729                 kp->ki_ruid = cred->cr_ruid;
  730                 kp->ki_svuid = cred->cr_svuid;
  731                 kp->ki_cr_flags = cred->cr_flags;
  732                 /* XXX bde doesn't like KI_NGROUPS */
  733                 if (cred->cr_ngroups > KI_NGROUPS) {
  734                         kp->ki_ngroups = KI_NGROUPS;
  735                         kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
  736                 } else
  737                         kp->ki_ngroups = cred->cr_ngroups;
  738                 bcopy(cred->cr_groups, kp->ki_groups,
  739                     kp->ki_ngroups * sizeof(gid_t));
  740                 kp->ki_rgid = cred->cr_rgid;
  741                 kp->ki_svgid = cred->cr_svgid;
  742                 /* If jailed(cred), emulate the old P_JAILED flag. */
  743                 if (jailed(cred)) {
  744                         kp->ki_flag |= P_JAILED;
  745                         /* If inside the jail, use 0 as a jail ID. */
  746                         if (cred->cr_prison != curthread->td_ucred->cr_prison)
  747                                 kp->ki_jid = cred->cr_prison->pr_id;
  748                 }
  749         }
  750         ps = p->p_sigacts;
  751         if (ps) {
  752                 mtx_lock(&ps->ps_mtx);
  753                 kp->ki_sigignore = ps->ps_sigignore;
  754                 kp->ki_sigcatch = ps->ps_sigcatch;
  755                 mtx_unlock(&ps->ps_mtx);
  756         }
  757         PROC_SLOCK(p);
  758         if (p->p_state != PRS_NEW &&
  759             p->p_state != PRS_ZOMBIE &&
  760             p->p_vmspace != NULL) {
  761                 struct vmspace *vm = p->p_vmspace;
  762 
  763                 kp->ki_size = vm->vm_map.size;
  764                 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
  765                 FOREACH_THREAD_IN_PROC(p, td0) {
  766                         if (!TD_IS_SWAPPED(td0))
  767                                 kp->ki_rssize += td0->td_kstack_pages;
  768                 }
  769                 kp->ki_swrss = vm->vm_swrss;
  770                 kp->ki_tsize = vm->vm_tsize;
  771                 kp->ki_dsize = vm->vm_dsize;
  772                 kp->ki_ssize = vm->vm_ssize;
  773         } else if (p->p_state == PRS_ZOMBIE)
  774                 kp->ki_stat = SZOMB;
  775         if (kp->ki_flag & P_INMEM)
  776                 kp->ki_sflag = PS_INMEM;
  777         else
  778                 kp->ki_sflag = 0;
  779         /* Calculate legacy swtime as seconds since 'swtick'. */
  780         kp->ki_swtime = (ticks - p->p_swtick) / hz;
  781         kp->ki_pid = p->p_pid;
  782         kp->ki_nice = p->p_nice;
  783         rufetch(p, &kp->ki_rusage);
  784         kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
  785         PROC_SUNLOCK(p);
  786         if ((p->p_flag & P_INMEM) && p->p_stats != NULL) {
  787                 kp->ki_start = p->p_stats->p_start;
  788                 timevaladd(&kp->ki_start, &boottime);
  789                 PROC_SLOCK(p);
  790                 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
  791                 PROC_SUNLOCK(p);
  792                 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
  793 
  794                 /* Some callers want child-times in a single value */
  795                 kp->ki_childtime = kp->ki_childstime;
  796                 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
  797         }
  798         tp = NULL;
  799         if (p->p_pgrp) {
  800                 kp->ki_pgid = p->p_pgrp->pg_id;
  801                 kp->ki_jobc = p->p_pgrp->pg_jobc;
  802                 sp = p->p_pgrp->pg_session;
  803 
  804                 if (sp != NULL) {
  805                         kp->ki_sid = sp->s_sid;
  806                         SESS_LOCK(sp);
  807                         strlcpy(kp->ki_login, sp->s_login,
  808                             sizeof(kp->ki_login));
  809                         if (sp->s_ttyvp)
  810                                 kp->ki_kiflag |= KI_CTTY;
  811                         if (SESS_LEADER(p))
  812                                 kp->ki_kiflag |= KI_SLEADER;
  813                         /* XXX proctree_lock */
  814                         tp = sp->s_ttyp;
  815                         SESS_UNLOCK(sp);
  816                 }
  817         }
  818         if ((p->p_flag & P_CONTROLT) && tp != NULL) {
  819                 kp->ki_tdev = tty_udev(tp);
  820                 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
  821                 if (tp->t_session)
  822                         kp->ki_tsid = tp->t_session->s_sid;
  823         } else
  824                 kp->ki_tdev = NODEV;
  825         if (p->p_comm[0] != '\0')
  826                 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
  827         if (p->p_sysent && p->p_sysent->sv_name != NULL &&
  828             p->p_sysent->sv_name[0] != '\0')
  829                 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
  830         kp->ki_siglist = p->p_siglist;
  831         kp->ki_xstat = p->p_xstat;
  832         kp->ki_acflag = p->p_acflag;
  833         kp->ki_lock = p->p_lock;
  834         if (p->p_pptr)
  835                 kp->ki_ppid = p->p_pptr->p_pid;
  836 }
  837 
  838 /*
  839  * Fill in information that is thread specific.  Must be called with
  840  * target process locked.  If 'preferthread' is set, overwrite certain
  841  * process-related fields that are maintained for both threads and
  842  * processes.
  843  */
  844 static void
  845 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
  846 {
  847         struct proc *p;
  848 
  849         p = td->td_proc;
  850         PROC_LOCK_ASSERT(p, MA_OWNED);
  851 
  852         thread_lock(td);
  853         if (td->td_wmesg != NULL)
  854                 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
  855         else
  856                 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
  857         strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
  858         if (TD_ON_LOCK(td)) {
  859                 kp->ki_kiflag |= KI_LOCKBLOCK;
  860                 strlcpy(kp->ki_lockname, td->td_lockname,
  861                     sizeof(kp->ki_lockname));
  862         } else {
  863                 kp->ki_kiflag &= ~KI_LOCKBLOCK;
  864                 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
  865         }
  866 
  867         if (p->p_state == PRS_NORMAL) { /* approximate. */
  868                 if (TD_ON_RUNQ(td) ||
  869                     TD_CAN_RUN(td) ||
  870                     TD_IS_RUNNING(td)) {
  871                         kp->ki_stat = SRUN;
  872                 } else if (P_SHOULDSTOP(p)) {
  873                         kp->ki_stat = SSTOP;
  874                 } else if (TD_IS_SLEEPING(td)) {
  875                         kp->ki_stat = SSLEEP;
  876                 } else if (TD_ON_LOCK(td)) {
  877                         kp->ki_stat = SLOCK;
  878                 } else {
  879                         kp->ki_stat = SWAIT;
  880                 }
  881         } else if (p->p_state == PRS_ZOMBIE) {
  882                 kp->ki_stat = SZOMB;
  883         } else {
  884                 kp->ki_stat = SIDL;
  885         }
  886 
  887         /* Things in the thread */
  888         kp->ki_wchan = td->td_wchan;
  889         kp->ki_pri.pri_level = td->td_priority;
  890         kp->ki_pri.pri_native = td->td_base_pri;
  891         kp->ki_lastcpu = td->td_lastcpu;
  892         kp->ki_oncpu = td->td_oncpu;
  893         kp->ki_tdflags = td->td_flags;
  894         kp->ki_tid = td->td_tid;
  895         kp->ki_numthreads = p->p_numthreads;
  896         kp->ki_pcb = td->td_pcb;
  897         kp->ki_kstack = (void *)td->td_kstack;
  898         kp->ki_slptime = (ticks - td->td_slptick) / hz;
  899         kp->ki_pri.pri_class = td->td_pri_class;
  900         kp->ki_pri.pri_user = td->td_user_pri;
  901 
  902         if (preferthread) {
  903                 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
  904                 kp->ki_pctcpu = sched_pctcpu(td);
  905                 kp->ki_estcpu = td->td_estcpu;
  906         }
  907 
  908         /* We can't get this anymore but ps etc never used it anyway. */
  909         kp->ki_rqindex = 0;
  910 
  911         if (preferthread)
  912                 kp->ki_siglist = td->td_siglist;
  913         kp->ki_sigmask = td->td_sigmask;
  914         thread_unlock(td);
  915 }
  916 
  917 /*
  918  * Fill in a kinfo_proc structure for the specified process.
  919  * Must be called with the target process locked.
  920  */
  921 void
  922 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
  923 {
  924 
  925         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
  926 
  927         fill_kinfo_proc_only(p, kp);
  928         fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
  929         fill_kinfo_aggregate(p, kp);
  930 }
  931 
  932 struct pstats *
  933 pstats_alloc(void)
  934 {
  935 
  936         return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
  937 }
  938 
  939 /*
  940  * Copy parts of p_stats; zero the rest of p_stats (statistics).
  941  */
  942 void
  943 pstats_fork(struct pstats *src, struct pstats *dst)
  944 {
  945 
  946         bzero(&dst->pstat_startzero,
  947             __rangeof(struct pstats, pstat_startzero, pstat_endzero));
  948         bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
  949             __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
  950 }
  951 
  952 void
  953 pstats_free(struct pstats *ps)
  954 {
  955 
  956         free(ps, M_SUBPROC);
  957 }
  958 
  959 /*
  960  * Locate a zombie process by number
  961  */
  962 struct proc *
  963 zpfind(pid_t pid)
  964 {
  965         struct proc *p;
  966 
  967         sx_slock(&allproc_lock);
  968         LIST_FOREACH(p, &zombproc, p_list)
  969                 if (p->p_pid == pid) {
  970                         PROC_LOCK(p);
  971                         break;
  972                 }
  973         sx_sunlock(&allproc_lock);
  974         return (p);
  975 }
  976 
  977 #define KERN_PROC_ZOMBMASK      0x3
  978 #define KERN_PROC_NOTHREADS     0x4
  979 
  980 #ifdef COMPAT_FREEBSD32
  981 
  982 /*
  983  * This function is typically used to copy out the kernel address, so
  984  * it can be replaced by assignment of zero.
  985  */
  986 static inline uint32_t
  987 ptr32_trim(void *ptr)
  988 {
  989         uintptr_t uptr;
  990 
  991         uptr = (uintptr_t)ptr;
  992         return ((uptr > UINT_MAX) ? 0 : uptr);
  993 }
  994 
  995 #define PTRTRIM_CP(src,dst,fld) \
  996         do { (dst).fld = ptr32_trim((src).fld); } while (0)
  997 
  998 static void
  999 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
 1000 {
 1001         int i;
 1002 
 1003         bzero(ki32, sizeof(struct kinfo_proc32));
 1004         ki32->ki_structsize = sizeof(struct kinfo_proc32);
 1005         CP(*ki, *ki32, ki_layout);
 1006         PTRTRIM_CP(*ki, *ki32, ki_args);
 1007         PTRTRIM_CP(*ki, *ki32, ki_paddr);
 1008         PTRTRIM_CP(*ki, *ki32, ki_addr);
 1009         PTRTRIM_CP(*ki, *ki32, ki_tracep);
 1010         PTRTRIM_CP(*ki, *ki32, ki_textvp);
 1011         PTRTRIM_CP(*ki, *ki32, ki_fd);
 1012         PTRTRIM_CP(*ki, *ki32, ki_vmspace);
 1013         PTRTRIM_CP(*ki, *ki32, ki_wchan);
 1014         CP(*ki, *ki32, ki_pid);
 1015         CP(*ki, *ki32, ki_ppid);
 1016         CP(*ki, *ki32, ki_pgid);
 1017         CP(*ki, *ki32, ki_tpgid);
 1018         CP(*ki, *ki32, ki_sid);
 1019         CP(*ki, *ki32, ki_tsid);
 1020         CP(*ki, *ki32, ki_jobc);
 1021         CP(*ki, *ki32, ki_tdev);
 1022         CP(*ki, *ki32, ki_siglist);
 1023         CP(*ki, *ki32, ki_sigmask);
 1024         CP(*ki, *ki32, ki_sigignore);
 1025         CP(*ki, *ki32, ki_sigcatch);
 1026         CP(*ki, *ki32, ki_uid);
 1027         CP(*ki, *ki32, ki_ruid);
 1028         CP(*ki, *ki32, ki_svuid);
 1029         CP(*ki, *ki32, ki_rgid);
 1030         CP(*ki, *ki32, ki_svgid);
 1031         CP(*ki, *ki32, ki_ngroups);
 1032         for (i = 0; i < KI_NGROUPS; i++)
 1033                 CP(*ki, *ki32, ki_groups[i]);
 1034         CP(*ki, *ki32, ki_size);
 1035         CP(*ki, *ki32, ki_rssize);
 1036         CP(*ki, *ki32, ki_swrss);
 1037         CP(*ki, *ki32, ki_tsize);
 1038         CP(*ki, *ki32, ki_dsize);
 1039         CP(*ki, *ki32, ki_ssize);
 1040         CP(*ki, *ki32, ki_xstat);
 1041         CP(*ki, *ki32, ki_acflag);
 1042         CP(*ki, *ki32, ki_pctcpu);
 1043         CP(*ki, *ki32, ki_estcpu);
 1044         CP(*ki, *ki32, ki_slptime);
 1045         CP(*ki, *ki32, ki_swtime);
 1046         CP(*ki, *ki32, ki_runtime);
 1047         TV_CP(*ki, *ki32, ki_start);
 1048         TV_CP(*ki, *ki32, ki_childtime);
 1049         CP(*ki, *ki32, ki_flag);
 1050         CP(*ki, *ki32, ki_kiflag);
 1051         CP(*ki, *ki32, ki_traceflag);
 1052         CP(*ki, *ki32, ki_stat);
 1053         CP(*ki, *ki32, ki_nice);
 1054         CP(*ki, *ki32, ki_lock);
 1055         CP(*ki, *ki32, ki_rqindex);
 1056         CP(*ki, *ki32, ki_oncpu);
 1057         CP(*ki, *ki32, ki_lastcpu);
 1058         bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1);
 1059         bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
 1060         bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
 1061         bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
 1062         bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
 1063         bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
 1064         CP(*ki, *ki32, ki_cr_flags);
 1065         CP(*ki, *ki32, ki_jid);
 1066         CP(*ki, *ki32, ki_numthreads);
 1067         CP(*ki, *ki32, ki_tid);
 1068         CP(*ki, *ki32, ki_pri);
 1069         freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
 1070         freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
 1071         PTRTRIM_CP(*ki, *ki32, ki_pcb);
 1072         PTRTRIM_CP(*ki, *ki32, ki_kstack);
 1073         PTRTRIM_CP(*ki, *ki32, ki_udata);
 1074         CP(*ki, *ki32, ki_sflag);
 1075         CP(*ki, *ki32, ki_tdflags);
 1076 }
 1077 
 1078 static int
 1079 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
 1080 {
 1081         struct kinfo_proc32 ki32;
 1082         int error;
 1083 
 1084         if (req->flags & SCTL_MASK32) {
 1085                 freebsd32_kinfo_proc_out(ki, &ki32);
 1086                 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
 1087         } else
 1088                 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
 1089         return (error);
 1090 }
 1091 #else
 1092 static int
 1093 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
 1094 {
 1095 
 1096         return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
 1097 }
 1098 #endif
 1099 
 1100 /*
 1101  * Must be called with the process locked and will return with it unlocked.
 1102  */
 1103 static int
 1104 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
 1105 {
 1106         struct thread *td;
 1107         struct kinfo_proc kinfo_proc;
 1108         int error = 0;
 1109         struct proc *np;
 1110         pid_t pid = p->p_pid;
 1111 
 1112         PROC_LOCK_ASSERT(p, MA_OWNED);
 1113         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
 1114 
 1115         fill_kinfo_proc(p, &kinfo_proc);
 1116         if (flags & KERN_PROC_NOTHREADS)
 1117                 error = sysctl_out_proc_copyout(&kinfo_proc, req);
 1118         else {
 1119                 FOREACH_THREAD_IN_PROC(p, td) {
 1120                         fill_kinfo_thread(td, &kinfo_proc, 1);
 1121                         error = sysctl_out_proc_copyout(&kinfo_proc, req);
 1122                         if (error)
 1123                                 break;
 1124                 }
 1125         }
 1126         PROC_UNLOCK(p);
 1127         if (error)
 1128                 return (error);
 1129         if (flags & KERN_PROC_ZOMBMASK)
 1130                 np = zpfind(pid);
 1131         else {
 1132                 if (pid == 0)
 1133                         return (0);
 1134                 np = pfind(pid);
 1135         }
 1136         if (np == NULL)
 1137                 return (ESRCH);
 1138         if (np != p) {
 1139                 PROC_UNLOCK(np);
 1140                 return (ESRCH);
 1141         }
 1142         PROC_UNLOCK(np);
 1143         return (0);
 1144 }
 1145 
 1146 static int
 1147 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
 1148 {
 1149         int *name = (int*) arg1;
 1150         u_int namelen = arg2;
 1151         struct proc *p;
 1152         int flags, doingzomb, oid_number;
 1153         int error = 0;
 1154 
 1155         oid_number = oidp->oid_number;
 1156         if (oid_number != KERN_PROC_ALL &&
 1157             (oid_number & KERN_PROC_INC_THREAD) == 0)
 1158                 flags = KERN_PROC_NOTHREADS;
 1159         else {
 1160                 flags = 0;
 1161                 oid_number &= ~KERN_PROC_INC_THREAD;
 1162         }
 1163         if (oid_number == KERN_PROC_PID) {
 1164                 if (namelen != 1) 
 1165                         return (EINVAL);
 1166                 error = sysctl_wire_old_buffer(req, 0);
 1167                 if (error)
 1168                         return (error);         
 1169                 p = pfind((pid_t)name[0]);
 1170                 if (!p)
 1171                         return (ESRCH);
 1172                 if ((error = p_cansee(curthread, p))) {
 1173                         PROC_UNLOCK(p);
 1174                         return (error);
 1175                 }
 1176                 error = sysctl_out_proc(p, req, flags);
 1177                 return (error);
 1178         }
 1179 
 1180         switch (oid_number) {
 1181         case KERN_PROC_ALL:
 1182                 if (namelen != 0)
 1183                         return (EINVAL);
 1184                 break;
 1185         case KERN_PROC_PROC:
 1186                 if (namelen != 0 && namelen != 1)
 1187                         return (EINVAL);
 1188                 break;
 1189         default:
 1190                 if (namelen != 1)
 1191                         return (EINVAL);
 1192                 break;
 1193         }
 1194         
 1195         if (!req->oldptr) {
 1196                 /* overestimate by 5 procs */
 1197                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
 1198                 if (error)
 1199                         return (error);
 1200         }
 1201         error = sysctl_wire_old_buffer(req, 0);
 1202         if (error != 0)
 1203                 return (error);
 1204         sx_slock(&allproc_lock);
 1205         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
 1206                 if (!doingzomb)
 1207                         p = LIST_FIRST(&allproc);
 1208                 else
 1209                         p = LIST_FIRST(&zombproc);
 1210                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
 1211                         /*
 1212                          * Skip embryonic processes.
 1213                          */
 1214                         PROC_SLOCK(p);
 1215                         if (p->p_state == PRS_NEW) {
 1216                                 PROC_SUNLOCK(p);
 1217                                 continue;
 1218                         }
 1219                         PROC_SUNLOCK(p);
 1220                         PROC_LOCK(p);
 1221                         KASSERT(p->p_ucred != NULL,
 1222                             ("process credential is NULL for non-NEW proc"));
 1223                         /*
 1224                          * Show a user only appropriate processes.
 1225                          */
 1226                         if (p_cansee(curthread, p)) {
 1227                                 PROC_UNLOCK(p);
 1228                                 continue;
 1229                         }
 1230                         /*
 1231                          * TODO - make more efficient (see notes below).
 1232                          * do by session.
 1233                          */
 1234                         switch (oid_number) {
 1235 
 1236                         case KERN_PROC_GID:
 1237                                 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
 1238                                         PROC_UNLOCK(p);
 1239                                         continue;
 1240                                 }
 1241                                 break;
 1242 
 1243                         case KERN_PROC_PGRP:
 1244                                 /* could do this by traversing pgrp */
 1245                                 if (p->p_pgrp == NULL ||
 1246                                     p->p_pgrp->pg_id != (pid_t)name[0]) {
 1247                                         PROC_UNLOCK(p);
 1248                                         continue;
 1249                                 }
 1250                                 break;
 1251 
 1252                         case KERN_PROC_RGID:
 1253                                 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
 1254                                         PROC_UNLOCK(p);
 1255                                         continue;
 1256                                 }
 1257                                 break;
 1258 
 1259                         case KERN_PROC_SESSION:
 1260                                 if (p->p_session == NULL ||
 1261                                     p->p_session->s_sid != (pid_t)name[0]) {
 1262                                         PROC_UNLOCK(p);
 1263                                         continue;
 1264                                 }
 1265                                 break;
 1266 
 1267                         case KERN_PROC_TTY:
 1268                                 if ((p->p_flag & P_CONTROLT) == 0 ||
 1269                                     p->p_session == NULL) {
 1270                                         PROC_UNLOCK(p);
 1271                                         continue;
 1272                                 }
 1273                                 /* XXX proctree_lock */
 1274                                 SESS_LOCK(p->p_session);
 1275                                 if (p->p_session->s_ttyp == NULL ||
 1276                                     tty_udev(p->p_session->s_ttyp) != 
 1277                                     (dev_t)name[0]) {
 1278                                         SESS_UNLOCK(p->p_session);
 1279                                         PROC_UNLOCK(p);
 1280                                         continue;
 1281                                 }
 1282                                 SESS_UNLOCK(p->p_session);
 1283                                 break;
 1284 
 1285                         case KERN_PROC_UID:
 1286                                 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
 1287                                         PROC_UNLOCK(p);
 1288                                         continue;
 1289                                 }
 1290                                 break;
 1291 
 1292                         case KERN_PROC_RUID:
 1293                                 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
 1294                                         PROC_UNLOCK(p);
 1295                                         continue;
 1296                                 }
 1297                                 break;
 1298 
 1299                         case KERN_PROC_PROC:
 1300                                 break;
 1301 
 1302                         default:
 1303                                 break;
 1304 
 1305                         }
 1306 
 1307                         error = sysctl_out_proc(p, req, flags | doingzomb);
 1308                         if (error) {
 1309                                 sx_sunlock(&allproc_lock);
 1310                                 return (error);
 1311                         }
 1312                 }
 1313         }
 1314         sx_sunlock(&allproc_lock);
 1315         return (0);
 1316 }
 1317 
 1318 struct pargs *
 1319 pargs_alloc(int len)
 1320 {
 1321         struct pargs *pa;
 1322 
 1323         pa = malloc(sizeof(struct pargs) + len, M_PARGS,
 1324                 M_WAITOK);
 1325         refcount_init(&pa->ar_ref, 1);
 1326         pa->ar_length = len;
 1327         return (pa);
 1328 }
 1329 
 1330 static void
 1331 pargs_free(struct pargs *pa)
 1332 {
 1333 
 1334         free(pa, M_PARGS);
 1335 }
 1336 
 1337 void
 1338 pargs_hold(struct pargs *pa)
 1339 {
 1340 
 1341         if (pa == NULL)
 1342                 return;
 1343         refcount_acquire(&pa->ar_ref);
 1344 }
 1345 
 1346 void
 1347 pargs_drop(struct pargs *pa)
 1348 {
 1349 
 1350         if (pa == NULL)
 1351                 return;
 1352         if (refcount_release(&pa->ar_ref))
 1353                 pargs_free(pa);
 1354 }
 1355 
 1356 /*
 1357  * This sysctl allows a process to retrieve the argument list or process
 1358  * title for another process without groping around in the address space
 1359  * of the other process.  It also allow a process to set its own "process 
 1360  * title to a string of its own choice.
 1361  */
 1362 static int
 1363 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 1364 {
 1365         int *name = (int*) arg1;
 1366         u_int namelen = arg2;
 1367         struct pargs *newpa, *pa;
 1368         struct proc *p;
 1369         int error = 0;
 1370 
 1371         if (namelen != 1) 
 1372                 return (EINVAL);
 1373 
 1374         p = pfind((pid_t)name[0]);
 1375         if (!p)
 1376                 return (ESRCH);
 1377 
 1378         if ((error = p_cansee(curthread, p)) != 0) {
 1379                 PROC_UNLOCK(p);
 1380                 return (error);
 1381         }
 1382 
 1383         if (req->newptr && curproc != p) {
 1384                 PROC_UNLOCK(p);
 1385                 return (EPERM);
 1386         }
 1387 
 1388         pa = p->p_args;
 1389         pargs_hold(pa);
 1390         PROC_UNLOCK(p);
 1391         if (req->oldptr != NULL && pa != NULL)
 1392                 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
 1393         pargs_drop(pa);
 1394         if (error != 0 || req->newptr == NULL)
 1395                 return (error);
 1396 
 1397         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
 1398                 return (ENOMEM);
 1399         newpa = pargs_alloc(req->newlen);
 1400         error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
 1401         if (error != 0) {
 1402                 pargs_free(newpa);
 1403                 return (error);
 1404         }
 1405         PROC_LOCK(p);
 1406         pa = p->p_args;
 1407         p->p_args = newpa;
 1408         PROC_UNLOCK(p);
 1409         pargs_drop(pa);
 1410         return (0);
 1411 }
 1412 
 1413 /*
 1414  * This sysctl allows a process to retrieve the path of the executable for
 1415  * itself or another process.
 1416  */
 1417 static int
 1418 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
 1419 {
 1420         pid_t *pidp = (pid_t *)arg1;
 1421         unsigned int arglen = arg2;
 1422         struct proc *p;
 1423         struct vnode *vp;
 1424         char *retbuf, *freebuf;
 1425         int error, vfslocked;
 1426 
 1427         if (arglen != 1)
 1428                 return (EINVAL);
 1429         if (*pidp == -1) {      /* -1 means this process */
 1430                 p = req->td->td_proc;
 1431         } else {
 1432                 p = pfind(*pidp);
 1433                 if (p == NULL)
 1434                         return (ESRCH);
 1435                 if ((error = p_cansee(curthread, p)) != 0) {
 1436                         PROC_UNLOCK(p);
 1437                         return (error);
 1438                 }
 1439         }
 1440 
 1441         vp = p->p_textvp;
 1442         if (vp == NULL) {
 1443                 if (*pidp != -1)
 1444                         PROC_UNLOCK(p);
 1445                 return (0);
 1446         }
 1447         vref(vp);
 1448         if (*pidp != -1)
 1449                 PROC_UNLOCK(p);
 1450         error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
 1451         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1452         vrele(vp);
 1453         VFS_UNLOCK_GIANT(vfslocked);
 1454         if (error)
 1455                 return (error);
 1456         error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
 1457         free(freebuf, M_TEMP);
 1458         return (error);
 1459 }
 1460 
 1461 static int
 1462 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
 1463 {
 1464         struct proc *p;
 1465         char *sv_name;
 1466         int *name;
 1467         int namelen;
 1468         int error;
 1469 
 1470         namelen = arg2;
 1471         if (namelen != 1) 
 1472                 return (EINVAL);
 1473 
 1474         name = (int *)arg1;
 1475         if ((p = pfind((pid_t)name[0])) == NULL)
 1476                 return (ESRCH);
 1477         if ((error = p_cansee(curthread, p))) {
 1478                 PROC_UNLOCK(p);
 1479                 return (error);
 1480         }
 1481         sv_name = p->p_sysent->sv_name;
 1482         PROC_UNLOCK(p);
 1483         return (sysctl_handle_string(oidp, sv_name, 0, req));
 1484 }
 1485 
 1486 #ifdef KINFO_OVMENTRY_SIZE
 1487 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
 1488 #endif
 1489 
 1490 #ifdef COMPAT_FREEBSD7
 1491 static int
 1492 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
 1493 {
 1494         vm_map_entry_t entry, tmp_entry;
 1495         unsigned int last_timestamp;
 1496         char *fullpath, *freepath;
 1497         struct kinfo_ovmentry *kve;
 1498         struct vattr va;
 1499         struct ucred *cred;
 1500         int error, *name;
 1501         struct vnode *vp;
 1502         struct proc *p;
 1503         vm_map_t map;
 1504         struct vmspace *vm;
 1505 
 1506         name = (int *)arg1;
 1507         if ((p = pfind((pid_t)name[0])) == NULL)
 1508                 return (ESRCH);
 1509         if (p->p_flag & P_WEXIT) {
 1510                 PROC_UNLOCK(p);
 1511                 return (ESRCH);
 1512         }
 1513         if ((error = p_candebug(curthread, p))) {
 1514                 PROC_UNLOCK(p);
 1515                 return (error);
 1516         }
 1517         _PHOLD(p);
 1518         PROC_UNLOCK(p);
 1519         vm = vmspace_acquire_ref(p);
 1520         if (vm == NULL) {
 1521                 PRELE(p);
 1522                 return (ESRCH);
 1523         }
 1524         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
 1525 
 1526         map = &p->p_vmspace->vm_map;    /* XXXRW: More locking required? */
 1527         vm_map_lock_read(map);
 1528         for (entry = map->header.next; entry != &map->header;
 1529             entry = entry->next) {
 1530                 vm_object_t obj, tobj, lobj;
 1531                 vm_offset_t addr;
 1532                 int vfslocked;
 1533 
 1534                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 1535                         continue;
 1536 
 1537                 bzero(kve, sizeof(*kve));
 1538                 kve->kve_structsize = sizeof(*kve);
 1539 
 1540                 kve->kve_private_resident = 0;
 1541                 obj = entry->object.vm_object;
 1542                 if (obj != NULL) {
 1543                         VM_OBJECT_LOCK(obj);
 1544                         if (obj->shadow_count == 1)
 1545                                 kve->kve_private_resident =
 1546                                     obj->resident_page_count;
 1547                 }
 1548                 kve->kve_resident = 0;
 1549                 addr = entry->start;
 1550                 while (addr < entry->end) {
 1551                         if (pmap_extract(map->pmap, addr))
 1552                                 kve->kve_resident++;
 1553                         addr += PAGE_SIZE;
 1554                 }
 1555 
 1556                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
 1557                         if (tobj != obj)
 1558                                 VM_OBJECT_LOCK(tobj);
 1559                         if (lobj != obj)
 1560                                 VM_OBJECT_UNLOCK(lobj);
 1561                         lobj = tobj;
 1562                 }
 1563 
 1564                 kve->kve_start = (void*)entry->start;
 1565                 kve->kve_end = (void*)entry->end;
 1566                 kve->kve_offset = (off_t)entry->offset;
 1567 
 1568                 if (entry->protection & VM_PROT_READ)
 1569                         kve->kve_protection |= KVME_PROT_READ;
 1570                 if (entry->protection & VM_PROT_WRITE)
 1571                         kve->kve_protection |= KVME_PROT_WRITE;
 1572                 if (entry->protection & VM_PROT_EXECUTE)
 1573                         kve->kve_protection |= KVME_PROT_EXEC;
 1574 
 1575                 if (entry->eflags & MAP_ENTRY_COW)
 1576                         kve->kve_flags |= KVME_FLAG_COW;
 1577                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
 1578                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
 1579 
 1580                 last_timestamp = map->timestamp;
 1581                 vm_map_unlock_read(map);
 1582 
 1583                 kve->kve_fileid = 0;
 1584                 kve->kve_fsid = 0;
 1585                 freepath = NULL;
 1586                 fullpath = "";
 1587                 if (lobj) {
 1588                         vp = NULL;
 1589                         switch (lobj->type) {
 1590                         case OBJT_DEFAULT:
 1591                                 kve->kve_type = KVME_TYPE_DEFAULT;
 1592                                 break;
 1593                         case OBJT_VNODE:
 1594                                 kve->kve_type = KVME_TYPE_VNODE;
 1595                                 vp = lobj->handle;
 1596                                 vref(vp);
 1597                                 break;
 1598                         case OBJT_SWAP:
 1599                                 kve->kve_type = KVME_TYPE_SWAP;
 1600                                 break;
 1601                         case OBJT_DEVICE:
 1602                                 kve->kve_type = KVME_TYPE_DEVICE;
 1603                                 break;
 1604                         case OBJT_PHYS:
 1605                                 kve->kve_type = KVME_TYPE_PHYS;
 1606                                 break;
 1607                         case OBJT_DEAD:
 1608                                 kve->kve_type = KVME_TYPE_DEAD;
 1609                                 break;
 1610                         case OBJT_SG:
 1611                                 kve->kve_type = KVME_TYPE_SG;
 1612                                 break;
 1613                         default:
 1614                                 kve->kve_type = KVME_TYPE_UNKNOWN;
 1615                                 break;
 1616                         }
 1617                         if (lobj != obj)
 1618                                 VM_OBJECT_UNLOCK(lobj);
 1619 
 1620                         kve->kve_ref_count = obj->ref_count;
 1621                         kve->kve_shadow_count = obj->shadow_count;
 1622                         VM_OBJECT_UNLOCK(obj);
 1623                         if (vp != NULL) {
 1624                                 vn_fullpath(curthread, vp, &fullpath,
 1625                                     &freepath);
 1626                                 cred = curthread->td_ucred;
 1627                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1628                                 vn_lock(vp, LK_SHARED | LK_RETRY);
 1629                                 if (VOP_GETATTR(vp, &va, cred) == 0) {
 1630                                         kve->kve_fileid = va.va_fileid;
 1631                                         kve->kve_fsid = va.va_fsid;
 1632                                 }
 1633                                 vput(vp);
 1634                                 VFS_UNLOCK_GIANT(vfslocked);
 1635                         }
 1636                 } else {
 1637                         kve->kve_type = KVME_TYPE_NONE;
 1638                         kve->kve_ref_count = 0;
 1639                         kve->kve_shadow_count = 0;
 1640                 }
 1641 
 1642                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
 1643                 if (freepath != NULL)
 1644                         free(freepath, M_TEMP);
 1645 
 1646                 error = SYSCTL_OUT(req, kve, sizeof(*kve));
 1647                 vm_map_lock_read(map);
 1648                 if (error)
 1649                         break;
 1650                 if (last_timestamp != map->timestamp) {
 1651                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
 1652                         entry = tmp_entry;
 1653                 }
 1654         }
 1655         vm_map_unlock_read(map);
 1656         vmspace_free(vm);
 1657         PRELE(p);
 1658         free(kve, M_TEMP);
 1659         return (error);
 1660 }
 1661 #endif  /* COMPAT_FREEBSD7 */
 1662 
 1663 #ifdef KINFO_VMENTRY_SIZE
 1664 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
 1665 #endif
 1666 
 1667 static int
 1668 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
 1669 {
 1670         vm_map_entry_t entry, tmp_entry;
 1671         unsigned int last_timestamp;
 1672         char *fullpath, *freepath;
 1673         struct kinfo_vmentry *kve;
 1674         struct vattr va;
 1675         struct ucred *cred;
 1676         int error, *name;
 1677         struct vnode *vp;
 1678         struct proc *p;
 1679         struct vmspace *vm;
 1680         vm_map_t map;
 1681 
 1682         name = (int *)arg1;
 1683         if ((p = pfind((pid_t)name[0])) == NULL)
 1684                 return (ESRCH);
 1685         if (p->p_flag & P_WEXIT) {
 1686                 PROC_UNLOCK(p);
 1687                 return (ESRCH);
 1688         }
 1689         if ((error = p_candebug(curthread, p))) {
 1690                 PROC_UNLOCK(p);
 1691                 return (error);
 1692         }
 1693         _PHOLD(p);
 1694         PROC_UNLOCK(p);
 1695         vm = vmspace_acquire_ref(p);
 1696         if (vm == NULL) {
 1697                 PRELE(p);
 1698                 return (ESRCH);
 1699         }
 1700         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
 1701 
 1702         map = &vm->vm_map;      /* XXXRW: More locking required? */
 1703         vm_map_lock_read(map);
 1704         for (entry = map->header.next; entry != &map->header;
 1705             entry = entry->next) {
 1706                 vm_object_t obj, tobj, lobj;
 1707                 vm_offset_t addr;
 1708                 int vfslocked;
 1709 
 1710                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 1711                         continue;
 1712 
 1713                 bzero(kve, sizeof(*kve));
 1714 
 1715                 kve->kve_private_resident = 0;
 1716                 obj = entry->object.vm_object;
 1717                 if (obj != NULL) {
 1718                         VM_OBJECT_LOCK(obj);
 1719                         if (obj->shadow_count == 1)
 1720                                 kve->kve_private_resident =
 1721                                     obj->resident_page_count;
 1722                 }
 1723                 kve->kve_resident = 0;
 1724                 addr = entry->start;
 1725                 while (addr < entry->end) {
 1726                         if (pmap_extract(map->pmap, addr))
 1727                                 kve->kve_resident++;
 1728                         addr += PAGE_SIZE;
 1729                 }
 1730 
 1731                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
 1732                         if (tobj != obj)
 1733                                 VM_OBJECT_LOCK(tobj);
 1734                         if (lobj != obj)
 1735                                 VM_OBJECT_UNLOCK(lobj);
 1736                         lobj = tobj;
 1737                 }
 1738 
 1739                 kve->kve_start = entry->start;
 1740                 kve->kve_end = entry->end;
 1741                 kve->kve_offset = entry->offset;
 1742 
 1743                 if (entry->protection & VM_PROT_READ)
 1744                         kve->kve_protection |= KVME_PROT_READ;
 1745                 if (entry->protection & VM_PROT_WRITE)
 1746                         kve->kve_protection |= KVME_PROT_WRITE;
 1747                 if (entry->protection & VM_PROT_EXECUTE)
 1748                         kve->kve_protection |= KVME_PROT_EXEC;
 1749 
 1750                 if (entry->eflags & MAP_ENTRY_COW)
 1751                         kve->kve_flags |= KVME_FLAG_COW;
 1752                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
 1753                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
 1754 
 1755                 last_timestamp = map->timestamp;
 1756                 vm_map_unlock_read(map);
 1757 
 1758                 kve->kve_fileid = 0;
 1759                 kve->kve_fsid = 0;
 1760                 freepath = NULL;
 1761                 fullpath = "";
 1762                 if (lobj) {
 1763                         vp = NULL;
 1764                         switch (lobj->type) {
 1765                         case OBJT_DEFAULT:
 1766                                 kve->kve_type = KVME_TYPE_DEFAULT;
 1767                                 break;
 1768                         case OBJT_VNODE:
 1769                                 kve->kve_type = KVME_TYPE_VNODE;
 1770                                 vp = lobj->handle;
 1771                                 vref(vp);
 1772                                 break;
 1773                         case OBJT_SWAP:
 1774                                 kve->kve_type = KVME_TYPE_SWAP;
 1775                                 break;
 1776                         case OBJT_DEVICE:
 1777                                 kve->kve_type = KVME_TYPE_DEVICE;
 1778                                 break;
 1779                         case OBJT_PHYS:
 1780                                 kve->kve_type = KVME_TYPE_PHYS;
 1781                                 break;
 1782                         case OBJT_DEAD:
 1783                                 kve->kve_type = KVME_TYPE_DEAD;
 1784                                 break;
 1785                         case OBJT_SG:
 1786                                 kve->kve_type = KVME_TYPE_SG;
 1787                                 break;
 1788                         default:
 1789                                 kve->kve_type = KVME_TYPE_UNKNOWN;
 1790                                 break;
 1791                         }
 1792                         if (lobj != obj)
 1793                                 VM_OBJECT_UNLOCK(lobj);
 1794 
 1795                         kve->kve_ref_count = obj->ref_count;
 1796                         kve->kve_shadow_count = obj->shadow_count;
 1797                         VM_OBJECT_UNLOCK(obj);
 1798                         if (vp != NULL) {
 1799                                 vn_fullpath(curthread, vp, &fullpath,
 1800                                     &freepath);
 1801                                 cred = curthread->td_ucred;
 1802                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1803                                 vn_lock(vp, LK_SHARED | LK_RETRY);
 1804                                 if (VOP_GETATTR(vp, &va, cred) == 0) {
 1805                                         kve->kve_fileid = va.va_fileid;
 1806                                         kve->kve_fsid = va.va_fsid;
 1807                                 }
 1808                                 vput(vp);
 1809                                 VFS_UNLOCK_GIANT(vfslocked);
 1810                         }
 1811                 } else {
 1812                         kve->kve_type = KVME_TYPE_NONE;
 1813                         kve->kve_ref_count = 0;
 1814                         kve->kve_shadow_count = 0;
 1815                 }
 1816 
 1817                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
 1818                 if (freepath != NULL)
 1819                         free(freepath, M_TEMP);
 1820 
 1821                 /* Pack record size down */
 1822                 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
 1823                     strlen(kve->kve_path) + 1;
 1824                 kve->kve_structsize = roundup(kve->kve_structsize,
 1825                     sizeof(uint64_t));
 1826                 error = SYSCTL_OUT(req, kve, kve->kve_structsize);
 1827                 vm_map_lock_read(map);
 1828                 if (error)
 1829                         break;
 1830                 if (last_timestamp != map->timestamp) {
 1831                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
 1832                         entry = tmp_entry;
 1833                 }
 1834         }
 1835         vm_map_unlock_read(map);
 1836         vmspace_free(vm);
 1837         PRELE(p);
 1838         free(kve, M_TEMP);
 1839         return (error);
 1840 }
 1841 
 1842 #if defined(STACK) || defined(DDB)
 1843 static int
 1844 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
 1845 {
 1846         struct kinfo_kstack *kkstp;
 1847         int error, i, *name, numthreads;
 1848         lwpid_t *lwpidarray;
 1849         struct thread *td;
 1850         struct stack *st;
 1851         struct sbuf sb;
 1852         struct proc *p;
 1853 
 1854         name = (int *)arg1;
 1855         if ((p = pfind((pid_t)name[0])) == NULL)
 1856                 return (ESRCH);
 1857         /* XXXRW: Not clear ESRCH is the right error during proc execve(). */
 1858         if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) {
 1859                 PROC_UNLOCK(p);
 1860                 return (ESRCH);
 1861         }
 1862         if ((error = p_candebug(curthread, p))) {
 1863                 PROC_UNLOCK(p);
 1864                 return (error);
 1865         }
 1866         _PHOLD(p);
 1867         PROC_UNLOCK(p);
 1868 
 1869         kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
 1870         st = stack_create();
 1871 
 1872         lwpidarray = NULL;
 1873         numthreads = 0;
 1874         PROC_LOCK(p);
 1875 repeat:
 1876         if (numthreads < p->p_numthreads) {
 1877                 if (lwpidarray != NULL) {
 1878                         free(lwpidarray, M_TEMP);
 1879                         lwpidarray = NULL;
 1880                 }
 1881                 numthreads = p->p_numthreads;
 1882                 PROC_UNLOCK(p);
 1883                 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
 1884                     M_WAITOK | M_ZERO);
 1885                 PROC_LOCK(p);
 1886                 goto repeat;
 1887         }
 1888         i = 0;
 1889 
 1890         /*
 1891          * XXXRW: During the below loop, execve(2) and countless other sorts
 1892          * of changes could have taken place.  Should we check to see if the
 1893          * vmspace has been replaced, or the like, in order to prevent
 1894          * giving a snapshot that spans, say, execve(2), with some threads
 1895          * before and some after?  Among other things, the credentials could
 1896          * have changed, in which case the right to extract debug info might
 1897          * no longer be assured.
 1898          */
 1899         FOREACH_THREAD_IN_PROC(p, td) {
 1900                 KASSERT(i < numthreads,
 1901                     ("sysctl_kern_proc_kstack: numthreads"));
 1902                 lwpidarray[i] = td->td_tid;
 1903                 i++;
 1904         }
 1905         numthreads = i;
 1906         for (i = 0; i < numthreads; i++) {
 1907                 td = thread_find(p, lwpidarray[i]);
 1908                 if (td == NULL) {
 1909                         continue;
 1910                 }
 1911                 bzero(kkstp, sizeof(*kkstp));
 1912                 (void)sbuf_new(&sb, kkstp->kkst_trace,
 1913                     sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
 1914                 thread_lock(td);
 1915                 kkstp->kkst_tid = td->td_tid;
 1916                 if (TD_IS_SWAPPED(td))
 1917                         kkstp->kkst_state = KKST_STATE_SWAPPED;
 1918                 else if (TD_IS_RUNNING(td))
 1919                         kkstp->kkst_state = KKST_STATE_RUNNING;
 1920                 else {
 1921                         kkstp->kkst_state = KKST_STATE_STACKOK;
 1922                         stack_save_td(st, td);
 1923                 }
 1924                 thread_unlock(td);
 1925                 PROC_UNLOCK(p);
 1926                 stack_sbuf_print(&sb, st);
 1927                 sbuf_finish(&sb);
 1928                 sbuf_delete(&sb);
 1929                 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
 1930                 PROC_LOCK(p);
 1931                 if (error)
 1932                         break;
 1933         }
 1934         _PRELE(p);
 1935         PROC_UNLOCK(p);
 1936         if (lwpidarray != NULL)
 1937                 free(lwpidarray, M_TEMP);
 1938         stack_destroy(st);
 1939         free(kkstp, M_TEMP);
 1940         return (error);
 1941 }
 1942 #endif
 1943 
 1944 /*
 1945  * This sysctl allows a process to retrieve the full list of groups from
 1946  * itself or another process.
 1947  */
 1948 static int
 1949 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
 1950 {
 1951         pid_t *pidp = (pid_t *)arg1;
 1952         unsigned int arglen = arg2;
 1953         struct proc *p;
 1954         struct ucred *cred;
 1955         int error;
 1956 
 1957         if (arglen != 1)
 1958                 return (EINVAL);
 1959         if (*pidp == -1) {      /* -1 means this process */
 1960                 p = req->td->td_proc;
 1961         } else {
 1962                 p = pfind(*pidp);
 1963                 if (p == NULL)
 1964                         return (ESRCH);
 1965                 if ((error = p_cansee(curthread, p)) != 0) {
 1966                         PROC_UNLOCK(p);
 1967                         return (error);
 1968                 }
 1969         }
 1970 
 1971         cred = crhold(p->p_ucred);
 1972         if (*pidp != -1)
 1973                 PROC_UNLOCK(p);
 1974 
 1975         error = SYSCTL_OUT(req, cred->cr_groups,
 1976             cred->cr_ngroups * sizeof(gid_t));
 1977         crfree(cred);
 1978         return (error);
 1979 }
 1980 
 1981 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
 1982 
 1983 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
 1984         CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
 1985         "Return entire process table");
 1986 
 1987 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1988         sysctl_kern_proc, "Process table");
 1989 
 1990 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1991         sysctl_kern_proc, "Process table");
 1992 
 1993 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1994         sysctl_kern_proc, "Process table");
 1995 
 1996 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
 1997         CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1998 
 1999 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 
 2000         sysctl_kern_proc, "Process table");
 2001 
 2002 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 
 2003         sysctl_kern_proc, "Process table");
 2004 
 2005 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2006         sysctl_kern_proc, "Process table");
 2007 
 2008 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2009         sysctl_kern_proc, "Process table");
 2010 
 2011 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2012         sysctl_kern_proc, "Return process table, no threads");
 2013 
 2014 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
 2015         CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
 2016         sysctl_kern_proc_args, "Process argument list");
 2017 
 2018 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
 2019         CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
 2020 
 2021 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
 2022         CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
 2023         "Process syscall vector name (ABI type)");
 2024 
 2025 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
 2026         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2027 
 2028 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
 2029         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2030 
 2031 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
 2032         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2033 
 2034 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
 2035         sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2036 
 2037 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
 2038         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2039 
 2040 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
 2041         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2042 
 2043 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
 2044         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2045 
 2046 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
 2047         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2048 
 2049 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
 2050         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
 2051         "Return process table, no threads");
 2052 
 2053 #ifdef COMPAT_FREEBSD7
 2054 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
 2055         CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
 2056 #endif
 2057 
 2058 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
 2059         CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
 2060 
 2061 #if defined(STACK) || defined(DDB)
 2062 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
 2063         CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
 2064 #endif
 2065 
 2066 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
 2067         CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");

Cache object: fc525b52f332571b1187ddcffaa5d1a6


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