blob: 88ead28c7b59fa100037d9c6e95285cb7fb93911 [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,
Amin Hassani2e4eda52019-01-07 14:01:17 -080053#if BASE_VER < 576279
Casey Dahlina93cd532016-01-14 16:55:11 -080054 const tracked_objects::Location& location,
Amin Hassani2e4eda52019-01-07 14:01:17 -080055#else
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070056 const base::Location& location,
Amin Hassani2e4eda52019-01-07 14:01:17 -080057#endif
Casey Dahlina93cd532016-01-14 16:55:11 -080058 const string& reason) {
59 brillo::Error::AddTo(error,
60 location,
61 UpdateEngineService::kErrorDomain,
62 UpdateEngineService::kErrorFailed,
63 reason);
64 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
65 << reason;
66}
67} // namespace
68
69const char* const UpdateEngineService::kErrorDomain = "update_engine";
70const char* const UpdateEngineService::kErrorFailed =
71 "org.chromium.UpdateEngine.Error.Failed";
72
73UpdateEngineService::UpdateEngineService(SystemState* system_state)
74 : system_state_(system_state) {
75}
76
77// org::chromium::UpdateEngineInterfaceInterface methods implementation.
78
Aaron Woodbf5a2522017-10-04 10:58:36 -070079bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
80 int32_t in_flags_as_int) {
81 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
82 LOG(INFO) << "Setting Update Attempt Flags: "
83 << "flags=0x" << std::hex << flags << " "
84 << "RestrictDownload="
85 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
86 : "no");
87 system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
88 return true;
89}
90
Casey Dahlina93cd532016-01-14 16:55:11 -080091bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
92 const string& in_app_version,
93 const string& in_omaha_url,
Aaron Wood081c0232017-10-19 17:14:58 -070094 int32_t in_flags_as_int,
95 bool* out_result) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070096 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
97 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
Aaron Wood081c0232017-10-19 17:14:58 -070098 bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
Casey Dahlina93cd532016-01-14 16:55:11 -080099
100 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
101 << "omaha_url=\"" << in_omaha_url << "\" "
102 << "flags=0x" << std::hex << flags << " "
Aaron Wood081c0232017-10-19 17:14:58 -0700103 << "interactive=" << (interactive ? "yes " : "no ")
104 << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
105
106 *out_result = system_state_->update_attempter()->CheckForUpdate(
107 in_app_version, in_omaha_url, flags);
Casey Dahlina93cd532016-01-14 16:55:11 -0800108 return true;
109}
110
Xiaochu Liu88d90382018-08-29 16:09:11 -0700111bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
112 const string& omaha_url,
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800113 const vector<string>& dlc_module_ids) {
114 if (!system_state_->update_attempter()->CheckForInstall(dlc_module_ids,
115 omaha_url)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700116 // TODO(xiaochu): support more detailed error messages.
117 LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
118 return false;
119 }
120 return true;
121}
122
Casey Dahlina93cd532016-01-14 16:55:11 -0800123bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
124 LOG(INFO) << "Attempting rollback to non-active partitions.";
125
126 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
127 // TODO(dgarrett): Give a more specific error code/reason.
128 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
129 return false;
130 }
131 return true;
132}
133
134bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
135 bool* out_can_rollback) {
136 bool can_rollback = system_state_->update_attempter()->CanRollback();
137 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
138 *out_can_rollback = can_rollback;
139 return true;
140}
141
142bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
143 if (!system_state_->update_attempter()->ResetStatus()) {
144 // TODO(dgarrett): Give a more specific error code/reason.
145 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
146 return false;
147 }
148 return true;
149}
150
151bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700152 UpdateEngineStatus* out_status) {
153 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800154 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
155 return false;
156 }
157 return true;
158}
159
160bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
161 if (!system_state_->update_attempter()->RebootIfNeeded()) {
162 // TODO(dgarrett): Give a more specific error code/reason.
163 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
164 return false;
165 }
166 return true;
167}
168
169bool UpdateEngineService::SetChannel(ErrorPtr* error,
170 const string& in_target_channel,
171 bool in_is_powerwash_allowed) {
172 const policy::DevicePolicy* device_policy = system_state_->device_policy();
173
174 // The device_policy is loaded in a lazy way before an update check. Load it
175 // now from the libbrillo cache if it wasn't already loaded.
176 if (!device_policy) {
177 UpdateAttempter* update_attempter = system_state_->update_attempter();
178 if (update_attempter) {
179 update_attempter->RefreshDevicePolicy();
180 device_policy = system_state_->device_policy();
181 }
182 }
183
184 bool delegated = false;
185 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
186 !delegated) {
187 LogAndSetError(error,
188 FROM_HERE,
189 "Cannot set target channel explicitly when channel "
190 "policy/settings is not delegated");
191 return false;
192 }
193
194 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
195 string error_message;
196 if (!system_state_->request_params()->SetTargetChannel(
197 in_target_channel, in_is_powerwash_allowed, &error_message)) {
198 LogAndSetError(error, FROM_HERE, error_message);
199 return false;
200 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800201 return true;
202}
203
204bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
205 bool in_get_current_channel,
206 string* out_channel) {
207 OmahaRequestParams* rp = system_state_->request_params();
208 *out_channel =
209 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
210 return true;
211}
212
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700213bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
214 string in_cohort_hint) {
215 PrefsInterface* prefs = system_state_->prefs();
216
217 // It is ok to override the cohort hint with an invalid value since it is
218 // stored in stateful partition. The code reading it should sanitize it
219 // anyway.
220 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
221 LogAndSetError(
222 error,
223 FROM_HERE,
224 StringPrintf("Error setting the cohort hint value to \"%s\".",
225 in_cohort_hint.c_str()));
226 return false;
227 }
228 return true;
229}
230
231bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
232 string* out_cohort_hint) {
233 PrefsInterface* prefs = system_state_->prefs();
234
235 *out_cohort_hint = "";
236 if (prefs->Exists(kPrefsOmahaCohortHint) &&
237 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
238 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
239 return false;
240 }
241 return true;
242}
243
Casey Dahlina93cd532016-01-14 16:55:11 -0800244bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
245 bool in_enabled) {
246 PrefsInterface* prefs = system_state_->prefs();
247
248 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
249 LogAndSetError(
250 error,
251 FROM_HERE,
252 StringPrintf("Error setting the update via p2p permission to %s.",
253 ToString(in_enabled).c_str()));
254 return false;
255 }
256 return true;
257}
258
259bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
260 bool* out_enabled) {
261 PrefsInterface* prefs = system_state_->prefs();
262
263 bool p2p_pref = false; // Default if no setting is present.
264 if (prefs->Exists(kPrefsP2PEnabled) &&
265 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
266 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
267 return false;
268 }
269
270 *out_enabled = p2p_pref;
271 return true;
272}
273
274bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
275 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700276 ConnectionManagerInterface* connection_manager =
277 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800278
279 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700280 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700281 LogAndSetError(error,
282 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800283 "Ignoring the update over cellular setting since there's "
284 "a device policy enforcing this setting.");
285 return false;
286 }
287
288 // If the policy wasn't loaded yet, then it is still OK to change the local
289 // setting because the policy will be checked again during the update check.
290
291 PrefsInterface* prefs = system_state_->prefs();
292
Weidong Guo421ff332017-04-17 10:08:38 -0700293 if (!prefs ||
294 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700295 LogAndSetError(error,
296 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800297 string("Error setting the update over cellular to ") +
298 (in_allowed ? "true" : "false"));
299 return false;
300 }
301 return true;
302}
303
Weidong Guo421ff332017-04-17 10:08:38 -0700304bool UpdateEngineService::SetUpdateOverCellularTarget(
305 brillo::ErrorPtr* error,
306 const std::string& target_version,
307 int64_t target_size) {
308 ConnectionManagerInterface* connection_manager =
309 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700310
Weidong Guo421ff332017-04-17 10:08:38 -0700311 // Check if this setting is allowed by the device policy.
312 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
313 LogAndSetError(error,
314 FROM_HERE,
315 "Ignoring the update over cellular setting since there's "
316 "a device policy enforcing this setting.");
317 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700318 }
Tao Bao5688d162017-06-06 13:09:06 -0700319
Weidong Guo421ff332017-04-17 10:08:38 -0700320 // If the policy wasn't loaded yet, then it is still OK to change the local
321 // setting because the policy will be checked again during the update check.
322
323 PrefsInterface* prefs = system_state_->prefs();
324
325 if (!prefs ||
326 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
327 target_version) ||
328 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
329 LogAndSetError(
330 error, FROM_HERE, "Error setting the target for update over cellular.");
331 return false;
332 }
333 return true;
334}
335
336bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
337 bool* out_allowed) {
338 ConnectionManagerInterface* connection_manager =
339 system_state_->connection_manager();
340
341 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
342 // We have device policy, so ignore the user preferences.
343 *out_allowed = connection_manager->IsUpdateAllowedOver(
344 ConnectionType::kCellular, ConnectionTethering::kUnknown);
345 } else {
346 PrefsInterface* prefs = system_state_->prefs();
347
348 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
349 // Update is not allowed as user preference is not set or not available.
350 *out_allowed = false;
351 return true;
352 }
353
354 bool is_allowed;
355
356 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
357 LogAndSetError(error,
358 FROM_HERE,
359 "Error getting the update over cellular preference.");
360 return false;
361 }
362 *out_allowed = is_allowed;
363 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800364 return true;
365}
366
367bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
368 int64_t* out_usec_wallclock) {
369 base::Time time;
370 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
371 LogAndSetError(error, FROM_HERE, "No pending update.");
372 return false;
373 }
374
375 ClockInterface* clock = system_state_->clock();
376 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
377 return true;
378}
379
380bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
381 string* out_prev_version) {
382 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
383 return true;
384}
385
386bool UpdateEngineService::GetRollbackPartition(
387 ErrorPtr* /* error */, string* out_rollback_partition_name) {
388 BootControlInterface::Slot rollback_slot =
389 system_state_->update_attempter()->GetRollbackSlot();
390
391 if (rollback_slot == BootControlInterface::kInvalidSlot) {
392 out_rollback_partition_name->clear();
393 return true;
394 }
395
396 string name;
397 if (!system_state_->boot_control()->GetPartitionDevice(
398 "KERNEL", rollback_slot, &name)) {
399 LOG(ERROR) << "Invalid rollback device";
400 return false;
401 }
402
403 LOG(INFO) << "Getting rollback partition name. Result: " << name;
404 *out_rollback_partition_name = name;
405 return true;
406}
407
Shuqian Zhao29971732016-02-05 11:29:32 -0800408bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
409 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700410 ErrorCode error_code =
411 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800412 *out_last_attempt_error = static_cast<int>(error_code);
413 return true;
414}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700415
416bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
417 int32_t* out_eol_status) {
418 PrefsInterface* prefs = system_state_->prefs();
419
420 string str_eol_status;
421 if (prefs->Exists(kPrefsOmahaEolStatus) &&
422 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
423 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
424 return false;
425 }
426
427 // StringToEolStatus will return kSupported for invalid values.
428 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
429 return true;
430}
431
Casey Dahlina93cd532016-01-14 16:55:11 -0800432} // namespace chromeos_update_engine