.../selftests/tmpfs/bug-link-o-tmpfile.c | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-)
Add kselftest support for openat, linkat, unshare, mount tests
- Replace direct error handling with
'ksft_test_result_*' , 'ksft_print_msg' macros for better
reporting.
- Add `ksft_print_header()` and `ksft_set_plan()`
to structure test outputs more effectively.
- Improve the test flow by adding more detailed pass/fail
reporting for unshare, mounting, file opening, and linking
operations.
- Skip the test if it's not run as root, providing an
appropriate Warning.
Test logs:
Before change:
- Without root
error: unshare, errno 1
- With root
No, output
After change:
- Without root
TAP version 13
1..1
ok 1 # SKIP This test needs root to run
- With root
TAP version 13
1..1
unshare(): Creat new mount namespace: Success.
mount(): Root filesystem private mount: Success
mount(): Mounting tmpfs on /tmp: Success
openat(): Open first temporary file: Success
linkat(): Linking the temporary file: Success
openat(): Opening the second temporary file: Success
ok 1 Test : Success
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
Signed-off-by: Shivam Chaudhary <cvam0000@gmail.com>
---
Notes:
Changes in v3:
- Remove extra ksft_set_plan()
- Remove function for unshare()
- Fix the comment style
link to v2: https://lore.kernel.org/all/20241026191621.2860376-1-cvam0000@gmail.com/
Changes in v2:
- Make the commit message more clear.
link to v1: https://lore.kernel.org/all/20241024200228.1075840-1-cvam0000@gmail.com/T/#u
.../selftests/tmpfs/bug-link-o-tmpfile.c | 69 +++++++++++++++----
1 file changed, 57 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
index b5c3ddb90942..9ca1245784d9 100644
--- a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
+++ b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
@@ -23,45 +23,90 @@
#include <sys/mount.h>
#include <unistd.h>
+#include "../kselftest.h"
+
+
int main(void)
{
int fd;
+ /* Setting up kselftest framework */
+ ksft_print_header();
+ ksft_set_plan(1);
+
+ /* Check if test is run as root */
+ if (geteuid()) {
+ ksft_test_result_skip("This test needs root to run!\n");
+ return 1;
+ }
+
+
if (unshare(CLONE_NEWNS) == -1) {
if (errno == ENOSYS || errno == EPERM) {
- fprintf(stderr, "error: unshare, errno %d\n", errno);
- return 4;
+ ksft_test_result_fail("unshare() error: unshare, errno %d\n", errno);
+ return 1;
}
- fprintf(stderr, "error: unshare, errno %d\n", errno);
- return 1;
+ else{
+ fprintf(stderr, "unshare() error: unshare, errno %d\n", errno);
+ ksft_test_result_fail("unshare() error: unshare, errno %d\n", errno);
+ return 1;
+
+ }
+ }
+
+ else {
+ ksft_print_msg("unshare(): Creat new mount namespace: Success.\n");
+
}
- if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
- fprintf(stderr, "error: mount '/', errno %d\n", errno);
+
+
+
+ if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1) {
+ ksft_test_result_fail("mount() error: Root filesystem private mount: Fail %d\n", errno);
return 1;
+ } else {
+ ksft_print_msg("mount(): Root filesystem private mount: Success\n");
}
+
/* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
- fprintf(stderr, "error: mount tmpfs, errno %d\n", errno);
+ ksft_test_result_fail("mount() error: Mounting tmpfs on /tmp: Fail %d\n", errno);
return 1;
+ } else {
+ ksft_print_msg("mount(): Mounting tmpfs on /tmp: Success\n");
}
- fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
+
+ fd = openat(AT_FDCWD, "/tmp", O_WRONLY | O_TMPFILE, 0600);
if (fd == -1) {
- fprintf(stderr, "error: open 1, errno %d\n", errno);
+ ksft_test_result_fail("openat() error: Open first temporary file: Fail %d\n", errno);
return 1;
+ } else {
+ ksft_print_msg("openat(): Open first temporary file: Success\n");
}
+
+
if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
- fprintf(stderr, "error: linkat, errno %d\n", errno);
+ ksft_test_result_fail("linkat() error: Linking the temporary file: Fail %d\n", errno);
+ /* Ensure fd is closed on failure */
+ close(fd);
return 1;
+ } else {
+ ksft_print_msg("linkat(): Linking the temporary file: Success\n");
}
close(fd);
- fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
+
+ fd = openat(AT_FDCWD, "/tmp", O_WRONLY | O_TMPFILE, 0600);
if (fd == -1) {
- fprintf(stderr, "error: open 2, errno %d\n", errno);
+ ksft_test_result_fail("openat() error: Opening the second temporary file: Fail %d\n", errno);
return 1;
+ } else {
+ ksft_print_msg("openat(): Opening the second temporary file: Success\n");
}
+ ksft_test_result_pass("Test : Success\n");
+ ksft_exit_pass();
return 0;
}
--
2.45.2
On 10/28/24 12:57, Shivam Chaudhary wrote:
> Add kselftest support for openat, linkat, unshare, mount tests
You are combining a few too many changes in this patch. As such
this patch doesn't add support for the above tests. Instead it
changes the reporting to use ksft_* framework.
>
> - Replace direct error handling with
> 'ksft_test_result_*' , 'ksft_print_msg' macros for better
> reporting.
>
That
> - Add `ksft_print_header()` and `ksft_set_plan()`
> to structure test outputs more effectively.
>
> - Improve the test flow by adding more detailed pass/fail
> reporting for unshare, mounting, file opening, and linking
> operations.
>
> - Skip the test if it's not run as root, providing an
> appropriate Warning.
Make this a separate patch and the very first patch in the series.
>
> Test logs:
>
> Before change:
>
> - Without root
> error: unshare, errno 1
>
> - With root
> No, output
>
> After change:
>
> - Without root
> TAP version 13
> 1..1
> ok 1 # SKIP This test needs root to run
>
> - With root
> TAP version 13
> 1..1
> unshare(): Creat new mount namespace: Success.
> mount(): Root filesystem private mount: Success
> mount(): Mounting tmpfs on /tmp: Success
> openat(): Open first temporary file: Success
> linkat(): Linking the temporary file: Success
> openat(): Opening the second temporary file: Success
> ok 1 Test : Success
> Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
>
> Signed-off-by: Shivam Chaudhary <cvam0000@gmail.com>
> ---
>
> Notes:
> Changes in v3:
> - Remove extra ksft_set_plan()
> - Remove function for unshare()
> - Fix the comment style
>
> link to v2: https://lore.kernel.org/all/20241026191621.2860376-1-cvam0000@gmail.com/
>
> Changes in v2:
> - Make the commit message more clear.
>
> link to v1: https://lore.kernel.org/all/20241024200228.1075840-1-cvam0000@gmail.com/T/#u
>
> .../selftests/tmpfs/bug-link-o-tmpfile.c | 69 +++++++++++++++----
> 1 file changed, 57 insertions(+), 12 deletions(-)
>
> diff --git a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
> index b5c3ddb90942..9ca1245784d9 100644
> --- a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
> +++ b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
> @@ -23,45 +23,90 @@
> #include <sys/mount.h>
> #include <unistd.h>
>
> +#include "../kselftest.h"
> +
> +
> int main(void)
> {
> int fd;
>
> + /* Setting up kselftest framework */
> + ksft_print_header();
> + ksft_set_plan(1);
> +
> + /* Check if test is run as root */
> + if (geteuid()) {
> + ksft_test_result_skip("This test needs root to run!\n");
> + return 1;
This should be SKIP - simply use KSFT_SKIP
> + }
> +
> +
> if (unshare(CLONE_NEWNS) == -1) {
> if (errno == ENOSYS || errno == EPERM) {
> - fprintf(stderr, "error: unshare, errno %d\n", errno);
> - return 4;
> + ksft_test_result_fail("unshare() error: unshare, errno %d\n", errno);
This isn't a failure - it just means that unsaher() might not be supported.
Since root check is already done, EPERM won't be returned here.
> + return 1;
Okay this wrong. This changes the return value fro 4 to 1 - SKIP becomes fail.
Use KSFT_SKIP
> }
> - fprintf(stderr, "error: unshare, errno %d\n", errno);
> - return 1;
> + else{
> + fprintf(stderr, "unshare() error: unshare, errno %d\n", errno);
> + ksft_test_result_fail("unshare() error: unshare, errno %d\n", errno);
> + return 1;
> +
> + }
> + }
> +
> + else {
> + ksft_print_msg("unshare(): Creat new mount namespace: Success.\n");
> +
> }
> - if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
> - fprintf(stderr, "error: mount '/', errno %d\n", errno);
> +
> +
> +
> + if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1) {
> + ksft_test_result_fail("mount() error: Root filesystem private mount: Fail %d\n", errno);
> return 1;
> + } else {
> + ksft_print_msg("mount(): Root filesystem private mount: Success\n");
> }
>
> +
> /* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
> if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
> - fprintf(stderr, "error: mount tmpfs, errno %d\n", errno);
> + ksft_test_result_fail("mount() error: Mounting tmpfs on /tmp: Fail %d\n", errno);
> return 1;
> + } else {
> + ksft_print_msg("mount(): Mounting tmpfs on /tmp: Success\n");
> }
>
> - fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
> +
> + fd = openat(AT_FDCWD, "/tmp", O_WRONLY | O_TMPFILE, 0600);
> if (fd == -1) {
> - fprintf(stderr, "error: open 1, errno %d\n", errno);
> + ksft_test_result_fail("openat() error: Open first temporary file: Fail %d\n", errno);
> return 1;
> + } else {
> + ksft_print_msg("openat(): Open first temporary file: Success\n");
> }
> +
> +
> if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
> - fprintf(stderr, "error: linkat, errno %d\n", errno);
> + ksft_test_result_fail("linkat() error: Linking the temporary file: Fail %d\n", errno);
> + /* Ensure fd is closed on failure */
> + close(fd);
> return 1;
> + } else {
> + ksft_print_msg("linkat(): Linking the temporary file: Success\n");
> }
> close(fd);
>
> - fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
> +
> + fd = openat(AT_FDCWD, "/tmp", O_WRONLY | O_TMPFILE, 0600);
> if (fd == -1) {
> - fprintf(stderr, "error: open 2, errno %d\n", errno);
> + ksft_test_result_fail("openat() error: Opening the second temporary file: Fail %d\n", errno);
> return 1;
> + } else {
> + ksft_print_msg("openat(): Opening the second temporary file: Success\n");
> }
>
> + ksft_test_result_pass("Test : Success\n");
> + ksft_exit_pass();
> return 0;
> }
Fix these and split the patches. It makes it easier to review.
thanks,
-- Shuah
© 2016 - 2026 Red Hat, Inc.