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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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.0/sys/kern/kern_proc.c 150962 2005-10-05 05:30:24Z truckman $
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/6.0/sys/kern/kern_proc.c 150962 2005-10-05 05:30:24Z truckman $");
   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 {
  780                 kp->ki_stat = SIDL;
  781         }
  782 
  783         kg = td->td_ksegrp;
  784 
  785         /* things in the KSE GROUP */
  786         kp->ki_estcpu = kg->kg_estcpu;
  787         kp->ki_slptime = kg->kg_slptime;
  788         kp->ki_pri.pri_user = kg->kg_user_pri;
  789         kp->ki_pri.pri_class = kg->kg_pri_class;
  790 
  791         /* Things in the thread */
  792         kp->ki_wchan = td->td_wchan;
  793         kp->ki_pri.pri_level = td->td_priority;
  794         kp->ki_pri.pri_native = td->td_base_pri;
  795         kp->ki_lastcpu = td->td_lastcpu;
  796         kp->ki_oncpu = td->td_oncpu;
  797         kp->ki_tdflags = td->td_flags;
  798         kp->ki_tid = td->td_tid;
  799         kp->ki_numthreads = p->p_numthreads;
  800         kp->ki_pcb = td->td_pcb;
  801         kp->ki_kstack = (void *)td->td_kstack;
  802         kp->ki_pctcpu = sched_pctcpu(td);
  803 
  804         /* We can't get this anymore but ps etc never used it anyway. */
  805         kp->ki_rqindex = 0;
  806 
  807         SIGSETOR(kp->ki_siglist, td->td_siglist);
  808         kp->ki_sigmask = td->td_sigmask;
  809 }
  810 
  811 /*
  812  * Fill in a kinfo_proc structure for the specified process.
  813  * Must be called with the target process locked.
  814  */
  815 void
  816 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
  817 {
  818 
  819         fill_kinfo_proc_only(p, kp);
  820         mtx_lock_spin(&sched_lock);
  821         if (FIRST_THREAD_IN_PROC(p) != NULL)
  822                 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
  823         mtx_unlock_spin(&sched_lock);
  824 }
  825 
  826 struct pstats *
  827 pstats_alloc(void)
  828 {
  829 
  830         return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
  831 }
  832 
  833 /*
  834  * Copy parts of p_stats; zero the rest of p_stats (statistics).
  835  */
  836 void
  837 pstats_fork(struct pstats *src, struct pstats *dst)
  838 {
  839 
  840         bzero(&dst->pstat_startzero,
  841             __rangeof(struct pstats, pstat_startzero, pstat_endzero));
  842         bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
  843             __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
  844 }
  845 
  846 void
  847 pstats_free(struct pstats *ps)
  848 {
  849 
  850         free(ps, M_SUBPROC);
  851 }
  852 
  853 /*
  854  * Locate a zombie process by number
  855  */
  856 struct proc *
  857 zpfind(pid_t pid)
  858 {
  859         struct proc *p;
  860 
  861         sx_slock(&allproc_lock);
  862         LIST_FOREACH(p, &zombproc, p_list)
  863                 if (p->p_pid == pid) {
  864                         PROC_LOCK(p);
  865                         break;
  866                 }
  867         sx_sunlock(&allproc_lock);
  868         return (p);
  869 }
  870 
  871 #define KERN_PROC_ZOMBMASK      0x3
  872 #define KERN_PROC_NOTHREADS     0x4
  873 
  874 /*
  875  * Must be called with the process locked and will return with it unlocked.
  876  */
  877 static int
  878 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
  879 {
  880         struct thread *td;
  881         struct kinfo_proc kinfo_proc;
  882         int error = 0;
  883         struct proc *np;
  884         pid_t pid = p->p_pid;
  885 
  886         PROC_LOCK_ASSERT(p, MA_OWNED);
  887 
  888         fill_kinfo_proc(p, &kinfo_proc);
  889         if (flags & KERN_PROC_NOTHREADS) {
  890                 mtx_lock_spin(&sched_lock);
  891                 if (FIRST_THREAD_IN_PROC(p) != NULL)
  892                         fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc);
  893                 mtx_unlock_spin(&sched_lock);
  894                 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  895                                    sizeof(kinfo_proc));
  896         } else {
  897                 mtx_lock_spin(&sched_lock);
  898                 if (FIRST_THREAD_IN_PROC(p) != NULL)
  899                         FOREACH_THREAD_IN_PROC(p, td) {
  900                                 fill_kinfo_thread(td, &kinfo_proc);
  901                                 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  902                                                 sizeof(kinfo_proc));
  903                                 if (error)
  904                                         break;
  905                         }
  906                 else
  907                         error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
  908                                         sizeof(kinfo_proc));
  909                 mtx_unlock_spin(&sched_lock);
  910         }
  911         PROC_UNLOCK(p);
  912         if (error)
  913                 return (error);
  914         if (flags & KERN_PROC_ZOMBMASK)
  915                 np = zpfind(pid);
  916         else {
  917                 if (pid == 0)
  918                         return (0);
  919                 np = pfind(pid);
  920         }
  921         if (np == NULL)
  922                 return EAGAIN;
  923         if (np != p) {
  924                 PROC_UNLOCK(np);
  925                 return EAGAIN;
  926         }
  927         PROC_UNLOCK(np);
  928         return (0);
  929 }
  930 
  931 static int
  932 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
  933 {
  934         int *name = (int*) arg1;
  935         u_int namelen = arg2;
  936         struct proc *p;
  937         int flags, doingzomb, oid_number;
  938         int error = 0;
  939 
  940         oid_number = oidp->oid_number;
  941         if (oid_number != KERN_PROC_ALL &&
  942             (oid_number & KERN_PROC_INC_THREAD) == 0)
  943                 flags = KERN_PROC_NOTHREADS;
  944         else {
  945                 flags = 0;
  946                 oid_number &= ~KERN_PROC_INC_THREAD;
  947         }
  948         if (oid_number == KERN_PROC_PID) {
  949                 if (namelen != 1) 
  950                         return (EINVAL);
  951                 error = sysctl_wire_old_buffer(req, 0);
  952                 if (error)
  953                         return (error);         
  954                 p = pfind((pid_t)name[0]);
  955                 if (!p)
  956                         return (ESRCH);
  957                 if ((error = p_cansee(curthread, p))) {
  958                         PROC_UNLOCK(p);
  959                         return (error);
  960                 }
  961                 error = sysctl_out_proc(p, req, flags);
  962                 return (error);
  963         }
  964 
  965         switch (oid_number) {
  966         case KERN_PROC_ALL:
  967                 if (namelen != 0)
  968                         return (EINVAL);
  969                 break;
  970         case KERN_PROC_PROC:
  971                 if (namelen != 0 && namelen != 1)
  972                         return (EINVAL);
  973                 break;
  974         default:
  975                 if (namelen != 1)
  976                         return (EINVAL);
  977                 break;
  978         }
  979         
  980         if (!req->oldptr) {
  981                 /* overestimate by 5 procs */
  982                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
  983                 if (error)
  984                         return (error);
  985         }
  986         error = sysctl_wire_old_buffer(req, 0);
  987         if (error != 0)
  988                 return (error);
  989         sx_slock(&allproc_lock);
  990         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
  991                 if (!doingzomb)
  992                         p = LIST_FIRST(&allproc);
  993                 else
  994                         p = LIST_FIRST(&zombproc);
  995                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
  996                         /*
  997                          * Skip embryonic processes.
  998                          */
  999                         mtx_lock_spin(&sched_lock);
 1000                         if (p->p_state == PRS_NEW) {
 1001                                 mtx_unlock_spin(&sched_lock);
 1002                                 continue;
 1003                         }
 1004                         mtx_unlock_spin(&sched_lock);
 1005                         PROC_LOCK(p);
 1006                         /*
 1007                          * Show a user only appropriate processes.
 1008                          */
 1009                         if (p_cansee(curthread, p)) {
 1010                                 PROC_UNLOCK(p);
 1011                                 continue;
 1012                         }
 1013                         /*
 1014                          * TODO - make more efficient (see notes below).
 1015                          * do by session.
 1016                          */
 1017                         switch (oid_number) {
 1018 
 1019                         case KERN_PROC_GID:
 1020                                 if (p->p_ucred == NULL ||
 1021                                     p->p_ucred->cr_gid != (gid_t)name[0]) {
 1022                                         PROC_UNLOCK(p);
 1023                                         continue;
 1024                                 }
 1025                                 break;
 1026 
 1027                         case KERN_PROC_PGRP:
 1028                                 /* could do this by traversing pgrp */
 1029                                 if (p->p_pgrp == NULL || 
 1030                                     p->p_pgrp->pg_id != (pid_t)name[0]) {
 1031                                         PROC_UNLOCK(p);
 1032                                         continue;
 1033                                 }
 1034                                 break;
 1035 
 1036                         case KERN_PROC_RGID:
 1037                                 if (p->p_ucred == NULL ||
 1038                                     p->p_ucred->cr_rgid != (gid_t)name[0]) {
 1039                                         PROC_UNLOCK(p);
 1040                                         continue;
 1041                                 }
 1042                                 break;
 1043 
 1044                         case KERN_PROC_SESSION:
 1045                                 if (p->p_session == NULL ||
 1046                                     p->p_session->s_sid != (pid_t)name[0]) {
 1047                                         PROC_UNLOCK(p);
 1048                                         continue;
 1049                                 }
 1050                                 break;
 1051 
 1052                         case KERN_PROC_TTY:
 1053                                 if ((p->p_flag & P_CONTROLT) == 0 ||
 1054                                     p->p_session == NULL) {
 1055                                         PROC_UNLOCK(p);
 1056                                         continue;
 1057                                 }
 1058                                 SESS_LOCK(p->p_session);
 1059                                 if (p->p_session->s_ttyp == NULL ||
 1060                                     dev2udev(p->p_session->s_ttyp->t_dev) != 
 1061                                     (dev_t)name[0]) {
 1062                                         SESS_UNLOCK(p->p_session);
 1063                                         PROC_UNLOCK(p);
 1064                                         continue;
 1065                                 }
 1066                                 SESS_UNLOCK(p->p_session);
 1067                                 break;
 1068 
 1069                         case KERN_PROC_UID:
 1070                                 if (p->p_ucred == NULL || 
 1071                                     p->p_ucred->cr_uid != (uid_t)name[0]) {
 1072                                         PROC_UNLOCK(p);
 1073                                         continue;
 1074                                 }
 1075                                 break;
 1076 
 1077                         case KERN_PROC_RUID:
 1078                                 if (p->p_ucred == NULL || 
 1079                                     p->p_ucred->cr_ruid != (uid_t)name[0]) {
 1080                                         PROC_UNLOCK(p);
 1081                                         continue;
 1082                                 }
 1083                                 break;
 1084 
 1085                         case KERN_PROC_PROC:
 1086                                 break;
 1087 
 1088                         default:
 1089                                 break;
 1090 
 1091                         }
 1092 
 1093                         error = sysctl_out_proc(p, req, flags | doingzomb);
 1094                         if (error) {
 1095                                 sx_sunlock(&allproc_lock);
 1096                                 return (error);
 1097                         }
 1098                 }
 1099         }
 1100         sx_sunlock(&allproc_lock);
 1101         return (0);
 1102 }
 1103 
 1104 struct pargs *
 1105 pargs_alloc(int len)
 1106 {
 1107         struct pargs *pa;
 1108 
 1109         MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
 1110                 M_WAITOK);
 1111         pa->ar_ref = 1;
 1112         pa->ar_length = len;
 1113         return (pa);
 1114 }
 1115 
 1116 void
 1117 pargs_free(struct pargs *pa)
 1118 {
 1119 
 1120         FREE(pa, M_PARGS);
 1121 }
 1122 
 1123 void
 1124 pargs_hold(struct pargs *pa)
 1125 {
 1126 
 1127         if (pa == NULL)
 1128                 return;
 1129         PARGS_LOCK(pa);
 1130         pa->ar_ref++;
 1131         PARGS_UNLOCK(pa);
 1132 }
 1133 
 1134 void
 1135 pargs_drop(struct pargs *pa)
 1136 {
 1137 
 1138         if (pa == NULL)
 1139                 return;
 1140         PARGS_LOCK(pa);
 1141         if (--pa->ar_ref == 0) {
 1142                 PARGS_UNLOCK(pa);
 1143                 pargs_free(pa);
 1144         } else
 1145                 PARGS_UNLOCK(pa);
 1146 }
 1147 
 1148 /*
 1149  * This sysctl allows a process to retrieve the argument list or process
 1150  * title for another process without groping around in the address space
 1151  * of the other process.  It also allow a process to set its own "process 
 1152  * title to a string of its own choice.
 1153  */
 1154 static int
 1155 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 1156 {
 1157         int *name = (int*) arg1;
 1158         u_int namelen = arg2;
 1159         struct pargs *newpa, *pa;
 1160         struct proc *p;
 1161         int error = 0;
 1162 
 1163         if (namelen != 1) 
 1164                 return (EINVAL);
 1165 
 1166         p = pfind((pid_t)name[0]);
 1167         if (!p)
 1168                 return (ESRCH);
 1169 
 1170         if ((error = p_cansee(curthread, p)) != 0) {
 1171                 PROC_UNLOCK(p);
 1172                 return (error);
 1173         }
 1174 
 1175         if (req->newptr && curproc != p) {
 1176                 PROC_UNLOCK(p);
 1177                 return (EPERM);
 1178         }
 1179 
 1180         pa = p->p_args;
 1181         pargs_hold(pa);
 1182         PROC_UNLOCK(p);
 1183         if (req->oldptr != NULL && pa != NULL)
 1184                 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
 1185         pargs_drop(pa);
 1186         if (error != 0 || req->newptr == NULL)
 1187                 return (error);
 1188 
 1189         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
 1190                 return (ENOMEM);
 1191         newpa = pargs_alloc(req->newlen);
 1192         error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
 1193         if (error != 0) {
 1194                 pargs_free(newpa);
 1195                 return (error);
 1196         }
 1197         PROC_LOCK(p);
 1198         pa = p->p_args;
 1199         p->p_args = newpa;
 1200         PROC_UNLOCK(p);
 1201         pargs_drop(pa);
 1202         return (0);
 1203 }
 1204 
 1205 /*
 1206  * This sysctl allows a process to retrieve the path of the executable for
 1207  * itself or another process.
 1208  */
 1209 static int
 1210 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
 1211 {
 1212         pid_t *pidp = (pid_t *)arg1;
 1213         unsigned int arglen = arg2;
 1214         struct proc *p;
 1215         struct vnode *vp;
 1216         char *retbuf, *freebuf;
 1217         int error;
 1218 
 1219         if (arglen != 1)
 1220                 return (EINVAL);
 1221         if (*pidp == -1) {      /* -1 means this process */
 1222                 p = req->td->td_proc;
 1223         } else {
 1224                 p = pfind(*pidp);
 1225                 if (p == NULL)
 1226                         return (ESRCH);
 1227                 if ((error = p_cansee(curthread, p)) != 0) {
 1228                         PROC_UNLOCK(p);
 1229                         return (error);
 1230                 }
 1231         }
 1232 
 1233         vp = p->p_textvp;
 1234         vref(vp);
 1235         if (*pidp != -1)
 1236                 PROC_UNLOCK(p);
 1237         error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
 1238         vrele(vp);
 1239         if (error)
 1240                 return (error);
 1241         error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
 1242         free(freebuf, M_TEMP);
 1243         return (error);
 1244 }
 1245 
 1246 static int
 1247 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
 1248 {
 1249         struct proc *p;
 1250         char *sv_name;
 1251         int *name;
 1252         int namelen;
 1253         int error;
 1254 
 1255         namelen = arg2;
 1256         if (namelen != 1) 
 1257                 return (EINVAL);
 1258 
 1259         name = (int *)arg1;
 1260         if ((p = pfind((pid_t)name[0])) == NULL)
 1261                 return (ESRCH);
 1262         if ((error = p_cansee(curthread, p))) {
 1263                 PROC_UNLOCK(p);
 1264                 return (error);
 1265         }
 1266         sv_name = p->p_sysent->sv_name;
 1267         PROC_UNLOCK(p);
 1268         return (sysctl_handle_string(oidp, sv_name, 0, req));
 1269 }
 1270 
 1271 
 1272 static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
 1273 
 1274 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
 1275         0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
 1276 
 1277 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
 1278         sysctl_kern_proc, "Process table");
 1279 
 1280 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 
 1281         sysctl_kern_proc, "Process table");
 1282 
 1283 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
 1284         sysctl_kern_proc, "Process table");
 1285 
 1286 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
 1287         sysctl_kern_proc, "Process table");
 1288 
 1289 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 
 1290         sysctl_kern_proc, "Process table");
 1291 
 1292 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 
 1293         sysctl_kern_proc, "Process table");
 1294 
 1295 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 
 1296         sysctl_kern_proc, "Process table");
 1297 
 1298 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 
 1299         sysctl_kern_proc, "Process table");
 1300 
 1301 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
 1302         sysctl_kern_proc, "Return process table, no threads");
 1303 
 1304 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
 1305         CTLFLAG_RW | CTLFLAG_ANYBODY,
 1306         sysctl_kern_proc_args, "Process argument list");
 1307 
 1308 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
 1309         sysctl_kern_proc_pathname, "Process executable path");
 1310 
 1311 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
 1312         sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
 1313 
 1314 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
 1315         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1316 
 1317 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
 1318         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1319 
 1320 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
 1321         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1322 
 1323 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
 1324         sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1325 
 1326 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
 1327         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1328 
 1329 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
 1330         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1331 
 1332 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
 1333         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1334 
 1335 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
 1336         CTLFLAG_RD, sysctl_kern_proc, "Process table");
 1337 
 1338 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
 1339         CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");

Cache object: 71318bdad350e39580adebf29ffa7c8c


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