proc_self_mountinfo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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, char *device) {
  44. struct mountinfo *mi;
  45. uint32_t hash = simple_hash(device);
  46. for(mi = root; mi ; mi = mi->next)
  47. if (unlikely(
  48. mi->major == major &&
  49. mi->minor == minor &&
  50. mi->mount_source_name_hash == hash &&
  51. !strcmp(mi->mount_source_name, device)))
  52. return mi;
  53. return NULL;
  54. }
  55. // find the mount info with the given filesystem and mount_source
  56. // in the supplied linked list of mountinfo structures
  57. struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
  58. struct mountinfo *mi;
  59. uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
  60. for(mi = root; mi ; mi = mi->next)
  61. if(unlikely(mi->filesystem
  62. && mi->mount_source
  63. && mi->filesystem_hash == filesystem_hash
  64. && mi->mount_source_hash == mount_source_hash
  65. && !strcmp(mi->filesystem, filesystem)
  66. && !strcmp(mi->mount_source, mount_source)))
  67. return mi;
  68. return NULL;
  69. }
  70. struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
  71. struct mountinfo *mi;
  72. uint32_t filesystem_hash = simple_hash(filesystem);
  73. size_t solen = strlen(super_options);
  74. for(mi = root; mi ; mi = mi->next)
  75. if(unlikely(mi->filesystem
  76. && mi->super_options
  77. && mi->filesystem_hash == filesystem_hash
  78. && !strcmp(mi->filesystem, filesystem))) {
  79. // super_options is a comma separated list
  80. char *s = mi->super_options, *e;
  81. while(*s) {
  82. e = s + 1;
  83. while(*e && *e != ',') e++;
  84. size_t len = e - s;
  85. if(unlikely(len == solen && !strncmp(s, super_options, len)))
  86. return mi;
  87. if(*e == ',') s = ++e;
  88. else s = e;
  89. }
  90. }
  91. return NULL;
  92. }
  93. static void mountinfo_free(struct mountinfo *mi) {
  94. freez(mi->root);
  95. freez(mi->mount_point);
  96. freez(mi->mount_options);
  97. freez(mi->persistent_id);
  98. /*
  99. if(mi->optional_fields_count) {
  100. int i;
  101. for(i = 0; i < mi->optional_fields_count ; i++)
  102. free(*mi->optional_fields[i]);
  103. }
  104. free(mi->optional_fields);
  105. */
  106. freez(mi->filesystem);
  107. freez(mi->mount_source);
  108. freez(mi->mount_source_name);
  109. freez(mi->super_options);
  110. freez(mi);
  111. }
  112. // free a linked list of mountinfo structures
  113. void mountinfo_free_all(struct mountinfo *mi) {
  114. while(mi) {
  115. struct mountinfo *t = mi;
  116. mi = mi->next;
  117. mountinfo_free(t);
  118. }
  119. }
  120. static char *strdupz_decoding_octal(const char *string) {
  121. char *buffer = strdupz(string);
  122. char *d = buffer;
  123. const char *s = string;
  124. while(*s) {
  125. if(unlikely(*s == '\\')) {
  126. s++;
  127. if(likely(isdigit(*s) && isdigit(s[1]) && isdigit(s[2]))) {
  128. char c = *s++ - '0';
  129. c <<= 3;
  130. c |= *s++ - '0';
  131. c <<= 3;
  132. c |= *s++ - '0';
  133. *d++ = c;
  134. }
  135. else *d++ = '_';
  136. }
  137. else *d++ = *s++;
  138. }
  139. *d = '\0';
  140. return buffer;
  141. }
  142. static inline int is_read_only(const char *s) {
  143. if(!s) return 0;
  144. size_t len = strlen(s);
  145. if(len < 2) return 0;
  146. if(len == 2) {
  147. if(!strcmp(s, "ro")) return 1;
  148. return 0;
  149. }
  150. if(!strncmp(s, "ro,", 3)) return 1;
  151. if(!strncmp(&s[len - 3], ",ro", 3)) return 1;
  152. if(strstr(s, ",ro,")) return 1;
  153. return 0;
  154. }
  155. // read the whole mountinfo into a linked list
  156. struct mountinfo *mountinfo_read(int do_statvfs) {
  157. char filename[FILENAME_MAX + 1];
  158. snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", netdata_configured_host_prefix);
  159. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  160. if(unlikely(!ff)) {
  161. snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", netdata_configured_host_prefix);
  162. ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  163. if(unlikely(!ff)) return NULL;
  164. }
  165. ff = procfile_readall(ff);
  166. if(unlikely(!ff))
  167. return NULL;
  168. struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
  169. unsigned long l, lines = procfile_lines(ff);
  170. for(l = 0; l < lines ;l++) {
  171. if(unlikely(procfile_linewords(ff, l) < 5))
  172. continue;
  173. mi = mallocz(sizeof(struct mountinfo));
  174. unsigned long w = 0;
  175. mi->id = str2ul(procfile_lineword(ff, l, w)); w++;
  176. mi->parentid = str2ul(procfile_lineword(ff, l, w)); w++;
  177. char *major = procfile_lineword(ff, l, w), *minor; w++;
  178. for(minor = major; *minor && *minor != ':' ;minor++) ;
  179. if(unlikely(!*minor)) {
  180. error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
  181. freez(mi);
  182. continue;
  183. }
  184. *minor = '\0';
  185. minor++;
  186. mi->flags = 0;
  187. mi->major = str2ul(major);
  188. mi->minor = str2ul(minor);
  189. mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
  190. mi->root_hash = simple_hash(mi->root);
  191. mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  192. mi->mount_point_hash = simple_hash(mi->mount_point);
  193. mi->persistent_id = strdupz(mi->mount_point);
  194. netdata_fix_chart_id(mi->persistent_id);
  195. mi->persistent_id_hash = simple_hash(mi->persistent_id);
  196. mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
  197. if(unlikely(is_read_only(mi->mount_options)))
  198. mi->flags |= MOUNTINFO_READONLY;
  199. // count the optional fields
  200. /*
  201. unsigned long wo = w;
  202. */
  203. mi->optional_fields_count = 0;
  204. char *s = procfile_lineword(ff, l, w);
  205. while(*s && *s != '-') {
  206. w++;
  207. s = procfile_lineword(ff, l, w);
  208. mi->optional_fields_count++;
  209. }
  210. /*
  211. if(unlikely(mi->optional_fields_count)) {
  212. // we have some optional fields
  213. // read them into a new array of pointers;
  214. mi->optional_fields = mallocz(mi->optional_fields_count * sizeof(char *));
  215. int i;
  216. for(i = 0; i < mi->optional_fields_count ; i++) {
  217. *mi->optional_fields[wo] = strdupz(procfile_lineword(ff, l, w));
  218. wo++;
  219. }
  220. }
  221. else
  222. mi->optional_fields = NULL;
  223. */
  224. if(likely(*s == '-')) {
  225. w++;
  226. mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
  227. mi->filesystem_hash = simple_hash(mi->filesystem);
  228. mi->mount_source = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  229. mi->mount_source_hash = simple_hash(mi->mount_source);
  230. mi->mount_source_name = strdupz(basename(mi->mount_source));
  231. mi->mount_source_name_hash = simple_hash(mi->mount_source_name);
  232. mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
  233. if(unlikely(is_read_only(mi->super_options)))
  234. mi->flags |= MOUNTINFO_READONLY;
  235. if(unlikely(ME_DUMMY(mi->mount_source, mi->filesystem)))
  236. mi->flags |= MOUNTINFO_IS_DUMMY;
  237. if(unlikely(ME_REMOTE(mi->mount_source, mi->filesystem)))
  238. mi->flags |= MOUNTINFO_IS_REMOTE;
  239. // mark as BIND the duplicates (i.e. same filesystem + same source)
  240. if(do_statvfs) {
  241. struct stat buf;
  242. if(unlikely(stat(mi->mount_point, &buf) == -1)) {
  243. mi->st_dev = 0;
  244. mi->flags |= MOUNTINFO_NO_STAT;
  245. }
  246. else {
  247. mi->st_dev = buf.st_dev;
  248. struct mountinfo *mt;
  249. for(mt = root; mt; mt = mt->next) {
  250. if(unlikely(mt->st_dev == mi->st_dev && !(mt->flags & MOUNTINFO_IS_SAME_DEV))) {
  251. if(strlen(mi->mount_point) < strlen(mt->mount_point))
  252. mt->flags |= MOUNTINFO_IS_SAME_DEV;
  253. else
  254. mi->flags |= MOUNTINFO_IS_SAME_DEV;
  255. }
  256. }
  257. }
  258. }
  259. else {
  260. mi->st_dev = 0;
  261. }
  262. }
  263. else {
  264. mi->filesystem = NULL;
  265. mi->filesystem_hash = 0;
  266. mi->mount_source = NULL;
  267. mi->mount_source_hash = 0;
  268. mi->mount_source_name = NULL;
  269. mi->mount_source_name_hash = 0;
  270. mi->super_options = NULL;
  271. mi->st_dev = 0;
  272. }
  273. // check if it has size
  274. if(do_statvfs && !(mi->flags & MOUNTINFO_IS_DUMMY)) {
  275. struct statvfs buff_statvfs;
  276. if(unlikely(statvfs(mi->mount_point, &buff_statvfs) < 0)) {
  277. mi->flags |= MOUNTINFO_NO_STAT;
  278. }
  279. else if(unlikely(!buff_statvfs.f_blocks /* || !buff_statvfs.f_files */)) {
  280. mi->flags |= MOUNTINFO_NO_SIZE;
  281. }
  282. }
  283. // link it
  284. if(unlikely(!root))
  285. root = mi;
  286. else
  287. last->next = mi;
  288. last = mi;
  289. mi->next = NULL;
  290. /*
  291. #ifdef NETDATA_INTERNAL_CHECKS
  292. 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",
  293. mi->id,
  294. mi->parentid,
  295. mi->major,
  296. mi->minor,
  297. mi->root,
  298. mi->persistent_id,
  299. (mi->mount_point)?mi->mount_point:"",
  300. (mi->mount_options)?mi->mount_options:"",
  301. (mi->filesystem)?mi->filesystem:"",
  302. (mi->mount_source)?mi->mount_source:"",
  303. (mi->super_options)?mi->super_options:"",
  304. (mi->flags & MOUNTINFO_IS_DUMMY)?" DUMMY":"",
  305. (mi->flags & MOUNTINFO_IS_BIND)?" BIND":"",
  306. (mi->flags & MOUNTINFO_IS_REMOTE)?" REMOTE":"",
  307. (mi->flags & MOUNTINFO_NO_STAT)?" NOSTAT":"",
  308. (mi->flags & MOUNTINFO_NO_SIZE)?" NOSIZE":"",
  309. (mi->flags & MOUNTINFO_IS_SAME_DEV)?" SAMEDEV":""
  310. );
  311. #endif
  312. */
  313. }
  314. /* find if the mount options have "bind" in them
  315. {
  316. FILE *fp = setmntent(MOUNTED, "r");
  317. if (fp != NULL) {
  318. struct mntent mntbuf;
  319. struct mntent *mnt;
  320. char buf[4096 + 1];
  321. while ((mnt = getmntent_r(fp, &mntbuf, buf, 4096))) {
  322. char *bind = hasmntopt(mnt, "bind");
  323. if(unlikely(bind)) {
  324. struct mountinfo *mi;
  325. for(mi = root; mi ; mi = mi->next) {
  326. if(unlikely(strcmp(mnt->mnt_dir, mi->mount_point) == 0)) {
  327. fprintf(stderr, "Mount point '%s' is BIND\n", mi->mount_point);
  328. mi->flags |= MOUNTINFO_IS_BIND;
  329. break;
  330. }
  331. }
  332. #ifdef NETDATA_INTERNAL_CHECKS
  333. if(unlikely(!mi)) {
  334. error("Mount point '%s' not found in /proc/self/mountinfo", mnt->mnt_dir);
  335. }
  336. #endif
  337. }
  338. }
  339. endmntent(fp);
  340. }
  341. }
  342. */
  343. procfile_close(ff);
  344. return root;
  345. }