Increase limit of open file descriptors to match the hard limit 36/319336/6
authorKrzysztof Malysa <k.malysa@samsung.com>
Mon, 21 Oct 2024 17:19:47 +0000 (19:19 +0200)
committerKrzysztof Malysa <k.malysa@samsung.com>
Thu, 24 Oct 2024 12:50:16 +0000 (14:50 +0200)
Change-Id: Ia72903b06b1636cad9c27faed1fb9e0dc5e7cb8f

src/service/main/main.cpp

index 57b45a59a992cb42fd4013b1fd3faab035e3d06f..a91aad5c4c0403ea7506366a1c1629a90d030cdb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2024 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  *
  * @brief       Main Cynara daemon file
  */
 
+#include <cerrno>
+#include <cstdlib>
 #include <exception>
 #include <fcntl.h>
-#include <iostream>
 #include <stdlib.h>
+#include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -41,6 +43,7 @@
 #endif
 
 #include <common.h>
+#include <common/error/SafeStrError.h>
 #include <log/log.h>
 
 #include "CmdlineParser.h"
@@ -113,6 +116,21 @@ int main(int argc, char **argv) {
 
         init_log();
 
+        // Increase the soft limit of open file descriptors
+        rlimit limit;
+        if (prlimit(0, RLIMIT_NOFILE, nullptr, &limit)) {
+            LOGE("prlimit() failed - %s", Cynara::safeStrError(errno).c_str());
+        } else {
+            LOGW("Open fd limit: %zu", static_cast<size_t>(limit.rlim_cur));
+            if (limit.rlim_cur < limit.rlim_max) {
+                limit.rlim_cur = limit.rlim_max;
+                LOGW("Increasing open fd limit to %zu", static_cast<size_t>(limit.rlim_max));
+                if (prlimit(0, RLIMIT_NOFILE, &limit, nullptr)) {
+                    LOGE("prlimit() failed - %s", Cynara::safeStrError(errno).c_str());
+                }
+            }
+        }
+
         Cynara::Cynara cynara;
         LOGI("Cynara service is starting ...");
         cynara.init();