diff options
author | Steven Rostedt (Red Hat) <rostedt@goodmis.org> | 2015-02-02 14:35:03 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-02-07 07:51:34 -0500 |
commit | cde164aee9e0343831467035eb96dd5506742648 (patch) | |
tree | 8353fdc346aa319d7b88990ea007b22be9a372f3 /tools/lib | |
parent | 5693c92660970851e95f769ff27397f5098a6296 (diff) |
tools lib fs: Add helper to find mounted file systems
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>
Diffstat (limited to 'tools/lib')
-rw-r--r-- | tools/lib/api/Makefile | 2 | ||||
-rw-r--r-- | tools/lib/api/fs/debugfs.c | 51 | ||||
-rw-r--r-- | tools/lib/api/fs/debugfs.h | 11 | ||||
-rw-r--r-- | tools/lib/api/fs/findfs.c | 63 | ||||
-rw-r--r-- | tools/lib/api/fs/findfs.h | 21 |
5 files changed, 94 insertions, 54 deletions
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index 36c08b1f4afb..22b2f15d7255 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile | |||
@@ -9,11 +9,13 @@ LIB_H= | |||
9 | LIB_OBJS= | 9 | LIB_OBJS= |
10 | 10 | ||
11 | LIB_H += fs/debugfs.h | 11 | LIB_H += fs/debugfs.h |
12 | LIB_H += fs/findfs.h | ||
12 | LIB_H += fs/fs.h | 13 | LIB_H += fs/fs.h |
13 | # See comment below about piggybacking... | 14 | # See comment below about piggybacking... |
14 | LIB_H += fd/array.h | 15 | LIB_H += fd/array.h |
15 | 16 | ||
16 | LIB_OBJS += $(OUTPUT)fs/debugfs.o | 17 | LIB_OBJS += $(OUTPUT)fs/debugfs.o |
18 | LIB_OBJS += $(OUTPUT)fs/findfs.o | ||
17 | LIB_OBJS += $(OUTPUT)fs/fs.o | 19 | LIB_OBJS += $(OUTPUT)fs/fs.o |
18 | # XXX piggybacking here, need to introduce libapikfd, or rename this | 20 | # XXX piggybacking here, need to introduce libapikfd, or rename this |
19 | # to plain libapik.a and make it have it all api goodies | 21 | # to plain libapik.a and make it have it all api goodies |
diff --git a/tools/lib/api/fs/debugfs.c b/tools/lib/api/fs/debugfs.c index d21d4d6b4fd2..91e1668348ce 100644 --- a/tools/lib/api/fs/debugfs.c +++ b/tools/lib/api/fs/debugfs.c | |||
@@ -20,58 +20,21 @@ static const char * const debugfs_known_mountpoints[] = { | |||
20 | 20 | ||
21 | static bool debugfs_found; | 21 | static bool debugfs_found; |
22 | 22 | ||
23 | /* verify that a mountpoint is actually a debugfs instance */ | ||
24 | |||
25 | static int debugfs_valid_mountpoint(const char *debugfs) | ||
26 | { | ||
27 | struct statfs st_fs; | ||
28 | |||
29 | if (statfs(debugfs, &st_fs) < 0) | ||
30 | return -ENOENT; | ||
31 | else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC) | ||
32 | return -ENOENT; | ||
33 | |||
34 | return 0; | ||
35 | } | ||
36 | |||
37 | /* find the path to the mounted debugfs */ | 23 | /* find the path to the mounted debugfs */ |
38 | const char *debugfs_find_mountpoint(void) | 24 | const char *debugfs_find_mountpoint(void) |
39 | { | 25 | { |
40 | const char * const *ptr; | 26 | const char *ret; |
41 | char type[100]; | ||
42 | FILE *fp; | ||
43 | 27 | ||
44 | if (debugfs_found) | 28 | if (debugfs_found) |
45 | return (const char *)debugfs_mountpoint; | 29 | return (const char *)debugfs_mountpoint; |
46 | 30 | ||
47 | ptr = debugfs_known_mountpoints; | 31 | ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC, |
48 | while (*ptr) { | 32 | debugfs_mountpoint, PATH_MAX + 1, |
49 | if (debugfs_valid_mountpoint(*ptr) == 0) { | 33 | debugfs_known_mountpoints); |
50 | debugfs_found = true; | 34 | if (ret) |
51 | strcpy(debugfs_mountpoint, *ptr); | 35 | debugfs_found = true; |
52 | return debugfs_mountpoint; | ||
53 | } | ||
54 | ptr++; | ||
55 | } | ||
56 | 36 | ||
57 | /* give up and parse /proc/mounts */ | 37 | return ret; |
58 | fp = fopen("/proc/mounts", "r"); | ||
59 | if (fp == NULL) | ||
60 | return NULL; | ||
61 | |||
62 | while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", | ||
63 | debugfs_mountpoint, type) == 2) { | ||
64 | if (strcmp(type, "debugfs") == 0) | ||
65 | break; | ||
66 | } | ||
67 | fclose(fp); | ||
68 | |||
69 | if (strcmp(type, "debugfs") != 0) | ||
70 | return NULL; | ||
71 | |||
72 | debugfs_found = true; | ||
73 | |||
74 | return debugfs_mountpoint; | ||
75 | } | 38 | } |
76 | 39 | ||
77 | /* mount the debugfs somewhere if it's not mounted */ | 40 | /* mount the debugfs somewhere if it's not mounted */ |
diff --git a/tools/lib/api/fs/debugfs.h b/tools/lib/api/fs/debugfs.h index 77bb36a95b07..1074ac81358e 100644 --- a/tools/lib/api/fs/debugfs.h +++ b/tools/lib/api/fs/debugfs.h | |||
@@ -1,16 +1,7 @@ | |||
1 | #ifndef __API_DEBUGFS_H__ | 1 | #ifndef __API_DEBUGFS_H__ |
2 | #define __API_DEBUGFS_H__ | 2 | #define __API_DEBUGFS_H__ |
3 | 3 | ||
4 | #define _STR(x) #x | 4 | #include "findfs.h" |
5 | #define STR(x) _STR(x) | ||
6 | |||
7 | /* | ||
8 | * On most systems <limits.h> would have given us this, but not on some systems | ||
9 | * (e.g. GNU/Hurd). | ||
10 | */ | ||
11 | #ifndef PATH_MAX | ||
12 | #define PATH_MAX 4096 | ||
13 | #endif | ||
14 | 5 | ||
15 | #ifndef DEBUGFS_MAGIC | 6 | #ifndef DEBUGFS_MAGIC |
16 | #define DEBUGFS_MAGIC 0x64626720 | 7 | #define DEBUGFS_MAGIC 0x64626720 |
diff --git a/tools/lib/api/fs/findfs.c b/tools/lib/api/fs/findfs.c new file mode 100644 index 000000000000..49946cb6d7af --- /dev/null +++ b/tools/lib/api/fs/findfs.c | |||
@@ -0,0 +1,63 @@ | |||
1 | #include <errno.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <stdbool.h> | ||
6 | #include <sys/vfs.h> | ||
7 | |||
8 | #include "findfs.h" | ||
9 | |||
10 | /* verify that a mountpoint is actually the type we want */ | ||
11 | |||
12 | int valid_mountpoint(const char *mount, long magic) | ||
13 | { | ||
14 | struct statfs st_fs; | ||
15 | |||
16 | if (statfs(mount, &st_fs) < 0) | ||
17 | return -ENOENT; | ||
18 | else if ((long)st_fs.f_type != magic) | ||
19 | return -ENOENT; | ||
20 | |||
21 | return 0; | ||
22 | } | ||
23 | |||
24 | /* find the path to a mounted file system */ | ||
25 | const char *find_mountpoint(const char *fstype, long magic, | ||
26 | char *mountpoint, int len, | ||
27 | const char * const *known_mountpoints) | ||
28 | { | ||
29 | const char * const *ptr; | ||
30 | char format[128]; | ||
31 | char type[100]; | ||
32 | FILE *fp; | ||
33 | |||
34 | if (known_mountpoints) { | ||
35 | ptr = known_mountpoints; | ||
36 | while (*ptr) { | ||
37 | if (valid_mountpoint(*ptr, magic) == 0) { | ||
38 | strncpy(mountpoint, *ptr, len - 1); | ||
39 | mountpoint[len-1] = 0; | ||
40 | return mountpoint; | ||
41 | } | ||
42 | ptr++; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | /* give up and parse /proc/mounts */ | ||
47 | fp = fopen("/proc/mounts", "r"); | ||
48 | if (fp == NULL) | ||
49 | return NULL; | ||
50 | |||
51 | snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len); | ||
52 | |||
53 | while (fscanf(fp, format, mountpoint, type) == 2) { | ||
54 | if (strcmp(type, fstype) == 0) | ||
55 | break; | ||
56 | } | ||
57 | fclose(fp); | ||
58 | |||
59 | if (strcmp(type, fstype) != 0) | ||
60 | return NULL; | ||
61 | |||
62 | return mountpoint; | ||
63 | } | ||
diff --git a/tools/lib/api/fs/findfs.h b/tools/lib/api/fs/findfs.h new file mode 100644 index 000000000000..9e7d876791e1 --- /dev/null +++ b/tools/lib/api/fs/findfs.h | |||
@@ -0,0 +1,21 @@ | |||
1 | #ifndef __API_FINDFS_H__ | ||
2 | #define __API_FINDFS_H__ | ||
3 | |||
4 | #define _STR(x) #x | ||
5 | #define STR(x) _STR(x) | ||
6 | |||
7 | /* | ||
8 | * On most systems <limits.h> would have given us this, but not on some systems | ||
9 | * (e.g. GNU/Hurd). | ||
10 | */ | ||
11 | #ifndef PATH_MAX | ||
12 | #define PATH_MAX 4096 | ||
13 | #endif | ||
14 | |||
15 | const char *find_mountpoint(const char *fstype, long magic, | ||
16 | char *mountpoint, int len, | ||
17 | const char * const *known_mountpoints); | ||
18 | |||
19 | int valid_mountpoint(const char *mount, long magic); | ||
20 | |||
21 | #endif /* __API_FINDFS_H__ */ | ||