[LTP] [PATCH v2 1/2] tst_device.c: Add tst_is_mounted_ro/w check mount option

Wei Gao wegao@suse.com
Fri Aug 22 05:41:39 CEST 2025


Signed-off-by: Wei Gao <wegao@suse.com>
---
 include/tst_device.h |  2 ++
 lib/tst_device.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/tst_device.h b/include/tst_device.h
index 2597fb4e2..3ea7b5500 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -34,6 +34,8 @@ int tst_umount(const char *path);
  */
 int tst_is_mounted(const char *path);
 int tst_is_mounted_at_tmpdir(const char *path);
+int tst_is_mounted_ro(const char *path);
+int tst_is_mounted_rw(const char *path);
 
 /*
  * Clears a first few blocks of the device. This is needed when device has
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 6d1abf065..34f24be7d 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -473,6 +473,48 @@ int tst_is_mounted_at_tmpdir(const char *path)
 	return tst_is_mounted(mpath);
 }
 
+static int tst_mount_has_opt(const char *path, const char *opt)
+{
+	char line[PATH_MAX];
+	FILE *file;
+	int ret = 0;
+
+	file = SAFE_FOPEN(NULL, "/proc/mounts", "r");
+
+	while (fgets(line, sizeof(line), file)) {
+		char mount_point[PATH_MAX], options[PATH_MAX];
+
+		if (sscanf(line, "%*s %s %*s %s", mount_point, options) < 2)
+			continue;
+
+		if (strcmp(mount_point, path) != 0)
+			continue;
+
+		char *tok = strtok(options, ",");
+		while (tok) {
+			if (strcmp(tok, opt) == 0) {
+				ret = 1;
+				break;
+			}
+			tok = strtok(NULL, ",");
+		}
+		if (ret)
+			break;
+	}
+
+	return ret;
+}
+
+int tst_is_mounted_ro(const char *path)
+{
+	return tst_mount_has_opt(path, "ro");
+}
+
+int tst_is_mounted_rw(const char *path)
+{
+	return tst_mount_has_opt(path, "rw");
+}
+
 static int find_stat_file(const char *dev, char *path, size_t path_len)
 {
 	const char *devname = strrchr(dev, '/') + 1;
-- 
2.43.0



More information about the ltp mailing list