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