]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: startup: move code that applies limits to limits.c
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Mon, 25 Nov 2024 14:48:54 +0000 (15:48 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Mon, 16 Dec 2024 09:44:01 +0000 (10:44 +0100)
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
src/haproxy.c
src/limits.c

index cff7de13b21597ab45a9d39b5d7cee1cb629c3c9..77e835525c96d109baa5395b03b2bbba6c73b0d2 100644 (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 */
index ff5723a49cd2d2c0e5a599c7792afe9ec629f337..b900422da777223395f65dfd12c679ca4e9c4d36 100644 (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
index 2ea771db93dd980bfd49f332cf388b6514bc227f..7f956edb0dd7a21f1696d64602fe1e1241d0ce1d 100644 (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);
+               }
+       }
+
+}