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

Cache object: 09462e813ff73db45866544739266756


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