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