selftests: add ksft_exit_fail_perror()
authorMuhammad Usama Anjum <usama.anjum@collabora.com>
Thu, 4 Apr 2024 16:14:32 +0000 (21:14 +0500)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 6 May 2024 19:57:19 +0000 (13:57 -0600)
Add a version of ksft_exit_fail_msg() which prints the errno and its
string form with ease. There is no benefit of exit message without
errno. Whenever some error occurs, instead of printing errno manually,
this function would be very helpful. In the next TAP ports or new tests,
this function will be used instead of ksft_exit_fail_msg() as it prints
errno.

Resolved merge conflict found in next between the following commits:
f7d5bcd35d42 ("selftests: kselftest: Mark functions that unconditionally call exit() as __noreturn")

f07041728422 ("selftests: add ksft_exit_fail_perror()")

Reported-by: Stephen Rothwell <sfr@canb.auug.org.au>
Shuah Khan <skhan@linuxfoundation.org>

Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/kselftest.h

index 8e9e4b86645a8223e6806dac2ee0751547263054..0a983b27771e1722cdbdf34cff56b7e9c846f3a4 100644 (file)
@@ -41,6 +41,7 @@
  * the program is aborting before finishing all tests):
  *
  *    ksft_exit_fail_msg(fmt, ...);
+ *    ksft_exit_fail_perror(msg);
  *
  */
 #ifndef __KSELFTEST_H
@@ -376,6 +377,19 @@ static inline __noreturn __printf(1, 2) int ksft_exit_fail_msg(const char *msg,
        exit(KSFT_FAIL);
 }
 
+static inline void ksft_exit_fail_perror(const char *msg)
+{
+#ifndef NOLIBC
+       ksft_exit_fail_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_exit_fail_msg("%s: %d)\n", msg, errno);
+#endif
+}
+
 static inline __noreturn int ksft_exit_xfail(void)
 {
        ksft_print_cnts();