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 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 17 | #include <base/eintr_wrapper.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 18 | #include <base/rand_util.h> |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 19 | #include <base/string_util.h> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 20 | #include <glib.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 21 | #include <metrics/metrics_library.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 22 | #include <policy/libpolicy.h> |
| 23 | #include <policy/device_policy.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 24 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 25 | #include "update_engine/certificate_checker.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 26 | #include "update_engine/dbus_service.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 27 | #include "update_engine/download_action.h" |
| 28 | #include "update_engine/filesystem_copier_action.h" |
| 29 | #include "update_engine/libcurl_http_fetcher.h" |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 30 | #include "update_engine/multi_range_http_fetcher.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 31 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 32 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 33 | #include "update_engine/omaha_response_handler_action.h" |
| 34 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 35 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 36 | #include "update_engine/subprocess.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 37 | #include "update_engine/update_check_scheduler.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 38 | |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 39 | using base::TimeDelta; |
| 40 | using base::TimeTicks; |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 41 | using google::protobuf::NewPermanentCallback; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 42 | using std::make_pair; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 43 | using std::tr1::shared_ptr; |
| 44 | using std::string; |
| 45 | using std::vector; |
| 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 49 | const int UpdateAttempter::kMaxDeltaUpdateFailures = 3; |
| 50 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 51 | // TODO(garnold) this is currently an arbitrary address and will change based on |
| 52 | // discussion about the actual test lab configuration. |
| 53 | const char* const UpdateAttempter::kTestUpdateUrl("https://10.0.0.1/update"); |
| 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; |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 60 | |
| 61 | // Names of udev properties that are linked to the GPIO chip device and identify |
| 62 | // the two dutflag GPIOs on different boards. |
| 63 | const char kIdGpioDutflaga[] = "ID_GPIO_DUTFLAGA"; |
| 64 | const char kIdGpioDutflagb[] = "ID_GPIO_DUTFLAGB"; |
| 65 | |
| 66 | // Scoped closer for udev and udev_enumerate objects. |
| 67 | // TODO(garnold) chromium-os:26934: it would be nice to generalize the different |
| 68 | // ScopedFooCloser implementations in update engine using a single template. |
| 69 | class ScopedUdevCloser { |
| 70 | public: |
| 71 | explicit ScopedUdevCloser(udev **udev_p) : udev_p_(udev_p) {} |
| 72 | ~ScopedUdevCloser() { |
| 73 | if (udev_p_ && *udev_p_) { |
| 74 | udev_unref(*udev_p_); |
| 75 | *udev_p_ = NULL; |
| 76 | } |
| 77 | } |
| 78 | private: |
| 79 | struct udev **udev_p_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(ScopedUdevCloser); |
| 82 | }; |
| 83 | |
| 84 | class ScopedUdevEnumerateCloser { |
| 85 | public: |
| 86 | explicit ScopedUdevEnumerateCloser(udev_enumerate **udev_enum_p) : |
| 87 | udev_enum_p_(udev_enum_p) {} |
| 88 | ~ScopedUdevEnumerateCloser() { |
| 89 | if (udev_enum_p_ && *udev_enum_p_) { |
| 90 | udev_enumerate_unref(*udev_enum_p_); |
| 91 | *udev_enum_p_ = NULL; |
| 92 | } |
| 93 | } |
| 94 | private: |
| 95 | struct udev_enumerate **udev_enum_p_; |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(ScopedUdevEnumerateCloser); |
| 98 | }; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 99 | } // namespace {} |
| 100 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 101 | const char* UpdateStatusToString(UpdateStatus status) { |
| 102 | switch (status) { |
| 103 | case UPDATE_STATUS_IDLE: |
| 104 | return "UPDATE_STATUS_IDLE"; |
| 105 | case UPDATE_STATUS_CHECKING_FOR_UPDATE: |
| 106 | return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; |
| 107 | case UPDATE_STATUS_UPDATE_AVAILABLE: |
| 108 | return "UPDATE_STATUS_UPDATE_AVAILABLE"; |
| 109 | case UPDATE_STATUS_DOWNLOADING: |
| 110 | return "UPDATE_STATUS_DOWNLOADING"; |
| 111 | case UPDATE_STATUS_VERIFYING: |
| 112 | return "UPDATE_STATUS_VERIFYING"; |
| 113 | case UPDATE_STATUS_FINALIZING: |
| 114 | return "UPDATE_STATUS_FINALIZING"; |
| 115 | case UPDATE_STATUS_UPDATED_NEED_REBOOT: |
| 116 | return "UPDATE_STATUS_UPDATED_NEED_REBOOT"; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 117 | case UPDATE_STATUS_REPORTING_ERROR_EVENT: |
| 118 | return "UPDATE_STATUS_REPORTING_ERROR_EVENT"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 119 | default: |
| 120 | return "unknown status"; |
| 121 | } |
| 122 | } |
| 123 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 124 | // Turns a generic kActionCodeError to a generic error code specific |
| 125 | // to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is |
| 126 | // not kActionCodeError, or the action is not matched, returns |code| |
| 127 | // unchanged. |
| 128 | ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 129 | ActionExitCode code) { |
| 130 | if (code != kActionCodeError) |
| 131 | return code; |
| 132 | |
| 133 | const string type = action->Type(); |
| 134 | if (type == OmahaRequestAction::StaticType()) |
| 135 | return kActionCodeOmahaRequestError; |
| 136 | if (type == OmahaResponseHandlerAction::StaticType()) |
| 137 | return kActionCodeOmahaResponseHandlerError; |
| 138 | if (type == FilesystemCopierAction::StaticType()) |
| 139 | return kActionCodeFilesystemCopierError; |
| 140 | if (type == PostinstallRunnerAction::StaticType()) |
| 141 | return kActionCodePostinstallRunnerError; |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 142 | |
| 143 | return code; |
| 144 | } |
| 145 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 146 | UpdateAttempter::UpdateAttempter(PrefsInterface* prefs, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 147 | MetricsLibraryInterface* metrics_lib, |
| 148 | DbusGlibInterface* dbus_iface) |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 149 | : processor_(new ActionProcessor()), |
| 150 | dbus_service_(NULL), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 151 | prefs_(prefs), |
| 152 | metrics_lib_(metrics_lib), |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 153 | update_check_scheduler_(NULL), |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 154 | fake_update_success_(false), |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 155 | http_response_code_(0), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 156 | priority_(utils::kProcessPriorityNormal), |
| 157 | manage_priority_source_(NULL), |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 158 | download_active_(false), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 159 | status_(UPDATE_STATUS_IDLE), |
| 160 | download_progress_(0.0), |
| 161 | last_checked_time_(0), |
| 162 | new_version_("0.0.0.0"), |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 163 | new_size_(0), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 164 | proxy_manual_checks_(0), |
| 165 | obeying_proxies_(true), |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 166 | chrome_proxy_resolver_(dbus_iface), |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 167 | updated_boot_flags_(false), |
| 168 | update_boot_flags_running_(false), |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 169 | start_action_processor_(false), |
| 170 | policy_provider_(NULL) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 171 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 172 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 173 | } |
| 174 | |
| 175 | UpdateAttempter::~UpdateAttempter() { |
| 176 | CleanupPriorityManagement(); |
| 177 | } |
| 178 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 179 | void UpdateAttempter::Update(const string& app_version, |
| 180 | const string& omaha_url, |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 181 | bool obey_proxies, |
| 182 | bool interactive) { |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 183 | chrome_proxy_resolver_.Init(); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 184 | fake_update_success_ = false; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 185 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 186 | // Although we have applied an update, we still want to ping Omaha |
| 187 | // to ensure the number of active statistics is accurate. |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 188 | 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] | 189 | << "reboot, we'll ping Omaha instead"; |
| 190 | PingOmaha(); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | if (status_ != UPDATE_STATUS_IDLE) { |
| 194 | // Update in progress. Do nothing |
| 195 | return; |
| 196 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 197 | http_response_code_ = 0; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 198 | |
| 199 | // Lazy initialize the policy provider, or reload the latest policy data. |
| 200 | if (!policy_provider_.get()) { |
| 201 | policy_provider_.reset(new policy::PolicyProvider()); |
| 202 | } else { |
| 203 | policy_provider_->Reload(); |
| 204 | } |
| 205 | |
| 206 | // If the release_track is specified by policy, that takes precedence. |
| 207 | string release_track; |
| 208 | if (policy_provider_->device_policy_is_loaded()) |
| 209 | policy_provider_->GetDevicePolicy().GetReleaseChannel(&release_track); |
| 210 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 211 | // Force alternate default address for automated test case, based on GPIO |
| 212 | // signal. We replicate the URL string so as not to overwrite the argument. |
| 213 | string omaha_url_to_use = omaha_url; |
| 214 | if (omaha_url_to_use.empty()) { |
| 215 | bool dutflaga_gpio_status; |
| 216 | if (GetDutflagaGpio(&dutflaga_gpio_status)) { |
| 217 | LOG(INFO) << "dutflaga GPIO status: " |
| 218 | << (dutflaga_gpio_status ? "on" : "off"); |
| 219 | |
| 220 | // The dut_flaga GPIO is actually signaled when in the 'off' position. |
| 221 | if (!dutflaga_gpio_status) { |
| 222 | LOG(INFO) << "using alternative server address: " << kTestUpdateUrl; |
| 223 | omaha_url_to_use = kTestUpdateUrl; |
| 224 | } |
| 225 | } else { |
| 226 | LOG(ERROR) << "reading dutflaga GPIO status failed"; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (!omaha_request_params_.Init(app_version, omaha_url_to_use, |
| 231 | release_track)) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 232 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
| 233 | return; |
| 234 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 235 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 236 | obeying_proxies_ = true; |
| 237 | if (obey_proxies || proxy_manual_checks_ == 0) { |
| 238 | LOG(INFO) << "forced to obey proxies"; |
| 239 | // If forced to obey proxies, every 20th request will not use proxies |
| 240 | proxy_manual_checks_++; |
| 241 | LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_; |
| 242 | if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) { |
| 243 | proxy_manual_checks_ = 0; |
| 244 | obeying_proxies_ = false; |
| 245 | } |
| 246 | } else if (base::RandInt(0, 4) == 0) { |
| 247 | obeying_proxies_ = false; |
| 248 | } |
| 249 | LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update " |
| 250 | "check we are ignoring the proxy settings and using " |
| 251 | "direct connections."; |
| 252 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 253 | DisableDeltaUpdateIfNeeded(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 254 | CHECK(!processor_->IsRunning()); |
| 255 | processor_->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 256 | |
| 257 | // Actions: |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 258 | LibcurlHttpFetcher* update_check_fetcher = |
| 259 | new LibcurlHttpFetcher(GetProxyResolver()); |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 260 | // Try harder to connect to the network, esp when not interactive. |
| 261 | // See comment in libcurl_http_fetcher.cc. |
| 262 | update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 263 | update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 264 | shared_ptr<OmahaRequestAction> update_check_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 265 | new OmahaRequestAction(prefs_, |
| 266 | omaha_request_params_, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 267 | NULL, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 268 | update_check_fetcher, // passes ownership |
| 269 | false)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 270 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 271 | new OmahaResponseHandlerAction(prefs_)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 272 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 273 | new FilesystemCopierAction(false, false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 274 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 275 | new FilesystemCopierAction(true, false)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 276 | shared_ptr<OmahaRequestAction> download_started_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 277 | new OmahaRequestAction(prefs_, |
| 278 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 279 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 280 | OmahaEvent::kTypeUpdateDownloadStarted), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 281 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 282 | false)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 283 | LibcurlHttpFetcher* download_fetcher = |
| 284 | new LibcurlHttpFetcher(GetProxyResolver()); |
| 285 | download_fetcher->set_check_certificate(CertificateChecker::kDownload); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 286 | shared_ptr<DownloadAction> download_action( |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 287 | new DownloadAction(prefs_, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 288 | new MultiRangeHttpFetcher( |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 289 | download_fetcher))); // passes ownership |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 290 | shared_ptr<OmahaRequestAction> download_finished_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 291 | new OmahaRequestAction(prefs_, |
| 292 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 293 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 294 | OmahaEvent::kTypeUpdateDownloadFinished), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 295 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 296 | false)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 297 | shared_ptr<FilesystemCopierAction> filesystem_verifier_action( |
| 298 | new FilesystemCopierAction(false, true)); |
| 299 | shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action( |
| 300 | new FilesystemCopierAction(true, true)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 301 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
| 302 | new PostinstallRunnerAction); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 303 | shared_ptr<OmahaRequestAction> update_complete_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 304 | new OmahaRequestAction(prefs_, |
| 305 | omaha_request_params_, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 306 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 307 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 308 | false)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 309 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 310 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 311 | response_handler_action_ = response_handler_action; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 312 | download_action_ = download_action; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 313 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 314 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 315 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 316 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 317 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 318 | kernel_filesystem_copier_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 319 | actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 320 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 321 | actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 322 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
| 323 | actions_.push_back(shared_ptr<AbstractAction>( |
| 324 | kernel_filesystem_verifier_action)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 325 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 326 | actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 327 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 328 | // Enqueue the actions |
| 329 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 330 | it != actions_.end(); ++it) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 331 | processor_->EnqueueAction(it->get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Bond them together. We have to use the leaf-types when calling |
| 335 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 336 | BondActions(update_check_action.get(), |
| 337 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 338 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 339 | filesystem_copier_action.get()); |
| 340 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 341 | kernel_filesystem_copier_action.get()); |
| 342 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 343 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 344 | BondActions(download_action.get(), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 345 | filesystem_verifier_action.get()); |
| 346 | BondActions(filesystem_verifier_action.get(), |
| 347 | kernel_filesystem_verifier_action.get()); |
| 348 | BondActions(kernel_filesystem_verifier_action.get(), |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 349 | postinstall_runner_action.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 350 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 351 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 352 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 353 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 354 | // before any update processing starts. |
| 355 | start_action_processor_ = true; |
| 356 | UpdateBootFlags(); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 359 | void UpdateAttempter::CheckForUpdate(const string& app_version, |
| 360 | const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 361 | if (status_ != UPDATE_STATUS_IDLE) { |
| 362 | LOG(INFO) << "Check for update requested, but status is " |
| 363 | << UpdateStatusToString(status_) << ", so not checking."; |
| 364 | return; |
| 365 | } |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 366 | Update(app_version, omaha_url, true, true); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 369 | bool UpdateAttempter::RebootIfNeeded() { |
| 370 | if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 371 | LOG(INFO) << "Reboot requested, but status is " |
| 372 | << UpdateStatusToString(status_) << ", so not rebooting."; |
| 373 | return false; |
| 374 | } |
| 375 | TEST_AND_RETURN_FALSE(utils::Reboot()); |
| 376 | return true; |
| 377 | } |
| 378 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 379 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 380 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 381 | ActionExitCode code) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 382 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 383 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 384 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 385 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 386 | // Reset process priority back to normal. |
| 387 | CleanupPriorityManagement(); |
| 388 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 389 | if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 390 | LOG(INFO) << "Error event sent."; |
| 391 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 392 | if (!fake_update_success_) { |
| 393 | return; |
| 394 | } |
| 395 | LOG(INFO) << "Booted from FW B and tried to install new firmware, " |
| 396 | "so requesting reboot from user."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 399 | if (code == kActionCodeSuccess) { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 400 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 401 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 402 | prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 403 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 404 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 405 | |
| 406 | // Report the time it took to update the system. |
| 407 | int64_t update_time = time(NULL) - last_checked_time_; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 408 | if (!fake_update_success_) |
| 409 | metrics_lib_->SendToUMA("Installer.UpdateTime", |
| 410 | static_cast<int>(update_time), // sample |
| 411 | 1, // min = 1 second |
| 412 | 20 * 60, // max = 20 minutes |
| 413 | 50); // buckets |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 414 | return; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 415 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 416 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 417 | if (ScheduleErrorEventAction()) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 418 | return; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 419 | } |
| 420 | LOG(INFO) << "No update."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 421 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 425 | // Reset process priority back to normal. |
| 426 | CleanupPriorityManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 427 | download_progress_ = 0.0; |
| 428 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 429 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 430 | error_event_.reset(NULL); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Called whenever an action has finished processing, either successfully |
| 434 | // or otherwise. |
| 435 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 436 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 437 | ActionExitCode code) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 438 | // Reset download progress regardless of whether or not the download |
| 439 | // action succeeded. Also, get the response code from HTTP request |
| 440 | // actions (update download as well as the initial update check |
| 441 | // actions). |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 442 | const string type = action->Type(); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 443 | if (type == DownloadAction::StaticType()) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 444 | download_progress_ = 0.0; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 445 | DownloadAction* download_action = dynamic_cast<DownloadAction*>(action); |
| 446 | http_response_code_ = download_action->GetHTTPResponseCode(); |
| 447 | } else if (type == OmahaRequestAction::StaticType()) { |
| 448 | OmahaRequestAction* omaha_request_action = |
| 449 | dynamic_cast<OmahaRequestAction*>(action); |
| 450 | // If the request is not an event, then it's the update-check. |
| 451 | if (!omaha_request_action->IsEvent()) { |
| 452 | http_response_code_ = omaha_request_action->GetHTTPResponseCode(); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 453 | // Forward the server-dictated poll interval to the update check |
| 454 | // scheduler, if any. |
| 455 | if (update_check_scheduler_) { |
| 456 | update_check_scheduler_->set_poll_interval( |
| 457 | omaha_request_action->GetOutputObject().poll_interval); |
| 458 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 461 | if (code != kActionCodeSuccess) { |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 462 | // If the current state is at or past the download phase, count the failure |
| 463 | // in case a switch to full update becomes necessary. Ignore network |
| 464 | // transfer timeouts and failures. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 465 | if (status_ >= UPDATE_STATUS_DOWNLOADING && |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 466 | code != kActionCodeDownloadTransferError) { |
| 467 | MarkDeltaUpdateFailure(); |
| 468 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 469 | // On failure, schedule an error event to be sent to Omaha. |
| 470 | CreatePendingErrorEvent(action, code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 471 | return; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 472 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 473 | // Find out which action completed. |
| 474 | if (type == OmahaResponseHandlerAction::StaticType()) { |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 475 | // Note that the status will be updated to DOWNLOADING when some bytes get |
| 476 | // actually downloaded from the server and the BytesReceived callback is |
| 477 | // invoked. This avoids notifying the user that a download has started in |
| 478 | // cases when the server and the client are unable to initiate the download. |
| 479 | CHECK(action == response_handler_action_.get()); |
| 480 | const InstallPlan& plan = response_handler_action_->install_plan(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 481 | last_checked_time_ = time(NULL); |
| 482 | // TODO(adlr): put version in InstallPlan |
| 483 | new_version_ = "0.0.0.0"; |
| 484 | new_size_ = plan.size; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 485 | SetupDownload(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 486 | SetupPriorityManagement(); |
Darin Petkov | b00bccc | 2010-10-26 14:13:08 -0700 | [diff] [blame] | 487 | SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 488 | } else if (type == DownloadAction::StaticType()) { |
| 489 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING); |
| 490 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Stop updating. An attempt will be made to record status to the disk |
| 494 | // so that updates can be resumed later. |
| 495 | void UpdateAttempter::Terminate() { |
| 496 | // TODO(adlr): implement this method. |
| 497 | NOTIMPLEMENTED(); |
| 498 | } |
| 499 | |
| 500 | // Try to resume from a previously Terminate()d update. |
| 501 | void UpdateAttempter::ResumeUpdating() { |
| 502 | // TODO(adlr): implement this method. |
| 503 | NOTIMPLEMENTED(); |
| 504 | } |
| 505 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 506 | void UpdateAttempter::SetDownloadStatus(bool active) { |
| 507 | download_active_ = active; |
| 508 | LOG(INFO) << "Download status: " << (active ? "active" : "inactive"); |
| 509 | } |
| 510 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 511 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 512 | if (!download_active_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 513 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 514 | return; |
| 515 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 516 | double progress = static_cast<double>(bytes_received) / |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 517 | static_cast<double>(total); |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 518 | // Self throttle based on progress. Also send notifications if |
| 519 | // progress is too slow. |
| 520 | const double kDeltaPercent = 0.01; // 1% |
| 521 | if (status_ != UPDATE_STATUS_DOWNLOADING || |
| 522 | bytes_received == total || |
| 523 | progress - download_progress_ >= kDeltaPercent || |
| 524 | TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) { |
| 525 | download_progress_ = progress; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 526 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 527 | } |
| 528 | } |
| 529 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 530 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 531 | double* progress, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 532 | string* current_operation, |
| 533 | string* new_version, |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 534 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 535 | *last_checked_time = last_checked_time_; |
| 536 | *progress = download_progress_; |
| 537 | *current_operation = UpdateStatusToString(status_); |
| 538 | *new_version = new_version_; |
| 539 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 543 | // Discovers the dut_flag GPIO identified by |gpio_dutflag_str| and stores the |
| 544 | // full device name in |dutflag_dev_name_p|. The function uses an open libudev |
| 545 | // instance |udev|. Returns zero on success, -1 otherwise. |
| 546 | bool UpdateAttempter::GetDutflagGpioDevName(struct udev* udev, |
| 547 | const string& gpio_dutflag_str, |
| 548 | string* dutflag_dev_name_p) { |
| 549 | CHECK(udev && dutflag_dev_name_p); |
| 550 | |
| 551 | struct udev_enumerate* udev_enum = NULL; |
| 552 | int num_gpio_dutflags = 0; |
| 553 | const string gpio_dutflag_pattern = "*" + gpio_dutflag_str; |
| 554 | int ret; |
| 555 | |
| 556 | // Initialize udev enumerate context and closer. |
| 557 | if (!(udev_enum = udev_enumerate_new(udev))) { |
| 558 | LOG(ERROR) << "failed to obtain udev enumerate context"; |
| 559 | return false; |
| 560 | } |
| 561 | ScopedUdevEnumerateCloser udev_enum_closer(&udev_enum); |
| 562 | |
| 563 | // Populate filters for find an initialized GPIO chip. |
| 564 | if ((ret = udev_enumerate_add_match_subsystem(udev_enum, "gpio")) || |
| 565 | (ret = udev_enumerate_add_match_sysname(udev_enum, |
| 566 | gpio_dutflag_pattern.c_str()))) { |
| 567 | LOG(ERROR) << "failed to initialize udev enumerate context (" << ret << ")"; |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | // Obtain list of matching devices. |
| 572 | if ((ret = udev_enumerate_scan_devices(udev_enum))) { |
| 573 | LOG(ERROR) << "udev enumerate context scan failed (error code " |
| 574 | << ret << ")"; |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | // Iterate over matching devices, obtain GPIO dut_flaga identifier. |
| 579 | struct udev_list_entry* list_entry; |
| 580 | udev_list_entry_foreach(list_entry, |
| 581 | udev_enumerate_get_list_entry(udev_enum)) { |
| 582 | // Make sure we're not enumerating more than one device. |
| 583 | num_gpio_dutflags++; |
| 584 | if (num_gpio_dutflags > 1) { |
| 585 | LOG(WARNING) << |
| 586 | "enumerated multiple dutflag GPIOs, ignoring this one"; |
| 587 | continue; |
| 588 | } |
| 589 | |
| 590 | // Obtain device name. |
| 591 | const char* dev_name = udev_list_entry_get_name(list_entry); |
| 592 | if (!dev_name) { |
| 593 | LOG(WARNING) << "enumerated device has a null name string, skipping"; |
| 594 | continue; |
| 595 | } |
| 596 | |
| 597 | // Obtain device object. |
| 598 | struct udev_device* dev = udev_device_new_from_syspath(udev, dev_name); |
| 599 | if (!dev) { |
| 600 | LOG(WARNING) << |
| 601 | "obtained a null device object for enumerated device, skipping"; |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | // Obtain device syspath. |
| 606 | const char* dev_syspath = udev_device_get_syspath(dev); |
| 607 | if (dev_syspath) { |
| 608 | LOG(INFO) << "obtained device syspath: " << dev_syspath; |
| 609 | *dutflag_dev_name_p = dev_syspath; |
| 610 | } else { |
| 611 | LOG(WARNING) << "could not obtain device syspath"; |
| 612 | } |
| 613 | |
| 614 | udev_device_unref(dev); |
| 615 | } |
| 616 | |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | // Discovers and stores the device names of the two dut_flag GPIOs. Returns zero |
| 621 | // upon success, -1 otherwise. |
| 622 | bool UpdateAttempter::GetDutflagGpioDevNames(string* dutflaga_dev_name_p, |
| 623 | string* dutflagb_dev_name_p) { |
| 624 | if (!(dutflaga_dev_name_p || dutflagb_dev_name_p)) |
| 625 | return true; // No output pointers, nothing to do. |
| 626 | |
| 627 | string gpio_dutflaga_str, gpio_dutflagb_str; |
| 628 | |
| 629 | if (dutflaga_dev_name_.empty() || dutflagb_dev_name_.empty()) { |
| 630 | struct udev* udev = NULL; |
| 631 | struct udev_enumerate* udev_enum = NULL; |
| 632 | int num_gpio_chips = 0; |
| 633 | const char* id_gpio_dutflaga = NULL; |
| 634 | const char* id_gpio_dutflagb = NULL; |
| 635 | int ret; |
| 636 | |
| 637 | LOG(INFO) << "begin discovery of dut_flaga/b devices"; |
| 638 | |
| 639 | // Obtain libudev instance and closer. |
| 640 | if (!(udev = udev_new())) { |
| 641 | LOG(ERROR) << "failed to obtain libudev instance"; |
| 642 | return false; |
| 643 | } |
| 644 | ScopedUdevCloser udev_closer(&udev); |
| 645 | |
| 646 | // Initialize a udev enumerate object and closer with a bounded lifespan. |
| 647 | { |
| 648 | if (!(udev_enum = udev_enumerate_new(udev))) { |
| 649 | LOG(ERROR) << "failed to obtain udev enumerate context"; |
| 650 | return false; |
| 651 | } |
| 652 | ScopedUdevEnumerateCloser udev_enum_closer(&udev_enum); |
| 653 | |
| 654 | // Populate filters for find an initialized GPIO chip. |
| 655 | if ((ret = udev_enumerate_add_match_subsystem(udev_enum, "gpio")) || |
| 656 | (ret = udev_enumerate_add_match_sysname(udev_enum, "gpiochip*")) || |
| 657 | (ret = udev_enumerate_add_match_property(udev_enum, |
| 658 | kIdGpioDutflaga, "*")) || |
| 659 | (ret = udev_enumerate_add_match_property(udev_enum, |
| 660 | kIdGpioDutflagb, "*"))) { |
| 661 | LOG(ERROR) << "failed to initialize udev enumerate context (" |
| 662 | << ret << ")"; |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | // Obtain list of matching devices. |
| 667 | if ((ret = udev_enumerate_scan_devices(udev_enum))) { |
| 668 | LOG(ERROR) << "udev enumerate context scan failed (" << ret << ")"; |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | // Iterate over matching devices, obtain GPIO dut_flaga identifier. |
| 673 | struct udev_list_entry* list_entry; |
| 674 | udev_list_entry_foreach(list_entry, |
| 675 | udev_enumerate_get_list_entry(udev_enum)) { |
| 676 | // Make sure we're not enumerating more than one device. |
| 677 | num_gpio_chips++; |
| 678 | if (num_gpio_chips > 1) { |
| 679 | LOG(WARNING) << "enumerated multiple GPIO chips, ignoring this one"; |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | // Obtain device name. |
| 684 | const char* dev_name = udev_list_entry_get_name(list_entry); |
| 685 | if (!dev_name) { |
| 686 | LOG(WARNING) << "enumerated device has a null name string, skipping"; |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | // Obtain device object. |
| 691 | struct udev_device* dev = udev_device_new_from_syspath(udev, dev_name); |
| 692 | if (!dev) { |
| 693 | LOG(WARNING) << |
| 694 | "obtained a null device object for enumerated device, skipping"; |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | // Obtain dut_flaga/b identifiers. |
| 699 | id_gpio_dutflaga = |
| 700 | udev_device_get_property_value(dev, kIdGpioDutflaga); |
| 701 | id_gpio_dutflagb = |
| 702 | udev_device_get_property_value(dev, kIdGpioDutflagb); |
| 703 | if (id_gpio_dutflaga && id_gpio_dutflagb) { |
| 704 | LOG(INFO) << "found dut_flaga/b identifiers: a=" << id_gpio_dutflaga |
| 705 | << " b=" << id_gpio_dutflagb; |
| 706 | |
| 707 | gpio_dutflaga_str = id_gpio_dutflaga; |
| 708 | gpio_dutflagb_str = id_gpio_dutflagb; |
| 709 | } else { |
| 710 | LOG(ERROR) << "GPIO chip missing dut_flaga/b properties"; |
| 711 | } |
| 712 | |
| 713 | udev_device_unref(dev); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // Obtain dut_flaga, reusing the same udev instance. |
| 718 | if (dutflaga_dev_name_.empty() && !gpio_dutflaga_str.empty()) { |
| 719 | LOG(INFO) << "discovering device for GPIO dut_flaga "; |
| 720 | if (!GetDutflagGpioDevName(udev, gpio_dutflaga_str, |
| 721 | &dutflaga_dev_name_)) { |
| 722 | LOG(ERROR) << "discovery of dut_flaga GPIO device failed"; |
| 723 | return false; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // Now obtain dut_flagb. |
| 728 | if (dutflagb_dev_name_.empty() && !gpio_dutflagb_str.empty()) { |
| 729 | LOG(INFO) << "discovering device for GPIO dut_flagb"; |
| 730 | if (!GetDutflagGpioDevName(udev, gpio_dutflagb_str, |
| 731 | &dutflagb_dev_name_)) { |
| 732 | LOG(ERROR) << "discovery of dut_flagb GPIO device failed"; |
| 733 | return false; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | LOG(INFO) << "end discovery of dut_flaga/b devices"; |
| 738 | } |
| 739 | |
| 740 | // Write cached GPIO dutflag(s) to output strings. |
| 741 | if (dutflaga_dev_name_p) |
| 742 | *dutflaga_dev_name_p = dutflaga_dev_name_; |
| 743 | if (dutflagb_dev_name_p) |
| 744 | *dutflagb_dev_name_p = dutflagb_dev_name_; |
| 745 | |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | // Reads the value of the dut_flaga GPIO and stores it in |status_p|. Returns |
| 750 | // true upon success, false otherwise (which also means that the GPIO value was |
| 751 | // not stored and should not be used). |
| 752 | bool UpdateAttempter::GetDutflagaGpio(bool* status_p) { |
| 753 | // Obtain GPIO device file name. |
| 754 | string dutflaga_dev_name; |
| 755 | GetDutflagGpioDevNames(&dutflaga_dev_name, NULL); |
| 756 | if (dutflaga_dev_name.empty()) { |
| 757 | LOG(WARNING) << "could not find dutflaga GPIO device"; |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | // Open device for reading. |
| 762 | string dutflaga_value_dev_name = dutflaga_dev_name + "/value"; |
Gilad Arnold | f960911 | 2012-02-29 11:38:47 -0800 | [diff] [blame] | 763 | int dutflaga_fd = HANDLE_EINTR(open(dutflaga_value_dev_name.c_str(), 0)); |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 764 | if (dutflaga_fd < 0) { |
| 765 | PLOG(ERROR) << "opening dutflaga GPIO device file failed"; |
| 766 | return false; |
| 767 | } |
Gilad Arnold | f960911 | 2012-02-29 11:38:47 -0800 | [diff] [blame] | 768 | ScopedEintrSafeFdCloser dutflaga_fd_closer(&dutflaga_fd); |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 769 | |
| 770 | // Read the dut_flaga GPIO signal. We attempt to read more than---but expect |
| 771 | // to receive exactly---two characters: a '0' or '1', and a newline. This is |
| 772 | // to ensure that the GPIO device returns a legible result. |
| 773 | char buf[3]; |
Gilad Arnold | f960911 | 2012-02-29 11:38:47 -0800 | [diff] [blame] | 774 | int ret = HANDLE_EINTR(read(dutflaga_fd, buf, 3)); |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 775 | if (ret != 2) { |
| 776 | if (ret < 0) |
| 777 | PLOG(ERROR) << "reading dutflaga GPIO status failed"; |
| 778 | else |
| 779 | LOG(ERROR) << "read more than one byte (" << ret << ")"; |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | // Identify and write GPIO status. |
| 784 | char c = buf[0]; |
| 785 | if ((c == '0' || c == '1') && buf[1] == '\n') { |
| 786 | *status_p = (c == '1'); |
| 787 | } else { |
| 788 | buf[2] = '\0'; |
| 789 | LOG(ERROR) << "read unexpected value from dutflaga GPIO: " << buf; |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | return true; |
| 794 | } |
| 795 | |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 796 | void UpdateAttempter::UpdateBootFlags() { |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 797 | if (update_boot_flags_running_) { |
| 798 | LOG(INFO) << "Update boot flags running, nothing to do."; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 799 | return; |
| 800 | } |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 801 | if (updated_boot_flags_) { |
| 802 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 803 | if (start_action_processor_) { |
| 804 | ScheduleProcessingStart(); |
| 805 | } |
| 806 | return; |
| 807 | } |
| 808 | // This is purely best effort. Failures should be logged by Subprocess. Run |
| 809 | // the script asynchronously to avoid blocking the event loop regardless of |
| 810 | // the script runtime. |
| 811 | update_boot_flags_running_ = true; |
| 812 | LOG(INFO) << "Updating boot flags..."; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 813 | vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel"); |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 814 | if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) { |
| 815 | CompleteUpdateBootFlags(1); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | void UpdateAttempter::CompleteUpdateBootFlags(int return_code) { |
| 820 | update_boot_flags_running_ = false; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 821 | updated_boot_flags_ = true; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 822 | if (start_action_processor_) { |
| 823 | ScheduleProcessingStart(); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | void UpdateAttempter::StaticCompleteUpdateBootFlags( |
| 828 | int return_code, |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 829 | const string& output, |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 830 | void* p) { |
| 831 | reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code); |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 834 | void UpdateAttempter::BroadcastStatus() { |
| 835 | if (!dbus_service_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 836 | return; |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 837 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 838 | last_notify_time_ = TimeTicks::Now(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 839 | update_engine_service_emit_status_update( |
| 840 | dbus_service_, |
| 841 | last_checked_time_, |
| 842 | download_progress_, |
| 843 | UpdateStatusToString(status_), |
| 844 | new_version_.c_str(), |
| 845 | new_size_); |
| 846 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 847 | |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 848 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { |
| 849 | status_ = status; |
| 850 | if (update_check_scheduler_) { |
| 851 | update_check_scheduler_->SetUpdateStatus(status_); |
| 852 | } |
| 853 | BroadcastStatus(); |
| 854 | } |
| 855 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 856 | void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, |
| 857 | ActionExitCode code) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 858 | if (error_event_.get()) { |
| 859 | // This shouldn't really happen. |
| 860 | LOG(WARNING) << "There's already an existing pending error event."; |
| 861 | return; |
| 862 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 863 | |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 864 | // For now assume that a generic Omaha response action failure means that |
| 865 | // there's no update so don't send an event. Also, double check that the |
| 866 | // failure has not occurred while sending an error event -- in which case |
| 867 | // don't schedule another. This shouldn't really happen but just in case... |
| 868 | if ((action->Type() == OmahaResponseHandlerAction::StaticType() && |
| 869 | code == kActionCodeError) || |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 870 | status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | code = GetErrorCodeForAction(action, code); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 875 | fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 876 | |
| 877 | // Apply the bit modifiers to the error code. |
| 878 | if (!utils::IsNormalBootMode()) { |
| 879 | code = static_cast<ActionExitCode>(code | kActionCodeBootModeFlag); |
| 880 | } |
| 881 | if (response_handler_action_.get() && |
| 882 | response_handler_action_->install_plan().is_resume) { |
| 883 | code = static_cast<ActionExitCode>(code | kActionCodeResumedFlag); |
| 884 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 885 | error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 886 | OmahaEvent::kResultError, |
| 887 | code)); |
| 888 | } |
| 889 | |
| 890 | bool UpdateAttempter::ScheduleErrorEventAction() { |
| 891 | if (error_event_.get() == NULL) |
| 892 | return false; |
| 893 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 894 | LOG(INFO) << "Update failed -- reporting the error event."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 895 | shared_ptr<OmahaRequestAction> error_event_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 896 | new OmahaRequestAction(prefs_, |
| 897 | omaha_request_params_, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 898 | error_event_.release(), // Pass ownership. |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 899 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 900 | false)); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 901 | actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 902 | processor_->EnqueueAction(error_event_action.get()); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 903 | SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 904 | processor_->StartProcessing(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 905 | return true; |
| 906 | } |
| 907 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 908 | void UpdateAttempter::SetPriority(utils::ProcessPriority priority) { |
| 909 | if (priority_ == priority) { |
| 910 | return; |
| 911 | } |
| 912 | if (utils::SetProcessPriority(priority)) { |
| 913 | priority_ = priority; |
| 914 | LOG(INFO) << "Process priority = " << priority_; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | void UpdateAttempter::SetupPriorityManagement() { |
| 919 | if (manage_priority_source_) { |
| 920 | LOG(ERROR) << "Process priority timeout source hasn't been destroyed."; |
| 921 | CleanupPriorityManagement(); |
| 922 | } |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 923 | const int kPriorityTimeout = 2 * 60 * 60; // 2 hours |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 924 | manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout); |
| 925 | g_source_set_callback(manage_priority_source_, |
| 926 | StaticManagePriorityCallback, |
| 927 | this, |
| 928 | NULL); |
| 929 | g_source_attach(manage_priority_source_, NULL); |
| 930 | SetPriority(utils::kProcessPriorityLow); |
| 931 | } |
| 932 | |
| 933 | void UpdateAttempter::CleanupPriorityManagement() { |
| 934 | if (manage_priority_source_) { |
| 935 | g_source_destroy(manage_priority_source_); |
| 936 | manage_priority_source_ = NULL; |
| 937 | } |
| 938 | SetPriority(utils::kProcessPriorityNormal); |
| 939 | } |
| 940 | |
| 941 | gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) { |
| 942 | return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback(); |
| 943 | } |
| 944 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 945 | gboolean UpdateAttempter::StaticStartProcessing(gpointer data) { |
| 946 | reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing(); |
| 947 | return FALSE; // Don't call this callback again. |
| 948 | } |
| 949 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 950 | void UpdateAttempter::ScheduleProcessingStart() { |
| 951 | LOG(INFO) << "Scheduling an action processor start."; |
| 952 | start_action_processor_ = false; |
| 953 | g_idle_add(&StaticStartProcessing, this); |
| 954 | } |
| 955 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 956 | bool UpdateAttempter::ManagePriorityCallback() { |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 957 | SetPriority(utils::kProcessPriorityNormal); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 958 | manage_priority_source_ = NULL; |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 959 | return false; // Destroy the timeout source. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 962 | void UpdateAttempter::DisableDeltaUpdateIfNeeded() { |
| 963 | int64_t delta_failures; |
| 964 | if (omaha_request_params_.delta_okay && |
| 965 | prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) && |
| 966 | delta_failures >= kMaxDeltaUpdateFailures) { |
| 967 | LOG(WARNING) << "Too many delta update failures, forcing full update."; |
| 968 | omaha_request_params_.delta_okay = false; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | void UpdateAttempter::MarkDeltaUpdateFailure() { |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 973 | // Don't try to resume a failed delta update. |
| 974 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 975 | int64_t delta_failures; |
| 976 | if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) || |
| 977 | delta_failures < 0) { |
| 978 | delta_failures = 0; |
| 979 | } |
| 980 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures); |
| 981 | } |
| 982 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 983 | void UpdateAttempter::SetupDownload() { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 984 | MultiRangeHttpFetcher* fetcher = |
| 985 | dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher()); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 986 | fetcher->ClearRanges(); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 987 | if (response_handler_action_->install_plan().is_resume) { |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 988 | // Resuming an update so fetch the update manifest metadata first. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 989 | int64_t manifest_metadata_size = 0; |
| 990 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 991 | fetcher->AddRange(0, manifest_metadata_size); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 992 | // If there're remaining unprocessed data blobs, fetch them. Be careful not |
| 993 | // to request data beyond the end of the payload to avoid 416 HTTP response |
| 994 | // error codes. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 995 | int64_t next_data_offset = 0; |
| 996 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 997 | uint64_t resume_offset = manifest_metadata_size + next_data_offset; |
| 998 | if (resume_offset < response_handler_action_->install_plan().size) { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 999 | fetcher->AddRange(resume_offset); |
Darin Petkov | b21ce5d | 2010-10-21 16:03:05 -0700 | [diff] [blame] | 1000 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1001 | } else { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1002 | fetcher->AddRange(0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1003 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1006 | void UpdateAttempter::PingOmaha() { |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 1007 | if (!processor_->IsRunning()) { |
| 1008 | shared_ptr<OmahaRequestAction> ping_action( |
| 1009 | new OmahaRequestAction(prefs_, |
| 1010 | omaha_request_params_, |
| 1011 | NULL, |
| 1012 | new LibcurlHttpFetcher(GetProxyResolver()), |
| 1013 | true)); |
| 1014 | actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action)); |
| 1015 | processor_->set_delegate(NULL); |
| 1016 | processor_->EnqueueAction(ping_action.get()); |
| 1017 | // Call StartProcessing() synchronously here to avoid any race conditions |
| 1018 | // caused by multiple outstanding ping Omaha requests. If we call |
| 1019 | // StartProcessing() asynchronously, the device can be suspended before we |
| 1020 | // get a chance to callback to StartProcessing(). When the device resumes |
| 1021 | // (assuming the device sleeps longer than the next update check period), |
| 1022 | // StartProcessing() is called back and at the same time, the next update |
| 1023 | // check is fired which eventually invokes StartProcessing(). A crash |
| 1024 | // can occur because StartProcessing() checks to make sure that the |
| 1025 | // processor is idle which it isn't due to the two concurrent ping Omaha |
| 1026 | // requests. |
| 1027 | processor_->StartProcessing(); |
| 1028 | } else { |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 1029 | LOG(WARNING) << "Action processor running, Omaha ping suppressed."; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 1030 | } |
Thieu Le | d88a857 | 2011-05-26 09:09:19 -0700 | [diff] [blame] | 1031 | |
| 1032 | // Update the status which will schedule the next update check |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1033 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
| 1034 | } |
| 1035 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 1036 | } // namespace chromeos_update_engine |