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" |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 34 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 35 | #include "update_engine/common/file_fetcher.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 36 | #include "update_engine/common/utils.h" |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 37 | #include "update_engine/daemon_state_interface.h" |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 38 | #include "update_engine/metrics_reporter_interface.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 39 | #include "update_engine/metrics_utils.h" |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 40 | #include "update_engine/network_selector.h" |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 41 | #include "update_engine/payload_consumer/delta_performer.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 42 | #include "update_engine/payload_consumer/download_action.h" |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 43 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 44 | #include "update_engine/payload_consumer/file_descriptor_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 45 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 46 | #include "update_engine/payload_consumer/payload_constants.h" |
| 47 | #include "update_engine/payload_consumer/payload_metadata.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 48 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 49 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 50 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 51 | #ifndef _UE_SIDELOAD |
| 52 | // Do not include support for external HTTP(s) urls when building |
| 53 | // update_engine_sideload. |
| 54 | #include "update_engine/libcurl_http_fetcher.h" |
| 55 | #endif |
| 56 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 57 | using base::Bind; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 58 | using base::Time; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 59 | using base::TimeDelta; |
| 60 | using base::TimeTicks; |
| 61 | using std::shared_ptr; |
| 62 | using std::string; |
| 63 | using std::vector; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 64 | using update_engine::UpdateEngineStatus; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 65 | |
| 66 | namespace chromeos_update_engine { |
| 67 | |
| 68 | namespace { |
| 69 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 70 | // Minimum threshold to broadcast an status update in progress and time. |
| 71 | const double kBroadcastThresholdProgress = 0.01; // 1% |
| 72 | const int kBroadcastThresholdSeconds = 10; |
| 73 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 74 | const char* const kErrorDomain = "update_engine"; |
| 75 | // TODO(deymo): Convert the different errors to a numeric value to report them |
| 76 | // back on the service error. |
| 77 | const char* const kGenericError = "generic_error"; |
| 78 | |
| 79 | // Log and set the error on the passed ErrorPtr. |
| 80 | bool LogAndSetError(brillo::ErrorPtr* error, |
| 81 | const tracked_objects::Location& location, |
| 82 | const string& reason) { |
| 83 | brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason); |
| 84 | LOG(ERROR) << "Replying with failure: " << location.ToString() << ": " |
| 85 | << reason; |
| 86 | return false; |
| 87 | } |
| 88 | |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 89 | bool GetHeaderAsBool(const string& header, bool default_value) { |
| 90 | int value = 0; |
| 91 | if (base::StringToInt(header, &value) && (value == 0 || value == 1)) |
| 92 | return value == 1; |
| 93 | return default_value; |
| 94 | } |
| 95 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 96 | } // namespace |
| 97 | |
| 98 | UpdateAttempterAndroid::UpdateAttempterAndroid( |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 99 | DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 100 | PrefsInterface* prefs, |
| 101 | BootControlInterface* boot_control, |
| 102 | HardwareInterface* hardware) |
| 103 | : daemon_state_(daemon_state), |
| 104 | prefs_(prefs), |
| 105 | boot_control_(boot_control), |
| 106 | hardware_(hardware), |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 107 | processor_(new ActionProcessor()), |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 108 | clock_(new Clock()) { |
| 109 | metrics_reporter_ = metrics::CreateMetricsReporter(); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 110 | network_selector_ = network::CreateNetworkSelector(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | UpdateAttempterAndroid::~UpdateAttempterAndroid() { |
| 114 | // Release ourselves as the ActionProcessor's delegate to prevent |
| 115 | // re-scheduling the updates due to the processing stopped. |
| 116 | processor_->set_delegate(nullptr); |
| 117 | } |
| 118 | |
| 119 | void UpdateAttempterAndroid::Init() { |
| 120 | // In case of update_engine restart without a reboot we need to restore the |
| 121 | // reboot needed state. |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 122 | if (UpdateCompletedOnThisBoot()) { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 123 | SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 124 | } else { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 125 | SetStatusAndNotify(UpdateStatus::IDLE); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 126 | UpdatePrefsAndReportUpdateMetricsOnReboot(); |
| 127 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool UpdateAttempterAndroid::ApplyPayload( |
| 131 | const string& payload_url, |
| 132 | int64_t payload_offset, |
| 133 | int64_t payload_size, |
| 134 | const vector<string>& key_value_pair_headers, |
| 135 | brillo::ErrorPtr* error) { |
| 136 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 137 | return LogAndSetError( |
| 138 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 139 | } |
| 140 | if (ongoing_update_) { |
| 141 | return LogAndSetError( |
| 142 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 143 | } |
| 144 | DCHECK(status_ == UpdateStatus::IDLE); |
| 145 | |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 146 | std::map<string, string> headers; |
| 147 | for (const string& key_value_pair : key_value_pair_headers) { |
| 148 | string key; |
| 149 | string value; |
| 150 | if (!brillo::string_utils::SplitAtFirst( |
| 151 | key_value_pair, "=", &key, &value, false)) { |
| 152 | return LogAndSetError( |
| 153 | error, FROM_HERE, "Passed invalid header: " + key_value_pair); |
| 154 | } |
| 155 | if (!headers.emplace(key, value).second) |
| 156 | return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key); |
| 157 | } |
| 158 | |
| 159 | // Unique identifier for the payload. An empty string means that the payload |
| 160 | // can't be resumed. |
| 161 | string payload_id = (headers[kPayloadPropertyFileHash] + |
| 162 | headers[kPayloadPropertyMetadataHash]); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 163 | |
| 164 | // Setup the InstallPlan based on the request. |
| 165 | install_plan_ = InstallPlan(); |
| 166 | |
| 167 | install_plan_.download_url = payload_url; |
| 168 | install_plan_.version = ""; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 169 | base_offset_ = payload_offset; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 170 | InstallPlan::Payload payload; |
| 171 | payload.size = payload_size; |
| 172 | if (!payload.size) { |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 173 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 174 | &payload.size)) { |
| 175 | payload.size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 178 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 179 | &payload.hash)) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 180 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 181 | << headers[kPayloadPropertyFileHash]; |
| 182 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 183 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 184 | &payload.metadata_size)) { |
| 185 | payload.metadata_size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 186 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 187 | // The |payload.type| is not used anymore since minor_version 3. |
| 188 | payload.type = InstallPayloadType::kUnknown; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 189 | install_plan_.payloads.push_back(payload); |
| 190 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 191 | // The |public_key_rsa| key would override the public key stored on disk. |
| 192 | install_plan_.public_key_rsa = ""; |
| 193 | |
| 194 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 195 | install_plan_.is_resume = !payload_id.empty() && |
| 196 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 197 | if (!install_plan_.is_resume) { |
| 198 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 199 | LOG(WARNING) << "Unable to reset the update progress."; |
| 200 | } |
| 201 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 202 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 203 | } |
| 204 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 205 | install_plan_.source_slot = boot_control_->GetCurrentSlot(); |
| 206 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 207 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 208 | install_plan_.powerwash_required = |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 209 | GetHeaderAsBool(headers[kPayloadPropertyPowerwash], false); |
| 210 | |
| 211 | install_plan_.switch_slot_on_reboot = |
| 212 | GetHeaderAsBool(headers[kPayloadPropertySwitchSlotOnReboot], true); |
| 213 | |
| 214 | install_plan_.run_post_install = true; |
| 215 | // Optionally skip post install if and only if: |
| 216 | // a) we're resuming |
| 217 | // b) post install has already succeeded before |
| 218 | // c) RUN_POST_INSTALL is set to 0. |
| 219 | if (install_plan_.is_resume && prefs_->Exists(kPrefsPostInstallSucceeded)) { |
| 220 | bool post_install_succeeded = false; |
| 221 | prefs_->GetBoolean(kPrefsPostInstallSucceeded, &post_install_succeeded); |
| 222 | if (post_install_succeeded) { |
| 223 | install_plan_.run_post_install = |
| 224 | GetHeaderAsBool(headers[kPayloadPropertyRunPostInstall], true); |
| 225 | } |
| 226 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 227 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 228 | NetworkId network_id = kDefaultNetworkId; |
| 229 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 230 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 231 | &network_id)) { |
| 232 | return LogAndSetError( |
| 233 | error, |
| 234 | FROM_HERE, |
| 235 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 236 | } |
| 237 | if (!network_selector_->SetProcessNetwork(network_id)) { |
Sen Jiang | cbd37c6 | 2017-09-12 15:04:35 -0700 | [diff] [blame] | 238 | return LogAndSetError( |
| 239 | error, |
| 240 | FROM_HERE, |
| 241 | "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 245 | LOG(INFO) << "Using this install plan:"; |
| 246 | install_plan_.Dump(); |
| 247 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 248 | BuildUpdateActions(payload_url); |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 249 | // Setup extra headers. |
| 250 | HttpFetcher* fetcher = download_action_->http_fetcher(); |
| 251 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 252 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 253 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 254 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 255 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 256 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 257 | ongoing_update_ = true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 258 | |
| 259 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 260 | // before any update processing starts. This will start the update process. |
| 261 | UpdateBootFlags(); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 262 | |
| 263 | UpdatePrefsOnUpdateStart(install_plan_.is_resume); |
| 264 | // TODO(xunchang) report the metrics for unresumable updates |
| 265 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | |
| 269 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 270 | if (!ongoing_update_) |
| 271 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 272 | processor_->SuspendProcessing(); |
| 273 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 277 | if (!ongoing_update_) |
| 278 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 279 | processor_->ResumeProcessing(); |
| 280 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 284 | if (!ongoing_update_) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 285 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 286 | processor_->StopProcessing(); |
| 287 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 290 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 291 | LOG(INFO) << "Attempting to reset state from " |
| 292 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
| 293 | |
| 294 | switch (status_) { |
| 295 | case UpdateStatus::IDLE: |
| 296 | return true; |
| 297 | |
| 298 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
| 299 | // Remove the reboot marker so that if the machine is rebooted |
| 300 | // after resetting to idle state, it doesn't go back to |
| 301 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 302 | bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 303 | ClearMetricsPrefs(); |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 304 | |
| 305 | // Update the boot flags so the current slot has higher priority. |
| 306 | if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot())) |
| 307 | ret_value = false; |
| 308 | |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 309 | // Mark the current slot as successful again, since marking it as active |
| 310 | // may reset the successful bit. We ignore the result of whether marking |
| 311 | // the current slot as successful worked. |
| 312 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){}))) |
| 313 | ret_value = false; |
| 314 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 315 | if (!ret_value) { |
| 316 | return LogAndSetError( |
| 317 | error, |
| 318 | FROM_HERE, |
| 319 | "Failed to reset the status to "); |
| 320 | } |
| 321 | |
| 322 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 323 | LOG(INFO) << "Reset status successful"; |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | default: |
| 328 | return LogAndSetError( |
| 329 | error, |
| 330 | FROM_HERE, |
| 331 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 332 | } |
| 333 | } |
| 334 | |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 335 | bool UpdateAttempterAndroid::VerifyPayloadApplicable( |
| 336 | const std::string& metadata_filename, brillo::ErrorPtr* error) { |
| 337 | FileDescriptorPtr fd(new EintrSafeFileDescriptor); |
| 338 | if (!fd->Open(metadata_filename.c_str(), O_RDONLY)) { |
| 339 | return LogAndSetError( |
| 340 | error, FROM_HERE, "Failed to open " + metadata_filename); |
| 341 | } |
| 342 | brillo::Blob metadata(kMaxPayloadHeaderSize); |
| 343 | if (!fd->Read(metadata.data(), metadata.size())) { |
| 344 | return LogAndSetError( |
| 345 | error, |
| 346 | FROM_HERE, |
| 347 | "Failed to read payload header from " + metadata_filename); |
| 348 | } |
| 349 | ErrorCode errorcode; |
| 350 | PayloadMetadata payload_metadata; |
Sen Jiang | dcaf797 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 351 | if (payload_metadata.ParsePayloadHeader(metadata, &errorcode) != |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 352 | MetadataParseResult::kSuccess) { |
| 353 | return LogAndSetError(error, |
| 354 | FROM_HERE, |
| 355 | "Failed to parse payload header: " + |
| 356 | utils::ErrorCodeToString(errorcode)); |
| 357 | } |
| 358 | metadata.resize(payload_metadata.GetMetadataSize() + |
| 359 | payload_metadata.GetMetadataSignatureSize()); |
| 360 | if (metadata.size() < kMaxPayloadHeaderSize) { |
| 361 | return LogAndSetError( |
| 362 | error, |
| 363 | FROM_HERE, |
| 364 | "Metadata size too small: " + std::to_string(metadata.size())); |
| 365 | } |
| 366 | if (!fd->Read(metadata.data() + kMaxPayloadHeaderSize, |
| 367 | metadata.size() - kMaxPayloadHeaderSize)) { |
| 368 | return LogAndSetError( |
| 369 | error, |
| 370 | FROM_HERE, |
| 371 | "Failed to read metadata and signature from " + metadata_filename); |
| 372 | } |
| 373 | fd->Close(); |
| 374 | errorcode = payload_metadata.ValidateMetadataSignature( |
| 375 | metadata, "", base::FilePath(constants::kUpdatePayloadPublicKeyPath)); |
| 376 | if (errorcode != ErrorCode::kSuccess) { |
| 377 | return LogAndSetError(error, |
| 378 | FROM_HERE, |
| 379 | "Failed to validate metadata signature: " + |
| 380 | utils::ErrorCodeToString(errorcode)); |
| 381 | } |
| 382 | DeltaArchiveManifest manifest; |
| 383 | if (!payload_metadata.GetManifest(metadata, &manifest)) { |
| 384 | return LogAndSetError(error, FROM_HERE, "Failed to parse manifest."); |
| 385 | } |
| 386 | |
| 387 | BootControlInterface::Slot current_slot = boot_control_->GetCurrentSlot(); |
| 388 | for (const PartitionUpdate& partition : manifest.partitions()) { |
| 389 | if (!partition.has_old_partition_info()) |
| 390 | continue; |
| 391 | string partition_path; |
| 392 | if (!boot_control_->GetPartitionDevice( |
| 393 | partition.partition_name(), current_slot, &partition_path)) { |
| 394 | return LogAndSetError( |
| 395 | error, |
| 396 | FROM_HERE, |
| 397 | "Failed to get partition device for " + partition.partition_name()); |
| 398 | } |
| 399 | if (!fd->Open(partition_path.c_str(), O_RDONLY)) { |
| 400 | return LogAndSetError( |
| 401 | error, FROM_HERE, "Failed to open " + partition_path); |
| 402 | } |
| 403 | for (const InstallOperation& operation : partition.operations()) { |
| 404 | if (!operation.has_src_sha256_hash()) |
| 405 | continue; |
| 406 | brillo::Blob source_hash; |
| 407 | if (!fd_utils::ReadAndHashExtents(fd, |
| 408 | operation.src_extents(), |
| 409 | manifest.block_size(), |
| 410 | &source_hash)) { |
| 411 | return LogAndSetError( |
| 412 | error, FROM_HERE, "Failed to hash " + partition_path); |
| 413 | } |
| 414 | if (!DeltaPerformer::ValidateSourceHash( |
| 415 | source_hash, operation, fd, &errorcode)) { |
| 416 | return false; |
| 417 | } |
| 418 | } |
| 419 | fd->Close(); |
| 420 | } |
| 421 | return true; |
| 422 | } |
| 423 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 424 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 425 | ErrorCode code) { |
| 426 | LOG(INFO) << "Processing Done."; |
| 427 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 428 | switch (code) { |
| 429 | case ErrorCode::kSuccess: |
| 430 | // Update succeeded. |
| 431 | WriteUpdateCompletedMarker(); |
| 432 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 433 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 434 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 435 | break; |
| 436 | |
| 437 | case ErrorCode::kFilesystemCopierError: |
| 438 | case ErrorCode::kNewRootfsVerificationError: |
| 439 | case ErrorCode::kNewKernelVerificationError: |
| 440 | case ErrorCode::kFilesystemVerifierError: |
| 441 | case ErrorCode::kDownloadStateInitializationError: |
| 442 | // Reset the ongoing update for these errors so it starts from the |
| 443 | // beginning next time. |
| 444 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 445 | LOG(INFO) << "Resetting update progress."; |
| 446 | break; |
| 447 | |
| 448 | default: |
| 449 | // Ignore all other error codes. |
| 450 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 451 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 452 | |
| 453 | TerminateUpdateAndNotify(code); |
| 454 | } |
| 455 | |
| 456 | void UpdateAttempterAndroid::ProcessingStopped( |
| 457 | const ActionProcessor* processor) { |
| 458 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 459 | } |
| 460 | |
| 461 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 462 | AbstractAction* action, |
| 463 | ErrorCode code) { |
| 464 | // Reset download progress regardless of whether or not the download |
| 465 | // action succeeded. |
| 466 | const string type = action->Type(); |
| 467 | if (type == DownloadAction::StaticType()) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 468 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 469 | } |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 470 | if (type == PostinstallRunnerAction::StaticType()) { |
| 471 | bool succeeded = |
| 472 | code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive; |
| 473 | prefs_->SetBoolean(kPrefsPostInstallSucceeded, succeeded); |
| 474 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 475 | if (code != ErrorCode::kSuccess) { |
| 476 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 477 | return; |
| 478 | } |
| 479 | if (type == DownloadAction::StaticType()) { |
| 480 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 485 | uint64_t bytes_received, |
| 486 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 487 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 488 | if (total) |
| 489 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 490 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 491 | download_progress_ = progress; |
| 492 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 493 | } else { |
| 494 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 495 | } |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 496 | |
| 497 | // Update the bytes downloaded in prefs. |
| 498 | int64_t current_bytes_downloaded = |
| 499 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, prefs_); |
| 500 | int64_t total_bytes_downloaded = |
| 501 | metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, prefs_); |
| 502 | prefs_->SetInt64(kPrefsCurrentBytesDownloaded, |
| 503 | current_bytes_downloaded + bytes_progressed); |
| 504 | prefs_->SetInt64(kPrefsTotalBytesDownloaded, |
| 505 | total_bytes_downloaded + bytes_progressed); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 509 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 510 | // download. |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | void UpdateAttempterAndroid::DownloadComplete() { |
| 515 | // Nothing needs to be done when the download completes. |
| 516 | } |
| 517 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 518 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 519 | // Self throttle based on progress. Also send notifications if progress is |
| 520 | // too slow. |
| 521 | if (progress == 1.0 || |
| 522 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 523 | TimeTicks::Now() - last_notify_time_ >= |
| 524 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 525 | download_progress_ = progress; |
| 526 | SetStatusAndNotify(status_); |
| 527 | } |
| 528 | } |
| 529 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 530 | void UpdateAttempterAndroid::UpdateBootFlags() { |
| 531 | if (updated_boot_flags_) { |
| 532 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 533 | CompleteUpdateBootFlags(true); |
| 534 | return; |
| 535 | } |
| 536 | // This is purely best effort. |
| 537 | LOG(INFO) << "Marking booted slot as good."; |
| 538 | if (!boot_control_->MarkBootSuccessfulAsync( |
| 539 | Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags, |
| 540 | base::Unretained(this)))) { |
| 541 | LOG(ERROR) << "Failed to mark current boot as successful."; |
| 542 | CompleteUpdateBootFlags(false); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) { |
| 547 | updated_boot_flags_ = true; |
| 548 | ScheduleProcessingStart(); |
| 549 | } |
| 550 | |
| 551 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 552 | LOG(INFO) << "Scheduling an action processor start."; |
| 553 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 554 | FROM_HERE, |
| 555 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 556 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 560 | if (status_ == UpdateStatus::IDLE) { |
| 561 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 562 | return; |
| 563 | } |
| 564 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 565 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 566 | actions_.clear(); |
| 567 | UpdateStatus new_status = |
| 568 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 569 | : UpdateStatus::IDLE); |
| 570 | SetStatusAndNotify(new_status); |
| 571 | ongoing_update_ = false; |
| 572 | |
Sen Jiang | b19c3ec | 2017-10-06 15:18:46 -0700 | [diff] [blame] | 573 | // The network id is only applicable to one download attempt and once it's |
| 574 | // done the network id should not be re-used anymore. |
| 575 | if (!network_selector_->SetProcessNetwork(kDefaultNetworkId)) { |
| 576 | LOG(WARNING) << "Unable to unbind network."; |
| 577 | } |
| 578 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 579 | for (auto observer : daemon_state_->service_observers()) |
| 580 | observer->SendPayloadApplicationComplete(error_code); |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 581 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 582 | CollectAndReportUpdateMetricsOnUpdateFinished(error_code); |
| 583 | ClearMetricsPrefs(); |
| 584 | if (error_code == ErrorCode::kSuccess) { |
| 585 | metrics_utils::SetSystemUpdatedMarker(clock_.get(), prefs_); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 586 | // Clear the total bytes downloaded if and only if the update succeeds. |
| 587 | prefs_->SetInt64(kPrefsTotalBytesDownloaded, 0); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 588 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) { |
| 592 | status_ = status; |
Sen Jiang | 2d1c87b | 2017-07-14 10:46:14 -0700 | [diff] [blame] | 593 | size_t payload_size = |
| 594 | install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 595 | UpdateEngineStatus status_to_send = {.status = status_, |
| 596 | .progress = download_progress_, |
| 597 | .new_size_bytes = payload_size}; |
| 598 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 599 | for (auto observer : daemon_state_->service_observers()) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 600 | observer->SendStatusUpdate(status_to_send); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 601 | } |
| 602 | last_notify_time_ = TimeTicks::Now(); |
| 603 | } |
| 604 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 605 | void UpdateAttempterAndroid::BuildUpdateActions(const string& url) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 606 | CHECK(!processor_->IsRunning()); |
| 607 | processor_->set_delegate(this); |
| 608 | |
| 609 | // Actions: |
| 610 | shared_ptr<InstallPlanAction> install_plan_action( |
| 611 | new InstallPlanAction(install_plan_)); |
| 612 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 613 | HttpFetcher* download_fetcher = nullptr; |
| 614 | if (FileFetcher::SupportedUrl(url)) { |
| 615 | DLOG(INFO) << "Using FileFetcher for file URL."; |
| 616 | download_fetcher = new FileFetcher(); |
| 617 | } else { |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 618 | #ifdef _UE_SIDELOAD |
| 619 | LOG(FATAL) << "Unsupported sideload URI: " << url; |
| 620 | #else |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 621 | LibcurlHttpFetcher* libcurl_fetcher = |
| 622 | new LibcurlHttpFetcher(&proxy_resolver_, hardware_); |
| 623 | libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload); |
| 624 | download_fetcher = libcurl_fetcher; |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 625 | #endif // _UE_SIDELOAD |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 626 | } |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 627 | shared_ptr<DownloadAction> download_action( |
| 628 | new DownloadAction(prefs_, |
| 629 | boot_control_, |
| 630 | hardware_, |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 631 | nullptr, // system_state, not used. |
| 632 | download_fetcher, // passes ownership |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 633 | true /* interactive */)); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 634 | shared_ptr<FilesystemVerifierAction> filesystem_verifier_action( |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 635 | new FilesystemVerifierAction()); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 636 | |
| 637 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 638 | new PostinstallRunnerAction(boot_control_, hardware_)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 639 | |
| 640 | download_action->set_delegate(this); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 641 | download_action->set_base_offset(base_offset_); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 642 | download_action_ = download_action; |
Alex Deymo | b6eef73 | 2016-06-10 12:58:11 -0700 | [diff] [blame] | 643 | postinstall_runner_action->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 644 | |
| 645 | actions_.push_back(shared_ptr<AbstractAction>(install_plan_action)); |
| 646 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 647 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 648 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
| 649 | |
| 650 | // Bond them together. We have to use the leaf-types when calling |
| 651 | // BondActions(). |
| 652 | BondActions(install_plan_action.get(), download_action.get()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 653 | BondActions(download_action.get(), filesystem_verifier_action.get()); |
| 654 | BondActions(filesystem_verifier_action.get(), |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 655 | postinstall_runner_action.get()); |
| 656 | |
| 657 | // Enqueue the actions. |
| 658 | for (const shared_ptr<AbstractAction>& action : actions_) |
| 659 | processor_->EnqueueAction(action.get()); |
| 660 | } |
| 661 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 662 | bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() { |
| 663 | string boot_id; |
| 664 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 665 | prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id); |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() { |
| 670 | // In case of an update_engine restart without a reboot, we stored the boot_id |
| 671 | // when the update was completed by setting a pref, so we can check whether |
| 672 | // the last update was on this boot or a previous one. |
| 673 | string boot_id; |
| 674 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 675 | |
| 676 | string update_completed_on_boot_id; |
| 677 | return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) && |
| 678 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, |
| 679 | &update_completed_on_boot_id) && |
| 680 | update_completed_on_boot_id == boot_id); |
| 681 | } |
| 682 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 683 | // Collect and report the android metrics when we terminate the update. |
| 684 | void UpdateAttempterAndroid::CollectAndReportUpdateMetricsOnUpdateFinished( |
| 685 | ErrorCode error_code) { |
| 686 | int64_t attempt_number = |
| 687 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 688 | PayloadType payload_type = kPayloadTypeFull; |
| 689 | int64_t payload_size = 0; |
| 690 | for (const auto& p : install_plan_.payloads) { |
| 691 | if (p.type == InstallPayloadType::kDelta) |
| 692 | payload_type = kPayloadTypeDelta; |
| 693 | payload_size += p.size; |
| 694 | } |
| 695 | |
| 696 | metrics::AttemptResult attempt_result = |
| 697 | metrics_utils::GetAttemptResult(error_code); |
| 698 | Time attempt_start_time = Time::FromInternalValue( |
| 699 | metrics_utils::GetPersistedValue(kPrefsUpdateTimestampStart, prefs_)); |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 700 | TimeDelta duration = clock_->GetBootTime() - attempt_start_time; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 701 | TimeDelta duration_uptime = clock_->GetMonotonicTime() - attempt_start_time; |
| 702 | |
| 703 | metrics_reporter_->ReportUpdateAttemptMetrics( |
| 704 | nullptr, // system_state |
| 705 | static_cast<int>(attempt_number), |
| 706 | payload_type, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 707 | duration, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 708 | duration_uptime, |
| 709 | payload_size, |
| 710 | attempt_result, |
| 711 | error_code); |
| 712 | |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 713 | int64_t current_bytes_downloaded = |
| 714 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, prefs_); |
| 715 | metrics_reporter_->ReportUpdateAttemptDownloadMetrics( |
| 716 | current_bytes_downloaded, |
| 717 | 0, |
| 718 | DownloadSource::kNumDownloadSources, |
| 719 | metrics::DownloadErrorCode::kUnset, |
| 720 | metrics::ConnectionType::kUnset); |
| 721 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 722 | if (error_code == ErrorCode::kSuccess) { |
| 723 | int64_t reboot_count = |
| 724 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 725 | string build_version; |
| 726 | prefs_->GetString(kPrefsPreviousVersion, &build_version); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 727 | |
| 728 | // For android metrics, we only care about the total bytes downloaded |
| 729 | // for all sources; for now we assume the only download source is |
| 730 | // HttpsServer. |
| 731 | int64_t total_bytes_downloaded = |
| 732 | metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, prefs_); |
| 733 | int64_t num_bytes_downloaded[kNumDownloadSources] = {}; |
| 734 | num_bytes_downloaded[DownloadSource::kDownloadSourceHttpsServer] = |
| 735 | total_bytes_downloaded; |
| 736 | |
| 737 | int download_overhead_percentage = 0; |
| 738 | if (current_bytes_downloaded > 0) { |
| 739 | download_overhead_percentage = |
| 740 | (total_bytes_downloaded - current_bytes_downloaded) * 100ull / |
| 741 | current_bytes_downloaded; |
| 742 | } |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 743 | metrics_reporter_->ReportSuccessfulUpdateMetrics( |
| 744 | static_cast<int>(attempt_number), |
| 745 | 0, // update abandoned count |
| 746 | payload_type, |
| 747 | payload_size, |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 748 | num_bytes_downloaded, |
| 749 | download_overhead_percentage, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 750 | duration, |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame^] | 751 | duration_uptime, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 752 | static_cast<int>(reboot_count), |
| 753 | 0); // url_switch_count |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | void UpdateAttempterAndroid::UpdatePrefsAndReportUpdateMetricsOnReboot() { |
| 758 | string current_boot_id; |
| 759 | TEST_AND_RETURN(utils::GetBootId(¤t_boot_id)); |
| 760 | // Example: [ro.build.version.incremental]: [4292972] |
| 761 | string current_version = |
| 762 | android::base::GetProperty("ro.build.version.incremental", ""); |
| 763 | TEST_AND_RETURN(!current_version.empty()); |
| 764 | |
| 765 | // If there's no record of previous version (e.g. due to a data wipe), we |
| 766 | // save the info of current boot and skip the metrics report. |
| 767 | if (!prefs_->Exists(kPrefsPreviousVersion)) { |
| 768 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 769 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
| 770 | ClearMetricsPrefs(); |
| 771 | return; |
| 772 | } |
| 773 | string previous_version; |
| 774 | // update_engine restarted under the same build. |
| 775 | // TODO(xunchang) identify and report rollback by checking UpdateMarker. |
| 776 | if (prefs_->GetString(kPrefsPreviousVersion, &previous_version) && |
| 777 | previous_version == current_version) { |
| 778 | string last_boot_id; |
| 779 | bool is_reboot = prefs_->Exists(kPrefsBootId) && |
| 780 | (prefs_->GetString(kPrefsBootId, &last_boot_id) && |
| 781 | last_boot_id != current_boot_id); |
| 782 | // Increment the reboot number if |kPrefsNumReboots| exists. That pref is |
| 783 | // set when we start a new update. |
| 784 | if (is_reboot && prefs_->Exists(kPrefsNumReboots)) { |
| 785 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 786 | int64_t reboot_count = |
| 787 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 788 | metrics_utils::SetNumReboots(reboot_count + 1, prefs_); |
| 789 | } |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | // Now that the build version changes, report the update metrics. |
| 794 | // TODO(xunchang) check the build version is larger than the previous one. |
| 795 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 796 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
| 797 | |
| 798 | bool previous_attempt_exists = prefs_->Exists(kPrefsPayloadAttemptNumber); |
| 799 | // |kPrefsPayloadAttemptNumber| should be cleared upon successful update. |
| 800 | if (previous_attempt_exists) { |
| 801 | metrics_reporter_->ReportAbnormallyTerminatedUpdateAttemptMetrics(); |
| 802 | } |
| 803 | |
| 804 | metrics_utils::LoadAndReportTimeToReboot( |
| 805 | metrics_reporter_.get(), prefs_, clock_.get()); |
| 806 | ClearMetricsPrefs(); |
| 807 | } |
| 808 | |
| 809 | // Save the update start time. Reset the reboot count and attempt number if the |
| 810 | // update isn't a resume; otherwise increment the attempt number. |
| 811 | void UpdateAttempterAndroid::UpdatePrefsOnUpdateStart(bool is_resume) { |
| 812 | if (!is_resume) { |
| 813 | metrics_utils::SetNumReboots(0, prefs_); |
| 814 | metrics_utils::SetPayloadAttemptNumber(1, prefs_); |
| 815 | } else { |
| 816 | int64_t attempt_number = |
| 817 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 818 | metrics_utils::SetPayloadAttemptNumber(attempt_number + 1, prefs_); |
| 819 | } |
| 820 | Time update_start_time = clock_->GetMonotonicTime(); |
| 821 | metrics_utils::SetUpdateTimestampStart(update_start_time, prefs_); |
| 822 | } |
| 823 | |
| 824 | void UpdateAttempterAndroid::ClearMetricsPrefs() { |
| 825 | CHECK(prefs_); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 826 | prefs_->Delete(kPrefsCurrentBytesDownloaded); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 827 | prefs_->Delete(kPrefsNumReboots); |
| 828 | prefs_->Delete(kPrefsPayloadAttemptNumber); |
| 829 | prefs_->Delete(kPrefsSystemUpdatedMarker); |
| 830 | prefs_->Delete(kPrefsUpdateTimestampStart); |
| 831 | } |
| 832 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 833 | } // namespace chromeos_update_engine |