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> |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 25 | #include <base/bind.h> |
| 26 | #include <base/logging.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 27 | #include <base/strings/string_number_conversions.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 28 | #include <brillo/bind_lambda.h> |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 29 | #include <brillo/data_encoding.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 30 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 31 | #include <brillo/strings/string_utils.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 32 | |
| 33 | #include "update_engine/common/constants.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 34 | #include "update_engine/common/file_fetcher.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 35 | #include "update_engine/common/utils.h" |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 36 | #include "update_engine/daemon_state_interface.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 37 | #include "update_engine/metrics_reporter_android.h" |
| 38 | #include "update_engine/metrics_utils.h" |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 39 | #include "update_engine/network_selector.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 40 | #include "update_engine/payload_consumer/download_action.h" |
| 41 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
| 42 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 43 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 44 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 45 | #ifndef _UE_SIDELOAD |
| 46 | // Do not include support for external HTTP(s) urls when building |
| 47 | // update_engine_sideload. |
| 48 | #include "update_engine/libcurl_http_fetcher.h" |
| 49 | #endif |
| 50 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 51 | using base::Bind; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 52 | using base::Time; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 53 | using base::TimeDelta; |
| 54 | using base::TimeTicks; |
| 55 | using std::shared_ptr; |
| 56 | using std::string; |
| 57 | using std::vector; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 58 | using update_engine::UpdateEngineStatus; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 59 | |
| 60 | namespace chromeos_update_engine { |
| 61 | |
| 62 | namespace { |
| 63 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 64 | // Minimum threshold to broadcast an status update in progress and time. |
| 65 | const double kBroadcastThresholdProgress = 0.01; // 1% |
| 66 | const int kBroadcastThresholdSeconds = 10; |
| 67 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 68 | const char* const kErrorDomain = "update_engine"; |
| 69 | // TODO(deymo): Convert the different errors to a numeric value to report them |
| 70 | // back on the service error. |
| 71 | const char* const kGenericError = "generic_error"; |
| 72 | |
| 73 | // Log and set the error on the passed ErrorPtr. |
| 74 | bool LogAndSetError(brillo::ErrorPtr* error, |
| 75 | const tracked_objects::Location& location, |
| 76 | const string& reason) { |
| 77 | brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason); |
| 78 | LOG(ERROR) << "Replying with failure: " << location.ToString() << ": " |
| 79 | << reason; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | UpdateAttempterAndroid::UpdateAttempterAndroid( |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 86 | DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 87 | PrefsInterface* prefs, |
| 88 | BootControlInterface* boot_control, |
| 89 | HardwareInterface* hardware) |
| 90 | : daemon_state_(daemon_state), |
| 91 | prefs_(prefs), |
| 92 | boot_control_(boot_control), |
| 93 | hardware_(hardware), |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 94 | processor_(new ActionProcessor()), |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 95 | clock_(new Clock()), |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 96 | metrics_reporter_(new MetricsReporterAndroid()) { |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 97 | network_selector_ = network::CreateNetworkSelector(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | UpdateAttempterAndroid::~UpdateAttempterAndroid() { |
| 101 | // Release ourselves as the ActionProcessor's delegate to prevent |
| 102 | // re-scheduling the updates due to the processing stopped. |
| 103 | processor_->set_delegate(nullptr); |
| 104 | } |
| 105 | |
| 106 | void UpdateAttempterAndroid::Init() { |
| 107 | // In case of update_engine restart without a reboot we need to restore the |
| 108 | // reboot needed state. |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 109 | if (UpdateCompletedOnThisBoot()) { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 110 | SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 111 | } else { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 112 | SetStatusAndNotify(UpdateStatus::IDLE); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 113 | UpdatePrefsAndReportUpdateMetricsOnReboot(); |
| 114 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool UpdateAttempterAndroid::ApplyPayload( |
| 118 | const string& payload_url, |
| 119 | int64_t payload_offset, |
| 120 | int64_t payload_size, |
| 121 | const vector<string>& key_value_pair_headers, |
| 122 | brillo::ErrorPtr* error) { |
| 123 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 124 | return LogAndSetError( |
| 125 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 126 | } |
| 127 | if (ongoing_update_) { |
| 128 | return LogAndSetError( |
| 129 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 130 | } |
| 131 | DCHECK(status_ == UpdateStatus::IDLE); |
| 132 | |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 133 | std::map<string, string> headers; |
| 134 | for (const string& key_value_pair : key_value_pair_headers) { |
| 135 | string key; |
| 136 | string value; |
| 137 | if (!brillo::string_utils::SplitAtFirst( |
| 138 | key_value_pair, "=", &key, &value, false)) { |
| 139 | return LogAndSetError( |
| 140 | error, FROM_HERE, "Passed invalid header: " + key_value_pair); |
| 141 | } |
| 142 | if (!headers.emplace(key, value).second) |
| 143 | return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key); |
| 144 | } |
| 145 | |
| 146 | // Unique identifier for the payload. An empty string means that the payload |
| 147 | // can't be resumed. |
| 148 | string payload_id = (headers[kPayloadPropertyFileHash] + |
| 149 | headers[kPayloadPropertyMetadataHash]); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 150 | |
| 151 | // Setup the InstallPlan based on the request. |
| 152 | install_plan_ = InstallPlan(); |
| 153 | |
| 154 | install_plan_.download_url = payload_url; |
| 155 | install_plan_.version = ""; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 156 | base_offset_ = payload_offset; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 157 | InstallPlan::Payload payload; |
| 158 | payload.size = payload_size; |
| 159 | if (!payload.size) { |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 160 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 161 | &payload.size)) { |
| 162 | payload.size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 165 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 166 | &payload.hash)) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 167 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 168 | << headers[kPayloadPropertyFileHash]; |
| 169 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 170 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 171 | &payload.metadata_size)) { |
| 172 | payload.metadata_size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 173 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 174 | // The |payload.type| is not used anymore since minor_version 3. |
| 175 | payload.type = InstallPayloadType::kUnknown; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 176 | install_plan_.payloads.push_back(payload); |
| 177 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 178 | // The |public_key_rsa| key would override the public key stored on disk. |
| 179 | install_plan_.public_key_rsa = ""; |
| 180 | |
| 181 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 182 | install_plan_.is_resume = !payload_id.empty() && |
| 183 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 184 | if (!install_plan_.is_resume) { |
| 185 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 186 | LOG(WARNING) << "Unable to reset the update progress."; |
| 187 | } |
| 188 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 189 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 190 | } |
| 191 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 192 | install_plan_.source_slot = boot_control_->GetCurrentSlot(); |
| 193 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 194 | |
| 195 | int data_wipe = 0; |
| 196 | install_plan_.powerwash_required = |
| 197 | base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) && |
| 198 | data_wipe != 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 199 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 200 | NetworkId network_id = kDefaultNetworkId; |
| 201 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 202 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 203 | &network_id)) { |
| 204 | return LogAndSetError( |
| 205 | error, |
| 206 | FROM_HERE, |
| 207 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 208 | } |
| 209 | if (!network_selector_->SetProcessNetwork(network_id)) { |
Sen Jiang | cbd37c6 | 2017-09-12 15:04:35 -0700 | [diff] [blame] | 210 | return LogAndSetError( |
| 211 | error, |
| 212 | FROM_HERE, |
| 213 | "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 217 | LOG(INFO) << "Using this install plan:"; |
| 218 | install_plan_.Dump(); |
| 219 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 220 | BuildUpdateActions(payload_url); |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 221 | // Setup extra headers. |
| 222 | HttpFetcher* fetcher = download_action_->http_fetcher(); |
| 223 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 224 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 225 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 226 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 227 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 228 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 229 | ongoing_update_ = true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 230 | |
| 231 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 232 | // before any update processing starts. This will start the update process. |
| 233 | UpdateBootFlags(); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 234 | |
| 235 | UpdatePrefsOnUpdateStart(install_plan_.is_resume); |
| 236 | // TODO(xunchang) report the metrics for unresumable updates |
| 237 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 242 | if (!ongoing_update_) |
| 243 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 244 | processor_->SuspendProcessing(); |
| 245 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 249 | if (!ongoing_update_) |
| 250 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 251 | processor_->ResumeProcessing(); |
| 252 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 256 | if (!ongoing_update_) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 257 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 258 | processor_->StopProcessing(); |
| 259 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 262 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 263 | LOG(INFO) << "Attempting to reset state from " |
| 264 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
| 265 | |
| 266 | switch (status_) { |
| 267 | case UpdateStatus::IDLE: |
| 268 | return true; |
| 269 | |
| 270 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
| 271 | // Remove the reboot marker so that if the machine is rebooted |
| 272 | // after resetting to idle state, it doesn't go back to |
| 273 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 274 | bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 275 | ClearMetricsPrefs(); |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 276 | |
| 277 | // Update the boot flags so the current slot has higher priority. |
| 278 | if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot())) |
| 279 | ret_value = false; |
| 280 | |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 281 | // Mark the current slot as successful again, since marking it as active |
| 282 | // may reset the successful bit. We ignore the result of whether marking |
| 283 | // the current slot as successful worked. |
| 284 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){}))) |
| 285 | ret_value = false; |
| 286 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 287 | if (!ret_value) { |
| 288 | return LogAndSetError( |
| 289 | error, |
| 290 | FROM_HERE, |
| 291 | "Failed to reset the status to "); |
| 292 | } |
| 293 | |
| 294 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 295 | LOG(INFO) << "Reset status successful"; |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | default: |
| 300 | return LogAndSetError( |
| 301 | error, |
| 302 | FROM_HERE, |
| 303 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 304 | } |
| 305 | } |
| 306 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 307 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 308 | ErrorCode code) { |
| 309 | LOG(INFO) << "Processing Done."; |
| 310 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 311 | switch (code) { |
| 312 | case ErrorCode::kSuccess: |
| 313 | // Update succeeded. |
| 314 | WriteUpdateCompletedMarker(); |
| 315 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
| 316 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 317 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 318 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 319 | break; |
| 320 | |
| 321 | case ErrorCode::kFilesystemCopierError: |
| 322 | case ErrorCode::kNewRootfsVerificationError: |
| 323 | case ErrorCode::kNewKernelVerificationError: |
| 324 | case ErrorCode::kFilesystemVerifierError: |
| 325 | case ErrorCode::kDownloadStateInitializationError: |
| 326 | // Reset the ongoing update for these errors so it starts from the |
| 327 | // beginning next time. |
| 328 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 329 | LOG(INFO) << "Resetting update progress."; |
| 330 | break; |
| 331 | |
| 332 | default: |
| 333 | // Ignore all other error codes. |
| 334 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 335 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 336 | |
| 337 | TerminateUpdateAndNotify(code); |
| 338 | } |
| 339 | |
| 340 | void UpdateAttempterAndroid::ProcessingStopped( |
| 341 | const ActionProcessor* processor) { |
| 342 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 343 | } |
| 344 | |
| 345 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 346 | AbstractAction* action, |
| 347 | ErrorCode code) { |
| 348 | // Reset download progress regardless of whether or not the download |
| 349 | // action succeeded. |
| 350 | const string type = action->Type(); |
| 351 | if (type == DownloadAction::StaticType()) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 352 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 353 | } |
| 354 | if (code != ErrorCode::kSuccess) { |
| 355 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 356 | return; |
| 357 | } |
| 358 | if (type == DownloadAction::StaticType()) { |
| 359 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 364 | uint64_t bytes_received, |
| 365 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 366 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 367 | if (total) |
| 368 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 369 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 370 | download_progress_ = progress; |
| 371 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 372 | } else { |
| 373 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 378 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 379 | // download. |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | void UpdateAttempterAndroid::DownloadComplete() { |
| 384 | // Nothing needs to be done when the download completes. |
| 385 | } |
| 386 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 387 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 388 | // Self throttle based on progress. Also send notifications if progress is |
| 389 | // too slow. |
| 390 | if (progress == 1.0 || |
| 391 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 392 | TimeTicks::Now() - last_notify_time_ >= |
| 393 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 394 | download_progress_ = progress; |
| 395 | SetStatusAndNotify(status_); |
| 396 | } |
| 397 | } |
| 398 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 399 | void UpdateAttempterAndroid::UpdateBootFlags() { |
| 400 | if (updated_boot_flags_) { |
| 401 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 402 | CompleteUpdateBootFlags(true); |
| 403 | return; |
| 404 | } |
| 405 | // This is purely best effort. |
| 406 | LOG(INFO) << "Marking booted slot as good."; |
| 407 | if (!boot_control_->MarkBootSuccessfulAsync( |
| 408 | Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags, |
| 409 | base::Unretained(this)))) { |
| 410 | LOG(ERROR) << "Failed to mark current boot as successful."; |
| 411 | CompleteUpdateBootFlags(false); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) { |
| 416 | updated_boot_flags_ = true; |
| 417 | ScheduleProcessingStart(); |
| 418 | } |
| 419 | |
| 420 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 421 | LOG(INFO) << "Scheduling an action processor start."; |
| 422 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 423 | FROM_HERE, |
| 424 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 425 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 429 | if (status_ == UpdateStatus::IDLE) { |
| 430 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 431 | return; |
| 432 | } |
| 433 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 434 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 435 | actions_.clear(); |
| 436 | UpdateStatus new_status = |
| 437 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 438 | : UpdateStatus::IDLE); |
| 439 | SetStatusAndNotify(new_status); |
| 440 | ongoing_update_ = false; |
| 441 | |
| 442 | for (auto observer : daemon_state_->service_observers()) |
| 443 | observer->SendPayloadApplicationComplete(error_code); |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 444 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 445 | CollectAndReportUpdateMetricsOnUpdateFinished(error_code); |
| 446 | ClearMetricsPrefs(); |
| 447 | if (error_code == ErrorCode::kSuccess) { |
| 448 | metrics_utils::SetSystemUpdatedMarker(clock_.get(), prefs_); |
| 449 | } |
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 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 543 | // Collect and report the android metrics when we terminate the update. |
| 544 | void UpdateAttempterAndroid::CollectAndReportUpdateMetricsOnUpdateFinished( |
| 545 | ErrorCode error_code) { |
| 546 | int64_t attempt_number = |
| 547 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 548 | PayloadType payload_type = kPayloadTypeFull; |
| 549 | int64_t payload_size = 0; |
| 550 | for (const auto& p : install_plan_.payloads) { |
| 551 | if (p.type == InstallPayloadType::kDelta) |
| 552 | payload_type = kPayloadTypeDelta; |
| 553 | payload_size += p.size; |
| 554 | } |
| 555 | |
| 556 | metrics::AttemptResult attempt_result = |
| 557 | metrics_utils::GetAttemptResult(error_code); |
| 558 | Time attempt_start_time = Time::FromInternalValue( |
| 559 | metrics_utils::GetPersistedValue(kPrefsUpdateTimestampStart, prefs_)); |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 560 | TimeDelta duration = clock_->GetBootTime() - attempt_start_time; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 561 | TimeDelta duration_uptime = clock_->GetMonotonicTime() - attempt_start_time; |
| 562 | |
| 563 | metrics_reporter_->ReportUpdateAttemptMetrics( |
| 564 | nullptr, // system_state |
| 565 | static_cast<int>(attempt_number), |
| 566 | payload_type, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 567 | duration, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 568 | duration_uptime, |
| 569 | payload_size, |
| 570 | attempt_result, |
| 571 | error_code); |
| 572 | |
| 573 | if (error_code == ErrorCode::kSuccess) { |
| 574 | int64_t reboot_count = |
| 575 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 576 | string build_version; |
| 577 | prefs_->GetString(kPrefsPreviousVersion, &build_version); |
| 578 | metrics_reporter_->ReportSuccessfulUpdateMetrics( |
| 579 | static_cast<int>(attempt_number), |
| 580 | 0, // update abandoned count |
| 581 | payload_type, |
| 582 | payload_size, |
| 583 | nullptr, // num bytes downloaded |
| 584 | 0, // download overhead percentage |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 585 | duration, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 586 | static_cast<int>(reboot_count), |
| 587 | 0); // url_switch_count |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void UpdateAttempterAndroid::UpdatePrefsAndReportUpdateMetricsOnReboot() { |
| 592 | string current_boot_id; |
| 593 | TEST_AND_RETURN(utils::GetBootId(¤t_boot_id)); |
| 594 | // Example: [ro.build.version.incremental]: [4292972] |
| 595 | string current_version = |
| 596 | android::base::GetProperty("ro.build.version.incremental", ""); |
| 597 | TEST_AND_RETURN(!current_version.empty()); |
| 598 | |
| 599 | // If there's no record of previous version (e.g. due to a data wipe), we |
| 600 | // save the info of current boot and skip the metrics report. |
| 601 | if (!prefs_->Exists(kPrefsPreviousVersion)) { |
| 602 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 603 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
| 604 | ClearMetricsPrefs(); |
| 605 | return; |
| 606 | } |
| 607 | string previous_version; |
| 608 | // update_engine restarted under the same build. |
| 609 | // TODO(xunchang) identify and report rollback by checking UpdateMarker. |
| 610 | if (prefs_->GetString(kPrefsPreviousVersion, &previous_version) && |
| 611 | previous_version == current_version) { |
| 612 | string last_boot_id; |
| 613 | bool is_reboot = prefs_->Exists(kPrefsBootId) && |
| 614 | (prefs_->GetString(kPrefsBootId, &last_boot_id) && |
| 615 | last_boot_id != current_boot_id); |
| 616 | // Increment the reboot number if |kPrefsNumReboots| exists. That pref is |
| 617 | // set when we start a new update. |
| 618 | if (is_reboot && prefs_->Exists(kPrefsNumReboots)) { |
| 619 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 620 | int64_t reboot_count = |
| 621 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 622 | metrics_utils::SetNumReboots(reboot_count + 1, prefs_); |
| 623 | } |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | // Now that the build version changes, report the update metrics. |
| 628 | // TODO(xunchang) check the build version is larger than the previous one. |
| 629 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 630 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
| 631 | |
| 632 | bool previous_attempt_exists = prefs_->Exists(kPrefsPayloadAttemptNumber); |
| 633 | // |kPrefsPayloadAttemptNumber| should be cleared upon successful update. |
| 634 | if (previous_attempt_exists) { |
| 635 | metrics_reporter_->ReportAbnormallyTerminatedUpdateAttemptMetrics(); |
| 636 | } |
| 637 | |
| 638 | metrics_utils::LoadAndReportTimeToReboot( |
| 639 | metrics_reporter_.get(), prefs_, clock_.get()); |
| 640 | ClearMetricsPrefs(); |
| 641 | } |
| 642 | |
| 643 | // Save the update start time. Reset the reboot count and attempt number if the |
| 644 | // update isn't a resume; otherwise increment the attempt number. |
| 645 | void UpdateAttempterAndroid::UpdatePrefsOnUpdateStart(bool is_resume) { |
| 646 | if (!is_resume) { |
| 647 | metrics_utils::SetNumReboots(0, prefs_); |
| 648 | metrics_utils::SetPayloadAttemptNumber(1, prefs_); |
| 649 | } else { |
| 650 | int64_t attempt_number = |
| 651 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 652 | metrics_utils::SetPayloadAttemptNumber(attempt_number + 1, prefs_); |
| 653 | } |
| 654 | Time update_start_time = clock_->GetMonotonicTime(); |
| 655 | metrics_utils::SetUpdateTimestampStart(update_start_time, prefs_); |
| 656 | } |
| 657 | |
| 658 | void UpdateAttempterAndroid::ClearMetricsPrefs() { |
| 659 | CHECK(prefs_); |
| 660 | prefs_->Delete(kPrefsNumReboots); |
| 661 | prefs_->Delete(kPrefsPayloadAttemptNumber); |
| 662 | prefs_->Delete(kPrefsSystemUpdatedMarker); |
| 663 | prefs_->Delete(kPrefsUpdateTimestampStart); |
| 664 | } |
| 665 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 666 | } // namespace chromeos_update_engine |