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() : sandboxed_(false), |
| 23 fd_(-1), |
| 24 pid_(-1) { |
| 25 } |
| 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 pid_ = -1; |
| 49 if (nacl_zygote_exe.length() != 0) { |
| 50 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); |
| 51 argv[0] = nacl_zygote_exe; |
| 52 base::LaunchAppWithClone(argv, fds_to_map, false, &pid_, |
| 53 CLONE_FS | SIGCHLD); |
| 54 // parent and error cases are handled below |
| 55 } |
| 56 HANDLE_EINTR(close(fds[1])); |
| 57 if (pid_ > 0) { |
| 58 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); |
| 59 char buf[kExpectedLength]; |
| 60 |
| 61 // Wait for ack from nacl_helper, indicating it is ready to help |
| 62 const ssize_t nread = read(fds[0], buf, sizeof(buf)); |
| 63 if (nread == kExpectedLength && |
| 64 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { |
| 65 // all is well |
| 66 fd_ = fds[0]; |
| 67 return; |
| 68 } |
| 69 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes)"; |
| 70 } |
| 71 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper |
| 72 // becomes the default. |
| 73 pid_ = -1; |
| 74 fd_ = -1; |
| 75 HANDLE_EINTR(close(fds[0])); |
| 76 } |
| 77 |
| 78 NaClForkDelegate::~NaClForkDelegate() { |
| 79 HANDLE_EINTR(close(fd_)); // side effect: delegate process will terminate |
| 80 } |
| 81 |
| 82 bool NaClForkDelegate::CanHelp(const std::string& process_type) { |
| 83 return (process_type == switches::kNaClLoaderProcess && pid_ != -1); |
| 84 } |
| 85 |
| 86 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { |
| 87 base::ProcessId naclchild; |
| 88 VLOG(1) << "NaClForkDelegate::Fork"; |
| 89 |
| 90 DCHECK(fds.size() == kNaClParentFDIndex + 1); |
| 91 if (!UnixDomainSocket::SendMsg(fd_, kNaClForkRequest, |
| 92 strlen(kNaClForkRequest), fds)) { |
| 93 LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; |
| 94 return -1; |
| 95 } |
| 96 int nread = read(fd_, &naclchild, sizeof(naclchild)); |
| 97 if (nread != sizeof(naclchild)) { |
| 98 LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; |
| 99 return -1; |
| 100 } |
| 101 VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; |
| 102 return naclchild; |
| 103 } |
| 104 |
| 105 bool NaClForkDelegate::AckChild(const int fd, |
| 106 const std::string& channel_switch) { |
| 107 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), |
| 108 channel_switch.length())); |
| 109 if (nwritten != static_cast<int>(channel_switch.length())) { |
| 110 return false; |
| 111 } |
| 112 return true; |
| 113 } |
OLD | NEW |