]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/kern_proc.c
Merge userspace DTrace support from head to stable/8:
[FreeBSD/stable/8.git] / sys / kern / kern_proc.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_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/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/proc.h>
51 #include <sys/refcount.h>
52 #include <sys/sbuf.h>
53 #include <sys/sysent.h>
54 #include <sys/sched.h>
55 #include <sys/smp.h>
56 #include <sys/stack.h>
57 #include <sys/sysctl.h>
58 #include <sys/filedesc.h>
59 #include <sys/tty.h>
60 #include <sys/signalvar.h>
61 #include <sys/sdt.h>
62 #include <sys/sx.h>
63 #include <sys/user.h>
64 #include <sys/jail.h>
65 #include <sys/vnode.h>
66 #include <sys/eventhandler.h>
67
68 #ifdef DDB
69 #include <ddb/ddb.h>
70 #endif
71
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74 #include <vm/pmap.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_object.h>
77 #include <vm/uma.h>
78
79 #ifdef COMPAT_FREEBSD32
80 #include <compat/freebsd32/freebsd32.h>
81 #include <compat/freebsd32/freebsd32_util.h>
82 #endif
83
84 SDT_PROVIDER_DEFINE(proc);
85 SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry);
86 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
87 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
88 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
89 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
90 SDT_PROBE_DEFINE(proc, kernel, ctor, return, return);
91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
94 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
95 SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry);
96 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
97 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
98 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
99 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
100 SDT_PROBE_DEFINE(proc, kernel, dtor, return, return);
101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
102 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
104 SDT_PROBE_DEFINE(proc, kernel, init, entry, entry);
105 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
106 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
107 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
108 SDT_PROBE_DEFINE(proc, kernel, init, return, return);
109 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
110 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
111 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
112
113 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
114 MALLOC_DEFINE(M_SESSION, "session", "session header");
115 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
116 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
117
118 static void doenterpgrp(struct proc *, struct pgrp *);
119 static void orphanpg(struct pgrp *pg);
120 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
121 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
122 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
123     int preferthread);
124 static void pgadjustjobc(struct pgrp *pgrp, int entering);
125 static void pgdelete(struct pgrp *);
126 static int proc_ctor(void *mem, int size, void *arg, int flags);
127 static void proc_dtor(void *mem, int size, void *arg);
128 static int proc_init(void *mem, int size, int flags);
129 static void proc_fini(void *mem, int size);
130 static void pargs_free(struct pargs *pa);
131
132 /*
133  * Other process lists
134  */
135 struct pidhashhead *pidhashtbl;
136 u_long pidhash;
137 struct pgrphashhead *pgrphashtbl;
138 u_long pgrphash;
139 struct proclist allproc;
140 struct proclist zombproc;
141 struct sx allproc_lock;
142 struct sx proctree_lock;
143 struct mtx ppeers_lock;
144 uma_zone_t proc_zone;
145
146 int kstack_pages = KSTACK_PAGES;
147 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
148     "Kernel stack size in pages");
149
150 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
151 #ifdef COMPAT_FREEBSD32
152 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
153 #endif
154
155 /*
156  * Initialize global process hashing structures.
157  */
158 void
159 procinit()
160 {
161
162         sx_init(&allproc_lock, "allproc");
163         sx_init(&proctree_lock, "proctree");
164         mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
165         LIST_INIT(&allproc);
166         LIST_INIT(&zombproc);
167         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
168         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
169         proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
170             proc_ctor, proc_dtor, proc_init, proc_fini,
171             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
172         uihashinit();
173 }
174
175 /*
176  * Prepare a proc for use.
177  */
178 static int
179 proc_ctor(void *mem, int size, void *arg, int flags)
180 {
181         struct proc *p;
182
183         p = (struct proc *)mem;
184         SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
185         EVENTHANDLER_INVOKE(process_ctor, p);
186         SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
187         return (0);
188 }
189
190 /*
191  * Reclaim a proc after use.
192  */
193 static void
194 proc_dtor(void *mem, int size, void *arg)
195 {
196         struct proc *p;
197         struct thread *td;
198
199         /* INVARIANTS checks go here */
200         p = (struct proc *)mem;
201         td = FIRST_THREAD_IN_PROC(p);
202         SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
203         if (td != NULL) {
204 #ifdef INVARIANTS
205                 KASSERT((p->p_numthreads == 1),
206                     ("bad number of threads in exiting process"));
207                 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
208 #endif
209                 /* Free all OSD associated to this thread. */
210                 osd_thread_exit(td);
211         }
212         EVENTHANDLER_INVOKE(process_dtor, p);
213         if (p->p_ksi != NULL)
214                 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
215         SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
216 }
217
218 /*
219  * Initialize type-stable parts of a proc (when newly created).
220  */
221 static int
222 proc_init(void *mem, int size, int flags)
223 {
224         struct proc *p;
225
226         p = (struct proc *)mem;
227         SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
228         p->p_sched = (struct p_sched *)&p[1];
229         bzero(&p->p_mtx, sizeof(struct mtx));
230         mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
231         mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
232         cv_init(&p->p_pwait, "ppwait");
233         cv_init(&p->p_dbgwait, "dbgwait");
234         TAILQ_INIT(&p->p_threads);           /* all threads in proc */
235         EVENTHANDLER_INVOKE(process_init, p);
236         p->p_stats = pstats_alloc();
237         SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
238         return (0);
239 }
240
241 /*
242  * UMA should ensure that this function is never called.
243  * Freeing a proc structure would violate type stability.
244  */
245 static void
246 proc_fini(void *mem, int size)
247 {
248 #ifdef notnow
249         struct proc *p;
250
251         p = (struct proc *)mem;
252         EVENTHANDLER_INVOKE(process_fini, p);
253         pstats_free(p->p_stats);
254         thread_free(FIRST_THREAD_IN_PROC(p));
255         mtx_destroy(&p->p_mtx);
256         if (p->p_ksi != NULL)
257                 ksiginfo_free(p->p_ksi);
258 #else
259         panic("proc reclaimed");
260 #endif
261 }
262
263 /*
264  * Is p an inferior of the current process?
265  */
266 int
267 inferior(p)
268         register struct proc *p;
269 {
270
271         sx_assert(&proctree_lock, SX_LOCKED);
272         for (; p != curproc; p = p->p_pptr)
273                 if (p->p_pid == 0)
274                         return (0);
275         return (1);
276 }
277
278 /*
279  * Locate a process by number; return only "live" processes -- i.e., neither
280  * zombies nor newly born but incompletely initialized processes.  By not
281  * returning processes in the PRS_NEW state, we allow callers to avoid
282  * testing for that condition to avoid dereferencing p_ucred, et al.
283  */
284 struct proc *
285 pfind(pid)
286         register pid_t pid;
287 {
288         register struct proc *p;
289
290         sx_slock(&allproc_lock);
291         LIST_FOREACH(p, PIDHASH(pid), p_hash)
292                 if (p->p_pid == pid) {
293                         if (p->p_state == PRS_NEW) {
294                                 p = NULL;
295                                 break;
296                         }
297                         PROC_LOCK(p);
298                         break;
299                 }
300         sx_sunlock(&allproc_lock);
301         return (p);
302 }
303
304 /*
305  * Locate a process group by number.
306  * The caller must hold proctree_lock.
307  */
308 struct pgrp *
309 pgfind(pgid)
310         register pid_t pgid;
311 {
312         register struct pgrp *pgrp;
313
314         sx_assert(&proctree_lock, SX_LOCKED);
315
316         LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
317                 if (pgrp->pg_id == pgid) {
318                         PGRP_LOCK(pgrp);
319                         return (pgrp);
320                 }
321         }
322         return (NULL);
323 }
324
325 /*
326  * Create a new process group.
327  * pgid must be equal to the pid of p.
328  * Begin a new session if required.
329  */
330 int
331 enterpgrp(p, pgid, pgrp, sess)
332         register struct proc *p;
333         pid_t pgid;
334         struct pgrp *pgrp;
335         struct session *sess;
336 {
337         struct pgrp *pgrp2;
338
339         sx_assert(&proctree_lock, SX_XLOCKED);
340
341         KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
342         KASSERT(p->p_pid == pgid,
343             ("enterpgrp: new pgrp and pid != pgid"));
344
345         pgrp2 = pgfind(pgid);
346
347         KASSERT(pgrp2 == NULL,
348             ("enterpgrp: pgrp with pgid exists"));
349         KASSERT(!SESS_LEADER(p),
350             ("enterpgrp: session leader attempted setpgrp"));
351
352         mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
353
354         if (sess != NULL) {
355                 /*
356                  * new session
357                  */
358                 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
359                 PROC_LOCK(p);
360                 p->p_flag &= ~P_CONTROLT;
361                 PROC_UNLOCK(p);
362                 PGRP_LOCK(pgrp);
363                 sess->s_leader = p;
364                 sess->s_sid = p->p_pid;
365                 refcount_init(&sess->s_count, 1);
366                 sess->s_ttyvp = NULL;
367                 sess->s_ttyp = NULL;
368                 bcopy(p->p_session->s_login, sess->s_login,
369                             sizeof(sess->s_login));
370                 pgrp->pg_session = sess;
371                 KASSERT(p == curproc,
372                     ("enterpgrp: mksession and p != curproc"));
373         } else {
374                 pgrp->pg_session = p->p_session;
375                 sess_hold(pgrp->pg_session);
376                 PGRP_LOCK(pgrp);
377         }
378         pgrp->pg_id = pgid;
379         LIST_INIT(&pgrp->pg_members);
380
381         /*
382          * As we have an exclusive lock of proctree_lock,
383          * this should not deadlock.
384          */
385         LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
386         pgrp->pg_jobc = 0;
387         SLIST_INIT(&pgrp->pg_sigiolst);
388         PGRP_UNLOCK(pgrp);
389
390         doenterpgrp(p, pgrp);
391
392         return (0);
393 }
394
395 /*
396  * Move p to an existing process group
397  */
398 int
399 enterthispgrp(p, pgrp)
400         register struct proc *p;
401         struct pgrp *pgrp;
402 {
403
404         sx_assert(&proctree_lock, SX_XLOCKED);
405         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
406         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
407         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
408         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
409         KASSERT(pgrp->pg_session == p->p_session,
410                 ("%s: pgrp's session %p, p->p_session %p.\n",
411                 __func__,
412                 pgrp->pg_session,
413                 p->p_session));
414         KASSERT(pgrp != p->p_pgrp,
415                 ("%s: p belongs to pgrp.", __func__));
416
417         doenterpgrp(p, pgrp);
418
419         return (0);
420 }
421
422 /*
423  * Move p to a process group
424  */
425 static void
426 doenterpgrp(p, pgrp)
427         struct proc *p;
428         struct pgrp *pgrp;
429 {
430         struct pgrp *savepgrp;
431
432         sx_assert(&proctree_lock, SX_XLOCKED);
433         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
434         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
435         PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
436         SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
437
438         savepgrp = p->p_pgrp;
439
440         /*
441          * Adjust eligibility of affected pgrps to participate in job control.
442          * Increment eligibility counts before decrementing, otherwise we
443          * could reach 0 spuriously during the first call.
444          */
445         fixjobc(p, pgrp, 1);
446         fixjobc(p, p->p_pgrp, 0);
447
448         PGRP_LOCK(pgrp);
449         PGRP_LOCK(savepgrp);
450         PROC_LOCK(p);
451         LIST_REMOVE(p, p_pglist);
452         p->p_pgrp = pgrp;
453         PROC_UNLOCK(p);
454         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
455         PGRP_UNLOCK(savepgrp);
456         PGRP_UNLOCK(pgrp);
457         if (LIST_EMPTY(&savepgrp->pg_members))
458                 pgdelete(savepgrp);
459 }
460
461 /*
462  * remove process from process group
463  */
464 int
465 leavepgrp(p)
466         register struct proc *p;
467 {
468         struct pgrp *savepgrp;
469
470         sx_assert(&proctree_lock, SX_XLOCKED);
471         savepgrp = p->p_pgrp;
472         PGRP_LOCK(savepgrp);
473         PROC_LOCK(p);
474         LIST_REMOVE(p, p_pglist);
475         p->p_pgrp = NULL;
476         PROC_UNLOCK(p);
477         PGRP_UNLOCK(savepgrp);
478         if (LIST_EMPTY(&savepgrp->pg_members))
479                 pgdelete(savepgrp);
480         return (0);
481 }
482
483 /*
484  * delete a process group
485  */
486 static void
487 pgdelete(pgrp)
488         register struct pgrp *pgrp;
489 {
490         struct session *savesess;
491         struct tty *tp;
492
493         sx_assert(&proctree_lock, SX_XLOCKED);
494         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
495         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
496
497         /*
498          * Reset any sigio structures pointing to us as a result of
499          * F_SETOWN with our pgid.
500          */
501         funsetownlst(&pgrp->pg_sigiolst);
502
503         PGRP_LOCK(pgrp);
504         tp = pgrp->pg_session->s_ttyp;
505         LIST_REMOVE(pgrp, pg_hash);
506         savesess = pgrp->pg_session;
507         PGRP_UNLOCK(pgrp);
508
509         /* Remove the reference to the pgrp before deallocating it. */
510         if (tp != NULL) {
511                 tty_lock(tp);
512                 tty_rel_pgrp(tp, pgrp);
513         }
514
515         mtx_destroy(&pgrp->pg_mtx);
516         free(pgrp, M_PGRP);
517         sess_release(savesess);
518 }
519
520 static void
521 pgadjustjobc(pgrp, entering)
522         struct pgrp *pgrp;
523         int entering;
524 {
525
526         PGRP_LOCK(pgrp);
527         if (entering)
528                 pgrp->pg_jobc++;
529         else {
530                 --pgrp->pg_jobc;
531                 if (pgrp->pg_jobc == 0)
532                         orphanpg(pgrp);
533         }
534         PGRP_UNLOCK(pgrp);
535 }
536
537 /*
538  * Adjust pgrp jobc counters when specified process changes process group.
539  * We count the number of processes in each process group that "qualify"
540  * the group for terminal job control (those with a parent in a different
541  * process group of the same session).  If that count reaches zero, the
542  * process group becomes orphaned.  Check both the specified process'
543  * process group and that of its children.
544  * entering == 0 => p is leaving specified group.
545  * entering == 1 => p is entering specified group.
546  */
547 void
548 fixjobc(p, pgrp, entering)
549         register struct proc *p;
550         register struct pgrp *pgrp;
551         int entering;
552 {
553         register struct pgrp *hispgrp;
554         register struct session *mysession;
555
556         sx_assert(&proctree_lock, SX_LOCKED);
557         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
558         PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
559         SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
560
561         /*
562          * Check p's parent to see whether p qualifies its own process
563          * group; if so, adjust count for p's process group.
564          */
565         mysession = pgrp->pg_session;
566         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
567             hispgrp->pg_session == mysession)
568                 pgadjustjobc(pgrp, entering);
569
570         /*
571          * Check this process' children to see whether they qualify
572          * their process groups; if so, adjust counts for children's
573          * process groups.
574          */
575         LIST_FOREACH(p, &p->p_children, p_sibling) {
576                 hispgrp = p->p_pgrp;
577                 if (hispgrp == pgrp ||
578                     hispgrp->pg_session != mysession)
579                         continue;
580                 PROC_LOCK(p);
581                 if (p->p_state == PRS_ZOMBIE) {
582                         PROC_UNLOCK(p);
583                         continue;
584                 }
585                 PROC_UNLOCK(p);
586                 pgadjustjobc(hispgrp, entering);
587         }
588 }
589
590 /*
591  * A process group has become orphaned;
592  * if there are any stopped processes in the group,
593  * hang-up all process in that group.
594  */
595 static void
596 orphanpg(pg)
597         struct pgrp *pg;
598 {
599         register struct proc *p;
600
601         PGRP_LOCK_ASSERT(pg, MA_OWNED);
602
603         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
604                 PROC_LOCK(p);
605                 if (P_SHOULDSTOP(p)) {
606                         PROC_UNLOCK(p);
607                         LIST_FOREACH(p, &pg->pg_members, p_pglist) {
608                                 PROC_LOCK(p);
609                                 psignal(p, SIGHUP);
610                                 psignal(p, SIGCONT);
611                                 PROC_UNLOCK(p);
612                         }
613                         return;
614                 }
615                 PROC_UNLOCK(p);
616         }
617 }
618
619 void
620 sess_hold(struct session *s)
621 {
622
623         refcount_acquire(&s->s_count);
624 }
625
626 void
627 sess_release(struct session *s)
628 {
629
630         if (refcount_release(&s->s_count)) {
631                 if (s->s_ttyp != NULL) {
632                         tty_lock(s->s_ttyp);
633                         tty_rel_sess(s->s_ttyp, s);
634                 }
635                 mtx_destroy(&s->s_mtx);
636                 free(s, M_SESSION);
637         }
638 }
639
640 #include "opt_ddb.h"
641 #ifdef DDB
642 #include <ddb/ddb.h>
643
644 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
645 {
646         register struct pgrp *pgrp;
647         register struct proc *p;
648         register int i;
649
650         for (i = 0; i <= pgrphash; i++) {
651                 if (!LIST_EMPTY(&pgrphashtbl[i])) {
652                         printf("\tindx %d\n", i);
653                         LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
654                                 printf(
655                         "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
656                                     (void *)pgrp, (long)pgrp->pg_id,
657                                     (void *)pgrp->pg_session,
658                                     pgrp->pg_session->s_count,
659                                     (void *)LIST_FIRST(&pgrp->pg_members));
660                                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
661                                         printf("\t\tpid %ld addr %p pgrp %p\n", 
662                                             (long)p->p_pid, (void *)p,
663                                             (void *)p->p_pgrp);
664                                 }
665                         }
666                 }
667         }
668 }
669 #endif /* DDB */
670
671 /*
672  * Calculate the kinfo_proc members which contain process-wide
673  * informations.
674  * Must be called with the target process locked.
675  */
676 static void
677 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
678 {
679         struct thread *td;
680
681         PROC_LOCK_ASSERT(p, MA_OWNED);
682
683         kp->ki_estcpu = 0;
684         kp->ki_pctcpu = 0;
685         FOREACH_THREAD_IN_PROC(p, td) {
686                 thread_lock(td);
687                 kp->ki_pctcpu += sched_pctcpu(td);
688                 kp->ki_estcpu += td->td_estcpu;
689                 thread_unlock(td);
690         }
691 }
692
693 /*
694  * Clear kinfo_proc and fill in any information that is common
695  * to all threads in the process.
696  * Must be called with the target process locked.
697  */
698 static void
699 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
700 {
701         struct thread *td0;
702         struct tty *tp;
703         struct session *sp;
704         struct ucred *cred;
705         struct sigacts *ps;
706
707         PROC_LOCK_ASSERT(p, MA_OWNED);
708         bzero(kp, sizeof(*kp));
709
710         kp->ki_structsize = sizeof(*kp);
711         kp->ki_paddr = p;
712         kp->ki_addr =/* p->p_addr; */0; /* XXX */
713         kp->ki_args = p->p_args;
714         kp->ki_textvp = p->p_textvp;
715 #ifdef KTRACE
716         kp->ki_tracep = p->p_tracevp;
717         kp->ki_traceflag = p->p_traceflag;
718 #endif
719         kp->ki_fd = p->p_fd;
720         kp->ki_vmspace = p->p_vmspace;
721         kp->ki_flag = p->p_flag;
722         cred = p->p_ucred;
723         if (cred) {
724                 kp->ki_uid = cred->cr_uid;
725                 kp->ki_ruid = cred->cr_ruid;
726                 kp->ki_svuid = cred->cr_svuid;
727                 kp->ki_cr_flags = cred->cr_flags;
728                 /* XXX bde doesn't like KI_NGROUPS */
729                 if (cred->cr_ngroups > KI_NGROUPS) {
730                         kp->ki_ngroups = KI_NGROUPS;
731                         kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
732                 } else
733                         kp->ki_ngroups = cred->cr_ngroups;
734                 bcopy(cred->cr_groups, kp->ki_groups,
735                     kp->ki_ngroups * sizeof(gid_t));
736                 kp->ki_rgid = cred->cr_rgid;
737                 kp->ki_svgid = cred->cr_svgid;
738                 /* If jailed(cred), emulate the old P_JAILED flag. */
739                 if (jailed(cred)) {
740                         kp->ki_flag |= P_JAILED;
741                         /* If inside the jail, use 0 as a jail ID. */
742                         if (cred->cr_prison != curthread->td_ucred->cr_prison)
743                                 kp->ki_jid = cred->cr_prison->pr_id;
744                 }
745         }
746         ps = p->p_sigacts;
747         if (ps) {
748                 mtx_lock(&ps->ps_mtx);
749                 kp->ki_sigignore = ps->ps_sigignore;
750                 kp->ki_sigcatch = ps->ps_sigcatch;
751                 mtx_unlock(&ps->ps_mtx);
752         }
753         PROC_SLOCK(p);
754         if (p->p_state != PRS_NEW &&
755             p->p_state != PRS_ZOMBIE &&
756             p->p_vmspace != NULL) {
757                 struct vmspace *vm = p->p_vmspace;
758
759                 kp->ki_size = vm->vm_map.size;
760                 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
761                 FOREACH_THREAD_IN_PROC(p, td0) {
762                         if (!TD_IS_SWAPPED(td0))
763                                 kp->ki_rssize += td0->td_kstack_pages;
764                 }
765                 kp->ki_swrss = vm->vm_swrss;
766                 kp->ki_tsize = vm->vm_tsize;
767                 kp->ki_dsize = vm->vm_dsize;
768                 kp->ki_ssize = vm->vm_ssize;
769         } else if (p->p_state == PRS_ZOMBIE)
770                 kp->ki_stat = SZOMB;
771         if (kp->ki_flag & P_INMEM)
772                 kp->ki_sflag = PS_INMEM;
773         else
774                 kp->ki_sflag = 0;
775         /* Calculate legacy swtime as seconds since 'swtick'. */
776         kp->ki_swtime = (ticks - p->p_swtick) / hz;
777         kp->ki_pid = p->p_pid;
778         kp->ki_nice = p->p_nice;
779         rufetch(p, &kp->ki_rusage);
780         kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
781         PROC_SUNLOCK(p);
782         if ((p->p_flag & P_INMEM) && p->p_stats != NULL) {
783                 kp->ki_start = p->p_stats->p_start;
784                 timevaladd(&kp->ki_start, &boottime);
785                 PROC_SLOCK(p);
786                 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
787                 PROC_SUNLOCK(p);
788                 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
789
790                 /* Some callers want child-times in a single value */
791                 kp->ki_childtime = kp->ki_childstime;
792                 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
793         }
794         tp = NULL;
795         if (p->p_pgrp) {
796                 kp->ki_pgid = p->p_pgrp->pg_id;
797                 kp->ki_jobc = p->p_pgrp->pg_jobc;
798                 sp = p->p_pgrp->pg_session;
799
800                 if (sp != NULL) {
801                         kp->ki_sid = sp->s_sid;
802                         SESS_LOCK(sp);
803                         strlcpy(kp->ki_login, sp->s_login,
804                             sizeof(kp->ki_login));
805                         if (sp->s_ttyvp)
806                                 kp->ki_kiflag |= KI_CTTY;
807                         if (SESS_LEADER(p))
808                                 kp->ki_kiflag |= KI_SLEADER;
809                         /* XXX proctree_lock */
810                         tp = sp->s_ttyp;
811                         SESS_UNLOCK(sp);
812                 }
813         }
814         if ((p->p_flag & P_CONTROLT) && tp != NULL) {
815                 kp->ki_tdev = tty_udev(tp);
816                 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
817                 if (tp->t_session)
818                         kp->ki_tsid = tp->t_session->s_sid;
819         } else
820                 kp->ki_tdev = NODEV;
821         if (p->p_comm[0] != '\0')
822                 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
823         if (p->p_sysent && p->p_sysent->sv_name != NULL &&
824             p->p_sysent->sv_name[0] != '\0')
825                 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
826         kp->ki_siglist = p->p_siglist;
827         kp->ki_xstat = p->p_xstat;
828         kp->ki_acflag = p->p_acflag;
829         kp->ki_lock = p->p_lock;
830         if (p->p_pptr)
831                 kp->ki_ppid = p->p_pptr->p_pid;
832 }
833
834 /*
835  * Fill in information that is thread specific.  Must be called with
836  * target process locked.  If 'preferthread' is set, overwrite certain
837  * process-related fields that are maintained for both threads and
838  * processes.
839  */
840 static void
841 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
842 {
843         struct proc *p;
844
845         p = td->td_proc;
846         kp->ki_tdaddr = td;
847         PROC_LOCK_ASSERT(p, MA_OWNED);
848
849         thread_lock(td);
850         if (td->td_wmesg != NULL)
851                 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
852         else
853                 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
854         strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
855         if (TD_ON_LOCK(td)) {
856                 kp->ki_kiflag |= KI_LOCKBLOCK;
857                 strlcpy(kp->ki_lockname, td->td_lockname,
858                     sizeof(kp->ki_lockname));
859         } else {
860                 kp->ki_kiflag &= ~KI_LOCKBLOCK;
861                 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
862         }
863
864         if (p->p_state == PRS_NORMAL) { /* approximate. */
865                 if (TD_ON_RUNQ(td) ||
866                     TD_CAN_RUN(td) ||
867                     TD_IS_RUNNING(td)) {
868                         kp->ki_stat = SRUN;
869                 } else if (P_SHOULDSTOP(p)) {
870                         kp->ki_stat = SSTOP;
871                 } else if (TD_IS_SLEEPING(td)) {
872                         kp->ki_stat = SSLEEP;
873                 } else if (TD_ON_LOCK(td)) {
874                         kp->ki_stat = SLOCK;
875                 } else {
876                         kp->ki_stat = SWAIT;
877                 }
878         } else if (p->p_state == PRS_ZOMBIE) {
879                 kp->ki_stat = SZOMB;
880         } else {
881                 kp->ki_stat = SIDL;
882         }
883
884         /* Things in the thread */
885         kp->ki_wchan = td->td_wchan;
886         kp->ki_pri.pri_level = td->td_priority;
887         kp->ki_pri.pri_native = td->td_base_pri;
888         kp->ki_lastcpu = td->td_lastcpu;
889         kp->ki_oncpu = td->td_oncpu;
890         kp->ki_tdflags = td->td_flags;
891         kp->ki_tid = td->td_tid;
892         kp->ki_numthreads = p->p_numthreads;
893         kp->ki_pcb = td->td_pcb;
894         kp->ki_kstack = (void *)td->td_kstack;
895         kp->ki_slptime = (ticks - td->td_slptick) / hz;
896         kp->ki_pri.pri_class = td->td_pri_class;
897         kp->ki_pri.pri_user = td->td_user_pri;
898
899         if (preferthread) {
900                 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
901                 kp->ki_pctcpu = sched_pctcpu(td);
902                 kp->ki_estcpu = td->td_estcpu;
903         }
904
905         /* We can't get this anymore but ps etc never used it anyway. */
906         kp->ki_rqindex = 0;
907
908         if (preferthread)
909                 kp->ki_siglist = td->td_siglist;
910         kp->ki_sigmask = td->td_sigmask;
911         thread_unlock(td);
912 }
913
914 /*
915  * Fill in a kinfo_proc structure for the specified process.
916  * Must be called with the target process locked.
917  */
918 void
919 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
920 {
921
922         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
923
924         fill_kinfo_proc_only(p, kp);
925         fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
926         fill_kinfo_aggregate(p, kp);
927 }
928
929 struct pstats *
930 pstats_alloc(void)
931 {
932
933         return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
934 }
935
936 /*
937  * Copy parts of p_stats; zero the rest of p_stats (statistics).
938  */
939 void
940 pstats_fork(struct pstats *src, struct pstats *dst)
941 {
942
943         bzero(&dst->pstat_startzero,
944             __rangeof(struct pstats, pstat_startzero, pstat_endzero));
945         bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
946             __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
947 }
948
949 void
950 pstats_free(struct pstats *ps)
951 {
952
953         free(ps, M_SUBPROC);
954 }
955
956 /*
957  * Locate a zombie process by number
958  */
959 struct proc *
960 zpfind(pid_t pid)
961 {
962         struct proc *p;
963
964         sx_slock(&allproc_lock);
965         LIST_FOREACH(p, &zombproc, p_list)
966                 if (p->p_pid == pid) {
967                         PROC_LOCK(p);
968                         break;
969                 }
970         sx_sunlock(&allproc_lock);
971         return (p);
972 }
973
974 #define KERN_PROC_ZOMBMASK      0x3
975 #define KERN_PROC_NOTHREADS     0x4
976
977 #ifdef COMPAT_FREEBSD32
978
979 /*
980  * This function is typically used to copy out the kernel address, so
981  * it can be replaced by assignment of zero.
982  */
983 static inline uint32_t
984 ptr32_trim(void *ptr)
985 {
986         uintptr_t uptr;
987
988         uptr = (uintptr_t)ptr;
989         return ((uptr > UINT_MAX) ? 0 : uptr);
990 }
991
992 #define PTRTRIM_CP(src,dst,fld) \
993         do { (dst).fld = ptr32_trim((src).fld); } while (0)
994
995 static void
996 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
997 {
998         int i;
999
1000         bzero(ki32, sizeof(struct kinfo_proc32));
1001         ki32->ki_structsize = sizeof(struct kinfo_proc32);
1002         CP(*ki, *ki32, ki_layout);
1003         PTRTRIM_CP(*ki, *ki32, ki_args);
1004         PTRTRIM_CP(*ki, *ki32, ki_paddr);
1005         PTRTRIM_CP(*ki, *ki32, ki_addr);
1006         PTRTRIM_CP(*ki, *ki32, ki_tracep);
1007         PTRTRIM_CP(*ki, *ki32, ki_textvp);
1008         PTRTRIM_CP(*ki, *ki32, ki_fd);
1009         PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1010         PTRTRIM_CP(*ki, *ki32, ki_wchan);
1011         CP(*ki, *ki32, ki_pid);
1012         CP(*ki, *ki32, ki_ppid);
1013         CP(*ki, *ki32, ki_pgid);
1014         CP(*ki, *ki32, ki_tpgid);
1015         CP(*ki, *ki32, ki_sid);
1016         CP(*ki, *ki32, ki_tsid);
1017         CP(*ki, *ki32, ki_jobc);
1018         CP(*ki, *ki32, ki_tdev);
1019         CP(*ki, *ki32, ki_siglist);
1020         CP(*ki, *ki32, ki_sigmask);
1021         CP(*ki, *ki32, ki_sigignore);
1022         CP(*ki, *ki32, ki_sigcatch);
1023         CP(*ki, *ki32, ki_uid);
1024         CP(*ki, *ki32, ki_ruid);
1025         CP(*ki, *ki32, ki_svuid);
1026         CP(*ki, *ki32, ki_rgid);
1027         CP(*ki, *ki32, ki_svgid);
1028         CP(*ki, *ki32, ki_ngroups);
1029         for (i = 0; i < KI_NGROUPS; i++)
1030                 CP(*ki, *ki32, ki_groups[i]);
1031         CP(*ki, *ki32, ki_size);
1032         CP(*ki, *ki32, ki_rssize);
1033         CP(*ki, *ki32, ki_swrss);
1034         CP(*ki, *ki32, ki_tsize);
1035         CP(*ki, *ki32, ki_dsize);
1036         CP(*ki, *ki32, ki_ssize);
1037         CP(*ki, *ki32, ki_xstat);
1038         CP(*ki, *ki32, ki_acflag);
1039         CP(*ki, *ki32, ki_pctcpu);
1040         CP(*ki, *ki32, ki_estcpu);
1041         CP(*ki, *ki32, ki_slptime);
1042         CP(*ki, *ki32, ki_swtime);
1043         CP(*ki, *ki32, ki_runtime);
1044         TV_CP(*ki, *ki32, ki_start);
1045         TV_CP(*ki, *ki32, ki_childtime);
1046         CP(*ki, *ki32, ki_flag);
1047         CP(*ki, *ki32, ki_kiflag);
1048         CP(*ki, *ki32, ki_traceflag);
1049         CP(*ki, *ki32, ki_stat);
1050         CP(*ki, *ki32, ki_nice);
1051         CP(*ki, *ki32, ki_lock);
1052         CP(*ki, *ki32, ki_rqindex);
1053         CP(*ki, *ki32, ki_oncpu);
1054         CP(*ki, *ki32, ki_lastcpu);
1055         bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1);
1056         bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1057         bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1058         bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1059         bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1060         bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1061         CP(*ki, *ki32, ki_cr_flags);
1062         CP(*ki, *ki32, ki_jid);
1063         CP(*ki, *ki32, ki_numthreads);
1064         CP(*ki, *ki32, ki_tid);
1065         CP(*ki, *ki32, ki_pri);
1066         freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1067         freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1068         PTRTRIM_CP(*ki, *ki32, ki_pcb);
1069         PTRTRIM_CP(*ki, *ki32, ki_kstack);
1070         PTRTRIM_CP(*ki, *ki32, ki_udata);
1071         CP(*ki, *ki32, ki_sflag);
1072         CP(*ki, *ki32, ki_tdflags);
1073 }
1074
1075 static int
1076 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1077 {
1078         struct kinfo_proc32 ki32;
1079         int error;
1080
1081         if (req->flags & SCTL_MASK32) {
1082                 freebsd32_kinfo_proc_out(ki, &ki32);
1083                 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
1084         } else
1085                 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
1086         return (error);
1087 }
1088 #else
1089 static int
1090 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1091 {
1092
1093         return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
1094 }
1095 #endif
1096
1097 /*
1098  * Must be called with the process locked and will return with it unlocked.
1099  */
1100 static int
1101 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1102 {
1103         struct thread *td;
1104         struct kinfo_proc kinfo_proc;
1105         int error = 0;
1106         struct proc *np;
1107         pid_t pid = p->p_pid;
1108
1109         PROC_LOCK_ASSERT(p, MA_OWNED);
1110         MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1111
1112         fill_kinfo_proc(p, &kinfo_proc);
1113         if (flags & KERN_PROC_NOTHREADS)
1114                 error = sysctl_out_proc_copyout(&kinfo_proc, req);
1115         else {
1116                 FOREACH_THREAD_IN_PROC(p, td) {
1117                         fill_kinfo_thread(td, &kinfo_proc, 1);
1118                         error = sysctl_out_proc_copyout(&kinfo_proc, req);
1119                         if (error)
1120                                 break;
1121                 }
1122         }
1123         PROC_UNLOCK(p);
1124         if (error)
1125                 return (error);
1126         if (flags & KERN_PROC_ZOMBMASK)
1127                 np = zpfind(pid);
1128         else {
1129                 if (pid == 0)
1130                         return (0);
1131                 np = pfind(pid);
1132         }
1133         if (np == NULL)
1134                 return (ESRCH);
1135         if (np != p) {
1136                 PROC_UNLOCK(np);
1137                 return (ESRCH);
1138         }
1139         PROC_UNLOCK(np);
1140         return (0);
1141 }
1142
1143 static int
1144 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1145 {
1146         int *name = (int*) arg1;
1147         u_int namelen = arg2;
1148         struct proc *p;
1149         int flags, doingzomb, oid_number;
1150         int error = 0;
1151
1152         oid_number = oidp->oid_number;
1153         if (oid_number != KERN_PROC_ALL &&
1154             (oid_number & KERN_PROC_INC_THREAD) == 0)
1155                 flags = KERN_PROC_NOTHREADS;
1156         else {
1157                 flags = 0;
1158                 oid_number &= ~KERN_PROC_INC_THREAD;
1159         }
1160         if (oid_number == KERN_PROC_PID) {
1161                 if (namelen != 1) 
1162                         return (EINVAL);
1163                 error = sysctl_wire_old_buffer(req, 0);
1164                 if (error)
1165                         return (error);         
1166                 p = pfind((pid_t)name[0]);
1167                 if (!p)
1168                         return (ESRCH);
1169                 if ((error = p_cansee(curthread, p))) {
1170                         PROC_UNLOCK(p);
1171                         return (error);
1172                 }
1173                 error = sysctl_out_proc(p, req, flags);
1174                 return (error);
1175         }
1176
1177         switch (oid_number) {
1178         case KERN_PROC_ALL:
1179                 if (namelen != 0)
1180                         return (EINVAL);
1181                 break;
1182         case KERN_PROC_PROC:
1183                 if (namelen != 0 && namelen != 1)
1184                         return (EINVAL);
1185                 break;
1186         default:
1187                 if (namelen != 1)
1188                         return (EINVAL);
1189                 break;
1190         }
1191         
1192         if (!req->oldptr) {
1193                 /* overestimate by 5 procs */
1194                 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1195                 if (error)
1196                         return (error);
1197         }
1198         error = sysctl_wire_old_buffer(req, 0);
1199         if (error != 0)
1200                 return (error);
1201         sx_slock(&allproc_lock);
1202         for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1203                 if (!doingzomb)
1204                         p = LIST_FIRST(&allproc);
1205                 else
1206                         p = LIST_FIRST(&zombproc);
1207                 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1208                         /*
1209                          * Skip embryonic processes.
1210                          */
1211                         PROC_SLOCK(p);
1212                         if (p->p_state == PRS_NEW) {
1213                                 PROC_SUNLOCK(p);
1214                                 continue;
1215                         }
1216                         PROC_SUNLOCK(p);
1217                         PROC_LOCK(p);
1218                         KASSERT(p->p_ucred != NULL,
1219                             ("process credential is NULL for non-NEW proc"));
1220                         /*
1221                          * Show a user only appropriate processes.
1222                          */
1223                         if (p_cansee(curthread, p)) {
1224                                 PROC_UNLOCK(p);
1225                                 continue;
1226                         }
1227                         /*
1228                          * TODO - make more efficient (see notes below).
1229                          * do by session.
1230                          */
1231                         switch (oid_number) {
1232
1233                         case KERN_PROC_GID:
1234                                 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1235                                         PROC_UNLOCK(p);
1236                                         continue;
1237                                 }
1238                                 break;
1239
1240                         case KERN_PROC_PGRP:
1241                                 /* could do this by traversing pgrp */
1242                                 if (p->p_pgrp == NULL ||
1243                                     p->p_pgrp->pg_id != (pid_t)name[0]) {
1244                                         PROC_UNLOCK(p);
1245                                         continue;
1246                                 }
1247                                 break;
1248
1249                         case KERN_PROC_RGID:
1250                                 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1251                                         PROC_UNLOCK(p);
1252                                         continue;
1253                                 }
1254                                 break;
1255
1256                         case KERN_PROC_SESSION:
1257                                 if (p->p_session == NULL ||
1258                                     p->p_session->s_sid != (pid_t)name[0]) {
1259                                         PROC_UNLOCK(p);
1260                                         continue;
1261                                 }
1262                                 break;
1263
1264                         case KERN_PROC_TTY:
1265                                 if ((p->p_flag & P_CONTROLT) == 0 ||
1266                                     p->p_session == NULL) {
1267                                         PROC_UNLOCK(p);
1268                                         continue;
1269                                 }
1270                                 /* XXX proctree_lock */
1271                                 SESS_LOCK(p->p_session);
1272                                 if (p->p_session->s_ttyp == NULL ||
1273                                     tty_udev(p->p_session->s_ttyp) != 
1274                                     (dev_t)name[0]) {
1275                                         SESS_UNLOCK(p->p_session);
1276                                         PROC_UNLOCK(p);
1277                                         continue;
1278                                 }
1279                                 SESS_UNLOCK(p->p_session);
1280                                 break;
1281
1282                         case KERN_PROC_UID:
1283                                 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1284                                         PROC_UNLOCK(p);
1285                                         continue;
1286                                 }
1287                                 break;
1288
1289                         case KERN_PROC_RUID:
1290                                 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1291                                         PROC_UNLOCK(p);
1292                                         continue;
1293                                 }
1294                                 break;
1295
1296                         case KERN_PROC_PROC:
1297                                 break;
1298
1299                         default:
1300                                 break;
1301
1302                         }
1303
1304                         error = sysctl_out_proc(p, req, flags | doingzomb);
1305                         if (error) {
1306                                 sx_sunlock(&allproc_lock);
1307                                 return (error);
1308                         }
1309                 }
1310         }
1311         sx_sunlock(&allproc_lock);
1312         return (0);
1313 }
1314
1315 struct pargs *
1316 pargs_alloc(int len)
1317 {
1318         struct pargs *pa;
1319
1320         pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1321                 M_WAITOK);
1322         refcount_init(&pa->ar_ref, 1);
1323         pa->ar_length = len;
1324         return (pa);
1325 }
1326
1327 static void
1328 pargs_free(struct pargs *pa)
1329 {
1330
1331         free(pa, M_PARGS);
1332 }
1333
1334 void
1335 pargs_hold(struct pargs *pa)
1336 {
1337
1338         if (pa == NULL)
1339                 return;
1340         refcount_acquire(&pa->ar_ref);
1341 }
1342
1343 void
1344 pargs_drop(struct pargs *pa)
1345 {
1346
1347         if (pa == NULL)
1348                 return;
1349         if (refcount_release(&pa->ar_ref))
1350                 pargs_free(pa);
1351 }
1352
1353 /*
1354  * This sysctl allows a process to retrieve the argument list or process
1355  * title for another process without groping around in the address space
1356  * of the other process.  It also allow a process to set its own "process 
1357  * title to a string of its own choice.
1358  */
1359 static int
1360 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1361 {
1362         int *name = (int*) arg1;
1363         u_int namelen = arg2;
1364         struct pargs *newpa, *pa;
1365         struct proc *p;
1366         int error = 0;
1367
1368         if (namelen != 1) 
1369                 return (EINVAL);
1370
1371         p = pfind((pid_t)name[0]);
1372         if (!p)
1373                 return (ESRCH);
1374
1375         if ((error = p_cansee(curthread, p)) != 0) {
1376                 PROC_UNLOCK(p);
1377                 return (error);
1378         }
1379
1380         if (req->newptr && curproc != p) {
1381                 PROC_UNLOCK(p);
1382                 return (EPERM);
1383         }
1384
1385         pa = p->p_args;
1386         pargs_hold(pa);
1387         PROC_UNLOCK(p);
1388         if (req->oldptr != NULL && pa != NULL)
1389                 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1390         pargs_drop(pa);
1391         if (error != 0 || req->newptr == NULL)
1392                 return (error);
1393
1394         if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1395                 return (ENOMEM);
1396         newpa = pargs_alloc(req->newlen);
1397         error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1398         if (error != 0) {
1399                 pargs_free(newpa);
1400                 return (error);
1401         }
1402         PROC_LOCK(p);
1403         pa = p->p_args;
1404         p->p_args = newpa;
1405         PROC_UNLOCK(p);
1406         pargs_drop(pa);
1407         return (0);
1408 }
1409
1410 /*
1411  * This sysctl allows a process to retrieve the path of the executable for
1412  * itself or another process.
1413  */
1414 static int
1415 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1416 {
1417         pid_t *pidp = (pid_t *)arg1;
1418         unsigned int arglen = arg2;
1419         struct proc *p;
1420         struct vnode *vp;
1421         char *retbuf, *freebuf;
1422         int error, vfslocked;
1423
1424         if (arglen != 1)
1425                 return (EINVAL);
1426         if (*pidp == -1) {      /* -1 means this process */
1427                 p = req->td->td_proc;
1428         } else {
1429                 p = pfind(*pidp);
1430                 if (p == NULL)
1431                         return (ESRCH);
1432                 if ((error = p_cansee(curthread, p)) != 0) {
1433                         PROC_UNLOCK(p);
1434                         return (error);
1435                 }
1436         }
1437
1438         vp = p->p_textvp;
1439         if (vp == NULL) {
1440                 if (*pidp != -1)
1441                         PROC_UNLOCK(p);
1442                 return (0);
1443         }
1444         vref(vp);
1445         if (*pidp != -1)
1446                 PROC_UNLOCK(p);
1447         error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1448         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1449         vrele(vp);
1450         VFS_UNLOCK_GIANT(vfslocked);
1451         if (error)
1452                 return (error);
1453         error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1454         free(freebuf, M_TEMP);
1455         return (error);
1456 }
1457
1458 static int
1459 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1460 {
1461         struct proc *p;
1462         char *sv_name;
1463         int *name;
1464         int namelen;
1465         int error;
1466
1467         namelen = arg2;
1468         if (namelen != 1) 
1469                 return (EINVAL);
1470
1471         name = (int *)arg1;
1472         if ((p = pfind((pid_t)name[0])) == NULL)
1473                 return (ESRCH);
1474         if ((error = p_cansee(curthread, p))) {
1475                 PROC_UNLOCK(p);
1476                 return (error);
1477         }
1478         sv_name = p->p_sysent->sv_name;
1479         PROC_UNLOCK(p);
1480         return (sysctl_handle_string(oidp, sv_name, 0, req));
1481 }
1482
1483 #ifdef KINFO_OVMENTRY_SIZE
1484 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1485 #endif
1486
1487 #ifdef COMPAT_FREEBSD7
1488 static int
1489 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1490 {
1491         vm_map_entry_t entry, tmp_entry;
1492         unsigned int last_timestamp;
1493         char *fullpath, *freepath;
1494         struct kinfo_ovmentry *kve;
1495         struct vattr va;
1496         struct ucred *cred;
1497         int error, *name;
1498         struct vnode *vp;
1499         struct proc *p;
1500         vm_map_t map;
1501         struct vmspace *vm;
1502
1503         name = (int *)arg1;
1504         if ((p = pfind((pid_t)name[0])) == NULL)
1505                 return (ESRCH);
1506         if (p->p_flag & P_WEXIT) {
1507                 PROC_UNLOCK(p);
1508                 return (ESRCH);
1509         }
1510         if ((error = p_candebug(curthread, p))) {
1511                 PROC_UNLOCK(p);
1512                 return (error);
1513         }
1514         _PHOLD(p);
1515         PROC_UNLOCK(p);
1516         vm = vmspace_acquire_ref(p);
1517         if (vm == NULL) {
1518                 PRELE(p);
1519                 return (ESRCH);
1520         }
1521         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1522
1523         map = &p->p_vmspace->vm_map;    /* XXXRW: More locking required? */
1524         vm_map_lock_read(map);
1525         for (entry = map->header.next; entry != &map->header;
1526             entry = entry->next) {
1527                 vm_object_t obj, tobj, lobj;
1528                 vm_offset_t addr;
1529                 int vfslocked;
1530
1531                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1532                         continue;
1533
1534                 bzero(kve, sizeof(*kve));
1535                 kve->kve_structsize = sizeof(*kve);
1536
1537                 kve->kve_private_resident = 0;
1538                 obj = entry->object.vm_object;
1539                 if (obj != NULL) {
1540                         VM_OBJECT_LOCK(obj);
1541                         if (obj->shadow_count == 1)
1542                                 kve->kve_private_resident =
1543                                     obj->resident_page_count;
1544                 }
1545                 kve->kve_resident = 0;
1546                 addr = entry->start;
1547                 while (addr < entry->end) {
1548                         if (pmap_extract(map->pmap, addr))
1549                                 kve->kve_resident++;
1550                         addr += PAGE_SIZE;
1551                 }
1552
1553                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1554                         if (tobj != obj)
1555                                 VM_OBJECT_LOCK(tobj);
1556                         if (lobj != obj)
1557                                 VM_OBJECT_UNLOCK(lobj);
1558                         lobj = tobj;
1559                 }
1560
1561                 kve->kve_start = (void*)entry->start;
1562                 kve->kve_end = (void*)entry->end;
1563                 kve->kve_offset = (off_t)entry->offset;
1564
1565                 if (entry->protection & VM_PROT_READ)
1566                         kve->kve_protection |= KVME_PROT_READ;
1567                 if (entry->protection & VM_PROT_WRITE)
1568                         kve->kve_protection |= KVME_PROT_WRITE;
1569                 if (entry->protection & VM_PROT_EXECUTE)
1570                         kve->kve_protection |= KVME_PROT_EXEC;
1571
1572                 if (entry->eflags & MAP_ENTRY_COW)
1573                         kve->kve_flags |= KVME_FLAG_COW;
1574                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1575                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1576                 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1577                         kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1578
1579                 last_timestamp = map->timestamp;
1580                 vm_map_unlock_read(map);
1581
1582                 kve->kve_fileid = 0;
1583                 kve->kve_fsid = 0;
1584                 freepath = NULL;
1585                 fullpath = "";
1586                 if (lobj) {
1587                         vp = NULL;
1588                         switch (lobj->type) {
1589                         case OBJT_DEFAULT:
1590                                 kve->kve_type = KVME_TYPE_DEFAULT;
1591                                 break;
1592                         case OBJT_VNODE:
1593                                 kve->kve_type = KVME_TYPE_VNODE;
1594                                 vp = lobj->handle;
1595                                 vref(vp);
1596                                 break;
1597                         case OBJT_SWAP:
1598                                 kve->kve_type = KVME_TYPE_SWAP;
1599                                 break;
1600                         case OBJT_DEVICE:
1601                                 kve->kve_type = KVME_TYPE_DEVICE;
1602                                 break;
1603                         case OBJT_PHYS:
1604                                 kve->kve_type = KVME_TYPE_PHYS;
1605                                 break;
1606                         case OBJT_DEAD:
1607                                 kve->kve_type = KVME_TYPE_DEAD;
1608                                 break;
1609                         case OBJT_SG:
1610                                 kve->kve_type = KVME_TYPE_SG;
1611                                 break;
1612                         default:
1613                                 kve->kve_type = KVME_TYPE_UNKNOWN;
1614                                 break;
1615                         }
1616                         if (lobj != obj)
1617                                 VM_OBJECT_UNLOCK(lobj);
1618
1619                         kve->kve_ref_count = obj->ref_count;
1620                         kve->kve_shadow_count = obj->shadow_count;
1621                         VM_OBJECT_UNLOCK(obj);
1622                         if (vp != NULL) {
1623                                 vn_fullpath(curthread, vp, &fullpath,
1624                                     &freepath);
1625                                 cred = curthread->td_ucred;
1626                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1627                                 vn_lock(vp, LK_SHARED | LK_RETRY);
1628                                 if (VOP_GETATTR(vp, &va, cred) == 0) {
1629                                         kve->kve_fileid = va.va_fileid;
1630                                         kve->kve_fsid = va.va_fsid;
1631                                 }
1632                                 vput(vp);
1633                                 VFS_UNLOCK_GIANT(vfslocked);
1634                         }
1635                 } else {
1636                         kve->kve_type = KVME_TYPE_NONE;
1637                         kve->kve_ref_count = 0;
1638                         kve->kve_shadow_count = 0;
1639                 }
1640
1641                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1642                 if (freepath != NULL)
1643                         free(freepath, M_TEMP);
1644
1645                 error = SYSCTL_OUT(req, kve, sizeof(*kve));
1646                 vm_map_lock_read(map);
1647                 if (error)
1648                         break;
1649                 if (last_timestamp != map->timestamp) {
1650                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1651                         entry = tmp_entry;
1652                 }
1653         }
1654         vm_map_unlock_read(map);
1655         vmspace_free(vm);
1656         PRELE(p);
1657         free(kve, M_TEMP);
1658         return (error);
1659 }
1660 #endif  /* COMPAT_FREEBSD7 */
1661
1662 #ifdef KINFO_VMENTRY_SIZE
1663 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1664 #endif
1665
1666 static int
1667 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
1668 {
1669         vm_map_entry_t entry, tmp_entry;
1670         unsigned int last_timestamp;
1671         char *fullpath, *freepath;
1672         struct kinfo_vmentry *kve;
1673         struct vattr va;
1674         struct ucred *cred;
1675         int error, *name;
1676         struct vnode *vp;
1677         struct proc *p;
1678         struct vmspace *vm;
1679         vm_map_t map;
1680
1681         name = (int *)arg1;
1682         if ((p = pfind((pid_t)name[0])) == NULL)
1683                 return (ESRCH);
1684         if (p->p_flag & P_WEXIT) {
1685                 PROC_UNLOCK(p);
1686                 return (ESRCH);
1687         }
1688         if ((error = p_candebug(curthread, p))) {
1689                 PROC_UNLOCK(p);
1690                 return (error);
1691         }
1692         _PHOLD(p);
1693         PROC_UNLOCK(p);
1694         vm = vmspace_acquire_ref(p);
1695         if (vm == NULL) {
1696                 PRELE(p);
1697                 return (ESRCH);
1698         }
1699         kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1700
1701         map = &vm->vm_map;      /* XXXRW: More locking required? */
1702         vm_map_lock_read(map);
1703         for (entry = map->header.next; entry != &map->header;
1704             entry = entry->next) {
1705                 vm_object_t obj, tobj, lobj;
1706                 vm_offset_t addr;
1707                 int vfslocked;
1708
1709                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1710                         continue;
1711
1712                 bzero(kve, sizeof(*kve));
1713
1714                 kve->kve_private_resident = 0;
1715                 obj = entry->object.vm_object;
1716                 if (obj != NULL) {
1717                         VM_OBJECT_LOCK(obj);
1718                         if (obj->shadow_count == 1)
1719                                 kve->kve_private_resident =
1720                                     obj->resident_page_count;
1721                 }
1722                 kve->kve_resident = 0;
1723                 addr = entry->start;
1724                 while (addr < entry->end) {
1725                         if (pmap_extract(map->pmap, addr))
1726                                 kve->kve_resident++;
1727                         addr += PAGE_SIZE;
1728                 }
1729
1730                 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1731                         if (tobj != obj)
1732                                 VM_OBJECT_LOCK(tobj);
1733                         if (lobj != obj)
1734                                 VM_OBJECT_UNLOCK(lobj);
1735                         lobj = tobj;
1736                 }
1737
1738                 kve->kve_start = entry->start;
1739                 kve->kve_end = entry->end;
1740                 kve->kve_offset = entry->offset;
1741
1742                 if (entry->protection & VM_PROT_READ)
1743                         kve->kve_protection |= KVME_PROT_READ;
1744                 if (entry->protection & VM_PROT_WRITE)
1745                         kve->kve_protection |= KVME_PROT_WRITE;
1746                 if (entry->protection & VM_PROT_EXECUTE)
1747                         kve->kve_protection |= KVME_PROT_EXEC;
1748
1749                 if (entry->eflags & MAP_ENTRY_COW)
1750                         kve->kve_flags |= KVME_FLAG_COW;
1751                 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1752                         kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1753                 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1754                         kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1755
1756                 last_timestamp = map->timestamp;
1757                 vm_map_unlock_read(map);
1758
1759                 kve->kve_fileid = 0;
1760                 kve->kve_fsid = 0;
1761                 freepath = NULL;
1762                 fullpath = "";
1763                 if (lobj) {
1764                         vp = NULL;
1765                         switch (lobj->type) {
1766                         case OBJT_DEFAULT:
1767                                 kve->kve_type = KVME_TYPE_DEFAULT;
1768                                 break;
1769                         case OBJT_VNODE:
1770                                 kve->kve_type = KVME_TYPE_VNODE;
1771                                 vp = lobj->handle;
1772                                 vref(vp);
1773                                 break;
1774                         case OBJT_SWAP:
1775                                 kve->kve_type = KVME_TYPE_SWAP;
1776                                 break;
1777                         case OBJT_DEVICE:
1778                                 kve->kve_type = KVME_TYPE_DEVICE;
1779                                 break;
1780                         case OBJT_PHYS:
1781                                 kve->kve_type = KVME_TYPE_PHYS;
1782                                 break;
1783                         case OBJT_DEAD:
1784                                 kve->kve_type = KVME_TYPE_DEAD;
1785                                 break;
1786                         case OBJT_SG:
1787                                 kve->kve_type = KVME_TYPE_SG;
1788                                 break;
1789                         default:
1790                                 kve->kve_type = KVME_TYPE_UNKNOWN;
1791                                 break;
1792                         }
1793                         if (lobj != obj)
1794                                 VM_OBJECT_UNLOCK(lobj);
1795
1796                         kve->kve_ref_count = obj->ref_count;
1797                         kve->kve_shadow_count = obj->shadow_count;
1798                         VM_OBJECT_UNLOCK(obj);
1799                         if (vp != NULL) {
1800                                 vn_fullpath(curthread, vp, &fullpath,
1801                                     &freepath);
1802                                 cred = curthread->td_ucred;
1803                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1804                                 vn_lock(vp, LK_SHARED | LK_RETRY);
1805                                 if (VOP_GETATTR(vp, &va, cred) == 0) {
1806                                         kve->kve_fileid = va.va_fileid;
1807                                         kve->kve_fsid = va.va_fsid;
1808                                 }
1809                                 vput(vp);
1810                                 VFS_UNLOCK_GIANT(vfslocked);
1811                         }
1812                 } else {
1813                         kve->kve_type = KVME_TYPE_NONE;
1814                         kve->kve_ref_count = 0;
1815                         kve->kve_shadow_count = 0;
1816                 }
1817
1818                 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1819                 if (freepath != NULL)
1820                         free(freepath, M_TEMP);
1821
1822                 /* Pack record size down */
1823                 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
1824                     strlen(kve->kve_path) + 1;
1825                 kve->kve_structsize = roundup(kve->kve_structsize,
1826                     sizeof(uint64_t));
1827                 error = SYSCTL_OUT(req, kve, kve->kve_structsize);
1828                 vm_map_lock_read(map);
1829                 if (error)
1830                         break;
1831                 if (last_timestamp != map->timestamp) {
1832                         vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1833                         entry = tmp_entry;
1834                 }
1835         }
1836         vm_map_unlock_read(map);
1837         vmspace_free(vm);
1838         PRELE(p);
1839         free(kve, M_TEMP);
1840         return (error);
1841 }
1842
1843 #if defined(STACK) || defined(DDB)
1844 static int
1845 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
1846 {
1847         struct kinfo_kstack *kkstp;
1848         int error, i, *name, numthreads;
1849         lwpid_t *lwpidarray;
1850         struct thread *td;
1851         struct stack *st;
1852         struct sbuf sb;
1853         struct proc *p;
1854
1855         name = (int *)arg1;
1856         if ((p = pfind((pid_t)name[0])) == NULL)
1857                 return (ESRCH);
1858         /* XXXRW: Not clear ESRCH is the right error during proc execve(). */
1859         if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) {
1860                 PROC_UNLOCK(p);
1861                 return (ESRCH);
1862         }
1863         if ((error = p_candebug(curthread, p))) {
1864                 PROC_UNLOCK(p);
1865                 return (error);
1866         }
1867         _PHOLD(p);
1868         PROC_UNLOCK(p);
1869
1870         kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
1871         st = stack_create();
1872
1873         lwpidarray = NULL;
1874         numthreads = 0;
1875         PROC_LOCK(p);
1876 repeat:
1877         if (numthreads < p->p_numthreads) {
1878                 if (lwpidarray != NULL) {
1879                         free(lwpidarray, M_TEMP);
1880                         lwpidarray = NULL;
1881                 }
1882                 numthreads = p->p_numthreads;
1883                 PROC_UNLOCK(p);
1884                 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
1885                     M_WAITOK | M_ZERO);
1886                 PROC_LOCK(p);
1887                 goto repeat;
1888         }
1889         i = 0;
1890
1891         /*
1892          * XXXRW: During the below loop, execve(2) and countless other sorts
1893          * of changes could have taken place.  Should we check to see if the
1894          * vmspace has been replaced, or the like, in order to prevent
1895          * giving a snapshot that spans, say, execve(2), with some threads
1896          * before and some after?  Among other things, the credentials could
1897          * have changed, in which case the right to extract debug info might
1898          * no longer be assured.
1899          */
1900         FOREACH_THREAD_IN_PROC(p, td) {
1901                 KASSERT(i < numthreads,
1902                     ("sysctl_kern_proc_kstack: numthreads"));
1903                 lwpidarray[i] = td->td_tid;
1904                 i++;
1905         }
1906         numthreads = i;
1907         for (i = 0; i < numthreads; i++) {
1908                 td = thread_find(p, lwpidarray[i]);
1909                 if (td == NULL) {
1910                         continue;
1911                 }
1912                 bzero(kkstp, sizeof(*kkstp));
1913                 (void)sbuf_new(&sb, kkstp->kkst_trace,
1914                     sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
1915                 thread_lock(td);
1916                 kkstp->kkst_tid = td->td_tid;
1917                 if (TD_IS_SWAPPED(td))
1918                         kkstp->kkst_state = KKST_STATE_SWAPPED;
1919                 else if (TD_IS_RUNNING(td))
1920                         kkstp->kkst_state = KKST_STATE_RUNNING;
1921                 else {
1922                         kkstp->kkst_state = KKST_STATE_STACKOK;
1923                         stack_save_td(st, td);
1924                 }
1925                 thread_unlock(td);
1926                 PROC_UNLOCK(p);
1927                 stack_sbuf_print(&sb, st);
1928                 sbuf_finish(&sb);
1929                 sbuf_delete(&sb);
1930                 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
1931                 PROC_LOCK(p);
1932                 if (error)
1933                         break;
1934         }
1935         _PRELE(p);
1936         PROC_UNLOCK(p);
1937         if (lwpidarray != NULL)
1938                 free(lwpidarray, M_TEMP);
1939         stack_destroy(st);
1940         free(kkstp, M_TEMP);
1941         return (error);
1942 }
1943 #endif
1944
1945 /*
1946  * This sysctl allows a process to retrieve the full list of groups from
1947  * itself or another process.
1948  */
1949 static int
1950 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
1951 {
1952         pid_t *pidp = (pid_t *)arg1;
1953         unsigned int arglen = arg2;
1954         struct proc *p;
1955         struct ucred *cred;
1956         int error;
1957
1958         if (arglen != 1)
1959                 return (EINVAL);
1960         if (*pidp == -1) {      /* -1 means this process */
1961                 p = req->td->td_proc;
1962         } else {
1963                 p = pfind(*pidp);
1964                 if (p == NULL)
1965                         return (ESRCH);
1966                 if ((error = p_cansee(curthread, p)) != 0) {
1967                         PROC_UNLOCK(p);
1968                         return (error);
1969                 }
1970         }
1971
1972         cred = crhold(p->p_ucred);
1973         if (*pidp != -1)
1974                 PROC_UNLOCK(p);
1975
1976         error = SYSCTL_OUT(req, cred->cr_groups,
1977             cred->cr_ngroups * sizeof(gid_t));
1978         crfree(cred);
1979         return (error);
1980 }
1981
1982 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1983
1984 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
1985         CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
1986         "Return entire process table");
1987
1988 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1989         sysctl_kern_proc, "Process table");
1990
1991 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
1992         sysctl_kern_proc, "Process table");
1993
1994 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1995         sysctl_kern_proc, "Process table");
1996
1997 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
1998         CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1999
2000 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 
2001         sysctl_kern_proc, "Process table");
2002
2003 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 
2004         sysctl_kern_proc, "Process table");
2005
2006 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2007         sysctl_kern_proc, "Process table");
2008
2009 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2010         sysctl_kern_proc, "Process table");
2011
2012 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2013         sysctl_kern_proc, "Return process table, no threads");
2014
2015 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2016         CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2017         sysctl_kern_proc_args, "Process argument list");
2018
2019 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2020         CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2021
2022 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2023         CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2024         "Process syscall vector name (ABI type)");
2025
2026 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2027         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2028
2029 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2030         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2031
2032 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2033         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2034
2035 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2036         sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2037
2038 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2039         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2040
2041 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2042         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2043
2044 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2045         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2046
2047 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2048         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2049
2050 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2051         CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2052         "Return process table, no threads");
2053
2054 #ifdef COMPAT_FREEBSD7
2055 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2056         CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2057 #endif
2058
2059 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2060         CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2061
2062 #if defined(STACK) || defined(DDB)
2063 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2064         CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2065 #endif
2066
2067 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2068         CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");