[stgt] [PATCH] increase RLIMIT_NOFILE value

FUJITA Tomonori fujita.tomonori at lab.ntt.co.jp
Tue Dec 23 13:22:43 CET 2008


The default value (such as 1024) is too small. This set it to
/prco/sys/fs/nr_open. If the file is not available (old kernels or
procfs disabled), this uses the deafult nr_open value (1024 * 1024).

Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
---
 usr/tgtd.c |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/usr/tgtd.c b/usr/tgtd.c
index 1a3cc02..520e77c 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/resource.h>
 #include <sys/epoll.h>
 
 #include "list.h"
@@ -96,6 +97,37 @@ static int oom_adjust(void)
 	return 0;
 }
 
+static int nr_file_adjust(void)
+{
+	int ret, fd, max = 1024 * 1024;
+	char path[] = "/proc/sys/fs/nr_open";
+	char buf[64];
+	struct rlimit rlim;
+
+	/* Avoid oom-killer */
+	fd = open(path, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "can't open %s, %m\n", path);
+		goto set_rlimit;
+	}
+	ret = read(fd, buf, sizeof(buf));
+	if (ret < 0) {
+		fprintf(stderr, "can't read %s, %m\n", path);
+		return errno;
+	}
+	close(fd);
+	max = atoi(buf);
+
+set_rlimit:
+	rlim.rlim_cur = rlim.rlim_max = max;
+
+	ret = setrlimit(RLIMIT_NOFILE, &rlim);
+	if (ret < 0)
+		fprintf(stderr, "can't adjust nr_open %d %m\n", max);
+
+	return 0;
+}
+
 int tgt_event_add(int fd, int events, event_handler_t handler, void *data)
 {
 	struct epoll_event ev;
@@ -340,6 +372,10 @@ int main(int argc, char **argv)
 	if (err)
 		exit(1);
 
+	err = nr_file_adjust();
+	if (err)
+		exit(1);
+
 	err = log_init(program_name, LOG_SPACE_SIZE, is_daemon, is_debug);
 	if (err)
 		exit(1);
-- 
1.5.6.5

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list