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

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

Issue 2419383008: Close the file handle before exiting the function (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698