Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "update_engine/update_attempter_android.h" |
| 18 | |
| 19 | #include <algorithm> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 20 | #include <map> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 21 | #include <utility> |
| 22 | |
| 23 | #include <base/bind.h> |
| 24 | #include <base/logging.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 25 | #include <base/strings/string_number_conversions.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 26 | #include <brillo/bind_lambda.h> |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 27 | #include <brillo/data_encoding.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 28 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 29 | #include <brillo/strings/string_utils.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 30 | |
| 31 | #include "update_engine/common/constants.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 32 | #include "update_engine/common/file_fetcher.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 33 | #include "update_engine/common/utils.h" |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 34 | #include "update_engine/daemon_state_interface.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 35 | #include "update_engine/metrics_reporter_android.h" |
| 36 | #include "update_engine/metrics_utils.h" |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 37 | #include "update_engine/network_selector.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 38 | #include "update_engine/payload_consumer/download_action.h" |
| 39 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
| 40 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 41 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 42 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 43 | #ifndef _UE_SIDELOAD |
| 44 | // Do not include support for external HTTP(s) urls when building |
| 45 | // update_engine_sideload. |
| 46 | #include "update_engine/libcurl_http_fetcher.h" |
| 47 | #endif |
| 48 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 49 | using base::Bind; |
| 50 | using base::TimeDelta; |
| 51 | using base::TimeTicks; |
| 52 | using std::shared_ptr; |
| 53 | using std::string; |
| 54 | using std::vector; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 55 | using update_engine::UpdateEngineStatus; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 56 | |
| 57 | namespace chromeos_update_engine { |
| 58 | |
| 59 | namespace { |
| 60 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 61 | // Minimum threshold to broadcast an status update in progress and time. |
| 62 | const double kBroadcastThresholdProgress = 0.01; // 1% |
| 63 | const int kBroadcastThresholdSeconds = 10; |
| 64 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 65 | const char* const kErrorDomain = "update_engine"; |
| 66 | // TODO(deymo): Convert the different errors to a numeric value to report them |
| 67 | // back on the service error. |
| 68 | const char* const kGenericError = "generic_error"; |
| 69 | |
| 70 | // Log and set the error on the passed ErrorPtr. |
| 71 | bool LogAndSetError(brillo::ErrorPtr* error, |
| 72 | const tracked_objects::Location& location, |
| 73 | const string& reason) { |
| 74 | brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason); |
| 75 | LOG(ERROR) << "Replying with failure: " << location.ToString() << ": " |
| 76 | << reason; |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | UpdateAttempterAndroid::UpdateAttempterAndroid( |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 83 | DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 84 | PrefsInterface* prefs, |
| 85 | BootControlInterface* boot_control, |
| 86 | HardwareInterface* hardware) |
| 87 | : daemon_state_(daemon_state), |
| 88 | prefs_(prefs), |
| 89 | boot_control_(boot_control), |
| 90 | hardware_(hardware), |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 91 | processor_(new ActionProcessor()), |
| 92 | metrics_reporter_(new MetricsReporterAndroid()) { |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 93 | network_selector_ = network::CreateNetworkSelector(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | UpdateAttempterAndroid::~UpdateAttempterAndroid() { |
| 97 | // Release ourselves as the ActionProcessor's delegate to prevent |
| 98 | // re-scheduling the updates due to the processing stopped. |
| 99 | processor_->set_delegate(nullptr); |
| 100 | } |
| 101 | |
| 102 | void UpdateAttempterAndroid::Init() { |
| 103 | // In case of update_engine restart without a reboot we need to restore the |
| 104 | // reboot needed state. |
| 105 | if (UpdateCompletedOnThisBoot()) |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 106 | SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 107 | else |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 108 | SetStatusAndNotify(UpdateStatus::IDLE); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool UpdateAttempterAndroid::ApplyPayload( |
| 112 | const string& payload_url, |
| 113 | int64_t payload_offset, |
| 114 | int64_t payload_size, |
| 115 | const vector<string>& key_value_pair_headers, |
| 116 | brillo::ErrorPtr* error) { |
| 117 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 118 | return LogAndSetError( |
| 119 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 120 | } |
| 121 | if (ongoing_update_) { |
| 122 | return LogAndSetError( |
| 123 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 124 | } |
| 125 | DCHECK(status_ == UpdateStatus::IDLE); |
| 126 | |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 127 | std::map<string, string> headers; |
| 128 | for (const string& key_value_pair : key_value_pair_headers) { |
| 129 | string key; |
| 130 | string value; |
| 131 | if (!brillo::string_utils::SplitAtFirst( |
| 132 | key_value_pair, "=", &key, &value, false)) { |
| 133 | return LogAndSetError( |
| 134 | error, FROM_HERE, "Passed invalid header: " + key_value_pair); |
| 135 | } |
| 136 | if (!headers.emplace(key, value).second) |
| 137 | return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key); |
| 138 | } |
| 139 | |
| 140 | // Unique identifier for the payload. An empty string means that the payload |
| 141 | // can't be resumed. |
| 142 | string payload_id = (headers[kPayloadPropertyFileHash] + |
| 143 | headers[kPayloadPropertyMetadataHash]); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 144 | |
| 145 | // Setup the InstallPlan based on the request. |
| 146 | install_plan_ = InstallPlan(); |
| 147 | |
| 148 | install_plan_.download_url = payload_url; |
| 149 | install_plan_.version = ""; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 150 | base_offset_ = payload_offset; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 151 | InstallPlan::Payload payload; |
| 152 | payload.size = payload_size; |
| 153 | if (!payload.size) { |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 154 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 155 | &payload.size)) { |
| 156 | payload.size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 159 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 160 | &payload.hash)) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 161 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 162 | << headers[kPayloadPropertyFileHash]; |
| 163 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 164 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 165 | &payload.metadata_size)) { |
| 166 | payload.metadata_size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 167 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 168 | // The |payload.type| is not used anymore since minor_version 3. |
| 169 | payload.type = InstallPayloadType::kUnknown; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 170 | install_plan_.payloads.push_back(payload); |
| 171 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 172 | // The |public_key_rsa| key would override the public key stored on disk. |
| 173 | install_plan_.public_key_rsa = ""; |
| 174 | |
| 175 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 176 | install_plan_.is_resume = !payload_id.empty() && |
| 177 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 178 | if (!install_plan_.is_resume) { |
| 179 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 180 | LOG(WARNING) << "Unable to reset the update progress."; |
| 181 | } |
| 182 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 183 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 184 | } |
| 185 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 186 | install_plan_.source_slot = boot_control_->GetCurrentSlot(); |
| 187 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 188 | |
| 189 | int data_wipe = 0; |
| 190 | install_plan_.powerwash_required = |
| 191 | base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) && |
| 192 | data_wipe != 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 193 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 194 | NetworkId network_id = kDefaultNetworkId; |
| 195 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 196 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 197 | &network_id)) { |
| 198 | return LogAndSetError( |
| 199 | error, |
| 200 | FROM_HERE, |
| 201 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 202 | } |
| 203 | if (!network_selector_->SetProcessNetwork(network_id)) { |
Sen Jiang | cbd37c6 | 2017-09-12 15:04:35 -0700 | [diff] [blame] | 204 | return LogAndSetError( |
| 205 | error, |
| 206 | FROM_HERE, |
| 207 | "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 211 | LOG(INFO) << "Using this install plan:"; |
| 212 | install_plan_.Dump(); |
| 213 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 214 | BuildUpdateActions(payload_url); |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 215 | // Setup extra headers. |
| 216 | HttpFetcher* fetcher = download_action_->http_fetcher(); |
| 217 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 218 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 219 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 220 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 221 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 222 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 223 | ongoing_update_ = true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 224 | |
| 225 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 226 | // before any update processing starts. This will start the update process. |
| 227 | UpdateBootFlags(); |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 232 | if (!ongoing_update_) |
| 233 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 234 | processor_->SuspendProcessing(); |
| 235 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 239 | if (!ongoing_update_) |
| 240 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 241 | processor_->ResumeProcessing(); |
| 242 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 246 | if (!ongoing_update_) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 247 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 248 | processor_->StopProcessing(); |
| 249 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 252 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 253 | LOG(INFO) << "Attempting to reset state from " |
| 254 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
| 255 | |
| 256 | switch (status_) { |
| 257 | case UpdateStatus::IDLE: |
| 258 | return true; |
| 259 | |
| 260 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
| 261 | // Remove the reboot marker so that if the machine is rebooted |
| 262 | // after resetting to idle state, it doesn't go back to |
| 263 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 264 | bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId); |
| 265 | |
| 266 | // Update the boot flags so the current slot has higher priority. |
| 267 | if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot())) |
| 268 | ret_value = false; |
| 269 | |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 270 | // Mark the current slot as successful again, since marking it as active |
| 271 | // may reset the successful bit. We ignore the result of whether marking |
| 272 | // the current slot as successful worked. |
| 273 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){}))) |
| 274 | ret_value = false; |
| 275 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 276 | if (!ret_value) { |
| 277 | return LogAndSetError( |
| 278 | error, |
| 279 | FROM_HERE, |
| 280 | "Failed to reset the status to "); |
| 281 | } |
| 282 | |
| 283 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 284 | LOG(INFO) << "Reset status successful"; |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | default: |
| 289 | return LogAndSetError( |
| 290 | error, |
| 291 | FROM_HERE, |
| 292 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 293 | } |
| 294 | } |
| 295 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 296 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 297 | ErrorCode code) { |
| 298 | LOG(INFO) << "Processing Done."; |
| 299 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 300 | switch (code) { |
| 301 | case ErrorCode::kSuccess: |
| 302 | // Update succeeded. |
| 303 | WriteUpdateCompletedMarker(); |
| 304 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
| 305 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 306 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 307 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 308 | break; |
| 309 | |
| 310 | case ErrorCode::kFilesystemCopierError: |
| 311 | case ErrorCode::kNewRootfsVerificationError: |
| 312 | case ErrorCode::kNewKernelVerificationError: |
| 313 | case ErrorCode::kFilesystemVerifierError: |
| 314 | case ErrorCode::kDownloadStateInitializationError: |
| 315 | // Reset the ongoing update for these errors so it starts from the |
| 316 | // beginning next time. |
| 317 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 318 | LOG(INFO) << "Resetting update progress."; |
| 319 | break; |
| 320 | |
| 321 | default: |
| 322 | // Ignore all other error codes. |
| 323 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 324 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 325 | |
| 326 | TerminateUpdateAndNotify(code); |
| 327 | } |
| 328 | |
| 329 | void UpdateAttempterAndroid::ProcessingStopped( |
| 330 | const ActionProcessor* processor) { |
| 331 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 332 | } |
| 333 | |
| 334 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 335 | AbstractAction* action, |
| 336 | ErrorCode code) { |
| 337 | // Reset download progress regardless of whether or not the download |
| 338 | // action succeeded. |
| 339 | const string type = action->Type(); |
| 340 | if (type == DownloadAction::StaticType()) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 341 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 342 | } |
| 343 | if (code != ErrorCode::kSuccess) { |
| 344 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 345 | return; |
| 346 | } |
| 347 | if (type == DownloadAction::StaticType()) { |
| 348 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 353 | uint64_t bytes_received, |
| 354 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 355 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 356 | if (total) |
| 357 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 358 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 359 | download_progress_ = progress; |
| 360 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 361 | } else { |
| 362 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 367 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 368 | // download. |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | void UpdateAttempterAndroid::DownloadComplete() { |
| 373 | // Nothing needs to be done when the download completes. |
| 374 | } |
| 375 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 376 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 377 | // Self throttle based on progress. Also send notifications if progress is |
| 378 | // too slow. |
| 379 | if (progress == 1.0 || |
| 380 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 381 | TimeTicks::Now() - last_notify_time_ >= |
| 382 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 383 | download_progress_ = progress; |
| 384 | SetStatusAndNotify(status_); |
| 385 | } |
| 386 | } |
| 387 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 388 | void UpdateAttempterAndroid::UpdateBootFlags() { |
| 389 | if (updated_boot_flags_) { |
| 390 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 391 | CompleteUpdateBootFlags(true); |
| 392 | return; |
| 393 | } |
| 394 | // This is purely best effort. |
| 395 | LOG(INFO) << "Marking booted slot as good."; |
| 396 | if (!boot_control_->MarkBootSuccessfulAsync( |
| 397 | Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags, |
| 398 | base::Unretained(this)))) { |
| 399 | LOG(ERROR) << "Failed to mark current boot as successful."; |
| 400 | CompleteUpdateBootFlags(false); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) { |
| 405 | updated_boot_flags_ = true; |
| 406 | ScheduleProcessingStart(); |
| 407 | } |
| 408 | |
| 409 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 410 | LOG(INFO) << "Scheduling an action processor start."; |
| 411 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 412 | FROM_HERE, |
| 413 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 414 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 418 | if (status_ == UpdateStatus::IDLE) { |
| 419 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 420 | return; |
| 421 | } |
| 422 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 423 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 424 | actions_.clear(); |
| 425 | UpdateStatus new_status = |
| 426 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 427 | : UpdateStatus::IDLE); |
| 428 | SetStatusAndNotify(new_status); |
| 429 | ongoing_update_ = false; |
| 430 | |
| 431 | for (auto observer : daemon_state_->service_observers()) |
| 432 | observer->SendPayloadApplicationComplete(error_code); |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 433 | |
| 434 | // TODO(xunchang): assign proper values to the metrics. |
| 435 | PayloadType payload_type = kNumPayloadTypes; |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 436 | metrics::AttemptResult attempt_result = |
| 437 | metrics_utils::GetAttemptResult(error_code); |
| 438 | TimeDelta duration; |
| 439 | TimeDelta duration_uptime; |
| 440 | // Report the android metrics when we terminate the update. Currently we are |
| 441 | // reporting error_code only. |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame] | 442 | metrics_reporter_->ReportUpdateAttemptMetrics(nullptr, // system_state |
| 443 | 0, // attempt_number |
| 444 | payload_type, |
| 445 | duration, |
| 446 | duration_uptime, |
| 447 | 0, // payload_size |
| 448 | attempt_result, |
| 449 | error_code); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) { |
| 453 | status_ = status; |
Sen Jiang | 2d1c87b | 2017-07-14 10:46:14 -0700 | [diff] [blame] | 454 | size_t payload_size = |
| 455 | install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 456 | UpdateEngineStatus status_to_send = {.status = status_, |
| 457 | .progress = download_progress_, |
| 458 | .new_size_bytes = payload_size}; |
| 459 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 460 | for (auto observer : daemon_state_->service_observers()) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 461 | observer->SendStatusUpdate(status_to_send); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 462 | } |
| 463 | last_notify_time_ = TimeTicks::Now(); |
| 464 | } |
| 465 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 466 | void UpdateAttempterAndroid::BuildUpdateActions(const string& url) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 467 | CHECK(!processor_->IsRunning()); |
| 468 | processor_->set_delegate(this); |
| 469 | |
| 470 | // Actions: |
| 471 | shared_ptr<InstallPlanAction> install_plan_action( |
| 472 | new InstallPlanAction(install_plan_)); |
| 473 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 474 | HttpFetcher* download_fetcher = nullptr; |
| 475 | if (FileFetcher::SupportedUrl(url)) { |
| 476 | DLOG(INFO) << "Using FileFetcher for file URL."; |
| 477 | download_fetcher = new FileFetcher(); |
| 478 | } else { |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 479 | #ifdef _UE_SIDELOAD |
| 480 | LOG(FATAL) << "Unsupported sideload URI: " << url; |
| 481 | #else |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 482 | LibcurlHttpFetcher* libcurl_fetcher = |
| 483 | new LibcurlHttpFetcher(&proxy_resolver_, hardware_); |
| 484 | libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload); |
| 485 | download_fetcher = libcurl_fetcher; |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 486 | #endif // _UE_SIDELOAD |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 487 | } |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 488 | shared_ptr<DownloadAction> download_action( |
| 489 | new DownloadAction(prefs_, |
| 490 | boot_control_, |
| 491 | hardware_, |
| 492 | nullptr, // system_state, not used. |
| 493 | download_fetcher)); // passes ownership |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 494 | shared_ptr<FilesystemVerifierAction> filesystem_verifier_action( |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 495 | new FilesystemVerifierAction()); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 496 | |
| 497 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 498 | new PostinstallRunnerAction(boot_control_, hardware_)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 499 | |
| 500 | download_action->set_delegate(this); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 501 | download_action->set_base_offset(base_offset_); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 502 | download_action_ = download_action; |
Alex Deymo | b6eef73 | 2016-06-10 12:58:11 -0700 | [diff] [blame] | 503 | postinstall_runner_action->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 504 | |
| 505 | actions_.push_back(shared_ptr<AbstractAction>(install_plan_action)); |
| 506 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 507 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 508 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
| 509 | |
| 510 | // Bond them together. We have to use the leaf-types when calling |
| 511 | // BondActions(). |
| 512 | BondActions(install_plan_action.get(), download_action.get()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 513 | BondActions(download_action.get(), filesystem_verifier_action.get()); |
| 514 | BondActions(filesystem_verifier_action.get(), |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 515 | postinstall_runner_action.get()); |
| 516 | |
| 517 | // Enqueue the actions. |
| 518 | for (const shared_ptr<AbstractAction>& action : actions_) |
| 519 | processor_->EnqueueAction(action.get()); |
| 520 | } |
| 521 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 522 | bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() { |
| 523 | string boot_id; |
| 524 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 525 | prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id); |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() { |
| 530 | // In case of an update_engine restart without a reboot, we stored the boot_id |
| 531 | // when the update was completed by setting a pref, so we can check whether |
| 532 | // the last update was on this boot or a previous one. |
| 533 | string boot_id; |
| 534 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 535 | |
| 536 | string update_completed_on_boot_id; |
| 537 | return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) && |
| 538 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, |
| 539 | &update_completed_on_boot_id) && |
| 540 | update_completed_on_boot_id == boot_id); |
| 541 | } |
| 542 | |
| 543 | } // namespace chromeos_update_engine |