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