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