Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/update_attempter.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 6 | |
| 7 | // From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L |
| 8 | #ifndef _POSIX_C_SOURCE |
| 9 | #define _POSIX_C_SOURCE 199309L |
| 10 | #endif // _POSIX_C_SOURCE |
| 11 | #include <time.h> |
| 12 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 13 | #include <string> |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 14 | #include <tr1/memory> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 15 | #include <vector> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 16 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 17 | #include <base/rand_util.h> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 18 | #include <glib.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 19 | #include <metrics/metrics_library.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 20 | #include <policy/libpolicy.h> |
| 21 | #include <policy/device_policy.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 22 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 23 | #include "update_engine/certificate_checker.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 24 | #include "update_engine/dbus_service.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 25 | #include "update_engine/download_action.h" |
| 26 | #include "update_engine/filesystem_copier_action.h" |
| 27 | #include "update_engine/libcurl_http_fetcher.h" |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 28 | #include "update_engine/multi_range_http_fetcher.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 29 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 30 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 31 | #include "update_engine/omaha_response_handler_action.h" |
| 32 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 33 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 34 | #include "update_engine/subprocess.h" |
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; |
| 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"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 80 | default: |
| 81 | return "unknown status"; |
| 82 | } |
| 83 | } |
| 84 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 85 | // Turns a generic kActionCodeError to a generic error code specific |
| 86 | // to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is |
| 87 | // not kActionCodeError, or the action is not matched, returns |code| |
| 88 | // unchanged. |
| 89 | ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 90 | ActionExitCode code) { |
| 91 | if (code != kActionCodeError) |
| 92 | return code; |
| 93 | |
| 94 | const string type = action->Type(); |
| 95 | if (type == OmahaRequestAction::StaticType()) |
| 96 | return kActionCodeOmahaRequestError; |
| 97 | if (type == OmahaResponseHandlerAction::StaticType()) |
| 98 | return kActionCodeOmahaResponseHandlerError; |
| 99 | if (type == FilesystemCopierAction::StaticType()) |
| 100 | return kActionCodeFilesystemCopierError; |
| 101 | if (type == PostinstallRunnerAction::StaticType()) |
| 102 | return kActionCodePostinstallRunnerError; |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 103 | |
| 104 | return code; |
| 105 | } |
| 106 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 107 | UpdateAttempter::UpdateAttempter(PrefsInterface* prefs, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 108 | MetricsLibraryInterface* metrics_lib, |
| 109 | DbusGlibInterface* dbus_iface) |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 110 | : processor_(new ActionProcessor()), |
| 111 | dbus_service_(NULL), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 112 | prefs_(prefs), |
| 113 | metrics_lib_(metrics_lib), |
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), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 117 | priority_(utils::kProcessPriorityNormal), |
| 118 | manage_priority_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"), |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 124 | new_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), |
| 132 | is_using_test_url_(false) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 133 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 134 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 135 | } |
| 136 | |
| 137 | UpdateAttempter::~UpdateAttempter() { |
| 138 | CleanupPriorityManagement(); |
| 139 | } |
| 140 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 141 | void UpdateAttempter::Update(const string& app_version, |
| 142 | const string& omaha_url, |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 143 | bool obey_proxies, |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 144 | bool interactive, |
| 145 | bool is_test) { |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 146 | chrome_proxy_resolver_.Init(); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 147 | fake_update_success_ = false; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 148 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 149 | // Although we have applied an update, we still want to ping Omaha |
| 150 | // to ensure the number of active statistics is accurate. |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 151 | 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] | 152 | << "reboot, we'll ping Omaha instead"; |
| 153 | PingOmaha(); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | if (status_ != UPDATE_STATUS_IDLE) { |
| 157 | // Update in progress. Do nothing |
| 158 | return; |
| 159 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 160 | http_response_code_ = 0; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 161 | |
| 162 | // Lazy initialize the policy provider, or reload the latest policy data. |
| 163 | if (!policy_provider_.get()) { |
| 164 | policy_provider_.reset(new policy::PolicyProvider()); |
| 165 | } else { |
| 166 | policy_provider_->Reload(); |
| 167 | } |
| 168 | |
| 169 | // If the release_track is specified by policy, that takes precedence. |
| 170 | string release_track; |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame^] | 171 | if (policy_provider_->device_policy_is_loaded()) { |
| 172 | const policy::DevicePolicy& device_policy = |
| 173 | policy_provider_->GetDevicePolicy(); |
| 174 | device_policy.GetReleaseChannel(&release_track); |
| 175 | device_policy.GetUpdateDisabled(&omaha_request_params_.update_disabled); |
| 176 | device_policy.GetTargetVersionPrefix( |
| 177 | &omaha_request_params_.target_version_prefix); |
| 178 | } |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 179 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 180 | // Determine whether an alternative test address should be used. |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 181 | string omaha_url_to_use = omaha_url; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 182 | if ((is_using_test_url_ = (omaha_url_to_use.empty() && is_test))) { |
| 183 | omaha_url_to_use = kTestUpdateUrl; |
| 184 | LOG(INFO) << "using alternative server address: " << omaha_url_to_use; |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame^] | 187 | if (!omaha_request_params_.Init(app_version, |
| 188 | omaha_url_to_use, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 189 | release_track)) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 190 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
| 191 | return; |
| 192 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 193 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame^] | 194 | LOG(INFO) << "update_disabled = " |
| 195 | << (omaha_request_params_.update_disabled ? "true" : "false") |
| 196 | << ", target_version_prefix = " |
| 197 | << omaha_request_params_.target_version_prefix; |
| 198 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 199 | obeying_proxies_ = true; |
| 200 | if (obey_proxies || proxy_manual_checks_ == 0) { |
| 201 | LOG(INFO) << "forced to obey proxies"; |
| 202 | // If forced to obey proxies, every 20th request will not use proxies |
| 203 | proxy_manual_checks_++; |
| 204 | LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_; |
| 205 | if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) { |
| 206 | proxy_manual_checks_ = 0; |
| 207 | obeying_proxies_ = false; |
| 208 | } |
| 209 | } else if (base::RandInt(0, 4) == 0) { |
| 210 | obeying_proxies_ = false; |
| 211 | } |
| 212 | LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update " |
| 213 | "check we are ignoring the proxy settings and using " |
| 214 | "direct connections."; |
| 215 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 216 | DisableDeltaUpdateIfNeeded(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 217 | CHECK(!processor_->IsRunning()); |
| 218 | processor_->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 219 | |
| 220 | // Actions: |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 221 | LibcurlHttpFetcher* update_check_fetcher = |
| 222 | new LibcurlHttpFetcher(GetProxyResolver()); |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 223 | // Try harder to connect to the network, esp when not interactive. |
| 224 | // See comment in libcurl_http_fetcher.cc. |
| 225 | update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 226 | update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 227 | shared_ptr<OmahaRequestAction> update_check_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 228 | new OmahaRequestAction(prefs_, |
| 229 | omaha_request_params_, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 230 | NULL, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 231 | update_check_fetcher, // passes ownership |
| 232 | false)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 233 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 234 | new OmahaResponseHandlerAction(prefs_)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 235 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 236 | new FilesystemCopierAction(false, false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 237 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 238 | new FilesystemCopierAction(true, false)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 239 | shared_ptr<OmahaRequestAction> download_started_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 240 | new OmahaRequestAction(prefs_, |
| 241 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 242 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 243 | OmahaEvent::kTypeUpdateDownloadStarted), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 244 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 245 | false)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 246 | LibcurlHttpFetcher* download_fetcher = |
| 247 | new LibcurlHttpFetcher(GetProxyResolver()); |
| 248 | download_fetcher->set_check_certificate(CertificateChecker::kDownload); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 249 | shared_ptr<DownloadAction> download_action( |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 250 | new DownloadAction(prefs_, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 251 | new MultiRangeHttpFetcher( |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 252 | download_fetcher))); // passes ownership |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 253 | shared_ptr<OmahaRequestAction> download_finished_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 254 | new OmahaRequestAction(prefs_, |
| 255 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 256 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 257 | OmahaEvent::kTypeUpdateDownloadFinished), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 258 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 259 | false)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 260 | shared_ptr<FilesystemCopierAction> filesystem_verifier_action( |
| 261 | new FilesystemCopierAction(false, true)); |
| 262 | shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action( |
| 263 | new FilesystemCopierAction(true, true)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 264 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
| 265 | new PostinstallRunnerAction); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 266 | shared_ptr<OmahaRequestAction> update_complete_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 267 | new OmahaRequestAction(prefs_, |
| 268 | omaha_request_params_, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 269 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 270 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 271 | false)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 272 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 273 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 274 | response_handler_action_ = response_handler_action; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 275 | download_action_ = download_action; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 276 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 277 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 278 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 279 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 280 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 281 | kernel_filesystem_copier_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 282 | actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 283 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 284 | actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 285 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
| 286 | actions_.push_back(shared_ptr<AbstractAction>( |
| 287 | kernel_filesystem_verifier_action)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 288 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 289 | actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 290 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 291 | // Enqueue the actions |
| 292 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 293 | it != actions_.end(); ++it) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 294 | processor_->EnqueueAction(it->get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Bond them together. We have to use the leaf-types when calling |
| 298 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 299 | BondActions(update_check_action.get(), |
| 300 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 301 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 302 | filesystem_copier_action.get()); |
| 303 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 304 | kernel_filesystem_copier_action.get()); |
| 305 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 306 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 307 | BondActions(download_action.get(), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 308 | filesystem_verifier_action.get()); |
| 309 | BondActions(filesystem_verifier_action.get(), |
| 310 | kernel_filesystem_verifier_action.get()); |
| 311 | BondActions(kernel_filesystem_verifier_action.get(), |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 312 | postinstall_runner_action.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 313 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 314 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE, |
| 315 | kUpdateNoticeUnspecified); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 316 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 317 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 318 | // before any update processing starts. |
| 319 | start_action_processor_ = true; |
| 320 | UpdateBootFlags(); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 323 | void UpdateAttempter::CheckForUpdate(const string& app_version, |
| 324 | const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 325 | if (status_ != UPDATE_STATUS_IDLE) { |
| 326 | LOG(INFO) << "Check for update requested, but status is " |
| 327 | << UpdateStatusToString(status_) << ", so not checking."; |
| 328 | return; |
| 329 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 330 | |
| 331 | // Read GPIO signals and determine whether this is an automated test scenario. |
| 332 | // For safety, we only allow a test update to be performed once; subsequent |
| 333 | // update requests will be carried out normally. |
| 334 | static bool is_test_used_once = false; |
| 335 | bool is_test = !is_test_used_once && GpioHandler::IsGpioSignalingTest(); |
| 336 | if (is_test) { |
| 337 | LOG(INFO) << "test mode signaled"; |
| 338 | is_test_used_once = true; |
| 339 | } |
| 340 | |
| 341 | Update(app_version, omaha_url, true, true, is_test); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 344 | bool UpdateAttempter::RebootIfNeeded() { |
| 345 | if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 346 | LOG(INFO) << "Reboot requested, but status is " |
| 347 | << UpdateStatusToString(status_) << ", so not rebooting."; |
| 348 | return false; |
| 349 | } |
| 350 | TEST_AND_RETURN_FALSE(utils::Reboot()); |
| 351 | return true; |
| 352 | } |
| 353 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 354 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 355 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 356 | ActionExitCode code) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 357 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 358 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 359 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 360 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 361 | // Reset process priority back to normal. |
| 362 | CleanupPriorityManagement(); |
| 363 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 364 | if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 365 | LOG(INFO) << "Error event sent."; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 366 | |
| 367 | // Inform scheduler of new status; also specifically inform about a failed |
| 368 | // update attempt with a test address. |
| 369 | SetStatusAndNotify(UPDATE_STATUS_IDLE, |
| 370 | (is_using_test_url_ ? kUpdateNoticeTestAddrFailed : |
| 371 | kUpdateNoticeUnspecified)); |
| 372 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 373 | if (!fake_update_success_) { |
| 374 | return; |
| 375 | } |
| 376 | LOG(INFO) << "Booted from FW B and tried to install new firmware, " |
| 377 | "so requesting reboot from user."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 380 | if (code == kActionCodeSuccess) { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 381 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 382 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 383 | prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 384 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 385 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 386 | kUpdateNoticeUnspecified); |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 387 | |
| 388 | // Report the time it took to update the system. |
| 389 | int64_t update_time = time(NULL) - last_checked_time_; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 390 | if (!fake_update_success_) |
| 391 | metrics_lib_->SendToUMA("Installer.UpdateTime", |
| 392 | static_cast<int>(update_time), // sample |
| 393 | 1, // min = 1 second |
| 394 | 20 * 60, // max = 20 minutes |
| 395 | 50); // buckets |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 396 | return; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 397 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 398 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 399 | if (ScheduleErrorEventAction()) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 400 | return; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 401 | } |
| 402 | LOG(INFO) << "No update."; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 403 | SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 407 | // Reset process priority back to normal. |
| 408 | CleanupPriorityManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 409 | download_progress_ = 0.0; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 410 | SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 411 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 412 | error_event_.reset(NULL); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Called whenever an action has finished processing, either successfully |
| 416 | // or otherwise. |
| 417 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 418 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 419 | ActionExitCode code) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 420 | // Reset download progress regardless of whether or not the download |
| 421 | // action succeeded. Also, get the response code from HTTP request |
| 422 | // actions (update download as well as the initial update check |
| 423 | // actions). |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 424 | const string type = action->Type(); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 425 | if (type == DownloadAction::StaticType()) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 426 | download_progress_ = 0.0; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 427 | DownloadAction* download_action = dynamic_cast<DownloadAction*>(action); |
| 428 | http_response_code_ = download_action->GetHTTPResponseCode(); |
| 429 | } else if (type == OmahaRequestAction::StaticType()) { |
| 430 | OmahaRequestAction* omaha_request_action = |
| 431 | dynamic_cast<OmahaRequestAction*>(action); |
| 432 | // If the request is not an event, then it's the update-check. |
| 433 | if (!omaha_request_action->IsEvent()) { |
| 434 | http_response_code_ = omaha_request_action->GetHTTPResponseCode(); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 435 | // Forward the server-dictated poll interval to the update check |
| 436 | // scheduler, if any. |
| 437 | if (update_check_scheduler_) { |
| 438 | update_check_scheduler_->set_poll_interval( |
| 439 | omaha_request_action->GetOutputObject().poll_interval); |
| 440 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 443 | if (code != kActionCodeSuccess) { |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 444 | // If the current state is at or past the download phase, count the failure |
| 445 | // in case a switch to full update becomes necessary. Ignore network |
| 446 | // transfer timeouts and failures. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 447 | if (status_ >= UPDATE_STATUS_DOWNLOADING && |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 448 | code != kActionCodeDownloadTransferError) { |
| 449 | MarkDeltaUpdateFailure(); |
| 450 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 451 | // On failure, schedule an error event to be sent to Omaha. |
| 452 | CreatePendingErrorEvent(action, code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 453 | return; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 454 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 455 | // Find out which action completed. |
| 456 | if (type == OmahaResponseHandlerAction::StaticType()) { |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 457 | // Note that the status will be updated to DOWNLOADING when some bytes get |
| 458 | // actually downloaded from the server and the BytesReceived callback is |
| 459 | // invoked. This avoids notifying the user that a download has started in |
| 460 | // cases when the server and the client are unable to initiate the download. |
| 461 | CHECK(action == response_handler_action_.get()); |
| 462 | const InstallPlan& plan = response_handler_action_->install_plan(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 463 | last_checked_time_ = time(NULL); |
| 464 | // TODO(adlr): put version in InstallPlan |
| 465 | new_version_ = "0.0.0.0"; |
| 466 | new_size_ = plan.size; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 467 | SetupDownload(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 468 | SetupPriorityManagement(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 469 | SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE, |
| 470 | kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 471 | } else if (type == DownloadAction::StaticType()) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 472 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 473 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Stop updating. An attempt will be made to record status to the disk |
| 477 | // so that updates can be resumed later. |
| 478 | void UpdateAttempter::Terminate() { |
| 479 | // TODO(adlr): implement this method. |
| 480 | NOTIMPLEMENTED(); |
| 481 | } |
| 482 | |
| 483 | // Try to resume from a previously Terminate()d update. |
| 484 | void UpdateAttempter::ResumeUpdating() { |
| 485 | // TODO(adlr): implement this method. |
| 486 | NOTIMPLEMENTED(); |
| 487 | } |
| 488 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 489 | void UpdateAttempter::SetDownloadStatus(bool active) { |
| 490 | download_active_ = active; |
| 491 | LOG(INFO) << "Download status: " << (active ? "active" : "inactive"); |
| 492 | } |
| 493 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 494 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 495 | if (!download_active_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 496 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 497 | return; |
| 498 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 499 | double progress = static_cast<double>(bytes_received) / |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 500 | static_cast<double>(total); |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 501 | // Self throttle based on progress. Also send notifications if |
| 502 | // progress is too slow. |
| 503 | const double kDeltaPercent = 0.01; // 1% |
| 504 | if (status_ != UPDATE_STATUS_DOWNLOADING || |
| 505 | bytes_received == total || |
| 506 | progress - download_progress_ >= kDeltaPercent || |
| 507 | TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) { |
| 508 | download_progress_ = progress; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 509 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING, kUpdateNoticeUnspecified); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 513 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 514 | double* progress, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 515 | string* current_operation, |
| 516 | string* new_version, |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 517 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 518 | *last_checked_time = last_checked_time_; |
| 519 | *progress = download_progress_; |
| 520 | *current_operation = UpdateStatusToString(status_); |
| 521 | *new_version = new_version_; |
| 522 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 523 | return true; |
| 524 | } |
| 525 | |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 526 | void UpdateAttempter::UpdateBootFlags() { |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 527 | if (update_boot_flags_running_) { |
| 528 | LOG(INFO) << "Update boot flags running, nothing to do."; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 529 | return; |
| 530 | } |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 531 | if (updated_boot_flags_) { |
| 532 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 533 | if (start_action_processor_) { |
| 534 | ScheduleProcessingStart(); |
| 535 | } |
| 536 | return; |
| 537 | } |
| 538 | // This is purely best effort. Failures should be logged by Subprocess. Run |
| 539 | // the script asynchronously to avoid blocking the event loop regardless of |
| 540 | // the script runtime. |
| 541 | update_boot_flags_running_ = true; |
| 542 | LOG(INFO) << "Updating boot flags..."; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 543 | vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel"); |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 544 | if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) { |
| 545 | CompleteUpdateBootFlags(1); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void UpdateAttempter::CompleteUpdateBootFlags(int return_code) { |
| 550 | update_boot_flags_running_ = false; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 551 | updated_boot_flags_ = true; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 552 | if (start_action_processor_) { |
| 553 | ScheduleProcessingStart(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void UpdateAttempter::StaticCompleteUpdateBootFlags( |
| 558 | int return_code, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 559 | const string& output, |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 560 | void* p) { |
| 561 | reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code); |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 564 | void UpdateAttempter::BroadcastStatus() { |
| 565 | if (!dbus_service_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 566 | return; |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 567 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 568 | last_notify_time_ = TimeTicks::Now(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 569 | update_engine_service_emit_status_update( |
| 570 | dbus_service_, |
| 571 | last_checked_time_, |
| 572 | download_progress_, |
| 573 | UpdateStatusToString(status_), |
| 574 | new_version_.c_str(), |
| 575 | new_size_); |
| 576 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 577 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 578 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status, |
| 579 | UpdateNotice notice) { |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 580 | status_ = status; |
| 581 | if (update_check_scheduler_) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 582 | update_check_scheduler_->SetUpdateStatus(status_, notice); |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | BroadcastStatus(); |
| 585 | } |
| 586 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 587 | void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, |
| 588 | ActionExitCode code) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 589 | if (error_event_.get()) { |
| 590 | // This shouldn't really happen. |
| 591 | LOG(WARNING) << "There's already an existing pending error event."; |
| 592 | return; |
| 593 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 594 | |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 595 | // For now assume that a generic Omaha response action failure means that |
| 596 | // there's no update so don't send an event. Also, double check that the |
| 597 | // failure has not occurred while sending an error event -- in which case |
| 598 | // don't schedule another. This shouldn't really happen but just in case... |
| 599 | if ((action->Type() == OmahaResponseHandlerAction::StaticType() && |
| 600 | code == kActionCodeError) || |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 601 | status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | code = GetErrorCodeForAction(action, code); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 606 | fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 607 | |
| 608 | // Apply the bit modifiers to the error code. |
| 609 | if (!utils::IsNormalBootMode()) { |
| 610 | code = static_cast<ActionExitCode>(code | kActionCodeBootModeFlag); |
| 611 | } |
| 612 | if (response_handler_action_.get() && |
| 613 | response_handler_action_->install_plan().is_resume) { |
| 614 | code = static_cast<ActionExitCode>(code | kActionCodeResumedFlag); |
| 615 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 616 | error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 617 | OmahaEvent::kResultError, |
| 618 | code)); |
| 619 | } |
| 620 | |
| 621 | bool UpdateAttempter::ScheduleErrorEventAction() { |
| 622 | if (error_event_.get() == NULL) |
| 623 | return false; |
| 624 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 625 | LOG(INFO) << "Update failed -- reporting the error event."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 626 | shared_ptr<OmahaRequestAction> error_event_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 627 | new OmahaRequestAction(prefs_, |
| 628 | omaha_request_params_, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 629 | error_event_.release(), // Pass ownership. |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 630 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 631 | false)); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 632 | actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 633 | processor_->EnqueueAction(error_event_action.get()); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 634 | SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT, |
| 635 | kUpdateNoticeUnspecified); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 636 | processor_->StartProcessing(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 637 | return true; |
| 638 | } |
| 639 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 640 | void UpdateAttempter::SetPriority(utils::ProcessPriority priority) { |
| 641 | if (priority_ == priority) { |
| 642 | return; |
| 643 | } |
| 644 | if (utils::SetProcessPriority(priority)) { |
| 645 | priority_ = priority; |
| 646 | LOG(INFO) << "Process priority = " << priority_; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | void UpdateAttempter::SetupPriorityManagement() { |
| 651 | if (manage_priority_source_) { |
| 652 | LOG(ERROR) << "Process priority timeout source hasn't been destroyed."; |
| 653 | CleanupPriorityManagement(); |
| 654 | } |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 655 | const int kPriorityTimeout = 2 * 60 * 60; // 2 hours |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 656 | manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout); |
| 657 | g_source_set_callback(manage_priority_source_, |
| 658 | StaticManagePriorityCallback, |
| 659 | this, |
| 660 | NULL); |
| 661 | g_source_attach(manage_priority_source_, NULL); |
| 662 | SetPriority(utils::kProcessPriorityLow); |
| 663 | } |
| 664 | |
| 665 | void UpdateAttempter::CleanupPriorityManagement() { |
| 666 | if (manage_priority_source_) { |
| 667 | g_source_destroy(manage_priority_source_); |
| 668 | manage_priority_source_ = NULL; |
| 669 | } |
| 670 | SetPriority(utils::kProcessPriorityNormal); |
| 671 | } |
| 672 | |
| 673 | gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) { |
| 674 | return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback(); |
| 675 | } |
| 676 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 677 | gboolean UpdateAttempter::StaticStartProcessing(gpointer data) { |
| 678 | reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing(); |
| 679 | return FALSE; // Don't call this callback again. |
| 680 | } |
| 681 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 682 | void UpdateAttempter::ScheduleProcessingStart() { |
| 683 | LOG(INFO) << "Scheduling an action processor start."; |
| 684 | start_action_processor_ = false; |
| 685 | g_idle_add(&StaticStartProcessing, this); |
| 686 | } |
| 687 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 688 | bool UpdateAttempter::ManagePriorityCallback() { |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 689 | SetPriority(utils::kProcessPriorityNormal); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 690 | manage_priority_source_ = NULL; |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 691 | return false; // Destroy the timeout source. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 694 | void UpdateAttempter::DisableDeltaUpdateIfNeeded() { |
| 695 | int64_t delta_failures; |
| 696 | if (omaha_request_params_.delta_okay && |
| 697 | prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) && |
| 698 | delta_failures >= kMaxDeltaUpdateFailures) { |
| 699 | LOG(WARNING) << "Too many delta update failures, forcing full update."; |
| 700 | omaha_request_params_.delta_okay = false; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | void UpdateAttempter::MarkDeltaUpdateFailure() { |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 705 | // Don't try to resume a failed delta update. |
| 706 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 707 | int64_t delta_failures; |
| 708 | if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) || |
| 709 | delta_failures < 0) { |
| 710 | delta_failures = 0; |
| 711 | } |
| 712 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures); |
| 713 | } |
| 714 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 715 | void UpdateAttempter::SetupDownload() { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 716 | MultiRangeHttpFetcher* fetcher = |
| 717 | dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher()); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 718 | fetcher->ClearRanges(); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 719 | if (response_handler_action_->install_plan().is_resume) { |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 720 | // Resuming an update so fetch the update manifest metadata first. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 721 | int64_t manifest_metadata_size = 0; |
| 722 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 723 | fetcher->AddRange(0, manifest_metadata_size); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 724 | // If there're remaining unprocessed data blobs, fetch them. Be careful not |
| 725 | // to request data beyond the end of the payload to avoid 416 HTTP response |
| 726 | // error codes. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 727 | int64_t next_data_offset = 0; |
| 728 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 729 | uint64_t resume_offset = manifest_metadata_size + next_data_offset; |
| 730 | if (resume_offset < response_handler_action_->install_plan().size) { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 731 | fetcher->AddRange(resume_offset); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 732 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 733 | } else { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 734 | fetcher->AddRange(0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 735 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 738 | void UpdateAttempter::PingOmaha() { |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 739 | if (!processor_->IsRunning()) { |
| 740 | shared_ptr<OmahaRequestAction> ping_action( |
| 741 | new OmahaRequestAction(prefs_, |
| 742 | omaha_request_params_, |
| 743 | NULL, |
| 744 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 745 | true)); |
| 746 | actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action)); |
| 747 | processor_->set_delegate(NULL); |
| 748 | processor_->EnqueueAction(ping_action.get()); |
| 749 | // Call StartProcessing() synchronously here to avoid any race conditions |
| 750 | // caused by multiple outstanding ping Omaha requests. If we call |
| 751 | // StartProcessing() asynchronously, the device can be suspended before we |
| 752 | // get a chance to callback to StartProcessing(). When the device resumes |
| 753 | // (assuming the device sleeps longer than the next update check period), |
| 754 | // StartProcessing() is called back and at the same time, the next update |
| 755 | // check is fired which eventually invokes StartProcessing(). A crash |
| 756 | // can occur because StartProcessing() checks to make sure that the |
| 757 | // processor is idle which it isn't due to the two concurrent ping Omaha |
| 758 | // requests. |
| 759 | processor_->StartProcessing(); |
| 760 | } else { |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 761 | LOG(WARNING) << "Action processor running, Omaha ping suppressed."; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 762 | } |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 763 | |
| 764 | // Update the status which will schedule the next update check |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 765 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 766 | kUpdateNoticeUnspecified); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 769 | } // namespace chromeos_update_engine |