3 // ----------------------------------------------------------------------------
4 // taken from gnulib/mountlist.c
7 /* A file system is "remote" if its Fs_name contains a ':'
8 or if (it is of type (smbfs or cifs) and its Fs_name starts with '//')
9 or Fs_name is equal to "-hosts" (used by autofs to mount remote fs). */
10 # define ME_REMOTE(Fs_name, Fs_type) \
11 (strchr (Fs_name, ':') != NULL \
12 || ((Fs_name)[0] == '/' \
13 && (Fs_name)[1] == '/' \
14 && (strcmp (Fs_type, "smbfs") == 0 \
15 || strcmp (Fs_type, "cifs") == 0)) \
16 || (strcmp("-hosts", Fs_name) == 0))
19 #define ME_DUMMY_0(Fs_name, Fs_type) \
20 (strcmp (Fs_type, "autofs") == 0 \
21 || strcmp (Fs_type, "proc") == 0 \
22 || strcmp (Fs_type, "subfs") == 0 \
23 /* for Linux 2.6/3.x */ \
24 || strcmp (Fs_type, "debugfs") == 0 \
25 || strcmp (Fs_type, "devpts") == 0 \
26 || strcmp (Fs_type, "fusectl") == 0 \
27 || strcmp (Fs_type, "mqueue") == 0 \
28 || strcmp (Fs_type, "rpc_pipefs") == 0 \
29 || strcmp (Fs_type, "sysfs") == 0 \
30 /* FreeBSD, Linux 2.4 */ \
31 || strcmp (Fs_type, "devfs") == 0 \
32 /* for NetBSD 3.0 */ \
33 || strcmp (Fs_type, "kernfs") == 0 \
35 || strcmp (Fs_type, "ignore") == 0)
37 /* Historically, we have marked as "dummy" any file system of type "none",
38 but now that programs like du need to know about bind-mounted directories,
39 we grant an exception to any with "bind" in its list of mount options.
40 I.e., those are *not* dummy entries. */
41 # define ME_DUMMY(Fs_name, Fs_type) \
42 (ME_DUMMY_0 (Fs_name, Fs_type) || strcmp (Fs_type, "none") == 0)
44 // ----------------------------------------------------------------------------
46 // find the mount info with the given major:minor
47 // in the supplied linked list of mountinfo structures
48 struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor) {
51 for(mi = root; mi ; mi = mi->next)
52 if(unlikely(mi->major == major && mi->minor == minor))
58 // find the mount info with the given filesystem and mount_source
59 // in the supplied linked list of mountinfo structures
60 struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
62 uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
64 for(mi = root; mi ; mi = mi->next)
65 if(unlikely(mi->filesystem
67 && mi->filesystem_hash == filesystem_hash
68 && mi->mount_source_hash == mount_source_hash
69 && !strcmp(mi->filesystem, filesystem)
70 && !strcmp(mi->mount_source, mount_source)))
76 struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
78 uint32_t filesystem_hash = simple_hash(filesystem);
80 size_t solen = strlen(super_options);
82 for(mi = root; mi ; mi = mi->next)
83 if(unlikely(mi->filesystem
85 && mi->filesystem_hash == filesystem_hash
86 && !strcmp(mi->filesystem, filesystem))) {
88 // super_options is a comma separated list
89 char *s = mi->super_options, *e;
92 while(*e && *e != ',') e++;
95 if(unlikely(len == solen && !strncmp(s, super_options, len)))
98 if(*e == ',') s = ++e;
107 // free a linked list of mountinfo structures
108 void mountinfo_free(struct mountinfo *mi) {
113 mountinfo_free(mi->next);
116 freez(mi->mount_point);
117 freez(mi->mount_options);
118 freez(mi->persistent_id);
121 if(mi->optional_fields_count) {
123 for(i = 0; i < mi->optional_fields_count ; i++)
124 free(*mi->optional_fields[i]);
126 free(mi->optional_fields);
128 freez(mi->filesystem);
129 freez(mi->mount_source);
130 freez(mi->super_options);
134 static char *strdupz_decoding_octal(const char *string) {
135 char *buffer = strdupz(string);
138 const char *s = string;
141 if(unlikely(*s == '\\')) {
143 if(likely(isdigit(*s) && isdigit(s[1]) && isdigit(s[2]))) {
160 // read the whole mountinfo into a linked list
161 struct mountinfo *mountinfo_read() {
162 char filename[FILENAME_MAX + 1];
163 snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
164 procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
166 snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
167 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
168 if(unlikely(!ff)) return NULL;
171 ff = procfile_readall(ff);
175 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
177 unsigned long l, lines = procfile_lines(ff);
178 for(l = 0; l < lines ;l++) {
179 if(unlikely(procfile_linewords(ff, l) < 5))
182 mi = mallocz(sizeof(struct mountinfo));
185 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
186 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
188 char *major = procfile_lineword(ff, l, w), *minor; w++;
189 for(minor = major; *minor && *minor != ':' ;minor++) ;
191 if(unlikely(!*minor)) {
192 error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
202 mi->major = strtoul(major, NULL, 10);
203 mi->minor = strtoul(minor, NULL, 10);
205 mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
206 mi->root_hash = simple_hash(mi->root);
208 mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
209 mi->mount_point_hash = simple_hash(mi->mount_point);
211 mi->persistent_id = strdupz(mi->mount_point);
212 netdata_fix_chart_id(mi->persistent_id);
213 mi->persistent_id_hash = simple_hash(mi->persistent_id);
215 mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
217 // count the optional fields
219 unsigned long wo = w;
221 mi->optional_fields_count = 0;
222 char *s = procfile_lineword(ff, l, w);
223 while(*s && *s != '-') {
225 s = procfile_lineword(ff, l, w);
226 mi->optional_fields_count++;
230 if(unlikely(mi->optional_fields_count)) {
231 // we have some optional fields
232 // read them into a new array of pointers;
234 mi->optional_fields = mallocz(mi->optional_fields_count * sizeof(char *));
237 for(i = 0; i < mi->optional_fields_count ; i++) {
238 *mi->optional_fields[wo] = strdupz(procfile_lineword(ff, l, w));
243 mi->optional_fields = NULL;
246 if(likely(*s == '-')) {
249 mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
250 mi->filesystem_hash = simple_hash(mi->filesystem);
252 mi->mount_source = strdupz(procfile_lineword(ff, l, w)); w++;
253 mi->mount_source_hash = simple_hash(mi->mount_source);
255 mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
257 if(unlikely(ME_DUMMY(mi->mount_source, mi->filesystem)))
258 mi->flags |= MOUNTINFO_IS_DUMMY;
260 if(unlikely(ME_REMOTE(mi->mount_source, mi->filesystem)))
261 mi->flags |= MOUNTINFO_IS_REMOTE;
263 // mark as BIND the duplicates (i.e. same filesystem + same source)
266 if(unlikely(stat(mi->mount_point, &buf) == -1)) {
268 mi->flags |= MOUNTINFO_NO_STAT;
271 mi->st_dev = buf.st_dev;
273 struct mountinfo *mt;
274 for(mt = root; mt; mt = mt->next) {
275 if(unlikely(mt->st_dev == mi->st_dev && !(mi->flags & MOUNTINFO_NO_STAT))) {
276 if(strlen(mi->mount_point) < strlen(mt->mount_point))
277 mt->flags |= MOUNTINFO_IS_SAME_DEV;
279 mi->flags |= MOUNTINFO_IS_SAME_DEV;
286 mi->filesystem = NULL;
287 mi->filesystem_hash = 0;
289 mi->mount_source = NULL;
290 mi->mount_source_hash = 0;
292 mi->super_options = NULL;
297 // check if it has size
299 struct statvfs buff_statvfs;
300 if(unlikely(statvfs(mi->mount_point, &buff_statvfs) < 0)) {
301 mi->flags |= MOUNTINFO_NO_STAT;
303 else if(unlikely(!buff_statvfs.f_blocks /* || !buff_statvfs.f_files */)) {
304 mi->flags |= MOUNTINFO_NO_SIZE;
318 #ifdef NETDATA_INTERNAL_CHECKS
319 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",
326 (mi->mount_point)?mi->mount_point:"",
327 (mi->mount_options)?mi->mount_options:"",
328 (mi->filesystem)?mi->filesystem:"",
329 (mi->mount_source)?mi->mount_source:"",
330 (mi->super_options)?mi->super_options:"",
331 (mi->flags & MOUNTINFO_IS_DUMMY)?" DUMMY":"",
332 (mi->flags & MOUNTINFO_IS_BIND)?" BIND":"",
333 (mi->flags & MOUNTINFO_IS_REMOTE)?" REMOTE":"",
334 (mi->flags & MOUNTINFO_NO_STAT)?" NOSTAT":"",
335 (mi->flags & MOUNTINFO_NO_SIZE)?" NOSIZE":"",
336 (mi->flags & MOUNTINFO_IS_SAME_DEV)?" SAMEDEV":""
342 /* find if the mount options have "bind" in them
344 FILE *fp = setmntent(MOUNTED, "r");
346 struct mntent mntbuf;
350 while ((mnt = getmntent_r(fp, &mntbuf, buf, 4096))) {
351 char *bind = hasmntopt(mnt, "bind");
353 struct mountinfo *mi;
354 for(mi = root; mi ; mi = mi->next) {
355 if(unlikely(strcmp(mnt->mnt_dir, mi->mount_point) == 0)) {
356 fprintf(stderr, "Mount point '%s' is BIND\n", mi->mount_point);
357 mi->flags |= MOUNTINFO_IS_BIND;
362 #ifdef NETDATA_INTERNAL_CHECKS
364 error("Mount point '%s' not found in /proc/self/mountinfo", mnt->mnt_dir);