blob: 38a3096aa723ec38489a58e628083de3ac3e00f2 [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
Tao Bao5688d162017-06-06 13:09:06 -070019#include <set>
Casey Dahlina93cd532016-01-14 16:55:11 -080020#include <string>
21
22#include <base/location.h>
23#include <base/logging.h>
24#include <base/strings/stringprintf.h>
25#include <brillo/bind_lambda.h>
26#include <brillo/message_loops/message_loop.h>
27#include <brillo/strings/string_utils.h>
28#include <policy/device_policy.h>
29
30#include "update_engine/common/clock_interface.h"
31#include "update_engine/common/hardware_interface.h"
32#include "update_engine/common/prefs.h"
33#include "update_engine/common/utils.h"
34#include "update_engine/connection_manager_interface.h"
35#include "update_engine/omaha_request_params.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070036#include "update_engine/omaha_utils.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080037#include "update_engine/p2p_manager.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080038#include "update_engine/payload_state_interface.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070039#include "update_engine/update_attempter.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080040
41using base::StringPrintf;
42using brillo::ErrorPtr;
43using brillo::string_utils::ToString;
Tao Bao5688d162017-06-06 13:09:06 -070044using std::set;
Casey Dahlina93cd532016-01-14 16:55:11 -080045using std::string;
Aaron Woodbf5a2522017-10-04 10:58:36 -070046using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070047using update_engine::UpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080048
49namespace chromeos_update_engine {
50
51namespace {
52// Log and set the error on the passed ErrorPtr.
53void LogAndSetError(ErrorPtr* error,
54 const tracked_objects::Location& location,
55 const string& reason) {
56 brillo::Error::AddTo(error,
57 location,
58 UpdateEngineService::kErrorDomain,
59 UpdateEngineService::kErrorFailed,
60 reason);
61 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
62 << reason;
63}
64} // namespace
65
66const char* const UpdateEngineService::kErrorDomain = "update_engine";
67const char* const UpdateEngineService::kErrorFailed =
68 "org.chromium.UpdateEngine.Error.Failed";
69
70UpdateEngineService::UpdateEngineService(SystemState* system_state)
71 : system_state_(system_state) {
72}
73
74// org::chromium::UpdateEngineInterfaceInterface methods implementation.
75
Aaron Woodbf5a2522017-10-04 10:58:36 -070076bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
77 int32_t in_flags_as_int) {
78 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
79 LOG(INFO) << "Setting Update Attempt Flags: "
80 << "flags=0x" << std::hex << flags << " "
81 << "RestrictDownload="
82 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
83 : "no");
84 system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
85 return true;
86}
87
Casey Dahlina93cd532016-01-14 16:55:11 -080088bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
89 const string& in_app_version,
90 const string& in_omaha_url,
91 int32_t in_flags_as_int) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070092 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
93 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
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 << " "
98 << "interactive=" << (interactive ? "yes" : "no");
99 system_state_->update_attempter()->CheckForUpdate(
100 in_app_version, in_omaha_url, interactive);
101 return true;
102}
103
104bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
105 LOG(INFO) << "Attempting rollback to non-active partitions.";
106
107 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
108 // TODO(dgarrett): Give a more specific error code/reason.
109 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
110 return false;
111 }
112 return true;
113}
114
115bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
116 bool* out_can_rollback) {
117 bool can_rollback = system_state_->update_attempter()->CanRollback();
118 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
119 *out_can_rollback = can_rollback;
120 return true;
121}
122
123bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
124 if (!system_state_->update_attempter()->ResetStatus()) {
125 // TODO(dgarrett): Give a more specific error code/reason.
126 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
127 return false;
128 }
129 return true;
130}
131
132bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700133 UpdateEngineStatus* out_status) {
134 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800135 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
136 return false;
137 }
138 return true;
139}
140
141bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
142 if (!system_state_->update_attempter()->RebootIfNeeded()) {
143 // TODO(dgarrett): Give a more specific error code/reason.
144 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
145 return false;
146 }
147 return true;
148}
149
150bool UpdateEngineService::SetChannel(ErrorPtr* error,
151 const string& in_target_channel,
152 bool in_is_powerwash_allowed) {
153 const policy::DevicePolicy* device_policy = system_state_->device_policy();
154
155 // The device_policy is loaded in a lazy way before an update check. Load it
156 // now from the libbrillo cache if it wasn't already loaded.
157 if (!device_policy) {
158 UpdateAttempter* update_attempter = system_state_->update_attempter();
159 if (update_attempter) {
160 update_attempter->RefreshDevicePolicy();
161 device_policy = system_state_->device_policy();
162 }
163 }
164
165 bool delegated = false;
166 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
167 !delegated) {
168 LogAndSetError(error,
169 FROM_HERE,
170 "Cannot set target channel explicitly when channel "
171 "policy/settings is not delegated");
172 return false;
173 }
174
175 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
176 string error_message;
177 if (!system_state_->request_params()->SetTargetChannel(
178 in_target_channel, in_is_powerwash_allowed, &error_message)) {
179 LogAndSetError(error, FROM_HERE, error_message);
180 return false;
181 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800182 return true;
183}
184
185bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
186 bool in_get_current_channel,
187 string* out_channel) {
188 OmahaRequestParams* rp = system_state_->request_params();
189 *out_channel =
190 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
191 return true;
192}
193
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700194bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
195 string in_cohort_hint) {
196 PrefsInterface* prefs = system_state_->prefs();
197
198 // It is ok to override the cohort hint with an invalid value since it is
199 // stored in stateful partition. The code reading it should sanitize it
200 // anyway.
201 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
202 LogAndSetError(
203 error,
204 FROM_HERE,
205 StringPrintf("Error setting the cohort hint value to \"%s\".",
206 in_cohort_hint.c_str()));
207 return false;
208 }
209 return true;
210}
211
212bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
213 string* out_cohort_hint) {
214 PrefsInterface* prefs = system_state_->prefs();
215
216 *out_cohort_hint = "";
217 if (prefs->Exists(kPrefsOmahaCohortHint) &&
218 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
219 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
220 return false;
221 }
222 return true;
223}
224
Casey Dahlina93cd532016-01-14 16:55:11 -0800225bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
226 bool in_enabled) {
227 PrefsInterface* prefs = system_state_->prefs();
228
229 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
230 LogAndSetError(
231 error,
232 FROM_HERE,
233 StringPrintf("Error setting the update via p2p permission to %s.",
234 ToString(in_enabled).c_str()));
235 return false;
236 }
237 return true;
238}
239
240bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
241 bool* out_enabled) {
242 PrefsInterface* prefs = system_state_->prefs();
243
244 bool p2p_pref = false; // Default if no setting is present.
245 if (prefs->Exists(kPrefsP2PEnabled) &&
246 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
247 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
248 return false;
249 }
250
251 *out_enabled = p2p_pref;
252 return true;
253}
254
255bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
256 bool in_allowed) {
Tao Bao5688d162017-06-06 13:09:06 -0700257 set<string> allowed_types;
258 const policy::DevicePolicy* device_policy = system_state_->device_policy();
259
260 // The device_policy is loaded in a lazy way before an update check. Load it
261 // now from the libbrillo cache if it wasn't already loaded.
262 if (!device_policy) {
263 UpdateAttempter* update_attempter = system_state_->update_attempter();
264 if (update_attempter) {
265 update_attempter->RefreshDevicePolicy();
266 device_policy = system_state_->device_policy();
267 }
268 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800269
270 // Check if this setting is allowed by the device policy.
Tao Bao5688d162017-06-06 13:09:06 -0700271 if (device_policy &&
272 device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
273 LogAndSetError(error,
274 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800275 "Ignoring the update over cellular setting since there's "
276 "a device policy enforcing this setting.");
277 return false;
278 }
279
280 // If the policy wasn't loaded yet, then it is still OK to change the local
281 // setting because the policy will be checked again during the update check.
282
283 PrefsInterface* prefs = system_state_->prefs();
284
Tao Bao5688d162017-06-06 13:09:06 -0700285 if (!prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
286 LogAndSetError(error,
287 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800288 string("Error setting the update over cellular to ") +
289 (in_allowed ? "true" : "false"));
290 return false;
291 }
292 return true;
293}
294
Tao Bao5688d162017-06-06 13:09:06 -0700295bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* /* error */,
Weidong Guo4b0d6032017-04-17 10:08:38 -0700296 bool* out_allowed) {
Tao Bao5688d162017-06-06 13:09:06 -0700297 ConnectionManagerInterface* cm = system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700298
Tao Bao5688d162017-06-06 13:09:06 -0700299 // The device_policy is loaded in a lazy way before an update check and is
300 // used to determine if an update is allowed over cellular. Load the device
301 // policy now from the libbrillo cache if it wasn't already loaded.
302 if (!system_state_->device_policy()) {
303 UpdateAttempter* update_attempter = system_state_->update_attempter();
304 if (update_attempter)
305 update_attempter->RefreshDevicePolicy();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700306 }
Tao Bao5688d162017-06-06 13:09:06 -0700307
308 // Return the current setting based on the same logic used while checking for
309 // updates. A log message could be printed as the result of this test.
310 LOG(INFO) << "Checking if updates over cellular networks are allowed:";
311 *out_allowed = cm->IsUpdateAllowedOver(ConnectionType::kCellular,
312 ConnectionTethering::kUnknown);
Casey Dahlina93cd532016-01-14 16:55:11 -0800313 return true;
314}
315
316bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
317 int64_t* out_usec_wallclock) {
318 base::Time time;
319 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
320 LogAndSetError(error, FROM_HERE, "No pending update.");
321 return false;
322 }
323
324 ClockInterface* clock = system_state_->clock();
325 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
326 return true;
327}
328
329bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
330 string* out_prev_version) {
331 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
332 return true;
333}
334
335bool UpdateEngineService::GetRollbackPartition(
336 ErrorPtr* /* error */, string* out_rollback_partition_name) {
337 BootControlInterface::Slot rollback_slot =
338 system_state_->update_attempter()->GetRollbackSlot();
339
340 if (rollback_slot == BootControlInterface::kInvalidSlot) {
341 out_rollback_partition_name->clear();
342 return true;
343 }
344
345 string name;
346 if (!system_state_->boot_control()->GetPartitionDevice(
347 "KERNEL", rollback_slot, &name)) {
348 LOG(ERROR) << "Invalid rollback device";
349 return false;
350 }
351
352 LOG(INFO) << "Getting rollback partition name. Result: " << name;
353 *out_rollback_partition_name = name;
354 return true;
355}
356
Shuqian Zhao29971732016-02-05 11:29:32 -0800357bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
358 int32_t* out_last_attempt_error) {
359 ErrorCode error_code = system_state_->payload_state()->GetAttemptErrorCode();
360 *out_last_attempt_error = static_cast<int>(error_code);
361 return true;
362}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700363
364bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
365 int32_t* out_eol_status) {
366 PrefsInterface* prefs = system_state_->prefs();
367
368 string str_eol_status;
369 if (prefs->Exists(kPrefsOmahaEolStatus) &&
370 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
371 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
372 return false;
373 }
374
375 // StringToEolStatus will return kSupported for invalid values.
376 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
377 return true;
378}
379
Casey Dahlina93cd532016-01-14 16:55:11 -0800380} // namespace chromeos_update_engine