Chromium Code Reviews| Index: chrome/browser/nacl_fork_delegate.cc |
| diff --git a/chrome/browser/nacl_fork_delegate.cc b/chrome/browser/nacl_fork_delegate.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c0d3148fcf2e2bb0d3f11809db38002ca4e8ccc5 |
| --- /dev/null |
| +++ b/chrome/browser/nacl_fork_delegate.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <signal.h> |
| +#include <stdlib.h> |
| +#include <sys/socket.h> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "base/file_path.h" |
| +#include "base/process_util.h" |
| +#include "content/common/unix_domain_socket_posix.h" |
| +#include "content/common/zygote_fork_delegate.h" |
| +#include "chrome/browser/nacl_fork_delegate.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/nacl_helper_linux.h" |
| + |
| +NaClForkDelegate::NaClForkDelegate() : sandboxed_(false), |
| + fd_(-1), |
| + pid_(-1) { |
| +} |
| + |
| +void NaClForkDelegate::Init(const bool sandboxed, |
| + const int browserdesc, |
| + const int sandboxdesc) { |
| + VLOG(1) << "NaClForkDelegate::Init()"; |
| + int fds[2]; |
| + |
| + sandboxed_ = sandboxed; |
| + // Confirm a couple hard-wired assumptions. |
| + // The NaCl constants are from chrome/nacl/nacl_linux_helper.h |
| + DCHECK(kNaClBrowserDescriptor == browserdesc); |
| + DCHECK(kNaClSandboxDescriptor == sandboxdesc); |
| + |
| + CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); |
| + base::file_handle_mapping_vector fds_to_map; |
| + fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); |
| + fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); |
| + |
| + const char* nacl_zygote_exe = getenv("NACL_NEW_ZYGOTE"); |
| + pid_ = -1; |
| + if (NULL != nacl_zygote_exe) { |
| + CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); |
| + argv[0] = nacl_zygote_exe; |
| + base::LaunchAppWithClone(argv, fds_to_map, false, &pid_, |
|
agl
2011/06/20 17:28:55
This looks like Linux specific code. If so, the fi
Brad Chen
2011/06/20 22:09:47
I renamed the file to nacl/nacl_fork_delegate_linu
|
| + CLONE_FS | SIGCHLD); |
| + } |
| + close(fds[1]); |
|
agl
2011/06/20 17:28:55
HANDLE_EINTR around this.
Brad Chen
2011/06/20 22:09:47
Done.
|
| + if (pid_ > 0) { |
| + const int kExpectedLength = sizeof(kNaClHelperStartupAck); |
|
agl
2011/06/20 17:28:55
s/int/size_t/
Brad Chen
2011/06/20 22:09:47
Done.
|
| + char buf[kExpectedLength]; |
| + |
| + // Wait for ack from nacl_helper, indicating it is ready to help |
| + int nread = read(fds[0], buf, sizeof(buf)); |
| + if (nread == kExpectedLength && |
| + memcmp(buf, kNaClHelperStartupAck, nread) == 0) { |
| + // all is well |
| + fd_ = fds[0]; |
| + return; |
| + } else { |
| + LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes"; |
| + } |
| + } |
| + // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper |
| + // becomes the default. |
| + VLOG(1) << "Could not launch NaCl helper"; |
| + pid_ = -1; |
| + fd_ = -1; |
| + close(fds[0]); |
|
agl
2011/06/20 17:28:55
HANDLE_EINTR
Brad Chen
2011/06/20 22:09:47
Done.
|
| +} |
| + |
| +NaClForkDelegate::~NaClForkDelegate() { |
| + close(fd_); // side effect: delegate process will terminate |
|
agl
2011/06/20 17:28:55
HANDLE_EINTR()
Brad Chen
2011/06/20 22:09:47
Done.
|
| +} |
| + |
| +bool NaClForkDelegate::CanHelp(const std::string& process_type) { |
| + return (process_type == switches::kNaClLoaderProcess && (Pid() != -1)); |
| +} |
| + |
| +pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { |
| + base::ProcessId naclchild; |
| + VLOG(1) << "NaClForkDelegate::Fork"; |
| + |
| + if (sandboxed_) { |
| + DCHECK(fds.size() == kNaClParentFDIndex + 1); |
| + } else { |
| + DCHECK(fds.size() == kNaClBrowserFDIndex + 1); |
| + } |
| + if (!UnixDomainSocket::SendMsg(FD(), kNaClForkRequest, |
| + sizeof(kNaClForkRequest), fds)) { |
| + LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; |
| + return -1; |
| + } |
| + int nread = read(FD(), &naclchild, sizeof(naclchild)); |
| + if (nread != sizeof(naclchild)) { |
| + LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; |
| + return -1; |
| + } |
| + VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; |
| + return naclchild; |
| +} |