| 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 | #ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_ | 
|  | 18 | #define UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_ | 
|  | 19 |  | 
|  | 20 | #include <stdint.h> | 
|  | 21 |  | 
|  | 22 | #include <memory> | 
|  | 23 | #include <string> | 
|  | 24 | #include <vector> | 
|  | 25 |  | 
| Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 27 | #include <base/time/time.h> | 
|  | 28 |  | 
|  | 29 | #include "update_engine/client_library/include/update_engine/update_status.h" | 
|  | 30 | #include "update_engine/common/action_processor.h" | 
|  | 31 | #include "update_engine/common/boot_control_interface.h" | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 32 | #include "update_engine/common/clock.h" | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 33 | #include "update_engine/common/hardware_interface.h" | 
|  | 34 | #include "update_engine/common/prefs_interface.h" | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 35 | #include "update_engine/daemon_state_interface.h" | 
| Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 36 | #include "update_engine/metrics_reporter_interface.h" | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 37 | #include "update_engine/metrics_utils.h" | 
| Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 38 | #include "update_engine/network_selector_interface.h" | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 39 | #include "update_engine/payload_consumer/download_action.h" | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 40 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 41 | #include "update_engine/payload_consumer/postinstall_runner_action.h" | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 42 | #include "update_engine/service_delegate_android_interface.h" | 
|  | 43 | #include "update_engine/service_observer_interface.h" | 
|  | 44 |  | 
|  | 45 | namespace chromeos_update_engine { | 
|  | 46 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 47 | class UpdateAttempterAndroid | 
|  | 48 | : public ServiceDelegateAndroidInterface, | 
|  | 49 | public ActionProcessorDelegate, | 
|  | 50 | public DownloadActionDelegate, | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 51 | public FilesystemVerifyDelegate, | 
| Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 52 | public PostinstallRunnerAction::DelegateInterface, | 
|  | 53 | public CleanupPreviousUpdateActionDelegateInterface { | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 54 | public: | 
|  | 55 | using UpdateStatus = update_engine::UpdateStatus; | 
|  | 56 |  | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 57 | UpdateAttempterAndroid(DaemonStateInterface* daemon_state, | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 58 | PrefsInterface* prefs, | 
|  | 59 | BootControlInterface* boot_control_, | 
|  | 60 | HardwareInterface* hardware_); | 
|  | 61 | ~UpdateAttempterAndroid() override; | 
|  | 62 |  | 
|  | 63 | // Further initialization to be done post construction. | 
|  | 64 | void Init(); | 
|  | 65 |  | 
|  | 66 | // ServiceDelegateAndroidInterface overrides. | 
|  | 67 | bool ApplyPayload(const std::string& payload_url, | 
|  | 68 | int64_t payload_offset, | 
|  | 69 | int64_t payload_size, | 
|  | 70 | const std::vector<std::string>& key_value_pair_headers, | 
|  | 71 | brillo::ErrorPtr* error) override; | 
| Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 72 | bool ApplyPayload(int fd, | 
|  | 73 | int64_t payload_offset, | 
|  | 74 | int64_t payload_size, | 
|  | 75 | const std::vector<std::string>& key_value_pair_headers, | 
|  | 76 | brillo::ErrorPtr* error) override; | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 77 | bool SuspendUpdate(brillo::ErrorPtr* error) override; | 
|  | 78 | bool ResumeUpdate(brillo::ErrorPtr* error) override; | 
|  | 79 | bool CancelUpdate(brillo::ErrorPtr* error) override; | 
| Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 80 | bool ResetStatus(brillo::ErrorPtr* error) override; | 
| Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 81 | bool VerifyPayloadApplicable(const std::string& metadata_filename, | 
|  | 82 | brillo::ErrorPtr* error) override; | 
| Yifan Hong | 6f7e29f | 2019-12-13 14:41:06 -0800 | [diff] [blame] | 83 | uint64_t AllocateSpaceForPayload( | 
|  | 84 | const std::string& metadata_filename, | 
|  | 85 | const std::vector<std::string>& key_value_pair_headers, | 
|  | 86 | brillo::ErrorPtr* error) override; | 
| Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 87 | void CleanupSuccessfulUpdate( | 
|  | 88 | std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback, | 
|  | 89 | brillo::ErrorPtr* error) override; | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 90 |  | 
|  | 91 | // ActionProcessorDelegate methods: | 
|  | 92 | void ProcessingDone(const ActionProcessor* processor, | 
|  | 93 | ErrorCode code) override; | 
|  | 94 | void ProcessingStopped(const ActionProcessor* processor) override; | 
|  | 95 | void ActionCompleted(ActionProcessor* processor, | 
|  | 96 | AbstractAction* action, | 
|  | 97 | ErrorCode code) override; | 
|  | 98 |  | 
|  | 99 | // DownloadActionDelegate overrides. | 
|  | 100 | void BytesReceived(uint64_t bytes_progressed, | 
|  | 101 | uint64_t bytes_received, | 
|  | 102 | uint64_t total) override; | 
|  | 103 | bool ShouldCancel(ErrorCode* cancel_reason) override; | 
|  | 104 | void DownloadComplete() override; | 
|  | 105 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 106 | // FilesystemVerifyDelegate overrides | 
|  | 107 | void OnVerifyProgressUpdate(double progress) override; | 
|  | 108 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 109 | // PostinstallRunnerAction::DelegateInterface | 
|  | 110 | void ProgressUpdate(double progress) override; | 
|  | 111 |  | 
| Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 112 | // CleanupPreviousUpdateActionDelegateInterface | 
|  | 113 | void OnCleanupProgressUpdate(double progress) override; | 
|  | 114 |  | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 115 | private: | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 116 | friend class UpdateAttempterAndroidTest; | 
|  | 117 |  | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 118 | // Schedules an event loop callback to start the action processor. This is | 
|  | 119 | // scheduled asynchronously to unblock the event loop. | 
|  | 120 | void ScheduleProcessingStart(); | 
|  | 121 |  | 
|  | 122 | // Notifies an update request completed with the given error |code| to all | 
|  | 123 | // observers. | 
|  | 124 | void TerminateUpdateAndNotify(ErrorCode error_code); | 
|  | 125 |  | 
|  | 126 | // Sets the status to the given |status| and notifies a status update to | 
|  | 127 | // all observers. | 
|  | 128 | void SetStatusAndNotify(UpdateStatus status); | 
|  | 129 |  | 
|  | 130 | // Helper method to construct the sequence of actions to be performed for | 
| Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 131 | // applying an update using a given HttpFetcher. The ownership of |fetcher| is | 
|  | 132 | // passed to this function. | 
|  | 133 | void BuildUpdateActions(HttpFetcher* fetcher); | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 134 |  | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 135 | // Writes to the processing completed marker. Does nothing if | 
|  | 136 | // |update_completed_marker_| is empty. | 
|  | 137 | bool WriteUpdateCompletedMarker(); | 
|  | 138 |  | 
|  | 139 | // Returns whether an update was completed in the current boot. | 
|  | 140 | bool UpdateCompletedOnThisBoot(); | 
|  | 141 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 142 | // Prefs to use for metrics report | 
|  | 143 | // |kPrefsPayloadAttemptNumber|: number of update attempts for the current | 
|  | 144 | // payload_id. | 
|  | 145 | // |KprefsNumReboots|: number of reboots when applying the current update. | 
|  | 146 | // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update. | 
| Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 147 | // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the | 
|  | 148 | // current update. | 
|  | 149 | // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of | 
|  | 150 | // the current update. | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 151 | // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current | 
|  | 152 | // payload_id. | 
|  | 153 | // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since | 
|  | 154 | // the last successful update. | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 155 |  | 
|  | 156 | // Metrics report function to call: | 
|  | 157 | //   |ReportUpdateAttemptMetrics| | 
|  | 158 | //   |ReportSuccessfulUpdateMetrics| | 
|  | 159 | // Prefs to update: | 
|  | 160 | //   |kPrefsSystemUpdatedMarker| | 
|  | 161 | void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code); | 
|  | 162 |  | 
|  | 163 | // Metrics report function to call: | 
|  | 164 | //   |ReportAbnormallyTerminatedUpdateAttemptMetrics| | 
|  | 165 | //   |ReportTimeToRebootMetrics| | 
|  | 166 | // Prefs to update: | 
|  | 167 | //   |kPrefsBootId|, |kPrefsPreviousVersion| | 
|  | 168 | void UpdatePrefsAndReportUpdateMetricsOnReboot(); | 
|  | 169 |  | 
|  | 170 | // Prefs to update: | 
| Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 171 | //   |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|, | 
|  | 172 | //   |kPrefsUpdateBootTimestampStart| | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 173 | void UpdatePrefsOnUpdateStart(bool is_resume); | 
|  | 174 |  | 
|  | 175 | // Prefs to delete: | 
| xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 176 | //   |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded| | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 177 | //   |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|, | 
| xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 178 | //   |kPrefsUpdateBootTimestampStart| | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 179 | void ClearMetricsPrefs(); | 
|  | 180 |  | 
| Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 181 | // Return source and target slots for update. | 
|  | 182 | BootControlInterface::Slot GetCurrentSlot() const; | 
|  | 183 | BootControlInterface::Slot GetTargetSlot() const; | 
|  | 184 |  | 
|  | 185 | // Helper of public VerifyPayloadApplicable. Return the parsed manifest in | 
|  | 186 | // |manifest|. | 
|  | 187 | static bool VerifyPayloadParseManifest(const std::string& metadata_filename, | 
|  | 188 | DeltaArchiveManifest* manifest, | 
|  | 189 | brillo::ErrorPtr* error); | 
|  | 190 |  | 
| Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 191 | // Enqueue and run a CleanupPreviousUpdateAction. | 
|  | 192 | void ScheduleCleanupPreviousUpdate(); | 
|  | 193 |  | 
| Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 194 | // Notify and clear |cleanup_previous_update_callbacks_|. | 
|  | 195 | void NotifyCleanupPreviousUpdateCallbacksAndClear(); | 
|  | 196 |  | 
|  | 197 | // Remove |callback| from |cleanup_previous_update_callbacks_|. | 
|  | 198 | void RemoveCleanupPreviousUpdateCallback( | 
|  | 199 | CleanupSuccessfulUpdateCallbackInterface* callback); | 
|  | 200 |  | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 201 | DaemonStateInterface* daemon_state_; | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 202 |  | 
|  | 203 | // DaemonStateAndroid pointers. | 
|  | 204 | PrefsInterface* prefs_; | 
|  | 205 | BootControlInterface* boot_control_; | 
|  | 206 | HardwareInterface* hardware_; | 
|  | 207 |  | 
|  | 208 | // Last status notification timestamp used for throttling. Use monotonic | 
|  | 209 | // TimeTicks to ensure that notifications are sent even if the system clock is | 
|  | 210 | // set back in the middle of an update. | 
|  | 211 | base::TimeTicks last_notify_time_; | 
|  | 212 |  | 
| Amin Hassani | 04d4162 | 2018-12-20 15:35:41 -0800 | [diff] [blame] | 213 | // Only direct proxy supported. | 
|  | 214 | DirectProxyResolver proxy_resolver_; | 
|  | 215 |  | 
| Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 216 | // The processor for running Actions. | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 217 | std::unique_ptr<ActionProcessor> processor_; | 
|  | 218 |  | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 219 | // The InstallPlan used during the ongoing update. | 
|  | 220 | InstallPlan install_plan_; | 
|  | 221 |  | 
|  | 222 | // For status: | 
|  | 223 | UpdateStatus status_{UpdateStatus::IDLE}; | 
|  | 224 | double download_progress_{0.0}; | 
|  | 225 |  | 
| Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 226 | // The offset in the payload file where the CrAU part starts. | 
|  | 227 | int64_t base_offset_{0}; | 
|  | 228 |  | 
| Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 229 | // Helper class to select the network to use during the update. | 
|  | 230 | std::unique_ptr<NetworkSelectorInterface> network_selector_; | 
|  | 231 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 232 | std::unique_ptr<ClockInterface> clock_; | 
|  | 233 |  | 
| Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 234 | std::unique_ptr<MetricsReporterInterface> metrics_reporter_; | 
|  | 235 |  | 
| Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 236 | ::android::base::unique_fd payload_fd_; | 
|  | 237 |  | 
| Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 238 | std::vector<std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface>> | 
|  | 239 | cleanup_previous_update_callbacks_; | 
|  | 240 | // Result of previous CleanupPreviousUpdateAction. Nullopt If | 
|  | 241 | // CleanupPreviousUpdateAction has not been executed. | 
|  | 242 | std::optional<ErrorCode> cleanup_previous_update_code_{std::nullopt}; | 
|  | 243 |  | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 244 | DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid); | 
|  | 245 | }; | 
|  | 246 |  | 
|  | 247 | }  // namespace chromeos_update_engine | 
|  | 248 |  | 
|  | 249 | #endif  // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_ |