summaryrefslogtreecommitdiffstats
path: root/kernel/tests/lib/tst_path_has_mnt_flags.c
blob: 154bf4126eaab9bd06c8f264689002e1ab9c411f (plain) (blame)
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
/*
 * Copyright (c) 2014 Fujitsu Ltd.
 * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <unistd.h>
#include <mntent.h>
#include <stdio.h>
#include <string.h>
#include "test.h"

/*
 * Check whether a path is on a filesystem that is mounted with
 * specified flags.
 */
int tst_path_has_mnt_flags_(void (cleanup_fn)(void),
		const char *path, const char *flags[])
{
	struct mntent *mnt;
	size_t prefix_max = 0, prefix_len;
	int flags_matched = 0;
	FILE *f;
	int i;
	char *tmpdir = NULL;

	/*
	 * Default parameter is test temporary directory
	 */
	if (path == NULL)
		path = tmpdir = tst_get_tmpdir();

	if (access(path, F_OK) == -1) {
		tst_brkm(TBROK | TERRNO, cleanup_fn,
			"tst_path_has_mnt_flags: path %s doesn't exist", path);
		return -1;
	}

	f = setmntent("/proc/mounts", "r");
	if (f == NULL) {
		tst_brkm(TBROK | TERRNO, cleanup_fn,
			"tst_path_has_mnt_flags: failed to open /proc/mounts");
		return -1;
	}

	while ((mnt = getmntent(f))) {
		/* ignore duplicit record for root fs */
		if (!strcmp(mnt->mnt_fsname, "rootfs"))
			continue;

		prefix_len = strlen(mnt->mnt_dir);

		if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
				&& prefix_len > prefix_max) {
			prefix_max = prefix_len;
			flags_matched = 0;
			i = 0;

			while (flags[i] != NULL) {
				if (hasmntopt(mnt, flags[i]) != NULL)
					flags_matched++;
				i++;
			}
		}
	}

	endmntent(f);

	free(tmpdir);

	return flags_matched;
}