]> granicus.if.org Git - pdns/commitdiff
make sure we don't exceed the number of available filedescriptors for mthreads. You...
authorbert hubert <bert.hubert@netherlabs.nl>
Thu, 6 Feb 2014 11:48:23 +0000 (12:48 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Thu, 6 Feb 2014 11:51:02 +0000 (12:51 +0100)
pdns/misc.cc
pdns/misc.hh
pdns/pdns_recursor.cc

index bc4ea96b575026cac23747f853929b68446c952a..02344c1fa6cc6d425d25a5a3a8ee82667bd45b2e 100644 (file)
@@ -1,6 +1,6 @@
 /*
     PowerDNS Versatile Database Driven Nameserver
-    Copyright (C) 2002 - 2010  PowerDNS.COM BV
+    Copyright (C) 2002 - 2014  PowerDNS.COM BV
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2
@@ -24,6 +24,7 @@
 #include <netdb.h>
 #include <sys/time.h>
 #include <time.h>
+#include <sys/resource.h>
 #include <netinet/in.h>
 #include <sys/un.h>
 #include <unistd.h>
@@ -816,3 +817,22 @@ void addCMsgSrcAddr(struct msghdr* msgh, void* cmsgbuf, ComboAddress* source)
     msgh->msg_controllen = cmsg->cmsg_len;
   }
 }
+
+unsigned int getFilenumLimit(bool hardOrSoft)
+{
+  struct rlimit rlim;
+  if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
+    unixDie("Requesting number of available file descriptors");
+  return hardOrSoft ? rlim.rlim_max : rlim.rlim_cur;
+}
+
+void setFilenumLimit(unsigned int lim)
+{
+  struct rlimit rlim;
+
+  if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
+    unixDie("Requesting number of available file descriptors");
+  rlim.rlim_cur=lim;
+  if(setrlimit(RLIMIT_NOFILE, &rlim) < 0)
+    unixDie("Setting number of available file descriptors");
+}
index 3b149c376da74d61c451ccd98175de4e07af6bdd..9c71e228eae4830eaebdf821390de51364726016 100644 (file)
@@ -504,4 +504,7 @@ private:
 
 union ComboAddress;
 void addCMsgSrcAddr(struct msghdr* msgh, void* cmsgbuf, ComboAddress* source);
+
+unsigned int getFilenumLimit(bool hardOrSoft=0);
+void setFilenumLimit(unsigned int lim);
 #endif
index 580a17628ff77f229911cb25ef7d0c9815f3166e..9a87eabee77e282bfe5f5b738c41fb7076e379ba 100644 (file)
@@ -1850,7 +1850,21 @@ int serviceMain(int argc, char*argv[])
   
   g_tcpTimeout=::arg().asNum("client-tcp-timeout");
   g_maxTCPPerClient=::arg().asNum("max-tcp-per-client");
-  g_maxMThreads=::arg().asNum("max-mthreads");
+  g_maxMThreads=::arg().asNum("max-mthreads"); 
+  unsigned int availFDs=getFilenumLimit();
+  if(g_maxMThreads * g_numThreads > availFDs) {
+    if(getFilenumLimit(true) >= g_maxMThreads * g_numThreads) {
+      setFilenumLimit(g_maxMThreads * g_numThreads);
+      L<<Logger::Warning<<"Raised soft limit on number of filedescriptors to "<<g_maxMThreads * g_numThreads<<" to match max-mthreads and threads settings"<<endl;
+    }
+    else {
+      int newval = getFilenumLimit(true) / g_numThreads;
+      L<<Logger::Warning<<"Insufficient number of filedescriptors available for max-mthreads*threads setting! ("<<availFDs<<" < "<<g_maxMThreads*g_numThreads<<"), reducing max-mthreads to "<<newval<<endl;
+      g_maxMThreads = newval;
+    }
+
+    
+  }
 
   if(g_numThreads == 1) {
     L<<Logger::Warning<<"Operating unthreaded"<<endl;