2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_ktrace.h"
37 #include "opt_kstack_pages.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/sysent.h>
47 #include <sys/sched.h>
49 #include <sys/sysctl.h>
50 #include <sys/filedesc.h>
52 #include <sys/signalvar.h>
58 #include <sys/ktrace.h>
62 #include <vm/vm_extern.h>
64 #include <vm/vm_map.h>
66 #include <machine/critical.h>
68 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
69 MALLOC_DEFINE(M_SESSION, "session", "session header");
70 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
71 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
73 static void doenterpgrp(struct proc *, struct pgrp *);
74 static void orphanpg(struct pgrp *pg);
75 static void pgadjustjobc(struct pgrp *pgrp, int entering);
76 static void pgdelete(struct pgrp *);
77 static int proc_ctor(void *mem, int size, void *arg, int flags);
78 static void proc_dtor(void *mem, int size, void *arg);
79 static int proc_init(void *mem, int size, int flags);
80 static void proc_fini(void *mem, int size);
85 struct pidhashhead *pidhashtbl;
87 struct pgrphashhead *pgrphashtbl;
89 struct proclist allproc;
90 struct proclist zombproc;
91 struct sx allproc_lock;
92 struct sx proctree_lock;
93 struct mtx pargs_ref_lock;
94 struct mtx ppeers_lock;
96 uma_zone_t ithread_zone;
98 int kstack_pages = KSTACK_PAGES;
99 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
101 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
104 * Initialize global process hashing structures.
110 sx_init(&allproc_lock, "allproc");
111 sx_init(&proctree_lock, "proctree");
112 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF);
113 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
115 LIST_INIT(&zombproc);
116 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
117 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
118 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
119 proc_ctor, proc_dtor, proc_init, proc_fini,
120 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
125 * Prepare a proc for use.
128 proc_ctor(void *mem, int size, void *arg, int flags)
132 p = (struct proc *)mem;
137 * Reclaim a proc after use.
140 proc_dtor(void *mem, int size, void *arg)
148 /* INVARIANTS checks go here */
149 p = (struct proc *)mem;
150 td = FIRST_THREAD_IN_PROC(p);
152 KASSERT((p->p_numthreads == 1),
153 ("bad number of threads in exiting process"));
154 KASSERT((p->p_numksegrps == 1), ("free proc with > 1 ksegrp"));
155 KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
156 kg = FIRST_KSEGRP_IN_PROC(p);
157 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
160 /* Dispose of an alternate kstack, if it exists.
161 * XXX What if there are more than one thread in the proc?
162 * The first thread in the proc is special and not
163 * freed, so you gotta do this here.
165 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
166 vm_thread_dispose_altkstack(td);
170 * Initialize type-stable parts of a proc (when newly created).
173 proc_init(void *mem, int size, int flags)
179 p = (struct proc *)mem;
180 p->p_sched = (struct p_sched *)&p[1];
183 bzero(&p->p_mtx, sizeof(struct mtx));
184 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
185 p->p_stats = pstats_alloc();
186 proc_linkup(p, kg, td);
187 sched_newproc(p, kg, td);
192 * UMA should ensure that this function is never called.
193 * Freeing a proc structure would violate type stability.
196 proc_fini(void *mem, int size)
199 panic("proc reclaimed");
203 * Is p an inferior of the current process?
207 register struct proc *p;
210 sx_assert(&proctree_lock, SX_LOCKED);
211 for (; p != curproc; p = p->p_pptr)
218 * Locate a process by number; return only "live" processes -- i.e., neither
219 * zombies nor newly born but incompletely initialized processes. By not
220 * returning processes in the PRS_NEW state, we allow callers to avoid
221 * testing for that condition to avoid dereferencing p_ucred, et al.
227 register struct proc *p;
229 sx_slock(&allproc_lock);
230 LIST_FOREACH(p, PIDHASH(pid), p_hash)
231 if (p->p_pid == pid) {
232 if (p->p_state == PRS_NEW) {
239 sx_sunlock(&allproc_lock);
244 * Locate a process group by number.
245 * The caller must hold proctree_lock.
251 register struct pgrp *pgrp;
253 sx_assert(&proctree_lock, SX_LOCKED);
255 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
256 if (pgrp->pg_id == pgid) {
265 * Create a new process group.
266 * pgid must be equal to the pid of p.
267 * Begin a new session if required.
270 enterpgrp(p, pgid, pgrp, sess)
271 register struct proc *p;
274 struct session *sess;
278 sx_assert(&proctree_lock, SX_XLOCKED);
280 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
281 KASSERT(p->p_pid == pgid,
282 ("enterpgrp: new pgrp and pid != pgid"));
284 pgrp2 = pgfind(pgid);
286 KASSERT(pgrp2 == NULL,
287 ("enterpgrp: pgrp with pgid exists"));
288 KASSERT(!SESS_LEADER(p),
289 ("enterpgrp: session leader attempted setpgrp"));
291 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
297 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
299 p->p_flag &= ~P_CONTROLT;
303 sess->s_sid = p->p_pid;
305 sess->s_ttyvp = NULL;
307 bcopy(p->p_session->s_login, sess->s_login,
308 sizeof(sess->s_login));
309 pgrp->pg_session = sess;
310 KASSERT(p == curproc,
311 ("enterpgrp: mksession and p != curproc"));
313 pgrp->pg_session = p->p_session;
314 SESS_LOCK(pgrp->pg_session);
315 pgrp->pg_session->s_count++;
316 SESS_UNLOCK(pgrp->pg_session);
320 LIST_INIT(&pgrp->pg_members);
323 * As we have an exclusive lock of proctree_lock,
324 * this should not deadlock.
326 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
328 SLIST_INIT(&pgrp->pg_sigiolst);
331 doenterpgrp(p, pgrp);
337 * Move p to an existing process group
340 enterthispgrp(p, pgrp)
341 register struct proc *p;
345 sx_assert(&proctree_lock, SX_XLOCKED);
346 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
347 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
348 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
349 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
350 KASSERT(pgrp->pg_session == p->p_session,
351 ("%s: pgrp's session %p, p->p_session %p.\n",
355 KASSERT(pgrp != p->p_pgrp,
356 ("%s: p belongs to pgrp.", __func__));
358 doenterpgrp(p, pgrp);
364 * Move p to a process group
371 struct pgrp *savepgrp;
373 sx_assert(&proctree_lock, SX_XLOCKED);
374 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
375 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
376 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
377 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
379 savepgrp = p->p_pgrp;
382 * Adjust eligibility of affected pgrps to participate in job control.
383 * Increment eligibility counts before decrementing, otherwise we
384 * could reach 0 spuriously during the first call.
387 fixjobc(p, p->p_pgrp, 0);
392 LIST_REMOVE(p, p_pglist);
395 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
396 PGRP_UNLOCK(savepgrp);
398 if (LIST_EMPTY(&savepgrp->pg_members))
403 * remove process from process group
407 register struct proc *p;
409 struct pgrp *savepgrp;
411 sx_assert(&proctree_lock, SX_XLOCKED);
412 savepgrp = p->p_pgrp;
415 LIST_REMOVE(p, p_pglist);
418 PGRP_UNLOCK(savepgrp);
419 if (LIST_EMPTY(&savepgrp->pg_members))
425 * delete a process group
429 register struct pgrp *pgrp;
431 struct session *savesess;
434 sx_assert(&proctree_lock, SX_XLOCKED);
435 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
436 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
439 * Reset any sigio structures pointing to us as a result of
440 * F_SETOWN with our pgid.
442 funsetownlst(&pgrp->pg_sigiolst);
445 if (pgrp->pg_session->s_ttyp != NULL &&
446 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
447 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
448 LIST_REMOVE(pgrp, pg_hash);
449 savesess = pgrp->pg_session;
451 i = --savesess->s_count;
452 SESS_UNLOCK(savesess);
455 if (savesess->s_ttyp != NULL)
456 ttyrel(savesess->s_ttyp);
457 mtx_destroy(&savesess->s_mtx);
458 FREE(savesess, M_SESSION);
460 mtx_destroy(&pgrp->pg_mtx);
465 pgadjustjobc(pgrp, entering)
475 if (pgrp->pg_jobc == 0)
482 * Adjust pgrp jobc counters when specified process changes process group.
483 * We count the number of processes in each process group that "qualify"
484 * the group for terminal job control (those with a parent in a different
485 * process group of the same session). If that count reaches zero, the
486 * process group becomes orphaned. Check both the specified process'
487 * process group and that of its children.
488 * entering == 0 => p is leaving specified group.
489 * entering == 1 => p is entering specified group.
492 fixjobc(p, pgrp, entering)
493 register struct proc *p;
494 register struct pgrp *pgrp;
497 register struct pgrp *hispgrp;
498 register struct session *mysession;
500 sx_assert(&proctree_lock, SX_LOCKED);
501 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
502 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
503 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
506 * Check p's parent to see whether p qualifies its own process
507 * group; if so, adjust count for p's process group.
509 mysession = pgrp->pg_session;
510 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
511 hispgrp->pg_session == mysession)
512 pgadjustjobc(pgrp, entering);
515 * Check this process' children to see whether they qualify
516 * their process groups; if so, adjust counts for children's
519 LIST_FOREACH(p, &p->p_children, p_sibling) {
521 if (hispgrp == pgrp ||
522 hispgrp->pg_session != mysession)
525 if (p->p_state == PRS_ZOMBIE) {
530 pgadjustjobc(hispgrp, entering);
535 * A process group has become orphaned;
536 * if there are any stopped processes in the group,
537 * hang-up all process in that group.
543 register struct proc *p;
545 PGRP_LOCK_ASSERT(pg, MA_OWNED);
547 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
549 if (P_SHOULDSTOP(p)) {
551 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
567 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
569 register struct pgrp *pgrp;
570 register struct proc *p;
573 for (i = 0; i <= pgrphash; i++) {
574 if (!LIST_EMPTY(&pgrphashtbl[i])) {
575 printf("\tindx %d\n", i);
576 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
578 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
579 (void *)pgrp, (long)pgrp->pg_id,
580 (void *)pgrp->pg_session,
581 pgrp->pg_session->s_count,
582 (void *)LIST_FIRST(&pgrp->pg_members));
583 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
584 printf("\t\tpid %ld addr %p pgrp %p\n",
585 (long)p->p_pid, (void *)p,
594 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp);
597 * Fill in a kinfo_proc structure for the specified process.
598 * Must be called with the target process locked.
601 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
603 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
607 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp)
620 bzero(kp, sizeof(*kp));
622 kp->ki_structsize = sizeof(*kp);
624 PROC_LOCK_ASSERT(p, MA_OWNED);
625 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
626 kp->ki_args = p->p_args;
627 kp->ki_textvp = p->p_textvp;
629 kp->ki_tracep = p->p_tracevp;
630 mtx_lock(&ktrace_mtx);
631 kp->ki_traceflag = p->p_traceflag;
632 mtx_unlock(&ktrace_mtx);
635 kp->ki_vmspace = p->p_vmspace;
636 kp->ki_flag = p->p_flag;
639 kp->ki_uid = cred->cr_uid;
640 kp->ki_ruid = cred->cr_ruid;
641 kp->ki_svuid = cred->cr_svuid;
642 /* XXX bde doesn't like KI_NGROUPS */
643 kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
644 bcopy(cred->cr_groups, kp->ki_groups,
645 kp->ki_ngroups * sizeof(gid_t));
646 kp->ki_rgid = cred->cr_rgid;
647 kp->ki_svgid = cred->cr_svgid;
648 /* If jailed(cred), emulate the old P_JAILED flag. */
650 kp->ki_flag |= P_JAILED;
654 mtx_lock(&ps->ps_mtx);
655 kp->ki_sigignore = ps->ps_sigignore;
656 kp->ki_sigcatch = ps->ps_sigcatch;
657 mtx_unlock(&ps->ps_mtx);
659 mtx_lock_spin(&sched_lock);
660 if (p->p_state != PRS_NEW &&
661 p->p_state != PRS_ZOMBIE &&
662 p->p_vmspace != NULL) {
663 struct vmspace *vm = p->p_vmspace;
665 kp->ki_size = vm->vm_map.size;
666 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
667 FOREACH_THREAD_IN_PROC(p, td0) {
668 if (!TD_IS_SWAPPED(td0))
669 kp->ki_rssize += td0->td_kstack_pages;
670 if (td0->td_altkstack_obj != NULL)
671 kp->ki_rssize += td0->td_altkstack_pages;
673 kp->ki_swrss = vm->vm_swrss;
674 kp->ki_tsize = vm->vm_tsize;
675 kp->ki_dsize = vm->vm_dsize;
676 kp->ki_ssize = vm->vm_ssize;
678 kp->ki_sflag = p->p_sflag;
679 kp->ki_swtime = p->p_swtime;
680 kp->ki_pid = p->p_pid;
681 kp->ki_nice = p->p_nice;
682 bintime2timeval(&p->p_rux.rux_runtime, &tv);
683 kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
684 if (p->p_state != PRS_ZOMBIE) {
687 /* XXXKSE: This should never happen. */
688 printf("fill_kinfo_proc(): pid %d has no threads!\n",
690 mtx_unlock_spin(&sched_lock);
694 if (td->td_wmesg != NULL) {
695 strlcpy(kp->ki_wmesg, td->td_wmesg,
696 sizeof(kp->ki_wmesg));
698 if (TD_ON_LOCK(td)) {
699 kp->ki_kiflag |= KI_LOCKBLOCK;
700 strlcpy(kp->ki_lockname, td->td_lockname,
701 sizeof(kp->ki_lockname));
704 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */
705 if (TD_ON_RUNQ(td) ||
709 } else if (P_SHOULDSTOP(p)) {
711 } else if (TD_IS_SLEEPING(td)) {
712 kp->ki_stat = SSLEEP;
713 } else if (TD_ON_LOCK(td)) {
724 /* things in the KSE GROUP */
725 kp->ki_estcpu = kg->kg_estcpu;
726 kp->ki_slptime = kg->kg_slptime;
727 kp->ki_pri.pri_user = kg->kg_user_pri;
728 kp->ki_pri.pri_class = kg->kg_pri_class;
730 /* Things in the thread */
731 kp->ki_wchan = td->td_wchan;
732 kp->ki_pri.pri_level = td->td_priority;
733 kp->ki_pri.pri_native = td->td_base_pri;
734 kp->ki_lastcpu = td->td_lastcpu;
735 kp->ki_oncpu = td->td_oncpu;
736 kp->ki_tdflags = td->td_flags;
737 kp->ki_tid = td->td_tid;
738 kp->ki_numthreads = p->p_numthreads;
739 kp->ki_pcb = td->td_pcb;
740 kp->ki_kstack = (void *)td->td_kstack;
741 kp->ki_pctcpu = sched_pctcpu(td);
743 /* We can't get this anymore but ps etc never used it anyway. */
749 mtx_unlock_spin(&sched_lock);
750 if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) {
751 kp->ki_start = p->p_stats->p_start;
752 timevaladd(&kp->ki_start, &boottime);
753 kp->ki_rusage = p->p_stats->p_ru;
754 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
755 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
757 /* Some callers want child-times in a single value */
758 kp->ki_childtime = kp->ki_childstime;
759 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
763 kp->ki_pgid = p->p_pgrp->pg_id;
764 kp->ki_jobc = p->p_pgrp->pg_jobc;
765 sp = p->p_pgrp->pg_session;
768 kp->ki_sid = sp->s_sid;
770 strlcpy(kp->ki_login, sp->s_login,
771 sizeof(kp->ki_login));
773 kp->ki_kiflag |= KI_CTTY;
775 kp->ki_kiflag |= KI_SLEADER;
780 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
781 kp->ki_tdev = dev2udev(tp->t_dev);
782 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
784 kp->ki_tsid = tp->t_session->s_sid;
787 if (p->p_comm[0] != '\0') {
788 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
789 strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
791 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
792 p->p_sysent->sv_name[0] != '\0')
793 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
794 kp->ki_siglist = p->p_siglist;
795 SIGSETOR(kp->ki_siglist, td->td_siglist);
796 kp->ki_sigmask = td->td_sigmask;
797 kp->ki_xstat = p->p_xstat;
798 kp->ki_acflag = p->p_acflag;
799 kp->ki_lock = p->p_lock;
801 kp->ki_ppid = p->p_pptr->p_pid;
808 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
812 * Copy parts of p_stats; zero the rest of p_stats (statistics).
815 pstats_fork(struct pstats *src, struct pstats *dst)
818 bzero(&dst->pstat_startzero,
819 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
820 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
821 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
825 pstats_free(struct pstats *ps)
832 * Locate a zombie process by number
839 sx_slock(&allproc_lock);
840 LIST_FOREACH(p, &zombproc, p_list)
841 if (p->p_pid == pid) {
845 sx_sunlock(&allproc_lock);
849 #define KERN_PROC_ZOMBMASK 0x3
850 #define KERN_PROC_NOTHREADS 0x4
853 * Must be called with the process locked and will return with it unlocked.
856 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
859 struct kinfo_proc kinfo_proc;
862 pid_t pid = p->p_pid;
864 PROC_LOCK_ASSERT(p, MA_OWNED);
866 if (flags & KERN_PROC_NOTHREADS) {
867 fill_kinfo_proc(p, &kinfo_proc);
869 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
874 FOREACH_THREAD_IN_PROC(p, td) {
875 fill_kinfo_thread(td, &kinfo_proc);
877 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
888 if (flags & KERN_PROC_ZOMBMASK)
906 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
908 int *name = (int*) arg1;
909 u_int namelen = arg2;
911 int flags, doingzomb, oid_number;
914 oid_number = oidp->oid_number;
915 if (oid_number != KERN_PROC_ALL &&
916 (oid_number & KERN_PROC_INC_THREAD) == 0)
917 flags = KERN_PROC_NOTHREADS;
920 oid_number &= ~KERN_PROC_INC_THREAD;
922 if (oid_number == KERN_PROC_PID) {
925 p = pfind((pid_t)name[0]);
928 if ((error = p_cansee(curthread, p))) {
932 error = sysctl_out_proc(p, req, flags);
936 switch (oid_number) {
942 if (namelen != 0 && namelen != 1)
952 /* overestimate by 5 procs */
953 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
957 error = sysctl_wire_old_buffer(req, 0);
960 sx_slock(&allproc_lock);
961 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
963 p = LIST_FIRST(&allproc);
965 p = LIST_FIRST(&zombproc);
966 for (; p != 0; p = LIST_NEXT(p, p_list)) {
968 * Skip embryonic processes.
970 mtx_lock_spin(&sched_lock);
971 if (p->p_state == PRS_NEW) {
972 mtx_unlock_spin(&sched_lock);
975 mtx_unlock_spin(&sched_lock);
978 * Show a user only appropriate processes.
980 if (p_cansee(curthread, p)) {
985 * TODO - make more efficient (see notes below).
988 switch (oid_number) {
991 if (p->p_ucred == NULL ||
992 p->p_ucred->cr_gid != (gid_t)name[0]) {
999 /* could do this by traversing pgrp */
1000 if (p->p_pgrp == NULL ||
1001 p->p_pgrp->pg_id != (pid_t)name[0]) {
1007 case KERN_PROC_RGID:
1008 if (p->p_ucred == NULL ||
1009 p->p_ucred->cr_rgid != (gid_t)name[0]) {
1015 case KERN_PROC_SESSION:
1016 if (p->p_session == NULL ||
1017 p->p_session->s_sid != (pid_t)name[0]) {
1024 if ((p->p_flag & P_CONTROLT) == 0 ||
1025 p->p_session == NULL) {
1029 SESS_LOCK(p->p_session);
1030 if (p->p_session->s_ttyp == NULL ||
1031 dev2udev(p->p_session->s_ttyp->t_dev) !=
1033 SESS_UNLOCK(p->p_session);
1037 SESS_UNLOCK(p->p_session);
1041 if (p->p_ucred == NULL ||
1042 p->p_ucred->cr_uid != (uid_t)name[0]) {
1048 case KERN_PROC_RUID:
1049 if (p->p_ucred == NULL ||
1050 p->p_ucred->cr_ruid != (uid_t)name[0]) {
1056 case KERN_PROC_PROC:
1064 error = sysctl_out_proc(p, req, flags | doingzomb);
1066 sx_sunlock(&allproc_lock);
1071 sx_sunlock(&allproc_lock);
1076 pargs_alloc(int len)
1080 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
1083 pa->ar_length = len;
1088 pargs_free(struct pargs *pa)
1095 pargs_hold(struct pargs *pa)
1106 pargs_drop(struct pargs *pa)
1112 if (--pa->ar_ref == 0) {
1120 * This sysctl allows a process to retrieve the argument list or process
1121 * title for another process without groping around in the address space
1122 * of the other process. It also allow a process to set its own "process
1123 * title to a string of its own choice.
1126 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1128 int *name = (int*) arg1;
1129 u_int namelen = arg2;
1130 struct pargs *newpa, *pa;
1137 p = pfind((pid_t)name[0]);
1141 if ((error = p_cansee(curthread, p)) != 0) {
1146 if (req->newptr && curproc != p) {
1154 if (req->oldptr != NULL && pa != NULL)
1155 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1157 if (error != 0 || req->newptr == NULL)
1160 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1162 newpa = pargs_alloc(req->newlen);
1163 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1177 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1190 if ((p = pfind((pid_t)name[0])) == NULL)
1192 if ((error = p_cansee(curthread, p))) {
1196 sv_name = p->p_sysent->sv_name;
1198 return (sysctl_handle_string(oidp, sv_name, 0, req));
1202 static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1204 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1205 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1207 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
1208 sysctl_kern_proc, "Process table");
1210 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1211 sysctl_kern_proc, "Process table");
1213 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
1214 sysctl_kern_proc, "Process table");
1216 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
1217 sysctl_kern_proc, "Process table");
1219 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1220 sysctl_kern_proc, "Process table");
1222 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1223 sysctl_kern_proc, "Process table");
1225 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1226 sysctl_kern_proc, "Process table");
1228 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1229 sysctl_kern_proc, "Process table");
1231 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
1232 sysctl_kern_proc, "Return process table, no threads");
1234 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
1235 CTLFLAG_RW | CTLFLAG_ANYBODY,
1236 sysctl_kern_proc_args, "Process argument list");
1238 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
1239 sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
1241 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
1242 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1244 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
1245 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1247 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
1248 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1250 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
1251 sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table");
1253 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
1254 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1256 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
1257 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1259 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
1260 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1262 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
1263 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1265 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
1266 CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");