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