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() : ready_(false), | |
agl
2011/07/18 21:19:35
nit: unless constructor init lists fit on the same
Brad Chen
2011/07/18 22:40:29
Done.
| |
23 sandboxed_(false), | |
24 fd_(-1) {} | |
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 ready_ = false; | |
48 if (nacl_zygote_exe.length() != 0) { | |
49 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); | |
50 argv[0] = nacl_zygote_exe; | |
51 base::LaunchOptions options; | |
52 options.fds_to_remap = &fds_to_map; | |
53 options.clone_flags = CLONE_FS | SIGCHLD; | |
54 ready_ = base::LaunchProcess(argv, options, NULL); | |
55 // parent and error cases are handled below | |
56 } | |
57 HANDLE_EINTR(close(fds[1])); | |
58 if (ready_) { | |
59 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | |
60 char buf[kExpectedLength]; | |
61 | |
62 // Wait for ack from nacl_helper, indicating it is ready to help | |
63 const ssize_t nread = read(fds[0], buf, sizeof(buf)); | |
agl
2011/07/18 21:19:35
HANDLE_EINTR around read()
Brad Chen
2011/07/18 22:40:29
Done.
| |
64 if (nread == kExpectedLength && | |
65 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
66 // all is well | |
67 fd_ = fds[0]; | |
68 return; | |
69 } | |
70 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes)"; | |
71 } | |
72 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
73 // becomes the default. | |
74 ready_ = false; | |
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 && ready_); | |
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)); | |
agl
2011/07/18 21:19:35
HANDLE_EINTR
Brad Chen
2011/07/18 22:40:29
Done.
| |
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 != static_cast<int>(channel_switch.length())) { | |
111 return false; | |
112 } | |
113 return true; | |
114 } | |
OLD | NEW |