Add netstat command

This adds the netstat command. All but one file is currently enabled.
That file does not currently build.

Also added libmemstat and libutil. libmemstat had a lot of code related
to kvm and kernel memory access disabled. This may or may not be an
issue.
This commit is contained in:
Joel Sherrill
2012-10-17 11:13:36 -05:00
parent 99ae4eb50f
commit b6ac989f8a
21 changed files with 3105 additions and 12 deletions

View File

@@ -80,11 +80,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <errno.h>
#ifdef __rtems__
/* apparently libutil.h is not needed */
#else
#include <libutil.h>
#endif
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
@@ -207,7 +203,9 @@ intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
#ifdef INET6
struct in6_ifaddr in6;
#endif
#ifndef __rtems__
struct ipx_ifaddr ipx;
#endif
} ifaddr;
u_long ifaddraddr;
u_long ifaddrfound;
@@ -380,6 +378,7 @@ intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
network_layer = 1;
break;
#endif /*INET6*/
#ifndef __rtems__
case AF_IPX:
{
struct sockaddr_ipx *sipx =
@@ -397,6 +396,7 @@ intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
network_layer = 1;
break;
#endif
case AF_APPLETALK:
printf("atalk:%-12.12s ",atalk_print(sa,0x10) );

View File

@@ -66,8 +66,28 @@ __FBSDID("$FreeBSD$");
#ifdef INET6
#include <netinet/ip6.h>
#endif /* INET6 */
#ifdef __rtems__
#include <freebsd/netinet/in_pcb.h>
#else
#include <netinet/in_pcb.h>
#endif
#include <netinet/ip_icmp.h>
#ifdef __rtems__
#include <freebsd/netinet/icmp_var.h>
#include <freebsd/netinet/igmp_var.h>
#include <freebsd/netinet/ip_var.h>
#include <freebsd/netinet/pim_var.h>
#include <netinet/tcp.h>
#include <freebsd/netinet/tcpip.h>
#include <freebsd/netinet/tcp_seq.h>
#define TCPSTATES
#include <freebsd/netinet/tcp_fsm.h>
#include <freebsd/netinet/tcp_timer.h>
#include <freebsd/netinet/tcp_var.h>
#include <freebsd/netinet/tcp_debug.h>
#include <netinet/udp.h>
#include <freebsd/netinet/udp_var.h>
#else
#include <netinet/icmp_var.h>
#include <netinet/igmp_var.h>
#include <netinet/ip_var.h>
@@ -82,6 +102,7 @@ __FBSDID("$FreeBSD$");
#include <netinet/tcp_debug.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#endif
#include <arpa/inet.h>
#include <err.h>

View File

@@ -1,3 +1,7 @@
#ifdef __rtems__
#define __need_getopt_newlib
#include <getopt.h>
#endif
/*-
* Copyright (c) 1983, 1988, 1993
* Regents of the University of California. All rights reserved.
@@ -65,7 +69,9 @@ __FBSDID("$FreeBSD$");
#include <ctype.h>
#include <err.h>
#include <errno.h>
#ifndef __rtems__
#include <kvm.h>
#endif
#include <limits.h>
#include <netdb.h>
#include <nlist.h>
@@ -323,7 +329,9 @@ static void usage(void);
static struct protox *name2protox(const char *);
static struct protox *knownname(const char *);
#ifndef __rtems__
static kvm_t *kvmd;
#endif
static char *nlistf = NULL, *memf = NULL;
int Aflag; /* show addresses of protocol control block */
@@ -356,14 +364,26 @@ int af; /* address family */
int live; /* true if we are examining a live system */
int
#ifdef __rtems__
main_netstat(int argc, char *argv[])
#else
main(int argc, char *argv[])
#endif
{
struct protox *tp = NULL; /* for printing cblocks & stats */
int ch;
#ifdef __rtems__
struct getopt_data getopt_reent;
#endif
af = AF_UNSPEC;
#ifdef __rtems__
memset(&getopt_reent, 0, sizeof(getopt_data));
while ((ch = getopt_r(argc, argv, "AaBbdf:ghI:iLlM:mN:np:q:rSstuWw:xz", &getopt_reent)) != -1)
#else
while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:q:rSstuWw:xz")) != -1)
#endif
switch(ch) {
case 'A':
Aflag = 1;
@@ -508,6 +528,7 @@ main(int argc, char *argv[])
}
#endif
#ifndef __rtems__
/*
* Discard setgid privileges if not the running kernel so that bad
* guys can't print interesting stuff from kernel memory.
@@ -530,6 +551,7 @@ main(int argc, char *argv[])
mbpr(NULL, 0);
exit(0);
}
#endif
#if 0
/*
* Keep file descriptors open to avoid overhead
@@ -544,7 +566,9 @@ main(int argc, char *argv[])
* used for the queries, which is slower.
*/
#endif
#ifndef __rtems__
kread(0, NULL, 0);
#endif
if (iflag && !sflag) {
intpr(interval, nl[N_IFNET].n_value, NULL);
exit(0);
@@ -679,6 +703,7 @@ printproto(tp, name)
(*pr)(off, name, af, tp->pr_protocol);
}
#ifndef __rtems__
/*
* Read kernel memory, return 0 on success.
*/
@@ -718,6 +743,7 @@ kread(u_long addr, void *buf, size_t size)
}
return (0);
}
#endif
const char *
plural(uintmax_t n)
@@ -803,3 +829,16 @@ usage(void)
" netstat -gs [-s] [-f address_family] [-M core] [-N system]");
exit(1);
}
#ifdef __rtems__
#include <rtems/shell.h>
rtems_shell_cmd_t rtems_shell_NETSTAT_Command = {
"netstat", /* name */
"netstat [args]", /* usage */
"net", /* topic */
main_netstat, /* command */
NULL, /* alias */
NULL /* next */
};
#endif

View File

@@ -56,12 +56,10 @@ __FBSDID("$FreeBSD$");
#include <sys/sysctl.h>
#include <err.h>
#ifdef __rtems__
/* XXX what to do? */
#else
#ifndef __rtems__
#include <kvm.h>
#include <memstat.h>
#endif
#include <memstat.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -110,6 +108,7 @@ mbpr(void *kvmd, u_long mbaddr)
goto out;
}
} else {
#ifndef __rtems__
if (memstat_kvm_all(mtlp, kvmd) < 0) {
error = memstat_mtl_geterror(mtlp);
if (error == MEMSTAT_ERROR_KVM)
@@ -120,6 +119,7 @@ mbpr(void *kvmd, u_long mbaddr)
memstat_strerror(error));
goto out;
}
#endif
}
mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);

View File

@@ -59,7 +59,9 @@ __FBSDID("$FreeBSD$");
#endif
#include <sys/sysctl.h>
#include <sys/un.h>
#ifndef __rtems__
#include <sys/unpcb.h>
#endif
#include <netinet/in.h>
@@ -70,7 +72,9 @@ __FBSDID("$FreeBSD$");
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#ifndef __rtems__
#include <kvm.h>
#endif
#include "netstat.h"
static void unixdomainpr(struct xunpcb *, struct xsocket *);
@@ -106,6 +110,7 @@ pcblist_sysctl(int type, char **bufp)
return (0);
}
#ifndef __rtems__
static int
pcblist_kvm(u_long count_off, u_long gencnt_off, u_long head_off, char **bufp)
{
@@ -292,3 +297,4 @@ unixdomainpr(struct xunpcb *xunp, struct xsocket *so)
sa->sun_path);
putchar('\n');
}
#endif