1 // SPDX-License-Identifier: GPL-2.0
10 #include <sys/types.h>
14 #include <sys/mount.h>
17 #include "debug-internal.h"
20 #define STR(x) _STR(x)
23 #define SYSFS_MAGIC 0x62656572
26 #ifndef PROC_SUPER_MAGIC
27 #define PROC_SUPER_MAGIC 0x9fa0
31 #define DEBUGFS_MAGIC 0x64626720
35 #define TRACEFS_MAGIC 0x74726163
38 #ifndef HUGETLBFS_MAGIC
39 #define HUGETLBFS_MAGIC 0x958458f6
43 #define BPF_FS_MAGIC 0xcafe4a11
46 static const char * const sysfs__fs_known_mountpoints[] = {
51 static const char * const procfs__known_mountpoints[] = {
56 #ifndef DEBUGFS_DEFAULT_PATH
57 #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
60 static const char * const debugfs__known_mountpoints[] = {
67 #ifndef TRACEFS_DEFAULT_PATH
68 #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
71 static const char * const tracefs__known_mountpoints[] = {
73 "/sys/kernel/debug/tracing",
79 static const char * const hugetlbfs__known_mountpoints[] = {
83 static const char * const bpf_fs__known_mountpoints[] = {
90 const char * const *mounts;
106 #ifndef TRACEFS_MAGIC
107 #define TRACEFS_MAGIC 0x74726163
110 static struct fs fs__entries[] = {
113 .mounts = sysfs__fs_known_mountpoints,
114 .magic = SYSFS_MAGIC,
119 .mounts = procfs__known_mountpoints,
120 .magic = PROC_SUPER_MAGIC,
125 .mounts = debugfs__known_mountpoints,
126 .magic = DEBUGFS_MAGIC,
131 .mounts = tracefs__known_mountpoints,
132 .magic = TRACEFS_MAGIC,
137 .mounts = hugetlbfs__known_mountpoints,
138 .magic = HUGETLBFS_MAGIC,
143 .mounts = bpf_fs__known_mountpoints,
144 .magic = BPF_FS_MAGIC,
149 static bool fs__read_mounts(struct fs *fs)
155 fp = fopen("/proc/mounts", "r");
160 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
161 fs->path, type) == 2) {
163 if (strcmp(type, fs->name) == 0)
169 return fs->found = found;
172 static int fs__valid_mount(const char *fs, long magic)
176 if (statfs(fs, &st_fs) < 0)
178 else if ((long)st_fs.f_type != magic)
184 static bool fs__check_mounts(struct fs *fs)
186 const char * const *ptr;
190 if (fs__valid_mount(*ptr, fs->magic) == 0) {
192 strcpy(fs->path, *ptr);
201 static void mem_toupper(char *f, size_t len)
211 * Check for "NAME_PATH" environment variable to override fs location (for
212 * testing). This matches the recommendation in Documentation/admin-guide/sysfs-rules.rst
215 static bool fs__env_override(struct fs *fs)
218 size_t name_len = strlen(fs->name);
219 /* name + "_PATH" + '\0' */
220 char upper_name[name_len + 5 + 1];
222 memcpy(upper_name, fs->name, name_len);
223 mem_toupper(upper_name, name_len);
224 strcpy(&upper_name[name_len], "_PATH");
226 override_path = getenv(upper_name);
232 strncpy(fs->path, override_path, sizeof(fs->path) - 1);
233 fs->path[sizeof(fs->path) - 1] = '\0';
237 static const char *fs__get_mountpoint(struct fs *fs)
239 if (fs__env_override(fs))
242 if (fs__check_mounts(fs))
245 if (fs__read_mounts(fs))
251 static const char *fs__mountpoint(int idx)
253 struct fs *fs = &fs__entries[idx];
256 return (const char *)fs->path;
258 /* the mount point was already checked for the mount point
259 * but and did not exist, so return NULL to avoid scanning again.
260 * This makes the found and not found paths cost equivalent
261 * in case of multiple calls.
266 return fs__get_mountpoint(fs);
269 static const char *mount_overload(struct fs *fs)
271 size_t name_len = strlen(fs->name);
272 /* "PERF_" + name + "_ENVIRONMENT" + '\0' */
273 char upper_name[5 + name_len + 12 + 1];
275 snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
276 mem_toupper(upper_name, name_len);
278 return getenv(upper_name) ?: *fs->mounts;
281 static const char *fs__mount(int idx)
283 struct fs *fs = &fs__entries[idx];
284 const char *mountpoint;
286 if (fs__mountpoint(idx))
287 return (const char *)fs->path;
289 mountpoint = mount_overload(fs);
291 if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
294 return fs__check_mounts(fs) ? fs->path : NULL;
297 #define FS(name, idx) \
298 const char *name##__mountpoint(void) \
300 return fs__mountpoint(idx); \
303 const char *name##__mount(void) \
305 return fs__mount(idx); \
308 bool name##__configured(void) \
310 return name##__mountpoint() != NULL; \
313 FS(sysfs, FS__SYSFS);
314 FS(procfs, FS__PROCFS);
315 FS(debugfs, FS__DEBUGFS);
316 FS(tracefs, FS__TRACEFS);
317 FS(hugetlbfs, FS__HUGETLBFS);
318 FS(bpf_fs, FS__BPF_FS);
320 int filename__read_int(const char *filename, int *value)
323 int fd = open(filename, O_RDONLY), err = -1;
328 if (read(fd, line, sizeof(line)) > 0) {
337 static int filename__read_ull_base(const char *filename,
338 unsigned long long *value, int base)
341 int fd = open(filename, O_RDONLY), err = -1;
346 if (read(fd, line, sizeof(line)) > 0) {
347 *value = strtoull(line, NULL, base);
348 if (*value != ULLONG_MAX)
357 * Parses @value out of @filename with strtoull.
358 * By using 16 for base to treat the number as hex.
360 int filename__read_xll(const char *filename, unsigned long long *value)
362 return filename__read_ull_base(filename, value, 16);
366 * Parses @value out of @filename with strtoull.
367 * By using 0 for base, the strtoull detects the
368 * base automatically (see man strtoull).
370 int filename__read_ull(const char *filename, unsigned long long *value)
372 return filename__read_ull_base(filename, value, 0);
375 #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
377 int filename__read_str(const char *filename, char **buf, size_t *sizep)
379 size_t size = 0, alloc_size = 0;
380 void *bf = NULL, *nbf;
382 char sbuf[STRERR_BUFSIZE];
384 fd = open(filename, O_RDONLY);
389 if (size == alloc_size) {
390 alloc_size += BUFSIZ;
391 nbf = realloc(bf, alloc_size);
400 n = read(fd, bf + size, alloc_size - size);
403 pr_warning("read failed %d: %s\n", errno,
404 strerror_r(errno, sbuf, sizeof(sbuf)));
425 int filename__write_int(const char *filename, int value)
427 int fd = open(filename, O_WRONLY), err = -1;
433 sprintf(buf, "%d", value);
434 if (write(fd, buf, sizeof(buf)) == sizeof(buf))
441 int procfs__read_str(const char *entry, char **buf, size_t *sizep)
444 const char *procfs = procfs__mountpoint();
449 snprintf(path, sizeof(path), "%s/%s", procfs, entry);
451 return filename__read_str(path, buf, sizep);
454 static int sysfs__read_ull_base(const char *entry,
455 unsigned long long *value, int base)
458 const char *sysfs = sysfs__mountpoint();
463 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
465 return filename__read_ull_base(path, value, base);
468 int sysfs__read_xll(const char *entry, unsigned long long *value)
470 return sysfs__read_ull_base(entry, value, 16);
473 int sysfs__read_ull(const char *entry, unsigned long long *value)
475 return sysfs__read_ull_base(entry, value, 0);
478 int sysfs__read_int(const char *entry, int *value)
481 const char *sysfs = sysfs__mountpoint();
486 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
488 return filename__read_int(path, value);
491 int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
494 const char *sysfs = sysfs__mountpoint();
499 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
501 return filename__read_str(path, buf, sizep);
504 int sysfs__read_bool(const char *entry, bool *value)
510 ret = sysfs__read_str(entry, &buf, &size);
533 int sysctl__read_int(const char *sysctl, int *value)
536 const char *procfs = procfs__mountpoint();
541 snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
543 return filename__read_int(path, value);
546 int sysfs__write_int(const char *entry, int value)
549 const char *sysfs = sysfs__mountpoint();
554 if (snprintf(path, sizeof(path), "%s/%s", sysfs, entry) >= PATH_MAX)
557 return filename__write_int(path, value);