| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 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. |
| 7 |
| 8 #include "process_util.h" |
| 9 |
| 10 #include <fcntl.h> |
| 11 #include <limits.h> |
| 12 #include <stdio.h> |
| 13 #include <stdlib.h> |
| 14 #include <string.h> |
| 15 #include <sys/stat.h> |
| 16 #include <sys/types.h> |
| 17 #include <unistd.h> |
| 18 |
| 19 bool AdjustOOMScore(pid_t process, int score) { |
| 20 if (score < 0 || score > 15) |
| 21 return false; |
| 22 |
| 23 char oom_adj[PATH_MAX]; |
| 24 snprintf(oom_adj, sizeof(oom_adj), "/proc/%lu", process); |
| 25 |
| 26 struct stat statbuf; |
| 27 if (stat(oom_adj, &statbuf) < 0) |
| 28 return false; |
| 29 if (getuid() != statbuf.st_uid) |
| 30 return false; |
| 31 |
| 32 strcat(oom_adj, "/oom_adj"); |
| 33 int fd = open(oom_adj, O_WRONLY); |
| 34 if (fd < 0) |
| 35 return false; |
| 36 |
| 37 char buf[3]; |
| 38 snprintf(buf, sizeof(buf), "%d", score); |
| 39 size_t len = strlen(buf); |
| 40 |
| 41 ssize_t bytes_written = write(fd, buf, len); |
| 42 close(fd); |
| 43 return (bytes_written == len); |
| 44 } |
| OLD | NEW |