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