]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-fd-util.c
tree-wide: introduce PIPE_EBADF macro
[thirdparty/systemd.git] / src / test / test-fd-util.c
index 0aa229fbc9a0e573a7c17d7b32187791d615df41..5068f8edf746c0cc9891e27409520828036a7b26 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <fcntl.h>
+#include <sys/eventfd.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
@@ -21,7 +22,7 @@
 #include "tests.h"
 #include "tmpfile-util.h"
 
-static void test_close_many(void) {
+TEST(close_many) {
         int fds[3];
         char name0[] = "/tmp/test-close-many.XXXXXX";
         char name1[] = "/tmp/test-close-many.XXXXXX";
@@ -44,7 +45,7 @@ static void test_close_many(void) {
         unlink(name2);
 }
 
-static void test_close_nointr(void) {
+TEST(close_nointr) {
         char name[] = "/tmp/test-test-close_nointr.XXXXXX";
         int fd;
 
@@ -56,9 +57,9 @@ static void test_close_nointr(void) {
         unlink(name);
 }
 
-static void test_same_fd(void) {
-        _cleanup_close_pair_ int p[2] = { -1, -1 };
-        _cleanup_close_ int a = -1, b = -1, c = -1;
+TEST(same_fd) {
+        _cleanup_close_pair_ int p[2];
+        _cleanup_close_ int a, b, c;
 
         assert_se(pipe2(p, O_CLOEXEC) >= 0);
         assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0);
@@ -90,8 +91,8 @@ static void test_same_fd(void) {
         assert_se(same_fd(b, a) == 0);
 }
 
-static void test_open_serialization_fd(void) {
-        _cleanup_close_ int fd = -1;
+TEST(open_serialization_fd) {
+        _cleanup_close_ int fd = -EBADF;
 
         fd = open_serialization_fd("test");
         assert_se(fd >= 0);
@@ -99,7 +100,7 @@ static void test_open_serialization_fd(void) {
         assert_se(write(fd, "test\n", 5) == 5);
 }
 
-static void test_fd_move_above_stdio(void) {
+TEST(fd_move_above_stdio) {
         int original_stdin, new_fd;
 
         original_stdin = fcntl(0, F_DUPFD, 3);
@@ -117,7 +118,7 @@ static void test_fd_move_above_stdio(void) {
         assert_se(close_nointr(new_fd) != EBADF);
 }
 
-static void test_rearrange_stdio(void) {
+TEST(rearrange_stdio) {
         pid_t pid;
         int r;
 
@@ -183,7 +184,7 @@ static void test_rearrange_stdio(void) {
         }
 }
 
-static void test_read_nr_open(void) {
+TEST(read_nr_open) {
         log_info("nr-open: %i", read_nr_open());
 }
 
@@ -291,7 +292,7 @@ static void test_close_all_fds_inner(void) {
 }
 
 static int seccomp_prohibit_close_range(void) {
-#if defined(HAVE_SECCOMP) && defined(__SNR_close_range)
+#if HAVE_SECCOMP && defined(__SNR_close_range)
         _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
         int r;
 
@@ -313,16 +314,16 @@ static int seccomp_prohibit_close_range(void) {
 
         return 0;
 #else
-        return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Seccomp support or close_range() syscall definition not availeble.");
+        return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Seccomp support or close_range() syscall definition not available.");
 #endif
 }
 
-static void test_close_all_fds(void) {
+TEST(close_all_fds) {
         int r;
 
         /* Runs the test four times. Once as is. Once with close_range() syscall blocked via seccomp, once
-         * with /proc overmounted, and once with the combination of both. This should trigger all fallbacks in
-         * the close_range_all() function. */
+         * with /proc/ overmounted, and once with the combination of both. This should trigger all fallbacks
+         * in the close_range_all() function. */
 
         r = safe_fork("(caf-plain)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
         if (r == 0) {
@@ -331,26 +332,22 @@ static void test_close_all_fds(void) {
         }
         assert_se(r >= 0);
 
-        if (geteuid() != 0) {
-                log_notice("Lacking privileges, skipping running tests with blocked close_range() and with /proc/ overnmounted.");
-                return;
-        }
+        if (geteuid() != 0)
+                return (void) log_tests_skipped("Lacking privileges for test with close_range() blocked and /proc/ overmounted");
 
         r = safe_fork("(caf-noproc)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
         if (r == 0) {
                 r = mount_nofollow_verbose(LOG_WARNING, "tmpfs", "/proc", "tmpfs", 0, NULL);
                 if (r < 0)
-                        log_notice("Overmounting /proc didn#t work, skipping close_all_fds() with masked /proc/.");
+                        log_notice("Overmounting /proc/ didn't work, skipping close_all_fds() with masked /proc/.");
                 else
                         test_close_all_fds_inner();
                 _exit(EXIT_SUCCESS);
         }
         assert_se(r >= 0);
 
-        if (!is_seccomp_available()) {
-                log_notice("Seccomp not available, skipping seccomp tests in %s", __func__);
-                return;
-        }
+        if (!is_seccomp_available())
+                return (void) log_tests_skipped("Seccomp not available");
 
         r = safe_fork("(caf-seccomp)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
         if (r == 0) {
@@ -372,7 +369,7 @@ static void test_close_all_fds(void) {
                 else {
                         r = mount_nofollow_verbose(LOG_WARNING, "tmpfs", "/proc", "tmpfs", 0, NULL);
                         if (r < 0)
-                                log_notice("Overmounting /proc didn#t work, skipping close_all_fds() with masked /proc/.");
+                                log_notice("Overmounting /proc/ didn't work, skipping close_all_fds() with masked /proc/.");
                         else
                                 test_close_all_fds_inner();
                 }
@@ -383,7 +380,7 @@ static void test_close_all_fds(void) {
         assert_se(r >= 0);
 }
 
-static void test_format_proc_fd_path(void) {
+TEST(format_proc_fd_path) {
         assert_se(streq_ptr(FORMAT_PROC_FD_PATH(0), "/proc/self/fd/0"));
         assert_se(streq_ptr(FORMAT_PROC_FD_PATH(1), "/proc/self/fd/1"));
         assert_se(streq_ptr(FORMAT_PROC_FD_PATH(2), "/proc/self/fd/2"));
@@ -391,8 +388,8 @@ static void test_format_proc_fd_path(void) {
         assert_se(streq_ptr(FORMAT_PROC_FD_PATH(2147483647), "/proc/self/fd/2147483647"));
 }
 
-static void test_fd_reopen(void) {
-        _cleanup_close_ int fd1 = -1, fd2 = -1;
+TEST(fd_reopen) {
+        _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF;
         struct stat st1, st2;
         int fl;
 
@@ -483,23 +480,93 @@ static void test_fd_reopen(void) {
         /* Also check the right error is generated if the fd is already closed */
         safe_close(fd1);
         assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC) == -EBADF);
-        fd1 = -1;
+        fd1 = -EBADF;
 }
 
-int main(int argc, char *argv[]) {
+TEST(fd_reopen_condition) {
+        _cleanup_close_ int fd1 = -EBADF, fd3 = -EBADF;
+        int fd2, fl;
 
-        test_setup_logging(LOG_DEBUG);
+        /* Open without O_PATH */
+        fd1 = open("/usr/", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+        assert_se(fd1 >= 0);
 
-        test_close_many();
-        test_close_nointr();
-        test_same_fd();
-        test_open_serialization_fd();
-        test_fd_move_above_stdio();
-        test_rearrange_stdio();
-        test_read_nr_open();
-        test_close_all_fds();
-        test_format_proc_fd_path();
-        test_fd_reopen();
+        fl = fcntl(fd1, F_GETFL);
+        assert_se(FLAGS_SET(fl, O_DIRECTORY));
+        assert_se(!FLAGS_SET(fl, O_PATH));
 
-        return 0;
+        fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3);
+        assert_se(fd2 == fd1);
+        assert_se(fd3 < 0);
+
+        /* Switch on O_PATH */
+        fd2 = fd_reopen_condition(fd1, O_DIRECTORY|O_PATH, O_DIRECTORY|O_PATH, &fd3);
+        assert_se(fd2 != fd1);
+        assert_se(fd3 == fd2);
+
+        fl = fcntl(fd2, F_GETFL);
+        assert_se(FLAGS_SET(fl, O_DIRECTORY));
+        assert_se(FLAGS_SET(fl, O_PATH));
+
+        close_and_replace(fd1, fd3);
+
+        fd2 = fd_reopen_condition(fd1, O_DIRECTORY|O_PATH, O_DIRECTORY|O_PATH, &fd3);
+        assert_se(fd2 == fd1);
+        assert_se(fd3 < 0);
+
+        /* Switch off O_PATH again */
+        fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3);
+        assert_se(fd2 != fd1);
+        assert_se(fd3 == fd2);
+
+        fl = fcntl(fd2, F_GETFL);
+        assert_se(FLAGS_SET(fl, O_DIRECTORY));
+        assert_se(!FLAGS_SET(fl, O_PATH));
+
+        close_and_replace(fd1, fd3);
+
+        fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3);
+        assert_se(fd2 == fd1);
+        assert_se(fd3 < 0);
 }
+
+TEST(take_fd) {
+        _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF;
+        int array[2] = PIPE_EBADF, i = 0;
+
+        assert_se(fd1 == -EBADF);
+        assert_se(fd2 == -EBADF);
+
+        fd1 = eventfd(0, EFD_CLOEXEC);
+        assert_se(fd1 >= 0);
+
+        fd2 = TAKE_FD(fd1);
+        assert_se(fd1 == -EBADF);
+        assert_se(fd2 >= 0);
+
+        assert_se(array[0] == -EBADF);
+        assert_se(array[1] == -EBADF);
+
+        array[0] = TAKE_FD(fd2);
+        assert_se(fd1 == -EBADF);
+        assert_se(fd2 == -EBADF);
+        assert_se(array[0] >= 0);
+        assert_se(array[1] == -EBADF);
+
+        array[1] = TAKE_FD(array[i]);
+        assert_se(array[0] == -EBADF);
+        assert_se(array[1] >= 0);
+
+        i = 1 - i;
+        array[0] = TAKE_FD(*(array + i));
+        assert_se(array[0] >= 0);
+        assert_se(array[1] == -EBADF);
+
+        i = 1 - i;
+        fd1 = TAKE_FD(array[i]);
+        assert_se(fd1 >= 0);
+        assert_se(array[0] == -EBADF);
+        assert_se(array[1] == -EBADF);
+}
+
+DEFINE_TEST_MAIN(LOG_DEBUG);