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


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

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

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

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

Cache object: a4094587d664f6e4914164337b2fc09e


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