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