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  * $FreeBSD$
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD$");
   35 
   36 #include "opt_ktrace.h"
   37 #include "opt_kstack_pages.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/kernel.h>
   42 #include <sys/lock.h>
   43 #include <sys/malloc.h>
   44 #include <sys/mutex.h>
   45 #include <sys/proc.h>
   46 #include <sys/sysent.h>
   47 #include <sys/sched.h>
   48 #include <sys/smp.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/filedesc.h>
   51 #include <sys/tty.h>
   52 #include <sys/signalvar.h>
   53 #include <sys/sx.h>
   54 #include <sys/user.h>
   55 #include <sys/jail.h>
   56 #include <sys/vnode.h>
   57 #ifdef KTRACE
   58 #include <sys/uio.h>
   59 #include <sys/ktrace.h>
   60 #endif
   61 
   62 #include <vm/vm.h>
   63 #include <vm/vm_extern.h>
   64 #include <vm/pmap.h>
   65 #include <vm/vm_map.h>
   66 #include <vm/uma.h>
   67 
   68 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
   69 MALLOC_DEFINE(M_SESSION, "session", "session header");
   70 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
   71 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
   72 
   73 static void doenterpgrp(struct proc *, struct pgrp *);
   74 static void orphanpg(struct pgrp *pg);
   75 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
   76 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
   77 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp);
   78 static void pgadjustjobc(struct pgrp *pgrp, int entering);
   79 static void pgdelete(struct pgrp *);
   80 static int proc_ctor(void *mem, int size, void *arg, int flags);
   81 static void proc_dtor(void *mem, int size, void *arg);
   82 static int proc_init(void *mem, int size, int flags);
   83 static void proc_fini(void *mem, int size);
   84 
   85 /*
   86  * Other process lists
   87  */
   88 struct pidhashhead *pidhashtbl;
   89 u_long pidhash;
   90 struct pgrphashhead *pgrphashtbl;
   91 u_long pgrphash;
   92 struct proclist allproc;
   93 struct proclist zombproc;
   94 struct sx allproc_lock;
   95 struct sx proctree_lock;
   96 struct mtx pargs_ref_lock;
   97 struct mtx ppeers_lock;
   98 uma_zone_t proc_zone;
   99 
  100 int kstack_pages = KSTACK_PAGES;
  101 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
  102 
  103 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
  104 
  105 /*
  106  * Initialize global process hashing structures.
  107  */
  108 void
  109 procinit()
  110 {
  111 
  112         sx_init(&allproc_lock, "allproc");
  113         sx_init(&proctree_lock, "proctree");
  114         mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF);
  115         mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
  116         LIST_INIT(&allproc);
  117         LIST_INIT(&zombproc);
  118         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
  119         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
  120         proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
  121             proc_ctor, proc_dtor, proc_init, proc_fini,
  122             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  123         uihashinit();
  124 }
  125 
  126 /*
  127  * Prepare a proc for use.
  128  */
  129 static int
  130 proc_ctor(void *mem, int size, void *arg, int flags)
  131 {
  132         struct proc *p;
  133 
  134         p = (struct proc *)mem;
  135         return (0);
  136 }
  137 
  138 /*
  139  * Reclaim a proc after use.
  140  */
  141 static void
  142 proc_dtor(void *mem, int size, void *arg)
  143 {
  144         struct proc *p;
  145         struct thread *td;
  146 #ifdef INVARIANTS
  147         struct ksegrp *kg;
  148 #endif
  149 
  150         /* INVARIANTS checks go here */
  151         p = (struct proc *)mem;
  152         td = FIRST_THREAD_IN_PROC(p);
  153 #ifdef INVARIANTS
  154         KASSERT((p->p_numthreads == 1),
  155             ("bad number of threads in exiting process"));
  156         KASSERT((p->p_numksegrps == 1), ("free proc with > 1 ksegrp"));
  157         KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
  158         kg = FIRST_KSEGRP_IN_PROC(p);
  159         KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
  160         KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
  161 #endif
  162 
  163         /* Dispose of an alternate kstack, if it exists.
  164          * XXX What if there are more than one thread in the proc?
  165          *     The first thread in the proc is special and not
  166          *     freed, so you gotta do this here.
  167          */
  168         if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
  169                 vm_thread_dispose_altkstack(td);
  170 }
  171 
  172 /*
  173  * Initialize type-stable parts of a proc (when newly created).
  174  */
  175 static int
  176 proc_init(void *mem, int size, int flags)
  177 {
  178         struct proc *p;
  179         struct thread *td;
  180         struct ksegrp *kg;
  181 
  182         p = (struct proc *)mem;
  183         p->p_sched = (struct p_sched *)&p[1];
  184         td = thread_alloc();
  185         kg = ksegrp_alloc();
  186         bzero(&p->p_mtx, sizeof(struct mtx));
  187         mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
  188         p->p_stats = pstats_alloc();
  189         proc_linkup(p, kg, td);
  190         sched_newproc(p, kg, td);
  191         return (0);
  192 }
  193 
  194 /*
  195  * UMA should ensure that this function is never called.
  196  * Freeing a proc structure would violate type stability.
  197  */
  198 static void
  199 proc_fini(void *mem, int size)
  200 {
  201 
  202         panic("proc reclaimed");
  203 }
  204 
  205 /*
  206  * Is p an inferior of the current process?
  207  */
  208 int
  209 inferior(p)
  210         register struct proc *p;
  211 {
  212 
  213         sx_assert(&proctree_lock, SX_LOCKED);
  214         for (; p != curproc; p = p->p_pptr)
  215                 if (p->p_pid == 0)
  216                         return (0);
  217         return (1);
  218 }
  219 
  220 /*
  221  * Locate a process by number; return only "live" processes -- i.e., neither
  222  * zombies nor newly born but incompletely initialized processes.  By not
  223  * returning processes in the PRS_NEW state, we allow callers to avoid
  224  * testing for that condition to avoid dereferencing p_ucred, et al.
  225  */
  226 struct proc *
  227 pfind(pid)
  228         register pid_t pid;
  229 {
  230         register struct proc *p;
  231 
  232         sx_slock(&allproc_lock);
  233         LIST_FOREACH(p, PIDHASH(pid), p_hash)
  234                 if (p->p_pid == pid) {
  235                         if (p->p_state == PRS_NEW) {
  236                                 p = NULL;
  237                                 break;
  238                         }
  239                         PROC_LOCK(p);
  240                         break;
  241                 }
  242         sx_sunlock(&allproc_lock);
  243         return (p);
  244 }
  245 
  246 /*
  247  * Locate a process group by number.
  248  * The caller must hold proctree_lock.
  249  */
  250 struct pgrp *
  251 pgfind(pgid)
  252         register pid_t pgid;
  253 {
  254         register struct pgrp *pgrp;
  255 
  256         sx_assert(&proctree_lock, SX_LOCKED);
  257 
  258         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
  259                 if (pgrp->pg_id == pgid) {
  260                         PGRP_LOCK(pgrp);
  261                         return (pgrp);
  262                 }
  263         }
  264         return (NULL);
  265 }
  266 
  267 /*
  268  * Create a new process group.
  269  * pgid must be equal to the pid of p.
  270  * Begin a new session if required.
  271  */
  272 int
  273 enterpgrp(p, pgid, pgrp, sess)
  274         register struct proc *p;
  275         pid_t pgid;
  276         struct pgrp *pgrp;
  277         struct session *sess;
  278 {
  279         struct pgrp *pgrp2;
  280 
  281         sx_assert(&proctree_lock, SX_XLOCKED);
  282 
  283         KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
  284         KASSERT(p->p_pid == pgid,
  285             ("enterpgrp: new pgrp and pid != pgid"));
  286 
  287         pgrp2 = pgfind(pgid);
  288 
  289         KASSERT(pgrp2 == NULL,
  290             ("enterpgrp: pgrp with pgid exists"));
  291         KASSERT(!SESS_LEADER(p),
  292             ("enterpgrp: session leader attempted setpgrp"));
  293 
  294         mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
  295 
  296         if (sess != NULL) {
  297                 /*
  298                  * new session
  299                  */
  300                 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
  301                 mtx_lock(&Giant);       /* XXX TTY */
  302                 PROC_LOCK(p);
  303                 p->p_flag &= ~P_CONTROLT;
  304                 PROC_UNLOCK(p);
  305                 PGRP_LOCK(pgrp);
  306                 sess->s_leader = p;
  307                 sess->s_sid = p->p_pid;
  308                 sess->s_count = 1;
  309                 sess->s_ttyvp = NULL;
  310                 sess->s_ttyp = NULL;
  311                 bcopy(p->p_session->s_login, sess->s_login,
  312                             sizeof(sess->s_login));
  313                 pgrp->pg_session = sess;
  314                 KASSERT(p == curproc,
  315                     ("enterpgrp: mksession and p != curproc"));
  316         } else {
  317                 mtx_lock(&Giant);       /* XXX TTY */
  318                 pgrp->pg_session = p->p_session;
  319                 SESS_LOCK(pgrp->pg_session);
  320                 pgrp->pg_session->s_count++;
  321                 SESS_UNLOCK(pgrp->pg_session);
  322                 PGRP_LOCK(pgrp);
  323         }
  324         pgrp->pg_id = pgid;
  325         LIST_INIT(&pgrp->pg_members);
  326 
  327         /*
  328          * As we have an exclusive lock of proctree_lock,
  329          * this should not deadlock.
  330          */
  331         LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
  332         pgrp->pg_jobc = 0;
  333         SLIST_INIT(&pgrp->pg_sigiolst);
  334         PGRP_UNLOCK(pgrp);
  335         mtx_unlock(&Giant);       /* XXX TTY */
  336 
  337         doenterpgrp(p, pgrp);
  338 
  339         return (0);
  340 }
  341 
  342 /*
  343  * Move p to an existing process group
  344  */
  345 int
  346 enterthispgrp(p, pgrp)
  347         register struct proc *p;
  348         struct pgrp *pgrp;
  349 {
  350 
  351         sx_assert(&proctree_lock, SX_XLOCKED);
  352         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  353         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  354         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
  355         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
  356         KASSERT(pgrp->pg_session == p->p_session,
  357                 ("%s: pgrp's session %p, p->p_session %p.\n",
  358                 __func__,
  359                 pgrp->pg_session,
  360                 p->p_session));
  361         KASSERT(pgrp != p->p_pgrp,
  362                 ("%s: p belongs to pgrp.", __func__));
  363 
  364         doenterpgrp(p, pgrp);
  365 
  366         return (0);
  367 }
  368 
  369 /*
  370  * Move p to a process group
  371  */
  372 static void
  373 doenterpgrp(p, pgrp)
  374         struct proc *p;
  375         struct pgrp *pgrp;
  376 {
  377         struct pgrp *savepgrp;
  378 
  379         sx_assert(&proctree_lock, SX_XLOCKED);
  380         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  381         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  382         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
  383         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
  384 
  385         savepgrp = p->p_pgrp;
  386 
  387         /*
  388          * Adjust eligibility of affected pgrps to participate in job control.
  389          * Increment eligibility counts before decrementing, otherwise we
  390          * could reach 0 spuriously during the first call.
  391          */
  392         fixjobc(p, pgrp, 1);
  393         fixjobc(p, p->p_pgrp, 0);
  394 
  395         mtx_lock(&Giant);       /* XXX TTY */
  396         PGRP_LOCK(pgrp);
  397         PGRP_LOCK(savepgrp);
  398         PROC_LOCK(p);
  399         LIST_REMOVE(p, p_pglist);
  400         p->p_pgrp = pgrp;
  401         PROC_UNLOCK(p);
  402         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
  403         PGRP_UNLOCK(savepgrp);
  404         PGRP_UNLOCK(pgrp);
  405         mtx_unlock(&Giant);     /* XXX TTY */
  406         if (LIST_EMPTY(&savepgrp->pg_members))
  407                 pgdelete(savepgrp);
  408 }
  409 
  410 /*
  411  * remove process from process group
  412  */
  413 int
  414 leavepgrp(p)
  415         register struct proc *p;
  416 {
  417         struct pgrp *savepgrp;
  418 
  419         sx_assert(&proctree_lock, SX_XLOCKED);
  420         savepgrp = p->p_pgrp;
  421         mtx_lock(&Giant);       /* XXX TTY */
  422         PGRP_LOCK(savepgrp);
  423         PROC_LOCK(p);
  424         LIST_REMOVE(p, p_pglist);
  425         p->p_pgrp = NULL;
  426         PROC_UNLOCK(p);
  427         PGRP_UNLOCK(savepgrp);
  428         mtx_unlock(&Giant);     /* XXX TTY */
  429         if (LIST_EMPTY(&savepgrp->pg_members))
  430                 pgdelete(savepgrp);
  431         return (0);
  432 }
  433 
  434 /*
  435  * delete a process group
  436  */
  437 static void
  438 pgdelete(pgrp)
  439         register struct pgrp *pgrp;
  440 {
  441         struct session *savesess;
  442 
  443         sx_assert(&proctree_lock, SX_XLOCKED);
  444         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  445         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  446 
  447         /*
  448          * Reset any sigio structures pointing to us as a result of
  449          * F_SETOWN with our pgid.
  450          */
  451         funsetownlst(&pgrp->pg_sigiolst);
  452 
  453         mtx_lock(&Giant);       /* XXX TTY */
  454         PGRP_LOCK(pgrp);
  455         if (pgrp->pg_session->s_ttyp != NULL &&
  456             pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
  457                 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
  458         LIST_REMOVE(pgrp, pg_hash);
  459         savesess = pgrp->pg_session;
  460         SESSRELE(savesess);
  461         PGRP_UNLOCK(pgrp);
  462         mtx_destroy(&pgrp->pg_mtx);
  463         FREE(pgrp, M_PGRP);
  464         mtx_unlock(&Giant);     /* XXX TTY */
  465 }
  466 
  467 static void
  468 pgadjustjobc(pgrp, entering)
  469         struct pgrp *pgrp;
  470         int entering;
  471 {
  472 
  473         PGRP_LOCK(pgrp);
  474         if (entering)
  475                 pgrp->pg_jobc++;
  476         else {
  477                 --pgrp->pg_jobc;
  478                 if (pgrp->pg_jobc == 0)
  479                         orphanpg(pgrp);
  480         }
  481         PGRP_UNLOCK(pgrp);
  482 }
  483 
  484 /*
  485  * Adjust pgrp jobc counters when specified process changes process group.
  486  * We count the number of processes in each process group that "qualify"
  487  * the group for terminal job control (those with a parent in a different
  488  * process group of the same session).  If that count reaches zero, the
  489  * process group becomes orphaned.  Check both the specified process'
  490  * process group and that of its children.
  491  * entering == 0 => p is leaving specified group.
  492  * entering == 1 => p is entering specified group.
  493  */
  494 void
  495 fixjobc(p, pgrp, entering)
  496         register struct proc *p;
  497         register struct pgrp *pgrp;
  498         int entering;
  499 {
  500         register struct pgrp *hispgrp;
  501         register struct session *mysession;
  502 
  503         sx_assert(&proctree_lock, SX_LOCKED);
  504         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  505         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  506         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  507 
  508         /*
  509          * Check p's parent to see whether p qualifies its own process
  510          * group; if so, adjust count for p's process group.
  511          */
  512         mysession = pgrp->pg_session;
  513         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
  514             hispgrp->pg_session == mysession)
  515                 pgadjustjobc(pgrp, entering);
  516 
  517         /*
  518          * Check this process' children to see whether they qualify
  519          * their process groups; if so, adjust counts for children's
  520          * process groups.
  521          */
  522         LIST_FOREACH(p, &p->p_children, p_sibling) {
  523                 hispgrp = p->p_pgrp;
  524                 if (hispgrp == pgrp ||
  525                     hispgrp->pg_session != mysession)
  526                         continue;
  527                 PROC_LOCK(p);
  528                 if (p->p_state == PRS_ZOMBIE) {
  529                         PROC_UNLOCK(p);
  530                         continue;
  531                 }
  532                 PROC_UNLOCK(p);
  533                 pgadjustjobc(hispgrp, entering);
  534         }
  535 }
  536 
  537 /*
  538  * A process group has become orphaned;
  539  * if there are any stopped processes in the group,
  540  * hang-up all process in that group.
  541  */
  542 static void
  543 orphanpg(pg)
  544         struct pgrp *pg;
  545 {
  546         register struct proc *p;
  547 
  548         PGRP_LOCK_ASSERT(pg, MA_OWNED);
  549 
  550         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  551                 PROC_LOCK(p);
  552                 if (P_SHOULDSTOP(p)) {
  553                         PROC_UNLOCK(p);
  554                         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  555                                 PROC_LOCK(p);
  556                                 psignal(p, SIGHUP);
  557                                 psignal(p, SIGCONT);
  558                                 PROC_UNLOCK(p);
  559                         }
  560                         return;
  561                 }
  562                 PROC_UNLOCK(p);
  563         }
  564 }
  565 
  566 void
  567 sessrele(struct session *s)
  568 {
  569         int i;
  570 
  571         SESS_LOCK(s);
  572         i = --s->s_count;
  573         SESS_UNLOCK(s);
  574         if (i == 0) {
  575                 if (s->s_ttyp != NULL)
  576                         ttyrel(s->s_ttyp);
  577                 mtx_destroy(&s->s_mtx);
  578                 FREE(s, M_SESSION);
  579         }
  580 }
  581 
  582 #include "opt_ddb.h"
  583 #ifdef DDB
  584 #include <ddb/ddb.h>
  585 
  586 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
  587 {
  588         register struct pgrp *pgrp;
  589         register struct proc *p;
  590         register int i;
  591 
  592         for (i = 0; i <= pgrphash; i++) {
  593                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
  594                         printf("\tindx %d\n", i);
  595                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
  596                                 printf(
  597                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
  598                                     (void *)pgrp, (long)pgrp->pg_id,
  599                                     (void *)pgrp->pg_session,
  600                                     pgrp->pg_session->s_count,
  601                                     (void *)LIST_FIRST(&pgrp->pg_members));
  602                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
  603                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
  604                                             (long)p->p_pid, (void *)p,
  605                                             (void *)p->p_pgrp);
  606                                 }
  607                         }
  608                 }
  609         }
  610 }
  611 #endif /* DDB */
  612 
  613 /*
  614  * Rework the kinfo_proc members which need to be aggregated in the
  615  * case of process-ware informations.
  616  * Must be called with sched_lock held.
  617  */
  618 static void
  619 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
  620 {
  621         struct thread *td;
  622 
  623         mtx_assert(&sched_lock, MA_OWNED);
  624 
  625         kp->ki_estcpu = 0;
  626         kp->ki_pctcpu = 0;
  627         FOREACH_THREAD_IN_PROC(p, td) {
  628                 kp->ki_pctcpu += sched_pctcpu(td);
  629                 kp->ki_estcpu += td->td_ksegrp->kg_estcpu;
  630         }
  631 }
  632 
  633 /*
  634  * Clear kinfo_proc and fill in any information that is common
  635  * to all threads in the process.
  636  * Must be called with the target process locked.
  637  */
  638 static void
  639 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
  640 {
  641         struct thread *td0;
  642         struct tty *tp;
  643         struct session *sp;
  644         struct timeval tv;
  645         struct ucred *cred;
  646         struct sigacts *ps;
  647 
  648         bzero(kp, sizeof(*kp));
  649 
  650         kp->ki_structsize = sizeof(*kp);
  651         kp->ki_paddr = p;
  652         PROC_LOCK_ASSERT(p, MA_OWNED);
  653         kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
  654         kp->ki_args = p->p_args;
  655         kp->ki_textvp = p->p_textvp;
  656 #ifdef KTRACE
  657         kp->ki_tracep = p->p_tracevp;
  658         mtx_lock(&ktrace_mtx);
  659         kp->ki_traceflag = p->p_traceflag;
  660         mtx_unlock(&ktrace_mtx);
  661 #endif
  662         kp->ki_fd = p->p_fd;
  663         kp->ki_vmspace = p->p_vmspace;
  664         kp->ki_flag = p->p_flag;
  665         cred = p->p_ucred;
  666         if (cred) {
  667                 kp->ki_uid = cred->cr_uid;
  668                 kp->ki_ruid = cred->cr_ruid;
  669                 kp->ki_svuid = cred->cr_svuid;
  670                 /* XXX bde doesn't like KI_NGROUPS */
  671                 kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
  672                 bcopy(cred->cr_groups, kp->ki_groups,
  673                     kp->ki_ngroups * sizeof(gid_t));
  674                 kp->ki_rgid = cred->cr_rgid;
  675                 kp->ki_svgid = cred->cr_svgid;
  676                 /* If jailed(cred), emulate the old P_JAILED flag. */
  677                 if (jailed(cred)) {
  678                         kp->ki_flag |= P_JAILED;
  679                         /* If inside a jail, use 0 as a jail ID. */
  680                         if (!jailed(curthread->td_ucred))
  681                                 kp->ki_jid = cred->cr_prison->pr_id;
  682                 }
  683         }
  684         ps = p->p_sigacts;
  685         if (ps) {
  686                 mtx_lock(&ps->ps_mtx);
  687                 kp->ki_sigignore = ps->ps_sigignore;
  688                 kp->ki_sigcatch = ps->ps_sigcatch;
  689                 mtx_unlock(&ps->ps_mtx);
  690         }
  691         mtx_lock_spin(&sched_lock);
  692         if (p->p_state != PRS_NEW &&
  693             p->p_state != PRS_ZOMBIE &&
  694             p->p_vmspace != NULL) {
  695                 struct vmspace *vm = p->p_vmspace;
  696 
  697                 kp->ki_size = vm->vm_map.size;
  698                 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
  699                 FOREACH_THREAD_IN_PROC(p, td0) {
  700                         if (!TD_IS_SWAPPED(td0))
  701                                 kp->ki_rssize += td0->td_kstack_pages;
  702                         if (td0->td_altkstack_obj != NULL)
  703                                 kp->ki_rssize += td0->td_altkstack_pages;
  704                 }
  705                 kp->ki_swrss = vm->vm_swrss;
  706                 kp->ki_tsize = vm->vm_tsize;
  707                 kp->ki_dsize = vm->vm_dsize;
  708                 kp->ki_ssize = vm->vm_ssize;
  709         } else if (p->p_state == PRS_ZOMBIE)
  710                 kp->ki_stat = SZOMB;
  711         kp->ki_sflag = p->p_sflag;
  712         kp->ki_swtime = p->p_swtime;
  713         kp->ki_pid = p->p_pid;
  714         kp->ki_nice = p->p_nice;
  715         bintime2timeval(&p->p_rux.rux_runtime, &tv);
  716         kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
  717         mtx_unlock_spin(&sched_lock);
  718         if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) {
  719                 kp->ki_start = p->p_stats->p_start;
  720                 timevaladd(&kp->ki_start, &boottime);
  721                 kp->ki_rusage = p->p_stats->p_ru;
  722                 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
  723                 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
  724 
  725                 /* Some callers want child-times in a single value */
  726                 kp->ki_childtime = kp->ki_childstime;
  727                 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
  728         }
  729         tp = NULL;
  730         if (p->p_pgrp) {
  731                 kp->ki_pgid = p->p_pgrp->pg_id;
  732                 kp->ki_jobc = p->p_pgrp->pg_jobc;
  733                 sp = p->p_pgrp->pg_session;
  734 
  735                 if (sp != NULL) {
  736                         kp->ki_sid = sp->s_sid;
  737                         SESS_LOCK(sp);
  738                         strlcpy(kp->ki_login, sp->s_login,
  739                             sizeof(kp->ki_login));
  740                         if (sp->s_ttyvp)
  741                                 kp->ki_kiflag |= KI_CTTY;
  742                         if (SESS_LEADER(p))
  743                                 kp->ki_kiflag |= KI_SLEADER;
  744                         tp = sp->s_ttyp;
  745                         SESS_UNLOCK(sp);
  746                 }
  747         }
  748         if ((p->p_flag & P_CONTROLT) && tp != NULL) {
  749                 kp->ki_tdev = dev2udev(tp->t_dev);
  750                 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
  751                 if (tp->t_session)
  752                         kp->ki_tsid = tp->t_session->s_sid;
  753         } else
  754                 kp->ki_tdev = NODEV;
  755         if (p->p_comm[0] != '\0') {
  756                 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
  757                 strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
  758         }
  759         if (p->p_sysent && p->p_sysent->sv_name != NULL &&
  760             p->p_sysent->sv_name[0] != '\0')
  761                 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
  762         kp->ki_siglist = p->p_siglist;
  763         kp->ki_xstat = p->p_xstat;
  764         kp->ki_acflag = p->p_acflag;
  765         kp->ki_lock = p->p_lock;
  766         if (p->p_pptr)
  767                 kp->ki_ppid = p->p_pptr->p_pid;
  768 }
  769 
  770 /*
  771  * Fill in information that is thread specific.
  772  * Must be called with sched_lock locked.
  773  */
  774 static void
  775 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp)
  776 {
  777         struct ksegrp *kg;
  778         struct proc *p;
  779 
  780         p = td->td_proc;
  781 
  782         if (td->td_wmesg != NULL)
  783                 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
  784         else
  785                 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
  786         if (TD_ON_LOCK(td)) {
  787                 kp->ki_kiflag |= KI_LOCKBLOCK;
  788                 strlcpy(kp->ki_lockname, td->td_lockname,
  789                     sizeof(kp->ki_lockname));
  790         } else {
  791                 kp->ki_kiflag &= ~KI_LOCKBLOCK;
  792                 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
  793         }
  794 
  795         if (p->p_state == PRS_NORMAL) { /*  XXXKSE very approximate */
  796                 if (TD_ON_RUNQ(td) ||
  797                     TD_CAN_RUN(td) ||
  798                     TD_IS_RUNNING(td)) {
  799                         kp->ki_stat = SRUN;
  800                 } else if (P_SHOULDSTOP(p)) {
  801                         kp->ki_stat = SSTOP;
  802                 } else if (TD_IS_SLEEPING(td)) {
  803                         kp->ki_stat = SSLEEP;
  804                 } else if (TD_ON_LOCK(td)) {
  805                         kp->ki_stat = SLOCK;
  806                 } else {
  807                         kp->ki_stat = SWAIT;
  808                 }
  809         } else if (p->p_state == PRS_ZOMBIE) {
  810                 kp->ki_stat = SZOMB;
  811         } else {
  812                 kp->ki_stat = SIDL;
  813         }
  814 
  815         kg = td->td_ksegrp;
  816 
  817         /* things in the KSE GROUP */
  818         kp->ki_estcpu = kg->kg_estcpu;
  819         kp->ki_slptime = kg->kg_slptime;
  820         kp->ki_pri.pri_user = kg->kg_user_pri;
  821         kp->ki_pri.pri_class = kg->kg_pri_class;
  822 
  823         /* Things in the thread */
  824         kp->ki_wchan = td->td_wchan;
  825         kp->ki_pri.pri_level = td->td_priority;
  826         kp->ki_pri.pri_native = td->td_base_pri;
  827         kp->ki_lastcpu = td->td_lastcpu;
  828         kp->ki_oncpu = td->td_oncpu;
  829         kp->ki_tdflags = td->td_flags;
  830         kp->ki_tid = td->td_tid;
  831         kp->ki_numthreads = p->p_numthreads;
  832         kp->ki_pcb = td->td_pcb;
  833         kp->ki_kstack = (void *)td->td_kstack;
  834         kp->ki_pctcpu = sched_pctcpu(td);
  835 
  836         /* We can't get this anymore but ps etc never used it anyway. */
  837         kp->ki_rqindex = 0;
  838 
  839         SIGSETOR(kp->ki_siglist, td->td_siglist);
  840         kp->ki_sigmask = td->td_sigmask;
  841 }
  842 
  843 /*
  844  * Fill in a kinfo_proc structure for the specified process.
  845  * Must be called with the target process locked.
  846  */
  847 void
  848 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
  849 {
  850 
  851         fill_kinfo_proc_only(p, kp);
  852         mtx_lock_spin(&sched_lock);
  853         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
  854         fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
  855         fill_kinfo_aggregate(p, kp);
  856         mtx_unlock_spin(&sched_lock);
  857 }
  858 
  859 struct pstats *
  860 pstats_alloc(void)
  861 {
  862 
  863         return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
  864 }
  865 
  866 /*
  867  * Copy parts of p_stats; zero the rest of p_stats (statistics).
  868  */
  869 void
  870 pstats_fork(struct pstats *src, struct pstats *dst)
  871 {
  872 
  873         bzero(&dst->pstat_startzero,
  874             __rangeof(struct pstats, pstat_startzero, pstat_endzero));
  875         bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
  876             __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
  877 }
  878 
  879 void
  880 pstats_free(struct pstats *ps)
  881 {
  882 
  883         free(ps, M_SUBPROC);
  884 }
  885 
  886 /*
  887  * Locate a zombie process by number
  888  */
  889 struct proc *
  890 zpfind(pid_t pid)
  891 {
  892         struct proc *p;
  893 
  894         sx_slock(&allproc_lock);
  895         LIST_FOREACH(p, &zombproc, p_list)
  896                 if (p->p_pid == pid) {
  897                         PROC_LOCK(p);
  898                         break;
  899                 }
  900         sx_sunlock(&allproc_lock);
  901         return (p);
  902 }
  903 
  904 #define KERN_PROC_ZOMBMASK      0x3
  905 #define KERN_PROC_NOTHREADS     0x4
  906 
  907 /*
  908  * Must be called with the process locked and will return with it unlocked.
  909  */
  910 static int
  911 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
  912 {
  913         struct thread *td;
  914         struct kinfo_proc kinfo_proc;
  915         int error = 0;
  916         struct proc *np;
  917         pid_t pid = p->p_pid;
  918 
  919         PROC_LOCK_ASSERT(p, MA_OWNED);
  920 
  921         fill_kinfo_proc(p, &kinfo_proc);
  922         if (flags & KERN_PROC_NOTHREADS)
  923                 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  924                     sizeof(kinfo_proc));
  925         else {
  926                 mtx_lock_spin(&sched_lock);
  927                 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
  928                 FOREACH_THREAD_IN_PROC(p, td) {
  929                         fill_kinfo_thread(td, &kinfo_proc);
  930                         error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  931                             sizeof(kinfo_proc));
  932                         if (error)
  933                                 break;
  934                 }
  935                 mtx_unlock_spin(&sched_lock);
  936         }
  937         PROC_UNLOCK(p);
  938         if (error)
  939                 return (error);
  940         if (flags & KERN_PROC_ZOMBMASK)
  941                 np = zpfind(pid);
  942         else {
  943                 if (pid == 0)
  944                         return (0);
  945                 np = pfind(pid);
  946         }
  947         if (np == NULL)
  948                 return EAGAIN;
  949         if (np != p) {
  950                 PROC_UNLOCK(np);
  951                 return EAGAIN;
  952         }
  953         PROC_UNLOCK(np);
  954         return (0);
  955 }
  956 
  957 static int
  958 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
  959 {
  960         int *name = (int*) arg1;
  961         u_int namelen = arg2;
  962         struct proc *p;
  963         int flags, doingzomb, oid_number;
  964         int error = 0;
  965 
  966         oid_number = oidp->oid_number;
  967         if (oid_number != KERN_PROC_ALL &&
  968             (oid_number & KERN_PROC_INC_THREAD) == 0)
  969                 flags = KERN_PROC_NOTHREADS;
  970         else {
  971                 flags = 0;
  972                 oid_number &= ~KERN_PROC_INC_THREAD;
  973         }
  974         if (oid_number == KERN_PROC_PID) {
  975                 if (namelen != 1) 
  976                         return (EINVAL);
  977                 error = sysctl_wire_old_buffer(req, 0);
  978                 if (error)
  979                         return (error);         
  980                 p = pfind((pid_t)name[0]);
  981                 if (!p)
  982                         return (ESRCH);
  983                 if ((error = p_cansee(curthread, p))) {
  984                         PROC_UNLOCK(p);
  985                         return (error);
  986                 }
  987                 error = sysctl_out_proc(p, req, flags);
  988                 return (error);
  989         }
  990 
  991         switch (oid_number) {
  992         case KERN_PROC_ALL:
  993                 if (namelen != 0)
  994                         return (EINVAL);
  995                 break;
  996         case KERN_PROC_PROC:
  997                 if (namelen != 0 && namelen != 1)
  998                         return (EINVAL);
  999                 break;
 1000         default:
 1001                 if (namelen != 1)
 1002                         return (EINVAL);
 1003                 break;
 1004         }
 1005         
 1006         if (!req->oldptr) {
 1007                 /* overestimate by 5 procs */
 1008                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
 1009                 if (error)
 1010                         return (error);
 1011         }
 1012         error = sysctl_wire_old_buffer(req, 0);
 1013         if (error != 0)
 1014                 return (error);
 1015         sx_slock(&allproc_lock);
 1016         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
 1017                 if (!doingzomb)
 1018                         p = LIST_FIRST(&allproc);
 1019                 else
 1020                         p = LIST_FIRST(&zombproc);
 1021                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
 1022                         /*
 1023                          * Skip embryonic processes.
 1024                          */
 1025                         mtx_lock_spin(&sched_lock);
 1026                         if (p->p_state == PRS_NEW) {
 1027                                 mtx_unlock_spin(&sched_lock);
 1028                                 continue;
 1029                         }
 1030                         mtx_unlock_spin(&sched_lock);
 1031                         PROC_LOCK(p);
 1032                         /*
 1033                          * Show a user only appropriate processes.
 1034                          */
 1035                         if (p_cansee(curthread, p)) {
 1036                                 PROC_UNLOCK(p);
 1037                                 continue;
 1038                         }
 1039                         /*
 1040                          * TODO - make more efficient (see notes below).
 1041                          * do by session.
 1042                          */
 1043                         switch (oid_number) {
 1044 
 1045                         case KERN_PROC_GID:
 1046                                 if (p->p_ucred == NULL ||
 1047                                     p->p_ucred->cr_gid != (gid_t)name[0]) {
 1048                                         PROC_UNLOCK(p);
 1049                                         continue;
 1050                                 }
 1051                                 break;
 1052 
 1053                         case KERN_PROC_PGRP:
 1054                                 /* could do this by traversing pgrp */
 1055                                 if (p->p_pgrp == NULL || 
 1056                                     p->p_pgrp->pg_id != (pid_t)name[0]) {
 1057                                         PROC_UNLOCK(p);
 1058                                         continue;
 1059                                 }
 1060                                 break;
 1061 
 1062                         case KERN_PROC_RGID:
 1063                                 if (p->p_ucred == NULL ||
 1064                                     p->p_ucred->cr_rgid != (gid_t)name[0]) {
 1065                                         PROC_UNLOCK(p);
 1066                                         continue;
 1067                                 }
 1068                                 break;
 1069 
 1070                         case KERN_PROC_SESSION:
 1071                                 if (p->p_session == NULL ||
 1072                                     p->p_session->s_sid != (pid_t)name[0]) {
 1073                                         PROC_UNLOCK(p);
 1074                                         continue;
 1075                                 }
 1076                                 break;
 1077 
 1078                         case KERN_PROC_TTY:
 1079                                 if ((p->p_flag & P_CONTROLT) == 0 ||
 1080                                     p->p_session == NULL) {
 1081                                         PROC_UNLOCK(p);
 1082                                         continue;
 1083                                 }
 1084                                 SESS_LOCK(p->p_session);
 1085                                 if (p->p_session->s_ttyp == NULL ||
 1086                                     dev2udev(p->p_session->s_ttyp->t_dev) != 
 1087                                     (dev_t)name[0]) {
 1088                                         SESS_UNLOCK(p->p_session);
 1089                                         PROC_UNLOCK(p);
 1090                                         continue;
 1091                                 }
 1092                                 SESS_UNLOCK(p->p_session);
 1093                                 break;
 1094 
 1095                         case KERN_PROC_UID:
 1096                                 if (p->p_ucred == NULL || 
 1097                                     p->p_ucred->cr_uid != (uid_t)name[0]) {
 1098                                         PROC_UNLOCK(p);
 1099                                         continue;
 1100                                 }
 1101                                 break;
 1102 
 1103                         case KERN_PROC_RUID:
 1104                                 if (p->p_ucred == NULL || 
 1105                                     p->p_ucred->cr_ruid != (uid_t)name[0]) {
 1106                                         PROC_UNLOCK(p);
 1107                                         continue;
 1108                                 }
 1109                                 break;
 1110 
 1111                         case KERN_PROC_PROC:
 1112                                 break;
 1113 
 1114                         default:
 1115                                 break;
 1116 
 1117                         }
 1118 
 1119                         error = sysctl_out_proc(p, req, flags | doingzomb);
 1120                         if (error) {
 1121                                 sx_sunlock(&allproc_lock);
 1122                                 return (error);
 1123                         }
 1124                 }
 1125         }
 1126         sx_sunlock(&allproc_lock);
 1127         return (0);
 1128 }
 1129 
 1130 struct pargs *
 1131 pargs_alloc(int len)
 1132 {
 1133         struct pargs *pa;
 1134 
 1135         MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
 1136                 M_WAITOK);
 1137         pa->ar_ref = 1;
 1138         pa->ar_length = len;
 1139         return (pa);
 1140 }
 1141 
 1142 void
 1143 pargs_free(struct pargs *pa)
 1144 {
 1145 
 1146         FREE(pa, M_PARGS);
 1147 }
 1148 
 1149 void
 1150 pargs_hold(struct pargs *pa)
 1151 {
 1152 
 1153         if (pa == NULL)
 1154                 return;
 1155         PARGS_LOCK(pa);
 1156         pa->ar_ref++;
 1157         PARGS_UNLOCK(pa);
 1158 }
 1159 
 1160 void
 1161 pargs_drop(struct pargs *pa)
 1162 {
 1163 
 1164         if (pa == NULL)
 1165                 return;
 1166         PARGS_LOCK(pa);
 1167         if (--pa->ar_ref == 0) {
 1168                 PARGS_UNLOCK(pa);
 1169                 pargs_free(pa);
 1170         } else
 1171                 PARGS_UNLOCK(pa);
 1172 }
 1173 
 1174 /*
 1175  * This sysctl allows a process to retrieve the argument list or process
 1176  * title for another process without groping around in the address space
 1177  * of the other process.  It also allow a process to set its own "process 
 1178  * title to a string of its own choice.
 1179  */
 1180 static int
 1181 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 1182 {
 1183         int *name = (int*) arg1;
 1184         u_int namelen = arg2;
 1185         struct pargs *newpa, *pa;
 1186         struct proc *p;
 1187         int error = 0;
 1188 
 1189         if (namelen != 1) 
 1190                 return (EINVAL);
 1191 
 1192         p = pfind((pid_t)name[0]);
 1193         if (!p)
 1194                 return (ESRCH);
 1195 
 1196         if ((error = p_cansee(curthread, p)) != 0) {
 1197                 PROC_UNLOCK(p);
 1198                 return (error);
 1199         }
 1200 
 1201         if (req->newptr && curproc != p) {
 1202                 PROC_UNLOCK(p);
 1203                 return (EPERM);
 1204         }
 1205 
 1206         pa = p->p_args;
 1207         pargs_hold(pa);
 1208         PROC_UNLOCK(p);
 1209         if (req->oldptr != NULL && pa != NULL)
 1210                 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
 1211         pargs_drop(pa);
 1212         if (error != 0 || req->newptr == NULL)
 1213                 return (error);
 1214 
 1215         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
 1216                 return (ENOMEM);
 1217         newpa = pargs_alloc(req->newlen);
 1218         error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
 1219         if (error != 0) {
 1220                 pargs_free(newpa);
 1221                 return (error);
 1222         }
 1223         PROC_LOCK(p);
 1224         pa = p->p_args;
 1225         p->p_args = newpa;
 1226         PROC_UNLOCK(p);
 1227         pargs_drop(pa);
 1228         return (0);
 1229 }
 1230 
 1231 /*
 1232  * This sysctl allows a process to retrieve the path of the executable for
 1233  * itself or another process.
 1234  */
 1235 static int
 1236 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
 1237 {
 1238         pid_t *pidp = (pid_t *)arg1;
 1239         unsigned int arglen = arg2;
 1240         struct proc *p;
 1241         struct vnode *vp;
 1242         char *retbuf, *freebuf;
 1243         int error;
 1244 
 1245         if (arglen != 1)
 1246                 return (EINVAL);
 1247         if (*pidp == -1) {      /* -1 means this process */
 1248                 p = req->td->td_proc;
 1249         } else {
 1250                 p = pfind(*pidp);
 1251                 if (p == NULL)
 1252                         return (ESRCH);
 1253                 if ((error = p_cansee(curthread, p)) != 0) {
 1254                         PROC_UNLOCK(p);
 1255                         return (error);
 1256                 }
 1257         }
 1258 
 1259         vp = p->p_textvp;
 1260         if (vp == NULL) {
 1261                 if (*pidp != -1)
 1262                         PROC_UNLOCK(p);
 1263                 return (0);
 1264         }
 1265         vref(vp);
 1266         if (*pidp != -1)
 1267                 PROC_UNLOCK(p);
 1268         error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
 1269         vrele(vp);
 1270         if (error)
 1271                 return (error);
 1272         error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
 1273         free(freebuf, M_TEMP);
 1274         return (error);
 1275 }
 1276 
 1277 static int
 1278 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
 1279 {
 1280         struct proc *p;
 1281         char *sv_name;
 1282         int *name;
 1283         int namelen;
 1284         int error;
 1285 
 1286         namelen = arg2;
 1287         if (namelen != 1) 
 1288                 return (EINVAL);
 1289 
 1290         name = (int *)arg1;
 1291         if ((p = pfind((pid_t)name[0])) == NULL)
 1292                 return (ESRCH);
 1293         if ((error = p_cansee(curthread, p))) {
 1294                 PROC_UNLOCK(p);
 1295                 return (error);
 1296         }
 1297         sv_name = p->p_sysent->sv_name;
 1298         PROC_UNLOCK(p);
 1299         return (sysctl_handle_string(oidp, sv_name, 0, req));
 1300 }
 1301 
 1302 
 1303 static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
 1304 
 1305 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
 1306         0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
 1307 
 1308 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
 1309         sysctl_kern_proc, "Process table");
 1310 
 1311 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 
 1312         sysctl_kern_proc, "Process table");
 1313 
 1314 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
 1315         sysctl_kern_proc, "Process table");
 1316 
 1317 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
 1318         sysctl_kern_proc, "Process table");
 1319 
 1320 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 
 1321         sysctl_kern_proc, "Process table");
 1322 
 1323 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 
 1324         sysctl_kern_proc, "Process table");
 1325 
 1326 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 
 1327         sysctl_kern_proc, "Process table");
 1328 
 1329 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 
 1330         sysctl_kern_proc, "Process table");
 1331 
 1332 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
 1333         sysctl_kern_proc, "Return process table, no threads");
 1334 
 1335 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
 1336         CTLFLAG_RW | CTLFLAG_ANYBODY,
 1337         sysctl_kern_proc_args, "Process argument list");
 1338 
 1339 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
 1340         sysctl_kern_proc_pathname, "Process executable path");
 1341 
 1342 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
 1343         sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
 1344 
 1345 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
 1346         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1347 
 1348 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
 1349         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1350 
 1351 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
 1352         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1353 
 1354 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
 1355         sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1356 
 1357 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
 1358         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1359 
 1360 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
 1361         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1362 
 1363 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
 1364         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1365 
 1366 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
 1367         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1368 
 1369 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
 1370         CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");

Cache object: c7aa9eb3d324ac92264e8ab624eb3736


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