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