Skip to content

Commit 58beae2

Browse files
cvam0000shuahkh
authored andcommitted
selftests: tmpfs: Add kselftest support to tmpfs
Replace direct error handling with 'ksft_test_result_*' macros for better reporting. Test logs: Before change: - Without root error: unshare, errno 1 - With root No, output After change: - Without root TAP version 13 1..1 ok 2 # SKIP This test needs root to run! Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0 - With root TAP version 13 1..1 ok 1 Test : Success Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 Link: https://lore.kernel.org/r/20250105085255.124929-3-cvam0000@gmail.com Signed-off-by: Shivam Chaudhary <cvam0000@gmail.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 41ca14e commit 58beae2

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,38 @@ int main(void)
4141

4242
if (unshare(CLONE_NEWNS) == -1) {
4343
if (errno == ENOSYS || errno == EPERM) {
44-
fprintf(stderr, "error: unshare, errno %d\n", errno);
45-
return 4;
44+
ksft_exit_skip("unshare() error: unshare, errno %d\n", errno);
45+
} else {
46+
ksft_exit_fail_msg("unshare() error: unshare, errno %d\n", errno);
4647
}
47-
fprintf(stderr, "error: unshare, errno %d\n", errno);
48-
return 1;
4948
}
49+
5050
if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
51-
fprintf(stderr, "error: mount '/', errno %d\n", errno);
52-
return 1;
51+
ksft_exit_fail_msg("mount() error: Root filesystem private mount: Fail %d\n", errno);
5352
}
5453

5554
/* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
5655
if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
57-
fprintf(stderr, "error: mount tmpfs, errno %d\n", errno);
58-
return 1;
56+
ksft_exit_fail_msg("mount() error: Mounting tmpfs on /tmp: Fail %d\n", errno);
5957
}
6058

6159
fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
6260
if (fd == -1) {
63-
fprintf(stderr, "error: open 1, errno %d\n", errno);
64-
return 1;
61+
ksft_exit_fail_msg("openat() error: Open first temporary file: Fail %d\n", errno);
6562
}
63+
6664
if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
67-
fprintf(stderr, "error: linkat, errno %d\n", errno);
68-
return 1;
65+
ksft_exit_fail_msg("linkat() error: Linking the temporary file: Fail %d\n", errno);
66+
/* Ensure fd is closed on failure */
67+
close(fd);
6968
}
7069
close(fd);
7170

7271
fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
7372
if (fd == -1) {
74-
fprintf(stderr, "error: open 2, errno %d\n", errno);
75-
return 1;
73+
ksft_exit_fail_msg("openat() error: Opening the second temporary file: Fail %d\n", errno);
7674
}
77-
75+
ksft_test_result_pass(" ");
76+
ksft_exit_pass();
7877
return 0;
7978
}

0 commit comments

Comments
 (0)