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