sockstat(1): tolerate situation where file info cannot be fetched

Either due to a race, or to the privilege restrictions, it is not
guaranteed that kern.files returned file information for all pcbs
read from net.inet.<proto>.pcblist.  In this case the file rbtree does
not return the matching file by data address, and code must avoid
dereferencing NULL.

PR:	279875
Reviewed by:	asomers
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D46050
This commit is contained in:
Konstantin Belousov 2024-07-20 03:30:55 +03:00
parent 9a3f7fb46c
commit 35f4984343

View file

@ -1164,8 +1164,11 @@ displaysock(struct sock *s, int pos)
f = RB_FIND(files_t, &ftree,
&(struct file){ .xf_data =
p->socket });
pos += xprintf("[%lu %d]",
(u_long)f->xf_pid, f->xf_fd);
if (f != NULL) {
pos += xprintf("[%lu %d]",
(u_long)f->xf_pid,
f->xf_fd);
}
} else
pos += printaddr(&p->laddr->address);
}
@ -1183,9 +1186,12 @@ displaysock(struct sock *s, int pos)
f = RB_FIND(files_t, &ftree,
&(struct file){ .xf_data =
p->socket });
pos += xprintf("%s[%lu %d]",
fref ? "" : ",",
(u_long)f->xf_pid, f->xf_fd);
if (f != NULL) {
pos += xprintf("%s[%lu %d]",
fref ? "" : ",",
(u_long)f->xf_pid,
f->xf_fd);
}
ref = p->faddr->nextref;
fref = false;
}