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