Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 2 | // 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 Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 6 | |
| 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 13 | #include <string> |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 14 | #include <tr1/memory> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 15 | #include <vector> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 16 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 17 | #include <base/file_util.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 18 | #include <base/rand_util.h> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 19 | #include <glib.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 20 | #include <metrics/metrics_library.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 21 | #include <policy/libpolicy.h> |
| 22 | #include <policy/device_policy.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 23 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 24 | #include "update_engine/certificate_checker.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 25 | #include "update_engine/dbus_service.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | #include "update_engine/download_action.h" |
| 27 | #include "update_engine/filesystem_copier_action.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 28 | #include "update_engine/gpio_handler.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 29 | #include "update_engine/libcurl_http_fetcher.h" |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 30 | #include "update_engine/multi_range_http_fetcher.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 31 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 32 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 33 | #include "update_engine/omaha_response_handler_action.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 34 | #include "update_engine/payload_state_interface.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 35 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 36 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 37 | #include "update_engine/subprocess.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 38 | #include "update_engine/system_state.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 39 | #include "update_engine/update_check_scheduler.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 40 | |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 41 | using base::TimeDelta; |
| 42 | using base::TimeTicks; |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 43 | using google::protobuf::NewPermanentCallback; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 44 | using std::make_pair; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 45 | using std::tr1::shared_ptr; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 46 | using std::set; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 47 | using std::string; |
| 48 | using std::vector; |
| 49 | |
| 50 | namespace chromeos_update_engine { |
| 51 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 52 | const int UpdateAttempter::kMaxDeltaUpdateFailures = 3; |
| 53 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 54 | // Private test server URL w/ custom port number. |
Gilad Arnold | ed74731 | 2012-03-15 18:20:41 -0700 | [diff] [blame] | 55 | // 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. |
| 57 | const char* const UpdateAttempter::kTestUpdateUrl( |
| 58 | "http://garnold.mtv.corp.google.com:8080/update"); |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 59 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 60 | const char* kUpdateCompletedMarker = |
| 61 | "/var/run/update_engine_autoupdate_completed"; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 62 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 63 | namespace { |
| 64 | const int kMaxConsecutiveObeyProxyRequests = 20; |
| 65 | } // namespace {} |
| 66 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 67 | const 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 Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 83 | case UPDATE_STATUS_REPORTING_ERROR_EVENT: |
| 84 | return "UPDATE_STATUS_REPORTING_ERROR_EVENT"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 85 | default: |
| 86 | return "unknown status"; |
| 87 | } |
| 88 | } |
| 89 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 90 | // 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. |
| 94 | ActionExitCode 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 Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 108 | |
| 109 | return code; |
| 110 | } |
| 111 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 112 | UpdateAttempter::UpdateAttempter(SystemState* system_state, |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 113 | DbusGlibInterface* dbus_iface) |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 114 | : processor_(new ActionProcessor()), |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 115 | system_state_(system_state), |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 116 | dbus_service_(NULL), |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 117 | update_check_scheduler_(NULL), |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 118 | fake_update_success_(false), |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 119 | http_response_code_(0), |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 120 | shares_(utils::kCpuSharesNormal), |
| 121 | manage_shares_source_(NULL), |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 122 | download_active_(false), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 123 | status_(UPDATE_STATUS_IDLE), |
| 124 | download_progress_(0.0), |
| 125 | last_checked_time_(0), |
| 126 | new_version_("0.0.0.0"), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 127 | new_payload_size_(0), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 128 | proxy_manual_checks_(0), |
| 129 | obeying_proxies_(true), |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 130 | chrome_proxy_resolver_(dbus_iface), |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 131 | updated_boot_flags_(false), |
| 132 | update_boot_flags_running_(false), |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 133 | start_action_processor_(false), |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 134 | policy_provider_(NULL), |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 135 | is_using_test_url_(false), |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 136 | is_test_mode_(false), |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 137 | is_test_update_attempted_(false) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 138 | prefs_ = system_state->prefs(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 139 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 140 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 141 | } |
| 142 | |
| 143 | UpdateAttempter::~UpdateAttempter() { |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 144 | CleanupCpuSharesManagement(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 147 | void UpdateAttempter::Update(const string& app_version, |
| 148 | const string& omaha_url, |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 149 | bool obey_proxies, |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 150 | bool interactive, |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 151 | bool is_test_mode, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 152 | bool is_user_initiated) { |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 153 | chrome_proxy_resolver_.Init(); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 154 | fake_update_success_ = false; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 155 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 156 | // 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 Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 158 | LOG(INFO) << "Not updating b/c we already updated and we're waiting for " |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 159 | << "reboot, we'll ping Omaha instead"; |
| 160 | PingOmaha(); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | if (status_ != UPDATE_STATUS_IDLE) { |
| 164 | // Update in progress. Do nothing |
| 165 | return; |
| 166 | } |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 167 | |
| 168 | if (!CalculateUpdateParams(app_version, |
| 169 | omaha_url, |
| 170 | obey_proxies, |
| 171 | interactive, |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 172 | is_test_mode, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 173 | is_user_initiated)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
| 177 | BuildUpdateActions(interactive); |
| 178 | |
| 179 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE, |
| 180 | kUpdateNoticeUnspecified); |
| 181 | |
| 182 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 183 | // before any update processing starts. |
| 184 | start_action_processor_ = true; |
| 185 | UpdateBootFlags(); |
| 186 | } |
| 187 | |
| 188 | bool UpdateAttempter::CalculateUpdateParams(const string& app_version, |
| 189 | const string& omaha_url, |
| 190 | bool obey_proxies, |
| 191 | bool interactive, |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 192 | bool is_test_mode, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 193 | bool is_user_initiated) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 194 | http_response_code_ = 0; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 195 | |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 196 | // Set the test mode flag for the current update attempt. |
| 197 | is_test_mode_ = is_test_mode; |
| 198 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 199 | // Lazy initialize the policy provider, or reload the latest policy data. |
| 200 | if (!policy_provider_.get()) { |
| 201 | policy_provider_.reset(new policy::PolicyProvider()); |
| 202 | } else { |
| 203 | policy_provider_->Reload(); |
| 204 | } |
| 205 | |
| 206 | // If the release_track is specified by policy, that takes precedence. |
| 207 | string release_track; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 208 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 209 | if (policy_provider_->device_policy_is_loaded()) { |
| 210 | const policy::DevicePolicy& device_policy = |
| 211 | policy_provider_->GetDevicePolicy(); |
| 212 | device_policy.GetReleaseChannel(&release_track); |
| 213 | device_policy.GetUpdateDisabled(&omaha_request_params_.update_disabled); |
| 214 | device_policy.GetTargetVersionPrefix( |
| 215 | &omaha_request_params_.target_version_prefix); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 216 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 217 | system_state_->set_device_policy(&device_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 218 | |
| 219 | set<string> allowed_types; |
| 220 | string allowed_types_str; |
| 221 | if (device_policy.GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 222 | set<string>::const_iterator iter; |
| 223 | for (iter = allowed_types.begin(); iter != allowed_types.end(); ++iter) |
| 224 | allowed_types_str += *iter + " "; |
| 225 | } |
| 226 | |
| 227 | LOG(INFO) << "Networks over which updates are allowed per policy : " |
| 228 | << (allowed_types_str.empty() ? "all" : allowed_types_str); |
| 229 | } else { |
| 230 | LOG(INFO) << "No device policies present."; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 231 | system_state_->set_device_policy(NULL); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 234 | CalculateScatteringParams(is_user_initiated); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 235 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 236 | // Determine whether an alternative test address should be used. |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 237 | string omaha_url_to_use = omaha_url; |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 238 | if ((is_using_test_url_ = (omaha_url_to_use.empty() && is_test_mode_))) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 239 | omaha_url_to_use = kTestUpdateUrl; |
| 240 | LOG(INFO) << "using alternative server address: " << omaha_url_to_use; |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 243 | if (!omaha_request_params_.Init(app_version, |
| 244 | omaha_url_to_use, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 245 | release_track)) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 246 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 247 | return false; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 248 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 249 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 250 | LOG(INFO) << "update_disabled = " |
| 251 | << (omaha_request_params_.update_disabled ? "true" : "false") |
| 252 | << ", target_version_prefix = " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 253 | << omaha_request_params_.target_version_prefix |
| 254 | << ", scatter_factor_in_seconds = " |
| 255 | << utils::FormatSecs(scatter_factor_.InSeconds()); |
| 256 | |
| 257 | LOG(INFO) << "Wall Clock Based Wait Enabled = " |
| 258 | << omaha_request_params_.wall_clock_based_wait_enabled |
| 259 | << ", Update Check Count Wait Enabled = " |
| 260 | << omaha_request_params_.update_check_count_wait_enabled |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 261 | << ", Waiting Period = " << utils::FormatSecs( |
| 262 | omaha_request_params_.waiting_period.InSeconds()); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 263 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 264 | obeying_proxies_ = true; |
| 265 | if (obey_proxies || proxy_manual_checks_ == 0) { |
| 266 | LOG(INFO) << "forced to obey proxies"; |
| 267 | // If forced to obey proxies, every 20th request will not use proxies |
| 268 | proxy_manual_checks_++; |
| 269 | LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_; |
| 270 | if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) { |
| 271 | proxy_manual_checks_ = 0; |
| 272 | obeying_proxies_ = false; |
| 273 | } |
| 274 | } else if (base::RandInt(0, 4) == 0) { |
| 275 | obeying_proxies_ = false; |
| 276 | } |
| 277 | LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update " |
| 278 | "check we are ignoring the proxy settings and using " |
| 279 | "direct connections."; |
| 280 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 281 | DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 285 | void UpdateAttempter::CalculateScatteringParams(bool is_user_initiated) { |
| 286 | // Take a copy of the old scatter value before we update it, as |
| 287 | // we need to update the waiting period if this value changes. |
| 288 | TimeDelta old_scatter_factor = scatter_factor_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 289 | const policy::DevicePolicy* device_policy = system_state_->device_policy(); |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 290 | if (device_policy) { |
| 291 | int64 new_scatter_factor_in_secs = 0; |
| 292 | device_policy->GetScatterFactorInSeconds(&new_scatter_factor_in_secs); |
| 293 | if (new_scatter_factor_in_secs < 0) // sanitize input, just in case. |
| 294 | new_scatter_factor_in_secs = 0; |
| 295 | scatter_factor_ = TimeDelta::FromSeconds(new_scatter_factor_in_secs); |
| 296 | } |
| 297 | |
| 298 | bool is_scatter_enabled = false; |
| 299 | if (scatter_factor_.InSeconds() == 0) { |
| 300 | LOG(INFO) << "Scattering disabled since scatter factor is set to 0"; |
| 301 | } else if (is_user_initiated) { |
| 302 | LOG(INFO) << "Scattering disabled as this update check is user-initiated"; |
| 303 | } else if (!system_state_->IsOOBEComplete()) { |
| 304 | LOG(INFO) << "Scattering disabled since OOBE is not complete yet"; |
| 305 | } else { |
| 306 | is_scatter_enabled = true; |
| 307 | LOG(INFO) << "Scattering is enabled"; |
| 308 | } |
| 309 | |
| 310 | if (is_scatter_enabled) { |
| 311 | // This means the scattering policy is turned on. |
| 312 | // Now check if we need to update the waiting period. The two cases |
| 313 | // in which we'd need to update the waiting period are: |
| 314 | // 1. First time in process or a scheduled check after a user-initiated one. |
| 315 | // (omaha_request_params_.waiting_period will be zero in this case). |
| 316 | // 2. Admin has changed the scattering policy value. |
| 317 | // (new scattering value will be different from old one in this case). |
| 318 | int64 wait_period_in_secs = 0; |
| 319 | if (omaha_request_params_.waiting_period.InSeconds() == 0) { |
| 320 | // First case. Check if we have a suitable value to set for |
| 321 | // the waiting period. |
| 322 | if (prefs_->GetInt64(kPrefsWallClockWaitPeriod, &wait_period_in_secs) && |
| 323 | wait_period_in_secs > 0 && |
| 324 | wait_period_in_secs <= scatter_factor_.InSeconds()) { |
| 325 | // This means: |
| 326 | // 1. There's a persisted value for the waiting period available. |
| 327 | // 2. And that persisted value is still valid. |
| 328 | // So, in this case, we should reuse the persisted value instead of |
| 329 | // generating a new random value to improve the chances of a good |
| 330 | // distribution for scattering. |
| 331 | omaha_request_params_.waiting_period = |
| 332 | TimeDelta::FromSeconds(wait_period_in_secs); |
| 333 | LOG(INFO) << "Using persisted wall-clock waiting period: " << |
| 334 | utils::FormatSecs(omaha_request_params_.waiting_period.InSeconds()); |
| 335 | } |
| 336 | else { |
| 337 | // This means there's no persisted value for the waiting period |
| 338 | // available or its value is invalid given the new scatter_factor value. |
| 339 | // So, we should go ahead and regenerate a new value for the |
| 340 | // waiting period. |
| 341 | LOG(INFO) << "Persisted value not present or not valid (" |
| 342 | << utils::FormatSecs(wait_period_in_secs) |
| 343 | << ") for wall-clock waiting period."; |
| 344 | GenerateNewWaitingPeriod(); |
| 345 | } |
| 346 | } else if (scatter_factor_ != old_scatter_factor) { |
| 347 | // This means there's already a waiting period value, but we detected |
| 348 | // a change in the scattering policy value. So, we should regenerate the |
| 349 | // waiting period to make sure it's within the bounds of the new scatter |
| 350 | // factor value. |
| 351 | GenerateNewWaitingPeriod(); |
| 352 | } else { |
| 353 | // Neither the first time scattering is enabled nor the scattering value |
| 354 | // changed. Nothing to do. |
| 355 | LOG(INFO) << "Keeping current wall-clock waiting period: " << |
| 356 | utils::FormatSecs(omaha_request_params_.waiting_period.InSeconds()); |
| 357 | } |
| 358 | |
| 359 | // The invariant at this point is that omaha_request_params_.waiting_period |
| 360 | // is non-zero no matter which path we took above. |
| 361 | LOG_IF(ERROR, omaha_request_params_.waiting_period.InSeconds() == 0) |
| 362 | << "Waiting Period should NOT be zero at this point!!!"; |
| 363 | |
| 364 | // Since scattering is enabled, wall clock based wait will always be |
| 365 | // enabled. |
| 366 | omaha_request_params_.wall_clock_based_wait_enabled = true; |
| 367 | |
| 368 | // If we don't have any issues in accessing the file system to update |
| 369 | // the update check count value, we'll turn that on as well. |
| 370 | bool decrement_succeeded = DecrementUpdateCheckCount(); |
| 371 | omaha_request_params_.update_check_count_wait_enabled = decrement_succeeded; |
| 372 | } else { |
| 373 | // This means the scattering feature is turned off or disabled for |
| 374 | // this particular update check. Make sure to disable |
| 375 | // all the knobs and artifacts so that we don't invoke any scattering |
| 376 | // related code. |
| 377 | omaha_request_params_.wall_clock_based_wait_enabled = false; |
| 378 | omaha_request_params_.update_check_count_wait_enabled = false; |
| 379 | omaha_request_params_.waiting_period = TimeDelta::FromSeconds(0); |
| 380 | prefs_->Delete(kPrefsWallClockWaitPeriod); |
| 381 | prefs_->Delete(kPrefsUpdateCheckCount); |
| 382 | // Don't delete the UpdateFirstSeenAt file as we don't want manual checks |
| 383 | // that result in no-updates (e.g. due to server side throttling) to |
| 384 | // cause update starvation by having the client generate a new |
| 385 | // UpdateFirstSeenAt for each scheduled check that follows a manual check. |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void UpdateAttempter::GenerateNewWaitingPeriod() { |
| 390 | omaha_request_params_.waiting_period = TimeDelta::FromSeconds( |
| 391 | base::RandInt(1, scatter_factor_.InSeconds())); |
| 392 | |
| 393 | LOG(INFO) << "Generated new wall-clock waiting period: " << utils::FormatSecs( |
| 394 | omaha_request_params_.waiting_period.InSeconds()); |
| 395 | |
| 396 | // Do a best-effort to persist this in all cases. Even if the persistence |
| 397 | // fails, we'll still be able to scatter based on our in-memory value. |
| 398 | // The persistence only helps in ensuring a good overall distribution |
| 399 | // across multiple devices if they tend to reboot too often. |
| 400 | prefs_->SetInt64(kPrefsWallClockWaitPeriod, |
| 401 | omaha_request_params_.waiting_period.InSeconds()); |
| 402 | } |
| 403 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 404 | void UpdateAttempter::BuildUpdateActions(bool interactive) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 405 | CHECK(!processor_->IsRunning()); |
| 406 | processor_->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 407 | |
| 408 | // Actions: |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 409 | LibcurlHttpFetcher* update_check_fetcher = |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 410 | new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_); |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 411 | // Try harder to connect to the network, esp when not interactive. |
| 412 | // See comment in libcurl_http_fetcher.cc. |
| 413 | update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 414 | update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 415 | shared_ptr<OmahaRequestAction> update_check_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 416 | new OmahaRequestAction(system_state_, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 417 | &omaha_request_params_, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 418 | NULL, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 419 | update_check_fetcher, // passes ownership |
| 420 | false)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 421 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 422 | new OmahaResponseHandlerAction(system_state_)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 423 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 424 | new FilesystemCopierAction(false, false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 425 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 426 | new FilesystemCopierAction(true, false)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 427 | shared_ptr<OmahaRequestAction> download_started_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 428 | new OmahaRequestAction(system_state_, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 429 | &omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 430 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 431 | OmahaEvent::kTypeUpdateDownloadStarted), |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 432 | new LibcurlHttpFetcher(GetProxyResolver(), |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 433 | system_state_, |
| 434 | is_test_mode_), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 435 | false)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 436 | LibcurlHttpFetcher* download_fetcher = |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 437 | new LibcurlHttpFetcher(GetProxyResolver(), system_state_, is_test_mode_); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 438 | download_fetcher->set_check_certificate(CertificateChecker::kDownload); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 439 | shared_ptr<DownloadAction> download_action( |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 440 | new DownloadAction(prefs_, |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 441 | system_state_, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 442 | new MultiRangeHttpFetcher( |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 443 | download_fetcher))); // passes ownership |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 444 | shared_ptr<OmahaRequestAction> download_finished_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 445 | new OmahaRequestAction(system_state_, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 446 | &omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 447 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 448 | OmahaEvent::kTypeUpdateDownloadFinished), |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 449 | new LibcurlHttpFetcher(GetProxyResolver(), |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 450 | system_state_, |
| 451 | is_test_mode_), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 452 | false)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 453 | shared_ptr<FilesystemCopierAction> filesystem_verifier_action( |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 454 | new FilesystemCopierAction(false, true)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 455 | shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action( |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 456 | new FilesystemCopierAction(true, true)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 457 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
| 458 | new PostinstallRunnerAction); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 459 | shared_ptr<OmahaRequestAction> update_complete_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 460 | new OmahaRequestAction(system_state_, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 461 | &omaha_request_params_, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 462 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 463 | new LibcurlHttpFetcher(GetProxyResolver(), |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 464 | system_state_, |
| 465 | is_test_mode_), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 466 | false)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 467 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 468 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 469 | response_handler_action_ = response_handler_action; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 470 | download_action_ = download_action; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 471 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 472 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 473 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 474 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 475 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 476 | kernel_filesystem_copier_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 477 | actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 478 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 479 | actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 480 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
| 481 | actions_.push_back(shared_ptr<AbstractAction>( |
| 482 | kernel_filesystem_verifier_action)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 483 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 484 | actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 485 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 486 | // Enqueue the actions |
| 487 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 488 | it != actions_.end(); ++it) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 489 | processor_->EnqueueAction(it->get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // Bond them together. We have to use the leaf-types when calling |
| 493 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 494 | BondActions(update_check_action.get(), |
| 495 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 496 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 497 | filesystem_copier_action.get()); |
| 498 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 499 | kernel_filesystem_copier_action.get()); |
| 500 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 501 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 502 | BondActions(download_action.get(), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 503 | filesystem_verifier_action.get()); |
| 504 | BondActions(filesystem_verifier_action.get(), |
| 505 | kernel_filesystem_verifier_action.get()); |
| 506 | BondActions(kernel_filesystem_verifier_action.get(), |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 507 | postinstall_runner_action.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 510 | void UpdateAttempter::CheckForUpdate(const string& app_version, |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 511 | const string& omaha_url, |
| 512 | bool is_user_initiated) { |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 513 | LOG(INFO) << "New update check requested"; |
| 514 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 515 | if (status_ != UPDATE_STATUS_IDLE) { |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 516 | LOG(INFO) << "Skipping update check because current status is " |
| 517 | << UpdateStatusToString(status_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 518 | return; |
| 519 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 520 | |
| 521 | // Read GPIO signals and determine whether this is an automated test scenario. |
| 522 | // For safety, we only allow a test update to be performed once; subsequent |
| 523 | // update requests will be carried out normally. |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 524 | bool is_test_mode = (!is_test_update_attempted_ && |
| 525 | system_state_->gpio_handler()->IsTestModeSignaled()); |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 526 | if (is_test_mode) { |
| 527 | LOG(WARNING) << "this is a test mode update attempt!"; |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 528 | is_test_update_attempted_ = true; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 531 | // Passing true for is_user_initiated to indicate that this |
| 532 | // is not a scheduled update check. |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 533 | Update(app_version, omaha_url, true, true, is_test_mode, is_user_initiated); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 536 | bool UpdateAttempter::RebootIfNeeded() { |
| 537 | if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 538 | LOG(INFO) << "Reboot requested, but status is " |
| 539 | << UpdateStatusToString(status_) << ", so not rebooting."; |
| 540 | return false; |
| 541 | } |
| 542 | TEST_AND_RETURN_FALSE(utils::Reboot()); |
| 543 | return true; |
| 544 | } |
| 545 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 546 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 547 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 548 | ActionExitCode code) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 549 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 550 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 551 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 552 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 553 | // Reset cpu shares back to normal. |
| 554 | CleanupCpuSharesManagement(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 555 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 556 | if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 557 | LOG(INFO) << "Error event sent."; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 558 | |
| 559 | // Inform scheduler of new status; also specifically inform about a failed |
| 560 | // update attempt with a test address. |
| 561 | SetStatusAndNotify(UPDATE_STATUS_IDLE, |
| 562 | (is_using_test_url_ ? kUpdateNoticeTestAddrFailed : |
| 563 | kUpdateNoticeUnspecified)); |
| 564 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 565 | if (!fake_update_success_) { |
| 566 | return; |
| 567 | } |
| 568 | LOG(INFO) << "Booted from FW B and tried to install new firmware, " |
| 569 | "so requesting reboot from user."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 572 | if (code == kActionCodeSuccess) { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 573 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 574 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 575 | prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 576 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 577 | |
| 578 | // Since we're done with scattering fully at this point, this is the |
| 579 | // safest point delete the state files, as we're sure that the status is |
| 580 | // set to reboot (which means no more updates will be applied until reboot) |
| 581 | // This deletion is required for correctness as we want the next update |
| 582 | // check to re-create a new random number for the update check count. |
| 583 | // Similarly, we also delete the wall-clock-wait period that was persisted |
| 584 | // so that we start with a new random value for the next update check |
| 585 | // after reboot so that the same device is not favored or punished in any |
| 586 | // way. |
| 587 | prefs_->Delete(kPrefsUpdateCheckCount); |
| 588 | prefs_->Delete(kPrefsWallClockWaitPeriod); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 589 | prefs_->Delete(kPrefsUpdateFirstSeenAt); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 590 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 591 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 592 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 593 | kUpdateNoticeUnspecified); |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 594 | |
| 595 | // Report the time it took to update the system. |
| 596 | int64_t update_time = time(NULL) - last_checked_time_; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 597 | if (!fake_update_success_) |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 598 | system_state_->metrics_lib()->SendToUMA( |
| 599 | "Installer.UpdateTime", |
| 600 | static_cast<int>(update_time), // sample |
| 601 | 1, // min = 1 second |
| 602 | 20 * 60, // max = 20 minutes |
| 603 | 50); // buckets |
| 604 | |
| 605 | // Also report the success code so that the percentiles can be |
| 606 | // interpreted properly for the remaining error codes in UMA. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 607 | utils::SendErrorCodeToUma(system_state_, code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 608 | return; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 609 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 610 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 611 | if (ScheduleErrorEventAction()) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 612 | return; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 613 | } |
| 614 | LOG(INFO) << "No update."; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 615 | SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 619 | // Reset cpu shares back to normal. |
| 620 | CleanupCpuSharesManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 621 | download_progress_ = 0.0; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 622 | SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 623 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 624 | error_event_.reset(NULL); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Called whenever an action has finished processing, either successfully |
| 628 | // or otherwise. |
| 629 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 630 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 631 | ActionExitCode code) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 632 | // Reset download progress regardless of whether or not the download |
| 633 | // action succeeded. Also, get the response code from HTTP request |
| 634 | // actions (update download as well as the initial update check |
| 635 | // actions). |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 636 | const string type = action->Type(); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 637 | if (type == DownloadAction::StaticType()) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 638 | download_progress_ = 0.0; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 639 | DownloadAction* download_action = dynamic_cast<DownloadAction*>(action); |
| 640 | http_response_code_ = download_action->GetHTTPResponseCode(); |
| 641 | } else if (type == OmahaRequestAction::StaticType()) { |
| 642 | OmahaRequestAction* omaha_request_action = |
| 643 | dynamic_cast<OmahaRequestAction*>(action); |
| 644 | // If the request is not an event, then it's the update-check. |
| 645 | if (!omaha_request_action->IsEvent()) { |
| 646 | http_response_code_ = omaha_request_action->GetHTTPResponseCode(); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 647 | // Forward the server-dictated poll interval to the update check |
| 648 | // scheduler, if any. |
| 649 | if (update_check_scheduler_) { |
| 650 | update_check_scheduler_->set_poll_interval( |
| 651 | omaha_request_action->GetOutputObject().poll_interval); |
| 652 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 655 | if (code != kActionCodeSuccess) { |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 656 | // If the current state is at or past the download phase, count the failure |
| 657 | // in case a switch to full update becomes necessary. Ignore network |
| 658 | // transfer timeouts and failures. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 659 | if (status_ >= UPDATE_STATUS_DOWNLOADING && |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 660 | code != kActionCodeDownloadTransferError) { |
| 661 | MarkDeltaUpdateFailure(); |
| 662 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 663 | // On failure, schedule an error event to be sent to Omaha. |
| 664 | CreatePendingErrorEvent(action, code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 665 | return; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 666 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 667 | // Find out which action completed. |
| 668 | if (type == OmahaResponseHandlerAction::StaticType()) { |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 669 | // Note that the status will be updated to DOWNLOADING when some bytes get |
| 670 | // actually downloaded from the server and the BytesReceived callback is |
| 671 | // invoked. This avoids notifying the user that a download has started in |
| 672 | // cases when the server and the client are unable to initiate the download. |
| 673 | CHECK(action == response_handler_action_.get()); |
| 674 | const InstallPlan& plan = response_handler_action_->install_plan(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 675 | last_checked_time_ = time(NULL); |
| 676 | // TODO(adlr): put version in InstallPlan |
| 677 | new_version_ = "0.0.0.0"; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 678 | new_payload_size_ = plan.payload_size; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 679 | SetupDownload(); |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 680 | SetupCpuSharesManagement(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 681 | SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE, |
| 682 | kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 683 | } else if (type == DownloadAction::StaticType()) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 684 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 685 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // Stop updating. An attempt will be made to record status to the disk |
| 689 | // so that updates can be resumed later. |
| 690 | void UpdateAttempter::Terminate() { |
| 691 | // TODO(adlr): implement this method. |
| 692 | NOTIMPLEMENTED(); |
| 693 | } |
| 694 | |
| 695 | // Try to resume from a previously Terminate()d update. |
| 696 | void UpdateAttempter::ResumeUpdating() { |
| 697 | // TODO(adlr): implement this method. |
| 698 | NOTIMPLEMENTED(); |
| 699 | } |
| 700 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 701 | void UpdateAttempter::SetDownloadStatus(bool active) { |
| 702 | download_active_ = active; |
| 703 | LOG(INFO) << "Download status: " << (active ? "active" : "inactive"); |
| 704 | } |
| 705 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 706 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 707 | if (!download_active_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 708 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 709 | return; |
| 710 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 711 | double progress = static_cast<double>(bytes_received) / |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 712 | static_cast<double>(total); |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 713 | // Self throttle based on progress. Also send notifications if |
| 714 | // progress is too slow. |
| 715 | const double kDeltaPercent = 0.01; // 1% |
| 716 | if (status_ != UPDATE_STATUS_DOWNLOADING || |
| 717 | bytes_received == total || |
| 718 | progress - download_progress_ >= kDeltaPercent || |
| 719 | TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) { |
| 720 | download_progress_ = progress; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 721 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 725 | bool UpdateAttempter::ResetStatus() { |
| 726 | LOG(INFO) << "Attempting to reset state from " |
| 727 | << UpdateStatusToString(status_) << " to UPDATE_STATUS_IDLE"; |
| 728 | |
| 729 | switch (status_) { |
| 730 | case UPDATE_STATUS_IDLE: |
| 731 | // no-op. |
| 732 | return true; |
| 733 | |
| 734 | case UPDATE_STATUS_UPDATED_NEED_REBOOT: { |
| 735 | status_ = UPDATE_STATUS_IDLE; |
| 736 | LOG(INFO) << "Reset Successful"; |
| 737 | |
| 738 | // also remove the reboot marker so that if the machine is rebooted |
| 739 | // after resetting to idle state, it doesn't go back to |
| 740 | // UPDATE_STATUS_UPDATED_NEED_REBOOT state. |
| 741 | const FilePath kUpdateCompletedMarkerPath(kUpdateCompletedMarker); |
| 742 | return file_util::Delete(kUpdateCompletedMarkerPath, false); |
| 743 | } |
| 744 | |
| 745 | default: |
| 746 | LOG(ERROR) << "Reset not allowed in this state."; |
| 747 | return false; |
| 748 | } |
| 749 | } |
| 750 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 751 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 752 | double* progress, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 753 | string* current_operation, |
| 754 | string* new_version, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 755 | int64_t* new_payload_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 756 | *last_checked_time = last_checked_time_; |
| 757 | *progress = download_progress_; |
| 758 | *current_operation = UpdateStatusToString(status_); |
| 759 | *new_version = new_version_; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 760 | *new_payload_size = new_payload_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 761 | return true; |
| 762 | } |
| 763 | |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 764 | void UpdateAttempter::UpdateBootFlags() { |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 765 | if (update_boot_flags_running_) { |
| 766 | LOG(INFO) << "Update boot flags running, nothing to do."; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 767 | return; |
| 768 | } |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 769 | if (updated_boot_flags_) { |
| 770 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 771 | if (start_action_processor_) { |
| 772 | ScheduleProcessingStart(); |
| 773 | } |
| 774 | return; |
| 775 | } |
| 776 | // This is purely best effort. Failures should be logged by Subprocess. Run |
| 777 | // the script asynchronously to avoid blocking the event loop regardless of |
| 778 | // the script runtime. |
| 779 | update_boot_flags_running_ = true; |
| 780 | LOG(INFO) << "Updating boot flags..."; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 781 | vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel"); |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 782 | if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) { |
| 783 | CompleteUpdateBootFlags(1); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | void UpdateAttempter::CompleteUpdateBootFlags(int return_code) { |
| 788 | update_boot_flags_running_ = false; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 789 | updated_boot_flags_ = true; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 790 | if (start_action_processor_) { |
| 791 | ScheduleProcessingStart(); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void UpdateAttempter::StaticCompleteUpdateBootFlags( |
| 796 | int return_code, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 797 | const string& output, |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 798 | void* p) { |
| 799 | reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code); |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 802 | void UpdateAttempter::BroadcastStatus() { |
| 803 | if (!dbus_service_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 804 | return; |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 805 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 806 | last_notify_time_ = TimeTicks::Now(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 807 | update_engine_service_emit_status_update( |
| 808 | dbus_service_, |
| 809 | last_checked_time_, |
| 810 | download_progress_, |
| 811 | UpdateStatusToString(status_), |
| 812 | new_version_.c_str(), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 813 | new_payload_size_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 814 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 815 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 816 | uint32_t UpdateAttempter::GetErrorCodeFlags() { |
| 817 | uint32_t flags = 0; |
| 818 | |
| 819 | if (!utils::IsNormalBootMode()) |
| 820 | flags |= kActionCodeDevModeFlag; |
| 821 | |
| 822 | if (response_handler_action_.get() && |
| 823 | response_handler_action_->install_plan().is_resume) |
| 824 | flags |= kActionCodeResumedFlag; |
| 825 | |
| 826 | if (!utils::IsOfficialBuild()) |
| 827 | flags |= kActionCodeTestImageFlag; |
| 828 | |
| 829 | if (omaha_request_params_.update_url != kProductionOmahaUrl) |
| 830 | flags |= kActionCodeTestOmahaUrlFlag; |
| 831 | |
| 832 | return flags; |
| 833 | } |
| 834 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 835 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status, |
| 836 | UpdateNotice notice) { |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 837 | status_ = status; |
| 838 | if (update_check_scheduler_) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 839 | update_check_scheduler_->SetUpdateStatus(status_, notice); |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | BroadcastStatus(); |
| 842 | } |
| 843 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 844 | void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, |
| 845 | ActionExitCode code) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 846 | if (error_event_.get()) { |
| 847 | // This shouldn't really happen. |
| 848 | LOG(WARNING) << "There's already an existing pending error event."; |
| 849 | return; |
| 850 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 851 | |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 852 | // For now assume that a generic Omaha response action failure means that |
| 853 | // there's no update so don't send an event. Also, double check that the |
| 854 | // failure has not occurred while sending an error event -- in which case |
| 855 | // don't schedule another. This shouldn't really happen but just in case... |
| 856 | if ((action->Type() == OmahaResponseHandlerAction::StaticType() && |
| 857 | code == kActionCodeError) || |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 858 | status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 859 | return; |
| 860 | } |
| 861 | |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 862 | // Classify the code to generate the appropriate result so that |
| 863 | // the Borgmon charts show up the results correctly. |
| 864 | // Do this before calling GetErrorCodeForAction which could potentially |
| 865 | // augment the bit representation of code and thus cause no matches for |
| 866 | // the switch cases below. |
| 867 | OmahaEvent::Result event_result; |
| 868 | switch (code) { |
| 869 | case kActionCodeOmahaUpdateIgnoredPerPolicy: |
| 870 | case kActionCodeOmahaUpdateDeferredPerPolicy: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 871 | case kActionCodeOmahaUpdateDeferredForBackoff: |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 872 | event_result = OmahaEvent::kResultUpdateDeferred; |
| 873 | break; |
| 874 | default: |
| 875 | event_result = OmahaEvent::kResultError; |
| 876 | break; |
| 877 | } |
| 878 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 879 | code = GetErrorCodeForAction(action, code); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 880 | fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 881 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 882 | // Compute the final error code with all the bit flags to be sent to Omaha. |
| 883 | code = static_cast<ActionExitCode>(code | GetErrorCodeFlags()); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 884 | error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 885 | event_result, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 886 | code)); |
| 887 | } |
| 888 | |
| 889 | bool UpdateAttempter::ScheduleErrorEventAction() { |
| 890 | if (error_event_.get() == NULL) |
| 891 | return false; |
| 892 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 893 | LOG(ERROR) << "Update failed."; |
| 894 | system_state_->payload_state()->UpdateFailed(error_event_->error_code); |
| 895 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 896 | // Send it to Uma. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 897 | LOG(INFO) << "Reporting the error event"; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 898 | utils::SendErrorCodeToUma(system_state_, error_event_->error_code); |
| 899 | |
| 900 | // Send it to Omaha. |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 901 | shared_ptr<OmahaRequestAction> error_event_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 902 | new OmahaRequestAction(system_state_, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 903 | &omaha_request_params_, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 904 | error_event_.release(), // Pass ownership. |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 905 | new LibcurlHttpFetcher(GetProxyResolver(), |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 906 | system_state_, |
| 907 | is_test_mode_), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 908 | false)); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 909 | actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 910 | processor_->EnqueueAction(error_event_action.get()); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 911 | SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT, |
| 912 | kUpdateNoticeUnspecified); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 913 | processor_->StartProcessing(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 914 | return true; |
| 915 | } |
| 916 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 917 | void UpdateAttempter::SetCpuShares(utils::CpuShares shares) { |
| 918 | if (shares_ == shares) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 919 | return; |
| 920 | } |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 921 | if (utils::SetCpuShares(shares)) { |
| 922 | shares_ = shares; |
| 923 | LOG(INFO) << "CPU shares = " << shares_; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 927 | void UpdateAttempter::SetupCpuSharesManagement() { |
| 928 | if (manage_shares_source_) { |
| 929 | LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed."; |
| 930 | CleanupCpuSharesManagement(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 931 | } |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 932 | const int kCpuSharesTimeout = 2 * 60 * 60; // 2 hours |
| 933 | manage_shares_source_ = g_timeout_source_new_seconds(kCpuSharesTimeout); |
| 934 | g_source_set_callback(manage_shares_source_, |
| 935 | StaticManageCpuSharesCallback, |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 936 | this, |
| 937 | NULL); |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 938 | g_source_attach(manage_shares_source_, NULL); |
| 939 | SetCpuShares(utils::kCpuSharesLow); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 942 | void UpdateAttempter::CleanupCpuSharesManagement() { |
| 943 | if (manage_shares_source_) { |
| 944 | g_source_destroy(manage_shares_source_); |
| 945 | manage_shares_source_ = NULL; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 946 | } |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 947 | SetCpuShares(utils::kCpuSharesNormal); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 950 | gboolean UpdateAttempter::StaticManageCpuSharesCallback(gpointer data) { |
| 951 | return reinterpret_cast<UpdateAttempter*>(data)->ManageCpuSharesCallback(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 954 | gboolean UpdateAttempter::StaticStartProcessing(gpointer data) { |
| 955 | reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing(); |
| 956 | return FALSE; // Don't call this callback again. |
| 957 | } |
| 958 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 959 | void UpdateAttempter::ScheduleProcessingStart() { |
| 960 | LOG(INFO) << "Scheduling an action processor start."; |
| 961 | start_action_processor_ = false; |
| 962 | g_idle_add(&StaticStartProcessing, this); |
| 963 | } |
| 964 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 965 | bool UpdateAttempter::ManageCpuSharesCallback() { |
| 966 | SetCpuShares(utils::kCpuSharesNormal); |
| 967 | manage_shares_source_ = NULL; |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 968 | return false; // Destroy the timeout source. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 971 | void UpdateAttempter::DisableDeltaUpdateIfNeeded() { |
| 972 | int64_t delta_failures; |
| 973 | if (omaha_request_params_.delta_okay && |
| 974 | prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) && |
| 975 | delta_failures >= kMaxDeltaUpdateFailures) { |
| 976 | LOG(WARNING) << "Too many delta update failures, forcing full update."; |
| 977 | omaha_request_params_.delta_okay = false; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | void UpdateAttempter::MarkDeltaUpdateFailure() { |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 982 | // Don't try to resume a failed delta update. |
| 983 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 984 | int64_t delta_failures; |
| 985 | if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) || |
| 986 | delta_failures < 0) { |
| 987 | delta_failures = 0; |
| 988 | } |
| 989 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures); |
| 990 | } |
| 991 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 992 | void UpdateAttempter::SetupDownload() { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 993 | MultiRangeHttpFetcher* fetcher = |
| 994 | dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher()); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 995 | fetcher->ClearRanges(); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 996 | if (response_handler_action_->install_plan().is_resume) { |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 997 | // Resuming an update so fetch the update manifest metadata first. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 998 | int64_t manifest_metadata_size = 0; |
| 999 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 1000 | fetcher->AddRange(0, manifest_metadata_size); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 1001 | // If there're remaining unprocessed data blobs, fetch them. Be careful not |
| 1002 | // to request data beyond the end of the payload to avoid 416 HTTP response |
| 1003 | // error codes. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1004 | int64_t next_data_offset = 0; |
| 1005 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 1006 | uint64_t resume_offset = manifest_metadata_size + next_data_offset; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1007 | if (resume_offset < response_handler_action_->install_plan().payload_size) { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1008 | fetcher->AddRange(resume_offset); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 1009 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1010 | } else { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1011 | fetcher->AddRange(0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1012 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1015 | void UpdateAttempter::PingOmaha() { |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 1016 | if (!processor_->IsRunning()) { |
| 1017 | shared_ptr<OmahaRequestAction> ping_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1018 | new OmahaRequestAction(system_state_, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1019 | &omaha_request_params_, |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 1020 | NULL, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1021 | new LibcurlHttpFetcher(GetProxyResolver(), |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 1022 | system_state_, |
| 1023 | is_test_mode_), |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 1024 | true)); |
| 1025 | actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action)); |
| 1026 | processor_->set_delegate(NULL); |
| 1027 | processor_->EnqueueAction(ping_action.get()); |
| 1028 | // Call StartProcessing() synchronously here to avoid any race conditions |
| 1029 | // caused by multiple outstanding ping Omaha requests. If we call |
| 1030 | // StartProcessing() asynchronously, the device can be suspended before we |
| 1031 | // get a chance to callback to StartProcessing(). When the device resumes |
| 1032 | // (assuming the device sleeps longer than the next update check period), |
| 1033 | // StartProcessing() is called back and at the same time, the next update |
| 1034 | // check is fired which eventually invokes StartProcessing(). A crash |
| 1035 | // can occur because StartProcessing() checks to make sure that the |
| 1036 | // processor is idle which it isn't due to the two concurrent ping Omaha |
| 1037 | // requests. |
| 1038 | processor_->StartProcessing(); |
| 1039 | } else { |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 1040 | LOG(WARNING) << "Action processor running, Omaha ping suppressed."; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 1041 | } |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 1042 | |
| 1043 | // Update the status which will schedule the next update check |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 1044 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 1045 | kUpdateNoticeUnspecified); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1048 | |
| 1049 | bool UpdateAttempter::DecrementUpdateCheckCount() { |
| 1050 | int64 update_check_count_value; |
| 1051 | |
| 1052 | if (!prefs_->Exists(kPrefsUpdateCheckCount)) { |
| 1053 | // This file does not exist. This means we haven't started our update |
| 1054 | // check count down yet, so nothing more to do. This file will be created |
| 1055 | // later when we first satisfy the wall-clock-based-wait period. |
| 1056 | LOG(INFO) << "No existing update check count. That's normal."; |
| 1057 | return true; |
| 1058 | } |
| 1059 | |
| 1060 | if (prefs_->GetInt64(kPrefsUpdateCheckCount, &update_check_count_value)) { |
| 1061 | // Only if we're able to read a proper integer value, then go ahead |
| 1062 | // and decrement and write back the result in the same file, if needed. |
| 1063 | LOG(INFO) << "Update check count = " << update_check_count_value; |
| 1064 | |
| 1065 | if (update_check_count_value == 0) { |
| 1066 | // It could be 0, if, for some reason, the file didn't get deleted |
| 1067 | // when we set our status to waiting for reboot. so we just leave it |
| 1068 | // as is so that we can prevent another update_check wait for this client. |
| 1069 | LOG(INFO) << "Not decrementing update check count as it's already 0."; |
| 1070 | return true; |
| 1071 | } |
| 1072 | |
| 1073 | if (update_check_count_value > 0) |
| 1074 | update_check_count_value--; |
| 1075 | else |
| 1076 | update_check_count_value = 0; |
| 1077 | |
| 1078 | // Write out the new value of update_check_count_value. |
| 1079 | if (prefs_->SetInt64(kPrefsUpdateCheckCount, update_check_count_value)) { |
| 1080 | // We successfully wrote out te new value, so enable the |
| 1081 | // update check based wait. |
| 1082 | LOG(INFO) << "New update check count = " << update_check_count_value; |
| 1083 | return true; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | LOG(INFO) << "Deleting update check count state due to read/write errors."; |
| 1088 | |
| 1089 | // We cannot read/write to the file, so disable the update check based wait |
| 1090 | // so that we don't get stuck in this OS version by any chance (which could |
| 1091 | // happen if there's some bug that causes to read/write incorrectly). |
| 1092 | // Also attempt to delete the file to do our best effort to cleanup. |
| 1093 | prefs_->Delete(kPrefsUpdateCheckCount); |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 1097 | } // namespace chromeos_update_engine |