proc_self_mountinfo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. // ----------------------------------------------------------------------------
  4. // taken from gnulib/mountlist.c
  5. #ifndef ME_REMOTE
  6. /* A file system is "remote" if its Fs_name contains a ':'
  7. or if (it is of type (smbfs or cifs) and its Fs_name starts with '//')
  8. or Fs_name is equal to "-hosts" (used by autofs to mount remote fs). */
  9. # define ME_REMOTE(Fs_name, Fs_type) \
  10. (strchr (Fs_name, ':') != NULL \
  11. || ((Fs_name)[0] == '/' \
  12. && (Fs_name)[1] == '/' \
  13. && (strcmp (Fs_type, "smbfs") == 0 \
  14. || strcmp (Fs_type, "cifs") == 0)) \
  15. || (strcmp("-hosts", Fs_name) == 0))
  16. #endif
  17. #define ME_DUMMY_0(Fs_name, Fs_type) \
  18. (strcmp (Fs_type, "autofs") == 0 \
  19. || strcmp (Fs_type, "proc") == 0 \
  20. || strcmp (Fs_type, "subfs") == 0 \
  21. /* for Linux 2.6/3.x */ \
  22. || strcmp (Fs_type, "debugfs") == 0 \
  23. || strcmp (Fs_type, "devpts") == 0 \
  24. || strcmp (Fs_type, "fusectl") == 0 \
  25. || strcmp (Fs_type, "mqueue") == 0 \
  26. || strcmp (Fs_type, "rpc_pipefs") == 0 \
  27. || strcmp (Fs_type, "sysfs") == 0 \
  28. /* FreeBSD, Linux 2.4 */ \
  29. || strcmp (Fs_type, "devfs") == 0 \
  30. /* for NetBSD 3.0 */ \
  31. || strcmp (Fs_type, "kernfs") == 0 \
  32. /* for Irix 6.5 */ \
  33. || strcmp (Fs_type, "ignore") == 0)
  34. /* Historically, we have marked as "dummy" any file system of type "none",
  35. but now that programs like du need to know about bind-mounted directories,
  36. we grant an exception to any with "bind" in its list of mount options.
  37. I.e., those are *not* dummy entries. */
  38. # define ME_DUMMY(Fs_name, Fs_type) \
  39. (ME_DUMMY_0 (Fs_name, Fs_type) || strcmp (Fs_type, "none") == 0)
  40. // ----------------------------------------------------------------------------
  41. // find the mount info with the given major:minor
  42. // in the supplied linked list of mountinfo structures
  43. struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor) {
  44. struct mountinfo *mi;
  45. for(mi = root; mi ; mi = mi->next)
  46. if(unlikely(mi->major == major && mi->minor == minor))
  47. return mi;
  48. return NULL;
  49. }
  50. // find the mount info with the given filesystem and mount_source
  51. // in the supplied linked list of mountinfo structures
  52. struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
  53. struct mountinfo *mi;
  54. uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
  55. for(mi = root; mi ; mi = mi->next)
  56. if(unlikely(mi->filesystem
  57. && mi->mount_source
  58. && mi->filesystem_hash == filesystem_hash
  59. && mi->mount_source_hash == mount_source_hash
  60. && !strcmp(mi->filesystem, filesystem)
  61. && !strcmp(mi->mount_source, mount_source)))
  62. return mi;
  63. return NULL;
  64. }
  65. struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
  66. struct mountinfo *mi;
  67. uint32_t filesystem_hash = simple_hash(filesystem);
  68. size_t solen = strlen(super_options);
  69. for(mi = root; mi ; mi = mi->next)
  70. if(unlikely(mi->filesystem
  71. && mi->super_options
  72. && mi->filesystem_hash == filesystem_hash
  73. && !strcmp(mi->filesystem, filesystem))) {
  74. // super_options is a comma separated list
  75. char *s = mi->super_options, *e;
  76. while(*s) {
  77. e = s + 1;
  78. while(*e && *e != ',') e++;
  79. size_t len = e - s;
  80. if(unlikely(len == solen && !strncmp(s, super_options, len)))
  81. return mi;
  82. if(*e == ',') s = ++e;
  83. else s = e;
  84. }
  85. }
  86. return NULL;
  87. }
  88. static void mountinfo_free(struct mountinfo *mi) {
  89. freez(mi->root);
  90. freez(mi->mount_point);
  91. freez(mi->mount_options);
  92. freez(mi->persistent_id);
  93. /*
  94. if(mi->optional_fields_count) {
  95. int i;
  96. for(i = 0; i < mi->optional_fields_count ; i++)
  97. free(*mi->optional_fields[i]);
  98. }
  99. free(mi->optional_fields);
  100. */
  101. freez(mi->filesystem);
  102. freez(mi->mount_source);
  103. freez(mi->super_options);
  104. freez(mi);
  105. }
  106. // free a linked list of mountinfo structures
  107. void mountinfo_free_all(struct mountinfo *mi) {
  108. while(mi) {
  109. struct mountinfo *t = mi;
  110. mi = mi->next;
  111. mountinfo_free(t);
  112. }
  113. }
  114. static char *strdupz_decoding_octal(const char *string) {
  115. char *buffer = strdupz(string);
  116. char *d = buffer;
  117. const char *s = string;
  118. while(*s) {
  119. if(unlikely(*s == '\\')) {
  120. s++;
  121. if(likely(isdigit(*s) && isdigit(s[1]) && isdigit(s[2]))) {
  122. char c = *s++ - '0';
  123. c <<= 3;
  124. c |= *s++ - '0';
  125. c <<= 3;
  126. c |= *s++ - '0';
  127. *d++ = c;
  128. }
  129. else *d++ = '_';
  130. }
  131. else *d++ = *s++;
  132. }
  133. *d = '\0';
  134. return buffer;
  135. }
  136. static inline int is_read_only(const char *s) {
  137. if(!s) return 0;
  138. size_t len = strlen(s);
  139. if(len < 2) return 0;
  140. if(len == 2) {
  141. if(!strcmp(s, "ro")) return 1;
  142. return 0;
  143. }
  144. if(!strncmp(s, "ro,", 3)) return 1;
  145. if(!strncmp(&s[len - 3], ",ro", 3)) return 1;
  146. if(strstr(s, ",ro,")) return 1;
  147. return 0;
  148. }
  149. // read the whole mountinfo into a linked list
  150. struct mountinfo *mountinfo_read(int do_statvfs) {
  151. char filename[FILENAME_MAX + 1];
  152. snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", netdata_configured_host_prefix);
  153. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  154. if(unlikely(!ff)) {
  155. snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", netdata_configured_host_prefix);
  156. ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  157. if(unlikely(!ff)) return NULL;
  158. }
  159. ff = procfile_readall(ff);
  160. if(unlikely(!ff))
  161. return NULL;
  162. struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
  163. unsigned long l, lines = procfile_lines(ff);
  164. for(l = 0; l < lines ;l++) {
  165. if(unlikely(procfile_linewords(ff, l) < 5))
  166. continue;
  167. mi = mallocz(sizeof(struct mountinfo));
  168. unsigned long w = 0;
  169. mi->id = str2ul(procfile_lineword(ff, l, w)); w++;
  170. mi->parentid = str2ul(procfile_lineword(ff, l, w)); w++;
  171. char *major = procfile_lineword(ff, l, w), *minor; w++;
  172. for(minor = major; *minor && *minor != ':' ;minor++) ;
  173. if(unlikely(!*minor)) {
  174. error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
  175. freez(mi);
  176. continue;
  177. }
  178. *minor = '\0';
  179. minor++;
  180. mi->flags = 0;
  181. mi->major = str2ul(major);
  182. mi->minor = str2ul(minor);
  183. mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
  184. mi->root_hash = simple_hash(mi->root);
  185. mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  186. mi->mount_point_hash = simple_hash(mi->mount_point);
  187. mi->persistent_id = strdupz(mi->mount_point);
  188. netdata_fix_chart_id(mi->persistent_id);
  189. mi->persistent_id_hash = simple_hash(mi->persistent_id);
  190. mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
  191. if(unlikely(is_read_only(mi->mount_options)))
  192. mi->flags |= MOUNTINFO_READONLY;
  193. // count the optional fields
  194. /*
  195. unsigned long wo = w;
  196. */
  197. mi->optional_fields_count = 0;
  198. char *s = procfile_lineword(ff, l, w);
  199. while(*s && *s != '-') {
  200. w++;
  201. s = procfile_lineword(ff, l, w);
  202. mi->optional_fields_count++;
  203. }
  204. /*
  205. if(unlikely(mi->optional_fields_count)) {
  206. // we have some optional fields
  207. // read them into a new array of pointers;
  208. mi->optional_fields = mallocz(mi->optional_fields_count * sizeof(char *));
  209. int i;
  210. for(i = 0; i < mi->optional_fields_count ; i++) {
  211. *mi->optional_fields[wo] = strdupz(procfile_lineword(ff, l, w));
  212. wo++;
  213. }
  214. }
  215. else
  216. mi->optional_fields = NULL;
  217. */
  218. if(likely(*s == '-')) {
  219. w++;
  220. mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
  221. mi->filesystem_hash = simple_hash(mi->filesystem);
  222. mi->mount_source = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  223. mi->mount_source_hash = simple_hash(mi->mount_source);
  224. mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
  225. if(unlikely(is_read_only(mi->super_options)))
  226. mi->flags |= MOUNTINFO_READONLY;
  227. if(unlikely(ME_DUMMY(mi->mount_source, mi->filesystem)))
  228. mi->flags |= MOUNTINFO_IS_DUMMY;
  229. if(unlikely(ME_REMOTE(mi->mount_source, mi->filesystem)))
  230. mi->flags |= MOUNTINFO_IS_REMOTE;
  231. // mark as BIND the duplicates (i.e. same filesystem + same source)
  232. if(do_statvfs) {
  233. struct stat buf;
  234. if(unlikely(stat(mi->mount_point, &buf) == -1)) {
  235. mi->st_dev = 0;
  236. mi->flags |= MOUNTINFO_NO_STAT;
  237. }
  238. else {
  239. mi->st_dev = buf.st_dev;
  240. struct mountinfo *mt;
  241. for(mt = root; mt; mt = mt->next) {
  242. if(unlikely(mt->st_dev == mi->st_dev && !(mt->flags & MOUNTINFO_IS_SAME_DEV))) {
  243. if(strlen(mi->mount_point) < strlen(mt->mount_point))
  244. mt->flags |= MOUNTINFO_IS_SAME_DEV;
  245. else
  246. mi->flags |= MOUNTINFO_IS_SAME_DEV;
  247. }
  248. }
  249. }
  250. }
  251. else {
  252. mi->st_dev = 0;
  253. }
  254. }
  255. else {
  256. mi->filesystem = NULL;
  257. mi->filesystem_hash = 0;
  258. mi->mount_source = NULL;
  259. mi->mount_source_hash = 0;
  260. mi->super_options = NULL;
  261. mi->st_dev = 0;
  262. }
  263. // check if it has size
  264. if(do_statvfs && !(mi->flags & MOUNTINFO_IS_DUMMY)) {
  265. struct statvfs buff_statvfs;
  266. if(unlikely(statvfs(mi->mount_point, &buff_statvfs) < 0)) {
  267. mi->flags |= MOUNTINFO_NO_STAT;
  268. }
  269. else if(unlikely(!buff_statvfs.f_blocks /* || !buff_statvfs.f_files */)) {
  270. mi->flags |= MOUNTINFO_NO_SIZE;
  271. }
  272. }
  273. // link it
  274. if(unlikely(!root))
  275. root = mi;
  276. else
  277. last->next = mi;
  278. last = mi;
  279. mi->next = NULL;
  280. /*
  281. #ifdef NETDATA_INTERNAL_CHECKS
  282. fprintf(stderr, "MOUNTINFO: %ld %ld %lu:%lu root '%s', persistent id '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'%s%s%s%s%s%s\n",
  283. mi->id,
  284. mi->parentid,
  285. mi->major,
  286. mi->minor,
  287. mi->root,
  288. mi->persistent_id,
  289. (mi->mount_point)?mi->mount_point:"",
  290. (mi->mount_options)?mi->mount_options:"",
  291. (mi->filesystem)?mi->filesystem:"",
  292. (mi->mount_source)?mi->mount_source:"",
  293. (mi->super_options)?mi->super_options:"",
  294. (mi->flags & MOUNTINFO_IS_DUMMY)?" DUMMY":"",
  295. (mi->flags & MOUNTINFO_IS_BIND)?" BIND":"",
  296. (mi->flags & MOUNTINFO_IS_REMOTE)?" REMOTE":"",
  297. (mi->flags & MOUNTINFO_NO_STAT)?" NOSTAT":"",
  298. (mi->flags & MOUNTINFO_NO_SIZE)?" NOSIZE":"",
  299. (mi->flags & MOUNTINFO_IS_SAME_DEV)?" SAMEDEV":""
  300. );
  301. #endif
  302. */
  303. }
  304. /* find if the mount options have "bind" in them
  305. {
  306. FILE *fp = setmntent(MOUNTED, "r");
  307. if (fp != NULL) {
  308. struct mntent mntbuf;
  309. struct mntent *mnt;
  310. char buf[4096 + 1];
  311. while ((mnt = getmntent_r(fp, &mntbuf, buf, 4096))) {
  312. char *bind = hasmntopt(mnt, "bind");
  313. if(unlikely(bind)) {
  314. struct mountinfo *mi;
  315. for(mi = root; mi ; mi = mi->next) {
  316. if(unlikely(strcmp(mnt->mnt_dir, mi->mount_point) == 0)) {
  317. fprintf(stderr, "Mount point '%s' is BIND\n", mi->mount_point);
  318. mi->flags |= MOUNTINFO_IS_BIND;
  319. break;
  320. }
  321. }
  322. #ifdef NETDATA_INTERNAL_CHECKS
  323. if(unlikely(!mi)) {
  324. error("Mount point '%s' not found in /proc/self/mountinfo", mnt->mnt_dir);
  325. }
  326. #endif
  327. }
  328. }
  329. endmntent(fp);
  330. }
  331. }
  332. */
  333. procfile_close(ff);
  334. return root;
  335. }