kinfo_proc: move job-control related data collection into a new helper.

This improves code structure and allows to put the lock asserts right
into place where the locks are needed.

Also move zeroing of the kinfo_proc structure from fill_kinfo_proc_only()
to fill_kinfo_proc(), this looks more symmetrical.

Reviewed by:	jilles
Tested by:	pho
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D27871
This commit is contained in:
Konstantin Belousov 2021-01-01 00:46:20 +02:00
parent 4daea93813
commit cf4f802e77

View File

@ -1133,22 +1133,18 @@ fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
}
/*
* Clear kinfo_proc and fill in any information that is common
* to all threads in the process.
* Fill in any information that is common to all threads in the process.
* Must be called with the target process locked.
*/
static void
fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
{
struct thread *td0;
struct tty *tp;
struct session *sp;
struct ucred *cred;
struct sigacts *ps;
struct timeval boottime;
PROC_LOCK_ASSERT(p, MA_OWNED);
bzero(kp, sizeof(*kp));
kp->ki_structsize = sizeof(*kp);
kp->ki_paddr = p;
@ -1241,36 +1237,6 @@ fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
FOREACH_THREAD_IN_PROC(p, td0)
kp->ki_cow += td0->td_cow;
tp = NULL;
if (p->p_pgrp) {
kp->ki_pgid = p->p_pgrp->pg_id;
kp->ki_jobc = p->p_pgrp->pg_jobc;
sp = p->p_pgrp->pg_session;
if (sp != NULL) {
kp->ki_sid = sp->s_sid;
SESS_LOCK(sp);
strlcpy(kp->ki_login, sp->s_login,
sizeof(kp->ki_login));
if (sp->s_ttyvp)
kp->ki_kiflag |= KI_CTTY;
if (SESS_LEADER(p))
kp->ki_kiflag |= KI_SLEADER;
/* XXX proctree_lock */
tp = sp->s_ttyp;
SESS_UNLOCK(sp);
}
}
if ((p->p_flag & P_CONTROLT) && tp != NULL) {
kp->ki_tdev = tty_udev(tp);
kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
if (tp->t_session)
kp->ki_tsid = tp->t_session->s_sid;
} else {
kp->ki_tdev = NODEV;
kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
}
if (p->p_comm[0] != '\0')
strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
if (p->p_sysent && p->p_sysent->sv_name != NULL &&
@ -1287,6 +1253,53 @@ fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
}
}
/*
* Fill job-related process information.
*/
static void
fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp)
{
struct tty *tp;
struct session *sp;
struct pgrp *pgrp;
sx_assert(&proctree_lock, SA_LOCKED);
PROC_LOCK_ASSERT(p, MA_OWNED);
pgrp = p->p_pgrp;
if (pgrp == NULL)
return;
kp->ki_pgid = pgrp->pg_id;
kp->ki_jobc = pgrp->pg_jobc;
sp = pgrp->pg_session;
tp = NULL;
if (sp != NULL) {
kp->ki_sid = sp->s_sid;
SESS_LOCK(sp);
strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login));
if (sp->s_ttyvp)
kp->ki_kiflag |= KI_CTTY;
if (SESS_LEADER(p))
kp->ki_kiflag |= KI_SLEADER;
tp = sp->s_ttyp;
SESS_UNLOCK(sp);
}
if ((p->p_flag & P_CONTROLT) && tp != NULL) {
kp->ki_tdev = tty_udev(tp);
kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
if (tp->t_session)
kp->ki_tsid = tp->t_session->s_sid;
} else {
kp->ki_tdev = NODEV;
kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
}
}
/*
* Fill in information that is thread specific. Must be called with
* target process locked. If 'preferthread' is set, overwrite certain
@ -1409,6 +1422,9 @@ fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
bzero(kp, sizeof(*kp));
fill_kinfo_proc_pgrp(p,kp);
fill_kinfo_proc_only(p, kp);
fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
fill_kinfo_aggregate(p, kp);