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