Commit 436a5ffae8891d84c0e3003de72f2a408772fd0c

Authored by Wiebe Cazemier
1 parent e305b754

Setting for max number of open files: rlimit_nofile

configfileparser.cpp
... ... @@ -104,6 +104,7 @@ ConfigFileParser::ConfigFileParser(const std::string &path) :
104 104 validKeys.insert("mosquitto_password_file");
105 105 validKeys.insert("mosquitto_acl_file");
106 106 validKeys.insert("allow_anonymous");
  107 + validKeys.insert("rlimit_nofile");
107 108  
108 109 validListenKeys.insert("port");
109 110 validListenKeys.insert("protocol");
... ... @@ -379,6 +380,16 @@ void ConfigFileParser::loadFile(bool test)
379 380 bool tmp = stringTruthiness(value);
380 381 tmpSettings->allowAnonymous = tmp;
381 382 }
  383 +
  384 + if (key == "rlimit_nofile")
  385 + {
  386 + int newVal = std::stoi(value);
  387 + if (newVal <= 0)
  388 + {
  389 + throw ConfigFileException(formatString("Value '%d' is negative.", newVal));
  390 + }
  391 + tmpSettings->rlimitNoFile = newVal;
  392 + }
382 393 }
383 394 }
384 395 catch (std::invalid_argument &ex) // catch for the stoi()
... ...
mainapp.cpp
... ... @@ -644,13 +644,14 @@ void MainApp::quit()
644 644 }
645 645  
646 646  
647   -void MainApp::setlimits(rlim_t nofile)
  647 +void MainApp::setlimits()
648 648 {
649   - logger->logf(LOG_INFO, "Setting ulimit nofile to %ld. TODO: configurable in config file.", nofile);
  649 + rlim_t nofile = settings->rlimitNoFile;
  650 + logger->logf(LOG_INFO, "Setting rlimit nofile to %ld.", nofile);
650 651 struct rlimit v = { nofile, nofile };
651 652 if (setrlimit(RLIMIT_NOFILE, &v) < 0)
652 653 {
653   - logger->logf(LOG_ERR, "Setting ulimit nofile failed: %s. This means the default is used.", strerror(errno));
  654 + logger->logf(LOG_ERR, "Setting ulimit nofile failed: '%s'. This means the default is used.", strerror(errno));
654 655 }
655 656 }
656 657  
... ... @@ -682,7 +683,7 @@ void MainApp::loadConfig()
682 683 logger->reOpen();
683 684 logger->setFlags(settings->logDebug, settings->logSubscriptions);
684 685  
685   - setlimits(1000000);
  686 + setlimits();
686 687  
687 688 for (std::shared_ptr<Listener> &l : this->listeners)
688 689 {
... ...
mainapp.h
... ... @@ -67,7 +67,7 @@ class MainApp
67 67  
68 68 Logger *logger = Logger::getInstance();
69 69  
70   - void setlimits(rlim_t nofile);
  70 + void setlimits();
71 71 void loadConfig();
72 72 void reloadConfig();
73 73 static void doHelp(const char *arg);
... ...
settings.h
... ... @@ -45,6 +45,7 @@ public:
45 45 std::string mosquittoPasswordFile;
46 46 std::string mosquittoAclFile;
47 47 bool allowAnonymous = false;
  48 + int rlimitNoFile = 1000000;
48 49 std::list<std::shared_ptr<Listener>> listeners; // Default one is created later, when none are defined.
49 50  
50 51 AuthOptCompatWrap &getAuthOptsCompat();
... ...