diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 50666e6375c..63c37fec4d8 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -187,7 +187,7 @@ _pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) { return false; } -static int get_max_fd(void) { +int get_max_fd(void) { struct rlimit rl; rlim_t m; diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index e929386b539..dd5207bd88a 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -57,6 +57,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL); int fd_nonblock(int fd, bool nonblock); int fd_cloexec(int fd, bool cloexec); +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); diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index 4c51592c263..6001f8e057a 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -215,18 +215,18 @@ static size_t validate_fds( static void test_close_all_fds(void) { _cleanup_free_ int *fds = NULL, *keep = NULL; - struct rlimit rl; size_t n_fds, n_keep; + int max_fd; log_info("/* %s */", __func__); rlimit_nofile_bump(-1); - assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0); - assert_se(rl.rlim_cur > 10); + max_fd = get_max_fd(); + assert_se(max_fd > 10); /* Try to use 5000 fds, but when we can't bump the rlimit to make that happen use the whole limit minus 10 */ - n_fds = MIN((rl.rlim_cur & ~1U) - 10U, 5000U); + n_fds = MIN(((size_t) max_fd & ~1U) - 10U, 5000U); assert_se((n_fds & 1U) == 0U); /* make sure even number of fds */ /* Allocate the determined number of fds, always two at a time */