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
3 // found in the LICENSE file.
5 #include "remoting/host/win/worker_process_launcher.h"
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/time/time.h"
11 #include "base/win/windows_version.h"
12 #include "ipc/ipc_message.h"
13 #include "remoting/host/chromoting_messages.h"
14 #include "remoting/host/host_exit_codes.h"
15 #include "remoting/host/worker_process_ipc_delegate.h"
17 using base::TimeDelta
;
18 using base::win::ScopedHandle
;
20 const net::BackoffEntry::Policy kDefaultBackoffPolicy
= {
21 // Number of initial errors (in sequence) to ignore before applying
22 // exponential back-off rules.
25 // Initial delay for exponential back-off in ms.
28 // Factor by which the waiting time will be multiplied.
31 // Fuzzing percentage. ex: 10% will spread requests randomly
32 // between 90%-100% of the calculated time.
35 // Maximum amount of time we are willing to delay our request in ms.
38 // Time to keep an entry from being discarded even when it
39 // has no significant state, -1 to never discard.
42 // Don't use initial delay unless the last request was an error.
46 const int kKillProcessTimeoutSeconds
= 5;
47 const int kLaunchResultTimeoutSeconds
= 5;
51 WorkerProcessLauncher::Delegate::~Delegate() {
54 WorkerProcessLauncher::WorkerProcessLauncher(
55 scoped_ptr
<WorkerProcessLauncher::Delegate
> launcher_delegate
,
56 WorkerProcessIpcDelegate
* ipc_handler
)
57 : ipc_handler_(ipc_handler
),
58 launcher_delegate_(launcher_delegate
.Pass()),
59 exit_code_(CONTROL_C_EXIT
),
61 kill_process_timeout_(
62 base::TimeDelta::FromSeconds(kKillProcessTimeoutSeconds
)),
63 launch_backoff_(&kDefaultBackoffPolicy
) {
64 DCHECK(ipc_handler_
!= nullptr);
69 WorkerProcessLauncher::~WorkerProcessLauncher() {
70 DCHECK(CalledOnValidThread());
72 ipc_handler_
= nullptr;
76 void WorkerProcessLauncher::Crash(
77 const tracked_objects::Location
& location
) {
78 DCHECK(CalledOnValidThread());
80 // Ask the worker process to crash voluntarily if it is still connected.
82 Send(new ChromotingDaemonMsg_Crash(location
.function_name(),
84 location
.line_number()));
87 // Close the channel and ignore any not yet processed messages.
88 launcher_delegate_
->CloseChannel();
91 // Give the worker process some time to crash.
92 if (!kill_process_timer_
.IsRunning()) {
93 kill_process_timer_
.Start(FROM_HERE
, kill_process_timeout_
, this,
94 &WorkerProcessLauncher::StopWorker
);
98 void WorkerProcessLauncher::Send(IPC::Message
* message
) {
99 DCHECK(CalledOnValidThread());
102 launcher_delegate_
->Send(message
);
108 void WorkerProcessLauncher::OnProcessLaunched(
109 base::win::ScopedHandle worker_process
) {
110 DCHECK(CalledOnValidThread());
111 DCHECK(!ipc_enabled_
);
112 DCHECK(!launch_timer_
.IsRunning());
113 DCHECK(!process_watcher_
.GetWatchedObject());
114 DCHECK(!worker_process_
.IsValid());
116 if (!process_watcher_
.StartWatching(worker_process
.Get(), this)) {
122 worker_process_
= worker_process
.Pass();
125 void WorkerProcessLauncher::OnFatalError() {
126 DCHECK(CalledOnValidThread());
131 bool WorkerProcessLauncher::OnMessageReceived(
132 const IPC::Message
& message
) {
133 DCHECK(CalledOnValidThread());
138 return ipc_handler_
->OnMessageReceived(message
);
141 void WorkerProcessLauncher::OnChannelConnected(int32 peer_pid
) {
142 DCHECK(CalledOnValidThread());
147 // This can result in |this| being deleted, so this call must be the last in
149 ipc_handler_
->OnChannelConnected(peer_pid
);
152 void WorkerProcessLauncher::OnChannelError() {
153 DCHECK(CalledOnValidThread());
155 // Schedule a delayed termination of the worker process. Usually, the pipe is
156 // disconnected when the worker process is about to exit. Waiting a little bit
157 // here allows the worker to exit completely and so, notify
158 // |process_watcher_|. As the result KillProcess() will not be called and
159 // the original exit code reported by the worker process will be retrieved.
160 if (!kill_process_timer_
.IsRunning()) {
161 kill_process_timer_
.Start(FROM_HERE
, kill_process_timeout_
, this,
162 &WorkerProcessLauncher::StopWorker
);
166 void WorkerProcessLauncher::OnObjectSignaled(HANDLE object
) {
167 DCHECK(CalledOnValidThread());
168 DCHECK(!process_watcher_
.GetWatchedObject());
169 DCHECK_EQ(exit_code_
, CONTROL_C_EXIT
);
170 DCHECK_EQ(worker_process_
.Get(), object
);
172 // Get exit code of the worker process if it is available.
173 if (!::GetExitCodeProcess(worker_process_
.Get(), &exit_code_
)) {
174 PLOG(INFO
) << "Failed to query the exit code of the worker process";
175 exit_code_
= CONTROL_C_EXIT
;
178 worker_process_
.Close();
182 void WorkerProcessLauncher::LaunchWorker() {
183 DCHECK(CalledOnValidThread());
184 DCHECK(!ipc_enabled_
);
185 DCHECK(!kill_process_timer_
.IsRunning());
186 DCHECK(!launch_timer_
.IsRunning());
187 DCHECK(!process_watcher_
.GetWatchedObject());
188 DCHECK(!launch_result_timer_
.IsRunning());
190 exit_code_
= CONTROL_C_EXIT
;
192 // Make sure launching a process will not take forever.
193 launch_result_timer_
.Start(
194 FROM_HERE
, base::TimeDelta::FromSeconds(kLaunchResultTimeoutSeconds
),
195 this, &WorkerProcessLauncher::RecordLaunchResult
);
197 launcher_delegate_
->LaunchProcess(this);
200 void WorkerProcessLauncher::RecordLaunchResult() {
201 DCHECK(CalledOnValidThread());
203 if (!worker_process_
.IsValid()) {
204 LOG(WARNING
) << "A worker process failed to start within "
205 << kLaunchResultTimeoutSeconds
<< " seconds.";
207 launch_backoff_
.InformOfRequest(false);
212 // Assume success if the worker process has been running for a few seconds.
213 launch_backoff_
.InformOfRequest(true);
216 void WorkerProcessLauncher::RecordSuccessfulLaunchForTest() {
217 DCHECK(CalledOnValidThread());
219 if (launch_result_timer_
.IsRunning()) {
220 launch_result_timer_
.Stop();
221 RecordLaunchResult();
225 void WorkerProcessLauncher::SetKillProcessTimeoutForTest(
226 const base::TimeDelta
& timeout
) {
227 DCHECK(CalledOnValidThread());
229 kill_process_timeout_
= timeout
;
232 void WorkerProcessLauncher::StopWorker() {
233 DCHECK(CalledOnValidThread());
235 // Record a launch failure if the process exited too soon.
236 if (launch_result_timer_
.IsRunning()) {
237 launch_backoff_
.InformOfRequest(false);
238 launch_result_timer_
.Stop();
241 // Ignore any remaining IPC messages.
242 ipc_enabled_
= false;
244 // Stop monitoring the worker process.
245 process_watcher_
.StopWatching();
246 worker_process_
.Close();
248 kill_process_timer_
.Stop();
249 launcher_delegate_
->KillProcess();
251 // Do not relaunch the worker process if the caller has asked us to stop.
255 // Stop trying to restart the worker process if it exited due to
257 if (kMinPermanentErrorExitCode
<= exit_code_
&&
258 exit_code_
<= kMaxPermanentErrorExitCode
) {
259 ipc_handler_
->OnPermanentError(exit_code_
);
263 // Schedule the next attempt to launch the worker process.
264 launch_timer_
.Start(FROM_HERE
, launch_backoff_
.GetTimeUntilRelease(), this,
265 &WorkerProcessLauncher::LaunchWorker
);
268 } // namespace remoting