Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 18 Jul 2018 10:20:40 +0000 (UTC)
From:      Hans Petter Selasky <hselasky@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r336452 - head/contrib/ofed/libibumad
Message-ID:  <201807181020.w6IAKeXY087467@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hselasky
Date: Wed Jul 18 10:20:39 2018
New Revision: 336452
URL: https://svnweb.freebsd.org/changeset/base/336452

Log:
  Add ability to parse sysfs paths under FreeBSD in libibumad.
  
  Add the ability to to parse sysfs paths to sysctl nodes by replacing '/' with '.'
  
  Submitted by:		slavash@
  MFC after:		1 week
  Sponsored by:		Mellanox Technologies

Modified:
  head/contrib/ofed/libibumad/sysfs.c
  head/contrib/ofed/libibumad/sysfs.h
  head/contrib/ofed/libibumad/umad.c

Modified: head/contrib/ofed/libibumad/sysfs.c
==============================================================================
--- head/contrib/ofed/libibumad/sysfs.c	Wed Jul 18 10:12:53 2018	(r336451)
+++ head/contrib/ofed/libibumad/sysfs.c	Wed Jul 18 10:20:39 2018	(r336452)
@@ -62,12 +62,8 @@ int sys_read_string(const char *dir_name, const char *
 
 	snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
 
-	for (s = &path[0]; *s != '\0'; s++)
-		if (*s == '/')
-			*s = '.';
-
 	len = max_len;
-	if (sysctlbyname(&path[1], str, &len, NULL, 0) == -1)
+	if (sysctlbyname(PATH_TO_SYS(path), str, &len, NULL, 0) == -1)
 		return ret_code();
 
 	str[(len < max_len) ? len : max_len - 1] = 0;
@@ -170,11 +166,8 @@ sys_scandir(const char *dirname, struct dirent ***name
 	int i;
 
 	*namelist = NULL;
-	/* Skip the leading / */
-	strncpy(name, &dirname[1], sizeof(name));
-	for (s = &name[0]; *s != '\0'; s++)
-		if (*s == '/')
-			*s = '.';
+	if (strlcpy(name, PATH_TO_SYS(dirname), sizeof(name)) >= sizeof(name))
+		return (-EINVAL);
 	/*
 	 * Resolve the path.
 	 */
@@ -259,7 +252,7 @@ out:
 	if (cnt && compar)
 		qsort(names, cnt, sizeof(struct dirent *),
 		    (int (*)(const void *, const void *))compar);
-		
+
 	*namelist = names;
 
 	return (cnt);

Modified: head/contrib/ofed/libibumad/sysfs.h
==============================================================================
--- head/contrib/ofed/libibumad/sysfs.h	Wed Jul 18 10:12:53 2018	(r336451)
+++ head/contrib/ofed/libibumad/sysfs.h	Wed Jul 18 10:20:39 2018	(r336452)
@@ -34,6 +34,8 @@
 #define _UMAD_SYSFS_H
 
 #include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
 #include <infiniband/types.h>
 #include <infiniband/umad.h>
 
@@ -48,5 +50,38 @@ extern int sys_read_uint(const char *dir_name, const c
 extern int sys_scandir(const char *dirname, struct dirent ***namelist,
     int (*select)(const struct dirent *),
     int (*compar)(const struct dirent **, const struct dirent **));
+
+#ifdef __FreeBSD__
+static inline const char *
+path_to_sysctl(const char *path, int out_len, char *out)
+{
+	const char *retval = out;
+
+	/* Validate that out is at least as long as the original path */
+	if (out_len < (strlen(path) + 1))
+		return NULL;
+
+	while (*path == '/')
+		path++;
+
+	while (*path) {
+		if (*path == '/') {
+			if (*(path + 1) == '/')
+				*out = '.';
+			else
+				*out++ = '.';
+		} else
+			*out++ = *path;
+		path++;
+	}
+	*out = 0;
+	return (retval);
+}
+
+#define	PATH_TO_SYS(str) \
+	path_to_sysctl(str, strlen(str) + 1, alloca(strlen(str) + 1))
+#else
+#define	PATH_TO_SYS(str) str
+#endif
 
 #endif /* _UMAD_SYSFS_H */

Modified: head/contrib/ofed/libibumad/umad.c
==============================================================================
--- head/contrib/ofed/libibumad/umad.c	Wed Jul 18 10:12:53 2018	(r336451)
+++ head/contrib/ofed/libibumad/umad.c	Wed Jul 18 10:20:39 2018	(r336452)
@@ -509,14 +509,14 @@ int umad_init(void)
 	TRACE("umad_init");
 	if (sys_read_uint(IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE, &abi_version) < 0) {
 		IBWARN
-		    ("can't read ABI version from %s/%s (%m): is ib_umad module loaded?",
-		     IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE);
+		    ("can't read ABI version from %s (%m): is ibcore module loaded?",
+		     PATH_TO_SYS(IB_UMAD_ABI_DIR "/" IB_UMAD_ABI_FILE));
 		return -1;
 	}
 	if (abi_version < IB_UMAD_ABI_VERSION) {
 		IBWARN
-		    ("wrong ABI version: %s/%s is %d but library minimal ABI is %d",
-		     IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE, abi_version,
+		    ("wrong ABI version: %s is %d but library minimal ABI is %d",
+		     PATH_TO_SYS(IB_UMAD_ABI_DIR "/" IB_UMAD_ABI_FILE), abi_version,
 		     IB_UMAD_ABI_VERSION);
 		return -1;
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201807181020.w6IAKeXY087467>