blob: 7a5a635518d7d0f9a33b117b4b9f6e75bf7d363f [file] [log] [blame]
Alex Deymo5e3ea272016-01-28 13:42:23 -08001//
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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_
18#define UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_
Alex Deymo5e3ea272016-01-28 13:42:23 -080019
20#include <stdint.h>
21
22#include <memory>
23#include <string>
24#include <vector>
25
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090026#include <android-base/unique_fd.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080027#include <base/time/time.h>
28
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000029#include "update_engine/aosp/apex_handler_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070030#include "update_engine/aosp/service_delegate_android_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080031#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"
Tianjie Xu90aaa102017-10-10 17:39:03 -070034#include "update_engine/common/clock.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070035#include "update_engine/common/daemon_state_interface.h"
36#include "update_engine/common/download_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080037#include "update_engine/common/hardware_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070038#include "update_engine/common/metrics_reporter_interface.h"
39#include "update_engine/common/network_selector_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080040#include "update_engine/common/prefs_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070041#include "update_engine/common/service_observer_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070042#include "update_engine/metrics_utils.h"
Kelvin Zhang70eef232020-06-12 20:32:40 +000043#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070044#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080045
46namespace chromeos_update_engine {
47
Alex Deymo0d298542016-03-30 18:31:49 -070048class UpdateAttempterAndroid
49 : public ServiceDelegateAndroidInterface,
50 public ActionProcessorDelegate,
51 public DownloadActionDelegate,
Kelvin Zhang70eef232020-06-12 20:32:40 +000052 public FilesystemVerifyDelegate,
Yifan Hong90965502020-02-19 15:22:47 -080053 public PostinstallRunnerAction::DelegateInterface,
54 public CleanupPreviousUpdateActionDelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080055 public:
56 using UpdateStatus = update_engine::UpdateStatus;
57
Alex Deymo03a4de72016-07-20 16:08:23 -070058 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080059 PrefsInterface* prefs,
60 BootControlInterface* boot_control_,
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000061 HardwareInterface* hardware_,
62 std::unique_ptr<ApexHandlerInterface> apex_handler);
Alex Deymo5e3ea272016-01-28 13:42:23 -080063 ~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;
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090074 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;
Alex Deymo5e3ea272016-01-28 13:42:23 -080079 bool SuspendUpdate(brillo::ErrorPtr* error) override;
80 bool ResumeUpdate(brillo::ErrorPtr* error) override;
81 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080082 bool ResetStatus(brillo::ErrorPtr* error) override;
Sen Jiang28d8ed92018-02-01 13:46:39 -080083 bool VerifyPayloadApplicable(const std::string& metadata_filename,
84 brillo::ErrorPtr* error) override;
Yifan Hong6f7e29f2019-12-13 14:41:06 -080085 uint64_t AllocateSpaceForPayload(
86 const std::string& metadata_filename,
87 const std::vector<std::string>& key_value_pair_headers,
88 brillo::ErrorPtr* error) override;
Yifan Hong40bb0d02020-02-24 17:33:14 -080089 void CleanupSuccessfulUpdate(
90 std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback,
91 brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080092
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
Kelvin Zhang70eef232020-06-12 20:32:40 +0000108 // FilesystemVerifyDelegate overrides
109 void OnVerifyProgressUpdate(double progress) override;
110
Alex Deymo0d298542016-03-30 18:31:49 -0700111 // PostinstallRunnerAction::DelegateInterface
112 void ProgressUpdate(double progress) override;
113
Yifan Hong90965502020-02-19 15:22:47 -0800114 // CleanupPreviousUpdateActionDelegateInterface
115 void OnCleanupProgressUpdate(double progress) override;
116
Alex Deymo5e3ea272016-01-28 13:42:23 -0800117 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -0700118 friend class UpdateAttempterAndroidTest;
119
Alex Deymo5e3ea272016-01-28 13:42:23 -0800120 // 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
Amin Hassani667cf7b2018-07-25 14:32:00 -0700133 // applying an update using a given HttpFetcher. The ownership of |fetcher| is
134 // passed to this function.
135 void BuildUpdateActions(HttpFetcher* fetcher);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800136
Alex Deymo5e3ea272016-01-28 13:42:23 -0800137 // 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
Tianjie Xu90aaa102017-10-10 17:39:03 -0700144 // 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.
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700149 // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
150 // current update.
151 // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
152 // the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700153 // |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.
Tianjie Xu90aaa102017-10-10 17:39:03 -0700157
158 // Metrics report function to call:
159 // |ReportUpdateAttemptMetrics|
160 // |ReportSuccessfulUpdateMetrics|
161 // Prefs to update:
162 // |kPrefsSystemUpdatedMarker|
163 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
164
Kelvin Zhang4061c512021-05-25 13:42:55 -0400165 // This function is called after update_engine is started after device
166 // reboots. If update_engine is restarted w/o device reboot, this function
167 // would not be called.
168
Tianjie Xu90aaa102017-10-10 17:39:03 -0700169 // Metrics report function to call:
170 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
171 // |ReportTimeToRebootMetrics|
172 // Prefs to update:
173 // |kPrefsBootId|, |kPrefsPreviousVersion|
Kelvin Zhang4061c512021-05-25 13:42:55 -0400174 void UpdateStateAfterReboot();
Tianjie Xu90aaa102017-10-10 17:39:03 -0700175
176 // Prefs to update:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700177 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
178 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700179 void UpdatePrefsOnUpdateStart(bool is_resume);
180
181 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800182 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700183 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800184 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700185 void ClearMetricsPrefs();
186
Yifan Hongbd47d622019-12-13 14:59:58 -0800187 // Return source and target slots for update.
188 BootControlInterface::Slot GetCurrentSlot() const;
189 BootControlInterface::Slot GetTargetSlot() const;
190
191 // Helper of public VerifyPayloadApplicable. Return the parsed manifest in
192 // |manifest|.
193 static bool VerifyPayloadParseManifest(const std::string& metadata_filename,
194 DeltaArchiveManifest* manifest,
195 brillo::ErrorPtr* error);
196
Yifan Hong90965502020-02-19 15:22:47 -0800197 // Enqueue and run a CleanupPreviousUpdateAction.
198 void ScheduleCleanupPreviousUpdate();
199
Yifan Hong40bb0d02020-02-24 17:33:14 -0800200 // Notify and clear |cleanup_previous_update_callbacks_|.
201 void NotifyCleanupPreviousUpdateCallbacksAndClear();
202
203 // Remove |callback| from |cleanup_previous_update_callbacks_|.
204 void RemoveCleanupPreviousUpdateCallback(
205 CleanupSuccessfulUpdateCallbackInterface* callback);
206
Alex Deymo03a4de72016-07-20 16:08:23 -0700207 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800208
209 // DaemonStateAndroid pointers.
210 PrefsInterface* prefs_;
211 BootControlInterface* boot_control_;
212 HardwareInterface* hardware_;
213
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000214 std::unique_ptr<ApexHandlerInterface> apex_handler_android_;
215
Alex Deymo5e3ea272016-01-28 13:42:23 -0800216 // Last status notification timestamp used for throttling. Use monotonic
217 // TimeTicks to ensure that notifications are sent even if the system clock is
218 // set back in the middle of an update.
219 base::TimeTicks last_notify_time_;
220
Amin Hassani04d41622018-12-20 15:35:41 -0800221 // Only direct proxy supported.
222 DirectProxyResolver proxy_resolver_;
223
Amin Hassani667cf7b2018-07-25 14:32:00 -0700224 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800225 std::unique_ptr<ActionProcessor> processor_;
226
Alex Deymo5e3ea272016-01-28 13:42:23 -0800227 // The InstallPlan used during the ongoing update.
228 InstallPlan install_plan_;
229
230 // For status:
231 UpdateStatus status_{UpdateStatus::IDLE};
232 double download_progress_{0.0};
233
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800234 // The offset in the payload file where the CrAU part starts.
235 int64_t base_offset_{0};
236
Alex Deymo87792ea2016-07-25 15:40:36 -0700237 // Helper class to select the network to use during the update.
238 std::unique_ptr<NetworkSelectorInterface> network_selector_;
239
Tianjie Xu90aaa102017-10-10 17:39:03 -0700240 std::unique_ptr<ClockInterface> clock_;
241
Tianjie Xu1b661142017-09-28 14:03:42 -0700242 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
243
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900244 ::android::base::unique_fd payload_fd_;
245
Yifan Hong40bb0d02020-02-24 17:33:14 -0800246 std::vector<std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface>>
247 cleanup_previous_update_callbacks_;
248 // Result of previous CleanupPreviousUpdateAction. Nullopt If
249 // CleanupPreviousUpdateAction has not been executed.
250 std::optional<ErrorCode> cleanup_previous_update_code_{std::nullopt};
251
Alex Deymo5e3ea272016-01-28 13:42:23 -0800252 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
253};
254
255} // namespace chromeos_update_engine
256
Amin Hassaniec7bc112020-10-29 16:47:58 -0700257#endif // UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_