[stgt] [PATCH] use oom_score_adj instead of oom_adj for avoiding OOM killer

Hitoshi Mitake mitake.hitoshi at gmail.com
Sun Jul 21 15:17:46 CEST 2013


Using /proc/self/oom_adj for avoiding OOM killer of linux is
deprecated because it will be removed in the future. This patch lets
tgtd use /proc/self/oom_score_adj if it is available.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at gmail.com>
---
 usr/tgtd.c |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/usr/tgtd.c b/usr/tgtd.c
index 9f0650b..f985510 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -35,6 +35,7 @@
 #include <sys/epoll.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
 
 #include "list.h"
 #include "tgtd.h"
@@ -110,17 +111,27 @@ static void signal_catch(int signo)
 static int oom_adjust(void)
 {
 	int fd, err;
-	char path[64];
+	const char *path, *score;
+	struct stat st;
 
 	/* Avoid oom-killer */
-	sprintf(path, "/proc/%d/oom_adj", getpid());
+	path = "/proc/self/oom_score_adj";
+	score = "-1000\n";
+
+	if (stat(path, &st)) {
+		/* oom_score_adj cannot be used, try oom_adj */
+		path = "/proc/self/oom_adj";
+		score = "-17\n";
+	}
+
 	fd = open(path, O_WRONLY);
 	if (fd < 0) {
 		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n",
 			path);
 		return errno;
 	}
-	err = write(fd, "-17\n", 4);
+
+	err = write(fd, score, strlen(score));
 	if (err < 0) {
 		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n",
 			path);
-- 
1.7.5.1

--
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