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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
34 * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $
35 * $DragonFly: src/sys/kern/kern_proc.c,v 1.18 2005/02/01 02:25:45 joerg Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
45 #include <sys/filedesc.h>
47 #include <sys/signalvar.h>
51 #include <vm/vm_map.h>
53 #include <vm/vm_zone.h>
54 #include <machine/smp.h>
56 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
57 MALLOC_DEFINE(M_SESSION, "session", "session header");
58 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
59 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
61 int ps_showallprocs = 1;
62 static int ps_showallthreads = 1;
63 SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
64 &ps_showallprocs, 0, "");
65 SYSCTL_INT(_kern, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
66 &ps_showallthreads, 0, "");
68 static void pgdelete (struct pgrp *);
70 static void orphanpg (struct pgrp *pg);
75 struct pidhashhead *pidhashtbl;
77 struct pgrphashhead *pgrphashtbl;
79 struct proclist allproc;
80 struct proclist zombproc;
82 vm_zone_t thread_zone;
85 * Initialize global process hashing structures.
93 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
94 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
95 proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
96 thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5);
101 * Is p an inferior of the current process?
108 for (; p != curproc; p = p->p_pptr)
115 * Locate a process by number
123 LIST_FOREACH(p, PIDHASH(pid), p_hash)
130 * Locate a process group by number
138 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
139 if (pgrp->pg_id == pgid)
145 * Move p to a new or existing process group (and session)
148 enterpgrp(p, pgid, mksess)
153 struct pgrp *pgrp = pgfind(pgid);
155 KASSERT(pgrp == NULL || !mksess,
156 ("enterpgrp: setsid into non-empty pgrp"));
157 KASSERT(!SESS_LEADER(p),
158 ("enterpgrp: session leader attempted setpgrp"));
161 pid_t savepid = p->p_pid;
166 KASSERT(p->p_pid == pgid,
167 ("enterpgrp: new pgrp and pid != pgid"));
168 if ((np = pfind(savepid)) == NULL || np != p)
170 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
173 struct session *sess;
178 MALLOC(sess, struct session *, sizeof(struct session),
179 M_SESSION, M_WAITOK);
181 sess->s_sid = p->p_pid;
183 sess->s_ttyvp = NULL;
185 bcopy(p->p_session->s_login, sess->s_login,
186 sizeof(sess->s_login));
187 p->p_flag &= ~P_CONTROLT;
188 pgrp->pg_session = sess;
189 KASSERT(p == curproc,
190 ("enterpgrp: mksession and p != curproc"));
192 pgrp->pg_session = p->p_session;
193 sess_hold(pgrp->pg_session);
196 LIST_INIT(&pgrp->pg_members);
197 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
199 SLIST_INIT(&pgrp->pg_sigiolst);
200 } else if (pgrp == p->p_pgrp)
204 * Adjust eligibility of affected pgrps to participate in job control.
205 * Increment eligibility counts before decrementing, otherwise we
206 * could reach 0 spuriously during the first call.
209 fixjobc(p, p->p_pgrp, 0);
211 LIST_REMOVE(p, p_pglist);
212 if (LIST_EMPTY(&p->p_pgrp->pg_members))
215 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
220 * remove process from process group
227 LIST_REMOVE(p, p_pglist);
228 if (LIST_EMPTY(&p->p_pgrp->pg_members))
235 * delete a process group
243 * Reset any sigio structures pointing to us as a result of
244 * F_SETOWN with our pgid.
246 funsetownlst(&pgrp->pg_sigiolst);
248 if (pgrp->pg_session->s_ttyp != NULL &&
249 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
250 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
251 LIST_REMOVE(pgrp, pg_hash);
252 sess_rele(pgrp->pg_session);
257 * Adjust the ref count on a session structure. When the ref count falls to
258 * zero the tty is disassociated from the session and the session structure
259 * is freed. Note that tty assocation is not itself ref-counted.
262 sess_hold(struct session *sp)
268 sess_rele(struct session *sp)
270 KKASSERT(sp->s_count > 0);
271 if (--sp->s_count == 0) {
272 if (sp->s_ttyp && sp->s_ttyp->t_session) {
273 #ifdef TTY_DO_FULL_CLOSE
274 /* FULL CLOSE, see ttyclearsession() */
275 KKASSERT(sp->s_ttyp->t_session == sp);
276 sp->s_ttyp->t_session = NULL;
278 /* HALF CLOSE, see ttyclearsession() */
279 if (sp->s_ttyp->t_session == sp)
280 sp->s_ttyp->t_session = NULL;
288 * Adjust pgrp jobc counters when specified process changes process group.
289 * We count the number of processes in each process group that "qualify"
290 * the group for terminal job control (those with a parent in a different
291 * process group of the same session). If that count reaches zero, the
292 * process group becomes orphaned. Check both the specified process'
293 * process group and that of its children.
294 * entering == 0 => p is leaving specified group.
295 * entering == 1 => p is entering specified group.
298 fixjobc(p, pgrp, entering)
303 struct pgrp *hispgrp;
304 struct session *mysession = pgrp->pg_session;
307 * Check p's parent to see whether p qualifies its own process
308 * group; if so, adjust count for p's process group.
310 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
311 hispgrp->pg_session == mysession) {
314 else if (--pgrp->pg_jobc == 0)
319 * Check this process' children to see whether they qualify
320 * their process groups; if so, adjust counts for children's
323 LIST_FOREACH(p, &p->p_children, p_sibling)
324 if ((hispgrp = p->p_pgrp) != pgrp &&
325 hispgrp->pg_session == mysession &&
326 p->p_stat != SZOMB) {
329 else if (--hispgrp->pg_jobc == 0)
335 * A process group has become orphaned;
336 * if there are any stopped processes in the group,
337 * hang-up all process in that group.
345 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
346 if (p->p_stat == SSTOP) {
347 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
360 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
366 for (i = 0; i <= pgrphash; i++) {
367 if (!LIST_EMPTY(&pgrphashtbl[i])) {
368 printf("\tindx %d\n", i);
369 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
371 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
372 (void *)pgrp, (long)pgrp->pg_id,
373 (void *)pgrp->pg_session,
374 pgrp->pg_session->s_count,
375 (void *)LIST_FIRST(&pgrp->pg_members));
376 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
377 printf("\t\tpid %ld addr %p pgrp %p\n",
378 (long)p->p_pid, (void *)p,
388 * Fill in an eproc structure for the specified thread.
391 fill_eproc_td(thread_t td, struct eproc *ep, struct proc *xp)
393 bzero(ep, sizeof(*ep));
395 ep->e_uticks = td->td_uticks;
396 ep->e_sticks = td->td_sticks;
397 ep->e_iticks = td->td_iticks;
399 ep->e_cpuid = td->td_gd->gd_cpuid;
401 strncpy(ep->e_wmesg, td->td_wmesg, WMESGLEN);
402 ep->e_wmesg[WMESGLEN] = 0;
406 * Fake up portions of the proc structure copied out by the sysctl
407 * to return useful information. Note that using td_pri directly
408 * is messy because it includes critial section data so we fake
409 * up an rtprio.prio for threads.
413 xp->p_rtprio.type = RTP_PRIO_THREAD;
414 xp->p_rtprio.prio = td->td_pri & TDPRI_MASK;
420 * Fill in an eproc structure for the specified process.
423 fill_eproc(struct proc *p, struct eproc *ep)
427 fill_eproc_td(p->p_thread, ep, NULL);
431 ep->e_ucred = *p->p_ucred;
434 ep->e_procsig = *p->p_procsig;
436 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
437 struct vmspace *vm = p->p_vmspace;
439 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
441 if ((p->p_flag & P_INMEM) && p->p_stats)
442 ep->e_stats = *p->p_stats;
444 ep->e_ppid = p->p_pptr->p_pid;
446 ep->e_pgid = p->p_pgrp->pg_id;
447 ep->e_jobc = p->p_pgrp->pg_jobc;
448 ep->e_sess = p->p_pgrp->pg_session;
451 bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
452 if (ep->e_sess->s_ttyvp)
453 ep->e_flag = EPROC_CTTY;
454 if (p->p_session && SESS_LEADER(p))
455 ep->e_flag |= EPROC_SLEADER;
458 if ((p->p_flag & P_CONTROLT) &&
459 (ep->e_sess != NULL) &&
460 ((tp = ep->e_sess->s_ttyp) != NULL)) {
461 ep->e_tdev = dev2udev(tp->t_dev);
462 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
463 ep->e_tsess = tp->t_session;
474 LIST_FOREACH(p, &zombproc, p_list)
481 sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb)
487 pid_t pid = p->p_pid;
492 fill_eproc(p, &eproc);
496 * Fixup p_stat from SRUN to SSLEEP if the LWKT thread is
497 * in a thread-blocked state.
499 * XXX temporary fix which might become permanent (I'd rather
500 * not pollute the thread scheduler with knowlege about
503 if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) {
504 xproc.p_stat = SSLEEP;
507 fill_eproc_td(td, &eproc, &xproc);
509 error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc));
512 error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
515 error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread));
519 if (!doingzomb && pid && (pfind(pid) != p))
521 if (doingzomb && zpfind(pid) != p)
528 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
530 int *name = (int*) arg1;
531 u_int namelen = arg2;
538 struct ucred *cr1 = curproc->p_ucred;
540 if (oidp->oid_number == KERN_PROC_PID) {
543 p = pfind((pid_t)name[0]);
546 if (!PRISON_CHECK(cr1, p->p_ucred))
548 error = sysctl_out_proc(p, NULL, req, 0);
551 if (oidp->oid_number == KERN_PROC_ALL && !namelen)
553 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
559 /* overestimate by 5 procs */
560 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
564 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
566 p = LIST_FIRST(&allproc);
568 p = LIST_FIRST(&zombproc);
569 for (; p != 0; p = LIST_NEXT(p, p_list)) {
571 * Show a user only their processes.
573 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
576 * Skip embryonic processes.
578 if (p->p_stat == SIDL)
581 * TODO - make more efficient (see notes below).
584 switch (oidp->oid_number) {
586 /* could do this by traversing pgrp */
587 if (p->p_pgrp == NULL ||
588 p->p_pgrp->pg_id != (pid_t)name[0])
593 if ((p->p_flag & P_CONTROLT) == 0 ||
594 p->p_session == NULL ||
595 p->p_session->s_ttyp == NULL ||
596 dev2udev(p->p_session->s_ttyp->t_dev) !=
602 if (p->p_ucred == NULL ||
603 p->p_ucred->cr_uid != (uid_t)name[0])
608 if (p->p_ucred == NULL ||
609 p->p_ucred->cr_ruid != (uid_t)name[0])
614 if (!PRISON_CHECK(cr1, p->p_ucred))
617 error = sysctl_out_proc(p, NULL, req, doingzomb);
625 * Iterate over all active cpus and scan their thread list. Start
626 * with the next logical cpu and end with our original cpu. We
627 * migrate our own thread to each target cpu in order to safely scan
628 * its thread list. In the last loop we migrate back to our original
631 origcpu = mycpu->gd_cpuid;
632 if (!ps_showallthreads || jailed(cr1))
634 for (n = 1; n <= ncpus; ++n) {
638 nid = (origcpu + n) % ncpus;
639 if ((smp_active_mask & (1 << nid)) == 0)
641 rgd = globaldata_find(nid);
642 lwkt_setcpu_self(rgd);
643 cpu_mb1(); /* CURRENT CPU HAS CHANGED */
645 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
648 switch (oidp->oid_number) {
658 error = sysctl_out_proc(NULL, td, req, doingzomb);
669 * This sysctl allows a process to retrieve the argument list or process
670 * title for another process without groping around in the address space
671 * of the other process. It also allow a process to set its own "process
672 * title to a string of its own choice.
675 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
677 int *name = (int*) arg1;
678 u_int namelen = arg2;
682 struct ucred *cr1 = curproc->p_ucred;
687 p = pfind((pid_t)name[0]);
691 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
694 if (req->newptr && curproc != p)
697 if (req->oldptr && p->p_args != NULL)
698 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
699 if (req->newptr == NULL)
702 if (p->p_args && --p->p_args->ar_ref == 0)
703 FREE(p->p_args, M_PARGS);
706 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
709 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
712 pa->ar_length = req->newlen;
713 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
721 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
723 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
724 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
726 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
727 sysctl_kern_proc, "Process table");
729 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
730 sysctl_kern_proc, "Process table");
732 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
733 sysctl_kern_proc, "Process table");
735 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
736 sysctl_kern_proc, "Process table");
738 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
739 sysctl_kern_proc, "Process table");
741 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
742 sysctl_kern_proc_args, "Process argument list");