summaryrefslogtreecommitdiffstats
path: root/src/proc_self_mountinfo.c
blob: 45630b4c0c3f7b1881b3b6e6d97d3a40cd27b3a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "common.h"
#include "log.h"
#include "appconfig.h"

#include "proc_self_mountinfo.h"

// find the mount info with the given major:minor
// in the supplied linked list of mountinfo structures
struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor) {
	struct mountinfo *mi;

	for(mi = root; mi ; mi = mi->next)
		if(mi->major == major && mi->minor == minor)
			return mi;

	return NULL;
}

// find the mount info with the given filesystem and mount_source
// in the supplied linked list of mountinfo structures
struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
	struct mountinfo *mi;
	uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);

	for(mi = root; mi ; mi = mi->next)
		if(mi->filesystem
		   		&& mi->mount_source
				&& mi->filesystem_hash == filesystem_hash
		   		&& mi->mount_source_hash == mount_source_hash
				&& !strcmp(mi->filesystem, filesystem)
		   		&& !strcmp(mi->mount_source, mount_source))
			return mi;

	return NULL;
}

struct mountinfo *mountinfo_find_by_filesystem_super_option(struct mountinfo *root, const char *filesystem, const char *super_options) {
	struct mountinfo *mi;
	uint32_t filesystem_hash = simple_hash(filesystem);

	size_t solen = strlen(super_options);

	for(mi = root; mi ; mi = mi->next)
		if(mi->filesystem
		   		&& mi->super_options
				&& mi->filesystem_hash == filesystem_hash
		   		&& !strcmp(mi->filesystem, filesystem)) {

			// super_options is a comma separated list
			char *s = mi->super_options, *e;
			while(*s) {
				e = ++s;
				while(*e && *e != ',') e++;

				size_t len = e - s;
				if(len == solen && !strncmp(s, super_options, len))
					return mi;

				if(*e == ',') s = ++e;
				else s = e;
			}
		}

	return NULL;
}


// free a linked list of mountinfo structures
void mountinfo_free(struct mountinfo *mi) {
	if(unlikely(!mi))
		return;

	if(likely(mi->next))
		mountinfo_free(mi->next);

	if(mi->root) free(mi->root);
	if(mi->mount_point) free(mi->mount_point);
	if(mi->mount_options) free(mi->mount_options);

/*
	if(mi->optional_fields_count) {
		int i;
		for(i = 0; i < mi->optional_fields_count ; i++)
			free(*mi->optional_fields[i]);
	}
	free(mi->optional_fields);
*/
	free(mi->filesystem);
	free(mi->mount_source);
	free(mi->super_options);
	free(mi);
}

// read the whole mountinfo into a linked list
struct mountinfo *mountinfo_read() {
	procfile *ff = NULL;

	char filename[FILENAME_MAX + 1];
	snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
	ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
	if(!ff) {
		snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
		ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
		if(!ff) return NULL;
	}

	ff = procfile_readall(ff);
	if(!ff) return NULL;

	struct mountinfo *root = NULL, *last = NULL, *mi = NULL;

	unsigned long l, lines = procfile_lines(ff);
	for(l = 0; l < lines ;l++) {
		if(procfile_linewords(ff, l) < 5)
			continue;

		mi = malloc(sizeof(struct mountinfo));
		if(unlikely(!mi)) fatal("Cannot allocate memory for mountinfo");

		if(unlikely(!root))
			root = last = mi;
		else
			last->next = mi;

		last = mi;
		mi->next = NULL;

		unsigned long w = 0;
		mi->id = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;
		mi->parentid = strtoul(procfile_lineword(ff, l, w), NULL, 10); w++;

		char *major = procfile_lineword(ff, l, w), *minor; w++;
		for(minor = major; *minor && *minor != ':' ;minor++) ;
		*minor = '\0';
		minor++;

		mi->major = strtoul(major, NULL, 10);
		mi->minor = strtoul(minor, NULL, 10);

		mi->root = strdup(procfile_lineword(ff, l, w)); w++;
		if(unlikely(!mi->root)) fatal("Cannot allocate memory");
		mi->root_hash = simple_hash(mi->root);

		mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
		if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
		mi->mount_point_hash = simple_hash(mi->mount_point);

		mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
		if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");

		// count the optional fields
/*
		unsigned long wo = w;
*/
		mi->optional_fields_count = 0;
		char *s = procfile_lineword(ff, l, w);
		while(*s && *s != '-') {
			w++;
			s = procfile_lineword(ff, l, w);
			mi->optional_fields_count++;
		}

/*
		if(unlikely(mi->optional_fields_count)) {
			// we have some optional fields
			// read them into a new array of pointers;

			mi->optional_fields = malloc(mi->optional_fields_count * sizeof(char *));
			if(unlikely(!mi->optional_fields))
				fatal("Cannot allocate memory for %d mountinfo optional fields", mi->optional_fields_count);

			int i;
			for(i = 0; i < mi->optional_fields_count ; i++) {
				*mi->optional_fields[wo] = strdup(procfile_lineword(ff, l, w));
				if(!mi->optional_fields[wo]) fatal("Cannot allocate memory");
				wo++;
			}
		}
		else
			mi->optional_fields = NULL;
*/

		if(likely(*s == '-')) {
			w++;

			mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
			if(!mi->filesystem) fatal("Cannot allocate memory");
			mi->filesystem_hash = simple_hash(mi->filesystem);

			mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
			if(!mi->mount_source) fatal("Cannot allocate memory");
			mi->mount_source_hash = simple_hash(mi->mount_source);

			mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
			if(!mi->super_options) fatal("Cannot allocate memory");
		}
		else {
			mi->filesystem = NULL;
			mi->mount_source = NULL;
			mi->super_options = NULL;
		}

/*
		info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
		     mi->id,
		     mi->parentid,
		     mi->major,
		     mi->minor,
		     mi->root,
		     mi->mount_point,
		     mi->mount_options,
		     mi->filesystem,
		     mi->mount_source,
		     mi->super_options
		);
*/
	}

	procfile_close(ff);
	return root;
}