[sheepdog] [PATCH v1] try to set RLIMIT_NOFILE ro recommended value

Vasiliy Tolstov v.tolstov at selfip.ru
Mon Mar 16 17:30:44 CET 2015


Signed-off-by: Vasiliy Tolstov <v.tolstov at selfip.ru>
---
 sheep/sheep.c | 63 +++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 21 deletions(-)

diff --git a/sheep/sheep.c b/sheep/sheep.c
index a1028a2..13cc927 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -561,26 +561,47 @@ static int create_work_queues(void)
 
 static void check_host_env(void)
 {
-	struct rlimit r;
+	struct rlimit new;
+  struct rlimit old;
+  int ret;
+  int fd;
+  int len;
+  char *buf = NULL;
+  pid_t pid;
+
+  pid = getpid();
+
+  new.rlim_cur = SD_RLIM_NOFILE;
+  new.rlim_max = SD_RLIM_NOFILE;
+
+  ret = prlimit(pid, RLIMIT_NOFILE, NULL, &old);
+  if (old.rlim_cur < new.rlim_cur) {
+    sd_info("Allowed open files %lu, suggested %lu", old.rlim_cur, new.rlim_cur);
+
+    len = asprintf(&buf, "%lu", new.rlim_max);
+    if (len > 0) {
+      fd = open("/proc/sys/fs/nr_open", O_WRONLY);
+      if (fd > 0) {
+        ret = xwrite(fd, buf, len);
+        if (ret > 0) {
+          if (prlimit(pid, RLIMIT_NOFILE, &new, NULL) != 0) {
+            sd_warn("Failed to set open files limit to suggested %lu",
+                    new.rlim_cur);
+          } else {
+            sd_info("Allowed open files set to suggested %lu", new.rlim_cur);
+          }
+        }
+        close(fd);
+      }
+      free(buf);
+    }
+  }
 
-	if (getrlimit(RLIMIT_NOFILE, &r) < 0)
-		sd_err("failed to get nofile %m");
-	/*
-	 * 1024 is default for NOFILE on most distributions, which is very
-	 * dangerous to run Sheepdog cluster.
-	 */
-	else if (r.rlim_cur == 1024)
-		sd_warn("Allowed open files 1024 too small, suggested %u",
-			SD_RLIM_NOFILE);
-	else if (r.rlim_cur < SD_RLIM_NOFILE)
-		sd_info("Allowed open files %lu, suggested %u", r.rlim_cur,
-			SD_RLIM_NOFILE);
-
-	if (getrlimit(RLIMIT_CORE, &r) < 0)
+	if (getrlimit(RLIMIT_CORE, &old) < 0)
 		sd_debug("failed to get core %m");
-	else if (r.rlim_cur < RLIM_INFINITY)
+	else if (old.rlim_cur < RLIM_INFINITY)
 		sd_debug("Allowed core file size %lu, suggested unlimited",
-			 r.rlim_cur);
+			 old.rlim_cur);
 
 	/*
 	 * Disable glibc's dynamic mmap threshold and set it as 512k.
-- 
2.2.2




More information about the sheepdog mailing list