From 14f5e00d38fb1426223e28db276a31cf5ada14e3 Mon Sep 17 00:00:00 2001 From: Valentine Krasnobaeva Date: Mon, 25 Nov 2024 15:48:54 +0100 Subject: [PATCH] REORG: startup: move code that applies limits to limits.c In step_init_3() we try to apply provided or calculated earlier haproxy maxsock and memmax limits. Let's encapsulate these code blocks in dedicated functions: apply_nofile_limit() and apply_memory_limit() and let's move them into limits.c. Limits.c gathers now all the logic for calculating and setting system limits in dependency of the provided configuration. --- include/haproxy/limits.h | 3 ++ src/haproxy.c | 51 ++--------------------------- src/limits.c | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 49 deletions(-) diff --git a/include/haproxy/limits.h b/include/haproxy/limits.h index cff7de13b..77e835525 100644 --- a/include/haproxy/limits.h +++ b/include/haproxy/limits.h @@ -38,5 +38,8 @@ int check_if_maxsock_permitted(int maxsock); 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); + #endif /* _HAPROXY_LIMITS_H */ diff --git a/src/haproxy.c b/src/haproxy.c index ff5723a49..b900422da 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -2312,7 +2312,6 @@ static void step_init_2(int argc, char** argv) */ static void step_init_3(void) { - struct rlimit limit; signal_register_fct(SIGQUIT, dump, SIGQUIT); signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1); @@ -2326,54 +2325,8 @@ static void step_init_3(void) signal_register_fct(SIGPIPE, NULL, 0); /* ulimits */ - if (!global.rlimit_nofile) - global.rlimit_nofile = global.maxsock; - - if (global.rlimit_nofile) { - limit.rlim_cur = global.rlimit_nofile; - limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur); - - if ((global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) || - 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; - - if (global.tune.options & GTUNE_STRICT_LIMITS) { - ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", - progname, global.rlimit_nofile, (int)limit.rlim_cur); - exit(1); - } - else { - /* try to set it to the max possible at least */ - limit.rlim_cur = limit.rlim_max; - if (global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) - limit.rlim_cur = global.fd_hard_limit; - - 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", - progname, global.rlimit_nofile, (int)limit.rlim_cur); - global.rlimit_nofile = limit.rlim_cur; - } - } - } - - if (global.rlimit_memmax) { - limit.rlim_cur = limit.rlim_max = - global.rlimit_memmax * 1048576ULL; - if (setrlimit(RLIMIT_DATA, &limit) == -1) { - if (global.tune.options & GTUNE_STRICT_LIMITS) { - ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n", - progname, global.rlimit_memmax); - exit(1); - } - else - ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", - progname, global.rlimit_memmax); - } - } + apply_nofile_limit(); + apply_memory_limit(); #if defined(USE_LINUX_CAP) /* If CAP_NET_BIND_SERVICE is in binary file permitted set and process diff --git a/src/limits.c b/src/limits.c index 2ea771db9..7f956edb0 100644 --- a/src/limits.c +++ b/src/limits.c @@ -431,3 +431,72 @@ void set_global_maxconn(void) } } } + +/* Sets the current and max nofile limits for the process. It may terminate the + * process, if it can't raise FD limit and there is no 'no strict-limits' in the + * global section. + */ +void apply_nofile_limit(void) +{ + struct rlimit limit; + + if (!global.rlimit_nofile) + global.rlimit_nofile = global.maxsock; + + if (global.rlimit_nofile) { + limit.rlim_cur = global.rlimit_nofile; + limit.rlim_max = MAX(rlim_fd_max_at_boot, limit.rlim_cur); + + if ((global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) || + 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; + + if (global.tune.options & GTUNE_STRICT_LIMITS) { + ha_alert("[%s.main()] Cannot raise FD limit to %d, limit is %d.\n", + progname, global.rlimit_nofile, (int)limit.rlim_cur); + exit(1); + } + else { + /* try to set it to the max possible at least */ + limit.rlim_cur = limit.rlim_max; + if (global.fd_hard_limit && limit.rlim_cur > global.fd_hard_limit) + limit.rlim_cur = global.fd_hard_limit; + + 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", + progname, global.rlimit_nofile, (int)limit.rlim_cur); + global.rlimit_nofile = limit.rlim_cur; + } + } + } + +} + +/* Sets the current and max memory limits for the process. It may terminate the + * process, if it can't raise RLIMIT_DATA limit and there is no + * 'no strict-limits' in the global section. + */ +void apply_memory_limit(void) +{ + struct rlimit limit; + + if (global.rlimit_memmax) { + limit.rlim_cur = limit.rlim_max = + global.rlimit_memmax * 1048576ULL; + if (setrlimit(RLIMIT_DATA, &limit) == -1) { + if (global.tune.options & GTUNE_STRICT_LIMITS) { + ha_alert("[%s.main()] Cannot fix MEM limit to %d megs.\n", + progname, global.rlimit_memmax); + exit(1); + } + else + ha_warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", + progname, global.rlimit_memmax); + } + } + +}