xref: /linux/tools/lib/api/fs/fs.c (revision 06ed6aa56ffac9241e03a24649e8d048f8f1b10c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <ctype.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/vfs.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <sys/mount.h>
15 
16 #include "fs.h"
17 #include "debug-internal.h"
18 
19 #define _STR(x) #x
20 #define STR(x) _STR(x)
21 
22 #ifndef SYSFS_MAGIC
23 #define SYSFS_MAGIC            0x62656572
24 #endif
25 
26 #ifndef PROC_SUPER_MAGIC
27 #define PROC_SUPER_MAGIC       0x9fa0
28 #endif
29 
30 #ifndef DEBUGFS_MAGIC
31 #define DEBUGFS_MAGIC          0x64626720
32 #endif
33 
34 #ifndef TRACEFS_MAGIC
35 #define TRACEFS_MAGIC          0x74726163
36 #endif
37 
38 #ifndef HUGETLBFS_MAGIC
39 #define HUGETLBFS_MAGIC        0x958458f6
40 #endif
41 
42 #ifndef BPF_FS_MAGIC
43 #define BPF_FS_MAGIC           0xcafe4a11
44 #endif
45 
46 static const char * const sysfs__fs_known_mountpoints[] = {
47 	"/sys",
48 	0,
49 };
50 
51 static const char * const procfs__known_mountpoints[] = {
52 	"/proc",
53 	0,
54 };
55 
56 #ifndef DEBUGFS_DEFAULT_PATH
57 #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
58 #endif
59 
60 static const char * const debugfs__known_mountpoints[] = {
61 	DEBUGFS_DEFAULT_PATH,
62 	"/debug",
63 	0,
64 };
65 
66 
67 #ifndef TRACEFS_DEFAULT_PATH
68 #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
69 #endif
70 
71 static const char * const tracefs__known_mountpoints[] = {
72 	TRACEFS_DEFAULT_PATH,
73 	"/sys/kernel/debug/tracing",
74 	"/tracing",
75 	"/trace",
76 	0,
77 };
78 
79 static const char * const hugetlbfs__known_mountpoints[] = {
80 	0,
81 };
82 
83 static const char * const bpf_fs__known_mountpoints[] = {
84 	"/sys/fs/bpf",
85 	0,
86 };
87 
88 struct fs {
89 	const char		*name;
90 	const char * const	*mounts;
91 	char			 path[PATH_MAX];
92 	bool			 found;
93 	long			 magic;
94 };
95 
96 enum {
97 	FS__SYSFS   = 0,
98 	FS__PROCFS  = 1,
99 	FS__DEBUGFS = 2,
100 	FS__TRACEFS = 3,
101 	FS__HUGETLBFS = 4,
102 	FS__BPF_FS = 5,
103 };
104 
105 #ifndef TRACEFS_MAGIC
106 #define TRACEFS_MAGIC 0x74726163
107 #endif
108 
109 static struct fs fs__entries[] = {
110 	[FS__SYSFS] = {
111 		.name	= "sysfs",
112 		.mounts	= sysfs__fs_known_mountpoints,
113 		.magic	= SYSFS_MAGIC,
114 	},
115 	[FS__PROCFS] = {
116 		.name	= "proc",
117 		.mounts	= procfs__known_mountpoints,
118 		.magic	= PROC_SUPER_MAGIC,
119 	},
120 	[FS__DEBUGFS] = {
121 		.name	= "debugfs",
122 		.mounts	= debugfs__known_mountpoints,
123 		.magic	= DEBUGFS_MAGIC,
124 	},
125 	[FS__TRACEFS] = {
126 		.name	= "tracefs",
127 		.mounts	= tracefs__known_mountpoints,
128 		.magic	= TRACEFS_MAGIC,
129 	},
130 	[FS__HUGETLBFS] = {
131 		.name	= "hugetlbfs",
132 		.mounts = hugetlbfs__known_mountpoints,
133 		.magic	= HUGETLBFS_MAGIC,
134 	},
135 	[FS__BPF_FS] = {
136 		.name	= "bpf",
137 		.mounts = bpf_fs__known_mountpoints,
138 		.magic	= BPF_FS_MAGIC,
139 	},
140 };
141 
142 static bool fs__read_mounts(struct fs *fs)
143 {
144 	bool found = false;
145 	char type[100];
146 	FILE *fp;
147 
148 	fp = fopen("/proc/mounts", "r");
149 	if (fp == NULL)
150 		return NULL;
151 
152 	while (!found &&
153 	       fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
154 		      fs->path, type) == 2) {
155 
156 		if (strcmp(type, fs->name) == 0)
157 			found = true;
158 	}
159 
160 	fclose(fp);
161 	return fs->found = found;
162 }
163 
164 static int fs__valid_mount(const char *fs, long magic)
165 {
166 	struct statfs st_fs;
167 
168 	if (statfs(fs, &st_fs) < 0)
169 		return -ENOENT;
170 	else if ((long)st_fs.f_type != magic)
171 		return -ENOENT;
172 
173 	return 0;
174 }
175 
176 static bool fs__check_mounts(struct fs *fs)
177 {
178 	const char * const *ptr;
179 
180 	ptr = fs->mounts;
181 	while (*ptr) {
182 		if (fs__valid_mount(*ptr, fs->magic) == 0) {
183 			fs->found = true;
184 			strcpy(fs->path, *ptr);
185 			return true;
186 		}
187 		ptr++;
188 	}
189 
190 	return false;
191 }
192 
193 static void mem_toupper(char *f, size_t len)
194 {
195 	while (len) {
196 		*f = toupper(*f);
197 		f++;
198 		len--;
199 	}
200 }
201 
202 /*
203  * Check for "NAME_PATH" environment variable to override fs location (for
204  * testing). This matches the recommendation in Documentation/admin-guide/sysfs-rules.rst
205  * for SYSFS_PATH.
206  */
207 static bool fs__env_override(struct fs *fs)
208 {
209 	char *override_path;
210 	size_t name_len = strlen(fs->name);
211 	/* name + "_PATH" + '\0' */
212 	char upper_name[name_len + 5 + 1];
213 
214 	memcpy(upper_name, fs->name, name_len);
215 	mem_toupper(upper_name, name_len);
216 	strcpy(&upper_name[name_len], "_PATH");
217 
218 	override_path = getenv(upper_name);
219 	if (!override_path)
220 		return false;
221 
222 	fs->found = true;
223 	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
224 	fs->path[sizeof(fs->path) - 1] = '\0';
225 	return true;
226 }
227 
228 static const char *fs__get_mountpoint(struct fs *fs)
229 {
230 	if (fs__env_override(fs))
231 		return fs->path;
232 
233 	if (fs__check_mounts(fs))
234 		return fs->path;
235 
236 	if (fs__read_mounts(fs))
237 		return fs->path;
238 
239 	return NULL;
240 }
241 
242 static const char *fs__mountpoint(int idx)
243 {
244 	struct fs *fs = &fs__entries[idx];
245 
246 	if (fs->found)
247 		return (const char *)fs->path;
248 
249 	return fs__get_mountpoint(fs);
250 }
251 
252 static const char *mount_overload(struct fs *fs)
253 {
254 	size_t name_len = strlen(fs->name);
255 	/* "PERF_" + name + "_ENVIRONMENT" + '\0' */
256 	char upper_name[5 + name_len + 12 + 1];
257 
258 	snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
259 	mem_toupper(upper_name, name_len);
260 
261 	return getenv(upper_name) ?: *fs->mounts;
262 }
263 
264 static const char *fs__mount(int idx)
265 {
266 	struct fs *fs = &fs__entries[idx];
267 	const char *mountpoint;
268 
269 	if (fs__mountpoint(idx))
270 		return (const char *)fs->path;
271 
272 	mountpoint = mount_overload(fs);
273 
274 	if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
275 		return NULL;
276 
277 	return fs__check_mounts(fs) ? fs->path : NULL;
278 }
279 
280 #define FS(name, idx)				\
281 const char *name##__mountpoint(void)		\
282 {						\
283 	return fs__mountpoint(idx);		\
284 }						\
285 						\
286 const char *name##__mount(void)			\
287 {						\
288 	return fs__mount(idx);			\
289 }						\
290 						\
291 bool name##__configured(void)			\
292 {						\
293 	return name##__mountpoint() != NULL;	\
294 }
295 
296 FS(sysfs,   FS__SYSFS);
297 FS(procfs,  FS__PROCFS);
298 FS(debugfs, FS__DEBUGFS);
299 FS(tracefs, FS__TRACEFS);
300 FS(hugetlbfs, FS__HUGETLBFS);
301 FS(bpf_fs, FS__BPF_FS);
302 
303 int filename__read_int(const char *filename, int *value)
304 {
305 	char line[64];
306 	int fd = open(filename, O_RDONLY), err = -1;
307 
308 	if (fd < 0)
309 		return -1;
310 
311 	if (read(fd, line, sizeof(line)) > 0) {
312 		*value = atoi(line);
313 		err = 0;
314 	}
315 
316 	close(fd);
317 	return err;
318 }
319 
320 static int filename__read_ull_base(const char *filename,
321 				   unsigned long long *value, int base)
322 {
323 	char line[64];
324 	int fd = open(filename, O_RDONLY), err = -1;
325 
326 	if (fd < 0)
327 		return -1;
328 
329 	if (read(fd, line, sizeof(line)) > 0) {
330 		*value = strtoull(line, NULL, base);
331 		if (*value != ULLONG_MAX)
332 			err = 0;
333 	}
334 
335 	close(fd);
336 	return err;
337 }
338 
339 /*
340  * Parses @value out of @filename with strtoull.
341  * By using 16 for base to treat the number as hex.
342  */
343 int filename__read_xll(const char *filename, unsigned long long *value)
344 {
345 	return filename__read_ull_base(filename, value, 16);
346 }
347 
348 /*
349  * Parses @value out of @filename with strtoull.
350  * By using 0 for base, the strtoull detects the
351  * base automatically (see man strtoull).
352  */
353 int filename__read_ull(const char *filename, unsigned long long *value)
354 {
355 	return filename__read_ull_base(filename, value, 0);
356 }
357 
358 #define STRERR_BUFSIZE  128     /* For the buffer size of strerror_r */
359 
360 int filename__read_str(const char *filename, char **buf, size_t *sizep)
361 {
362 	size_t size = 0, alloc_size = 0;
363 	void *bf = NULL, *nbf;
364 	int fd, n, err = 0;
365 	char sbuf[STRERR_BUFSIZE];
366 
367 	fd = open(filename, O_RDONLY);
368 	if (fd < 0)
369 		return -errno;
370 
371 	do {
372 		if (size == alloc_size) {
373 			alloc_size += BUFSIZ;
374 			nbf = realloc(bf, alloc_size);
375 			if (!nbf) {
376 				err = -ENOMEM;
377 				break;
378 			}
379 
380 			bf = nbf;
381 		}
382 
383 		n = read(fd, bf + size, alloc_size - size);
384 		if (n < 0) {
385 			if (size) {
386 				pr_warn("read failed %d: %s\n", errno,
387 					strerror_r(errno, sbuf, sizeof(sbuf)));
388 				err = 0;
389 			} else
390 				err = -errno;
391 
392 			break;
393 		}
394 
395 		size += n;
396 	} while (n > 0);
397 
398 	if (!err) {
399 		*sizep = size;
400 		*buf   = bf;
401 	} else
402 		free(bf);
403 
404 	close(fd);
405 	return err;
406 }
407 
408 int filename__write_int(const char *filename, int value)
409 {
410 	int fd = open(filename, O_WRONLY), err = -1;
411 	char buf[64];
412 
413 	if (fd < 0)
414 		return err;
415 
416 	sprintf(buf, "%d", value);
417 	if (write(fd, buf, sizeof(buf)) == sizeof(buf))
418 		err = 0;
419 
420 	close(fd);
421 	return err;
422 }
423 
424 int procfs__read_str(const char *entry, char **buf, size_t *sizep)
425 {
426 	char path[PATH_MAX];
427 	const char *procfs = procfs__mountpoint();
428 
429 	if (!procfs)
430 		return -1;
431 
432 	snprintf(path, sizeof(path), "%s/%s", procfs, entry);
433 
434 	return filename__read_str(path, buf, sizep);
435 }
436 
437 static int sysfs__read_ull_base(const char *entry,
438 				unsigned long long *value, int base)
439 {
440 	char path[PATH_MAX];
441 	const char *sysfs = sysfs__mountpoint();
442 
443 	if (!sysfs)
444 		return -1;
445 
446 	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
447 
448 	return filename__read_ull_base(path, value, base);
449 }
450 
451 int sysfs__read_xll(const char *entry, unsigned long long *value)
452 {
453 	return sysfs__read_ull_base(entry, value, 16);
454 }
455 
456 int sysfs__read_ull(const char *entry, unsigned long long *value)
457 {
458 	return sysfs__read_ull_base(entry, value, 0);
459 }
460 
461 int sysfs__read_int(const char *entry, int *value)
462 {
463 	char path[PATH_MAX];
464 	const char *sysfs = sysfs__mountpoint();
465 
466 	if (!sysfs)
467 		return -1;
468 
469 	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
470 
471 	return filename__read_int(path, value);
472 }
473 
474 int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
475 {
476 	char path[PATH_MAX];
477 	const char *sysfs = sysfs__mountpoint();
478 
479 	if (!sysfs)
480 		return -1;
481 
482 	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
483 
484 	return filename__read_str(path, buf, sizep);
485 }
486 
487 int sysfs__read_bool(const char *entry, bool *value)
488 {
489 	char *buf;
490 	size_t size;
491 	int ret;
492 
493 	ret = sysfs__read_str(entry, &buf, &size);
494 	if (ret < 0)
495 		return ret;
496 
497 	switch (buf[0]) {
498 	case '1':
499 	case 'y':
500 	case 'Y':
501 		*value = true;
502 		break;
503 	case '0':
504 	case 'n':
505 	case 'N':
506 		*value = false;
507 		break;
508 	default:
509 		ret = -1;
510 	}
511 
512 	free(buf);
513 
514 	return ret;
515 }
516 int sysctl__read_int(const char *sysctl, int *value)
517 {
518 	char path[PATH_MAX];
519 	const char *procfs = procfs__mountpoint();
520 
521 	if (!procfs)
522 		return -1;
523 
524 	snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
525 
526 	return filename__read_int(path, value);
527 }
528 
529 int sysfs__write_int(const char *entry, int value)
530 {
531 	char path[PATH_MAX];
532 	const char *sysfs = sysfs__mountpoint();
533 
534 	if (!sysfs)
535 		return -1;
536 
537 	if (snprintf(path, sizeof(path), "%s/%s", sysfs, entry) >= PATH_MAX)
538 		return -1;
539 
540 	return filename__write_int(path, value);
541 }
542