From: Yu Watanabe Date: Mon, 5 Jan 2026 04:24:42 +0000 (+0900) Subject: fd-util: drop close_all_fds_by_proc() X-Git-Tag: v260-rc1~408^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d58c826c541598060002cfeb608f35ccd2a229e3;p=thirdparty%2Fsystemd.git fd-util: drop close_all_fds_by_proc() With the previous commit, now the function is used only on OOM. In that case, let's do like close_all_fds_without_malloc(). --- diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 79903370856..e08ad7d3242 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -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)); diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index db4b5914380..bb9669a84ee 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -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); diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index 5a8b58440b8..d43c8735164 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -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);