From 6714796ecd07a2e38201fb5ce2a48aff9ace104b Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 5 Dec 2013 11:04:47 +0100 Subject: [PATCH] Set rlimits in the Application class (rather than in the init script). Fixes #5260 --- etc/init.d/icinga2.cmake | 3 --- icinga-app/icinga.cpp | 2 ++ lib/base/application.cpp | 26 ++++++++++++++++++++++++++ lib/base/application.h | 2 ++ lib/base/unix.h | 2 ++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/etc/init.d/icinga2.cmake b/etc/init.d/icinga2.cmake index 6f9c93349..d70d0f9cc 100644 --- a/etc/init.d/icinga2.cmake +++ b/etc/init.d/icinga2.cmake @@ -65,9 +65,6 @@ start() { fi echo "Starting Icinga 2: " - ulimit -n 32768 - ulimit -s 512 - ulimit -u 16384 $DAEMON -c $ICINGA2_CONFIG_FILE -d -e $ICINGA2_ERROR_LOG -u $ICINGA2_USER -g $ICINGA2_GROUP echo "Done" diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index f41db959d..01313ead6 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -187,6 +187,8 @@ int main(int argc, char **argv) { Application::SetStartTime(Utility::GetTime()); + Application::SetResourceLimits(); + /* Set thread title. */ Utility::SetThreadName("Main Thread", false); diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 7a8564875..a18e35aa1 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -112,6 +112,32 @@ Application::Ptr Application::GetInstance(void) return m_Instance->GetSelf(); } +void Application::SetResourceLimits(void) +{ +#ifndef _WIN32 +# ifdef RLIMIT_NOFILE + rlimit rl; + rl.rlim_cur = 16 * 1024; + rl.rlim_max = rl.rlim_cur; + + if (setrlimit(RLIMIT_NOFILE, &rl) < 0) + Log(LogCritical, "base", "Could not adjust resource limit for open file handles (RLIMIT_NOFILE)"); +# else /* RLIMIT_NOFILE */ + Log(LogCritical, "base", "System does not support adjusting the resource limit for open file handles (RLIMIT_NOFILE)"); +# endif /* RLIMIT_NOFILE */ + +# ifdef RLIMIT_NPROC + rl.rlim_cur = 16 * 1024; + rl.rlim_max = rl.rlim_cur; + + if (setrlimit(RLIMIT_NPROC, &rl) < 0) + Log(LogCritical, "base", "Could not adjust resource limit for number of processes (RLIMIT_NPROC)"); +# else /* RLIMIT_NPROC */ + Log(LogCritical, "base", "System does not support adjusting the resource limit for number of processes (RLIMIT_NPROC)"); +# endif /* RLIMIT_NPROC */ +#endif /* _WIN32 */ +} + int Application::GetArgC(void) { return m_ArgC; diff --git a/lib/base/application.h b/lib/base/application.h index 583980c35..1c61aa35a 100644 --- a/lib/base/application.h +++ b/lib/base/application.h @@ -51,6 +51,8 @@ public: */ virtual int Main(void) = 0; + static void SetResourceLimits(void); + static int GetArgC(void); static void SetArgC(int argc); diff --git a/lib/base/unix.h b/lib/base/unix.h index 0f6cf9a12..03be340e6 100644 --- a/lib/base/unix.h +++ b/lib/base/unix.h @@ -39,6 +39,8 @@ #include #include #include +#include +#include typedef int SOCKET; #define INVALID_SOCKET (-1)