Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: sandbox/linux/suid/process_util_linux.c

Issue 7708020: Trying again to land OOM priority manager changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moving switch closer to use location Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Ranges for the current (oom_score_adj) and previous (oom_adj)
23 // flavors of OOM score.
24 static const int kMaxOomScore = 1000;
25 static const int kMaxOldOomScore = 15;
26
22 bool AdjustOOMScore(pid_t process, int score) { 27 bool AdjustOOMScore(pid_t process, int score) {
23 if (score < 0 || score > 15) 28 if (score < 0 || score > kMaxOomScore)
24 return false; 29 return false;
25 30
26 char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0" 31 char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0"
27 // 6 + 20 + 1 = 27 32 // 6 + 20 + 1 = 27
28 snprintf(oom_adj, sizeof(oom_adj), "/proc/%" PRIdMAX, (intmax_t)process); 33 snprintf(oom_adj, sizeof(oom_adj), "/proc/%" PRIdMAX, (intmax_t)process);
29 34
30 const int dirfd = open(oom_adj, O_RDONLY | O_DIRECTORY); 35 const int dirfd = open(oom_adj, O_RDONLY | O_DIRECTORY);
31 if (dirfd < 0) 36 if (dirfd < 0)
32 return false; 37 return false;
33 38
34 struct stat statbuf; 39 struct stat statbuf;
35 if (fstat(dirfd, &statbuf) < 0) { 40 if (fstat(dirfd, &statbuf) < 0) {
36 close(dirfd); 41 close(dirfd);
37 return false; 42 return false;
38 } 43 }
39 if (getuid() != statbuf.st_uid) { 44 if (getuid() != statbuf.st_uid) {
40 close(dirfd); 45 close(dirfd);
41 return false; 46 return false;
42 } 47 }
43 48
44 const int fd = openat(dirfd, "oom_adj", O_WRONLY); 49 int fd = openat(dirfd, "oom_score_adj", O_WRONLY);
50 if (fd < 0) {
51 // We failed to open oom_score_adj, so let's try for the older
52 // oom_adj file instead.
53 fd = openat(dirfd, "oom_adj", O_WRONLY);
54 if (fd < 0) {
55 // Nope, that doesn't work either.
56 return false;
57 } else {
58 // If we're using the old oom_adj file, the allowed range is now
59 // [0, kMaxOldOomScore], so we scale the score. This may result in some
60 // aliasing of values, of course.
61 score = score * kMaxOldOomScore / kMaxOomScore;
62 }
63 }
45 close(dirfd); 64 close(dirfd);
46 65
47 if (fd < 0) 66 char buf[11]; // 0 <= |score| <= kMaxOomScore; using log_10(2**32) + 1 size
48 return false;
49
50 char buf[3]; // 0 <= |score| <= 15;
51 snprintf(buf, sizeof(buf), "%d", score); 67 snprintf(buf, sizeof(buf), "%d", score);
52 size_t len = strlen(buf); 68 size_t len = strlen(buf);
53 69
54 ssize_t bytes_written = write(fd, buf, len); 70 ssize_t bytes_written = write(fd, buf, len);
55 close(fd); 71 close(fd);
56 return (bytes_written == len); 72 return (bytes_written == len);
57 } 73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698