OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/service_process_launcher.h" |
6 | |
7 #include <stdint.h> | |
8 | 6 |
9 #include <utility> | 7 #include <utility> |
10 | 8 |
11 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
12 #include "base/bind.h" | 10 #include "base/bind.h" |
13 #include "base/command_line.h" | 11 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | |
15 #include "base/location.h" | 12 #include "base/location.h" |
16 #include "base/logging.h" | 13 #include "base/logging.h" |
17 #include "base/macros.h" | |
18 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
19 #include "base/path_service.h" | 15 #include "base/path_service.h" |
20 #include "base/process/kill.h" | 16 #include "base/process/kill.h" |
21 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
22 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
23 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
24 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
25 #include "mojo/edk/embedder/embedder.h" | 21 #include "mojo/edk/embedder/embedder.h" |
26 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
27 #include "mojo/public/cpp/system/core.h" | 23 #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" | 24 #include "services/service_manager/public/cpp/standalone_service/switches.h" |
30 #include "services/service_manager/runner/common/client_util.h" | 25 #include "services/service_manager/runner/common/client_util.h" |
31 #include "services/service_manager/runner/common/switches.h" | 26 #include "services/service_manager/runner/common/switches.h" |
32 | 27 |
33 #if defined(OS_LINUX) | 28 #if defined(OS_LINUX) |
34 #include "sandbox/linux/services/namespace_sandbox.h" | 29 #include "sandbox/linux/services/namespace_sandbox.h" |
35 #endif | 30 #endif |
36 | 31 |
37 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
38 #include "base/win/windows_version.h" | 33 #include "base/win/windows_version.h" |
(...skipping 20 matching lines...) Expand all Loading... |
59 | 54 |
60 private: | 55 private: |
61 DISALLOW_COPY_AND_ASSIGN(ScopedDllDirectoryOverride); | 56 DISALLOW_COPY_AND_ASSIGN(ScopedDllDirectoryOverride); |
62 }; | 57 }; |
63 | 58 |
64 } // namespace | 59 } // namespace |
65 #endif // defined(OS_WIN) && defined(COMPONENT_BUILD) | 60 #endif // defined(OS_WIN) && defined(COMPONENT_BUILD) |
66 | 61 |
67 namespace service_manager { | 62 namespace service_manager { |
68 | 63 |
69 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner, | 64 ServiceProcessLauncher::ServiceProcessLauncher( |
70 NativeRunnerDelegate* delegate, | 65 base::TaskRunner* launch_process_runner, |
71 bool start_sandboxed, | 66 Delegate* delegate, |
72 const Identity& target, | 67 const base::FilePath& service_path) |
73 const base::FilePath& service_path) | |
74 : launch_process_runner_(launch_process_runner), | 68 : launch_process_runner_(launch_process_runner), |
75 delegate_(delegate), | 69 delegate_(delegate), |
76 start_sandboxed_(start_sandboxed), | 70 start_sandboxed_(false), |
77 target_(target), | |
78 service_path_(service_path), | 71 service_path_(service_path), |
79 child_token_(mojo::edk::GenerateRandomToken()), | 72 child_token_(mojo::edk::GenerateRandomToken()), |
80 start_child_process_event_( | 73 start_child_process_event_( |
81 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 74 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
82 base::WaitableEvent::InitialState::NOT_SIGNALED), | 75 base::WaitableEvent::InitialState::NOT_SIGNALED), |
83 weak_factory_(this) { | 76 weak_factory_(this) { |
84 if (service_path_.empty()) | 77 if (service_path_.empty()) |
85 service_path_ = base::CommandLine::ForCurrentProcess()->GetProgram(); | 78 service_path_ = base::CommandLine::ForCurrentProcess()->GetProgram(); |
86 } | 79 } |
87 | 80 |
88 ChildProcessHost::~ChildProcessHost() { | 81 ServiceProcessLauncher::~ServiceProcessLauncher() { |
89 DCHECK(!mojo_ipc_channel_) | 82 Join(); |
90 << "Destroying ChildProcessHost before calling Join"; | |
91 } | 83 } |
92 | 84 |
93 mojom::ServicePtr ChildProcessHost::Start( | 85 mojom::ServicePtr ServiceProcessLauncher::Start( |
94 const Identity& target, | 86 const Identity& target, |
95 const ProcessReadyCallback& callback, | 87 bool start_sandboxed, |
96 const base::Closure& quit_closure) { | 88 const ProcessReadyCallback& callback) { |
97 DCHECK(!child_process_.IsValid()); | 89 DCHECK(!child_process_.IsValid()); |
98 | 90 |
| 91 start_sandboxed_ = start_sandboxed; |
| 92 target_ = target; |
| 93 |
99 const base::CommandLine& parent_command_line = | 94 const base::CommandLine& parent_command_line = |
100 *base::CommandLine::ForCurrentProcess(); | 95 *base::CommandLine::ForCurrentProcess(); |
101 | 96 |
102 std::unique_ptr<base::CommandLine> child_command_line( | 97 std::unique_ptr<base::CommandLine> child_command_line( |
103 new base::CommandLine(service_path_)); | 98 new base::CommandLine(service_path_)); |
104 | 99 |
105 child_command_line->AppendArguments(parent_command_line, false); | 100 child_command_line->AppendArguments(parent_command_line, false); |
106 | 101 |
107 child_command_line->AppendSwitchASCII(::switches::kProcessServiceName, | 102 child_command_line->AppendSwitchASCII(::switches::kProcessServiceName, |
108 target.name()); | 103 target.name()); |
109 #ifndef NDEBUG | 104 #ifndef NDEBUG |
110 child_command_line->AppendSwitchASCII("u", target.user_id()); | 105 child_command_line->AppendSwitchASCII("u", target.user_id()); |
111 #endif | 106 #endif |
112 | 107 |
113 if (start_sandboxed_) | 108 if (start_sandboxed_) |
114 child_command_line->AppendSwitch(::switches::kEnableSandbox); | 109 child_command_line->AppendSwitch(::switches::kEnableSandbox); |
115 | 110 |
116 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); | 111 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); |
117 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( | 112 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( |
118 child_command_line.get(), &handle_passing_info_); | 113 child_command_line.get(), &handle_passing_info_); |
119 | 114 |
120 mojom::ServicePtr client = | 115 mojom::ServicePtr client = |
121 PassServiceRequestOnCommandLine(child_command_line.get(), | 116 PassServiceRequestOnCommandLine(child_command_line.get(), child_token_); |
122 child_token_); | |
123 launch_process_runner_->PostTaskAndReply( | 117 launch_process_runner_->PostTaskAndReply( |
124 FROM_HERE, | 118 FROM_HERE, |
125 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this), | 119 base::Bind(&ServiceProcessLauncher::DoLaunch, base::Unretained(this), |
126 base::Passed(&child_command_line)), | 120 base::Passed(&child_command_line)), |
127 base::Bind(&ChildProcessHost::DidStart, | 121 base::Bind(&ServiceProcessLauncher::DidStart, |
128 weak_factory_.GetWeakPtr(), callback)); | 122 weak_factory_.GetWeakPtr(), callback)); |
129 return client; | 123 return client; |
130 } | 124 } |
131 | 125 |
132 void ChildProcessHost::Join() { | 126 void ServiceProcessLauncher::Join() { |
133 if (mojo_ipc_channel_) | 127 if (mojo_ipc_channel_) |
134 start_child_process_event_.Wait(); | 128 start_child_process_event_.Wait(); |
135 mojo_ipc_channel_.reset(); | 129 mojo_ipc_channel_.reset(); |
136 if (child_process_.IsValid()) { | 130 if (child_process_.IsValid()) { |
137 int rv = -1; | 131 int rv = -1; |
138 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) | 132 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) |
139 << "Failed to wait for child process"; | 133 << "Failed to wait for child process"; |
140 child_process_.Close(); | 134 child_process_.Close(); |
141 } | 135 } |
142 } | 136 } |
143 | 137 |
144 void ChildProcessHost::DidStart(const ProcessReadyCallback& callback) { | 138 void ServiceProcessLauncher::DidStart(const ProcessReadyCallback& callback) { |
145 if (child_process_.IsValid()) { | 139 if (child_process_.IsValid()) { |
146 callback.Run(child_process_.Pid()); | 140 callback.Run(child_process_.Pid()); |
147 } else { | 141 } else { |
148 LOG(ERROR) << "Failed to start child process"; | 142 LOG(ERROR) << "Failed to start child process"; |
149 mojo_ipc_channel_.reset(); | 143 mojo_ipc_channel_.reset(); |
150 callback.Run(base::kNullProcessId); | 144 callback.Run(base::kNullProcessId); |
151 } | 145 } |
152 } | 146 } |
153 | 147 |
154 void ChildProcessHost::DoLaunch( | 148 void ServiceProcessLauncher::DoLaunch( |
155 std::unique_ptr<base::CommandLine> child_command_line) { | 149 std::unique_ptr<base::CommandLine> child_command_line) { |
156 if (delegate_) { | 150 if (delegate_) { |
157 delegate_->AdjustCommandLineArgumentsForTarget(target_, | 151 delegate_->AdjustCommandLineArgumentsForTarget(target_, |
158 child_command_line.get()); | 152 child_command_line.get()); |
159 } | 153 } |
160 | 154 |
161 base::LaunchOptions options; | 155 base::LaunchOptions options; |
162 | 156 |
163 base::FilePath exe_dir; | 157 base::FilePath exe_dir; |
164 base::PathService::Get(base::DIR_EXE, &exe_dir); | 158 base::PathService::Get(base::DIR_EXE, &exe_dir); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 child_process_.Handle(), | 243 child_process_.Handle(), |
250 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( | 244 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( |
251 mojo_ipc_channel_->PassServerHandle().release().handle)), | 245 mojo_ipc_channel_->PassServerHandle().release().handle)), |
252 child_token_); | 246 child_token_); |
253 } | 247 } |
254 } | 248 } |
255 start_child_process_event_.Signal(); | 249 start_child_process_event_.Signal(); |
256 } | 250 } |
257 | 251 |
258 } // namespace service_manager | 252 } // namespace service_manager |
OLD | NEW |