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


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

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

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

    1 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include "opt_compat.h"
   36 #include "opt_ddb.h"
   37 #include "opt_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/lock.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mount.h>
   48 #include <sys/mutex.h>
   49 #include <sys/proc.h>
   50 #include <sys/refcount.h>
   51 #include <sys/sbuf.h>
   52 #include <sys/sysent.h>
   53 #include <sys/sched.h>
   54 #include <sys/smp.h>
   55 #include <sys/stack.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/filedesc.h>
   58 #include <sys/tty.h>
   59 #include <sys/sdt.h>
   60 #include <sys/signalvar.h>
   61 #include <sys/sx.h>
   62 #include <sys/user.h>
   63 #include <sys/jail.h>
   64 #include <sys/vnode.h>
   65 #include <sys/eventhandler.h>
   66 #ifdef KTRACE
   67 #include <sys/uio.h>
   68 #include <sys/ktrace.h>
   69 #endif
   70 
   71 #ifdef DDB
   72 #include <ddb/ddb.h>
   73 #endif
   74 
   75 #include <vm/vm.h>
   76 #include <vm/vm_extern.h>
   77 #include <vm/pmap.h>
   78 #include <vm/vm_map.h>
   79 #include <vm/vm_object.h>
   80 #include <vm/uma.h>
   81 
   82 SDT_PROVIDER_DEFINE(proc);
   83 SDT_PROBE_DEFINE(proc, kernel, ctor, entry);
   84 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
   85 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
   86 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
   87 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
   88 SDT_PROBE_DEFINE(proc, kernel, ctor, return);
   89 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
   90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
   91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
   92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
   93 SDT_PROBE_DEFINE(proc, kernel, dtor, entry);
   94 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
   95 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
   96 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
   97 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
   98 SDT_PROBE_DEFINE(proc, kernel, dtor, return);
   99 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
  100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
  101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
  102 SDT_PROBE_DEFINE(proc, kernel, init, entry);
  103 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
  104 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
  105 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
  106 SDT_PROBE_DEFINE(proc, kernel, init, return);
  107 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
  108 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
  109 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
  110 
  111 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
  112 MALLOC_DEFINE(M_SESSION, "session", "session header");
  113 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
  114 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
  115 
  116 static void doenterpgrp(struct proc *, struct pgrp *);
  117 static void orphanpg(struct pgrp *pg);
  118 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
  119 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
  120     int preferthread);
  121 static void pgadjustjobc(struct pgrp *pgrp, int entering);
  122 static void pgdelete(struct pgrp *);
  123 static int proc_ctor(void *mem, int size, void *arg, int flags);
  124 static void proc_dtor(void *mem, int size, void *arg);
  125 static int proc_init(void *mem, int size, int flags);
  126 static void proc_fini(void *mem, int size);
  127 static void pargs_free(struct pargs *pa);
  128 
  129 /*
  130  * Other process lists
  131  */
  132 struct pidhashhead *pidhashtbl;
  133 u_long pidhash;
  134 struct pgrphashhead *pgrphashtbl;
  135 u_long pgrphash;
  136 struct proclist allproc;
  137 struct proclist zombproc;
  138 struct sx allproc_lock;
  139 struct sx proctree_lock;
  140 struct mtx ppeers_lock;
  141 uma_zone_t proc_zone;
  142 uma_zone_t ithread_zone;
  143 
  144 int kstack_pages = KSTACK_PAGES;
  145 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
  146 
  147 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
  148 
  149 /*
  150  * Initialize global process hashing structures.
  151  */
  152 void
  153 procinit()
  154 {
  155 
  156         sx_init(&allproc_lock, "allproc");
  157         sx_init(&proctree_lock, "proctree");
  158         mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
  159         LIST_INIT(&allproc);
  160         LIST_INIT(&zombproc);
  161         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
  162         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
  163         proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
  164             proc_ctor, proc_dtor, proc_init, proc_fini,
  165             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  166         uihashinit();
  167 }
  168 
  169 /*
  170  * Prepare a proc for use.
  171  */
  172 static int
  173 proc_ctor(void *mem, int size, void *arg, int flags)
  174 {
  175         struct proc *p;
  176 
  177         p = (struct proc *)mem;
  178         SDT_PROBE(proc, kernel, ctor, entry, p, size, arg, flags, 0);
  179         EVENTHANDLER_INVOKE(process_ctor, p);
  180         SDT_PROBE(proc, kernel, ctor, return, p, size, arg, flags, 0);
  181         return (0);
  182 }
  183 
  184 /*
  185  * Reclaim a proc after use.
  186  */
  187 static void
  188 proc_dtor(void *mem, int size, void *arg)
  189 {
  190         struct proc *p;
  191         struct thread *td;
  192 
  193         /* INVARIANTS checks go here */
  194         p = (struct proc *)mem;
  195         td = FIRST_THREAD_IN_PROC(p);
  196         SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
  197         if (td != NULL) {
  198 #ifdef INVARIANTS
  199                 KASSERT((p->p_numthreads == 1),
  200                     ("bad number of threads in exiting process"));
  201                 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
  202 #endif
  203 
  204                 /* Dispose of an alternate kstack, if it exists.
  205                  * XXX What if there are more than one thread in the proc?
  206                  *     The first thread in the proc is special and not
  207                  *     freed, so you gotta do this here.
  208                  */
  209                 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
  210                         vm_thread_dispose_altkstack(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         TAILQ_INIT(&p->p_threads);           /* all threads in proc */
  233         EVENTHANDLER_INVOKE(process_init, p);
  234         p->p_stats = pstats_alloc();
  235         SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
  236         return (0);
  237 }
  238 
  239 /*
  240  * UMA should ensure that this function is never called.
  241  * Freeing a proc structure would violate type stability.
  242  */
  243 static void
  244 proc_fini(void *mem, int size)
  245 {
  246 #ifdef notnow
  247         struct proc *p;
  248 
  249         p = (struct proc *)mem;
  250         EVENTHANDLER_INVOKE(process_fini, p);
  251         pstats_free(p->p_stats);
  252         thread_free(FIRST_THREAD_IN_PROC(p));
  253         mtx_destroy(&p->p_mtx);
  254         if (p->p_ksi != NULL)
  255                 ksiginfo_free(p->p_ksi);
  256 #else
  257         panic("proc reclaimed");
  258 #endif
  259 }
  260 
  261 /*
  262  * Is p an inferior of the current process?
  263  */
  264 int
  265 inferior(p)
  266         register struct proc *p;
  267 {
  268 
  269         sx_assert(&proctree_lock, SX_LOCKED);
  270         for (; p != curproc; p = p->p_pptr)
  271                 if (p->p_pid == 0)
  272                         return (0);
  273         return (1);
  274 }
  275 
  276 /*
  277  * Locate a process by number; return only "live" processes -- i.e., neither
  278  * zombies nor newly born but incompletely initialized processes.  By not
  279  * returning processes in the PRS_NEW state, we allow callers to avoid
  280  * testing for that condition to avoid dereferencing p_ucred, et al.
  281  */
  282 struct proc *
  283 pfind(pid)
  284         register pid_t pid;
  285 {
  286         register struct proc *p;
  287 
  288         sx_slock(&allproc_lock);
  289         LIST_FOREACH(p, PIDHASH(pid), p_hash)
  290                 if (p->p_pid == pid) {
  291                         if (p->p_state == PRS_NEW) {
  292                                 p = NULL;
  293                                 break;
  294                         }
  295                         PROC_LOCK(p);
  296                         break;
  297                 }
  298         sx_sunlock(&allproc_lock);
  299         return (p);
  300 }
  301 
  302 /*
  303  * Locate a process group by number.
  304  * The caller must hold proctree_lock.
  305  */
  306 struct pgrp *
  307 pgfind(pgid)
  308         register pid_t pgid;
  309 {
  310         register struct pgrp *pgrp;
  311 
  312         sx_assert(&proctree_lock, SX_LOCKED);
  313 
  314         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
  315                 if (pgrp->pg_id == pgid) {
  316                         PGRP_LOCK(pgrp);
  317                         return (pgrp);
  318                 }
  319         }
  320         return (NULL);
  321 }
  322 
  323 /*
  324  * Create a new process group.
  325  * pgid must be equal to the pid of p.
  326  * Begin a new session if required.
  327  */
  328 int
  329 enterpgrp(p, pgid, pgrp, sess)
  330         register struct proc *p;
  331         pid_t pgid;
  332         struct pgrp *pgrp;
  333         struct session *sess;
  334 {
  335         struct pgrp *pgrp2;
  336 
  337         sx_assert(&proctree_lock, SX_XLOCKED);
  338 
  339         KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
  340         KASSERT(p->p_pid == pgid,
  341             ("enterpgrp: new pgrp and pid != pgid"));
  342 
  343         pgrp2 = pgfind(pgid);
  344 
  345         KASSERT(pgrp2 == NULL,
  346             ("enterpgrp: pgrp with pgid exists"));
  347         KASSERT(!SESS_LEADER(p),
  348             ("enterpgrp: session leader attempted setpgrp"));
  349 
  350         mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
  351 
  352         if (sess != NULL) {
  353                 /*
  354                  * new session
  355                  */
  356                 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
  357                 mtx_lock(&Giant);       /* XXX TTY */
  358                 PROC_LOCK(p);
  359                 p->p_flag &= ~P_CONTROLT;
  360                 PROC_UNLOCK(p);
  361                 PGRP_LOCK(pgrp);
  362                 sess->s_leader = p;
  363                 sess->s_sid = p->p_pid;
  364                 sess->s_count = 1;
  365                 sess->s_ttyvp = NULL;
  366                 sess->s_ttyp = NULL;
  367                 bcopy(p->p_session->s_login, sess->s_login,
  368                             sizeof(sess->s_login));
  369                 pgrp->pg_session = sess;
  370                 KASSERT(p == curproc,
  371                     ("enterpgrp: mksession and p != curproc"));
  372         } else {
  373                 mtx_lock(&Giant);       /* XXX TTY */
  374                 pgrp->pg_session = p->p_session;
  375                 SESS_LOCK(pgrp->pg_session);
  376                 pgrp->pg_session->s_count++;
  377                 SESS_UNLOCK(pgrp->pg_session);
  378                 PGRP_LOCK(pgrp);
  379         }
  380         pgrp->pg_id = pgid;
  381         LIST_INIT(&pgrp->pg_members);
  382 
  383         /*
  384          * As we have an exclusive lock of proctree_lock,
  385          * this should not deadlock.
  386          */
  387         LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
  388         pgrp->pg_jobc = 0;
  389         SLIST_INIT(&pgrp->pg_sigiolst);
  390         PGRP_UNLOCK(pgrp);
  391         mtx_unlock(&Giant);       /* XXX TTY */
  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         mtx_lock(&Giant);       /* XXX TTY */
  452         PGRP_LOCK(pgrp);
  453         PGRP_LOCK(savepgrp);
  454         PROC_LOCK(p);
  455         LIST_REMOVE(p, p_pglist);
  456         p->p_pgrp = pgrp;
  457         PROC_UNLOCK(p);
  458         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
  459         PGRP_UNLOCK(savepgrp);
  460         PGRP_UNLOCK(pgrp);
  461         mtx_unlock(&Giant);     /* XXX TTY */
  462         if (LIST_EMPTY(&savepgrp->pg_members))
  463                 pgdelete(savepgrp);
  464 }
  465 
  466 /*
  467  * remove process from process group
  468  */
  469 int
  470 leavepgrp(p)
  471         register struct proc *p;
  472 {
  473         struct pgrp *savepgrp;
  474 
  475         sx_assert(&proctree_lock, SX_XLOCKED);
  476         savepgrp = p->p_pgrp;
  477         mtx_lock(&Giant);       /* XXX TTY */
  478         PGRP_LOCK(savepgrp);
  479         PROC_LOCK(p);
  480         LIST_REMOVE(p, p_pglist);
  481         p->p_pgrp = NULL;
  482         PROC_UNLOCK(p);
  483         PGRP_UNLOCK(savepgrp);
  484         mtx_unlock(&Giant);     /* XXX TTY */
  485         if (LIST_EMPTY(&savepgrp->pg_members))
  486                 pgdelete(savepgrp);
  487         return (0);
  488 }
  489 
  490 /*
  491  * delete a process group
  492  */
  493 static void
  494 pgdelete(pgrp)
  495         register struct pgrp *pgrp;
  496 {
  497         struct session *savesess;
  498 
  499         sx_assert(&proctree_lock, SX_XLOCKED);
  500         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  501         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  502 
  503         /*
  504          * Reset any sigio structures pointing to us as a result of
  505          * F_SETOWN with our pgid.
  506          */
  507         funsetownlst(&pgrp->pg_sigiolst);
  508 
  509         mtx_lock(&Giant);       /* XXX TTY */
  510         PGRP_LOCK(pgrp);
  511         if (pgrp->pg_session->s_ttyp != NULL &&
  512             pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
  513                 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
  514         LIST_REMOVE(pgrp, pg_hash);
  515         savesess = pgrp->pg_session;
  516         SESSRELE(savesess);
  517         PGRP_UNLOCK(pgrp);
  518         mtx_destroy(&pgrp->pg_mtx);
  519         FREE(pgrp, M_PGRP);
  520         mtx_unlock(&Giant);     /* XXX TTY */
  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 sessrele(struct session *s)
  624 {
  625         int i;
  626 
  627         SESS_LOCK(s);
  628         i = --s->s_count;
  629         SESS_UNLOCK(s);
  630         if (i == 0) {
  631                 if (s->s_ttyp != NULL)
  632                         ttyrel(s->s_ttyp);
  633                 mtx_destroy(&s->s_mtx);
  634                 FREE(s, M_SESSION);
  635         }
  636 }
  637 
  638 #include "opt_ddb.h"
  639 #ifdef DDB
  640 #include <ddb/ddb.h>
  641 
  642 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
  643 {
  644         register struct pgrp *pgrp;
  645         register struct proc *p;
  646         register int i;
  647 
  648         for (i = 0; i <= pgrphash; i++) {
  649                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
  650                         printf("\tindx %d\n", i);
  651                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
  652                                 printf(
  653                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
  654                                     (void *)pgrp, (long)pgrp->pg_id,
  655                                     (void *)pgrp->pg_session,
  656                                     pgrp->pg_session->s_count,
  657                                     (void *)LIST_FIRST(&pgrp->pg_members));
  658                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
  659                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
  660                                             (long)p->p_pid, (void *)p,
  661                                             (void *)p->p_pgrp);
  662                                 }
  663                         }
  664                 }
  665         }
  666 }
  667 #endif /* DDB */
  668 
  669 /*
  670  * Clear kinfo_proc and fill in any information that is common
  671  * to all threads in the process.
  672  * Must be called with the target process locked.
  673  */
  674 static void
  675 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
  676 {
  677         struct thread *td0;
  678         struct tty *tp;
  679         struct session *sp;
  680         struct ucred *cred;
  681         struct sigacts *ps;
  682 
  683         bzero(kp, sizeof(*kp));
  684 
  685         kp->ki_structsize = sizeof(*kp);
  686         kp->ki_paddr = p;
  687         PROC_LOCK_ASSERT(p, MA_OWNED);
  688         kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
  689         kp->ki_args = p->p_args;
  690         kp->ki_textvp = p->p_textvp;
  691 #ifdef KTRACE
  692         kp->ki_tracep = p->p_tracevp;
  693         mtx_lock(&ktrace_mtx);
  694         kp->ki_traceflag = p->p_traceflag;
  695         mtx_unlock(&ktrace_mtx);
  696 #endif
  697         kp->ki_fd = p->p_fd;
  698         kp->ki_vmspace = p->p_vmspace;
  699         kp->ki_flag = p->p_flag;
  700         cred = p->p_ucred;
  701         if (cred) {
  702                 kp->ki_uid = cred->cr_uid;
  703                 kp->ki_ruid = cred->cr_ruid;
  704                 kp->ki_svuid = cred->cr_svuid;
  705                 /* XXX bde doesn't like KI_NGROUPS */
  706                 kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
  707                 bcopy(cred->cr_groups, kp->ki_groups,
  708                     kp->ki_ngroups * sizeof(gid_t));
  709                 kp->ki_rgid = cred->cr_rgid;
  710                 kp->ki_svgid = cred->cr_svgid;
  711                 /* If jailed(cred), emulate the old P_JAILED flag. */
  712                 if (jailed(cred)) {
  713                         kp->ki_flag |= P_JAILED;
  714                         /* If inside a jail, use 0 as a jail ID. */
  715                         if (!jailed(curthread->td_ucred))
  716                                 kp->ki_jid = cred->cr_prison->pr_id;
  717                 }
  718         }
  719         ps = p->p_sigacts;
  720         if (ps) {
  721                 mtx_lock(&ps->ps_mtx);
  722                 kp->ki_sigignore = ps->ps_sigignore;
  723                 kp->ki_sigcatch = ps->ps_sigcatch;
  724                 mtx_unlock(&ps->ps_mtx);
  725         }
  726         PROC_SLOCK(p);
  727         if (p->p_state != PRS_NEW &&
  728             p->p_state != PRS_ZOMBIE &&
  729             p->p_vmspace != NULL) {
  730                 struct vmspace *vm = p->p_vmspace;
  731 
  732                 kp->ki_size = vm->vm_map.size;
  733                 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
  734                 FOREACH_THREAD_IN_PROC(p, td0) {
  735                         if (!TD_IS_SWAPPED(td0))
  736                                 kp->ki_rssize += td0->td_kstack_pages;
  737                         if (td0->td_altkstack_obj != NULL)
  738                                 kp->ki_rssize += td0->td_altkstack_pages;
  739                 }
  740                 kp->ki_swrss = vm->vm_swrss;
  741                 kp->ki_tsize = vm->vm_tsize;
  742                 kp->ki_dsize = vm->vm_dsize;
  743                 kp->ki_ssize = vm->vm_ssize;
  744         } else if (p->p_state == PRS_ZOMBIE)
  745                 kp->ki_stat = SZOMB;
  746         if (kp->ki_flag & P_INMEM)
  747                 kp->ki_sflag = PS_INMEM;
  748         else
  749                 kp->ki_sflag = 0;
  750         /* Calculate legacy swtime as seconds since 'swtick'. */
  751         kp->ki_swtime = (ticks - p->p_swtick) / hz;
  752         kp->ki_pid = p->p_pid;
  753         kp->ki_nice = p->p_nice;
  754         rufetch(p, &kp->ki_rusage);
  755         kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
  756         PROC_SUNLOCK(p);
  757         if ((p->p_flag & P_INMEM) && p->p_stats != NULL) {
  758                 kp->ki_start = p->p_stats->p_start;
  759                 timevaladd(&kp->ki_start, &boottime);
  760                 PROC_SLOCK(p);
  761                 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
  762                 PROC_SUNLOCK(p);
  763                 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
  764 
  765                 /* Some callers want child-times in a single value */
  766                 kp->ki_childtime = kp->ki_childstime;
  767                 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
  768         }
  769         tp = NULL;
  770         if (p->p_pgrp) {
  771                 kp->ki_pgid = p->p_pgrp->pg_id;
  772                 kp->ki_jobc = p->p_pgrp->pg_jobc;
  773                 sp = p->p_pgrp->pg_session;
  774 
  775                 if (sp != NULL) {
  776                         kp->ki_sid = sp->s_sid;
  777                         SESS_LOCK(sp);
  778                         strlcpy(kp->ki_login, sp->s_login,
  779                             sizeof(kp->ki_login));
  780                         if (sp->s_ttyvp)
  781                                 kp->ki_kiflag |= KI_CTTY;
  782                         if (SESS_LEADER(p))
  783                                 kp->ki_kiflag |= KI_SLEADER;
  784                         tp = sp->s_ttyp;
  785                         SESS_UNLOCK(sp);
  786                 }
  787         }
  788         if ((p->p_flag & P_CONTROLT) && tp != NULL) {
  789                 kp->ki_tdev = dev2udev(tp->t_dev);
  790                 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
  791                 if (tp->t_session)
  792                         kp->ki_tsid = tp->t_session->s_sid;
  793         } else
  794                 kp->ki_tdev = NODEV;
  795         if (p->p_comm[0] != '\0')
  796                 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
  797         if (p->p_sysent && p->p_sysent->sv_name != NULL &&
  798             p->p_sysent->sv_name[0] != '\0')
  799                 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
  800         kp->ki_siglist = p->p_siglist;
  801         kp->ki_xstat = p->p_xstat;
  802         kp->ki_acflag = p->p_acflag;
  803         kp->ki_lock = p->p_lock;
  804         if (p->p_pptr)
  805                 kp->ki_ppid = p->p_pptr->p_pid;
  806 }
  807 
  808 /*
  809  * Fill in information that is thread specific.  Must be called with p_slock
  810  * locked.  If 'preferthread' is set, overwrite certain process-related
  811  * fields that are maintained for both threads and processes.
  812  */
  813 static void
  814 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
  815 {
  816         struct proc *p;
  817 
  818         p = td->td_proc;
  819         PROC_SLOCK_ASSERT(p, MA_OWNED);
  820 
  821         thread_lock(td);
  822         if (td->td_wmesg != NULL)
  823                 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
  824         else
  825                 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
  826         if (td->td_name[0] != '\0')
  827                 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
  828         if (TD_ON_LOCK(td)) {
  829                 kp->ki_kiflag |= KI_LOCKBLOCK;
  830                 strlcpy(kp->ki_lockname, td->td_lockname,
  831                     sizeof(kp->ki_lockname));
  832         } else {
  833                 kp->ki_kiflag &= ~KI_LOCKBLOCK;
  834                 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
  835         }
  836 
  837         if (p->p_state == PRS_NORMAL) { /*  XXXKSE very approximate */
  838                 if (TD_ON_RUNQ(td) ||
  839                     TD_CAN_RUN(td) ||
  840                     TD_IS_RUNNING(td)) {
  841                         kp->ki_stat = SRUN;
  842                 } else if (P_SHOULDSTOP(p)) {
  843                         kp->ki_stat = SSTOP;
  844                 } else if (TD_IS_SLEEPING(td)) {
  845                         kp->ki_stat = SSLEEP;
  846                 } else if (TD_ON_LOCK(td)) {
  847                         kp->ki_stat = SLOCK;
  848                 } else {
  849                         kp->ki_stat = SWAIT;
  850                 }
  851         } else if (p->p_state == PRS_ZOMBIE) {
  852                 kp->ki_stat = SZOMB;
  853         } else {
  854                 kp->ki_stat = SIDL;
  855         }
  856 
  857         /* Things in the thread */
  858         kp->ki_wchan = td->td_wchan;
  859         kp->ki_pri.pri_level = td->td_priority;
  860         kp->ki_pri.pri_native = td->td_base_pri;
  861         kp->ki_lastcpu = td->td_lastcpu;
  862         kp->ki_oncpu = td->td_oncpu;
  863         kp->ki_tdflags = td->td_flags;
  864         kp->ki_tid = td->td_tid;
  865         kp->ki_numthreads = p->p_numthreads;
  866         kp->ki_pcb = td->td_pcb;
  867         kp->ki_kstack = (void *)td->td_kstack;
  868         kp->ki_pctcpu = sched_pctcpu(td);
  869         kp->ki_estcpu = td->td_estcpu;
  870         kp->ki_slptime = (ticks - td->td_slptick) / hz;
  871         kp->ki_pri.pri_class = td->td_pri_class;
  872         kp->ki_pri.pri_user = td->td_user_pri;
  873 
  874         if (preferthread)
  875                 kp->ki_runtime = cputick2usec(td->td_runtime);
  876 
  877         /* We can't get this anymore but ps etc never used it anyway. */
  878         kp->ki_rqindex = 0;
  879 
  880         SIGSETOR(kp->ki_siglist, td->td_siglist);
  881         kp->ki_sigmask = td->td_sigmask;
  882         thread_unlock(td);
  883 }
  884 
  885 /*
  886  * Fill in a kinfo_proc structure for the specified process.
  887  * Must be called with the target process locked.
  888  */
  889 void
  890 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
  891 {
  892 
  893         fill_kinfo_proc_only(p, kp);
  894         PROC_SLOCK(p);
  895         if (FIRST_THREAD_IN_PROC(p) != NULL)
  896                 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
  897         PROC_SUNLOCK(p);
  898 }
  899 
  900 struct pstats *
  901 pstats_alloc(void)
  902 {
  903 
  904         return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
  905 }
  906 
  907 /*
  908  * Copy parts of p_stats; zero the rest of p_stats (statistics).
  909  */
  910 void
  911 pstats_fork(struct pstats *src, struct pstats *dst)
  912 {
  913 
  914         bzero(&dst->pstat_startzero,
  915             __rangeof(struct pstats, pstat_startzero, pstat_endzero));
  916         bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
  917             __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
  918 }
  919 
  920 void
  921 pstats_free(struct pstats *ps)
  922 {
  923 
  924         free(ps, M_SUBPROC);
  925 }
  926 
  927 /*
  928  * Locate a zombie process by number
  929  */
  930 struct proc *
  931 zpfind(pid_t pid)
  932 {
  933         struct proc *p;
  934 
  935         sx_slock(&allproc_lock);
  936         LIST_FOREACH(p, &zombproc, p_list)
  937                 if (p->p_pid == pid) {
  938                         PROC_LOCK(p);
  939                         break;
  940                 }
  941         sx_sunlock(&allproc_lock);
  942         return (p);
  943 }
  944 
  945 #define KERN_PROC_ZOMBMASK      0x3
  946 #define KERN_PROC_NOTHREADS     0x4
  947 
  948 /*
  949  * Must be called with the process locked and will return with it unlocked.
  950  */
  951 static int
  952 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
  953 {
  954         struct thread *td;
  955         struct kinfo_proc kinfo_proc;
  956         int error = 0;
  957         struct proc *np;
  958         pid_t pid = p->p_pid;
  959 
  960         PROC_LOCK_ASSERT(p, MA_OWNED);
  961 
  962         fill_kinfo_proc_only(p, &kinfo_proc);
  963         if (flags & KERN_PROC_NOTHREADS) {
  964                 PROC_SLOCK(p);
  965                 if (FIRST_THREAD_IN_PROC(p) != NULL)
  966                         fill_kinfo_thread(FIRST_THREAD_IN_PROC(p),
  967                             &kinfo_proc, 0);
  968                 PROC_SUNLOCK(p);
  969                 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  970                                    sizeof(kinfo_proc));
  971         } else {
  972                 PROC_SLOCK(p);
  973                 if (FIRST_THREAD_IN_PROC(p) != NULL)
  974                         FOREACH_THREAD_IN_PROC(p, td) {
  975                                 fill_kinfo_thread(td, &kinfo_proc, 1);
  976                                 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  977                                                    sizeof(kinfo_proc));
  978                                 if (error)
  979                                         break;
  980                         }
  981                 else
  982                         error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  983                                            sizeof(kinfo_proc));
  984                 PROC_SUNLOCK(p);
  985         }
  986         PROC_UNLOCK(p);
  987         if (error)
  988                 return (error);
  989         if (flags & KERN_PROC_ZOMBMASK)
  990                 np = zpfind(pid);
  991         else {
  992                 if (pid == 0)
  993                         return (0);
  994                 np = pfind(pid);
  995         }
  996         if (np == NULL)
  997                 return (ESRCH);
  998         if (np != p) {
  999                 PROC_UNLOCK(np);
 1000                 return (ESRCH);
 1001         }
 1002         PROC_UNLOCK(np);
 1003         return (0);
 1004 }
 1005 
 1006 static int
 1007 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
 1008 {
 1009         int *name = (int*) arg1;
 1010         u_int namelen = arg2;
 1011         struct proc *p;
 1012         int flags, doingzomb, oid_number;
 1013         int error = 0;
 1014 
 1015         oid_number = oidp->oid_number;
 1016         if (oid_number != KERN_PROC_ALL &&
 1017             (oid_number & KERN_PROC_INC_THREAD) == 0)
 1018                 flags = KERN_PROC_NOTHREADS;
 1019         else {
 1020                 flags = 0;
 1021                 oid_number &= ~KERN_PROC_INC_THREAD;
 1022         }
 1023         if (oid_number == KERN_PROC_PID) {
 1024                 if (namelen != 1) 
 1025                         return (EINVAL);
 1026                 error = sysctl_wire_old_buffer(req, 0);
 1027                 if (error)
 1028                         return (error);         
 1029                 p = pfind((pid_t)name[0]);
 1030                 if (!p)
 1031                         return (ESRCH);
 1032                 if ((error = p_cansee(curthread, p))) {
 1033                         PROC_UNLOCK(p);
 1034                         return (error);
 1035                 }
 1036                 error = sysctl_out_proc(p, req, flags);
 1037                 return (error);
 1038         }
 1039 
 1040         switch (oid_number) {
 1041         case KERN_PROC_ALL:
 1042                 if (namelen != 0)
 1043                         return (EINVAL);
 1044                 break;
 1045         case KERN_PROC_PROC:
 1046                 if (namelen != 0 && namelen != 1)
 1047                         return (EINVAL);
 1048                 break;
 1049         default:
 1050                 if (namelen != 1)
 1051                         return (EINVAL);
 1052                 break;
 1053         }
 1054         
 1055         if (!req->oldptr) {
 1056                 /* overestimate by 5 procs */
 1057                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
 1058                 if (error)
 1059                         return (error);
 1060         }
 1061         error = sysctl_wire_old_buffer(req, 0);
 1062         if (error != 0)
 1063                 return (error);
 1064         sx_slock(&allproc_lock);
 1065         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
 1066                 if (!doingzomb)
 1067                         p = LIST_FIRST(&allproc);
 1068                 else
 1069                         p = LIST_FIRST(&zombproc);
 1070                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
 1071                         /*
 1072                          * Skip embryonic processes.
 1073                          */
 1074                         PROC_SLOCK(p);
 1075                         if (p->p_state == PRS_NEW) {
 1076                                 PROC_SUNLOCK(p);
 1077                                 continue;
 1078                         }
 1079                         PROC_SUNLOCK(p);
 1080                         PROC_LOCK(p);
 1081                         KASSERT(p->p_ucred != NULL,
 1082                             ("process credential is NULL for non-NEW proc"));
 1083                         /*
 1084                          * Show a user only appropriate processes.
 1085                          */
 1086                         if (p_cansee(curthread, p)) {
 1087                                 PROC_UNLOCK(p);
 1088                                 continue;
 1089                         }
 1090                         /*
 1091                          * TODO - make more efficient (see notes below).
 1092                          * do by session.
 1093                          */
 1094                         switch (oid_number) {
 1095 
 1096                         case KERN_PROC_GID:
 1097                                 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
 1098                                         PROC_UNLOCK(p);
 1099                                         continue;
 1100                                 }
 1101                                 break;
 1102 
 1103                         case KERN_PROC_PGRP:
 1104                                 /* could do this by traversing pgrp */
 1105                                 if (p->p_pgrp == NULL ||
 1106                                     p->p_pgrp->pg_id != (pid_t)name[0]) {
 1107                                         PROC_UNLOCK(p);
 1108                                         continue;
 1109                                 }
 1110                                 break;
 1111 
 1112                         case KERN_PROC_RGID:
 1113                                 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
 1114                                         PROC_UNLOCK(p);
 1115                                         continue;
 1116                                 }
 1117                                 break;
 1118 
 1119                         case KERN_PROC_SESSION:
 1120                                 if (p->p_session == NULL ||
 1121                                     p->p_session->s_sid != (pid_t)name[0]) {
 1122                                         PROC_UNLOCK(p);
 1123                                         continue;
 1124                                 }
 1125                                 break;
 1126 
 1127                         case KERN_PROC_TTY:
 1128                                 if ((p->p_flag & P_CONTROLT) == 0 ||
 1129                                     p->p_session == NULL) {
 1130                                         PROC_UNLOCK(p);
 1131                                         continue;
 1132                                 }
 1133                                 SESS_LOCK(p->p_session);
 1134                                 if (p->p_session->s_ttyp == NULL ||
 1135                                     dev2udev(p->p_session->s_ttyp->t_dev) != 
 1136                                     (dev_t)name[0]) {
 1137                                         SESS_UNLOCK(p->p_session);
 1138                                         PROC_UNLOCK(p);
 1139                                         continue;
 1140                                 }
 1141                                 SESS_UNLOCK(p->p_session);
 1142                                 break;
 1143 
 1144                         case KERN_PROC_UID:
 1145                                 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
 1146                                         PROC_UNLOCK(p);
 1147                                         continue;
 1148                                 }
 1149                                 break;
 1150 
 1151                         case KERN_PROC_RUID:
 1152                                 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
 1153                                         PROC_UNLOCK(p);
 1154                                         continue;
 1155                                 }
 1156                                 break;
 1157 
 1158                         case KERN_PROC_PROC:
 1159                                 break;
 1160 
 1161                         default:
 1162                                 break;
 1163 
 1164                         }
 1165 
 1166                         error = sysctl_out_proc(p, req, flags | doingzomb);
 1167                         if (error) {
 1168                                 sx_sunlock(&allproc_lock);
 1169                                 return (error);
 1170                         }
 1171                 }
 1172         }
 1173         sx_sunlock(&allproc_lock);
 1174         return (0);
 1175 }
 1176 
 1177 struct pargs *
 1178 pargs_alloc(int len)
 1179 {
 1180         struct pargs *pa;
 1181 
 1182         MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
 1183                 M_WAITOK);
 1184         refcount_init(&pa->ar_ref, 1);
 1185         pa->ar_length = len;
 1186         return (pa);
 1187 }
 1188 
 1189 static void
 1190 pargs_free(struct pargs *pa)
 1191 {
 1192 
 1193         FREE(pa, M_PARGS);
 1194 }
 1195 
 1196 void
 1197 pargs_hold(struct pargs *pa)
 1198 {
 1199 
 1200         if (pa == NULL)
 1201                 return;
 1202         refcount_acquire(&pa->ar_ref);
 1203 }
 1204 
 1205 void
 1206 pargs_drop(struct pargs *pa)
 1207 {
 1208 
 1209         if (pa == NULL)
 1210                 return;
 1211         if (refcount_release(&pa->ar_ref))
 1212                 pargs_free(pa);
 1213 }
 1214 
 1215 /*
 1216  * This sysctl allows a process to retrieve the argument list or process
 1217  * title for another process without groping around in the address space
 1218  * of the other process.  It also allow a process to set its own "process 
 1219  * title to a string of its own choice.
 1220  */
 1221 static int
 1222 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 1223 {
 1224         int *name = (int*) arg1;
 1225         u_int namelen = arg2;
 1226         struct pargs *newpa, *pa;
 1227         struct proc *p;
 1228         int error = 0;
 1229 
 1230         if (namelen != 1) 
 1231                 return (EINVAL);
 1232 
 1233         p = pfind((pid_t)name[0]);
 1234         if (!p)
 1235                 return (ESRCH);
 1236 
 1237         if ((error = p_cansee(curthread, p)) != 0) {
 1238                 PROC_UNLOCK(p);
 1239                 return (error);
 1240         }
 1241 
 1242         if (req->newptr && curproc != p) {
 1243                 PROC_UNLOCK(p);
 1244                 return (EPERM);
 1245         }
 1246 
 1247         pa = p->p_args;
 1248         pargs_hold(pa);
 1249         PROC_UNLOCK(p);
 1250         if (req->oldptr != NULL && pa != NULL)
 1251                 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
 1252         pargs_drop(pa);
 1253         if (error != 0 || req->newptr == NULL)
 1254                 return (error);
 1255 
 1256         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
 1257                 return (ENOMEM);
 1258         newpa = pargs_alloc(req->newlen);
 1259         error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
 1260         if (error != 0) {
 1261                 pargs_free(newpa);
 1262                 return (error);
 1263         }
 1264         PROC_LOCK(p);
 1265         pa = p->p_args;
 1266         p->p_args = newpa;
 1267         PROC_UNLOCK(p);
 1268         pargs_drop(pa);
 1269         return (0);
 1270 }
 1271 
 1272 /*
 1273  * This sysctl allows a process to retrieve the path of the executable for
 1274  * itself or another process.
 1275  */
 1276 static int
 1277 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
 1278 {
 1279         pid_t *pidp = (pid_t *)arg1;
 1280         unsigned int arglen = arg2;
 1281         struct proc *p;
 1282         struct vnode *vp;
 1283         char *retbuf, *freebuf;
 1284         int error, vfslocked;
 1285 
 1286         if (arglen != 1)
 1287                 return (EINVAL);
 1288         if (*pidp == -1) {      /* -1 means this process */
 1289                 p = req->td->td_proc;
 1290         } else {
 1291                 p = pfind(*pidp);
 1292                 if (p == NULL)
 1293                         return (ESRCH);
 1294                 if ((error = p_cansee(curthread, p)) != 0) {
 1295                         PROC_UNLOCK(p);
 1296                         return (error);
 1297                 }
 1298         }
 1299 
 1300         vp = p->p_textvp;
 1301         if (vp == NULL) {
 1302                 if (*pidp != -1)
 1303                         PROC_UNLOCK(p);
 1304                 return (0);
 1305         }
 1306         vref(vp);
 1307         if (*pidp != -1)
 1308                 PROC_UNLOCK(p);
 1309         error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
 1310         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1311         vrele(vp);
 1312         VFS_UNLOCK_GIANT(vfslocked);
 1313         if (error)
 1314                 return (error);
 1315         error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
 1316         free(freebuf, M_TEMP);
 1317         return (error);
 1318 }
 1319 
 1320 static int
 1321 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
 1322 {
 1323         struct proc *p;
 1324         char *sv_name;
 1325         int *name;
 1326         int namelen;
 1327         int error;
 1328 
 1329         namelen = arg2;
 1330         if (namelen != 1) 
 1331                 return (EINVAL);
 1332 
 1333         name = (int *)arg1;
 1334         if ((p = pfind((pid_t)name[0])) == NULL)
 1335                 return (ESRCH);
 1336         if ((error = p_cansee(curthread, p))) {
 1337                 PROC_UNLOCK(p);
 1338                 return (error);
 1339         }
 1340         sv_name = p->p_sysent->sv_name;
 1341         PROC_UNLOCK(p);
 1342         return (sysctl_handle_string(oidp, sv_name, 0, req));
 1343 }
 1344 
 1345 #ifdef KINFO_OVMENTRY_SIZE
 1346 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
 1347 #endif
 1348 
 1349 /* Compatability with early 7-stable */
 1350 static int
 1351 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
 1352 {
 1353         vm_map_entry_t entry, tmp_entry;
 1354         unsigned int last_timestamp;
 1355         char *fullpath, *freepath;
 1356         struct kinfo_ovmentry *kve;
 1357         struct vattr va;
 1358         struct ucred *cred;
 1359         int error, *name;
 1360         struct vnode *vp;
 1361         struct proc *p;
 1362         vm_map_t map;
 1363         struct vmspace *vm;
 1364 
 1365         name = (int *)arg1;
 1366         if ((p = pfind((pid_t)name[0])) == NULL)
 1367                 return (ESRCH);
 1368         if (p->p_flag & P_WEXIT) {
 1369                 PROC_UNLOCK(p);
 1370                 return (ESRCH);
 1371         }
 1372         if ((error = p_candebug(curthread, p))) {
 1373                 PROC_UNLOCK(p);
 1374                 return (error);
 1375         }
 1376         _PHOLD(p);
 1377         PROC_UNLOCK(p);
 1378         vm = vmspace_acquire_ref(p);
 1379         if (vm == NULL) {
 1380                 PRELE(p);
 1381                 return (ESRCH);
 1382         }
 1383         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
 1384 
 1385         map = &p->p_vmspace->vm_map;    /* XXXRW: More locking required? */
 1386         vm_map_lock_read(map);
 1387         for (entry = map->header.next; entry != &map->header;
 1388             entry = entry->next) {
 1389                 vm_object_t obj, tobj, lobj;
 1390                 vm_offset_t addr;
 1391                 int vfslocked;
 1392 
 1393                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 1394                         continue;
 1395 
 1396                 bzero(kve, sizeof(*kve));
 1397                 kve->kve_structsize = sizeof(*kve);
 1398 
 1399                 kve->kve_private_resident = 0;
 1400                 obj = entry->object.vm_object;
 1401                 if (obj != NULL) {
 1402                         VM_OBJECT_LOCK(obj);
 1403                         if (obj->shadow_count == 1)
 1404                                 kve->kve_private_resident =
 1405                                     obj->resident_page_count;
 1406                 }
 1407                 kve->kve_resident = 0;
 1408                 addr = entry->start;
 1409                 while (addr < entry->end) {
 1410                         if (pmap_extract(map->pmap, addr))
 1411                                 kve->kve_resident++;
 1412                         addr += PAGE_SIZE;
 1413                 }
 1414 
 1415                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
 1416                         if (tobj != obj)
 1417                                 VM_OBJECT_LOCK(tobj);
 1418                         if (lobj != obj)
 1419                                 VM_OBJECT_UNLOCK(lobj);
 1420                         lobj = tobj;
 1421                 }
 1422 
 1423                 kve->kve_start = (void*)entry->start;
 1424                 kve->kve_end = (void*)entry->end;
 1425                 kve->kve_offset = (off_t)entry->offset;
 1426 
 1427                 if (entry->protection & VM_PROT_READ)
 1428                         kve->kve_protection |= KVME_PROT_READ;
 1429                 if (entry->protection & VM_PROT_WRITE)
 1430                         kve->kve_protection |= KVME_PROT_WRITE;
 1431                 if (entry->protection & VM_PROT_EXECUTE)
 1432                         kve->kve_protection |= KVME_PROT_EXEC;
 1433 
 1434                 if (entry->eflags & MAP_ENTRY_COW)
 1435                         kve->kve_flags |= KVME_FLAG_COW;
 1436                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
 1437                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
 1438 
 1439                 last_timestamp = map->timestamp;
 1440                 vm_map_unlock_read(map);
 1441 
 1442                 kve->kve_fileid = 0;
 1443                 kve->kve_fsid = 0;
 1444                 freepath = NULL;
 1445                 fullpath = "";
 1446                 if (lobj) {
 1447                         vp = NULL;
 1448                         switch (lobj->type) {
 1449                         case OBJT_DEFAULT:
 1450                                 kve->kve_type = KVME_TYPE_DEFAULT;
 1451                                 break;
 1452                         case OBJT_VNODE:
 1453                                 kve->kve_type = KVME_TYPE_VNODE;
 1454                                 vp = lobj->handle;
 1455                                 vref(vp);
 1456                                 break;
 1457                         case OBJT_SWAP:
 1458                                 kve->kve_type = KVME_TYPE_SWAP;
 1459                                 break;
 1460                         case OBJT_DEVICE:
 1461                                 kve->kve_type = KVME_TYPE_DEVICE;
 1462                                 break;
 1463                         case OBJT_PHYS:
 1464                                 kve->kve_type = KVME_TYPE_PHYS;
 1465                                 break;
 1466                         case OBJT_DEAD:
 1467                                 kve->kve_type = KVME_TYPE_DEAD;
 1468                                 break;
 1469                         default:
 1470                                 kve->kve_type = KVME_TYPE_UNKNOWN;
 1471                                 break;
 1472                         }
 1473                         if (lobj != obj)
 1474                                 VM_OBJECT_UNLOCK(lobj);
 1475 
 1476                         kve->kve_ref_count = obj->ref_count;
 1477                         kve->kve_shadow_count = obj->shadow_count;
 1478                         VM_OBJECT_UNLOCK(obj);
 1479                         if (vp != NULL) {
 1480                                 vn_fullpath(curthread, vp, &fullpath,
 1481                                     &freepath);
 1482                                 cred = curthread->td_ucred;
 1483                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1484                                 vn_lock(vp, LK_SHARED | LK_RETRY, curthread);
 1485                                 if (VOP_GETATTR(vp, &va, cred, curthread) == 0) {
 1486                                         kve->kve_fileid = va.va_fileid;
 1487                                         kve->kve_fsid = va.va_fsid;
 1488                                 }
 1489                                 vput(vp);
 1490                                 VFS_UNLOCK_GIANT(vfslocked);
 1491                         }
 1492                 } else {
 1493                         kve->kve_type = KVME_TYPE_NONE;
 1494                         kve->kve_ref_count = 0;
 1495                         kve->kve_shadow_count = 0;
 1496                 }
 1497 
 1498                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
 1499                 if (freepath != NULL)
 1500                         free(freepath, M_TEMP);
 1501 
 1502                 error = SYSCTL_OUT(req, kve, sizeof(*kve));
 1503                 vm_map_lock_read(map);
 1504                 if (error)
 1505                         break;
 1506                 if (last_timestamp != map->timestamp) {
 1507                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
 1508                         entry = tmp_entry;
 1509                 }
 1510         }
 1511         vm_map_unlock_read(map);
 1512         vmspace_free(vm);
 1513         PRELE(p);
 1514         free(kve, M_TEMP);
 1515         return (error);
 1516 }
 1517 
 1518 #ifdef KINFO_VMENTRY_SIZE
 1519 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
 1520 #endif
 1521 
 1522 static int
 1523 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
 1524 {
 1525         vm_map_entry_t entry, tmp_entry;
 1526         unsigned int last_timestamp;
 1527         char *fullpath, *freepath;
 1528         struct kinfo_vmentry *kve;
 1529         struct vattr va;
 1530         struct ucred *cred;
 1531         int error, *name;
 1532         struct vnode *vp;
 1533         struct proc *p;
 1534         struct vmspace *vm;
 1535         vm_map_t map;
 1536 
 1537         name = (int *)arg1;
 1538         if ((p = pfind((pid_t)name[0])) == NULL)
 1539                 return (ESRCH);
 1540         if (p->p_flag & P_WEXIT) {
 1541                 PROC_UNLOCK(p);
 1542                 return (ESRCH);
 1543         }
 1544         if ((error = p_candebug(curthread, p))) {
 1545                 PROC_UNLOCK(p);
 1546                 return (error);
 1547         }
 1548         _PHOLD(p);
 1549         PROC_UNLOCK(p);
 1550         vm = vmspace_acquire_ref(p);
 1551         if (vm == NULL) {
 1552                 PRELE(p);
 1553                 return (ESRCH);
 1554         }
 1555         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
 1556 
 1557         map = &vm->vm_map;      /* XXXRW: More locking required? */
 1558         vm_map_lock_read(map);
 1559         for (entry = map->header.next; entry != &map->header;
 1560             entry = entry->next) {
 1561                 vm_object_t obj, tobj, lobj;
 1562                 vm_offset_t addr;
 1563                 int vfslocked;
 1564 
 1565                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 1566                         continue;
 1567 
 1568                 bzero(kve, sizeof(*kve));
 1569 
 1570                 kve->kve_private_resident = 0;
 1571                 obj = entry->object.vm_object;
 1572                 if (obj != NULL) {
 1573                         VM_OBJECT_LOCK(obj);
 1574                         if (obj->shadow_count == 1)
 1575                                 kve->kve_private_resident =
 1576                                     obj->resident_page_count;
 1577                 }
 1578                 kve->kve_resident = 0;
 1579                 addr = entry->start;
 1580                 while (addr < entry->end) {
 1581                         if (pmap_extract(map->pmap, addr))
 1582                                 kve->kve_resident++;
 1583                         addr += PAGE_SIZE;
 1584                 }
 1585 
 1586                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
 1587                         if (tobj != obj)
 1588                                 VM_OBJECT_LOCK(tobj);
 1589                         if (lobj != obj)
 1590                                 VM_OBJECT_UNLOCK(lobj);
 1591                         lobj = tobj;
 1592                 }
 1593 
 1594                 kve->kve_start = entry->start;
 1595                 kve->kve_end = entry->end;
 1596                 kve->kve_offset = entry->offset;
 1597 
 1598                 if (entry->protection & VM_PROT_READ)
 1599                         kve->kve_protection |= KVME_PROT_READ;
 1600                 if (entry->protection & VM_PROT_WRITE)
 1601                         kve->kve_protection |= KVME_PROT_WRITE;
 1602                 if (entry->protection & VM_PROT_EXECUTE)
 1603                         kve->kve_protection |= KVME_PROT_EXEC;
 1604 
 1605                 if (entry->eflags & MAP_ENTRY_COW)
 1606                         kve->kve_flags |= KVME_FLAG_COW;
 1607                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
 1608                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
 1609 
 1610                 last_timestamp = map->timestamp;
 1611                 vm_map_unlock_read(map);
 1612 
 1613                 kve->kve_fileid = 0;
 1614                 kve->kve_fsid = 0;
 1615                 freepath = NULL;
 1616                 fullpath = "";
 1617                 if (lobj) {
 1618                         vp = NULL;
 1619                         switch (lobj->type) {
 1620                         case OBJT_DEFAULT:
 1621                                 kve->kve_type = KVME_TYPE_DEFAULT;
 1622                                 break;
 1623                         case OBJT_VNODE:
 1624                                 kve->kve_type = KVME_TYPE_VNODE;
 1625                                 vp = lobj->handle;
 1626                                 vref(vp);
 1627                                 break;
 1628                         case OBJT_SWAP:
 1629                                 kve->kve_type = KVME_TYPE_SWAP;
 1630                                 break;
 1631                         case OBJT_DEVICE:
 1632                                 kve->kve_type = KVME_TYPE_DEVICE;
 1633                                 break;
 1634                         case OBJT_PHYS:
 1635                                 kve->kve_type = KVME_TYPE_PHYS;
 1636                                 break;
 1637                         case OBJT_DEAD:
 1638                                 kve->kve_type = KVME_TYPE_DEAD;
 1639                                 break;
 1640                         default:
 1641                                 kve->kve_type = KVME_TYPE_UNKNOWN;
 1642                                 break;
 1643                         }
 1644                         if (lobj != obj)
 1645                                 VM_OBJECT_UNLOCK(lobj);
 1646 
 1647                         kve->kve_ref_count = obj->ref_count;
 1648                         kve->kve_shadow_count = obj->shadow_count;
 1649                         VM_OBJECT_UNLOCK(obj);
 1650                         if (vp != NULL) {
 1651                                 vn_fullpath(curthread, vp, &fullpath,
 1652                                     &freepath);
 1653                                 cred = curthread->td_ucred;
 1654                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1655                                 vn_lock(vp, LK_SHARED | LK_RETRY, curthread);
 1656                                 if (VOP_GETATTR(vp, &va, cred, curthread) == 0) {
 1657                                         kve->kve_fileid = va.va_fileid;
 1658                                         kve->kve_fsid = va.va_fsid;
 1659                                 }
 1660                                 vput(vp);
 1661                                 VFS_UNLOCK_GIANT(vfslocked);
 1662                         }
 1663                 } else {
 1664                         kve->kve_type = KVME_TYPE_NONE;
 1665                         kve->kve_ref_count = 0;
 1666                         kve->kve_shadow_count = 0;
 1667                 }
 1668 
 1669                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
 1670                 if (freepath != NULL)
 1671                         free(freepath, M_TEMP);
 1672 
 1673                 /* Pack record size down */
 1674                 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
 1675                     strlen(kve->kve_path) + 1;
 1676                 kve->kve_structsize = roundup(kve->kve_structsize,
 1677                     sizeof(uint64_t));
 1678                 error = SYSCTL_OUT(req, kve, kve->kve_structsize);
 1679                 vm_map_lock_read(map);
 1680                 if (error)
 1681                         break;
 1682                 if (last_timestamp != map->timestamp) {
 1683                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
 1684                         entry = tmp_entry;
 1685                 }
 1686         }
 1687         vm_map_unlock_read(map);
 1688         vmspace_free(vm);
 1689         PRELE(p);
 1690         free(kve, M_TEMP);
 1691         return (error);
 1692 }
 1693 
 1694 #if defined(STACK) || defined(DDB)
 1695 static int
 1696 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
 1697 {
 1698         struct kinfo_kstack *kkstp;
 1699         int error, i, *name, numthreads;
 1700         lwpid_t *lwpidarray;
 1701         struct thread *td;
 1702         struct stack *st;
 1703         struct sbuf sb;
 1704         struct proc *p;
 1705 
 1706         name = (int *)arg1;
 1707         if ((p = pfind((pid_t)name[0])) == NULL)
 1708                 return (ESRCH);
 1709         /* XXXRW: Not clear ESRCH is the right error during proc execve(). */
 1710         if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) {
 1711                 PROC_UNLOCK(p);
 1712                 return (ESRCH);
 1713         }
 1714         if ((error = p_candebug(curthread, p))) {
 1715                 PROC_UNLOCK(p);
 1716                 return (error);
 1717         }
 1718         _PHOLD(p);
 1719         PROC_UNLOCK(p);
 1720 
 1721         kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
 1722         st = stack_create();
 1723 
 1724         lwpidarray = NULL;
 1725         numthreads = 0;
 1726         PROC_SLOCK(p);
 1727 repeat:
 1728         if (numthreads < p->p_numthreads) {
 1729                 if (lwpidarray != NULL) {
 1730                         free(lwpidarray, M_TEMP);
 1731                         lwpidarray = NULL;
 1732                 }
 1733                 numthreads = p->p_numthreads;
 1734                 PROC_SUNLOCK(p);
 1735                 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
 1736                     M_WAITOK | M_ZERO);
 1737                 PROC_SLOCK(p);
 1738                 goto repeat;
 1739         }
 1740         PROC_SUNLOCK(p);
 1741         i = 0;
 1742 
 1743         /*
 1744          * XXXRW: During the below loop, execve(2) and countless other sorts
 1745          * of changes could have taken place.  Should we check to see if the
 1746          * vmspace has been replaced, or the like, in order to prevent
 1747          * giving a snapshot that spans, say, execve(2), with some threads
 1748          * before and some after?  Among other things, the credentials could
 1749          * have changed, in which case the right to extract debug info might
 1750          * no longer be assured.
 1751          */
 1752         PROC_LOCK(p);
 1753         FOREACH_THREAD_IN_PROC(p, td) {
 1754                 KASSERT(i < numthreads,
 1755                     ("sysctl_kern_proc_kstack: numthreads"));
 1756                 lwpidarray[i] = td->td_tid;
 1757                 i++;
 1758         }
 1759         numthreads = i;
 1760         for (i = 0; i < numthreads; i++) {
 1761                 td = thread_find(p, lwpidarray[i]);
 1762                 if (td == NULL) {
 1763                         continue;
 1764                 }
 1765                 bzero(kkstp, sizeof(*kkstp));
 1766                 (void)sbuf_new(&sb, kkstp->kkst_trace,
 1767                     sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
 1768                 thread_lock(td);
 1769                 kkstp->kkst_tid = td->td_tid;
 1770                 if (TD_IS_SWAPPED(td))
 1771                         kkstp->kkst_state = KKST_STATE_SWAPPED;
 1772                 else if (TD_IS_RUNNING(td))
 1773                         kkstp->kkst_state = KKST_STATE_RUNNING;
 1774                 else {
 1775                         kkstp->kkst_state = KKST_STATE_STACKOK;
 1776                         stack_save_td(st, td);
 1777                 }
 1778                 thread_unlock(td);
 1779                 PROC_UNLOCK(p);
 1780                 stack_sbuf_print(&sb, st);
 1781                 sbuf_finish(&sb);
 1782                 sbuf_delete(&sb);
 1783                 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
 1784                 PROC_LOCK(p);
 1785                 if (error)
 1786                         break;
 1787         }
 1788         _PRELE(p);
 1789         PROC_UNLOCK(p);
 1790         if (lwpidarray != NULL)
 1791                 free(lwpidarray, M_TEMP);
 1792         stack_destroy(st);
 1793         free(kkstp, M_TEMP);
 1794         return (error);
 1795 }
 1796 #endif
 1797 
 1798 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
 1799 
 1800 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
 1801         CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
 1802         "Return entire process table");
 1803 
 1804 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1805         sysctl_kern_proc, "Process table");
 1806 
 1807 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1808         sysctl_kern_proc, "Process table");
 1809 
 1810 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1811         sysctl_kern_proc, "Process table");
 1812 
 1813 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
 1814         CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1815 
 1816 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 
 1817         sysctl_kern_proc, "Process table");
 1818 
 1819 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 
 1820         sysctl_kern_proc, "Process table");
 1821 
 1822 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1823         sysctl_kern_proc, "Process table");
 1824 
 1825 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1826         sysctl_kern_proc, "Process table");
 1827 
 1828 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
 1829         sysctl_kern_proc, "Return process table, no threads");
 1830 
 1831 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
 1832         CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
 1833         sysctl_kern_proc_args, "Process argument list");
 1834 
 1835 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
 1836         CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
 1837 
 1838 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
 1839         CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
 1840         "Process syscall vector name (ABI type)");
 1841 
 1842 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
 1843         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1844 
 1845 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
 1846         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1847 
 1848 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
 1849         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1850 
 1851 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
 1852         sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1853 
 1854 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
 1855         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1856 
 1857 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
 1858         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1859 
 1860 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
 1861         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1862 
 1863 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
 1864         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 1865 
 1866 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
 1867         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
 1868         "Return process table, no threads");
 1869 
 1870 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
 1871         CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
 1872 
 1873 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
 1874         CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
 1875 
 1876 #if defined(STACK) || defined(DDB)
 1877 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
 1878         CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
 1879 #endif

Cache object: 2dc6eaac918b85ef6fa16824659adda0


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