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

Cache object: 0a9c47806a7adb25d614b77ce866dad0


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