Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Steven Rostedt (Red Hat) and committed by
Arnaldo Carvalho de Melo
cde164ae 5693c926

+94 -54
+2
tools/lib/api/Makefile
··· 9 9 LIB_OBJS= 10 10 11 11 LIB_H += fs/debugfs.h 12 + LIB_H += fs/findfs.h 12 13 LIB_H += fs/fs.h 13 14 # See comment below about piggybacking... 14 15 LIB_H += fd/array.h 15 16 16 17 LIB_OBJS += $(OUTPUT)fs/debugfs.o 18 + LIB_OBJS += $(OUTPUT)fs/findfs.o 17 19 LIB_OBJS += $(OUTPUT)fs/fs.o 18 20 # XXX piggybacking here, need to introduce libapikfd, or rename this 19 21 # to plain libapik.a and make it have it all api goodies
+7 -44
tools/lib/api/fs/debugfs.c
··· 20 20 21 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 23 /* find the path to the mounted debugfs */ 38 24 const char *debugfs_find_mountpoint(void) 39 25 { 40 - const char * const *ptr; 41 - char type[100]; 42 - FILE *fp; 26 + const char *ret; 43 27 44 28 if (debugfs_found) 45 29 return (const char *)debugfs_mountpoint; 46 30 47 - ptr = debugfs_known_mountpoints; 48 - while (*ptr) { 49 - if (debugfs_valid_mountpoint(*ptr) == 0) { 50 - debugfs_found = true; 51 - strcpy(debugfs_mountpoint, *ptr); 52 - return debugfs_mountpoint; 53 - } 54 - ptr++; 55 - } 31 + ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC, 32 + debugfs_mountpoint, PATH_MAX + 1, 33 + debugfs_known_mountpoints); 34 + if (ret) 35 + debugfs_found = true; 56 36 57 - /* give up and parse /proc/mounts */ 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; 37 + return ret; 75 38 } 76 39 77 40 /* mount the debugfs somewhere if it's not mounted */
+1 -10
tools/lib/api/fs/debugfs.h
··· 1 1 #ifndef __API_DEBUGFS_H__ 2 2 #define __API_DEBUGFS_H__ 3 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 4 + #include "findfs.h" 14 5 15 6 #ifndef DEBUGFS_MAGIC 16 7 #define DEBUGFS_MAGIC 0x64626720
+63
tools/lib/api/fs/findfs.c
··· 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 + }
+21
tools/lib/api/fs/findfs.h
··· 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__ */