Commit d68708d35240b8af805d476f94f301f8fcd28377

Authored by Wiebe Cazemier
1 parent d06dcb0b

Make setting rlimits a little better

main.cpp
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 #include <signal.h> 2 #include <signal.h>
3 #include <memory> 3 #include <memory>
4 #include <string.h> 4 #include <string.h>
5 -#include <sys/resource.h>  
6 #include <openssl/ssl.h> 5 #include <openssl/ssl.h>
7 6
8 #include "mainapp.h" 7 #include "mainapp.h"
@@ -32,12 +31,6 @@ static void signal_handler(int signal) @@ -32,12 +31,6 @@ static void signal_handler(int signal)
32 31
33 int register_signal_handers() 32 int register_signal_handers()
34 { 33 {
35 - // Quick'n dirty temp. TODO properly, with config file, checks, etc  
36 - rlim_t rlim = 1000000;  
37 - printf("Setting ulimit nofile to %ld.\n", rlim);  
38 - struct rlimit v = { rlim, rlim };  
39 - setrlimit(RLIMIT_NOFILE, &v);  
40 -  
41 struct sigaction sa; 34 struct sigaction sa;
42 memset(&sa, 0, sizeof (struct sigaction)); 35 memset(&sa, 0, sizeof (struct sigaction));
43 sa.sa_handler = &signal_handler; 36 sa.sa_handler = &signal_handler;
mainapp.cpp
@@ -539,7 +539,20 @@ void MainApp::quit() @@ -539,7 +539,20 @@ void MainApp::quit()
539 running = false; 539 running = false;
540 } 540 }
541 541
542 -// Loaded on app start where you want it to crash, loaded from within try/catch on reload, to allow the program to continue. 542 +
  543 +void MainApp::setlimits(rlim_t nofile)
  544 +{
  545 + logger->logf(LOG_INFO, "Setting ulimit nofile to %ld. TODO: configurable in config file.", nofile);
  546 + struct rlimit v = { nofile, nofile };
  547 + if (setrlimit(RLIMIT_NOFILE, &v) < 0)
  548 + {
  549 + logger->logf(LOG_ERR, "Setting ulimit nofile failed: %s. This means the default is used.", strerror(errno));
  550 + }
  551 +}
  552 +
  553 +/**
  554 + * @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.
  555 + */
543 void MainApp::loadConfig() 556 void MainApp::loadConfig()
544 { 557 {
545 Logger *logger = Logger::getInstance(); 558 Logger *logger = Logger::getInstance();
@@ -564,6 +577,8 @@ void MainApp::loadConfig() @@ -564,6 +577,8 @@ void MainApp::loadConfig()
564 logger->setLogPath(settings->logPath); 577 logger->setLogPath(settings->logPath);
565 logger->reOpen(); 578 logger->reOpen();
566 579
  580 + setlimits(1000000);
  581 +
567 for (std::shared_ptr<Listener> &l : this->listeners) 582 for (std::shared_ptr<Listener> &l : this->listeners)
568 { 583 {
569 l->loadCertAndKeyFromConfig(); 584 l->loadCertAndKeyFromConfig();
mainapp.h
@@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
11 #include <functional> 11 #include <functional>
12 #include <forward_list> 12 #include <forward_list>
13 #include <list> 13 #include <list>
  14 +#include <sys/resource.h>
14 15
15 #include "forward_declarations.h" 16 #include "forward_declarations.h"
16 17
@@ -46,6 +47,7 @@ class MainApp @@ -46,6 +47,7 @@ class MainApp
46 47
47 Logger *logger = Logger::getInstance(); 48 Logger *logger = Logger::getInstance();
48 49
  50 + void setlimits(rlim_t nofile);
49 void loadConfig(); 51 void loadConfig();
50 void reloadConfig(); 52 void reloadConfig();
51 static void doHelp(const char *arg); 53 static void doHelp(const char *arg);