diff options
| author | Borislav Petkov <bp@suse.de> | 2013-12-09 11:14:24 -0500 |
|---|---|---|
| committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2014-02-18 07:34:49 -0500 |
| commit | cd0cfad74eb88e54ba9d205da3ed376e48981448 (patch) | |
| tree | 5eb32d93129d48be57b9b9e933e14c62c6f0d435 /tools/lib/api | |
| parent | c9b951c4d12f0b2e9a07dd459c554bc05628d092 (diff) | |
perf tools: Move fs.* to lib/api/fs/
Move to generic library and kill magic.h as it is needed only in fs.h.
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Robert Richter <rric@kernel.org>
Cc: Stanislav Fomichev <stfomichev@yandex-team.ru>
Cc: Stephane Eranian <eranian@google.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/r/1386605664-24041-3-git-send-email-bp@alien8.de
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/api')
| -rw-r--r-- | tools/lib/api/Makefile | 2 | ||||
| -rw-r--r-- | tools/lib/api/fs/fs.c | 124 | ||||
| -rw-r--r-- | tools/lib/api/fs/fs.h | 14 |
3 files changed, 140 insertions, 0 deletions
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index ed2f51e11b80..ce00f7ee6455 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile | |||
| @@ -9,8 +9,10 @@ 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/fs.h | ||
| 12 | 13 | ||
| 13 | LIB_OBJS += $(OUTPUT)fs/debugfs.o | 14 | LIB_OBJS += $(OUTPUT)fs/debugfs.o |
| 15 | LIB_OBJS += $(OUTPUT)fs/fs.o | ||
| 14 | 16 | ||
| 15 | LIBFILE = libapikfs.a | 17 | LIBFILE = libapikfs.a |
| 16 | 18 | ||
diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c new file mode 100644 index 000000000000..5b5eb788996e --- /dev/null +++ b/tools/lib/api/fs/fs.c | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | /* TODO merge/factor in debugfs.c here */ | ||
| 2 | |||
| 3 | #include <errno.h> | ||
| 4 | #include <stdbool.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <sys/vfs.h> | ||
| 8 | |||
| 9 | #include "debugfs.h" | ||
| 10 | #include "fs.h" | ||
| 11 | |||
| 12 | static const char * const sysfs__fs_known_mountpoints[] = { | ||
| 13 | "/sys", | ||
| 14 | 0, | ||
| 15 | }; | ||
| 16 | |||
| 17 | static const char * const procfs__known_mountpoints[] = { | ||
| 18 | "/proc", | ||
| 19 | 0, | ||
| 20 | }; | ||
| 21 | |||
| 22 | struct fs { | ||
| 23 | const char *name; | ||
| 24 | const char * const *mounts; | ||
| 25 | char path[PATH_MAX + 1]; | ||
| 26 | bool found; | ||
| 27 | long magic; | ||
| 28 | }; | ||
| 29 | |||
| 30 | enum { | ||
| 31 | FS__SYSFS = 0, | ||
| 32 | FS__PROCFS = 1, | ||
| 33 | }; | ||
| 34 | |||
| 35 | static struct fs fs__entries[] = { | ||
| 36 | [FS__SYSFS] = { | ||
| 37 | .name = "sysfs", | ||
| 38 | .mounts = sysfs__fs_known_mountpoints, | ||
| 39 | .magic = SYSFS_MAGIC, | ||
| 40 | }, | ||
| 41 | [FS__PROCFS] = { | ||
| 42 | .name = "proc", | ||
| 43 | .mounts = procfs__known_mountpoints, | ||
| 44 | .magic = PROC_SUPER_MAGIC, | ||
| 45 | }, | ||
| 46 | }; | ||
| 47 | |||
| 48 | static bool fs__read_mounts(struct fs *fs) | ||
| 49 | { | ||
| 50 | bool found = false; | ||
| 51 | char type[100]; | ||
| 52 | FILE *fp; | ||
| 53 | |||
| 54 | fp = fopen("/proc/mounts", "r"); | ||
| 55 | if (fp == NULL) | ||
| 56 | return NULL; | ||
| 57 | |||
| 58 | while (!found && | ||
| 59 | fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", | ||
| 60 | fs->path, type) == 2) { | ||
| 61 | |||
| 62 | if (strcmp(type, fs->name) == 0) | ||
| 63 | found = true; | ||
| 64 | } | ||
| 65 | |||
| 66 | fclose(fp); | ||
| 67 | return fs->found = found; | ||
| 68 | } | ||
| 69 | |||
| 70 | static int fs__valid_mount(const char *fs, long magic) | ||
| 71 | { | ||
| 72 | struct statfs st_fs; | ||
| 73 | |||
| 74 | if (statfs(fs, &st_fs) < 0) | ||
| 75 | return -ENOENT; | ||
| 76 | else if (st_fs.f_type != magic) | ||
| 77 | return -ENOENT; | ||
| 78 | |||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | static bool fs__check_mounts(struct fs *fs) | ||
| 83 | { | ||
| 84 | const char * const *ptr; | ||
| 85 | |||
| 86 | ptr = fs->mounts; | ||
| 87 | while (*ptr) { | ||
| 88 | if (fs__valid_mount(*ptr, fs->magic) == 0) { | ||
| 89 | fs->found = true; | ||
| 90 | strcpy(fs->path, *ptr); | ||
| 91 | return true; | ||
| 92 | } | ||
| 93 | ptr++; | ||
| 94 | } | ||
| 95 | |||
| 96 | return false; | ||
| 97 | } | ||
| 98 | |||
| 99 | static const char *fs__get_mountpoint(struct fs *fs) | ||
| 100 | { | ||
| 101 | if (fs__check_mounts(fs)) | ||
| 102 | return fs->path; | ||
| 103 | |||
| 104 | return fs__read_mounts(fs) ? fs->path : NULL; | ||
| 105 | } | ||
| 106 | |||
| 107 | static const char *fs__mountpoint(int idx) | ||
| 108 | { | ||
| 109 | struct fs *fs = &fs__entries[idx]; | ||
| 110 | |||
| 111 | if (fs->found) | ||
| 112 | return (const char *)fs->path; | ||
| 113 | |||
| 114 | return fs__get_mountpoint(fs); | ||
| 115 | } | ||
| 116 | |||
| 117 | #define FS__MOUNTPOINT(name, idx) \ | ||
| 118 | const char *name##__mountpoint(void) \ | ||
| 119 | { \ | ||
| 120 | return fs__mountpoint(idx); \ | ||
| 121 | } | ||
| 122 | |||
| 123 | FS__MOUNTPOINT(sysfs, FS__SYSFS); | ||
| 124 | FS__MOUNTPOINT(procfs, FS__PROCFS); | ||
diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h new file mode 100644 index 000000000000..cb7049551f33 --- /dev/null +++ b/tools/lib/api/fs/fs.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #ifndef __API_FS__ | ||
| 2 | #define __API_FS__ | ||
| 3 | |||
| 4 | #ifndef SYSFS_MAGIC | ||
| 5 | #define SYSFS_MAGIC 0x62656572 | ||
| 6 | #endif | ||
| 7 | |||
| 8 | #ifndef PROC_SUPER_MAGIC | ||
| 9 | #define PROC_SUPER_MAGIC 0x9fa0 | ||
| 10 | #endif | ||
| 11 | |||
| 12 | const char *sysfs__mountpoint(void); | ||
| 13 | const char *procfs__mountpoint(void); | ||
| 14 | #endif /* __API_FS__ */ | ||
