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

Side by Side Diff: services/service_manager/runner/host/child_process_host.cc

Issue 2576233002: Consolidating the mojo NativeRunner functionality. (Closed)
Patch Set: Merged mojo_runner_host_unittests in service_manager_unittests Created 4 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "services/service_manager/runner/host/child_process_host.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10
11 #include "base/base_paths.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/files/file_path.h"
15 #include "base/location.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/path_service.h"
20 #include "base/process/kill.h"
21 #include "base/process/launch.h"
22 #include "base/synchronization/lock.h"
23 #include "base/task_runner.h"
24 #include "base/threading/thread_task_runner_handle.h"
25 #include "mojo/edk/embedder/embedder.h"
26 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
27 #include "mojo/public/cpp/system/core.h"
28 #include "services/service_manager/native_runner_delegate.h"
29 #include "services/service_manager/public/cpp/standalone_service/switches.h"
30 #include "services/service_manager/runner/common/client_util.h"
31 #include "services/service_manager/runner/common/switches.h"
32
33 #if defined(OS_LINUX)
34 #include "sandbox/linux/services/namespace_sandbox.h"
35 #endif
36
37 #if defined(OS_WIN)
38 #include "base/win/windows_version.h"
39 #endif
40
41 #if defined(OS_MACOSX)
42 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h"
43 #endif
44
45 #if defined(OS_WIN) && defined(COMPONENT_BUILD)
46 #include <windows.h>
47
48 namespace {
49
50 class ScopedDllDirectoryOverride {
51 public:
52 explicit ScopedDllDirectoryOverride(const base::FilePath& path) {
53 SetDllDirectory(path.value().c_str());
54 }
55
56 ~ScopedDllDirectoryOverride() {
57 SetDllDirectory(NULL);
58 }
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(ScopedDllDirectoryOverride);
62 };
63
64 } // namespace
65 #endif // defined(OS_WIN) && defined(COMPONENT_BUILD)
66
67 namespace service_manager {
68
69 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner,
70 NativeRunnerDelegate* delegate,
71 bool start_sandboxed,
72 const Identity& target,
73 const base::FilePath& service_path)
74 : launch_process_runner_(launch_process_runner),
75 delegate_(delegate),
76 start_sandboxed_(start_sandboxed),
77 target_(target),
78 service_path_(service_path),
79 child_token_(mojo::edk::GenerateRandomToken()),
80 start_child_process_event_(
81 base::WaitableEvent::ResetPolicy::AUTOMATIC,
82 base::WaitableEvent::InitialState::NOT_SIGNALED),
83 weak_factory_(this) {
84 if (service_path_.empty())
85 service_path_ = base::CommandLine::ForCurrentProcess()->GetProgram();
86 }
87
88 ChildProcessHost::~ChildProcessHost() {
89 DCHECK(!mojo_ipc_channel_)
90 << "Destroying ChildProcessHost before calling Join";
91 }
92
93 mojom::ServicePtr ChildProcessHost::Start(
94 const Identity& target,
95 const ProcessReadyCallback& callback,
96 const base::Closure& quit_closure) {
97 DCHECK(!child_process_.IsValid());
98
99 const base::CommandLine& parent_command_line =
100 *base::CommandLine::ForCurrentProcess();
101
102 std::unique_ptr<base::CommandLine> child_command_line(
103 new base::CommandLine(service_path_));
104
105 child_command_line->AppendArguments(parent_command_line, false);
106
107 child_command_line->AppendSwitchASCII(::switches::kProcessServiceName,
108 target.name());
109 #ifndef NDEBUG
110 child_command_line->AppendSwitchASCII("u", target.user_id());
111 #endif
112
113 if (start_sandboxed_)
114 child_command_line->AppendSwitch(::switches::kEnableSandbox);
115
116 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair);
117 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess(
118 child_command_line.get(), &handle_passing_info_);
119
120 mojom::ServicePtr client =
121 PassServiceRequestOnCommandLine(child_command_line.get(),
122 child_token_);
123 launch_process_runner_->PostTaskAndReply(
124 FROM_HERE,
125 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this),
126 base::Passed(&child_command_line)),
127 base::Bind(&ChildProcessHost::DidStart,
128 weak_factory_.GetWeakPtr(), callback));
129 return client;
130 }
131
132 void ChildProcessHost::Join() {
133 if (mojo_ipc_channel_)
134 start_child_process_event_.Wait();
135 mojo_ipc_channel_.reset();
136 if (child_process_.IsValid()) {
137 int rv = -1;
138 LOG_IF(ERROR, !child_process_.WaitForExit(&rv))
139 << "Failed to wait for child process";
140 child_process_.Close();
141 }
142 }
143
144 void ChildProcessHost::DidStart(const ProcessReadyCallback& callback) {
145 if (child_process_.IsValid()) {
146 callback.Run(child_process_.Pid());
147 } else {
148 LOG(ERROR) << "Failed to start child process";
149 mojo_ipc_channel_.reset();
150 callback.Run(base::kNullProcessId);
151 }
152 }
153
154 void ChildProcessHost::DoLaunch(
155 std::unique_ptr<base::CommandLine> child_command_line) {
156 if (delegate_) {
157 delegate_->AdjustCommandLineArgumentsForTarget(target_,
158 child_command_line.get());
159 }
160
161 base::LaunchOptions options;
162
163 base::FilePath exe_dir;
164 base::PathService::Get(base::DIR_EXE, &exe_dir);
165 options.current_directory = exe_dir;
166
167 // The service should look for ICU data next to the service runner's
168 // executable rather than its own.
169 child_command_line->AppendSwitchPath(switches::kIcuDataDir, exe_dir);
170
171 #if defined(OS_POSIX)
172 // We need the dynamic loader to be able to locate things like libbase.so
173 // in component builds, as well as some other dynamic runtime dependencies in
174 // other build environments (e.g. libosmesa.so). For this we set
175 // LD_LIBRARY_PATH to the service runner's executable path where such
176 // artifacts are typically expected to reside.
177 options.environ["LD_LIBRARY_PATH"] = exe_dir.value();
178 #endif
179
180 #if defined(OS_WIN)
181 options.handles_to_inherit = &handle_passing_info_;
182 #if defined(OFFICIAL_BUILD)
183 CHECK(false) << "Launching mojo process with inherit_handles is insecure!";
184 #endif
185 options.inherit_handles = true;
186 options.stdin_handle = INVALID_HANDLE_VALUE;
187 options.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
188 options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
189 // Always inherit stdout/stderr as a pair.
190 if (!options.stdout_handle || !options.stdin_handle)
191 options.stdin_handle = options.stdout_handle = nullptr;
192
193 // Pseudo handles are used when stdout and stderr redirect to the console. In
194 // that case, they're automatically inherited by child processes. See
195 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx
196 // Trying to add them to the list of handles to inherit causes CreateProcess
197 // to fail. When this process is launched from Python then a real handle is
198 // used. In that case, we do want to add it to the list of handles that is
199 // inherited.
200 if (options.stdout_handle &&
201 GetFileType(options.stdout_handle) != FILE_TYPE_CHAR) {
202 handle_passing_info_.push_back(options.stdout_handle);
203 }
204 if (options.stderr_handle &&
205 GetFileType(options.stderr_handle) != FILE_TYPE_CHAR &&
206 options.stdout_handle != options.stderr_handle) {
207 handle_passing_info_.push_back(options.stderr_handle);
208 }
209 #elif defined(OS_POSIX)
210 handle_passing_info_.push_back(std::make_pair(STDIN_FILENO, STDIN_FILENO));
211 handle_passing_info_.push_back(std::make_pair(STDOUT_FILENO, STDOUT_FILENO));
212 handle_passing_info_.push_back(std::make_pair(STDERR_FILENO, STDERR_FILENO));
213 options.fds_to_remap = &handle_passing_info_;
214 #endif
215 DVLOG(2) << "Launching child with command line: "
216 << child_command_line->GetCommandLineString();
217 #if defined(OS_LINUX)
218 if (start_sandboxed_) {
219 child_process_ =
220 sandbox::NamespaceSandbox::LaunchProcess(*child_command_line, options);
221 if (!child_process_.IsValid()) {
222 LOG(ERROR) << "Starting the process with a sandbox failed. Missing kernel"
223 << " support.";
224 }
225 } else
226 #endif
227 {
228 #if defined(OS_MACOSX)
229 MachBroker* mach_broker = MachBroker::GetInstance();
230 base::AutoLock locker(mach_broker->GetLock());
231 #elif defined(OS_WIN) && defined(COMPONENT_BUILD)
232 ScopedDllDirectoryOverride dll_override(exe_dir);
233 #endif
234 child_process_ = base::LaunchProcess(*child_command_line, options);
235 #if defined(OS_MACOSX)
236 mach_broker->ExpectPid(child_process_.Handle());
237 #endif
238 }
239
240 if (child_process_.IsValid()) {
241 DVLOG(0) << "Launched child process pid=" << child_process_.Pid()
242 << ", instance=" << target_.instance()
243 << ", name=" << target_.name()
244 << ", user_id=" << target_.user_id();
245
246 if (mojo_ipc_channel_.get()) {
247 mojo_ipc_channel_->ChildProcessLaunched();
248 mojo::edk::ChildProcessLaunched(
249 child_process_.Handle(),
250 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(
251 mojo_ipc_channel_->PassServerHandle().release().handle)),
252 child_token_);
253 }
254 }
255 start_child_process_event_.Signal();
256 }
257
258 } // namespace service_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698