From d68708d35240b8af805d476f94f301f8fcd28377 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sat, 13 Mar 2021 14:14:35 +0100 Subject: [PATCH] Make setting rlimits a little better --- main.cpp | 7 ------- mainapp.cpp | 17 ++++++++++++++++- mainapp.h | 2 ++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 66b79c3..fd289d3 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include "mainapp.h" @@ -32,12 +31,6 @@ static void signal_handler(int signal) int register_signal_handers() { - // Quick'n dirty temp. TODO properly, with config file, checks, etc - rlim_t rlim = 1000000; - printf("Setting ulimit nofile to %ld.\n", rlim); - struct rlimit v = { rlim, rlim }; - setrlimit(RLIMIT_NOFILE, &v); - struct sigaction sa; memset(&sa, 0, sizeof (struct sigaction)); sa.sa_handler = &signal_handler; diff --git a/mainapp.cpp b/mainapp.cpp index 0216b74..3132cf6 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -539,7 +539,20 @@ void MainApp::quit() running = false; } -// Loaded on app start where you want it to crash, loaded from within try/catch on reload, to allow the program to continue. + +void MainApp::setlimits(rlim_t nofile) +{ + logger->logf(LOG_INFO, "Setting ulimit nofile to %ld. TODO: configurable in config file.", nofile); + struct rlimit v = { nofile, nofile }; + if (setrlimit(RLIMIT_NOFILE, &v) < 0) + { + logger->logf(LOG_ERR, "Setting ulimit nofile failed: %s. This means the default is used.", strerror(errno)); + } +} + +/** + * @brief MainApp::loadConfig is loaded on app start where you want it to crash, loaded from within try/catch on reload, to allow the program to continue. + */ void MainApp::loadConfig() { Logger *logger = Logger::getInstance(); @@ -564,6 +577,8 @@ void MainApp::loadConfig() logger->setLogPath(settings->logPath); logger->reOpen(); + setlimits(1000000); + for (std::shared_ptr &l : this->listeners) { l->loadCertAndKeyFromConfig(); diff --git a/mainapp.h b/mainapp.h index ac139f0..0c44522 100644 --- a/mainapp.h +++ b/mainapp.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "forward_declarations.h" @@ -46,6 +47,7 @@ class MainApp Logger *logger = Logger::getInstance(); + void setlimits(rlim_t nofile); void loadConfig(); void reloadConfig(); static void doHelp(const char *arg); -- libgit2 0.21.4