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 == '\\')) {
115 if(likely(isdigit(*s) && isdigit(s[1]) && isdigit(s[2]))) {
132 // read the whole mountinfo into a linked list
133 struct mountinfo *mountinfo_read() {
136 char filename[FILENAME_MAX + 1];
137 snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
138 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
140 snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
141 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
145 ff = procfile_readall(ff);
148 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
150 unsigned long l, lines = procfile_lines(ff);
151 for(l = 0; l < lines ;l++) {
152 if(procfile_linewords(ff, l) < 5)
155 mi = malloc(sizeof(struct mountinfo));
156 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
167 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
168 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
170 char *major = procfile_lineword(ff, l, w), *minor; w++;
171 for(minor = major; *minor && *minor != ':' ;minor++) ;
175 mi->major = strtoul(major, NULL, 10);
176 mi->minor = strtoul(minor, NULL, 10);
178 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
179 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
180 mi->root_hash = simple_hash(mi->root);
182 mi->mount_point = strdup_decoding_octal(procfile_lineword(ff, l, w)); w++;
183 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
184 mi->mount_point_hash = simple_hash(mi->mount_point);
186 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
187 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
189 // count the optional fields
191 unsigned long wo = w;
193 mi->optional_fields_count = 0;
194 char *s = procfile_lineword(ff, l, w);
195 while(*s && *s != '-') {
197 s = procfile_lineword(ff, l, w);
198 mi->optional_fields_count++;
202 if(unlikely(mi->optional_fields_count)) {
203 // we have some optional fields
204 // read them into a new array of pointers;
206 mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
207 if(unlikely(!mi->optional_fields))
208 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
211 for(i = 0; i < mi->optional_fields_count ; i++) {
212 *mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
213 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
218 mi->optional_fields = NULL;
221 if(likely(*s == '-')) {
224 mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
225 if(!mi->filesystem) fatal("Cannot allocate memory");
226 mi->filesystem_hash = simple_hash(mi->filesystem);
228 mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
229 if(!mi->mount_source) fatal("Cannot allocate memory");
230 mi->mount_source_hash = simple_hash(mi->mount_source);
232 mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
233 if(!mi->super_options) fatal("Cannot allocate memory");
236 mi->filesystem = NULL;
237 mi->mount_source = NULL;
238 mi->super_options = NULL;
242 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",