Skip to contentSkip to conversation

Add SAFE_TRY_FILE_PRINTF() helper function

PATCH In review Martin Doucha 3 participants 5 messages Sep 21, 2021 – Sep 23, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add SAFE_TRY_FILE_PRINTF() helper function

SAFE_TRY_FILE_PRINTF() works just like SAFE_FILE_PRINTF() but it quietly returns if the path doesn't exist instead of terminating the test.

---
 include/safe_file_ops_fn.h  |  4 ++++
 include/tst_safe_file_ops.h |  5 +++++
 lib/safe_file_ops.c         | 34 ++++++++++++++++++++++++++--------
 3 files changed, 35 insertions(+), 8 deletions(-)
include/safe_file_ops_fn.h +4 -0
index 6d680967b..e8ed85382 100644
@@ -62,6 +62,10 @@ void safe_file_printf(const char *file, const int lineno,
                       const char *path, const char *fmt, ...)
                       __attribute__ ((format (printf, 5, 6)));
 
+void safe_try_file_printf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
+	__attribute__ ((format (printf, 5, 6)));
+
 /*
  * Safe function to copy files, no more system("cp ...") please.
  */
include/tst_safe_file_ops.h +5 -0
index 223eddd1f..62f6600ec 100644
@@ -44,6 +44,11 @@
 	safe_file_printf(__FILE__, __LINE__, NULL, \
 	                 (path), (fmt), ## __VA_ARGS__)
 
+/* Same as SAFE_FILE_PRINTF() but returns quietly if the path doesn't exist */
+#define SAFE_TRY_FILE_PRINTF(path, fmt, ...) \
+	safe_try_file_printf(__FILE__, __LINE__, NULL, \
+		(path), (fmt), ## __VA_ARGS__)
+
 #define SAFE_CP(src, dst) \
 	safe_cp(__FILE__, __LINE__, NULL, (src), (dst))
 
lib/safe_file_ops.c +26 -8
index 249a512a1..f803691d8 100644
@@ -250,11 +250,10 @@ err:
 	return 1;
 }
 
-void safe_file_printf(const char *file, const int lineno,
-		      void (*cleanup_fn) (void),
-		      const char *path, const char *fmt, ...)
+static void safe_file_vprintf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt,
+	va_list va)
 {
-	va_list va;
 	FILE *f;
 
 	f = fopen(path, "w");
@@ -265,16 +264,12 @@ void safe_file_printf(const char *file, const int lineno,
 		return;
 	}
 
-	va_start(va, fmt);
-
 	if (vfprintf(f, fmt, va) < 0) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
 			"Failed to print to FILE '%s'", path);
 		return;
 	}
 
-	va_end(va);
-
 	if (fclose(f)) {
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Failed to close FILE '%s'", path);
@@ -282,6 +277,29 @@ void safe_file_printf(const char *file, const int lineno,
 	}
 }
 
+void safe_file_printf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
+{
+	va_list va;
+
+	va_start(va, fmt);
+	safe_file_vprintf(file, lineno, cleanup_fn, path, fmt, va);
+	va_end(va);
+}
+
+void safe_try_file_printf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
+{
+	va_list va;
+
+	if (access(path, F_OK))
+		return;
+
+	va_start(va, fmt);
+	safe_file_vprintf(file, lineno, cleanup_fn, path, fmt, va);
+	va_end(va);
+}
+
 //TODO: C implementation? better error condition reporting?
 int safe_cp(const char *file, const int lineno,
 	     void (*cleanup_fn) (void), const char *src, const char *dst)
Signature · 5 lines
2.33.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/2 Fix failures due to missing max_user_namespaces procfile

---
 testcases/cve/icmp_rate_limit01.c                   | 2 +-
 testcases/kernel/containers/userns/userns08.c       | 2 +-
 testcases/kernel/syscalls/bind/bind06.c             | 2 +-
 testcases/kernel/syscalls/sendto/sendto03.c         | 2 +-
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 2 +-
 testcases/kernel/syscalls/setsockopt/setsockopt06.c | 2 +-
 testcases/kernel/syscalls/setsockopt/setsockopt07.c | 2 +-
 testcases/kernel/syscalls/setsockopt/setsockopt08.c | 2 +-
 8 files changed, 8 insertions(+), 8 deletions(-)
testcases/cve/icmp_rate_limit01.c +1 -1
index 913fba3eb..b3a237b30 100644
@@ -58,7 +58,7 @@ static void setup(void)
 	for (i = 0; i < SRCADDR_COUNT; i++)
 		fds[i] = -1;
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
testcases/kernel/containers/userns/userns08.c +1 -1
index 775561081..c141b1aca 100644
@@ -121,7 +121,7 @@ static void setup(void)
 	SAFE_WRITE(fd, 1, "\n", 1);
 	SAFE_CLOSE(fd);
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 }
 
 static struct tst_test test = {
testcases/kernel/syscalls/bind/bind06.c +1 -1
index 4a85d5b0e..297311c04 100644
@@ -34,7 +34,7 @@ static void setup(void)
 	int real_gid = getgid();
 	struct ifreq ifr;
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
testcases/kernel/syscalls/sendto/sendto03.c +1 -1
index c99fb144f..217383993 100644
@@ -43,7 +43,7 @@ static void setup(void)
 	int real_gid = getgid();
 	struct ifreq ifr;
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -1
index a65ec6861..4b8b3d22e 100644
@@ -40,7 +40,7 @@ static void setup(void)
 	struct ifreq ifr;
 	socklen_t addrlen = sizeof(addr);
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
testcases/kernel/syscalls/setsockopt/setsockopt06.c +1 -1
index ee460cb18..12a80dee4 100644
@@ -35,7 +35,7 @@ static void setup(void)
 	int real_uid = getuid();
 	int real_gid = getgid();
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
testcases/kernel/syscalls/setsockopt/setsockopt07.c +1 -1
index 917cce0d2..d2c568e3e 100644
@@ -38,7 +38,7 @@ static void setup(void)
 	int real_uid = getuid();
 	int real_gid = getgid();
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
testcases/kernel/syscalls/setsockopt/setsockopt08.c +1 -1
index e8035edb0..5b648d754 100644
@@ -95,7 +95,7 @@ void setup(void)
 			"The vulnerability was only present in 32-bit compat mode");
 	}
 
-	SAFE_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
+	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
Signature · 5 lines
2.33.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 2/2
#2 lore raw

Re: [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile

Hi Martin,

Thanks!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp