3 // find the mount info with the given major:minor
4 // in the supplied linked list of mountinfo structures
5 struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor) {
8 for(mi = root; mi ; mi = mi->next)
9 if(mi->major == major && mi->minor == minor)
15 // find the mount info with the given filesystem and mount_source
16 // in the supplied linked list of mountinfo structures
17 struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
19 uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
21 for(mi = root; mi ; mi = mi->next)
24 && mi->filesystem_hash == filesystem_hash
25 && mi->mount_source_hash == mount_source_hash
26 && !strcmp(mi->filesystem, filesystem)
27 && !strcmp(mi->mount_source, mount_source))
33 struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
35 uint32_t filesystem_hash = simple_hash(filesystem);
37 size_t solen = strlen(super_options);
39 for(mi = root; mi ; mi = mi->next)
42 && mi->filesystem_hash == filesystem_hash
43 && !strcmp(mi->filesystem, filesystem)) {
45 // super_options is a comma separated list
46 char *s = mi->super_options, *e;
49 while(*e && *e != ',') e++;
52 if(len == solen && !strncmp(s, super_options, len))
55 if(*e == ',') s = ++e;
64 // free a linked list of mountinfo structures
65 void mountinfo_free(struct mountinfo *mi) {
70 mountinfo_free(mi->next);
73 freez(mi->mount_point);
74 freez(mi->mount_options);
77 if(mi->optional_fields_count) {
79 for(i = 0; i < mi->optional_fields_count ; i++)
80 free(*mi->optional_fields[i]);
82 free(mi->optional_fields);
84 freez(mi->filesystem);
85 freez(mi->mount_source);
86 freez(mi->super_options);
90 static char *strdupz_decoding_octal(const char *string) {
91 char *buffer = strdupz(string);
94 const char *s = string;
97 if(unlikely(*s == '\\')) {
99 if(likely(isdigit(*s) && isdigit(s[1]) && isdigit(s[2]))) {
116 // read the whole mountinfo into a linked list
117 struct mountinfo *mountinfo_read() {
120 char filename[FILENAME_MAX + 1];
121 snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
122 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
124 snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
125 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
129 ff = procfile_readall(ff);
132 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
134 unsigned long l, lines = procfile_lines(ff);
135 for(l = 0; l < lines ;l++) {
136 if(procfile_linewords(ff, l) < 5)
139 mi = mallocz(sizeof(struct mountinfo));
150 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
151 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
153 char *major = procfile_lineword(ff, l, w), *minor; w++;
154 for(minor = major; *minor && *minor != ':' ;minor++) ;
157 error("Cannot parse major:minor on '%s' at line %lu of '%s'", major, l + 1, filename);
164 mi->major = strtoul(major, NULL, 10);
165 mi->minor = strtoul(minor, NULL, 10);
167 mi->root = strdupz(procfile_lineword(ff, l, w)); w++;
168 mi->root_hash = simple_hash(mi->root);
170 mi->mount_point = strdupz_decoding_octal(procfile_lineword(ff, l, w)); w++;
171 mi->mount_point_hash = simple_hash(mi->mount_point);
173 mi->mount_options = strdupz(procfile_lineword(ff, l, w)); w++;
175 // count the optional fields
177 unsigned long wo = w;
179 mi->optional_fields_count = 0;
180 char *s = procfile_lineword(ff, l, w);
181 while(*s && *s != '-') {
183 s = procfile_lineword(ff, l, w);
184 mi->optional_fields_count++;
188 if(unlikely(mi->optional_fields_count)) {
189 // we have some optional fields
190 // read them into a new array of pointers;
192 mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
193 if(unlikely(!mi->optional_fields))
194 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
197 for(i = 0; i < mi->optional_fields_count ; i++) {
198 *mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
199 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
204 mi->optional_fields = NULL;
207 if(likely(*s == '-')) {
210 mi->filesystem = strdupz(procfile_lineword(ff, l, w)); w++;
211 mi->filesystem_hash = simple_hash(mi->filesystem);
213 mi->mount_source = strdupz(procfile_lineword(ff, l, w)); w++;
214 mi->mount_source_hash = simple_hash(mi->mount_source);
216 mi->super_options = strdupz(procfile_lineword(ff, l, w)); w++;
219 mi->filesystem = NULL;
220 mi->mount_source = NULL;
221 mi->super_options = NULL;
225 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",