Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #define _GNU_SOURCE // needed for O_DIRECTORY | 8 #define _GNU_SOURCE // needed for O_DIRECTORY |
| 9 | 9 |
| 10 #include "process_util.h" | 10 #include "process_util.h" |
| 11 | 11 |
| 12 #include <fcntl.h> | 12 #include <fcntl.h> |
| 13 #include <inttypes.h> | 13 #include <inttypes.h> |
| 14 #include <limits.h> | 14 #include <limits.h> |
| 15 #include <stdio.h> | 15 #include <stdio.h> |
| 16 #include <stdlib.h> | 16 #include <stdlib.h> |
| 17 #include <string.h> | 17 #include <string.h> |
| 18 #include <sys/stat.h> | 18 #include <sys/stat.h> |
| 19 #include <sys/types.h> | 19 #include <sys/types.h> |
| 20 #include <unistd.h> | 20 #include <unistd.h> |
| 21 | 21 |
| 22 bool AdjustOOMScore(pid_t process, int score) { | 22 bool AdjustOOMScore(pid_t process, int score) { |
| 23 if (score < 0 || score > 15) | 23 if (score < 0 || score > 1000) |
|
stevenjb
2011/08/18 00:20:07
And in the darkness bind them?
Greg Spencer (Chromium)
2011/08/18 23:10:50
Also moved into a constant.
| |
| 24 return false; | 24 return false; |
| 25 | 25 |
| 26 char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0" | 26 char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0" |
| 27 // 6 + 20 + 1 = 27 | 27 // 6 + 20 + 1 = 27 |
| 28 snprintf(oom_adj, sizeof(oom_adj), "/proc/%" PRIdMAX, (intmax_t)process); | 28 snprintf(oom_adj, sizeof(oom_adj), "/proc/%" PRIdMAX, (intmax_t)process); |
| 29 | 29 |
| 30 const int dirfd = open(oom_adj, O_RDONLY | O_DIRECTORY); | 30 const int dirfd = open(oom_adj, O_RDONLY | O_DIRECTORY); |
| 31 if (dirfd < 0) | 31 if (dirfd < 0) |
| 32 return false; | 32 return false; |
| 33 | 33 |
| 34 struct stat statbuf; | 34 struct stat statbuf; |
| 35 if (fstat(dirfd, &statbuf) < 0) { | 35 if (fstat(dirfd, &statbuf) < 0) { |
| 36 close(dirfd); | 36 close(dirfd); |
| 37 return false; | 37 return false; |
| 38 } | 38 } |
| 39 if (getuid() != statbuf.st_uid) { | 39 if (getuid() != statbuf.st_uid) { |
| 40 close(dirfd); | 40 close(dirfd); |
| 41 return false; | 41 return false; |
| 42 } | 42 } |
| 43 | 43 |
| 44 const int fd = openat(dirfd, "oom_adj", O_WRONLY); | 44 int fd = openat(dirfd, "oom_score_adj", O_WRONLY); |
| 45 if (fd < 0) { | |
| 46 // We failed to open oom_score_adj, so let's try for the older | |
| 47 // oom_adj file instead. | |
| 48 fd = openat(dirfd, "oom_adj", O_WRONLY); | |
| 49 if (fd < 0) { | |
| 50 // Nope, that doesn't work either. | |
| 51 return false; | |
| 52 } else { | |
| 53 // If we're using the old oom_adj file, the allowed range is now | |
| 54 // [0, 15], so we scale the score. This may result in some | |
| 55 // aliasing of values, of course. | |
| 56 score = score * 15 / 1000; | |
| 57 } | |
| 58 } | |
| 45 close(dirfd); | 59 close(dirfd); |
| 46 | 60 |
| 47 if (fd < 0) | 61 char buf[5]; // 0 <= |score| <= 1000; |
|
stevenjb
2011/08/18 00:20:07
Maybe size this for any 32 bit range?
Greg Spencer (Chromium)
2011/08/18 23:10:50
Done.
| |
| 48 return false; | |
| 49 | |
| 50 char buf[3]; // 0 <= |score| <= 15; | |
| 51 snprintf(buf, sizeof(buf), "%d", score); | 62 snprintf(buf, sizeof(buf), "%d", score); |
| 52 size_t len = strlen(buf); | 63 size_t len = strlen(buf); |
| 53 | 64 |
| 54 ssize_t bytes_written = write(fd, buf, len); | 65 ssize_t bytes_written = write(fd, buf, len); |
| 55 close(fd); | 66 close(fd); |
| 56 return (bytes_written == len); | 67 return (bytes_written == len); |
| 57 } | 68 } |
| OLD | NEW |