Skip to content

Commit 73fc0cb

Browse files
committed
fd-util: export get_max_fd() so that we can use it in tests
1 parent ab27b2f commit 73fc0cb

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/basic/fd-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ _pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
187187
return false;
188188
}
189189

190-
static int get_max_fd(void) {
190+
int get_max_fd(void) {
191191
struct rlimit rl;
192192
rlim_t m;
193193

src/basic/fd-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
5757
int fd_nonblock(int fd, bool nonblock);
5858
int fd_cloexec(int fd, bool cloexec);
5959

60+
int get_max_fd(void);
61+
6062
int close_all_fds(const int except[], size_t n_except);
6163
int close_all_fds_without_malloc(const int except[], size_t n_except);
6264

src/test/test-fd-util.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,18 @@ static size_t validate_fds(
215215

216216
static void test_close_all_fds(void) {
217217
_cleanup_free_ int *fds = NULL, *keep = NULL;
218-
struct rlimit rl;
219218
size_t n_fds, n_keep;
219+
int max_fd;
220220

221221
log_info("/* %s */", __func__);
222222

223223
rlimit_nofile_bump(-1);
224224

225-
assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0);
226-
assert_se(rl.rlim_cur > 10);
225+
max_fd = get_max_fd();
226+
assert_se(max_fd > 10);
227227

228228
/* Try to use 5000 fds, but when we can't bump the rlimit to make that happen use the whole limit minus 10 */
229-
n_fds = MIN((rl.rlim_cur & ~1U) - 10U, 5000U);
229+
n_fds = MIN(((size_t) max_fd & ~1U) - 10U, 5000U);
230230
assert_se((n_fds & 1U) == 0U); /* make sure even number of fds */
231231

232232
/* Allocate the determined number of fds, always two at a time */

0 commit comments

Comments
 (0)