13 #include "appconfig.h"
15 #include "proc_self_mountinfo.h"
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) {
22 for(mi = root; mi ; mi = mi->next)
23 if(mi->major == major && mi->minor == minor)
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) {
33 uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
35 for(mi = root; mi ; mi = mi->next)
38 && mi->filesystem_hash == filesystem_hash
39 && mi->mount_source_hash == mount_source_hash
40 && !strcmp(mi->filesystem, filesystem)
41 && !strcmp(mi->mount_source, mount_source))
47 struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
49 uint32_t filesystem_hash = simple_hash(filesystem);
51 size_t solen = strlen(super_options);
53 for(mi = root; mi ; mi = mi->next)
56 && mi->filesystem_hash == filesystem_hash
57 && !strcmp(mi->filesystem, filesystem)) {
59 // super_options is a comma separated list
60 char *s = mi->super_options, *e;
63 while(*e && *e != ',') e++;
66 if(len == solen && !strncmp(s, super_options, len))
69 if(*e == ',') s = ++e;
78 // free a linked list of mountinfo structures
79 void mountinfo_free(struct mountinfo *mi) {
84 mountinfo_free(mi->next);
86 if(mi->root) free(mi->root);
87 if(mi->mount_point) free(mi->mount_point);
88 if(mi->mount_options) free(mi->mount_options);
91 if(mi->optional_fields_count) {
93 for(i = 0; i < mi->optional_fields_count ; i++)
94 free(*mi->optional_fields[i]);
96 free(mi->optional_fields);
99 free(mi->mount_source);
100 free(mi->super_options);
104 // read the whole mountinfo into a linked list
105 struct mountinfo *mountinfo_read() {
108 char filename[FILENAME_MAX + 1];
109 snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
110 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
112 snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
113 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
117 ff = procfile_readall(ff);
120 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
122 unsigned long l, lines = procfile_lines(ff);
123 for(l = 0; l < lines ;l++) {
124 if(procfile_linewords(ff, l) < 5)
127 mi = malloc(sizeof(struct mountinfo));
128 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
139 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
140 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
142 char *major = procfile_lineword(ff, l, w), *minor; w++;
143 for(minor = major; *minor && *minor != ':' ;minor++) ;
147 mi->major = strtoul(major, NULL, 10);
148 mi->minor = strtoul(minor, NULL, 10);
150 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
151 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
152 mi->root_hash = simple_hash(mi->root);
154 mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
155 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
156 mi->mount_point_hash = simple_hash(mi->mount_point);
158 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
159 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
161 // count the optional fields
163 unsigned long wo = w;
165 mi->optional_fields_count = 0;
166 char *s = procfile_lineword(ff, l, w);
167 while(*s && *s != '-') {
169 s = procfile_lineword(ff, l, w);
170 mi->optional_fields_count++;
174 if(unlikely(mi->optional_fields_count)) {
175 // we have some optional fields
176 // read them into a new array of pointers;
178 mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
179 if(unlikely(!mi->optional_fields))
180 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
183 for(i = 0; i < mi->optional_fields_count ; i++) {
184 *mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
185 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
190 mi->optional_fields = NULL;
193 if(likely(*s == '-')) {
196 mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
197 if(!mi->filesystem) fatal("Cannot allocate memory");
198 mi->filesystem_hash = simple_hash(mi->filesystem);
200 mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
201 if(!mi->mount_source) fatal("Cannot allocate memory");
202 mi->mount_source_hash = simple_hash(mi->mount_source);
204 mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
205 if(!mi->super_options) fatal("Cannot allocate memory");
208 mi->filesystem = NULL;
209 mi->mount_source = NULL;
210 mi->super_options = NULL;
214 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",