summaryrefslogtreecommitdiff
path: root/libfstools/find.c
blob: 1d91c515b42070406dee79a00dcc5f0575f8a0fd (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
/*
 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>

#include "libfstools.h"

int
find_overlay_mount(char *overlay)
{
	FILE *fp = fopen("/proc/mounts", "r");
	size_t len = strlen(overlay);
	static char line[256];
	int ret = -1;

	if(!fp)
		return ret;

	while (ret && fgets(line, sizeof(line), fp))
		if (len < sizeof(line) && !strncmp(line, overlay, len) && line[len] == ' ')
			ret = 0;

	fclose(fp);

	return ret;
}

/*
 * Find path of a device mounted to the given point.
 */
char*
find_mount(char *mp)
{
	FILE *fp = fopen("/proc/mounts", "r");
	static char line[256];

	if(!fp)
		return NULL;

	while (fgets(line, sizeof(line), fp)) {
		char *s, *t = strstr(line, " ");

		if (!t) {
			fclose(fp);
			return NULL;
		}
		*t = '\0';
		t++;
		s = strstr(t, " ");
		if (!s) {
			fclose(fp);
			return NULL;
		}
		*s = '\0';

		if (!strcmp(t, mp)) {
			fclose(fp);
			return line;
		}
	}

	fclose(fp);

	return NULL;
}

/*
 * Match filesystem type against a bunch of valid types
 *
 * jffs2reset may ask if the filesystem type is actually ready for use
 * with overlayfs before wiping it...
 */
static int fs_rootfs_only(char *fstype)
{
	if (strncmp(fstype, "ext4", 4) &&
	    strncmp(fstype, "f2fs", 4) &&
	    strncmp(fstype, "jffs2", 5) &&
	    strncmp(fstype, "ubifs", 5)) {
		ULOG_ERR("block is mounted with wrong fs\n");
		return 1;
	}
	return 0;
}

/*
 * Check if a given device is mounted and return its mountpoint
 */
char*
find_mount_point(char *block, int root_only)
{
	FILE *fp = fopen("/proc/self/mountinfo", "r");
	static char line[256];
	char *point = NULL, *pos, *tmp, *cpoint, *devname, *fstype;
	struct stat s;
	int rstat;
	unsigned int minor, major;

	if (!block)
		return NULL;

	if (!fp)
		return NULL;

	rstat = stat(block, &s);

	while (fgets(line, sizeof(line), fp)) {
		/* skip first two columns */
		pos = strchr(line, ' ');
		if (!pos)
			continue;

		pos = strchr(pos + 1, ' ');
		if (!pos)
			continue;

		/* extract block device major:minor */
		tmp = ++pos;
		pos = strchr(pos, ':');
		if (!pos)
			continue;

		*pos = '\0';
		major = atoi(tmp);

		tmp = ++pos;
		pos = strchr(pos, ' ');
		if (!pos)
			continue;

		*pos = '\0';
		minor = atoi(tmp);

		/* skip another column */
		pos = strchr(pos + 1, ' ');
		if (!pos)
			continue;

		/* get mountpoint */
		tmp = ++pos;
		pos = strchr(pos, ' ');
		if (!pos)
			continue;

		*pos = '\0';
		cpoint = tmp;

		/* skip another two columns */
		pos = strchr(pos + 1, ' ');
		if (!pos)
			continue;

		pos = strchr(pos + 1, ' ');
		if (!pos)
			continue;

		/* get fstype */
		tmp = ++pos;
		pos = strchr(pos, ' ');
		if (!pos)
			continue;

		*pos = '\0';
		fstype = tmp;

		/* get device name */
		tmp = ++pos;
		pos = strchr(pos, ' ');
		if (!pos)
			continue;

		*pos = '\0';
		devname = tmp;

		/* if device name matches */
		if (!strcmp(block, devname)) {
			if (root_only && fs_rootfs_only(fstype))
				break;

			/* found, return mountpoint */
			point = strdup(cpoint);
			break;
		}

		/* last chance: check if major:minor of block device match */
		if (rstat)
			continue;

		if (!S_ISBLK(s.st_mode))
			continue;

		if (major == major(s.st_rdev) &&
		    minor == minor(s.st_rdev)) {
			if (root_only && fs_rootfs_only(fstype))
				break;

			/* found, return mountpoint */
			point = strdup(cpoint);
			break;
		}
	}

	fclose(fp);

	return point;
}

int
find_filesystem(char *fs)
{
	FILE *fp = fopen("/proc/filesystems", "r");
	static char line[256];
	int ret = -1;

	if (!fp) {
		ULOG_ERR("opening /proc/filesystems failed: %m\n");
		goto out;
	}

	while (ret && fgets(line, sizeof(line), fp))
		if (strstr(line, fs))
			ret = 0;

	fclose(fp);

out:
	return ret;
}