Hover effects in views should not be invoked when mouse events are disabled
[chromium-blink-merge.git] / base / process_util_linux.cc
blob331580123a043d9d8df6f7befe5ce0d244b0fb3e
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
3 // found in the LICENSE file.
5 #include "base/process_util.h"
7 #include <dirent.h>
8 #include <malloc.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <unistd.h>
13 #include "base/file_util.h"
14 #include "base/logging.h"
15 #include "base/process/internal_linux.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/sys_info.h"
20 #include "base/threading/thread_restrictions.h"
22 namespace base {
24 #if defined(USE_LINUX_BREAKPAD)
25 size_t g_oom_size = 0U;
26 #endif
28 const char kProcSelfExe[] = "/proc/self/exe";
30 ProcessId GetParentProcessId(ProcessHandle process) {
31 ProcessId pid =
32 internal::ReadProcStatsAndGetFieldAsInt(process, internal::VM_PPID);
33 if (pid)
34 return pid;
35 return -1;
38 FilePath GetProcessExecutablePath(ProcessHandle process) {
39 FilePath stat_file = internal::GetProcPidDir(process).Append("exe");
40 FilePath exe_name;
41 if (!file_util::ReadSymbolicLink(stat_file, &exe_name)) {
42 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses
43 return FilePath();
45 return exe_name;
48 int GetNumberOfThreads(ProcessHandle process) {
49 return internal::ReadProcStatsAndGetFieldAsInt(process,
50 internal::VM_NUMTHREADS);
53 namespace {
55 void OnNoMemorySize(size_t size) {
56 #if defined(USE_LINUX_BREAKPAD)
57 g_oom_size = size;
58 #endif
60 if (size != 0)
61 LOG(FATAL) << "Out of memory, size = " << size;
62 LOG(FATAL) << "Out of memory.";
65 void OnNoMemory() {
66 OnNoMemorySize(0);
69 } // namespace
71 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \
72 !defined(THREAD_SANITIZER)
74 #if defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
76 extern "C" {
77 void* __libc_malloc(size_t size);
78 void* __libc_realloc(void* ptr, size_t size);
79 void* __libc_calloc(size_t nmemb, size_t size);
80 void* __libc_valloc(size_t size);
81 void* __libc_pvalloc(size_t size);
82 void* __libc_memalign(size_t alignment, size_t size);
84 // Overriding the system memory allocation functions:
86 // For security reasons, we want malloc failures to be fatal. Too much code
87 // doesn't check for a NULL return value from malloc and unconditionally uses
88 // the resulting pointer. If the first offset that they try to access is
89 // attacker controlled, then the attacker can direct the code to access any
90 // part of memory.
92 // Thus, we define all the standard malloc functions here and mark them as
93 // visibility 'default'. This means that they replace the malloc functions for
94 // all Chromium code and also for all code in shared libraries. There are tests
95 // for this in process_util_unittest.cc.
97 // If we are using tcmalloc, then the problem is moot since tcmalloc handles
98 // this for us. Thus this code is in a !defined(USE_TCMALLOC) block.
100 // If we are testing the binary with AddressSanitizer, we should not
101 // redefine malloc and let AddressSanitizer do it instead.
103 // We call the real libc functions in this code by using __libc_malloc etc.
104 // Previously we tried using dlsym(RTLD_NEXT, ...) but that failed depending on
105 // the link order. Since ld.so needs calloc during symbol resolution, it
106 // defines its own versions of several of these functions in dl-minimal.c.
107 // Depending on the runtime library order, dlsym ended up giving us those
108 // functions and bad things happened. See crbug.com/31809
110 // This means that any code which calls __libc_* gets the raw libc versions of
111 // these functions.
113 #define DIE_ON_OOM_1(function_name) \
114 void* function_name(size_t) __attribute__ ((visibility("default"))); \
116 void* function_name(size_t size) { \
117 void* ret = __libc_##function_name(size); \
118 if (ret == NULL && size != 0) \
119 OnNoMemorySize(size); \
120 return ret; \
123 #define DIE_ON_OOM_2(function_name, arg1_type) \
124 void* function_name(arg1_type, size_t) \
125 __attribute__ ((visibility("default"))); \
127 void* function_name(arg1_type arg1, size_t size) { \
128 void* ret = __libc_##function_name(arg1, size); \
129 if (ret == NULL && size != 0) \
130 OnNoMemorySize(size); \
131 return ret; \
134 DIE_ON_OOM_1(malloc)
135 DIE_ON_OOM_1(valloc)
136 DIE_ON_OOM_1(pvalloc)
138 DIE_ON_OOM_2(calloc, size_t)
139 DIE_ON_OOM_2(realloc, void*)
140 DIE_ON_OOM_2(memalign, size_t)
142 // posix_memalign has a unique signature and doesn't have a __libc_ variant.
143 int posix_memalign(void** ptr, size_t alignment, size_t size)
144 __attribute__ ((visibility("default")));
146 int posix_memalign(void** ptr, size_t alignment, size_t size) {
147 // This will use the safe version of memalign, above.
148 *ptr = memalign(alignment, size);
149 return 0;
152 } // extern C
154 #else
156 // TODO(mostynb@opera.com): dlsym dance
158 #endif // LIBC_GLIBC && !USE_TCMALLOC
160 #endif // !*_SANITIZER
162 void EnableTerminationOnHeapCorruption() {
163 // On Linux, there nothing to do AFAIK.
166 void EnableTerminationOnOutOfMemory() {
167 #if defined(OS_ANDROID)
168 // Android doesn't support setting a new handler.
169 DLOG(WARNING) << "Not feasible.";
170 #else
171 // Set the new-out of memory handler.
172 std::set_new_handler(&OnNoMemory);
173 // If we're using glibc's allocator, the above functions will override
174 // malloc and friends and make them die on out of memory.
175 #endif
178 // NOTE: This is not the only version of this function in the source:
179 // the setuid sandbox (in process_util_linux.c, in the sandbox source)
180 // also has its own C version.
181 bool AdjustOOMScore(ProcessId process, int score) {
182 if (score < 0 || score > kMaxOomScore)
183 return false;
185 FilePath oom_path(internal::GetProcPidDir(process));
187 // Attempt to write the newer oom_score_adj file first.
188 FilePath oom_file = oom_path.AppendASCII("oom_score_adj");
189 if (file_util::PathExists(oom_file)) {
190 std::string score_str = IntToString(score);
191 DVLOG(1) << "Adjusting oom_score_adj of " << process << " to "
192 << score_str;
193 int score_len = static_cast<int>(score_str.length());
194 return (score_len == file_util::WriteFile(oom_file,
195 score_str.c_str(),
196 score_len));
199 // If the oom_score_adj file doesn't exist, then we write the old
200 // style file and translate the oom_adj score to the range 0-15.
201 oom_file = oom_path.AppendASCII("oom_adj");
202 if (file_util::PathExists(oom_file)) {
203 // Max score for the old oom_adj range. Used for conversion of new
204 // values to old values.
205 const int kMaxOldOomScore = 15;
207 int converted_score = score * kMaxOldOomScore / kMaxOomScore;
208 std::string score_str = IntToString(converted_score);
209 DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str;
210 int score_len = static_cast<int>(score_str.length());
211 return (score_len == file_util::WriteFile(oom_file,
212 score_str.c_str(),
213 score_len));
216 return false;
219 } // namespace base