1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/win/worker_process_launcher.h"
#include <windows.h>
#include <sddl.h>
#include <limits>
#include "base/base_switches.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/process_util.h"
#include "base/rand_util.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "base/win/scoped_handle.h"
#include "ipc/ipc_channel_proxy.h"
#include "ipc/ipc_message.h"
using base::win::ScopedHandle;
namespace {
// Match the pipe name prefix used by Chrome IPC channels so that the client
// could use Chrome IPC APIs instead of connecting manually.
const char kChromePipeNamePrefix[] = "\\\\.\\pipe\\chrome.";
} // namespace
namespace remoting {
WorkerProcessLauncher::Delegate::~Delegate() {
}
WorkerProcessLauncher::WorkerProcessLauncher(
Delegate* delegate,
const base::Closure& stopped_callback,
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner)
: Stoppable(main_task_runner, stopped_callback),
delegate_(delegate),
main_task_runner_(main_task_runner),
ipc_task_runner_(ipc_task_runner) {
}
WorkerProcessLauncher::~WorkerProcessLauncher() {
DCHECK(main_task_runner_->BelongsToCurrentThread());
}
void WorkerProcessLauncher::Start(const std::string& pipe_sddl) {
DCHECK(main_task_runner_->BelongsToCurrentThread());
DCHECK(ipc_channel_.get() == NULL);
DCHECK(!pipe_.IsValid());
DCHECK(!process_exit_event_.IsValid());
DCHECK(process_watcher_.GetWatchedObject() == NULL);
std::string channel_name = GenerateRandomChannelId();
if (CreatePipeForIpcChannel(channel_name, pipe_sddl, &pipe_)) {
// Wrap the pipe into an IPC channel.
ipc_channel_.reset(new IPC::ChannelProxy(
IPC::ChannelHandle(pipe_),
IPC::Channel::MODE_SERVER,
this,
ipc_task_runner_));
// Launch the process and attach an object watcher to the returned process
// handle so that we get notified if the process terminates.
if (delegate_->DoLaunchProcess(channel_name, &process_exit_event_)) {
if (process_watcher_.StartWatching(process_exit_event_, this)) {
return;
}
delegate_->DoKillProcess(CONTROL_C_EXIT);
}
}
Stop();
}
void WorkerProcessLauncher::Send(IPC::Message* message) {
DCHECK(main_task_runner_->BelongsToCurrentThread());
ipc_channel_->Send(message);
}
void WorkerProcessLauncher::OnObjectSignaled(HANDLE object) {
DCHECK(main_task_runner_->BelongsToCurrentThread());
DCHECK(process_watcher_.GetWatchedObject() == NULL);
Stop();
}
bool WorkerProcessLauncher::OnMessageReceived(const IPC::Message& message) {
DCHECK(main_task_runner_->BelongsToCurrentThread());
DCHECK(ipc_channel_.get() != NULL);
DCHECK(pipe_.IsValid());
DCHECK(process_exit_event_.IsValid());
return delegate_->OnMessageReceived(message);
}
void WorkerProcessLauncher::OnChannelConnected(int32 peer_pid) {
DCHECK(main_task_runner_->BelongsToCurrentThread());
DCHECK(ipc_channel_.get() != NULL);
DCHECK(pipe_.IsValid());
DCHECK(process_exit_event_.IsValid());
// Get the actual peer's PID (i.e. reported by the OS) instead of the PID
// reported by the peer itself (|peer_pid|).
DWORD actual_peer_pid;
if (!GetNamedPipeClientProcessId(pipe_, &actual_peer_pid)) {
LOG_GETLASTERROR(ERROR) << "Failed to query the peer's PID";
Stop();
return;
}
delegate_->OnChannelConnected(actual_peer_pid);
}
void WorkerProcessLauncher::OnChannelError() {
DCHECK(main_task_runner_->BelongsToCurrentThread());
DCHECK(ipc_channel_.get() != NULL);
DCHECK(pipe_.IsValid());
DCHECK(process_exit_event_.IsValid());
Stop();
}
void WorkerProcessLauncher::DoStop() {
DCHECK(main_task_runner_->BelongsToCurrentThread());
ipc_channel_.reset();
pipe_.Close();
// Kill the process if it has been started already.
if (process_watcher_.GetWatchedObject() != NULL) {
delegate_->DoKillProcess(CONTROL_C_EXIT);
return;
}
DCHECK(ipc_channel_.get() == NULL);
DCHECK(!pipe_.IsValid());
DCHECK(process_watcher_.GetWatchedObject() == NULL);
process_exit_event_.Close();
CompleteStopping();
}
// Creates the server end of the Chromoting IPC channel.
bool WorkerProcessLauncher::CreatePipeForIpcChannel(
const std::string& channel_name,
const std::string& pipe_sddl,
ScopedHandle* pipe_out) {
// Create security descriptor for the channel.
SECURITY_ATTRIBUTES security_attributes;
security_attributes.nLength = sizeof(security_attributes);
security_attributes.bInheritHandle = FALSE;
ULONG security_descriptor_length = 0;
if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
UTF8ToUTF16(pipe_sddl).c_str(),
SDDL_REVISION_1,
reinterpret_cast<PSECURITY_DESCRIPTOR*>(
&security_attributes.lpSecurityDescriptor),
&security_descriptor_length)) {
LOG_GETLASTERROR(ERROR) <<
"Failed to create a security descriptor for the Chromoting IPC channel";
return false;
}
// Convert the channel name to the pipe name.
std::string pipe_name(kChromePipeNamePrefix);
pipe_name.append(channel_name);
// Create the server end of the pipe. This code should match the code in
// IPC::Channel with exception of passing a non-default security descriptor.
base::win::ScopedHandle pipe;
pipe.Set(CreateNamedPipe(
UTF8ToUTF16(pipe_name).c_str(),
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
1,
IPC::Channel::kReadBufferSize,
IPC::Channel::kReadBufferSize,
5000,
&security_attributes));
if (!pipe.IsValid()) {
LOG_GETLASTERROR(ERROR) <<
"Failed to create the server end of the Chromoting IPC channel";
LocalFree(security_attributes.lpSecurityDescriptor);
return false;
}
LocalFree(security_attributes.lpSecurityDescriptor);
*pipe_out = pipe.Pass();
return true;
}
// N.B. Copied from src/content/common/child_process_host_impl.cc
std::string WorkerProcessLauncher::GenerateRandomChannelId() {
return base::StringPrintf("%d.%p.%d",
base::GetCurrentProcId(), this,
base::RandInt(0, std::numeric_limits<int>::max()));
}
} // namespace remoting
|