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 "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 } else { | |
|
agl
2011/06/27 18:59:32
nit: there shouldn't be an else{} clause if the if
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 69 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes"; | |
| 70 } | |
| 71 } | |
| 72 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
| 73 // becomes the default. | |
| 74 pid_ = -1; | |
| 75 fd_ = -1; | |
| 76 HANDLE_EINTR(close(fds[0])); | |
| 77 } | |
| 78 | |
| 79 NaClForkDelegate::~NaClForkDelegate() { | |
| 80 HANDLE_EINTR(close(fd_)); // side effect: delegate process will terminate | |
| 81 } | |
| 82 | |
| 83 bool NaClForkDelegate::CanHelp(const std::string& process_type) { | |
| 84 return (process_type == switches::kNaClLoaderProcess && (pid_ != -1)); | |
|
agl
2011/06/27 18:59:32
nit: remove superfluous parenthesis.
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 85 } | |
| 86 | |
| 87 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { | |
| 88 base::ProcessId naclchild; | |
| 89 VLOG(1) << "NaClForkDelegate::Fork"; | |
| 90 | |
| 91 DCHECK(fds.size() == kNaClParentFDIndex + 1); | |
| 92 if (!UnixDomainSocket::SendMsg(fd_, kNaClForkRequest, | |
| 93 strlen(kNaClForkRequest), fds)) { | |
| 94 LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; | |
| 95 return -1; | |
| 96 } | |
| 97 int nread = read(fd_, &naclchild, sizeof(naclchild)); | |
| 98 if (nread != sizeof(naclchild)) { | |
| 99 LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; | |
| 100 return -1; | |
| 101 } | |
| 102 VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; | |
| 103 return naclchild; | |
| 104 } | |
| 105 | |
| 106 bool NaClForkDelegate::AckChild(const int fd, | |
| 107 const std::string& channel_switch) { | |
| 108 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), | |
| 109 channel_switch.length())); | |
| 110 if (nwritten < 0) { | |
|
agl
2011/06/27 18:59:32
This is actually just a disjunction since the body
Brad Chen
2011/06/27 22:06:25
As per Evan's suggestion I think I'll just drop th
| |
| 111 return false; | |
|
Evan Martin
2011/06/27 18:49:32
I guess this <0 test is redundant with the test ju
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 112 } else if (nwritten != static_cast<int>(channel_switch.length())) { | |
| 113 return false; | |
| 114 } | |
| 115 return true; | |
| 116 } | |
| OLD | NEW |