Index: sandbox/linux/suid/process_util_linux.c |
diff --git a/sandbox/linux/suid/process_util_linux.c b/sandbox/linux/suid/process_util_linux.c |
index 17453def621006ca2d824c45c21cb922d6849d60..13f45ce92cf79c4b8d58a40512b99a5381317cb3 100644 |
--- a/sandbox/linux/suid/process_util_linux.c |
+++ b/sandbox/linux/suid/process_util_linux.c |
@@ -1,4 +1,4 @@ |
-// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
@@ -19,8 +19,13 @@ |
#include <sys/types.h> |
#include <unistd.h> |
+// Ranges for the current (oom_score_adj) and previous (oom_adj) |
+// flavors of OOM score. |
+static const int kMaxOomScore = 1000; |
+static const int kMaxOldOomScore = 15; |
+ |
bool AdjustOOMScore(pid_t process, int score) { |
- if (score < 0 || score > 15) |
+ if (score < 0 || score > kMaxOomScore) |
return false; |
char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0" |
@@ -41,13 +46,24 @@ bool AdjustOOMScore(pid_t process, int score) { |
return false; |
} |
- const int fd = openat(dirfd, "oom_adj", O_WRONLY); |
+ int fd = openat(dirfd, "oom_score_adj", O_WRONLY); |
+ if (fd < 0) { |
+ // We failed to open oom_score_adj, so let's try for the older |
+ // oom_adj file instead. |
+ fd = openat(dirfd, "oom_adj", O_WRONLY); |
+ if (fd < 0) { |
+ // Nope, that doesn't work either. |
+ return false; |
+ } else { |
+ // If we're using the old oom_adj file, the allowed range is now |
+ // [0, kMaxOldOomScore], so we scale the score. This may result in some |
+ // aliasing of values, of course. |
+ score = score * kMaxOldOomScore / kMaxOomScore; |
+ } |
+ } |
close(dirfd); |
- if (fd < 0) |
- return false; |
- |
- char buf[3]; // 0 <= |score| <= 15; |
+ char buf[11]; // 0 <= |score| <= kMaxOomScore; using log_10(2**32) + 1 size |
snprintf(buf, sizeof(buf), "%d", score); |
size_t len = strlen(buf); |