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