]> arthur.barton.de Git - netdata.git/blob - src/proc_self_mountinfo.c
Merge pull request #335 from ktsaou/master
[netdata.git] / src / proc_self_mountinfo.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10
11 #include "common.h"
12 #include "log.h"
13 #include "appconfig.h"
14
15 #include "proc_self_mountinfo.h"
16
17 // find the mount info with the given major:minor
18 // in the supplied linked list of mountinfo structures
19 struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor) {
20         struct mountinfo *mi;
21
22         for(mi = root; mi ; mi = mi->next)
23                 if(mi->major == major && mi->minor == minor)
24                         return mi;
25
26         return NULL;
27 }
28
29 // find the mount info with the given filesystem and mount_source
30 // in the supplied linked list of mountinfo structures
31 struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
32         struct mountinfo *mi;
33         uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
34
35         for(mi = root; mi ; mi = mi->next)
36                 if(mi->filesystem_hash == filesystem_hash && mi->mount_source_hash == mount_source_hash
37                                 && !strcmp(mi->filesystem, filesystem) && !strcmp(mi->mount_source, mount_source))
38                         return mi;
39
40         return NULL;
41 }
42
43 // free a linked list of mountinfo structures
44 void mountinfo_free(struct mountinfo *mi) {
45         if(unlikely(!mi))
46                 return;
47
48         if(likely(mi->next))
49                 mountinfo_free(mi->next);
50
51         free(mi->root);
52         free(mi->mount_point);
53         free(mi->mount_options);
54
55         if(mi->optional_fields_count) {
56                 int i;
57                 for(i = 0; i < mi->optional_fields_count ; i++)
58                         free(mi->optional_fields[i]);
59         }
60         free(mi->optional_fields);
61
62         free(mi->filesystem);
63         free(mi->mount_source);
64         free(mi->super_options);
65         free(mi);
66 }
67
68 // read the whole mountinfo into a linked list
69 struct mountinfo *mountinfo_read() {
70         procfile *ff = NULL;
71
72         char filename[FILENAME_MAX + 1];
73         snprintf(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
74         ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
75         if(!ff) {
76                 snprintf(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
77                 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
78                 if(!ff) return NULL;
79         }
80
81         ff = procfile_readall(ff);
82         if(!ff) return NULL;
83
84         struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
85
86         unsigned long l, lines = procfile_lines(ff);
87         for(l = 0; l < lines ;l++) {
88                 if(procfile_linewords(ff, l) < 5)
89                         continue;
90
91                 mi = malloc(sizeof(struct mountinfo));
92                 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
93
94                 if(unlikely(!root))
95                         root = last = mi;
96                 else
97                         last->next = mi;
98
99                 last = mi;
100                 mi->next = NULL;
101
102                 unsigned long w = 0;
103                 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
104                 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
105
106                 char *major = procfile_lineword(ff, l, w), *minor; w++;
107                 for(minor = major; *minor && *minor != ':' ;minor++) ;
108                 *minor = '\0';
109                 minor++;
110
111                 mi->major = strtoul(major, NULL, 10);
112                 mi->minor = strtoul(minor, NULL, 10);
113
114                 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
115                 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
116                 mi->root_hash = simple_hash(mi->root);
117
118                 mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
119                 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
120                 mi->mount_point_hash = simple_hash(mi->mount_point);
121
122                 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
123                 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
124
125                 // count the optional fields
126                 unsigned long wo = w;
127                 mi->optional_fields_count = 0;
128                 char *s = procfile_lineword(ff, l, w);
129                 while(*s && *s != '-') {
130                         w++;
131                         s = procfile_lineword(ff, l, w);
132                         mi->optional_fields_count++;
133                 }
134
135                 if(unlikely(mi->optional_fields_count)) {
136                         // we have some optional fields
137                         // read them into a new array of pointers;
138
139                         mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
140                         if(unlikely(!mi->optional_fields))
141                                 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
142
143                         int i;
144                         for(i = 0; i < mi->optional_fields_count ; i++) {
145                                 mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
146                                 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
147                                 wo++;
148                         }
149                 }
150                 else
151                         mi->optional_fields = NULL;
152
153                 if(likely(*s == '-')) {
154                         w++;
155
156                         mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
157                         if(!mi->filesystem) fatal("Cannot allocate memory");
158                         mi->filesystem_hash = simple_hash(mi->filesystem);
159
160                         mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
161                         if(!mi->mount_source) fatal("Cannot allocate memory");
162                         mi->mount_source_hash = simple_hash(mi->mount_source);
163
164                         mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
165                         if(!mi->super_options) fatal("Cannot allocate memory");
166                 }
167                 else {
168                         mi->filesystem = NULL;
169                         mi->mount_source = NULL;
170                         mi->super_options = NULL;
171                 }
172
173 /*
174                 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
175                      mi->id,
176                      mi->parentid,
177                      mi->major,
178                      mi->minor,
179                      mi->root,
180                      mi->mount_point,
181                      mi->mount_options,
182                      mi->filesystem,
183                      mi->mount_source,
184                      mi->super_options
185                 );
186 */
187         }
188
189         procfile_close(ff);
190         return root;
191 }