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

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

Issue 2557213002: Build services as standalone executables (Closed)
Patch Set: remove DCHECKs with side effects -_- 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
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 "services/service_manager/runner/host/child_process_host.h" 5 #include "services/service_manager/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/base_paths.h"
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/command_line.h" 13 #include "base/command_line.h"
13 #include "base/location.h" 14 #include "base/location.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/path_service.h"
17 #include "base/process/kill.h" 19 #include "base/process/kill.h"
18 #include "base/process/launch.h" 20 #include "base/process/launch.h"
19 #include "base/synchronization/lock.h" 21 #include "base/synchronization/lock.h"
20 #include "base/task_runner.h" 22 #include "base/task_runner.h"
21 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
22 #include "mojo/edk/embedder/embedder.h" 24 #include "mojo/edk/embedder/embedder.h"
23 #include "mojo/public/cpp/bindings/interface_ptr_info.h" 25 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
24 #include "mojo/public/cpp/system/core.h" 26 #include "mojo/public/cpp/system/core.h"
25 #include "services/service_manager/native_runner_delegate.h" 27 #include "services/service_manager/native_runner_delegate.h"
28 #include "services/service_manager/public/cpp/standalone_service/switches.h"
26 #include "services/service_manager/runner/common/client_util.h" 29 #include "services/service_manager/runner/common/client_util.h"
27 #include "services/service_manager/runner/common/switches.h" 30 #include "services/service_manager/runner/common/switches.h"
28 31
29 #if defined(OS_LINUX) 32 #if defined(OS_LINUX)
30 #include "sandbox/linux/services/namespace_sandbox.h" 33 #include "sandbox/linux/services/namespace_sandbox.h"
31 #endif 34 #endif
32 35
33 #if defined(OS_WIN) 36 #if defined(OS_WIN)
34 #include "base/win/windows_version.h" 37 #include "base/win/windows_version.h"
35 #endif 38 #endif
36 39
37 #if defined(OS_MACOSX) 40 #if defined(OS_MACOSX)
38 #include "services/service_manager/runner/host/mach_broker.h" 41 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h"
39 #endif 42 #endif
40 43
41 namespace service_manager { 44 namespace service_manager {
42 45
43 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner, 46 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner,
44 NativeRunnerDelegate* delegate, 47 NativeRunnerDelegate* delegate,
45 bool start_sandboxed, 48 bool start_sandboxed,
46 const Identity& target, 49 const Identity& target,
47 const base::FilePath& app_path) 50 const base::FilePath& service_path)
48 : launch_process_runner_(launch_process_runner), 51 : launch_process_runner_(launch_process_runner),
49 delegate_(delegate), 52 delegate_(delegate),
50 start_sandboxed_(start_sandboxed), 53 start_sandboxed_(start_sandboxed),
51 target_(target), 54 target_(target),
52 app_path_(app_path), 55 service_path_(service_path),
53 child_token_(mojo::edk::GenerateRandomToken()), 56 child_token_(mojo::edk::GenerateRandomToken()),
54 start_child_process_event_( 57 start_child_process_event_(
55 base::WaitableEvent::ResetPolicy::AUTOMATIC, 58 base::WaitableEvent::ResetPolicy::AUTOMATIC,
56 base::WaitableEvent::InitialState::NOT_SIGNALED), 59 base::WaitableEvent::InitialState::NOT_SIGNALED),
57 weak_factory_(this) {} 60 weak_factory_(this) {
61 if (service_path_.empty())
62 service_path_ = base::CommandLine::ForCurrentProcess()->GetProgram();
63 }
58 64
59 ChildProcessHost::~ChildProcessHost() { 65 ChildProcessHost::~ChildProcessHost() {
60 if (!app_path_.empty()) { 66 DCHECK(!mojo_ipc_channel_)
61 CHECK(!mojo_ipc_channel_) 67 << "Destroying ChildProcessHost before calling Join";
62 << "Destroying ChildProcessHost before calling Join";
63 }
64 } 68 }
65 69
66 mojom::ServicePtr ChildProcessHost::Start( 70 mojom::ServicePtr ChildProcessHost::Start(
67 const Identity& target, 71 const Identity& target,
68 const ProcessReadyCallback& callback, 72 const ProcessReadyCallback& callback,
69 const base::Closure& quit_closure) { 73 const base::Closure& quit_closure) {
70 DCHECK(!child_process_.IsValid()); 74 DCHECK(!child_process_.IsValid());
71 75
72 const base::CommandLine* parent_command_line = 76 const base::CommandLine& parent_command_line =
73 base::CommandLine::ForCurrentProcess(); 77 *base::CommandLine::ForCurrentProcess();
74 base::FilePath target_path = parent_command_line->GetProgram();
75 // |app_path_| can be empty in tests.
76 if (!app_path_.MatchesExtension(FILE_PATH_LITERAL(".library")) &&
77 !app_path_.empty()) {
78 target_path = app_path_;
79 }
80 78
81 std::unique_ptr<base::CommandLine> child_command_line( 79 std::unique_ptr<base::CommandLine> child_command_line(
82 new base::CommandLine(target_path)); 80 new base::CommandLine(service_path_));
83 81
84 child_command_line->AppendArguments(*parent_command_line, false); 82 child_command_line->AppendArguments(parent_command_line, false);
85 83
86 #ifndef NDEBUG 84 #ifndef NDEBUG
87 child_command_line->AppendSwitchASCII("n", target.name()); 85 child_command_line->AppendSwitchASCII("n", target.name());
88 child_command_line->AppendSwitchASCII("u", target.user_id()); 86 child_command_line->AppendSwitchASCII("u", target.user_id());
89 #endif 87 #endif
90 88
91 if (target_path != app_path_)
92 child_command_line->AppendSwitchPath(switches::kChildProcess, app_path_);
93
94 if (start_sandboxed_) 89 if (start_sandboxed_)
95 child_command_line->AppendSwitch(switches::kEnableSandbox); 90 child_command_line->AppendSwitch(::switches::kEnableSandbox);
96 91
97 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); 92 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair);
98 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( 93 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess(
99 child_command_line.get(), &handle_passing_info_); 94 child_command_line.get(), &handle_passing_info_);
100 95
101 mojom::ServicePtr client = 96 mojom::ServicePtr client =
102 PassServiceRequestOnCommandLine(child_command_line.get(), 97 PassServiceRequestOnCommandLine(child_command_line.get(),
103 child_token_); 98 child_token_);
104 launch_process_runner_->PostTaskAndReply( 99 launch_process_runner_->PostTaskAndReply(
105 FROM_HERE, 100 FROM_HERE,
(...skipping 27 matching lines...) Expand all
133 } 128 }
134 129
135 void ChildProcessHost::DoLaunch( 130 void ChildProcessHost::DoLaunch(
136 std::unique_ptr<base::CommandLine> child_command_line) { 131 std::unique_ptr<base::CommandLine> child_command_line) {
137 if (delegate_) { 132 if (delegate_) {
138 delegate_->AdjustCommandLineArgumentsForTarget(target_, 133 delegate_->AdjustCommandLineArgumentsForTarget(target_,
139 child_command_line.get()); 134 child_command_line.get());
140 } 135 }
141 136
142 base::LaunchOptions options; 137 base::LaunchOptions options;
138
139 base::FilePath exe_dir;
140 base::PathService::Get(base::DIR_EXE, &exe_dir);
141 options.current_directory = exe_dir;
142
143 // The service should look for ICU data next to the service runner's
144 // executable rather than its own.
145 child_command_line->AppendSwitchPath(switches::kIcuDataDir, exe_dir);
146
147 #if defined(OS_POSIX)
148 // We need the dynamic loader to be able to locate things like libbase.so
149 // in component builds, as well as some other dynamic runtime dependencies in
150 // other build environments (e.g. libosmesa.so). For this we set
151 // LD_LIBRARY_PATH to the service runner's executable path where such
152 // artifacts are typically expected to reside.
153 options.environ["LD_LIBRARY_PATH"] = exe_dir.value();
154 #endif
155
143 #if defined(OS_WIN) 156 #if defined(OS_WIN)
144 options.handles_to_inherit = &handle_passing_info_; 157 options.handles_to_inherit = &handle_passing_info_;
145 #if defined(OFFICIAL_BUILD) 158 #if defined(OFFICIAL_BUILD)
146 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; 159 CHECK(false) << "Launching mojo process with inherit_handles is insecure!";
147 #endif 160 #endif
148 options.inherit_handles = true; 161 options.inherit_handles = true;
149 options.stdin_handle = INVALID_HANDLE_VALUE; 162 options.stdin_handle = INVALID_HANDLE_VALUE;
150 options.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); 163 options.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
151 options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE); 164 options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
152 // Always inherit stdout/stderr as a pair. 165 // Always inherit stdout/stderr as a pair.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 child_process_.Handle(), 223 child_process_.Handle(),
211 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( 224 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(
212 mojo_ipc_channel_->PassServerHandle().release().handle)), 225 mojo_ipc_channel_->PassServerHandle().release().handle)),
213 child_token_); 226 child_token_);
214 } 227 }
215 } 228 }
216 start_child_process_event_.Signal(); 229 start_child_process_event_.Signal();
217 } 230 }
218 231
219 } // namespace service_manager 232 } // namespace service_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698