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