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


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

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

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

    1 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/10.4/sys/kern/kern_proc.c 315260 2017-03-14 15:50:36Z hselasky $");
   34 
   35 #include "opt_compat.h"
   36 #include "opt_ddb.h"
   37 #include "opt_kdtrace.h"
   38 #include "opt_ktrace.h"
   39 #include "opt_kstack_pages.h"
   40 #include "opt_stack.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/elf.h>
   45 #include <sys/exec.h>
   46 #include <sys/kernel.h>
   47 #include <sys/limits.h>
   48 #include <sys/lock.h>
   49 #include <sys/loginclass.h>
   50 #include <sys/malloc.h>
   51 #include <sys/mman.h>
   52 #include <sys/mount.h>
   53 #include <sys/mutex.h>
   54 #include <sys/proc.h>
   55 #include <sys/ptrace.h>
   56 #include <sys/refcount.h>
   57 #include <sys/resourcevar.h>
   58 #include <sys/rwlock.h>
   59 #include <sys/sbuf.h>
   60 #include <sys/sysent.h>
   61 #include <sys/sched.h>
   62 #include <sys/smp.h>
   63 #include <sys/stack.h>
   64 #include <sys/stat.h>
   65 #include <sys/sysctl.h>
   66 #include <sys/filedesc.h>
   67 #include <sys/tty.h>
   68 #include <sys/signalvar.h>
   69 #include <sys/sdt.h>
   70 #include <sys/sx.h>
   71 #include <sys/user.h>
   72 #include <sys/jail.h>
   73 #include <sys/vnode.h>
   74 #include <sys/eventhandler.h>
   75 
   76 #ifdef DDB
   77 #include <ddb/ddb.h>
   78 #endif
   79 
   80 #include <vm/vm.h>
   81 #include <vm/vm_param.h>
   82 #include <vm/vm_extern.h>
   83 #include <vm/pmap.h>
   84 #include <vm/vm_map.h>
   85 #include <vm/vm_object.h>
   86 #include <vm/vm_page.h>
   87 #include <vm/uma.h>
   88 
   89 #ifdef COMPAT_FREEBSD32
   90 #include <compat/freebsd32/freebsd32.h>
   91 #include <compat/freebsd32/freebsd32_util.h>
   92 #endif
   93 
   94 SDT_PROVIDER_DEFINE(proc);
   95 SDT_PROBE_DEFINE4(proc, , ctor, entry, "struct proc *", "int", "void *",
   96     "int");
   97 SDT_PROBE_DEFINE4(proc, , ctor, return, "struct proc *", "int", "void *",
   98     "int");
   99 SDT_PROBE_DEFINE4(proc, , dtor, entry, "struct proc *", "int", "void *",
  100     "struct thread *");
  101 SDT_PROBE_DEFINE3(proc, , dtor, return, "struct proc *", "int", "void *");
  102 SDT_PROBE_DEFINE3(proc, , init, entry, "struct proc *", "int", "int");
  103 SDT_PROBE_DEFINE3(proc, , init, return, "struct proc *", "int", "int");
  104 
  105 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
  106 MALLOC_DEFINE(M_SESSION, "session", "session header");
  107 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
  108 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
  109 
  110 static void doenterpgrp(struct proc *, struct pgrp *);
  111 static void orphanpg(struct pgrp *pg);
  112 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
  113 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
  114 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
  115     int preferthread);
  116 static void pgadjustjobc(struct pgrp *pgrp, int entering);
  117 static void pgdelete(struct pgrp *);
  118 static int proc_ctor(void *mem, int size, void *arg, int flags);
  119 static void proc_dtor(void *mem, int size, void *arg);
  120 static int proc_init(void *mem, int size, int flags);
  121 static void proc_fini(void *mem, int size);
  122 static void pargs_free(struct pargs *pa);
  123 static struct proc *zpfind_locked(pid_t pid);
  124 
  125 /*
  126  * Other process lists
  127  */
  128 struct pidhashhead *pidhashtbl;
  129 u_long pidhash;
  130 struct pgrphashhead *pgrphashtbl;
  131 u_long pgrphash;
  132 struct proclist allproc;
  133 struct proclist zombproc;
  134 struct sx allproc_lock;
  135 struct sx proctree_lock;
  136 struct mtx ppeers_lock;
  137 uma_zone_t proc_zone;
  138 
  139 /*
  140  * The offset of various fields in struct proc and struct thread.
  141  * These are used by kernel debuggers to enumerate kernel threads and
  142  * processes.
  143  */
  144 const int proc_off_p_pid = offsetof(struct proc, p_pid);
  145 const int proc_off_p_comm = offsetof(struct proc, p_comm);
  146 const int proc_off_p_list = offsetof(struct proc, p_list);
  147 const int proc_off_p_threads = offsetof(struct proc, p_threads);
  148 const int thread_off_td_tid = offsetof(struct thread, td_tid);
  149 const int thread_off_td_name = offsetof(struct thread, td_name);
  150 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
  151 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
  152 const int thread_off_td_plist = offsetof(struct thread, td_plist);
  153 
  154 int kstack_pages = KSTACK_PAGES;
  155 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
  156     "Kernel stack size in pages");
  157 static int vmmap_skip_res_cnt = 0;
  158 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
  159     &vmmap_skip_res_cnt, 0,
  160     "Skip calculation of the pages resident count in kern.proc.vmmap");
  161 
  162 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
  163 #ifdef COMPAT_FREEBSD32
  164 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
  165 #endif
  166 
  167 /*
  168  * Initialize global process hashing structures.
  169  */
  170 void
  171 procinit()
  172 {
  173 
  174         sx_init(&allproc_lock, "allproc");
  175         sx_init(&proctree_lock, "proctree");
  176         mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
  177         LIST_INIT(&allproc);
  178         LIST_INIT(&zombproc);
  179         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
  180         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
  181         proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
  182             proc_ctor, proc_dtor, proc_init, proc_fini,
  183             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  184         uihashinit();
  185 }
  186 
  187 /*
  188  * Prepare a proc for use.
  189  */
  190 static int
  191 proc_ctor(void *mem, int size, void *arg, int flags)
  192 {
  193         struct proc *p;
  194         struct thread *td;
  195 
  196         p = (struct proc *)mem;
  197         SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
  198         EVENTHANDLER_INVOKE(process_ctor, p);
  199         SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
  200         td = FIRST_THREAD_IN_PROC(p);
  201         if (td != NULL) {
  202                 /* Make sure all thread constructors are executed */
  203                 EVENTHANDLER_INVOKE(thread_ctor, td);
  204         }
  205         return (0);
  206 }
  207 
  208 /*
  209  * Reclaim a proc after use.
  210  */
  211 static void
  212 proc_dtor(void *mem, int size, void *arg)
  213 {
  214         struct proc *p;
  215         struct thread *td;
  216 
  217         /* INVARIANTS checks go here */
  218         p = (struct proc *)mem;
  219         td = FIRST_THREAD_IN_PROC(p);
  220         SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
  221         if (td != NULL) {
  222 #ifdef INVARIANTS
  223                 KASSERT((p->p_numthreads == 1),
  224                     ("bad number of threads in exiting process"));
  225                 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
  226 #endif
  227                 /* Free all OSD associated to this thread. */
  228                 osd_thread_exit(td);
  229 
  230                 /* Make sure all thread destructors are executed */
  231                 EVENTHANDLER_INVOKE(thread_dtor, td);
  232         }
  233         EVENTHANDLER_INVOKE(process_dtor, p);
  234         if (p->p_ksi != NULL)
  235                 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
  236         SDT_PROBE3(proc, , dtor, return, p, size, arg);
  237 }
  238 
  239 /*
  240  * Initialize type-stable parts of a proc (when newly created).
  241  */
  242 static int
  243 proc_init(void *mem, int size, int flags)
  244 {
  245         struct proc *p;
  246 
  247         p = (struct proc *)mem;
  248         SDT_PROBE3(proc, , init, entry, p, size, flags);
  249         p->p_sched = (struct p_sched *)&p[1];
  250         bzero(&p->p_mtx, sizeof(struct mtx));
  251         mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
  252         mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
  253         cv_init(&p->p_pwait, "ppwait");
  254         cv_init(&p->p_dbgwait, "dbgwait");
  255         TAILQ_INIT(&p->p_threads);           /* all threads in proc */
  256         EVENTHANDLER_INVOKE(process_init, p);
  257         p->p_stats = pstats_alloc();
  258         p->p_pgrp = NULL;
  259         SDT_PROBE3(proc, , init, return, p, size, flags);
  260         return (0);
  261 }
  262 
  263 /*
  264  * UMA should ensure that this function is never called.
  265  * Freeing a proc structure would violate type stability.
  266  */
  267 static void
  268 proc_fini(void *mem, int size)
  269 {
  270 #ifdef notnow
  271         struct proc *p;
  272 
  273         p = (struct proc *)mem;
  274         EVENTHANDLER_INVOKE(process_fini, p);
  275         pstats_free(p->p_stats);
  276         thread_free(FIRST_THREAD_IN_PROC(p));
  277         mtx_destroy(&p->p_mtx);
  278         if (p->p_ksi != NULL)
  279                 ksiginfo_free(p->p_ksi);
  280 #else
  281         panic("proc reclaimed");
  282 #endif
  283 }
  284 
  285 /*
  286  * Is p an inferior of the current process?
  287  */
  288 int
  289 inferior(struct proc *p)
  290 {
  291 
  292         sx_assert(&proctree_lock, SX_LOCKED);
  293         PROC_LOCK_ASSERT(p, MA_OWNED);
  294         for (; p != curproc; p = proc_realparent(p)) {
  295                 if (p->p_pid == 0)
  296                         return (0);
  297         }
  298         return (1);
  299 }
  300 
  301 struct proc *
  302 pfind_locked(pid_t pid)
  303 {
  304         struct proc *p;
  305 
  306         sx_assert(&allproc_lock, SX_LOCKED);
  307         LIST_FOREACH(p, PIDHASH(pid), p_hash) {
  308                 if (p->p_pid == pid) {
  309                         PROC_LOCK(p);
  310                         if (p->p_state == PRS_NEW) {
  311                                 PROC_UNLOCK(p);
  312                                 p = NULL;
  313                         }
  314                         break;
  315                 }
  316         }
  317         return (p);
  318 }
  319 
  320 /*
  321  * Locate a process by number; return only "live" processes -- i.e., neither
  322  * zombies nor newly born but incompletely initialized processes.  By not
  323  * returning processes in the PRS_NEW state, we allow callers to avoid
  324  * testing for that condition to avoid dereferencing p_ucred, et al.
  325  */
  326 struct proc *
  327 pfind(pid_t pid)
  328 {
  329         struct proc *p;
  330 
  331         sx_slock(&allproc_lock);
  332         p = pfind_locked(pid);
  333         sx_sunlock(&allproc_lock);
  334         return (p);
  335 }
  336 
  337 static struct proc *
  338 pfind_tid_locked(pid_t tid)
  339 {
  340         struct proc *p;
  341         struct thread *td;
  342 
  343         sx_assert(&allproc_lock, SX_LOCKED);
  344         FOREACH_PROC_IN_SYSTEM(p) {
  345                 PROC_LOCK(p);
  346                 if (p->p_state == PRS_NEW) {
  347                         PROC_UNLOCK(p);
  348                         continue;
  349                 }
  350                 FOREACH_THREAD_IN_PROC(p, td) {
  351                         if (td->td_tid == tid)
  352                                 goto found;
  353                 }
  354                 PROC_UNLOCK(p);
  355         }
  356 found:
  357         return (p);
  358 }
  359 
  360 /*
  361  * Locate a process group by number.
  362  * The caller must hold proctree_lock.
  363  */
  364 struct pgrp *
  365 pgfind(pgid)
  366         register pid_t pgid;
  367 {
  368         register struct pgrp *pgrp;
  369 
  370         sx_assert(&proctree_lock, SX_LOCKED);
  371 
  372         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
  373                 if (pgrp->pg_id == pgid) {
  374                         PGRP_LOCK(pgrp);
  375                         return (pgrp);
  376                 }
  377         }
  378         return (NULL);
  379 }
  380 
  381 /*
  382  * Locate process and do additional manipulations, depending on flags.
  383  */
  384 int
  385 pget(pid_t pid, int flags, struct proc **pp)
  386 {
  387         struct proc *p;
  388         int error;
  389 
  390         sx_slock(&allproc_lock);
  391         if (pid <= PID_MAX) {
  392                 p = pfind_locked(pid);
  393                 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
  394                         p = zpfind_locked(pid);
  395         } else if ((flags & PGET_NOTID) == 0) {
  396                 p = pfind_tid_locked(pid);
  397         } else {
  398                 p = NULL;
  399         }
  400         sx_sunlock(&allproc_lock);
  401         if (p == NULL)
  402                 return (ESRCH);
  403         if ((flags & PGET_CANSEE) != 0) {
  404                 error = p_cansee(curthread, p);
  405                 if (error != 0)
  406                         goto errout;
  407         }
  408         if ((flags & PGET_CANDEBUG) != 0) {
  409                 error = p_candebug(curthread, p);
  410                 if (error != 0)
  411                         goto errout;
  412         }
  413         if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
  414                 error = EPERM;
  415                 goto errout;
  416         }
  417         if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
  418                 error = ESRCH;
  419                 goto errout;
  420         }
  421         if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
  422                 /*
  423                  * XXXRW: Not clear ESRCH is the right error during proc
  424                  * execve().
  425                  */
  426                 error = ESRCH;
  427                 goto errout;
  428         }
  429         if ((flags & PGET_HOLD) != 0) {
  430                 _PHOLD(p);
  431                 PROC_UNLOCK(p);
  432         }
  433         *pp = p;
  434         return (0);
  435 errout:
  436         PROC_UNLOCK(p);
  437         return (error);
  438 }
  439 
  440 /*
  441  * Create a new process group.
  442  * pgid must be equal to the pid of p.
  443  * Begin a new session if required.
  444  */
  445 int
  446 enterpgrp(p, pgid, pgrp, sess)
  447         register struct proc *p;
  448         pid_t pgid;
  449         struct pgrp *pgrp;
  450         struct session *sess;
  451 {
  452 
  453         sx_assert(&proctree_lock, SX_XLOCKED);
  454 
  455         KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
  456         KASSERT(p->p_pid == pgid,
  457             ("enterpgrp: new pgrp and pid != pgid"));
  458         KASSERT(pgfind(pgid) == NULL,
  459             ("enterpgrp: pgrp with pgid exists"));
  460         KASSERT(!SESS_LEADER(p),
  461             ("enterpgrp: session leader attempted setpgrp"));
  462 
  463         mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
  464 
  465         if (sess != NULL) {
  466                 /*
  467                  * new session
  468                  */
  469                 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
  470                 PROC_LOCK(p);
  471                 p->p_flag &= ~P_CONTROLT;
  472                 PROC_UNLOCK(p);
  473                 PGRP_LOCK(pgrp);
  474                 sess->s_leader = p;
  475                 sess->s_sid = p->p_pid;
  476                 refcount_init(&sess->s_count, 1);
  477                 sess->s_ttyvp = NULL;
  478                 sess->s_ttydp = NULL;
  479                 sess->s_ttyp = NULL;
  480                 bcopy(p->p_session->s_login, sess->s_login,
  481                             sizeof(sess->s_login));
  482                 pgrp->pg_session = sess;
  483                 KASSERT(p == curproc,
  484                     ("enterpgrp: mksession and p != curproc"));
  485         } else {
  486                 pgrp->pg_session = p->p_session;
  487                 sess_hold(pgrp->pg_session);
  488                 PGRP_LOCK(pgrp);
  489         }
  490         pgrp->pg_id = pgid;
  491         LIST_INIT(&pgrp->pg_members);
  492 
  493         /*
  494          * As we have an exclusive lock of proctree_lock,
  495          * this should not deadlock.
  496          */
  497         LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
  498         pgrp->pg_jobc = 0;
  499         SLIST_INIT(&pgrp->pg_sigiolst);
  500         PGRP_UNLOCK(pgrp);
  501 
  502         doenterpgrp(p, pgrp);
  503 
  504         return (0);
  505 }
  506 
  507 /*
  508  * Move p to an existing process group
  509  */
  510 int
  511 enterthispgrp(p, pgrp)
  512         register struct proc *p;
  513         struct pgrp *pgrp;
  514 {
  515 
  516         sx_assert(&proctree_lock, SX_XLOCKED);
  517         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  518         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  519         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
  520         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
  521         KASSERT(pgrp->pg_session == p->p_session,
  522                 ("%s: pgrp's session %p, p->p_session %p.\n",
  523                 __func__,
  524                 pgrp->pg_session,
  525                 p->p_session));
  526         KASSERT(pgrp != p->p_pgrp,
  527                 ("%s: p belongs to pgrp.", __func__));
  528 
  529         doenterpgrp(p, pgrp);
  530 
  531         return (0);
  532 }
  533 
  534 /*
  535  * Move p to a process group
  536  */
  537 static void
  538 doenterpgrp(p, pgrp)
  539         struct proc *p;
  540         struct pgrp *pgrp;
  541 {
  542         struct pgrp *savepgrp;
  543 
  544         sx_assert(&proctree_lock, SX_XLOCKED);
  545         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  546         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  547         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
  548         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
  549 
  550         savepgrp = p->p_pgrp;
  551 
  552         /*
  553          * Adjust eligibility of affected pgrps to participate in job control.
  554          * Increment eligibility counts before decrementing, otherwise we
  555          * could reach 0 spuriously during the first call.
  556          */
  557         fixjobc(p, pgrp, 1);
  558         fixjobc(p, p->p_pgrp, 0);
  559 
  560         PGRP_LOCK(pgrp);
  561         PGRP_LOCK(savepgrp);
  562         PROC_LOCK(p);
  563         LIST_REMOVE(p, p_pglist);
  564         p->p_pgrp = pgrp;
  565         PROC_UNLOCK(p);
  566         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
  567         PGRP_UNLOCK(savepgrp);
  568         PGRP_UNLOCK(pgrp);
  569         if (LIST_EMPTY(&savepgrp->pg_members))
  570                 pgdelete(savepgrp);
  571 }
  572 
  573 /*
  574  * remove process from process group
  575  */
  576 int
  577 leavepgrp(p)
  578         register struct proc *p;
  579 {
  580         struct pgrp *savepgrp;
  581 
  582         sx_assert(&proctree_lock, SX_XLOCKED);
  583         savepgrp = p->p_pgrp;
  584         PGRP_LOCK(savepgrp);
  585         PROC_LOCK(p);
  586         LIST_REMOVE(p, p_pglist);
  587         p->p_pgrp = NULL;
  588         PROC_UNLOCK(p);
  589         PGRP_UNLOCK(savepgrp);
  590         if (LIST_EMPTY(&savepgrp->pg_members))
  591                 pgdelete(savepgrp);
  592         return (0);
  593 }
  594 
  595 /*
  596  * delete a process group
  597  */
  598 static void
  599 pgdelete(pgrp)
  600         register struct pgrp *pgrp;
  601 {
  602         struct session *savesess;
  603         struct tty *tp;
  604 
  605         sx_assert(&proctree_lock, SX_XLOCKED);
  606         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  607         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  608 
  609         /*
  610          * Reset any sigio structures pointing to us as a result of
  611          * F_SETOWN with our pgid.
  612          */
  613         funsetownlst(&pgrp->pg_sigiolst);
  614 
  615         PGRP_LOCK(pgrp);
  616         tp = pgrp->pg_session->s_ttyp;
  617         LIST_REMOVE(pgrp, pg_hash);
  618         savesess = pgrp->pg_session;
  619         PGRP_UNLOCK(pgrp);
  620 
  621         /* Remove the reference to the pgrp before deallocating it. */
  622         if (tp != NULL) {
  623                 tty_lock(tp);
  624                 tty_rel_pgrp(tp, pgrp);
  625         }
  626 
  627         mtx_destroy(&pgrp->pg_mtx);
  628         free(pgrp, M_PGRP);
  629         sess_release(savesess);
  630 }
  631 
  632 static void
  633 pgadjustjobc(pgrp, entering)
  634         struct pgrp *pgrp;
  635         int entering;
  636 {
  637 
  638         PGRP_LOCK(pgrp);
  639         if (entering)
  640                 pgrp->pg_jobc++;
  641         else {
  642                 --pgrp->pg_jobc;
  643                 if (pgrp->pg_jobc == 0)
  644                         orphanpg(pgrp);
  645         }
  646         PGRP_UNLOCK(pgrp);
  647 }
  648 
  649 /*
  650  * Adjust pgrp jobc counters when specified process changes process group.
  651  * We count the number of processes in each process group that "qualify"
  652  * the group for terminal job control (those with a parent in a different
  653  * process group of the same session).  If that count reaches zero, the
  654  * process group becomes orphaned.  Check both the specified process'
  655  * process group and that of its children.
  656  * entering == 0 => p is leaving specified group.
  657  * entering == 1 => p is entering specified group.
  658  */
  659 void
  660 fixjobc(p, pgrp, entering)
  661         register struct proc *p;
  662         register struct pgrp *pgrp;
  663         int entering;
  664 {
  665         register struct pgrp *hispgrp;
  666         register struct session *mysession;
  667 
  668         sx_assert(&proctree_lock, SX_LOCKED);
  669         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  670         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
  671         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
  672 
  673         /*
  674          * Check p's parent to see whether p qualifies its own process
  675          * group; if so, adjust count for p's process group.
  676          */
  677         mysession = pgrp->pg_session;
  678         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
  679             hispgrp->pg_session == mysession)
  680                 pgadjustjobc(pgrp, entering);
  681 
  682         /*
  683          * Check this process' children to see whether they qualify
  684          * their process groups; if so, adjust counts for children's
  685          * process groups.
  686          */
  687         LIST_FOREACH(p, &p->p_children, p_sibling) {
  688                 hispgrp = p->p_pgrp;
  689                 if (hispgrp == pgrp ||
  690                     hispgrp->pg_session != mysession)
  691                         continue;
  692                 PROC_LOCK(p);
  693                 if (p->p_state == PRS_ZOMBIE) {
  694                         PROC_UNLOCK(p);
  695                         continue;
  696                 }
  697                 PROC_UNLOCK(p);
  698                 pgadjustjobc(hispgrp, entering);
  699         }
  700 }
  701 
  702 /*
  703  * A process group has become orphaned;
  704  * if there are any stopped processes in the group,
  705  * hang-up all process in that group.
  706  */
  707 static void
  708 orphanpg(pg)
  709         struct pgrp *pg;
  710 {
  711         register struct proc *p;
  712 
  713         PGRP_LOCK_ASSERT(pg, MA_OWNED);
  714 
  715         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  716                 PROC_LOCK(p);
  717                 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
  718                         PROC_UNLOCK(p);
  719                         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  720                                 PROC_LOCK(p);
  721                                 kern_psignal(p, SIGHUP);
  722                                 kern_psignal(p, SIGCONT);
  723                                 PROC_UNLOCK(p);
  724                         }
  725                         return;
  726                 }
  727                 PROC_UNLOCK(p);
  728         }
  729 }
  730 
  731 void
  732 sess_hold(struct session *s)
  733 {
  734 
  735         refcount_acquire(&s->s_count);
  736 }
  737 
  738 void
  739 sess_release(struct session *s)
  740 {
  741 
  742         if (refcount_release(&s->s_count)) {
  743                 if (s->s_ttyp != NULL) {
  744                         tty_lock(s->s_ttyp);
  745                         tty_rel_sess(s->s_ttyp, s);
  746                 }
  747                 mtx_destroy(&s->s_mtx);
  748                 free(s, M_SESSION);
  749         }
  750 }
  751 
  752 #ifdef DDB
  753 
  754 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
  755 {
  756         register struct pgrp *pgrp;
  757         register struct proc *p;
  758         register int i;
  759 
  760         for (i = 0; i <= pgrphash; i++) {
  761                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
  762                         printf("\tindx %d\n", i);
  763                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
  764                                 printf(
  765                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
  766                                     (void *)pgrp, (long)pgrp->pg_id,
  767                                     (void *)pgrp->pg_session,
  768                                     pgrp->pg_session->s_count,
  769                                     (void *)LIST_FIRST(&pgrp->pg_members));
  770                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
  771                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
  772                                             (long)p->p_pid, (void *)p,
  773                                             (void *)p->p_pgrp);
  774                                 }
  775                         }
  776                 }
  777         }
  778 }
  779 #endif /* DDB */
  780 
  781 /*
  782  * Calculate the kinfo_proc members which contain process-wide
  783  * informations.
  784  * Must be called with the target process locked.
  785  */
  786 static void
  787 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
  788 {
  789         struct thread *td;
  790 
  791         PROC_LOCK_ASSERT(p, MA_OWNED);
  792 
  793         kp->ki_estcpu = 0;
  794         kp->ki_pctcpu = 0;
  795         FOREACH_THREAD_IN_PROC(p, td) {
  796                 thread_lock(td);
  797                 kp->ki_pctcpu += sched_pctcpu(td);
  798                 kp->ki_estcpu += td->td_estcpu;
  799                 thread_unlock(td);
  800         }
  801 }
  802 
  803 /*
  804  * Clear kinfo_proc and fill in any information that is common
  805  * to all threads in the process.
  806  * Must be called with the target process locked.
  807  */
  808 static void
  809 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
  810 {
  811         struct thread *td0;
  812         struct tty *tp;
  813         struct session *sp;
  814         struct ucred *cred;
  815         struct sigacts *ps;
  816 
  817         PROC_LOCK_ASSERT(p, MA_OWNED);
  818         bzero(kp, sizeof(*kp));
  819 
  820         kp->ki_structsize = sizeof(*kp);
  821         kp->ki_paddr = p;
  822         kp->ki_addr =/* p->p_addr; */0; /* XXX */
  823         kp->ki_args = p->p_args;
  824         kp->ki_textvp = p->p_textvp;
  825 #ifdef KTRACE
  826         kp->ki_tracep = p->p_tracevp;
  827         kp->ki_traceflag = p->p_traceflag;
  828 #endif
  829         kp->ki_fd = p->p_fd;
  830         kp->ki_vmspace = p->p_vmspace;
  831         kp->ki_flag = p->p_flag;
  832         kp->ki_flag2 = p->p_flag2;
  833         cred = p->p_ucred;
  834         if (cred) {
  835                 kp->ki_uid = cred->cr_uid;
  836                 kp->ki_ruid = cred->cr_ruid;
  837                 kp->ki_svuid = cred->cr_svuid;
  838                 kp->ki_cr_flags = 0;
  839                 if (cred->cr_flags & CRED_FLAG_CAPMODE)
  840                         kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
  841                 /* XXX bde doesn't like KI_NGROUPS */
  842                 if (cred->cr_ngroups > KI_NGROUPS) {
  843                         kp->ki_ngroups = KI_NGROUPS;
  844                         kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
  845                 } else
  846                         kp->ki_ngroups = cred->cr_ngroups;
  847                 bcopy(cred->cr_groups, kp->ki_groups,
  848                     kp->ki_ngroups * sizeof(gid_t));
  849                 kp->ki_rgid = cred->cr_rgid;
  850                 kp->ki_svgid = cred->cr_svgid;
  851                 /* If jailed(cred), emulate the old P_JAILED flag. */
  852                 if (jailed(cred)) {
  853                         kp->ki_flag |= P_JAILED;
  854                         /* If inside the jail, use 0 as a jail ID. */
  855                         if (cred->cr_prison != curthread->td_ucred->cr_prison)
  856                                 kp->ki_jid = cred->cr_prison->pr_id;
  857                 }
  858                 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
  859                     sizeof(kp->ki_loginclass));
  860         }
  861         ps = p->p_sigacts;
  862         if (ps) {
  863                 mtx_lock(&ps->ps_mtx);
  864                 kp->ki_sigignore = ps->ps_sigignore;
  865                 kp->ki_sigcatch = ps->ps_sigcatch;
  866                 mtx_unlock(&ps->ps_mtx);
  867         }
  868         if (p->p_state != PRS_NEW &&
  869             p->p_state != PRS_ZOMBIE &&
  870             p->p_vmspace != NULL) {
  871                 struct vmspace *vm = p->p_vmspace;
  872 
  873                 kp->ki_size = vm->vm_map.size;
  874                 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
  875                 FOREACH_THREAD_IN_PROC(p, td0) {
  876                         if (!TD_IS_SWAPPED(td0))
  877                                 kp->ki_rssize += td0->td_kstack_pages;
  878                 }
  879                 kp->ki_swrss = vm->vm_swrss;
  880                 kp->ki_tsize = vm->vm_tsize;
  881                 kp->ki_dsize = vm->vm_dsize;
  882                 kp->ki_ssize = vm->vm_ssize;
  883         } else if (p->p_state == PRS_ZOMBIE)
  884                 kp->ki_stat = SZOMB;
  885         if (kp->ki_flag & P_INMEM)
  886                 kp->ki_sflag = PS_INMEM;
  887         else
  888                 kp->ki_sflag = 0;
  889         /* Calculate legacy swtime as seconds since 'swtick'. */
  890         kp->ki_swtime = (ticks - p->p_swtick) / hz;
  891         kp->ki_pid = p->p_pid;
  892         kp->ki_nice = p->p_nice;
  893         kp->ki_fibnum = p->p_fibnum;
  894         kp->ki_start = p->p_stats->p_start;
  895         timevaladd(&kp->ki_start, &boottime);
  896         PROC_STATLOCK(p);
  897         rufetch(p, &kp->ki_rusage);
  898         kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
  899         calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
  900         PROC_STATUNLOCK(p);
  901         calccru(p, &kp->ki_childutime, &kp->ki_childstime);
  902         /* Some callers want child times in a single value. */
  903         kp->ki_childtime = kp->ki_childstime;
  904         timevaladd(&kp->ki_childtime, &kp->ki_childutime);
  905 
  906         FOREACH_THREAD_IN_PROC(p, td0)
  907                 kp->ki_cow += td0->td_cow;
  908 
  909         tp = NULL;
  910         if (p->p_pgrp) {
  911                 kp->ki_pgid = p->p_pgrp->pg_id;
  912                 kp->ki_jobc = p->p_pgrp->pg_jobc;
  913                 sp = p->p_pgrp->pg_session;
  914 
  915                 if (sp != NULL) {
  916                         kp->ki_sid = sp->s_sid;
  917                         SESS_LOCK(sp);
  918                         strlcpy(kp->ki_login, sp->s_login,
  919                             sizeof(kp->ki_login));
  920                         if (sp->s_ttyvp)
  921                                 kp->ki_kiflag |= KI_CTTY;
  922                         if (SESS_LEADER(p))
  923                                 kp->ki_kiflag |= KI_SLEADER;
  924                         /* XXX proctree_lock */
  925                         tp = sp->s_ttyp;
  926                         SESS_UNLOCK(sp);
  927                 }
  928         }
  929         if ((p->p_flag & P_CONTROLT) && tp != NULL) {
  930                 kp->ki_tdev = tty_udev(tp);
  931                 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
  932                 if (tp->t_session)
  933                         kp->ki_tsid = tp->t_session->s_sid;
  934         } else
  935                 kp->ki_tdev = NODEV;
  936         if (p->p_comm[0] != '\0')
  937                 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
  938         if (p->p_sysent && p->p_sysent->sv_name != NULL &&
  939             p->p_sysent->sv_name[0] != '\0')
  940                 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
  941         kp->ki_siglist = p->p_siglist;
  942         kp->ki_xstat = p->p_xstat;
  943         kp->ki_acflag = p->p_acflag;
  944         kp->ki_lock = p->p_lock;
  945         if (p->p_pptr)
  946                 kp->ki_ppid = p->p_pptr->p_pid;
  947 }
  948 
  949 /*
  950  * Fill in information that is thread specific.  Must be called with
  951  * target process locked.  If 'preferthread' is set, overwrite certain
  952  * process-related fields that are maintained for both threads and
  953  * processes.
  954  */
  955 static void
  956 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
  957 {
  958         struct proc *p;
  959 
  960         p = td->td_proc;
  961         kp->ki_tdaddr = td;
  962         PROC_LOCK_ASSERT(p, MA_OWNED);
  963 
  964         if (preferthread)
  965                 PROC_STATLOCK(p);
  966         thread_lock(td);
  967         if (td->td_wmesg != NULL)
  968                 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
  969         else
  970                 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
  971         if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
  972             sizeof(kp->ki_tdname)) {
  973                 strlcpy(kp->ki_moretdname,
  974                     td->td_name + sizeof(kp->ki_tdname) - 1,
  975                     sizeof(kp->ki_moretdname));
  976         } else {
  977                 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
  978         }
  979         if (TD_ON_LOCK(td)) {
  980                 kp->ki_kiflag |= KI_LOCKBLOCK;
  981                 strlcpy(kp->ki_lockname, td->td_lockname,
  982                     sizeof(kp->ki_lockname));
  983         } else {
  984                 kp->ki_kiflag &= ~KI_LOCKBLOCK;
  985                 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
  986         }
  987 
  988         if (p->p_state == PRS_NORMAL) { /* approximate. */
  989                 if (TD_ON_RUNQ(td) ||
  990                     TD_CAN_RUN(td) ||
  991                     TD_IS_RUNNING(td)) {
  992                         kp->ki_stat = SRUN;
  993                 } else if (P_SHOULDSTOP(p)) {
  994                         kp->ki_stat = SSTOP;
  995                 } else if (TD_IS_SLEEPING(td)) {
  996                         kp->ki_stat = SSLEEP;
  997                 } else if (TD_ON_LOCK(td)) {
  998                         kp->ki_stat = SLOCK;
  999                 } else {
 1000                         kp->ki_stat = SWAIT;
 1001                 }
 1002         } else if (p->p_state == PRS_ZOMBIE) {
 1003                 kp->ki_stat = SZOMB;
 1004         } else {
 1005                 kp->ki_stat = SIDL;
 1006         }
 1007 
 1008         /* Things in the thread */
 1009         kp->ki_wchan = td->td_wchan;
 1010         kp->ki_pri.pri_level = td->td_priority;
 1011         kp->ki_pri.pri_native = td->td_base_pri;
 1012         kp->ki_lastcpu = td->td_lastcpu;
 1013         kp->ki_oncpu = td->td_oncpu;
 1014         kp->ki_tdflags = td->td_flags;
 1015         kp->ki_tid = td->td_tid;
 1016         kp->ki_numthreads = p->p_numthreads;
 1017         kp->ki_pcb = td->td_pcb;
 1018         kp->ki_kstack = (void *)td->td_kstack;
 1019         kp->ki_slptime = (ticks - td->td_slptick) / hz;
 1020         kp->ki_pri.pri_class = td->td_pri_class;
 1021         kp->ki_pri.pri_user = td->td_user_pri;
 1022 
 1023         if (preferthread) {
 1024                 rufetchtd(td, &kp->ki_rusage);
 1025                 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
 1026                 kp->ki_pctcpu = sched_pctcpu(td);
 1027                 kp->ki_estcpu = td->td_estcpu;
 1028                 kp->ki_cow = td->td_cow;
 1029         }
 1030 
 1031         /* We can't get this anymore but ps etc never used it anyway. */
 1032         kp->ki_rqindex = 0;
 1033 
 1034         if (preferthread)
 1035                 kp->ki_siglist = td->td_siglist;
 1036         kp->ki_sigmask = td->td_sigmask;
 1037         thread_unlock(td);
 1038         if (preferthread)
 1039                 PROC_STATUNLOCK(p);
 1040 }
 1041 
 1042 /*
 1043  * Fill in a kinfo_proc structure for the specified process.
 1044  * Must be called with the target process locked.
 1045  */
 1046 void
 1047 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
 1048 {
 1049 
 1050         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
 1051 
 1052         fill_kinfo_proc_only(p, kp);
 1053         fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
 1054         fill_kinfo_aggregate(p, kp);
 1055 }
 1056 
 1057 struct pstats *
 1058 pstats_alloc(void)
 1059 {
 1060 
 1061         return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
 1062 }
 1063 
 1064 /*
 1065  * Copy parts of p_stats; zero the rest of p_stats (statistics).
 1066  */
 1067 void
 1068 pstats_fork(struct pstats *src, struct pstats *dst)
 1069 {
 1070 
 1071         bzero(&dst->pstat_startzero,
 1072             __rangeof(struct pstats, pstat_startzero, pstat_endzero));
 1073         bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
 1074             __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
 1075 }
 1076 
 1077 void
 1078 pstats_free(struct pstats *ps)
 1079 {
 1080 
 1081         free(ps, M_SUBPROC);
 1082 }
 1083 
 1084 static struct proc *
 1085 zpfind_locked(pid_t pid)
 1086 {
 1087         struct proc *p;
 1088 
 1089         sx_assert(&allproc_lock, SX_LOCKED);
 1090         LIST_FOREACH(p, &zombproc, p_list) {
 1091                 if (p->p_pid == pid) {
 1092                         PROC_LOCK(p);
 1093                         break;
 1094                 }
 1095         }
 1096         return (p);
 1097 }
 1098 
 1099 /*
 1100  * Locate a zombie process by number
 1101  */
 1102 struct proc *
 1103 zpfind(pid_t pid)
 1104 {
 1105         struct proc *p;
 1106 
 1107         sx_slock(&allproc_lock);
 1108         p = zpfind_locked(pid);
 1109         sx_sunlock(&allproc_lock);
 1110         return (p);
 1111 }
 1112 
 1113 #ifdef COMPAT_FREEBSD32
 1114 
 1115 /*
 1116  * This function is typically used to copy out the kernel address, so
 1117  * it can be replaced by assignment of zero.
 1118  */
 1119 static inline uint32_t
 1120 ptr32_trim(void *ptr)
 1121 {
 1122         uintptr_t uptr;
 1123 
 1124         uptr = (uintptr_t)ptr;
 1125         return ((uptr > UINT_MAX) ? 0 : uptr);
 1126 }
 1127 
 1128 #define PTRTRIM_CP(src,dst,fld) \
 1129         do { (dst).fld = ptr32_trim((src).fld); } while (0)
 1130 
 1131 static void
 1132 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
 1133 {
 1134         int i;
 1135 
 1136         bzero(ki32, sizeof(struct kinfo_proc32));
 1137         ki32->ki_structsize = sizeof(struct kinfo_proc32);
 1138         CP(*ki, *ki32, ki_layout);
 1139         PTRTRIM_CP(*ki, *ki32, ki_args);
 1140         PTRTRIM_CP(*ki, *ki32, ki_paddr);
 1141         PTRTRIM_CP(*ki, *ki32, ki_addr);
 1142         PTRTRIM_CP(*ki, *ki32, ki_tracep);
 1143         PTRTRIM_CP(*ki, *ki32, ki_textvp);
 1144         PTRTRIM_CP(*ki, *ki32, ki_fd);
 1145         PTRTRIM_CP(*ki, *ki32, ki_vmspace);
 1146         PTRTRIM_CP(*ki, *ki32, ki_wchan);
 1147         CP(*ki, *ki32, ki_pid);
 1148         CP(*ki, *ki32, ki_ppid);
 1149         CP(*ki, *ki32, ki_pgid);
 1150         CP(*ki, *ki32, ki_tpgid);
 1151         CP(*ki, *ki32, ki_sid);
 1152         CP(*ki, *ki32, ki_tsid);
 1153         CP(*ki, *ki32, ki_jobc);
 1154         CP(*ki, *ki32, ki_tdev);
 1155         CP(*ki, *ki32, ki_siglist);
 1156         CP(*ki, *ki32, ki_sigmask);
 1157         CP(*ki, *ki32, ki_sigignore);
 1158         CP(*ki, *ki32, ki_sigcatch);
 1159         CP(*ki, *ki32, ki_uid);
 1160         CP(*ki, *ki32, ki_ruid);
 1161         CP(*ki, *ki32, ki_svuid);
 1162         CP(*ki, *ki32, ki_rgid);
 1163         CP(*ki, *ki32, ki_svgid);
 1164         CP(*ki, *ki32, ki_ngroups);
 1165         for (i = 0; i < KI_NGROUPS; i++)
 1166                 CP(*ki, *ki32, ki_groups[i]);
 1167         CP(*ki, *ki32, ki_size);
 1168         CP(*ki, *ki32, ki_rssize);
 1169         CP(*ki, *ki32, ki_swrss);
 1170         CP(*ki, *ki32, ki_tsize);
 1171         CP(*ki, *ki32, ki_dsize);
 1172         CP(*ki, *ki32, ki_ssize);
 1173         CP(*ki, *ki32, ki_xstat);
 1174         CP(*ki, *ki32, ki_acflag);
 1175         CP(*ki, *ki32, ki_pctcpu);
 1176         CP(*ki, *ki32, ki_estcpu);
 1177         CP(*ki, *ki32, ki_slptime);
 1178         CP(*ki, *ki32, ki_swtime);
 1179         CP(*ki, *ki32, ki_cow);
 1180         CP(*ki, *ki32, ki_runtime);
 1181         TV_CP(*ki, *ki32, ki_start);
 1182         TV_CP(*ki, *ki32, ki_childtime);
 1183         CP(*ki, *ki32, ki_flag);
 1184         CP(*ki, *ki32, ki_kiflag);
 1185         CP(*ki, *ki32, ki_traceflag);
 1186         CP(*ki, *ki32, ki_stat);
 1187         CP(*ki, *ki32, ki_nice);
 1188         CP(*ki, *ki32, ki_lock);
 1189         CP(*ki, *ki32, ki_rqindex);
 1190         CP(*ki, *ki32, ki_oncpu);
 1191         CP(*ki, *ki32, ki_lastcpu);
 1192         bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
 1193         bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
 1194         bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
 1195         bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
 1196         bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
 1197         bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
 1198         bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
 1199         bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
 1200         CP(*ki, *ki32, ki_flag2);
 1201         CP(*ki, *ki32, ki_fibnum);
 1202         CP(*ki, *ki32, ki_cr_flags);
 1203         CP(*ki, *ki32, ki_jid);
 1204         CP(*ki, *ki32, ki_numthreads);
 1205         CP(*ki, *ki32, ki_tid);
 1206         CP(*ki, *ki32, ki_pri);
 1207         freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
 1208         freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
 1209         PTRTRIM_CP(*ki, *ki32, ki_pcb);
 1210         PTRTRIM_CP(*ki, *ki32, ki_kstack);
 1211         PTRTRIM_CP(*ki, *ki32, ki_udata);
 1212         CP(*ki, *ki32, ki_sflag);
 1213         CP(*ki, *ki32, ki_tdflags);
 1214 }
 1215 #endif
 1216 
 1217 int
 1218 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
 1219 {
 1220         struct thread *td;
 1221         struct kinfo_proc ki;
 1222 #ifdef COMPAT_FREEBSD32
 1223         struct kinfo_proc32 ki32;
 1224 #endif
 1225         int error;
 1226 
 1227         PROC_LOCK_ASSERT(p, MA_OWNED);
 1228         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
 1229 
 1230         error = 0;
 1231         fill_kinfo_proc(p, &ki);
 1232         if ((flags & KERN_PROC_NOTHREADS) != 0) {
 1233 #ifdef COMPAT_FREEBSD32
 1234                 if ((flags & KERN_PROC_MASK32) != 0) {
 1235                         freebsd32_kinfo_proc_out(&ki, &ki32);
 1236                         if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
 1237                                 error = ENOMEM;
 1238                 } else
 1239 #endif
 1240                         if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
 1241                                 error = ENOMEM;
 1242         } else {
 1243                 FOREACH_THREAD_IN_PROC(p, td) {
 1244                         fill_kinfo_thread(td, &ki, 1);
 1245 #ifdef COMPAT_FREEBSD32
 1246                         if ((flags & KERN_PROC_MASK32) != 0) {
 1247                                 freebsd32_kinfo_proc_out(&ki, &ki32);
 1248                                 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
 1249                                         error = ENOMEM;
 1250                         } else
 1251 #endif
 1252                                 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
 1253                                         error = ENOMEM;
 1254                         if (error != 0)
 1255                                 break;
 1256                 }
 1257         }
 1258         PROC_UNLOCK(p);
 1259         return (error);
 1260 }
 1261 
 1262 static int
 1263 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
 1264     int doingzomb)
 1265 {
 1266         struct sbuf sb;
 1267         struct kinfo_proc ki;
 1268         struct proc *np;
 1269         int error, error2;
 1270         pid_t pid;
 1271 
 1272         pid = p->p_pid;
 1273         sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
 1274         error = kern_proc_out(p, &sb, flags);
 1275         error2 = sbuf_finish(&sb);
 1276         sbuf_delete(&sb);
 1277         if (error != 0)
 1278                 return (error);
 1279         else if (error2 != 0)
 1280                 return (error2);
 1281         if (doingzomb)
 1282                 np = zpfind(pid);
 1283         else {
 1284                 if (pid == 0)
 1285                         return (0);
 1286                 np = pfind(pid);
 1287         }
 1288         if (np == NULL)
 1289                 return (ESRCH);
 1290         if (np != p) {
 1291                 PROC_UNLOCK(np);
 1292                 return (ESRCH);
 1293         }
 1294         PROC_UNLOCK(np);
 1295         return (0);
 1296 }
 1297 
 1298 static int
 1299 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
 1300 {
 1301         int *name = (int *)arg1;
 1302         u_int namelen = arg2;
 1303         struct proc *p;
 1304         int flags, doingzomb, oid_number;
 1305         int error = 0;
 1306 
 1307         oid_number = oidp->oid_number;
 1308         if (oid_number != KERN_PROC_ALL &&
 1309             (oid_number & KERN_PROC_INC_THREAD) == 0)
 1310                 flags = KERN_PROC_NOTHREADS;
 1311         else {
 1312                 flags = 0;
 1313                 oid_number &= ~KERN_PROC_INC_THREAD;
 1314         }
 1315 #ifdef COMPAT_FREEBSD32
 1316         if (req->flags & SCTL_MASK32)
 1317                 flags |= KERN_PROC_MASK32;
 1318 #endif
 1319         if (oid_number == KERN_PROC_PID) {
 1320                 if (namelen != 1)
 1321                         return (EINVAL);
 1322                 error = sysctl_wire_old_buffer(req, 0);
 1323                 if (error)
 1324                         return (error);
 1325                 error = pget((pid_t)name[0], PGET_CANSEE, &p);
 1326                 if (error != 0)
 1327                         return (error);
 1328                 error = sysctl_out_proc(p, req, flags, 0);
 1329                 return (error);
 1330         }
 1331 
 1332         switch (oid_number) {
 1333         case KERN_PROC_ALL:
 1334                 if (namelen != 0)
 1335                         return (EINVAL);
 1336                 break;
 1337         case KERN_PROC_PROC:
 1338                 if (namelen != 0 && namelen != 1)
 1339                         return (EINVAL);
 1340                 break;
 1341         default:
 1342                 if (namelen != 1)
 1343                         return (EINVAL);
 1344                 break;
 1345         }
 1346 
 1347         if (!req->oldptr) {
 1348                 /* overestimate by 5 procs */
 1349                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
 1350                 if (error)
 1351                         return (error);
 1352         }
 1353         error = sysctl_wire_old_buffer(req, 0);
 1354         if (error != 0)
 1355                 return (error);
 1356         sx_slock(&allproc_lock);
 1357         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
 1358                 if (!doingzomb)
 1359                         p = LIST_FIRST(&allproc);
 1360                 else
 1361                         p = LIST_FIRST(&zombproc);
 1362                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
 1363                         /*
 1364                          * Skip embryonic processes.
 1365                          */
 1366                         PROC_LOCK(p);
 1367                         if (p->p_state == PRS_NEW) {
 1368                                 PROC_UNLOCK(p);
 1369                                 continue;
 1370                         }
 1371                         KASSERT(p->p_ucred != NULL,
 1372                             ("process credential is NULL for non-NEW proc"));
 1373                         /*
 1374                          * Show a user only appropriate processes.
 1375                          */
 1376                         if (p_cansee(curthread, p)) {
 1377                                 PROC_UNLOCK(p);
 1378                                 continue;
 1379                         }
 1380                         /*
 1381                          * TODO - make more efficient (see notes below).
 1382                          * do by session.
 1383                          */
 1384                         switch (oid_number) {
 1385 
 1386                         case KERN_PROC_GID:
 1387                                 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
 1388                                         PROC_UNLOCK(p);
 1389                                         continue;
 1390                                 }
 1391                                 break;
 1392 
 1393                         case KERN_PROC_PGRP:
 1394                                 /* could do this by traversing pgrp */
 1395                                 if (p->p_pgrp == NULL ||
 1396                                     p->p_pgrp->pg_id != (pid_t)name[0]) {
 1397                                         PROC_UNLOCK(p);
 1398                                         continue;
 1399                                 }
 1400                                 break;
 1401 
 1402                         case KERN_PROC_RGID:
 1403                                 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
 1404                                         PROC_UNLOCK(p);
 1405                                         continue;
 1406                                 }
 1407                                 break;
 1408 
 1409                         case KERN_PROC_SESSION:
 1410                                 if (p->p_session == NULL ||
 1411                                     p->p_session->s_sid != (pid_t)name[0]) {
 1412                                         PROC_UNLOCK(p);
 1413                                         continue;
 1414                                 }
 1415                                 break;
 1416 
 1417                         case KERN_PROC_TTY:
 1418                                 if ((p->p_flag & P_CONTROLT) == 0 ||
 1419                                     p->p_session == NULL) {
 1420                                         PROC_UNLOCK(p);
 1421                                         continue;
 1422                                 }
 1423                                 /* XXX proctree_lock */
 1424                                 SESS_LOCK(p->p_session);
 1425                                 if (p->p_session->s_ttyp == NULL ||
 1426                                     tty_udev(p->p_session->s_ttyp) !=
 1427                                     (dev_t)name[0]) {
 1428                                         SESS_UNLOCK(p->p_session);
 1429                                         PROC_UNLOCK(p);
 1430                                         continue;
 1431                                 }
 1432                                 SESS_UNLOCK(p->p_session);
 1433                                 break;
 1434 
 1435                         case KERN_PROC_UID:
 1436                                 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
 1437                                         PROC_UNLOCK(p);
 1438                                         continue;
 1439                                 }
 1440                                 break;
 1441 
 1442                         case KERN_PROC_RUID:
 1443                                 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
 1444                                         PROC_UNLOCK(p);
 1445                                         continue;
 1446                                 }
 1447                                 break;
 1448 
 1449                         case KERN_PROC_PROC:
 1450                                 break;
 1451 
 1452                         default:
 1453                                 break;
 1454 
 1455                         }
 1456 
 1457                         error = sysctl_out_proc(p, req, flags, doingzomb);
 1458                         if (error) {
 1459                                 sx_sunlock(&allproc_lock);
 1460                                 return (error);
 1461                         }
 1462                 }
 1463         }
 1464         sx_sunlock(&allproc_lock);
 1465         return (0);
 1466 }
 1467 
 1468 struct pargs *
 1469 pargs_alloc(int len)
 1470 {
 1471         struct pargs *pa;
 1472 
 1473         pa = malloc(sizeof(struct pargs) + len, M_PARGS,
 1474                 M_WAITOK);
 1475         refcount_init(&pa->ar_ref, 1);
 1476         pa->ar_length = len;
 1477         return (pa);
 1478 }
 1479 
 1480 static void
 1481 pargs_free(struct pargs *pa)
 1482 {
 1483 
 1484         free(pa, M_PARGS);
 1485 }
 1486 
 1487 void
 1488 pargs_hold(struct pargs *pa)
 1489 {
 1490 
 1491         if (pa == NULL)
 1492                 return;
 1493         refcount_acquire(&pa->ar_ref);
 1494 }
 1495 
 1496 void
 1497 pargs_drop(struct pargs *pa)
 1498 {
 1499 
 1500         if (pa == NULL)
 1501                 return;
 1502         if (refcount_release(&pa->ar_ref))
 1503                 pargs_free(pa);
 1504 }
 1505 
 1506 static int
 1507 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
 1508     size_t len)
 1509 {
 1510         struct iovec iov;
 1511         struct uio uio;
 1512 
 1513         iov.iov_base = (caddr_t)buf;
 1514         iov.iov_len = len;
 1515         uio.uio_iov = &iov;
 1516         uio.uio_iovcnt = 1;
 1517         uio.uio_offset = offset;
 1518         uio.uio_resid = (ssize_t)len;
 1519         uio.uio_segflg = UIO_SYSSPACE;
 1520         uio.uio_rw = UIO_READ;
 1521         uio.uio_td = td;
 1522 
 1523         return (proc_rwmem(p, &uio));
 1524 }
 1525 
 1526 static int
 1527 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
 1528     size_t len)
 1529 {
 1530         size_t i;
 1531         int error;
 1532 
 1533         error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
 1534         /*
 1535          * Reading the chunk may validly return EFAULT if the string is shorter
 1536          * than the chunk and is aligned at the end of the page, assuming the
 1537          * next page is not mapped.  So if EFAULT is returned do a fallback to
 1538          * one byte read loop.
 1539          */
 1540         if (error == EFAULT) {
 1541                 for (i = 0; i < len; i++, buf++, sptr++) {
 1542                         error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
 1543                         if (error != 0)
 1544                                 return (error);
 1545                         if (*buf == '\0')
 1546                                 break;
 1547                 }
 1548                 error = 0;
 1549         }
 1550         return (error);
 1551 }
 1552 
 1553 #define PROC_AUXV_MAX   256     /* Safety limit on auxv size. */
 1554 
 1555 enum proc_vector_type {
 1556         PROC_ARG,
 1557         PROC_ENV,
 1558         PROC_AUX,
 1559 };
 1560 
 1561 #ifdef COMPAT_FREEBSD32
 1562 static int
 1563 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
 1564     size_t *vsizep, enum proc_vector_type type)
 1565 {
 1566         struct freebsd32_ps_strings pss;
 1567         Elf32_Auxinfo aux;
 1568         vm_offset_t vptr, ptr;
 1569         uint32_t *proc_vector32;
 1570         char **proc_vector;
 1571         size_t vsize, size;
 1572         int i, error;
 1573 
 1574         error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
 1575             &pss, sizeof(pss));
 1576         if (error != 0)
 1577                 return (error);
 1578         switch (type) {
 1579         case PROC_ARG:
 1580                 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
 1581                 vsize = pss.ps_nargvstr;
 1582                 if (vsize > ARG_MAX)
 1583                         return (ENOEXEC);
 1584                 size = vsize * sizeof(int32_t);
 1585                 break;
 1586         case PROC_ENV:
 1587                 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
 1588                 vsize = pss.ps_nenvstr;
 1589                 if (vsize > ARG_MAX)
 1590                         return (ENOEXEC);
 1591                 size = vsize * sizeof(int32_t);
 1592                 break;
 1593         case PROC_AUX:
 1594                 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
 1595                     (pss.ps_nenvstr + 1) * sizeof(int32_t);
 1596                 if (vptr % 4 != 0)
 1597                         return (ENOEXEC);
 1598                 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
 1599                         error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
 1600                         if (error != 0)
 1601                                 return (error);
 1602                         if (aux.a_type == AT_NULL)
 1603                                 break;
 1604                         ptr += sizeof(aux);
 1605                 }
 1606                 if (aux.a_type != AT_NULL)
 1607                         return (ENOEXEC);
 1608                 vsize = i + 1;
 1609                 size = vsize * sizeof(aux);
 1610                 break;
 1611         default:
 1612                 KASSERT(0, ("Wrong proc vector type: %d", type));
 1613                 return (EINVAL);
 1614         }
 1615         proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
 1616         error = proc_read_mem(td, p, vptr, proc_vector32, size);
 1617         if (error != 0)
 1618                 goto done;
 1619         if (type == PROC_AUX) {
 1620                 *proc_vectorp = (char **)proc_vector32;
 1621                 *vsizep = vsize;
 1622                 return (0);
 1623         }
 1624         proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
 1625         for (i = 0; i < (int)vsize; i++)
 1626                 proc_vector[i] = PTRIN(proc_vector32[i]);
 1627         *proc_vectorp = proc_vector;
 1628         *vsizep = vsize;
 1629 done:
 1630         free(proc_vector32, M_TEMP);
 1631         return (error);
 1632 }
 1633 #endif
 1634 
 1635 static int
 1636 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
 1637     size_t *vsizep, enum proc_vector_type type)
 1638 {
 1639         struct ps_strings pss;
 1640         Elf_Auxinfo aux;
 1641         vm_offset_t vptr, ptr;
 1642         char **proc_vector;
 1643         size_t vsize, size;
 1644         int error, i;
 1645 
 1646 #ifdef COMPAT_FREEBSD32
 1647         if (SV_PROC_FLAG(p, SV_ILP32) != 0)
 1648                 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
 1649 #endif
 1650         error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
 1651             &pss, sizeof(pss));
 1652         if (error != 0)
 1653                 return (error);
 1654         switch (type) {
 1655         case PROC_ARG:
 1656                 vptr = (vm_offset_t)pss.ps_argvstr;
 1657                 vsize = pss.ps_nargvstr;
 1658                 if (vsize > ARG_MAX)
 1659                         return (ENOEXEC);
 1660                 size = vsize * sizeof(char *);
 1661                 break;
 1662         case PROC_ENV:
 1663                 vptr = (vm_offset_t)pss.ps_envstr;
 1664                 vsize = pss.ps_nenvstr;
 1665                 if (vsize > ARG_MAX)
 1666                         return (ENOEXEC);
 1667                 size = vsize * sizeof(char *);
 1668                 break;
 1669         case PROC_AUX:
 1670                 /*
 1671                  * The aux array is just above env array on the stack. Check
 1672                  * that the address is naturally aligned.
 1673                  */
 1674                 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
 1675                     * sizeof(char *);
 1676 #if __ELF_WORD_SIZE == 64
 1677                 if (vptr % sizeof(uint64_t) != 0)
 1678 #else
 1679                 if (vptr % sizeof(uint32_t) != 0)
 1680 #endif
 1681                         return (ENOEXEC);
 1682                 /*
 1683                  * We count the array size reading the aux vectors from the
 1684                  * stack until AT_NULL vector is returned.  So (to keep the code
 1685                  * simple) we read the process stack twice: the first time here
 1686                  * to find the size and the second time when copying the vectors
 1687                  * to the allocated proc_vector.
 1688                  */
 1689                 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
 1690                         error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
 1691                         if (error != 0)
 1692                                 return (error);
 1693                         if (aux.a_type == AT_NULL)
 1694                                 break;
 1695                         ptr += sizeof(aux);
 1696                 }
 1697                 /*
 1698                  * If the PROC_AUXV_MAX entries are iterated over, and we have
 1699                  * not reached AT_NULL, it is most likely we are reading wrong
 1700                  * data: either the process doesn't have auxv array or data has
 1701                  * been modified. Return the error in this case.
 1702                  */
 1703                 if (aux.a_type != AT_NULL)
 1704                         return (ENOEXEC);
 1705                 vsize = i + 1;
 1706                 size = vsize * sizeof(aux);
 1707                 break;
 1708         default:
 1709                 KASSERT(0, ("Wrong proc vector type: %d", type));
 1710                 return (EINVAL); /* In case we are built without INVARIANTS. */
 1711         }
 1712         proc_vector = malloc(size, M_TEMP, M_WAITOK);
 1713         if (proc_vector == NULL)
 1714                 return (ENOMEM);
 1715         error = proc_read_mem(td, p, vptr, proc_vector, size);
 1716         if (error != 0) {
 1717                 free(proc_vector, M_TEMP);
 1718                 return (error);
 1719         }
 1720         *proc_vectorp = proc_vector;
 1721         *vsizep = vsize;
 1722 
 1723         return (0);
 1724 }
 1725 
 1726 #define GET_PS_STRINGS_CHUNK_SZ 256     /* Chunk size (bytes) for ps_strings operations. */
 1727 
 1728 static int
 1729 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
 1730     enum proc_vector_type type)
 1731 {
 1732         size_t done, len, nchr, vsize;
 1733         int error, i;
 1734         char **proc_vector, *sptr;
 1735         char pss_string[GET_PS_STRINGS_CHUNK_SZ];
 1736 
 1737         PROC_ASSERT_HELD(p);
 1738 
 1739         /*
 1740          * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
 1741          */
 1742         nchr = 2 * (PATH_MAX + ARG_MAX);
 1743 
 1744         error = get_proc_vector(td, p, &proc_vector, &vsize, type);
 1745         if (error != 0)
 1746                 return (error);
 1747         for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
 1748                 /*
 1749                  * The program may have scribbled into its argv array, e.g. to
 1750                  * remove some arguments.  If that has happened, break out
 1751                  * before trying to read from NULL.
 1752                  */
 1753                 if (proc_vector[i] == NULL)
 1754                         break;
 1755                 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
 1756                         error = proc_read_string(td, p, sptr, pss_string,
 1757                             sizeof(pss_string));
 1758                         if (error != 0)
 1759                                 goto done;
 1760                         len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
 1761                         if (done + len >= nchr)
 1762                                 len = nchr - done - 1;
 1763                         sbuf_bcat(sb, pss_string, len);
 1764                         if (len != GET_PS_STRINGS_CHUNK_SZ)
 1765                                 break;
 1766                         done += GET_PS_STRINGS_CHUNK_SZ;
 1767                 }
 1768                 sbuf_bcat(sb, "", 1);
 1769                 done += len + 1;
 1770         }
 1771 done:
 1772         free(proc_vector, M_TEMP);
 1773         return (error);
 1774 }
 1775 
 1776 int
 1777 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
 1778 {
 1779 
 1780         return (get_ps_strings(curthread, p, sb, PROC_ARG));
 1781 }
 1782 
 1783 int
 1784 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
 1785 {
 1786 
 1787         return (get_ps_strings(curthread, p, sb, PROC_ENV));
 1788 }
 1789 
 1790 int
 1791 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
 1792 {
 1793         size_t vsize, size;
 1794         char **auxv;
 1795         int error;
 1796 
 1797         error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
 1798         if (error == 0) {
 1799 #ifdef COMPAT_FREEBSD32
 1800                 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
 1801                         size = vsize * sizeof(Elf32_Auxinfo);
 1802                 else
 1803 #endif
 1804                         size = vsize * sizeof(Elf_Auxinfo);
 1805                 if (sbuf_bcat(sb, auxv, size) != 0)
 1806                         error = ENOMEM;
 1807                 free(auxv, M_TEMP);
 1808         }
 1809         return (error);
 1810 }
 1811 
 1812 /*
 1813  * This sysctl allows a process to retrieve the argument list or process
 1814  * title for another process without groping around in the address space
 1815  * of the other process.  It also allow a process to set its own "process 
 1816  * title to a string of its own choice.
 1817  */
 1818 static int
 1819 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 1820 {
 1821         int *name = (int *)arg1;
 1822         u_int namelen = arg2;
 1823         struct pargs *newpa, *pa;
 1824         struct proc *p;
 1825         struct sbuf sb;
 1826         int flags, error = 0, error2;
 1827 
 1828         if (namelen != 1)
 1829                 return (EINVAL);
 1830 
 1831         flags = PGET_CANSEE;
 1832         if (req->newptr != NULL)
 1833                 flags |= PGET_ISCURRENT;
 1834         error = pget((pid_t)name[0], flags, &p);
 1835         if (error)
 1836                 return (error);
 1837 
 1838         pa = p->p_args;
 1839         if (pa != NULL) {
 1840                 pargs_hold(pa);
 1841                 PROC_UNLOCK(p);
 1842                 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
 1843                 pargs_drop(pa);
 1844         } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
 1845                 _PHOLD(p);
 1846                 PROC_UNLOCK(p);
 1847                 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
 1848                 error = proc_getargv(curthread, p, &sb);
 1849                 error2 = sbuf_finish(&sb);
 1850                 PRELE(p);
 1851                 sbuf_delete(&sb);
 1852                 if (error == 0 && error2 != 0)
 1853                         error = error2;
 1854         } else {
 1855                 PROC_UNLOCK(p);
 1856         }
 1857         if (error != 0 || req->newptr == NULL)
 1858                 return (error);
 1859 
 1860         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
 1861                 return (ENOMEM);
 1862         newpa = pargs_alloc(req->newlen);
 1863         error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
 1864         if (error != 0) {
 1865                 pargs_free(newpa);
 1866                 return (error);
 1867         }
 1868         PROC_LOCK(p);
 1869         pa = p->p_args;
 1870         p->p_args = newpa;
 1871         PROC_UNLOCK(p);
 1872         pargs_drop(pa);
 1873         return (0);
 1874 }
 1875 
 1876 /*
 1877  * This sysctl allows a process to retrieve environment of another process.
 1878  */
 1879 static int
 1880 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
 1881 {
 1882         int *name = (int *)arg1;
 1883         u_int namelen = arg2;
 1884         struct proc *p;
 1885         struct sbuf sb;
 1886         int error, error2;
 1887 
 1888         if (namelen != 1)
 1889                 return (EINVAL);
 1890 
 1891         error = pget((pid_t)name[0], PGET_WANTREAD, &p);
 1892         if (error != 0)
 1893                 return (error);
 1894         if ((p->p_flag & P_SYSTEM) != 0) {
 1895                 PRELE(p);
 1896                 return (0);
 1897         }
 1898 
 1899         sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
 1900         error = proc_getenvv(curthread, p, &sb);
 1901         error2 = sbuf_finish(&sb);
 1902         PRELE(p);
 1903         sbuf_delete(&sb);
 1904         return (error != 0 ? error : error2);
 1905 }
 1906 
 1907 /*
 1908  * This sysctl allows a process to retrieve ELF auxiliary vector of
 1909  * another process.
 1910  */
 1911 static int
 1912 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
 1913 {
 1914         int *name = (int *)arg1;
 1915         u_int namelen = arg2;
 1916         struct proc *p;
 1917         struct sbuf sb;
 1918         int error, error2;
 1919 
 1920         if (namelen != 1)
 1921                 return (EINVAL);
 1922 
 1923         error = pget((pid_t)name[0], PGET_WANTREAD, &p);
 1924         if (error != 0)
 1925                 return (error);
 1926         if ((p->p_flag & P_SYSTEM) != 0) {
 1927                 PRELE(p);
 1928                 return (0);
 1929         }
 1930         sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
 1931         error = proc_getauxv(curthread, p, &sb);
 1932         error2 = sbuf_finish(&sb);
 1933         PRELE(p);
 1934         sbuf_delete(&sb);
 1935         return (error != 0 ? error : error2);
 1936 }
 1937 
 1938 /*
 1939  * This sysctl allows a process to retrieve the path of the executable for
 1940  * itself or another process.
 1941  */
 1942 static int
 1943 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
 1944 {
 1945         pid_t *pidp = (pid_t *)arg1;
 1946         unsigned int arglen = arg2;
 1947         struct proc *p;
 1948         struct vnode *vp;
 1949         char *retbuf, *freebuf;
 1950         int error;
 1951 
 1952         if (arglen != 1)
 1953                 return (EINVAL);
 1954         if (*pidp == -1) {      /* -1 means this process */
 1955                 p = req->td->td_proc;
 1956         } else {
 1957                 error = pget(*pidp, PGET_CANSEE, &p);
 1958                 if (error != 0)
 1959                         return (error);
 1960         }
 1961 
 1962         vp = p->p_textvp;
 1963         if (vp == NULL) {
 1964                 if (*pidp != -1)
 1965                         PROC_UNLOCK(p);
 1966                 return (0);
 1967         }
 1968         vref(vp);
 1969         if (*pidp != -1)
 1970                 PROC_UNLOCK(p);
 1971         error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
 1972         vrele(vp);
 1973         if (error)
 1974                 return (error);
 1975         error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
 1976         free(freebuf, M_TEMP);
 1977         return (error);
 1978 }
 1979 
 1980 static int
 1981 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
 1982 {
 1983         struct proc *p;
 1984         char *sv_name;
 1985         int *name;
 1986         int namelen;
 1987         int error;
 1988 
 1989         namelen = arg2;
 1990         if (namelen != 1)
 1991                 return (EINVAL);
 1992 
 1993         name = (int *)arg1;
 1994         error = pget((pid_t)name[0], PGET_CANSEE, &p);
 1995         if (error != 0)
 1996                 return (error);
 1997         sv_name = p->p_sysent->sv_name;
 1998         PROC_UNLOCK(p);
 1999         return (sysctl_handle_string(oidp, sv_name, 0, req));
 2000 }
 2001 
 2002 #ifdef KINFO_OVMENTRY_SIZE
 2003 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
 2004 #endif
 2005 
 2006 #ifdef COMPAT_FREEBSD7
 2007 static int
 2008 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
 2009 {
 2010         vm_map_entry_t entry, tmp_entry;
 2011         unsigned int last_timestamp;
 2012         char *fullpath, *freepath;
 2013         struct kinfo_ovmentry *kve;
 2014         struct vattr va;
 2015         struct ucred *cred;
 2016         int error, *name;
 2017         struct vnode *vp;
 2018         struct proc *p;
 2019         vm_map_t map;
 2020         struct vmspace *vm;
 2021 
 2022         name = (int *)arg1;
 2023         error = pget((pid_t)name[0], PGET_WANTREAD, &p);
 2024         if (error != 0)
 2025                 return (error);
 2026         vm = vmspace_acquire_ref(p);
 2027         if (vm == NULL) {
 2028                 PRELE(p);
 2029                 return (ESRCH);
 2030         }
 2031         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
 2032 
 2033         map = &vm->vm_map;
 2034         vm_map_lock_read(map);
 2035         for (entry = map->header.next; entry != &map->header;
 2036             entry = entry->next) {
 2037                 vm_object_t obj, tobj, lobj;
 2038                 vm_offset_t addr;
 2039 
 2040                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 2041                         continue;
 2042 
 2043                 bzero(kve, sizeof(*kve));
 2044                 kve->kve_structsize = sizeof(*kve);
 2045 
 2046                 kve->kve_private_resident = 0;
 2047                 obj = entry->object.vm_object;
 2048                 if (obj != NULL) {
 2049                         VM_OBJECT_RLOCK(obj);
 2050                         if (obj->shadow_count == 1)
 2051                                 kve->kve_private_resident =
 2052                                     obj->resident_page_count;
 2053                 }
 2054                 kve->kve_resident = 0;
 2055                 addr = entry->start;
 2056                 while (addr < entry->end) {
 2057                         if (pmap_extract(map->pmap, addr))
 2058                                 kve->kve_resident++;
 2059                         addr += PAGE_SIZE;
 2060                 }
 2061 
 2062                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
 2063                         if (tobj != obj)
 2064                                 VM_OBJECT_RLOCK(tobj);
 2065                         if (lobj != obj)
 2066                                 VM_OBJECT_RUNLOCK(lobj);
 2067                         lobj = tobj;
 2068                 }
 2069 
 2070                 kve->kve_start = (void*)entry->start;
 2071                 kve->kve_end = (void*)entry->end;
 2072                 kve->kve_offset = (off_t)entry->offset;
 2073 
 2074                 if (entry->protection & VM_PROT_READ)
 2075                         kve->kve_protection |= KVME_PROT_READ;
 2076                 if (entry->protection & VM_PROT_WRITE)
 2077                         kve->kve_protection |= KVME_PROT_WRITE;
 2078                 if (entry->protection & VM_PROT_EXECUTE)
 2079                         kve->kve_protection |= KVME_PROT_EXEC;
 2080 
 2081                 if (entry->eflags & MAP_ENTRY_COW)
 2082                         kve->kve_flags |= KVME_FLAG_COW;
 2083                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
 2084                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
 2085                 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
 2086                         kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
 2087 
 2088                 last_timestamp = map->timestamp;
 2089                 vm_map_unlock_read(map);
 2090 
 2091                 kve->kve_fileid = 0;
 2092                 kve->kve_fsid = 0;
 2093                 freepath = NULL;
 2094                 fullpath = "";
 2095                 if (lobj) {
 2096                         vp = NULL;
 2097                         switch (lobj->type) {
 2098                         case OBJT_DEFAULT:
 2099                                 kve->kve_type = KVME_TYPE_DEFAULT;
 2100                                 break;
 2101                         case OBJT_VNODE:
 2102                                 kve->kve_type = KVME_TYPE_VNODE;
 2103                                 vp = lobj->handle;
 2104                                 vref(vp);
 2105                                 break;
 2106                         case OBJT_SWAP:
 2107                                 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
 2108                                         kve->kve_type = KVME_TYPE_VNODE;
 2109                                         if ((lobj->flags & OBJ_TMPFS) != 0) {
 2110                                                 vp = lobj->un_pager.swp.swp_tmpfs;
 2111                                                 vref(vp);
 2112                                         }
 2113                                 } else {
 2114                                         kve->kve_type = KVME_TYPE_SWAP;
 2115                                 }
 2116                                 break;
 2117                         case OBJT_DEVICE:
 2118                                 kve->kve_type = KVME_TYPE_DEVICE;
 2119                                 break;
 2120                         case OBJT_PHYS:
 2121                                 kve->kve_type = KVME_TYPE_PHYS;
 2122                                 break;
 2123                         case OBJT_DEAD:
 2124                                 kve->kve_type = KVME_TYPE_DEAD;
 2125                                 break;
 2126                         case OBJT_SG:
 2127                                 kve->kve_type = KVME_TYPE_SG;
 2128                                 break;
 2129                         default:
 2130                                 kve->kve_type = KVME_TYPE_UNKNOWN;
 2131                                 break;
 2132                         }
 2133                         if (lobj != obj)
 2134                                 VM_OBJECT_RUNLOCK(lobj);
 2135 
 2136                         kve->kve_ref_count = obj->ref_count;
 2137                         kve->kve_shadow_count = obj->shadow_count;
 2138                         VM_OBJECT_RUNLOCK(obj);
 2139                         if (vp != NULL) {
 2140                                 vn_fullpath(curthread, vp, &fullpath,
 2141                                     &freepath);
 2142                                 cred = curthread->td_ucred;
 2143                                 vn_lock(vp, LK_SHARED | LK_RETRY);
 2144                                 if (VOP_GETATTR(vp, &va, cred) == 0) {
 2145                                         kve->kve_fileid = va.va_fileid;
 2146                                         kve->kve_fsid = va.va_fsid;
 2147                                 }
 2148                                 vput(vp);
 2149                         }
 2150                 } else {
 2151                         kve->kve_type = KVME_TYPE_NONE;
 2152                         kve->kve_ref_count = 0;
 2153                         kve->kve_shadow_count = 0;
 2154                 }
 2155 
 2156                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
 2157                 if (freepath != NULL)
 2158                         free(freepath, M_TEMP);
 2159 
 2160                 error = SYSCTL_OUT(req, kve, sizeof(*kve));
 2161                 vm_map_lock_read(map);
 2162                 if (error)
 2163                         break;
 2164                 if (last_timestamp != map->timestamp) {
 2165                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
 2166                         entry = tmp_entry;
 2167                 }
 2168         }
 2169         vm_map_unlock_read(map);
 2170         vmspace_free(vm);
 2171         PRELE(p);
 2172         free(kve, M_TEMP);
 2173         return (error);
 2174 }
 2175 #endif  /* COMPAT_FREEBSD7 */
 2176 
 2177 #ifdef KINFO_VMENTRY_SIZE
 2178 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
 2179 #endif
 2180 
 2181 static void
 2182 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
 2183     struct kinfo_vmentry *kve)
 2184 {
 2185         vm_object_t obj, tobj;
 2186         vm_page_t m, m_adv;
 2187         vm_offset_t addr;
 2188         vm_paddr_t locked_pa;
 2189         vm_pindex_t pi, pi_adv, pindex;
 2190 
 2191         locked_pa = 0;
 2192         obj = entry->object.vm_object;
 2193         addr = entry->start;
 2194         m_adv = NULL;
 2195         pi = OFF_TO_IDX(entry->offset);
 2196         for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
 2197                 if (m_adv != NULL) {
 2198                         m = m_adv;
 2199                 } else {
 2200                         pi_adv = OFF_TO_IDX(entry->end - addr);
 2201                         pindex = pi;
 2202                         for (tobj = obj;; tobj = tobj->backing_object) {
 2203                                 m = vm_page_find_least(tobj, pindex);
 2204                                 if (m != NULL) {
 2205                                         if (m->pindex == pindex)
 2206                                                 break;
 2207                                         if (pi_adv > m->pindex - pindex) {
 2208                                                 pi_adv = m->pindex - pindex;
 2209                                                 m_adv = m;
 2210                                         }
 2211                                 }
 2212                                 if (tobj->backing_object == NULL)
 2213                                         goto next;
 2214                                 pindex += OFF_TO_IDX(tobj->
 2215                                     backing_object_offset);
 2216                         }
 2217                 }
 2218                 m_adv = NULL;
 2219                 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
 2220                     (addr & (pagesizes[1] - 1)) == 0 &&
 2221                     (pmap_mincore(map->pmap, addr, &locked_pa) &
 2222                     MINCORE_SUPER) != 0) {
 2223                         kve->kve_flags |= KVME_FLAG_SUPER;
 2224                         pi_adv = OFF_TO_IDX(pagesizes[1]);
 2225                 } else {
 2226                         /*
 2227                          * We do not test the found page on validity.
 2228                          * Either the page is busy and being paged in,
 2229                          * or it was invalidated.  The first case
 2230                          * should be counted as resident, the second
 2231                          * is not so clear; we do account both.
 2232                          */
 2233                         pi_adv = 1;
 2234                 }
 2235                 kve->kve_resident += pi_adv;
 2236 next:;
 2237         }
 2238         PA_UNLOCK_COND(locked_pa);
 2239 }
 2240 
 2241 /*
 2242  * Must be called with the process locked and will return unlocked.
 2243  */
 2244 int
 2245 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
 2246 {
 2247         vm_map_entry_t entry, tmp_entry;
 2248         struct vattr va;
 2249         vm_map_t map;
 2250         vm_object_t obj, tobj, lobj;
 2251         char *fullpath, *freepath;
 2252         struct kinfo_vmentry *kve;
 2253         struct ucred *cred;
 2254         struct vnode *vp;
 2255         struct vmspace *vm;
 2256         vm_offset_t addr;
 2257         unsigned int last_timestamp;
 2258         int error;
 2259 
 2260         PROC_LOCK_ASSERT(p, MA_OWNED);
 2261 
 2262         _PHOLD(p);
 2263         PROC_UNLOCK(p);
 2264         vm = vmspace_acquire_ref(p);
 2265         if (vm == NULL) {
 2266                 PRELE(p);
 2267                 return (ESRCH);
 2268         }
 2269         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
 2270 
 2271         error = 0;
 2272         map = &vm->vm_map;
 2273         vm_map_lock_read(map);
 2274         for (entry = map->header.next; entry != &map->header;
 2275             entry = entry->next) {
 2276                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 2277                         continue;
 2278 
 2279                 addr = entry->end;
 2280                 bzero(kve, sizeof(*kve));
 2281                 obj = entry->object.vm_object;
 2282                 if (obj != NULL) {
 2283                         for (tobj = obj; tobj != NULL;
 2284                             tobj = tobj->backing_object) {
 2285                                 VM_OBJECT_RLOCK(tobj);
 2286                                 lobj = tobj;
 2287                         }
 2288                         if (obj->backing_object == NULL)
 2289                                 kve->kve_private_resident =
 2290                                     obj->resident_page_count;
 2291                         if (!vmmap_skip_res_cnt)
 2292                                 kern_proc_vmmap_resident(map, entry, kve);
 2293                         for (tobj = obj; tobj != NULL;
 2294                             tobj = tobj->backing_object) {
 2295                                 if (tobj != obj && tobj != lobj)
 2296                                         VM_OBJECT_RUNLOCK(tobj);
 2297                         }
 2298                 } else {
 2299                         lobj = NULL;
 2300                 }
 2301 
 2302                 kve->kve_start = entry->start;
 2303                 kve->kve_end = entry->end;
 2304                 kve->kve_offset = entry->offset;
 2305 
 2306                 if (entry->protection & VM_PROT_READ)
 2307                         kve->kve_protection |= KVME_PROT_READ;
 2308                 if (entry->protection & VM_PROT_WRITE)
 2309                         kve->kve_protection |= KVME_PROT_WRITE;
 2310                 if (entry->protection & VM_PROT_EXECUTE)
 2311                         kve->kve_protection |= KVME_PROT_EXEC;
 2312 
 2313                 if (entry->eflags & MAP_ENTRY_COW)
 2314                         kve->kve_flags |= KVME_FLAG_COW;
 2315                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
 2316                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
 2317                 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
 2318                         kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
 2319                 if (entry->eflags & MAP_ENTRY_GROWS_UP)
 2320                         kve->kve_flags |= KVME_FLAG_GROWS_UP;
 2321                 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
 2322                         kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
 2323 
 2324                 last_timestamp = map->timestamp;
 2325                 vm_map_unlock_read(map);
 2326 
 2327                 freepath = NULL;
 2328                 fullpath = "";
 2329                 if (lobj != NULL) {
 2330                         vp = NULL;
 2331                         switch (lobj->type) {
 2332                         case OBJT_DEFAULT:
 2333                                 kve->kve_type = KVME_TYPE_DEFAULT;
 2334                                 break;
 2335                         case OBJT_VNODE:
 2336                                 kve->kve_type = KVME_TYPE_VNODE;
 2337                                 vp = lobj->handle;
 2338                                 vref(vp);
 2339                                 break;
 2340                         case OBJT_SWAP:
 2341                                 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
 2342                                         kve->kve_type = KVME_TYPE_VNODE;
 2343                                         if ((lobj->flags & OBJ_TMPFS) != 0) {
 2344                                                 vp = lobj->un_pager.swp.swp_tmpfs;
 2345                                                 vref(vp);
 2346                                         }
 2347                                 } else {
 2348                                         kve->kve_type = KVME_TYPE_SWAP;
 2349                                 }
 2350                                 break;
 2351                         case OBJT_DEVICE:
 2352                                 kve->kve_type = KVME_TYPE_DEVICE;
 2353                                 break;
 2354                         case OBJT_PHYS:
 2355                                 kve->kve_type = KVME_TYPE_PHYS;
 2356                                 break;
 2357                         case OBJT_DEAD:
 2358                                 kve->kve_type = KVME_TYPE_DEAD;
 2359                                 break;
 2360                         case OBJT_SG:
 2361                                 kve->kve_type = KVME_TYPE_SG;
 2362                                 break;
 2363                         case OBJT_MGTDEVICE:
 2364                                 kve->kve_type = KVME_TYPE_MGTDEVICE;
 2365                                 break;
 2366                         default:
 2367                                 kve->kve_type = KVME_TYPE_UNKNOWN;
 2368                                 break;
 2369                         }
 2370                         if (lobj != obj)
 2371                                 VM_OBJECT_RUNLOCK(lobj);
 2372 
 2373                         kve->kve_ref_count = obj->ref_count;
 2374                         kve->kve_shadow_count = obj->shadow_count;
 2375                         VM_OBJECT_RUNLOCK(obj);
 2376                         if (vp != NULL) {
 2377                                 vn_fullpath(curthread, vp, &fullpath,
 2378                                     &freepath);
 2379                                 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
 2380                                 cred = curthread->td_ucred;
 2381                                 vn_lock(vp, LK_SHARED | LK_RETRY);
 2382                                 if (VOP_GETATTR(vp, &va, cred) == 0) {
 2383                                         kve->kve_vn_fileid = va.va_fileid;
 2384                                         kve->kve_vn_fsid = va.va_fsid;
 2385                                         kve->kve_vn_mode =
 2386                                             MAKEIMODE(va.va_type, va.va_mode);
 2387                                         kve->kve_vn_size = va.va_size;
 2388                                         kve->kve_vn_rdev = va.va_rdev;
 2389                                         kve->kve_status = KF_ATTR_VALID;
 2390                                 }
 2391                                 vput(vp);
 2392                         }
 2393                 } else {
 2394                         kve->kve_type = KVME_TYPE_NONE;
 2395                         kve->kve_ref_count = 0;
 2396                         kve->kve_shadow_count = 0;
 2397                 }
 2398 
 2399                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
 2400                 if (freepath != NULL)
 2401                         free(freepath, M_TEMP);
 2402 
 2403                 /* Pack record size down */
 2404                 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
 2405                         kve->kve_structsize =
 2406                             offsetof(struct kinfo_vmentry, kve_path) +
 2407                             strlen(kve->kve_path) + 1;
 2408                 else
 2409                         kve->kve_structsize = sizeof(*kve);
 2410                 kve->kve_structsize = roundup(kve->kve_structsize,
 2411                     sizeof(uint64_t));
 2412 
 2413                 /* Halt filling and truncate rather than exceeding maxlen */
 2414                 if (maxlen != -1 && maxlen < kve->kve_structsize) {
 2415                         error = 0;
 2416                         vm_map_lock_read(map);
 2417                         break;
 2418                 } else if (maxlen != -1)
 2419                         maxlen -= kve->kve_structsize;
 2420 
 2421                 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
 2422                         error = ENOMEM;
 2423                 vm_map_lock_read(map);
 2424                 if (error != 0)
 2425                         break;
 2426                 if (last_timestamp != map->timestamp) {
 2427                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
 2428                         entry = tmp_entry;
 2429                 }
 2430         }
 2431         vm_map_unlock_read(map);
 2432         vmspace_free(vm);
 2433         PRELE(p);
 2434         free(kve, M_TEMP);
 2435         return (error);
 2436 }
 2437 
 2438 static int
 2439 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
 2440 {
 2441         struct proc *p;
 2442         struct sbuf sb;
 2443         int error, error2, *name;
 2444 
 2445         name = (int *)arg1;
 2446         sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
 2447         error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
 2448         if (error != 0) {
 2449                 sbuf_delete(&sb);
 2450                 return (error);
 2451         }
 2452         error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
 2453         error2 = sbuf_finish(&sb);
 2454         sbuf_delete(&sb);
 2455         return (error != 0 ? error : error2);
 2456 }
 2457 
 2458 #if defined(STACK) || defined(DDB)
 2459 static int
 2460 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
 2461 {
 2462         struct kinfo_kstack *kkstp;
 2463         int error, i, *name, numthreads;
 2464         lwpid_t *lwpidarray;
 2465         struct thread *td;
 2466         struct stack *st;
 2467         struct sbuf sb;
 2468         struct proc *p;
 2469 
 2470         name = (int *)arg1;
 2471         error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
 2472         if (error != 0)
 2473                 return (error);
 2474 
 2475         kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
 2476         st = stack_create();
 2477 
 2478         lwpidarray = NULL;
 2479         PROC_LOCK(p);
 2480         do {
 2481                 if (lwpidarray != NULL) {
 2482                         free(lwpidarray, M_TEMP);
 2483                         lwpidarray = NULL;
 2484                 }
 2485                 numthreads = p->p_numthreads;
 2486                 PROC_UNLOCK(p);
 2487                 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
 2488                     M_WAITOK | M_ZERO);
 2489                 PROC_LOCK(p);
 2490         } while (numthreads < p->p_numthreads);
 2491 
 2492         /*
 2493          * XXXRW: During the below loop, execve(2) and countless other sorts
 2494          * of changes could have taken place.  Should we check to see if the
 2495          * vmspace has been replaced, or the like, in order to prevent
 2496          * giving a snapshot that spans, say, execve(2), with some threads
 2497          * before and some after?  Among other things, the credentials could
 2498          * have changed, in which case the right to extract debug info might
 2499          * no longer be assured.
 2500          */
 2501         i = 0;
 2502         FOREACH_THREAD_IN_PROC(p, td) {
 2503                 KASSERT(i < numthreads,
 2504                     ("sysctl_kern_proc_kstack: numthreads"));
 2505                 lwpidarray[i] = td->td_tid;
 2506                 i++;
 2507         }
 2508         numthreads = i;
 2509         for (i = 0; i < numthreads; i++) {
 2510                 td = thread_find(p, lwpidarray[i]);
 2511                 if (td == NULL) {
 2512                         continue;
 2513                 }
 2514                 bzero(kkstp, sizeof(*kkstp));
 2515                 (void)sbuf_new(&sb, kkstp->kkst_trace,
 2516                     sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
 2517                 thread_lock(td);
 2518                 kkstp->kkst_tid = td->td_tid;
 2519                 if (TD_IS_SWAPPED(td))
 2520                         kkstp->kkst_state = KKST_STATE_SWAPPED;
 2521                 else if (TD_IS_RUNNING(td))
 2522                         kkstp->kkst_state = KKST_STATE_RUNNING;
 2523                 else {
 2524                         kkstp->kkst_state = KKST_STATE_STACKOK;
 2525                         stack_save_td(st, td);
 2526                 }
 2527                 thread_unlock(td);
 2528                 PROC_UNLOCK(p);
 2529                 stack_sbuf_print(&sb, st);
 2530                 sbuf_finish(&sb);
 2531                 sbuf_delete(&sb);
 2532                 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
 2533                 PROC_LOCK(p);
 2534                 if (error)
 2535                         break;
 2536         }
 2537         _PRELE(p);
 2538         PROC_UNLOCK(p);
 2539         if (lwpidarray != NULL)
 2540                 free(lwpidarray, M_TEMP);
 2541         stack_destroy(st);
 2542         free(kkstp, M_TEMP);
 2543         return (error);
 2544 }
 2545 #endif
 2546 
 2547 /*
 2548  * This sysctl allows a process to retrieve the full list of groups from
 2549  * itself or another process.
 2550  */
 2551 static int
 2552 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
 2553 {
 2554         pid_t *pidp = (pid_t *)arg1;
 2555         unsigned int arglen = arg2;
 2556         struct proc *p;
 2557         struct ucred *cred;
 2558         int error;
 2559 
 2560         if (arglen != 1)
 2561                 return (EINVAL);
 2562         if (*pidp == -1) {      /* -1 means this process */
 2563                 p = req->td->td_proc;
 2564         } else {
 2565                 error = pget(*pidp, PGET_CANSEE, &p);
 2566                 if (error != 0)
 2567                         return (error);
 2568         }
 2569 
 2570         cred = crhold(p->p_ucred);
 2571         if (*pidp != -1)
 2572                 PROC_UNLOCK(p);
 2573 
 2574         error = SYSCTL_OUT(req, cred->cr_groups,
 2575             cred->cr_ngroups * sizeof(gid_t));
 2576         crfree(cred);
 2577         return (error);
 2578 }
 2579 
 2580 /*
 2581  * This sysctl allows a process to retrieve or/and set the resource limit for
 2582  * another process.
 2583  */
 2584 static int
 2585 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
 2586 {
 2587         int *name = (int *)arg1;
 2588         u_int namelen = arg2;
 2589         struct rlimit rlim;
 2590         struct proc *p;
 2591         u_int which;
 2592         int flags, error;
 2593 
 2594         if (namelen != 2)
 2595                 return (EINVAL);
 2596 
 2597         which = (u_int)name[1];
 2598         if (which >= RLIM_NLIMITS)
 2599                 return (EINVAL);
 2600 
 2601         if (req->newptr != NULL && req->newlen != sizeof(rlim))
 2602                 return (EINVAL);
 2603 
 2604         flags = PGET_HOLD | PGET_NOTWEXIT;
 2605         if (req->newptr != NULL)
 2606                 flags |= PGET_CANDEBUG;
 2607         else
 2608                 flags |= PGET_CANSEE;
 2609         error = pget((pid_t)name[0], flags, &p);
 2610         if (error != 0)
 2611                 return (error);
 2612 
 2613         /*
 2614          * Retrieve limit.
 2615          */
 2616         if (req->oldptr != NULL) {
 2617                 PROC_LOCK(p);
 2618                 lim_rlimit(p, which, &rlim);
 2619                 PROC_UNLOCK(p);
 2620         }
 2621         error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
 2622         if (error != 0)
 2623                 goto errout;
 2624 
 2625         /*
 2626          * Set limit.
 2627          */
 2628         if (req->newptr != NULL) {
 2629                 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
 2630                 if (error == 0)
 2631                         error = kern_proc_setrlimit(curthread, p, which, &rlim);
 2632         }
 2633 
 2634 errout:
 2635         PRELE(p);
 2636         return (error);
 2637 }
 2638 
 2639 /*
 2640  * This sysctl allows a process to retrieve ps_strings structure location of
 2641  * another process.
 2642  */
 2643 static int
 2644 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
 2645 {
 2646         int *name = (int *)arg1;
 2647         u_int namelen = arg2;
 2648         struct proc *p;
 2649         vm_offset_t ps_strings;
 2650         int error;
 2651 #ifdef COMPAT_FREEBSD32
 2652         uint32_t ps_strings32;
 2653 #endif
 2654 
 2655         if (namelen != 1)
 2656                 return (EINVAL);
 2657 
 2658         error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
 2659         if (error != 0)
 2660                 return (error);
 2661 #ifdef COMPAT_FREEBSD32
 2662         if ((req->flags & SCTL_MASK32) != 0) {
 2663                 /*
 2664                  * We return 0 if the 32 bit emulation request is for a 64 bit
 2665                  * process.
 2666                  */
 2667                 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
 2668                     PTROUT(p->p_sysent->sv_psstrings) : 0;
 2669                 PROC_UNLOCK(p);
 2670                 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
 2671                 return (error);
 2672         }
 2673 #endif
 2674         ps_strings = p->p_sysent->sv_psstrings;
 2675         PROC_UNLOCK(p);
 2676         error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
 2677         return (error);
 2678 }
 2679 
 2680 /*
 2681  * This sysctl allows a process to retrieve umask of another process.
 2682  */
 2683 static int
 2684 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
 2685 {
 2686         int *name = (int *)arg1;
 2687         u_int namelen = arg2;
 2688         struct proc *p;
 2689         int error;
 2690         u_short fd_cmask;
 2691 
 2692         if (namelen != 1)
 2693                 return (EINVAL);
 2694 
 2695         error = pget((pid_t)name[0], PGET_WANTREAD, &p);
 2696         if (error != 0)
 2697                 return (error);
 2698 
 2699         FILEDESC_SLOCK(p->p_fd);
 2700         fd_cmask = p->p_fd->fd_cmask;
 2701         FILEDESC_SUNLOCK(p->p_fd);
 2702         PRELE(p);
 2703         error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
 2704         return (error);
 2705 }
 2706 
 2707 /*
 2708  * This sysctl allows a process to set and retrieve binary osreldate of
 2709  * another process.
 2710  */
 2711 static int
 2712 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
 2713 {
 2714         int *name = (int *)arg1;
 2715         u_int namelen = arg2;
 2716         struct proc *p;
 2717         int flags, error, osrel;
 2718 
 2719         if (namelen != 1)
 2720                 return (EINVAL);
 2721 
 2722         if (req->newptr != NULL && req->newlen != sizeof(osrel))
 2723                 return (EINVAL);
 2724 
 2725         flags = PGET_HOLD | PGET_NOTWEXIT;
 2726         if (req->newptr != NULL)
 2727                 flags |= PGET_CANDEBUG;
 2728         else
 2729                 flags |= PGET_CANSEE;
 2730         error = pget((pid_t)name[0], flags, &p);
 2731         if (error != 0)
 2732                 return (error);
 2733 
 2734         error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
 2735         if (error != 0)
 2736                 goto errout;
 2737 
 2738         if (req->newptr != NULL) {
 2739                 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
 2740                 if (error != 0)
 2741                         goto errout;
 2742                 if (osrel < 0) {
 2743                         error = EINVAL;
 2744                         goto errout;
 2745                 }
 2746                 p->p_osrel = osrel;
 2747         }
 2748 errout:
 2749         PRELE(p);
 2750         return (error);
 2751 }
 2752 
 2753 static int
 2754 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
 2755 {
 2756         int *name = (int *)arg1;
 2757         u_int namelen = arg2;
 2758         struct proc *p;
 2759         struct kinfo_sigtramp kst;
 2760         const struct sysentvec *sv;
 2761         int error;
 2762 #ifdef COMPAT_FREEBSD32
 2763         struct kinfo_sigtramp32 kst32;
 2764 #endif
 2765 
 2766         if (namelen != 1)
 2767                 return (EINVAL);
 2768 
 2769         error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
 2770         if (error != 0)
 2771                 return (error);
 2772         sv = p->p_sysent;
 2773 #ifdef COMPAT_FREEBSD32
 2774         if ((req->flags & SCTL_MASK32) != 0) {
 2775                 bzero(&kst32, sizeof(kst32));
 2776                 if (SV_PROC_FLAG(p, SV_ILP32)) {
 2777                         if (sv->sv_sigcode_base != 0) {
 2778                                 kst32.ksigtramp_start = sv->sv_sigcode_base;
 2779                                 kst32.ksigtramp_end = sv->sv_sigcode_base +
 2780                                     *sv->sv_szsigcode;
 2781                         } else {
 2782                                 kst32.ksigtramp_start = sv->sv_psstrings -
 2783                                     *sv->sv_szsigcode;
 2784                                 kst32.ksigtramp_end = sv->sv_psstrings;
 2785                         }
 2786                 }
 2787                 PROC_UNLOCK(p);
 2788                 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
 2789                 return (error);
 2790         }
 2791 #endif
 2792         bzero(&kst, sizeof(kst));
 2793         if (sv->sv_sigcode_base != 0) {
 2794                 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
 2795                 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
 2796                     *sv->sv_szsigcode;
 2797         } else {
 2798                 kst.ksigtramp_start = (char *)sv->sv_psstrings -
 2799                     *sv->sv_szsigcode;
 2800                 kst.ksigtramp_end = (char *)sv->sv_psstrings;
 2801         }
 2802         PROC_UNLOCK(p);
 2803         error = SYSCTL_OUT(req, &kst, sizeof(kst));
 2804         return (error);
 2805 }
 2806 
 2807 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
 2808 
 2809 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
 2810         CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
 2811         "Return entire process table");
 2812 
 2813 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2814         sysctl_kern_proc, "Process table");
 2815 
 2816 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2817         sysctl_kern_proc, "Process table");
 2818 
 2819 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2820         sysctl_kern_proc, "Process table");
 2821 
 2822 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
 2823         CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2824 
 2825 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2826         sysctl_kern_proc, "Process table");
 2827 
 2828 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2829         sysctl_kern_proc, "Process table");
 2830 
 2831 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2832         sysctl_kern_proc, "Process table");
 2833 
 2834 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2835         sysctl_kern_proc, "Process table");
 2836 
 2837 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2838         sysctl_kern_proc, "Return process table, no threads");
 2839 
 2840 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
 2841         CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
 2842         sysctl_kern_proc_args, "Process argument list");
 2843 
 2844 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
 2845         sysctl_kern_proc_env, "Process environment");
 2846 
 2847 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
 2848         CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
 2849 
 2850 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
 2851         CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
 2852 
 2853 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
 2854         CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
 2855         "Process syscall vector name (ABI type)");
 2856 
 2857 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
 2858         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2859 
 2860 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
 2861         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2862 
 2863 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
 2864         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2865 
 2866 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
 2867         sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2868 
 2869 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
 2870         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2871 
 2872 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
 2873         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2874 
 2875 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
 2876         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2877 
 2878 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
 2879         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
 2880 
 2881 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
 2882         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
 2883         "Return process table, no threads");
 2884 
 2885 #ifdef COMPAT_FREEBSD7
 2886 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
 2887         CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
 2888 #endif
 2889 
 2890 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
 2891         CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
 2892 
 2893 #if defined(STACK) || defined(DDB)
 2894 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
 2895         CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
 2896 #endif
 2897 
 2898 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
 2899         CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
 2900 
 2901 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
 2902         CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
 2903         "Process resource limits");
 2904 
 2905 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
 2906         CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
 2907         "Process ps_strings location");
 2908 
 2909 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
 2910         CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
 2911 
 2912 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
 2913         CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
 2914         "Process binary osreldate");
 2915 
 2916 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
 2917         CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
 2918         "Process signal trampoline location");
 2919 
 2920 int allproc_gen;
 2921 
 2922 /*
 2923  * stop_all_proc() purpose is to stop all process which have usermode,
 2924  * except current process for obvious reasons.  This makes it somewhat
 2925  * unreliable when invoked from multithreaded process.  The service
 2926  * must not be user-callable anyway.
 2927  */
 2928 void
 2929 stop_all_proc(void)
 2930 {
 2931         struct proc *cp, *p;
 2932         int r, gen;
 2933         bool restart, seen_stopped, seen_exiting, stopped_some;
 2934 
 2935         cp = curproc;
 2936 allproc_loop:
 2937         sx_xlock(&allproc_lock);
 2938         gen = allproc_gen;
 2939         seen_exiting = seen_stopped = stopped_some = restart = false;
 2940         LIST_REMOVE(cp, p_list);
 2941         LIST_INSERT_HEAD(&allproc, cp, p_list);
 2942         for (;;) {
 2943                 p = LIST_NEXT(cp, p_list);
 2944                 if (p == NULL)
 2945                         break;
 2946                 LIST_REMOVE(cp, p_list);
 2947                 LIST_INSERT_AFTER(p, cp, p_list);
 2948                 PROC_LOCK(p);
 2949                 if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
 2950                     P_TOTAL_STOP)) != 0) {
 2951                         PROC_UNLOCK(p);
 2952                         continue;
 2953                 }
 2954                 if ((p->p_flag & P_WEXIT) != 0) {
 2955                         seen_exiting = true;
 2956                         PROC_UNLOCK(p);
 2957                         continue;
 2958                 }
 2959                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
 2960                         /*
 2961                          * Stopped processes are tolerated when there
 2962                          * are no other processes which might continue
 2963                          * them.  P_STOPPED_SINGLE but not
 2964                          * P_TOTAL_STOP process still has at least one
 2965                          * thread running.
 2966                          */
 2967                         seen_stopped = true;
 2968                         PROC_UNLOCK(p);
 2969                         continue;
 2970                 }
 2971                 _PHOLD(p);
 2972                 sx_xunlock(&allproc_lock);
 2973                 r = thread_single(p, SINGLE_ALLPROC);
 2974                 if (r != 0)
 2975                         restart = true;
 2976                 else
 2977                         stopped_some = true;
 2978                 _PRELE(p);
 2979                 PROC_UNLOCK(p);
 2980                 sx_xlock(&allproc_lock);
 2981         }
 2982         /* Catch forked children we did not see in iteration. */
 2983         if (gen != allproc_gen)
 2984                 restart = true;
 2985         sx_xunlock(&allproc_lock);
 2986         if (restart || stopped_some || seen_exiting || seen_stopped) {
 2987                 kern_yield(PRI_USER);
 2988                 goto allproc_loop;
 2989         }
 2990 }
 2991 
 2992 void
 2993 resume_all_proc(void)
 2994 {
 2995         struct proc *cp, *p;
 2996 
 2997         cp = curproc;
 2998         sx_xlock(&allproc_lock);
 2999         LIST_REMOVE(cp, p_list);
 3000         LIST_INSERT_HEAD(&allproc, cp, p_list);
 3001         for (;;) {
 3002                 p = LIST_NEXT(cp, p_list);
 3003                 if (p == NULL)
 3004                         break;
 3005                 LIST_REMOVE(cp, p_list);
 3006                 LIST_INSERT_AFTER(p, cp, p_list);
 3007                 PROC_LOCK(p);
 3008                 if ((p->p_flag & P_TOTAL_STOP) != 0) {
 3009                         sx_xunlock(&allproc_lock);
 3010                         _PHOLD(p);
 3011                         thread_single_end(p, SINGLE_ALLPROC);
 3012                         _PRELE(p);
 3013                         PROC_UNLOCK(p);
 3014                         sx_xlock(&allproc_lock);
 3015                 } else {
 3016                         PROC_UNLOCK(p);
 3017                 }
 3018         }
 3019         sx_xunlock(&allproc_lock);
 3020 }
 3021 
 3022 /* #define      TOTAL_STOP_DEBUG        1 */
 3023 #ifdef TOTAL_STOP_DEBUG
 3024 volatile static int ap_resume;
 3025 #include <sys/mount.h>
 3026 
 3027 static int
 3028 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
 3029 {
 3030         int error, val;
 3031 
 3032         val = 0;
 3033         ap_resume = 0;
 3034         error = sysctl_handle_int(oidp, &val, 0, req);
 3035         if (error != 0 || req->newptr == NULL)
 3036                 return (error);
 3037         if (val != 0) {
 3038                 stop_all_proc();
 3039                 syncer_suspend();
 3040                 while (ap_resume == 0)
 3041                         ;
 3042                 syncer_resume();
 3043                 resume_all_proc();
 3044         }
 3045         return (0);
 3046 }
 3047 
 3048 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
 3049     CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
 3050     sysctl_debug_stop_all_proc, "I",
 3051     "");
 3052 #endif

Cache object: fe3bcda1cfe68cc015c77bbd81d9ff0a


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