blob: 6420b6534ca9203d89371d19b30fbe42deb4a335 [file] [log] [blame]
Casey Dahlina93cd532016-01-14 16:55:11 -08001//
2// Copyright (C) 2012 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#include "update_engine/common_service.h"
18
Casey Dahlina93cd532016-01-14 16:55:11 -080019#include <string>
20
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090021#include <base/bind.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080022#include <base/location.h>
23#include <base/logging.h>
24#include <base/strings/stringprintf.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080025#include <brillo/message_loops/message_loop.h>
26#include <brillo/strings/string_utils.h>
27#include <policy/device_policy.h>
28
29#include "update_engine/common/clock_interface.h"
30#include "update_engine/common/hardware_interface.h"
31#include "update_engine/common/prefs.h"
32#include "update_engine/common/utils.h"
33#include "update_engine/connection_manager_interface.h"
34#include "update_engine/omaha_request_params.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070035#include "update_engine/omaha_utils.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080036#include "update_engine/p2p_manager.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080037#include "update_engine/payload_state_interface.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070038#include "update_engine/update_attempter.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080039
40using base::StringPrintf;
41using brillo::ErrorPtr;
42using brillo::string_utils::ToString;
Casey Dahlina93cd532016-01-14 16:55:11 -080043using std::string;
Xiaochu Liu88d90382018-08-29 16:09:11 -070044using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070045using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070046using update_engine::UpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080047
48namespace chromeos_update_engine {
49
50namespace {
51// Log and set the error on the passed ErrorPtr.
52void LogAndSetError(ErrorPtr* error,
53 const tracked_objects::Location& location,
54 const string& reason) {
55 brillo::Error::AddTo(error,
56 location,
57 UpdateEngineService::kErrorDomain,
58 UpdateEngineService::kErrorFailed,
59 reason);
60 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
61 << reason;
62}
63} // namespace
64
65const char* const UpdateEngineService::kErrorDomain = "update_engine";
66const char* const UpdateEngineService::kErrorFailed =
67 "org.chromium.UpdateEngine.Error.Failed";
68
69UpdateEngineService::UpdateEngineService(SystemState* system_state)
70 : system_state_(system_state) {
71}
72
73// org::chromium::UpdateEngineInterfaceInterface methods implementation.
74
Aaron Woodbf5a2522017-10-04 10:58:36 -070075bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
76 int32_t in_flags_as_int) {
77 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
78 LOG(INFO) << "Setting Update Attempt Flags: "
79 << "flags=0x" << std::hex << flags << " "
80 << "RestrictDownload="
81 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
82 : "no");
83 system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
84 return true;
85}
86
Casey Dahlina93cd532016-01-14 16:55:11 -080087bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
88 const string& in_app_version,
89 const string& in_omaha_url,
Aaron Wood081c0232017-10-19 17:14:58 -070090 int32_t in_flags_as_int,
91 bool* out_result) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070092 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
93 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
Aaron Wood081c0232017-10-19 17:14:58 -070094 bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
Casey Dahlina93cd532016-01-14 16:55:11 -080095
96 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
97 << "omaha_url=\"" << in_omaha_url << "\" "
98 << "flags=0x" << std::hex << flags << " "
Aaron Wood081c0232017-10-19 17:14:58 -070099 << "interactive=" << (interactive ? "yes " : "no ")
100 << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
101
102 *out_result = system_state_->update_attempter()->CheckForUpdate(
103 in_app_version, in_omaha_url, flags);
Casey Dahlina93cd532016-01-14 16:55:11 -0800104 return true;
105}
106
Xiaochu Liu88d90382018-08-29 16:09:11 -0700107bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
108 const string& omaha_url,
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800109 const vector<string>& dlc_module_ids) {
110 if (!system_state_->update_attempter()->CheckForInstall(dlc_module_ids,
111 omaha_url)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700112 // TODO(xiaochu): support more detailed error messages.
113 LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
114 return false;
115 }
116 return true;
117}
118
Casey Dahlina93cd532016-01-14 16:55:11 -0800119bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
120 LOG(INFO) << "Attempting rollback to non-active partitions.";
121
122 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
123 // TODO(dgarrett): Give a more specific error code/reason.
124 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
125 return false;
126 }
127 return true;
128}
129
130bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
131 bool* out_can_rollback) {
132 bool can_rollback = system_state_->update_attempter()->CanRollback();
133 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
134 *out_can_rollback = can_rollback;
135 return true;
136}
137
138bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
139 if (!system_state_->update_attempter()->ResetStatus()) {
140 // TODO(dgarrett): Give a more specific error code/reason.
141 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
142 return false;
143 }
144 return true;
145}
146
147bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700148 UpdateEngineStatus* out_status) {
149 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800150 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
151 return false;
152 }
153 return true;
154}
155
156bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
157 if (!system_state_->update_attempter()->RebootIfNeeded()) {
158 // TODO(dgarrett): Give a more specific error code/reason.
159 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
160 return false;
161 }
162 return true;
163}
164
165bool UpdateEngineService::SetChannel(ErrorPtr* error,
166 const string& in_target_channel,
167 bool in_is_powerwash_allowed) {
168 const policy::DevicePolicy* device_policy = system_state_->device_policy();
169
170 // The device_policy is loaded in a lazy way before an update check. Load it
171 // now from the libbrillo cache if it wasn't already loaded.
172 if (!device_policy) {
173 UpdateAttempter* update_attempter = system_state_->update_attempter();
174 if (update_attempter) {
175 update_attempter->RefreshDevicePolicy();
176 device_policy = system_state_->device_policy();
177 }
178 }
179
180 bool delegated = false;
181 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
182 !delegated) {
183 LogAndSetError(error,
184 FROM_HERE,
185 "Cannot set target channel explicitly when channel "
186 "policy/settings is not delegated");
187 return false;
188 }
189
190 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
191 string error_message;
192 if (!system_state_->request_params()->SetTargetChannel(
193 in_target_channel, in_is_powerwash_allowed, &error_message)) {
194 LogAndSetError(error, FROM_HERE, error_message);
195 return false;
196 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800197 return true;
198}
199
200bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
201 bool in_get_current_channel,
202 string* out_channel) {
203 OmahaRequestParams* rp = system_state_->request_params();
204 *out_channel =
205 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
206 return true;
207}
208
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700209bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
210 string in_cohort_hint) {
211 PrefsInterface* prefs = system_state_->prefs();
212
213 // It is ok to override the cohort hint with an invalid value since it is
214 // stored in stateful partition. The code reading it should sanitize it
215 // anyway.
216 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
217 LogAndSetError(
218 error,
219 FROM_HERE,
220 StringPrintf("Error setting the cohort hint value to \"%s\".",
221 in_cohort_hint.c_str()));
222 return false;
223 }
224 return true;
225}
226
227bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
228 string* out_cohort_hint) {
229 PrefsInterface* prefs = system_state_->prefs();
230
231 *out_cohort_hint = "";
232 if (prefs->Exists(kPrefsOmahaCohortHint) &&
233 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
234 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
235 return false;
236 }
237 return true;
238}
239
Casey Dahlina93cd532016-01-14 16:55:11 -0800240bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
241 bool in_enabled) {
242 PrefsInterface* prefs = system_state_->prefs();
243
244 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
245 LogAndSetError(
246 error,
247 FROM_HERE,
248 StringPrintf("Error setting the update via p2p permission to %s.",
249 ToString(in_enabled).c_str()));
250 return false;
251 }
252 return true;
253}
254
255bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
256 bool* out_enabled) {
257 PrefsInterface* prefs = system_state_->prefs();
258
259 bool p2p_pref = false; // Default if no setting is present.
260 if (prefs->Exists(kPrefsP2PEnabled) &&
261 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
262 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
263 return false;
264 }
265
266 *out_enabled = p2p_pref;
267 return true;
268}
269
270bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
271 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700272 ConnectionManagerInterface* connection_manager =
273 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800274
275 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700276 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700277 LogAndSetError(error,
278 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800279 "Ignoring the update over cellular setting since there's "
280 "a device policy enforcing this setting.");
281 return false;
282 }
283
284 // If the policy wasn't loaded yet, then it is still OK to change the local
285 // setting because the policy will be checked again during the update check.
286
287 PrefsInterface* prefs = system_state_->prefs();
288
Weidong Guo421ff332017-04-17 10:08:38 -0700289 if (!prefs ||
290 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700291 LogAndSetError(error,
292 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800293 string("Error setting the update over cellular to ") +
294 (in_allowed ? "true" : "false"));
295 return false;
296 }
297 return true;
298}
299
Weidong Guo421ff332017-04-17 10:08:38 -0700300bool UpdateEngineService::SetUpdateOverCellularTarget(
301 brillo::ErrorPtr* error,
302 const std::string& target_version,
303 int64_t target_size) {
304 ConnectionManagerInterface* connection_manager =
305 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700306
Weidong Guo421ff332017-04-17 10:08:38 -0700307 // Check if this setting is allowed by the device policy.
308 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
309 LogAndSetError(error,
310 FROM_HERE,
311 "Ignoring the update over cellular setting since there's "
312 "a device policy enforcing this setting.");
313 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700314 }
Tao Bao5688d162017-06-06 13:09:06 -0700315
Weidong Guo421ff332017-04-17 10:08:38 -0700316 // If the policy wasn't loaded yet, then it is still OK to change the local
317 // setting because the policy will be checked again during the update check.
318
319 PrefsInterface* prefs = system_state_->prefs();
320
321 if (!prefs ||
322 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
323 target_version) ||
324 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
325 LogAndSetError(
326 error, FROM_HERE, "Error setting the target for update over cellular.");
327 return false;
328 }
329 return true;
330}
331
332bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
333 bool* out_allowed) {
334 ConnectionManagerInterface* connection_manager =
335 system_state_->connection_manager();
336
337 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
338 // We have device policy, so ignore the user preferences.
339 *out_allowed = connection_manager->IsUpdateAllowedOver(
340 ConnectionType::kCellular, ConnectionTethering::kUnknown);
341 } else {
342 PrefsInterface* prefs = system_state_->prefs();
343
344 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
345 // Update is not allowed as user preference is not set or not available.
346 *out_allowed = false;
347 return true;
348 }
349
350 bool is_allowed;
351
352 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
353 LogAndSetError(error,
354 FROM_HERE,
355 "Error getting the update over cellular preference.");
356 return false;
357 }
358 *out_allowed = is_allowed;
359 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800360 return true;
361}
362
363bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
364 int64_t* out_usec_wallclock) {
365 base::Time time;
366 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
367 LogAndSetError(error, FROM_HERE, "No pending update.");
368 return false;
369 }
370
371 ClockInterface* clock = system_state_->clock();
372 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
373 return true;
374}
375
376bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
377 string* out_prev_version) {
378 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
379 return true;
380}
381
382bool UpdateEngineService::GetRollbackPartition(
383 ErrorPtr* /* error */, string* out_rollback_partition_name) {
384 BootControlInterface::Slot rollback_slot =
385 system_state_->update_attempter()->GetRollbackSlot();
386
387 if (rollback_slot == BootControlInterface::kInvalidSlot) {
388 out_rollback_partition_name->clear();
389 return true;
390 }
391
392 string name;
393 if (!system_state_->boot_control()->GetPartitionDevice(
394 "KERNEL", rollback_slot, &name)) {
395 LOG(ERROR) << "Invalid rollback device";
396 return false;
397 }
398
399 LOG(INFO) << "Getting rollback partition name. Result: " << name;
400 *out_rollback_partition_name = name;
401 return true;
402}
403
Shuqian Zhao29971732016-02-05 11:29:32 -0800404bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
405 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700406 ErrorCode error_code =
407 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800408 *out_last_attempt_error = static_cast<int>(error_code);
409 return true;
410}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700411
412bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
413 int32_t* out_eol_status) {
414 PrefsInterface* prefs = system_state_->prefs();
415
416 string str_eol_status;
417 if (prefs->Exists(kPrefsOmahaEolStatus) &&
418 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
419 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
420 return false;
421 }
422
423 // StringToEolStatus will return kSupported for invalid values.
424 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
425 return true;
426}
427
Casey Dahlina93cd532016-01-14 16:55:11 -0800428} // namespace chromeos_update_engine