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/logging.h" | |
12 #include "base/file_path.h" | |
13 #include "base/process_util.h" | |
14 #include "content/common/unix_domain_socket_posix.h" | |
15 #include "content/common/zygote_fork_delegate.h" | |
16 #include "chrome/browser/nacl_fork_delegate.h" | |
17 #include "chrome/common/chrome_switches.h" | |
18 #include "chrome/common/nacl_helper_linux.h" | |
19 | |
20 NaClForkDelegate::NaClForkDelegate() : sandboxed_(false), | |
21 fd_(-1), | |
22 pid_(-1) { | |
23 } | |
24 | |
25 void NaClForkDelegate::Init(const bool sandboxed, | |
26 const int browserdesc, | |
27 const int sandboxdesc) { | |
28 VLOG(1) << "NaClForkDelegate::Init()"; | |
29 int fds[2]; | |
30 | |
31 sandboxed_ = sandboxed; | |
32 // Confirm a couple hard-wired assumptions. | |
33 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h | |
34 DCHECK(kNaClBrowserDescriptor == browserdesc); | |
35 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | |
36 | |
37 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
38 base::file_handle_mapping_vector fds_to_map; | |
39 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | |
40 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | |
41 | |
42 const char* nacl_zygote_exe = getenv("NACL_NEW_ZYGOTE"); | |
43 pid_ = -1; | |
44 if (NULL != 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_, | |
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
| |
48 CLONE_FS | SIGCHLD); | |
49 } | |
50 close(fds[1]); | |
agl
2011/06/20 17:28:55
HANDLE_EINTR around this.
Brad Chen
2011/06/20 22:09:47
Done.
| |
51 if (pid_ > 0) { | |
52 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.
| |
53 char buf[kExpectedLength]; | |
54 | |
55 // Wait for ack from nacl_helper, indicating it is ready to help | |
56 int nread = read(fds[0], buf, sizeof(buf)); | |
57 if (nread == kExpectedLength && | |
58 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
59 // all is well | |
60 fd_ = fds[0]; | |
61 return; | |
62 } else { | |
63 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes"; | |
64 } | |
65 } | |
66 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
67 // becomes the default. | |
68 VLOG(1) << "Could not launch NaCl helper"; | |
69 pid_ = -1; | |
70 fd_ = -1; | |
71 close(fds[0]); | |
agl
2011/06/20 17:28:55
HANDLE_EINTR
Brad Chen
2011/06/20 22:09:47
Done.
| |
72 } | |
73 | |
74 NaClForkDelegate::~NaClForkDelegate() { | |
75 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.
| |
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 sizeof(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 |