aboutsummaryrefslogtreecommitdiff
path: root/src/mountinfo.c
blob: 9a44466a4d76b0a095da057d95e36dc15a777cee (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
   mountinfo.c
   Obtains information about mounted filesystems.

   Copyright 2007 Gentoo Foundation
   */

#define APPLET "mountinfo"

#include <sys/types.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#elif defined(__linux__)
#include <limits.h>
#endif

#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "einfo.h"
#include "rc.h"
#include "rc-misc.h"
#include "strlist.h"


#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
						   regex_t *fstype_regex, regex_t *skip_fstype_regex,
						   char **mounts, bool list_nodes, bool list_fstype)
{
	struct statfs *mnts;
	int nmnts;
	int i;
	char **list = NULL;

	if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
		eerrorx ("getmntinfo: %s", strerror (errno));

	for (i = 0; i < nmnts; i++) {
		if (node_regex &&
			regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
			continue;
		if (skip_node_regex &&
			regexec (skip_node_regex, mnts[i].f_mntfromname, 0, NULL, 0) == 0)
			continue;
		if (fstype_regex &&
			regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
			continue;
		if (skip_fstype_regex &&
			regexec (skip_fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) == 0)
			continue;

		if (mounts) {
			bool found = false;
			int j;
			char *mnt;
			STRLIST_FOREACH (mounts, mnt, j)
				if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
					found = true;
					break;
				}
			if (! found)
				continue;
		}

		list = rc_strlist_addsortc (list, list_nodes ?
									mnts[i].f_mntfromname :
									list_fstype ? mnts[i].f_fstypename :
									mnts[i].f_mntonname);
	}

	return (list);
}

#elif defined (__linux__)
static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
						   regex_t *fstype_regex, regex_t *skip_fstype_regex,
						   char **mounts, bool list_nodes, bool list_fstype)
{
	FILE *fp;
	char buffer[PATH_MAX * 3];
	char *p;
	char *from;
	char *to;
	char *fstype;
	char **list = NULL;

	if ((fp = fopen ("/proc/mounts", "r")) == NULL)
		eerrorx ("getmntinfo: %s", strerror (errno));

	while (fgets (buffer, sizeof (buffer), fp)) {
		p = buffer;
		from = strsep (&p, " ");
		if (node_regex &&
			regexec (node_regex, from, 0, NULL, 0) != 0)
			continue;
		if (skip_node_regex &&
			regexec (skip_node_regex, from, 0, NULL, 0) == 0)
			continue;

		to = strsep (&p, " ");
		fstype = strsep (&p, " ");
		/* Skip the really silly rootfs */
		if (strcmp (fstype, "rootfs") == 0)
			continue;
		if (fstype_regex &&
			regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
			continue;
		if (skip_fstype_regex &&
			regexec (skip_fstype_regex, fstype, 0, NULL, 0) == 0)
			continue;

		if (mounts)	{
			bool found = false;
			int j;
			char *mnt;
			STRLIST_FOREACH (mounts, mnt, j)
				if (strcmp (mnt, to) == 0) {
					found = true;
					break;
				}
			if (! found)
				continue;
		}

		list = rc_strlist_addsortc (list,
									list_nodes ? 
									list_fstype ? fstype :
									from : to);
	}
	fclose (fp);

	return (list);
}

#else
#  error "Operating system not supported!"
#endif

static regex_t *get_regex (char *string)
{
	regex_t *reg = rc_xmalloc (sizeof (regex_t));
	int result;
	char buffer[256];

	if ((result = regcomp (reg, string, REG_EXTENDED | REG_NOSUB)) != 0)
	{
		regerror (result, reg, buffer, sizeof (buffer));
		eerrorx ("%s: invalid regex `%s'", APPLET, buffer);
	}

	return (reg);
}

#include "_usage.h"
#define getoptstring "f:F:n:N:p:P:os" getoptstring_COMMON
static struct option longopts[] = {
	{ "fstype-regex",        1, NULL, 'f'},
	{ "skip-fstype-regex",   1, NULL, 'F'},
	{ "node-regex",          1, NULL, 'n'},
	{ "skip-node-regex",     1, NULL, 'N'},
	{ "point-regex",         1, NULL, 'p'},
	{ "skip-point-regex",    1, NULL, 'P'},
	{ "list-nodes",          0, NULL, 'o'},
	{ "list-fstype",         0, NULL, 's'},
	longopts_COMMON
	{ NULL,             0, NULL, 0}
};
#include "_usage.c"

int main (int argc, char **argv)
{
	int i;
	regex_t *fstype_regex = NULL;
	regex_t *node_regex = NULL;
	regex_t *point_regex = NULL;
	regex_t *skip_fstype_regex = NULL;
	regex_t *skip_node_regex = NULL;
	regex_t *skip_point_regex = NULL;
	char **nodes = NULL;
	char *node;
	bool list_nodes = false;
	bool list_fstype = false;
	char **mounts = NULL;
	int opt;
	int result;

#define DO_REG(_var) \
	if (_var) free (_var); \
	_var = get_regex (optarg);

	while ((opt = getopt_long (argc, argv, getoptstring,
							   longopts, (int *) 0)) != -1)
	{
		switch (opt) {
			case 'f':
				DO_REG (fstype_regex);
				break;
			case 'F':
				DO_REG (skip_fstype_regex);
				break;
			case 'n':
				DO_REG (node_regex);
				break;
			case 'N':
				DO_REG (skip_node_regex);
				break;
			case 'p':
				DO_REG (point_regex);
				break;
			case 'P':
				DO_REG (skip_point_regex);
				break;
			case 'o':
				list_nodes = true;
				list_fstype = false;
				break;
			case 's':
				list_nodes = false;
				list_fstype = true;
				break;

				case_RC_COMMON_GETOPT
		}
	}

	while (optind < argc) {
		if (argv[optind][0] != '/')
			eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
		mounts = rc_strlist_add (mounts, argv[optind++]);
	}

	nodes = find_mounts (node_regex, skip_node_regex,
						 fstype_regex, skip_fstype_regex,
						 mounts, list_nodes, list_fstype);

	if (node_regex)
		regfree (node_regex);
	if (skip_node_regex)
		regfree (skip_node_regex);
	if (fstype_regex)
		regfree (fstype_regex);
	if (skip_fstype_regex)
		regfree (skip_fstype_regex);

	rc_strlist_reverse (nodes);

	result = EXIT_FAILURE;
	STRLIST_FOREACH (nodes, node, i) {
		if (point_regex && regexec (point_regex, node, 0, NULL, 0) != 0)
			continue;
		if (skip_point_regex && regexec (skip_point_regex, node, 0, NULL, 0) == 0)
			continue;
		printf ("%s\n", node);
		result = EXIT_SUCCESS;
	}
	rc_strlist_free (nodes);

	if (point_regex)
		regfree (point_regex);
	if (skip_point_regex)
		regfree (skip_point_regex);

	exit (result);
}