]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: drop close_all_fds_by_proc()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 Jan 2026 04:24:42 +0000 (13:24 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 Jan 2026 01:21:36 +0000 (10:21 +0900)
With the previous commit, now the function is used only on OOM.
In that case, let's do like close_all_fds_without_malloc().

src/basic/fd-util.c
src/basic/fd-util.h
src/test/test-fd-util.c

index 79903370856bb5939c2c63581e6e79b0da008c3d..e08ad7d32426adeb121f06182973414ea1f7270b 100644 (file)
@@ -251,10 +251,9 @@ int close_all_fds_frugal(const int except[], size_t n_except) {
 
         assert(except || n_except == 0);
 
-        /* This is the inner fallback core of close_all_fds(). This never calls malloc() or opendir() or so
-         * and hence is safe to be called in signal handler context. Most users should call close_all_fds(),
-         * but when we assume we are called from signal handler context, then use this simpler call
-         * instead. */
+        /* This is the inner fallback core of close_all_fds(). This never calls malloc() or so and hence is
+         * safe to be called in signal handler context. Most users should call close_all_fds(), but when we
+         * assume we are called from signal handler context, then use this simpler call instead. */
 
         max_fd = get_max_fd();
         if (max_fd < 0)
@@ -280,42 +279,6 @@ int close_all_fds_frugal(const int except[], size_t n_except) {
         return r;
 }
 
-int close_all_fds_by_proc(const int except[], size_t n_except) {
-        _cleanup_closedir_ DIR *d = NULL;
-        int r = 0;
-
-        d = opendir("/proc/self/fd");
-        if (!d)
-                return close_all_fds_frugal(except, n_except); /* ultimate fallback if /proc/ is not available */
-
-        FOREACH_DIRENT(de, d, return -errno) {
-                int fd = -EBADF, q;
-
-                if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
-                        continue;
-
-                fd = parse_fd(de->d_name);
-                if (fd < 0)
-                        /* Let's better ignore this, just in case */
-                        continue;
-
-                if (fd < 3)
-                        continue;
-
-                if (fd == dirfd(d))
-                        continue;
-
-                if (fd_in_set(fd, except, n_except))
-                        continue;
-
-                q = close_nointr(fd);
-                if (q != -EBADF) /* Valgrind has its own FD and doesn't want to have it closed */
-                        RET_GATHER(r, q);
-        }
-
-        return r;
-}
-
 static int close_all_fds_special_case(const int except[], size_t n_except) {
         assert(n_except == 0 || except);
 
@@ -397,7 +360,7 @@ int close_all_fds(const int except[], size_t n_except) {
                 sorted = newa(int, n_sorted);
 
         if (!sorted) /* Fallback on OOM. */
-                return close_all_fds_by_proc(except, n_except);
+                return close_all_fds_frugal(except, n_except);
 
         memcpy(sorted, except, n_except * sizeof(int));
 
index db4b591438014907ad6dff65fafb36ee1b48c5b0..bb9669a84ee8f47cae919782d3e0a11038e6ed40 100644 (file)
@@ -112,7 +112,6 @@ int get_max_fd(void);
 
 int close_all_fds(const int except[], size_t n_except);
 int close_all_fds_without_malloc(const int except[], size_t n_except);
-int close_all_fds_by_proc(const int except[], size_t n_except);
 int close_all_fds_frugal(const int except[], size_t n_except);
 
 int pack_fds(int fds[], size_t n);
index 5a8b58440b846bdb175b0718fa9b1a6df1e5f240..d43c8735164d50db21628dcc3d558a871fa09649 100644 (file)
@@ -343,19 +343,13 @@ TEST(close_all_fds) {
                 _exit(EXIT_SUCCESS);
         }
 
-        ASSERT_OK(r = pidref_safe_fork("(caf-nomalloc)", flags, NULL));
+        r = ASSERT_OK(pidref_safe_fork("(caf-nomalloc)", flags, NULL));
         if (r == 0) {
                 test_close_all_fds_inner(close_all_fds_without_malloc);
                 _exit(EXIT_SUCCESS);
         }
 
-        ASSERT_OK(r = pidref_safe_fork("(caf-proc)", flags, NULL));
-        if (r == 0) {
-                test_close_all_fds_inner(close_all_fds_by_proc);
-                _exit(EXIT_SUCCESS);
-        }
-
-        ASSERT_OK(r = pidref_safe_fork("(caf-frugal)", flags, NULL));
+        r = ASSERT_OK(pidref_safe_fork("(caf-frugal)", flags, NULL));
         if (r == 0) {
                 test_close_all_fds_inner(close_all_fds_frugal);
                 _exit(EXIT_SUCCESS);