PERFORCE change 163709 for review

Gabor Pali pgj at FreeBSD.org
Sun Jun 7 13:18:37 UTC 2009


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

Change 163709 by pgj at petymeg-current on 2009/06/07 13:18:13

	Add a very simple implementation for socket_type_iterator as another
	option for processing socket_type_list data -- this solution was
	also suggested by rwatson, and it should not be dropped without
	giving it a try :P (and "let's defer design decisions" anyway; it
	might also provide a more comfortable interface for some tasks).
	
	    - Add USE_ITERATOR_TYPE define to keep alternate versions using
	      this iterator type (for experimentations and testing)

Affected files ...

.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#8 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#5 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#7 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/unix.c#7 edit

Differences ...

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

@@ -19,19 +19,31 @@
 
 struct socket_type;
 struct socket_type_list;
+struct socket_type_iterator;
 
 __BEGIN_DECLS
 const char		    *netstat_strerror(int);
 
 /* "Socket" */
+
+/* Socket list: */
 struct socket_type_list	    *netstat_stl_alloc(void);
 struct socket_type	    *netstat_stl_find(struct socket_type_list *list,
 				unsigned short family, const char *name);
 void			    netstat_stl_iterate(struct socket_type_list *list,
 				void (*func)(struct socket_type *));
-
 void	netstat_stl_free(struct socket_type_list *list);
 int	netstat_stl_geterror(struct socket_type_list *list);
+int	netstat_stl_refresh(struct socket_type_list *list);
+
+/* Socket iterator: */
+int		    netstat_sti_alloc(struct socket_type_list *list,
+			struct socket_type_iterator **iterator);
+struct socket_type  *netstat_sti_first(struct socket_type_iterator *iterator);
+struct socket_type  *netstat_sti_next(struct socket_type_iterator *iterator);
+void		    netstat_sti_free(struct socket_type_iterator *iterator);
+
+void	netstat_st_free(struct socket_type *stp);
 
 int	netstat_socket(int domain, int type, int protocol,
 	    struct socket_type_list *, int flags, void *kvm_handle);

==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#5 (text+ko) ====

@@ -47,6 +47,12 @@
 	int			    stl_error;
 };
 
+struct socket_type_iterator {
+	struct socket_type_list	    *sti_list;
+	struct socket_type	    *sti_first;
+	struct socket_type	    *sti_next;
+};
+
 int kread(kvm_t *kvm, u_long kvm_pointer, void *address, size_t size);
 int kread_string(kvm_t *kvm, u_long kvm_pointer, char *buffer, int buflen);
 

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

@@ -101,7 +101,13 @@
 }
 
 void
-_netstat_stl_free(struct socket_type_list *list)
+netstat_st_free(struct socket_type *stp)
+{
+	free(stp);
+}
+
+void
+netstat_stl_free(struct socket_type_list *list)
 {
 	_netstat_stl_empty(list);
 	free(list);
@@ -113,6 +119,13 @@
 	return (list->stl_error);
 }
 
+int
+netstat_stl_refresh(struct socket_type_list *list)
+{
+	/* XXX: it is a dummy yet */
+	return (0);
+}
+
 struct socket_type *
 netstat_stl_find(struct socket_type_list *list, unsigned short family,
     const char *name)
@@ -155,6 +168,65 @@
 	/* XXX: empty */
 }
 
+/* Socket type iterator. */
+int
+netstat_sti_alloc(struct socket_type_list *list,
+    struct socket_type_iterator **iterator)
+{
+	struct socket_type_iterator *itp;
+
+	itp = malloc(sizeof(*itp));
+	if (itp == NULL)
+		return (-1);
+
+	bzero(itp, sizeof(*itp));
+
+	/* XXX: Should it copy the list? */
+	itp->sti_list = list;
+	itp->sti_first = LIST_FIRST(&list->stl_list);
+	itp->sti_next = itp->sti_first;
+	*iterator = itp;
+	return (0);
+}
+
+struct socket_type *
+netstat_sti_first(struct socket_type_iterator *iterator)
+{
+	struct socket_type *stp;
+
+	stp = malloc(sizeof(*stp));
+	if (stp == NULL)
+		return (NULL);
+	
+	memcpy(stp, iterator->sti_first, sizeof(*stp));
+	return (stp);
+}
+
+struct socket_type *
+netstat_sti_next(struct socket_type_iterator *iterator)
+{
+	struct socket_type *stp;
+
+	stp = malloc(sizeof(*stp));
+	if (stp == NULL)
+		return (NULL);
+	
+	iterator->sti_next = LIST_NEXT(iterator->sti_next, st_list);
+	if (iterator->sti_next == NULL) {
+		free(stp);
+		return (NULL);
+	}
+
+	memcpy(stp, iterator->sti_next, sizeof(*stp));
+	return (stp);
+}
+
+void
+netstat_sti_free(struct socket_type_iterator *iterator)
+{
+	free(iterator);
+}
+
 /* Accessor functions. */
 int
 netstat_st_get_family(const struct socket_type *stp)

==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/unix.c#7 (text+ko) ====

@@ -66,6 +66,8 @@
 #include <netstat.h>
 #include "extern.h"
 
+#define USE_ITERATOR_TYPE
+
 static	void unixdomainpr(struct socket_type *);
 
 void
@@ -74,6 +76,10 @@
 	struct socket_type_list *stlp;
 	int error, st_flags;
 	kvm_t *kvm;
+#ifdef USE_ITERATOR_TYPE
+	struct socket_type_iterator *stip;
+	struct socket_type	    *stp;
+#endif
 
 	kvm = (kvm_t *)kvmd;
 	stlp = netstat_stl_alloc();
@@ -102,7 +108,19 @@
 	 * It should be guaranteed (by libnetstat) that only active PCBs are
 	 * returned.
 	 */
+#ifdef USE_ITERATOR_TYPE
+	if (netstat_sti_alloc(stlp, &stip) < 0) {
+		warnx("netstat_sti_alloc");	
+		return;
+	}
+	for (stp = netstat_sti_first(stip); stp != NULL;
+	    stp = netstat_sti_next(stip)) {
+		unixdomainpr(stp);
+		netstat_st_free(stp);
+	}
+#else
 	netstat_stl_iterate(stlp, unixdomainpr);
+#endif
 }
 
 static void


More information about the p4-projects mailing list