]> arthur.barton.de Git - netdata.git/blob - src/proc_self_mountinfo.c
Merge pull request #334 from simonnagl/bugfix/sh_arithmetic_expand
[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         if(mi->root) free(mi->root);
52         if(mi->mount_point) free(mi->mount_point);
53         if(mi->mount_options) free(mi->mount_options);
54
55 /*
56         if(mi->optional_fields_count) {
57                 int i;
58                 for(i = 0; i < mi->optional_fields_count ; i++)
59                         free(mi->optional_fields[i]);
60         }
61         free(mi->optional_fields);
62 */
63
64         free(mi->filesystem);
65         free(mi->mount_source);
66         free(mi->super_options);
67         free(mi);
68 }
69
70 // read the whole mountinfo into a linked list
71 struct mountinfo *mountinfo_read() {
72         procfile *ff = NULL;
73
74         char filename[FILENAME_MAX + 1];
75         snprintf(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
76         ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
77         if(!ff) {
78                 snprintf(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
79                 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
80                 if(!ff) return NULL;
81         }
82
83         ff = procfile_readall(ff);
84         if(!ff) return NULL;
85
86         struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
87
88         unsigned long l, lines = procfile_lines(ff);
89         for(l = 0; l < lines ;l++) {
90                 if(procfile_linewords(ff, l) < 5)
91                         continue;
92
93                 mi = malloc(sizeof(struct mountinfo));
94                 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
95
96                 if(unlikely(!root))
97                         root = last = mi;
98                 else
99                         last->next = mi;
100
101                 last = mi;
102                 mi->next = NULL;
103
104                 unsigned long w = 0;
105                 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
106                 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
107
108                 char *major = procfile_lineword(ff, l, w), *minor; w++;
109                 for(minor = major; *minor && *minor != ':' ;minor++) ;
110                 *minor = '\0';
111                 minor++;
112
113                 mi->major = strtoul(major, NULL, 10);
114                 mi->minor = strtoul(minor, NULL, 10);
115
116                 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
117                 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
118                 mi->root_hash = simple_hash(mi->root);
119
120                 mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
121                 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
122                 mi->mount_point_hash = simple_hash(mi->mount_point);
123
124                 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
125                 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
126
127                 // count the optional fields
128 /*
129                 unsigned long wo = w;
130 */
131                 mi->optional_fields_count = 0;
132                 char *s = procfile_lineword(ff, l, w);
133                 while(*s && *s != '-') {
134                         w++;
135                         s = procfile_lineword(ff, l, w);
136                         mi->optional_fields_count++;
137                 }
138
139 /*
140                 if(unlikely(mi->optional_fields_count)) {
141                         // we have some optional fields
142                         // read them into a new array of pointers;
143
144                         mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
145                         if(unlikely(!mi->optional_fields))
146                                 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
147
148                         int i;
149                         for(i = 0; i < mi->optional_fields_count ; i++) {
150                                 mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
151                                 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
152                                 wo++;
153                         }
154                 }
155                 else
156                         mi->optional_fields = NULL;
157 */
158
159                 if(likely(*s == '-')) {
160                         w++;
161
162                         mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
163                         if(!mi->filesystem) fatal("Cannot allocate memory");
164                         mi->filesystem_hash = simple_hash(mi->filesystem);
165
166                         mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
167                         if(!mi->mount_source) fatal("Cannot allocate memory");
168                         mi->mount_source_hash = simple_hash(mi->mount_source);
169
170                         mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
171                         if(!mi->super_options) fatal("Cannot allocate memory");
172                 }
173                 else {
174                         mi->filesystem = NULL;
175                         mi->mount_source = NULL;
176                         mi->super_options = NULL;
177                 }
178
179 /*
180                 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
181                      mi->id,
182                      mi->parentid,
183                      mi->major,
184                      mi->minor,
185                      mi->root,
186                      mi->mount_point,
187                      mi->mount_options,
188                      mi->filesystem,
189                      mi->mount_source,
190                      mi->super_options
191                 );
192 */
193         }
194
195         procfile_close(ff);
196         return root;
197 }