Add SAFE_TRY_FILE_PRINTF() helper function
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.
Signed-off-by Martin Doucha <[email protected]>
--- 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
@@ -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
@@ -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
@@ -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
PATCH 2/2 Fix failures due to missing max_user_namespaces procfile
Signed-off-by Martin Doucha <[email protected]>
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [LTP] [PATCH 2/2] Fix failures due to missing max_user_namespaces procfile
Hi Martin,
Reviewed-by Petr Vorel <[email protected]>
Thanks!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Martin,
Reviewed-by Petr Vorel <[email protected]>
Thanks!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Both pushed, thanks.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp