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

Cache object: 3d536afa2f8edfc31f8798b17742ce2c


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