fs.c 9.8 KB

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