blob: 8d7e4551c93c076f2dd3b1b970bcbb49aac832fb [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
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07007#include <string>
Darin Petkov9b230572010-10-08 10:20:09 -07008#include <tr1/memory>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07009#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070010
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -070011#include <base/file_util.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080012#include <base/rand_util.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070013#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070014#include <metrics/metrics_library.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020015#include <policy/libpolicy.h>
16#include <policy/device_policy.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070017
Bruno Rocha7f9aea22011-09-12 14:31:24 -070018#include "update_engine/certificate_checker.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070019#include "update_engine/constants.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070020#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/download_action.h"
22#include "update_engine/filesystem_copier_action.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080023#include "update_engine/gpio_handler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyes819fef22010-12-17 11:33:58 -080025#include "update_engine/multi_range_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070026#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070028#include "update_engine/omaha_response_handler_action.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080029#include "update_engine/payload_state_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070030#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070031#include "update_engine/prefs_interface.h"
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -070032#include "update_engine/subprocess.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070033#include "update_engine/system_state.h"
Darin Petkov1023a602010-08-30 13:47:51 -070034#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070035
Darin Petkovaf183052010-08-23 12:07:13 -070036using base::TimeDelta;
37using base::TimeTicks;
Andrew de los Reyes21816e12011-04-07 14:18:56 -070038using google::protobuf::NewPermanentCallback;
Darin Petkov9b230572010-10-08 10:20:09 -070039using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070040using std::tr1::shared_ptr;
Jay Srinivasan43488792012-06-19 00:25:31 -070041using std::set;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070042using std::string;
43using std::vector;
44
45namespace chromeos_update_engine {
46
Darin Petkov36275772010-10-01 11:40:57 -070047const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
48
Gilad Arnold1ebd8132012-03-05 10:19:29 -080049// Private test server URL w/ custom port number.
Gilad Arnolded747312012-03-15 18:20:41 -070050// TODO(garnold) This is a temporary hack to allow us to test the closed loop
51// automated update testing. To be replaced with an hard-coded local IP address.
52const char* const UpdateAttempter::kTestUpdateUrl(
53 "http://garnold.mtv.corp.google.com:8080/update");
Gilad Arnold28e2f392012-02-09 14:36:46 -080054
Darin Petkovcd1666f2010-09-23 09:53:44 -070055const char* kUpdateCompletedMarker =
56 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070057
Andrew de los Reyes45168102010-11-22 11:13:50 -080058namespace {
59const int kMaxConsecutiveObeyProxyRequests = 20;
60} // namespace {}
61
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070062const char* UpdateStatusToString(UpdateStatus status) {
63 switch (status) {
64 case UPDATE_STATUS_IDLE:
65 return "UPDATE_STATUS_IDLE";
66 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
67 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
68 case UPDATE_STATUS_UPDATE_AVAILABLE:
69 return "UPDATE_STATUS_UPDATE_AVAILABLE";
70 case UPDATE_STATUS_DOWNLOADING:
71 return "UPDATE_STATUS_DOWNLOADING";
72 case UPDATE_STATUS_VERIFYING:
73 return "UPDATE_STATUS_VERIFYING";
74 case UPDATE_STATUS_FINALIZING:
75 return "UPDATE_STATUS_FINALIZING";
76 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
77 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070078 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
79 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Chris Sosad317e402013-06-12 13:47:09 -070080 case UPDATE_STATUS_ATTEMPTING_ROLLBACK:
81 return "UPDATE_STATUS_ATTEMPTING_ROLLBACK";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070082 default:
83 return "unknown status";
84 }
85}
86
David Zeuthena99981f2013-04-29 13:42:47 -070087// Turns a generic kErrorCodeError to a generic error code specific
88// to |action| (e.g., kErrorCodeFilesystemCopierError). If |code| is
89// not kErrorCodeError, or the action is not matched, returns |code|
Darin Petkov777dbfa2010-07-20 15:03:37 -070090// unchanged.
David Zeuthena99981f2013-04-29 13:42:47 -070091ErrorCode GetErrorCodeForAction(AbstractAction* action,
92 ErrorCode code) {
93 if (code != kErrorCodeError)
Darin Petkov777dbfa2010-07-20 15:03:37 -070094 return code;
95
96 const string type = action->Type();
97 if (type == OmahaRequestAction::StaticType())
David Zeuthena99981f2013-04-29 13:42:47 -070098 return kErrorCodeOmahaRequestError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070099 if (type == OmahaResponseHandlerAction::StaticType())
David Zeuthena99981f2013-04-29 13:42:47 -0700100 return kErrorCodeOmahaResponseHandlerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -0700101 if (type == FilesystemCopierAction::StaticType())
David Zeuthena99981f2013-04-29 13:42:47 -0700102 return kErrorCodeFilesystemCopierError;
Darin Petkov777dbfa2010-07-20 15:03:37 -0700103 if (type == PostinstallRunnerAction::StaticType())
David Zeuthena99981f2013-04-29 13:42:47 -0700104 return kErrorCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -0700105
106 return code;
107}
108
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800109UpdateAttempter::UpdateAttempter(SystemState* system_state,
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800110 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700111 : processor_(new ActionProcessor()),
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800112 system_state_(system_state),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700113 dbus_service_(NULL),
Darin Petkov1023a602010-08-30 13:47:51 -0700114 update_check_scheduler_(NULL),
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700115 fake_update_success_(false),
Darin Petkov1023a602010-08-30 13:47:51 -0700116 http_response_code_(0),
Chris Sosa4f8ee272012-11-30 13:01:54 -0800117 shares_(utils::kCpuSharesNormal),
118 manage_shares_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700119 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700120 status_(UPDATE_STATUS_IDLE),
121 download_progress_(0.0),
122 last_checked_time_(0),
123 new_version_("0.0.0.0"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700124 new_payload_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800125 proxy_manual_checks_(0),
126 obeying_proxies_(true),
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700127 chrome_proxy_resolver_(dbus_iface),
Darin Petkov58dd1342011-05-06 12:05:13 -0700128 updated_boot_flags_(false),
129 update_boot_flags_running_(false),
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200130 start_action_processor_(false),
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800131 policy_provider_(NULL),
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700132 is_using_test_url_(false),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700133 is_test_mode_(false),
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800134 is_test_update_attempted_(false) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800135 prefs_ = system_state->prefs();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700136 omaha_request_params_ = system_state->request_params();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700137 if (utils::FileExists(kUpdateCompletedMarker))
138 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
139}
140
141UpdateAttempter::~UpdateAttempter() {
Chris Sosa4f8ee272012-11-30 13:01:54 -0800142 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700143}
144
Gilad Arnold28e2f392012-02-09 14:36:46 -0800145void UpdateAttempter::Update(const string& app_version,
146 const string& omaha_url,
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700147 bool obey_proxies,
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800148 bool interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800149 bool is_test_mode) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800150 chrome_proxy_resolver_.Init();
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700151 fake_update_success_ = false;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700152 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Thieu Le116fda32011-04-19 11:01:54 -0700153 // Although we have applied an update, we still want to ping Omaha
154 // to ensure the number of active statistics is accurate.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700155 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
Thieu Le116fda32011-04-19 11:01:54 -0700156 << "reboot, we'll ping Omaha instead";
157 PingOmaha();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700158 return;
159 }
160 if (status_ != UPDATE_STATUS_IDLE) {
161 // Update in progress. Do nothing
162 return;
163 }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700164
165 if (!CalculateUpdateParams(app_version,
166 omaha_url,
167 obey_proxies,
168 interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800169 is_test_mode)) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700170 return;
171 }
172
173 BuildUpdateActions(interactive);
174
175 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE,
176 kUpdateNoticeUnspecified);
177
178 // Just in case we didn't update boot flags yet, make sure they're updated
179 // before any update processing starts.
180 start_action_processor_ = true;
181 UpdateBootFlags();
182}
183
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700184void UpdateAttempter::RefreshDevicePolicy() {
185 // Lazy initialize the policy provider, or reload the latest policy data.
186 if (!policy_provider_.get())
187 policy_provider_.reset(new policy::PolicyProvider());
188 policy_provider_->Reload();
189
190 const policy::DevicePolicy* device_policy = NULL;
191 if (policy_provider_->device_policy_is_loaded())
192 device_policy = &policy_provider_->GetDevicePolicy();
193
194 if (device_policy)
195 LOG(INFO) << "Device policies/settings present";
196 else
197 LOG(INFO) << "No device policies/settings present.";
198
199 system_state_->set_device_policy(device_policy);
200}
201
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700202bool UpdateAttempter::CalculateUpdateParams(const string& app_version,
203 const string& omaha_url,
204 bool obey_proxies,
205 bool interactive,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800206 bool is_test_mode) {
Darin Petkov1023a602010-08-30 13:47:51 -0700207 http_response_code_ = 0;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200208
Gilad Arnold7c04e762012-05-23 10:54:02 -0700209 // Set the test mode flag for the current update attempt.
210 is_test_mode_ = is_test_mode;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700211 RefreshDevicePolicy();
212 const policy::DevicePolicy* device_policy = system_state_->device_policy();
213 if (device_policy) {
Jay Srinivasandc9a8ca2013-03-19 18:12:38 -0700214 bool update_disabled = false;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700215 if (device_policy->GetUpdateDisabled(&update_disabled))
216 omaha_request_params_->set_update_disabled(update_disabled);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700217
218 string target_version_prefix;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700219 if (device_policy->GetTargetVersionPrefix(&target_version_prefix))
220 omaha_request_params_->set_target_version_prefix(target_version_prefix);
Jay Srinivasan43488792012-06-19 00:25:31 -0700221
222 set<string> allowed_types;
223 string allowed_types_str;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700224 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700225 set<string>::const_iterator iter;
226 for (iter = allowed_types.begin(); iter != allowed_types.end(); ++iter)
227 allowed_types_str += *iter + " ";
228 }
229
230 LOG(INFO) << "Networks over which updates are allowed per policy : "
231 << (allowed_types_str.empty() ? "all" : allowed_types_str);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700232 }
233
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800234 CalculateScatteringParams(interactive);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200235
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800236 // Determine whether an alternative test address should be used.
Gilad Arnold28e2f392012-02-09 14:36:46 -0800237 string omaha_url_to_use = omaha_url;
Gilad Arnold7c04e762012-05-23 10:54:02 -0700238 if ((is_using_test_url_ = (omaha_url_to_use.empty() && is_test_mode_))) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800239 omaha_url_to_use = kTestUpdateUrl;
240 LOG(INFO) << "using alternative server address: " << omaha_url_to_use;
Gilad Arnold28e2f392012-02-09 14:36:46 -0800241 }
242
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700243 if (!omaha_request_params_->Init(app_version,
244 omaha_url_to_use,
245 interactive)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700246 LOG(ERROR) << "Unable to initialize Omaha request params.";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700247 return false;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700248 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800249
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700250 // Set the target channel iff ReleaseChannelDelegated policy is set to
251 // false and a non-empty ReleaseChannel policy is present. If delegated
252 // is true, we'll ignore ReleaseChannel policy value.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700253 if (device_policy) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700254 bool delegated = false;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700255 if (device_policy->GetReleaseChannelDelegated(&delegated) && delegated) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700256 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;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700262 if (device_policy->GetReleaseChannel(&target_channel) &&
263 !target_channel.empty()) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700264 // Pass in false for powerwash_allowed until we add it to the policy
265 // protobuf.
266 LOG(INFO) << "Setting target channel from ReleaseChannel policy value";
267 omaha_request_params_->SetTargetChannel(target_channel, false);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700268
269 // Since this is the beginning of a new attempt, update the download
270 // channel. The download channel won't be updated until the next
271 // attempt, even if target channel changes meanwhile, so that how we'll
272 // know if we should cancel the current download attempt if there's
273 // such a change in target channel.
274 omaha_request_params_->UpdateDownloadChannel();
275 } else {
276 LOG(INFO) << "No ReleaseChannel specified in policy";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700277 }
278 }
279 }
280
Jay Srinivasan0a708742012-03-20 11:26:12 -0700281 LOG(INFO) << "update_disabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700282 << utils::ToString(omaha_request_params_->update_disabled())
Jay Srinivasan0a708742012-03-20 11:26:12 -0700283 << ", target_version_prefix = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700284 << omaha_request_params_->target_version_prefix()
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700285 << ", scatter_factor_in_seconds = "
286 << utils::FormatSecs(scatter_factor_.InSeconds());
287
288 LOG(INFO) << "Wall Clock Based Wait Enabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700289 << omaha_request_params_->wall_clock_based_wait_enabled()
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700290 << ", Update Check Count Wait Enabled = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700291 << omaha_request_params_->update_check_count_wait_enabled()
Jay Srinivasan21be0752012-07-25 15:44:56 -0700292 << ", Waiting Period = " << utils::FormatSecs(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700293 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700294
Andrew de los Reyes45168102010-11-22 11:13:50 -0800295 obeying_proxies_ = true;
296 if (obey_proxies || proxy_manual_checks_ == 0) {
297 LOG(INFO) << "forced to obey proxies";
298 // If forced to obey proxies, every 20th request will not use proxies
299 proxy_manual_checks_++;
300 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
301 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
302 proxy_manual_checks_ = 0;
303 obeying_proxies_ = false;
304 }
305 } else if (base::RandInt(0, 4) == 0) {
306 obeying_proxies_ = false;
307 }
308 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
309 "check we are ignoring the proxy settings and using "
310 "direct connections.";
311
Darin Petkov36275772010-10-01 11:40:57 -0700312 DisableDeltaUpdateIfNeeded();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700313 return true;
314}
315
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800316void UpdateAttempter::CalculateScatteringParams(bool interactive) {
Jay Srinivasan21be0752012-07-25 15:44:56 -0700317 // Take a copy of the old scatter value before we update it, as
318 // we need to update the waiting period if this value changes.
319 TimeDelta old_scatter_factor = scatter_factor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800320 const policy::DevicePolicy* device_policy = system_state_->device_policy();
Jay Srinivasan21be0752012-07-25 15:44:56 -0700321 if (device_policy) {
322 int64 new_scatter_factor_in_secs = 0;
323 device_policy->GetScatterFactorInSeconds(&new_scatter_factor_in_secs);
324 if (new_scatter_factor_in_secs < 0) // sanitize input, just in case.
325 new_scatter_factor_in_secs = 0;
326 scatter_factor_ = TimeDelta::FromSeconds(new_scatter_factor_in_secs);
327 }
328
329 bool is_scatter_enabled = false;
330 if (scatter_factor_.InSeconds() == 0) {
331 LOG(INFO) << "Scattering disabled since scatter factor is set to 0";
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800332 } else if (interactive) {
333 LOG(INFO) << "Scattering disabled as this is an interactive update check";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700334 } else if (!system_state_->IsOOBEComplete()) {
335 LOG(INFO) << "Scattering disabled since OOBE is not complete yet";
336 } else {
337 is_scatter_enabled = true;
338 LOG(INFO) << "Scattering is enabled";
339 }
340
341 if (is_scatter_enabled) {
342 // This means the scattering policy is turned on.
343 // Now check if we need to update the waiting period. The two cases
344 // in which we'd need to update the waiting period are:
345 // 1. First time in process or a scheduled check after a user-initiated one.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700346 // (omaha_request_params_->waiting_period will be zero in this case).
Jay Srinivasan21be0752012-07-25 15:44:56 -0700347 // 2. Admin has changed the scattering policy value.
348 // (new scattering value will be different from old one in this case).
349 int64 wait_period_in_secs = 0;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700350 if (omaha_request_params_->waiting_period().InSeconds() == 0) {
Jay Srinivasan21be0752012-07-25 15:44:56 -0700351 // First case. Check if we have a suitable value to set for
352 // the waiting period.
353 if (prefs_->GetInt64(kPrefsWallClockWaitPeriod, &wait_period_in_secs) &&
354 wait_period_in_secs > 0 &&
355 wait_period_in_secs <= scatter_factor_.InSeconds()) {
356 // This means:
357 // 1. There's a persisted value for the waiting period available.
358 // 2. And that persisted value is still valid.
359 // So, in this case, we should reuse the persisted value instead of
360 // generating a new random value to improve the chances of a good
361 // distribution for scattering.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700362 omaha_request_params_->set_waiting_period(
363 TimeDelta::FromSeconds(wait_period_in_secs));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700364 LOG(INFO) << "Using persisted wall-clock waiting period: " <<
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700365 utils::FormatSecs(
366 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700367 }
368 else {
369 // This means there's no persisted value for the waiting period
370 // available or its value is invalid given the new scatter_factor value.
371 // So, we should go ahead and regenerate a new value for the
372 // waiting period.
373 LOG(INFO) << "Persisted value not present or not valid ("
374 << utils::FormatSecs(wait_period_in_secs)
375 << ") for wall-clock waiting period.";
376 GenerateNewWaitingPeriod();
377 }
378 } else if (scatter_factor_ != old_scatter_factor) {
379 // This means there's already a waiting period value, but we detected
380 // a change in the scattering policy value. So, we should regenerate the
381 // waiting period to make sure it's within the bounds of the new scatter
382 // factor value.
383 GenerateNewWaitingPeriod();
384 } else {
385 // Neither the first time scattering is enabled nor the scattering value
386 // changed. Nothing to do.
387 LOG(INFO) << "Keeping current wall-clock waiting period: " <<
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700388 utils::FormatSecs(
389 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700390 }
391
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700392 // The invariant at this point is that omaha_request_params_->waiting_period
Jay Srinivasan21be0752012-07-25 15:44:56 -0700393 // is non-zero no matter which path we took above.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700394 LOG_IF(ERROR, omaha_request_params_->waiting_period().InSeconds() == 0)
Jay Srinivasan21be0752012-07-25 15:44:56 -0700395 << "Waiting Period should NOT be zero at this point!!!";
396
397 // Since scattering is enabled, wall clock based wait will always be
398 // enabled.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700399 omaha_request_params_->set_wall_clock_based_wait_enabled(true);
Jay Srinivasan21be0752012-07-25 15:44:56 -0700400
401 // If we don't have any issues in accessing the file system to update
402 // the update check count value, we'll turn that on as well.
403 bool decrement_succeeded = DecrementUpdateCheckCount();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700404 omaha_request_params_->set_update_check_count_wait_enabled(
405 decrement_succeeded);
Jay Srinivasan21be0752012-07-25 15:44:56 -0700406 } else {
407 // This means the scattering feature is turned off or disabled for
408 // this particular update check. Make sure to disable
409 // all the knobs and artifacts so that we don't invoke any scattering
410 // related code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700411 omaha_request_params_->set_wall_clock_based_wait_enabled(false);
412 omaha_request_params_->set_update_check_count_wait_enabled(false);
413 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(0));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700414 prefs_->Delete(kPrefsWallClockWaitPeriod);
415 prefs_->Delete(kPrefsUpdateCheckCount);
416 // Don't delete the UpdateFirstSeenAt file as we don't want manual checks
417 // that result in no-updates (e.g. due to server side throttling) to
418 // cause update starvation by having the client generate a new
419 // UpdateFirstSeenAt for each scheduled check that follows a manual check.
420 }
421}
422
423void UpdateAttempter::GenerateNewWaitingPeriod() {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700424 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(
425 base::RandInt(1, scatter_factor_.InSeconds())));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700426
427 LOG(INFO) << "Generated new wall-clock waiting period: " << utils::FormatSecs(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700428 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700429
430 // Do a best-effort to persist this in all cases. Even if the persistence
431 // fails, we'll still be able to scatter based on our in-memory value.
432 // The persistence only helps in ensuring a good overall distribution
433 // across multiple devices if they tend to reboot too often.
434 prefs_->SetInt64(kPrefsWallClockWaitPeriod,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700435 omaha_request_params_->waiting_period().InSeconds());
Jay Srinivasan21be0752012-07-25 15:44:56 -0700436}
437
Chris Sosad317e402013-06-12 13:47:09 -0700438void UpdateAttempter::BuildPostInstallActions(
439 InstallPlanAction* previous_action) {
440 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
441 new PostinstallRunnerAction());
442 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
443 BondActions(previous_action,
444 postinstall_runner_action.get());
445}
446
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700447void UpdateAttempter::BuildUpdateActions(bool interactive) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700448 CHECK(!processor_->IsRunning());
449 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700450
451 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800452 LibcurlHttpFetcher* update_check_fetcher =
Gilad Arnold7c04e762012-05-23 10:54:02 -0700453 new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_);
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700454 // Try harder to connect to the network, esp when not interactive.
455 // See comment in libcurl_http_fetcher.cc.
456 update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700457 update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700458 shared_ptr<OmahaRequestAction> update_check_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800459 new OmahaRequestAction(system_state_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700460 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700461 update_check_fetcher, // passes ownership
462 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700463 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800464 new OmahaResponseHandlerAction(system_state_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700465 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700466 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700467 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700468 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700469 shared_ptr<OmahaRequestAction> download_started_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::kTypeUpdateDownloadStarted),
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));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700477 LibcurlHttpFetcher* download_fetcher =
Gilad Arnold7c04e762012-05-23 10:54:02 -0700478 new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700479 download_fetcher->set_check_certificate(CertificateChecker::kDownload);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700480 shared_ptr<DownloadAction> download_action(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700481 new DownloadAction(prefs_,
Jay Srinivasanf0572052012-10-23 18:12:56 -0700482 system_state_,
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800483 new MultiRangeHttpFetcher(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700484 download_fetcher))); // passes ownership
Darin Petkov8c2980e2010-07-16 15:16:49 -0700485 shared_ptr<OmahaRequestAction> download_finished_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800486 new OmahaRequestAction(system_state_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700487 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700488 OmahaEvent::kTypeUpdateDownloadFinished),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700489 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700490 system_state_,
491 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700492 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800493 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700494 new FilesystemCopierAction(false, true));
Darin Petkov3aefa862010-12-07 14:45:00 -0800495 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700496 new FilesystemCopierAction(true, true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700497 shared_ptr<OmahaRequestAction> update_complete_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800498 new OmahaRequestAction(system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700499 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700500 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -0700501 system_state_,
502 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -0700503 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700504
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700505 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700506 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700507 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700508
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700509 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
510 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
511 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700512 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700513 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700514 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700515 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700516 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800517 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
Chris Sosad317e402013-06-12 13:47:09 -0700518 actions_.push_back(shared_ptr<AbstractAction>(
519 kernel_filesystem_verifier_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700520
521 // Bond them together. We have to use the leaf-types when calling
522 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700523 BondActions(update_check_action.get(),
524 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700525 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700526 filesystem_copier_action.get());
527 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700528 kernel_filesystem_copier_action.get());
529 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700530 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700531 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800532 filesystem_verifier_action.get());
533 BondActions(filesystem_verifier_action.get(),
534 kernel_filesystem_verifier_action.get());
Chris Sosad317e402013-06-12 13:47:09 -0700535
536 BuildPostInstallActions(kernel_filesystem_verifier_action.get());
537
538 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
539
540 // Enqueue the actions
541 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
542 it != actions_.end(); ++it) {
543 processor_->EnqueueAction(it->get());
544 }
545}
546
Chris Sosa76a29ae2013-07-11 17:59:24 -0700547bool UpdateAttempter::Rollback(bool powerwash, string *install_path) {
Chris Sosad317e402013-06-12 13:47:09 -0700548 CHECK(!processor_->IsRunning());
549 processor_->set_delegate(this);
550
Chris Sosaaa18e162013-06-20 13:20:30 -0700551 // TODO(sosa): crbug.com/252539 -- refactor channel into system_state and
552 // check for != stable-channel here.
553 RefreshDevicePolicy();
554
Chris Sosa28e479c2013-07-12 11:39:53 -0700555 // Initialize the default request params.
556 if (!omaha_request_params_->Init("", "", true)) {
557 LOG(ERROR) << "Unable to initialize Omaha request params.";
558 return false;
559 }
560
561 if (omaha_request_params_->current_channel() == "stable-channel") {
562 LOG(ERROR) << "Rollback is not supported while on the stable-channel.";
563 return false;
564 }
565
Chris Sosad317e402013-06-12 13:47:09 -0700566 LOG(INFO) << "Setting rollback options.";
567 InstallPlan install_plan;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700568 if (install_path == NULL) {
569 TEST_AND_RETURN_FALSE(utils::GetInstallDev(utils::BootDevice(),
570 &install_plan.install_path));
571 }
572 else {
573 install_plan.install_path = *install_path;
574 }
575
Chris Sosad317e402013-06-12 13:47:09 -0700576 install_plan.kernel_install_path = utils::BootKernelDevice(
577 install_plan.install_path);
578 install_plan.powerwash_required = powerwash;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700579 if (powerwash) {
580 // Enterprise-enrolled devices have an empty owner in their device policy.
581 string owner;
582 const policy::DevicePolicy* device_policy = system_state_->device_policy();
583 if (!device_policy->GetOwner(&owner) || owner.empty()) {
584 LOG(ERROR) << "Enterprise device detected. "
585 << "Cannot perform a powerwash for enterprise devices.";
586 return false;
587 }
588 }
Chris Sosad317e402013-06-12 13:47:09 -0700589
590 LOG(INFO) << "Using this install plan:";
591 install_plan.Dump();
592
593 shared_ptr<InstallPlanAction> install_plan_action(
594 new InstallPlanAction(install_plan));
595 actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
596
597 BuildPostInstallActions(install_plan_action.get());
598
599 // Enqueue the actions
600 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
601 it != actions_.end(); ++it) {
602 processor_->EnqueueAction(it->get());
603 }
Chris Sosaaa18e162013-06-20 13:20:30 -0700604
605 // Update the payload state for Rollback.
606 system_state_->payload_state()->Rollback();
607
Chris Sosad317e402013-06-12 13:47:09 -0700608 SetStatusAndNotify(UPDATE_STATUS_ATTEMPTING_ROLLBACK,
609 kUpdateNoticeUnspecified);
610
611 // Just in case we didn't update boot flags yet, make sure they're updated
612 // before any update processing starts. This also schedules the start of the
613 // actions we just posted.
614 start_action_processor_ = true;
615 UpdateBootFlags();
Chris Sosaaa18e162013-06-20 13:20:30 -0700616 return true;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700617}
618
Gilad Arnold28e2f392012-02-09 14:36:46 -0800619void UpdateAttempter::CheckForUpdate(const string& app_version,
Jay Srinivasane73acab2012-07-10 14:34:03 -0700620 const string& omaha_url,
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800621 bool interactive) {
Jay Srinivasan08fce042012-06-07 16:31:01 -0700622 LOG(INFO) << "New update check requested";
623
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700624 if (status_ != UPDATE_STATUS_IDLE) {
Jay Srinivasan08fce042012-06-07 16:31:01 -0700625 LOG(INFO) << "Skipping update check because current status is "
626 << UpdateStatusToString(status_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700627 return;
628 }
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800629
630 // Read GPIO signals and determine whether this is an automated test scenario.
631 // For safety, we only allow a test update to be performed once; subsequent
632 // update requests will be carried out normally.
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800633 bool is_test_mode = (!is_test_update_attempted_ &&
634 system_state_->gpio_handler()->IsTestModeSignaled());
Gilad Arnold7c04e762012-05-23 10:54:02 -0700635 if (is_test_mode) {
636 LOG(WARNING) << "this is a test mode update attempt!";
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700637 is_test_update_attempted_ = true;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800638 }
639
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800640 // Pass through the interactive flag, in case we want to simulate a scheduled
641 // test.
642 Update(app_version, omaha_url, true, interactive, is_test_mode);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700643}
644
Darin Petkov296889c2010-07-23 16:20:54 -0700645bool UpdateAttempter::RebootIfNeeded() {
646 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
647 LOG(INFO) << "Reboot requested, but status is "
648 << UpdateStatusToString(status_) << ", so not rebooting.";
649 return false;
650 }
651 TEST_AND_RETURN_FALSE(utils::Reboot());
652 return true;
653}
654
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700655// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700656void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700657 ErrorCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700658 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700659 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700660
Chris Sosa4f8ee272012-11-30 13:01:54 -0800661 // Reset cpu shares back to normal.
662 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700663
Darin Petkov09f96c32010-07-20 09:24:57 -0700664 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
665 LOG(INFO) << "Error event sent.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800666
667 // Inform scheduler of new status; also specifically inform about a failed
668 // update attempt with a test address.
669 SetStatusAndNotify(UPDATE_STATUS_IDLE,
670 (is_using_test_url_ ? kUpdateNoticeTestAddrFailed :
671 kUpdateNoticeUnspecified));
672
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700673 if (!fake_update_success_) {
674 return;
675 }
676 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
677 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700678 }
679
David Zeuthena99981f2013-04-29 13:42:47 -0700680 if (code == kErrorCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700681 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700682 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700683 prefs_->SetString(kPrefsPreviousVersion,
684 omaha_request_params_->app_version());
Darin Petkov9b230572010-10-08 10:20:09 -0700685 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700686
David Zeuthen9a017f22013-04-11 16:10:26 -0700687 system_state_->payload_state()->UpdateSucceeded();
688
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700689 // Since we're done with scattering fully at this point, this is the
690 // safest point delete the state files, as we're sure that the status is
691 // set to reboot (which means no more updates will be applied until reboot)
692 // This deletion is required for correctness as we want the next update
693 // check to re-create a new random number for the update check count.
694 // Similarly, we also delete the wall-clock-wait period that was persisted
695 // so that we start with a new random value for the next update check
696 // after reboot so that the same device is not favored or punished in any
697 // way.
698 prefs_->Delete(kPrefsUpdateCheckCount);
699 prefs_->Delete(kPrefsWallClockWaitPeriod);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700700 prefs_->Delete(kPrefsUpdateFirstSeenAt);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700701
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800702 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
703 kUpdateNoticeUnspecified);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700704 LOG(INFO) << "Update successfully applied, waiting to reboot.";
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700705
Jay Srinivasanf0572052012-10-23 18:12:56 -0700706 // Also report the success code so that the percentiles can be
707 // interpreted properly for the remaining error codes in UMA.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800708 utils::SendErrorCodeToUma(system_state_, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700709 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700710 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700711
Darin Petkov1023a602010-08-30 13:47:51 -0700712 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700713 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700714 }
715 LOG(INFO) << "No update.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800716 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700717}
718
719void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Chris Sosa4f8ee272012-11-30 13:01:54 -0800720 // Reset cpu shares back to normal.
721 CleanupCpuSharesManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700722 download_progress_ = 0.0;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800723 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700724 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700725 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700726}
727
728// Called whenever an action has finished processing, either successfully
729// or otherwise.
730void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
731 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700732 ErrorCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700733 // Reset download progress regardless of whether or not the download
734 // action succeeded. Also, get the response code from HTTP request
735 // actions (update download as well as the initial update check
736 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700737 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700738 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700739 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700740 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
741 http_response_code_ = download_action->GetHTTPResponseCode();
742 } else if (type == OmahaRequestAction::StaticType()) {
743 OmahaRequestAction* omaha_request_action =
744 dynamic_cast<OmahaRequestAction*>(action);
745 // If the request is not an event, then it's the update-check.
746 if (!omaha_request_action->IsEvent()) {
747 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700748 // Forward the server-dictated poll interval to the update check
749 // scheduler, if any.
750 if (update_check_scheduler_) {
751 update_check_scheduler_->set_poll_interval(
752 omaha_request_action->GetOutputObject().poll_interval);
753 }
Darin Petkov1023a602010-08-30 13:47:51 -0700754 }
755 }
David Zeuthena99981f2013-04-29 13:42:47 -0700756 if (code != kErrorCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700757 // If the current state is at or past the download phase, count the failure
758 // in case a switch to full update becomes necessary. Ignore network
759 // transfer timeouts and failures.
Darin Petkov36275772010-10-01 11:40:57 -0700760 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
David Zeuthena99981f2013-04-29 13:42:47 -0700761 code != kErrorCodeDownloadTransferError) {
Darin Petkov36275772010-10-01 11:40:57 -0700762 MarkDeltaUpdateFailure();
763 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700764 // On failure, schedule an error event to be sent to Omaha.
765 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700766 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700767 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700768 // Find out which action completed.
769 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700770 // Note that the status will be updated to DOWNLOADING when some bytes get
771 // actually downloaded from the server and the BytesReceived callback is
772 // invoked. This avoids notifying the user that a download has started in
773 // cases when the server and the client are unable to initiate the download.
774 CHECK(action == response_handler_action_.get());
775 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700776 last_checked_time_ = time(NULL);
777 // TODO(adlr): put version in InstallPlan
778 new_version_ = "0.0.0.0";
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700779 new_payload_size_ = plan.payload_size;
Darin Petkov9b230572010-10-08 10:20:09 -0700780 SetupDownload();
Chris Sosa4f8ee272012-11-30 13:01:54 -0800781 SetupCpuSharesManagement();
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800782 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE,
783 kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700784 } else if (type == DownloadAction::StaticType()) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800785 SetStatusAndNotify(UPDATE_STATUS_FINALIZING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700786 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700787}
788
789// Stop updating. An attempt will be made to record status to the disk
790// so that updates can be resumed later.
791void UpdateAttempter::Terminate() {
792 // TODO(adlr): implement this method.
793 NOTIMPLEMENTED();
794}
795
796// Try to resume from a previously Terminate()d update.
797void UpdateAttempter::ResumeUpdating() {
798 // TODO(adlr): implement this method.
799 NOTIMPLEMENTED();
800}
801
Darin Petkov9d911fa2010-08-19 09:36:08 -0700802void UpdateAttempter::SetDownloadStatus(bool active) {
803 download_active_ = active;
804 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
805}
806
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700807void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700808 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700809 LOG(ERROR) << "BytesReceived called while not downloading.";
810 return;
811 }
Darin Petkovaf183052010-08-23 12:07:13 -0700812 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700813 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700814 // Self throttle based on progress. Also send notifications if
815 // progress is too slow.
816 const double kDeltaPercent = 0.01; // 1%
817 if (status_ != UPDATE_STATUS_DOWNLOADING ||
818 bytes_received == total ||
819 progress - download_progress_ >= kDeltaPercent ||
820 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
821 download_progress_ = progress;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800822 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700823 }
824}
825
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700826bool UpdateAttempter::ResetStatus() {
827 LOG(INFO) << "Attempting to reset state from "
828 << UpdateStatusToString(status_) << " to UPDATE_STATUS_IDLE";
829
830 switch (status_) {
831 case UPDATE_STATUS_IDLE:
832 // no-op.
833 return true;
834
835 case UPDATE_STATUS_UPDATED_NEED_REBOOT: {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700836 bool ret_value = true;
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700837 status_ = UPDATE_STATUS_IDLE;
838 LOG(INFO) << "Reset Successful";
839
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700840 // Remove the reboot marker so that if the machine is rebooted
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700841 // after resetting to idle state, it doesn't go back to
842 // UPDATE_STATUS_UPDATED_NEED_REBOOT state.
843 const FilePath kUpdateCompletedMarkerPath(kUpdateCompletedMarker);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700844 if (!file_util::Delete(kUpdateCompletedMarkerPath, false))
845 ret_value = false;
846
847 return ret_value;
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700848 }
849
850 default:
851 LOG(ERROR) << "Reset not allowed in this state.";
852 return false;
853 }
854}
855
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700856bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
857 double* progress,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800858 string* current_operation,
859 string* new_version,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700860 int64_t* new_payload_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700861 *last_checked_time = last_checked_time_;
862 *progress = download_progress_;
863 *current_operation = UpdateStatusToString(status_);
864 *new_version = new_version_;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700865 *new_payload_size = new_payload_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700866 return true;
867}
868
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700869void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700870 if (update_boot_flags_running_) {
871 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700872 return;
873 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700874 if (updated_boot_flags_) {
875 LOG(INFO) << "Already updated boot flags. Skipping.";
876 if (start_action_processor_) {
877 ScheduleProcessingStart();
878 }
879 return;
880 }
881 // This is purely best effort. Failures should be logged by Subprocess. Run
882 // the script asynchronously to avoid blocking the event loop regardless of
883 // the script runtime.
884 update_boot_flags_running_ = true;
885 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700886 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700887 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
888 CompleteUpdateBootFlags(1);
889 }
890}
891
892void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
893 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700894 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700895 if (start_action_processor_) {
896 ScheduleProcessingStart();
897 }
898}
899
900void UpdateAttempter::StaticCompleteUpdateBootFlags(
901 int return_code,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800902 const string& output,
Darin Petkov58dd1342011-05-06 12:05:13 -0700903 void* p) {
904 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700905}
906
Darin Petkov61635a92011-05-18 16:20:36 -0700907void UpdateAttempter::BroadcastStatus() {
908 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700909 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700910 }
Darin Petkovaf183052010-08-23 12:07:13 -0700911 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700912 update_engine_service_emit_status_update(
913 dbus_service_,
914 last_checked_time_,
915 download_progress_,
916 UpdateStatusToString(status_),
917 new_version_.c_str(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700918 new_payload_size_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700919}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700920
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800921uint32_t UpdateAttempter::GetErrorCodeFlags() {
922 uint32_t flags = 0;
923
924 if (!utils::IsNormalBootMode())
David Zeuthena99981f2013-04-29 13:42:47 -0700925 flags |= kErrorCodeDevModeFlag;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800926
927 if (response_handler_action_.get() &&
928 response_handler_action_->install_plan().is_resume)
David Zeuthena99981f2013-04-29 13:42:47 -0700929 flags |= kErrorCodeResumedFlag;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800930
931 if (!utils::IsOfficialBuild())
David Zeuthena99981f2013-04-29 13:42:47 -0700932 flags |= kErrorCodeTestImageFlag;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800933
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700934 if (omaha_request_params_->update_url() != kProductionOmahaUrl)
David Zeuthena99981f2013-04-29 13:42:47 -0700935 flags |= kErrorCodeTestOmahaUrlFlag;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800936
937 return flags;
938}
939
David Zeuthena99981f2013-04-29 13:42:47 -0700940bool UpdateAttempter::ShouldCancel(ErrorCode* cancel_reason) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700941 // Check if the channel we're attempting to update to is the same as the
942 // target channel currently chosen by the user.
943 OmahaRequestParams* params = system_state_->request_params();
944 if (params->download_channel() != params->target_channel()) {
945 LOG(ERROR) << "Aborting download as target channel: "
946 << params->target_channel()
947 << " is different from the download channel: "
948 << params->download_channel();
David Zeuthena99981f2013-04-29 13:42:47 -0700949 *cancel_reason = kErrorCodeUpdateCanceledByChannelChange;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700950 return true;
951 }
952
953 return false;
954}
955
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800956void UpdateAttempter::SetStatusAndNotify(UpdateStatus status,
957 UpdateNotice notice) {
Darin Petkov61635a92011-05-18 16:20:36 -0700958 status_ = status;
959 if (update_check_scheduler_) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800960 update_check_scheduler_->SetUpdateStatus(status_, notice);
Darin Petkov61635a92011-05-18 16:20:36 -0700961 }
962 BroadcastStatus();
963}
964
Darin Petkov777dbfa2010-07-20 15:03:37 -0700965void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700966 ErrorCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700967 if (error_event_.get()) {
968 // This shouldn't really happen.
969 LOG(WARNING) << "There's already an existing pending error event.";
970 return;
971 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700972
Darin Petkovabc7bc02011-02-23 14:39:43 -0800973 // For now assume that a generic Omaha response action failure means that
974 // there's no update so don't send an event. Also, double check that the
975 // failure has not occurred while sending an error event -- in which case
976 // don't schedule another. This shouldn't really happen but just in case...
977 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
David Zeuthena99981f2013-04-29 13:42:47 -0700978 code == kErrorCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700979 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
980 return;
981 }
982
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700983 // Classify the code to generate the appropriate result so that
984 // the Borgmon charts show up the results correctly.
985 // Do this before calling GetErrorCodeForAction which could potentially
986 // augment the bit representation of code and thus cause no matches for
987 // the switch cases below.
988 OmahaEvent::Result event_result;
989 switch (code) {
David Zeuthena99981f2013-04-29 13:42:47 -0700990 case kErrorCodeOmahaUpdateIgnoredPerPolicy:
991 case kErrorCodeOmahaUpdateDeferredPerPolicy:
992 case kErrorCodeOmahaUpdateDeferredForBackoff:
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700993 event_result = OmahaEvent::kResultUpdateDeferred;
994 break;
995 default:
996 event_result = OmahaEvent::kResultError;
997 break;
998 }
999
Darin Petkov777dbfa2010-07-20 15:03:37 -07001000 code = GetErrorCodeForAction(action, code);
David Zeuthena99981f2013-04-29 13:42:47 -07001001 fake_update_success_ = code == kErrorCodePostinstallBootedFromFirmwareB;
Darin Petkov18c7bce2011-06-16 14:07:00 -07001002
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001003 // Compute the final error code with all the bit flags to be sent to Omaha.
David Zeuthena99981f2013-04-29 13:42:47 -07001004 code = static_cast<ErrorCode>(code | GetErrorCodeFlags());
Darin Petkov09f96c32010-07-20 09:24:57 -07001005 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -07001006 event_result,
Darin Petkov09f96c32010-07-20 09:24:57 -07001007 code));
1008}
1009
1010bool UpdateAttempter::ScheduleErrorEventAction() {
1011 if (error_event_.get() == NULL)
1012 return false;
1013
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001014 LOG(ERROR) << "Update failed.";
1015 system_state_->payload_state()->UpdateFailed(error_event_->error_code);
1016
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001017 // Send it to Uma.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001018 LOG(INFO) << "Reporting the error event";
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001019 utils::SendErrorCodeToUma(system_state_, error_event_->error_code);
1020
1021 // Send it to Omaha.
Darin Petkov09f96c32010-07-20 09:24:57 -07001022 shared_ptr<OmahaRequestAction> error_event_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001023 new OmahaRequestAction(system_state_,
Darin Petkov09f96c32010-07-20 09:24:57 -07001024 error_event_.release(), // Pass ownership.
Jay Srinivasan08fce042012-06-07 16:31:01 -07001025 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -07001026 system_state_,
1027 is_test_mode_),
Thieu Le116fda32011-04-19 11:01:54 -07001028 false));
Darin Petkov09f96c32010-07-20 09:24:57 -07001029 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001030 processor_->EnqueueAction(error_event_action.get());
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001031 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT,
1032 kUpdateNoticeUnspecified);
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001033 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -07001034 return true;
1035}
1036
Chris Sosa4f8ee272012-11-30 13:01:54 -08001037void UpdateAttempter::SetCpuShares(utils::CpuShares shares) {
1038 if (shares_ == shares) {
Darin Petkovc6c135c2010-08-11 13:36:18 -07001039 return;
1040 }
Chris Sosa4f8ee272012-11-30 13:01:54 -08001041 if (utils::SetCpuShares(shares)) {
1042 shares_ = shares;
1043 LOG(INFO) << "CPU shares = " << shares_;
Darin Petkovc6c135c2010-08-11 13:36:18 -07001044 }
1045}
1046
Chris Sosa4f8ee272012-11-30 13:01:54 -08001047void UpdateAttempter::SetupCpuSharesManagement() {
1048 if (manage_shares_source_) {
1049 LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed.";
1050 CleanupCpuSharesManagement();
Darin Petkovc6c135c2010-08-11 13:36:18 -07001051 }
Chris Sosa4f8ee272012-11-30 13:01:54 -08001052 const int kCpuSharesTimeout = 2 * 60 * 60; // 2 hours
1053 manage_shares_source_ = g_timeout_source_new_seconds(kCpuSharesTimeout);
1054 g_source_set_callback(manage_shares_source_,
1055 StaticManageCpuSharesCallback,
Darin Petkovc6c135c2010-08-11 13:36:18 -07001056 this,
1057 NULL);
Chris Sosa4f8ee272012-11-30 13:01:54 -08001058 g_source_attach(manage_shares_source_, NULL);
1059 SetCpuShares(utils::kCpuSharesLow);
Darin Petkovc6c135c2010-08-11 13:36:18 -07001060}
1061
Chris Sosa4f8ee272012-11-30 13:01:54 -08001062void UpdateAttempter::CleanupCpuSharesManagement() {
1063 if (manage_shares_source_) {
1064 g_source_destroy(manage_shares_source_);
1065 manage_shares_source_ = NULL;
Darin Petkovc6c135c2010-08-11 13:36:18 -07001066 }
Chris Sosa4f8ee272012-11-30 13:01:54 -08001067 SetCpuShares(utils::kCpuSharesNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -07001068}
1069
Chris Sosa4f8ee272012-11-30 13:01:54 -08001070gboolean UpdateAttempter::StaticManageCpuSharesCallback(gpointer data) {
1071 return reinterpret_cast<UpdateAttempter*>(data)->ManageCpuSharesCallback();
Darin Petkovc6c135c2010-08-11 13:36:18 -07001072}
1073
Darin Petkove6ef2f82011-03-07 17:31:11 -08001074gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
1075 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
1076 return FALSE; // Don't call this callback again.
1077}
1078
Darin Petkov58dd1342011-05-06 12:05:13 -07001079void UpdateAttempter::ScheduleProcessingStart() {
1080 LOG(INFO) << "Scheduling an action processor start.";
1081 start_action_processor_ = false;
1082 g_idle_add(&StaticStartProcessing, this);
1083}
1084
Chris Sosa4f8ee272012-11-30 13:01:54 -08001085bool UpdateAttempter::ManageCpuSharesCallback() {
1086 SetCpuShares(utils::kCpuSharesNormal);
1087 manage_shares_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -07001088 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -07001089}
1090
Darin Petkov36275772010-10-01 11:40:57 -07001091void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
1092 int64_t delta_failures;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001093 if (omaha_request_params_->delta_okay() &&
Darin Petkov36275772010-10-01 11:40:57 -07001094 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
1095 delta_failures >= kMaxDeltaUpdateFailures) {
1096 LOG(WARNING) << "Too many delta update failures, forcing full update.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001097 omaha_request_params_->set_delta_okay(false);
Darin Petkov36275772010-10-01 11:40:57 -07001098 }
1099}
1100
1101void UpdateAttempter::MarkDeltaUpdateFailure() {
Darin Petkov2dd01092010-10-08 15:43:05 -07001102 // Don't try to resume a failed delta update.
1103 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -07001104 int64_t delta_failures;
1105 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
1106 delta_failures < 0) {
1107 delta_failures = 0;
1108 }
1109 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
1110}
1111
Darin Petkov9b230572010-10-08 10:20:09 -07001112void UpdateAttempter::SetupDownload() {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001113 MultiRangeHttpFetcher* fetcher =
1114 dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001115 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -07001116 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001117 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -07001118 int64_t manifest_metadata_size = 0;
1119 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001120 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001121 // If there're remaining unprocessed data blobs, fetch them. Be careful not
1122 // to request data beyond the end of the payload to avoid 416 HTTP response
1123 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -07001124 int64_t next_data_offset = 0;
1125 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001126 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001127 if (resume_offset < response_handler_action_->install_plan().payload_size) {
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001128 fetcher->AddRange(resume_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -07001129 }
Darin Petkov9b230572010-10-08 10:20:09 -07001130 } else {
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001131 fetcher->AddRange(0);
Darin Petkov9b230572010-10-08 10:20:09 -07001132 }
Darin Petkov9b230572010-10-08 10:20:09 -07001133}
1134
Thieu Le116fda32011-04-19 11:01:54 -07001135void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -07001136 if (!processor_->IsRunning()) {
1137 shared_ptr<OmahaRequestAction> ping_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001138 new OmahaRequestAction(system_state_,
Thieu Led88a8572011-05-26 09:09:19 -07001139 NULL,
Jay Srinivasan08fce042012-06-07 16:31:01 -07001140 new LibcurlHttpFetcher(GetProxyResolver(),
Gilad Arnold7c04e762012-05-23 10:54:02 -07001141 system_state_,
1142 is_test_mode_),
Thieu Led88a8572011-05-26 09:09:19 -07001143 true));
1144 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
1145 processor_->set_delegate(NULL);
1146 processor_->EnqueueAction(ping_action.get());
1147 // Call StartProcessing() synchronously here to avoid any race conditions
1148 // caused by multiple outstanding ping Omaha requests. If we call
1149 // StartProcessing() asynchronously, the device can be suspended before we
1150 // get a chance to callback to StartProcessing(). When the device resumes
1151 // (assuming the device sleeps longer than the next update check period),
1152 // StartProcessing() is called back and at the same time, the next update
1153 // check is fired which eventually invokes StartProcessing(). A crash
1154 // can occur because StartProcessing() checks to make sure that the
1155 // processor is idle which it isn't due to the two concurrent ping Omaha
1156 // requests.
1157 processor_->StartProcessing();
1158 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -07001159 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -07001160 }
Thieu Led88a8572011-05-26 09:09:19 -07001161
1162 // Update the status which will schedule the next update check
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001163 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
1164 kUpdateNoticeUnspecified);
Thieu Le116fda32011-04-19 11:01:54 -07001165}
1166
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001167
1168bool UpdateAttempter::DecrementUpdateCheckCount() {
1169 int64 update_check_count_value;
1170
1171 if (!prefs_->Exists(kPrefsUpdateCheckCount)) {
1172 // This file does not exist. This means we haven't started our update
1173 // check count down yet, so nothing more to do. This file will be created
1174 // later when we first satisfy the wall-clock-based-wait period.
1175 LOG(INFO) << "No existing update check count. That's normal.";
1176 return true;
1177 }
1178
1179 if (prefs_->GetInt64(kPrefsUpdateCheckCount, &update_check_count_value)) {
1180 // Only if we're able to read a proper integer value, then go ahead
1181 // and decrement and write back the result in the same file, if needed.
1182 LOG(INFO) << "Update check count = " << update_check_count_value;
1183
1184 if (update_check_count_value == 0) {
1185 // It could be 0, if, for some reason, the file didn't get deleted
1186 // when we set our status to waiting for reboot. so we just leave it
1187 // as is so that we can prevent another update_check wait for this client.
1188 LOG(INFO) << "Not decrementing update check count as it's already 0.";
1189 return true;
1190 }
1191
1192 if (update_check_count_value > 0)
1193 update_check_count_value--;
1194 else
1195 update_check_count_value = 0;
1196
1197 // Write out the new value of update_check_count_value.
1198 if (prefs_->SetInt64(kPrefsUpdateCheckCount, update_check_count_value)) {
1199 // We successfully wrote out te new value, so enable the
1200 // update check based wait.
1201 LOG(INFO) << "New update check count = " << update_check_count_value;
1202 return true;
1203 }
1204 }
1205
1206 LOG(INFO) << "Deleting update check count state due to read/write errors.";
1207
1208 // We cannot read/write to the file, so disable the update check based wait
1209 // so that we don't get stuck in this OS version by any chance (which could
1210 // happen if there's some bug that causes to read/write incorrectly).
1211 // Also attempt to delete the file to do our best effort to cleanup.
1212 prefs_->Delete(kPrefsUpdateCheckCount);
1213 return false;
1214}
Chris Sosad317e402013-06-12 13:47:09 -07001215
David Zeuthene4c58bf2013-06-18 17:26:50 -07001216void UpdateAttempter::UpdateEngineStarted() {
1217 system_state_->payload_state()->UpdateEngineStarted();
1218}
1219
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001220} // namespace chromeos_update_engine