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