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

Cache object: c28438cf634684bcc624c7b17f505ce0


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