Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 Aug 2018 08:57:41 +0000 (UTC)
From:      Hans Petter Selasky <hselasky@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r337116 - stable/11/contrib/ofed/libibumad
Message-ID:  <201808020857.w728vfq9062361@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hselasky
Date: Thu Aug  2 08:57:40 2018
New Revision: 337116
URL: https://svnweb.freebsd.org/changeset/base/337116

Log:
  MFC r336452:
  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@
  Sponsored by:		Mellanox Technologies

Modified:
  stable/11/contrib/ofed/libibumad/sysfs.c
  stable/11/contrib/ofed/libibumad/sysfs.h
  stable/11/contrib/ofed/libibumad/umad.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/contrib/ofed/libibumad/sysfs.c
==============================================================================
--- stable/11/contrib/ofed/libibumad/sysfs.c	Thu Aug  2 08:56:27 2018	(r337115)
+++ stable/11/contrib/ofed/libibumad/sysfs.c	Thu Aug  2 08:57:40 2018	(r337116)
@@ -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: stable/11/contrib/ofed/libibumad/sysfs.h
==============================================================================
--- stable/11/contrib/ofed/libibumad/sysfs.h	Thu Aug  2 08:56:27 2018	(r337115)
+++ stable/11/contrib/ofed/libibumad/sysfs.h	Thu Aug  2 08:57:40 2018	(r337116)
@@ -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: stable/11/contrib/ofed/libibumad/umad.c
==============================================================================
--- stable/11/contrib/ofed/libibumad/umad.c	Thu Aug  2 08:56:27 2018	(r337115)
+++ stable/11/contrib/ofed/libibumad/umad.c	Thu Aug  2 08:57:40 2018	(r337116)
@@ -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?201808020857.w728vfq9062361>