* [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function
@ 2021-09-21 16:20 Martin Doucha
2021-09-21 16:20 ` [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile Martin Doucha
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Martin Doucha @ 2021-09-21 16:20 UTC (permalink / raw)
To: ltp
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.
Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
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(-)
diff --git a/include/safe_file_ops_fn.h b/include/safe_file_ops_fn.h
index 6d680967b..e8ed85382 100644
--- a/include/safe_file_ops_fn.h
+++ b/include/safe_file_ops_fn.h
@@ -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.
*/
diff --git a/include/tst_safe_file_ops.h b/include/tst_safe_file_ops.h
index 223eddd1f..62f6600ec 100644
--- a/include/tst_safe_file_ops.h
+++ b/include/tst_safe_file_ops.h
@@ -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))
diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index 249a512a1..f803691d8 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -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)
--
2.33.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile
2021-09-21 16:20 [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Martin Doucha
@ 2021-09-21 16:20 ` Martin Doucha
2021-09-22 9:40 ` Petr Vorel
2021-09-22 9:39 ` [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Petr Vorel
2021-09-23 9:58 ` Cyril Hrubis
2 siblings, 1 reply; 5+ messages in thread
From: Martin Doucha @ 2021-09-21 16:20 UTC (permalink / raw)
To: ltp
Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
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(-)
diff --git a/testcases/cve/icmp_rate_limit01.c b/testcases/cve/icmp_rate_limit01.c
index 913fba3eb..b3a237b30 100644
--- a/testcases/cve/icmp_rate_limit01.c
+++ b/testcases/cve/icmp_rate_limit01.c
@@ -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);
diff --git a/testcases/kernel/containers/userns/userns08.c b/testcases/kernel/containers/userns/userns08.c
index 775561081..c141b1aca 100644
--- a/testcases/kernel/containers/userns/userns08.c
+++ b/testcases/kernel/containers/userns/userns08.c
@@ -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 = {
diff --git a/testcases/kernel/syscalls/bind/bind06.c b/testcases/kernel/syscalls/bind/bind06.c
index 4a85d5b0e..297311c04 100644
--- a/testcases/kernel/syscalls/bind/bind06.c
+++ b/testcases/kernel/syscalls/bind/bind06.c
@@ -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);
diff --git a/testcases/kernel/syscalls/sendto/sendto03.c b/testcases/kernel/syscalls/sendto/sendto03.c
index c99fb144f..217383993 100644
--- a/testcases/kernel/syscalls/sendto/sendto03.c
+++ b/testcases/kernel/syscalls/sendto/sendto03.c
@@ -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);
diff --git a/testcases/kernel/syscalls/setsockopt/setsockopt05.c b/testcases/kernel/syscalls/setsockopt/setsockopt05.c
index a65ec6861..4b8b3d22e 100644
--- a/testcases/kernel/syscalls/setsockopt/setsockopt05.c
+++ b/testcases/kernel/syscalls/setsockopt/setsockopt05.c
@@ -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);
diff --git a/testcases/kernel/syscalls/setsockopt/setsockopt06.c b/testcases/kernel/syscalls/setsockopt/setsockopt06.c
index ee460cb18..12a80dee4 100644
--- a/testcases/kernel/syscalls/setsockopt/setsockopt06.c
+++ b/testcases/kernel/syscalls/setsockopt/setsockopt06.c
@@ -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);
diff --git a/testcases/kernel/syscalls/setsockopt/setsockopt07.c b/testcases/kernel/syscalls/setsockopt/setsockopt07.c
index 917cce0d2..d2c568e3e 100644
--- a/testcases/kernel/syscalls/setsockopt/setsockopt07.c
+++ b/testcases/kernel/syscalls/setsockopt/setsockopt07.c
@@ -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);
diff --git a/testcases/kernel/syscalls/setsockopt/setsockopt08.c b/testcases/kernel/syscalls/setsockopt/setsockopt08.c
index e8035edb0..5b648d754 100644
--- a/testcases/kernel/syscalls/setsockopt/setsockopt08.c
+++ b/testcases/kernel/syscalls/setsockopt/setsockopt08.c
@@ -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);
--
2.33.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function
2021-09-21 16:20 [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Martin Doucha
2021-09-21 16:20 ` [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile Martin Doucha
@ 2021-09-22 9:39 ` Petr Vorel
2021-09-23 9:58 ` Cyril Hrubis
2 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2021-09-22 9:39 UTC (permalink / raw)
To: Martin Doucha; +Cc: ltp
Hi Martin,
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Thanks!
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile
2021-09-21 16:20 ` [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile Martin Doucha
@ 2021-09-22 9:40 ` Petr Vorel
0 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2021-09-22 9:40 UTC (permalink / raw)
To: Martin Doucha; +Cc: ltp
Hi Martin,
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Thanks!
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function
2021-09-21 16:20 [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Martin Doucha
2021-09-21 16:20 ` [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile Martin Doucha
2021-09-22 9:39 ` [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Petr Vorel
@ 2021-09-23 9:58 ` Cyril Hrubis
2 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2021-09-23 9:58 UTC (permalink / raw)
To: Martin Doucha; +Cc: ltp
Hi!
Both pushed, thanks.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-09-23 9:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-21 16:20 [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Martin Doucha
2021-09-21 16:20 ` [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile Martin Doucha
2021-09-22 9:40 ` Petr Vorel
2021-09-22 9:39 ` [LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function Petr Vorel
2021-09-23 9:58 ` Cyril Hrubis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.