proc_self_mountinfo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. // for the full list of protected mount points look at
  156. // https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L142-L149
  157. // https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L180-L194
  158. static const char *systemd_protected_mount_points[] = {
  159. "/home",
  160. "/root",
  161. "/usr",
  162. "/boot",
  163. "/efi",
  164. "/etc",
  165. "/run/user",
  166. "/lib",
  167. "/lib64",
  168. "/bin",
  169. "/sbin",
  170. NULL
  171. };
  172. static inline int mount_point_is_protected(char *mount_point)
  173. {
  174. for (size_t i = 0; systemd_protected_mount_points[i] != NULL; i++)
  175. if (!strcmp(mount_point, systemd_protected_mount_points[i]))
  176. return 1;
  177. return 0;
  178. }
  179. // read the whole mountinfo into a linked list
  180. struct mountinfo *mountinfo_read(int do_statvfs) {
  181. char filename[FILENAME_MAX + 1];
  182. snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", netdata_configured_host_prefix);
  183. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  184. if(unlikely(!ff)) {
  185. snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", netdata_configured_host_prefix);
  186. ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  187. if(unlikely(!ff)) return NULL;
  188. }
  189. ff = procfile_readall(ff);
  190. if(unlikely(!ff))
  191. return NULL;
  192. struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
  193. // create a dictionary to track uniqueness
  194. DICTIONARY *dict = dictionary_create_advanced(
  195. DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_NAME_LINK_DONT_CLONE,
  196. &dictionary_stats_category_collectors, 0);
  197. unsigned long l, lines = procfile_lines(ff);
  198. for(l = 0; l < lines ;l++) {
  199. if(unlikely(procfile_linewords(ff, l) < 5))
  200. continue;
  201. // make sure we don't add the same item twice
  202. char *v = (char *)dictionary_set(dict, procfile_lineword(ff, l, 4), "N", 2);
  203. if(v) {
  204. if(*v == 'O') continue;
  205. *v = 'O';
  206. }
  207. mi = mallocz(sizeof(struct mountinfo));
  208. unsigned long w = 0;
  209. mi->id = str2ul(procfile_lineword(ff, l, w)); w++;
  210. mi->parentid = str2ul(procfile_lineword(ff, l, w)); w++;
  211. char *major = procfile_lineword(ff, l, w), *minor; w++;
  212. for(minor = major; *minor && *minor != ':' ;minor++) ;
  213. if(unlikely(!*minor)) {
  214. collector_error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
  215. freez(mi);
  216. continue;
  217. }
  218. *minor = '\0';
  219. minor++;
  220. mi->flags = 0;
  221. mi->major = str2ul(major);
  222. mi->minor = str2ul(minor);
  223. mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
  224. mi->root_hash = simple_hash(mi->root);
  225. mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  226. mi->mount_point_hash = simple_hash(mi->mount_point);
  227. mi->persistent_id = strdupz(mi->mount_point);
  228. netdata_fix_chart_id(mi->persistent_id);
  229. mi->persistent_id_hash = simple_hash(mi->persistent_id);
  230. mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
  231. if(unlikely(is_read_only(mi->mount_options)))
  232. mi->flags |= MOUNTINFO_READONLY;
  233. if(unlikely(mount_point_is_protected(mi->mount_point)))
  234. mi->flags |= MOUNTINFO_IS_IN_SYSD_PROTECTED_LIST;
  235. // count the optional fields
  236. /*
  237. unsigned long wo = w;
  238. */
  239. mi->optional_fields_count = 0;
  240. char *s = procfile_lineword(ff, l, w);
  241. while(*s && *s != '-') {
  242. w++;
  243. s = procfile_lineword(ff, l, w);
  244. mi->optional_fields_count++;
  245. }
  246. /*
  247. if(unlikely(mi->optional_fields_count)) {
  248. // we have some optional fields
  249. // read them into a new array of pointers;
  250. mi->optional_fields = mallocz(mi->optional_fields_count * sizeof(char *));
  251. int i;
  252. for(i = 0; i < mi->optional_fields_count ; i++) {
  253. *mi->optional_fields[wo] = strdupz(procfile_lineword(ff, l, w));
  254. wo++;
  255. }
  256. }
  257. else
  258. mi->optional_fields = NULL;
  259. */
  260. if(likely(*s == '-')) {
  261. w++;
  262. mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
  263. mi->filesystem_hash = simple_hash(mi->filesystem);
  264. mi->mount_source = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  265. mi->mount_source_hash = simple_hash(mi->mount_source);
  266. mi->mount_source_name = strdupz(basename(mi->mount_source));
  267. mi->mount_source_name_hash = simple_hash(mi->mount_source_name);
  268. mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
  269. if(unlikely(is_read_only(mi->super_options)))
  270. mi->flags |= MOUNTINFO_READONLY;
  271. if(unlikely(ME_DUMMY(mi->mount_source, mi->filesystem)))
  272. mi->flags |= MOUNTINFO_IS_DUMMY;
  273. if(unlikely(ME_REMOTE(mi->mount_source, mi->filesystem)))
  274. mi->flags |= MOUNTINFO_IS_REMOTE;
  275. // mark as BIND the duplicates (i.e. same filesystem + same source)
  276. if(do_statvfs) {
  277. struct stat buf;
  278. if(unlikely(stat(mi->mount_point, &buf) == -1)) {
  279. mi->st_dev = 0;
  280. mi->flags |= MOUNTINFO_NO_STAT;
  281. }
  282. else {
  283. mi->st_dev = buf.st_dev;
  284. struct mountinfo *mt;
  285. for(mt = root; mt; mt = mt->next) {
  286. if(unlikely(mt->st_dev == mi->st_dev && !(mt->flags & MOUNTINFO_IS_SAME_DEV))) {
  287. if(strlen(mi->mount_point) < strlen(mt->mount_point))
  288. mt->flags |= MOUNTINFO_IS_SAME_DEV;
  289. else
  290. mi->flags |= MOUNTINFO_IS_SAME_DEV;
  291. }
  292. }
  293. }
  294. }
  295. else {
  296. mi->st_dev = 0;
  297. }
  298. //try to detect devices with same minor and major modes. Within these,
  299. //the larger mount point is considered a bind.
  300. struct mountinfo *mt;
  301. for(mt = root; mt; mt = mt->next) {
  302. if(unlikely(mt->major == mi->major && mt->minor == mi->minor && !(mi->flags & MOUNTINFO_IS_BIND))) {
  303. if(strlen(mi->root) < strlen(mt->root))
  304. mt->flags |= MOUNTINFO_IS_BIND;
  305. else
  306. mi->flags |= MOUNTINFO_IS_BIND;
  307. }
  308. }
  309. }
  310. else {
  311. mi->filesystem = NULL;
  312. mi->filesystem_hash = 0;
  313. mi->mount_source = NULL;
  314. mi->mount_source_hash = 0;
  315. mi->mount_source_name = NULL;
  316. mi->mount_source_name_hash = 0;
  317. mi->super_options = NULL;
  318. mi->st_dev = 0;
  319. }
  320. // check if it has size
  321. if(do_statvfs && !(mi->flags & MOUNTINFO_IS_DUMMY)) {
  322. struct statvfs buff_statvfs;
  323. if(unlikely(statvfs(mi->mount_point, &buff_statvfs) < 0)) {
  324. mi->flags |= MOUNTINFO_NO_STAT;
  325. }
  326. else if(unlikely(!buff_statvfs.f_blocks /* || !buff_statvfs.f_files */)) {
  327. mi->flags |= MOUNTINFO_NO_SIZE;
  328. }
  329. }
  330. // link it
  331. if(unlikely(!root))
  332. root = mi;
  333. else
  334. last->next = mi;
  335. last = mi;
  336. mi->next = NULL;
  337. /*
  338. #ifdef NETDATA_INTERNAL_CHECKS
  339. 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",
  340. mi->id,
  341. mi->parentid,
  342. mi->major,
  343. mi->minor,
  344. mi->root,
  345. mi->persistent_id,
  346. (mi->mount_point)?mi->mount_point:"",
  347. (mi->mount_options)?mi->mount_options:"",
  348. (mi->filesystem)?mi->filesystem:"",
  349. (mi->mount_source)?mi->mount_source:"",
  350. (mi->super_options)?mi->super_options:"",
  351. (mi->flags & MOUNTINFO_IS_DUMMY)?" DUMMY":"",
  352. (mi->flags & MOUNTINFO_IS_BIND)?" BIND":"",
  353. (mi->flags & MOUNTINFO_IS_REMOTE)?" REMOTE":"",
  354. (mi->flags & MOUNTINFO_NO_STAT)?" NOSTAT":"",
  355. (mi->flags & MOUNTINFO_NO_SIZE)?" NOSIZE":"",
  356. (mi->flags & MOUNTINFO_IS_SAME_DEV)?" SAMEDEV":""
  357. );
  358. #endif
  359. */
  360. }
  361. /* find if the mount options have "bind" in them
  362. {
  363. FILE *fp = setmntent(MOUNTED, "r");
  364. if (fp != NULL) {
  365. struct mntent mntbuf;
  366. struct mntent *mnt;
  367. char buf[4096 + 1];
  368. while ((mnt = getmntent_r(fp, &mntbuf, buf, 4096))) {
  369. char *bind = hasmntopt(mnt, "bind");
  370. if(unlikely(bind)) {
  371. struct mountinfo *mi;
  372. for(mi = root; mi ; mi = mi->next) {
  373. if(unlikely(strcmp(mnt->mnt_dir, mi->mount_point) == 0)) {
  374. fprintf(stderr, "Mount point '%s' is BIND\n", mi->mount_point);
  375. mi->flags |= MOUNTINFO_IS_BIND;
  376. break;
  377. }
  378. }
  379. #ifdef NETDATA_INTERNAL_CHECKS
  380. if(unlikely(!mi)) {
  381. collector_error("Mount point '%s' not found in /proc/self/mountinfo", mnt->mnt_dir);
  382. }
  383. #endif
  384. }
  385. }
  386. endmntent(fp);
  387. }
  388. }
  389. */
  390. dictionary_destroy(dict);
  391. procfile_close(ff);
  392. return root;
  393. }