| 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/zygote/zygote_linux.h" | 5 #include "content/zygote/zygote_linux.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/debug/trace_event.h" | 14 #include "base/debug/trace_event.h" |
| 15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 16 #include "base/linux_util.h" | 16 #include "base/linux_util.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/pickle.h" | 18 #include "base/pickle.h" |
| 19 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/posix/global_descriptors.h" | 20 #include "base/posix/global_descriptors.h" |
| 21 #include "base/posix/unix_domain_socket_linux.h" | 21 #include "base/posix/unix_domain_socket_linux.h" |
| 22 #include "base/process/kill.h" | 22 #include "base/process/kill.h" |
| 23 #include "content/common/child_process_sandbox_support_impl_linux.h" |
| 23 #include "content/common/sandbox_linux.h" | 24 #include "content/common/sandbox_linux.h" |
| 24 #include "content/common/set_process_title.h" | 25 #include "content/common/set_process_title.h" |
| 25 #include "content/common/zygote_commands_linux.h" | 26 #include "content/common/zygote_commands_linux.h" |
| 26 #include "content/public/common/content_descriptors.h" | 27 #include "content/public/common/content_descriptors.h" |
| 27 #include "content/public/common/result_codes.h" | 28 #include "content/public/common/result_codes.h" |
| 28 #include "content/public/common/sandbox_linux.h" | 29 #include "content/public/common/sandbox_linux.h" |
| 29 #include "content/public/common/zygote_fork_delegate_linux.h" | 30 #include "content/public/common/zygote_fork_delegate_linux.h" |
| 30 #include "ipc/ipc_channel.h" | 31 #include "ipc/ipc_channel.h" |
| 31 #include "ipc/ipc_switches.h" | 32 #include "ipc/ipc_switches.h" |
| 32 | 33 |
| 33 // See http://code.google.com/p/chromium/wiki/LinuxZygote | 34 // See http://code.google.com/p/chromium/wiki/LinuxZygote |
| 34 | 35 |
| 35 namespace content { | 36 namespace content { |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 // NOP function. See below where this handler is installed. | 40 // NOP function. See below where this handler is installed. |
| 40 void SIGCHLDHandler(int signal) { | 41 void SIGCHLDHandler(int signal) { |
| 41 } | 42 } |
| 42 | 43 |
| 43 } // namespace | 44 } // namespace |
| 44 | 45 |
| 45 const int Zygote::kMagicSandboxIPCDescriptor; | |
| 46 | |
| 47 Zygote::Zygote(int sandbox_flags, | 46 Zygote::Zygote(int sandbox_flags, |
| 48 ZygoteForkDelegate* helper) | 47 ZygoteForkDelegate* helper) |
| 49 : sandbox_flags_(sandbox_flags), | 48 : sandbox_flags_(sandbox_flags), |
| 50 helper_(helper), | 49 helper_(helper), |
| 51 initial_uma_sample_(0), | 50 initial_uma_sample_(0), |
| 52 initial_uma_boundary_value_(0) { | 51 initial_uma_boundary_value_(0) { |
| 53 if (helper_) { | 52 if (helper_) { |
| 54 helper_->InitialUMA(&initial_uma_name_, | 53 helper_->InitialUMA(&initial_uma_name_, |
| 55 &initial_uma_sample_, | 54 &initial_uma_sample_, |
| 56 &initial_uma_boundary_value_); | 55 &initial_uma_boundary_value_); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 70 // otherwise we cannot wait on children. (According to POSIX 2001.) | 69 // otherwise we cannot wait on children. (According to POSIX 2001.) |
| 71 struct sigaction action; | 70 struct sigaction action; |
| 72 memset(&action, 0, sizeof(action)); | 71 memset(&action, 0, sizeof(action)); |
| 73 action.sa_handler = &SIGCHLDHandler; | 72 action.sa_handler = &SIGCHLDHandler; |
| 74 CHECK(sigaction(SIGCHLD, &action, NULL) == 0); | 73 CHECK(sigaction(SIGCHLD, &action, NULL) == 0); |
| 75 | 74 |
| 76 if (UsingSUIDSandbox()) { | 75 if (UsingSUIDSandbox()) { |
| 77 // Let the ZygoteHost know we are ready to go. | 76 // Let the ZygoteHost know we are ready to go. |
| 78 // The receiving code is in content/browser/zygote_host_linux.cc. | 77 // The receiving code is in content/browser/zygote_host_linux.cc. |
| 79 std::vector<int> empty; | 78 std::vector<int> empty; |
| 80 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, | 79 bool r = UnixDomainSocket::SendMsg(kZygoteSocketPairFd, |
| 81 kZygoteHelloMessage, | 80 kZygoteHelloMessage, |
| 82 sizeof(kZygoteHelloMessage), empty); | 81 sizeof(kZygoteHelloMessage), empty); |
| 83 #if defined(OS_CHROMEOS) | 82 #if defined(OS_CHROMEOS) |
| 84 LOG_IF(WARNING, !r) << "Sending zygote magic failed"; | 83 LOG_IF(WARNING, !r) << "Sending zygote magic failed"; |
| 85 // Exit normally on chromeos because session manager may send SIGTERM | 84 // Exit normally on chromeos because session manager may send SIGTERM |
| 86 // right after the process starts and it may fail to send zygote magic | 85 // right after the process starts and it may fail to send zygote magic |
| 87 // number to browser process. | 86 // number to browser process. |
| 88 if (!r) | 87 if (!r) |
| 89 _exit(RESULT_CODE_NORMAL_EXIT); | 88 _exit(RESULT_CODE_NORMAL_EXIT); |
| 90 #else | 89 #else |
| 91 CHECK(r) << "Sending zygote magic failed"; | 90 CHECK(r) << "Sending zygote magic failed"; |
| 92 #endif | 91 #endif |
| 93 } | 92 } |
| 94 | 93 |
| 95 for (;;) { | 94 for (;;) { |
| 96 // This function call can return multiple times, once per fork(). | 95 // This function call can return multiple times, once per fork(). |
| 97 if (HandleRequestFromBrowser(kBrowserDescriptor)) | 96 if (HandleRequestFromBrowser(kZygoteSocketPairFd)) |
| 98 return true; | 97 return true; |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 | 100 |
| 102 bool Zygote::GetProcessInfo(base::ProcessHandle pid, | 101 bool Zygote::GetProcessInfo(base::ProcessHandle pid, |
| 103 ZygoteProcessInfo* process_info) { | 102 ZygoteProcessInfo* process_info) { |
| 104 DCHECK(process_info); | 103 DCHECK(process_info); |
| 105 const ZygoteProcessMap::const_iterator it = process_info_map_.find(pid); | 104 const ZygoteProcessMap::const_iterator it = process_info_map_.find(pid); |
| 106 if (it == process_info_map_.end()) { | 105 if (it == process_info_map_.end()) { |
| 107 return false; | 106 return false; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 close(pipe_fds[0]); | 355 close(pipe_fds[0]); |
| 357 pipe_fds[0] = -1; | 356 pipe_fds[0] = -1; |
| 358 base::ProcessId real_pid; | 357 base::ProcessId real_pid; |
| 359 if (UsingSUIDSandbox()) { | 358 if (UsingSUIDSandbox()) { |
| 360 uint8_t reply_buf[512]; | 359 uint8_t reply_buf[512]; |
| 361 Pickle request; | 360 Pickle request; |
| 362 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE); | 361 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE); |
| 363 request.WriteUInt64(dummy_inode); | 362 request.WriteUInt64(dummy_inode); |
| 364 | 363 |
| 365 const ssize_t r = UnixDomainSocket::SendRecvMsg( | 364 const ssize_t r = UnixDomainSocket::SendRecvMsg( |
| 366 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, | 365 GetSandboxFD(), reply_buf, sizeof(reply_buf), NULL, |
| 367 request); | 366 request); |
| 368 if (r == -1) { | 367 if (r == -1) { |
| 369 LOG(ERROR) << "Failed to get child process's real PID"; | 368 LOG(ERROR) << "Failed to get child process's real PID"; |
| 370 goto error; | 369 goto error; |
| 371 } | 370 } |
| 372 | 371 |
| 373 Pickle reply(reinterpret_cast<char*>(reply_buf), r); | 372 Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 374 PickleIterator iter(reply); | 373 PickleIterator iter(reply); |
| 375 if (!reply.ReadInt(&iter, &real_pid)) | 374 if (!reply.ReadInt(&iter, &real_pid)) |
| 376 goto error; | 375 goto error; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 return -1; | 458 return -1; |
| 460 | 459 |
| 461 for (int i = 0; i < numfds; ++i) { | 460 for (int i = 0; i < numfds; ++i) { |
| 462 base::GlobalDescriptors::Key key; | 461 base::GlobalDescriptors::Key key; |
| 463 if (!pickle.ReadUInt32(&iter, &key)) | 462 if (!pickle.ReadUInt32(&iter, &key)) |
| 464 return -1; | 463 return -1; |
| 465 mapping.push_back(std::make_pair(key, fds[i])); | 464 mapping.push_back(std::make_pair(key, fds[i])); |
| 466 } | 465 } |
| 467 | 466 |
| 468 mapping.push_back(std::make_pair( | 467 mapping.push_back(std::make_pair( |
| 469 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor)); | 468 static_cast<uint32_t>(kSandboxIPCChannel), GetSandboxFD())); |
| 470 | 469 |
| 471 // Returns twice, once per process. | 470 // Returns twice, once per process. |
| 472 base::ProcessId child_pid = ForkWithRealPid(process_type, fds, channel_id, | 471 base::ProcessId child_pid = ForkWithRealPid(process_type, fds, channel_id, |
| 473 uma_name, uma_sample, | 472 uma_name, uma_sample, |
| 474 uma_boundary_value); | 473 uma_boundary_value); |
| 475 if (!child_pid) { | 474 if (!child_pid) { |
| 476 // This is the child process. | 475 // This is the child process. |
| 477 | 476 |
| 478 close(kBrowserDescriptor); // Our socket from the browser. | 477 close(kZygoteSocketPairFd); // Our socket from the browser. |
| 479 if (UsingSUIDSandbox()) | 478 if (UsingSUIDSandbox()) |
| 480 close(kZygoteIdFd); // Another socket from the browser. | 479 close(kZygoteIdFd); // Another socket from the browser. |
| 481 base::GlobalDescriptors::GetInstance()->Reset(mapping); | 480 base::GlobalDescriptors::GetInstance()->Reset(mapping); |
| 482 | 481 |
| 483 // Reset the process-wide command line to our new command line. | 482 // Reset the process-wide command line to our new command line. |
| 484 CommandLine::Reset(); | 483 CommandLine::Reset(); |
| 485 CommandLine::Init(0, NULL); | 484 CommandLine::Init(0, NULL); |
| 486 CommandLine::ForCurrentProcess()->InitFromArgv(args); | 485 CommandLine::ForCurrentProcess()->InitFromArgv(args); |
| 487 | 486 |
| 488 // Update the process title. The argv was already cached by the call to | 487 // Update the process title. The argv was already cached by the call to |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 PickleIterator iter) { | 538 PickleIterator iter) { |
| 540 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) != | 539 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) != |
| 541 sizeof(sandbox_flags_)) { | 540 sizeof(sandbox_flags_)) { |
| 542 PLOG(ERROR) << "write"; | 541 PLOG(ERROR) << "write"; |
| 543 } | 542 } |
| 544 | 543 |
| 545 return false; | 544 return false; |
| 546 } | 545 } |
| 547 | 546 |
| 548 } // namespace content | 547 } // namespace content |
| OLD | NEW |