Commit d68708d35240b8af805d476f94f301f8fcd28377
1 parent
d06dcb0b
Make setting rlimits a little better
Showing
3 changed files
with
18 additions
and
8 deletions
main.cpp
| ... | ... | @@ -2,7 +2,6 @@ |
| 2 | 2 | #include <signal.h> |
| 3 | 3 | #include <memory> |
| 4 | 4 | #include <string.h> |
| 5 | -#include <sys/resource.h> | |
| 6 | 5 | #include <openssl/ssl.h> |
| 7 | 6 | |
| 8 | 7 | #include "mainapp.h" |
| ... | ... | @@ -32,12 +31,6 @@ static void signal_handler(int signal) |
| 32 | 31 | |
| 33 | 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 | 34 | struct sigaction sa; |
| 42 | 35 | memset(&sa, 0, sizeof (struct sigaction)); |
| 43 | 36 | sa.sa_handler = &signal_handler; | ... | ... |
mainapp.cpp
| ... | ... | @@ -539,7 +539,20 @@ void MainApp::quit() |
| 539 | 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 | 556 | void MainApp::loadConfig() |
| 544 | 557 | { |
| 545 | 558 | Logger *logger = Logger::getInstance(); |
| ... | ... | @@ -564,6 +577,8 @@ void MainApp::loadConfig() |
| 564 | 577 | logger->setLogPath(settings->logPath); |
| 565 | 578 | logger->reOpen(); |
| 566 | 579 | |
| 580 | + setlimits(1000000); | |
| 581 | + | |
| 567 | 582 | for (std::shared_ptr<Listener> &l : this->listeners) |
| 568 | 583 | { |
| 569 | 584 | l->loadCertAndKeyFromConfig(); | ... | ... |
mainapp.h
| ... | ... | @@ -11,6 +11,7 @@ |
| 11 | 11 | #include <functional> |
| 12 | 12 | #include <forward_list> |
| 13 | 13 | #include <list> |
| 14 | +#include <sys/resource.h> | |
| 14 | 15 | |
| 15 | 16 | #include "forward_declarations.h" |
| 16 | 17 | |
| ... | ... | @@ -46,6 +47,7 @@ class MainApp |
| 46 | 47 | |
| 47 | 48 | Logger *logger = Logger::getInstance(); |
| 48 | 49 | |
| 50 | + void setlimits(rlim_t nofile); | |
| 49 | 51 | void loadConfig(); |
| 50 | 52 | void reloadConfig(); |
| 51 | 53 | static void doHelp(const char *arg); | ... | ... |