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