From 3a8a4d68735a0465dff9623c49fb6bf45e0850d8 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Thu, 6 Feb 2014 12:48:23 +0100 Subject: [PATCH] make sure we don't exceed the number of available filedescriptors for mthreads. You can still overshoot a little bit, but not all mthreads will be using an fd. --- pdns/misc.cc | 22 +++++++++++++++++++++- pdns/misc.hh | 3 +++ pdns/pdns_recursor.cc | 16 +++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/pdns/misc.cc b/pdns/misc.cc index bc4ea96b57..02344c1fa6 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -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 #include #include +#include #include #include #include @@ -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"); +} diff --git a/pdns/misc.hh b/pdns/misc.hh index 3b149c376d..9c71e228ea 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -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 diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 580a17628f..9a87eabee7 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -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<