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