blob: b8bc2983a9ca6b60084f68794ab4f83b12ca44fb [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
21#include <base/location.h>
22#include <base/logging.h>
23#include <base/strings/stringprintf.h>
24#include <brillo/bind_lambda.h>
25#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;
44
45namespace chromeos_update_engine {
46
47namespace {
48// Log and set the error on the passed ErrorPtr.
49void LogAndSetError(ErrorPtr* error,
50 const tracked_objects::Location& location,
51 const string& reason) {
52 brillo::Error::AddTo(error,
53 location,
54 UpdateEngineService::kErrorDomain,
55 UpdateEngineService::kErrorFailed,
56 reason);
57 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
58 << reason;
59}
60} // namespace
61
62const char* const UpdateEngineService::kErrorDomain = "update_engine";
63const char* const UpdateEngineService::kErrorFailed =
64 "org.chromium.UpdateEngine.Error.Failed";
65
66UpdateEngineService::UpdateEngineService(SystemState* system_state)
67 : system_state_(system_state) {
68}
69
70// org::chromium::UpdateEngineInterfaceInterface methods implementation.
71
72bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
73 const string& in_app_version,
74 const string& in_omaha_url,
75 int32_t in_flags_as_int) {
76 AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>(in_flags_as_int);
77 bool interactive = !(flags & kAttemptUpdateFlagNonInteractive);
78
79 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
80 << "omaha_url=\"" << in_omaha_url << "\" "
81 << "flags=0x" << std::hex << flags << " "
82 << "interactive=" << (interactive ? "yes" : "no");
83 system_state_->update_attempter()->CheckForUpdate(
84 in_app_version, in_omaha_url, interactive);
85 return true;
86}
87
88bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
89 LOG(INFO) << "Attempting rollback to non-active partitions.";
90
91 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
92 // TODO(dgarrett): Give a more specific error code/reason.
93 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
94 return false;
95 }
96 return true;
97}
98
99bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
100 bool* out_can_rollback) {
101 bool can_rollback = system_state_->update_attempter()->CanRollback();
102 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
103 *out_can_rollback = can_rollback;
104 return true;
105}
106
107bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
108 if (!system_state_->update_attempter()->ResetStatus()) {
109 // TODO(dgarrett): Give a more specific error code/reason.
110 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
111 return false;
112 }
113 return true;
114}
115
116bool UpdateEngineService::GetStatus(ErrorPtr* error,
117 int64_t* out_last_checked_time,
118 double* out_progress,
119 string* out_current_operation,
120 string* out_new_version,
121 int64_t* out_new_size) {
122 if (!system_state_->update_attempter()->GetStatus(out_last_checked_time,
123 out_progress,
124 out_current_operation,
125 out_new_version,
126 out_new_size)) {
127 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
128 return false;
129 }
130 return true;
131}
132
133bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
134 if (!system_state_->update_attempter()->RebootIfNeeded()) {
135 // TODO(dgarrett): Give a more specific error code/reason.
136 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
137 return false;
138 }
139 return true;
140}
141
142bool UpdateEngineService::SetChannel(ErrorPtr* error,
143 const string& in_target_channel,
144 bool in_is_powerwash_allowed) {
145 const policy::DevicePolicy* device_policy = system_state_->device_policy();
146
147 // The device_policy is loaded in a lazy way before an update check. Load it
148 // now from the libbrillo cache if it wasn't already loaded.
149 if (!device_policy) {
150 UpdateAttempter* update_attempter = system_state_->update_attempter();
151 if (update_attempter) {
152 update_attempter->RefreshDevicePolicy();
153 device_policy = system_state_->device_policy();
154 }
155 }
156
157 bool delegated = false;
158 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
159 !delegated) {
160 LogAndSetError(error,
161 FROM_HERE,
162 "Cannot set target channel explicitly when channel "
163 "policy/settings is not delegated");
164 return false;
165 }
166
167 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
168 string error_message;
169 if (!system_state_->request_params()->SetTargetChannel(
170 in_target_channel, in_is_powerwash_allowed, &error_message)) {
171 LogAndSetError(error, FROM_HERE, error_message);
172 return false;
173 }
174 // Update the weave state because updated the target channel.
Alex Deymofa78f142016-01-26 21:36:16 -0800175 system_state_->update_attempter()->BroadcastChannel();
Casey Dahlina93cd532016-01-14 16:55:11 -0800176 return true;
177}
178
179bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
180 bool in_get_current_channel,
181 string* out_channel) {
182 OmahaRequestParams* rp = system_state_->request_params();
183 *out_channel =
184 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
185 return true;
186}
187
Alex Deymod63fab32016-10-06 15:40:49 -0700188bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
189 string in_cohort_hint) {
190 PrefsInterface* prefs = system_state_->prefs();
191
192 // It is ok to override the cohort hint with an invalid value since it is
193 // stored in stateful partition. The code reading it should sanitize it
194 // anyway.
195 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
196 LogAndSetError(
197 error,
198 FROM_HERE,
199 StringPrintf("Error setting the cohort hint value to \"%s\".",
200 in_cohort_hint.c_str()));
201 return false;
202 }
203 return true;
204}
205
206bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
207 string* out_cohort_hint) {
208 PrefsInterface* prefs = system_state_->prefs();
209
210 *out_cohort_hint = "";
211 if (prefs->Exists(kPrefsOmahaCohortHint) &&
212 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
213 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
214 return false;
215 }
216 return true;
217}
218
Casey Dahlina93cd532016-01-14 16:55:11 -0800219bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
220 bool in_enabled) {
221 PrefsInterface* prefs = system_state_->prefs();
222
223 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
224 LogAndSetError(
225 error,
226 FROM_HERE,
227 StringPrintf("Error setting the update via p2p permission to %s.",
228 ToString(in_enabled).c_str()));
229 return false;
230 }
231 return true;
232}
233
234bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
235 bool* out_enabled) {
236 PrefsInterface* prefs = system_state_->prefs();
237
238 bool p2p_pref = false; // Default if no setting is present.
239 if (prefs->Exists(kPrefsP2PEnabled) &&
240 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
241 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
242 return false;
243 }
244
245 *out_enabled = p2p_pref;
246 return true;
247}
248
249bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
250 bool in_allowed) {
Weidong Guo70063d92017-04-17 10:08:38 -0700251 ConnectionManagerInterface* connection_manager =
252 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800253
254 // Check if this setting is allowed by the device policy.
Weidong Guo70063d92017-04-17 10:08:38 -0700255 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
256 LogAndSetError(error, FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800257 "Ignoring the update over cellular setting since there's "
258 "a device policy enforcing this setting.");
259 return false;
260 }
261
262 // If the policy wasn't loaded yet, then it is still OK to change the local
263 // setting because the policy will be checked again during the update check.
264
265 PrefsInterface* prefs = system_state_->prefs();
266
Weidong Guo70063d92017-04-17 10:08:38 -0700267 if (!prefs ||
268 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
269 LogAndSetError(error, FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800270 string("Error setting the update over cellular to ") +
271 (in_allowed ? "true" : "false"));
272 return false;
273 }
274 return true;
275}
276
Weidong Guo70063d92017-04-17 10:08:38 -0700277bool UpdateEngineService::SetUpdateOverCellularTarget(
278 brillo::ErrorPtr* error, const std::string &target_version,
279 int64_t target_size) {
280 ConnectionManagerInterface* connection_manager =
281 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800282
Weidong Guo70063d92017-04-17 10:08:38 -0700283 // Check if this setting is allowed by the device policy.
284 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
285 LogAndSetError(error, FROM_HERE,
286 "Ignoring the update over cellular setting since there's "
287 "a device policy enforcing this setting.");
288 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800289 }
290
Weidong Guo70063d92017-04-17 10:08:38 -0700291 // If the policy wasn't loaded yet, then it is still OK to change the local
292 // setting because the policy will be checked again during the update check.
293
294 PrefsInterface* prefs = system_state_->prefs();
295
296 if (!prefs ||
297 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
298 target_version) ||
299 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
300 LogAndSetError(error, FROM_HERE,
301 "Error setting the target for update over cellular.");
302 return false;
303 }
304 return true;
305}
306
307bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
308 bool* out_allowed) {
309 ConnectionManagerInterface* connection_manager =
310 system_state_->connection_manager();
311
312 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
313 // We have device policy, so ignore the user preferences.
314 *out_allowed = connection_manager->IsUpdateAllowedOver(
315 NetworkConnectionType::kCellular, NetworkTethering::kUnknown);
316 } else {
317 PrefsInterface* prefs = system_state_->prefs();
318
319 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
320 // Update is not allowed as user preference is not set or not available.
321 *out_allowed = false;
322 return true;
323 }
324
325 bool is_allowed;
326
327 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
328 LogAndSetError(error, FROM_HERE,
329 "Error getting the update over cellular preference.");
330 return false;
331 }
332 *out_allowed = is_allowed;
333 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800334 return true;
335}
336
337bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
338 int64_t* out_usec_wallclock) {
339 base::Time time;
340 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
341 LogAndSetError(error, FROM_HERE, "No pending update.");
342 return false;
343 }
344
345 ClockInterface* clock = system_state_->clock();
346 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
347 return true;
348}
349
350bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
351 string* out_prev_version) {
352 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
353 return true;
354}
355
356bool UpdateEngineService::GetRollbackPartition(
357 ErrorPtr* /* error */, string* out_rollback_partition_name) {
358 BootControlInterface::Slot rollback_slot =
359 system_state_->update_attempter()->GetRollbackSlot();
360
361 if (rollback_slot == BootControlInterface::kInvalidSlot) {
362 out_rollback_partition_name->clear();
363 return true;
364 }
365
366 string name;
367 if (!system_state_->boot_control()->GetPartitionDevice(
368 "KERNEL", rollback_slot, &name)) {
369 LOG(ERROR) << "Invalid rollback device";
370 return false;
371 }
372
373 LOG(INFO) << "Getting rollback partition name. Result: " << name;
374 *out_rollback_partition_name = name;
375 return true;
376}
377
Shuqian Zhao29971732016-02-05 11:29:32 -0800378bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
379 int32_t* out_last_attempt_error) {
380 ErrorCode error_code = system_state_->payload_state()->GetAttemptErrorCode();
381 *out_last_attempt_error = static_cast<int>(error_code);
382 return true;
383}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700384
385bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
386 int32_t* out_eol_status) {
387 PrefsInterface* prefs = system_state_->prefs();
388
389 string str_eol_status;
390 if (prefs->Exists(kPrefsOmahaEolStatus) &&
391 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
392 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
393 return false;
394 }
395
396 // StringToEolStatus will return kSupported for invalid values.
397 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
398 return true;
399}
400
Casey Dahlina93cd532016-01-14 16:55:11 -0800401} // namespace chromeos_update_engine