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 // free a linked list of mountinfo structures
30 void mountinfo_free(struct mountinfo *mi) {
35 mountinfo_free(mi->next);
38 free(mi->mount_point);
39 free(mi->mount_options);
41 if(mi->optional_fields_count) {
43 for(i = 0; i < mi->optional_fields_count ; i++)
44 free(mi->optional_fields[i]);
46 free(mi->optional_fields);
49 free(mi->mount_source);
50 free(mi->super_options);
54 // read the whole mountinfo into a linked list
55 struct mountinfo *mountinfo_read() {
58 char filename[FILENAME_MAX + 1];
59 snprintf(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
60 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
62 snprintf(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
63 ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
67 ff = procfile_readall(ff);
70 struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
72 unsigned long l, lines = procfile_lines(ff);
73 error("MOUNTINFO: file has %u lines", lines);
74 for(l = 0; l < lines ;l++) {
75 if(procfile_linewords(ff, l) < 5)
78 mi = malloc(sizeof(struct mountinfo));
79 if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");
90 mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
91 mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
93 char *major = procfile_lineword(ff, l, w), *minor; w++;
94 for(minor = major; *minor && *minor != ':' ;minor++) ;
98 mi->major = strtoul(major, NULL, 10);
99 mi->minor = strtoul(minor, NULL, 10);
101 mi->root = strdup(procfile_lineword(ff, l, w)); w++;
102 if(unlikely(!mi->root)) fatal("Cannot allocate memory");
104 mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
105 if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
107 mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
108 if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
110 // count the optional fields
111 unsigned long wo = w;
112 mi->optional_fields_count = 0;
113 char *s = procfile_lineword(ff, l, w);
114 while(*s && *s != '-') {
116 s = procfile_lineword(ff, l, w);
117 mi->optional_fields_count++;
120 if(unlikely(mi->optional_fields_count)) {
121 // we have some optional fields
122 // read them into a new array of pointers;
124 mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
125 if(unlikely(!mi->optional_fields))
126 fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);
129 for(i = 0; i < mi->optional_fields_count ; i++) {
130 mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
131 if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
136 mi->optional_fields = NULL;
138 if(likely(*s == '-')) {
139 mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
140 if(!mi->filesystem) fatal("Cannot allocate memory");
142 mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
143 if(!mi->mount_source) fatal("Cannot allocate memory");
145 mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
146 if(!mi->super_options) fatal("Cannot allocate memory");
149 mi->filesystem = NULL;
150 mi->mount_source = NULL;
151 mi->super_options = NULL;
154 //info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
161 // mi->mount_options,