blob: 0d5ee6dce5b9e590ccebea18d99db0c8b0142699 [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)
Amin Hassani7cc8bb02019-01-14 16:29:47 -080074 : system_state_(system_state) {}
Casey Dahlina93cd532016-01-14 16:55:11 -080075
76// org::chromium::UpdateEngineInterfaceInterface methods implementation.
77
Aaron Woodbf5a2522017-10-04 10:58:36 -070078bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
79 int32_t in_flags_as_int) {
80 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
81 LOG(INFO) << "Setting Update Attempt Flags: "
82 << "flags=0x" << std::hex << flags << " "
83 << "RestrictDownload="
84 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
85 : "no");
86 system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
87 return true;
88}
89
Casey Dahlina93cd532016-01-14 16:55:11 -080090bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
91 const string& in_app_version,
92 const string& in_omaha_url,
Aaron Wood081c0232017-10-19 17:14:58 -070093 int32_t in_flags_as_int,
94 bool* out_result) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070095 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
96 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
Aaron Wood081c0232017-10-19 17:14:58 -070097 bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
Casey Dahlina93cd532016-01-14 16:55:11 -080098
99 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
100 << "omaha_url=\"" << in_omaha_url << "\" "
101 << "flags=0x" << std::hex << flags << " "
Aaron Wood081c0232017-10-19 17:14:58 -0700102 << "interactive=" << (interactive ? "yes " : "no ")
103 << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
104
105 *out_result = system_state_->update_attempter()->CheckForUpdate(
106 in_app_version, in_omaha_url, flags);
Casey Dahlina93cd532016-01-14 16:55:11 -0800107 return true;
108}
109
Xiaochu Liu88d90382018-08-29 16:09:11 -0700110bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
111 const string& omaha_url,
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800112 const vector<string>& dlc_module_ids) {
113 if (!system_state_->update_attempter()->CheckForInstall(dlc_module_ids,
114 omaha_url)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700115 // TODO(xiaochu): support more detailed error messages.
116 LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
117 return false;
118 }
119 return true;
120}
121
Casey Dahlina93cd532016-01-14 16:55:11 -0800122bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
123 LOG(INFO) << "Attempting rollback to non-active partitions.";
124
125 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
126 // TODO(dgarrett): Give a more specific error code/reason.
127 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
128 return false;
129 }
130 return true;
131}
132
133bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
134 bool* out_can_rollback) {
135 bool can_rollback = system_state_->update_attempter()->CanRollback();
136 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
137 *out_can_rollback = can_rollback;
138 return true;
139}
140
141bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
142 if (!system_state_->update_attempter()->ResetStatus()) {
143 // TODO(dgarrett): Give a more specific error code/reason.
144 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
145 return false;
146 }
147 return true;
148}
149
150bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700151 UpdateEngineStatus* out_status) {
152 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800153 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
154 return false;
155 }
156 return true;
157}
158
159bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
160 if (!system_state_->update_attempter()->RebootIfNeeded()) {
161 // TODO(dgarrett): Give a more specific error code/reason.
162 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
163 return false;
164 }
165 return true;
166}
167
168bool UpdateEngineService::SetChannel(ErrorPtr* error,
169 const string& in_target_channel,
170 bool in_is_powerwash_allowed) {
171 const policy::DevicePolicy* device_policy = system_state_->device_policy();
172
173 // The device_policy is loaded in a lazy way before an update check. Load it
174 // now from the libbrillo cache if it wasn't already loaded.
175 if (!device_policy) {
176 UpdateAttempter* update_attempter = system_state_->update_attempter();
177 if (update_attempter) {
178 update_attempter->RefreshDevicePolicy();
179 device_policy = system_state_->device_policy();
180 }
181 }
182
183 bool delegated = false;
184 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
185 !delegated) {
186 LogAndSetError(error,
187 FROM_HERE,
188 "Cannot set target channel explicitly when channel "
189 "policy/settings is not delegated");
190 return false;
191 }
192
193 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
194 string error_message;
195 if (!system_state_->request_params()->SetTargetChannel(
196 in_target_channel, in_is_powerwash_allowed, &error_message)) {
197 LogAndSetError(error, FROM_HERE, error_message);
198 return false;
199 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800200 return true;
201}
202
203bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
204 bool in_get_current_channel,
205 string* out_channel) {
206 OmahaRequestParams* rp = system_state_->request_params();
207 *out_channel =
208 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
209 return true;
210}
211
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700212bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
213 string in_cohort_hint) {
214 PrefsInterface* prefs = system_state_->prefs();
215
216 // It is ok to override the cohort hint with an invalid value since it is
217 // stored in stateful partition. The code reading it should sanitize it
218 // anyway.
219 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
220 LogAndSetError(
221 error,
222 FROM_HERE,
223 StringPrintf("Error setting the cohort hint value to \"%s\".",
224 in_cohort_hint.c_str()));
225 return false;
226 }
227 return true;
228}
229
230bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
231 string* out_cohort_hint) {
232 PrefsInterface* prefs = system_state_->prefs();
233
234 *out_cohort_hint = "";
235 if (prefs->Exists(kPrefsOmahaCohortHint) &&
236 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
237 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
238 return false;
239 }
240 return true;
241}
242
Casey Dahlina93cd532016-01-14 16:55:11 -0800243bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
244 bool in_enabled) {
245 PrefsInterface* prefs = system_state_->prefs();
246
247 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
248 LogAndSetError(
249 error,
250 FROM_HERE,
251 StringPrintf("Error setting the update via p2p permission to %s.",
252 ToString(in_enabled).c_str()));
253 return false;
254 }
255 return true;
256}
257
258bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
259 bool* out_enabled) {
260 PrefsInterface* prefs = system_state_->prefs();
261
262 bool p2p_pref = false; // Default if no setting is present.
263 if (prefs->Exists(kPrefsP2PEnabled) &&
264 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
265 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
266 return false;
267 }
268
269 *out_enabled = p2p_pref;
270 return true;
271}
272
273bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
274 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700275 ConnectionManagerInterface* connection_manager =
276 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800277
278 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700279 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700280 LogAndSetError(error,
281 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800282 "Ignoring the update over cellular setting since there's "
283 "a device policy enforcing this setting.");
284 return false;
285 }
286
287 // If the policy wasn't loaded yet, then it is still OK to change the local
288 // setting because the policy will be checked again during the update check.
289
290 PrefsInterface* prefs = system_state_->prefs();
291
Weidong Guo421ff332017-04-17 10:08:38 -0700292 if (!prefs ||
293 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700294 LogAndSetError(error,
295 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800296 string("Error setting the update over cellular to ") +
297 (in_allowed ? "true" : "false"));
298 return false;
299 }
300 return true;
301}
302
Weidong Guo421ff332017-04-17 10:08:38 -0700303bool UpdateEngineService::SetUpdateOverCellularTarget(
304 brillo::ErrorPtr* error,
305 const std::string& target_version,
306 int64_t target_size) {
307 ConnectionManagerInterface* connection_manager =
308 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700309
Weidong Guo421ff332017-04-17 10:08:38 -0700310 // Check if this setting is allowed by the device policy.
311 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
312 LogAndSetError(error,
313 FROM_HERE,
314 "Ignoring the update over cellular setting since there's "
315 "a device policy enforcing this setting.");
316 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700317 }
Tao Bao5688d162017-06-06 13:09:06 -0700318
Weidong Guo421ff332017-04-17 10:08:38 -0700319 // If the policy wasn't loaded yet, then it is still OK to change the local
320 // setting because the policy will be checked again during the update check.
321
322 PrefsInterface* prefs = system_state_->prefs();
323
324 if (!prefs ||
325 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
326 target_version) ||
327 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
328 LogAndSetError(
329 error, FROM_HERE, "Error setting the target for update over cellular.");
330 return false;
331 }
332 return true;
333}
334
335bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
336 bool* out_allowed) {
337 ConnectionManagerInterface* connection_manager =
338 system_state_->connection_manager();
339
340 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
341 // We have device policy, so ignore the user preferences.
342 *out_allowed = connection_manager->IsUpdateAllowedOver(
343 ConnectionType::kCellular, ConnectionTethering::kUnknown);
344 } else {
345 PrefsInterface* prefs = system_state_->prefs();
346
347 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
348 // Update is not allowed as user preference is not set or not available.
349 *out_allowed = false;
350 return true;
351 }
352
353 bool is_allowed;
354
355 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
356 LogAndSetError(error,
357 FROM_HERE,
358 "Error getting the update over cellular preference.");
359 return false;
360 }
361 *out_allowed = is_allowed;
362 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800363 return true;
364}
365
366bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
367 int64_t* out_usec_wallclock) {
368 base::Time time;
369 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
370 LogAndSetError(error, FROM_HERE, "No pending update.");
371 return false;
372 }
373
374 ClockInterface* clock = system_state_->clock();
375 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
376 return true;
377}
378
379bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
380 string* out_prev_version) {
381 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
382 return true;
383}
384
385bool UpdateEngineService::GetRollbackPartition(
386 ErrorPtr* /* error */, string* out_rollback_partition_name) {
387 BootControlInterface::Slot rollback_slot =
388 system_state_->update_attempter()->GetRollbackSlot();
389
390 if (rollback_slot == BootControlInterface::kInvalidSlot) {
391 out_rollback_partition_name->clear();
392 return true;
393 }
394
395 string name;
396 if (!system_state_->boot_control()->GetPartitionDevice(
397 "KERNEL", rollback_slot, &name)) {
398 LOG(ERROR) << "Invalid rollback device";
399 return false;
400 }
401
402 LOG(INFO) << "Getting rollback partition name. Result: " << name;
403 *out_rollback_partition_name = name;
404 return true;
405}
406
Shuqian Zhao29971732016-02-05 11:29:32 -0800407bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
408 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700409 ErrorCode error_code =
410 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800411 *out_last_attempt_error = static_cast<int>(error_code);
412 return true;
413}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700414
415bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
416 int32_t* out_eol_status) {
417 PrefsInterface* prefs = system_state_->prefs();
418
419 string str_eol_status;
420 if (prefs->Exists(kPrefsOmahaEolStatus) &&
421 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
422 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
423 return false;
424 }
425
426 // StringToEolStatus will return kSupported for invalid values.
427 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
428 return true;
429}
430
Casey Dahlina93cd532016-01-14 16:55:11 -0800431} // namespace chromeos_update_engine