mbuf, inet, and unix modules no longer read kvm.

This commit is contained in:
wollman 1998-05-15 20:19:21 +00:00
parent bbc4497ada
commit e37570d812
5 changed files with 265 additions and 199 deletions

View File

@ -36,7 +36,7 @@
static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
*/
static const char rcsid[] =
"$Id: inet.c,v 1.25 1997/02/22 19:56:21 peter Exp $";
"$Id: inet.c,v 1.26 1997/08/25 16:57:05 wollman Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -67,16 +67,15 @@ static const char rcsid[] =
#include <netinet/udp_var.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "netstat.h"
struct inpcb inpcb;
struct tcpcb tcpcb;
struct socket sockb;
char *inetname __P((struct in_addr *));
void inetprint __P((struct in_addr *, int, char *, int));
@ -87,50 +86,83 @@ void inetprint __P((struct in_addr *, int, char *, int));
* -a (all) flag is specified.
*/
void
protopr(off, name)
u_long off;
protopr(proto, name)
u_long proto; /* for sysctl version we pass proto # */
char *name;
{
struct inpcbhead head;
register struct inpcb *prev, *next;
int istcp;
static int first = 1;
char *buf;
const char *mibvar;
struct tcpcb *tp;
struct inpcb *inp;
struct xinpgen *xig, *oxig;
struct xsocket *so;
size_t len;
if (off == 0)
istcp = 0;
switch (proto) {
case IPPROTO_TCP:
istcp = 1;
mibvar = "net.inet.tcp.pcblist";
break;
case IPPROTO_UDP:
mibvar = "net.inet.udp.pcblist";
break;
case IPPROTO_DIVERT:
mibvar = "net.inet.divert.pcblist";
break;
default:
mibvar = "net.inet.raw.pcblist";
break;
}
len = 0;
if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
if (errno != ENOENT)
warn("sysctl: %s", mibvar);
return;
}
if ((buf = malloc(len)) == 0) {
warn("malloc %lu bytes", (u_long)len);
return;
}
if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
warn("sysctl: %s", mibvar);
free(buf);
return;
}
istcp = strcmp(name, "tcp") == 0;
kread(off, (char *)&head, sizeof (struct inpcbhead));
prev = (struct inpcb *)off;
for (next = head.lh_first; next != NULL; next = inpcb.inp_list.le_next) {
if (kread((u_long)next, (char *)&inpcb, sizeof (inpcb))) {
printf("???\n");
break;
}
if (!aflag &&
inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {
prev = next;
continue;
}
if (kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb))) {
printf("???\n");
break;
};
oxig = xig = (struct xinpgen *)buf;
for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
xig->xig_len > sizeof(struct xinpgen);
xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
if (istcp) {
if (kread((u_long)inpcb.inp_ppcb,
(char *)&tcpcb, sizeof (tcpcb))) {
printf("???\n");
break;
};
tp = &((struct xtcpcb *)xig)->xt_tp;
inp = &((struct xtcpcb *)xig)->xt_inp;
so = &((struct xtcpcb *)xig)->xt_socket;
} else {
inp = &((struct xinpcb *)xig)->xi_inp;
so = &((struct xinpcb *)xig)->xi_socket;
}
/* Ignore sockets for protocols other than the desired one. */
if (so->xso_protocol != proto)
continue;
/* Ignore PCBs which were freed during copyout. */
if (inp->inp_gencnt > oxig->xig_gen)
continue;
if (!aflag && inet_lnaof(inp->inp_laddr) == INADDR_ANY)
continue;
if (first) {
printf("Active Internet connections");
if (aflag)
printf(" (including servers)");
putchar('\n');
if (Aflag)
printf("%-8.8s ", "PCB");
printf("%-8.8s ", "Socket");
printf(Aflag ?
"%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
"%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
@ -139,43 +171,52 @@ protopr(off, name)
first = 0;
}
if (Aflag)
if (istcp)
printf("%8x ", (int)inpcb.inp_ppcb);
else
printf("%8x ", (int)next);
printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
sockb.so_snd.sb_cc);
printf("%8lx ", (u_long)so->so_pcb);
printf("%-5.5s %6ld %6ld ", name, so->so_rcv.sb_cc,
so->so_snd.sb_cc);
if (nflag) {
inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
inetprint(&inp->inp_laddr, (int)inp->inp_lport,
name, 1);
inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
inetprint(&inp->inp_faddr, (int)inp->inp_fport,
name, 1);
} else if (inpcb.inp_flags & INP_ANONPORT) {
inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
} else if (inp->inp_flags & INP_ANONPORT) {
inetprint(&inp->inp_laddr, (int)inp->inp_lport,
name, 1);
inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
inetprint(&inp->inp_faddr, (int)inp->inp_fport,
name, 0);
} else {
inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
inetprint(&inp->inp_laddr, (int)inp->inp_lport,
name, 0);
inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
name, inpcb.inp_lport != inpcb.inp_fport);
inetprint(&inp->inp_faddr, (int)inp->inp_fport,
name, inp->inp_lport != inp->inp_fport);
}
if (istcp) {
if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
printf(" %d", tcpcb.t_state);
if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
printf(" %d", tp->t_state);
else {
printf(" %s", tcpstates[tcpcb.t_state]);
printf(" %s", tcpstates[tp->t_state]);
#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
/* Show T/TCP `hidden state' */
if (tcpcb.t_flags & (TF_NEEDSYN|TF_NEEDFIN))
if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
putchar('*');
#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
}
}
putchar('\n');
prev = next;
}
if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
if (oxig->xig_count > xig->xig_count) {
printf("Some %s sockets may have been deleted.\n",
name);
} else if (oxig->xig_count < xig->xig_count) {
printf("Some %s sockets may have been created.\n",
name);
} else {
printf("Some %s sockets may have been created or deleted",
name);
}
}
free(buf);
}
/*
@ -187,11 +228,14 @@ tcp_stats(off, name)
char *name;
{
struct tcpstat tcpstat;
if (off == 0)
size_t len = sizeof tcpstat;
if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.tcp.stats");
return;
}
printf ("%s:\n", name);
kread(off, (char *)&tcpstat, sizeof (tcpstat));
#define p(f, m) if (tcpstat.f || sflag <= 1) \
printf(m, tcpstat.f, plural(tcpstat.f))
@ -271,11 +315,14 @@ udp_stats(off, name)
char *name;
{
struct udpstat udpstat;
size_t len = sizeof udpstat;
u_long delivered;
if (off == 0)
if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.udp.stats");
return;
kread(off, (char *)&udpstat, sizeof (udpstat));
}
printf("%s:\n", name);
#define p(f, m) if (udpstat.f || sflag <= 1) \
printf(m, udpstat.f, plural(udpstat.f))
@ -309,10 +356,13 @@ ip_stats(off, name)
char *name;
{
struct ipstat ipstat;
size_t len = sizeof ipstat;
if (off == 0)
if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.ip.stats");
return;
kread(off, (char *)&ipstat, sizeof (ipstat));
}
printf("%s:\n", name);
#define p(f, m) if (ipstat.f || sflag <= 1) \
@ -444,10 +494,13 @@ igmp_stats(off, name)
char *name;
{
struct igmpstat igmpstat;
size_t len = sizeof igmpstat;
if (off == 0)
if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.igmp.stats");
return;
kread(off, (char *)&igmpstat, sizeof (igmpstat));
}
printf("%s:\n", name);
#define p(f, m) if (igmpstat.f || sflag <= 1) \

View File

@ -42,7 +42,7 @@ char const copyright[] =
static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
#endif
static const char rcsid[] =
"$Id$";
"$Id: main.c,v 1.19 1997/07/29 06:51:40 charnier Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -67,82 +67,62 @@ static const char rcsid[] =
#include "netstat.h"
struct nlist nl[] = {
#define N_MBSTAT 0
{ "_mbstat" },
#define N_IPSTAT 1
{ "_ipstat" },
#define N_TCB 2
{ "_tcb" },
#define N_TCPSTAT 3
{ "_tcpstat" },
#define N_UDB 4
{ "_udb" },
#define N_UDPSTAT 5
{ "_udpstat" },
#define N_IFNET 6
#define N_IFNET 0
{ "_ifnet" },
#define N_IMP 7
#define N_IMP 1
{ "_imp_softc" },
#define N_ICMPSTAT 8
{ "_icmpstat" },
#define N_RTSTAT 9
#define N_RTSTAT 2
{ "_rtstat" },
#define N_UNIXSW 10
#define N_UNIXSW 3
{ "_localsw" },
#define N_IDP 11
#define N_IDP 4
{ "_nspcb"},
#define N_IDPSTAT 12
#define N_IDPSTAT 5
{ "_idpstat"},
#define N_SPPSTAT 13
#define N_SPPSTAT 6
{ "_spp_istat"},
#define N_NSERR 14
#define N_NSERR 7
{ "_ns_errstat"},
#define N_CLNPSTAT 15
#define N_CLNPSTAT 8
{ "_clnp_stat"},
#define IN_NOTUSED 16
#define IN_NOTUSED 9
{ "_tp_inpcb" },
#define ISO_TP 17
#define ISO_TP 10
{ "_tp_refinfo" },
#define N_TPSTAT 18
#define N_TPSTAT 11
{ "_tp_stat" },
#define N_ESISSTAT 19
#define N_ESISSTAT 12
{ "_esis_stat"},
#define N_NIMP 20
#define N_NIMP 13
{ "_nimp"},
#define N_RTREE 21
#define N_RTREE 14
{ "_rt_tables"},
#define N_CLTP 22
#define N_CLTP 15
{ "_cltb"},
#define N_CLTPSTAT 23
#define N_CLTPSTAT 16
{ "_cltpstat"},
#define N_NFILE 24
#define N_NFILE 17
{ "_nfile" },
#define N_FILE 25
#define N_FILE 18
{ "_file" },
#define N_IGMPSTAT 26
{ "_igmpstat" },
#define N_MRTPROTO 27
#define N_MRTPROTO 19
{ "_ip_mrtproto" },
#define N_MRTSTAT 28
#define N_MRTSTAT 20
{ "_mrtstat" },
#define N_MFCTABLE 29
#define N_MFCTABLE 21
{ "_mfctable" },
#define N_VIFTABLE 30
#define N_VIFTABLE 22
{ "_viftable" },
#define N_IPX 31
#define N_IPX 23
{ "_ipxpcb"},
#define N_IPXSTAT 32
#define N_IPXSTAT 24
{ "_ipxstat"},
#define N_SPXSTAT 33
#define N_SPXSTAT 25
{ "_spx_istat"},
#define N_DDPSTAT 34
#define N_DDPSTAT 26
{ "_ddpstat"},
#define N_DDPCB 35
#define N_DDPCB 27
{ "_ddpcb"},
#define N_DIVPCB 36
{ "_divcb"},
#define N_DIVSTAT 37
{ "_divstat"},
{ "" },
};
@ -153,19 +133,20 @@ struct protox {
void (*pr_cblocks)(); /* control blocks printing routine */
void (*pr_stats)(); /* statistics printing routine */
char *pr_name; /* well-known name */
int pr_usesysctl; /* true if we use sysctl, not kvm */
} protox[] = {
{ N_TCB, N_TCPSTAT, 1, protopr,
tcp_stats, "tcp" },
{ N_UDB, N_UDPSTAT, 1, protopr,
udp_stats, "udp" },
{ N_DIVPCB, N_DIVSTAT, 1, protopr,
NULL, "divert" }, /* no stat structure yet */
{ -1, N_IPSTAT, 1, 0,
ip_stats, "ip" },
{ -1, N_ICMPSTAT, 1, 0,
icmp_stats, "icmp" },
{ -1, N_IGMPSTAT, 1, 0,
igmp_stats, "igmp" },
{ -1, -1, 1, protopr,
tcp_stats, "tcp", IPPROTO_TCP },
{ -1, -1, 1, protopr,
udp_stats, "udp", IPPROTO_UDP },
{ -1, -1, 1, protopr,
NULL, "divert", IPPROTO_DIVERT },
{ -1, -1, 1, protopr,
ip_stats, "ip", IPPROTO_RAW },
{ -1, -1, 1, protopr,
icmp_stats, "icmp", IPPROTO_ICMP },
{ -1, -1, 1, protopr,
igmp_stats, "igmp", IPPROTO_IGMP },
{ -1, -1, 0, 0,
0, 0 }
};
@ -362,31 +343,37 @@ main(argc, argv)
if (nlistf != NULL || memf != NULL)
setgid(getgid());
/*
* XXX.
*/
kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
if (kvmd == NULL) {
errx(1, "kvm_open: %s", buf);
}
if (kvm_nlist(kvmd, nl) < 0) {
if(nlistf)
errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
else
errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
}
if (kvmd != NULL) {
if (kvm_nlist(kvmd, nl) < 0) {
if(nlistf)
errx(1, "%s: kvm_nlist: %s", nlistf,
kvm_geterr(kvmd));
else
errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
}
if (nl[0].n_type == 0) {
if(nlistf)
errx(1, "%s: no namelist", nlistf);
else
errx(1, "no namelist");
if (nl[0].n_type == 0) {
if(nlistf)
errx(1, "%s: no namelist", nlistf);
else
errx(1, "no namelist");
}
} else {
errx(1, "%s", buf);
}
if (mflag) {
mbpr(nl[N_MBSTAT].n_value);
mbpr();
exit(0);
}
if (pflag) {
if (tp->pr_stats)
(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
tp->pr_name);
(*tp->pr_stats)(tp->pr_usesysctl ? tp->pr_usesysctl
: nl[tp->pr_sindex].n_value,
tp->pr_name);
else
printf("%s: no stats routine\n", tp->pr_name);
exit(0);
@ -457,7 +444,7 @@ main(argc, argv)
printproto(tp, tp->pr_name);
#endif
if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
unixpr(nl[N_UNIXSW].n_value);
unixpr();
exit(0);
}
@ -476,10 +463,12 @@ printproto(tp, name)
if (sflag) {
pr = tp->pr_stats;
off = nl[tp->pr_sindex].n_value;
off = tp->pr_usesysctl ? tp->pr_usesysctl
: nl[tp->pr_sindex].n_value;
} else {
pr = tp->pr_cblocks;
off = nl[tp->pr_index].n_value;
off = tp->pr_usesysctl ? tp->pr_usesysctl
: nl[tp->pr_index].n_value;
}
if (pr != NULL && (off || af != AF_UNSPEC))
(*pr)(off, name);
@ -494,7 +483,10 @@ kread(addr, buf, size)
char *buf;
int size;
{
if (kvmd == 0) {
warnx("KVM is not open");
return -1;
}
if (kvm_read(kvmd, addr, buf, size) != size) {
warnx("%s", kvm_geterr(kvmd));
return (-1);

View File

@ -36,7 +36,7 @@
static char sccsid[] = "@(#)mbuf.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id: mbuf.c,v 1.9 1997/11/10 08:03:36 ache Exp $";
"$Id: mbuf.c,v 1.10 1998/04/24 04:30:27 dg Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -94,8 +94,7 @@ bool seen[256]; /* "have we seen this type yet?" */
* Print mbuf statistics.
*/
void
mbpr(mbaddr)
u_long mbaddr;
mbpr()
{
register int totmem, totfree, totmbufs;
register int i;

View File

@ -68,7 +68,7 @@ void icmp_stats __P((u_long, char *));
void igmp_stats __P((u_long, char *));
void protopr __P((u_long, char *));
void mbpr __P((u_long));
void mbpr __P((void));
void hostpr __P((u_long, u_long));
void impstats __P((u_long, u_long));
@ -106,7 +106,7 @@ void ddp_stats __P((u_long, char *));
void intpr __P((int, u_long));
void unixpr __P((u_long));
void unixpr __P((void));
void esis_stats __P((u_long, char *));
void clnp_stats __P((u_long, char *));

View File

@ -36,13 +36,12 @@
static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id: unix.c,v 1.4 1997/07/29 06:51:41 charnier Exp $";
"$Id: unix.c,v 1.5 1997/08/25 16:55:00 wollman Exp $";
#endif /* not lint */
/*
* Display protocol blocks in the unix domain.
*/
#include <kvm.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/protosw.h>
@ -52,71 +51,93 @@ static const char rcsid[] =
#include <sys/sysctl.h>
#include <sys/un.h>
#include <sys/unpcb.h>
#define KERNEL
struct uio;
struct proc;
#include <sys/file.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <kvm.h>
#include "netstat.h"
static void unixdomainpr __P((struct socket *, caddr_t));
static void unixdomainpr __P((struct xunpcb *, struct xsocket *));
static struct file *file, *fileNFILE;
static int nfiles;
extern kvm_t *kvmd;
static const char *const socktype[] =
{ "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
void
unixpr(off)
u_long off;
unixpr()
{
register struct file *fp;
struct socket sock, *so = &sock;
char *filebuf;
struct protosw *unixsw = (struct protosw *)off;
char *buf;
int type;
size_t len;
struct xsocket *so;
struct xunpgen *xug, *oxug;
struct xunpcb *xunp;
char mibvar[sizeof "net.local.seqpacket.pcblist"];
filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
if (filebuf == 0) {
printf("Out of memory (file table).\n");
return;
}
file = (struct file *)(filebuf + sizeof(fp));
fileNFILE = file + nfiles;
for (fp = file; fp < fileNFILE; fp++) {
if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
for (type = SOCK_STREAM; type < SOCK_SEQPACKET; type++) {
sprintf(mibvar, "net.local.%s.pcblist", socktype[type]);
len = 0;
if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
if (errno != ENOENT)
warn("sysctl: %s", mibvar);
continue;
if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
continue;
/* kludge */
if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
if (so->so_pcb)
unixdomainpr(so, fp->f_data);
}
if ((buf = malloc(len)) == 0) {
warn("malloc %lu bytes", (u_long)len);
return;
}
if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
warn("sysctl: %s", mibvar);
free(buf);
return;
}
oxug = xug = (struct xunpgen *)buf;
for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
xug->xug_len > sizeof(struct xunpgen);
xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
xunp = (struct xunpcb *)xug;
so = &xunp->xu_socket;
/* Ignore PCBs which were freed during copyout. */
if (xunp->xu_unp.unp_gencnt > oxug->xug_gen)
continue;
unixdomainpr(xunp, so);
}
if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
if (oxug->xug_count > xug->xug_count) {
printf("Some %s sockets may have been deleted.\n",
socktype[type]);
} else if (oxug->xug_count < xug->xug_count) {
printf("Some %s sockets may have been created.\n",
socktype[type]);
} else {
printf("Some %s sockets may have been created or deleted",
socktype[type]);
}
}
free(buf);
}
}
static char *socktype[] =
{ "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
static void
unixdomainpr(so, soaddr)
register struct socket *so;
caddr_t soaddr;
unixdomainpr(xunp, so)
struct xunpcb *xunp;
struct xsocket *so;
{
struct unpcb unpcb, *unp = &unpcb;
struct sockaddr_un s_un, *sa = NULL;
struct unpcb *unp;
struct sockaddr_un *sa;
static int first = 1;
if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
return;
if (unp->unp_addr) {
sa = &s_un;
if (kread((u_long)unp->unp_addr, (char *)sa, sizeof *sa))
sa = (struct sockaddr_un *)0;
} else
unp = &xunp->xu_unp;
if (unp->unp_addr)
sa = &xunp->xu_addr;
else
sa = (struct sockaddr_un *)0;
if (first) {
printf("Active UNIX domain sockets\n");
printf(
@ -125,10 +146,11 @@ unixdomainpr(so, soaddr)
"Inode", "Conn", "Refs", "Nextref");
first = 0;
}
printf("%8x %-6.6s %6ld %6ld %8x %8x %8x %8x",
(int)soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
(int)unp->unp_vnode, (int)unp->unp_conn,
(int)unp->unp_refs, (int)unp->unp_nextref);
printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
(long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
so->so_snd.sb_cc,
(long)unp->unp_vnode, (long)unp->unp_conn,
(long)unp->unp_refs.lh_first, (long)unp->unp_reflink.le_next);
if (sa)
printf(" %.*s", sa->sun_len, sa->sun_path);
putchar('\n');