From: Valentine Krasnobaeva Date: Mon, 25 Nov 2024 15:15:17 +0000 (+0100) Subject: REORG: startup: move nofile limit checks in limits.c X-Git-Tag: v3.2-dev2~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbc534a6f;p=thirdparty%2Fhaproxy.git REORG: startup: move nofile limit checks in limits.c Let's encapsulate the code, which checks the applied nofile limit into a separate helper check_nofile_lim_and_prealloc_fd(). Let's keep in this new function scope the block, which tries to create a copy of FD with the highest number, if prealloc-fd is set in the configuration. --- diff --git a/include/haproxy/limits.h b/include/haproxy/limits.h index 77e835525c..0185baf927 100644 --- a/include/haproxy/limits.h +++ b/include/haproxy/limits.h @@ -7,6 +7,8 @@ #ifndef _HAPROXY_LIMITS_H #define _HAPROXY_LIMITS_H +#include +#include #include #include @@ -40,6 +42,7 @@ int raise_rlim_nofile(struct rlimit *old_limit, struct rlimit *new_limit); void set_global_maxconn(void); void apply_nofile_limit(void); void apply_memory_limit(void); +void check_nofile_lim_and_prealloc_fd(void); #endif /* _HAPROXY_LIMITS_H */ diff --git a/src/haproxy.c b/src/haproxy.c index b900422da7..e5de013d57 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -23,8 +23,6 @@ #include #include #include -#include -#include #include #include #include @@ -2345,8 +2343,6 @@ static void step_init_3(void) */ static void step_init_4(void) { - struct rlimit limit; - /* MODE_QUIET is applied here, it can inhibit alerts and warnings below this line */ if (getenv("HAPROXY_MWORKER_REEXEC") != NULL) { /* either stdin/out/err are already closed or should stay as they are. */ @@ -2366,31 +2362,10 @@ static void step_init_4(void) * be able to restart the old pids. */ - /* check ulimits */ - limit.rlim_cur = limit.rlim_max = 0; - getrlimit(RLIMIT_NOFILE, &limit); - if (limit.rlim_cur < global.maxsock) { - if (global.tune.options & GTUNE_STRICT_LIMITS) { - ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. " - "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", - progname, (int)limit.rlim_cur, global.maxconn, global.maxsock, - global.maxsock); - exit(1); - } - else - ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. " - "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", - progname, (int)limit.rlim_cur, global.maxconn, global.maxsock, - global.maxsock); - } - - if (global.prealloc_fd && fcntl((int)limit.rlim_cur - 1, F_GETFD) == -1) { - if (dup2(0, (int)limit.rlim_cur - 1) == -1) - ha_warning("[%s.main()] Unable to preallocate file descriptor %d : %s", - progname, (int)limit.rlim_cur - 1, strerror(errno)); - else - close((int)limit.rlim_cur - 1); - } + /* check current nofile limit reported via getrlimit() and check if we + * can preallocate FDs, if global.prealloc_fd is set. + */ + check_nofile_lim_and_prealloc_fd(); /* update the ready date a last time to also account for final setup time */ clock_update_date(0, 1); diff --git a/src/limits.c b/src/limits.c index 7f956edb0d..83ddb37e44 100644 --- a/src/limits.c +++ b/src/limits.c @@ -500,3 +500,38 @@ void apply_memory_limit(void) } } + +/* Checks the current nofile limit via getrlimit and preallocates the + * (limit.rlim_cur - 1) of FDs. It may terminate the process, if its current + * nofile limit is lower than global.maxsock and there is no 'no strict-limits' + * in the global section. + */ +void check_nofile_lim_and_prealloc_fd(void) +{ + struct rlimit limit; + + limit.rlim_cur = limit.rlim_max = 0; + getrlimit(RLIMIT_NOFILE, &limit); + if (limit.rlim_cur < global.maxsock) { + if (global.tune.options & GTUNE_STRICT_LIMITS) { + ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. " + "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", + progname, (int)limit.rlim_cur, global.maxconn, global.maxsock, + global.maxsock); + exit(1); + } + else + ha_alert("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. " + "Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", + progname, (int)limit.rlim_cur, global.maxconn, global.maxsock, + global.maxsock); + } + + if (global.prealloc_fd && fcntl((int)limit.rlim_cur - 1, F_GETFD) == -1) { + if (dup2(0, (int)limit.rlim_cur - 1) == -1) + ha_warning("[%s.main()] Unable to preallocate file descriptor %d : %s", + progname, (int)limit.rlim_cur - 1, strerror(errno)); + else + close((int)limit.rlim_cur - 1); + } +}