blob: 8ebe267d8870e485904d1893c0f0697c87445056 [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/update_attempter.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -07006
7// From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L
8#ifndef _POSIX_C_SOURCE
9#define _POSIX_C_SOURCE 199309L
10#endif // _POSIX_C_SOURCE
11#include <time.h>
12
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070013#include <string>
Darin Petkov9b230572010-10-08 10:20:09 -070014#include <tr1/memory>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070015#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070016
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -070017#include <base/file_util.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080018#include <base/rand_util.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070019#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070020#include <metrics/metrics_library.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020021#include <policy/libpolicy.h>
22#include <policy/device_policy.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070023
Bruno Rocha7f9aea22011-09-12 14:31:24 -070024#include "update_engine/certificate_checker.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070025#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/download_action.h"
27#include "update_engine/filesystem_copier_action.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080028#include "update_engine/gpio_handler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyes819fef22010-12-17 11:33:58 -080030#include "update_engine/multi_range_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070031#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070032#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070033#include "update_engine/omaha_response_handler_action.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080034#include "update_engine/payload_state_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070035#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070036#include "update_engine/prefs_interface.h"
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -070037#include "update_engine/subprocess.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070038#include "update_engine/system_state.h"
Darin Petkov1023a602010-08-30 13:47:51 -070039#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070040
Darin Petkovaf183052010-08-23 12:07:13 -070041using base::TimeDelta;
42using base::TimeTicks;
Andrew de los Reyes21816e12011-04-07 14:18:56 -070043using google::protobuf::NewPermanentCallback;
Darin Petkov9b230572010-10-08 10:20:09 -070044using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070045using std::tr1::shared_ptr;
Jay Srinivasan43488792012-06-19 00:25:31 -070046using std::set;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070047using std::string;
48using std::vector;
49
50namespace chromeos_update_engine {
51
Darin Petkov36275772010-10-01 11:40:57 -070052const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
53
Gilad Arnold1ebd8132012-03-05 10:19:29 -080054// Private test server URL w/ custom port number.
Gilad Arnolded747312012-03-15 18:20:41 -070055// TODO(garnold) This is a temporary hack to allow us to test the closed loop
56// automated update testing. To be replaced with an hard-coded local IP address.
57const char* const UpdateAttempter::kTestUpdateUrl(
58 "http://garnold.mtv.corp.google.com:8080/update");
Gilad Arnold28e2f392012-02-09 14:36:46 -080059
Darin Petkovcd1666f2010-09-23 09:53:44 -070060const char* kUpdateCompletedMarker =
61 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070062
Andrew de los Reyes45168102010-11-22 11:13:50 -080063namespace {
64const int kMaxConsecutiveObeyProxyRequests = 20;
65} // namespace {}
66
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070067const char* UpdateStatusToString(UpdateStatus status) {
68 switch (status) {
69 case UPDATE_STATUS_IDLE:
70 return "UPDATE_STATUS_IDLE";
71 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
72 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
73 case UPDATE_STATUS_UPDATE_AVAILABLE:
74 return "UPDATE_STATUS_UPDATE_AVAILABLE";
75 case UPDATE_STATUS_DOWNLOADING:
76 return "UPDATE_STATUS_DOWNLOADING";
77 case UPDATE_STATUS_VERIFYING:
78 return "UPDATE_STATUS_VERIFYING";
79 case UPDATE_STATUS_FINALIZING:
80 return "UPDATE_STATUS_FINALIZING";
81 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
82 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070083 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
84 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070085 default:
86 return "unknown status";
87 }
88}
89
Darin Petkov777dbfa2010-07-20 15:03:37 -070090// Turns a generic kActionCodeError to a generic error code specific
91// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
92// not kActionCodeError, or the action is not matched, returns |code|
93// unchanged.
94ActionExitCode GetErrorCodeForAction(AbstractAction* action,
95 ActionExitCode code) {
96 if (code != kActionCodeError)
97 return code;
98
99 const string type = action->Type();
100 if (type == OmahaRequestAction::StaticType())
101 return kActionCodeOmahaRequestError;
102 if (type == OmahaResponseHandlerAction::StaticType())
103 return kActionCodeOmahaResponseHandlerError;
104 if (type == FilesystemCopierAction::StaticType())
105 return kActionCodeFilesystemCopierError;
106 if (type == PostinstallRunnerAction::StaticType())
107 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -0700108
109 return code;
110}
111
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800112UpdateAttempter::UpdateAttempter(SystemState* system_state,
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800113 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700114 : processor_(new ActionProcessor()),
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800115 system_state_(system_state),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700116 dbus_service_(NULL),
Darin Petkov1023a602010-08-30 13:47:51 -0700117 update_check_scheduler_(NULL),
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700118 fake_update_success_(false),
Darin Petkov1023a602010-08-30 13:47:51 -0700119 http_response_code_(0),
Chris Sosa4f8ee272012-11-30 13:01:54 -0800120 shares_(utils::kCpuSharesNormal),
121 manage_shares_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700122 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700123 status_(UPDATE_STATUS_IDLE),
124 download_progress_(0.0),
125 last_checked_time_(0),
126 new_version_("0.0.0.0"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700127 new_payload_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800128 proxy_manual_checks_(0),
129 obeying_proxies_(true),
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700130 chrome_proxy_resolver_(dbus_iface),
Darin Petkov58dd1342011-05-06 12:05:13 -0700131 updated_boot_flags_(false),
132 update_boot_flags_running_(false),
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200133 start_action_processor_(false),
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800134 policy_provider_(NULL),
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700135 is_using_test_url_(false),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700136 is_test_mode_(false),
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800137 is_test_update_attempted_(false) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800138 prefs_ = system_state->prefs();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700139 omaha_request_params_ = system_state->request_params();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700140 if (utils::FileExists(kUpdateCompletedMarker))
141 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
142}
143
144UpdateAttempter::~UpdateAttempter() {
Chris Sosa4f8ee272012-11-30 13:01:54 -0800145 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700146}
147
Gilad Arnold28e2f392012-02-09 14:36:46 -0800148void UpdateAttempter::Update(const string& app_version,
149 const string& omaha_url,
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700150 bool obey_proxies,
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800151 bool interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800152 bool is_test_mode) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800153 chrome_proxy_resolver_.Init();
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700154 fake_update_success_ = false;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700155 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Thieu Le116fda32011-04-19 11:01:54 -0700156 // Although we have applied an update, we still want to ping Omaha
157 // to ensure the number of active statistics is accurate.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700158 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
Thieu Le116fda32011-04-19 11:01:54 -0700159 << "reboot, we'll ping Omaha instead";
160 PingOmaha();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700161 return;
162 }
163 if (status_ != UPDATE_STATUS_IDLE) {
164 // Update in progress. Do nothing
165 return;
166 }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700167
168 if (!CalculateUpdateParams(app_version,
169 omaha_url,
170 obey_proxies,
171 interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800172 is_test_mode)) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700173 return;
174 }
175
176 BuildUpdateActions(interactive);
177
178 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE,
179 kUpdateNoticeUnspecified);
180
181 // Just in case we didn't update boot flags yet, make sure they're updated
182 // before any update processing starts.
183 start_action_processor_ = true;
184 UpdateBootFlags();
185}
186
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700187void UpdateAttempter::RefreshDevicePolicy() {
188 // Lazy initialize the policy provider, or reload the latest policy data.
189 if (!policy_provider_.get())
190 policy_provider_.reset(new policy::PolicyProvider());
191 policy_provider_->Reload();
192
193 const policy::DevicePolicy* device_policy = NULL;
194 if (policy_provider_->device_policy_is_loaded())
195 device_policy = &policy_provider_->GetDevicePolicy();
196
197 if (device_policy)
198 LOG(INFO) << "Device policies/settings present";
199 else
200 LOG(INFO) << "No device policies/settings present.";
201
202 system_state_->set_device_policy(device_policy);
203}
204
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700205bool UpdateAttempter::CalculateUpdateParams(const string& app_version,
206 const string& omaha_url,
207 bool obey_proxies,
208 bool interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800209 bool is_test_mode) {
Darin Petkov1023a602010-08-30 13:47:51 -0700210 http_response_code_ = 0;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200211
Gilad Arnold7c04e762012-05-23 10:54:02 -0700212 // Set the test mode flag for the current update attempt.
213 is_test_mode_ = is_test_mode;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700214 RefreshDevicePolicy();
215 const policy::DevicePolicy* device_policy = system_state_->device_policy();
216 if (device_policy) {
Jay Srinivasandc9a8ca2013-03-19 18:12:38 -0700217 bool update_disabled = false;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700218 if (device_policy->GetUpdateDisabled(&update_disabled))
219 omaha_request_params_->set_update_disabled(update_disabled);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700220
221 string target_version_prefix;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700222 if (device_policy->GetTargetVersionPrefix(&target_version_prefix))
223 omaha_request_params_->set_target_version_prefix(target_version_prefix);
Jay Srinivasan43488792012-06-19 00:25:31 -0700224
225 set<string> allowed_types;
226 string allowed_types_str;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700227 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700228 set<string>::const_iterator iter;
229 for (iter = allowed_types.begin(); iter != allowed_types.end(); ++iter)
230 allowed_types_str += *iter + " ";
231 }
232
233 LOG(INFO) << "Networks over which updates are allowed per policy : "
234 << (allowed_types_str.empty() ? "all" : allowed_types_str);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700235 }
236
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800237 CalculateScatteringParams(interactive);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200238
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800239 // Determine whether an alternative test address should be used.
Gilad Arnold28e2f392012-02-09 14:36:46 -0800240 string omaha_url_to_use = omaha_url;
Gilad Arnold7c04e762012-05-23 10:54:02 -0700241 if ((is_using_test_url_ = (omaha_url_to_use.empty() && is_test_mode_))) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800242 omaha_url_to_use = kTestUpdateUrl;
243 LOG(INFO) << "using alternative server address: " << omaha_url_to_use;
Gilad Arnold28e2f392012-02-09 14:36:46 -0800244 }
245
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700246 if (!omaha_request_params_->Init(app_version,
247 omaha_url_to_use,
248 interactive)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700249 LOG(ERROR) << "Unable to initialize Omaha request params.";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700250 return false;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700251 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800252
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700253 // Set the target channel iff ReleaseChannelDelegated policy is set to
254 // false and a non-empty ReleaseChannel policy is present. If delegated
255 // is true, we'll ignore ReleaseChannel policy value.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700256 if (device_policy) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700257 bool delegated = false;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700258 if (device_policy->GetReleaseChannelDelegated(&delegated) && delegated) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700259 LOG(INFO) << "Channel settings are delegated to user by policy. "
260 "Ignoring ReleaseChannel policy value";
261 }
262 else {
263 LOG(INFO) << "Channel settings are not delegated to the user by policy";
264 string target_channel;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700265 if (device_policy->GetReleaseChannel(&target_channel) &&
266 !target_channel.empty()) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700267 // Pass in false for powerwash_allowed until we add it to the policy
268 // protobuf.
269 LOG(INFO) << "Setting target channel from ReleaseChannel policy value";
270 omaha_request_params_->SetTargetChannel(target_channel, false);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700271
272 // Since this is the beginning of a new attempt, update the download
273 // channel. The download channel won't be updated until the next
274 // attempt, even if target channel changes meanwhile, so that how we'll
275 // know if we should cancel the current download attempt if there's
276 // such a change in target channel.
277 omaha_request_params_->UpdateDownloadChannel();
278 } else {
279 LOG(INFO) << "No ReleaseChannel specified in policy";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700280 }
281 }
282 }
283
Jay Srinivasan0a708742012-03-20 11:26:12 -0700284 LOG(INFO) << "update_disabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700285 << utils::ToString(omaha_request_params_->update_disabled())
Jay Srinivasan0a708742012-03-20 11:26:12 -0700286 << ", target_version_prefix = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700287 << omaha_request_params_->target_version_prefix()
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700288 << ", scatter_factor_in_seconds = "
289 << utils::FormatSecs(scatter_factor_.InSeconds());
290
291 LOG(INFO) << "Wall Clock Based Wait Enabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700292 << omaha_request_params_->wall_clock_based_wait_enabled()
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700293 << ", Update Check Count Wait Enabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700294 << omaha_request_params_->update_check_count_wait_enabled()
Jay Srinivasan21be0752012-07-25 15:44:56 -0700295 << ", Waiting Period = " << utils::FormatSecs(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700296 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700297
Andrew de los Reyes45168102010-11-22 11:13:50 -0800298 obeying_proxies_ = true;
299 if (obey_proxies || proxy_manual_checks_ == 0) {
300 LOG(INFO) << "forced to obey proxies";
301 // If forced to obey proxies, every 20th request will not use proxies
302 proxy_manual_checks_++;
303 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
304 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
305 proxy_manual_checks_ = 0;
306 obeying_proxies_ = false;
307 }
308 } else if (base::RandInt(0, 4) == 0) {
309 obeying_proxies_ = false;
310 }
311 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
312 "check we are ignoring the proxy settings and using "
313 "direct connections.";
314
Darin Petkov36275772010-10-01 11:40:57 -0700315 DisableDeltaUpdateIfNeeded();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700316 return true;
317}
318
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800319void UpdateAttempter::CalculateScatteringParams(bool interactive) {
Jay Srinivasan21be0752012-07-25 15:44:56 -0700320 // Take a copy of the old scatter value before we update it, as
321 // we need to update the waiting period if this value changes.
322 TimeDelta old_scatter_factor = scatter_factor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800323 const policy::DevicePolicy* device_policy = system_state_->device_policy();
Jay Srinivasan21be0752012-07-25 15:44:56 -0700324 if (device_policy) {
325 int64 new_scatter_factor_in_secs = 0;
326 device_policy->GetScatterFactorInSeconds(&new_scatter_factor_in_secs);
327 if (new_scatter_factor_in_secs < 0) // sanitize input, just in case.
328 new_scatter_factor_in_secs = 0;
329 scatter_factor_ = TimeDelta::FromSeconds(new_scatter_factor_in_secs);
330 }
331
332 bool is_scatter_enabled = false;
333 if (scatter_factor_.InSeconds() == 0) {
334 LOG(INFO) << "Scattering disabled since scatter factor is set to 0";
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800335 } else if (interactive) {
336 LOG(INFO) << "Scattering disabled as this is an interactive update check";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700337 } else if (!system_state_->IsOOBEComplete()) {
338 LOG(INFO) << "Scattering disabled since OOBE is not complete yet";
339 } else {
340 is_scatter_enabled = true;
341 LOG(INFO) << "Scattering is enabled";
342 }
343
344 if (is_scatter_enabled) {
345 // This means the scattering policy is turned on.
346 // Now check if we need to update the waiting period. The two cases
347 // in which we'd need to update the waiting period are:
348 // 1. First time in process or a scheduled check after a user-initiated one.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700349 // (omaha_request_params_->waiting_period will be zero in this case).
Jay Srinivasan21be0752012-07-25 15:44:56 -0700350 // 2. Admin has changed the scattering policy value.
351 // (new scattering value will be different from old one in this case).
352 int64 wait_period_in_secs = 0;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700353 if (omaha_request_params_->waiting_period().InSeconds() == 0) {
Jay Srinivasan21be0752012-07-25 15:44:56 -0700354 // First case. Check if we have a suitable value to set for
355 // the waiting period.
356 if (prefs_->GetInt64(kPrefsWallClockWaitPeriod, &wait_period_in_secs) &&
357 wait_period_in_secs > 0 &&
358 wait_period_in_secs <= scatter_factor_.InSeconds()) {
359 // This means:
360 // 1. There's a persisted value for the waiting period available.
361 // 2. And that persisted value is still valid.
362 // So, in this case, we should reuse the persisted value instead of
363 // generating a new random value to improve the chances of a good
364 // distribution for scattering.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700365 omaha_request_params_->set_waiting_period(
366 TimeDelta::FromSeconds(wait_period_in_secs));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700367 LOG(INFO) << "Using persisted wall-clock waiting period: " <<
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700368 utils::FormatSecs(
369 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700370 }
371 else {
372 // This means there's no persisted value for the waiting period
373 // available or its value is invalid given the new scatter_factor value.
374 // So, we should go ahead and regenerate a new value for the
375 // waiting period.
376 LOG(INFO) << "Persisted value not present or not valid ("
377 << utils::FormatSecs(wait_period_in_secs)
378 << ") for wall-clock waiting period.";
379 GenerateNewWaitingPeriod();
380 }
381 } else if (scatter_factor_ != old_scatter_factor) {
382 // This means there's already a waiting period value, but we detected
383 // a change in the scattering policy value. So, we should regenerate the
384 // waiting period to make sure it's within the bounds of the new scatter
385 // factor value.
386 GenerateNewWaitingPeriod();
387 } else {
388 // Neither the first time scattering is enabled nor the scattering value
389 // changed. Nothing to do.
390 LOG(INFO) << "Keeping current wall-clock waiting period: " <<
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700391 utils::FormatSecs(
392 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700393 }
394
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700395 // The invariant at this point is that omaha_request_params_->waiting_period
Jay Srinivasan21be0752012-07-25 15:44:56 -0700396 // is non-zero no matter which path we took above.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700397 LOG_IF(ERROR, omaha_request_params_->waiting_period().InSeconds() == 0)
Jay Srinivasan21be0752012-07-25 15:44:56 -0700398 << "Waiting Period should NOT be zero at this point!!!";
399
400 // Since scattering is enabled, wall clock based wait will always be
401 // enabled.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700402 omaha_request_params_->set_wall_clock_based_wait_enabled(true);
Jay Srinivasan21be0752012-07-25 15:44:56 -0700403
404 // If we don't have any issues in accessing the file system to update
405 // the update check count value, we'll turn that on as well.
406 bool decrement_succeeded = DecrementUpdateCheckCount();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700407 omaha_request_params_->set_update_check_count_wait_enabled(
408 decrement_succeeded);
Jay Srinivasan21be0752012-07-25 15:44:56 -0700409 } else {
410 // This means the scattering feature is turned off or disabled for
411 // this particular update check. Make sure to disable
412 // all the knobs and artifacts so that we don't invoke any scattering
413 // related code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700414 omaha_request_params_->set_wall_clock_based_wait_enabled(false);
415 omaha_request_params_->set_update_check_count_wait_enabled(false);
416 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(0));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700417 prefs_->Delete(kPrefsWallClockWaitPeriod);
418 prefs_->Delete(kPrefsUpdateCheckCount);
419 // Don't delete the UpdateFirstSeenAt file as we don't want manual checks
420 // that result in no-updates (e.g. due to server side throttling) to
421 // cause update starvation by having the client generate a new
422 // UpdateFirstSeenAt for each scheduled check that follows a manual check.
423 }
424}
425
426void UpdateAttempter::GenerateNewWaitingPeriod() {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700427 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(
428 base::RandInt(1, scatter_factor_.InSeconds())));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700429
430 LOG(INFO) << "Generated new wall-clock waiting period: " << utils::FormatSecs(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700431 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700432
433 // Do a best-effort to persist this in all cases. Even if the persistence
434 // fails, we'll still be able to scatter based on our in-memory value.
435 // The persistence only helps in ensuring a good overall distribution
436 // across multiple devices if they tend to reboot too often.
437 prefs_->SetInt64(kPrefsWallClockWaitPeriod,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700438 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700439}
440
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700441void UpdateAttempter::BuildUpdateActions(bool interactive) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700442 CHECK(!processor_->IsRunning());
443 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700444
445 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800446 LibcurlHttpFetcher* update_check_fetcher =
Gilad Arnold7c04e762012-05-23 10:54:02 -0700447 new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_);
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700448 // Try harder to connect to the network, esp when not interactive.
449 // See comment in libcurl_http_fetcher.cc.
450 update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700451 update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700452 shared_ptr<OmahaRequestAction> update_check_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800453 new OmahaRequestAction(system_state_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700454 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700455 update_check_fetcher, // passes ownership
456 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700457 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800458 new OmahaResponseHandlerAction(system_state_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700459 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700460 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700461 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700462 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700463 shared_ptr<OmahaRequestAction> download_started_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800464 new OmahaRequestAction(system_state_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700465 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700466 OmahaEvent::kTypeUpdateDownloadStarted),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700467 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700468 system_state_,
469 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700470 false));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700471 LibcurlHttpFetcher* download_fetcher =
Gilad Arnold7c04e762012-05-23 10:54:02 -0700472 new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700473 download_fetcher->set_check_certificate(CertificateChecker::kDownload);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700474 shared_ptr<DownloadAction> download_action(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700475 new DownloadAction(prefs_,
Jay Srinivasanf0572052012-10-23 18:12:56 -0700476 system_state_,
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800477 new MultiRangeHttpFetcher(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700478 download_fetcher))); // passes ownership
Darin Petkov8c2980e2010-07-16 15:16:49 -0700479 shared_ptr<OmahaRequestAction> download_finished_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800480 new OmahaRequestAction(system_state_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700481 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700482 OmahaEvent::kTypeUpdateDownloadFinished),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700483 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700484 system_state_,
485 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700486 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800487 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700488 new FilesystemCopierAction(false, true));
Darin Petkov3aefa862010-12-07 14:45:00 -0800489 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700490 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800491 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
492 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700493 shared_ptr<OmahaRequestAction> update_complete_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800494 new OmahaRequestAction(system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700495 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700496 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700497 system_state_,
498 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700499 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700500
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700501 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700502 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700503 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700504
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700505 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
506 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
507 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700508 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700509 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700510 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700511 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700512 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800513 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
514 actions_.push_back(shared_ptr<AbstractAction>(
515 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800516 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700517 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700518
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700519 // Enqueue the actions
520 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
521 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700522 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700523 }
524
525 // Bond them together. We have to use the leaf-types when calling
526 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700527 BondActions(update_check_action.get(),
528 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700529 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700530 filesystem_copier_action.get());
531 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700532 kernel_filesystem_copier_action.get());
533 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700534 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700535 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800536 filesystem_verifier_action.get());
537 BondActions(filesystem_verifier_action.get(),
538 kernel_filesystem_verifier_action.get());
539 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800540 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700541}
542
Gilad Arnold28e2f392012-02-09 14:36:46 -0800543void UpdateAttempter::CheckForUpdate(const string& app_version,
Jay Srinivasane73acab2012-07-10 14:34:03 -0700544 const string& omaha_url,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800545 bool interactive) {
Jay Srinivasan08fce042012-06-07 16:31:01 -0700546 LOG(INFO) << "New update check requested";
547
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700548 if (status_ != UPDATE_STATUS_IDLE) {
Jay Srinivasan08fce042012-06-07 16:31:01 -0700549 LOG(INFO) << "Skipping update check because current status is "
550 << UpdateStatusToString(status_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700551 return;
552 }
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800553
554 // Read GPIO signals and determine whether this is an automated test scenario.
555 // For safety, we only allow a test update to be performed once; subsequent
556 // update requests will be carried out normally.
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800557 bool is_test_mode = (!is_test_update_attempted_ &&
558 system_state_->gpio_handler()->IsTestModeSignaled());
Gilad Arnold7c04e762012-05-23 10:54:02 -0700559 if (is_test_mode) {
560 LOG(WARNING) << "this is a test mode update attempt!";
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700561 is_test_update_attempted_ = true;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800562 }
563
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800564 // Pass through the interactive flag, in case we want to simulate a scheduled
565 // test.
566 Update(app_version, omaha_url, true, interactive, is_test_mode);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700567}
568
Darin Petkov296889c2010-07-23 16:20:54 -0700569bool UpdateAttempter::RebootIfNeeded() {
570 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
571 LOG(INFO) << "Reboot requested, but status is "
572 << UpdateStatusToString(status_) << ", so not rebooting.";
573 return false;
574 }
575 TEST_AND_RETURN_FALSE(utils::Reboot());
576 return true;
577}
578
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700579// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700580void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700581 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700582 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700583 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700584 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700585
Chris Sosa4f8ee272012-11-30 13:01:54 -0800586 // Reset cpu shares back to normal.
587 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700588
Darin Petkov09f96c32010-07-20 09:24:57 -0700589 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
590 LOG(INFO) << "Error event sent.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800591
592 // Inform scheduler of new status; also specifically inform about a failed
593 // update attempt with a test address.
594 SetStatusAndNotify(UPDATE_STATUS_IDLE,
595 (is_using_test_url_ ? kUpdateNoticeTestAddrFailed :
596 kUpdateNoticeUnspecified));
597
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700598 if (!fake_update_success_) {
599 return;
600 }
601 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
602 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700603 }
604
Darin Petkovc1a8b422010-07-19 11:34:49 -0700605 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700606 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700607 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700608 prefs_->SetString(kPrefsPreviousVersion,
609 omaha_request_params_->app_version());
Darin Petkov9b230572010-10-08 10:20:09 -0700610 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700611
612 // Since we're done with scattering fully at this point, this is the
613 // safest point delete the state files, as we're sure that the status is
614 // set to reboot (which means no more updates will be applied until reboot)
615 // This deletion is required for correctness as we want the next update
616 // check to re-create a new random number for the update check count.
617 // Similarly, we also delete the wall-clock-wait period that was persisted
618 // so that we start with a new random value for the next update check
619 // after reboot so that the same device is not favored or punished in any
620 // way.
621 prefs_->Delete(kPrefsUpdateCheckCount);
622 prefs_->Delete(kPrefsWallClockWaitPeriod);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700623 prefs_->Delete(kPrefsUpdateFirstSeenAt);
Jay Srinivasanf0572052012-10-23 18:12:56 -0700624 LOG(INFO) << "Update successfully applied, waiting to reboot.";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700625
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800626 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
627 kUpdateNoticeUnspecified);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700628
629 // Report the time it took to update the system.
630 int64_t update_time = time(NULL) - last_checked_time_;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700631 if (!fake_update_success_)
Jay Srinivasanf0572052012-10-23 18:12:56 -0700632 system_state_->metrics_lib()->SendToUMA(
633 "Installer.UpdateTime",
634 static_cast<int>(update_time), // sample
635 1, // min = 1 second
636 20 * 60, // max = 20 minutes
637 50); // buckets
638
639 // Also report the success code so that the percentiles can be
640 // interpreted properly for the remaining error codes in UMA.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800641 utils::SendErrorCodeToUma(system_state_, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700642 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700643 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700644
Darin Petkov1023a602010-08-30 13:47:51 -0700645 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700646 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700647 }
648 LOG(INFO) << "No update.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800649 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700650}
651
652void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Chris Sosa4f8ee272012-11-30 13:01:54 -0800653 // Reset cpu shares back to normal.
654 CleanupCpuSharesManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700655 download_progress_ = 0.0;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800656 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700657 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700658 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700659}
660
661// Called whenever an action has finished processing, either successfully
662// or otherwise.
663void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
664 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700665 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700666 // Reset download progress regardless of whether or not the download
667 // action succeeded. Also, get the response code from HTTP request
668 // actions (update download as well as the initial update check
669 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700670 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700671 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700672 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700673 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
674 http_response_code_ = download_action->GetHTTPResponseCode();
675 } else if (type == OmahaRequestAction::StaticType()) {
676 OmahaRequestAction* omaha_request_action =
677 dynamic_cast<OmahaRequestAction*>(action);
678 // If the request is not an event, then it's the update-check.
679 if (!omaha_request_action->IsEvent()) {
680 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700681 // Forward the server-dictated poll interval to the update check
682 // scheduler, if any.
683 if (update_check_scheduler_) {
684 update_check_scheduler_->set_poll_interval(
685 omaha_request_action->GetOutputObject().poll_interval);
686 }
Darin Petkov1023a602010-08-30 13:47:51 -0700687 }
688 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700689 if (code != kActionCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700690 // If the current state is at or past the download phase, count the failure
691 // in case a switch to full update becomes necessary. Ignore network
692 // transfer timeouts and failures.
Darin Petkov36275772010-10-01 11:40:57 -0700693 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
Darin Petkov36275772010-10-01 11:40:57 -0700694 code != kActionCodeDownloadTransferError) {
695 MarkDeltaUpdateFailure();
696 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700697 // On failure, schedule an error event to be sent to Omaha.
698 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700699 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700700 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700701 // Find out which action completed.
702 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700703 // Note that the status will be updated to DOWNLOADING when some bytes get
704 // actually downloaded from the server and the BytesReceived callback is
705 // invoked. This avoids notifying the user that a download has started in
706 // cases when the server and the client are unable to initiate the download.
707 CHECK(action == response_handler_action_.get());
708 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700709 last_checked_time_ = time(NULL);
710 // TODO(adlr): put version in InstallPlan
711 new_version_ = "0.0.0.0";
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700712 new_payload_size_ = plan.payload_size;
Darin Petkov9b230572010-10-08 10:20:09 -0700713 SetupDownload();
Chris Sosa4f8ee272012-11-30 13:01:54 -0800714 SetupCpuSharesManagement();
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800715 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE,
716 kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700717 } else if (type == DownloadAction::StaticType()) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800718 SetStatusAndNotify(UPDATE_STATUS_FINALIZING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700719 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700720}
721
722// Stop updating. An attempt will be made to record status to the disk
723// so that updates can be resumed later.
724void UpdateAttempter::Terminate() {
725 // TODO(adlr): implement this method.
726 NOTIMPLEMENTED();
727}
728
729// Try to resume from a previously Terminate()d update.
730void UpdateAttempter::ResumeUpdating() {
731 // TODO(adlr): implement this method.
732 NOTIMPLEMENTED();
733}
734
Darin Petkov9d911fa2010-08-19 09:36:08 -0700735void UpdateAttempter::SetDownloadStatus(bool active) {
736 download_active_ = active;
737 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
738}
739
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700740void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700741 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700742 LOG(ERROR) << "BytesReceived called while not downloading.";
743 return;
744 }
Darin Petkovaf183052010-08-23 12:07:13 -0700745 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700746 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700747 // Self throttle based on progress. Also send notifications if
748 // progress is too slow.
749 const double kDeltaPercent = 0.01; // 1%
750 if (status_ != UPDATE_STATUS_DOWNLOADING ||
751 bytes_received == total ||
752 progress - download_progress_ >= kDeltaPercent ||
753 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
754 download_progress_ = progress;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800755 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700756 }
757}
758
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700759bool UpdateAttempter::ResetStatus() {
760 LOG(INFO) << "Attempting to reset state from "
761 << UpdateStatusToString(status_) << " to UPDATE_STATUS_IDLE";
762
763 switch (status_) {
764 case UPDATE_STATUS_IDLE:
765 // no-op.
766 return true;
767
768 case UPDATE_STATUS_UPDATED_NEED_REBOOT: {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700769 bool ret_value = true;
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700770 status_ = UPDATE_STATUS_IDLE;
771 LOG(INFO) << "Reset Successful";
772
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700773 // Remove the reboot marker so that if the machine is rebooted
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700774 // after resetting to idle state, it doesn't go back to
775 // UPDATE_STATUS_UPDATED_NEED_REBOOT state.
776 const FilePath kUpdateCompletedMarkerPath(kUpdateCompletedMarker);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700777 if (!file_util::Delete(kUpdateCompletedMarkerPath, false))
778 ret_value = false;
779
780 return ret_value;
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700781 }
782
783 default:
784 LOG(ERROR) << "Reset not allowed in this state.";
785 return false;
786 }
787}
788
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700789bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
790 double* progress,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800791 string* current_operation,
792 string* new_version,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700793 int64_t* new_payload_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700794 *last_checked_time = last_checked_time_;
795 *progress = download_progress_;
796 *current_operation = UpdateStatusToString(status_);
797 *new_version = new_version_;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700798 *new_payload_size = new_payload_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700799 return true;
800}
801
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700802void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700803 if (update_boot_flags_running_) {
804 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700805 return;
806 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700807 if (updated_boot_flags_) {
808 LOG(INFO) << "Already updated boot flags. Skipping.";
809 if (start_action_processor_) {
810 ScheduleProcessingStart();
811 }
812 return;
813 }
814 // This is purely best effort. Failures should be logged by Subprocess. Run
815 // the script asynchronously to avoid blocking the event loop regardless of
816 // the script runtime.
817 update_boot_flags_running_ = true;
818 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700819 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700820 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
821 CompleteUpdateBootFlags(1);
822 }
823}
824
825void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
826 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700827 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700828 if (start_action_processor_) {
829 ScheduleProcessingStart();
830 }
831}
832
833void UpdateAttempter::StaticCompleteUpdateBootFlags(
834 int return_code,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800835 const string& output,
Darin Petkov58dd1342011-05-06 12:05:13 -0700836 void* p) {
837 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700838}
839
Darin Petkov61635a92011-05-18 16:20:36 -0700840void UpdateAttempter::BroadcastStatus() {
841 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700842 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700843 }
Darin Petkovaf183052010-08-23 12:07:13 -0700844 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700845 update_engine_service_emit_status_update(
846 dbus_service_,
847 last_checked_time_,
848 download_progress_,
849 UpdateStatusToString(status_),
850 new_version_.c_str(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700851 new_payload_size_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700852}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700853
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800854uint32_t UpdateAttempter::GetErrorCodeFlags() {
855 uint32_t flags = 0;
856
857 if (!utils::IsNormalBootMode())
858 flags |= kActionCodeDevModeFlag;
859
860 if (response_handler_action_.get() &&
861 response_handler_action_->install_plan().is_resume)
862 flags |= kActionCodeResumedFlag;
863
864 if (!utils::IsOfficialBuild())
865 flags |= kActionCodeTestImageFlag;
866
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700867 if (omaha_request_params_->update_url() != kProductionOmahaUrl)
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800868 flags |= kActionCodeTestOmahaUrlFlag;
869
870 return flags;
871}
872
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700873bool UpdateAttempter::ShouldCancel(ActionExitCode* cancel_reason) {
874 // Check if the channel we're attempting to update to is the same as the
875 // target channel currently chosen by the user.
876 OmahaRequestParams* params = system_state_->request_params();
877 if (params->download_channel() != params->target_channel()) {
878 LOG(ERROR) << "Aborting download as target channel: "
879 << params->target_channel()
880 << " is different from the download channel: "
881 << params->download_channel();
882 *cancel_reason = kActionCodeUpdateCanceledByChannelChange;
883 return true;
884 }
885
886 return false;
887}
888
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800889void UpdateAttempter::SetStatusAndNotify(UpdateStatus status,
890 UpdateNotice notice) {
Darin Petkov61635a92011-05-18 16:20:36 -0700891 status_ = status;
892 if (update_check_scheduler_) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800893 update_check_scheduler_->SetUpdateStatus(status_, notice);
Darin Petkov61635a92011-05-18 16:20:36 -0700894 }
895 BroadcastStatus();
896}
897
Darin Petkov777dbfa2010-07-20 15:03:37 -0700898void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
899 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700900 if (error_event_.get()) {
901 // This shouldn't really happen.
902 LOG(WARNING) << "There's already an existing pending error event.";
903 return;
904 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700905
Darin Petkovabc7bc02011-02-23 14:39:43 -0800906 // For now assume that a generic Omaha response action failure means that
907 // there's no update so don't send an event. Also, double check that the
908 // failure has not occurred while sending an error event -- in which case
909 // don't schedule another. This shouldn't really happen but just in case...
910 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
911 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700912 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
913 return;
914 }
915
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700916 // Classify the code to generate the appropriate result so that
917 // the Borgmon charts show up the results correctly.
918 // Do this before calling GetErrorCodeForAction which could potentially
919 // augment the bit representation of code and thus cause no matches for
920 // the switch cases below.
921 OmahaEvent::Result event_result;
922 switch (code) {
923 case kActionCodeOmahaUpdateIgnoredPerPolicy:
924 case kActionCodeOmahaUpdateDeferredPerPolicy:
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800925 case kActionCodeOmahaUpdateDeferredForBackoff:
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700926 event_result = OmahaEvent::kResultUpdateDeferred;
927 break;
928 default:
929 event_result = OmahaEvent::kResultError;
930 break;
931 }
932
Darin Petkov777dbfa2010-07-20 15:03:37 -0700933 code = GetErrorCodeForAction(action, code);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700934 fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700935
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800936 // Compute the final error code with all the bit flags to be sent to Omaha.
937 code = static_cast<ActionExitCode>(code | GetErrorCodeFlags());
Darin Petkov09f96c32010-07-20 09:24:57 -0700938 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700939 event_result,
Darin Petkov09f96c32010-07-20 09:24:57 -0700940 code));
941}
942
943bool UpdateAttempter::ScheduleErrorEventAction() {
944 if (error_event_.get() == NULL)
945 return false;
946
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800947 LOG(ERROR) << "Update failed.";
948 system_state_->payload_state()->UpdateFailed(error_event_->error_code);
949
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800950 // Send it to Uma.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800951 LOG(INFO) << "Reporting the error event";
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800952 utils::SendErrorCodeToUma(system_state_, error_event_->error_code);
953
954 // Send it to Omaha.
Darin Petkov09f96c32010-07-20 09:24:57 -0700955 shared_ptr<OmahaRequestAction> error_event_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800956 new OmahaRequestAction(system_state_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700957 error_event_.release(), // Pass ownership.
Jay Srinivasan08fce042012-06-07 16:31:01 -0700958 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700959 system_state_,
960 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700961 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700962 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700963 processor_->EnqueueAction(error_event_action.get());
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800964 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT,
965 kUpdateNoticeUnspecified);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700966 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700967 return true;
968}
969
Chris Sosa4f8ee272012-11-30 13:01:54 -0800970void UpdateAttempter::SetCpuShares(utils::CpuShares shares) {
971 if (shares_ == shares) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700972 return;
973 }
Chris Sosa4f8ee272012-11-30 13:01:54 -0800974 if (utils::SetCpuShares(shares)) {
975 shares_ = shares;
976 LOG(INFO) << "CPU shares = " << shares_;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700977 }
978}
979
Chris Sosa4f8ee272012-11-30 13:01:54 -0800980void UpdateAttempter::SetupCpuSharesManagement() {
981 if (manage_shares_source_) {
982 LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed.";
983 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700984 }
Chris Sosa4f8ee272012-11-30 13:01:54 -0800985 const int kCpuSharesTimeout = 2 * 60 * 60; // 2 hours
986 manage_shares_source_ = g_timeout_source_new_seconds(kCpuSharesTimeout);
987 g_source_set_callback(manage_shares_source_,
988 StaticManageCpuSharesCallback,
Darin Petkovc6c135c2010-08-11 13:36:18 -0700989 this,
990 NULL);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800991 g_source_attach(manage_shares_source_, NULL);
992 SetCpuShares(utils::kCpuSharesLow);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700993}
994
Chris Sosa4f8ee272012-11-30 13:01:54 -0800995void UpdateAttempter::CleanupCpuSharesManagement() {
996 if (manage_shares_source_) {
997 g_source_destroy(manage_shares_source_);
998 manage_shares_source_ = NULL;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700999 }
Chris Sosa4f8ee272012-11-30 13:01:54 -08001000 SetCpuShares(utils::kCpuSharesNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -07001001}
1002
Chris Sosa4f8ee272012-11-30 13:01:54 -08001003gboolean UpdateAttempter::StaticManageCpuSharesCallback(gpointer data) {
1004 return reinterpret_cast<UpdateAttempter*>(data)->ManageCpuSharesCallback();
Darin Petkovc6c135c2010-08-11 13:36:18 -07001005}
1006
Darin Petkove6ef2f82011-03-07 17:31:11 -08001007gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
1008 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
1009 return FALSE; // Don't call this callback again.
1010}
1011
Darin Petkov58dd1342011-05-06 12:05:13 -07001012void UpdateAttempter::ScheduleProcessingStart() {
1013 LOG(INFO) << "Scheduling an action processor start.";
1014 start_action_processor_ = false;
1015 g_idle_add(&StaticStartProcessing, this);
1016}
1017
Chris Sosa4f8ee272012-11-30 13:01:54 -08001018bool UpdateAttempter::ManageCpuSharesCallback() {
1019 SetCpuShares(utils::kCpuSharesNormal);
1020 manage_shares_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -07001021 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -07001022}
1023
Darin Petkov36275772010-10-01 11:40:57 -07001024void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
1025 int64_t delta_failures;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001026 if (omaha_request_params_->delta_okay() &&
Darin Petkov36275772010-10-01 11:40:57 -07001027 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
1028 delta_failures >= kMaxDeltaUpdateFailures) {
1029 LOG(WARNING) << "Too many delta update failures, forcing full update.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001030 omaha_request_params_->set_delta_okay(false);
Darin Petkov36275772010-10-01 11:40:57 -07001031 }
1032}
1033
1034void UpdateAttempter::MarkDeltaUpdateFailure() {
Darin Petkov2dd01092010-10-08 15:43:05 -07001035 // Don't try to resume a failed delta update.
1036 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -07001037 int64_t delta_failures;
1038 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
1039 delta_failures < 0) {
1040 delta_failures = 0;
1041 }
1042 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
1043}
1044
Darin Petkov9b230572010-10-08 10:20:09 -07001045void UpdateAttempter::SetupDownload() {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001046 MultiRangeHttpFetcher* fetcher =
1047 dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001048 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -07001049 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001050 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -07001051 int64_t manifest_metadata_size = 0;
1052 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001053 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001054 // If there're remaining unprocessed data blobs, fetch them. Be careful not
1055 // to request data beyond the end of the payload to avoid 416 HTTP response
1056 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -07001057 int64_t next_data_offset = 0;
1058 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001059 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001060 if (resume_offset < response_handler_action_->install_plan().payload_size) {
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001061 fetcher->AddRange(resume_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001062 }
Darin Petkov9b230572010-10-08 10:20:09 -07001063 } else {
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001064 fetcher->AddRange(0);
Darin Petkov9b230572010-10-08 10:20:09 -07001065 }
Darin Petkov9b230572010-10-08 10:20:09 -07001066}
1067
Thieu Le116fda32011-04-19 11:01:54 -07001068void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -07001069 if (!processor_->IsRunning()) {
1070 shared_ptr<OmahaRequestAction> ping_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001071 new OmahaRequestAction(system_state_,
Thieu Led88a8572011-05-26 09:09:19 -07001072 NULL,
Jay Srinivasan08fce042012-06-07 16:31:01 -07001073 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -07001074 system_state_,
1075 is_test_mode_),
Thieu Led88a8572011-05-26 09:09:19 -07001076 true));
1077 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
1078 processor_->set_delegate(NULL);
1079 processor_->EnqueueAction(ping_action.get());
1080 // Call StartProcessing() synchronously here to avoid any race conditions
1081 // caused by multiple outstanding ping Omaha requests. If we call
1082 // StartProcessing() asynchronously, the device can be suspended before we
1083 // get a chance to callback to StartProcessing(). When the device resumes
1084 // (assuming the device sleeps longer than the next update check period),
1085 // StartProcessing() is called back and at the same time, the next update
1086 // check is fired which eventually invokes StartProcessing(). A crash
1087 // can occur because StartProcessing() checks to make sure that the
1088 // processor is idle which it isn't due to the two concurrent ping Omaha
1089 // requests.
1090 processor_->StartProcessing();
1091 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -07001092 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -07001093 }
Thieu Led88a8572011-05-26 09:09:19 -07001094
1095 // Update the status which will schedule the next update check
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001096 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
1097 kUpdateNoticeUnspecified);
Thieu Le116fda32011-04-19 11:01:54 -07001098}
1099
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001100
1101bool UpdateAttempter::DecrementUpdateCheckCount() {
1102 int64 update_check_count_value;
1103
1104 if (!prefs_->Exists(kPrefsUpdateCheckCount)) {
1105 // This file does not exist. This means we haven't started our update
1106 // check count down yet, so nothing more to do. This file will be created
1107 // later when we first satisfy the wall-clock-based-wait period.
1108 LOG(INFO) << "No existing update check count. That's normal.";
1109 return true;
1110 }
1111
1112 if (prefs_->GetInt64(kPrefsUpdateCheckCount, &update_check_count_value)) {
1113 // Only if we're able to read a proper integer value, then go ahead
1114 // and decrement and write back the result in the same file, if needed.
1115 LOG(INFO) << "Update check count = " << update_check_count_value;
1116
1117 if (update_check_count_value == 0) {
1118 // It could be 0, if, for some reason, the file didn't get deleted
1119 // when we set our status to waiting for reboot. so we just leave it
1120 // as is so that we can prevent another update_check wait for this client.
1121 LOG(INFO) << "Not decrementing update check count as it's already 0.";
1122 return true;
1123 }
1124
1125 if (update_check_count_value > 0)
1126 update_check_count_value--;
1127 else
1128 update_check_count_value = 0;
1129
1130 // Write out the new value of update_check_count_value.
1131 if (prefs_->SetInt64(kPrefsUpdateCheckCount, update_check_count_value)) {
1132 // We successfully wrote out te new value, so enable the
1133 // update check based wait.
1134 LOG(INFO) << "New update check count = " << update_check_count_value;
1135 return true;
1136 }
1137 }
1138
1139 LOG(INFO) << "Deleting update check count state due to read/write errors.";
1140
1141 // We cannot read/write to the file, so disable the update check based wait
1142 // so that we don't get stuck in this OS version by any chance (which could
1143 // happen if there's some bug that causes to read/write incorrectly).
1144 // Also attempt to delete the file to do our best effort to cleanup.
1145 prefs_->Delete(kPrefsUpdateCheckCount);
1146 return false;
1147}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001148} // namespace chromeos_update_engine