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