2024-04-04 16:14:23

by Muhammad Usama Anjum

[permalink] [raw]
Subject: [PATCH 0/2] selftests: add ksft_exit_fail_perror()

In this series, ksft_exit_fail_perror() is being added which is helper
function on top of ksft_exit_fail_msg(). It prints errno and its string
form always. After writing and porting several kselftests, I've found
out that most of times ksft_exit_fail_msg() isn't useful if errno value
isn't printed. The ksft_exit_fail_perror() provides a convenient way to
always print errno when its used.

Muhammad Usama Anjum (2):
selftests: add ksft_exit_fail_perror()
selftests: exec: Use new ksft_exit_fail_perror() helper

tools/testing/selftests/exec/recursion-depth.c | 10 +++++-----
tools/testing/selftests/kselftest.h | 14 ++++++++++++++
2 files changed, 19 insertions(+), 5 deletions(-)

--
2.39.2



2024-04-04 16:14:40

by Muhammad Usama Anjum

[permalink] [raw]
Subject: [PATCH 1/2] selftests: add ksft_exit_fail_perror()

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.

Signed-off-by: Muhammad Usama Anjum <[email protected]>
---
tools/testing/selftests/kselftest.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index 159bf8e314fa3..2cd93d220f434 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -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
@@ -370,6 +371,19 @@ static inline __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 int ksft_exit_xfail(void)
{
ksft_print_cnts();
--
2.39.2


2024-04-04 16:14:56

by Muhammad Usama Anjum

[permalink] [raw]
Subject: [PATCH 2/2] selftests: exec: Use new ksft_exit_fail_perror() helper

Use ksft_exit_fail_perror() to print the value of errno and its string
form. This is the first user of the ksft_exit_fail_perror() and proves
the usefulness of this API.

Signed-off-by: Muhammad Usama Anjum <[email protected]>
---
tools/testing/selftests/exec/recursion-depth.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/exec/recursion-depth.c b/tools/testing/selftests/exec/recursion-depth.c
index b2f37d86a5f62..438c8ff2fd260 100644
--- a/tools/testing/selftests/exec/recursion-depth.c
+++ b/tools/testing/selftests/exec/recursion-depth.c
@@ -37,25 +37,25 @@ int main(void)
ksft_test_result_skip("error: unshare, errno %d\n", errno);
ksft_finished();
}
- ksft_exit_fail_msg("error: unshare, errno %d\n", errno);
+ ksft_exit_fail_perror("error: unshare");
}

if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1)
- ksft_exit_fail_msg("error: mount '/', errno %d\n", errno);
+ ksft_exit_fail_perror("error: mount '/'");

/* Require "exec" filesystem. */
if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1)
- ksft_exit_fail_msg("error: mount ramfs, errno %d\n", errno);
+ ksft_exit_fail_perror("error: mount ramfs");

#define FILENAME "/tmp/1"

fd = creat(FILENAME, 0700);
if (fd == -1)
- ksft_exit_fail_msg("error: creat, errno %d\n", errno);
+ ksft_exit_fail_perror("error: creat");

#define S "#!" FILENAME "\n"
if (write(fd, S, strlen(S)) != strlen(S))
- ksft_exit_fail_msg("error: write, errno %d\n", errno);
+ ksft_exit_fail_perror("error: write");

close(fd);

--
2.39.2


2024-04-04 17:48:36

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH 0/2] selftests: add ksft_exit_fail_perror()

On 4/4/24 10:14, Muhammad Usama Anjum wrote:
> In this series, ksft_exit_fail_perror() is being added which is helper
> function on top of ksft_exit_fail_msg(). It prints errno and its string
> form always. After writing and porting several kselftests, I've found
> out that most of times ksft_exit_fail_msg() isn't useful if errno value
> isn't printed. The ksft_exit_fail_perror() provides a convenient way to
> always print errno when its used.
>
> Muhammad Usama Anjum (2):
> selftests: add ksft_exit_fail_perror()> selftests: exec: Use new ksft_exit_fail_perror() helper
>
> tools/testing/selftests/exec/recursion-depth.c | 10 +++++-----
> tools/testing/selftests/kselftest.h | 14 ++++++++++++++
> 2 files changed, 19 insertions(+), 5 deletions(-)
>

Applied the two patches to linux-kselftest next for Linux 6.10-rc1.

thanks,
-- Shuah