Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: mojo/shell/runner/host/child_process_host.cc

Issue 1801963002: Change primordial pipes to ShellClient (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase onto catalog CL Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "mojo/shell/runner/host/child_process_host.h" 5 #include "mojo/shell/runner/host/child_process_host.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/process/kill.h" 17 #include "base/process/kill.h"
18 #include "base/process/launch.h" 18 #include "base/process/launch.h"
19 #include "base/task_runner.h" 19 #include "base/task_runner.h"
20 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
21 #include "mojo/edk/embedder/embedder.h" 21 #include "mojo/edk/embedder/embedder.h"
22 #include "mojo/public/cpp/bindings/interface_ptr_info.h" 22 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
23 #include "mojo/public/cpp/system/core.h" 23 #include "mojo/public/cpp/system/core.h"
24 #include "mojo/shell/native_runner_delegate.h" 24 #include "mojo/shell/native_runner_delegate.h"
25 #include "mojo/shell/runner/common/client_util.h"
25 #include "mojo/shell/runner/common/switches.h" 26 #include "mojo/shell/runner/common/switches.h"
26 27
27 #if defined(OS_LINUX) && !defined(OS_ANDROID) 28 #if defined(OS_LINUX) && !defined(OS_ANDROID)
28 #include "sandbox/linux/services/namespace_sandbox.h" 29 #include "sandbox/linux/services/namespace_sandbox.h"
29 #endif 30 #endif
30 31
31 #if defined(OS_WIN) 32 #if defined(OS_WIN)
32 #include "base/win/windows_version.h" 33 #include "base/win/windows_version.h"
33 #endif 34 #endif
34 35
35 namespace mojo { 36 namespace mojo {
36 namespace shell { 37 namespace shell {
37 38
38 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner, 39 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner,
39 NativeRunnerDelegate* delegate, 40 NativeRunnerDelegate* delegate,
40 bool start_sandboxed, 41 bool start_sandboxed,
41 const Identity& target, 42 const Identity& target,
42 const base::FilePath& app_path) 43 const base::FilePath& app_path)
43 : launch_process_runner_(launch_process_runner), 44 : launch_process_runner_(launch_process_runner),
44 delegate_(delegate), 45 delegate_(delegate),
45 start_sandboxed_(start_sandboxed), 46 start_sandboxed_(start_sandboxed),
46 target_(target), 47 target_(target),
47 app_path_(app_path), 48 app_path_(app_path),
48 start_child_process_event_(false, false), 49 start_child_process_event_(false, false),
49 weak_factory_(this) { 50 weak_factory_(this) {
50 node_channel_.reset(new edk::PlatformChannelPair);
51 primordial_pipe_token_ = edk::GenerateRandomToken();
52 factory_.Bind(InterfacePtrInfo<mojom::ShellClientFactory>(
53 edk::CreateParentMessagePipe(primordial_pipe_token_), 0u));
54 } 51 }
55 52
56 ChildProcessHost::~ChildProcessHost() { 53 ChildProcessHost::~ChildProcessHost() {
57 if (!app_path_.empty()) 54 if (!app_path_.empty()) {
58 CHECK(!factory_) << "Destroying ChildProcessHost before calling Join"; 55 CHECK(!mojo_ipc_channel_)
56 << "Destroying ChildProcessHost before calling Join";
57 }
59 } 58 }
60 59
61 void ChildProcessHost::Start(mojom::ShellClientRequest request, 60 mojom::ShellClientPtr ChildProcessHost::Start(
62 const String& name, 61 const String& name,
63 const ProcessReadyCallback& callback, 62 const ProcessReadyCallback& callback,
64 const base::Closure& quit_closure) { 63 const base::Closure& quit_closure) {
65 DCHECK(!child_process_.IsValid()); 64 DCHECK(!child_process_.IsValid());
66 // Request is invalid in child_process_host_unittest. 65
67 if (request.is_pending()) { 66 const base::CommandLine* parent_command_line =
68 factory_->CreateShellClient(std::move(request), name); 67 base::CommandLine::ForCurrentProcess();
69 factory_.set_connection_error_handler(quit_closure); 68 base::FilePath target_path = parent_command_line->GetProgram();
69 // |app_path_| can be empty in tests.
70 if (!app_path_.MatchesExtension(FILE_PATH_LITERAL(".mojo")) &&
71 !app_path_.empty()) {
72 target_path = app_path_;
70 } 73 }
74
75 scoped_ptr<base::CommandLine> child_command_line(
76 new base::CommandLine(target_path));
77
78 child_command_line->AppendArguments(*parent_command_line, false);
79
80 if (target_path != app_path_)
81 child_command_line->AppendSwitchPath(switches::kChildProcess, app_path_);
82
83 if (start_sandboxed_)
84 child_command_line->AppendSwitch(switches::kEnableSandbox);
85
86 mojo_ipc_channel_.reset(new edk::PlatformChannelPair);
87 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess(
88 child_command_line.get(), &handle_passing_info_);
89
90 mojom::ShellClientPtr client =
91 PassShellClientRequestOnCommandLine(child_command_line.get());
71 launch_process_runner_->PostTaskAndReply( 92 launch_process_runner_->PostTaskAndReply(
72 FROM_HERE, 93 FROM_HERE,
73 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), 94 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this),
95 base::Passed(&child_command_line)),
74 base::Bind(&ChildProcessHost::DidStart, 96 base::Bind(&ChildProcessHost::DidStart,
75 weak_factory_.GetWeakPtr(), callback)); 97 weak_factory_.GetWeakPtr(), callback));
98 return client;
76 } 99 }
77 100
78 void ChildProcessHost::Join() { 101 void ChildProcessHost::Join() {
79 if (factory_) 102 if (mojo_ipc_channel_)
80 start_child_process_event_.Wait(); 103 start_child_process_event_.Wait();
81 104 mojo_ipc_channel_.reset();
82 factory_.reset();
83
84 // This host may be hosting a child process whose lifetime is controlled
85 // elsewhere. In this case we have no known process handle to wait on.
86 if (child_process_.IsValid()) { 105 if (child_process_.IsValid()) {
87 int rv = -1; 106 int rv = -1;
88 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) 107 LOG_IF(ERROR, !child_process_.WaitForExit(&rv))
89 << "Failed to wait for child process"; 108 << "Failed to wait for child process";
90
91 child_process_.Close(); 109 child_process_.Close();
92 } 110 }
93 } 111 }
94 112
95 void ChildProcessHost::DidStart(const ProcessReadyCallback& callback) { 113 void ChildProcessHost::DidStart(const ProcessReadyCallback& callback) {
96 if (child_process_.IsValid()) { 114 if (child_process_.IsValid()) {
97 callback.Run(child_process_.Pid()); 115 callback.Run(child_process_.Pid());
98 } else { 116 } else {
99 LOG(ERROR) << "Failed to start child process"; 117 LOG(ERROR) << "Failed to start child process";
100 factory_.reset(); 118 mojo_ipc_channel_.reset();
101 } 119 }
102 } 120 }
103 121
104 void ChildProcessHost::DoLaunch() { 122 void ChildProcessHost::DoLaunch(
105 const base::CommandLine* parent_command_line = 123 scoped_ptr<base::CommandLine> child_command_line) {
106 base::CommandLine::ForCurrentProcess();
107 base::FilePath target_path = parent_command_line->GetProgram();
108 // |app_path_| can be empty in tests.
109 if (!app_path_.MatchesExtension(FILE_PATH_LITERAL(".mojo")) &&
110 !app_path_.empty()) {
111 target_path = app_path_;
112 }
113
114 base::CommandLine child_command_line(target_path);
115 child_command_line.AppendArguments(*parent_command_line, false);
116
117 if (target_path != app_path_)
118 child_command_line.AppendSwitchPath(switches::kChildProcess, app_path_);
119
120 if (start_sandboxed_)
121 child_command_line.AppendSwitch(switches::kEnableSandbox);
122
123 if (node_channel_.get()) {
124 node_channel_->PrepareToPassClientHandleToChildProcess(
125 &child_command_line, &handle_passing_info_);
126 }
127
128 child_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken,
129 primordial_pipe_token_);
130
131 if (delegate_) { 124 if (delegate_) {
132 delegate_->AdjustCommandLineArgumentsForTarget(target_, 125 delegate_->AdjustCommandLineArgumentsForTarget(target_,
133 &child_command_line); 126 child_command_line.get());
134 } 127 }
135 128
136 base::LaunchOptions options; 129 base::LaunchOptions options;
137 #if defined(OS_WIN) 130 #if defined(OS_WIN)
138 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { 131 options.handles_to_inherit = &handle_passing_info_;
139 options.handles_to_inherit = &handle_passing_info_;
140 } else {
141 #if defined(OFFICIAL_BUILD) 132 #if defined(OFFICIAL_BUILD)
142 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; 133 CHECK(false) << "Launching mojo process with inherit_handles is insecure!";
143 #endif 134 #endif
144 options.inherit_handles = true; 135 options.inherit_handles = true;
145 }
146 options.stdin_handle = INVALID_HANDLE_VALUE; 136 options.stdin_handle = INVALID_HANDLE_VALUE;
147 options.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); 137 options.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
148 options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE); 138 options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
149 // Always inherit stdout/stderr as a pair. 139 // Always inherit stdout/stderr as a pair.
150 if (!options.stdout_handle || !options.stdin_handle) 140 if (!options.stdout_handle || !options.stdin_handle)
151 options.stdin_handle = options.stdout_handle = nullptr; 141 options.stdin_handle = options.stdout_handle = nullptr;
152 142
153 // Pseudo handles are used when stdout and stderr redirect to the console. In 143 // Pseudo handles are used when stdout and stderr redirect to the console. In
154 // that case, they're automatically inherited by child processes. See 144 // that case, they're automatically inherited by child processes. See
155 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx 145 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx
(...skipping 10 matching lines...) Expand all
166 options.stdout_handle != options.stderr_handle) { 156 options.stdout_handle != options.stderr_handle) {
167 handle_passing_info_.push_back(options.stderr_handle); 157 handle_passing_info_.push_back(options.stderr_handle);
168 } 158 }
169 #elif defined(OS_POSIX) 159 #elif defined(OS_POSIX)
170 handle_passing_info_.push_back(std::make_pair(STDIN_FILENO, STDIN_FILENO)); 160 handle_passing_info_.push_back(std::make_pair(STDIN_FILENO, STDIN_FILENO));
171 handle_passing_info_.push_back(std::make_pair(STDOUT_FILENO, STDOUT_FILENO)); 161 handle_passing_info_.push_back(std::make_pair(STDOUT_FILENO, STDOUT_FILENO));
172 handle_passing_info_.push_back(std::make_pair(STDERR_FILENO, STDERR_FILENO)); 162 handle_passing_info_.push_back(std::make_pair(STDERR_FILENO, STDERR_FILENO));
173 options.fds_to_remap = &handle_passing_info_; 163 options.fds_to_remap = &handle_passing_info_;
174 #endif 164 #endif
175 DVLOG(2) << "Launching child with command line: " 165 DVLOG(2) << "Launching child with command line: "
176 << child_command_line.GetCommandLineString(); 166 << child_command_line->GetCommandLineString();
177 #if defined(OS_LINUX) && !defined(OS_ANDROID) 167 #if defined(OS_LINUX) && !defined(OS_ANDROID)
178 if (start_sandboxed_) { 168 if (start_sandboxed_) {
179 child_process_ = 169 child_process_ =
180 sandbox::NamespaceSandbox::LaunchProcess(child_command_line, options); 170 sandbox::NamespaceSandbox::LaunchProcess(*child_command_line, options);
181 if (!child_process_.IsValid()) { 171 if (!child_process_.IsValid()) {
182 LOG(ERROR) << "Starting the process with a sandbox failed. Missing kernel" 172 LOG(ERROR) << "Starting the process with a sandbox failed. Missing kernel"
183 << " support."; 173 << " support.";
184 } 174 }
185 } else 175 } else
186 #endif 176 #endif
187 child_process_ = base::LaunchProcess(child_command_line, options); 177 child_process_ = base::LaunchProcess(*child_command_line, options);
188 178
189 if (child_process_.IsValid()) { 179 if (child_process_.IsValid()) {
190 platform_channel_pair_.ChildProcessLaunched(); 180 if (mojo_ipc_channel_.get()) {
191 if (node_channel_.get()) { 181 mojo_ipc_channel_->ChildProcessLaunched();
192 node_channel_->ChildProcessLaunched();
193 mojo::edk::ChildProcessLaunched( 182 mojo::edk::ChildProcessLaunched(
194 child_process_.Handle(), 183 child_process_.Handle(),
195 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( 184 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(
196 node_channel_->PassServerHandle().release().handle))); 185 mojo_ipc_channel_->PassServerHandle().release().handle)));
197 } 186 }
198 } 187 }
199 start_child_process_event_.Signal(); 188 start_child_process_event_.Signal();
200 } 189 }
201 190
202 } // namespace shell 191 } // namespace shell
203 } // namespace mojo 192 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/runner/host/child_process_host.h ('k') | mojo/shell/runner/host/child_process_host_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698