OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // The following is the C version of code from base/process_utils_linux.cc. | 5 // The following is the C version of code from base/process_utils_linux.cc. |
6 // We shouldn't link against C++ code in a setuid binary. | 6 // We shouldn't link against C++ code in a setuid binary. |
7 | 7 |
8 // Needed for O_DIRECTORY, must be defined before fcntl.h is included | 8 // Needed for O_DIRECTORY, must be defined before fcntl.h is included |
9 // (and it can be included earlier than the explicit #include below | 9 // (and it can be included earlier than the explicit #include below |
10 // in some versions of glibc). | 10 // in some versions of glibc). |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 return false; | 52 return false; |
53 } | 53 } |
54 | 54 |
55 int fd = openat(dirfd, "oom_score_adj", O_WRONLY); | 55 int fd = openat(dirfd, "oom_score_adj", O_WRONLY); |
56 if (fd < 0) { | 56 if (fd < 0) { |
57 // We failed to open oom_score_adj, so let's try for the older | 57 // We failed to open oom_score_adj, so let's try for the older |
58 // oom_adj file instead. | 58 // oom_adj file instead. |
59 fd = openat(dirfd, "oom_adj", O_WRONLY); | 59 fd = openat(dirfd, "oom_adj", O_WRONLY); |
60 if (fd < 0) { | 60 if (fd < 0) { |
61 // Nope, that doesn't work either. | 61 // Nope, that doesn't work either. |
| 62 close(dirfd); |
62 return false; | 63 return false; |
63 } else { | 64 } else { |
64 // If we're using the old oom_adj file, the allowed range is now | 65 // If we're using the old oom_adj file, the allowed range is now |
65 // [0, kMaxOldOomScore], so we scale the score. This may result in some | 66 // [0, kMaxOldOomScore], so we scale the score. This may result in some |
66 // aliasing of values, of course. | 67 // aliasing of values, of course. |
67 score = score * kMaxOldOomScore / kMaxOomScore; | 68 score = score * kMaxOldOomScore / kMaxOomScore; |
68 } | 69 } |
69 } | 70 } |
70 close(dirfd); | 71 close(dirfd); |
71 | 72 |
72 char buf[11]; // 0 <= |score| <= kMaxOomScore; using log_10(2**32) + 1 size | 73 char buf[11]; // 0 <= |score| <= kMaxOomScore; using log_10(2**32) + 1 size |
73 snprintf(buf, sizeof(buf), "%d", score); | 74 snprintf(buf, sizeof(buf), "%d", score); |
74 size_t len = strlen(buf); | 75 size_t len = strlen(buf); |
75 | 76 |
76 ssize_t bytes_written = write(fd, buf, len); | 77 ssize_t bytes_written = write(fd, buf, len); |
77 close(fd); | 78 close(fd); |
78 return (bytes_written == (ssize_t)len); | 79 return (bytes_written == (ssize_t)len); |
79 } | 80 } |
OLD | NEW |