fs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* TODO merge/factor in debugfs.c here */
  2. #include <ctype.h>
  3. #include <errno.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 "debugfs.h"
  14. #include "fs.h"
  15. static const char * const sysfs__fs_known_mountpoints[] = {
  16. "/sys",
  17. 0,
  18. };
  19. static const char * const procfs__known_mountpoints[] = {
  20. "/proc",
  21. 0,
  22. };
  23. struct fs {
  24. const char *name;
  25. const char * const *mounts;
  26. char path[PATH_MAX + 1];
  27. bool found;
  28. long magic;
  29. };
  30. enum {
  31. FS__SYSFS = 0,
  32. FS__PROCFS = 1,
  33. };
  34. static struct fs fs__entries[] = {
  35. [FS__SYSFS] = {
  36. .name = "sysfs",
  37. .mounts = sysfs__fs_known_mountpoints,
  38. .magic = SYSFS_MAGIC,
  39. },
  40. [FS__PROCFS] = {
  41. .name = "proc",
  42. .mounts = procfs__known_mountpoints,
  43. .magic = PROC_SUPER_MAGIC,
  44. },
  45. };
  46. static bool fs__read_mounts(struct fs *fs)
  47. {
  48. bool found = false;
  49. char type[100];
  50. FILE *fp;
  51. fp = fopen("/proc/mounts", "r");
  52. if (fp == NULL)
  53. return NULL;
  54. while (!found &&
  55. fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  56. fs->path, type) == 2) {
  57. if (strcmp(type, fs->name) == 0)
  58. found = true;
  59. }
  60. fclose(fp);
  61. return fs->found = found;
  62. }
  63. static int fs__valid_mount(const char *fs, long magic)
  64. {
  65. struct statfs st_fs;
  66. if (statfs(fs, &st_fs) < 0)
  67. return -ENOENT;
  68. else if ((long)st_fs.f_type != magic)
  69. return -ENOENT;
  70. return 0;
  71. }
  72. static bool fs__check_mounts(struct fs *fs)
  73. {
  74. const char * const *ptr;
  75. ptr = fs->mounts;
  76. while (*ptr) {
  77. if (fs__valid_mount(*ptr, fs->magic) == 0) {
  78. fs->found = true;
  79. strcpy(fs->path, *ptr);
  80. return true;
  81. }
  82. ptr++;
  83. }
  84. return false;
  85. }
  86. static void mem_toupper(char *f, size_t len)
  87. {
  88. while (len) {
  89. *f = toupper(*f);
  90. f++;
  91. len--;
  92. }
  93. }
  94. /*
  95. * Check for "NAME_PATH" environment variable to override fs location (for
  96. * testing). This matches the recommendation in Documentation/sysfs-rules.txt
  97. * for SYSFS_PATH.
  98. */
  99. static bool fs__env_override(struct fs *fs)
  100. {
  101. char *override_path;
  102. size_t name_len = strlen(fs->name);
  103. /* name + "_PATH" + '\0' */
  104. char upper_name[name_len + 5 + 1];
  105. memcpy(upper_name, fs->name, name_len);
  106. mem_toupper(upper_name, name_len);
  107. strcpy(&upper_name[name_len], "_PATH");
  108. override_path = getenv(upper_name);
  109. if (!override_path)
  110. return false;
  111. fs->found = true;
  112. strncpy(fs->path, override_path, sizeof(fs->path));
  113. return true;
  114. }
  115. static const char *fs__get_mountpoint(struct fs *fs)
  116. {
  117. if (fs__env_override(fs))
  118. return fs->path;
  119. if (fs__check_mounts(fs))
  120. return fs->path;
  121. if (fs__read_mounts(fs))
  122. return fs->path;
  123. return NULL;
  124. }
  125. static const char *fs__mountpoint(int idx)
  126. {
  127. struct fs *fs = &fs__entries[idx];
  128. if (fs->found)
  129. return (const char *)fs->path;
  130. return fs__get_mountpoint(fs);
  131. }
  132. #define FS__MOUNTPOINT(name, idx) \
  133. const char *name##__mountpoint(void) \
  134. { \
  135. return fs__mountpoint(idx); \
  136. }
  137. FS__MOUNTPOINT(sysfs, FS__SYSFS);
  138. FS__MOUNTPOINT(procfs, FS__PROCFS);
  139. int filename__read_int(const char *filename, int *value)
  140. {
  141. char line[64];
  142. int fd = open(filename, O_RDONLY), err = -1;
  143. if (fd < 0)
  144. return -1;
  145. if (read(fd, line, sizeof(line)) > 0) {
  146. *value = atoi(line);
  147. err = 0;
  148. }
  149. close(fd);
  150. return err;
  151. }
  152. int sysctl__read_int(const char *sysctl, int *value)
  153. {
  154. char path[PATH_MAX];
  155. const char *procfs = procfs__mountpoint();
  156. if (!procfs)
  157. return -1;
  158. snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
  159. return filename__read_int(path, value);
  160. }