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