kselftest: Add a ksft_perror() helper
authorMark Brown <broonie@kernel.org>
Thu, 28 Sep 2023 14:38:11 +0000 (16:38 +0200)
committerShuah Khan <skhan@linuxfoundation.org>
Thu, 5 Oct 2023 20:49:58 +0000 (14:49 -0600)
The standard library perror() function provides a convenient way to print
an error message based on the current errno but this doesn't play nicely
with KTAP output. Provide a helper which does an equivalent thing in a KTAP
compatible format.

nolibc doesn't have a strerror() and adding the table of strings required
doesn't seem like a good fit for what it's trying to do so when we're using
that only print the errno.

Signed-off-by: Mark Brown <broonie@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/kselftest.h

index 529d29a359002cf25bb169e75bd3d32452acf77f..af9f1202d4230ce79b6a1fde0897374e0d1713af 100644 (file)
@@ -48,6 +48,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <string.h>
 #include <stdio.h>
 #endif
 
@@ -155,6 +156,19 @@ static inline void ksft_print_msg(const char *msg, ...)
        va_end(args);
 }
 
+static inline void ksft_perror(const char *msg)
+{
+#ifndef NOLIBC
+       ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
+#else
+       /*
+        * nolibc doesn't provide strerror() and it seems
+        * inappropriate to add one, just print the errno.
+        */
+       ksft_print_msg("%s: %d)\n", msg, errno);
+#endif
+}
+
 static inline void ksft_test_result_pass(const char *msg, ...)
 {
        int saved_errno = errno;