]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: init: do not try to shrink existing RLIMIT_NOFIlE
authorWilly Tarreau <w@1wt.eu>
Thu, 22 Sep 2022 14:12:08 +0000 (16:12 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 4 Oct 2022 06:38:47 +0000 (08:38 +0200)
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.

src/extcheck.c
src/haproxy.c

index 6b5b2b68520dfb2c69cad28dfdd1e286265efb9e..2093b405143448d038112aa6729e9fc5ce5deb7e 100644 (file)
@@ -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,
index 6aff7d63eebf438855d9e2371e4a707c2e1d5f1a..a95fc21e451b68aff8b1c03b4ae2758e5b0c8087 100644 (file)
@@ -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",