19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes *
7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes * are met:
10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
1569a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes * without specific prior written permission.
18df8bae1dSRodney W. Grimes *
19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes */
31df8bae1dSRodney W. Grimes
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33cc43c38cSRobert Watson #include "opt_ddb.h"
346c84de02SJohn Baldwin #include "opt_ktrace.h"
35b9f009b0SPeter Wemm #include "opt_kstack_pages.h"
361cc8c45cSRobert Watson #include "opt_stack.h"
376c84de02SJohn Baldwin
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
4034ebdceaSMateusz Guzik #include <sys/bitstring.h>
41ac9b565bSKonstantin Belousov #include <sys/conf.h>
42c5cfcb1cSMikolaj Golub #include <sys/elf.h>
43b4490c6eSKonstantin Belousov #include <sys/eventhandler.h>
44c5cfcb1cSMikolaj Golub #include <sys/exec.h>
45ee92c8a8SKonstantin Belousov #include <sys/fcntl.h>
46d3dd6bd4SKonstantin Belousov #include <sys/ipc.h>
47b4490c6eSKonstantin Belousov #include <sys/jail.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
4905e06d11SKonstantin Belousov #include <sys/limits.h>
50fb919e4dSMark Murray #include <sys/lock.h>
517123f4cdSEdward Tomasz Napierala #include <sys/loginclass.h>
52df8bae1dSRodney W. Grimes #include <sys/malloc.h>
535384d089SMikolaj Golub #include <sys/mman.h>
54cc43c38cSRobert Watson #include <sys/mount.h>
55fb919e4dSMark Murray #include <sys/mutex.h>
56ee92c8a8SKonstantin Belousov #include <sys/namei.h>
57b9df5231SPoul-Henning Kamp #include <sys/proc.h>
58c5cfcb1cSMikolaj Golub #include <sys/ptrace.h>
5955b4a5aeSJohn Baldwin #include <sys/refcount.h>
609e7d0583SMikolaj Golub #include <sys/resourcevar.h>
6189f6b863SAttilio Rao #include <sys/rwlock.h>
62cc43c38cSRobert Watson #include <sys/sbuf.h>
63baf731e6SRobert Drehmel #include <sys/sysent.h>
64de028f5aSJeff Roberson #include <sys/sched.h>
65d3dd6bd4SKonstantin Belousov #include <sys/shm.h>
66165d2b99SJulian Elischer #include <sys/smp.h>
671cc8c45cSRobert Watson #include <sys/stack.h>
686ce13747SMikolaj Golub #include <sys/stat.h>
69598f2b81SMateusz Guzik #include <sys/dtrace_bsd.h>
70fb919e4dSMark Murray #include <sys/sysctl.h>
7162d6ce3aSDon Lewis #include <sys/filedesc.h>
72df8bae1dSRodney W. Grimes #include <sys/tty.h>
73bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h>
745d217f17SJohn Birrell #include <sys/sdt.h>
751005a129SJohn Baldwin #include <sys/sx.h>
76fb919e4dSMark Murray #include <sys/user.h>
77fe769cddSDavid Schultz #include <sys/vnode.h>
78b4490c6eSKonstantin Belousov #include <sys/wait.h>
791762f674SKonstantin Belousov #ifdef KTRACE
801762f674SKonstantin Belousov #include <sys/ktrace.h>
811762f674SKonstantin Belousov #endif
82fb919e4dSMark Murray
83cc43c38cSRobert Watson #ifdef DDB
84cc43c38cSRobert Watson #include <ddb/ddb.h>
85cc43c38cSRobert Watson #endif
86cc43c38cSRobert Watson
87efeaf95aSDavid Greenman #include <vm/vm.h>
881c771f92SKonstantin Belousov #include <vm/vm_param.h>
89a136efe9SPeter Wemm #include <vm/vm_extern.h>
90efeaf95aSDavid Greenman #include <vm/pmap.h>
91efeaf95aSDavid Greenman #include <vm/vm_map.h>
92cc43c38cSRobert Watson #include <vm/vm_object.h>
935384d089SMikolaj Golub #include <vm/vm_page.h>
94*1123986dSJohn Baldwin #include <vm/vm_pager.h>
95c897b813SJeff Roberson #include <vm/uma.h>
96df8bae1dSRodney W. Grimes
973b444436SMateusz Guzik #include <fs/devfs/devfs.h>
983b444436SMateusz Guzik
9905e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32
10005e06d11SKonstantin Belousov #include <compat/freebsd32/freebsd32.h>
10105e06d11SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
10205e06d11SKonstantin Belousov #endif
10305e06d11SKonstantin Belousov
1045d217f17SJohn Birrell SDT_PROVIDER_DEFINE(proc);
1055d217f17SJohn Birrell
106a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header");
107876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
108a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
10955166637SPoul-Henning Kamp
1104d77a549SAlfred Perlstein static void doenterpgrp(struct proc *, struct pgrp *);
1114d77a549SAlfred Perlstein static void orphanpg(struct pgrp *pg);
112f8d90480SAttilio Rao static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
1135032ff81SDon Lewis static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
114d92909c1SRobert Watson static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
115d92909c1SRobert Watson int preferthread);
11602e878d9SJohn Baldwin static void pgdelete(struct pgrp *);
117ef739c73SKonstantin Belousov static int pgrp_init(void *mem, int size, int flags);
118b23f72e9SBrian Feldman static int proc_ctor(void *mem, int size, void *arg, int flags);
119a136efe9SPeter Wemm static void proc_dtor(void *mem, int size, void *arg);
120b23f72e9SBrian Feldman static int proc_init(void *mem, int size, int flags);
121a136efe9SPeter Wemm static void proc_fini(void *mem, int size);
12258e8af1bSKonstantin Belousov static void pargs_free(struct pargs *pa);
123a136efe9SPeter Wemm
124df8bae1dSRodney W. Grimes /*
125b75356e1SJeffrey Hsu * Other process lists
126b75356e1SJeffrey Hsu */
1274ccaa87fSMitchell Horne struct pidhashhead *pidhashtbl = NULL;
1283d3e6793SMateusz Guzik struct sx *pidhashtbl_lock;
129b75356e1SJeffrey Hsu u_long pidhash;
1303d3e6793SMateusz Guzik u_long pidhashlock;
131b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl;
132b75356e1SJeffrey Hsu u_long pgrphash;
1334ccaa87fSMitchell Horne struct proclist allproc = LIST_HEAD_INITIALIZER(allproc);
1343e72c844SMateusz Guzik struct sx __exclusive_cache_line allproc_lock;
1353e72c844SMateusz Guzik struct sx __exclusive_cache_line proctree_lock;
1363e72c844SMateusz Guzik struct mtx __exclusive_cache_line ppeers_lock;
13734ebdceaSMateusz Guzik struct mtx __exclusive_cache_line procid_lock;
138c897b813SJeff Roberson uma_zone_t proc_zone;
139ef739c73SKonstantin Belousov uma_zone_t pgrp_zone;
140b75356e1SJeffrey Hsu
141645743eaSJohn Baldwin /*
142645743eaSJohn Baldwin * The offset of various fields in struct proc and struct thread.
143645743eaSJohn Baldwin * These are used by kernel debuggers to enumerate kernel threads and
144645743eaSJohn Baldwin * processes.
145645743eaSJohn Baldwin */
146645743eaSJohn Baldwin const int proc_off_p_pid = offsetof(struct proc, p_pid);
147645743eaSJohn Baldwin const int proc_off_p_comm = offsetof(struct proc, p_comm);
148645743eaSJohn Baldwin const int proc_off_p_list = offsetof(struct proc, p_list);
1494e7d1b52SJohn Baldwin const int proc_off_p_hash = offsetof(struct proc, p_hash);
150645743eaSJohn Baldwin const int proc_off_p_threads = offsetof(struct proc, p_threads);
151645743eaSJohn Baldwin const int thread_off_td_tid = offsetof(struct thread, td_tid);
152645743eaSJohn Baldwin const int thread_off_td_name = offsetof(struct thread, td_name);
153645743eaSJohn Baldwin const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
154645743eaSJohn Baldwin const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
155645743eaSJohn Baldwin const int thread_off_td_plist = offsetof(struct thread, td_plist);
156645743eaSJohn Baldwin
1572ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_ctor);
1582ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_dtor);
1592ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_init);
1602ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_fini);
1612ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_exit);
1622ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_fork);
1632ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(process_exec);
1642ca45184SMatt Joras
165b9f009b0SPeter Wemm int kstack_pages = KSTACK_PAGES;
16635b3be81SZhenlei Huang SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
16735b3be81SZhenlei Huang &kstack_pages, 0,
168b389be97SRebecca Cran "Kernel stack size in pages");
169a91831a2SKonstantin Belousov static int vmmap_skip_res_cnt = 0;
170a91831a2SKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
171a91831a2SKonstantin Belousov &vmmap_skip_res_cnt, 0,
172a91831a2SKonstantin Belousov "Skip calculation of the pages resident count in kern.proc.vmmap");
173b9f009b0SPeter Wemm
174a30d7c60SJake Burkholder CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
175ed780687SKonstantin Belousov #ifdef COMPAT_FREEBSD32
176ed780687SKonstantin Belousov CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
177ed780687SKonstantin Belousov #endif
178a30d7c60SJake Burkholder
179b75356e1SJeffrey Hsu /*
180b75356e1SJeffrey Hsu * Initialize global process hashing structures.
181df8bae1dSRodney W. Grimes */
18226f9a767SRodney W. Grimes void
procinit(void)183314381b5SKonstantin Belousov procinit(void)
184df8bae1dSRodney W. Grimes {
1853d3e6793SMateusz Guzik u_long i;
186df8bae1dSRodney W. Grimes
1871005a129SJohn Baldwin sx_init(&allproc_lock, "allproc");
1881005a129SJohn Baldwin sx_init(&proctree_lock, "proctree");
189c6544064SJohn Baldwin mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
19034ebdceaSMateusz Guzik mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
191b75356e1SJeffrey Hsu pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
1923d3e6793SMateusz Guzik pidhashlock = (pidhash + 1) / 64;
1933d3e6793SMateusz Guzik if (pidhashlock > 0)
1943d3e6793SMateusz Guzik pidhashlock--;
1953d3e6793SMateusz Guzik pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
1963d3e6793SMateusz Guzik M_PROC, M_WAITOK | M_ZERO);
1973d3e6793SMateusz Guzik for (i = 0; i < pidhashlock + 1; i++)
198eeb1f3edSRick Macklem sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
199b75356e1SJeffrey Hsu pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
200de028f5aSJeff Roberson proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
201a136efe9SPeter Wemm proc_ctor, proc_dtor, proc_init, proc_fini,
202a136efe9SPeter Wemm UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
203ef739c73SKonstantin Belousov pgrp_zone = uma_zcreate("PGRP", sizeof(struct pgrp), NULL, NULL,
204ef739c73SKonstantin Belousov pgrp_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
205f535380cSDon Lewis uihashinit();
206df8bae1dSRodney W. Grimes }
207df8bae1dSRodney W. Grimes
208df8bae1dSRodney W. Grimes /*
209a136efe9SPeter Wemm * Prepare a proc for use.
210a136efe9SPeter Wemm */
211b23f72e9SBrian Feldman static int
proc_ctor(void * mem,int size,void * arg,int flags)212b23f72e9SBrian Feldman proc_ctor(void *mem, int size, void *arg, int flags)
213a136efe9SPeter Wemm {
214a136efe9SPeter Wemm struct proc *p;
215316e092aSHans Petter Selasky struct thread *td;
216a136efe9SPeter Wemm
217a136efe9SPeter Wemm p = (struct proc *)mem;
218598f2b81SMateusz Guzik #ifdef KDTRACE_HOOKS
219598f2b81SMateusz Guzik kdtrace_proc_ctor(p);
220598f2b81SMateusz Guzik #endif
2212ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
222316e092aSHans Petter Selasky td = FIRST_THREAD_IN_PROC(p);
223316e092aSHans Petter Selasky if (td != NULL) {
224316e092aSHans Petter Selasky /* Make sure all thread constructors are executed */
2252ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
226316e092aSHans Petter Selasky }
227b23f72e9SBrian Feldman return (0);
228a136efe9SPeter Wemm }
229a136efe9SPeter Wemm
230a136efe9SPeter Wemm /*
231a136efe9SPeter Wemm * Reclaim a proc after use.
232a136efe9SPeter Wemm */
233a136efe9SPeter Wemm static void
proc_dtor(void * mem,int size,void * arg)234a136efe9SPeter Wemm proc_dtor(void *mem, int size, void *arg)
235a136efe9SPeter Wemm {
236a136efe9SPeter Wemm struct proc *p;
2371faf202eSJulian Elischer struct thread *td;
238a136efe9SPeter Wemm
2391faf202eSJulian Elischer /* INVARIANTS checks go here */
240a136efe9SPeter Wemm p = (struct proc *)mem;
241ed062c8dSJulian Elischer td = FIRST_THREAD_IN_PROC(p);
24289b57fcfSKonstantin Belousov if (td != NULL) {
243ed062c8dSJulian Elischer #ifdef INVARIANTS
2441faf202eSJulian Elischer KASSERT((p->p_numthreads == 1),
2451faf202eSJulian Elischer ("bad number of threads in exiting process"));
2462c255e9dSRobert Watson KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
247ed062c8dSJulian Elischer #endif
2481ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */
2491ba4a712SPawel Jakub Dawidek osd_thread_exit(td);
250c6d31b83SKonstantin Belousov ast_kclear(td);
251316e092aSHans Petter Selasky
252316e092aSHans Petter Selasky /* Make sure all thread destructors are executed */
2532ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
25489b57fcfSKonstantin Belousov }
2552ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
256598f2b81SMateusz Guzik #ifdef KDTRACE_HOOKS
257598f2b81SMateusz Guzik kdtrace_proc_dtor(p);
258598f2b81SMateusz Guzik #endif
259ebceaf6dSDavid Xu if (p->p_ksi != NULL)
260ebceaf6dSDavid Xu KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
261a136efe9SPeter Wemm }
262a136efe9SPeter Wemm
263a136efe9SPeter Wemm /*
264a136efe9SPeter Wemm * Initialize type-stable parts of a proc (when newly created).
265a136efe9SPeter Wemm */
266b23f72e9SBrian Feldman static int
proc_init(void * mem,int size,int flags)267b23f72e9SBrian Feldman proc_init(void *mem, int size, int flags)
268a136efe9SPeter Wemm {
269a136efe9SPeter Wemm struct proc *p;
270a136efe9SPeter Wemm
271a136efe9SPeter Wemm p = (struct proc *)mem;
272f97af970SMateusz Guzik mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
273f97af970SMateusz Guzik mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
274f97af970SMateusz Guzik mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
275f97af970SMateusz Guzik mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
276f97af970SMateusz Guzik mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
277aeb32571SKonstantin Belousov cv_init(&p->p_pwait, "ppwait");
27889b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */
2792ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(process_init, p);
2808b059651SDavid Schultz p->p_stats = pstats_alloc();
281aa0241d6SMateusz Guzik p->p_pgrp = NULL;
282171f0832SKonstantin Belousov TAILQ_INIT(&p->p_kqtim_stop);
283b23f72e9SBrian Feldman return (0);
284a136efe9SPeter Wemm }
285a136efe9SPeter Wemm
286a136efe9SPeter Wemm /*
2878daa8c60SDavid Schultz * UMA should ensure that this function is never called.
2888daa8c60SDavid Schultz * Freeing a proc structure would violate type stability.
289a136efe9SPeter Wemm */
290a136efe9SPeter Wemm static void
proc_fini(void * mem,int size)291a136efe9SPeter Wemm proc_fini(void *mem, int size)
292a136efe9SPeter Wemm {
293f55ab994SJohn Baldwin #ifdef notnow
294f55ab994SJohn Baldwin struct proc *p;
295a136efe9SPeter Wemm
296f55ab994SJohn Baldwin p = (struct proc *)mem;
2972ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
298f55ab994SJohn Baldwin pstats_free(p->p_stats);
299f55ab994SJohn Baldwin thread_free(FIRST_THREAD_IN_PROC(p));
300f55ab994SJohn Baldwin mtx_destroy(&p->p_mtx);
301ebceaf6dSDavid Xu if (p->p_ksi != NULL)
302ebceaf6dSDavid Xu ksiginfo_free(p->p_ksi);
303f55ab994SJohn Baldwin #else
3048daa8c60SDavid Schultz panic("proc reclaimed");
305f55ab994SJohn Baldwin #endif
306a136efe9SPeter Wemm }
307a136efe9SPeter Wemm
308ef739c73SKonstantin Belousov static int
pgrp_init(void * mem,int size,int flags)309ef739c73SKonstantin Belousov pgrp_init(void *mem, int size, int flags)
310ef739c73SKonstantin Belousov {
311ef739c73SKonstantin Belousov struct pgrp *pg;
312ef739c73SKonstantin Belousov
313ef739c73SKonstantin Belousov pg = mem;
314ef739c73SKonstantin Belousov mtx_init(&pg->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
3153360b485SKonstantin Belousov sx_init(&pg->pg_killsx, "killpg racer");
316ef739c73SKonstantin Belousov return (0);
317ef739c73SKonstantin Belousov }
318ef739c73SKonstantin Belousov
319a136efe9SPeter Wemm /*
32034ebdceaSMateusz Guzik * PID space management.
32134ebdceaSMateusz Guzik *
32234ebdceaSMateusz Guzik * These bitmaps are used by fork_findpid.
32334ebdceaSMateusz Guzik */
32434ebdceaSMateusz Guzik bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
32534ebdceaSMateusz Guzik bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
32634ebdceaSMateusz Guzik bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
32734ebdceaSMateusz Guzik bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
32834ebdceaSMateusz Guzik
32934ebdceaSMateusz Guzik static bitstr_t *proc_id_array[] = {
33034ebdceaSMateusz Guzik proc_id_pidmap,
33134ebdceaSMateusz Guzik proc_id_grpidmap,
33234ebdceaSMateusz Guzik proc_id_sessidmap,
33334ebdceaSMateusz Guzik proc_id_reapmap,
33434ebdceaSMateusz Guzik };
33534ebdceaSMateusz Guzik
33634ebdceaSMateusz Guzik void
proc_id_set(int type,pid_t id)33734ebdceaSMateusz Guzik proc_id_set(int type, pid_t id)
33834ebdceaSMateusz Guzik {
33934ebdceaSMateusz Guzik
34034ebdceaSMateusz Guzik KASSERT(type >= 0 && type < nitems(proc_id_array),
34134ebdceaSMateusz Guzik ("invalid type %d\n", type));
34234ebdceaSMateusz Guzik mtx_lock(&procid_lock);
34334ebdceaSMateusz Guzik KASSERT(bit_test(proc_id_array[type], id) == 0,
34434ebdceaSMateusz Guzik ("bit %d already set in %d\n", id, type));
34534ebdceaSMateusz Guzik bit_set(proc_id_array[type], id);
34634ebdceaSMateusz Guzik mtx_unlock(&procid_lock);
34734ebdceaSMateusz Guzik }
34834ebdceaSMateusz Guzik
34934ebdceaSMateusz Guzik void
proc_id_set_cond(int type,pid_t id)35034ebdceaSMateusz Guzik proc_id_set_cond(int type, pid_t id)
35134ebdceaSMateusz Guzik {
35234ebdceaSMateusz Guzik
35334ebdceaSMateusz Guzik KASSERT(type >= 0 && type < nitems(proc_id_array),
35434ebdceaSMateusz Guzik ("invalid type %d\n", type));
35534ebdceaSMateusz Guzik if (bit_test(proc_id_array[type], id))
35634ebdceaSMateusz Guzik return;
35734ebdceaSMateusz Guzik mtx_lock(&procid_lock);
35834ebdceaSMateusz Guzik bit_set(proc_id_array[type], id);
35934ebdceaSMateusz Guzik mtx_unlock(&procid_lock);
36034ebdceaSMateusz Guzik }
36134ebdceaSMateusz Guzik
36234ebdceaSMateusz Guzik void
proc_id_clear(int type,pid_t id)36334ebdceaSMateusz Guzik proc_id_clear(int type, pid_t id)
36434ebdceaSMateusz Guzik {
36534ebdceaSMateusz Guzik
36634ebdceaSMateusz Guzik KASSERT(type >= 0 && type < nitems(proc_id_array),
36734ebdceaSMateusz Guzik ("invalid type %d\n", type));
36834ebdceaSMateusz Guzik mtx_lock(&procid_lock);
36934ebdceaSMateusz Guzik KASSERT(bit_test(proc_id_array[type], id) != 0,
37034ebdceaSMateusz Guzik ("bit %d not set in %d\n", id, type));
37134ebdceaSMateusz Guzik bit_clear(proc_id_array[type], id);
37234ebdceaSMateusz Guzik mtx_unlock(&procid_lock);
37334ebdceaSMateusz Guzik }
37434ebdceaSMateusz Guzik
37534ebdceaSMateusz Guzik /*
376df8bae1dSRodney W. Grimes * Is p an inferior of the current process?
377df8bae1dSRodney W. Grimes */
3781a432a2fSDima Ruban int
inferior(struct proc * p)379d7359980SKonstantin Belousov inferior(struct proc *p)
380df8bae1dSRodney W. Grimes {
381df8bae1dSRodney W. Grimes
3825b29d6e9SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED);
383d7359980SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED);
384d7359980SKonstantin Belousov for (; p != curproc; p = proc_realparent(p)) {
38500f13cb3SJohn Baldwin if (p->p_pid == 0)
38600f13cb3SJohn Baldwin return (0);
387d7359980SKonstantin Belousov }
38800f13cb3SJohn Baldwin return (1);
389df8bae1dSRodney W. Grimes }
390df8bae1dSRodney W. Grimes
391134eb42eSKonstantin Belousov /*
392eeb1f3edSRick Macklem * Shared lock all the pid hash lists.
393eeb1f3edSRick Macklem */
394eeb1f3edSRick Macklem void
pidhash_slockall(void)395eeb1f3edSRick Macklem pidhash_slockall(void)
396eeb1f3edSRick Macklem {
397eeb1f3edSRick Macklem u_long i;
398eeb1f3edSRick Macklem
399eeb1f3edSRick Macklem for (i = 0; i < pidhashlock + 1; i++)
400eeb1f3edSRick Macklem sx_slock(&pidhashtbl_lock[i]);
401eeb1f3edSRick Macklem }
402eeb1f3edSRick Macklem
403eeb1f3edSRick Macklem /*
404eeb1f3edSRick Macklem * Shared unlock all the pid hash lists.
405eeb1f3edSRick Macklem */
406eeb1f3edSRick Macklem void
pidhash_sunlockall(void)407eeb1f3edSRick Macklem pidhash_sunlockall(void)
408eeb1f3edSRick Macklem {
409eeb1f3edSRick Macklem u_long i;
410eeb1f3edSRick Macklem
411eeb1f3edSRick Macklem for (i = 0; i < pidhashlock + 1; i++)
412eeb1f3edSRick Macklem sx_sunlock(&pidhashtbl_lock[i]);
413eeb1f3edSRick Macklem }
414eeb1f3edSRick Macklem
415eeb1f3edSRick Macklem /*
416509300e1SYan-Hao Wang * Similar to pfind(), this function locate a process by number.
417eeb1f3edSRick Macklem */
418eeb1f3edSRick Macklem struct proc *
pfind_any_locked(pid_t pid)419eeb1f3edSRick Macklem pfind_any_locked(pid_t pid)
420eeb1f3edSRick Macklem {
421eeb1f3edSRick Macklem struct proc *p;
422eeb1f3edSRick Macklem
423eeb1f3edSRick Macklem sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
424eeb1f3edSRick Macklem LIST_FOREACH(p, PIDHASH(pid), p_hash) {
425eeb1f3edSRick Macklem if (p->p_pid == pid) {
426eeb1f3edSRick Macklem PROC_LOCK(p);
427eeb1f3edSRick Macklem if (p->p_state == PRS_NEW) {
428eeb1f3edSRick Macklem PROC_UNLOCK(p);
429eeb1f3edSRick Macklem p = NULL;
430eeb1f3edSRick Macklem }
431eeb1f3edSRick Macklem break;
432eeb1f3edSRick Macklem }
433eeb1f3edSRick Macklem }
434eeb1f3edSRick Macklem return (p);
435eeb1f3edSRick Macklem }
436eeb1f3edSRick Macklem
437eeb1f3edSRick Macklem /*
43853011553SMateusz Guzik * Locate a process by number.
43953011553SMateusz Guzik *
44053011553SMateusz Guzik * By not returning processes in the PRS_NEW state, we allow callers to avoid
441134eb42eSKonstantin Belousov * testing for that condition to avoid dereferencing p_ucred, et al.
442134eb42eSKonstantin Belousov */
44353011553SMateusz Guzik static __always_inline struct proc *
_pfind(pid_t pid,bool zombie)44453011553SMateusz Guzik _pfind(pid_t pid, bool zombie)
445134eb42eSKonstantin Belousov {
446134eb42eSKonstantin Belousov struct proc *p;
447134eb42eSKonstantin Belousov
448272640b7SMateusz Guzik p = curproc;
449272640b7SMateusz Guzik if (p->p_pid == pid) {
450272640b7SMateusz Guzik PROC_LOCK(p);
451272640b7SMateusz Guzik return (p);
452272640b7SMateusz Guzik }
45353011553SMateusz Guzik sx_slock(PIDHASHLOCK(pid));
45453011553SMateusz Guzik LIST_FOREACH(p, PIDHASH(pid), p_hash) {
45553011553SMateusz Guzik if (p->p_pid == pid) {
45653011553SMateusz Guzik PROC_LOCK(p);
45753011553SMateusz Guzik if (p->p_state == PRS_NEW ||
4580abc7e41SJilles Tjoelker (!zombie && p->p_state == PRS_ZOMBIE)) {
45953011553SMateusz Guzik PROC_UNLOCK(p);
46053011553SMateusz Guzik p = NULL;
46153011553SMateusz Guzik }
46253011553SMateusz Guzik break;
46353011553SMateusz Guzik }
46453011553SMateusz Guzik }
46553011553SMateusz Guzik sx_sunlock(PIDHASHLOCK(pid));
466df8bae1dSRodney W. Grimes return (p);
467df8bae1dSRodney W. Grimes }
468df8bae1dSRodney W. Grimes
46953011553SMateusz Guzik struct proc *
pfind(pid_t pid)47053011553SMateusz Guzik pfind(pid_t pid)
47153011553SMateusz Guzik {
47253011553SMateusz Guzik
47353011553SMateusz Guzik return (_pfind(pid, false));
47453011553SMateusz Guzik }
47553011553SMateusz Guzik
4766e1619daSMateusz Guzik /*
4776e1619daSMateusz Guzik * Same as pfind but allow zombies.
4786e1619daSMateusz Guzik */
4796e1619daSMateusz Guzik struct proc *
pfind_any(pid_t pid)4806e1619daSMateusz Guzik pfind_any(pid_t pid)
4816e1619daSMateusz Guzik {
4826e1619daSMateusz Guzik
48353011553SMateusz Guzik return (_pfind(pid, true));
4846e1619daSMateusz Guzik }
4856e1619daSMateusz Guzik
486df8bae1dSRodney W. Grimes /*
487f591779bSSeigo Tanimura * Locate a process group by number.
488f089b570SJohn Baldwin * The caller must hold proctree_lock.
489df8bae1dSRodney W. Grimes */
490df8bae1dSRodney W. Grimes struct pgrp *
pgfind(pid_t pgid)49118c23dffSEd Maste pgfind(pid_t pgid)
492df8bae1dSRodney W. Grimes {
49318c23dffSEd Maste struct pgrp *pgrp;
494df8bae1dSRodney W. Grimes
495f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED);
496f591779bSSeigo Tanimura
497f591779bSSeigo Tanimura LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
498f591779bSSeigo Tanimura if (pgrp->pg_id == pgid) {
499f591779bSSeigo Tanimura PGRP_LOCK(pgrp);
500df8bae1dSRodney W. Grimes return (pgrp);
501f591779bSSeigo Tanimura }
502f591779bSSeigo Tanimura }
503df8bae1dSRodney W. Grimes return (NULL);
504df8bae1dSRodney W. Grimes }
505df8bae1dSRodney W. Grimes
506df8bae1dSRodney W. Grimes /*
507fa3935bcSMikolaj Golub * Locate process and do additional manipulations, depending on flags.
508fa3935bcSMikolaj Golub */
509fa3935bcSMikolaj Golub int
pget(pid_t pid,int flags,struct proc ** pp)510fa3935bcSMikolaj Golub pget(pid_t pid, int flags, struct proc **pp)
511fa3935bcSMikolaj Golub {
512fa3935bcSMikolaj Golub struct proc *p;
5134bc5ce2cSKonstantin Belousov struct thread *td1;
514fa3935bcSMikolaj Golub int error;
515fa3935bcSMikolaj Golub
516a2c36a24SMateusz Guzik p = curproc;
517a2c36a24SMateusz Guzik if (p->p_pid == pid) {
518a2c36a24SMateusz Guzik PROC_LOCK(p);
519a2c36a24SMateusz Guzik } else {
5204f666417SPawel Jakub Dawidek p = NULL;
52153011553SMateusz Guzik if (pid <= PID_MAX) {
52253011553SMateusz Guzik if ((flags & PGET_NOTWEXIT) == 0)
52353011553SMateusz Guzik p = pfind_any(pid);
52453011553SMateusz Guzik else
52553011553SMateusz Guzik p = pfind(pid);
52653011553SMateusz Guzik } else if ((flags & PGET_NOTID) == 0) {
5274bc5ce2cSKonstantin Belousov td1 = tdfind(pid, -1);
5284bc5ce2cSKonstantin Belousov if (td1 != NULL)
5294bc5ce2cSKonstantin Belousov p = td1->td_proc;
5304f666417SPawel Jakub Dawidek }
531fa3935bcSMikolaj Golub if (p == NULL)
532fa3935bcSMikolaj Golub return (ESRCH);
533fa3935bcSMikolaj Golub if ((flags & PGET_CANSEE) != 0) {
534fa3935bcSMikolaj Golub error = p_cansee(curthread, p);
535fa3935bcSMikolaj Golub if (error != 0)
536fa3935bcSMikolaj Golub goto errout;
537fa3935bcSMikolaj Golub }
538a2c36a24SMateusz Guzik }
539fa3935bcSMikolaj Golub if ((flags & PGET_CANDEBUG) != 0) {
540fa3935bcSMikolaj Golub error = p_candebug(curthread, p);
541fa3935bcSMikolaj Golub if (error != 0)
542fa3935bcSMikolaj Golub goto errout;
543fa3935bcSMikolaj Golub }
544fa3935bcSMikolaj Golub if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
545fa3935bcSMikolaj Golub error = EPERM;
546fa3935bcSMikolaj Golub goto errout;
547fa3935bcSMikolaj Golub }
548fa3935bcSMikolaj Golub if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
549fa3935bcSMikolaj Golub error = ESRCH;
550fa3935bcSMikolaj Golub goto errout;
551fa3935bcSMikolaj Golub }
552fa3935bcSMikolaj Golub if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
553fa3935bcSMikolaj Golub /*
554fa3935bcSMikolaj Golub * XXXRW: Not clear ESRCH is the right error during proc
555fa3935bcSMikolaj Golub * execve().
556fa3935bcSMikolaj Golub */
557fa3935bcSMikolaj Golub error = ESRCH;
558fa3935bcSMikolaj Golub goto errout;
559fa3935bcSMikolaj Golub }
560fa3935bcSMikolaj Golub if ((flags & PGET_HOLD) != 0) {
561fa3935bcSMikolaj Golub _PHOLD(p);
562fa3935bcSMikolaj Golub PROC_UNLOCK(p);
563fa3935bcSMikolaj Golub }
564fa3935bcSMikolaj Golub *pp = p;
565fa3935bcSMikolaj Golub return (0);
566fa3935bcSMikolaj Golub errout:
567fa3935bcSMikolaj Golub PROC_UNLOCK(p);
568fa3935bcSMikolaj Golub return (error);
569fa3935bcSMikolaj Golub }
570fa3935bcSMikolaj Golub
571fa3935bcSMikolaj Golub /*
572f591779bSSeigo Tanimura * Create a new process group.
573f591779bSSeigo Tanimura * pgid must be equal to the pid of p.
574f591779bSSeigo Tanimura * Begin a new session if required.
575df8bae1dSRodney W. Grimes */
57626f9a767SRodney W. Grimes int
enterpgrp(struct proc * p,pid_t pgid,struct pgrp * pgrp,struct session * sess)57718c23dffSEd Maste enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
578df8bae1dSRodney W. Grimes {
5793360b485SKonstantin Belousov struct pgrp *old_pgrp;
580df8bae1dSRodney W. Grimes
581f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED);
582f591779bSSeigo Tanimura
583f591779bSSeigo Tanimura KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
584f591779bSSeigo Tanimura KASSERT(p->p_pid == pgid,
585f591779bSSeigo Tanimura ("enterpgrp: new pgrp and pid != pgid"));
5864419a8a8SMateusz Guzik KASSERT(pgfind(pgid) == NULL,
587f591779bSSeigo Tanimura ("enterpgrp: pgrp with pgid exists"));
5885526d2d9SEivind Eklund KASSERT(!SESS_LEADER(p),
5895526d2d9SEivind Eklund ("enterpgrp: session leader attempted setpgrp"));
590219cbf59SEivind Eklund
5913360b485SKonstantin Belousov old_pgrp = p->p_pgrp;
5927a70f17aSKonstantin Belousov if (!sx_try_xlock(&old_pgrp->pg_killsx)) {
5937a70f17aSKonstantin Belousov sx_xunlock(&proctree_lock);
5947a70f17aSKonstantin Belousov sx_xlock(&old_pgrp->pg_killsx);
5957a70f17aSKonstantin Belousov sx_xunlock(&old_pgrp->pg_killsx);
5963360b485SKonstantin Belousov return (ERESTART);
5977a70f17aSKonstantin Belousov }
5983360b485SKonstantin Belousov MPASS(old_pgrp == p->p_pgrp);
5993360b485SKonstantin Belousov
600f591779bSSeigo Tanimura if (sess != NULL) {
601df8bae1dSRodney W. Grimes /*
602df8bae1dSRodney W. Grimes * new session
603df8bae1dSRodney W. Grimes */
6046008862bSJohn Baldwin mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
605f591779bSSeigo Tanimura PROC_LOCK(p);
606f591779bSSeigo Tanimura p->p_flag &= ~P_CONTROLT;
607f591779bSSeigo Tanimura PROC_UNLOCK(p);
608f591779bSSeigo Tanimura PGRP_LOCK(pgrp);
609df8bae1dSRodney W. Grimes sess->s_leader = p;
610643a8daaSDon Lewis sess->s_sid = p->p_pid;
61134ebdceaSMateusz Guzik proc_id_set(PROC_ID_SESSION, p->p_pid);
612bc093719SEd Schouten refcount_init(&sess->s_count, 1);
613df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL;
6148dc9b4cfSEd Schouten sess->s_ttydp = NULL;
615df8bae1dSRodney W. Grimes sess->s_ttyp = NULL;
616df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login,
617df8bae1dSRodney W. Grimes sizeof(sess->s_login));
618df8bae1dSRodney W. Grimes pgrp->pg_session = sess;
6195526d2d9SEivind Eklund KASSERT(p == curproc,
6205526d2d9SEivind Eklund ("enterpgrp: mksession and p != curproc"));
621df8bae1dSRodney W. Grimes } else {
622df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session;
623bc093719SEd Schouten sess_hold(pgrp->pg_session);
624f591779bSSeigo Tanimura PGRP_LOCK(pgrp);
625df8bae1dSRodney W. Grimes }
626df8bae1dSRodney W. Grimes pgrp->pg_id = pgid;
62734ebdceaSMateusz Guzik proc_id_set(PROC_ID_GROUP, p->p_pid);
628b75356e1SJeffrey Hsu LIST_INIT(&pgrp->pg_members);
6295844bd05SKonstantin Belousov pgrp->pg_flags = 0;
630f591779bSSeigo Tanimura
631f591779bSSeigo Tanimura /*
632f089b570SJohn Baldwin * As we have an exclusive lock of proctree_lock,
633f591779bSSeigo Tanimura * this should not deadlock.
634f591779bSSeigo Tanimura */
635b75356e1SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
636831d27a9SDon Lewis SLIST_INIT(&pgrp->pg_sigiolst);
637f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp);
638f591779bSSeigo Tanimura
639f591779bSSeigo Tanimura doenterpgrp(p, pgrp);
640f591779bSSeigo Tanimura
6413360b485SKonstantin Belousov sx_xunlock(&old_pgrp->pg_killsx);
642df8bae1dSRodney W. Grimes return (0);
643f591779bSSeigo Tanimura }
644f591779bSSeigo Tanimura
645f591779bSSeigo Tanimura /*
646f591779bSSeigo Tanimura * Move p to an existing process group
647f591779bSSeigo Tanimura */
648f591779bSSeigo Tanimura int
enterthispgrp(struct proc * p,struct pgrp * pgrp)64918c23dffSEd Maste enterthispgrp(struct proc *p, struct pgrp *pgrp)
650f591779bSSeigo Tanimura {
6513360b485SKonstantin Belousov struct pgrp *old_pgrp;
652f089b570SJohn Baldwin
653f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED);
654f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED);
655f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
656f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
657f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
658f591779bSSeigo Tanimura KASSERT(pgrp->pg_session == p->p_session,
659993a1699SKonstantin Belousov ("%s: pgrp's session %p, p->p_session %p proc %p\n",
660993a1699SKonstantin Belousov __func__, pgrp->pg_session, p->p_session, p));
661f591779bSSeigo Tanimura KASSERT(pgrp != p->p_pgrp,
662993a1699SKonstantin Belousov ("%s: p %p belongs to pgrp %p", __func__, p, pgrp));
663f591779bSSeigo Tanimura
6643360b485SKonstantin Belousov old_pgrp = p->p_pgrp;
6657a70f17aSKonstantin Belousov if (!sx_try_xlock(&old_pgrp->pg_killsx)) {
6667a70f17aSKonstantin Belousov sx_xunlock(&proctree_lock);
6677a70f17aSKonstantin Belousov sx_xlock(&old_pgrp->pg_killsx);
6687a70f17aSKonstantin Belousov sx_xunlock(&old_pgrp->pg_killsx);
6693360b485SKonstantin Belousov return (ERESTART);
6707a70f17aSKonstantin Belousov }
6713360b485SKonstantin Belousov MPASS(old_pgrp == p->p_pgrp);
6723360b485SKonstantin Belousov if (!sx_try_xlock(&pgrp->pg_killsx)) {
6733360b485SKonstantin Belousov sx_xunlock(&old_pgrp->pg_killsx);
6747a70f17aSKonstantin Belousov sx_xunlock(&proctree_lock);
6757a70f17aSKonstantin Belousov sx_xlock(&pgrp->pg_killsx);
6767a70f17aSKonstantin Belousov sx_xunlock(&pgrp->pg_killsx);
6773360b485SKonstantin Belousov return (ERESTART);
6783360b485SKonstantin Belousov }
6793360b485SKonstantin Belousov
680f591779bSSeigo Tanimura doenterpgrp(p, pgrp);
681f591779bSSeigo Tanimura
6823360b485SKonstantin Belousov sx_xunlock(&pgrp->pg_killsx);
6833360b485SKonstantin Belousov sx_xunlock(&old_pgrp->pg_killsx);
684f591779bSSeigo Tanimura return (0);
685f591779bSSeigo Tanimura }
686f591779bSSeigo Tanimura
687f591779bSSeigo Tanimura /*
688c5bc28b2SKonstantin Belousov * If true, any child of q which belongs to group pgrp, qualifies the
689c5bc28b2SKonstantin Belousov * process group pgrp as not orphaned.
690c5bc28b2SKonstantin Belousov */
691c5bc28b2SKonstantin Belousov static bool
isjobproc(struct proc * q,struct pgrp * pgrp)692c5bc28b2SKonstantin Belousov isjobproc(struct proc *q, struct pgrp *pgrp)
693c5bc28b2SKonstantin Belousov {
694c5bc28b2SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED);
6955844bd05SKonstantin Belousov
696c5bc28b2SKonstantin Belousov return (q->p_pgrp != pgrp &&
697c5bc28b2SKonstantin Belousov q->p_pgrp->pg_session == pgrp->pg_session);
698c5bc28b2SKonstantin Belousov }
699c5bc28b2SKonstantin Belousov
7002f5f11f5SKonstantin Belousov static struct proc *
jobc_reaper(struct proc * p)7012f5f11f5SKonstantin Belousov jobc_reaper(struct proc *p)
7022f5f11f5SKonstantin Belousov {
7032f5f11f5SKonstantin Belousov struct proc *pp;
7042f5f11f5SKonstantin Belousov
7055844bd05SKonstantin Belousov sx_assert(&proctree_lock, SA_LOCKED);
7062f5f11f5SKonstantin Belousov
7072f5f11f5SKonstantin Belousov for (pp = p;;) {
7082f5f11f5SKonstantin Belousov pp = pp->p_reaper;
7092f5f11f5SKonstantin Belousov if (pp->p_reaper == pp ||
7102f5f11f5SKonstantin Belousov (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
7112f5f11f5SKonstantin Belousov return (pp);
7122f5f11f5SKonstantin Belousov }
7132f5f11f5SKonstantin Belousov }
7142f5f11f5SKonstantin Belousov
7152f5f11f5SKonstantin Belousov static struct proc *
jobc_parent(struct proc * p,struct proc * p_exiting)7165844bd05SKonstantin Belousov jobc_parent(struct proc *p, struct proc *p_exiting)
7172f5f11f5SKonstantin Belousov {
7182f5f11f5SKonstantin Belousov struct proc *pp;
7192f5f11f5SKonstantin Belousov
7205844bd05SKonstantin Belousov sx_assert(&proctree_lock, SA_LOCKED);
7212f5f11f5SKonstantin Belousov
7222f5f11f5SKonstantin Belousov pp = proc_realparent(p);
7235844bd05SKonstantin Belousov if (pp->p_pptr == NULL || pp == p_exiting ||
7242f5f11f5SKonstantin Belousov (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
7252f5f11f5SKonstantin Belousov return (pp);
7262f5f11f5SKonstantin Belousov return (jobc_reaper(pp));
7272f5f11f5SKonstantin Belousov }
7282f5f11f5SKonstantin Belousov
7296e55ba5bSjoyu liao int
pgrp_calc_jobc(struct pgrp * pgrp)7305844bd05SKonstantin Belousov pgrp_calc_jobc(struct pgrp *pgrp)
731c5bc28b2SKonstantin Belousov {
732c5bc28b2SKonstantin Belousov struct proc *q;
733c5bc28b2SKonstantin Belousov int cnt;
734c5bc28b2SKonstantin Belousov
7355844bd05SKonstantin Belousov #ifdef INVARIANTS
7365844bd05SKonstantin Belousov if (!mtx_owned(&pgrp->pg_mtx))
7375844bd05SKonstantin Belousov sx_assert(&proctree_lock, SA_LOCKED);
7385844bd05SKonstantin Belousov #endif
739c5bc28b2SKonstantin Belousov
740c5bc28b2SKonstantin Belousov cnt = 0;
741c5bc28b2SKonstantin Belousov LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
742c5bc28b2SKonstantin Belousov if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
743c5bc28b2SKonstantin Belousov q->p_pptr == NULL)
744c5bc28b2SKonstantin Belousov continue;
7455844bd05SKonstantin Belousov if (isjobproc(jobc_parent(q, NULL), pgrp))
746c5bc28b2SKonstantin Belousov cnt++;
747c5bc28b2SKonstantin Belousov }
7485844bd05SKonstantin Belousov return (cnt);
749c5bc28b2SKonstantin Belousov }
750c5bc28b2SKonstantin Belousov
751c5bc28b2SKonstantin Belousov /*
752f591779bSSeigo Tanimura * Move p to a process group
753f591779bSSeigo Tanimura */
754f591779bSSeigo Tanimura static void
doenterpgrp(struct proc * p,struct pgrp * pgrp)75518c23dffSEd Maste doenterpgrp(struct proc *p, struct pgrp *pgrp)
756f591779bSSeigo Tanimura {
757f591779bSSeigo Tanimura struct pgrp *savepgrp;
7585844bd05SKonstantin Belousov struct proc *pp;
759f591779bSSeigo Tanimura
760f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED);
761f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED);
762f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
763f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
764f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
765f591779bSSeigo Tanimura
766f591779bSSeigo Tanimura savepgrp = p->p_pgrp;
7675844bd05SKonstantin Belousov pp = jobc_parent(p, NULL);
768df8bae1dSRodney W. Grimes
769f591779bSSeigo Tanimura PGRP_LOCK(pgrp);
770f591779bSSeigo Tanimura PGRP_LOCK(savepgrp);
7715844bd05SKonstantin Belousov if (isjobproc(pp, savepgrp) && pgrp_calc_jobc(savepgrp) == 1)
7725844bd05SKonstantin Belousov orphanpg(savepgrp);
77315e9ec51SJohn Baldwin PROC_LOCK(p);
774b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist);
775df8bae1dSRodney W. Grimes p->p_pgrp = pgrp;
77615e9ec51SJohn Baldwin PROC_UNLOCK(p);
777f591779bSSeigo Tanimura LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
7785844bd05SKonstantin Belousov if (isjobproc(pp, pgrp))
7795844bd05SKonstantin Belousov pgrp->pg_flags &= ~PGRP_ORPHANED;
780f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp);
781f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp);
782f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members))
783f591779bSSeigo Tanimura pgdelete(savepgrp);
784df8bae1dSRodney W. Grimes }
785df8bae1dSRodney W. Grimes
786df8bae1dSRodney W. Grimes /*
787df8bae1dSRodney W. Grimes * remove process from process group
788df8bae1dSRodney W. Grimes */
78926f9a767SRodney W. Grimes int
leavepgrp(struct proc * p)79018c23dffSEd Maste leavepgrp(struct proc *p)
791df8bae1dSRodney W. Grimes {
792f591779bSSeigo Tanimura struct pgrp *savepgrp;
793df8bae1dSRodney W. Grimes
794f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED);
795f591779bSSeigo Tanimura savepgrp = p->p_pgrp;
796f591779bSSeigo Tanimura PGRP_LOCK(savepgrp);
79715e9ec51SJohn Baldwin PROC_LOCK(p);
798b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist);
79915e9ec51SJohn Baldwin p->p_pgrp = NULL;
80015e9ec51SJohn Baldwin PROC_UNLOCK(p);
801f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp);
802f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members))
803f591779bSSeigo Tanimura pgdelete(savepgrp);
804df8bae1dSRodney W. Grimes return (0);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes
807df8bae1dSRodney W. Grimes /*
808df8bae1dSRodney W. Grimes * delete a process group
809df8bae1dSRodney W. Grimes */
81087b6de2bSPoul-Henning Kamp static void
pgdelete(struct pgrp * pgrp)81118c23dffSEd Maste pgdelete(struct pgrp *pgrp)
812df8bae1dSRodney W. Grimes {
813f591779bSSeigo Tanimura struct session *savesess;
814bc093719SEd Schouten struct tty *tp;
815f591779bSSeigo Tanimura
816f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED);
817f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
818f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
819f591779bSSeigo Tanimura
820831d27a9SDon Lewis /*
821831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of
822f5297909SMark Johnston * F_SETOWN with our pgid. The proctree lock ensures that
823f5297909SMark Johnston * new sigio structures will not be added after this point.
824831d27a9SDon Lewis */
825831d27a9SDon Lewis funsetownlst(&pgrp->pg_sigiolst);
826831d27a9SDon Lewis
827e649887bSAlfred Perlstein PGRP_LOCK(pgrp);
828bc093719SEd Schouten tp = pgrp->pg_session->s_ttyp;
829b75356e1SJeffrey Hsu LIST_REMOVE(pgrp, pg_hash);
830f591779bSSeigo Tanimura savesess = pgrp->pg_session;
831f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp);
832bc093719SEd Schouten
833bc093719SEd Schouten /* Remove the reference to the pgrp before deallocating it. */
834bc093719SEd Schouten if (tp != NULL) {
835bc093719SEd Schouten tty_lock(tp);
836bc093719SEd Schouten tty_rel_pgrp(tp, pgrp);
837bc093719SEd Schouten }
838bc093719SEd Schouten
83934ebdceaSMateusz Guzik proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
840ef739c73SKonstantin Belousov uma_zfree(pgrp_zone, pgrp);
841bc093719SEd Schouten sess_release(savesess);
842df8bae1dSRodney W. Grimes }
843df8bae1dSRodney W. Grimes
844c5bc28b2SKonstantin Belousov
845c5bc28b2SKonstantin Belousov static void
fixjobc_kill(struct proc * p)846c5bc28b2SKonstantin Belousov fixjobc_kill(struct proc *p)
847c5bc28b2SKonstantin Belousov {
848c5bc28b2SKonstantin Belousov struct proc *q;
8492f5f11f5SKonstantin Belousov struct pgrp *pgrp;
850c5bc28b2SKonstantin Belousov
851c5bc28b2SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED);
852c5bc28b2SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_NOTOWNED);
853c5bc28b2SKonstantin Belousov pgrp = p->p_pgrp;
854c5bc28b2SKonstantin Belousov PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
855c5bc28b2SKonstantin Belousov SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
856c5bc28b2SKonstantin Belousov
857c5bc28b2SKonstantin Belousov /*
858c5bc28b2SKonstantin Belousov * p no longer affects process group orphanage for children.
859c5bc28b2SKonstantin Belousov * It is marked by the flag because p is only physically
860c5bc28b2SKonstantin Belousov * removed from its process group on wait(2).
861c5bc28b2SKonstantin Belousov */
862928b8538SKonstantin Belousov MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
863c5bc28b2SKonstantin Belousov p->p_treeflag |= P_TREE_GRPEXITED;
864c5bc28b2SKonstantin Belousov
865df8bae1dSRodney W. Grimes /*
8665844bd05SKonstantin Belousov * Check if exiting p orphans its own group.
867df8bae1dSRodney W. Grimes */
8685844bd05SKonstantin Belousov pgrp = p->p_pgrp;
8695844bd05SKonstantin Belousov if (isjobproc(jobc_parent(p, NULL), pgrp)) {
8705844bd05SKonstantin Belousov PGRP_LOCK(pgrp);
8715844bd05SKonstantin Belousov if (pgrp_calc_jobc(pgrp) == 0)
8725844bd05SKonstantin Belousov orphanpg(pgrp);
8735844bd05SKonstantin Belousov PGRP_UNLOCK(pgrp);
8745844bd05SKonstantin Belousov }
875df8bae1dSRodney W. Grimes
876df8bae1dSRodney W. Grimes /*
877df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify
8785844bd05SKonstantin Belousov * their process groups after reparenting to reaper.
879df8bae1dSRodney W. Grimes */
8806760182cSMateusz Guzik LIST_FOREACH(q, &p->p_children, p_sibling) {
8815844bd05SKonstantin Belousov pgrp = q->p_pgrp;
8825844bd05SKonstantin Belousov PGRP_LOCK(pgrp);
8835844bd05SKonstantin Belousov if (pgrp_calc_jobc(pgrp) == 0) {
8845844bd05SKonstantin Belousov /*
8855844bd05SKonstantin Belousov * We want to handle exactly the children that
8865844bd05SKonstantin Belousov * has p as realparent. Then, when calculating
8875844bd05SKonstantin Belousov * jobc_parent for children, we should ignore
8885844bd05SKonstantin Belousov * P_TREE_GRPEXITED flag already set on p.
8895844bd05SKonstantin Belousov */
8905844bd05SKonstantin Belousov if (jobc_parent(q, p) == p && isjobproc(p, pgrp))
8915844bd05SKonstantin Belousov orphanpg(pgrp);
8925844bd05SKonstantin Belousov } else
8935844bd05SKonstantin Belousov pgrp->pg_flags &= ~PGRP_ORPHANED;
8945844bd05SKonstantin Belousov PGRP_UNLOCK(pgrp);
895c5bc28b2SKonstantin Belousov }
8965844bd05SKonstantin Belousov LIST_FOREACH(q, &p->p_orphans, p_orphan) {
8975844bd05SKonstantin Belousov pgrp = q->p_pgrp;
8985844bd05SKonstantin Belousov PGRP_LOCK(pgrp);
8995844bd05SKonstantin Belousov if (pgrp_calc_jobc(pgrp) == 0) {
9005844bd05SKonstantin Belousov if (isjobproc(p, pgrp))
9015844bd05SKonstantin Belousov orphanpg(pgrp);
9025844bd05SKonstantin Belousov } else
9035844bd05SKonstantin Belousov pgrp->pg_flags &= ~PGRP_ORPHANED;
9045844bd05SKonstantin Belousov PGRP_UNLOCK(pgrp);
905f591779bSSeigo Tanimura }
906dfd5dee1SPeter Wemm }
907df8bae1dSRodney W. Grimes
90817182b13SMateusz Guzik void
killjobc(void)90917182b13SMateusz Guzik killjobc(void)
91017182b13SMateusz Guzik {
91117182b13SMateusz Guzik struct session *sp;
91217182b13SMateusz Guzik struct tty *tp;
91317182b13SMateusz Guzik struct proc *p;
91417182b13SMateusz Guzik struct vnode *ttyvp;
91517182b13SMateusz Guzik
91617182b13SMateusz Guzik p = curproc;
91717182b13SMateusz Guzik MPASS(p->p_flag & P_WEXIT);
918c5bc28b2SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED);
91917182b13SMateusz Guzik
92017182b13SMateusz Guzik if (SESS_LEADER(p)) {
92117182b13SMateusz Guzik sp = p->p_session;
92217182b13SMateusz Guzik
92317182b13SMateusz Guzik /*
92417182b13SMateusz Guzik * s_ttyp is not zero'd; we use this to indicate that
92517182b13SMateusz Guzik * the session once had a controlling terminal. (for
92617182b13SMateusz Guzik * logging and informational purposes)
92717182b13SMateusz Guzik */
92817182b13SMateusz Guzik SESS_LOCK(sp);
92917182b13SMateusz Guzik ttyvp = sp->s_ttyvp;
93017182b13SMateusz Guzik tp = sp->s_ttyp;
93117182b13SMateusz Guzik sp->s_ttyvp = NULL;
93217182b13SMateusz Guzik sp->s_ttydp = NULL;
93317182b13SMateusz Guzik sp->s_leader = NULL;
93417182b13SMateusz Guzik SESS_UNLOCK(sp);
93517182b13SMateusz Guzik
93617182b13SMateusz Guzik /*
93717182b13SMateusz Guzik * Signal foreground pgrp and revoke access to
93817182b13SMateusz Guzik * controlling terminal if it has not been revoked
93917182b13SMateusz Guzik * already.
94017182b13SMateusz Guzik *
94117182b13SMateusz Guzik * Because the TTY may have been revoked in the mean
94217182b13SMateusz Guzik * time and could already have a new session associated
94317182b13SMateusz Guzik * with it, make sure we don't send a SIGHUP to a
94417182b13SMateusz Guzik * foreground process group that does not belong to this
94517182b13SMateusz Guzik * session.
94617182b13SMateusz Guzik */
94717182b13SMateusz Guzik
94817182b13SMateusz Guzik if (tp != NULL) {
94917182b13SMateusz Guzik tty_lock(tp);
95017182b13SMateusz Guzik if (tp->t_session == sp)
95117182b13SMateusz Guzik tty_signal_pgrp(tp, SIGHUP);
95217182b13SMateusz Guzik tty_unlock(tp);
95317182b13SMateusz Guzik }
95417182b13SMateusz Guzik
95517182b13SMateusz Guzik if (ttyvp != NULL) {
95617182b13SMateusz Guzik sx_xunlock(&proctree_lock);
95717182b13SMateusz Guzik if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
95817182b13SMateusz Guzik VOP_REVOKE(ttyvp, REVOKEALL);
959b249ce48SMateusz Guzik VOP_UNLOCK(ttyvp);
96017182b13SMateusz Guzik }
9613b444436SMateusz Guzik devfs_ctty_unref(ttyvp);
96217182b13SMateusz Guzik sx_xlock(&proctree_lock);
96317182b13SMateusz Guzik }
96417182b13SMateusz Guzik }
965c5bc28b2SKonstantin Belousov fixjobc_kill(p);
96617182b13SMateusz Guzik }
96717182b13SMateusz Guzik
968df8bae1dSRodney W. Grimes /*
9695844bd05SKonstantin Belousov * A process group has become orphaned, mark it as such for signal
9705844bd05SKonstantin Belousov * delivery code. If there are any stopped processes in the group,
971df8bae1dSRodney W. Grimes * hang-up all process in that group.
972df8bae1dSRodney W. Grimes */
973df8bae1dSRodney W. Grimes static void
orphanpg(struct pgrp * pg)97418c23dffSEd Maste orphanpg(struct pgrp *pg)
975df8bae1dSRodney W. Grimes {
97618c23dffSEd Maste struct proc *p;
977df8bae1dSRodney W. Grimes
978f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pg, MA_OWNED);
979f591779bSSeigo Tanimura
9805844bd05SKonstantin Belousov pg->pg_flags |= PGRP_ORPHANED;
9815844bd05SKonstantin Belousov
9821b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) {
98302e878d9SJohn Baldwin PROC_LOCK(p);
9844295bec1SJohn Baldwin if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
98502e878d9SJohn Baldwin PROC_UNLOCK(p);
9861b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) {
98715e9ec51SJohn Baldwin PROC_LOCK(p);
9888451d0ddSKip Macy kern_psignal(p, SIGHUP);
9898451d0ddSKip Macy kern_psignal(p, SIGCONT);
99015e9ec51SJohn Baldwin PROC_UNLOCK(p);
991df8bae1dSRodney W. Grimes }
992df8bae1dSRodney W. Grimes return;
993df8bae1dSRodney W. Grimes }
99402e878d9SJohn Baldwin PROC_UNLOCK(p);
995df8bae1dSRodney W. Grimes }
996df8bae1dSRodney W. Grimes }
997df8bae1dSRodney W. Grimes
998572b4402SPoul-Henning Kamp void
sess_hold(struct session * s)999bc093719SEd Schouten sess_hold(struct session *s)
1000572b4402SPoul-Henning Kamp {
1001572b4402SPoul-Henning Kamp
1002bc093719SEd Schouten refcount_acquire(&s->s_count);
1003bc093719SEd Schouten }
1004bc093719SEd Schouten
1005bc093719SEd Schouten void
sess_release(struct session * s)1006bc093719SEd Schouten sess_release(struct session *s)
1007bc093719SEd Schouten {
1008bc093719SEd Schouten
1009bc093719SEd Schouten if (refcount_release(&s->s_count)) {
1010bc093719SEd Schouten if (s->s_ttyp != NULL) {
1011bc093719SEd Schouten tty_lock(s->s_ttyp);
1012bc093719SEd Schouten tty_rel_sess(s->s_ttyp, s);
1013bc093719SEd Schouten }
101434ebdceaSMateusz Guzik proc_id_clear(PROC_ID_SESSION, s->s_sid);
1015572b4402SPoul-Henning Kamp mtx_destroy(&s->s_mtx);
10161ede983cSDag-Erling Smørgrav free(s, M_SESSION);
1017572b4402SPoul-Henning Kamp }
1018572b4402SPoul-Henning Kamp }
1019572b4402SPoul-Henning Kamp
1020831031ceSBruce Evans #ifdef DDB
1021831031ceSBruce Evans
102282207cd2SKonstantin Belousov static void
db_print_pgrp_one(struct pgrp * pgrp,struct proc * p)102382207cd2SKonstantin Belousov db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
102482207cd2SKonstantin Belousov {
102582207cd2SKonstantin Belousov db_printf(
102682207cd2SKonstantin Belousov " pid %d at %p pr %d pgrp %p e %d jc %d\n",
102782207cd2SKonstantin Belousov p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
102882207cd2SKonstantin Belousov p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
102982207cd2SKonstantin Belousov p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
103082207cd2SKonstantin Belousov }
103182207cd2SKonstantin Belousov
DB_SHOW_COMMAND_FLAGS(pgrpdump,pgrpdump,DB_CMD_MEMSAFE)1032c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(pgrpdump, pgrpdump, DB_CMD_MEMSAFE)
1033df8bae1dSRodney W. Grimes {
103418c23dffSEd Maste struct pgrp *pgrp;
103518c23dffSEd Maste struct proc *p;
103618c23dffSEd Maste int i;
1037df8bae1dSRodney W. Grimes
1038b75356e1SJeffrey Hsu for (i = 0; i <= pgrphash; i++) {
10391b727751SPoul-Henning Kamp if (!LIST_EMPTY(&pgrphashtbl[i])) {
104082207cd2SKonstantin Belousov db_printf("indx %d\n", i);
10411b727751SPoul-Henning Kamp LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
104282207cd2SKonstantin Belousov db_printf(
104382207cd2SKonstantin Belousov " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
104482207cd2SKonstantin Belousov pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1045b75356e1SJeffrey Hsu pgrp->pg_session->s_count,
104682207cd2SKonstantin Belousov LIST_FIRST(&pgrp->pg_members));
104782207cd2SKonstantin Belousov LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
104882207cd2SKonstantin Belousov db_print_pgrp_one(pgrp, p);
1049df8bae1dSRodney W. Grimes }
1050df8bae1dSRodney W. Grimes }
1051df8bae1dSRodney W. Grimes }
1052df8bae1dSRodney W. Grimes }
1053831031ceSBruce Evans #endif /* DDB */
1054972f9b20SPoul-Henning Kamp
1055972f9b20SPoul-Henning Kamp /*
1056f8d90480SAttilio Rao * Calculate the kinfo_proc members which contain process-wide
1057f8d90480SAttilio Rao * informations.
1058f8d90480SAttilio Rao * Must be called with the target process locked.
1059f8d90480SAttilio Rao */
1060f8d90480SAttilio Rao static void
fill_kinfo_aggregate(struct proc * p,struct kinfo_proc * kp)1061f8d90480SAttilio Rao fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1062f8d90480SAttilio Rao {
1063f8d90480SAttilio Rao struct thread *td;
1064f8d90480SAttilio Rao
1065f8d90480SAttilio Rao PROC_LOCK_ASSERT(p, MA_OWNED);
1066f8d90480SAttilio Rao
1067f8d90480SAttilio Rao kp->ki_estcpu = 0;
1068f8d90480SAttilio Rao kp->ki_pctcpu = 0;
1069f8d90480SAttilio Rao FOREACH_THREAD_IN_PROC(p, td) {
1070f8d90480SAttilio Rao thread_lock(td);
1071f8d90480SAttilio Rao kp->ki_pctcpu += sched_pctcpu(td);
1072ccd0ec40SKonstantin Belousov kp->ki_estcpu += sched_estcpu(td);
1073f8d90480SAttilio Rao thread_unlock(td);
1074f8d90480SAttilio Rao }
1075f8d90480SAttilio Rao }
1076f8d90480SAttilio Rao
1077f8d90480SAttilio Rao /*
1078cf4f802eSKonstantin Belousov * Fill in any information that is common to all threads in the process.
107965c9b430SJohn Baldwin * Must be called with the target process locked.
1080972f9b20SPoul-Henning Kamp */
10815032ff81SDon Lewis static void
fill_kinfo_proc_only(struct proc * p,struct kinfo_proc * kp)10825032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1083972f9b20SPoul-Henning Kamp {
10844093529dSJeff Roberson struct thread *td0;
1085d079d0a0SPawel Jakub Dawidek struct ucred *cred;
108690af4afaSJohn Baldwin struct sigacts *ps;
1087584b675eSKonstantin Belousov struct timeval boottime;
1088972f9b20SPoul-Henning Kamp
1089374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED);
1090972f9b20SPoul-Henning Kamp
10911f7d2501SKirk McKusick kp->ki_structsize = sizeof(*kp);
10921f7d2501SKirk McKusick kp->ki_paddr = p;
10936617724cSJeff Roberson kp->ki_addr =/* p->p_addr; */0; /* XXX */
10941f7d2501SKirk McKusick kp->ki_args = p->p_args;
10951f7d2501SKirk McKusick kp->ki_textvp = p->p_textvp;
10966c84de02SJohn Baldwin #ifdef KTRACE
10971762f674SKonstantin Belousov kp->ki_tracep = ktr_get_tracevp(p, false);
10986c84de02SJohn Baldwin kp->ki_traceflag = p->p_traceflag;
10996c84de02SJohn Baldwin #endif
11001f7d2501SKirk McKusick kp->ki_fd = p->p_fd;
110185078b85SConrad Meyer kp->ki_pd = p->p_pd;
11021f7d2501SKirk McKusick kp->ki_vmspace = p->p_vmspace;
1103cefcecbeSPawel Jakub Dawidek kp->ki_flag = p->p_flag;
110455648840SJohn Baldwin kp->ki_flag2 = p->p_flag2;
1105d079d0a0SPawel Jakub Dawidek cred = p->p_ucred;
1106d079d0a0SPawel Jakub Dawidek if (cred) {
1107d079d0a0SPawel Jakub Dawidek kp->ki_uid = cred->cr_uid;
1108d079d0a0SPawel Jakub Dawidek kp->ki_ruid = cred->cr_ruid;
1109d079d0a0SPawel Jakub Dawidek kp->ki_svuid = cred->cr_svuid;
111096fcc75fSRobert Watson kp->ki_cr_flags = 0;
111196fcc75fSRobert Watson if (cred->cr_flags & CRED_FLAG_CAPMODE)
111296fcc75fSRobert Watson kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
11131b5768beSBrooks Davis /* XXX bde doesn't like KI_NGROUPS */
11141b5768beSBrooks Davis if (cred->cr_ngroups > KI_NGROUPS) {
11151b5768beSBrooks Davis kp->ki_ngroups = KI_NGROUPS;
11161b5768beSBrooks Davis kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
11171b5768beSBrooks Davis } else
1118838d9858SBrooks Davis kp->ki_ngroups = cred->cr_ngroups;
11191b5768beSBrooks Davis bcopy(cred->cr_groups, kp->ki_groups,
11201b5768beSBrooks Davis kp->ki_ngroups * sizeof(gid_t));
1121d079d0a0SPawel Jakub Dawidek kp->ki_rgid = cred->cr_rgid;
1122d079d0a0SPawel Jakub Dawidek kp->ki_svgid = cred->cr_svgid;
1123cefcecbeSPawel Jakub Dawidek /* If jailed(cred), emulate the old P_JAILED flag. */
1124c78941e6SPawel Jakub Dawidek if (jailed(cred)) {
1125cefcecbeSPawel Jakub Dawidek kp->ki_flag |= P_JAILED;
11260304c731SJamie Gritton /* If inside the jail, use 0 as a jail ID. */
11270304c731SJamie Gritton if (cred->cr_prison != curthread->td_ucred->cr_prison)
1128c78941e6SPawel Jakub Dawidek kp->ki_jid = cred->cr_prison->pr_id;
1129c78941e6SPawel Jakub Dawidek }
11307123f4cdSEdward Tomasz Napierala strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
11317123f4cdSEdward Tomasz Napierala sizeof(kp->ki_loginclass));
1132972f9b20SPoul-Henning Kamp }
113390af4afaSJohn Baldwin ps = p->p_sigacts;
1134d079d0a0SPawel Jakub Dawidek if (ps) {
113590af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx);
113690af4afaSJohn Baldwin kp->ki_sigignore = ps->ps_sigignore;
113790af4afaSJohn Baldwin kp->ki_sigcatch = ps->ps_sigcatch;
113890af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx);
1139d8c85307SJulian Elischer }
1140e602ba25SJulian Elischer if (p->p_state != PRS_NEW &&
1141e602ba25SJulian Elischer p->p_state != PRS_ZOMBIE &&
1142e602ba25SJulian Elischer p->p_vmspace != NULL) {
11431f7d2501SKirk McKusick struct vmspace *vm = p->p_vmspace;
1144cd73303cSDavid Greenman
11451f7d2501SKirk McKusick kp->ki_size = vm->vm_map.size;
11461f7d2501SKirk McKusick kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1147e24a6552SMark Johnston FOREACH_THREAD_IN_PROC(p, td0)
1148ceff7f2aSTim J. Robbins kp->ki_rssize += td0->td_kstack_pages;
11491f7d2501SKirk McKusick kp->ki_swrss = vm->vm_swrss;
11501f7d2501SKirk McKusick kp->ki_tsize = vm->vm_tsize;
11511f7d2501SKirk McKusick kp->ki_dsize = vm->vm_dsize;
11521f7d2501SKirk McKusick kp->ki_ssize = vm->vm_ssize;
11535032ff81SDon Lewis } else if (p->p_state == PRS_ZOMBIE)
11545032ff81SDon Lewis kp->ki_stat = SZOMB;
1155b61ce5b0SJeff Roberson kp->ki_sflag = PS_INMEM;
115654b0e65fSJeff Roberson /* Calculate legacy swtime as seconds since 'swtick'. */
115754b0e65fSJeff Roberson kp->ki_swtime = (ticks - p->p_swtick) / hz;
1158cebabef0SPawel Jakub Dawidek kp->ki_pid = p->p_pid;
1159cebabef0SPawel Jakub Dawidek kp->ki_nice = p->p_nice;
11605e9ccc87SWill Andrews kp->ki_fibnum = p->p_fibnum;
116178c85e8dSJohn Baldwin kp->ki_start = p->p_stats->p_start;
1162584b675eSKonstantin Belousov getboottime(&boottime);
116378c85e8dSJohn Baldwin timevaladd(&kp->ki_start, &boottime);
11645c7bebf9SKonstantin Belousov PROC_STATLOCK(p);
11658e6fa660SJohn Baldwin rufetch(p, &kp->ki_rusage);
11668e6fa660SJohn Baldwin kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
116778c85e8dSJohn Baldwin calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
11685c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p);
116978c85e8dSJohn Baldwin calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1170a37e14e1SEdward Tomasz Napierala /* Some callers want child times in a single value. */
117178c85e8dSJohn Baldwin kp->ki_childtime = kp->ki_childstime;
117278c85e8dSJohn Baldwin timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1173a37e14e1SEdward Tomasz Napierala
11744d34e019SKonstantin Belousov FOREACH_THREAD_IN_PROC(p, td0)
11754d34e019SKonstantin Belousov kp->ki_cow += td0->td_cow;
11764d34e019SKonstantin Belousov
117713b762a3SEd Maste if (p->p_comm[0] != '\0')
1178e80fb434SRobert Drehmel strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1179078842c5SGarance A Drosehn if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1180078842c5SGarance A Drosehn p->p_sysent->sv_name[0] != '\0')
1181078842c5SGarance A Drosehn strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
11821d9c5696SJuli Mallett kp->ki_siglist = p->p_siglist;
1183b4490c6eSKonstantin Belousov kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
11841f7d2501SKirk McKusick kp->ki_acflag = p->p_acflag;
11851f7d2501SKirk McKusick kp->ki_lock = p->p_lock;
11865b5477d7SGleb Smirnoff if (p->p_pptr) {
11872c054ce9SMateusz Guzik kp->ki_ppid = p->p_oppid;
11888b04bbefSMateusz Guzik if (p->p_flag & P_TRACED)
11898b04bbefSMateusz Guzik kp->ki_tracer = p->p_pptr->p_pid;
1190972f9b20SPoul-Henning Kamp }
11915b5477d7SGleb Smirnoff }
1192972f9b20SPoul-Henning Kamp
11935032ff81SDon Lewis /*
1194cf4f802eSKonstantin Belousov * Fill job-related process information.
1195cf4f802eSKonstantin Belousov */
1196cf4f802eSKonstantin Belousov static void
fill_kinfo_proc_pgrp(struct proc * p,struct kinfo_proc * kp)1197cf4f802eSKonstantin Belousov fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp)
1198cf4f802eSKonstantin Belousov {
1199cf4f802eSKonstantin Belousov struct tty *tp;
1200cf4f802eSKonstantin Belousov struct session *sp;
1201cf4f802eSKonstantin Belousov struct pgrp *pgrp;
1202cf4f802eSKonstantin Belousov
1203cf4f802eSKonstantin Belousov sx_assert(&proctree_lock, SA_LOCKED);
1204cf4f802eSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED);
1205cf4f802eSKonstantin Belousov
1206cf4f802eSKonstantin Belousov pgrp = p->p_pgrp;
1207cf4f802eSKonstantin Belousov if (pgrp == NULL)
1208cf4f802eSKonstantin Belousov return;
1209cf4f802eSKonstantin Belousov
1210cf4f802eSKonstantin Belousov kp->ki_pgid = pgrp->pg_id;
12115844bd05SKonstantin Belousov kp->ki_jobc = pgrp_calc_jobc(pgrp);
1212cf4f802eSKonstantin Belousov
1213cf4f802eSKonstantin Belousov sp = pgrp->pg_session;
1214cf4f802eSKonstantin Belousov tp = NULL;
1215cf4f802eSKonstantin Belousov
1216cf4f802eSKonstantin Belousov if (sp != NULL) {
1217cf4f802eSKonstantin Belousov kp->ki_sid = sp->s_sid;
1218cf4f802eSKonstantin Belousov SESS_LOCK(sp);
1219cf4f802eSKonstantin Belousov strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login));
1220cf4f802eSKonstantin Belousov if (sp->s_ttyvp)
1221cf4f802eSKonstantin Belousov kp->ki_kiflag |= KI_CTTY;
1222cf4f802eSKonstantin Belousov if (SESS_LEADER(p))
1223cf4f802eSKonstantin Belousov kp->ki_kiflag |= KI_SLEADER;
1224cf4f802eSKonstantin Belousov tp = sp->s_ttyp;
1225cf4f802eSKonstantin Belousov SESS_UNLOCK(sp);
1226cf4f802eSKonstantin Belousov }
1227cf4f802eSKonstantin Belousov
1228cf4f802eSKonstantin Belousov if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1229cf4f802eSKonstantin Belousov kp->ki_tdev = tty_udev(tp);
1230cf4f802eSKonstantin Belousov kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1231cf4f802eSKonstantin Belousov kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1232cf4f802eSKonstantin Belousov if (tp->t_session)
1233cf4f802eSKonstantin Belousov kp->ki_tsid = tp->t_session->s_sid;
1234cf4f802eSKonstantin Belousov } else {
1235cf4f802eSKonstantin Belousov kp->ki_tdev = NODEV;
1236cf4f802eSKonstantin Belousov kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1237cf4f802eSKonstantin Belousov }
1238cf4f802eSKonstantin Belousov }
1239cf4f802eSKonstantin Belousov
1240cf4f802eSKonstantin Belousov /*
124100305546SKonstantin Belousov * Fill in information that is thread specific. Must be called with
124200305546SKonstantin Belousov * target process locked. If 'preferthread' is set, overwrite certain
124300305546SKonstantin Belousov * process-related fields that are maintained for both threads and
124400305546SKonstantin Belousov * processes.
12455032ff81SDon Lewis */
12465032ff81SDon Lewis static void
fill_kinfo_thread(struct thread * td,struct kinfo_proc * kp,int preferthread)1247d92909c1SRobert Watson fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
12485032ff81SDon Lewis {
12495032ff81SDon Lewis struct proc *p;
12505032ff81SDon Lewis
12515032ff81SDon Lewis p = td->td_proc;
12526239ef1dSEd Maste kp->ki_tdaddr = td;
1253374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED);
12545032ff81SDon Lewis
12552417d97eSJohn Baldwin if (preferthread)
12565c7bebf9SKonstantin Belousov PROC_STATLOCK(p);
1257982d11f8SJeff Roberson thread_lock(td);
12585032ff81SDon Lewis if (td->td_wmesg != NULL)
12595032ff81SDon Lewis strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
12605032ff81SDon Lewis else
12615032ff81SDon Lewis bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
12623d32d4a7SEric van Gyzen if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
12633d32d4a7SEric van Gyzen sizeof(kp->ki_tdname)) {
12643d32d4a7SEric van Gyzen strlcpy(kp->ki_moretdname,
12653d32d4a7SEric van Gyzen td->td_name + sizeof(kp->ki_tdname) - 1,
12663d32d4a7SEric van Gyzen sizeof(kp->ki_moretdname));
12673d32d4a7SEric van Gyzen } else {
12683d32d4a7SEric van Gyzen bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
12693d32d4a7SEric van Gyzen }
12705032ff81SDon Lewis if (TD_ON_LOCK(td)) {
12715032ff81SDon Lewis kp->ki_kiflag |= KI_LOCKBLOCK;
12725032ff81SDon Lewis strlcpy(kp->ki_lockname, td->td_lockname,
12735032ff81SDon Lewis sizeof(kp->ki_lockname));
12745032ff81SDon Lewis } else {
12755032ff81SDon Lewis kp->ki_kiflag &= ~KI_LOCKBLOCK;
12765032ff81SDon Lewis bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
12775032ff81SDon Lewis }
12785032ff81SDon Lewis
12796617724cSJeff Roberson if (p->p_state == PRS_NORMAL) { /* approximate. */
12805032ff81SDon Lewis if (TD_ON_RUNQ(td) ||
12815032ff81SDon Lewis TD_CAN_RUN(td) ||
12825032ff81SDon Lewis TD_IS_RUNNING(td)) {
12835032ff81SDon Lewis kp->ki_stat = SRUN;
12845032ff81SDon Lewis } else if (P_SHOULDSTOP(p)) {
12855032ff81SDon Lewis kp->ki_stat = SSTOP;
12865032ff81SDon Lewis } else if (TD_IS_SLEEPING(td)) {
12875032ff81SDon Lewis kp->ki_stat = SSLEEP;
12885032ff81SDon Lewis } else if (TD_ON_LOCK(td)) {
12895032ff81SDon Lewis kp->ki_stat = SLOCK;
12905032ff81SDon Lewis } else {
12915032ff81SDon Lewis kp->ki_stat = SWAIT;
12925032ff81SDon Lewis }
12933357835aSDavid Xu } else if (p->p_state == PRS_ZOMBIE) {
12943357835aSDavid Xu kp->ki_stat = SZOMB;
12955032ff81SDon Lewis } else {
12965032ff81SDon Lewis kp->ki_stat = SIDL;
12975032ff81SDon Lewis }
12985032ff81SDon Lewis
12995032ff81SDon Lewis /* Things in the thread */
13005032ff81SDon Lewis kp->ki_wchan = td->td_wchan;
13015032ff81SDon Lewis kp->ki_pri.pri_level = td->td_priority;
13025032ff81SDon Lewis kp->ki_pri.pri_native = td->td_base_pri;
1303e77f9fedSAdrian Chadd
1304e77f9fedSAdrian Chadd /*
1305e77f9fedSAdrian Chadd * Note: legacy fields; clamp at the old NOCPU value and/or
1306e77f9fedSAdrian Chadd * the maximum u_char CPU value.
1307e77f9fedSAdrian Chadd */
1308e77f9fedSAdrian Chadd if (td->td_lastcpu == NOCPU)
1309e77f9fedSAdrian Chadd kp->ki_lastcpu_old = NOCPU_OLD;
1310e77f9fedSAdrian Chadd else if (td->td_lastcpu > MAXCPU_OLD)
1311e77f9fedSAdrian Chadd kp->ki_lastcpu_old = MAXCPU_OLD;
1312e77f9fedSAdrian Chadd else
1313e77f9fedSAdrian Chadd kp->ki_lastcpu_old = td->td_lastcpu;
1314e77f9fedSAdrian Chadd
1315e77f9fedSAdrian Chadd if (td->td_oncpu == NOCPU)
1316e77f9fedSAdrian Chadd kp->ki_oncpu_old = NOCPU_OLD;
1317e77f9fedSAdrian Chadd else if (td->td_oncpu > MAXCPU_OLD)
1318e77f9fedSAdrian Chadd kp->ki_oncpu_old = MAXCPU_OLD;
1319e77f9fedSAdrian Chadd else
1320e77f9fedSAdrian Chadd kp->ki_oncpu_old = td->td_oncpu;
1321e77f9fedSAdrian Chadd
13225032ff81SDon Lewis kp->ki_lastcpu = td->td_lastcpu;
13235032ff81SDon Lewis kp->ki_oncpu = td->td_oncpu;
13245032ff81SDon Lewis kp->ki_tdflags = td->td_flags;
13255032ff81SDon Lewis kp->ki_tid = td->td_tid;
13265032ff81SDon Lewis kp->ki_numthreads = p->p_numthreads;
13275032ff81SDon Lewis kp->ki_pcb = td->td_pcb;
13285032ff81SDon Lewis kp->ki_kstack = (void *)td->td_kstack;
132954b0e65fSJeff Roberson kp->ki_slptime = (ticks - td->td_slptick) / hz;
13308460a577SJohn Birrell kp->ki_pri.pri_class = td->td_pri_class;
13318460a577SJohn Birrell kp->ki_pri.pri_user = td->td_user_pri;
13325032ff81SDon Lewis
1333f8d90480SAttilio Rao if (preferthread) {
13342417d97eSJohn Baldwin rufetchtd(td, &kp->ki_rusage);
1335213c077fSKonstantin Belousov kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1336f8d90480SAttilio Rao kp->ki_pctcpu = sched_pctcpu(td);
1337ccd0ec40SKonstantin Belousov kp->ki_estcpu = sched_estcpu(td);
13384d34e019SKonstantin Belousov kp->ki_cow = td->td_cow;
1339f8d90480SAttilio Rao }
1340d92909c1SRobert Watson
13415032ff81SDon Lewis /* We can't get this anymore but ps etc never used it anyway. */
13425032ff81SDon Lewis kp->ki_rqindex = 0;
13435032ff81SDon Lewis
134400305546SKonstantin Belousov if (preferthread)
134500305546SKonstantin Belousov kp->ki_siglist = td->td_siglist;
13465032ff81SDon Lewis kp->ki_sigmask = td->td_sigmask;
1347982d11f8SJeff Roberson thread_unlock(td);
13482417d97eSJohn Baldwin if (preferthread)
13495c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p);
13505032ff81SDon Lewis }
13515032ff81SDon Lewis
13525032ff81SDon Lewis /*
13535032ff81SDon Lewis * Fill in a kinfo_proc structure for the specified process.
13545032ff81SDon Lewis * Must be called with the target process locked.
13555032ff81SDon Lewis */
13565032ff81SDon Lewis void
fill_kinfo_proc(struct proc * p,struct kinfo_proc * kp)13575032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
13585032ff81SDon Lewis {
1359f8d90480SAttilio Rao MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1360f8d90480SAttilio Rao
1361cf4f802eSKonstantin Belousov bzero(kp, sizeof(*kp));
1362cf4f802eSKonstantin Belousov
1363cf4f802eSKonstantin Belousov fill_kinfo_proc_pgrp(p,kp);
13645032ff81SDon Lewis fill_kinfo_proc_only(p, kp);
1365d92909c1SRobert Watson fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1366f8d90480SAttilio Rao fill_kinfo_aggregate(p, kp);
13675032ff81SDon Lewis }
13685032ff81SDon Lewis
13698b059651SDavid Schultz struct pstats *
pstats_alloc(void)13708b059651SDavid Schultz pstats_alloc(void)
13718b059651SDavid Schultz {
13728b059651SDavid Schultz
13738b059651SDavid Schultz return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
13748b059651SDavid Schultz }
13758b059651SDavid Schultz
13768b059651SDavid Schultz /*
13778b059651SDavid Schultz * Copy parts of p_stats; zero the rest of p_stats (statistics).
13788b059651SDavid Schultz */
13798b059651SDavid Schultz void
pstats_fork(struct pstats * src,struct pstats * dst)13808b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst)
13818b059651SDavid Schultz {
13828b059651SDavid Schultz
13838b059651SDavid Schultz bzero(&dst->pstat_startzero,
13846db36923SDavid Schultz __rangeof(struct pstats, pstat_startzero, pstat_endzero));
13858b059651SDavid Schultz bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
13866db36923SDavid Schultz __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
13878b059651SDavid Schultz }
13888b059651SDavid Schultz
13898b059651SDavid Schultz void
pstats_free(struct pstats * ps)13908b059651SDavid Schultz pstats_free(struct pstats *ps)
13918b059651SDavid Schultz {
13928b059651SDavid Schultz
13938b059651SDavid Schultz free(ps, M_SUBPROC);
13948b059651SDavid Schultz }
13958b059651SDavid Schultz
139605e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32
139705e06d11SKonstantin Belousov
139805e06d11SKonstantin Belousov /*
139905e06d11SKonstantin Belousov * This function is typically used to copy out the kernel address, so
140005e06d11SKonstantin Belousov * it can be replaced by assignment of zero.
140105e06d11SKonstantin Belousov */
140205e06d11SKonstantin Belousov static inline uint32_t
ptr32_trim(const void * ptr)1403fea73412SConrad Meyer ptr32_trim(const void *ptr)
140405e06d11SKonstantin Belousov {
140505e06d11SKonstantin Belousov uintptr_t uptr;
140605e06d11SKonstantin Belousov
140705e06d11SKonstantin Belousov uptr = (uintptr_t)ptr;
140805e06d11SKonstantin Belousov return ((uptr > UINT_MAX) ? 0 : uptr);
140905e06d11SKonstantin Belousov }
141005e06d11SKonstantin Belousov
141105e06d11SKonstantin Belousov #define PTRTRIM_CP(src,dst,fld) \
141205e06d11SKonstantin Belousov do { (dst).fld = ptr32_trim((src).fld); } while (0)
141305e06d11SKonstantin Belousov
141405e06d11SKonstantin Belousov static void
freebsd32_kinfo_proc_out(const struct kinfo_proc * ki,struct kinfo_proc32 * ki32)141505e06d11SKonstantin Belousov freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
141605e06d11SKonstantin Belousov {
141705e06d11SKonstantin Belousov int i;
141805e06d11SKonstantin Belousov
141905e06d11SKonstantin Belousov bzero(ki32, sizeof(struct kinfo_proc32));
142005e06d11SKonstantin Belousov ki32->ki_structsize = sizeof(struct kinfo_proc32);
142105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_layout);
142205e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_args);
142305e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_paddr);
142405e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_addr);
142505e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_tracep);
142605e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_textvp);
142705e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_fd);
142805e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_vmspace);
142905e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_wchan);
143005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pid);
143105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ppid);
143205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pgid);
143305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tpgid);
143405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sid);
143505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tsid);
143605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_jobc);
143705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tdev);
143869921123SKonstantin Belousov CP(*ki, *ki32, ki_tdev_freebsd11);
143905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_siglist);
144005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigmask);
144105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigignore);
144205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigcatch);
144305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_uid);
144405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ruid);
144505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_svuid);
144605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rgid);
144705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_svgid);
144805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ngroups);
144905e06d11SKonstantin Belousov for (i = 0; i < KI_NGROUPS; i++)
145005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_groups[i]);
145105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_size);
145205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rssize);
145305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_swrss);
145405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tsize);
145505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_dsize);
145605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ssize);
145705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_xstat);
145805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_acflag);
145905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pctcpu);
146005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_estcpu);
146105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_slptime);
146205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_swtime);
1463371778a3SKonstantin Belousov CP(*ki, *ki32, ki_cow);
146405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_runtime);
146505e06d11SKonstantin Belousov TV_CP(*ki, *ki32, ki_start);
146605e06d11SKonstantin Belousov TV_CP(*ki, *ki32, ki_childtime);
146705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_flag);
146805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_kiflag);
146905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_traceflag);
147005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_stat);
147105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_nice);
147205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_lock);
147305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rqindex);
147405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_oncpu);
147505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_lastcpu);
1476e77f9fedSAdrian Chadd
1477e77f9fedSAdrian Chadd /* XXX TODO: wrap cpu value as appropriate */
1478e77f9fedSAdrian Chadd CP(*ki, *ki32, ki_oncpu_old);
1479e77f9fedSAdrian Chadd CP(*ki, *ki32, ki_lastcpu_old);
1480e77f9fedSAdrian Chadd
1481925af544SBjoern A. Zeeb bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
148205e06d11SKonstantin Belousov bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
148305e06d11SKonstantin Belousov bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
148405e06d11SKonstantin Belousov bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
148505e06d11SKonstantin Belousov bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
148605e06d11SKonstantin Belousov bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
14877123f4cdSEdward Tomasz Napierala bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
14883d32d4a7SEric van Gyzen bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
14898b04bbefSMateusz Guzik CP(*ki, *ki32, ki_tracer);
149055648840SJohn Baldwin CP(*ki, *ki32, ki_flag2);
14915e9ccc87SWill Andrews CP(*ki, *ki32, ki_fibnum);
149205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_cr_flags);
149305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_jid);
149405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_numthreads);
149505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tid);
149605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pri);
149705e06d11SKonstantin Belousov freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
149805e06d11SKonstantin Belousov freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
149905e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_pcb);
150005e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_kstack);
150105e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_udata);
15026aacc698SBrooks Davis PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
150305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sflag);
150405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tdflags);
150505e06d11SKonstantin Belousov }
150605e06d11SKonstantin Belousov #endif
150705e06d11SKonstantin Belousov
15089d4e369aSMateusz Guzik static ssize_t
kern_proc_out_size(struct proc * p,int flags)15099d4e369aSMateusz Guzik kern_proc_out_size(struct proc *p, int flags)
15109d4e369aSMateusz Guzik {
15119d4e369aSMateusz Guzik ssize_t size = 0;
15129d4e369aSMateusz Guzik
15139d4e369aSMateusz Guzik PROC_LOCK_ASSERT(p, MA_OWNED);
15149d4e369aSMateusz Guzik
15159d4e369aSMateusz Guzik if ((flags & KERN_PROC_NOTHREADS) != 0) {
15169d4e369aSMateusz Guzik #ifdef COMPAT_FREEBSD32
15179d4e369aSMateusz Guzik if ((flags & KERN_PROC_MASK32) != 0) {
15189d4e369aSMateusz Guzik size += sizeof(struct kinfo_proc32);
15199d4e369aSMateusz Guzik } else
15209d4e369aSMateusz Guzik #endif
15219d4e369aSMateusz Guzik size += sizeof(struct kinfo_proc);
15229d4e369aSMateusz Guzik } else {
15239d4e369aSMateusz Guzik #ifdef COMPAT_FREEBSD32
15249d4e369aSMateusz Guzik if ((flags & KERN_PROC_MASK32) != 0)
15259d4e369aSMateusz Guzik size += sizeof(struct kinfo_proc32) * p->p_numthreads;
15269d4e369aSMateusz Guzik else
15279d4e369aSMateusz Guzik #endif
15289d4e369aSMateusz Guzik size += sizeof(struct kinfo_proc) * p->p_numthreads;
15299d4e369aSMateusz Guzik }
15309d4e369aSMateusz Guzik PROC_UNLOCK(p);
15319d4e369aSMateusz Guzik return (size);
15329d4e369aSMateusz Guzik }
15339d4e369aSMateusz Guzik
1534fe52cf54SMikolaj Golub int
kern_proc_out(struct proc * p,struct sbuf * sb,int flags)1535fe52cf54SMikolaj Golub kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
15363ce93e4eSPoul-Henning Kamp {
153730c6f34eSScott Long struct thread *td;
1538fe52cf54SMikolaj Golub struct kinfo_proc ki;
1539fe52cf54SMikolaj Golub #ifdef COMPAT_FREEBSD32
1540fe52cf54SMikolaj Golub struct kinfo_proc32 ki32;
1541fe52cf54SMikolaj Golub #endif
1542fe52cf54SMikolaj Golub int error;
15433ce93e4eSPoul-Henning Kamp
154465c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED);
1545f8d90480SAttilio Rao MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
154630c6f34eSScott Long
1547fe52cf54SMikolaj Golub error = 0;
1548fe52cf54SMikolaj Golub fill_kinfo_proc(p, &ki);
1549fe52cf54SMikolaj Golub if ((flags & KERN_PROC_NOTHREADS) != 0) {
1550fe52cf54SMikolaj Golub #ifdef COMPAT_FREEBSD32
1551fe52cf54SMikolaj Golub if ((flags & KERN_PROC_MASK32) != 0) {
1552fe52cf54SMikolaj Golub freebsd32_kinfo_proc_out(&ki, &ki32);
155357c2505eSKonstantin Belousov if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
155457c2505eSKonstantin Belousov error = ENOMEM;
1555fe52cf54SMikolaj Golub } else
1556fe52cf54SMikolaj Golub #endif
155757c2505eSKonstantin Belousov if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
155857c2505eSKonstantin Belousov error = ENOMEM;
1559fe52cf54SMikolaj Golub } else {
156030c6f34eSScott Long FOREACH_THREAD_IN_PROC(p, td) {
1561fe52cf54SMikolaj Golub fill_kinfo_thread(td, &ki, 1);
1562fe52cf54SMikolaj Golub #ifdef COMPAT_FREEBSD32
1563fe52cf54SMikolaj Golub if ((flags & KERN_PROC_MASK32) != 0) {
1564fe52cf54SMikolaj Golub freebsd32_kinfo_proc_out(&ki, &ki32);
156557c2505eSKonstantin Belousov if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
156657c2505eSKonstantin Belousov error = ENOMEM;
1567fe52cf54SMikolaj Golub } else
1568fe52cf54SMikolaj Golub #endif
156957c2505eSKonstantin Belousov if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
157057c2505eSKonstantin Belousov error = ENOMEM;
157157c2505eSKonstantin Belousov if (error != 0)
157230c6f34eSScott Long break;
157330c6f34eSScott Long }
157430c6f34eSScott Long }
157530c6f34eSScott Long PROC_UNLOCK(p);
15763ce93e4eSPoul-Henning Kamp return (error);
1577fe52cf54SMikolaj Golub }
1578fe52cf54SMikolaj Golub
1579fe52cf54SMikolaj Golub static int
sysctl_out_proc(struct proc * p,struct sysctl_req * req,int flags)15809b57bf75SMateusz Guzik sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1581fe52cf54SMikolaj Golub {
1582fe52cf54SMikolaj Golub struct sbuf sb;
1583fe52cf54SMikolaj Golub struct kinfo_proc ki;
1584fe52cf54SMikolaj Golub int error, error2;
1585fe52cf54SMikolaj Golub
15869d4e369aSMateusz Guzik if (req->oldptr == NULL)
15879d4e369aSMateusz Guzik return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
15889d4e369aSMateusz Guzik
1589fe52cf54SMikolaj Golub sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
15901eafc078SIan Lepore sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1591fe52cf54SMikolaj Golub error = kern_proc_out(p, &sb, flags);
1592fe52cf54SMikolaj Golub error2 = sbuf_finish(&sb);
1593fe52cf54SMikolaj Golub sbuf_delete(&sb);
1594fe52cf54SMikolaj Golub if (error != 0)
1595fe52cf54SMikolaj Golub return (error);
1596fe52cf54SMikolaj Golub else if (error2 != 0)
1597fe52cf54SMikolaj Golub return (error2);
15983ce93e4eSPoul-Henning Kamp return (0);
15993ce93e4eSPoul-Henning Kamp }
16003ce93e4eSPoul-Henning Kamp
16013d3e6793SMateusz Guzik int
proc_iterate(int (* cb)(struct proc *,void *),void * cbarg)16023d3e6793SMateusz Guzik proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
16033d3e6793SMateusz Guzik {
16043d3e6793SMateusz Guzik struct proc *p;
16053d3e6793SMateusz Guzik int error, i, j;
16063d3e6793SMateusz Guzik
16073d3e6793SMateusz Guzik for (i = 0; i < pidhashlock + 1; i++) {
16084daea938SKonstantin Belousov sx_slock(&proctree_lock);
16093d3e6793SMateusz Guzik sx_slock(&pidhashtbl_lock[i]);
16103d3e6793SMateusz Guzik for (j = i; j <= pidhash; j += pidhashlock + 1) {
16113d3e6793SMateusz Guzik LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
16123d3e6793SMateusz Guzik if (p->p_state == PRS_NEW)
16133d3e6793SMateusz Guzik continue;
16143d3e6793SMateusz Guzik error = cb(p, cbarg);
16153d3e6793SMateusz Guzik PROC_LOCK_ASSERT(p, MA_NOTOWNED);
16163d3e6793SMateusz Guzik if (error != 0) {
16173d3e6793SMateusz Guzik sx_sunlock(&pidhashtbl_lock[i]);
16184daea938SKonstantin Belousov sx_sunlock(&proctree_lock);
16193d3e6793SMateusz Guzik return (error);
16203d3e6793SMateusz Guzik }
16213d3e6793SMateusz Guzik }
16223d3e6793SMateusz Guzik }
16233d3e6793SMateusz Guzik sx_sunlock(&pidhashtbl_lock[i]);
16244daea938SKonstantin Belousov sx_sunlock(&proctree_lock);
16253d3e6793SMateusz Guzik }
16263d3e6793SMateusz Guzik return (0);
16273d3e6793SMateusz Guzik }
16283d3e6793SMateusz Guzik
16293d3e6793SMateusz Guzik struct kern_proc_out_args {
16303d3e6793SMateusz Guzik struct sysctl_req *req;
16313d3e6793SMateusz Guzik int flags;
16323d3e6793SMateusz Guzik int oid_number;
16333d3e6793SMateusz Guzik int *name;
16343d3e6793SMateusz Guzik };
16353d3e6793SMateusz Guzik
16363d3e6793SMateusz Guzik static int
sysctl_kern_proc_iterate(struct proc * p,void * origarg)16373d3e6793SMateusz Guzik sysctl_kern_proc_iterate(struct proc *p, void *origarg)
16383d3e6793SMateusz Guzik {
16393d3e6793SMateusz Guzik struct kern_proc_out_args *arg = origarg;
16403d3e6793SMateusz Guzik int *name = arg->name;
16413d3e6793SMateusz Guzik int oid_number = arg->oid_number;
16423d3e6793SMateusz Guzik int flags = arg->flags;
16433d3e6793SMateusz Guzik struct sysctl_req *req = arg->req;
16443d3e6793SMateusz Guzik int error = 0;
16453d3e6793SMateusz Guzik
16463d3e6793SMateusz Guzik PROC_LOCK(p);
16473d3e6793SMateusz Guzik
16483d3e6793SMateusz Guzik KASSERT(p->p_ucred != NULL,
16493d3e6793SMateusz Guzik ("process credential is NULL for non-NEW proc"));
16503d3e6793SMateusz Guzik /*
16513d3e6793SMateusz Guzik * Show a user only appropriate processes.
16523d3e6793SMateusz Guzik */
16533d3e6793SMateusz Guzik if (p_cansee(curthread, p))
16543d3e6793SMateusz Guzik goto skip;
16553d3e6793SMateusz Guzik /*
16563d3e6793SMateusz Guzik * TODO - make more efficient (see notes below).
16573d3e6793SMateusz Guzik * do by session.
16583d3e6793SMateusz Guzik */
16593d3e6793SMateusz Guzik switch (oid_number) {
16603d3e6793SMateusz Guzik case KERN_PROC_GID:
16613d3e6793SMateusz Guzik if (p->p_ucred->cr_gid != (gid_t)name[0])
16623d3e6793SMateusz Guzik goto skip;
16633d3e6793SMateusz Guzik break;
16643d3e6793SMateusz Guzik
16653d3e6793SMateusz Guzik case KERN_PROC_PGRP:
16663d3e6793SMateusz Guzik /* could do this by traversing pgrp */
16673d3e6793SMateusz Guzik if (p->p_pgrp == NULL ||
16683d3e6793SMateusz Guzik p->p_pgrp->pg_id != (pid_t)name[0])
16693d3e6793SMateusz Guzik goto skip;
16703d3e6793SMateusz Guzik break;
16713d3e6793SMateusz Guzik
16723d3e6793SMateusz Guzik case KERN_PROC_RGID:
16733d3e6793SMateusz Guzik if (p->p_ucred->cr_rgid != (gid_t)name[0])
16743d3e6793SMateusz Guzik goto skip;
16753d3e6793SMateusz Guzik break;
16763d3e6793SMateusz Guzik
16773d3e6793SMateusz Guzik case KERN_PROC_SESSION:
16783d3e6793SMateusz Guzik if (p->p_session == NULL ||
16793d3e6793SMateusz Guzik p->p_session->s_sid != (pid_t)name[0])
16803d3e6793SMateusz Guzik goto skip;
16813d3e6793SMateusz Guzik break;
16823d3e6793SMateusz Guzik
16833d3e6793SMateusz Guzik case KERN_PROC_TTY:
16843d3e6793SMateusz Guzik if ((p->p_flag & P_CONTROLT) == 0 ||
16853d3e6793SMateusz Guzik p->p_session == NULL)
16863d3e6793SMateusz Guzik goto skip;
16873d3e6793SMateusz Guzik /* XXX proctree_lock */
16883d3e6793SMateusz Guzik SESS_LOCK(p->p_session);
16893d3e6793SMateusz Guzik if (p->p_session->s_ttyp == NULL ||
16903d3e6793SMateusz Guzik tty_udev(p->p_session->s_ttyp) !=
16913d3e6793SMateusz Guzik (dev_t)name[0]) {
16923d3e6793SMateusz Guzik SESS_UNLOCK(p->p_session);
16933d3e6793SMateusz Guzik goto skip;
16943d3e6793SMateusz Guzik }
16953d3e6793SMateusz Guzik SESS_UNLOCK(p->p_session);
16963d3e6793SMateusz Guzik break;
16973d3e6793SMateusz Guzik
16983d3e6793SMateusz Guzik case KERN_PROC_UID:
16993d3e6793SMateusz Guzik if (p->p_ucred->cr_uid != (uid_t)name[0])
17003d3e6793SMateusz Guzik goto skip;
17013d3e6793SMateusz Guzik break;
17023d3e6793SMateusz Guzik
17033d3e6793SMateusz Guzik case KERN_PROC_RUID:
17043d3e6793SMateusz Guzik if (p->p_ucred->cr_ruid != (uid_t)name[0])
17053d3e6793SMateusz Guzik goto skip;
17063d3e6793SMateusz Guzik break;
17073d3e6793SMateusz Guzik
17083d3e6793SMateusz Guzik case KERN_PROC_PROC:
17093d3e6793SMateusz Guzik break;
17103d3e6793SMateusz Guzik
17113d3e6793SMateusz Guzik default:
17123d3e6793SMateusz Guzik break;
17133d3e6793SMateusz Guzik }
17143d3e6793SMateusz Guzik error = sysctl_out_proc(p, req, flags);
17153d3e6793SMateusz Guzik PROC_LOCK_ASSERT(p, MA_NOTOWNED);
17163d3e6793SMateusz Guzik return (error);
17173d3e6793SMateusz Guzik skip:
17183d3e6793SMateusz Guzik PROC_UNLOCK(p);
17193d3e6793SMateusz Guzik return (0);
17203d3e6793SMateusz Guzik }
17213d3e6793SMateusz Guzik
1722972f9b20SPoul-Henning Kamp static int
sysctl_kern_proc(SYSCTL_HANDLER_ARGS)172382d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1724972f9b20SPoul-Henning Kamp {
17253d3e6793SMateusz Guzik struct kern_proc_out_args iterarg;
1726972f9b20SPoul-Henning Kamp int *name = (int *)arg1;
1727972f9b20SPoul-Henning Kamp u_int namelen = arg2;
1728972f9b20SPoul-Henning Kamp struct proc *p;
17293d3e6793SMateusz Guzik int flags, oid_number;
1730972f9b20SPoul-Henning Kamp int error = 0;
1731972f9b20SPoul-Henning Kamp
17322648efa6SDaniel Eischen oid_number = oidp->oid_number;
17332648efa6SDaniel Eischen if (oid_number != KERN_PROC_ALL &&
17342648efa6SDaniel Eischen (oid_number & KERN_PROC_INC_THREAD) == 0)
17352648efa6SDaniel Eischen flags = KERN_PROC_NOTHREADS;
17362648efa6SDaniel Eischen else {
17372648efa6SDaniel Eischen flags = 0;
17382648efa6SDaniel Eischen oid_number &= ~KERN_PROC_INC_THREAD;
17392648efa6SDaniel Eischen }
1740fe52cf54SMikolaj Golub #ifdef COMPAT_FREEBSD32
1741fe52cf54SMikolaj Golub if (req->flags & SCTL_MASK32)
1742fe52cf54SMikolaj Golub flags |= KERN_PROC_MASK32;
1743fe52cf54SMikolaj Golub #endif
17442648efa6SDaniel Eischen if (oid_number == KERN_PROC_PID) {
17453ce93e4eSPoul-Henning Kamp if (namelen != 1)
1746972f9b20SPoul-Henning Kamp return (EINVAL);
17475032ff81SDon Lewis error = sysctl_wire_old_buffer(req, 0);
17485032ff81SDon Lewis if (error)
17495032ff81SDon Lewis return (error);
17504daea938SKonstantin Belousov sx_slock(&proctree_lock);
1751fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANSEE, &p);
17528b04bbefSMateusz Guzik if (error == 0)
17539b57bf75SMateusz Guzik error = sysctl_out_proc(p, req, flags);
17544daea938SKonstantin Belousov sx_sunlock(&proctree_lock);
17553ce93e4eSPoul-Henning Kamp return (error);
17563ce93e4eSPoul-Henning Kamp }
17573ddaef40STim J. Robbins
17582648efa6SDaniel Eischen switch (oid_number) {
17593ddaef40STim J. Robbins case KERN_PROC_ALL:
17603ddaef40STim J. Robbins if (namelen != 0)
17613ce93e4eSPoul-Henning Kamp return (EINVAL);
17623ddaef40STim J. Robbins break;
176325e247afSPeter Wemm case KERN_PROC_PROC:
176425e247afSPeter Wemm if (namelen != 0 && namelen != 1)
176525e247afSPeter Wemm return (EINVAL);
176625e247afSPeter Wemm break;
17673ddaef40STim J. Robbins default:
17683ddaef40STim J. Robbins if (namelen != 1)
17693ddaef40STim J. Robbins return (EINVAL);
17703ddaef40STim J. Robbins break;
17713ddaef40STim J. Robbins }
17723ce93e4eSPoul-Henning Kamp
17739d4e369aSMateusz Guzik if (req->oldptr == NULL) {
17743ce93e4eSPoul-Henning Kamp /* overestimate by 5 procs */
1775972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1776972f9b20SPoul-Henning Kamp if (error)
1777972f9b20SPoul-Henning Kamp return (error);
17789d4e369aSMateusz Guzik } else {
177947934cefSDon Lewis error = sysctl_wire_old_buffer(req, 0);
178047934cefSDon Lewis if (error != 0)
178147934cefSDon Lewis return (error);
17829d4e369aSMateusz Guzik }
17833d3e6793SMateusz Guzik iterarg.flags = flags;
17843d3e6793SMateusz Guzik iterarg.oid_number = oid_number;
17853d3e6793SMateusz Guzik iterarg.req = req;
17863d3e6793SMateusz Guzik iterarg.name = name;
17873d3e6793SMateusz Guzik error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1788972f9b20SPoul-Henning Kamp return (error);
1789972f9b20SPoul-Henning Kamp }
1790972f9b20SPoul-Henning Kamp
1791c1508b28SAlfred Perlstein struct pargs *
pargs_alloc(int len)1792c1508b28SAlfred Perlstein pargs_alloc(int len)
1793c1508b28SAlfred Perlstein {
1794c1508b28SAlfred Perlstein struct pargs *pa;
1795c1508b28SAlfred Perlstein
17961ede983cSDag-Erling Smørgrav pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1797a163d034SWarner Losh M_WAITOK);
179855b4a5aeSJohn Baldwin refcount_init(&pa->ar_ref, 1);
1799c1508b28SAlfred Perlstein pa->ar_length = len;
1800c1508b28SAlfred Perlstein return (pa);
1801c1508b28SAlfred Perlstein }
1802c1508b28SAlfred Perlstein
180358e8af1bSKonstantin Belousov static void
pargs_free(struct pargs * pa)1804c1508b28SAlfred Perlstein pargs_free(struct pargs *pa)
1805c1508b28SAlfred Perlstein {
1806c1508b28SAlfred Perlstein
18071ede983cSDag-Erling Smørgrav free(pa, M_PARGS);
1808c1508b28SAlfred Perlstein }
1809c1508b28SAlfred Perlstein
1810c1508b28SAlfred Perlstein void
pargs_hold(struct pargs * pa)1811c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa)
1812c1508b28SAlfred Perlstein {
1813c1508b28SAlfred Perlstein
1814c1508b28SAlfred Perlstein if (pa == NULL)
1815c1508b28SAlfred Perlstein return;
181655b4a5aeSJohn Baldwin refcount_acquire(&pa->ar_ref);
1817c1508b28SAlfred Perlstein }
1818c1508b28SAlfred Perlstein
1819c1508b28SAlfred Perlstein void
pargs_drop(struct pargs * pa)1820c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa)
1821c1508b28SAlfred Perlstein {
1822c1508b28SAlfred Perlstein
1823c1508b28SAlfred Perlstein if (pa == NULL)
1824c1508b28SAlfred Perlstein return;
182555b4a5aeSJohn Baldwin if (refcount_release(&pa->ar_ref))
1826c1508b28SAlfred Perlstein pargs_free(pa);
1827c1508b28SAlfred Perlstein }
1828c1508b28SAlfred Perlstein
1829c5cfcb1cSMikolaj Golub static int
proc_read_string(struct thread * td,struct proc * p,const char * sptr,char * buf,size_t len)1830c5cfcb1cSMikolaj Golub proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1831c5cfcb1cSMikolaj Golub size_t len)
1832c5cfcb1cSMikolaj Golub {
1833711fbd17SMark Johnston ssize_t n;
1834c5cfcb1cSMikolaj Golub
1835c5cfcb1cSMikolaj Golub /*
1836711fbd17SMark Johnston * This may return a short read if the string is shorter than the chunk
1837711fbd17SMark Johnston * and is aligned at the end of the page, and the following page is not
1838711fbd17SMark Johnston * mapped.
1839c5cfcb1cSMikolaj Golub */
1840711fbd17SMark Johnston n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1841711fbd17SMark Johnston if (n <= 0)
1842711fbd17SMark Johnston return (ENOMEM);
1843711fbd17SMark Johnston return (0);
1844c5cfcb1cSMikolaj Golub }
1845c5cfcb1cSMikolaj Golub
1846c5cfcb1cSMikolaj Golub #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1847c5cfcb1cSMikolaj Golub
1848c5cfcb1cSMikolaj Golub enum proc_vector_type {
1849c5cfcb1cSMikolaj Golub PROC_ARG,
1850c5cfcb1cSMikolaj Golub PROC_ENV,
1851c5cfcb1cSMikolaj Golub PROC_AUX,
1852c5cfcb1cSMikolaj Golub };
1853c5cfcb1cSMikolaj Golub
1854c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32
1855c5cfcb1cSMikolaj Golub static int
get_proc_vector32(struct thread * td,struct proc * p,char *** proc_vectorp,size_t * vsizep,enum proc_vector_type type)1856c5cfcb1cSMikolaj Golub get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1857c5cfcb1cSMikolaj Golub size_t *vsizep, enum proc_vector_type type)
1858c5cfcb1cSMikolaj Golub {
1859c5cfcb1cSMikolaj Golub struct freebsd32_ps_strings pss;
1860c5cfcb1cSMikolaj Golub Elf32_Auxinfo aux;
1861c5cfcb1cSMikolaj Golub vm_offset_t vptr, ptr;
1862c5cfcb1cSMikolaj Golub uint32_t *proc_vector32;
1863c5cfcb1cSMikolaj Golub char **proc_vector;
1864c5cfcb1cSMikolaj Golub size_t vsize, size;
1865c5cfcb1cSMikolaj Golub int i, error;
1866c5cfcb1cSMikolaj Golub
1867711fbd17SMark Johnston error = 0;
1868706f4a81SMark Johnston if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1869706f4a81SMark Johnston sizeof(pss))
1870711fbd17SMark Johnston return (ENOMEM);
1871c5cfcb1cSMikolaj Golub switch (type) {
1872c5cfcb1cSMikolaj Golub case PROC_ARG:
1873c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1874c5cfcb1cSMikolaj Golub vsize = pss.ps_nargvstr;
1875c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX)
1876c5cfcb1cSMikolaj Golub return (ENOEXEC);
1877c5cfcb1cSMikolaj Golub size = vsize * sizeof(int32_t);
1878c5cfcb1cSMikolaj Golub break;
1879c5cfcb1cSMikolaj Golub case PROC_ENV:
1880c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1881c5cfcb1cSMikolaj Golub vsize = pss.ps_nenvstr;
1882c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX)
1883c5cfcb1cSMikolaj Golub return (ENOEXEC);
1884c5cfcb1cSMikolaj Golub size = vsize * sizeof(int32_t);
1885c5cfcb1cSMikolaj Golub break;
1886c5cfcb1cSMikolaj Golub case PROC_AUX:
1887c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1888c5cfcb1cSMikolaj Golub (pss.ps_nenvstr + 1) * sizeof(int32_t);
1889c5cfcb1cSMikolaj Golub if (vptr % 4 != 0)
1890c5cfcb1cSMikolaj Golub return (ENOEXEC);
1891c5cfcb1cSMikolaj Golub for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1892711fbd17SMark Johnston if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1893711fbd17SMark Johnston sizeof(aux))
1894711fbd17SMark Johnston return (ENOMEM);
1895c5cfcb1cSMikolaj Golub if (aux.a_type == AT_NULL)
1896c5cfcb1cSMikolaj Golub break;
1897c5cfcb1cSMikolaj Golub ptr += sizeof(aux);
1898c5cfcb1cSMikolaj Golub }
1899c5cfcb1cSMikolaj Golub if (aux.a_type != AT_NULL)
1900c5cfcb1cSMikolaj Golub return (ENOEXEC);
1901c5cfcb1cSMikolaj Golub vsize = i + 1;
1902c5cfcb1cSMikolaj Golub size = vsize * sizeof(aux);
1903c5cfcb1cSMikolaj Golub break;
1904c5cfcb1cSMikolaj Golub default:
1905c5cfcb1cSMikolaj Golub KASSERT(0, ("Wrong proc vector type: %d", type));
19067ad9baaeSMikolaj Golub return (EINVAL);
1907c5cfcb1cSMikolaj Golub }
1908c5cfcb1cSMikolaj Golub proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1909711fbd17SMark Johnston if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1910711fbd17SMark Johnston error = ENOMEM;
1911c5cfcb1cSMikolaj Golub goto done;
1912711fbd17SMark Johnston }
1913c5cfcb1cSMikolaj Golub if (type == PROC_AUX) {
1914c5cfcb1cSMikolaj Golub *proc_vectorp = (char **)proc_vector32;
1915c5cfcb1cSMikolaj Golub *vsizep = vsize;
1916c5cfcb1cSMikolaj Golub return (0);
1917c5cfcb1cSMikolaj Golub }
1918c5cfcb1cSMikolaj Golub proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1919c5cfcb1cSMikolaj Golub for (i = 0; i < (int)vsize; i++)
1920c5cfcb1cSMikolaj Golub proc_vector[i] = PTRIN(proc_vector32[i]);
1921c5cfcb1cSMikolaj Golub *proc_vectorp = proc_vector;
1922c5cfcb1cSMikolaj Golub *vsizep = vsize;
1923c5cfcb1cSMikolaj Golub done:
1924c5cfcb1cSMikolaj Golub free(proc_vector32, M_TEMP);
1925c5cfcb1cSMikolaj Golub return (error);
1926c5cfcb1cSMikolaj Golub }
1927c5cfcb1cSMikolaj Golub #endif
1928c5cfcb1cSMikolaj Golub
1929c5cfcb1cSMikolaj Golub static int
get_proc_vector(struct thread * td,struct proc * p,char *** proc_vectorp,size_t * vsizep,enum proc_vector_type type)1930c5cfcb1cSMikolaj Golub get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1931c5cfcb1cSMikolaj Golub size_t *vsizep, enum proc_vector_type type)
1932c5cfcb1cSMikolaj Golub {
1933c5cfcb1cSMikolaj Golub struct ps_strings pss;
1934c5cfcb1cSMikolaj Golub Elf_Auxinfo aux;
1935c5cfcb1cSMikolaj Golub vm_offset_t vptr, ptr;
1936c5cfcb1cSMikolaj Golub char **proc_vector;
1937c5cfcb1cSMikolaj Golub size_t vsize, size;
1938711fbd17SMark Johnston int i;
1939c5cfcb1cSMikolaj Golub
1940c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32
1941c5cfcb1cSMikolaj Golub if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1942c5cfcb1cSMikolaj Golub return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1943c5cfcb1cSMikolaj Golub #endif
1944706f4a81SMark Johnston if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1945706f4a81SMark Johnston sizeof(pss))
1946711fbd17SMark Johnston return (ENOMEM);
1947c5cfcb1cSMikolaj Golub switch (type) {
1948c5cfcb1cSMikolaj Golub case PROC_ARG:
1949c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_argvstr;
1950c5cfcb1cSMikolaj Golub vsize = pss.ps_nargvstr;
1951c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX)
1952c5cfcb1cSMikolaj Golub return (ENOEXEC);
1953c5cfcb1cSMikolaj Golub size = vsize * sizeof(char *);
1954c5cfcb1cSMikolaj Golub break;
1955c5cfcb1cSMikolaj Golub case PROC_ENV:
1956c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_envstr;
1957c5cfcb1cSMikolaj Golub vsize = pss.ps_nenvstr;
1958c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX)
1959c5cfcb1cSMikolaj Golub return (ENOEXEC);
1960c5cfcb1cSMikolaj Golub size = vsize * sizeof(char *);
1961c5cfcb1cSMikolaj Golub break;
1962c5cfcb1cSMikolaj Golub case PROC_AUX:
1963c5cfcb1cSMikolaj Golub /*
1964c5cfcb1cSMikolaj Golub * The aux array is just above env array on the stack. Check
1965c5cfcb1cSMikolaj Golub * that the address is naturally aligned.
1966c5cfcb1cSMikolaj Golub */
1967c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1968c5cfcb1cSMikolaj Golub * sizeof(char *);
1969c5cfcb1cSMikolaj Golub #if __ELF_WORD_SIZE == 64
1970c5cfcb1cSMikolaj Golub if (vptr % sizeof(uint64_t) != 0)
1971c5cfcb1cSMikolaj Golub #else
1972c5cfcb1cSMikolaj Golub if (vptr % sizeof(uint32_t) != 0)
1973c5cfcb1cSMikolaj Golub #endif
1974c5cfcb1cSMikolaj Golub return (ENOEXEC);
1975c5cfcb1cSMikolaj Golub /*
1976c5cfcb1cSMikolaj Golub * We count the array size reading the aux vectors from the
1977c5cfcb1cSMikolaj Golub * stack until AT_NULL vector is returned. So (to keep the code
1978c5cfcb1cSMikolaj Golub * simple) we read the process stack twice: the first time here
1979c5cfcb1cSMikolaj Golub * to find the size and the second time when copying the vectors
1980c5cfcb1cSMikolaj Golub * to the allocated proc_vector.
1981c5cfcb1cSMikolaj Golub */
1982c5cfcb1cSMikolaj Golub for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1983711fbd17SMark Johnston if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1984711fbd17SMark Johnston sizeof(aux))
1985711fbd17SMark Johnston return (ENOMEM);
1986c5cfcb1cSMikolaj Golub if (aux.a_type == AT_NULL)
1987c5cfcb1cSMikolaj Golub break;
1988c5cfcb1cSMikolaj Golub ptr += sizeof(aux);
1989c5cfcb1cSMikolaj Golub }
1990c5cfcb1cSMikolaj Golub /*
1991c5cfcb1cSMikolaj Golub * If the PROC_AUXV_MAX entries are iterated over, and we have
1992c5cfcb1cSMikolaj Golub * not reached AT_NULL, it is most likely we are reading wrong
1993c5cfcb1cSMikolaj Golub * data: either the process doesn't have auxv array or data has
1994c5cfcb1cSMikolaj Golub * been modified. Return the error in this case.
1995c5cfcb1cSMikolaj Golub */
1996c5cfcb1cSMikolaj Golub if (aux.a_type != AT_NULL)
1997c5cfcb1cSMikolaj Golub return (ENOEXEC);
1998c5cfcb1cSMikolaj Golub vsize = i + 1;
1999c5cfcb1cSMikolaj Golub size = vsize * sizeof(aux);
2000c5cfcb1cSMikolaj Golub break;
2001c5cfcb1cSMikolaj Golub default:
2002c5cfcb1cSMikolaj Golub KASSERT(0, ("Wrong proc vector type: %d", type));
20037ad9baaeSMikolaj Golub return (EINVAL); /* In case we are built without INVARIANTS. */
2004c5cfcb1cSMikolaj Golub }
2005c5cfcb1cSMikolaj Golub proc_vector = malloc(size, M_TEMP, M_WAITOK);
2006711fbd17SMark Johnston if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
2007c5cfcb1cSMikolaj Golub free(proc_vector, M_TEMP);
2008711fbd17SMark Johnston return (ENOMEM);
2009c5cfcb1cSMikolaj Golub }
2010c5cfcb1cSMikolaj Golub *proc_vectorp = proc_vector;
2011c5cfcb1cSMikolaj Golub *vsizep = vsize;
2012c5cfcb1cSMikolaj Golub
2013c5cfcb1cSMikolaj Golub return (0);
2014c5cfcb1cSMikolaj Golub }
2015c5cfcb1cSMikolaj Golub
2016c5cfcb1cSMikolaj Golub #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
2017c5cfcb1cSMikolaj Golub
2018c5cfcb1cSMikolaj Golub static int
get_ps_strings(struct thread * td,struct proc * p,struct sbuf * sb,enum proc_vector_type type)2019c5cfcb1cSMikolaj Golub get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
2020fe7f89b7SMikolaj Golub enum proc_vector_type type)
2021c5cfcb1cSMikolaj Golub {
2022fe7f89b7SMikolaj Golub size_t done, len, nchr, vsize;
2023c5cfcb1cSMikolaj Golub int error, i;
2024c5cfcb1cSMikolaj Golub char **proc_vector, *sptr;
2025c5cfcb1cSMikolaj Golub char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2026c5cfcb1cSMikolaj Golub
2027c5cfcb1cSMikolaj Golub PROC_ASSERT_HELD(p);
2028c5cfcb1cSMikolaj Golub
2029c5cfcb1cSMikolaj Golub /*
2030c5cfcb1cSMikolaj Golub * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2031c5cfcb1cSMikolaj Golub */
2032c5cfcb1cSMikolaj Golub nchr = 2 * (PATH_MAX + ARG_MAX);
2033c5cfcb1cSMikolaj Golub
2034c5cfcb1cSMikolaj Golub error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2035c5cfcb1cSMikolaj Golub if (error != 0)
2036c5cfcb1cSMikolaj Golub return (error);
2037c5cfcb1cSMikolaj Golub for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2038c5cfcb1cSMikolaj Golub /*
2039c5cfcb1cSMikolaj Golub * The program may have scribbled into its argv array, e.g. to
2040c5cfcb1cSMikolaj Golub * remove some arguments. If that has happened, break out
2041c5cfcb1cSMikolaj Golub * before trying to read from NULL.
2042c5cfcb1cSMikolaj Golub */
2043c5cfcb1cSMikolaj Golub if (proc_vector[i] == NULL)
2044c5cfcb1cSMikolaj Golub break;
2045c5cfcb1cSMikolaj Golub for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2046c5cfcb1cSMikolaj Golub error = proc_read_string(td, p, sptr, pss_string,
2047c5cfcb1cSMikolaj Golub sizeof(pss_string));
2048c5cfcb1cSMikolaj Golub if (error != 0)
2049c5cfcb1cSMikolaj Golub goto done;
2050c5cfcb1cSMikolaj Golub len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2051c5cfcb1cSMikolaj Golub if (done + len >= nchr)
2052c5cfcb1cSMikolaj Golub len = nchr - done - 1;
2053c5cfcb1cSMikolaj Golub sbuf_bcat(sb, pss_string, len);
2054c5cfcb1cSMikolaj Golub if (len != GET_PS_STRINGS_CHUNK_SZ)
2055c5cfcb1cSMikolaj Golub break;
2056c5cfcb1cSMikolaj Golub done += GET_PS_STRINGS_CHUNK_SZ;
2057c5cfcb1cSMikolaj Golub }
2058c5cfcb1cSMikolaj Golub sbuf_bcat(sb, "", 1);
2059c5cfcb1cSMikolaj Golub done += len + 1;
2060c5cfcb1cSMikolaj Golub }
2061c5cfcb1cSMikolaj Golub done:
2062c5cfcb1cSMikolaj Golub free(proc_vector, M_TEMP);
2063c5cfcb1cSMikolaj Golub return (error);
2064c5cfcb1cSMikolaj Golub }
2065c5cfcb1cSMikolaj Golub
2066c5cfcb1cSMikolaj Golub int
proc_getargv(struct thread * td,struct proc * p,struct sbuf * sb)2067fe7f89b7SMikolaj Golub proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2068c5cfcb1cSMikolaj Golub {
2069c5cfcb1cSMikolaj Golub
2070fe7f89b7SMikolaj Golub return (get_ps_strings(curthread, p, sb, PROC_ARG));
2071c5cfcb1cSMikolaj Golub }
2072c5cfcb1cSMikolaj Golub
2073c5cfcb1cSMikolaj Golub int
proc_getenvv(struct thread * td,struct proc * p,struct sbuf * sb)2074fe7f89b7SMikolaj Golub proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2075c5cfcb1cSMikolaj Golub {
2076c5cfcb1cSMikolaj Golub
2077fe7f89b7SMikolaj Golub return (get_ps_strings(curthread, p, sb, PROC_ENV));
2078c5cfcb1cSMikolaj Golub }
2079c5cfcb1cSMikolaj Golub
20805ea21e69SMikolaj Golub int
proc_getauxv(struct thread * td,struct proc * p,struct sbuf * sb)20815ea21e69SMikolaj Golub proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
20825ea21e69SMikolaj Golub {
20835ea21e69SMikolaj Golub size_t vsize, size;
20845ea21e69SMikolaj Golub char **auxv;
20855ea21e69SMikolaj Golub int error;
20865ea21e69SMikolaj Golub
20875ea21e69SMikolaj Golub error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
20885ea21e69SMikolaj Golub if (error == 0) {
20895ea21e69SMikolaj Golub #ifdef COMPAT_FREEBSD32
20905ea21e69SMikolaj Golub if (SV_PROC_FLAG(p, SV_ILP32) != 0)
20915ea21e69SMikolaj Golub size = vsize * sizeof(Elf32_Auxinfo);
20925ea21e69SMikolaj Golub else
20935ea21e69SMikolaj Golub #endif
20945ea21e69SMikolaj Golub size = vsize * sizeof(Elf_Auxinfo);
209557c2505eSKonstantin Belousov if (sbuf_bcat(sb, auxv, size) != 0)
209657c2505eSKonstantin Belousov error = ENOMEM;
20975ea21e69SMikolaj Golub free(auxv, M_TEMP);
20985ea21e69SMikolaj Golub }
20995ea21e69SMikolaj Golub return (error);
21005ea21e69SMikolaj Golub }
21015ea21e69SMikolaj Golub
2102b9df5231SPoul-Henning Kamp /*
2103b9df5231SPoul-Henning Kamp * This sysctl allows a process to retrieve the argument list or process
2104b9df5231SPoul-Henning Kamp * title for another process without groping around in the address space
2105b9df5231SPoul-Henning Kamp * of the other process. It also allow a process to set its own "process
2106b9df5231SPoul-Henning Kamp * title to a string of its own choice.
2107b9df5231SPoul-Henning Kamp */
2108b9df5231SPoul-Henning Kamp static int
sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)210982d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2110b9df5231SPoul-Henning Kamp {
2111b9df5231SPoul-Henning Kamp int *name = (int *)arg1;
2112b9df5231SPoul-Henning Kamp u_int namelen = arg2;
21138510f2a8SJohn Baldwin struct pargs *newpa, *pa;
2114b9df5231SPoul-Henning Kamp struct proc *p;
2115c5cfcb1cSMikolaj Golub struct sbuf sb;
2116fa3935bcSMikolaj Golub int flags, error = 0, error2;
2117baaa6ec7SMateusz Guzik pid_t pid;
2118b9df5231SPoul-Henning Kamp
2119b9df5231SPoul-Henning Kamp if (namelen != 1)
2120b9df5231SPoul-Henning Kamp return (EINVAL);
2121b9df5231SPoul-Henning Kamp
2122fe258f23SMateusz Guzik p = curproc;
2123baaa6ec7SMateusz Guzik pid = (pid_t)name[0];
2124fe258f23SMateusz Guzik if (pid == -1) {
2125fe258f23SMateusz Guzik pid = p->p_pid;
2126fe258f23SMateusz Guzik }
2127fe258f23SMateusz Guzik
2128baaa6ec7SMateusz Guzik /*
2129baaa6ec7SMateusz Guzik * If the query is for this process and it is single-threaded, there
2130baaa6ec7SMateusz Guzik * is nobody to modify pargs, thus we can just read.
2131baaa6ec7SMateusz Guzik */
21321b25176cSAntoine Brodin if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
21331b25176cSAntoine Brodin (pa = p->p_args) != NULL)
21341b25176cSAntoine Brodin return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2135baaa6ec7SMateusz Guzik
2136fa3935bcSMikolaj Golub flags = PGET_CANSEE;
2137fa3935bcSMikolaj Golub if (req->newptr != NULL)
2138fa3935bcSMikolaj Golub flags |= PGET_ISCURRENT;
2139baaa6ec7SMateusz Guzik error = pget(pid, flags, &p);
2140fa3935bcSMikolaj Golub if (error)
2141e76bad96SRobert Drehmel return (error);
2142b9df5231SPoul-Henning Kamp
21437b11fea6SAlfred Perlstein pa = p->p_args;
2144c5cfcb1cSMikolaj Golub if (pa != NULL) {
21457b11fea6SAlfred Perlstein pargs_hold(pa);
21467b11fea6SAlfred Perlstein PROC_UNLOCK(p);
21477b11fea6SAlfred Perlstein error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
21487b11fea6SAlfred Perlstein pargs_drop(pa);
2149c5cfcb1cSMikolaj Golub } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2150c5cfcb1cSMikolaj Golub _PHOLD(p);
2151c5cfcb1cSMikolaj Golub PROC_UNLOCK(p);
2152c5cfcb1cSMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2153296f235dSIan Lepore sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2154fe7f89b7SMikolaj Golub error = proc_getargv(curthread, p, &sb);
2155c5cfcb1cSMikolaj Golub error2 = sbuf_finish(&sb);
2156c5cfcb1cSMikolaj Golub PRELE(p);
2157c5cfcb1cSMikolaj Golub sbuf_delete(&sb);
2158c5cfcb1cSMikolaj Golub if (error == 0 && error2 != 0)
2159c5cfcb1cSMikolaj Golub error = error2;
2160c5cfcb1cSMikolaj Golub } else {
2161c5cfcb1cSMikolaj Golub PROC_UNLOCK(p);
2162c5cfcb1cSMikolaj Golub }
21638510f2a8SJohn Baldwin if (error != 0 || req->newptr == NULL)
2164b9df5231SPoul-Henning Kamp return (error);
2165b9df5231SPoul-Henning Kamp
2166712dda7fSXin LI if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
21678510f2a8SJohn Baldwin return (ENOMEM);
2168d6eff083SKonstantin Belousov
2169d6eff083SKonstantin Belousov if (req->newlen == 0) {
2170d6eff083SKonstantin Belousov /*
2171d6eff083SKonstantin Belousov * Clear the argument pointer, so that we'll fetch arguments
2172d6eff083SKonstantin Belousov * with proc_getargv() until further notice.
2173d6eff083SKonstantin Belousov */
2174d6eff083SKonstantin Belousov newpa = NULL;
2175d6eff083SKonstantin Belousov } else {
21764bc6471bSJohn Baldwin newpa = pargs_alloc(req->newlen);
21774bc6471bSJohn Baldwin error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
21788510f2a8SJohn Baldwin if (error != 0) {
21794bc6471bSJohn Baldwin pargs_free(newpa);
2180b9df5231SPoul-Henning Kamp return (error);
2181b9df5231SPoul-Henning Kamp }
2182d6eff083SKonstantin Belousov }
21834bc6471bSJohn Baldwin PROC_LOCK(p);
21844bc6471bSJohn Baldwin pa = p->p_args;
21854bc6471bSJohn Baldwin p->p_args = newpa;
21864bc6471bSJohn Baldwin PROC_UNLOCK(p);
21874bc6471bSJohn Baldwin pargs_drop(pa);
21884bc6471bSJohn Baldwin return (0);
21894bc6471bSJohn Baldwin }
21904bc6471bSJohn Baldwin
2191fe769cddSDavid Schultz /*
2192c5cfcb1cSMikolaj Golub * This sysctl allows a process to retrieve environment of another process.
2193c5cfcb1cSMikolaj Golub */
2194c5cfcb1cSMikolaj Golub static int
sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)2195c5cfcb1cSMikolaj Golub sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2196c5cfcb1cSMikolaj Golub {
2197c5cfcb1cSMikolaj Golub int *name = (int *)arg1;
2198c5cfcb1cSMikolaj Golub u_int namelen = arg2;
2199c5cfcb1cSMikolaj Golub struct proc *p;
2200c5cfcb1cSMikolaj Golub struct sbuf sb;
2201c5cfcb1cSMikolaj Golub int error, error2;
2202c5cfcb1cSMikolaj Golub
2203c5cfcb1cSMikolaj Golub if (namelen != 1)
2204c5cfcb1cSMikolaj Golub return (EINVAL);
2205c5cfcb1cSMikolaj Golub
2206fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2207fa3935bcSMikolaj Golub if (error != 0)
2208c5cfcb1cSMikolaj Golub return (error);
2209c5cfcb1cSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) {
2210fa3935bcSMikolaj Golub PRELE(p);
2211c5cfcb1cSMikolaj Golub return (0);
2212c5cfcb1cSMikolaj Golub }
2213fa3935bcSMikolaj Golub
2214c5cfcb1cSMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2215296f235dSIan Lepore sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2216fe7f89b7SMikolaj Golub error = proc_getenvv(curthread, p, &sb);
2217c5cfcb1cSMikolaj Golub error2 = sbuf_finish(&sb);
2218c5cfcb1cSMikolaj Golub PRELE(p);
2219c5cfcb1cSMikolaj Golub sbuf_delete(&sb);
2220c5cfcb1cSMikolaj Golub return (error != 0 ? error : error2);
2221c5cfcb1cSMikolaj Golub }
2222c5cfcb1cSMikolaj Golub
2223c5cfcb1cSMikolaj Golub /*
2224c5cfcb1cSMikolaj Golub * This sysctl allows a process to retrieve ELF auxiliary vector of
2225c5cfcb1cSMikolaj Golub * another process.
2226c5cfcb1cSMikolaj Golub */
2227c5cfcb1cSMikolaj Golub static int
sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)2228c5cfcb1cSMikolaj Golub sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2229c5cfcb1cSMikolaj Golub {
2230c5cfcb1cSMikolaj Golub int *name = (int *)arg1;
2231c5cfcb1cSMikolaj Golub u_int namelen = arg2;
2232c5cfcb1cSMikolaj Golub struct proc *p;
22335ea21e69SMikolaj Golub struct sbuf sb;
22345ea21e69SMikolaj Golub int error, error2;
2235c5cfcb1cSMikolaj Golub
2236c5cfcb1cSMikolaj Golub if (namelen != 1)
2237c5cfcb1cSMikolaj Golub return (EINVAL);
2238c5cfcb1cSMikolaj Golub
2239fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2240fa3935bcSMikolaj Golub if (error != 0)
2241c5cfcb1cSMikolaj Golub return (error);
2242c5cfcb1cSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) {
2243fa3935bcSMikolaj Golub PRELE(p);
2244c5cfcb1cSMikolaj Golub return (0);
2245c5cfcb1cSMikolaj Golub }
22465ea21e69SMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
22471eafc078SIan Lepore sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
22485ea21e69SMikolaj Golub error = proc_getauxv(curthread, p, &sb);
22495ea21e69SMikolaj Golub error2 = sbuf_finish(&sb);
22504fd6053bSMikolaj Golub PRELE(p);
22515ea21e69SMikolaj Golub sbuf_delete(&sb);
22525ea21e69SMikolaj Golub return (error != 0 ? error : error2);
2253c5cfcb1cSMikolaj Golub }
2254c5cfcb1cSMikolaj Golub
2255c5cfcb1cSMikolaj Golub /*
2256f34fc6baSKonstantin Belousov * Look up the canonical executable path running in the specified process.
2257f34fc6baSKonstantin Belousov * It tries to return the same hardlink name as was used for execve(2).
2258f34fc6baSKonstantin Belousov * This allows the programs that modify their behavior based on their progname,
2259f34fc6baSKonstantin Belousov * to operate correctly.
2260f34fc6baSKonstantin Belousov *
2261f34fc6baSKonstantin Belousov * Result is returned in retbuf, it must not be freed, similar to vn_fullpath()
2262f34fc6baSKonstantin Belousov * calling conventions.
2263f34fc6baSKonstantin Belousov * binname is a pointer to temporary string buffer of length MAXPATHLEN,
2264f34fc6baSKonstantin Belousov * allocated and freed by caller.
2265f34fc6baSKonstantin Belousov * freebuf should be freed by caller, from the M_TEMP malloc type.
2266fe769cddSDavid Schultz */
2267f34fc6baSKonstantin Belousov int
proc_get_binpath(struct proc * p,char * binname,char ** retbuf,char ** freebuf)2268f34fc6baSKonstantin Belousov proc_get_binpath(struct proc *p, char *binname, char **retbuf,
2269f34fc6baSKonstantin Belousov char **freebuf)
2270fe769cddSDavid Schultz {
2271ee92c8a8SKonstantin Belousov struct nameidata nd;
2272f34fc6baSKonstantin Belousov struct vnode *vp, *dvp;
2273ee92c8a8SKonstantin Belousov size_t freepath_size;
22745050aa86SKonstantin Belousov int error;
2275ee92c8a8SKonstantin Belousov bool do_fullpath;
2276fe769cddSDavid Schultz
2277f34fc6baSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED);
2278fe769cddSDavid Schultz
2279fe769cddSDavid Schultz vp = p->p_textvp;
2280965b55e2SRobert Watson if (vp == NULL) {
2281965b55e2SRobert Watson PROC_UNLOCK(p);
228202de91d7SKonstantin Belousov *retbuf = "";
2283f34fc6baSKonstantin Belousov *freebuf = NULL;
2284965b55e2SRobert Watson return (0);
2285965b55e2SRobert Watson }
2286fe769cddSDavid Schultz vref(vp);
2287ee92c8a8SKonstantin Belousov dvp = p->p_textdvp;
2288ee92c8a8SKonstantin Belousov if (dvp != NULL)
2289ee92c8a8SKonstantin Belousov vref(dvp);
2290ee92c8a8SKonstantin Belousov if (p->p_binname != NULL)
2291ee92c8a8SKonstantin Belousov strlcpy(binname, p->p_binname, MAXPATHLEN);
2292fe769cddSDavid Schultz PROC_UNLOCK(p);
2293f34fc6baSKonstantin Belousov
2294ee92c8a8SKonstantin Belousov do_fullpath = true;
2295f34fc6baSKonstantin Belousov *freebuf = NULL;
2296ee92c8a8SKonstantin Belousov if (dvp != NULL && binname[0] != '\0') {
2297ee92c8a8SKonstantin Belousov freepath_size = MAXPATHLEN;
2298ee92c8a8SKonstantin Belousov if (vn_fullpath_hardlink(vp, dvp, binname, strlen(binname),
2299f34fc6baSKonstantin Belousov retbuf, freebuf, &freepath_size) == 0) {
2300ee92c8a8SKonstantin Belousov /*
2301ee92c8a8SKonstantin Belousov * Recheck the looked up path. The binary
2302ee92c8a8SKonstantin Belousov * might have been renamed or replaced, in
2303ee92c8a8SKonstantin Belousov * which case we should not report old name.
2304ee92c8a8SKonstantin Belousov */
23057e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, *retbuf);
2306ee92c8a8SKonstantin Belousov error = namei(&nd);
2307ee92c8a8SKonstantin Belousov if (error == 0) {
2308ee92c8a8SKonstantin Belousov if (nd.ni_vp == vp)
2309ee92c8a8SKonstantin Belousov do_fullpath = false;
2310ee92c8a8SKonstantin Belousov vrele(nd.ni_vp);
2311bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd);
2312ee92c8a8SKonstantin Belousov }
2313ee92c8a8SKonstantin Belousov }
2314ee92c8a8SKonstantin Belousov }
2315ee92c8a8SKonstantin Belousov if (do_fullpath) {
2316f34fc6baSKonstantin Belousov free(*freebuf, M_TEMP);
2317f34fc6baSKonstantin Belousov *freebuf = NULL;
2318f34fc6baSKonstantin Belousov error = vn_fullpath(vp, retbuf, freebuf);
2319ee92c8a8SKonstantin Belousov }
2320fe769cddSDavid Schultz vrele(vp);
2321ee92c8a8SKonstantin Belousov if (dvp != NULL)
2322ee92c8a8SKonstantin Belousov vrele(dvp);
2323f34fc6baSKonstantin Belousov return (error);
2324f34fc6baSKonstantin Belousov }
2325f34fc6baSKonstantin Belousov
2326f34fc6baSKonstantin Belousov /*
2327f34fc6baSKonstantin Belousov * This sysctl allows a process to retrieve the path of the executable for
2328f34fc6baSKonstantin Belousov * itself or another process.
2329f34fc6baSKonstantin Belousov */
2330f34fc6baSKonstantin Belousov static int
sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)2331f34fc6baSKonstantin Belousov sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2332f34fc6baSKonstantin Belousov {
2333f34fc6baSKonstantin Belousov pid_t *pidp = (pid_t *)arg1;
2334f34fc6baSKonstantin Belousov unsigned int arglen = arg2;
2335f34fc6baSKonstantin Belousov struct proc *p;
2336f34fc6baSKonstantin Belousov char *retbuf, *freebuf, *binname;
2337f34fc6baSKonstantin Belousov int error;
2338f34fc6baSKonstantin Belousov
2339f34fc6baSKonstantin Belousov if (arglen != 1)
2340f34fc6baSKonstantin Belousov return (EINVAL);
2341f34fc6baSKonstantin Belousov binname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2342f34fc6baSKonstantin Belousov binname[0] = '\0';
2343f34fc6baSKonstantin Belousov if (*pidp == -1) { /* -1 means this process */
2344f34fc6baSKonstantin Belousov error = 0;
2345f34fc6baSKonstantin Belousov p = req->td->td_proc;
2346f34fc6baSKonstantin Belousov PROC_LOCK(p);
2347f34fc6baSKonstantin Belousov } else {
2348f34fc6baSKonstantin Belousov error = pget(*pidp, PGET_CANSEE, &p);
2349f34fc6baSKonstantin Belousov }
2350f34fc6baSKonstantin Belousov
2351f34fc6baSKonstantin Belousov if (error == 0)
2352f34fc6baSKonstantin Belousov error = proc_get_binpath(p, binname, &retbuf, &freebuf);
2353ee92c8a8SKonstantin Belousov free(binname, M_TEMP);
2354ee92c8a8SKonstantin Belousov if (error != 0)
2355fe769cddSDavid Schultz return (error);
2356fe769cddSDavid Schultz error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2357fe769cddSDavid Schultz free(freebuf, M_TEMP);
2358fe769cddSDavid Schultz return (error);
2359fe769cddSDavid Schultz }
2360fe769cddSDavid Schultz
2361baf731e6SRobert Drehmel static int
sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)2362baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2363baf731e6SRobert Drehmel {
2364baf731e6SRobert Drehmel struct proc *p;
2365baf731e6SRobert Drehmel char *sv_name;
2366baf731e6SRobert Drehmel int *name;
2367baf731e6SRobert Drehmel int namelen;
2368e76bad96SRobert Drehmel int error;
2369baf731e6SRobert Drehmel
2370baf731e6SRobert Drehmel namelen = arg2;
2371baf731e6SRobert Drehmel if (namelen != 1)
2372baf731e6SRobert Drehmel return (EINVAL);
2373baf731e6SRobert Drehmel
2374baf731e6SRobert Drehmel name = (int *)arg1;
2375fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANSEE, &p);
2376fa3935bcSMikolaj Golub if (error != 0)
2377e76bad96SRobert Drehmel return (error);
2378baf731e6SRobert Drehmel sv_name = p->p_sysent->sv_name;
2379baf731e6SRobert Drehmel PROC_UNLOCK(p);
2380baf731e6SRobert Drehmel return (sysctl_handle_string(oidp, sv_name, 0, req));
2381baf731e6SRobert Drehmel }
2382baf731e6SRobert Drehmel
238343151ee6SPeter Wemm #ifdef KINFO_OVMENTRY_SIZE
238443151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
238543151ee6SPeter Wemm #endif
238643151ee6SPeter Wemm
238743151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7
2388cc43c38cSRobert Watson static int
sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)238943151ee6SPeter Wemm sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2390cc43c38cSRobert Watson {
2391cc43c38cSRobert Watson vm_map_entry_t entry, tmp_entry;
23920dcef81dSMark Johnston unsigned int last_timestamp, namelen;
2393cc43c38cSRobert Watson char *fullpath, *freepath;
239443151ee6SPeter Wemm struct kinfo_ovmentry *kve;
23957a9c4d24SPeter Wemm struct vattr va;
23967a9c4d24SPeter Wemm struct ucred *cred;
2397cc43c38cSRobert Watson int error, *name;
2398cc43c38cSRobert Watson struct vnode *vp;
2399cc43c38cSRobert Watson struct proc *p;
2400cc43c38cSRobert Watson vm_map_t map;
2401c7462f43SKonstantin Belousov struct vmspace *vm;
2402baf731e6SRobert Drehmel
24030dcef81dSMark Johnston namelen = arg2;
24040dcef81dSMark Johnston if (namelen != 1)
24050dcef81dSMark Johnston return (EINVAL);
24060dcef81dSMark Johnston
2407cc43c38cSRobert Watson name = (int *)arg1;
2408fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2409fa3935bcSMikolaj Golub if (error != 0)
2410cc43c38cSRobert Watson return (error);
2411c7462f43SKonstantin Belousov vm = vmspace_acquire_ref(p);
2412c7462f43SKonstantin Belousov if (vm == NULL) {
2413c7462f43SKonstantin Belousov PRELE(p);
2414c7462f43SKonstantin Belousov return (ESRCH);
2415c7462f43SKonstantin Belousov }
2416cc43c38cSRobert Watson kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2417cc43c38cSRobert Watson
2418ca4aa8c3SSergey Kandaurov map = &vm->vm_map;
2419cc43c38cSRobert Watson vm_map_lock_read(map);
24202288078cSDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) {
2421cc43c38cSRobert Watson vm_object_t obj, tobj, lobj;
2422cc43c38cSRobert Watson vm_offset_t addr;
2423cc43c38cSRobert Watson
2424cc43c38cSRobert Watson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2425cc43c38cSRobert Watson continue;
2426cc43c38cSRobert Watson
2427cc43c38cSRobert Watson bzero(kve, sizeof(*kve));
2428cc43c38cSRobert Watson kve->kve_structsize = sizeof(*kve);
2429cc43c38cSRobert Watson
2430cc43c38cSRobert Watson kve->kve_private_resident = 0;
2431cc43c38cSRobert Watson obj = entry->object.vm_object;
2432cc43c38cSRobert Watson if (obj != NULL) {
2433bc403f03SAttilio Rao VM_OBJECT_RLOCK(obj);
2434cc43c38cSRobert Watson if (obj->shadow_count == 1)
2435cc43c38cSRobert Watson kve->kve_private_resident =
2436cc43c38cSRobert Watson obj->resident_page_count;
2437cc43c38cSRobert Watson }
2438cc43c38cSRobert Watson kve->kve_resident = 0;
2439cc43c38cSRobert Watson addr = entry->start;
2440cc43c38cSRobert Watson while (addr < entry->end) {
2441cc43c38cSRobert Watson if (pmap_extract(map->pmap, addr))
2442cc43c38cSRobert Watson kve->kve_resident++;
2443cc43c38cSRobert Watson addr += PAGE_SIZE;
2444cc43c38cSRobert Watson }
2445cc43c38cSRobert Watson
2446cc43c38cSRobert Watson for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
244731608624SJohn Baldwin if (tobj != obj) {
2448bc403f03SAttilio Rao VM_OBJECT_RLOCK(tobj);
244931608624SJohn Baldwin kve->kve_offset += tobj->backing_object_offset;
245031608624SJohn Baldwin }
2451cc43c38cSRobert Watson if (lobj != obj)
2452bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(lobj);
2453cc43c38cSRobert Watson lobj = tobj;
2454cc43c38cSRobert Watson }
2455cc43c38cSRobert Watson
2456118d0afaSKonstantin Belousov kve->kve_start = (void*)entry->start;
2457118d0afaSKonstantin Belousov kve->kve_end = (void*)entry->end;
245831608624SJohn Baldwin kve->kve_offset += (off_t)entry->offset;
2459118d0afaSKonstantin Belousov
2460118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_READ)
2461118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_READ;
2462118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_WRITE)
2463118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_WRITE;
2464118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_EXECUTE)
2465118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_EXEC;
2466118d0afaSKonstantin Belousov
2467118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_COW)
2468118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_COW;
2469118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2470118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2471937912eaSAttilio Rao if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2472937912eaSAttilio Rao kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2473118d0afaSKonstantin Belousov
2474118d0afaSKonstantin Belousov last_timestamp = map->timestamp;
2475118d0afaSKonstantin Belousov vm_map_unlock_read(map);
2476118d0afaSKonstantin Belousov
24777a9c4d24SPeter Wemm kve->kve_fileid = 0;
24787a9c4d24SPeter Wemm kve->kve_fsid = 0;
2479cc43c38cSRobert Watson freepath = NULL;
2480cc43c38cSRobert Watson fullpath = "";
2481cc43c38cSRobert Watson if (lobj) {
24825e38e3f5SEric van Gyzen kve->kve_type = vm_object_kvme_type(lobj, &vp);
24835e38e3f5SEric van Gyzen if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2484cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_UNKNOWN;
24855e38e3f5SEric van Gyzen if (vp != NULL)
24865e38e3f5SEric van Gyzen vref(vp);
2487cc43c38cSRobert Watson if (lobj != obj)
2488bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(lobj);
2489cc43c38cSRobert Watson
2490cc43c38cSRobert Watson kve->kve_ref_count = obj->ref_count;
2491cc43c38cSRobert Watson kve->kve_shadow_count = obj->shadow_count;
2492bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(obj);
2493cc43c38cSRobert Watson if (vp != NULL) {
2494feabaaf9SMateusz Guzik vn_fullpath(vp, &fullpath, &freepath);
24957a9c4d24SPeter Wemm cred = curthread->td_ucred;
24962ff47c5fSJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY);
24977a9c4d24SPeter Wemm if (VOP_GETATTR(vp, &va, cred) == 0) {
24987a9c4d24SPeter Wemm kve->kve_fileid = va.va_fileid;
249969921123SKonstantin Belousov /* truncate */
25007a9c4d24SPeter Wemm kve->kve_fsid = va.va_fsid;
25017a9c4d24SPeter Wemm }
2502cc43c38cSRobert Watson vput(vp);
2503cc43c38cSRobert Watson }
2504cc43c38cSRobert Watson } else {
2505cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_NONE;
2506cc43c38cSRobert Watson kve->kve_ref_count = 0;
2507cc43c38cSRobert Watson kve->kve_shadow_count = 0;
2508cc43c38cSRobert Watson }
2509cc43c38cSRobert Watson
2510cc43c38cSRobert Watson strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2511cc43c38cSRobert Watson if (freepath != NULL)
2512cc43c38cSRobert Watson free(freepath, M_TEMP);
2513cc43c38cSRobert Watson
2514cc43c38cSRobert Watson error = SYSCTL_OUT(req, kve, sizeof(*kve));
2515cc43c38cSRobert Watson vm_map_lock_read(map);
2516cc43c38cSRobert Watson if (error)
2517cc43c38cSRobert Watson break;
251822a448c4SKonstantin Belousov if (last_timestamp != map->timestamp) {
2519cc43c38cSRobert Watson vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2520cc43c38cSRobert Watson entry = tmp_entry;
2521cc43c38cSRobert Watson }
2522cc43c38cSRobert Watson }
2523cc43c38cSRobert Watson vm_map_unlock_read(map);
2524c7462f43SKonstantin Belousov vmspace_free(vm);
2525cc43c38cSRobert Watson PRELE(p);
2526cc43c38cSRobert Watson free(kve, M_TEMP);
2527cc43c38cSRobert Watson return (error);
2528cc43c38cSRobert Watson }
252943151ee6SPeter Wemm #endif /* COMPAT_FREEBSD7 */
253043151ee6SPeter Wemm
253143151ee6SPeter Wemm #ifdef KINFO_VMENTRY_SIZE
253243151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
253343151ee6SPeter Wemm #endif
253443151ee6SPeter Wemm
2535baaa7969SKonstantin Belousov void
kern_proc_vmmap_resident(vm_map_t map,vm_map_entry_t entry,int * resident_count,bool * super)2536a62eb139SKonstantin Belousov kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2537baaa7969SKonstantin Belousov int *resident_count, bool *super)
2538a62eb139SKonstantin Belousov {
2539a62eb139SKonstantin Belousov vm_object_t obj, tobj;
2540a62eb139SKonstantin Belousov vm_page_t m, m_adv;
2541a62eb139SKonstantin Belousov vm_offset_t addr;
254201cef4caSMark Johnston vm_paddr_t pa;
2543a62eb139SKonstantin Belousov vm_pindex_t pi, pi_adv, pindex;
25443e00c11aSAlan Cox int incore;
2545a62eb139SKonstantin Belousov
2546baaa7969SKonstantin Belousov *super = false;
2547baaa7969SKonstantin Belousov *resident_count = 0;
2548baaa7969SKonstantin Belousov if (vmmap_skip_res_cnt)
2549baaa7969SKonstantin Belousov return;
2550baaa7969SKonstantin Belousov
255101cef4caSMark Johnston pa = 0;
2552a62eb139SKonstantin Belousov obj = entry->object.vm_object;
2553a62eb139SKonstantin Belousov addr = entry->start;
2554a62eb139SKonstantin Belousov m_adv = NULL;
2555d3a3b8b0SKonstantin Belousov pi = OFF_TO_IDX(entry->offset);
2556a62eb139SKonstantin Belousov for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2557a62eb139SKonstantin Belousov if (m_adv != NULL) {
2558a62eb139SKonstantin Belousov m = m_adv;
2559a62eb139SKonstantin Belousov } else {
2560d1780e8dSKonstantin Belousov pi_adv = atop(entry->end - addr);
2561a62eb139SKonstantin Belousov pindex = pi;
2562a62eb139SKonstantin Belousov for (tobj = obj;; tobj = tobj->backing_object) {
2563a62eb139SKonstantin Belousov m = vm_page_find_least(tobj, pindex);
2564a62eb139SKonstantin Belousov if (m != NULL) {
2565a62eb139SKonstantin Belousov if (m->pindex == pindex)
2566a62eb139SKonstantin Belousov break;
2567a62eb139SKonstantin Belousov if (pi_adv > m->pindex - pindex) {
2568a62eb139SKonstantin Belousov pi_adv = m->pindex - pindex;
2569a62eb139SKonstantin Belousov m_adv = m;
2570a62eb139SKonstantin Belousov }
2571a62eb139SKonstantin Belousov }
2572a62eb139SKonstantin Belousov if (tobj->backing_object == NULL)
2573a62eb139SKonstantin Belousov goto next;
2574a62eb139SKonstantin Belousov pindex += OFF_TO_IDX(tobj->
2575a62eb139SKonstantin Belousov backing_object_offset);
2576a62eb139SKonstantin Belousov }
2577a62eb139SKonstantin Belousov }
2578a62eb139SKonstantin Belousov m_adv = NULL;
2579a62eb139SKonstantin Belousov if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
25803e00c11aSAlan Cox (addr & (pagesizes[1] - 1)) == 0 && (incore =
25813e00c11aSAlan Cox pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2582baaa7969SKonstantin Belousov *super = true;
25833e00c11aSAlan Cox /*
25843e00c11aSAlan Cox * The virtual page might be smaller than the physical
25853e00c11aSAlan Cox * page, so we use the page size reported by the pmap
25863e00c11aSAlan Cox * rather than m->psind.
25873e00c11aSAlan Cox */
25883e00c11aSAlan Cox pi_adv = atop(pagesizes[incore >> MINCORE_PSIND_SHIFT]);
2589a62eb139SKonstantin Belousov } else {
2590a62eb139SKonstantin Belousov /*
2591a62eb139SKonstantin Belousov * We do not test the found page on validity.
2592a62eb139SKonstantin Belousov * Either the page is busy and being paged in,
2593a62eb139SKonstantin Belousov * or it was invalidated. The first case
2594a62eb139SKonstantin Belousov * should be counted as resident, the second
2595a62eb139SKonstantin Belousov * is not so clear; we do account both.
2596a62eb139SKonstantin Belousov */
2597a62eb139SKonstantin Belousov pi_adv = 1;
2598a62eb139SKonstantin Belousov }
2599baaa7969SKonstantin Belousov *resident_count += pi_adv;
2600a62eb139SKonstantin Belousov next:;
2601a62eb139SKonstantin Belousov }
2602a62eb139SKonstantin Belousov }
2603a62eb139SKonstantin Belousov
2604fe52cf54SMikolaj Golub /*
2605fe52cf54SMikolaj Golub * Must be called with the process locked and will return unlocked.
2606fe52cf54SMikolaj Golub */
2607fe52cf54SMikolaj Golub int
kern_proc_vmmap_out(struct proc * p,struct sbuf * sb,ssize_t maxlen,int flags)2608e6b95927SConrad Meyer kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
260943151ee6SPeter Wemm {
261043151ee6SPeter Wemm vm_map_entry_t entry, tmp_entry;
2611a91831a2SKonstantin Belousov struct vattr va;
2612a91831a2SKonstantin Belousov vm_map_t map;
2613d7671ad8SRyan Libby vm_object_t lobj, nobj, obj, tobj;
261443151ee6SPeter Wemm char *fullpath, *freepath;
261543151ee6SPeter Wemm struct kinfo_vmentry *kve;
261643151ee6SPeter Wemm struct ucred *cred;
261743151ee6SPeter Wemm struct vnode *vp;
2618c7462f43SKonstantin Belousov struct vmspace *vm;
2619a91831a2SKonstantin Belousov vm_offset_t addr;
2620a91831a2SKonstantin Belousov unsigned int last_timestamp;
2621*1123986dSJohn Baldwin int error;
2622d3dd6bd4SKonstantin Belousov key_t key;
2623d3dd6bd4SKonstantin Belousov unsigned short seq;
262425c6318cSKonstantin Belousov bool guard, super;
262543151ee6SPeter Wemm
2626fe52cf54SMikolaj Golub PROC_LOCK_ASSERT(p, MA_OWNED);
2627fe52cf54SMikolaj Golub
2628fe52cf54SMikolaj Golub _PHOLD(p);
2629fe52cf54SMikolaj Golub PROC_UNLOCK(p);
2630c7462f43SKonstantin Belousov vm = vmspace_acquire_ref(p);
2631c7462f43SKonstantin Belousov if (vm == NULL) {
2632c7462f43SKonstantin Belousov PRELE(p);
2633c7462f43SKonstantin Belousov return (ESRCH);
2634c7462f43SKonstantin Belousov }
2635e6b95927SConrad Meyer kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
263643151ee6SPeter Wemm
2637fe52cf54SMikolaj Golub error = 0;
2638ca4aa8c3SSergey Kandaurov map = &vm->vm_map;
263943151ee6SPeter Wemm vm_map_lock_read(map);
26402288078cSDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) {
264143151ee6SPeter Wemm if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
264243151ee6SPeter Wemm continue;
264343151ee6SPeter Wemm
2644479fcb4eSKonstantin Belousov addr = entry->end;
264543151ee6SPeter Wemm bzero(kve, sizeof(*kve));
264643151ee6SPeter Wemm obj = entry->object.vm_object;
264743151ee6SPeter Wemm if (obj != NULL) {
2648ecfbddf0SKonstantin Belousov if ((obj->flags & OBJ_ANON) != 0)
2649ecfbddf0SKonstantin Belousov kve->kve_obj = (uintptr_t)obj;
2650ecfbddf0SKonstantin Belousov
2651a91831a2SKonstantin Belousov for (tobj = obj; tobj != NULL;
2652a91831a2SKonstantin Belousov tobj = tobj->backing_object) {
2653a91831a2SKonstantin Belousov VM_OBJECT_RLOCK(tobj);
265431608624SJohn Baldwin kve->kve_offset += tobj->backing_object_offset;
2655a91831a2SKonstantin Belousov lobj = tobj;
2656a91831a2SKonstantin Belousov }
26573760e341SKonstantin Belousov if (obj->backing_object == NULL)
265843151ee6SPeter Wemm kve->kve_private_resident =
265943151ee6SPeter Wemm obj->resident_page_count;
2660baaa7969SKonstantin Belousov kern_proc_vmmap_resident(map, entry,
2661baaa7969SKonstantin Belousov &kve->kve_resident, &super);
2662baaa7969SKonstantin Belousov if (super)
2663baaa7969SKonstantin Belousov kve->kve_flags |= KVME_FLAG_SUPER;
2664d7671ad8SRyan Libby for (tobj = obj; tobj != NULL; tobj = nobj) {
2665d7671ad8SRyan Libby nobj = tobj->backing_object;
2666a91831a2SKonstantin Belousov if (tobj != obj && tobj != lobj)
2667a91831a2SKonstantin Belousov VM_OBJECT_RUNLOCK(tobj);
2668a91831a2SKonstantin Belousov }
2669a91831a2SKonstantin Belousov } else {
2670a91831a2SKonstantin Belousov lobj = NULL;
267143151ee6SPeter Wemm }
267243151ee6SPeter Wemm
2673118d0afaSKonstantin Belousov kve->kve_start = entry->start;
2674118d0afaSKonstantin Belousov kve->kve_end = entry->end;
267531608624SJohn Baldwin kve->kve_offset += entry->offset;
2676118d0afaSKonstantin Belousov
2677118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_READ)
2678118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_READ;
2679118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_WRITE)
2680118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_WRITE;
2681118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_EXECUTE)
2682118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_EXEC;
2683409c2fa3SKonstantin Belousov if (entry->max_protection & VM_PROT_READ)
2684409c2fa3SKonstantin Belousov kve->kve_protection |= KVME_MAX_PROT_READ;
2685409c2fa3SKonstantin Belousov if (entry->max_protection & VM_PROT_WRITE)
2686409c2fa3SKonstantin Belousov kve->kve_protection |= KVME_MAX_PROT_WRITE;
2687409c2fa3SKonstantin Belousov if (entry->max_protection & VM_PROT_EXECUTE)
2688409c2fa3SKonstantin Belousov kve->kve_protection |= KVME_MAX_PROT_EXEC;
2689118d0afaSKonstantin Belousov
2690118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_COW)
2691118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_COW;
2692118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2693118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2694937912eaSAttilio Rao if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2695937912eaSAttilio Rao kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2696599fc82bSGabor Pali if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2697599fc82bSGabor Pali kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
26986a855903SMark Johnston if (entry->eflags & MAP_ENTRY_USER_WIRED)
26996a855903SMark Johnston kve->kve_flags |= KVME_FLAG_USER_WIRED;
2700118d0afaSKonstantin Belousov
270125c6318cSKonstantin Belousov guard = (entry->eflags & MAP_ENTRY_GUARD) != 0;
270225c6318cSKonstantin Belousov
2703118d0afaSKonstantin Belousov last_timestamp = map->timestamp;
2704118d0afaSKonstantin Belousov vm_map_unlock_read(map);
2705118d0afaSKonstantin Belousov
270643151ee6SPeter Wemm freepath = NULL;
270743151ee6SPeter Wemm fullpath = "";
2708a91831a2SKonstantin Belousov if (lobj != NULL) {
27095e38e3f5SEric van Gyzen kve->kve_type = vm_object_kvme_type(lobj, &vp);
27105e38e3f5SEric van Gyzen if (vp != NULL)
271143151ee6SPeter Wemm vref(vp);
271243151ee6SPeter Wemm if (lobj != obj)
2713bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(lobj);
271443151ee6SPeter Wemm
271543151ee6SPeter Wemm kve->kve_ref_count = obj->ref_count;
271643151ee6SPeter Wemm kve->kve_shadow_count = obj->shadow_count;
2717*1123986dSJohn Baldwin if (obj->type == OBJT_DEVICE ||
2718*1123986dSJohn Baldwin obj->type == OBJT_MGTDEVICE) {
2719*1123986dSJohn Baldwin cdev_pager_get_path(obj, kve->kve_path,
2720*1123986dSJohn Baldwin sizeof(kve->kve_path));
2721ac9b565bSKonstantin Belousov }
2722bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(obj);
2723d3dd6bd4SKonstantin Belousov if ((lobj->flags & OBJ_SYSVSHM) != 0) {
2724d3dd6bd4SKonstantin Belousov kve->kve_flags |= KVME_FLAG_SYSVSHM;
2725d3dd6bd4SKonstantin Belousov shmobjinfo(lobj, &key, &seq);
2726d3dd6bd4SKonstantin Belousov kve->kve_vn_fileid = key;
2727d3dd6bd4SKonstantin Belousov kve->kve_vn_fsid_freebsd11 = seq;
2728d3dd6bd4SKonstantin Belousov }
2729a8c641bbSKonstantin Belousov if ((lobj->flags & OBJ_POSIXSHM) != 0) {
2730a8c641bbSKonstantin Belousov kve->kve_flags |= KVME_FLAG_POSIXSHM;
2731a8c641bbSKonstantin Belousov shm_get_path(lobj, kve->kve_path,
2732a8c641bbSKonstantin Belousov sizeof(kve->kve_path));
2733a8c641bbSKonstantin Belousov }
273443151ee6SPeter Wemm if (vp != NULL) {
2735feabaaf9SMateusz Guzik vn_fullpath(vp, &fullpath, &freepath);
27360daf62d9SStanislav Sedov kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
273743151ee6SPeter Wemm cred = curthread->td_ucred;
273843151ee6SPeter Wemm vn_lock(vp, LK_SHARED | LK_RETRY);
273943151ee6SPeter Wemm if (VOP_GETATTR(vp, &va, cred) == 0) {
27400daf62d9SStanislav Sedov kve->kve_vn_fileid = va.va_fileid;
27410daf62d9SStanislav Sedov kve->kve_vn_fsid = va.va_fsid;
274269921123SKonstantin Belousov kve->kve_vn_fsid_freebsd11 =
274369921123SKonstantin Belousov kve->kve_vn_fsid; /* truncate */
27440daf62d9SStanislav Sedov kve->kve_vn_mode =
27450daf62d9SStanislav Sedov MAKEIMODE(va.va_type, va.va_mode);
27460daf62d9SStanislav Sedov kve->kve_vn_size = va.va_size;
27470daf62d9SStanislav Sedov kve->kve_vn_rdev = va.va_rdev;
274869921123SKonstantin Belousov kve->kve_vn_rdev_freebsd11 =
274969921123SKonstantin Belousov kve->kve_vn_rdev; /* truncate */
27500daf62d9SStanislav Sedov kve->kve_status = KF_ATTR_VALID;
275143151ee6SPeter Wemm }
275243151ee6SPeter Wemm vput(vp);
275371a66883SKonstantin Belousov strlcpy(kve->kve_path, fullpath, sizeof(
275471a66883SKonstantin Belousov kve->kve_path));
275571a66883SKonstantin Belousov free(freepath, M_TEMP);
275643151ee6SPeter Wemm }
275743151ee6SPeter Wemm } else {
275825c6318cSKonstantin Belousov kve->kve_type = guard ? KVME_TYPE_GUARD :
275925c6318cSKonstantin Belousov KVME_TYPE_NONE;
276043151ee6SPeter Wemm kve->kve_ref_count = 0;
276143151ee6SPeter Wemm kve->kve_shadow_count = 0;
276243151ee6SPeter Wemm }
276343151ee6SPeter Wemm
276443151ee6SPeter Wemm /* Pack record size down */
2765e6b95927SConrad Meyer if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2766e6b95927SConrad Meyer kve->kve_structsize =
2767e6b95927SConrad Meyer offsetof(struct kinfo_vmentry, kve_path) +
276843151ee6SPeter Wemm strlen(kve->kve_path) + 1;
2769e6b95927SConrad Meyer else
2770e6b95927SConrad Meyer kve->kve_structsize = sizeof(*kve);
277143151ee6SPeter Wemm kve->kve_structsize = roundup(kve->kve_structsize,
277243151ee6SPeter Wemm sizeof(uint64_t));
2773e6b95927SConrad Meyer
2774e6b95927SConrad Meyer /* Halt filling and truncate rather than exceeding maxlen */
2775e6b95927SConrad Meyer if (maxlen != -1 && maxlen < kve->kve_structsize) {
2776e6b95927SConrad Meyer error = 0;
2777e6b95927SConrad Meyer vm_map_lock_read(map);
2778e6b95927SConrad Meyer break;
2779e6b95927SConrad Meyer } else if (maxlen != -1)
2780e6b95927SConrad Meyer maxlen -= kve->kve_structsize;
2781e6b95927SConrad Meyer
278257c2505eSKonstantin Belousov if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
278357c2505eSKonstantin Belousov error = ENOMEM;
278443151ee6SPeter Wemm vm_map_lock_read(map);
278557c2505eSKonstantin Belousov if (error != 0)
278643151ee6SPeter Wemm break;
278722a448c4SKonstantin Belousov if (last_timestamp != map->timestamp) {
278843151ee6SPeter Wemm vm_map_lookup_entry(map, addr - 1, &tmp_entry);
278943151ee6SPeter Wemm entry = tmp_entry;
279043151ee6SPeter Wemm }
279143151ee6SPeter Wemm }
279243151ee6SPeter Wemm vm_map_unlock_read(map);
2793c7462f43SKonstantin Belousov vmspace_free(vm);
279443151ee6SPeter Wemm PRELE(p);
279543151ee6SPeter Wemm free(kve, M_TEMP);
279643151ee6SPeter Wemm return (error);
279743151ee6SPeter Wemm }
2798cc43c38cSRobert Watson
2799fe52cf54SMikolaj Golub static int
sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)2800fe52cf54SMikolaj Golub sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2801fe52cf54SMikolaj Golub {
2802fe52cf54SMikolaj Golub struct proc *p;
2803fe52cf54SMikolaj Golub struct sbuf sb;
28040dcef81dSMark Johnston u_int namelen;
2805fe52cf54SMikolaj Golub int error, error2, *name;
2806fe52cf54SMikolaj Golub
28070dcef81dSMark Johnston namelen = arg2;
28080dcef81dSMark Johnston if (namelen != 1)
28090dcef81dSMark Johnston return (EINVAL);
28100dcef81dSMark Johnston
2811fe52cf54SMikolaj Golub name = (int *)arg1;
2812fe52cf54SMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
28131eafc078SIan Lepore sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2814fe52cf54SMikolaj Golub error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2815fe52cf54SMikolaj Golub if (error != 0) {
2816fe52cf54SMikolaj Golub sbuf_delete(&sb);
2817fe52cf54SMikolaj Golub return (error);
2818fe52cf54SMikolaj Golub }
2819e6b95927SConrad Meyer error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2820fe52cf54SMikolaj Golub error2 = sbuf_finish(&sb);
2821fe52cf54SMikolaj Golub sbuf_delete(&sb);
2822fe52cf54SMikolaj Golub return (error != 0 ? error : error2);
2823fe52cf54SMikolaj Golub }
2824fe52cf54SMikolaj Golub
28251cc8c45cSRobert Watson #if defined(STACK) || defined(DDB)
28261cc8c45cSRobert Watson static int
sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)28271cc8c45cSRobert Watson sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
28281cc8c45cSRobert Watson {
28291cc8c45cSRobert Watson struct kinfo_kstack *kkstp;
28301cc8c45cSRobert Watson int error, i, *name, numthreads;
28311cc8c45cSRobert Watson lwpid_t *lwpidarray;
28321cc8c45cSRobert Watson struct thread *td;
28331cc8c45cSRobert Watson struct stack *st;
28341cc8c45cSRobert Watson struct sbuf sb;
28351cc8c45cSRobert Watson struct proc *p;
28360dcef81dSMark Johnston u_int namelen;
28370dcef81dSMark Johnston
28380dcef81dSMark Johnston namelen = arg2;
28390dcef81dSMark Johnston if (namelen != 1)
28400dcef81dSMark Johnston return (EINVAL);
28411cc8c45cSRobert Watson
28421cc8c45cSRobert Watson name = (int *)arg1;
2843fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2844fa3935bcSMikolaj Golub if (error != 0)
28451cc8c45cSRobert Watson return (error);
28461cc8c45cSRobert Watson
28471cc8c45cSRobert Watson kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2848f38c0c46SMark Johnston st = stack_create(M_WAITOK);
28491cc8c45cSRobert Watson
28501cc8c45cSRobert Watson lwpidarray = NULL;
2851374ae2a3SJeff Roberson PROC_LOCK(p);
285211748daeSMark Johnston do {
28531cc8c45cSRobert Watson if (lwpidarray != NULL) {
28541cc8c45cSRobert Watson free(lwpidarray, M_TEMP);
28551cc8c45cSRobert Watson lwpidarray = NULL;
28561cc8c45cSRobert Watson }
28571cc8c45cSRobert Watson numthreads = p->p_numthreads;
2858374ae2a3SJeff Roberson PROC_UNLOCK(p);
28591cc8c45cSRobert Watson lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
28601cc8c45cSRobert Watson M_WAITOK | M_ZERO);
2861374ae2a3SJeff Roberson PROC_LOCK(p);
286211748daeSMark Johnston } while (numthreads < p->p_numthreads);
28631cc8c45cSRobert Watson
28641cc8c45cSRobert Watson /*
28651cc8c45cSRobert Watson * XXXRW: During the below loop, execve(2) and countless other sorts
28661cc8c45cSRobert Watson * of changes could have taken place. Should we check to see if the
28671cc8c45cSRobert Watson * vmspace has been replaced, or the like, in order to prevent
28681cc8c45cSRobert Watson * giving a snapshot that spans, say, execve(2), with some threads
28691cc8c45cSRobert Watson * before and some after? Among other things, the credentials could
28701cc8c45cSRobert Watson * have changed, in which case the right to extract debug info might
28711cc8c45cSRobert Watson * no longer be assured.
28721cc8c45cSRobert Watson */
287311748daeSMark Johnston i = 0;
28741cc8c45cSRobert Watson FOREACH_THREAD_IN_PROC(p, td) {
28751cc8c45cSRobert Watson KASSERT(i < numthreads,
28761cc8c45cSRobert Watson ("sysctl_kern_proc_kstack: numthreads"));
28771cc8c45cSRobert Watson lwpidarray[i] = td->td_tid;
28781cc8c45cSRobert Watson i++;
28791cc8c45cSRobert Watson }
2880f837888aSMateusz Guzik PROC_UNLOCK(p);
28811cc8c45cSRobert Watson numthreads = i;
28821cc8c45cSRobert Watson for (i = 0; i < numthreads; i++) {
2883f837888aSMateusz Guzik td = tdfind(lwpidarray[i], p->p_pid);
28841cc8c45cSRobert Watson if (td == NULL) {
28851cc8c45cSRobert Watson continue;
28861cc8c45cSRobert Watson }
28871cc8c45cSRobert Watson bzero(kkstp, sizeof(*kkstp));
28881cc8c45cSRobert Watson (void)sbuf_new(&sb, kkstp->kkst_trace,
28891cc8c45cSRobert Watson sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
28901cc8c45cSRobert Watson thread_lock(td);
28911cc8c45cSRobert Watson kkstp->kkst_tid = td->td_tid;
2892e24a6552SMark Johnston if (stack_save_td(st, td) == 0)
2893610141ceSMark Johnston kkstp->kkst_state = KKST_STATE_STACKOK;
2894610141ceSMark Johnston else
28951cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_RUNNING;
28961cc8c45cSRobert Watson thread_unlock(td);
28971cc8c45cSRobert Watson PROC_UNLOCK(p);
28981cc8c45cSRobert Watson stack_sbuf_print(&sb, st);
28991cc8c45cSRobert Watson sbuf_finish(&sb);
29001cc8c45cSRobert Watson sbuf_delete(&sb);
29011cc8c45cSRobert Watson error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
29021cc8c45cSRobert Watson if (error)
29031cc8c45cSRobert Watson break;
29041cc8c45cSRobert Watson }
290540aad3e4SMateusz Guzik PRELE(p);
29061cc8c45cSRobert Watson if (lwpidarray != NULL)
29071cc8c45cSRobert Watson free(lwpidarray, M_TEMP);
29081cc8c45cSRobert Watson stack_destroy(st);
29091cc8c45cSRobert Watson free(kkstp, M_TEMP);
29101cc8c45cSRobert Watson return (error);
29111cc8c45cSRobert Watson }
29121cc8c45cSRobert Watson #endif
29131cc8c45cSRobert Watson
2914254d03c5SBrooks Davis /*
2915254d03c5SBrooks Davis * This sysctl allows a process to retrieve the full list of groups from
2916254d03c5SBrooks Davis * itself or another process.
2917254d03c5SBrooks Davis */
2918254d03c5SBrooks Davis static int
sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)2919254d03c5SBrooks Davis sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2920254d03c5SBrooks Davis {
2921254d03c5SBrooks Davis pid_t *pidp = (pid_t *)arg1;
2922254d03c5SBrooks Davis unsigned int arglen = arg2;
2923254d03c5SBrooks Davis struct proc *p;
2924254d03c5SBrooks Davis struct ucred *cred;
2925254d03c5SBrooks Davis int error;
2926254d03c5SBrooks Davis
2927254d03c5SBrooks Davis if (arglen != 1)
2928254d03c5SBrooks Davis return (EINVAL);
2929254d03c5SBrooks Davis if (*pidp == -1) { /* -1 means this process */
2930254d03c5SBrooks Davis p = req->td->td_proc;
29312570cdd6SMateusz Guzik PROC_LOCK(p);
2932254d03c5SBrooks Davis } else {
2933fa3935bcSMikolaj Golub error = pget(*pidp, PGET_CANSEE, &p);
2934fa3935bcSMikolaj Golub if (error != 0)
2935254d03c5SBrooks Davis return (error);
2936254d03c5SBrooks Davis }
2937254d03c5SBrooks Davis
2938254d03c5SBrooks Davis cred = crhold(p->p_ucred);
2939254d03c5SBrooks Davis PROC_UNLOCK(p);
2940254d03c5SBrooks Davis
2941254d03c5SBrooks Davis error = SYSCTL_OUT(req, cred->cr_groups,
2942254d03c5SBrooks Davis cred->cr_ngroups * sizeof(gid_t));
2943254d03c5SBrooks Davis crfree(cred);
2944254d03c5SBrooks Davis return (error);
2945254d03c5SBrooks Davis }
2946254d03c5SBrooks Davis
29479e7d0583SMikolaj Golub /*
29488854fe39SMikolaj Golub * This sysctl allows a process to retrieve or/and set the resource limit for
29499e7d0583SMikolaj Golub * another process.
29509e7d0583SMikolaj Golub */
29519e7d0583SMikolaj Golub static int
sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)29529e7d0583SMikolaj Golub sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
29539e7d0583SMikolaj Golub {
29549e7d0583SMikolaj Golub int *name = (int *)arg1;
29559e7d0583SMikolaj Golub u_int namelen = arg2;
29568854fe39SMikolaj Golub struct rlimit rlim;
29579e7d0583SMikolaj Golub struct proc *p;
29588854fe39SMikolaj Golub u_int which;
29598854fe39SMikolaj Golub int flags, error;
29609e7d0583SMikolaj Golub
29618854fe39SMikolaj Golub if (namelen != 2)
29629e7d0583SMikolaj Golub return (EINVAL);
29639e7d0583SMikolaj Golub
29648854fe39SMikolaj Golub which = (u_int)name[1];
29658854fe39SMikolaj Golub if (which >= RLIM_NLIMITS)
29668854fe39SMikolaj Golub return (EINVAL);
29678854fe39SMikolaj Golub
29688854fe39SMikolaj Golub if (req->newptr != NULL && req->newlen != sizeof(rlim))
29698854fe39SMikolaj Golub return (EINVAL);
29708854fe39SMikolaj Golub
29718854fe39SMikolaj Golub flags = PGET_HOLD | PGET_NOTWEXIT;
29728854fe39SMikolaj Golub if (req->newptr != NULL)
29738854fe39SMikolaj Golub flags |= PGET_CANDEBUG;
29748854fe39SMikolaj Golub else
29758854fe39SMikolaj Golub flags |= PGET_CANSEE;
29768854fe39SMikolaj Golub error = pget((pid_t)name[0], flags, &p);
2977fa3935bcSMikolaj Golub if (error != 0)
29789e7d0583SMikolaj Golub return (error);
29798854fe39SMikolaj Golub
29809e7d0583SMikolaj Golub /*
29818854fe39SMikolaj Golub * Retrieve limit.
29829e7d0583SMikolaj Golub */
29838854fe39SMikolaj Golub if (req->oldptr != NULL) {
29848854fe39SMikolaj Golub PROC_LOCK(p);
2985f6f6d240SMateusz Guzik lim_rlimit_proc(p, which, &rlim);
29869e7d0583SMikolaj Golub PROC_UNLOCK(p);
29878854fe39SMikolaj Golub }
29888854fe39SMikolaj Golub error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
29898854fe39SMikolaj Golub if (error != 0)
29908854fe39SMikolaj Golub goto errout;
29918854fe39SMikolaj Golub
29928854fe39SMikolaj Golub /*
29938854fe39SMikolaj Golub * Set limit.
29948854fe39SMikolaj Golub */
29958854fe39SMikolaj Golub if (req->newptr != NULL) {
29968854fe39SMikolaj Golub error = SYSCTL_IN(req, &rlim, sizeof(rlim));
29978854fe39SMikolaj Golub if (error == 0)
29988854fe39SMikolaj Golub error = kern_proc_setrlimit(curthread, p, which, &rlim);
29999e7d0583SMikolaj Golub }
30009e7d0583SMikolaj Golub
30018854fe39SMikolaj Golub errout:
30028854fe39SMikolaj Golub PRELE(p);
30039e7d0583SMikolaj Golub return (error);
30049e7d0583SMikolaj Golub }
30059e7d0583SMikolaj Golub
30069732458fSMikolaj Golub /*
30079732458fSMikolaj Golub * This sysctl allows a process to retrieve ps_strings structure location of
30089732458fSMikolaj Golub * another process.
30099732458fSMikolaj Golub */
30109732458fSMikolaj Golub static int
sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)30119732458fSMikolaj Golub sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
30129732458fSMikolaj Golub {
30139732458fSMikolaj Golub int *name = (int *)arg1;
30149732458fSMikolaj Golub u_int namelen = arg2;
30159732458fSMikolaj Golub struct proc *p;
30169732458fSMikolaj Golub vm_offset_t ps_strings;
30179732458fSMikolaj Golub int error;
30189732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32
30199732458fSMikolaj Golub uint32_t ps_strings32;
30209732458fSMikolaj Golub #endif
30219732458fSMikolaj Golub
30229732458fSMikolaj Golub if (namelen != 1)
30239732458fSMikolaj Golub return (EINVAL);
30249732458fSMikolaj Golub
3025fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3026fa3935bcSMikolaj Golub if (error != 0)
30279732458fSMikolaj Golub return (error);
30289732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32
30299732458fSMikolaj Golub if ((req->flags & SCTL_MASK32) != 0) {
30309732458fSMikolaj Golub /*
30319732458fSMikolaj Golub * We return 0 if the 32 bit emulation request is for a 64 bit
30329732458fSMikolaj Golub * process.
30339732458fSMikolaj Golub */
30349732458fSMikolaj Golub ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
3035706f4a81SMark Johnston PTROUT(PROC_PS_STRINGS(p)) : 0;
30369732458fSMikolaj Golub PROC_UNLOCK(p);
30379732458fSMikolaj Golub error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
30389732458fSMikolaj Golub return (error);
30399732458fSMikolaj Golub }
30409732458fSMikolaj Golub #endif
3041706f4a81SMark Johnston ps_strings = PROC_PS_STRINGS(p);
30429732458fSMikolaj Golub PROC_UNLOCK(p);
30439732458fSMikolaj Golub error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
30449732458fSMikolaj Golub return (error);
30459732458fSMikolaj Golub }
30469732458fSMikolaj Golub
30476ce13747SMikolaj Golub /*
3048e0fcf639SMikolaj Golub * This sysctl allows a process to retrieve umask of another process.
30496ce13747SMikolaj Golub */
30506ce13747SMikolaj Golub static int
sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)30516ce13747SMikolaj Golub sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
30526ce13747SMikolaj Golub {
30536ce13747SMikolaj Golub int *name = (int *)arg1;
30546ce13747SMikolaj Golub u_int namelen = arg2;
30556ce13747SMikolaj Golub struct proc *p;
30566ce13747SMikolaj Golub int error;
305785078b85SConrad Meyer u_short cmask;
3058cee09850SBartek Rutkowski pid_t pid;
30596ce13747SMikolaj Golub
30606ce13747SMikolaj Golub if (namelen != 1)
30616ce13747SMikolaj Golub return (EINVAL);
30626ce13747SMikolaj Golub
3063cee09850SBartek Rutkowski pid = (pid_t)name[0];
3064cee09850SBartek Rutkowski p = curproc;
3065cee09850SBartek Rutkowski if (pid == p->p_pid || pid == 0) {
306685078b85SConrad Meyer cmask = p->p_pd->pd_cmask;
3067cee09850SBartek Rutkowski goto out;
3068cee09850SBartek Rutkowski }
3069cee09850SBartek Rutkowski
3070cee09850SBartek Rutkowski error = pget(pid, PGET_WANTREAD, &p);
30716ce13747SMikolaj Golub if (error != 0)
30726ce13747SMikolaj Golub return (error);
30736ce13747SMikolaj Golub
307485078b85SConrad Meyer cmask = p->p_pd->pd_cmask;
30756ce13747SMikolaj Golub PRELE(p);
3076cee09850SBartek Rutkowski out:
307785078b85SConrad Meyer error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
30786ce13747SMikolaj Golub return (error);
30796ce13747SMikolaj Golub }
30806ce13747SMikolaj Golub
3081903712c9SMikolaj Golub /*
3082903712c9SMikolaj Golub * This sysctl allows a process to set and retrieve binary osreldate of
3083903712c9SMikolaj Golub * another process.
3084903712c9SMikolaj Golub */
3085903712c9SMikolaj Golub static int
sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)3086903712c9SMikolaj Golub sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3087903712c9SMikolaj Golub {
3088903712c9SMikolaj Golub int *name = (int *)arg1;
3089903712c9SMikolaj Golub u_int namelen = arg2;
3090903712c9SMikolaj Golub struct proc *p;
3091903712c9SMikolaj Golub int flags, error, osrel;
3092903712c9SMikolaj Golub
3093903712c9SMikolaj Golub if (namelen != 1)
3094903712c9SMikolaj Golub return (EINVAL);
3095903712c9SMikolaj Golub
3096903712c9SMikolaj Golub if (req->newptr != NULL && req->newlen != sizeof(osrel))
3097903712c9SMikolaj Golub return (EINVAL);
3098903712c9SMikolaj Golub
3099903712c9SMikolaj Golub flags = PGET_HOLD | PGET_NOTWEXIT;
3100903712c9SMikolaj Golub if (req->newptr != NULL)
3101903712c9SMikolaj Golub flags |= PGET_CANDEBUG;
3102903712c9SMikolaj Golub else
3103903712c9SMikolaj Golub flags |= PGET_CANSEE;
3104903712c9SMikolaj Golub error = pget((pid_t)name[0], flags, &p);
3105903712c9SMikolaj Golub if (error != 0)
3106903712c9SMikolaj Golub return (error);
3107903712c9SMikolaj Golub
3108903712c9SMikolaj Golub error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3109903712c9SMikolaj Golub if (error != 0)
3110903712c9SMikolaj Golub goto errout;
3111903712c9SMikolaj Golub
3112903712c9SMikolaj Golub if (req->newptr != NULL) {
3113903712c9SMikolaj Golub error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3114903712c9SMikolaj Golub if (error != 0)
3115903712c9SMikolaj Golub goto errout;
3116903712c9SMikolaj Golub if (osrel < 0) {
3117903712c9SMikolaj Golub error = EINVAL;
3118903712c9SMikolaj Golub goto errout;
3119903712c9SMikolaj Golub }
3120903712c9SMikolaj Golub p->p_osrel = osrel;
3121903712c9SMikolaj Golub }
3122903712c9SMikolaj Golub errout:
3123903712c9SMikolaj Golub PRELE(p);
3124903712c9SMikolaj Golub return (error);
3125903712c9SMikolaj Golub }
3126903712c9SMikolaj Golub
312780c3af4eSKonstantin Belousov static int
sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)312880c3af4eSKonstantin Belousov sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
312980c3af4eSKonstantin Belousov {
313080c3af4eSKonstantin Belousov int *name = (int *)arg1;
313180c3af4eSKonstantin Belousov u_int namelen = arg2;
313280c3af4eSKonstantin Belousov struct proc *p;
313380c3af4eSKonstantin Belousov struct kinfo_sigtramp kst;
313480c3af4eSKonstantin Belousov const struct sysentvec *sv;
313580c3af4eSKonstantin Belousov int error;
313680c3af4eSKonstantin Belousov #ifdef COMPAT_FREEBSD32
313780c3af4eSKonstantin Belousov struct kinfo_sigtramp32 kst32;
313880c3af4eSKonstantin Belousov #endif
313980c3af4eSKonstantin Belousov
314080c3af4eSKonstantin Belousov if (namelen != 1)
314180c3af4eSKonstantin Belousov return (EINVAL);
314280c3af4eSKonstantin Belousov
314380c3af4eSKonstantin Belousov error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
314480c3af4eSKonstantin Belousov if (error != 0)
314580c3af4eSKonstantin Belousov return (error);
314680c3af4eSKonstantin Belousov sv = p->p_sysent;
314780c3af4eSKonstantin Belousov #ifdef COMPAT_FREEBSD32
314880c3af4eSKonstantin Belousov if ((req->flags & SCTL_MASK32) != 0) {
314980c3af4eSKonstantin Belousov bzero(&kst32, sizeof(kst32));
315080c3af4eSKonstantin Belousov if (SV_PROC_FLAG(p, SV_ILP32)) {
3151361971fbSKornel Dulęba if (PROC_HAS_SHP(p)) {
3152f6ac79fbSKornel Dulęba kst32.ksigtramp_start = PROC_SIGCODE(p);
3153f6ac79fbSKornel Dulęba kst32.ksigtramp_end = kst32.ksigtramp_start +
3154ab4524b3SKonstantin Belousov ((sv->sv_flags & SV_DSO_SIG) == 0 ?
3155ab4524b3SKonstantin Belousov *sv->sv_szsigcode :
3156ab4524b3SKonstantin Belousov (uintptr_t)sv->sv_szsigcode);
315780c3af4eSKonstantin Belousov } else {
3158706f4a81SMark Johnston kst32.ksigtramp_start = PROC_PS_STRINGS(p) -
315980c3af4eSKonstantin Belousov *sv->sv_szsigcode;
3160706f4a81SMark Johnston kst32.ksigtramp_end = PROC_PS_STRINGS(p);
316180c3af4eSKonstantin Belousov }
316280c3af4eSKonstantin Belousov }
316380c3af4eSKonstantin Belousov PROC_UNLOCK(p);
316480c3af4eSKonstantin Belousov error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
316580c3af4eSKonstantin Belousov return (error);
316680c3af4eSKonstantin Belousov }
316780c3af4eSKonstantin Belousov #endif
316880c3af4eSKonstantin Belousov bzero(&kst, sizeof(kst));
3169361971fbSKornel Dulęba if (PROC_HAS_SHP(p)) {
3170f6ac79fbSKornel Dulęba kst.ksigtramp_start = (char *)PROC_SIGCODE(p);
3171f6ac79fbSKornel Dulęba kst.ksigtramp_end = (char *)kst.ksigtramp_start +
3172ab4524b3SKonstantin Belousov ((sv->sv_flags & SV_DSO_SIG) == 0 ? *sv->sv_szsigcode :
3173ab4524b3SKonstantin Belousov (uintptr_t)sv->sv_szsigcode);
317480c3af4eSKonstantin Belousov } else {
3175706f4a81SMark Johnston kst.ksigtramp_start = (char *)PROC_PS_STRINGS(p) -
317680c3af4eSKonstantin Belousov *sv->sv_szsigcode;
3177706f4a81SMark Johnston kst.ksigtramp_end = (char *)PROC_PS_STRINGS(p);
317880c3af4eSKonstantin Belousov }
317980c3af4eSKonstantin Belousov PROC_UNLOCK(p);
318080c3af4eSKonstantin Belousov error = SYSCTL_OUT(req, &kst, sizeof(kst));
318180c3af4eSKonstantin Belousov return (error);
318280c3af4eSKonstantin Belousov }
318380c3af4eSKonstantin Belousov
318448fcb463SKonstantin Belousov static int
sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)318548fcb463SKonstantin Belousov sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
318648fcb463SKonstantin Belousov {
318748fcb463SKonstantin Belousov int *name = (int *)arg1;
318848fcb463SKonstantin Belousov u_int namelen = arg2;
318948fcb463SKonstantin Belousov pid_t pid;
319048fcb463SKonstantin Belousov struct proc *p;
319148fcb463SKonstantin Belousov struct thread *td1;
319248fcb463SKonstantin Belousov uintptr_t addr;
319348fcb463SKonstantin Belousov #ifdef COMPAT_FREEBSD32
319448fcb463SKonstantin Belousov uint32_t addr32;
319548fcb463SKonstantin Belousov #endif
319648fcb463SKonstantin Belousov int error;
319748fcb463SKonstantin Belousov
319848fcb463SKonstantin Belousov if (namelen != 1 || req->newptr != NULL)
319948fcb463SKonstantin Belousov return (EINVAL);
320048fcb463SKonstantin Belousov
320148fcb463SKonstantin Belousov pid = (pid_t)name[0];
320248fcb463SKonstantin Belousov error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
320348fcb463SKonstantin Belousov if (error != 0)
320448fcb463SKonstantin Belousov return (error);
320548fcb463SKonstantin Belousov
320648fcb463SKonstantin Belousov PROC_LOCK(p);
320748fcb463SKonstantin Belousov #ifdef COMPAT_FREEBSD32
320848fcb463SKonstantin Belousov if (SV_CURPROC_FLAG(SV_ILP32)) {
320948fcb463SKonstantin Belousov if (!SV_PROC_FLAG(p, SV_ILP32)) {
321048fcb463SKonstantin Belousov error = EINVAL;
321148fcb463SKonstantin Belousov goto errlocked;
321248fcb463SKonstantin Belousov }
321348fcb463SKonstantin Belousov }
321448fcb463SKonstantin Belousov #endif
321548fcb463SKonstantin Belousov if (pid <= PID_MAX) {
321648fcb463SKonstantin Belousov td1 = FIRST_THREAD_IN_PROC(p);
321748fcb463SKonstantin Belousov } else {
321848fcb463SKonstantin Belousov FOREACH_THREAD_IN_PROC(p, td1) {
321948fcb463SKonstantin Belousov if (td1->td_tid == pid)
322048fcb463SKonstantin Belousov break;
322148fcb463SKonstantin Belousov }
322248fcb463SKonstantin Belousov }
322348fcb463SKonstantin Belousov if (td1 == NULL) {
322448fcb463SKonstantin Belousov error = ESRCH;
322548fcb463SKonstantin Belousov goto errlocked;
322648fcb463SKonstantin Belousov }
322748fcb463SKonstantin Belousov /*
322848fcb463SKonstantin Belousov * The access to the private thread flags. It is fine as far
322948fcb463SKonstantin Belousov * as no out-of-thin-air values are read from td_pflags, and
323048fcb463SKonstantin Belousov * usermode read of the td_sigblock_ptr is racy inherently,
323148fcb463SKonstantin Belousov * since target process might have already changed it
323248fcb463SKonstantin Belousov * meantime.
323348fcb463SKonstantin Belousov */
323448fcb463SKonstantin Belousov if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
323548fcb463SKonstantin Belousov addr = (uintptr_t)td1->td_sigblock_ptr;
323648fcb463SKonstantin Belousov else
323748fcb463SKonstantin Belousov error = ENOTTY;
323848fcb463SKonstantin Belousov
323948fcb463SKonstantin Belousov errlocked:
324048fcb463SKonstantin Belousov _PRELE(p);
324148fcb463SKonstantin Belousov PROC_UNLOCK(p);
324248fcb463SKonstantin Belousov if (error != 0)
324348fcb463SKonstantin Belousov return (error);
324448fcb463SKonstantin Belousov
324548fcb463SKonstantin Belousov #ifdef COMPAT_FREEBSD32
324648fcb463SKonstantin Belousov if (SV_CURPROC_FLAG(SV_ILP32)) {
324748fcb463SKonstantin Belousov addr32 = addr;
324848fcb463SKonstantin Belousov error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
324948fcb463SKonstantin Belousov } else
325048fcb463SKonstantin Belousov #endif
325148fcb463SKonstantin Belousov error = SYSCTL_OUT(req, &addr, sizeof(addr));
325248fcb463SKonstantin Belousov return (error);
325348fcb463SKonstantin Belousov }
325448fcb463SKonstantin Belousov
32553ce04acaSMark Johnston static int
sysctl_kern_proc_vm_layout(SYSCTL_HANDLER_ARGS)32563ce04acaSMark Johnston sysctl_kern_proc_vm_layout(SYSCTL_HANDLER_ARGS)
32573ce04acaSMark Johnston {
32583ce04acaSMark Johnston struct kinfo_vm_layout kvm;
32593ce04acaSMark Johnston struct proc *p;
32603ce04acaSMark Johnston struct vmspace *vmspace;
32613ce04acaSMark Johnston int error, *name;
32623ce04acaSMark Johnston
32633ce04acaSMark Johnston name = (int *)arg1;
32643ce04acaSMark Johnston if ((u_int)arg2 != 1)
32653ce04acaSMark Johnston return (EINVAL);
32663ce04acaSMark Johnston
32673ce04acaSMark Johnston error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
32683ce04acaSMark Johnston if (error != 0)
32693ce04acaSMark Johnston return (error);
32703ce04acaSMark Johnston #ifdef COMPAT_FREEBSD32
32713ce04acaSMark Johnston if (SV_CURPROC_FLAG(SV_ILP32)) {
32723ce04acaSMark Johnston if (!SV_PROC_FLAG(p, SV_ILP32)) {
32733ce04acaSMark Johnston PROC_UNLOCK(p);
32743ce04acaSMark Johnston return (EINVAL);
32753ce04acaSMark Johnston }
32763ce04acaSMark Johnston }
32773ce04acaSMark Johnston #endif
32783ce04acaSMark Johnston vmspace = vmspace_acquire_ref(p);
32793ce04acaSMark Johnston PROC_UNLOCK(p);
32803ce04acaSMark Johnston
32813ce04acaSMark Johnston memset(&kvm, 0, sizeof(kvm));
32823ce04acaSMark Johnston kvm.kvm_min_user_addr = vm_map_min(&vmspace->vm_map);
32833ce04acaSMark Johnston kvm.kvm_max_user_addr = vm_map_max(&vmspace->vm_map);
32843ce04acaSMark Johnston kvm.kvm_text_addr = (uintptr_t)vmspace->vm_taddr;
32853ce04acaSMark Johnston kvm.kvm_text_size = vmspace->vm_tsize;
32863ce04acaSMark Johnston kvm.kvm_data_addr = (uintptr_t)vmspace->vm_daddr;
32873ce04acaSMark Johnston kvm.kvm_data_size = vmspace->vm_dsize;
32883ce04acaSMark Johnston kvm.kvm_stack_addr = (uintptr_t)vmspace->vm_maxsaddr;
32893ce04acaSMark Johnston kvm.kvm_stack_size = vmspace->vm_ssize;
3290939f0b63SKornel Dulęba kvm.kvm_shp_addr = vmspace->vm_shp_base;
3291939f0b63SKornel Dulęba kvm.kvm_shp_size = p->p_sysent->sv_shared_page_len;
32923ce04acaSMark Johnston if ((vmspace->vm_map.flags & MAP_WIREFUTURE) != 0)
32933ce04acaSMark Johnston kvm.kvm_map_flags |= KMAP_FLAG_WIREFUTURE;
32943ce04acaSMark Johnston if ((vmspace->vm_map.flags & MAP_ASLR) != 0)
32953ce04acaSMark Johnston kvm.kvm_map_flags |= KMAP_FLAG_ASLR;
32963ce04acaSMark Johnston if ((vmspace->vm_map.flags & MAP_ASLR_IGNSTART) != 0)
32973ce04acaSMark Johnston kvm.kvm_map_flags |= KMAP_FLAG_ASLR_IGNSTART;
32983ce04acaSMark Johnston if ((vmspace->vm_map.flags & MAP_WXORX) != 0)
32993ce04acaSMark Johnston kvm.kvm_map_flags |= KMAP_FLAG_WXORX;
33003ce04acaSMark Johnston if ((vmspace->vm_map.flags & MAP_ASLR_STACK) != 0)
33013ce04acaSMark Johnston kvm.kvm_map_flags |= KMAP_FLAG_ASLR_STACK;
3302939f0b63SKornel Dulęba if (vmspace->vm_shp_base != p->p_sysent->sv_shared_page_base &&
3303939f0b63SKornel Dulęba PROC_HAS_SHP(p))
3304939f0b63SKornel Dulęba kvm.kvm_map_flags |= KMAP_FLAG_ASLR_SHARED_PAGE;
33053ce04acaSMark Johnston
33063ce04acaSMark Johnston #ifdef COMPAT_FREEBSD32
33073ce04acaSMark Johnston if (SV_CURPROC_FLAG(SV_ILP32)) {
33083ce04acaSMark Johnston struct kinfo_vm_layout32 kvm32;
33093ce04acaSMark Johnston
33103ce04acaSMark Johnston memset(&kvm32, 0, sizeof(kvm32));
33113ce04acaSMark Johnston kvm32.kvm_min_user_addr = (uint32_t)kvm.kvm_min_user_addr;
33123ce04acaSMark Johnston kvm32.kvm_max_user_addr = (uint32_t)kvm.kvm_max_user_addr;
33133ce04acaSMark Johnston kvm32.kvm_text_addr = (uint32_t)kvm.kvm_text_addr;
33143ce04acaSMark Johnston kvm32.kvm_text_size = (uint32_t)kvm.kvm_text_size;
33153ce04acaSMark Johnston kvm32.kvm_data_addr = (uint32_t)kvm.kvm_data_addr;
33163ce04acaSMark Johnston kvm32.kvm_data_size = (uint32_t)kvm.kvm_data_size;
33173ce04acaSMark Johnston kvm32.kvm_stack_addr = (uint32_t)kvm.kvm_stack_addr;
33183ce04acaSMark Johnston kvm32.kvm_stack_size = (uint32_t)kvm.kvm_stack_size;
3319939f0b63SKornel Dulęba kvm32.kvm_shp_addr = (uint32_t)kvm.kvm_shp_addr;
3320939f0b63SKornel Dulęba kvm32.kvm_shp_size = (uint32_t)kvm.kvm_shp_size;
33213ce04acaSMark Johnston kvm32.kvm_map_flags = kvm.kvm_map_flags;
33223ce04acaSMark Johnston error = SYSCTL_OUT(req, &kvm32, sizeof(kvm32));
33233ce04acaSMark Johnston goto out;
33243ce04acaSMark Johnston }
33253ce04acaSMark Johnston #endif
33263ce04acaSMark Johnston
33273ce04acaSMark Johnston error = SYSCTL_OUT(req, &kvm, sizeof(kvm));
33283ce04acaSMark Johnston #ifdef COMPAT_FREEBSD32
33293ce04acaSMark Johnston out:
33303ce04acaSMark Johnston #endif
33313ce04acaSMark Johnston vmspace_free(vmspace);
33323ce04acaSMark Johnston return (error);
33333ce04acaSMark Johnston }
33343ce04acaSMark Johnston
33357029da5cSPawel Biernacki SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
33367029da5cSPawel Biernacki "Process table");
33373ce93e4eSPoul-Henning Kamp
333824f87fdbSJohn Baldwin SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
333924f87fdbSJohn Baldwin CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
334024f87fdbSJohn Baldwin "Return entire process table");
33413ce93e4eSPoul-Henning Kamp
334224f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3343078842c5SGarance A Drosehn sysctl_kern_proc, "Process table");
3344078842c5SGarance A Drosehn
334524f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
33463ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table");
33473ce93e4eSPoul-Henning Kamp
334824f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3349b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table");
3350b8fdc89dSGarance A Drosehn
335124f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
335224f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
335324f87fdbSJohn Baldwin
335424f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3355b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table");
3356b8fdc89dSGarance A Drosehn
335724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
33583ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table");
33593ce93e4eSPoul-Henning Kamp
336024f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
33613ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table");
33623ce93e4eSPoul-Henning Kamp
336324f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
33643ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table");
33653ce93e4eSPoul-Henning Kamp
336624f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
336730c6f34eSScott Long sysctl_kern_proc, "Return process table, no threads");
336830c6f34eSScott Long
33690c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3370aa32b5e0SEdward Tomasz Napierala CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
33719b6d9dbaSPoul-Henning Kamp sysctl_kern_proc_args, "Process argument list");
3372baf731e6SRobert Drehmel
337345efc9b4SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3374c5cfcb1cSMikolaj Golub sysctl_kern_proc_env, "Process environment");
3375c5cfcb1cSMikolaj Golub
337645efc9b4SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
337745efc9b4SMikolaj Golub CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3378c5cfcb1cSMikolaj Golub
337924f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
338024f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3381fe769cddSDavid Schultz
338224f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
338324f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
338424f87fdbSJohn Baldwin "Process syscall vector name (ABI type)");
33852648efa6SDaniel Eischen
33860c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
338724f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3388078842c5SGarance A Drosehn
33890c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
339024f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
33912648efa6SDaniel Eischen
33920c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
339324f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3394b8fdc89dSGarance A Drosehn
33950c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
339624f87fdbSJohn Baldwin sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
33970c898376SPoul-Henning Kamp
33980c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
339924f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3400b8fdc89dSGarance A Drosehn
34010c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
340224f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
34032648efa6SDaniel Eischen
34040c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
340524f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
34062648efa6SDaniel Eischen
34070c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
340824f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
34092648efa6SDaniel Eischen
34100c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
341124f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3412edc374e7SEd Maste "Return process table, including threads");
3413cc43c38cSRobert Watson
341443151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7
341524f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
341624f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
341743151ee6SPeter Wemm #endif
341843151ee6SPeter Wemm
341924f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
342024f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
34211cc8c45cSRobert Watson
34221cc8c45cSRobert Watson #if defined(STACK) || defined(DDB)
342324f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
342424f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
34251cc8c45cSRobert Watson #endif
3426254d03c5SBrooks Davis
3427254d03c5SBrooks Davis static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3428254d03c5SBrooks Davis CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
34299e7d0583SMikolaj Golub
34308854fe39SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
34318854fe39SMikolaj Golub CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
34328854fe39SMikolaj Golub "Process resource limits");
34339732458fSMikolaj Golub
343445efc9b4SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
343545efc9b4SMikolaj Golub CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
343645efc9b4SMikolaj Golub "Process ps_strings location");
34376ce13747SMikolaj Golub
3438e0fcf639SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3439e0fcf639SMikolaj Golub CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3440903712c9SMikolaj Golub
3441903712c9SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3442903712c9SMikolaj Golub CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3443903712c9SMikolaj Golub "Process binary osreldate");
344480c3af4eSKonstantin Belousov
344580c3af4eSKonstantin Belousov static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
344680c3af4eSKonstantin Belousov CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
344780c3af4eSKonstantin Belousov "Process signal trampoline location");
34486ddcc233SKonstantin Belousov
344948fcb463SKonstantin Belousov static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
345048fcb463SKonstantin Belousov CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
345148fcb463SKonstantin Belousov "Thread sigfastblock address");
345248fcb463SKonstantin Belousov
34533ce04acaSMark Johnston static SYSCTL_NODE(_kern_proc, KERN_PROC_VM_LAYOUT, vm_layout, CTLFLAG_RD |
34543ce04acaSMark Johnston CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_vm_layout,
34553ce04acaSMark Johnston "Process virtual address space layout info");
34563ce04acaSMark Johnston
34572e7595efSKonstantin Belousov static struct sx stop_all_proc_blocker;
34582e7595efSKonstantin Belousov SX_SYSINIT(stop_all_proc_blocker, &stop_all_proc_blocker, "sapblk");
34592e7595efSKonstantin Belousov
3460008b2e65SKonstantin Belousov bool
stop_all_proc_block(void)34612e7595efSKonstantin Belousov stop_all_proc_block(void)
34622e7595efSKonstantin Belousov {
3463008b2e65SKonstantin Belousov return (sx_xlock_sig(&stop_all_proc_blocker) == 0);
34642e7595efSKonstantin Belousov }
34652e7595efSKonstantin Belousov
34662e7595efSKonstantin Belousov void
stop_all_proc_unblock(void)34672e7595efSKonstantin Belousov stop_all_proc_unblock(void)
34682e7595efSKonstantin Belousov {
34692e7595efSKonstantin Belousov sx_xunlock(&stop_all_proc_blocker);
34702e7595efSKonstantin Belousov }
34712e7595efSKonstantin Belousov
34726ddcc233SKonstantin Belousov int allproc_gen;
34736ddcc233SKonstantin Belousov
34742dab579bSKonstantin Belousov /*
34752dab579bSKonstantin Belousov * stop_all_proc() purpose is to stop all process which have usermode,
34762dab579bSKonstantin Belousov * except current process for obvious reasons. This makes it somewhat
34772dab579bSKonstantin Belousov * unreliable when invoked from multithreaded process. The service
34782dab579bSKonstantin Belousov * must not be user-callable anyway.
34792dab579bSKonstantin Belousov */
34806ddcc233SKonstantin Belousov void
stop_all_proc(void)34816ddcc233SKonstantin Belousov stop_all_proc(void)
34826ddcc233SKonstantin Belousov {
34836ddcc233SKonstantin Belousov struct proc *cp, *p;
34846ddcc233SKonstantin Belousov int r, gen;
34856ddcc233SKonstantin Belousov bool restart, seen_stopped, seen_exiting, stopped_some;
34866ddcc233SKonstantin Belousov
3487008b2e65SKonstantin Belousov if (!stop_all_proc_block())
3488008b2e65SKonstantin Belousov return;
34892e7595efSKonstantin Belousov
34906ddcc233SKonstantin Belousov cp = curproc;
34916ddcc233SKonstantin Belousov allproc_loop:
34926ddcc233SKonstantin Belousov sx_xlock(&allproc_lock);
34936ddcc233SKonstantin Belousov gen = allproc_gen;
34946ddcc233SKonstantin Belousov seen_exiting = seen_stopped = stopped_some = restart = false;
34956ddcc233SKonstantin Belousov LIST_REMOVE(cp, p_list);
34966ddcc233SKonstantin Belousov LIST_INSERT_HEAD(&allproc, cp, p_list);
34976ddcc233SKonstantin Belousov for (;;) {
34986ddcc233SKonstantin Belousov p = LIST_NEXT(cp, p_list);
34996ddcc233SKonstantin Belousov if (p == NULL)
35006ddcc233SKonstantin Belousov break;
35016ddcc233SKonstantin Belousov LIST_REMOVE(cp, p_list);
35026ddcc233SKonstantin Belousov LIST_INSERT_AFTER(p, cp, p_list);
35036ddcc233SKonstantin Belousov PROC_LOCK(p);
3504235436d6SKonstantin Belousov if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP |
3505235436d6SKonstantin Belousov P_STOPPED_SIG)) != 0) {
35066ddcc233SKonstantin Belousov PROC_UNLOCK(p);
35076ddcc233SKonstantin Belousov continue;
35086ddcc233SKonstantin Belousov }
3509d3000939SKonstantin Belousov if ((p->p_flag2 & P2_WEXIT) != 0) {
35106ddcc233SKonstantin Belousov seen_exiting = true;
35116ddcc233SKonstantin Belousov PROC_UNLOCK(p);
35126ddcc233SKonstantin Belousov continue;
35136ddcc233SKonstantin Belousov }
35146ddcc233SKonstantin Belousov if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
35156ddcc233SKonstantin Belousov /*
35166ddcc233SKonstantin Belousov * Stopped processes are tolerated when there
35176ddcc233SKonstantin Belousov * are no other processes which might continue
35186ddcc233SKonstantin Belousov * them. P_STOPPED_SINGLE but not
35196ddcc233SKonstantin Belousov * P_TOTAL_STOP process still has at least one
35206ddcc233SKonstantin Belousov * thread running.
35216ddcc233SKonstantin Belousov */
35226ddcc233SKonstantin Belousov seen_stopped = true;
35236ddcc233SKonstantin Belousov PROC_UNLOCK(p);
35246ddcc233SKonstantin Belousov continue;
35256ddcc233SKonstantin Belousov }
3526235436d6SKonstantin Belousov if ((p->p_flag & P_TRACED) != 0) {
3527235436d6SKonstantin Belousov /*
3528235436d6SKonstantin Belousov * thread_single() below cannot stop traced p,
3529235436d6SKonstantin Belousov * so skip it. OTOH, we cannot require
3530235436d6SKonstantin Belousov * restart because debugger might be either
3531235436d6SKonstantin Belousov * already stopped or traced as well.
3532235436d6SKonstantin Belousov */
3533235436d6SKonstantin Belousov PROC_UNLOCK(p);
3534235436d6SKonstantin Belousov continue;
3535235436d6SKonstantin Belousov }
35366ddcc233SKonstantin Belousov sx_xunlock(&allproc_lock);
3537f02bc51cSKonstantin Belousov _PHOLD(p);
35386ddcc233SKonstantin Belousov r = thread_single(p, SINGLE_ALLPROC);
35396ddcc233SKonstantin Belousov if (r != 0)
35406ddcc233SKonstantin Belousov restart = true;
35416ddcc233SKonstantin Belousov else
35426ddcc233SKonstantin Belousov stopped_some = true;
35436ddcc233SKonstantin Belousov _PRELE(p);
35446ddcc233SKonstantin Belousov PROC_UNLOCK(p);
35456ddcc233SKonstantin Belousov sx_xlock(&allproc_lock);
35466ddcc233SKonstantin Belousov }
35476ddcc233SKonstantin Belousov /* Catch forked children we did not see in iteration. */
35486ddcc233SKonstantin Belousov if (gen != allproc_gen)
35496ddcc233SKonstantin Belousov restart = true;
35506ddcc233SKonstantin Belousov sx_xunlock(&allproc_lock);
35516ddcc233SKonstantin Belousov if (restart || stopped_some || seen_exiting || seen_stopped) {
35526ddcc233SKonstantin Belousov kern_yield(PRI_USER);
35536ddcc233SKonstantin Belousov goto allproc_loop;
35546ddcc233SKonstantin Belousov }
35556ddcc233SKonstantin Belousov }
35566ddcc233SKonstantin Belousov
35576ddcc233SKonstantin Belousov void
resume_all_proc(void)35586ddcc233SKonstantin Belousov resume_all_proc(void)
35596ddcc233SKonstantin Belousov {
35606ddcc233SKonstantin Belousov struct proc *cp, *p;
35616ddcc233SKonstantin Belousov
35626ddcc233SKonstantin Belousov cp = curproc;
35636ddcc233SKonstantin Belousov sx_xlock(&allproc_lock);
3564baaa7969SKonstantin Belousov again:
35656ddcc233SKonstantin Belousov LIST_REMOVE(cp, p_list);
35666ddcc233SKonstantin Belousov LIST_INSERT_HEAD(&allproc, cp, p_list);
35676ddcc233SKonstantin Belousov for (;;) {
35686ddcc233SKonstantin Belousov p = LIST_NEXT(cp, p_list);
35696ddcc233SKonstantin Belousov if (p == NULL)
35706ddcc233SKonstantin Belousov break;
35716ddcc233SKonstantin Belousov LIST_REMOVE(cp, p_list);
35726ddcc233SKonstantin Belousov LIST_INSERT_AFTER(p, cp, p_list);
35736ddcc233SKonstantin Belousov PROC_LOCK(p);
35746ddcc233SKonstantin Belousov if ((p->p_flag & P_TOTAL_STOP) != 0) {
35756ddcc233SKonstantin Belousov sx_xunlock(&allproc_lock);
35766ddcc233SKonstantin Belousov _PHOLD(p);
35776ddcc233SKonstantin Belousov thread_single_end(p, SINGLE_ALLPROC);
35786ddcc233SKonstantin Belousov _PRELE(p);
35796ddcc233SKonstantin Belousov PROC_UNLOCK(p);
35806ddcc233SKonstantin Belousov sx_xlock(&allproc_lock);
35816ddcc233SKonstantin Belousov } else {
35826ddcc233SKonstantin Belousov PROC_UNLOCK(p);
35836ddcc233SKonstantin Belousov }
35846ddcc233SKonstantin Belousov }
3585baaa7969SKonstantin Belousov /* Did the loop above missed any stopped process ? */
35867938a442SBjoern A. Zeeb FOREACH_PROC_IN_SYSTEM(p) {
3587baaa7969SKonstantin Belousov /* No need for proc lock. */
3588baaa7969SKonstantin Belousov if ((p->p_flag & P_TOTAL_STOP) != 0)
3589baaa7969SKonstantin Belousov goto again;
3590baaa7969SKonstantin Belousov }
35916ddcc233SKonstantin Belousov sx_xunlock(&allproc_lock);
35922e7595efSKonstantin Belousov
35932e7595efSKonstantin Belousov stop_all_proc_unblock();
35946ddcc233SKonstantin Belousov }
35956ddcc233SKonstantin Belousov
35962dab579bSKonstantin Belousov /* #define TOTAL_STOP_DEBUG 1 */
35976ddcc233SKonstantin Belousov #ifdef TOTAL_STOP_DEBUG
35986ddcc233SKonstantin Belousov volatile static int ap_resume;
35996ddcc233SKonstantin Belousov #include <sys/mount.h>
36006ddcc233SKonstantin Belousov
36016ddcc233SKonstantin Belousov static int
sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)36026ddcc233SKonstantin Belousov sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
36036ddcc233SKonstantin Belousov {
36046ddcc233SKonstantin Belousov int error, val;
36056ddcc233SKonstantin Belousov
36066ddcc233SKonstantin Belousov val = 0;
36076ddcc233SKonstantin Belousov ap_resume = 0;
36086ddcc233SKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req);
36096ddcc233SKonstantin Belousov if (error != 0 || req->newptr == NULL)
36106ddcc233SKonstantin Belousov return (error);
36116ddcc233SKonstantin Belousov if (val != 0) {
36126ddcc233SKonstantin Belousov stop_all_proc();
36136ddcc233SKonstantin Belousov syncer_suspend();
36146ddcc233SKonstantin Belousov while (ap_resume == 0)
36156ddcc233SKonstantin Belousov ;
36166ddcc233SKonstantin Belousov syncer_resume();
36176ddcc233SKonstantin Belousov resume_all_proc();
36186ddcc233SKonstantin Belousov }
36196ddcc233SKonstantin Belousov return (0);
36206ddcc233SKonstantin Belousov }
36216ddcc233SKonstantin Belousov
36226ddcc233SKonstantin Belousov SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
362383d7eb54SKonstantin Belousov CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
362483d7eb54SKonstantin Belousov sysctl_debug_stop_all_proc, "I",
36256ddcc233SKonstantin Belousov "");
36266ddcc233SKonstantin Belousov #endif
3627