[Mac] Implement Ambient Light API
[chromium-blink-merge.git] / content / browser / zygote_host / zygote_host_impl_linux.cc
blob2a2339bf03bd314b7bd50562d60629c17230a201
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 "content/browser/zygote_host/zygote_host_impl_linux.h"
7 #include <string.h>
8 #include <sys/socket.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
13 #include "base/base_switches.h"
14 #include "base/command_line.h"
15 #include "base/environment.h"
16 #include "base/files/file_enumerator.h"
17 #include "base/files/file_util.h"
18 #include "base/files/scoped_file.h"
19 #include "base/linux_util.h"
20 #include "base/logging.h"
21 #include "base/memory/linked_ptr.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/scoped_vector.h"
24 #include "base/metrics/histogram.h"
25 #include "base/path_service.h"
26 #include "base/posix/eintr_wrapper.h"
27 #include "base/posix/unix_domain_socket_linux.h"
28 #include "base/process/launch.h"
29 #include "base/process/memory.h"
30 #include "base/process/process_handle.h"
31 #include "base/strings/string_number_conversions.h"
32 #include "base/strings/string_util.h"
33 #include "base/strings/utf_string_conversions.h"
34 #include "base/time/time.h"
35 #include "content/browser/renderer_host/render_sandbox_host_linux.h"
36 #include "content/common/child_process_sandbox_support_impl_linux.h"
37 #include "content/common/zygote_commands_linux.h"
38 #include "content/public/browser/content_browser_client.h"
39 #include "content/public/common/content_switches.h"
40 #include "content/public/common/result_codes.h"
41 #include "sandbox/linux/suid/client/setuid_sandbox_client.h"
42 #include "sandbox/linux/suid/common/sandbox.h"
43 #include "ui/base/ui_base_switches.h"
44 #include "ui/gfx/switches.h"
46 #if defined(USE_TCMALLOC)
47 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
48 #endif
50 namespace content {
52 // Receive a fixed message on fd and return the sender's PID.
53 // Returns true if the message received matches the expected message.
54 static bool ReceiveFixedMessage(int fd,
55 const char* expect_msg,
56 size_t expect_len,
57 base::ProcessId* sender_pid) {
58 char buf[expect_len + 1];
59 ScopedVector<base::ScopedFD> fds_vec;
61 const ssize_t len = UnixDomainSocket::RecvMsgWithPid(
62 fd, buf, sizeof(buf), &fds_vec, sender_pid);
63 if (static_cast<size_t>(len) != expect_len)
64 return false;
65 if (memcmp(buf, expect_msg, expect_len) != 0)
66 return false;
67 if (!fds_vec.empty())
68 return false;
69 return true;
72 // static
73 ZygoteHost* ZygoteHost::GetInstance() {
74 return ZygoteHostImpl::GetInstance();
77 ZygoteHostImpl::ZygoteHostImpl()
78 : control_fd_(-1),
79 control_lock_(),
80 pid_(-1),
81 init_(false),
82 using_suid_sandbox_(false),
83 sandbox_binary_(),
84 have_read_sandbox_status_word_(false),
85 sandbox_status_(0),
86 child_tracking_lock_(),
87 list_of_running_zygote_children_(),
88 should_teardown_after_last_child_exits_(false) {}
90 ZygoteHostImpl::~ZygoteHostImpl() { TearDown(); }
92 // static
93 ZygoteHostImpl* ZygoteHostImpl::GetInstance() {
94 return Singleton<ZygoteHostImpl>::get();
97 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) {
98 DCHECK(!init_);
99 init_ = true;
101 base::FilePath chrome_path;
102 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
103 base::CommandLine cmd_line(chrome_path);
105 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kZygoteProcess);
107 int fds[2];
108 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0);
109 CHECK(UnixDomainSocket::EnableReceiveProcessId(fds[0]));
110 base::FileHandleMappingVector fds_to_map;
111 fds_to_map.push_back(std::make_pair(fds[1], kZygoteSocketPairFd));
113 base::LaunchOptions options;
114 const base::CommandLine& browser_command_line =
115 *base::CommandLine::ForCurrentProcess();
116 if (browser_command_line.HasSwitch(switches::kZygoteCmdPrefix)) {
117 cmd_line.PrependWrapper(
118 browser_command_line.GetSwitchValueNative(switches::kZygoteCmdPrefix));
120 // Append any switches from the browser process that need to be forwarded on
121 // to the zygote/renderers.
122 // Should this list be obtained from browser_render_process_host.cc?
123 static const char* kForwardSwitches[] = {
124 switches::kAllowSandboxDebugging,
125 switches::kDisableSeccompFilterSandbox,
126 switches::kEnableLogging, // Support, e.g., --enable-logging=stderr.
127 // Zygote process needs to know what resources to have loaded when it
128 // becomes a renderer process.
129 switches::kForceDeviceScaleFactor,
130 switches::kLoggingLevel,
131 switches::kNoSandbox,
132 switches::kPpapiInProcess,
133 switches::kRegisterPepperPlugins,
134 switches::kV,
135 switches::kVModule,
137 cmd_line.CopySwitchesFrom(browser_command_line, kForwardSwitches,
138 arraysize(kForwardSwitches));
140 GetContentClient()->browser()->AppendExtraCommandLineSwitches(&cmd_line, -1);
142 sandbox_binary_ = sandbox_cmd.c_str();
144 // A non empty sandbox_cmd means we want a SUID sandbox.
145 using_suid_sandbox_ = !sandbox_cmd.empty();
147 // Start up the sandbox host process and get the file descriptor for the
148 // renderers to talk to it.
149 const int sfd = RenderSandboxHostLinux::GetInstance()->GetRendererSocket();
150 fds_to_map.push_back(std::make_pair(sfd, GetSandboxFD()));
152 base::ScopedFD dummy_fd;
153 if (using_suid_sandbox_) {
154 scoped_ptr<sandbox::SetuidSandboxClient>
155 sandbox_client(sandbox::SetuidSandboxClient::Create());
156 sandbox_client->PrependWrapper(&cmd_line);
157 sandbox_client->SetupLaunchOptions(&options, &fds_to_map, &dummy_fd);
158 sandbox_client->SetupLaunchEnvironment();
161 options.fds_to_remap = &fds_to_map;
162 base::Process process = base::LaunchProcess(cmd_line.argv(), options);
163 CHECK(process.IsValid()) << "Failed to launch zygote process";
164 dummy_fd.reset();
166 if (using_suid_sandbox_) {
167 // The SUID sandbox will execute the zygote in a new PID namespace, and
168 // the main zygote process will then fork from there. Watch now our
169 // elaborate dance to find and validate the zygote's PID.
171 // First we receive a message from the zygote boot process.
172 base::ProcessId boot_pid;
173 CHECK(ReceiveFixedMessage(
174 fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid));
176 // Within the PID namespace, the zygote boot process thinks it's PID 1,
177 // but its real PID can never be 1. This gives us a reliable test that
178 // the kernel is translating the sender's PID to our namespace.
179 CHECK_GT(boot_pid, 1)
180 << "Received invalid process ID for zygote; kernel might be too old? "
181 "See crbug.com/357670 or try using --"
182 << switches::kDisableSetuidSandbox << " to workaround.";
184 // Now receive the message that the zygote's ready to go, along with the
185 // main zygote process's ID.
186 CHECK(ReceiveFixedMessage(
187 fds[0], kZygoteHelloMessage, sizeof(kZygoteHelloMessage), &pid_));
188 CHECK_GT(pid_, 1);
190 if (process.pid() != pid_) {
191 // Reap the sandbox.
192 base::EnsureProcessGetsReaped(process.pid());
194 } else {
195 // Not using the SUID sandbox.
196 // Note that ~base::Process() will reset the internal value, but there's no
197 // real "handle" on POSIX so that is safe.
198 pid_ = process.pid();
201 close(fds[1]);
202 control_fd_ = fds[0];
204 Pickle pickle;
205 pickle.WriteInt(kZygoteCommandGetSandboxStatus);
206 if (!SendMessage(pickle, NULL))
207 LOG(FATAL) << "Cannot communicate with zygote";
208 // We don't wait for the reply. We'll read it in ReadReply.
211 void ZygoteHostImpl::TearDownAfterLastChild() {
212 bool do_teardown = false;
214 base::AutoLock lock(child_tracking_lock_);
215 should_teardown_after_last_child_exits_ = true;
216 do_teardown = list_of_running_zygote_children_.empty();
218 if (do_teardown) {
219 TearDown();
223 // Note: this is also called from the destructor.
224 void ZygoteHostImpl::TearDown() {
225 base::AutoLock lock(control_lock_);
226 if (control_fd_ > -1) {
227 // Closing the IPC channel will act as a notification to exit
228 // to the Zygote.
229 if (IGNORE_EINTR(close(control_fd_))) {
230 PLOG(ERROR) << "Could not close Zygote control channel.";
231 NOTREACHED();
233 control_fd_ = -1;
237 void ZygoteHostImpl::ZygoteChildBorn(pid_t process) {
238 base::AutoLock lock(child_tracking_lock_);
239 bool new_element_inserted =
240 list_of_running_zygote_children_.insert(process).second;
241 DCHECK(new_element_inserted);
244 void ZygoteHostImpl::ZygoteChildDied(pid_t process) {
245 bool do_teardown = false;
247 base::AutoLock lock(child_tracking_lock_);
248 size_t num_erased = list_of_running_zygote_children_.erase(process);
249 DCHECK_EQ(1U, num_erased);
250 do_teardown = should_teardown_after_last_child_exits_ &&
251 list_of_running_zygote_children_.empty();
253 if (do_teardown) {
254 TearDown();
258 bool ZygoteHostImpl::SendMessage(const Pickle& data,
259 const std::vector<int>* fds) {
260 DCHECK_NE(-1, control_fd_);
261 CHECK(data.size() <= kZygoteMaxMessageLength)
262 << "Trying to send too-large message to zygote (sending " << data.size()
263 << " bytes, max is " << kZygoteMaxMessageLength << ")";
264 CHECK(!fds || fds->size() <= UnixDomainSocket::kMaxFileDescriptors)
265 << "Trying to send message with too many file descriptors to zygote "
266 << "(sending " << fds->size() << ", max is "
267 << UnixDomainSocket::kMaxFileDescriptors << ")";
269 return UnixDomainSocket::SendMsg(control_fd_,
270 data.data(), data.size(),
271 fds ? *fds : std::vector<int>());
274 ssize_t ZygoteHostImpl::ReadReply(void* buf, size_t buf_len) {
275 DCHECK_NE(-1, control_fd_);
276 // At startup we send a kZygoteCommandGetSandboxStatus request to the zygote,
277 // but don't wait for the reply. Thus, the first time that we read from the
278 // zygote, we get the reply to that request.
279 if (!have_read_sandbox_status_word_) {
280 if (HANDLE_EINTR(read(control_fd_, &sandbox_status_,
281 sizeof(sandbox_status_))) !=
282 sizeof(sandbox_status_)) {
283 return -1;
285 have_read_sandbox_status_word_ = true;
288 return HANDLE_EINTR(read(control_fd_, buf, buf_len));
291 pid_t ZygoteHostImpl::ForkRequest(const std::vector<std::string>& argv,
292 scoped_ptr<FileDescriptorInfo> mapping,
293 const std::string& process_type) {
294 DCHECK(init_);
295 Pickle pickle;
297 int raw_socks[2];
298 PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks));
299 base::ScopedFD my_sock(raw_socks[0]);
300 base::ScopedFD peer_sock(raw_socks[1]);
301 CHECK(UnixDomainSocket::EnableReceiveProcessId(my_sock.get()));
303 pickle.WriteInt(kZygoteCommandFork);
304 pickle.WriteString(process_type);
305 pickle.WriteInt(argv.size());
306 for (std::vector<std::string>::const_iterator
307 i = argv.begin(); i != argv.end(); ++i)
308 pickle.WriteString(*i);
310 // Fork requests contain one file descriptor for the PID oracle, and one
311 // more for each file descriptor mapping for the child process.
312 const size_t num_fds_to_send = 1 + mapping->GetMappingSize();
313 pickle.WriteInt(num_fds_to_send);
315 std::vector<int> fds;
317 // First FD to send is peer_sock.
318 // TODO(morrita): Ideally, this should be part of the mapping so that
319 // FileDescriptorInfo can manages its lifetime.
320 fds.push_back(peer_sock.get());
322 // The rest come from mapping.
323 for (size_t i = 0; i < mapping->GetMappingSize(); ++i) {
324 pickle.WriteUInt32(mapping->GetIDAt(i));
325 fds.push_back(mapping->GetFDAt(i));
328 // Sanity check that we've populated |fds| correctly.
329 DCHECK_EQ(num_fds_to_send, fds.size());
331 pid_t pid;
333 base::AutoLock lock(control_lock_);
334 if (!SendMessage(pickle, &fds))
335 return base::kNullProcessHandle;
336 mapping.reset();
337 peer_sock.reset();
340 char buf[sizeof(kZygoteChildPingMessage) + 1];
341 ScopedVector<base::ScopedFD> recv_fds;
342 base::ProcessId real_pid;
344 ssize_t n = UnixDomainSocket::RecvMsgWithPid(
345 my_sock.get(), buf, sizeof(buf), &recv_fds, &real_pid);
346 if (n != sizeof(kZygoteChildPingMessage) ||
347 0 != memcmp(buf,
348 kZygoteChildPingMessage,
349 sizeof(kZygoteChildPingMessage))) {
350 // Zygote children should still be trustworthy when they're supposed to
351 // ping us, so something's broken if we don't receive a valid ping.
352 LOG(ERROR) << "Did not receive ping from zygote child";
353 NOTREACHED();
354 real_pid = -1;
356 my_sock.reset();
358 // Always send PID back to zygote.
359 Pickle pid_pickle;
360 pid_pickle.WriteInt(kZygoteCommandForkRealPID);
361 pid_pickle.WriteInt(real_pid);
362 if (!SendMessage(pid_pickle, NULL))
363 return base::kNullProcessHandle;
366 // Read the reply, which pickles the PID and an optional UMA enumeration.
367 static const unsigned kMaxReplyLength = 2048;
368 char buf[kMaxReplyLength];
369 const ssize_t len = ReadReply(buf, sizeof(buf));
371 Pickle reply_pickle(buf, len);
372 PickleIterator iter(reply_pickle);
373 if (len <= 0 || !iter.ReadInt(&pid))
374 return base::kNullProcessHandle;
376 // If there is a nonempty UMA name string, then there is a UMA
377 // enumeration to record.
378 std::string uma_name;
379 int uma_sample;
380 int uma_boundary_value;
381 if (iter.ReadString(&uma_name) &&
382 !uma_name.empty() &&
383 iter.ReadInt(&uma_sample) &&
384 iter.ReadInt(&uma_boundary_value)) {
385 // We cannot use the UMA_HISTOGRAM_ENUMERATION macro here,
386 // because that's only for when the name is the same every time.
387 // Here we're using whatever name we got from the other side.
388 // But since it's likely that the same one will be used repeatedly
389 // (even though it's not guaranteed), we cache it here.
390 static base::HistogramBase* uma_histogram;
391 if (!uma_histogram || uma_histogram->histogram_name() != uma_name) {
392 uma_histogram = base::LinearHistogram::FactoryGet(
393 uma_name, 1,
394 uma_boundary_value,
395 uma_boundary_value + 1,
396 base::HistogramBase::kUmaTargetedHistogramFlag);
398 uma_histogram->Add(uma_sample);
401 if (pid <= 0)
402 return base::kNullProcessHandle;
405 #if !defined(OS_OPENBSD)
406 // This is just a starting score for a renderer or extension (the
407 // only types of processes that will be started this way). It will
408 // get adjusted as time goes on. (This is the same value as
409 // chrome::kLowestRendererOomScore in chrome/chrome_constants.h, but
410 // that's not something we can include here.)
411 const int kLowestRendererOomScore = 300;
412 AdjustRendererOOMScore(pid, kLowestRendererOomScore);
413 #endif
415 ZygoteChildBorn(pid);
416 return pid;
419 #if !defined(OS_OPENBSD)
420 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid,
421 int score) {
422 // 1) You can't change the oom_score_adj of a non-dumpable process
423 // (EPERM) unless you're root. Because of this, we can't set the
424 // oom_adj from the browser process.
426 // 2) We can't set the oom_score_adj before entering the sandbox
427 // because the zygote is in the sandbox and the zygote is as
428 // critical as the browser process. Its oom_adj value shouldn't
429 // be changed.
431 // 3) A non-dumpable process can't even change its own oom_score_adj
432 // because it's root owned 0644. The sandboxed processes don't
433 // even have /proc, but one could imagine passing in a descriptor
434 // from outside.
436 // So, in the normal case, we use the SUID binary to change it for us.
437 // However, Fedora (and other SELinux systems) don't like us touching other
438 // process's oom_score_adj (or oom_adj) values
439 // (https://bugzilla.redhat.com/show_bug.cgi?id=581256).
441 // The offical way to get the SELinux mode is selinux_getenforcemode, but I
442 // don't want to add another library to the build as it's sure to cause
443 // problems with other, non-SELinux distros.
445 // So we just check for files in /selinux. This isn't foolproof, but it's not
446 // bad and it's easy.
448 static bool selinux;
449 static bool selinux_valid = false;
451 if (!selinux_valid) {
452 const base::FilePath kSelinuxPath("/selinux");
453 base::FileEnumerator en(kSelinuxPath, false, base::FileEnumerator::FILES);
454 bool has_selinux_files = !en.Next().empty();
456 selinux = access(kSelinuxPath.value().c_str(), X_OK) == 0 &&
457 has_selinux_files;
458 selinux_valid = true;
461 if (using_suid_sandbox_ && !selinux) {
462 #if defined(USE_TCMALLOC)
463 // If heap profiling is running, these processes are not exiting, at least
464 // on ChromeOS. The easiest thing to do is not launch them when profiling.
465 // TODO(stevenjb): Investigate further and fix.
466 if (IsHeapProfilerRunning())
467 return;
468 #endif
469 std::vector<std::string> adj_oom_score_cmdline;
470 adj_oom_score_cmdline.push_back(sandbox_binary_);
471 adj_oom_score_cmdline.push_back(sandbox::kAdjustOOMScoreSwitch);
472 adj_oom_score_cmdline.push_back(base::Int64ToString(pid));
473 adj_oom_score_cmdline.push_back(base::IntToString(score));
475 base::Process sandbox_helper_process;
476 base::LaunchOptions options;
478 // sandbox_helper_process is a setuid binary.
479 options.allow_new_privs = true;
481 sandbox_helper_process =
482 base::LaunchProcess(adj_oom_score_cmdline, options);
483 if (sandbox_helper_process.IsValid())
484 base::EnsureProcessGetsReaped(sandbox_helper_process.pid());
485 } else if (!using_suid_sandbox_) {
486 if (!base::AdjustOOMScore(pid, score))
487 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid;
490 #endif
492 void ZygoteHostImpl::EnsureProcessTerminated(pid_t process) {
493 DCHECK(init_);
494 Pickle pickle;
496 pickle.WriteInt(kZygoteCommandReap);
497 pickle.WriteInt(process);
498 if (!SendMessage(pickle, NULL))
499 LOG(ERROR) << "Failed to send Reap message to zygote";
500 ZygoteChildDied(process);
503 base::TerminationStatus ZygoteHostImpl::GetTerminationStatus(
504 base::ProcessHandle handle,
505 bool known_dead,
506 int* exit_code) {
507 DCHECK(init_);
508 Pickle pickle;
509 pickle.WriteInt(kZygoteCommandGetTerminationStatus);
510 pickle.WriteBool(known_dead);
511 pickle.WriteInt(handle);
513 static const unsigned kMaxMessageLength = 128;
514 char buf[kMaxMessageLength];
515 ssize_t len;
517 base::AutoLock lock(control_lock_);
518 if (!SendMessage(pickle, NULL))
519 LOG(ERROR) << "Failed to send GetTerminationStatus message to zygote";
520 len = ReadReply(buf, sizeof(buf));
523 // Set this now to handle the error cases.
524 if (exit_code)
525 *exit_code = RESULT_CODE_NORMAL_EXIT;
526 int status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
528 if (len == -1) {
529 LOG(WARNING) << "Error reading message from zygote: " << errno;
530 } else if (len == 0) {
531 LOG(WARNING) << "Socket closed prematurely.";
532 } else {
533 Pickle read_pickle(buf, len);
534 int tmp_status, tmp_exit_code;
535 PickleIterator iter(read_pickle);
536 if (!iter.ReadInt(&tmp_status) || !iter.ReadInt(&tmp_exit_code)) {
537 LOG(WARNING)
538 << "Error parsing GetTerminationStatus response from zygote.";
539 } else {
540 if (exit_code)
541 *exit_code = tmp_exit_code;
542 status = tmp_status;
546 if (status != base::TERMINATION_STATUS_STILL_RUNNING) {
547 ZygoteChildDied(handle);
549 return static_cast<base::TerminationStatus>(status);
552 pid_t ZygoteHostImpl::GetPid() const {
553 return pid_;
556 int ZygoteHostImpl::GetSandboxStatus() const {
557 if (have_read_sandbox_status_word_)
558 return sandbox_status_;
559 return 0;
562 } // namespace content