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