Skip to content

Commit cde164a

Browse files
rostedtacmel
authored andcommitted
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>
1 parent 5693c92 commit cde164a

5 files changed

Lines changed: 94 additions & 54 deletions

File tree

‎tools/lib/api/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ LIB_H=
99
LIB_OBJS=
1010

1111
LIB_H += fs/debugfs.h
12+
LIB_H += fs/findfs.h
1213
LIB_H += fs/fs.h
1314
# See comment below about piggybacking...
1415
LIB_H += fd/array.h
1516

1617
LIB_OBJS += $(OUTPUT)fs/debugfs.o
18+
LIB_OBJS += $(OUTPUT)fs/findfs.o
1719
LIB_OBJS += $(OUTPUT)fs/fs.o
1820
# XXX piggybacking here, need to introduce libapikfd, or rename this
1921
# to plain libapik.a and make it have it all api goodies

‎tools/lib/api/fs/debugfs.c‎

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,21 @@ static const char * const debugfs_known_mountpoints[] = {
2020

2121
static bool debugfs_found;
2222

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-
3723
/* find the path to the mounted debugfs */
3824
const char *debugfs_find_mountpoint(void)
3925
{
40-
const char * const *ptr;
41-
char type[100];
42-
FILE *fp;
26+
const char *ret;
4327

4428
if (debugfs_found)
4529
return (const char *)debugfs_mountpoint;
4630

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;
5636

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;
7538
}
7639

7740
/* mount the debugfs somewhere if it's not mounted */

‎tools/lib/api/fs/debugfs.h‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
#ifndef __API_DEBUGFS_H__
22
#define __API_DEBUGFS_H__
33

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"
145

156
#ifndef DEBUGFS_MAGIC
167
#define DEBUGFS_MAGIC 0x64626720

‎tools/lib/api/fs/findfs.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎tools/lib/api/fs/findfs.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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__ */

0 commit comments

Comments
 (0)