1 /* TODO merge/factor in debugfs.c here */
10 #include <sys/types.h>
18 static const char * const sysfs__fs_known_mountpoints
[] = {
23 static const char * const procfs__known_mountpoints
[] = {
30 const char * const *mounts
;
31 char path
[PATH_MAX
+ 1];
41 static struct fs fs__entries
[] = {
44 .mounts
= sysfs__fs_known_mountpoints
,
49 .mounts
= procfs__known_mountpoints
,
50 .magic
= PROC_SUPER_MAGIC
,
54 static bool fs__read_mounts(struct fs
*fs
)
60 fp
= fopen("/proc/mounts", "r");
65 fscanf(fp
, "%*s %" STR(PATH_MAX
) "s %99s %*s %*d %*d\n",
66 fs
->path
, type
) == 2) {
68 if (strcmp(type
, fs
->name
) == 0)
73 return fs
->found
= found
;
76 static int fs__valid_mount(const char *fs
, long magic
)
80 if (statfs(fs
, &st_fs
) < 0)
82 else if ((long)st_fs
.f_type
!= magic
)
88 static bool fs__check_mounts(struct fs
*fs
)
90 const char * const *ptr
;
94 if (fs__valid_mount(*ptr
, fs
->magic
) == 0) {
96 strcpy(fs
->path
, *ptr
);
105 static void mem_toupper(char *f
, size_t len
)
115 * Check for "NAME_PATH" environment variable to override fs location (for
116 * testing). This matches the recommendation in Documentation/sysfs-rules.txt
119 static bool fs__env_override(struct fs
*fs
)
122 size_t name_len
= strlen(fs
->name
);
123 /* name + "_PATH" + '\0' */
124 char upper_name
[name_len
+ 5 + 1];
125 memcpy(upper_name
, fs
->name
, name_len
);
126 mem_toupper(upper_name
, name_len
);
127 strcpy(&upper_name
[name_len
], "_PATH");
129 override_path
= getenv(upper_name
);
134 strncpy(fs
->path
, override_path
, sizeof(fs
->path
));
138 static const char *fs__get_mountpoint(struct fs
*fs
)
140 if (fs__env_override(fs
))
143 if (fs__check_mounts(fs
))
146 if (fs__read_mounts(fs
))
152 static const char *fs__mountpoint(int idx
)
154 struct fs
*fs
= &fs__entries
[idx
];
157 return (const char *)fs
->path
;
159 return fs__get_mountpoint(fs
);
162 #define FS__MOUNTPOINT(name, idx) \
163 const char *name##__mountpoint(void) \
165 return fs__mountpoint(idx); \
168 FS__MOUNTPOINT(sysfs
, FS__SYSFS
);
169 FS__MOUNTPOINT(procfs
, FS__PROCFS
);
171 int filename__read_int(const char *filename
, int *value
)
174 int fd
= open(filename
, O_RDONLY
), err
= -1;
179 if (read(fd
, line
, sizeof(line
)) > 0) {
188 int sysctl__read_int(const char *sysctl
, int *value
)
191 const char *procfs
= procfs__mountpoint();
196 snprintf(path
, sizeof(path
), "%s/sys/%s", procfs
, sysctl
);
198 return filename__read_int(path
, value
);