svn commit: r334415 - head/lib/libdevinfo

Warner Losh imp at FreeBSD.org
Thu May 31 02:58:07 UTC 2018


Author: imp
Date: Thu May 31 02:58:03 2018
New Revision: 334415
URL: https://svnweb.freebsd.org/changeset/base/334415

Log:
  Update to device enumeration protocol 2
  
  The new protocol from the kernel encodes things as a string table,
  extract it into fields. strdup the strings, and free them when we're
  done.
  
  Differential Revision: https://reviews.freebsd.org/D15629

Modified:
  head/lib/libdevinfo/devinfo.c
  head/lib/libdevinfo/devinfo_var.h

Modified: head/lib/libdevinfo/devinfo.c
==============================================================================
--- head/lib/libdevinfo/devinfo.c	Thu May 31 02:57:58 2018	(r334414)
+++ head/lib/libdevinfo/devinfo.c	Thu May 31 02:58:03 2018	(r334415)
@@ -175,7 +175,7 @@ devinfo_init_devices(int generation)
 	int			name2oid[2];
 	int			oid[CTL_MAXNAME + 12];
 	size_t			oidlen, rlen;
-	char			*name;
+	char			*name, *walker, *ep;
 	int			error;
 
 	/* 
@@ -229,22 +229,31 @@ devinfo_init_devices(int generation)
 			return(ENOMEM);
 		dd->dd_dev.dd_handle = udev.dv_handle;
 		dd->dd_dev.dd_parent = udev.dv_parent;
-		snprintf(dd->dd_name, sizeof(dd->dd_name), "%s", udev.dv_name);
-		dd->dd_dev.dd_name = &dd->dd_name[0];
-		snprintf(dd->dd_desc, sizeof(dd->dd_desc), "%s", udev.dv_desc);
-		dd->dd_dev.dd_desc = &dd->dd_desc[0];
-		snprintf(dd->dd_drivername, sizeof(dd->dd_drivername), "%s",
-		    udev.dv_drivername);
-		dd->dd_dev.dd_drivername = &dd->dd_drivername[0];
-		snprintf(dd->dd_pnpinfo, sizeof(dd->dd_pnpinfo), "%s",
-		    udev.dv_pnpinfo);
-		dd->dd_dev.dd_pnpinfo = &dd->dd_pnpinfo[0];
-		snprintf(dd->dd_location, sizeof(dd->dd_location), "%s",
-		    udev.dv_location);
-		dd->dd_dev.dd_location = &dd->dd_location[0];
 		dd->dd_dev.dd_devflags = udev.dv_devflags;
 		dd->dd_dev.dd_flags = udev.dv_flags;
 		dd->dd_dev.dd_state = udev.dv_state;
+
+		walker = udev.dv_fields;
+		ep = walker + sizeof(udev.dv_fields);
+		dd->dd_name = NULL;
+		dd->dd_desc = NULL;
+		dd->dd_drivername = NULL;
+		dd->dd_pnpinfo = NULL;
+		dd->dd_location = NULL;
+#define UNPACK(x)							\
+		dd->dd_dev.x = dd->x = strdup(walker);			\
+		if (dd->x == NULL)					\
+			return(ENOMEM);					\
+		if (walker + strnlen(walker, ep - walker) >= ep)	\
+			return(EINVAL);					\
+		walker += strlen(walker) + 1;
+
+		UNPACK(dd_name);
+		UNPACK(dd_desc);
+		UNPACK(dd_drivername);
+		UNPACK(dd_pnpinfo);
+		UNPACK(dd_location);
+#undef UNPACK
 		TAILQ_INSERT_TAIL(&devinfo_dev, dd, dd_link);
 	}
 	debug("fetched %d devices", dev_idx);
@@ -367,6 +376,11 @@ devinfo_free(void)
 
 	while ((dd = TAILQ_FIRST(&devinfo_dev)) != NULL) {
 		TAILQ_REMOVE(&devinfo_dev, dd, dd_link);
+		free(dd->dd_name);
+		free(dd->dd_desc);
+		free(dd->dd_drivername);
+		free(dd->dd_pnpinfo);
+		free(dd->dd_location);
 		free(dd);
 	}
 	while ((dm = TAILQ_FIRST(&devinfo_rman)) != NULL) {

Modified: head/lib/libdevinfo/devinfo_var.h
==============================================================================
--- head/lib/libdevinfo/devinfo_var.h	Thu May 31 02:57:58 2018	(r334414)
+++ head/lib/libdevinfo/devinfo_var.h	Thu May 31 02:58:03 2018	(r334415)
@@ -45,11 +45,11 @@
  */
 struct devinfo_i_dev {
 	struct devinfo_dev		dd_dev;
-	char				dd_name[DEVINFO_STRLEN];
-	char				dd_desc[DEVINFO_STRLEN];
-	char				dd_drivername[DEVINFO_STRLEN];
-	char				dd_pnpinfo[DEVINFO_STRLEN * 4];
-	char				dd_location[DEVINFO_STRLEN * 4];
+	char				*dd_name;
+	char				*dd_desc;
+	char				*dd_drivername;
+	char				*dd_pnpinfo;
+	char				*dd_location;
 	uint32_t			dd_devflags;
 	uint16_t			dd_flags;
 	device_state_t			dd_state;


More information about the svn-src-head mailing list