From: Willy Tarreau Date: Thu, 22 Sep 2022 14:12:08 +0000 (+0200) Subject: MINOR: init: do not try to shrink existing RLIMIT_NOFIlE X-Git-Tag: v2.7-dev8~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c06557c23b0e4e932405fe3f0739c303ac90926e;p=thirdparty%2Fhaproxy.git MINOR: init: do not try to shrink existing RLIMIT_NOFIlE As seen in issue #1866, some environments will not allow to change the current FD limit, and actually we don't need to do it, we only do it as a byproduct of adjusting the limit to the one that fits. Here we're replacing calls to setrlimit() with calls to raise_rlim_nofile(), which will avoid making the setrlimit() syscall in case the desired value is lower than the current process' one. This depends on previous commit "MINOR: fd: add a new function to only raise RLIMIT_NOFILE" and may need to be backported to 2.6, possibly earlier, depending on users' experience in such environments. --- diff --git a/src/extcheck.c b/src/extcheck.c index 6b5b2b6852..2093b40514 100644 --- a/src/extcheck.c +++ b/src/extcheck.c @@ -419,7 +419,7 @@ static int connect_proc_chk(struct task *t) /* restore the initial FD limits */ limit.rlim_cur = rlim_fd_cur_at_boot; limit.rlim_max = rlim_fd_max_at_boot; - if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { + if (raise_rlim_nofile(NULL, &limit) != 0) { getrlimit(RLIMIT_NOFILE, &limit); ha_warning("External check: failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", rlim_fd_cur_at_boot, rlim_fd_max_at_boot, diff --git a/src/haproxy.c b/src/haproxy.c index 6aff7d63ee..a95fc21e45 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -709,8 +709,7 @@ static void mworker_reexec() /* restore the initial FD limits */ limit.rlim_cur = rlim_fd_cur_at_boot; limit.rlim_max = rlim_fd_max_at_boot; - if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { - getrlimit(RLIMIT_NOFILE, &limit); + if (raise_rlim_nofile(&limit, &limit) != 0) { ha_warning("Failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", rlim_fd_cur_at_boot, rlim_fd_max_at_boot, (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max); @@ -1452,14 +1451,14 @@ static int check_if_maxsock_permitted(int maxsock) return 1; /* don't go further if we can't even set to what we have */ - if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0) + if (raise_rlim_nofile(NULL, &orig_limit) != 0) return 1; test_limit.rlim_max = MAX(maxsock, orig_limit.rlim_max); test_limit.rlim_cur = test_limit.rlim_max; - ret = setrlimit(RLIMIT_NOFILE, &test_limit); + ret = raise_rlim_nofile(NULL, &test_limit); - if (setrlimit(RLIMIT_NOFILE, &orig_limit) != 0) + if (raise_rlim_nofile(NULL, &orig_limit) != 0) return 1; return ret == 0; @@ -3180,7 +3179,7 @@ int main(int argc, char **argv) limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur); if ((global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) || - setrlimit(RLIMIT_NOFILE, &limit) == -1) { + raise_rlim_nofile(NULL, &limit) != 0) { getrlimit(RLIMIT_NOFILE, &limit); if (global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) limit.rlim_cur = global.fd_hard_limit; @@ -3196,7 +3195,7 @@ int main(int argc, char **argv) if (global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) limit.rlim_cur = global.fd_hard_limit; - if (setrlimit(RLIMIT_NOFILE, &limit) != -1) + if (raise_rlim_nofile(&limit, &limit) == 0) getrlimit(RLIMIT_NOFILE, &limit); ha_warning("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n",