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