proc_self_mountinfo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_DONT_OVERWRITE_VALUE|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE);
  195. unsigned long l, lines = procfile_lines(ff);
  196. for(l = 0; l < lines ;l++) {
  197. if(unlikely(procfile_linewords(ff, l) < 5))
  198. continue;
  199. // make sure we don't add the same item twice
  200. char *v = (char *)dictionary_set(dict, procfile_lineword(ff, l, 4), "N", 2);
  201. if(v) {
  202. if(*v == 'O') continue;
  203. *v = 'O';
  204. }
  205. mi = mallocz(sizeof(struct mountinfo));
  206. unsigned long w = 0;
  207. mi->id = str2ul(procfile_lineword(ff, l, w)); w++;
  208. mi->parentid = str2ul(procfile_lineword(ff, l, w)); w++;
  209. char *major = procfile_lineword(ff, l, w), *minor; w++;
  210. for(minor = major; *minor && *minor != ':' ;minor++) ;
  211. if(unlikely(!*minor)) {
  212. error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
  213. freez(mi);
  214. continue;
  215. }
  216. *minor = '\0';
  217. minor++;
  218. mi->flags = 0;
  219. mi->major = str2ul(major);
  220. mi->minor = str2ul(minor);
  221. mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
  222. mi->root_hash = simple_hash(mi->root);
  223. mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  224. mi->mount_point_hash = simple_hash(mi->mount_point);
  225. mi->persistent_id = strdupz(mi->mount_point);
  226. netdata_fix_chart_id(mi->persistent_id);
  227. mi->persistent_id_hash = simple_hash(mi->persistent_id);
  228. mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
  229. if(unlikely(is_read_only(mi->mount_options)))
  230. mi->flags |= MOUNTINFO_READONLY;
  231. if(unlikely(mount_point_is_protected(mi->mount_point)))
  232. mi->flags |= MOUNTINFO_IS_IN_SYSD_PROTECTED_LIST;
  233. // count the optional fields
  234. /*
  235. unsigned long wo = w;
  236. */
  237. mi->optional_fields_count = 0;
  238. char *s = procfile_lineword(ff, l, w);
  239. while(*s && *s != '-') {
  240. w++;
  241. s = procfile_lineword(ff, l, w);
  242. mi->optional_fields_count++;
  243. }
  244. /*
  245. if(unlikely(mi->optional_fields_count)) {
  246. // we have some optional fields
  247. // read them into a new array of pointers;
  248. mi->optional_fields = mallocz(mi->optional_fields_count * sizeof(char *));
  249. int i;
  250. for(i = 0; i < mi->optional_fields_count ; i++) {
  251. *mi->optional_fields[wo] = strdupz(procfile_lineword(ff, l, w));
  252. wo++;
  253. }
  254. }
  255. else
  256. mi->optional_fields = NULL;
  257. */
  258. if(likely(*s == '-')) {
  259. w++;
  260. mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
  261. mi->filesystem_hash = simple_hash(mi->filesystem);
  262. mi->mount_source = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
  263. mi->mount_source_hash = simple_hash(mi->mount_source);
  264. mi->mount_source_name = strdupz(basename(mi->mount_source));
  265. mi->mount_source_name_hash = simple_hash(mi->mount_source_name);
  266. mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
  267. if(unlikely(is_read_only(mi->super_options)))
  268. mi->flags |= MOUNTINFO_READONLY;
  269. if(unlikely(ME_DUMMY(mi->mount_source, mi->filesystem)))
  270. mi->flags |= MOUNTINFO_IS_DUMMY;
  271. if(unlikely(ME_REMOTE(mi->mount_source, mi->filesystem)))
  272. mi->flags |= MOUNTINFO_IS_REMOTE;
  273. // mark as BIND the duplicates (i.e. same filesystem + same source)
  274. if(do_statvfs) {
  275. struct stat buf;
  276. if(unlikely(stat(mi->mount_point, &buf) == -1)) {
  277. mi->st_dev = 0;
  278. mi->flags |= MOUNTINFO_NO_STAT;
  279. }
  280. else {
  281. mi->st_dev = buf.st_dev;
  282. struct mountinfo *mt;
  283. for(mt = root; mt; mt = mt->next) {
  284. if(unlikely(mt->st_dev == mi->st_dev && !(mt->flags & MOUNTINFO_IS_SAME_DEV))) {
  285. if(strlen(mi->mount_point) < strlen(mt->mount_point))
  286. mt->flags |= MOUNTINFO_IS_SAME_DEV;
  287. else
  288. mi->flags |= MOUNTINFO_IS_SAME_DEV;
  289. }
  290. }
  291. }
  292. }
  293. else {
  294. mi->st_dev = 0;
  295. }
  296. }
  297. else {
  298. mi->filesystem = NULL;
  299. mi->filesystem_hash = 0;
  300. mi->mount_source = NULL;
  301. mi->mount_source_hash = 0;
  302. mi->mount_source_name = NULL;
  303. mi->mount_source_name_hash = 0;
  304. mi->super_options = NULL;
  305. mi->st_dev = 0;
  306. }
  307. // check if it has size
  308. if(do_statvfs && !(mi->flags & MOUNTINFO_IS_DUMMY)) {
  309. struct statvfs buff_statvfs;
  310. if(unlikely(statvfs(mi->mount_point, &buff_statvfs) < 0)) {
  311. mi->flags |= MOUNTINFO_NO_STAT;
  312. }
  313. else if(unlikely(!buff_statvfs.f_blocks /* || !buff_statvfs.f_files */)) {
  314. mi->flags |= MOUNTINFO_NO_SIZE;
  315. }
  316. }
  317. // link it
  318. if(unlikely(!root))
  319. root = mi;
  320. else
  321. last->next = mi;
  322. last = mi;
  323. mi->next = NULL;
  324. /*
  325. #ifdef NETDATA_INTERNAL_CHECKS
  326. 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",
  327. mi->id,
  328. mi->parentid,
  329. mi->major,
  330. mi->minor,
  331. mi->root,
  332. mi->persistent_id,
  333. (mi->mount_point)?mi->mount_point:"",
  334. (mi->mount_options)?mi->mount_options:"",
  335. (mi->filesystem)?mi->filesystem:"",
  336. (mi->mount_source)?mi->mount_source:"",
  337. (mi->super_options)?mi->super_options:"",
  338. (mi->flags & MOUNTINFO_IS_DUMMY)?" DUMMY":"",
  339. (mi->flags & MOUNTINFO_IS_BIND)?" BIND":"",
  340. (mi->flags & MOUNTINFO_IS_REMOTE)?" REMOTE":"",
  341. (mi->flags & MOUNTINFO_NO_STAT)?" NOSTAT":"",
  342. (mi->flags & MOUNTINFO_NO_SIZE)?" NOSIZE":"",
  343. (mi->flags & MOUNTINFO_IS_SAME_DEV)?" SAMEDEV":""
  344. );
  345. #endif
  346. */
  347. }
  348. /* find if the mount options have "bind" in them
  349. {
  350. FILE *fp = setmntent(MOUNTED, "r");
  351. if (fp != NULL) {
  352. struct mntent mntbuf;
  353. struct mntent *mnt;
  354. char buf[4096 + 1];
  355. while ((mnt = getmntent_r(fp, &mntbuf, buf, 4096))) {
  356. char *bind = hasmntopt(mnt, "bind");
  357. if(unlikely(bind)) {
  358. struct mountinfo *mi;
  359. for(mi = root; mi ; mi = mi->next) {
  360. if(unlikely(strcmp(mnt->mnt_dir, mi->mount_point) == 0)) {
  361. fprintf(stderr, "Mount point '%s' is BIND\n", mi->mount_point);
  362. mi->flags |= MOUNTINFO_IS_BIND;
  363. break;
  364. }
  365. }
  366. #ifdef NETDATA_INTERNAL_CHECKS
  367. if(unlikely(!mi)) {
  368. error("Mount point '%s' not found in /proc/self/mountinfo", mnt->mnt_dir);
  369. }
  370. #endif
  371. }
  372. }
  373. endmntent(fp);
  374. }
  375. }
  376. */
  377. dictionary_destroy(dict);
  378. procfile_close(ff);
  379. return root;
  380. }