[Midnightbsd-cvs] src: usr.bin/sockstat: sync with freebsd7
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Fri Mar 27 18:32:14 EDT 2009
Log Message:
-----------
sync with freebsd7
Modified Files:
--------------
src/usr.bin/sockstat:
sockstat.1 (r1.1.1.1 -> r1.2)
sockstat.c (r1.1.1.1 -> r1.2)
-------------- next part --------------
Index: sockstat.1
===================================================================
RCS file: /home/cvs/src/usr.bin/sockstat/sockstat.1,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -L usr.bin/sockstat/sockstat.1 -L usr.bin/sockstat/sockstat.1 -u -r1.1.1.1 -r1.2
--- usr.bin/sockstat/sockstat.1
+++ usr.bin/sockstat/sockstat.1
@@ -25,9 +25,9 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.\" $FreeBSD: src/usr.bin/sockstat/sockstat.1,v 1.20 2005/01/11 10:32:52 ru Exp $
+.\" $FreeBSD: src/usr.bin/sockstat/sockstat.1,v 1.21 2006/11/11 22:11:54 keramida Exp $
.\"
-.Dd August 25, 2004
+.Dd November 11, 2006
.Dt SOCKSTAT 1
.Os
.Sh NAME
@@ -37,6 +37,7 @@
.Nm
.Op Fl 46clu
.Op Fl p Ar ports
+.Op Fl P Ar protocols
.Sh DESCRIPTION
The
.Nm
@@ -65,6 +66,14 @@
.Ar ports
argument is a comma-separated list of port numbers and ranges
specified as first and last port separated by a dash.
+.It Fl P Ar protocols
+Only show sockets of the specified
+.Ar protocols .
+The
+.Ar protocols
+argument is a comma-separated list of protocol names,
+as they are defined in
+.Xr protocols 5 .
.It Fl u
Show
.Dv AF_LOCAL
@@ -139,7 +148,8 @@
.Xr fstat 1 ,
.Xr netstat 1 ,
.Xr inet 4 ,
-.Xr inet6 4
+.Xr inet6 4 ,
+.Xr protocols 5
.Sh HISTORY
The
.Nm
Index: sockstat.c
===================================================================
RCS file: /home/cvs/src/usr.bin/sockstat/sockstat.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -L usr.bin/sockstat/sockstat.c -L usr.bin/sockstat/sockstat.c -u -r1.1.1.1 -r1.2
--- usr.bin/sockstat/sockstat.c
+++ usr.bin/sockstat/sockstat.c
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.13 2005/06/10 06:36:03 des Exp $");
+__FBSDID("$FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.17 2007/06/16 20:24:55 maxim Exp $");
#include <sys/param.h>
#include <sys/socket.h>
@@ -66,6 +66,16 @@
static int opt_u; /* Show Unix domain sockets */
static int opt_v; /* Verbose mode */
+/*
+ * Default protocols to use if no -P was defined.
+ */
+static const char *default_protos[] = {"tcp", "udp", "divert" };
+static size_t default_numprotos =
+ sizeof(default_protos) / sizeof(default_protos[0]);
+
+static int *protos; /* protocols to use */
+static size_t numprotos; /* allocated size of protos[] */
+
static int *ports;
#define INT_BIT (sizeof(int)*CHAR_BIT)
@@ -104,6 +114,66 @@
return (len);
}
+
+static int
+get_proto_type(const char *proto)
+{
+ struct protoent *pent;
+
+ if (strlen(proto) == 0)
+ return (0);
+ pent = getprotobyname(proto);
+ if (pent == NULL) {
+ warn("getprotobyname");
+ return (-1);
+ }
+ return (pent->p_proto);
+}
+
+
+static void init_protos(int num)
+{
+ int proto_count = 0;
+
+ if (num > 0) {
+ proto_count = num;
+ } else {
+ /* Find the maximum number of possible protocols. */
+ while (getprotoent() != NULL)
+ proto_count++;
+ endprotoent();
+ }
+
+ if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
+ err(1, "malloc");
+ numprotos = proto_count;
+}
+
+
+static int
+parse_protos(char *protospec)
+{
+ char *prot;
+ char *tmp = protospec;
+ int proto_type, proto_index;
+
+ if (protospec == NULL)
+ return (-1);
+
+ init_protos(0);
+ proto_index = 0;
+ while ((prot = strsep(&tmp, ",")) != NULL) {
+ if (strlen(prot) == 0)
+ continue;
+ proto_type = get_proto_type(prot);
+ if (proto_type != -1)
+ protos[proto_index++] = proto_type;
+ }
+ numprotos = proto_index;
+ return (proto_index);
+}
+
+
static void
parse_ports(const char *portspec)
{
@@ -209,7 +279,7 @@
protoname = "div";
break;
default:
- abort();
+ errx(1, "protocol %d not supported", proto);
}
buf = NULL;
@@ -264,7 +334,7 @@
so = &xip->xi_socket;
break;
default:
- abort();
+ errx(1, "protocol %d not supported", proto);
}
if ((inp->inp_vflag & vflag) == 0)
continue;
@@ -456,10 +526,12 @@
mib[3] = (int)pid;
len = sizeof proc;
if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
- warn("sysctl()");
+ /* Do not warn if the process exits before we get its name. */
+ if (errno != ESRCH)
+ warn("sysctl()");
return ("??");
}
- return (proc.ki_ocomm);
+ return (proc.ki_comm);
}
static int
@@ -573,19 +645,41 @@
}
}
+static int set_default_protos(void)
+{
+ struct protoent *prot;
+ const char *pname;
+ size_t pindex;
+
+ init_protos(default_numprotos);
+
+ for (pindex = 0; pindex < default_numprotos; pindex++) {
+ pname = default_protos[pindex];
+ prot = getprotobyname(pname);
+ if (prot == NULL)
+ err(1, "getprotobyname: %s", pname);
+ protos[pindex] = prot->p_proto;
+ }
+ numprotos = pindex;
+ return (pindex);
+}
+
+
static void
usage(void)
{
- fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n");
+ fprintf(stderr,
+ "Usage: sockstat [-46clu] [-p ports] [-P protocols]\n");
exit(1);
}
int
main(int argc, char *argv[])
{
- int o;
+ int protos_defined = -1;
+ int o, i;
- while ((o = getopt(argc, argv, "46clp:uv")) != -1)
+ while ((o = getopt(argc, argv, "46clp:P:uv")) != -1)
switch (o) {
case '4':
opt_4 = 1;
@@ -602,6 +696,9 @@
case 'p':
parse_ports(optarg);
break;
+ case 'P':
+ protos_defined = parse_protos(optarg);
+ break;
case 'u':
opt_u = 1;
break;
@@ -618,22 +715,25 @@
if (argc > 0)
usage();
+ if ((!opt_4 && !opt_6) && protos_defined != -1)
+ opt_4 = opt_6 = 1;
if (!opt_4 && !opt_6 && !opt_u)
opt_4 = opt_6 = opt_u = 1;
+ if ((opt_4 || opt_6) && protos_defined == -1)
+ protos_defined = set_default_protos();
if (!opt_c && !opt_l)
opt_c = opt_l = 1;
if (opt_4 || opt_6) {
- gather_inet(IPPROTO_TCP);
- gather_inet(IPPROTO_UDP);
- gather_inet(IPPROTO_DIVERT);
+ for (i = 0; i < protos_defined; i++)
+ gather_inet(protos[i]);
}
- if (opt_u) {
+
+ if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
gather_unix(SOCK_STREAM);
gather_unix(SOCK_DGRAM);
}
getfiles();
display();
-
exit(0);
}
More information about the Midnightbsd-cvs
mailing list