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)
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))
43 // free a linked list of mountinfo structures
44 void mountinfo_free(struct mountinfo *mi) {
49 mountinfo_free(mi->next);
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);
56 if(mi->optional_fields_count) {
58 for(i = 0; i < mi->optional_fields_count ; i++)
59 free(*mi->optional_fields[i]);
61 free(mi->optional_fields);
64 free(mi->mount_source);
65 free(mi->super_options);
69 // read the whole mountinfo into a linked list
70 struct mountinfo *mountinfo_read() {
73 char filename[FILENAME_MAX + 1];
74 snprintf(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
75 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
77 snprintf(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
78 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
82 ff = procfile_readall(ff);
85 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
87 unsigned long l, lines = procfile_lines(ff);
88 for(l = 0; l < lines ;l++) {
89 if(procfile_linewords(ff, l) < 5)
92 mi = malloc(sizeof(struct mountinfo));
93 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
104 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
105 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
107 char *major = procfile_lineword(ff, l, w), *minor; w++;
108 for(minor = major; *minor && *minor != ':' ;minor++) ;
112 mi->major = strtoul(major, NULL, 10);
113 mi->minor = strtoul(minor, NULL, 10);
115 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
116 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
117 mi->root_hash = simple_hash(mi->root);
119 mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
120 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
121 mi->mount_point_hash = simple_hash(mi->mount_point);
123 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
124 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
126 // count the optional fields
128 unsigned long wo = w;
130 mi->optional_fields_count = 0;
131 char *s = procfile_lineword(ff, l, w);
132 while(*s && *s != '-') {
134 s = procfile_lineword(ff, l, w);
135 mi->optional_fields_count++;
139 if(unlikely(mi->optional_fields_count)) {
140 // we have some optional fields
141 // read them into a new array of pointers;
143 mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
144 if(unlikely(!mi->optional_fields))
145 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
148 for(i = 0; i < mi->optional_fields_count ; i++) {
149 *mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
150 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
155 mi->optional_fields = NULL;
158 if(likely(*s == '-')) {
161 mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
162 if(!mi->filesystem) fatal("Cannot allocate memory");
163 mi->filesystem_hash = simple_hash(mi->filesystem);
165 mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
166 if(!mi->mount_source) fatal("Cannot allocate memory");
167 mi->mount_source_hash = simple_hash(mi->mount_source);
169 mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
170 if(!mi->super_options) fatal("Cannot allocate memory");
173 mi->filesystem = NULL;
174 mi->mount_source = NULL;
175 mi->super_options = NULL;
179 info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",