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