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.h" | |
17 #include "chrome/nacl/nacl_fork_delegate_linux.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "chrome/common/nacl_helper_linux.h" | |
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 const char* nacl_zygote_exe = getenv("NACL_NEW_ZYGOTE"); | |
gregoryd
2011/06/22 12:46:07
Will we need to set the environment variable on us
Brad Chen
2011/06/22 16:10:31
I've added a TODO() comment. Before enabling this
| |
43 pid_ = -1; | |
44 if (nacl_zygote_exe) { | |
45 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); | |
46 argv[0] = nacl_zygote_exe; | |
47 base::LaunchAppWithClone(argv, fds_to_map, false, &pid_, | |
48 CLONE_FS | SIGCHLD); | |
49 // parent and error cases are handled below | |
50 } | |
51 HANDLE_EINTR(close(fds[1])); | |
52 if (pid_ > 0) { | |
53 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | |
54 char buf[kExpectedLength]; | |
55 | |
56 // Wait for ack from nacl_helper, indicating it is ready to help | |
57 const ssize_t nread = read(fds[0], buf, sizeof(buf)); | |
58 if (nread == kExpectedLength && | |
59 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
60 // all is well | |
61 fd_ = fds[0]; | |
62 return; | |
63 } else { | |
64 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes"; | |
65 } | |
66 } | |
67 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
68 // becomes the default. | |
69 pid_ = -1; | |
70 fd_ = -1; | |
71 HANDLE_EINTR(close(fds[0])); | |
72 } | |
73 | |
74 NaClForkDelegate::~NaClForkDelegate() { | |
75 HANDLE_EINTR(close(fd_)); // side effect: delegate process will terminate | |
76 } | |
77 | |
78 bool NaClForkDelegate::CanHelp(const std::string& process_type) { | |
79 return (process_type == switches::kNaClLoaderProcess && (Pid() != -1)); | |
80 } | |
81 | |
82 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { | |
83 base::ProcessId naclchild; | |
84 VLOG(1) << "NaClForkDelegate::Fork"; | |
85 | |
86 if (sandboxed_) { | |
87 DCHECK(fds.size() == kNaClParentFDIndex + 1); | |
88 } else { | |
89 DCHECK(fds.size() == kNaClBrowserFDIndex + 1); | |
90 } | |
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 } | |
OLD | NEW |