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
This commit is contained in:
Hans Petter Selasky 2018-07-18 10:20:39 +00:00
parent 7b9b93a8dd
commit 7600168453
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=336452
3 changed files with 43 additions and 15 deletions

View File

@ -62,12 +62,8 @@ int sys_read_string(const char *dir_name, const char *file_name, char *str, int
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 ***namelist,
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 @@ sys_scandir(const char *dirname, struct dirent ***namelist,
if (cnt && compar)
qsort(names, cnt, sizeof(struct dirent *),
(int (*)(const void *, const void *))compar);
*namelist = names;
return (cnt);

View File

@ -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>
@ -49,4 +51,37 @@ 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 */

View File

@ -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;
}