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" | 16 #include "base/path_service.h" |
17 #include "base/process_util.h" | 17 #include "base/process_util.h" |
18 #include "content/common/unix_domain_socket_posix.h" | 18 #include "content/common/unix_domain_socket_posix.h" |
19 #include "content/common/zygote_fork_delegate_linux.h" | 19 #include "content/common/zygote_fork_delegate_linux.h" |
20 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/nacl_helper_linux.h" | 22 #include "chrome/common/nacl_helper_linux.h" |
23 | 23 |
24 NaClForkDelegate::NaClForkDelegate() | 24 NaClForkDelegate::NaClForkDelegate() |
25 : ready_(false), | 25 : ready_(false), |
26 sandboxed_(false), | 26 sandboxed_(false), |
27 fd_(-1) {} | 27 fd_(-1) {} |
28 | 28 |
29 const char kNaClHelperAtZero[] = "--at-zero"; | |
30 | |
31 void NaClForkDelegate::Init(const bool sandboxed, | 29 void NaClForkDelegate::Init(const bool sandboxed, |
32 const int browserdesc, | 30 const int browserdesc, |
33 const int sandboxdesc) { | 31 const int sandboxdesc) { |
34 VLOG(1) << "NaClForkDelegate::Init()"; | 32 VLOG(1) << "NaClForkDelegate::Init()"; |
35 int fds[2]; | 33 int fds[2]; |
36 | 34 |
37 sandboxed_ = sandboxed; | 35 sandboxed_ = sandboxed; |
38 // Confirm a couple hard-wired assumptions. | 36 // Confirm a couple hard-wired assumptions. |
39 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h | 37 // The NaCl constants are from chrome/nacl/nacl_linux_helper.h |
40 DCHECK(kNaClBrowserDescriptor == browserdesc); | 38 DCHECK(kNaClBrowserDescriptor == browserdesc); |
41 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | 39 DCHECK(kNaClSandboxDescriptor == sandboxdesc); |
42 | 40 |
43 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | 41 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); |
44 base::file_handle_mapping_vector fds_to_map; | 42 base::file_handle_mapping_vector fds_to_map; |
45 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | 43 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); |
46 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | 44 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); |
47 // TODO(bradchen): To make this the default for release builds, | 45 // TODO(bradchen): To make this the default for release builds, |
48 // remove command line switch. | 46 // remove command line switch. |
49 ready_ = false; | 47 ready_ = false; |
50 const bool use_helper = CommandLine::ForCurrentProcess()->HasSwitch( | 48 const bool use_helper = CommandLine::ForCurrentProcess()->HasSwitch( |
51 switches::kNaClLinuxHelper); | 49 switches::kNaClLinuxHelper); |
52 FilePath helper_exe; | 50 FilePath helper_exe; |
53 FilePath helper_bootstrap_exe; | 51 if (use_helper && PathService::Get(chrome::FILE_NACL_HELPER, &helper_exe)) { |
54 if (use_helper && | |
55 PathService::Get(chrome::FILE_NACL_HELPER, &helper_exe) && | |
56 PathService::Get(chrome::FILE_NACL_HELPER_BOOTSTRAP, | |
57 &helper_bootstrap_exe)) { | |
58 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); | 52 CommandLine::StringVector argv = CommandLine::ForCurrentProcess()->argv(); |
59 argv[0] = helper_bootstrap_exe.value(); | 53 argv[0] = helper_exe.value(); |
60 argv[1] = helper_exe.value(); | |
61 argv[2] = kNaClHelperAtZero; | |
62 base::LaunchOptions options; | 54 base::LaunchOptions options; |
63 options.fds_to_remap = &fds_to_map; | 55 options.fds_to_remap = &fds_to_map; |
64 options.clone_flags = CLONE_FS | SIGCHLD; | 56 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; |
65 ready_ = base::LaunchProcess(argv, options, NULL); | 63 ready_ = base::LaunchProcess(argv, options, NULL); |
66 // parent and error cases are handled below | 64 // parent and error cases are handled below |
67 } | 65 } |
68 if (HANDLE_EINTR(close(fds[1])) != 0) | 66 if (HANDLE_EINTR(close(fds[1])) != 0) |
69 LOG(ERROR) << "close(fds[1]) failed"; | 67 LOG(ERROR) << "close(fds[1]) failed"; |
70 if (ready_) { | 68 if (ready_) { |
71 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | 69 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); |
72 char buf[kExpectedLength]; | 70 char buf[kExpectedLength]; |
73 | 71 |
74 // Wait for ack from nacl_helper, indicating it is ready to help | 72 // 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... |
122 | 120 |
123 bool NaClForkDelegate::AckChild(const int fd, | 121 bool NaClForkDelegate::AckChild(const int fd, |
124 const std::string& channel_switch) { | 122 const std::string& channel_switch) { |
125 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), | 123 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), |
126 channel_switch.length())); | 124 channel_switch.length())); |
127 if (nwritten != static_cast<int>(channel_switch.length())) { | 125 if (nwritten != static_cast<int>(channel_switch.length())) { |
128 return false; | 126 return false; |
129 } | 127 } |
130 return true; | 128 return true; |
131 } | 129 } |
OLD | NEW |