PERFORCE change 164271 for review

Gabor Pali pgj at FreeBSD.org
Sat Jun 13 14:13:03 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=164271

Change 164271 by pgj at petymeg-current on 2009/06/13 14:12:39

	Respect WARNS=3

Affected files ...

.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/Makefile#6 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#25 edit

Differences ...

==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/Makefile#6 (text+ko) ====

@@ -14,7 +14,7 @@
 DPADD=	${LIBKVM}
 LDADD=	-lkvm
 
-#WARNS?=	3
+WARNS?=	3
 
 WITHOUT_MAN=	yes
 

==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#25 (text+ko) ====

@@ -1,4 +1,5 @@
 
+#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
@@ -23,32 +24,36 @@
 #include "netstat.h"
 #include "netstat_internal.h"
 
-static struct nlist nl[] = {
-/* UNP_###-related */
-#define X_UNP_COUNT	    0
-	{ .n_name = "_unp_count" },
-#define X_UNP_GENCNT	    1
-	{ .n_name = "_unp_gencnt" },
-#define X_UNP_DHEAD	    2
-	{ .n_name = "_unp_dhead" },
-#define X_UNP_SHEAD	    3
-	{ .n_name = "_unp_shead" },
-	{ .n_name = NULL },
-/* INET-related */
-#define X_TCBINFO	    5
-	{ .n_name = "_tcbinfo" },
-#define X_UDBINFO	    6
-	{ .n_name = "_udbinfo" },
-#define X_DIVCBINFO	    7
-	{ .n_name = "_divcbinfo" },
-#define X_RIPCBINFO	    8
-	{ .n_name = "_ripcbinfo" },
-	{ .n_name = NULL },
+/* nlist(3) indices and symbols. */
+enum nlUNP {
+    nlUNP_count = 0,
+    nlUNP_gencnt,
+    nlUNP_dhead,
+    nlUNP_shead,
+    nlUNP_MAX
+};
+
+static const char *const unp_symbol[] =
+    { "_unp_count", "_unp_gencnt", "_unp_dhead", "_unp_shead" };
+
+enum nlINP {
+    nlINP_tcbinfo = 0,
+    nlINP_udbinfo,
+    nlINP_divcbinfo,
+    nlINP_ripcbinfo,
+    nlINP_MAX
 };
 
+static const char *const inp_symbol[] =
+    { "_tcbinfo", "_udbinfo", "_divcbinfo", "_ripcbinfo" };
+
 static void extract_xunpcb_data(struct xunpcb *, struct socket_type *);
 static void extract_inet_data(struct tcpcb *, struct inpcb *,
     struct xsocket *, struct socket_type *);
+static int  netstat_local_sockets(int, struct socket_type_list *, kvm_t *,
+    struct nlist *, int);
+static int  netstat_inet_sockets(int, int, struct socket_type_list *,
+    kvm_t *, struct nlist *, int);
 
 /* type names */
 static const char *const socktype[] =
@@ -56,7 +61,7 @@
 
 static int
 net_local_pcblist_sysctl(int family, int type, struct socket_type_list *list,
-    int flags)
+    __unused int flags)
 {
 	char	*buf;
 	size_t	len;
@@ -218,7 +223,7 @@
 
 static int
 net_local_pcblist_kvm(int family, int type, struct socket_type_list *list,
-    kvm_t *kvm, struct nlist *nlistp, int flags)
+    kvm_t *kvm, struct nlist *nlistp, __unused int flags)
 {
 	struct unp_head head;
 	struct unpcb *unp, unp_conn;
@@ -230,8 +235,8 @@
 	struct socket_type *stp;
 	u_long count_off, gencnt_off, head_off;
 
-	count_off = nlistp[X_UNP_COUNT].n_value;
-	gencnt_off = nlistp[X_UNP_GENCNT].n_value;
+	count_off = nlistp[nlUNP_count].n_value;
+	gencnt_off = nlistp[nlUNP_gencnt].n_value;
 
 	if (count_off == 0 || gencnt_off == 0) {
 		list->stl_error = NETSTAT_ERROR_UNDEFINED;
@@ -239,10 +244,10 @@
 	}
 	switch (type) {
 	case SOCK_STREAM:
-		head_off = nlistp[X_UNP_SHEAD].n_value;
+		head_off = nlistp[nlUNP_shead].n_value;
 		break;
 	case SOCK_DGRAM:
-		head_off = nlistp[X_UNP_DHEAD].n_value;
+		head_off = nlistp[nlUNP_dhead].n_value;
 		break;
 	default:
 		list->stl_error = NETSTAT_ERROR_UNDEFINED;
@@ -289,36 +294,35 @@
 
 static int
 net_inet_pcblist_kvm(int family, int protocol, struct socket_type_list *list,
-    kvm_t *kvm, struct nlist *nlistp, int flags)
+    kvm_t *kvm, struct nlist *nlistp, __unused int flags)
 {
 	struct inpcbinfo pcbinfo;
 	struct inpcbhead listhead;
 	struct inpcb *inp;
 	struct xinpcb xi;
-	struct xinpgen xig;
 	struct xtcpcb xt;
 	struct tcpcb *tcb = NULL;
 	struct socket so;
-	struct xsocket *xso;
+	struct xsocket xso;
 	u_long off;
 
 	struct socket_type  *stp;
 
 	switch (protocol) {
 	case IPPROTO_TCP:
-		off = nlistp[X_TCBINFO].n_value;
+		off = nlistp[nlINP_tcbinfo].n_value;
 		break;
 	case IPPROTO_UDP:
-		off = nlistp[X_UDBINFO].n_value;
+		off = nlistp[nlINP_udbinfo].n_value;
 		break;
 	case IPPROTO_DIVERT:
-		off = nlistp[X_DIVCBINFO].n_value;
+		off = nlistp[nlINP_divcbinfo].n_value;
 		break;
 	case IPPROTO_RAW:
 	case IPPROTO_ICMP:
 	case IPPROTO_IGMP:
 	case IPPROTO_PIM:
-		off = nlistp[X_RIPCBINFO].n_value;
+		off = nlistp[nlINP_ripcbinfo].n_value;
 		break;
 	default:
 		list->stl_error = NETSTAT_ERROR_UNDEFINED;
@@ -351,18 +355,18 @@
 		}
 		if (inp->inp_socket) {
 			KREAD(inp->inp_socket, &so, sizeof(so));
-			if (sotoxsocket(kvm, &so, xso) != 0) {
+			if (sotoxsocket(kvm, &so, &xso) != 0) {
 				list->stl_error = NETSTAT_ERROR_UNDEFINED;
 				return (-1);
 			}
 		} else {
-			bzero(xso, sizeof(*xso));
+			bzero(&xso, sizeof(xso));
 			if (protocol == IPPROTO_TCP)
-				xso->xso_protocol = protocol;
+				xso.xso_protocol = protocol;
 		}
 		stp = _netstat_st_allocate(list, family, protocol,
 		   ipproto(protocol));
-		extract_inet_data(tcb, inp, xso, stp);
+		extract_inet_data(tcb, inp, &xso, stp);
 	}
 
 	return (0);
@@ -389,7 +393,7 @@
 
 int
 netstat_local_sockets(int type, struct socket_type_list *list, kvm_t *kvm,
-    struct nlist *nl, int flags)
+    struct nlist *nlp, int flags)
 {
 	int use_kvm = flags & NETSTAT_SOCKET_KVM;
 
@@ -397,7 +401,7 @@
 	case SOCK_STREAM:
 	case SOCK_DGRAM:
 		if (use_kvm)
-			NPCB_KVM(local, PF_LOCAL, type, list, kvm, nl,
+			NPCB_KVM(local, PF_LOCAL, type, list, kvm, nlp,
 			    flags);
 		else
 			/* Use sysctl (or something else). */
@@ -407,9 +411,9 @@
 	case 0:
 		if (use_kvm) {
 			NPCB_KVM(local, PF_LOCAL, SOCK_STREAM, list, kvm,
-			    nl, flags);
+			    nlp, flags);
 			NPCB_KVM(local, PF_LOCAL, SOCK_DGRAM, list, kvm,
-			    nl, flags);
+			    nlp, flags);
 		} else {
 			NPCB_SCT(local, PF_LOCAL, SOCK_STREAM, list,
 			    flags);
@@ -426,7 +430,7 @@
 
 int
 netstat_inet_sockets(int domain, int protocol, struct socket_type_list *list,
-    kvm_t *kvm, struct nlist *nl, int flags)
+    kvm_t *kvm, struct nlist *nlp, int flags)
 {
 	int use_kvm = flags & NETSTAT_SOCKET_KVM;
 
@@ -440,7 +444,7 @@
 	case IPPROTO_PIM:
 	case IPPROTO_ICMPV6:
 		if (use_kvm)
-			NPCB_KVM(inet, domain, protocol, list, kvm, nl,
+			NPCB_KVM(inet, domain, protocol, list, kvm, nlp,
 			    flags);
 		else
 			NPCB_SCT(inet, domain, protocol, list, flags);
@@ -450,13 +454,13 @@
 		/* Errors do not count here. */
 		if (use_kvm) {
 			net_inet_pcblist_kvm(domain, IPPROTO_TCP, list, kvm,
-			    nl, flags);
+			    nlp, flags);
 			net_inet_pcblist_kvm(domain, IPPROTO_UDP, list, kvm,
-			    nl, flags);
+			    nlp, flags);
 			net_inet_pcblist_kvm(domain, IPPROTO_DIVERT, list,
-			    kvm, nl, flags);
+			    kvm, nlp, flags);
 			net_inet_pcblist_kvm(domain, IPPROTO_RAW, list, kvm,
-			    nl, flags);
+			    nlp, flags);
 		} else {
 			net_inet_pcblist_sysctl(domain, IPPROTO_TCP, list,
 			    flags);
@@ -484,56 +488,69 @@
     struct socket_type_list *list, int flags, void *kvm_handle)
 {
 	kvm_t	*kvm;
-	int	result;
-	struct nlist *nlp = NULL;
+	int	result, i;
+	struct nlist nls[MAX(nlUNP_MAX, nlINP_MAX) + 1];
 
+	nls[0].n_name = NULL;
 	if (flags & NETSTAT_SOCKET_KVM) {
 		/* Use KVM to retrieve data. */
+		kvm = (kvm_t *)kvm_handle;
 		switch (domain) {
 		case PF_LOCAL:
-			nlp = &nl[X_UNP_COUNT];
+			for (i = 0; i < nlUNP_MAX; i++)
+				nls[i].n_name = strdup(unp_symbol[i]);
+			nls[nlUNP_MAX].n_name = NULL;
 			break;
 		case PF_INET:
 		case PF_INET6:
-			nlp = &nl[X_TCBINFO];
+			for (i = 0; i < nlINP_MAX; i++)
+				nls[i].n_name = strdup(inp_symbol[i]);
+			nls[nlINP_MAX].n_name = NULL;
 			break;
 		default:
 			break;
 		}
-		kvm = (kvm_t *)kvm_handle;
-		if (kvm_nlist(kvm, nlp) < 0) {
+		if (kvm_nlist(kvm, nls) < 0) {
 			list->stl_error = NETSTAT_ERROR_KVM;
-			return (-1);
+			result = -1;
+			goto end;
 		}
 	}
 	switch (domain) {
 	case PF_UNSPEC:
 		/* "Everything" */
-		result = netstat_local_sockets(0, list, kvm, nl, flags);
+		result = netstat_local_sockets(0, list, kvm, nls, flags);
 		if (result < 0)
-			return (result);
-		result = netstat_inet_sockets(PF_INET, 0, list, kvm, nl,
+			goto end;
+		result = netstat_inet_sockets(PF_INET, 0, list, kvm, nls,
 		    flags);
 		if (result < 0)
-			return (result);
-		result = netstat_inet_sockets(PF_INET6, 0, list, kvm, nl,
+			goto end;
+		result = netstat_inet_sockets(PF_INET6, 0, list, kvm, nls,
 		    flags);
 		if (result < 0)
-			return (result);
+			goto end;
 		break;
 	case PF_LOCAL:
-		return (netstat_local_sockets(type, list, kvm, nl, flags));
+		result = (netstat_local_sockets(type, list, kvm, nls, flags));
+		goto end;
 		break;
 	case PF_INET:
 	case PF_INET6:
-		return (netstat_inet_sockets(domain, protocol, list, kvm, nl,
+		result = (netstat_inet_sockets(domain, protocol, list, kvm, nls,
 		    flags));
+		goto end;
 		break;
 	default:
 		list->stl_error = NETSTAT_ERROR_UNSUPPORTED;
-		return (-1);
+		result = -1;
+		goto end;
 	}
-	return (0);
+
+end:
+	for (i = 0; nls[i].n_name != NULL && i < MAX(nlUNP_MAX, nlINP_MAX); i++)
+		free(nls[i].n_name);
+	return (result);
 }
 
 void


More information about the p4-projects mailing list