aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/api/fs/fs.c
diff options
context:
space:
mode:
authorIngo Molnar <mingo@kernel.org>2014-02-22 11:26:24 -0500
committerIngo Molnar <mingo@kernel.org>2014-02-22 11:26:24 -0500
commit7e74efcf76c16f851df5c838c143c4a1865ea9fa (patch)
tree71aba57213c2f5c5a47fd1ab93d4fea100f68008 /tools/lib/api/fs/fs.c
parente9d9768824a18212712ae3afbebd5bfef05176f4 (diff)
parenta15ad2f5360c821f030c53266ebf467738249c68 (diff)
Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux into perf/core
Pull perf/core improvements and fixes from Arnaldo Carvalho de Melo: User Visible: * Allow setting preferred callchain method in .perfconfig (Jiri Olsa) * Show in what binaries/modules 'perf probe's are set (Masami Hiramatsu) * Support distro-style debuginfo for uprobe in 'perf probe' (Masami Hiramatsu) Developer stuff: * Move some hashing and fs related code from tools/perf/util/ to tools/lib/ so that it can be used by more tools/ living utilities (Borislav Petkov) * Prepare DWARF unwinding code for using an elfutils alternative unwinding library (Jiri Olsa) * Fix DWARF unwind max_stack processing (Jiri Olsa) * Add dwarf unwind 'perf test' entry (Jiri Olsa) * 'perf probe' improvements including memory leak fixes, sharing the intlist class with other tools, uprobes/kprobes code sharing and use of ref_reloc_sym (Masami Hiramatsu) * Shorten sample symbol resolving by adding cpumode to struct addr_location (Arnaldo Carvalho de Melo) Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'tools/lib/api/fs/fs.c')
-rw-r--r--tools/lib/api/fs/fs.c124
1 files changed, 124 insertions, 0 deletions
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
12static const char * const sysfs__fs_known_mountpoints[] = {
13 "/sys",
14 0,
15};
16
17static const char * const procfs__known_mountpoints[] = {
18 "/proc",
19 0,
20};
21
22struct fs {
23 const char *name;
24 const char * const *mounts;
25 char path[PATH_MAX + 1];
26 bool found;
27 long magic;
28};
29
30enum {
31 FS__SYSFS = 0,
32 FS__PROCFS = 1,
33};
34
35static 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
48static 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
70static 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
82static 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
99static 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
107static 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) \
118const char *name##__mountpoint(void) \
119{ \
120 return fs__mountpoint(idx); \
121}
122
123FS__MOUNTPOINT(sysfs, FS__SYSFS);
124FS__MOUNTPOINT(procfs, FS__PROCFS);