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