OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/nacl_fork_delegate_linux.h" | 5 #include "chrome/common/nacl_fork_delegate_linux.h" |
6 | 6 |
7 #include <signal.h> | 7 #include <signal.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/eintr_wrapper.h" | 13 #include "base/eintr_wrapper.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/path_service.h" | |
17 #include "base/process_util.h" | 16 #include "base/process_util.h" |
18 #include "content/common/unix_domain_socket_posix.h" | 17 #include "content/common/unix_domain_socket_posix.h" |
19 #include "content/common/zygote_fork_delegate_linux.h" | 18 #include "content/common/zygote_fork_delegate_linux.h" |
20 #include "chrome/common/chrome_paths.h" | |
21 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/nacl_helper_linux.h" | 20 #include "chrome/common/nacl_helper_linux.h" |
23 | 21 |
24 NaClForkDelegate::NaClForkDelegate() | 22 NaClForkDelegate::NaClForkDelegate() |
25 : ready_(false), | 23 : ready_(false), |
26 sandboxed_(false), | 24 sandboxed_(false), |
27 fd_(-1) {} | 25 fd_(-1) {} |
28 | 26 |
29 void NaClForkDelegate::Init(const bool sandboxed, | 27 void NaClForkDelegate::Init(const bool sandboxed, |
30 const int browserdesc, | 28 const int browserdesc, |
31 const int sandboxdesc) { | 29 const int sandboxdesc) { |
32 VLOG(1) << "NaClForkDelegate::Init()"; | 30 VLOG(1) << "NaClForkDelegate::Init()"; |
33 int fds[2]; | 31 int fds[2]; |
34 | 32 |
35 sandboxed_ = sandboxed; | 33 sandboxed_ = sandboxed; |
36 // Confirm a couple hard-wired assumptions. | 34 // Confirm a couple hard-wired assumptions. |
37 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h | 35 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h |
38 DCHECK(kNaClBrowserDescriptor == browserdesc); | 36 DCHECK(kNaClBrowserDescriptor == browserdesc); |
39 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | 37 DCHECK(kNaClSandboxDescriptor == sandboxdesc); |
40 | 38 |
41 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | 39 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); |
42 base::file_handle_mapping_vector fds_to_map; | 40 base::file_handle_mapping_vector fds_to_map; |
43 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | 41 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); |
44 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | 42 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); |
45 // TODO(bradchen): To make this the default for release builds, | 43 // TODO(bradchen): Before making this the default for release builds, |
46 // remove command line switch. | 44 // replace command line switch with PathService::Get(). |
| 45 const std::string nacl_zygote_exe = |
| 46 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 47 switches::kNaClLinuxHelper); |
47 ready_ = false; | 48 ready_ = false; |
48 const bool use_helper = CommandLine::ForCurrentProcess()->HasSwitch( | 49 if (nacl_zygote_exe.length() != 0) { |
49 switches::kNaClLinuxHelper); | |
50 FilePath helper_exe; | |
51 if (use_helper && PathService::Get(chrome::FILE_NACL_HELPER, &helper_exe)) { | |
52 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); | 50 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); |
53 argv[0] = helper_exe.value(); | 51 argv[0] = nacl_zygote_exe; |
54 base::LaunchOptions options; | 52 base::LaunchOptions options; |
55 options.fds_to_remap = &fds_to_map; | 53 options.fds_to_remap = &fds_to_map; |
56 options.clone_flags = CLONE_FS | SIGCHLD; | 54 options.clone_flags = CLONE_FS | SIGCHLD; |
57 // LD_BIND_NOW forces non-lazy binding in the dynamic linker, to | |
58 // prevent the linker from trying to look at the text of the nacl_helper | |
59 // program after it has been replaced by the nacl module. | |
60 base::environment_vector env; | |
61 env.push_back(std::make_pair("LD_BIND_NOW", "1")); | |
62 options.environ = &env; | |
63 ready_ = base::LaunchProcess(argv, options, NULL); | 55 ready_ = base::LaunchProcess(argv, options, NULL); |
64 // parent and error cases are handled below | 56 // parent and error cases are handled below |
65 } | 57 } |
66 if (HANDLE_EINTR(close(fds[1])) != 0) | 58 if (HANDLE_EINTR(close(fds[1])) != 0) |
67 LOG(ERROR) << "close(fds[1]) failed"; | 59 LOG(ERROR) << "close(fds[1]) failed"; |
68 if (ready_) { | 60 if (ready_) { |
69 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | 61 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); |
70 char buf[kExpectedLength]; | 62 char buf[kExpectedLength]; |
71 | 63 |
72 // Wait for ack from nacl_helper, indicating it is ready to help | 64 // Wait for ack from nacl_helper, indicating it is ready to help |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 112 |
121 bool NaClForkDelegate::AckChild(const int fd, | 113 bool NaClForkDelegate::AckChild(const int fd, |
122 const std::string& channel_switch) { | 114 const std::string& channel_switch) { |
123 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), | 115 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), |
124 channel_switch.length())); | 116 channel_switch.length())); |
125 if (nwritten != static_cast<int>(channel_switch.length())) { | 117 if (nwritten != static_cast<int>(channel_switch.length())) { |
126 return false; | 118 return false; |
127 } | 119 } |
128 return true; | 120 return true; |
129 } | 121 } |
OLD | NEW |