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


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

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

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

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

Cache object: bf84182969a565dd478687080472ec6a


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