Whamcloud - gitweb
LU-12352 libcfs: crashes with certain cpu part numbers
[fs/lustre-release.git] / libcfs / libcfs / util / param.c
index 74a3726..18fe84d 100644 (file)
@@ -38,6 +38,8 @@
 #include <unistd.h>
 #include <linux/limits.h>
 #include <libcfs/util/string.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
 
 /**
  * Get parameter path matching the pattern
 int
 cfs_get_param_paths(glob_t *paths, const char *pattern, ...)
 {
-       char path[PATH_MAX] = "{/sys/{fs,kernel/debug}/{lnet,lustre}/,"
-                              "/proc/{fs,sys}/{lnet,lustre}/}";
+       char topdir[PATH_MAX] = "{/sys/{fs,kernel/debug}/{lnet,lustre},"
+                               "/proc/{fs,sys}/{lnet,lustre}}";
        static bool test_mounted = false;
-       size_t len = strlen(path);
+       char path[PATH_MAX];
        char buf[PATH_MAX];
+       struct statfs statfsbuf;
        va_list args;
        int rc;
 
+
        if (test_mounted)
                goto skip_mounting;
        test_mounted = true;
 
+       rc = statfs("/sys/kernel/debug/", &statfsbuf);
+       if (rc == 0 && statfsbuf.f_type == DEBUGFS_MAGIC)
+               goto skip_mounting;
+
        if (mount("none", "/sys/kernel/debug", "debugfs", 0, "") == -1) {
                /* Already mounted or don't have permission to mount is okay */
                if (errno != EPERM && errno != EBUSY)
@@ -119,9 +127,9 @@ skip_mounting:
                errno = EINVAL;
                return -1;
        }
-       len += rc;
 
-       if (strlcat(path, buf, sizeof(path)) != len) {
+       if (snprintf(path, sizeof(path), "%s/%s", topdir, buf) >=
+           sizeof(path)) {
                errno = E2BIG;
                return -1;
        }