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 <errno.h> | |
8 #include <signal.h> | |
9 #include <stdlib.h> | |
10 #include <sys/socket.h> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/command_line.h" | |
14 #include "base/eintr_wrapper.h" | |
15 #include "base/logging.h" | |
16 #include "base/file_path.h" | |
17 #include "base/process_util.h" | |
18 #include "content/common/unix_domain_socket_posix.h" | |
19 #include "content/common/zygote_fork_delegate_linux.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 #include "chrome/common/nacl_helper_linux.h" | |
22 | |
23 NaClForkDelegate::NaClForkDelegate() : sandboxed_(false), | |
24 fd_(-1), | |
25 pid_(-1) { | |
26 } | |
27 | |
28 void NaClForkDelegate::Init(const bool sandboxed, | |
29 const int browserdesc, | |
30 const int sandboxdesc) { | |
31 VLOG(1) << "NaClForkDelegate::Init()"; | |
32 int fds[2]; | |
33 | |
34 sandboxed_ = sandboxed; | |
35 // Confirm a couple hard-wired assumptions. | |
36 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h | |
37 DCHECK(kNaClBrowserDescriptor == browserdesc); | |
38 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | |
39 | |
40 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
41 base::file_handle_mapping_vector fds_to_map; | |
42 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | |
43 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | |
44 // TODO(bradchen): Before making this the default for release builds, | |
45 // replace command line switch with PathService::Get(). | |
46 const std::string nacl_zygote_exe = | |
47 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
48 switches::kNaClLinuxHelper); | |
49 pid_ = -1; | |
50 if (nacl_zygote_exe.length() != 0) { | |
51 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); | |
52 argv[0] = nacl_zygote_exe; | |
53 base::LaunchAppWithClone(argv, fds_to_map, false, &pid_, | |
54 CLONE_FS | SIGCHLD); | |
55 // parent and error cases are handled below | |
56 } | |
57 if (HANDLE_EINTR(close(fds[1])) < 0) | |
58 LOG(ERROR) << "close failed: " << errno; | |
59 | |
60 if (pid_ > 0) { | |
61 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | |
62 char buf[kExpectedLength]; | |
63 | |
64 // Wait for ack from nacl_helper, indicating it is ready to help | |
65 const ssize_t nread = read(fds[0], buf, sizeof(buf)); | |
66 if (nread == kExpectedLength && | |
67 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
68 // all is well | |
69 fd_ = fds[0]; | |
70 return; | |
71 } | |
72 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes)"; | |
73 } | |
74 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
75 // becomes the default. | |
76 pid_ = -1; | |
77 fd_ = -1; | |
78 if (HANDLE_EINTR(close(fds[0])) < 0) | |
79 LOG(ERROR) << "close failed: " << errno; | |
80 } | |
81 | |
82 NaClForkDelegate::~NaClForkDelegate() { | |
83 // side effect of close: delegate process will terminate | |
84 if (HANDLE_EINTR(close(fd_)) < 0) | |
85 LOG(ERROR) << "close failed: " << errno; | |
86 } | |
87 | |
88 bool NaClForkDelegate::CanHelp(const std::string& process_type) { | |
89 return (process_type == switches::kNaClLoaderProcess && pid_ != -1); | |
90 } | |
91 | |
92 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { | |
93 base::ProcessId naclchild; | |
94 VLOG(1) << "NaClForkDelegate::Fork"; | |
95 | |
96 DCHECK(fds.size() == kNaClParentFDIndex + 1); | |
97 if (!UnixDomainSocket::SendMsg(fd_, kNaClForkRequest, | |
98 strlen(kNaClForkRequest), fds)) { | |
99 LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; | |
100 return -1; | |
101 } | |
102 int nread = read(fd_, &naclchild, sizeof(naclchild)); | |
103 if (nread != sizeof(naclchild)) { | |
104 LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; | |
105 return -1; | |
106 } | |
107 VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; | |
108 return naclchild; | |
109 } | |
110 | |
111 bool NaClForkDelegate::AckChild(const int fd, | |
112 const std::string& channel_switch) { | |
113 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), | |
114 channel_switch.length())); | |
115 if (nwritten != static_cast<int>(channel_switch.length())) { | |
116 return false; | |
117 } | |
118 return true; | |
119 } | |
OLD | NEW |