OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file implements the Windows service controlling Me2Me host processes | 5 // This file implements the Windows service controlling Me2Me host processes |
6 // running within user sessions. | 6 // running within user sessions. |
7 | 7 |
8 #include "remoting/host/win/wts_session_process_launcher.h" | 8 #include "remoting/host/win/wts_session_process_launcher.h" |
9 | 9 |
10 #include <windows.h> | 10 #include <windows.h> |
11 #include <sddl.h> | 11 #include <sddl.h> |
12 #include <limits> | 12 #include <limits> |
13 | 13 |
14 #include "base/base_switches.h" | 14 #include "base/base_switches.h" |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
18 #include "base/file_path.h" | 18 #include "base/file_path.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
22 #include "base/path_service.h" | 22 #include "base/path_service.h" |
23 #include "base/process_util.h" | 23 #include "base/process_util.h" |
24 #include "base/rand_util.h" | 24 #include "base/rand_util.h" |
25 #include "base/stringprintf.h" | 25 #include "base/stringprintf.h" |
26 #include "base/utf_string_conversions.h" | |
26 #include "base/win/scoped_handle.h" | 27 #include "base/win/scoped_handle.h" |
27 #include "ipc/ipc_channel_proxy.h" | 28 #include "ipc/ipc_channel_proxy.h" |
28 #include "ipc/ipc_message.h" | 29 #include "ipc/ipc_message.h" |
29 #include "ipc/ipc_message_macros.h" | 30 #include "ipc/ipc_message_macros.h" |
30 #include "remoting/host/constants.h" | 31 #include "remoting/host/constants.h" |
31 #include "remoting/host/chromoting_messages.h" | 32 #include "remoting/host/chromoting_messages.h" |
32 #include "remoting/host/sas_injector.h" | 33 #include "remoting/host/sas_injector.h" |
33 #include "remoting/host/win/launch_process_with_token.h" | 34 #include "remoting/host/win/launch_process_with_token.h" |
34 #include "remoting/host/win/wts_console_monitor.h" | 35 #include "remoting/host/win/wts_console_monitor.h" |
35 | 36 |
36 using base::win::ScopedHandle; | 37 using base::win::ScopedHandle; |
37 using base::TimeDelta; | 38 using base::TimeDelta; |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 // The minimum and maximum delays between attempts to inject host process into | 42 // The minimum and maximum delays between attempts to inject host process into |
42 // a session. | 43 // a session. |
43 const int kMaxLaunchDelaySeconds = 60; | 44 const int kMaxLaunchDelaySeconds = 60; |
44 const int kMinLaunchDelaySeconds = 1; | 45 const int kMinLaunchDelaySeconds = 1; |
45 | 46 |
46 const FilePath::CharType kMe2meHostBinaryName[] = | 47 const FilePath::CharType kMe2meHostBinaryName[] = |
47 FILE_PATH_LITERAL("remoting_me2me_host.exe"); | 48 FILE_PATH_LITERAL("remoting_me2me_host.exe"); |
48 | 49 |
49 // Match the pipe name prefix used by Chrome IPC channels. | 50 // Match the pipe name prefix used by Chrome IPC channels. |
50 const wchar_t kChromePipeNamePrefix[] = L"\\\\.\\pipe\\chrome."; | 51 const char kChromePipeNamePrefix[] = "\\\\.\\pipe\\chrome."; |
51 | 52 |
52 // The IPC channel name is passed to the host in the command line. | 53 // The IPC channel name is passed to the host in the command line. |
53 const char kChromotingIpcSwitchName[] = "chromoting-ipc"; | 54 const char kChromotingIpcSwitchName[] = "chromoting-ipc"; |
54 | 55 |
55 // The command line parameters that should be copied from the service's command | 56 // The command line parameters that should be copied from the service's command |
56 // line to the host process. | 57 // line to the host process. |
57 const char* kCopiedSwitchNames[] = { | 58 const char* kCopiedSwitchNames[] = { |
58 "auth-config", "host-config", switches::kV, switches::kVModule }; | 59 "auth-config", "host-config", switches::kV, switches::kVModule }; |
59 | 60 |
60 // The security descriptor of the Chromoting IPC channel. It gives full access | 61 // The security descriptor of the Chromoting IPC channel. It gives full access |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 "Failed to change session ID of a token"; | 144 "Failed to change session ID of a token"; |
144 return false; | 145 return false; |
145 } | 146 } |
146 | 147 |
147 token_out->Set(session_token.Take()); | 148 token_out->Set(session_token.Take()); |
148 return true; | 149 return true; |
149 } | 150 } |
150 | 151 |
151 // Generates random channel ID. | 152 // Generates random channel ID. |
152 // N.B. Stolen from src/content/common/child_process_host_impl.cc | 153 // N.B. Stolen from src/content/common/child_process_host_impl.cc |
153 std::wstring GenerateRandomChannelId(void* instance) { | 154 std::string GenerateRandomChannelId(void* instance) { |
154 return base::StringPrintf(L"%d.%p.%d", | 155 return base::StringPrintf("%d.%p.%d", |
155 base::GetCurrentProcId(), instance, | 156 base::GetCurrentProcId(), instance, |
156 base::RandInt(0, std::numeric_limits<int>::max())); | 157 base::RandInt(0, std::numeric_limits<int>::max())); |
157 } | 158 } |
158 | 159 |
159 // Creates the server end of the Chromoting IPC channel. | 160 // Creates the server end of the Chromoting IPC channel. |
160 // N.B. This code is based on IPC::Channel's implementation. | 161 // N.B. This code is based on IPC::Channel's implementation. |
161 bool CreatePipeForIpcChannel(void* instance, | 162 bool CreatePipeForIpcChannel(void* instance, |
162 std::wstring* channel_name_out, | 163 std::string* channel_name_out, |
163 ScopedHandle* pipe_out) { | 164 ScopedHandle* pipe_out) { |
164 // Create security descriptor for the channel. | 165 // Create security descriptor for the channel. |
165 SECURITY_ATTRIBUTES security_attributes; | 166 SECURITY_ATTRIBUTES security_attributes; |
166 security_attributes.nLength = sizeof(security_attributes); | 167 security_attributes.nLength = sizeof(security_attributes); |
167 security_attributes.bInheritHandle = FALSE; | 168 security_attributes.bInheritHandle = FALSE; |
168 | 169 |
169 ULONG security_descriptor_length = 0; | 170 ULONG security_descriptor_length = 0; |
170 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW( | 171 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW( |
171 kChromotingChannelSecurityDescriptor, | 172 kChromotingChannelSecurityDescriptor, |
172 SDDL_REVISION_1, | 173 SDDL_REVISION_1, |
173 reinterpret_cast<PSECURITY_DESCRIPTOR*>( | 174 reinterpret_cast<PSECURITY_DESCRIPTOR*>( |
174 &security_attributes.lpSecurityDescriptor), | 175 &security_attributes.lpSecurityDescriptor), |
175 &security_descriptor_length)) { | 176 &security_descriptor_length)) { |
176 LOG_GETLASTERROR(ERROR) << | 177 LOG_GETLASTERROR(ERROR) << |
177 "Failed to create a security descriptor for the Chromoting IPC channel"; | 178 "Failed to create a security descriptor for the Chromoting IPC channel"; |
178 return false; | 179 return false; |
179 } | 180 } |
180 | 181 |
181 // Generate a random channel name. | 182 // Generate a random channel name. |
182 std::wstring channel_name(GenerateRandomChannelId(instance)); | 183 std::string channel_name(GenerateRandomChannelId(instance)); |
183 | 184 |
184 // Convert it to the pipe name. | 185 // Convert it to the pipe name. |
185 std::wstring pipe_name(kChromePipeNamePrefix); | 186 std::string pipe_name(kChromePipeNamePrefix); |
186 pipe_name.append(channel_name); | 187 pipe_name.append(channel_name); |
187 | 188 |
188 // Create the server end of the pipe. This code should match the code in | 189 // Create the server end of the pipe. This code should match the code in |
189 // IPC::Channel with exception of passing a non-default security descriptor. | 190 // IPC::Channel with exception of passing a non-default security descriptor. |
190 HANDLE pipe = CreateNamedPipeW(pipe_name.c_str(), | 191 HANDLE pipe = CreateNamedPipeW(UTF8ToUTF16(pipe_name).c_str(), |
Wez
2012/08/03 21:26:15
nit: UTF8ToWide()?
alexeypa (please no reviews)
2012/08/03 22:00:18
WCHAR == UTF-16
Wez
2012/08/06 17:29:38
Yes; but the intent is to match UTF8ToWide/UTF8ToU
alexeypa (please no reviews)
2012/08/06 17:56:18
The function takes WCHAR*, which is assumed to be
Wez
2012/08/06 18:41:21
Of course; my mistake.
| |
191 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | | 192 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
192 FILE_FLAG_FIRST_PIPE_INSTANCE, | 193 FILE_FLAG_FIRST_PIPE_INSTANCE, |
193 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, | 194 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
194 1, | 195 1, |
195 IPC::Channel::kReadBufferSize, | 196 IPC::Channel::kReadBufferSize, |
196 IPC::Channel::kReadBufferSize, | 197 IPC::Channel::kReadBufferSize, |
197 5000, | 198 5000, |
198 &security_attributes); | 199 &security_attributes); |
199 if (pipe == INVALID_HANDLE_VALUE) { | 200 if (pipe == INVALID_HANDLE_VALUE) { |
200 LOG_GETLASTERROR(ERROR) << | 201 LOG_GETLASTERROR(ERROR) << |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 | 253 |
253 // Construct the host binary name. | 254 // Construct the host binary name. |
254 FilePath dir_path; | 255 FilePath dir_path; |
255 if (!PathService::Get(base::DIR_EXE, &dir_path)) { | 256 if (!PathService::Get(base::DIR_EXE, &dir_path)) { |
256 LOG(ERROR) << "Failed to get the executable file name."; | 257 LOG(ERROR) << "Failed to get the executable file name."; |
257 Stop(); | 258 Stop(); |
258 return; | 259 return; |
259 } | 260 } |
260 FilePath host_binary = dir_path.Append(kMe2meHostBinaryName); | 261 FilePath host_binary = dir_path.Append(kMe2meHostBinaryName); |
261 | 262 |
262 std::wstring channel_name; | 263 std::string channel_name; |
263 ScopedHandle pipe; | 264 ScopedHandle pipe; |
264 if (CreatePipeForIpcChannel(this, &channel_name, &pipe)) { | 265 if (CreatePipeForIpcChannel(this, &channel_name, &pipe)) { |
265 // Wrap the pipe into an IPC channel. | 266 // Wrap the pipe into an IPC channel. |
266 chromoting_channel_.reset(new IPC::ChannelProxy( | 267 chromoting_channel_.reset(new IPC::ChannelProxy( |
267 IPC::ChannelHandle(pipe.Get()), | 268 IPC::ChannelHandle(pipe.Get()), |
268 IPC::Channel::MODE_SERVER, | 269 IPC::Channel::MODE_SERVER, |
269 this, | 270 this, |
270 ipc_message_loop_)); | 271 ipc_message_loop_)); |
271 | 272 |
272 // Create the host process command line passing the name of the IPC channel | 273 // Create the host process command line passing the name of the IPC channel |
273 // to use and copying known switches from the service's command line. | 274 // to use and copying known switches from the service's command line. |
274 CommandLine command_line(host_binary); | 275 CommandLine command_line(host_binary); |
275 command_line.AppendSwitchNative(kChromotingIpcSwitchName, channel_name); | 276 command_line.AppendSwitchASCII(kChromotingIpcSwitchName, channel_name); |
276 command_line.CopySwitchesFrom(*CommandLine::ForCurrentProcess(), | 277 command_line.CopySwitchesFrom(*CommandLine::ForCurrentProcess(), |
277 kCopiedSwitchNames, | 278 kCopiedSwitchNames, |
278 _countof(kCopiedSwitchNames)); | 279 _countof(kCopiedSwitchNames)); |
279 | 280 |
280 // Try to launch the process and attach an object watcher to the returned | 281 // Try to launch the process and attach an object watcher to the returned |
281 // handle so that we get notified when the process terminates. | 282 // handle so that we get notified when the process terminates. |
282 if (LaunchProcessWithToken(host_binary, | 283 if (LaunchProcessWithToken(host_binary, |
283 command_line.GetCommandLineString(), | 284 command_line.GetCommandLineString(), |
284 session_token_, | 285 session_token_, |
285 &process_)) { | 286 &process_)) { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 | 480 |
480 void WtsSessionProcessLauncher::DoStop() { | 481 void WtsSessionProcessLauncher::DoStop() { |
481 if (state_ != StateDetached) { | 482 if (state_ != StateDetached) { |
482 OnSessionDetached(); | 483 OnSessionDetached(); |
483 } | 484 } |
484 | 485 |
485 CompleteStopping(); | 486 CompleteStopping(); |
486 } | 487 } |
487 | 488 |
488 } // namespace remoting | 489 } // namespace remoting |
OLD | NEW |