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