blob: 2aee0be6652c69d66844d37eec97152eda35bf8c [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
187bool UpdateAttempter::CalculateUpdateParams(const string& app_version,
188 const string& omaha_url,
189 bool obey_proxies,
190 bool interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800191 bool is_test_mode) {
Darin Petkov1023a602010-08-30 13:47:51 -0700192 http_response_code_ = 0;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200193
Gilad Arnold7c04e762012-05-23 10:54:02 -0700194 // Set the test mode flag for the current update attempt.
195 is_test_mode_ = is_test_mode;
196
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200197 // Lazy initialize the policy provider, or reload the latest policy data.
Julian Pastarmov5b3f3b92013-02-04 18:04:48 +0100198 if (!policy_provider_.get())
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200199 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmov5b3f3b92013-02-04 18:04:48 +0100200 policy_provider_->Reload();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200201
Jay Srinivasan0a708742012-03-20 11:26:12 -0700202 if (policy_provider_->device_policy_is_loaded()) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700203 LOG(INFO) << "Device policies/settings present";
204
Jay Srinivasan0a708742012-03-20 11:26:12 -0700205 const policy::DevicePolicy& device_policy =
206 policy_provider_->GetDevicePolicy();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700207
208 bool update_disabled;
209 device_policy.GetUpdateDisabled(&update_disabled);
210 omaha_request_params_->set_update_disabled(update_disabled);
211
212 string target_version_prefix;
213 device_policy.GetTargetVersionPrefix(&target_version_prefix);
214 omaha_request_params_->set_target_version_prefix(target_version_prefix);
Jay Srinivasan43488792012-06-19 00:25:31 -0700215
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800216 system_state_->set_device_policy(&device_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700217
218 set<string> allowed_types;
219 string allowed_types_str;
220 if (device_policy.GetAllowedConnectionTypesForUpdate(&allowed_types)) {
221 set<string>::const_iterator iter;
222 for (iter = allowed_types.begin(); iter != allowed_types.end(); ++iter)
223 allowed_types_str += *iter + " ";
224 }
225
226 LOG(INFO) << "Networks over which updates are allowed per policy : "
227 << (allowed_types_str.empty() ? "all" : allowed_types_str);
228 } else {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700229 LOG(INFO) << "No device policies/settings present.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 system_state_->set_device_policy(NULL);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700231 }
232
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800233 CalculateScatteringParams(interactive);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200234
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800235 // Determine whether an alternative test address should be used.
Gilad Arnold28e2f392012-02-09 14:36:46 -0800236 string omaha_url_to_use = omaha_url;
Gilad Arnold7c04e762012-05-23 10:54:02 -0700237 if ((is_using_test_url_ = (omaha_url_to_use.empty() && is_test_mode_))) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800238 omaha_url_to_use = kTestUpdateUrl;
239 LOG(INFO) << "using alternative server address: " << omaha_url_to_use;
Gilad Arnold28e2f392012-02-09 14:36:46 -0800240 }
241
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700242 if (!omaha_request_params_->Init(app_version,
243 omaha_url_to_use,
244 interactive)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700245 LOG(ERROR) << "Unable to initialize Omaha request device params.";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700246 return false;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700247 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800248
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700249 // Set the target channel iff ReleaseChannelDelegated policy is set to
250 // false and a non-empty ReleaseChannel policy is present. If delegated
251 // is true, we'll ignore ReleaseChannel policy value.
252 if (system_state_->device_policy()) {
253 bool delegated = false;
254 system_state_->device_policy()->GetReleaseChannelDelegated(&delegated);
255 if (delegated) {
256 LOG(INFO) << "Channel settings are delegated to user by policy. "
257 "Ignoring ReleaseChannel policy value";
258 }
259 else {
260 LOG(INFO) << "Channel settings are not delegated to the user by policy";
261 string target_channel;
262 system_state_->device_policy()->GetReleaseChannel(&target_channel);
263 if (target_channel.empty()) {
264 LOG(INFO) << "No ReleaseChannel specified in policy";
265 } else {
266 // Pass in false for powerwash_allowed until we add it to the policy
267 // protobuf.
268 LOG(INFO) << "Setting target channel from ReleaseChannel policy value";
269 omaha_request_params_->SetTargetChannel(target_channel, false);
270 }
271 }
272 }
273
Jay Srinivasan0a708742012-03-20 11:26:12 -0700274 LOG(INFO) << "update_disabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700275 << utils::ToString(omaha_request_params_->update_disabled())
Jay Srinivasan0a708742012-03-20 11:26:12 -0700276 << ", target_version_prefix = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700277 << omaha_request_params_->target_version_prefix()
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700278 << ", scatter_factor_in_seconds = "
279 << utils::FormatSecs(scatter_factor_.InSeconds());
280
281 LOG(INFO) << "Wall Clock Based Wait Enabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700282 << omaha_request_params_->wall_clock_based_wait_enabled()
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700283 << ", Update Check Count Wait Enabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700284 << omaha_request_params_->update_check_count_wait_enabled()
Jay Srinivasan21be0752012-07-25 15:44:56 -0700285 << ", Waiting Period = " << utils::FormatSecs(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700286 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700287
Andrew de los Reyes45168102010-11-22 11:13:50 -0800288 obeying_proxies_ = true;
289 if (obey_proxies || proxy_manual_checks_ == 0) {
290 LOG(INFO) << "forced to obey proxies";
291 // If forced to obey proxies, every 20th request will not use proxies
292 proxy_manual_checks_++;
293 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
294 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
295 proxy_manual_checks_ = 0;
296 obeying_proxies_ = false;
297 }
298 } else if (base::RandInt(0, 4) == 0) {
299 obeying_proxies_ = false;
300 }
301 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
302 "check we are ignoring the proxy settings and using "
303 "direct connections.";
304
Darin Petkov36275772010-10-01 11:40:57 -0700305 DisableDeltaUpdateIfNeeded();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700306 return true;
307}
308
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800309void UpdateAttempter::CalculateScatteringParams(bool interactive) {
Jay Srinivasan21be0752012-07-25 15:44:56 -0700310 // Take a copy of the old scatter value before we update it, as
311 // we need to update the waiting period if this value changes.
312 TimeDelta old_scatter_factor = scatter_factor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800313 const policy::DevicePolicy* device_policy = system_state_->device_policy();
Jay Srinivasan21be0752012-07-25 15:44:56 -0700314 if (device_policy) {
315 int64 new_scatter_factor_in_secs = 0;
316 device_policy->GetScatterFactorInSeconds(&new_scatter_factor_in_secs);
317 if (new_scatter_factor_in_secs < 0) // sanitize input, just in case.
318 new_scatter_factor_in_secs = 0;
319 scatter_factor_ = TimeDelta::FromSeconds(new_scatter_factor_in_secs);
320 }
321
322 bool is_scatter_enabled = false;
323 if (scatter_factor_.InSeconds() == 0) {
324 LOG(INFO) << "Scattering disabled since scatter factor is set to 0";
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800325 } else if (interactive) {
326 LOG(INFO) << "Scattering disabled as this is an interactive update check";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700327 } else if (!system_state_->IsOOBEComplete()) {
328 LOG(INFO) << "Scattering disabled since OOBE is not complete yet";
329 } else {
330 is_scatter_enabled = true;
331 LOG(INFO) << "Scattering is enabled";
332 }
333
334 if (is_scatter_enabled) {
335 // This means the scattering policy is turned on.
336 // Now check if we need to update the waiting period. The two cases
337 // in which we'd need to update the waiting period are:
338 // 1. First time in process or a scheduled check after a user-initiated one.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700339 // (omaha_request_params_->waiting_period will be zero in this case).
Jay Srinivasan21be0752012-07-25 15:44:56 -0700340 // 2. Admin has changed the scattering policy value.
341 // (new scattering value will be different from old one in this case).
342 int64 wait_period_in_secs = 0;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700343 if (omaha_request_params_->waiting_period().InSeconds() == 0) {
Jay Srinivasan21be0752012-07-25 15:44:56 -0700344 // First case. Check if we have a suitable value to set for
345 // the waiting period.
346 if (prefs_->GetInt64(kPrefsWallClockWaitPeriod, &wait_period_in_secs) &&
347 wait_period_in_secs > 0 &&
348 wait_period_in_secs <= scatter_factor_.InSeconds()) {
349 // This means:
350 // 1. There's a persisted value for the waiting period available.
351 // 2. And that persisted value is still valid.
352 // So, in this case, we should reuse the persisted value instead of
353 // generating a new random value to improve the chances of a good
354 // distribution for scattering.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700355 omaha_request_params_->set_waiting_period(
356 TimeDelta::FromSeconds(wait_period_in_secs));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700357 LOG(INFO) << "Using persisted wall-clock waiting period: " <<
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700358 utils::FormatSecs(
359 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700360 }
361 else {
362 // This means there's no persisted value for the waiting period
363 // available or its value is invalid given the new scatter_factor value.
364 // So, we should go ahead and regenerate a new value for the
365 // waiting period.
366 LOG(INFO) << "Persisted value not present or not valid ("
367 << utils::FormatSecs(wait_period_in_secs)
368 << ") for wall-clock waiting period.";
369 GenerateNewWaitingPeriod();
370 }
371 } else if (scatter_factor_ != old_scatter_factor) {
372 // This means there's already a waiting period value, but we detected
373 // a change in the scattering policy value. So, we should regenerate the
374 // waiting period to make sure it's within the bounds of the new scatter
375 // factor value.
376 GenerateNewWaitingPeriod();
377 } else {
378 // Neither the first time scattering is enabled nor the scattering value
379 // changed. Nothing to do.
380 LOG(INFO) << "Keeping current wall-clock waiting period: " <<
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700381 utils::FormatSecs(
382 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700383 }
384
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700385 // The invariant at this point is that omaha_request_params_->waiting_period
Jay Srinivasan21be0752012-07-25 15:44:56 -0700386 // is non-zero no matter which path we took above.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700387 LOG_IF(ERROR, omaha_request_params_->waiting_period().InSeconds() == 0)
Jay Srinivasan21be0752012-07-25 15:44:56 -0700388 << "Waiting Period should NOT be zero at this point!!!";
389
390 // Since scattering is enabled, wall clock based wait will always be
391 // enabled.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700392 omaha_request_params_->set_wall_clock_based_wait_enabled(true);
Jay Srinivasan21be0752012-07-25 15:44:56 -0700393
394 // If we don't have any issues in accessing the file system to update
395 // the update check count value, we'll turn that on as well.
396 bool decrement_succeeded = DecrementUpdateCheckCount();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700397 omaha_request_params_->set_update_check_count_wait_enabled(
398 decrement_succeeded);
Jay Srinivasan21be0752012-07-25 15:44:56 -0700399 } else {
400 // This means the scattering feature is turned off or disabled for
401 // this particular update check. Make sure to disable
402 // all the knobs and artifacts so that we don't invoke any scattering
403 // related code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700404 omaha_request_params_->set_wall_clock_based_wait_enabled(false);
405 omaha_request_params_->set_update_check_count_wait_enabled(false);
406 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(0));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700407 prefs_->Delete(kPrefsWallClockWaitPeriod);
408 prefs_->Delete(kPrefsUpdateCheckCount);
409 // Don't delete the UpdateFirstSeenAt file as we don't want manual checks
410 // that result in no-updates (e.g. due to server side throttling) to
411 // cause update starvation by having the client generate a new
412 // UpdateFirstSeenAt for each scheduled check that follows a manual check.
413 }
414}
415
416void UpdateAttempter::GenerateNewWaitingPeriod() {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700417 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(
418 base::RandInt(1, scatter_factor_.InSeconds())));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700419
420 LOG(INFO) << "Generated new wall-clock waiting period: " << utils::FormatSecs(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700421 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700422
423 // Do a best-effort to persist this in all cases. Even if the persistence
424 // fails, we'll still be able to scatter based on our in-memory value.
425 // The persistence only helps in ensuring a good overall distribution
426 // across multiple devices if they tend to reboot too often.
427 prefs_->SetInt64(kPrefsWallClockWaitPeriod,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700428 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700429}
430
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700431void UpdateAttempter::BuildUpdateActions(bool interactive) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700432 CHECK(!processor_->IsRunning());
433 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700434
435 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800436 LibcurlHttpFetcher* update_check_fetcher =
Gilad Arnold7c04e762012-05-23 10:54:02 -0700437 new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_);
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700438 // Try harder to connect to the network, esp when not interactive.
439 // See comment in libcurl_http_fetcher.cc.
440 update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700441 update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700442 shared_ptr<OmahaRequestAction> update_check_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800443 new OmahaRequestAction(system_state_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700444 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700445 update_check_fetcher, // passes ownership
446 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700447 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800448 new OmahaResponseHandlerAction(system_state_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700449 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700450 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700451 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700452 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700453 shared_ptr<OmahaRequestAction> download_started_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800454 new OmahaRequestAction(system_state_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700455 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700456 OmahaEvent::kTypeUpdateDownloadStarted),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700457 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700458 system_state_,
459 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700460 false));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700461 LibcurlHttpFetcher* download_fetcher =
Gilad Arnold7c04e762012-05-23 10:54:02 -0700462 new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700463 download_fetcher->set_check_certificate(CertificateChecker::kDownload);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700464 shared_ptr<DownloadAction> download_action(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700465 new DownloadAction(prefs_,
Jay Srinivasanf0572052012-10-23 18:12:56 -0700466 system_state_,
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800467 new MultiRangeHttpFetcher(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700468 download_fetcher))); // passes ownership
Darin Petkov8c2980e2010-07-16 15:16:49 -0700469 shared_ptr<OmahaRequestAction> download_finished_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800470 new OmahaRequestAction(system_state_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700471 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700472 OmahaEvent::kTypeUpdateDownloadFinished),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700473 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700474 system_state_,
475 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700476 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800477 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700478 new FilesystemCopierAction(false, true));
Darin Petkov3aefa862010-12-07 14:45:00 -0800479 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700480 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800481 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
482 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700483 shared_ptr<OmahaRequestAction> update_complete_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800484 new OmahaRequestAction(system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700485 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700486 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700487 system_state_,
488 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700489 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700490
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700491 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700492 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700493 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700494
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700495 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
496 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
497 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700498 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700499 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700500 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700501 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700502 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800503 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
504 actions_.push_back(shared_ptr<AbstractAction>(
505 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800506 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700507 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700508
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700509 // Enqueue the actions
510 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
511 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700512 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700513 }
514
515 // Bond them together. We have to use the leaf-types when calling
516 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700517 BondActions(update_check_action.get(),
518 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700519 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700520 filesystem_copier_action.get());
521 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700522 kernel_filesystem_copier_action.get());
523 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700524 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700525 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800526 filesystem_verifier_action.get());
527 BondActions(filesystem_verifier_action.get(),
528 kernel_filesystem_verifier_action.get());
529 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800530 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700531}
532
Gilad Arnold28e2f392012-02-09 14:36:46 -0800533void UpdateAttempter::CheckForUpdate(const string& app_version,
Jay Srinivasane73acab2012-07-10 14:34:03 -0700534 const string& omaha_url,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800535 bool interactive) {
Jay Srinivasan08fce042012-06-07 16:31:01 -0700536 LOG(INFO) << "New update check requested";
537
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700538 if (status_ != UPDATE_STATUS_IDLE) {
Jay Srinivasan08fce042012-06-07 16:31:01 -0700539 LOG(INFO) << "Skipping update check because current status is "
540 << UpdateStatusToString(status_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700541 return;
542 }
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800543
544 // Read GPIO signals and determine whether this is an automated test scenario.
545 // For safety, we only allow a test update to be performed once; subsequent
546 // update requests will be carried out normally.
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800547 bool is_test_mode = (!is_test_update_attempted_ &&
548 system_state_->gpio_handler()->IsTestModeSignaled());
Gilad Arnold7c04e762012-05-23 10:54:02 -0700549 if (is_test_mode) {
550 LOG(WARNING) << "this is a test mode update attempt!";
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700551 is_test_update_attempted_ = true;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800552 }
553
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800554 // Pass through the interactive flag, in case we want to simulate a scheduled
555 // test.
556 Update(app_version, omaha_url, true, interactive, is_test_mode);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700557}
558
Darin Petkov296889c2010-07-23 16:20:54 -0700559bool UpdateAttempter::RebootIfNeeded() {
560 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
561 LOG(INFO) << "Reboot requested, but status is "
562 << UpdateStatusToString(status_) << ", so not rebooting.";
563 return false;
564 }
565 TEST_AND_RETURN_FALSE(utils::Reboot());
566 return true;
567}
568
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700569// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700570void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700571 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700572 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700573 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700574 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700575
Chris Sosa4f8ee272012-11-30 13:01:54 -0800576 // Reset cpu shares back to normal.
577 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700578
Darin Petkov09f96c32010-07-20 09:24:57 -0700579 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
580 LOG(INFO) << "Error event sent.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800581
582 // Inform scheduler of new status; also specifically inform about a failed
583 // update attempt with a test address.
584 SetStatusAndNotify(UPDATE_STATUS_IDLE,
585 (is_using_test_url_ ? kUpdateNoticeTestAddrFailed :
586 kUpdateNoticeUnspecified));
587
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700588 if (!fake_update_success_) {
589 return;
590 }
591 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
592 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700593 }
594
Darin Petkovc1a8b422010-07-19 11:34:49 -0700595 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700596 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700597 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700598 prefs_->SetString(kPrefsPreviousVersion,
599 omaha_request_params_->app_version());
Darin Petkov9b230572010-10-08 10:20:09 -0700600 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700601
602 // Since we're done with scattering fully at this point, this is the
603 // safest point delete the state files, as we're sure that the status is
604 // set to reboot (which means no more updates will be applied until reboot)
605 // This deletion is required for correctness as we want the next update
606 // check to re-create a new random number for the update check count.
607 // Similarly, we also delete the wall-clock-wait period that was persisted
608 // so that we start with a new random value for the next update check
609 // after reboot so that the same device is not favored or punished in any
610 // way.
611 prefs_->Delete(kPrefsUpdateCheckCount);
612 prefs_->Delete(kPrefsWallClockWaitPeriod);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700613 prefs_->Delete(kPrefsUpdateFirstSeenAt);
Jay Srinivasanf0572052012-10-23 18:12:56 -0700614 LOG(INFO) << "Update successfully applied, waiting to reboot.";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700615
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800616 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
617 kUpdateNoticeUnspecified);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700618
619 // Report the time it took to update the system.
620 int64_t update_time = time(NULL) - last_checked_time_;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700621 if (!fake_update_success_)
Jay Srinivasanf0572052012-10-23 18:12:56 -0700622 system_state_->metrics_lib()->SendToUMA(
623 "Installer.UpdateTime",
624 static_cast<int>(update_time), // sample
625 1, // min = 1 second
626 20 * 60, // max = 20 minutes
627 50); // buckets
628
629 // Also report the success code so that the percentiles can be
630 // interpreted properly for the remaining error codes in UMA.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800631 utils::SendErrorCodeToUma(system_state_, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700632 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700633 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700634
Darin Petkov1023a602010-08-30 13:47:51 -0700635 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700636 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700637 }
638 LOG(INFO) << "No update.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800639 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700640}
641
642void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Chris Sosa4f8ee272012-11-30 13:01:54 -0800643 // Reset cpu shares back to normal.
644 CleanupCpuSharesManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700645 download_progress_ = 0.0;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800646 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700647 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700648 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700649}
650
651// Called whenever an action has finished processing, either successfully
652// or otherwise.
653void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
654 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700655 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700656 // Reset download progress regardless of whether or not the download
657 // action succeeded. Also, get the response code from HTTP request
658 // actions (update download as well as the initial update check
659 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700660 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700661 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700662 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700663 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
664 http_response_code_ = download_action->GetHTTPResponseCode();
665 } else if (type == OmahaRequestAction::StaticType()) {
666 OmahaRequestAction* omaha_request_action =
667 dynamic_cast<OmahaRequestAction*>(action);
668 // If the request is not an event, then it's the update-check.
669 if (!omaha_request_action->IsEvent()) {
670 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700671 // Forward the server-dictated poll interval to the update check
672 // scheduler, if any.
673 if (update_check_scheduler_) {
674 update_check_scheduler_->set_poll_interval(
675 omaha_request_action->GetOutputObject().poll_interval);
676 }
Darin Petkov1023a602010-08-30 13:47:51 -0700677 }
678 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700679 if (code != kActionCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700680 // If the current state is at or past the download phase, count the failure
681 // in case a switch to full update becomes necessary. Ignore network
682 // transfer timeouts and failures.
Darin Petkov36275772010-10-01 11:40:57 -0700683 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
Darin Petkov36275772010-10-01 11:40:57 -0700684 code != kActionCodeDownloadTransferError) {
685 MarkDeltaUpdateFailure();
686 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700687 // On failure, schedule an error event to be sent to Omaha.
688 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700689 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700690 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700691 // Find out which action completed.
692 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700693 // Note that the status will be updated to DOWNLOADING when some bytes get
694 // actually downloaded from the server and the BytesReceived callback is
695 // invoked. This avoids notifying the user that a download has started in
696 // cases when the server and the client are unable to initiate the download.
697 CHECK(action == response_handler_action_.get());
698 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700699 last_checked_time_ = time(NULL);
700 // TODO(adlr): put version in InstallPlan
701 new_version_ = "0.0.0.0";
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700702 new_payload_size_ = plan.payload_size;
Darin Petkov9b230572010-10-08 10:20:09 -0700703 SetupDownload();
Chris Sosa4f8ee272012-11-30 13:01:54 -0800704 SetupCpuSharesManagement();
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800705 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE,
706 kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700707 } else if (type == DownloadAction::StaticType()) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800708 SetStatusAndNotify(UPDATE_STATUS_FINALIZING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700709 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700710}
711
712// Stop updating. An attempt will be made to record status to the disk
713// so that updates can be resumed later.
714void UpdateAttempter::Terminate() {
715 // TODO(adlr): implement this method.
716 NOTIMPLEMENTED();
717}
718
719// Try to resume from a previously Terminate()d update.
720void UpdateAttempter::ResumeUpdating() {
721 // TODO(adlr): implement this method.
722 NOTIMPLEMENTED();
723}
724
Darin Petkov9d911fa2010-08-19 09:36:08 -0700725void UpdateAttempter::SetDownloadStatus(bool active) {
726 download_active_ = active;
727 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
728}
729
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700730void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700731 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700732 LOG(ERROR) << "BytesReceived called while not downloading.";
733 return;
734 }
Darin Petkovaf183052010-08-23 12:07:13 -0700735 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700736 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700737 // Self throttle based on progress. Also send notifications if
738 // progress is too slow.
739 const double kDeltaPercent = 0.01; // 1%
740 if (status_ != UPDATE_STATUS_DOWNLOADING ||
741 bytes_received == total ||
742 progress - download_progress_ >= kDeltaPercent ||
743 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
744 download_progress_ = progress;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800745 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700746 }
747}
748
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700749bool UpdateAttempter::ResetStatus() {
750 LOG(INFO) << "Attempting to reset state from "
751 << UpdateStatusToString(status_) << " to UPDATE_STATUS_IDLE";
752
753 switch (status_) {
754 case UPDATE_STATUS_IDLE:
755 // no-op.
756 return true;
757
758 case UPDATE_STATUS_UPDATED_NEED_REBOOT: {
759 status_ = UPDATE_STATUS_IDLE;
760 LOG(INFO) << "Reset Successful";
761
762 // also remove the reboot marker so that if the machine is rebooted
763 // after resetting to idle state, it doesn't go back to
764 // UPDATE_STATUS_UPDATED_NEED_REBOOT state.
765 const FilePath kUpdateCompletedMarkerPath(kUpdateCompletedMarker);
766 return file_util::Delete(kUpdateCompletedMarkerPath, false);
767 }
768
769 default:
770 LOG(ERROR) << "Reset not allowed in this state.";
771 return false;
772 }
773}
774
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700775bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
776 double* progress,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800777 string* current_operation,
778 string* new_version,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700779 int64_t* new_payload_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700780 *last_checked_time = last_checked_time_;
781 *progress = download_progress_;
782 *current_operation = UpdateStatusToString(status_);
783 *new_version = new_version_;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700784 *new_payload_size = new_payload_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700785 return true;
786}
787
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700788void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700789 if (update_boot_flags_running_) {
790 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700791 return;
792 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700793 if (updated_boot_flags_) {
794 LOG(INFO) << "Already updated boot flags. Skipping.";
795 if (start_action_processor_) {
796 ScheduleProcessingStart();
797 }
798 return;
799 }
800 // This is purely best effort. Failures should be logged by Subprocess. Run
801 // the script asynchronously to avoid blocking the event loop regardless of
802 // the script runtime.
803 update_boot_flags_running_ = true;
804 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700805 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700806 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
807 CompleteUpdateBootFlags(1);
808 }
809}
810
811void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
812 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700813 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700814 if (start_action_processor_) {
815 ScheduleProcessingStart();
816 }
817}
818
819void UpdateAttempter::StaticCompleteUpdateBootFlags(
820 int return_code,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800821 const string& output,
Darin Petkov58dd1342011-05-06 12:05:13 -0700822 void* p) {
823 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700824}
825
Darin Petkov61635a92011-05-18 16:20:36 -0700826void UpdateAttempter::BroadcastStatus() {
827 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700828 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700829 }
Darin Petkovaf183052010-08-23 12:07:13 -0700830 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700831 update_engine_service_emit_status_update(
832 dbus_service_,
833 last_checked_time_,
834 download_progress_,
835 UpdateStatusToString(status_),
836 new_version_.c_str(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700837 new_payload_size_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700838}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700839
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800840uint32_t UpdateAttempter::GetErrorCodeFlags() {
841 uint32_t flags = 0;
842
843 if (!utils::IsNormalBootMode())
844 flags |= kActionCodeDevModeFlag;
845
846 if (response_handler_action_.get() &&
847 response_handler_action_->install_plan().is_resume)
848 flags |= kActionCodeResumedFlag;
849
850 if (!utils::IsOfficialBuild())
851 flags |= kActionCodeTestImageFlag;
852
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700853 if (omaha_request_params_->update_url() != kProductionOmahaUrl)
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800854 flags |= kActionCodeTestOmahaUrlFlag;
855
856 return flags;
857}
858
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800859void UpdateAttempter::SetStatusAndNotify(UpdateStatus status,
860 UpdateNotice notice) {
Darin Petkov61635a92011-05-18 16:20:36 -0700861 status_ = status;
862 if (update_check_scheduler_) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800863 update_check_scheduler_->SetUpdateStatus(status_, notice);
Darin Petkov61635a92011-05-18 16:20:36 -0700864 }
865 BroadcastStatus();
866}
867
Darin Petkov777dbfa2010-07-20 15:03:37 -0700868void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
869 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700870 if (error_event_.get()) {
871 // This shouldn't really happen.
872 LOG(WARNING) << "There's already an existing pending error event.";
873 return;
874 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700875
Darin Petkovabc7bc02011-02-23 14:39:43 -0800876 // For now assume that a generic Omaha response action failure means that
877 // there's no update so don't send an event. Also, double check that the
878 // failure has not occurred while sending an error event -- in which case
879 // don't schedule another. This shouldn't really happen but just in case...
880 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
881 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700882 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
883 return;
884 }
885
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700886 // Classify the code to generate the appropriate result so that
887 // the Borgmon charts show up the results correctly.
888 // Do this before calling GetErrorCodeForAction which could potentially
889 // augment the bit representation of code and thus cause no matches for
890 // the switch cases below.
891 OmahaEvent::Result event_result;
892 switch (code) {
893 case kActionCodeOmahaUpdateIgnoredPerPolicy:
894 case kActionCodeOmahaUpdateDeferredPerPolicy:
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800895 case kActionCodeOmahaUpdateDeferredForBackoff:
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700896 event_result = OmahaEvent::kResultUpdateDeferred;
897 break;
898 default:
899 event_result = OmahaEvent::kResultError;
900 break;
901 }
902
Darin Petkov777dbfa2010-07-20 15:03:37 -0700903 code = GetErrorCodeForAction(action, code);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700904 fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700905
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800906 // Compute the final error code with all the bit flags to be sent to Omaha.
907 code = static_cast<ActionExitCode>(code | GetErrorCodeFlags());
Darin Petkov09f96c32010-07-20 09:24:57 -0700908 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700909 event_result,
Darin Petkov09f96c32010-07-20 09:24:57 -0700910 code));
911}
912
913bool UpdateAttempter::ScheduleErrorEventAction() {
914 if (error_event_.get() == NULL)
915 return false;
916
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800917 LOG(ERROR) << "Update failed.";
918 system_state_->payload_state()->UpdateFailed(error_event_->error_code);
919
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800920 // Send it to Uma.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800921 LOG(INFO) << "Reporting the error event";
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800922 utils::SendErrorCodeToUma(system_state_, error_event_->error_code);
923
924 // Send it to Omaha.
Darin Petkov09f96c32010-07-20 09:24:57 -0700925 shared_ptr<OmahaRequestAction> error_event_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800926 new OmahaRequestAction(system_state_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700927 error_event_.release(), // Pass ownership.
Jay Srinivasan08fce042012-06-07 16:31:01 -0700928 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700929 system_state_,
930 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700931 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700932 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700933 processor_->EnqueueAction(error_event_action.get());
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800934 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT,
935 kUpdateNoticeUnspecified);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700936 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700937 return true;
938}
939
Chris Sosa4f8ee272012-11-30 13:01:54 -0800940void UpdateAttempter::SetCpuShares(utils::CpuShares shares) {
941 if (shares_ == shares) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700942 return;
943 }
Chris Sosa4f8ee272012-11-30 13:01:54 -0800944 if (utils::SetCpuShares(shares)) {
945 shares_ = shares;
946 LOG(INFO) << "CPU shares = " << shares_;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700947 }
948}
949
Chris Sosa4f8ee272012-11-30 13:01:54 -0800950void UpdateAttempter::SetupCpuSharesManagement() {
951 if (manage_shares_source_) {
952 LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed.";
953 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700954 }
Chris Sosa4f8ee272012-11-30 13:01:54 -0800955 const int kCpuSharesTimeout = 2 * 60 * 60; // 2 hours
956 manage_shares_source_ = g_timeout_source_new_seconds(kCpuSharesTimeout);
957 g_source_set_callback(manage_shares_source_,
958 StaticManageCpuSharesCallback,
Darin Petkovc6c135c2010-08-11 13:36:18 -0700959 this,
960 NULL);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800961 g_source_attach(manage_shares_source_, NULL);
962 SetCpuShares(utils::kCpuSharesLow);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700963}
964
Chris Sosa4f8ee272012-11-30 13:01:54 -0800965void UpdateAttempter::CleanupCpuSharesManagement() {
966 if (manage_shares_source_) {
967 g_source_destroy(manage_shares_source_);
968 manage_shares_source_ = NULL;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700969 }
Chris Sosa4f8ee272012-11-30 13:01:54 -0800970 SetCpuShares(utils::kCpuSharesNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700971}
972
Chris Sosa4f8ee272012-11-30 13:01:54 -0800973gboolean UpdateAttempter::StaticManageCpuSharesCallback(gpointer data) {
974 return reinterpret_cast<UpdateAttempter*>(data)->ManageCpuSharesCallback();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700975}
976
Darin Petkove6ef2f82011-03-07 17:31:11 -0800977gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
978 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
979 return FALSE; // Don't call this callback again.
980}
981
Darin Petkov58dd1342011-05-06 12:05:13 -0700982void UpdateAttempter::ScheduleProcessingStart() {
983 LOG(INFO) << "Scheduling an action processor start.";
984 start_action_processor_ = false;
985 g_idle_add(&StaticStartProcessing, this);
986}
987
Chris Sosa4f8ee272012-11-30 13:01:54 -0800988bool UpdateAttempter::ManageCpuSharesCallback() {
989 SetCpuShares(utils::kCpuSharesNormal);
990 manage_shares_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700991 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700992}
993
Darin Petkov36275772010-10-01 11:40:57 -0700994void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
995 int64_t delta_failures;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700996 if (omaha_request_params_->delta_okay() &&
Darin Petkov36275772010-10-01 11:40:57 -0700997 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
998 delta_failures >= kMaxDeltaUpdateFailures) {
999 LOG(WARNING) << "Too many delta update failures, forcing full update.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001000 omaha_request_params_->set_delta_okay(false);
Darin Petkov36275772010-10-01 11:40:57 -07001001 }
1002}
1003
1004void UpdateAttempter::MarkDeltaUpdateFailure() {
Darin Petkov2dd01092010-10-08 15:43:05 -07001005 // Don't try to resume a failed delta update.
1006 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -07001007 int64_t delta_failures;
1008 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
1009 delta_failures < 0) {
1010 delta_failures = 0;
1011 }
1012 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
1013}
1014
Darin Petkov9b230572010-10-08 10:20:09 -07001015void UpdateAttempter::SetupDownload() {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001016 MultiRangeHttpFetcher* fetcher =
1017 dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001018 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -07001019 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001020 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -07001021 int64_t manifest_metadata_size = 0;
1022 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001023 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001024 // If there're remaining unprocessed data blobs, fetch them. Be careful not
1025 // to request data beyond the end of the payload to avoid 416 HTTP response
1026 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -07001027 int64_t next_data_offset = 0;
1028 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001029 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001030 if (resume_offset < response_handler_action_->install_plan().payload_size) {
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001031 fetcher->AddRange(resume_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001032 }
Darin Petkov9b230572010-10-08 10:20:09 -07001033 } else {
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001034 fetcher->AddRange(0);
Darin Petkov9b230572010-10-08 10:20:09 -07001035 }
Darin Petkov9b230572010-10-08 10:20:09 -07001036}
1037
Thieu Le116fda32011-04-19 11:01:54 -07001038void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -07001039 if (!processor_->IsRunning()) {
1040 shared_ptr<OmahaRequestAction> ping_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001041 new OmahaRequestAction(system_state_,
Thieu Led88a8572011-05-26 09:09:19 -07001042 NULL,
Jay Srinivasan08fce042012-06-07 16:31:01 -07001043 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -07001044 system_state_,
1045 is_test_mode_),
Thieu Led88a8572011-05-26 09:09:19 -07001046 true));
1047 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
1048 processor_->set_delegate(NULL);
1049 processor_->EnqueueAction(ping_action.get());
1050 // Call StartProcessing() synchronously here to avoid any race conditions
1051 // caused by multiple outstanding ping Omaha requests. If we call
1052 // StartProcessing() asynchronously, the device can be suspended before we
1053 // get a chance to callback to StartProcessing(). When the device resumes
1054 // (assuming the device sleeps longer than the next update check period),
1055 // StartProcessing() is called back and at the same time, the next update
1056 // check is fired which eventually invokes StartProcessing(). A crash
1057 // can occur because StartProcessing() checks to make sure that the
1058 // processor is idle which it isn't due to the two concurrent ping Omaha
1059 // requests.
1060 processor_->StartProcessing();
1061 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -07001062 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -07001063 }
Thieu Led88a8572011-05-26 09:09:19 -07001064
1065 // Update the status which will schedule the next update check
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001066 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
1067 kUpdateNoticeUnspecified);
Thieu Le116fda32011-04-19 11:01:54 -07001068}
1069
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001070
1071bool UpdateAttempter::DecrementUpdateCheckCount() {
1072 int64 update_check_count_value;
1073
1074 if (!prefs_->Exists(kPrefsUpdateCheckCount)) {
1075 // This file does not exist. This means we haven't started our update
1076 // check count down yet, so nothing more to do. This file will be created
1077 // later when we first satisfy the wall-clock-based-wait period.
1078 LOG(INFO) << "No existing update check count. That's normal.";
1079 return true;
1080 }
1081
1082 if (prefs_->GetInt64(kPrefsUpdateCheckCount, &update_check_count_value)) {
1083 // Only if we're able to read a proper integer value, then go ahead
1084 // and decrement and write back the result in the same file, if needed.
1085 LOG(INFO) << "Update check count = " << update_check_count_value;
1086
1087 if (update_check_count_value == 0) {
1088 // It could be 0, if, for some reason, the file didn't get deleted
1089 // when we set our status to waiting for reboot. so we just leave it
1090 // as is so that we can prevent another update_check wait for this client.
1091 LOG(INFO) << "Not decrementing update check count as it's already 0.";
1092 return true;
1093 }
1094
1095 if (update_check_count_value > 0)
1096 update_check_count_value--;
1097 else
1098 update_check_count_value = 0;
1099
1100 // Write out the new value of update_check_count_value.
1101 if (prefs_->SetInt64(kPrefsUpdateCheckCount, update_check_count_value)) {
1102 // We successfully wrote out te new value, so enable the
1103 // update check based wait.
1104 LOG(INFO) << "New update check count = " << update_check_count_value;
1105 return true;
1106 }
1107 }
1108
1109 LOG(INFO) << "Deleting update check count state due to read/write errors.";
1110
1111 // We cannot read/write to the file, so disable the update check based wait
1112 // so that we don't get stuck in this OS version by any chance (which could
1113 // happen if there's some bug that causes to read/write incorrectly).
1114 // Also attempt to delete the file to do our best effort to cleanup.
1115 prefs_->Delete(kPrefsUpdateCheckCount);
1116 return false;
1117}
1118
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001119} // namespace chromeos_update_engine