Chromium Code Reviews| 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 <signal.h> | |
| 6 #include <stdlib.h> | |
| 7 #include <sys/socket.h> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/eintr_wrapper.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/process_util.h" | |
| 15 #include "content/common/unix_domain_socket_posix.h" | |
| 16 #include "content/common/zygote_fork_delegate_linux.h" | |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/common/nacl_helper_linux.h" | |
| 19 #include "chrome/common/nacl_fork_delegate_linux.h" | |
|
Evan Martin
2011/06/24 18:57:33
This should be the first #include.
Brad Chen
2011/06/25 22:14:51
Done.
| |
| 20 | |
| 21 NaClForkDelegate::NaClForkDelegate() : sandboxed_(false), | |
| 22 fd_(-1), | |
| 23 pid_(-1) { | |
| 24 } | |
| 25 | |
| 26 void NaClForkDelegate::Init(const bool sandboxed, | |
| 27 const int browserdesc, | |
| 28 const int sandboxdesc) { | |
| 29 VLOG(1) << "NaClForkDelegate::Init()"; | |
| 30 int fds[2]; | |
| 31 | |
| 32 sandboxed_ = sandboxed; | |
| 33 // Confirm a couple hard-wired assumptions. | |
| 34 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h | |
| 35 DCHECK(kNaClBrowserDescriptor == browserdesc); | |
| 36 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | |
| 37 | |
| 38 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
| 39 base::file_handle_mapping_vector fds_to_map; | |
| 40 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | |
| 41 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | |
| 42 // TODO(bradchen): Before making this the default for release builds, | |
| 43 // replace command line switch with PathService::Get(). | |
| 44 const std::string nacl_zygote_exe = | |
| 45 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 46 switches::kNaClLinuxHelper); | |
| 47 pid_ = -1; | |
| 48 if (nacl_zygote_exe.length() != 0) { | |
| 49 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); | |
| 50 argv[0] = nacl_zygote_exe; | |
| 51 base::LaunchAppWithClone(argv, fds_to_map, false, &pid_, | |
| 52 CLONE_FS | SIGCHLD); | |
| 53 // parent and error cases are handled below | |
| 54 } | |
| 55 HANDLE_EINTR(close(fds[1])); | |
| 56 if (pid_ > 0) { | |
| 57 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | |
| 58 char buf[kExpectedLength]; | |
| 59 | |
| 60 // Wait for ack from nacl_helper, indicating it is ready to help | |
| 61 const ssize_t nread = read(fds[0], buf, sizeof(buf)); | |
| 62 if (nread == kExpectedLength && | |
| 63 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
| 64 // all is well | |
| 65 fd_ = fds[0]; | |
| 66 return; | |
| 67 } else { | |
| 68 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes"; | |
| 69 } | |
| 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 < 0) { | |
| 110 return false; | |
| 111 } else if (nwritten != static_cast<int>(channel_switch.length())) { | |
| 112 return false; | |
| 113 } | |
| 114 return true; | |
| 115 } | |
| OLD | NEW |