fs.c 9.8 KB

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