blob: c88d9408d180784bbbcaa50705596b759f0dcf64 [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;
Aaron Woodbf5a2522017-10-04 10:58:36 -070044using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070045using update_engine::UpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080046
47namespace chromeos_update_engine {
48
49namespace {
50// Log and set the error on the passed ErrorPtr.
51void LogAndSetError(ErrorPtr* error,
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070052 const base::Location& location,
Casey Dahlina93cd532016-01-14 16:55:11 -080053 const string& reason) {
54 brillo::Error::AddTo(error,
55 location,
56 UpdateEngineService::kErrorDomain,
57 UpdateEngineService::kErrorFailed,
58 reason);
59 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
60 << reason;
61}
62} // namespace
63
64const char* const UpdateEngineService::kErrorDomain = "update_engine";
65const char* const UpdateEngineService::kErrorFailed =
66 "org.chromium.UpdateEngine.Error.Failed";
67
68UpdateEngineService::UpdateEngineService(SystemState* system_state)
69 : system_state_(system_state) {
70}
71
72// org::chromium::UpdateEngineInterfaceInterface methods implementation.
73
Aaron Woodbf5a2522017-10-04 10:58:36 -070074bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
75 int32_t in_flags_as_int) {
76 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
77 LOG(INFO) << "Setting Update Attempt Flags: "
78 << "flags=0x" << std::hex << flags << " "
79 << "RestrictDownload="
80 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
81 : "no");
82 system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
83 return true;
84}
85
Casey Dahlina93cd532016-01-14 16:55:11 -080086bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
87 const string& in_app_version,
88 const string& in_omaha_url,
Aaron Wood081c0232017-10-19 17:14:58 -070089 int32_t in_flags_as_int,
90 bool* out_result) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070091 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
92 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
Aaron Wood081c0232017-10-19 17:14:58 -070093 bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
Casey Dahlina93cd532016-01-14 16:55:11 -080094
95 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
96 << "omaha_url=\"" << in_omaha_url << "\" "
97 << "flags=0x" << std::hex << flags << " "
Aaron Wood081c0232017-10-19 17:14:58 -070098 << "interactive=" << (interactive ? "yes " : "no ")
99 << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
100
101 *out_result = system_state_->update_attempter()->CheckForUpdate(
102 in_app_version, in_omaha_url, flags);
Casey Dahlina93cd532016-01-14 16:55:11 -0800103 return true;
104}
105
106bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
107 LOG(INFO) << "Attempting rollback to non-active partitions.";
108
109 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
110 // TODO(dgarrett): Give a more specific error code/reason.
111 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
112 return false;
113 }
114 return true;
115}
116
117bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
118 bool* out_can_rollback) {
119 bool can_rollback = system_state_->update_attempter()->CanRollback();
120 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
121 *out_can_rollback = can_rollback;
122 return true;
123}
124
125bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
126 if (!system_state_->update_attempter()->ResetStatus()) {
127 // TODO(dgarrett): Give a more specific error code/reason.
128 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
129 return false;
130 }
131 return true;
132}
133
134bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700135 UpdateEngineStatus* out_status) {
136 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800137 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
138 return false;
139 }
140 return true;
141}
142
143bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
144 if (!system_state_->update_attempter()->RebootIfNeeded()) {
145 // TODO(dgarrett): Give a more specific error code/reason.
146 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
147 return false;
148 }
149 return true;
150}
151
152bool UpdateEngineService::SetChannel(ErrorPtr* error,
153 const string& in_target_channel,
154 bool in_is_powerwash_allowed) {
155 const policy::DevicePolicy* device_policy = system_state_->device_policy();
156
157 // The device_policy is loaded in a lazy way before an update check. Load it
158 // now from the libbrillo cache if it wasn't already loaded.
159 if (!device_policy) {
160 UpdateAttempter* update_attempter = system_state_->update_attempter();
161 if (update_attempter) {
162 update_attempter->RefreshDevicePolicy();
163 device_policy = system_state_->device_policy();
164 }
165 }
166
167 bool delegated = false;
168 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
169 !delegated) {
170 LogAndSetError(error,
171 FROM_HERE,
172 "Cannot set target channel explicitly when channel "
173 "policy/settings is not delegated");
174 return false;
175 }
176
177 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
178 string error_message;
179 if (!system_state_->request_params()->SetTargetChannel(
180 in_target_channel, in_is_powerwash_allowed, &error_message)) {
181 LogAndSetError(error, FROM_HERE, error_message);
182 return false;
183 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800184 return true;
185}
186
187bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
188 bool in_get_current_channel,
189 string* out_channel) {
190 OmahaRequestParams* rp = system_state_->request_params();
191 *out_channel =
192 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
193 return true;
194}
195
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700196bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
197 string in_cohort_hint) {
198 PrefsInterface* prefs = system_state_->prefs();
199
200 // It is ok to override the cohort hint with an invalid value since it is
201 // stored in stateful partition. The code reading it should sanitize it
202 // anyway.
203 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
204 LogAndSetError(
205 error,
206 FROM_HERE,
207 StringPrintf("Error setting the cohort hint value to \"%s\".",
208 in_cohort_hint.c_str()));
209 return false;
210 }
211 return true;
212}
213
214bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
215 string* out_cohort_hint) {
216 PrefsInterface* prefs = system_state_->prefs();
217
218 *out_cohort_hint = "";
219 if (prefs->Exists(kPrefsOmahaCohortHint) &&
220 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
221 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
222 return false;
223 }
224 return true;
225}
226
Casey Dahlina93cd532016-01-14 16:55:11 -0800227bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
228 bool in_enabled) {
229 PrefsInterface* prefs = system_state_->prefs();
230
231 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
232 LogAndSetError(
233 error,
234 FROM_HERE,
235 StringPrintf("Error setting the update via p2p permission to %s.",
236 ToString(in_enabled).c_str()));
237 return false;
238 }
239 return true;
240}
241
242bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
243 bool* out_enabled) {
244 PrefsInterface* prefs = system_state_->prefs();
245
246 bool p2p_pref = false; // Default if no setting is present.
247 if (prefs->Exists(kPrefsP2PEnabled) &&
248 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
249 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
250 return false;
251 }
252
253 *out_enabled = p2p_pref;
254 return true;
255}
256
257bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
258 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700259 ConnectionManagerInterface* connection_manager =
260 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800261
262 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700263 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700264 LogAndSetError(error,
265 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800266 "Ignoring the update over cellular setting since there's "
267 "a device policy enforcing this setting.");
268 return false;
269 }
270
271 // If the policy wasn't loaded yet, then it is still OK to change the local
272 // setting because the policy will be checked again during the update check.
273
274 PrefsInterface* prefs = system_state_->prefs();
275
Weidong Guo421ff332017-04-17 10:08:38 -0700276 if (!prefs ||
277 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700278 LogAndSetError(error,
279 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800280 string("Error setting the update over cellular to ") +
281 (in_allowed ? "true" : "false"));
282 return false;
283 }
284 return true;
285}
286
Weidong Guo421ff332017-04-17 10:08:38 -0700287bool UpdateEngineService::SetUpdateOverCellularTarget(
288 brillo::ErrorPtr* error,
289 const std::string& target_version,
290 int64_t target_size) {
291 ConnectionManagerInterface* connection_manager =
292 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700293
Weidong Guo421ff332017-04-17 10:08:38 -0700294 // Check if this setting is allowed by the device policy.
295 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
296 LogAndSetError(error,
297 FROM_HERE,
298 "Ignoring the update over cellular setting since there's "
299 "a device policy enforcing this setting.");
300 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700301 }
Tao Bao5688d162017-06-06 13:09:06 -0700302
Weidong Guo421ff332017-04-17 10:08:38 -0700303 // If the policy wasn't loaded yet, then it is still OK to change the local
304 // setting because the policy will be checked again during the update check.
305
306 PrefsInterface* prefs = system_state_->prefs();
307
308 if (!prefs ||
309 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
310 target_version) ||
311 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
312 LogAndSetError(
313 error, FROM_HERE, "Error setting the target for update over cellular.");
314 return false;
315 }
316 return true;
317}
318
319bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
320 bool* out_allowed) {
321 ConnectionManagerInterface* connection_manager =
322 system_state_->connection_manager();
323
324 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
325 // We have device policy, so ignore the user preferences.
326 *out_allowed = connection_manager->IsUpdateAllowedOver(
327 ConnectionType::kCellular, ConnectionTethering::kUnknown);
328 } else {
329 PrefsInterface* prefs = system_state_->prefs();
330
331 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
332 // Update is not allowed as user preference is not set or not available.
333 *out_allowed = false;
334 return true;
335 }
336
337 bool is_allowed;
338
339 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
340 LogAndSetError(error,
341 FROM_HERE,
342 "Error getting the update over cellular preference.");
343 return false;
344 }
345 *out_allowed = is_allowed;
346 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800347 return true;
348}
349
350bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
351 int64_t* out_usec_wallclock) {
352 base::Time time;
353 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
354 LogAndSetError(error, FROM_HERE, "No pending update.");
355 return false;
356 }
357
358 ClockInterface* clock = system_state_->clock();
359 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
360 return true;
361}
362
363bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
364 string* out_prev_version) {
365 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
366 return true;
367}
368
369bool UpdateEngineService::GetRollbackPartition(
370 ErrorPtr* /* error */, string* out_rollback_partition_name) {
371 BootControlInterface::Slot rollback_slot =
372 system_state_->update_attempter()->GetRollbackSlot();
373
374 if (rollback_slot == BootControlInterface::kInvalidSlot) {
375 out_rollback_partition_name->clear();
376 return true;
377 }
378
379 string name;
380 if (!system_state_->boot_control()->GetPartitionDevice(
381 "KERNEL", rollback_slot, &name)) {
382 LOG(ERROR) << "Invalid rollback device";
383 return false;
384 }
385
386 LOG(INFO) << "Getting rollback partition name. Result: " << name;
387 *out_rollback_partition_name = name;
388 return true;
389}
390
Shuqian Zhao29971732016-02-05 11:29:32 -0800391bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
392 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700393 ErrorCode error_code =
394 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800395 *out_last_attempt_error = static_cast<int>(error_code);
396 return true;
397}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700398
399bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
400 int32_t* out_eol_status) {
401 PrefsInterface* prefs = system_state_->prefs();
402
403 string str_eol_status;
404 if (prefs->Exists(kPrefsOmahaEolStatus) &&
405 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
406 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
407 return false;
408 }
409
410 // StringToEolStatus will return kSupported for invalid values.
411 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
412 return true;
413}
414
Casey Dahlina93cd532016-01-14 16:55:11 -0800415} // namespace chromeos_update_engine