fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / util / fs.c
blobf5be1f26e724d303ff4a98051b8cf933e1ec1cb4
2 /* TODO merge/factor into tools/lib/lk/debugfs.c */
4 #include "util.h"
5 #include "util/fs.h"
7 static const char * const sysfs__fs_known_mountpoints[] = {
8 "/sys",
9 0,
12 static const char * const procfs__known_mountpoints[] = {
13 "/proc",
17 struct fs {
18 const char *name;
19 const char * const *mounts;
20 char path[PATH_MAX + 1];
21 bool found;
22 long magic;
25 enum {
26 FS__SYSFS = 0,
27 FS__PROCFS = 1,
30 static struct fs fs__entries[] = {
31 [FS__SYSFS] = {
32 .name = "sysfs",
33 .mounts = sysfs__fs_known_mountpoints,
34 .magic = SYSFS_MAGIC,
36 [FS__PROCFS] = {
37 .name = "proc",
38 .mounts = procfs__known_mountpoints,
39 .magic = PROC_SUPER_MAGIC,
43 static bool fs__read_mounts(struct fs *fs)
45 bool found = false;
46 char type[100];
47 FILE *fp;
49 fp = fopen("/proc/mounts", "r");
50 if (fp == NULL)
51 return NULL;
53 while (!found &&
54 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
55 fs->path, type) == 2) {
57 if (strcmp(type, fs->name) == 0)
58 found = true;
61 fclose(fp);
62 return fs->found = found;
65 static int fs__valid_mount(const char *fs, long magic)
67 struct statfs st_fs;
69 if (statfs(fs, &st_fs) < 0)
70 return -ENOENT;
71 else if (st_fs.f_type != magic)
72 return -ENOENT;
74 return 0;
77 static bool fs__check_mounts(struct fs *fs)
79 const char * const *ptr;
81 ptr = fs->mounts;
82 while (*ptr) {
83 if (fs__valid_mount(*ptr, fs->magic) == 0) {
84 fs->found = true;
85 strcpy(fs->path, *ptr);
86 return true;
88 ptr++;
91 return false;
94 static const char *fs__get_mountpoint(struct fs *fs)
96 if (fs__check_mounts(fs))
97 return fs->path;
99 return fs__read_mounts(fs) ? fs->path : NULL;
102 static const char *fs__mountpoint(int idx)
104 struct fs *fs = &fs__entries[idx];
106 if (fs->found)
107 return (const char *)fs->path;
109 return fs__get_mountpoint(fs);
112 #define FS__MOUNTPOINT(name, idx) \
113 const char *name##__mountpoint(void) \
115 return fs__mountpoint(idx); \
118 FS__MOUNTPOINT(sysfs, FS__SYSFS);
119 FS__MOUNTPOINT(procfs, FS__PROCFS);