tools lib fs: Add helper to find mounted file systems
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>
Mon, 2 Feb 2015 19:35:03 +0000 (14:35 -0500)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 7 Feb 2015 12:51:34 +0000 (13:51 +0100)
In preparation for adding tracefs for perf to use, create a findfs
helper utility that find_debugfs uses instead of hard coding the search
in the code. This will allow for a find_tracefs to be used as well.

Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: http://lkml.kernel.org/r/20150202193552.735023362@goodmis.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/api/Makefile
tools/lib/api/fs/debugfs.c
tools/lib/api/fs/debugfs.h
tools/lib/api/fs/findfs.c [new file with mode: 0644]
tools/lib/api/fs/findfs.h [new file with mode: 0644]

index 36c08b1f4afbc9379007c3b49e79fb5f87a3f1c9..22b2f15d72558497823eb767b62ef37b58728893 100644 (file)
@@ -9,11 +9,13 @@ LIB_H=
 LIB_OBJS=
 
 LIB_H += fs/debugfs.h
+LIB_H += fs/findfs.h
 LIB_H += fs/fs.h
 # See comment below about piggybacking...
 LIB_H += fd/array.h
 
 LIB_OBJS += $(OUTPUT)fs/debugfs.o
+LIB_OBJS += $(OUTPUT)fs/findfs.o
 LIB_OBJS += $(OUTPUT)fs/fs.o
 # XXX piggybacking here, need to introduce libapikfd, or rename this
 # to plain libapik.a and make it have it all api goodies
index d21d4d6b4fd2c79e5fe3b3ade3edc1dbec033c97..91e1668348ce6f254047736c167e8817a52c76e3 100644 (file)
@@ -20,58 +20,21 @@ static const char * const debugfs_known_mountpoints[] = {
 
 static bool debugfs_found;
 
-/* verify that a mountpoint is actually a debugfs instance */
-
-static int debugfs_valid_mountpoint(const char *debugfs)
-{
-       struct statfs st_fs;
-
-       if (statfs(debugfs, &st_fs) < 0)
-               return -ENOENT;
-       else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC)
-               return -ENOENT;
-
-       return 0;
-}
-
 /* find the path to the mounted debugfs */
 const char *debugfs_find_mountpoint(void)
 {
-       const char * const *ptr;
-       char type[100];
-       FILE *fp;
+       const char *ret;
 
        if (debugfs_found)
                return (const char *)debugfs_mountpoint;
 
-       ptr = debugfs_known_mountpoints;
-       while (*ptr) {
-               if (debugfs_valid_mountpoint(*ptr) == 0) {
-                       debugfs_found = true;
-                       strcpy(debugfs_mountpoint, *ptr);
-                       return debugfs_mountpoint;
-               }
-               ptr++;
-       }
+       ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC,
+                             debugfs_mountpoint, PATH_MAX + 1,
+                             debugfs_known_mountpoints);
+       if (ret)
+               debugfs_found = true;
 
-       /* give up and parse /proc/mounts */
-       fp = fopen("/proc/mounts", "r");
-       if (fp == NULL)
-               return NULL;
-
-       while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
-                     debugfs_mountpoint, type) == 2) {
-               if (strcmp(type, "debugfs") == 0)
-                       break;
-       }
-       fclose(fp);
-
-       if (strcmp(type, "debugfs") != 0)
-               return NULL;
-
-       debugfs_found = true;
-
-       return debugfs_mountpoint;
+       return ret;
 }
 
 /* mount the debugfs somewhere if it's not mounted */
index 77bb36a95b07339bb1e420ba815de2a7382f0e34..1074ac81358e79df9c481425f731597178b03ac5 100644 (file)
@@ -1,16 +1,7 @@
 #ifndef __API_DEBUGFS_H__
 #define __API_DEBUGFS_H__
 
-#define _STR(x) #x
-#define STR(x) _STR(x)
-
-/*
- * On most systems <limits.h> would have given us this, but  not on some systems
- * (e.g. GNU/Hurd).
- */
-#ifndef PATH_MAX
-#define PATH_MAX 4096
-#endif
+#include "findfs.h"
 
 #ifndef DEBUGFS_MAGIC
 #define DEBUGFS_MAGIC          0x64626720
diff --git a/tools/lib/api/fs/findfs.c b/tools/lib/api/fs/findfs.c
new file mode 100644 (file)
index 0000000..49946cb
--- /dev/null
@@ -0,0 +1,63 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <sys/vfs.h>
+
+#include "findfs.h"
+
+/* verify that a mountpoint is actually the type we want */
+
+int valid_mountpoint(const char *mount, long magic)
+{
+       struct statfs st_fs;
+
+       if (statfs(mount, &st_fs) < 0)
+               return -ENOENT;
+       else if ((long)st_fs.f_type != magic)
+               return -ENOENT;
+
+       return 0;
+}
+
+/* find the path to a mounted file system */
+const char *find_mountpoint(const char *fstype, long magic,
+                           char *mountpoint, int len,
+                           const char * const *known_mountpoints)
+{
+       const char * const *ptr;
+       char format[128];
+       char type[100];
+       FILE *fp;
+
+       if (known_mountpoints) {
+               ptr = known_mountpoints;
+               while (*ptr) {
+                       if (valid_mountpoint(*ptr, magic) == 0) {
+                               strncpy(mountpoint, *ptr, len - 1);
+                               mountpoint[len-1] = 0;
+                               return mountpoint;
+                       }
+                       ptr++;
+               }
+       }
+
+       /* give up and parse /proc/mounts */
+       fp = fopen("/proc/mounts", "r");
+       if (fp == NULL)
+               return NULL;
+
+       snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len);
+
+       while (fscanf(fp, format, mountpoint, type) == 2) {
+               if (strcmp(type, fstype) == 0)
+                       break;
+       }
+       fclose(fp);
+
+       if (strcmp(type, fstype) != 0)
+               return NULL;
+
+       return mountpoint;
+}
diff --git a/tools/lib/api/fs/findfs.h b/tools/lib/api/fs/findfs.h
new file mode 100644 (file)
index 0000000..9e7d876
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef __API_FINDFS_H__
+#define __API_FINDFS_H__
+
+#define _STR(x) #x
+#define STR(x) _STR(x)
+
+/*
+ * On most systems <limits.h> would have given us this, but  not on some systems
+ * (e.g. GNU/Hurd).
+ */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
+const char *find_mountpoint(const char *fstype, long magic,
+                           char *mountpoint, int len,
+                           const char * const *known_mountpoints);
+
+int valid_mountpoint(const char *mount, long magic);
+
+#endif /* __API_FINDFS_H__ */