1
0
mirror of http://git.haproxy.org/git/haproxy.git/ synced 2025-04-06 17:22:17 +00:00

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.
This commit is contained in:
Valentine Krasnobaeva 2024-11-25 15:48:54 +01:00 committed by William Lallemand
parent 1332e9b58d
commit 14f5e00d38
3 changed files with 74 additions and 49 deletions
include/haproxy
src

View File

@ -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 */

View File

@ -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

View File

@ -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);
}
}
}