OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/common/nacl_fork_delegate_linux.h" |
| 6 |
| 7 #include <signal.h> |
| 8 #include <stdlib.h> |
| 9 #include <sys/socket.h> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/eintr_wrapper.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/file_path.h" |
| 16 #include "base/process_util.h" |
| 17 #include "content/common/unix_domain_socket_posix.h" |
| 18 #include "content/common/zygote_fork_delegate_linux.h" |
| 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/nacl_helper_linux.h" |
| 21 |
| 22 NaClForkDelegate::NaClForkDelegate() |
| 23 : ready_(false), |
| 24 sandboxed_(false), |
| 25 fd_(-1) {} |
| 26 |
| 27 void NaClForkDelegate::Init(const bool sandboxed, |
| 28 const int browserdesc, |
| 29 const int sandboxdesc) { |
| 30 VLOG(1) << "NaClForkDelegate::Init()"; |
| 31 int fds[2]; |
| 32 |
| 33 sandboxed_ = sandboxed; |
| 34 // Confirm a couple hard-wired assumptions. |
| 35 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h |
| 36 DCHECK(kNaClBrowserDescriptor == browserdesc); |
| 37 DCHECK(kNaClSandboxDescriptor == sandboxdesc); |
| 38 |
| 39 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); |
| 40 base::file_handle_mapping_vector fds_to_map; |
| 41 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); |
| 42 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); |
| 43 // TODO(bradchen): Before making this the default for release builds, |
| 44 // replace command line switch with PathService::Get(). |
| 45 const std::string nacl_zygote_exe = |
| 46 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 47 switches::kNaClLinuxHelper); |
| 48 ready_ = false; |
| 49 if (nacl_zygote_exe.length() != 0) { |
| 50 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); |
| 51 argv[0] = nacl_zygote_exe; |
| 52 base::LaunchOptions options; |
| 53 options.fds_to_remap = &fds_to_map; |
| 54 options.clone_flags = CLONE_FS | SIGCHLD; |
| 55 ready_ = base::LaunchProcess(argv, options, NULL); |
| 56 // parent and error cases are handled below |
| 57 } |
| 58 HANDLE_EINTR(close(fds[1])); |
| 59 if (ready_) { |
| 60 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); |
| 61 char buf[kExpectedLength]; |
| 62 |
| 63 // Wait for ack from nacl_helper, indicating it is ready to help |
| 64 const ssize_t nread = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); |
| 65 if (nread == kExpectedLength && |
| 66 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { |
| 67 // all is well |
| 68 fd_ = fds[0]; |
| 69 return; |
| 70 } |
| 71 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes)"; |
| 72 } |
| 73 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper |
| 74 // becomes the default. |
| 75 ready_ = false; |
| 76 fd_ = -1; |
| 77 HANDLE_EINTR(close(fds[0])); |
| 78 } |
| 79 |
| 80 NaClForkDelegate::~NaClForkDelegate() { |
| 81 HANDLE_EINTR(close(fd_)); // side effect: delegate process will terminate |
| 82 } |
| 83 |
| 84 bool NaClForkDelegate::CanHelp(const std::string& process_type) { |
| 85 return (process_type == switches::kNaClLoaderProcess && ready_); |
| 86 } |
| 87 |
| 88 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { |
| 89 base::ProcessId naclchild; |
| 90 VLOG(1) << "NaClForkDelegate::Fork"; |
| 91 |
| 92 DCHECK(fds.size() == kNaClParentFDIndex + 1); |
| 93 if (!UnixDomainSocket::SendMsg(fd_, kNaClForkRequest, |
| 94 strlen(kNaClForkRequest), fds)) { |
| 95 LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; |
| 96 return -1; |
| 97 } |
| 98 int nread = HANDLE_EINTR(read(fd_, &naclchild, sizeof(naclchild))); |
| 99 if (nread != sizeof(naclchild)) { |
| 100 LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; |
| 101 return -1; |
| 102 } |
| 103 VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; |
| 104 return naclchild; |
| 105 } |
| 106 |
| 107 bool NaClForkDelegate::AckChild(const int fd, |
| 108 const std::string& channel_switch) { |
| 109 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), |
| 110 channel_switch.length())); |
| 111 if (nwritten != static_cast<int>(channel_switch.length())) { |
| 112 return false; |
| 113 } |
| 114 return true; |
| 115 } |
OLD | NEW |