fs.c 8.5 KB

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