blob: ad6994c6b48af19b43a0ed5dd0f4c5c3177358c8 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 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//
Alex Deymoc705cc82014-02-19 11:15:00 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_
Alex Deymoc705cc82014-02-19 11:15:00 -080019
Amin Hassanifbb600f2019-08-14 19:52:30 -070020#include <memory>
Alex Deymo0d11c602014-04-23 20:12:20 -070021#include <string>
Gilad Arnolddc4bb262014-07-23 10:45:19 -070022#include <tuple>
Gilad Arnoldb3b05442014-05-30 14:25:05 -070023#include <vector>
Alex Deymo0d11c602014-04-23 20:12:20 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/error_code.h"
Aaron Wood23bd3392017-10-06 14:48:25 -070026#include "update_engine/payload_consumer/install_plan.h"
Alex Deymo63784a52014-05-28 10:46:14 -070027#include "update_engine/update_manager/evaluation_context.h"
Marton Hunyady0e0e3542018-02-21 18:51:39 +010028#include "update_engine/update_manager/rollback_prefs.h"
Alex Deymo63784a52014-05-28 10:46:14 -070029#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080030
Alex Deymo63784a52014-05-28 10:46:14 -070031namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080032
33// The three different results of a policy request.
Alex Deymoe636c3c2014-03-11 19:02:08 -070034enum class EvalStatus {
35 kFailed,
36 kSucceeded,
37 kAskMeAgainLater,
Aaron Wood56d8ab32017-09-22 15:56:18 -070038 kContinue,
Alex Deymoc705cc82014-02-19 11:15:00 -080039};
40
Alex Deymo0d11c602014-04-23 20:12:20 -070041std::string ToString(EvalStatus status);
42
43// Parameters of an update check. These parameters are determined by the
44// UpdateCheckAllowed policy.
45struct UpdateCheckParams {
Amin Hassanie8153632020-10-27 15:11:28 -070046 // Whether the auto-updates are enabled on this build.
47 bool updates_enabled{true};
Gilad Arnold42f253b2014-06-25 12:39:17 -070048
49 // Attributes pertaining to the case where update checks are allowed.
50 //
Gilad Arnoldd4b30322014-07-21 15:35:27 -070051 // A target version prefix, if imposed by policy; otherwise, an empty string.
52 std::string target_version_prefix;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +020053 // Specifies whether rollback images are allowed by device policy.
Amin Hassanie8153632020-10-27 15:11:28 -070054 bool rollback_allowed{false};
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080055 // Specifies if rollbacks should attempt to preserve some system state.
Amin Hassanie8153632020-10-27 15:11:28 -070056 bool rollback_data_save_requested{false};
Marton Hunyady0e0e3542018-02-21 18:51:39 +010057 // Specifies the number of Chrome milestones rollback should be allowed,
58 // starting from the stable version at any time. Value is -1 if unspecified
59 // (e.g. no device policy is available yet), in this case no version
60 // roll-forward should happen.
Amin Hassanie8153632020-10-27 15:11:28 -070061 int rollback_allowed_milestones{0};
Miriam Polzeraff72002020-08-27 08:20:39 +020062 // Whether a rollback with data save should be initiated on channel
63 // downgrade (e.g. beta to stable).
64 bool rollback_on_channel_downgrade{false};
Gilad Arnold42f253b2014-06-25 12:39:17 -070065 // A target channel, if so imposed by policy; otherwise, an empty string.
66 std::string target_channel;
Amin Hassani37b67232020-08-13 09:29:48 -070067 // Specifies if the channel hint, e.g. LTS (Long Term Support) updates.
68 std::string lts_tag;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -070069
70 // Whether the allowed update is interactive (user-initiated) or periodic.
Amin Hassanie8153632020-10-27 15:11:28 -070071 bool interactive{false};
Alex Deymo0d11c602014-04-23 20:12:20 -070072};
73
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070074// Input arguments to UpdateCanStart.
75//
Gilad Arnolddc4bb262014-07-23 10:45:19 -070076// A snapshot of the state of the current update process. This includes
77// everything that a policy might need and that occurred since the first time
78// the current payload was first seen and attempted (consecutively).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070079struct UpdateState {
Gilad Arnolddc4bb262014-07-23 10:45:19 -070080 // Information pertaining to the current update payload and/or check.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070081 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -070082 // Whether the current update check is an interactive one. The caller should
83 // feed the value returned by the preceding call to UpdateCheckAllowed().
Amin Hassanied37d682018-04-06 13:22:00 -070084 bool interactive;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070085 // Whether it is a delta payload.
86 bool is_delta_payload;
87 // Wallclock time when payload was first (consecutively) offered by Omaha.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070088 base::Time first_seen;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070089 // Number of consecutive update checks returning the current update.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070090 int num_checks;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070091 // Number of update payload failures and the wallclock time when it was last
92 // updated by the updater. These should both be nullified whenever a new
93 // update is seen; they are updated at the policy's descretion (via
94 // UpdateDownloadParams.do_increment_failures) once all of the usable download
95 // URLs for the payload have been used without success. They should be
96 // persisted across reboots.
97 int num_failures;
98 base::Time failures_last_updated;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070099
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700100 // Information pertaining to downloading and applying of the current update.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700101 //
102 // An array of download URLs provided by Omaha.
103 std::vector<std::string> download_urls;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700104 // Max number of errors allowed per download URL.
105 int download_errors_max;
106 // The index of the URL to download from, as determined in the previous call
107 // to the policy. For a newly seen payload, this should be -1.
108 int last_download_url_idx;
109 // The number of successive download errors pertaining to this last URL, as
110 // determined in the previous call to the policy. For a newly seen payload,
111 // this should be zero.
112 int last_download_url_num_errors;
113 // An array of errors that occurred while trying to download this update since
114 // the previous call to this policy has returned, or since this payload was
115 // first seen, or since the updater process has started (whichever is later).
116 // Includes the URL index attempted, the error code, and the wallclock-based
117 // timestamp when it occurred.
118 std::vector<std::tuple<int, chromeos_update_engine::ErrorCode, base::Time>>
119 download_errors;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700120 // Whether Omaha forbids use of P2P for downloading and/or sharing.
121 bool p2p_downloading_disabled;
122 bool p2p_sharing_disabled;
Gilad Arnold349ac832014-10-06 14:20:28 -0700123 // The number of P2P download attempts and wallclock-based time when P2P
124 // download was first attempted.
125 int p2p_num_attempts;
126 base::Time p2p_first_attempted;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700127
128 // Information pertaining to update backoff mechanism.
129 //
130 // The currently known (persisted) wallclock-based backoff expiration time;
131 // zero if none.
132 base::Time backoff_expiry;
133 // Whether backoff is disabled by Omaha.
134 bool is_backoff_disabled;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700135
136 // Information pertaining to update scattering.
137 //
Sen Jiang771f6482018-04-04 17:59:10 -0700138 // The currently known (persisted) scattering wallclock-based wait period and
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700139 // update check threshold; zero if none.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700140 base::TimeDelta scatter_wait_period;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700141 int scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700142 // Maximum wait period allowed for this update, as determined by Omaha.
143 base::TimeDelta scatter_wait_period_max;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700144 // Minimum/maximum check threshold values.
145 // TODO(garnold) These appear to not be related to the current update and so
Gilad Arnoldddd3fe32014-05-22 12:57:09 -0700146 // should probably be obtained as variables via UpdaterProvider.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700147 int scatter_check_threshold_min;
148 int scatter_check_threshold_max;
149};
150
151// Results regarding the downloading and applying of an update, as determined by
152// UpdateCanStart.
153//
154// An enumerator for the reasons of not allowing an update to start.
155enum class UpdateCannotStartReason {
156 kUndefined,
157 kCheckDue,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700158 kScattering,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700159 kBackoff,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700160 kCannotDownload,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700161};
162
Gilad Arnold42f253b2014-06-25 12:39:17 -0700163struct UpdateDownloadParams {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700164 // Whether the update attempt is allowed to proceed.
165 bool update_can_start;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700166 // If update cannot proceed, a reason code for why it cannot do so.
167 UpdateCannotStartReason cannot_start_reason;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700168
Gilad Arnold14a9e702014-10-08 08:09:09 -0700169 // Download related attributes. The update engine uses them to choose the
170 // means for downloading and applying an update.
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700171 //
Gilad Arnold14a9e702014-10-08 08:09:09 -0700172 // The index of the download URL to use (-1 means no suitable URL was found)
173 // and whether it can be used. Even if there's no URL or its use is not
174 // allowed (backoff, scattering) there may still be other means for download
175 // (like P2P). The URL index needs to be persisted and handed back to the
176 // policy on the next time it is called.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700177 int download_url_idx;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700178 bool download_url_allowed;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700179 // The number of download errors associated with this download URL. This value
180 // needs to be persisted and handed back to the policy on the next time it is
181 // called.
182 int download_url_num_errors;
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700183 // Whether P2P download and sharing are allowed.
184 bool p2p_downloading_allowed;
185 bool p2p_sharing_allowed;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700186
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700187 // Other values that need to be persisted and handed to the policy as need on
188 // the next call.
189 //
190 // Whether an update failure has been identified by the policy. The client
191 // should increment and persist its update failure count, and record the time
192 // when this was done; it needs to hand these values back to the policy
193 // (UpdateState.{num_failures,failures_last_updated}) on the next time it is
194 // called.
195 bool do_increment_failures;
196 // The current backof expiry.
197 base::Time backoff_expiry;
198 // The scattering wait period and check threshold.
199 base::TimeDelta scatter_wait_period;
200 int scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700201};
Alex Deymo0d11c602014-04-23 20:12:20 -0700202
Alex Deymoc705cc82014-02-19 11:15:00 -0800203// The Policy class is an interface to the ensemble of policy requests that the
204// client can make. A derived class includes the policy implementations of
205// these.
206//
207// When compile-time selection of the policy is required due to missing or extra
208// parts in a given platform, a different Policy subclass can be used.
209class Policy {
210 public:
211 virtual ~Policy() {}
212
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700213 // Returns the name of a public policy request.
214 // IMPORTANT: Be sure to add a conditional for each new public policy that is
215 // being added to this class in the future.
Amin Hassani4b717432019-01-14 16:24:20 -0800216 template <typename R, typename... Args>
217 std::string PolicyRequestName(EvalStatus (Policy::*policy_method)(
218 EvaluationContext*, State*, std::string*, R*, Args...) const) const {
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700219 std::string class_name = PolicyName() + "::";
220
Amin Hassani4b717432019-01-14 16:24:20 -0800221 if (reinterpret_cast<typeof(&Policy::UpdateCheckAllowed)>(policy_method) ==
222 &Policy::UpdateCheckAllowed)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700223 return class_name + "UpdateCheckAllowed";
Aaron Wood23bd3392017-10-06 14:48:25 -0700224 if (reinterpret_cast<typeof(&Policy::UpdateCanBeApplied)>(policy_method) ==
225 &Policy::UpdateCanBeApplied)
226 return class_name + "UpdateCanBeApplied";
Amin Hassani4b717432019-01-14 16:24:20 -0800227 if (reinterpret_cast<typeof(&Policy::UpdateCanStart)>(policy_method) ==
228 &Policy::UpdateCanStart)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700229 return class_name + "UpdateCanStart";
Gilad Arnold684219d2014-07-07 14:54:57 -0700230 if (reinterpret_cast<typeof(&Policy::UpdateDownloadAllowed)>(
231 policy_method) == &Policy::UpdateDownloadAllowed)
232 return class_name + "UpdateDownloadAllowed";
Amin Hassani4b717432019-01-14 16:24:20 -0800233 if (reinterpret_cast<typeof(&Policy::P2PEnabled)>(policy_method) ==
234 &Policy::P2PEnabled)
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700235 return class_name + "P2PEnabled";
Amin Hassani4b717432019-01-14 16:24:20 -0800236 if (reinterpret_cast<typeof(&Policy::P2PEnabledChanged)>(policy_method) ==
237 &Policy::P2PEnabledChanged)
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700238 return class_name + "P2PEnabledChanged";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700239
240 NOTREACHED();
241 return class_name + "(unknown)";
242 }
243
Alex Deymoc705cc82014-02-19 11:15:00 -0800244 // List of policy requests. A policy request takes an EvaluationContext as the
Alex Deymo2de23f52014-02-26 14:30:13 -0800245 // first argument, a State instance, a returned error message, a returned
246 // value and optionally followed by one or more arbitrary constant arguments.
Alex Deymoc705cc82014-02-19 11:15:00 -0800247 //
Alex Deymoe636c3c2014-03-11 19:02:08 -0700248 // When the implementation fails, the method returns EvalStatus::kFailed and
249 // sets the |error| string.
Alex Deymoc705cc82014-02-19 11:15:00 -0800250
251 // UpdateCheckAllowed returns whether it is allowed to request an update check
252 // to Omaha.
Amin Hassani4b717432019-01-14 16:24:20 -0800253 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
254 State* state,
255 std::string* error,
256 UpdateCheckParams* result) const = 0;
Alex Deymoc705cc82014-02-19 11:15:00 -0800257
Aaron Wood23bd3392017-10-06 14:48:25 -0700258 // UpdateCanBeApplied returns whether the given |install_plan| can be acted
259 // on at this time. The reason for not applying is returned in |result|.
260 // The Policy may modify the passed-in |install_plan|, based on the
261 // implementation in the Policy and values provided by the EvaluationContext.
262 virtual EvalStatus UpdateCanBeApplied(
263 EvaluationContext* ec,
264 State* state,
265 std::string* error,
266 chromeos_update_engine::ErrorCode* result,
267 chromeos_update_engine::InstallPlan* install_plan) const = 0;
268
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700269 // Returns EvalStatus::kSucceeded if either an update can start being
270 // processed, or the attempt needs to be aborted. In cases where the update
271 // needs to wait for some condition to be satisfied, but none of the values
272 // that need to be persisted has changed, returns
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700273 // EvalStatus::kAskMeAgainLater. Arguments include an |update_state| that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700274 // encapsulates data pertaining to the current ongoing update process.
Amin Hassani4b717432019-01-14 16:24:20 -0800275 virtual EvalStatus UpdateCanStart(EvaluationContext* ec,
276 State* state,
277 std::string* error,
278 UpdateDownloadParams* result,
279 UpdateState update_state) const = 0;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700280
Gilad Arnold684219d2014-07-07 14:54:57 -0700281 // Checks whether downloading of an update is allowed; currently, this checks
282 // whether the network connection type is suitable for updating over. May
283 // consult the shill provider as well as the device policy (if available).
Gilad Arnold0adbc942014-05-12 10:35:43 -0700284 // Returns |EvalStatus::kSucceeded|, setting |result| according to whether or
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700285 // not the current connection can be used; on error, returns
Gilad Arnold0adbc942014-05-12 10:35:43 -0700286 // |EvalStatus::kFailed| and sets |error| accordingly.
Amin Hassani4b717432019-01-14 16:24:20 -0800287 virtual EvalStatus UpdateDownloadAllowed(EvaluationContext* ec,
288 State* state,
289 std::string* error,
290 bool* result) const = 0;
Gilad Arnold0adbc942014-05-12 10:35:43 -0700291
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700292 // Checks whether P2P is enabled. This may consult device policy and other
293 // global settings.
Amin Hassani4b717432019-01-14 16:24:20 -0800294 virtual EvalStatus P2PEnabled(EvaluationContext* ec,
295 State* state,
296 std::string* error,
297 bool* result) const = 0;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700298
299 // Checks whether P2P is enabled, but blocks (returns
300 // |EvalStatus::kAskMeAgainLater|) until it is different from |prev_result|.
301 // If the P2P enabled status is not expected to change, will return
302 // immediately with |EvalStatus::kSucceeded|. This internally uses the
303 // P2PEnabled() policy above.
Amin Hassani4b717432019-01-14 16:24:20 -0800304 virtual EvalStatus P2PEnabledChanged(EvaluationContext* ec,
305 State* state,
306 std::string* error,
307 bool* result,
308 bool prev_result) const = 0;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700309
Alex Deymoc705cc82014-02-19 11:15:00 -0800310 protected:
311 Policy() {}
312
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700313 // Returns the name of the actual policy class.
314 virtual std::string PolicyName() const = 0;
315
Alex Deymoc705cc82014-02-19 11:15:00 -0800316 private:
317 DISALLOW_COPY_AND_ASSIGN(Policy);
318};
319
Amin Hassanifbb600f2019-08-14 19:52:30 -0700320// Get system dependent (Chrome OS vs. Android) policy
321// implementation. Implementations can be found in chromeos_policy.cc and
322// android_things_policy.cc.
323std::unique_ptr<Policy> GetSystemPolicy();
324
Alex Deymo63784a52014-05-28 10:46:14 -0700325} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800326
Gilad Arnold48415f12014-06-27 07:10:58 -0700327#endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_