summaryrefslogtreecommitdiffstats
path: root/src/test/test-fd-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2024-10-08 10:01:22 +0200
committerLennart Poettering <lennart@poettering.net>2024-10-08 13:13:49 +0200
commite7f905347526dd17543dd54561f56a047e6ff9f4 (patch)
tree61f574ac97abcbd16d2b0ebe1ac0968ba4531dd4 /src/test/test-fd-util.c
parentfd-util: introduce fd_validate() helper (diff)
downloadsystemd-e7f905347526dd17543dd54561f56a047e6ff9f4.tar.xz
systemd-e7f905347526dd17543dd54561f56a047e6ff9f4.zip
fd-util: use F_DUPFD_QUERY for same_fd()
Catch up with the nice little toys the kernel fs developers have added for us. Preferably, let's make use of the new F_DUPFD_QUERY fcntl() call that checks whether two fds are just duplicates of each other (duplicates as in dup(), not as in open() of the same inode, i.e. whether they share a single file offset and so on). This API is much nicer, since it is a core kernel feature, unlike the kcmp() call we so far used, which is part of the (optional) checkpoint/restore stuff. F_DUPFD_QUERY is available since kernel 6.10.
Diffstat (limited to 'src/test/test-fd-util.c')
-rw-r--r--src/test/test-fd-util.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c
index e49a5dde45..20cf7b7627 100644
--- a/src/test/test-fd-util.c
+++ b/src/test/test-fd-util.c
@@ -72,12 +72,14 @@ TEST(fd_validate) {
TEST(same_fd) {
_cleanup_close_pair_ int p[2];
- _cleanup_close_ int a, b, c;
+ _cleanup_close_ int a, b, c, d, e;
assert_se(pipe2(p, O_CLOEXEC) >= 0);
assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0);
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0);
+ assert_se((d = open("/dev/null", O_RDONLY|O_CLOEXEC|O_PATH)) >= 0); /* O_PATH changes error returns in F_DUPFD_QUERY, let's test explicitly */
+ assert_se((e = fcntl(d, F_DUPFD, 3)) >= 0);
assert_se(same_fd(p[0], p[0]) > 0);
assert_se(same_fd(p[1], p[1]) > 0);
@@ -102,6 +104,20 @@ TEST(same_fd) {
assert_se(same_fd(a, b) == 0);
assert_se(same_fd(b, a) == 0);
+
+ assert_se(same_fd(a, d) == 0);
+ assert_se(same_fd(d, a) == 0);
+ assert_se(same_fd(d, d) > 0);
+ assert_se(same_fd(d, e) > 0);
+ assert_se(same_fd(e, d) > 0);
+
+ /* Let's now compare with a valid fd nr, that is definitely closed, and verify it returns the right error code */
+ safe_close(d);
+ assert_se(same_fd(d, d) == -EBADF);
+ assert_se(same_fd(e, d) == -EBADF);
+ assert_se(same_fd(d, e) == -EBADF);
+ assert_se(same_fd(e, e) > 0);
+ TAKE_FD(d);
}
TEST(open_serialization_fd) {