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 */
*/
static void step_init_3(void)
{
- struct rlimit limit;
signal_register_fct(SIGQUIT, dump, SIGQUIT);
signal_register_fct(SIGUSR1, sig_soft_stop, SIGUSR1);
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
}
}
}
+
+/* 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);
+ }
+ }
+
+}