]> arthur.barton.de Git - netdata.git/blob - src/proc_self_mountinfo.c
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / proc_self_mountinfo.c
1 #include "common.h"
2
3 // ----------------------------------------------------------------------------
4 // taken from gnulib/mountlist.c
5
6 #ifndef ME_REMOTE
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))
17 #endif
18
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           \
34    /* for Irix 6.5 */                           \
35    || strcmp (Fs_type, "ignore") == 0)
36
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)
43
44 // ----------------------------------------------------------------------------
45
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) {
49     struct mountinfo *mi;
50
51     for(mi = root; mi ; mi = mi->next)
52         if(unlikely(mi->major == major && mi->minor == minor))
53             return mi;
54
55     return NULL;
56 }
57
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) {
61     struct mountinfo *mi;
62     uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
63
64     for(mi = root; mi ; mi = mi->next)
65         if(unlikely(mi->filesystem
66                 && mi->mount_source
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)))
71             return mi;
72
73     return NULL;
74 }
75
76 struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
77     struct mountinfo *mi;
78     uint32_t filesystem_hash = simple_hash(filesystem);
79
80     size_t solen = strlen(super_options);
81
82     for(mi = root; mi ; mi = mi->next)
83         if(unlikely(mi->filesystem
84                 && mi->super_options
85                 && mi->filesystem_hash == filesystem_hash
86                 && !strcmp(mi->filesystem, filesystem))) {
87
88             // super_options is a comma separated list
89             char *s = mi->super_options, *e;
90             while(*s) {
91                 e = s + 1;
92                 while(*e && *e != ',') e++;
93
94                 size_t len = e - s;
95                 if(unlikely(len == solen && !strncmp(s, super_options, len)))
96                     return mi;
97
98                 if(*e == ',') s = ++e;
99                 else s = e;
100             }
101         }
102
103     return NULL;
104 }
105
106
107 // free a linked list of mountinfo structures
108 void mountinfo_free(struct mountinfo *mi) {
109     if(unlikely(!mi))
110         return;
111
112     if(likely(mi->next))
113         mountinfo_free(mi->next);
114
115     freez(mi->root);
116     freez(mi->mount_point);
117     freez(mi->mount_options);
118     freez(mi->persistent_id);
119
120 /*
121     if(mi->optional_fields_count) {
122         int i;
123         for(i = 0; i < mi->optional_fields_count ; i++)
124             free(*mi->optional_fields[i]);
125     }
126     free(mi->optional_fields);
127 */
128     freez(mi->filesystem);
129     freez(mi->mount_source);
130     freez(mi->super_options);
131     freez(mi);
132 }
133
134 static char *strdupz_decoding_octal(const char *string) {
135     char *buffer = strdupz(string);
136
137     char *d = buffer;
138     const char *s = string;
139
140     while(*s) {
141         if(unlikely(*s == '\\')) {
142             s++;
143             if(likely(isdigit(*s) && isdigit(s[1]) && isdigit(s[2]))) {
144                 char c = *s++ - '0';
145                 c <<= 3;
146                 c |= *s++ - '0';
147                 c <<= 3;
148                 c |= *s++ - '0';
149                 *d++ = c;
150             }
151             else *d++ = '_';
152         }
153         else *d++ = *s++;
154     }
155     *d = '\0';
156
157     return buffer;
158 }
159
160 static inline int is_read_only(const char *s) {
161     if(!s) return 0;
162
163     size_t len = strlen(s);
164     if(len < 2) return 0;
165     if(len == 2) {
166         if(!strcmp(s, "ro")) return 1;
167         return 0;
168     }
169     if(!strncmp(s, "ro,", 3)) return 1;
170     if(!strncmp(&s[len - 3], ",ro", 3)) return 1;
171     if(strstr(s, ",ro,")) return 1;
172     return 0;
173 }
174
175 // read the whole mountinfo into a linked list
176 struct mountinfo *mountinfo_read(int do_statvfs) {
177     char filename[FILENAME_MAX + 1];
178     snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", netdata_configured_host_prefix);
179     procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
180     if(unlikely(!ff)) {
181         snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", netdata_configured_host_prefix);
182         ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
183         if(unlikely(!ff)) return NULL;
184     }
185
186     ff = procfile_readall(ff);
187     if(unlikely(!ff))
188         return NULL;
189
190     struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
191
192     unsigned long l, lines = procfile_lines(ff);
193     for(l = 0; l < lines ;l++) {
194         if(unlikely(procfile_linewords(ff, l) < 5))
195             continue;
196
197         mi = mallocz(sizeof(struct mountinfo));
198
199         unsigned long w = 0;
200         mi->id = str2ul(procfile_lineword(ff, l, w)); w++;
201         mi->parentid = str2ul(procfile_lineword(ff, l, w)); w++;
202
203         char *major = procfile_lineword(ff, l, w), *minor; w++;
204         for(minor = major; *minor && *minor != ':' ;minor++) ;
205
206         if(unlikely(!*minor)) {
207             error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
208             freez(mi);
209             continue;
210         }
211
212         *minor = '\0';
213         minor++;
214
215         mi->flags = 0;
216
217         mi->major = str2ul(major);
218         mi->minor = str2ul(minor);
219
220         mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
221         mi->root_hash = simple_hash(mi->root);
222
223         mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
224         mi->mount_point_hash = simple_hash(mi->mount_point);
225
226         mi->persistent_id = strdupz(mi->mount_point);
227         netdata_fix_chart_id(mi->persistent_id);
228         mi->persistent_id_hash = simple_hash(mi->persistent_id);
229
230         mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
231
232         if(unlikely(is_read_only(mi->mount_options)))
233             mi->flags |= MOUNTINFO_READONLY;
234
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 /*
248         if(unlikely(mi->optional_fields_count)) {
249             // we have some optional fields
250             // read them into a new array of pointers;
251
252             mi->optional_fields = mallocz(mi->optional_fields_count * sizeof(char *));
253
254             int i;
255             for(i = 0; i < mi->optional_fields_count ; i++) {
256                 *mi->optional_fields[wo] = strdupz(procfile_lineword(ff, l, w));
257                 wo++;
258             }
259         }
260         else
261             mi->optional_fields = NULL;
262 */
263
264         if(likely(*s == '-')) {
265             w++;
266
267             mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
268             mi->filesystem_hash = simple_hash(mi->filesystem);
269
270             mi->mount_source = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
271             mi->mount_source_hash = simple_hash(mi->mount_source);
272
273             mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
274
275             if(unlikely(is_read_only(mi->super_options)))
276                 mi->flags |= MOUNTINFO_READONLY;
277
278             if(unlikely(ME_DUMMY(mi->mount_source, mi->filesystem)))
279                 mi->flags |= MOUNTINFO_IS_DUMMY;
280
281             if(unlikely(ME_REMOTE(mi->mount_source, mi->filesystem)))
282                 mi->flags |= MOUNTINFO_IS_REMOTE;
283
284             // mark as BIND the duplicates (i.e. same filesystem + same source)
285             if(do_statvfs) {
286                 struct stat buf;
287                 if(unlikely(stat(mi->mount_point, &buf) == -1)) {
288                     mi->st_dev = 0;
289                     mi->flags |= MOUNTINFO_NO_STAT;
290                 }
291                 else {
292                     mi->st_dev = buf.st_dev;
293
294                     struct mountinfo *mt;
295                     for(mt = root; mt; mt = mt->next) {
296                         if(unlikely(mt->st_dev == mi->st_dev && !(mt->flags & MOUNTINFO_IS_SAME_DEV))) {
297                             if(strlen(mi->mount_point) < strlen(mt->mount_point))
298                                 mt->flags |= MOUNTINFO_IS_SAME_DEV;
299                             else
300                                 mi->flags |= MOUNTINFO_IS_SAME_DEV;
301                         }
302                     }
303                 }
304             }
305             else {
306                 mi->st_dev = 0;
307             }
308         }
309         else {
310             mi->filesystem = NULL;
311             mi->filesystem_hash = 0;
312
313             mi->mount_source = NULL;
314             mi->mount_source_hash = 0;
315
316             mi->super_options = NULL;
317
318             mi->st_dev = 0;
319         }
320
321         // check if it has size
322         if(do_statvfs && !(mi->flags & MOUNTINFO_IS_DUMMY)) {
323             struct statvfs buff_statvfs;
324             if(unlikely(statvfs(mi->mount_point, &buff_statvfs) < 0)) {
325                 mi->flags |= MOUNTINFO_NO_STAT;
326             }
327             else if(unlikely(!buff_statvfs.f_blocks /* || !buff_statvfs.f_files */)) {
328                 mi->flags |= MOUNTINFO_NO_SIZE;
329             }
330         }
331
332         // link it
333         if(unlikely(!root))
334             root = mi;
335         else
336             last->next = mi;
337
338         last = mi;
339         mi->next = NULL;
340
341 /*
342 #ifdef NETDATA_INTERNAL_CHECKS
343         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",
344              mi->id,
345              mi->parentid,
346              mi->major,
347              mi->minor,
348              mi->root,
349              mi->persistent_id,
350                 (mi->mount_point)?mi->mount_point:"",
351                 (mi->mount_options)?mi->mount_options:"",
352                 (mi->filesystem)?mi->filesystem:"",
353                 (mi->mount_source)?mi->mount_source:"",
354                 (mi->super_options)?mi->super_options:"",
355                 (mi->flags & MOUNTINFO_IS_DUMMY)?" DUMMY":"",
356                 (mi->flags & MOUNTINFO_IS_BIND)?" BIND":"",
357                 (mi->flags & MOUNTINFO_IS_REMOTE)?" REMOTE":"",
358                 (mi->flags & MOUNTINFO_NO_STAT)?" NOSTAT":"",
359                 (mi->flags & MOUNTINFO_NO_SIZE)?" NOSIZE":"",
360                 (mi->flags & MOUNTINFO_IS_SAME_DEV)?" SAMEDEV":""
361         );
362 #endif
363 */
364     }
365
366 /* find if the mount options have "bind" in them
367     {
368         FILE *fp = setmntent(MOUNTED, "r");
369         if (fp != NULL) {
370             struct mntent mntbuf;
371             struct mntent *mnt;
372             char buf[4096 + 1];
373
374             while ((mnt = getmntent_r(fp, &mntbuf, buf, 4096))) {
375                 char *bind = hasmntopt(mnt, "bind");
376                 if(unlikely(bind)) {
377                     struct mountinfo *mi;
378                     for(mi = root; mi ; mi = mi->next) {
379                         if(unlikely(strcmp(mnt->mnt_dir, mi->mount_point) == 0)) {
380                             fprintf(stderr, "Mount point '%s' is BIND\n", mi->mount_point);
381                             mi->flags |= MOUNTINFO_IS_BIND;
382                             break;
383                         }
384                     }
385
386 #ifdef NETDATA_INTERNAL_CHECKS
387                     if(unlikely(!mi)) {
388                         error("Mount point '%s' not found in /proc/self/mountinfo", mnt->mnt_dir);
389                     }
390 #endif
391                 }
392             }
393             endmntent(fp);
394         }
395     }
396 */
397
398     procfile_close(ff);
399     return root;
400 }