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