| 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/path_service.h" | |
| 17 #include "base/process_util.h" | |
| 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
| 19 #include "content/common/unix_domain_socket_posix.h" | |
| 20 #include "content/common/zygote_fork_delegate_linux.h" | |
| 21 #include "chrome/common/chrome_paths.h" | |
| 22 #include "chrome/common/chrome_switches.h" | |
| 23 #include "chrome/common/nacl_helper_linux.h" | |
| 24 | |
| 25 NaClForkDelegate::NaClForkDelegate() | |
| 26 : status_(kNaClHelperUnused), | |
| 27 sandboxed_(false), | |
| 28 fd_(-1) {} | |
| 29 | |
| 30 const char kNaClHelperAtZero[] = "--at-zero"; | |
| 31 | |
| 32 void NaClForkDelegate::Init(const bool sandboxed, | |
| 33 const int browserdesc, | |
| 34 const int sandboxdesc) { | |
| 35 VLOG(1) << "NaClForkDelegate::Init()"; | |
| 36 int fds[2]; | |
| 37 | |
| 38 sandboxed_ = sandboxed; | |
| 39 // Confirm a couple hard-wired assumptions. | |
| 40 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h | |
| 41 DCHECK(kNaClBrowserDescriptor == browserdesc); | |
| 42 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | |
| 43 | |
| 44 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
| 45 base::file_handle_mapping_vector fds_to_map; | |
| 46 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | |
| 47 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | |
| 48 | |
| 49 status_ = kNaClHelperUnused; | |
| 50 FilePath helper_exe; | |
| 51 FilePath helper_bootstrap_exe; | |
| 52 if (!PathService::Get(chrome::FILE_NACL_HELPER, &helper_exe)) { | |
| 53 status_ = kNaClHelperMissing; | |
| 54 } else if (!PathService::Get(chrome::FILE_NACL_HELPER_BOOTSTRAP, | |
| 55 &helper_bootstrap_exe)) { | |
| 56 status_ = kNaClHelperBootstrapMissing; | |
| 57 } else if (RunningOnValgrind()) { | |
| 58 status_ = kNaClHelperValgrind; | |
| 59 } else { | |
| 60 CommandLine cmd_line(helper_bootstrap_exe); | |
| 61 cmd_line.AppendArgPath(helper_exe); | |
| 62 cmd_line.AppendArgNative(kNaClHelperAtZero); | |
| 63 base::LaunchOptions options; | |
| 64 options.fds_to_remap = &fds_to_map; | |
| 65 options.clone_flags = CLONE_FS | SIGCHLD; | |
| 66 if (!base::LaunchProcess(cmd_line.argv(), options, NULL)) | |
| 67 status_ = kNaClHelperLaunchFailed; | |
| 68 // parent and error cases are handled below | |
| 69 } | |
| 70 if (HANDLE_EINTR(close(fds[1])) != 0) | |
| 71 LOG(ERROR) << "close(fds[1]) failed"; | |
| 72 if (status_ == kNaClHelperUnused) { | |
| 73 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | |
| 74 char buf[kExpectedLength]; | |
| 75 | |
| 76 // Wait for ack from nacl_helper, indicating it is ready to help | |
| 77 const ssize_t nread = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); | |
| 78 if (nread == kExpectedLength && | |
| 79 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
| 80 // all is well | |
| 81 status_ = kNaClHelperSuccess; | |
| 82 fd_ = fds[0]; | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 status_ = kNaClHelperAckFailed; | |
| 87 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes)"; | |
| 88 } | |
| 89 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
| 90 // becomes the default. | |
| 91 fd_ = -1; | |
| 92 if (HANDLE_EINTR(close(fds[0])) != 0) | |
| 93 LOG(ERROR) << "close(fds[0]) failed"; | |
| 94 } | |
| 95 | |
| 96 void NaClForkDelegate::InitialUMA(std::string* uma_name, | |
| 97 int* uma_sample, | |
| 98 int* uma_boundary_value) { | |
| 99 *uma_name = "NaCl.Client.Helper.InitState"; | |
| 100 *uma_sample = status_; | |
| 101 *uma_boundary_value = kNaClHelperStatusBoundary; | |
| 102 } | |
| 103 | |
| 104 NaClForkDelegate::~NaClForkDelegate() { | |
| 105 // side effect of close: delegate process will terminate | |
| 106 if (status_ == kNaClHelperSuccess) { | |
| 107 if (HANDLE_EINTR(close(fd_)) != 0) | |
| 108 LOG(ERROR) << "close(fd_) failed"; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 bool NaClForkDelegate::CanHelp(const std::string& process_type, | |
| 113 std::string* uma_name, | |
| 114 int* uma_sample, | |
| 115 int* uma_boundary_value) { | |
| 116 if (process_type != switches::kNaClLoaderProcess) | |
| 117 return false; | |
| 118 *uma_name = "NaCl.Client.Helper.StateOnFork"; | |
| 119 *uma_sample = status_; | |
| 120 *uma_boundary_value = kNaClHelperStatusBoundary; | |
| 121 return status_ == kNaClHelperSuccess; | |
| 122 } | |
| 123 | |
| 124 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { | |
| 125 base::ProcessId naclchild; | |
| 126 VLOG(1) << "NaClForkDelegate::Fork"; | |
| 127 | |
| 128 DCHECK(fds.size() == kNaClParentFDIndex + 1); | |
| 129 if (!UnixDomainSocket::SendMsg(fd_, kNaClForkRequest, | |
| 130 strlen(kNaClForkRequest), fds)) { | |
| 131 LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; | |
| 132 return -1; | |
| 133 } | |
| 134 int nread = HANDLE_EINTR(read(fd_, &naclchild, sizeof(naclchild))); | |
| 135 if (nread != sizeof(naclchild)) { | |
| 136 LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; | |
| 137 return -1; | |
| 138 } | |
| 139 VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; | |
| 140 return naclchild; | |
| 141 } | |
| 142 | |
| 143 bool NaClForkDelegate::AckChild(const int fd, | |
| 144 const std::string& channel_switch) { | |
| 145 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), | |
| 146 channel_switch.length())); | |
| 147 if (nwritten != static_cast<int>(channel_switch.length())) { | |
| 148 return false; | |
| 149 } | |
| 150 return true; | |
| 151 } | |
| OLD | NEW |