blob: 70938bcdd51de93951da5727a0cf4f903056c8bb [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
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:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700173 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
174 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700175 void UpdatePrefsOnUpdateStart(bool is_resume);
176
177 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800178 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700179 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800180 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700181 void ClearMetricsPrefs();
182
Yifan Hongbd47d622019-12-13 14:59:58 -0800183 // 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
Yifan Hong90965502020-02-19 15:22:47 -0800193 // Enqueue and run a CleanupPreviousUpdateAction.
194 void ScheduleCleanupPreviousUpdate();
195
Yifan Hong40bb0d02020-02-24 17:33:14 -0800196 // 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
Alex Deymo03a4de72016-07-20 16:08:23 -0700203 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800204
205 // DaemonStateAndroid pointers.
206 PrefsInterface* prefs_;
207 BootControlInterface* boot_control_;
208 HardwareInterface* hardware_;
209
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000210 std::unique_ptr<ApexHandlerInterface> apex_handler_android_;
211
Alex Deymo5e3ea272016-01-28 13:42:23 -0800212 // 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
Amin Hassani04d41622018-12-20 15:35:41 -0800217 // Only direct proxy supported.
218 DirectProxyResolver proxy_resolver_;
219
Amin Hassani667cf7b2018-07-25 14:32:00 -0700220 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800221 std::unique_ptr<ActionProcessor> processor_;
222
Alex Deymo5e3ea272016-01-28 13:42:23 -0800223 // 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
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800230 // The offset in the payload file where the CrAU part starts.
231 int64_t base_offset_{0};
232
Alex Deymo87792ea2016-07-25 15:40:36 -0700233 // Helper class to select the network to use during the update.
234 std::unique_ptr<NetworkSelectorInterface> network_selector_;
235
Tianjie Xu90aaa102017-10-10 17:39:03 -0700236 std::unique_ptr<ClockInterface> clock_;
237
Tianjie Xu1b661142017-09-28 14:03:42 -0700238 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
239
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900240 ::android::base::unique_fd payload_fd_;
241
Yifan Hong40bb0d02020-02-24 17:33:14 -0800242 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
Alex Deymo5e3ea272016-01-28 13:42:23 -0800248 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
249};
250
251} // namespace chromeos_update_engine
252
Amin Hassaniec7bc112020-10-29 16:47:58 -0700253#endif // UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_