14 #include "appconfig.h"
16 #include "proc_self_mountinfo.h"
18 // find the mount info with the given major:minor
19 // in the supplied linked list of mountinfo structures
20 struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor) {
23 for(mi = root; mi ; mi = mi->next)
24 if(mi->major == major && mi->minor == minor)
30 // find the mount info with the given filesystem and mount_source
31 // in the supplied linked list of mountinfo structures
32 struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
34 uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
36 for(mi = root; mi ; mi = mi->next)
39 && mi->filesystem_hash == filesystem_hash
40 && mi->mount_source_hash == mount_source_hash
41 && !strcmp(mi->filesystem, filesystem)
42 && !strcmp(mi->mount_source, mount_source))
48 struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
50 uint32_t filesystem_hash = simple_hash(filesystem);
52 size_t solen = strlen(super_options);
54 for(mi = root; mi ; mi = mi->next)
57 && mi->filesystem_hash == filesystem_hash
58 && !strcmp(mi->filesystem, filesystem)) {
60 // super_options is a comma separated list
61 char *s = mi->super_options, *e;
64 while(*e && *e != ',') e++;
67 if(len == solen && !strncmp(s, super_options, len))
70 if(*e == ',') s = ++e;
79 // free a linked list of mountinfo structures
80 void mountinfo_free(struct mountinfo *mi) {
85 mountinfo_free(mi->next);
87 if(mi->root) free(mi->root);
88 if(mi->mount_point) free(mi->mount_point);
89 if(mi->mount_options) free(mi->mount_options);
92 if(mi->optional_fields_count) {
94 for(i = 0; i < mi->optional_fields_count ; i++)
95 free(*mi->optional_fields[i]);
97 free(mi->optional_fields);
100 free(mi->mount_source);
101 free(mi->super_options);
105 static char *strdup_decoding_octal(const char *string) {
106 char *buffer = strdup(string);
107 if(!buffer) fatal("Cannot allocate memory.");
110 const char *s = string;
113 if(unlikely(*s == '\\')) {
114 if(likely(isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3]))) {
134 // read the whole mountinfo into a linked list
135 struct mountinfo *mountinfo_read() {
138 char filename[FILENAME_MAX + 1];
139 snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
140 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
142 snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
143 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
147 ff = procfile_readall(ff);
150 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
152 unsigned long l, lines = procfile_lines(ff);
153 for(l = 0; l < lines ;l++) {
154 if(procfile_linewords(ff, l) < 5)
157 mi = malloc(sizeof(struct mountinfo));
158 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
169 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
170 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
172 char *major = procfile_lineword(ff, l, w), *minor; w++;
173 for(minor = major; *minor && *minor != ':' ;minor++) ;
177 mi->major = strtoul(major, NULL, 10);
178 mi->minor = strtoul(minor, NULL, 10);
180 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
181 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
182 mi->root_hash = simple_hash(mi->root);
184 mi->mount_point = strdup_decoding_octal(procfile_lineword(ff, l, w)); w++;
185 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
186 mi->mount_point_hash = simple_hash(mi->mount_point);
188 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
189 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
191 // count the optional fields
193 unsigned long wo = w;
195 mi->optional_fields_count = 0;
196 char *s = procfile_lineword(ff, l, w);
197 while(*s && *s != '-') {
199 s = procfile_lineword(ff, l, w);
200 mi->optional_fields_count++;
204 if(unlikely(mi->optional_fields_count)) {
205 // we have some optional fields
206 // read them into a new array of pointers;
208 mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
209 if(unlikely(!mi->optional_fields))
210 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
213 for(i = 0; i < mi->optional_fields_count ; i++) {
214 *mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
215 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
220 mi->optional_fields = NULL;
223 if(likely(*s == '-')) {
226 mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
227 if(!mi->filesystem) fatal("Cannot allocate memory");
228 mi->filesystem_hash = simple_hash(mi->filesystem);
230 mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
231 if(!mi->mount_source) fatal("Cannot allocate memory");
232 mi->mount_source_hash = simple_hash(mi->mount_source);
234 mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
235 if(!mi->super_options) fatal("Cannot allocate memory");
238 mi->filesystem = NULL;
239 mi->mount_source = NULL;
240 mi->super_options = NULL;
244 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",