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