| OLD | NEW |
| 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 #include "content/browser/zygote_host/zygote_host_impl_linux.h" | 5 #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
| 6 | 6 |
| 7 #include <errno.h> | |
| 8 #include <string.h> | |
| 9 #include <sys/socket.h> | |
| 10 #include <sys/stat.h> | |
| 11 #include <sys/types.h> | |
| 12 #include <unistd.h> | |
| 13 | |
| 14 #include "base/base_switches.h" | |
| 15 #include "base/command_line.h" | |
| 16 #include "base/environment.h" | |
| 17 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 18 #include "base/files/file_util.h" | 8 #include "base/process/kill.h" |
| 19 #include "base/files/scoped_file.h" | |
| 20 #include "base/linux_util.h" | |
| 21 #include "base/logging.h" | |
| 22 #include "base/memory/linked_ptr.h" | |
| 23 #include "base/memory/scoped_ptr.h" | |
| 24 #include "base/metrics/histogram.h" | |
| 25 #include "base/metrics/sparse_histogram.h" | |
| 26 #include "base/path_service.h" | |
| 27 #include "base/posix/eintr_wrapper.h" | |
| 28 #include "base/posix/unix_domain_socket_linux.h" | |
| 29 #include "base/process/launch.h" | |
| 30 #include "base/process/memory.h" | 9 #include "base/process/memory.h" |
| 31 #include "base/process/process_handle.h" | |
| 32 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 33 #include "base/strings/string_util.h" | |
| 34 #include "base/strings/utf_string_conversions.h" | |
| 35 #include "base/time/time.h" | |
| 36 #include "content/browser/renderer_host/render_sandbox_host_linux.h" | |
| 37 #include "content/common/child_process_sandbox_support_impl_linux.h" | |
| 38 #include "content/common/zygote_commands_linux.h" | |
| 39 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
| 40 #include "content/public/common/content_switches.h" | |
| 41 #include "content/public/common/result_codes.h" | |
| 42 #include "sandbox/linux/services/credentials.h" | |
| 43 #include "sandbox/linux/services/namespace_sandbox.h" | |
| 44 #include "sandbox/linux/services/namespace_utils.h" | |
| 45 #include "sandbox/linux/suid/client/setuid_sandbox_host.h" | |
| 46 #include "sandbox/linux/suid/common/sandbox.h" | 12 #include "sandbox/linux/suid/common/sandbox.h" |
| 47 #include "ui/base/ui_base_switches.h" | |
| 48 #include "ui/gfx/switches.h" | |
| 49 | 13 |
| 50 #if defined(USE_TCMALLOC) | 14 #if defined(USE_TCMALLOC) |
| 51 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" | 15 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| 52 #endif | 16 #endif |
| 53 | 17 |
| 54 namespace content { | 18 namespace content { |
| 55 | 19 |
| 56 namespace { | |
| 57 | |
| 58 // Receive a fixed message on fd and return the sender's PID. | |
| 59 // Returns true if the message received matches the expected message. | |
| 60 bool ReceiveFixedMessage(int fd, | |
| 61 const char* expect_msg, | |
| 62 size_t expect_len, | |
| 63 base::ProcessId* sender_pid) { | |
| 64 char buf[expect_len + 1]; | |
| 65 std::vector<base::ScopedFD> fds_vec; | |
| 66 | |
| 67 const ssize_t len = base::UnixDomainSocket::RecvMsgWithPid( | |
| 68 fd, buf, sizeof(buf), &fds_vec, sender_pid); | |
| 69 if (static_cast<size_t>(len) != expect_len) | |
| 70 return false; | |
| 71 if (memcmp(buf, expect_msg, expect_len) != 0) | |
| 72 return false; | |
| 73 if (!fds_vec.empty()) | |
| 74 return false; | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 // static | 20 // static |
| 81 ZygoteHost* ZygoteHost::GetInstance() { | 21 ZygoteHost* ZygoteHost::GetInstance() { |
| 82 return ZygoteHostImpl::GetInstance(); | 22 return ZygoteHostImpl::GetInstance(); |
| 83 } | 23 } |
| 84 | 24 |
| 85 ZygoteHostImpl::ZygoteHostImpl() | 25 ZygoteHostImpl::ZygoteHostImpl() |
| 86 : control_fd_(-1), | 26 : use_suid_sandbox_for_adj_oom_score_(false), |
| 87 control_lock_(), | |
| 88 pid_(-1), | |
| 89 init_(false), | |
| 90 use_suid_sandbox_for_adj_oom_score_(false), | |
| 91 sandbox_binary_(), | 27 sandbox_binary_(), |
| 92 have_read_sandbox_status_word_(false), | 28 zygote_pids_lock_(), |
| 93 sandbox_status_(0), | 29 zygote_pids_() {} |
| 94 child_tracking_lock_(), | |
| 95 list_of_running_zygote_children_(), | |
| 96 should_teardown_after_last_child_exits_(false) {} | |
| 97 | 30 |
| 98 ZygoteHostImpl::~ZygoteHostImpl() { TearDown(); } | 31 ZygoteHostImpl::~ZygoteHostImpl() {} |
| 99 | 32 |
| 100 // static | 33 // static |
| 101 ZygoteHostImpl* ZygoteHostImpl::GetInstance() { | 34 ZygoteHostImpl* ZygoteHostImpl::GetInstance() { |
| 102 return base::Singleton<ZygoteHostImpl>::get(); | 35 return base::Singleton<ZygoteHostImpl>::get(); |
| 103 } | 36 } |
| 104 | 37 |
| 105 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) { | 38 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) { |
| 106 DCHECK(!init_); | 39 sandbox_binary_ = sandbox_cmd; |
| 107 init_ = true; | |
| 108 | |
| 109 base::FilePath chrome_path; | |
| 110 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); | |
| 111 base::CommandLine cmd_line(chrome_path); | |
| 112 | |
| 113 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kZygoteProcess); | |
| 114 | |
| 115 int fds[2]; | |
| 116 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
| 117 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(fds[0])); | |
| 118 base::FileHandleMappingVector fds_to_map; | |
| 119 fds_to_map.push_back(std::make_pair(fds[1], kZygoteSocketPairFd)); | |
| 120 | |
| 121 base::LaunchOptions options; | |
| 122 const base::CommandLine& browser_command_line = | |
| 123 *base::CommandLine::ForCurrentProcess(); | |
| 124 if (browser_command_line.HasSwitch(switches::kZygoteCmdPrefix)) { | |
| 125 cmd_line.PrependWrapper( | |
| 126 browser_command_line.GetSwitchValueNative(switches::kZygoteCmdPrefix)); | |
| 127 } | |
| 128 // Append any switches from the browser process that need to be forwarded on | |
| 129 // to the zygote/renderers. | |
| 130 // Should this list be obtained from browser_render_process_host.cc? | |
| 131 static const char* kForwardSwitches[] = { | |
| 132 switches::kAllowSandboxDebugging, | |
| 133 switches::kDisableSeccompFilterSandbox, | |
| 134 switches::kEnableHeapProfiling, | |
| 135 switches::kEnableLogging, // Support, e.g., --enable-logging=stderr. | |
| 136 // Zygote process needs to know what resources to have loaded when it | |
| 137 // becomes a renderer process. | |
| 138 switches::kForceDeviceScaleFactor, | |
| 139 switches::kLoggingLevel, | |
| 140 switches::kNoSandbox, | |
| 141 switches::kPpapiInProcess, | |
| 142 switches::kRegisterPepperPlugins, | |
| 143 switches::kV, | |
| 144 switches::kVModule, | |
| 145 }; | |
| 146 cmd_line.CopySwitchesFrom(browser_command_line, kForwardSwitches, | |
| 147 arraysize(kForwardSwitches)); | |
| 148 | |
| 149 GetContentClient()->browser()->AppendExtraCommandLineSwitches(&cmd_line, -1); | |
| 150 | |
| 151 sandbox_binary_ = sandbox_cmd.c_str(); | |
| 152 | |
| 153 const bool using_namespace_sandbox = ShouldUseNamespaceSandbox(); | |
| 154 // A non empty sandbox_cmd means we want a SUID sandbox. | |
| 155 const bool using_suid_sandbox = | |
| 156 !sandbox_cmd.empty() && !using_namespace_sandbox; | |
| 157 | |
| 158 // Use the SUID sandbox for adjusting OOM scores when we are using the setuid | |
| 159 // or namespace sandbox. This is needed beacuse the processes are | |
| 160 // non-dumpable, so /proc/pid/oom_score_adj can only be written by root. | |
| 161 use_suid_sandbox_for_adj_oom_score_ = | |
| 162 !sandbox_binary_.empty() && | |
| 163 (using_namespace_sandbox || using_suid_sandbox); | |
| 164 | |
| 165 // Start up the sandbox host process and get the file descriptor for the | |
| 166 // renderers to talk to it. | |
| 167 const int sfd = RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); | |
| 168 fds_to_map.push_back(std::make_pair(sfd, GetSandboxFD())); | |
| 169 | |
| 170 base::ScopedFD dummy_fd; | |
| 171 if (using_suid_sandbox) { | |
| 172 scoped_ptr<sandbox::SetuidSandboxHost> sandbox_host( | |
| 173 sandbox::SetuidSandboxHost::Create()); | |
| 174 sandbox_host->PrependWrapper(&cmd_line); | |
| 175 sandbox_host->SetupLaunchOptions(&options, &fds_to_map, &dummy_fd); | |
| 176 sandbox_host->SetupLaunchEnvironment(); | |
| 177 } | |
| 178 | |
| 179 options.fds_to_remap = &fds_to_map; | |
| 180 base::Process process = | |
| 181 using_namespace_sandbox | |
| 182 ? sandbox::NamespaceSandbox::LaunchProcess(cmd_line, options) | |
| 183 : base::LaunchProcess(cmd_line, options); | |
| 184 CHECK(process.IsValid()) << "Failed to launch zygote process"; | |
| 185 | |
| 186 dummy_fd.reset(); | |
| 187 | |
| 188 if (using_suid_sandbox || using_namespace_sandbox) { | |
| 189 // The SUID sandbox will execute the zygote in a new PID namespace, and | |
| 190 // the main zygote process will then fork from there. Watch now our | |
| 191 // elaborate dance to find and validate the zygote's PID. | |
| 192 | |
| 193 // First we receive a message from the zygote boot process. | |
| 194 base::ProcessId boot_pid; | |
| 195 CHECK(ReceiveFixedMessage( | |
| 196 fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid)); | |
| 197 | |
| 198 // Within the PID namespace, the zygote boot process thinks it's PID 1, | |
| 199 // but its real PID can never be 1. This gives us a reliable test that | |
| 200 // the kernel is translating the sender's PID to our namespace. | |
| 201 CHECK_GT(boot_pid, 1) | |
| 202 << "Received invalid process ID for zygote; kernel might be too old? " | |
| 203 "See crbug.com/357670 or try using --" | |
| 204 << switches::kDisableSetuidSandbox << " to workaround."; | |
| 205 | |
| 206 // Now receive the message that the zygote's ready to go, along with the | |
| 207 // main zygote process's ID. | |
| 208 CHECK(ReceiveFixedMessage( | |
| 209 fds[0], kZygoteHelloMessage, sizeof(kZygoteHelloMessage), &pid_)); | |
| 210 CHECK_GT(pid_, 1); | |
| 211 | |
| 212 if (process.Pid() != pid_) { | |
| 213 // Reap the sandbox. | |
| 214 base::EnsureProcessGetsReaped(process.Pid()); | |
| 215 } | |
| 216 } else { | |
| 217 // Not using the SUID sandbox. | |
| 218 // Note that ~base::Process() will reset the internal value, but there's no | |
| 219 // real "handle" on POSIX so that is safe. | |
| 220 pid_ = process.Pid(); | |
| 221 } | |
| 222 | |
| 223 close(fds[1]); | |
| 224 control_fd_ = fds[0]; | |
| 225 | |
| 226 base::Pickle pickle; | |
| 227 pickle.WriteInt(kZygoteCommandGetSandboxStatus); | |
| 228 if (!SendMessage(pickle, NULL)) | |
| 229 LOG(FATAL) << "Cannot communicate with zygote"; | |
| 230 // We don't wait for the reply. We'll read it in ReadReply. | |
| 231 } | 40 } |
| 232 | 41 |
| 233 void ZygoteHostImpl::TearDownAfterLastChild() { | 42 void ZygoteHostImpl::AddZygotePid(pid_t pid) { |
| 234 bool do_teardown = false; | 43 base::AutoLock lock(zygote_pids_lock_); |
| 235 { | 44 zygote_pids_.insert(pid); |
| 236 base::AutoLock lock(child_tracking_lock_); | |
| 237 should_teardown_after_last_child_exits_ = true; | |
| 238 do_teardown = list_of_running_zygote_children_.empty(); | |
| 239 } | |
| 240 if (do_teardown) { | |
| 241 TearDown(); | |
| 242 } | |
| 243 } | 45 } |
| 244 | 46 |
| 245 // Note: this is also called from the destructor. | 47 bool ZygoteHostImpl::IsZygotePid(pid_t pid) { |
| 246 void ZygoteHostImpl::TearDown() { | 48 base::AutoLock lock(zygote_pids_lock_); |
| 247 base::AutoLock lock(control_lock_); | 49 return zygote_pids_.find(pid) != zygote_pids_.end(); |
| 248 if (control_fd_ > -1) { | |
| 249 // Closing the IPC channel will act as a notification to exit | |
| 250 // to the Zygote. | |
| 251 if (IGNORE_EINTR(close(control_fd_))) { | |
| 252 PLOG(ERROR) << "Could not close Zygote control channel."; | |
| 253 NOTREACHED(); | |
| 254 } | |
| 255 control_fd_ = -1; | |
| 256 } | |
| 257 } | 50 } |
| 258 | 51 |
| 259 void ZygoteHostImpl::ZygoteChildBorn(pid_t process) { | 52 const std::string& ZygoteHostImpl::SandboxCommand() const { |
| 260 base::AutoLock lock(child_tracking_lock_); | 53 return sandbox_binary_; |
| 261 bool new_element_inserted = | |
| 262 list_of_running_zygote_children_.insert(process).second; | |
| 263 DCHECK(new_element_inserted); | |
| 264 } | 54 } |
| 265 | 55 |
| 266 void ZygoteHostImpl::ZygoteChildDied(pid_t process) { | 56 void ZygoteHostImpl::SetRendererSandboxStatus(int status) { |
| 267 bool do_teardown = false; | 57 renderer_sandbox_status_ = status; |
| 268 { | |
| 269 base::AutoLock lock(child_tracking_lock_); | |
| 270 size_t num_erased = list_of_running_zygote_children_.erase(process); | |
| 271 DCHECK_EQ(1U, num_erased); | |
| 272 do_teardown = should_teardown_after_last_child_exits_ && | |
| 273 list_of_running_zygote_children_.empty(); | |
| 274 } | |
| 275 if (do_teardown) { | |
| 276 TearDown(); | |
| 277 } | |
| 278 } | 58 } |
| 279 | 59 |
| 280 bool ZygoteHostImpl::SendMessage(const base::Pickle& data, | 60 int ZygoteHostImpl::GetRendererSandboxStatus() const { |
| 281 const std::vector<int>* fds) { | 61 return renderer_sandbox_status_; |
| 282 DCHECK_NE(-1, control_fd_); | |
| 283 CHECK(data.size() <= kZygoteMaxMessageLength) | |
| 284 << "Trying to send too-large message to zygote (sending " << data.size() | |
| 285 << " bytes, max is " << kZygoteMaxMessageLength << ")"; | |
| 286 CHECK(!fds || fds->size() <= base::UnixDomainSocket::kMaxFileDescriptors) | |
| 287 << "Trying to send message with too many file descriptors to zygote " | |
| 288 << "(sending " << fds->size() << ", max is " | |
| 289 << base::UnixDomainSocket::kMaxFileDescriptors << ")"; | |
| 290 | |
| 291 return base::UnixDomainSocket::SendMsg(control_fd_, | |
| 292 data.data(), data.size(), | |
| 293 fds ? *fds : std::vector<int>()); | |
| 294 } | |
| 295 | |
| 296 ssize_t ZygoteHostImpl::ReadReply(void* buf, size_t buf_len) { | |
| 297 DCHECK_NE(-1, control_fd_); | |
| 298 // At startup we send a kZygoteCommandGetSandboxStatus request to the zygote, | |
| 299 // but don't wait for the reply. Thus, the first time that we read from the | |
| 300 // zygote, we get the reply to that request. | |
| 301 if (!have_read_sandbox_status_word_) { | |
| 302 if (HANDLE_EINTR(read(control_fd_, &sandbox_status_, | |
| 303 sizeof(sandbox_status_))) != | |
| 304 sizeof(sandbox_status_)) { | |
| 305 return -1; | |
| 306 } | |
| 307 | |
| 308 have_read_sandbox_status_word_ = true; | |
| 309 UMA_HISTOGRAM_SPARSE_SLOWLY("Linux.SandboxStatus", sandbox_status_); | |
| 310 } | |
| 311 | |
| 312 return HANDLE_EINTR(read(control_fd_, buf, buf_len)); | |
| 313 } | |
| 314 | |
| 315 pid_t ZygoteHostImpl::ForkRequest(const std::vector<std::string>& argv, | |
| 316 scoped_ptr<FileDescriptorInfo> mapping, | |
| 317 const std::string& process_type) { | |
| 318 DCHECK(init_); | |
| 319 base::Pickle pickle; | |
| 320 | |
| 321 int raw_socks[2]; | |
| 322 PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks)); | |
| 323 base::ScopedFD my_sock(raw_socks[0]); | |
| 324 base::ScopedFD peer_sock(raw_socks[1]); | |
| 325 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(my_sock.get())); | |
| 326 | |
| 327 pickle.WriteInt(kZygoteCommandFork); | |
| 328 pickle.WriteString(process_type); | |
| 329 pickle.WriteInt(argv.size()); | |
| 330 for (std::vector<std::string>::const_iterator | |
| 331 i = argv.begin(); i != argv.end(); ++i) | |
| 332 pickle.WriteString(*i); | |
| 333 | |
| 334 // Fork requests contain one file descriptor for the PID oracle, and one | |
| 335 // more for each file descriptor mapping for the child process. | |
| 336 const size_t num_fds_to_send = 1 + mapping->GetMappingSize(); | |
| 337 pickle.WriteInt(num_fds_to_send); | |
| 338 | |
| 339 std::vector<int> fds; | |
| 340 | |
| 341 // First FD to send is peer_sock. | |
| 342 // TODO(morrita): Ideally, this should be part of the mapping so that | |
| 343 // FileDescriptorInfo can manages its lifetime. | |
| 344 fds.push_back(peer_sock.get()); | |
| 345 | |
| 346 // The rest come from mapping. | |
| 347 for (size_t i = 0; i < mapping->GetMappingSize(); ++i) { | |
| 348 pickle.WriteUInt32(mapping->GetIDAt(i)); | |
| 349 fds.push_back(mapping->GetFDAt(i)); | |
| 350 } | |
| 351 | |
| 352 // Sanity check that we've populated |fds| correctly. | |
| 353 DCHECK_EQ(num_fds_to_send, fds.size()); | |
| 354 | |
| 355 pid_t pid; | |
| 356 { | |
| 357 base::AutoLock lock(control_lock_); | |
| 358 if (!SendMessage(pickle, &fds)) | |
| 359 return base::kNullProcessHandle; | |
| 360 mapping.reset(); | |
| 361 peer_sock.reset(); | |
| 362 | |
| 363 { | |
| 364 char buf[sizeof(kZygoteChildPingMessage) + 1]; | |
| 365 std::vector<base::ScopedFD> recv_fds; | |
| 366 base::ProcessId real_pid; | |
| 367 | |
| 368 ssize_t n = base::UnixDomainSocket::RecvMsgWithPid( | |
| 369 my_sock.get(), buf, sizeof(buf), &recv_fds, &real_pid); | |
| 370 if (n != sizeof(kZygoteChildPingMessage) || | |
| 371 0 != memcmp(buf, | |
| 372 kZygoteChildPingMessage, | |
| 373 sizeof(kZygoteChildPingMessage))) { | |
| 374 // Zygote children should still be trustworthy when they're supposed to | |
| 375 // ping us, so something's broken if we don't receive a valid ping. | |
| 376 LOG(ERROR) << "Did not receive ping from zygote child"; | |
| 377 NOTREACHED(); | |
| 378 real_pid = -1; | |
| 379 } | |
| 380 my_sock.reset(); | |
| 381 | |
| 382 // Always send PID back to zygote. | |
| 383 base::Pickle pid_pickle; | |
| 384 pid_pickle.WriteInt(kZygoteCommandForkRealPID); | |
| 385 pid_pickle.WriteInt(real_pid); | |
| 386 if (!SendMessage(pid_pickle, NULL)) | |
| 387 return base::kNullProcessHandle; | |
| 388 } | |
| 389 | |
| 390 // Read the reply, which pickles the PID and an optional UMA enumeration. | |
| 391 static const unsigned kMaxReplyLength = 2048; | |
| 392 char buf[kMaxReplyLength]; | |
| 393 const ssize_t len = ReadReply(buf, sizeof(buf)); | |
| 394 | |
| 395 base::Pickle reply_pickle(buf, len); | |
| 396 base::PickleIterator iter(reply_pickle); | |
| 397 if (len <= 0 || !iter.ReadInt(&pid)) | |
| 398 return base::kNullProcessHandle; | |
| 399 | |
| 400 // If there is a nonempty UMA name string, then there is a UMA | |
| 401 // enumeration to record. | |
| 402 std::string uma_name; | |
| 403 int uma_sample; | |
| 404 int uma_boundary_value; | |
| 405 if (iter.ReadString(&uma_name) && | |
| 406 !uma_name.empty() && | |
| 407 iter.ReadInt(&uma_sample) && | |
| 408 iter.ReadInt(&uma_boundary_value)) { | |
| 409 // We cannot use the UMA_HISTOGRAM_ENUMERATION macro here, | |
| 410 // because that's only for when the name is the same every time. | |
| 411 // Here we're using whatever name we got from the other side. | |
| 412 // But since it's likely that the same one will be used repeatedly | |
| 413 // (even though it's not guaranteed), we cache it here. | |
| 414 static base::HistogramBase* uma_histogram; | |
| 415 if (!uma_histogram || uma_histogram->histogram_name() != uma_name) { | |
| 416 uma_histogram = base::LinearHistogram::FactoryGet( | |
| 417 uma_name, 1, | |
| 418 uma_boundary_value, | |
| 419 uma_boundary_value + 1, | |
| 420 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 421 } | |
| 422 uma_histogram->Add(uma_sample); | |
| 423 } | |
| 424 | |
| 425 if (pid <= 0) | |
| 426 return base::kNullProcessHandle; | |
| 427 } | |
| 428 | |
| 429 #if !defined(OS_OPENBSD) | |
| 430 // This is just a starting score for a renderer or extension (the | |
| 431 // only types of processes that will be started this way). It will | |
| 432 // get adjusted as time goes on. (This is the same value as | |
| 433 // chrome::kLowestRendererOomScore in chrome/chrome_constants.h, but | |
| 434 // that's not something we can include here.) | |
| 435 const int kLowestRendererOomScore = 300; | |
| 436 AdjustRendererOOMScore(pid, kLowestRendererOomScore); | |
| 437 #endif | |
| 438 | |
| 439 ZygoteChildBorn(pid); | |
| 440 return pid; | |
| 441 } | 62 } |
| 442 | 63 |
| 443 #if !defined(OS_OPENBSD) | 64 #if !defined(OS_OPENBSD) |
| 444 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid, | 65 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid, |
| 445 int score) { | 66 int score) { |
| 446 // 1) You can't change the oom_score_adj of a non-dumpable process | 67 // 1) You can't change the oom_score_adj of a non-dumpable process |
| 447 // (EPERM) unless you're root. Because of this, we can't set the | 68 // (EPERM) unless you're root. Because of this, we can't set the |
| 448 // oom_adj from the browser process. | 69 // oom_adj from the browser process. |
| 449 // | 70 // |
| 450 // 2) We can't set the oom_score_adj before entering the sandbox | 71 // 2) We can't set the oom_score_adj before entering the sandbox |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 base::LaunchProcess(adj_oom_score_cmdline, options); | 127 base::LaunchProcess(adj_oom_score_cmdline, options); |
| 507 if (sandbox_helper_process.IsValid()) | 128 if (sandbox_helper_process.IsValid()) |
| 508 base::EnsureProcessGetsReaped(sandbox_helper_process.Pid()); | 129 base::EnsureProcessGetsReaped(sandbox_helper_process.Pid()); |
| 509 } else if (!use_suid_sandbox_for_adj_oom_score_) { | 130 } else if (!use_suid_sandbox_for_adj_oom_score_) { |
| 510 if (!base::AdjustOOMScore(pid, score)) | 131 if (!base::AdjustOOMScore(pid, score)) |
| 511 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; | 132 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; |
| 512 } | 133 } |
| 513 } | 134 } |
| 514 #endif | 135 #endif |
| 515 | 136 |
| 516 void ZygoteHostImpl::EnsureProcessTerminated(pid_t process) { | |
| 517 DCHECK(init_); | |
| 518 base::Pickle pickle; | |
| 519 | |
| 520 pickle.WriteInt(kZygoteCommandReap); | |
| 521 pickle.WriteInt(process); | |
| 522 if (!SendMessage(pickle, NULL)) | |
| 523 LOG(ERROR) << "Failed to send Reap message to zygote"; | |
| 524 ZygoteChildDied(process); | |
| 525 } | |
| 526 | |
| 527 base::TerminationStatus ZygoteHostImpl::GetTerminationStatus( | |
| 528 base::ProcessHandle handle, | |
| 529 bool known_dead, | |
| 530 int* exit_code) { | |
| 531 DCHECK(init_); | |
| 532 base::Pickle pickle; | |
| 533 pickle.WriteInt(kZygoteCommandGetTerminationStatus); | |
| 534 pickle.WriteBool(known_dead); | |
| 535 pickle.WriteInt(handle); | |
| 536 | |
| 537 static const unsigned kMaxMessageLength = 128; | |
| 538 char buf[kMaxMessageLength]; | |
| 539 ssize_t len; | |
| 540 { | |
| 541 base::AutoLock lock(control_lock_); | |
| 542 if (!SendMessage(pickle, NULL)) | |
| 543 LOG(ERROR) << "Failed to send GetTerminationStatus message to zygote"; | |
| 544 len = ReadReply(buf, sizeof(buf)); | |
| 545 } | |
| 546 | |
| 547 // Set this now to handle the error cases. | |
| 548 if (exit_code) | |
| 549 *exit_code = RESULT_CODE_NORMAL_EXIT; | |
| 550 int status = base::TERMINATION_STATUS_NORMAL_TERMINATION; | |
| 551 | |
| 552 if (len == -1) { | |
| 553 LOG(WARNING) << "Error reading message from zygote: " << errno; | |
| 554 } else if (len == 0) { | |
| 555 LOG(WARNING) << "Socket closed prematurely."; | |
| 556 } else { | |
| 557 base::Pickle read_pickle(buf, len); | |
| 558 int tmp_status, tmp_exit_code; | |
| 559 base::PickleIterator iter(read_pickle); | |
| 560 if (!iter.ReadInt(&tmp_status) || !iter.ReadInt(&tmp_exit_code)) { | |
| 561 LOG(WARNING) | |
| 562 << "Error parsing GetTerminationStatus response from zygote."; | |
| 563 } else { | |
| 564 if (exit_code) | |
| 565 *exit_code = tmp_exit_code; | |
| 566 status = tmp_status; | |
| 567 } | |
| 568 } | |
| 569 | |
| 570 if (status != base::TERMINATION_STATUS_STILL_RUNNING) { | |
| 571 ZygoteChildDied(handle); | |
| 572 } | |
| 573 return static_cast<base::TerminationStatus>(status); | |
| 574 } | |
| 575 | |
| 576 pid_t ZygoteHostImpl::GetPid() const { | |
| 577 return pid_; | |
| 578 } | |
| 579 | |
| 580 int ZygoteHostImpl::GetSandboxStatus() const { | |
| 581 if (have_read_sandbox_status_word_) | |
| 582 return sandbox_status_; | |
| 583 return 0; | |
| 584 } | |
| 585 | |
| 586 bool ZygoteHostImpl::ShouldUseNamespaceSandbox() { | |
| 587 const base::CommandLine& command_line = | |
| 588 *base::CommandLine::ForCurrentProcess(); | |
| 589 if (command_line.HasSwitch(switches::kNoSandbox)) { | |
| 590 return false; | |
| 591 } | |
| 592 | |
| 593 if (command_line.HasSwitch(switches::kDisableNamespaceSandbox)) { | |
| 594 return false; | |
| 595 } | |
| 596 | |
| 597 if (!sandbox::Credentials::CanCreateProcessInNewUserNS()) { | |
| 598 return false; | |
| 599 } | |
| 600 | |
| 601 return true; | |
| 602 } | |
| 603 | |
| 604 } // namespace content | 137 } // namespace content |
| OLD | NEW |