blob: 15feca75fc6673c01dbf042495b047b6ea861430 [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 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,
53 const tracked_objects::Location& location,
54 const string& reason) {
55 brillo::Error::AddTo(error,
56 location,
57 UpdateEngineService::kErrorDomain,
58 UpdateEngineService::kErrorFailed,
59 reason);
60 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
61 << reason;
62}
63} // namespace
64
65const char* const UpdateEngineService::kErrorDomain = "update_engine";
66const char* const UpdateEngineService::kErrorFailed =
67 "org.chromium.UpdateEngine.Error.Failed";
68
69UpdateEngineService::UpdateEngineService(SystemState* system_state)
70 : system_state_(system_state) {
71}
72
73// org::chromium::UpdateEngineInterfaceInterface methods implementation.
74
75bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
76 const string& in_app_version,
77 const string& in_omaha_url,
78 int32_t in_flags_as_int) {
79 AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>(in_flags_as_int);
80 bool interactive = !(flags & kAttemptUpdateFlagNonInteractive);
81
82 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
83 << "omaha_url=\"" << in_omaha_url << "\" "
84 << "flags=0x" << std::hex << flags << " "
85 << "interactive=" << (interactive ? "yes" : "no");
86 system_state_->update_attempter()->CheckForUpdate(
87 in_app_version, in_omaha_url, interactive);
88 return true;
89}
90
91bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
92 LOG(INFO) << "Attempting rollback to non-active partitions.";
93
94 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
95 // TODO(dgarrett): Give a more specific error code/reason.
96 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
97 return false;
98 }
99 return true;
100}
101
102bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
103 bool* out_can_rollback) {
104 bool can_rollback = system_state_->update_attempter()->CanRollback();
105 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
106 *out_can_rollback = can_rollback;
107 return true;
108}
109
110bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
111 if (!system_state_->update_attempter()->ResetStatus()) {
112 // TODO(dgarrett): Give a more specific error code/reason.
113 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
114 return false;
115 }
116 return true;
117}
118
119bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700120 UpdateEngineStatus* out_status) {
121 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800122 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
123 return false;
124 }
125 return true;
126}
127
128bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
129 if (!system_state_->update_attempter()->RebootIfNeeded()) {
130 // TODO(dgarrett): Give a more specific error code/reason.
131 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
132 return false;
133 }
134 return true;
135}
136
137bool UpdateEngineService::SetChannel(ErrorPtr* error,
138 const string& in_target_channel,
139 bool in_is_powerwash_allowed) {
140 const policy::DevicePolicy* device_policy = system_state_->device_policy();
141
142 // The device_policy is loaded in a lazy way before an update check. Load it
143 // now from the libbrillo cache if it wasn't already loaded.
144 if (!device_policy) {
145 UpdateAttempter* update_attempter = system_state_->update_attempter();
146 if (update_attempter) {
147 update_attempter->RefreshDevicePolicy();
148 device_policy = system_state_->device_policy();
149 }
150 }
151
152 bool delegated = false;
153 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
154 !delegated) {
155 LogAndSetError(error,
156 FROM_HERE,
157 "Cannot set target channel explicitly when channel "
158 "policy/settings is not delegated");
159 return false;
160 }
161
162 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
163 string error_message;
164 if (!system_state_->request_params()->SetTargetChannel(
165 in_target_channel, in_is_powerwash_allowed, &error_message)) {
166 LogAndSetError(error, FROM_HERE, error_message);
167 return false;
168 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800169 return true;
170}
171
172bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
173 bool in_get_current_channel,
174 string* out_channel) {
175 OmahaRequestParams* rp = system_state_->request_params();
176 *out_channel =
177 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
178 return true;
179}
180
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700181bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
182 string in_cohort_hint) {
183 PrefsInterface* prefs = system_state_->prefs();
184
185 // It is ok to override the cohort hint with an invalid value since it is
186 // stored in stateful partition. The code reading it should sanitize it
187 // anyway.
188 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
189 LogAndSetError(
190 error,
191 FROM_HERE,
192 StringPrintf("Error setting the cohort hint value to \"%s\".",
193 in_cohort_hint.c_str()));
194 return false;
195 }
196 return true;
197}
198
199bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
200 string* out_cohort_hint) {
201 PrefsInterface* prefs = system_state_->prefs();
202
203 *out_cohort_hint = "";
204 if (prefs->Exists(kPrefsOmahaCohortHint) &&
205 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
206 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
207 return false;
208 }
209 return true;
210}
211
Casey Dahlina93cd532016-01-14 16:55:11 -0800212bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
213 bool in_enabled) {
214 PrefsInterface* prefs = system_state_->prefs();
215
216 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
217 LogAndSetError(
218 error,
219 FROM_HERE,
220 StringPrintf("Error setting the update via p2p permission to %s.",
221 ToString(in_enabled).c_str()));
222 return false;
223 }
224 return true;
225}
226
227bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
228 bool* out_enabled) {
229 PrefsInterface* prefs = system_state_->prefs();
230
231 bool p2p_pref = false; // Default if no setting is present.
232 if (prefs->Exists(kPrefsP2PEnabled) &&
233 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
234 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
235 return false;
236 }
237
238 *out_enabled = p2p_pref;
239 return true;
240}
241
242bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
243 bool in_allowed) {
Tao Bao5688d162017-06-06 13:09:06 -0700244 set<string> allowed_types;
245 const policy::DevicePolicy* device_policy = system_state_->device_policy();
246
247 // The device_policy is loaded in a lazy way before an update check. Load it
248 // now from the libbrillo cache if it wasn't already loaded.
249 if (!device_policy) {
250 UpdateAttempter* update_attempter = system_state_->update_attempter();
251 if (update_attempter) {
252 update_attempter->RefreshDevicePolicy();
253 device_policy = system_state_->device_policy();
254 }
255 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800256
257 // Check if this setting is allowed by the device policy.
Tao Bao5688d162017-06-06 13:09:06 -0700258 if (device_policy &&
259 device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
260 LogAndSetError(error,
261 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800262 "Ignoring the update over cellular setting since there's "
263 "a device policy enforcing this setting.");
264 return false;
265 }
266
267 // If the policy wasn't loaded yet, then it is still OK to change the local
268 // setting because the policy will be checked again during the update check.
269
270 PrefsInterface* prefs = system_state_->prefs();
271
Tao Bao5688d162017-06-06 13:09:06 -0700272 if (!prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
273 LogAndSetError(error,
274 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800275 string("Error setting the update over cellular to ") +
276 (in_allowed ? "true" : "false"));
277 return false;
278 }
279 return true;
280}
281
Tao Bao5688d162017-06-06 13:09:06 -0700282bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* /* error */,
Weidong Guo4b0d6032017-04-17 10:08:38 -0700283 bool* out_allowed) {
Tao Bao5688d162017-06-06 13:09:06 -0700284 ConnectionManagerInterface* cm = system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700285
Tao Bao5688d162017-06-06 13:09:06 -0700286 // The device_policy is loaded in a lazy way before an update check and is
287 // used to determine if an update is allowed over cellular. Load the device
288 // policy now from the libbrillo cache if it wasn't already loaded.
289 if (!system_state_->device_policy()) {
290 UpdateAttempter* update_attempter = system_state_->update_attempter();
291 if (update_attempter)
292 update_attempter->RefreshDevicePolicy();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700293 }
Tao Bao5688d162017-06-06 13:09:06 -0700294
295 // Return the current setting based on the same logic used while checking for
296 // updates. A log message could be printed as the result of this test.
297 LOG(INFO) << "Checking if updates over cellular networks are allowed:";
298 *out_allowed = cm->IsUpdateAllowedOver(ConnectionType::kCellular,
299 ConnectionTethering::kUnknown);
Casey Dahlina93cd532016-01-14 16:55:11 -0800300 return true;
301}
302
303bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
304 int64_t* out_usec_wallclock) {
305 base::Time time;
306 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
307 LogAndSetError(error, FROM_HERE, "No pending update.");
308 return false;
309 }
310
311 ClockInterface* clock = system_state_->clock();
312 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
313 return true;
314}
315
316bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
317 string* out_prev_version) {
318 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
319 return true;
320}
321
322bool UpdateEngineService::GetRollbackPartition(
323 ErrorPtr* /* error */, string* out_rollback_partition_name) {
324 BootControlInterface::Slot rollback_slot =
325 system_state_->update_attempter()->GetRollbackSlot();
326
327 if (rollback_slot == BootControlInterface::kInvalidSlot) {
328 out_rollback_partition_name->clear();
329 return true;
330 }
331
332 string name;
333 if (!system_state_->boot_control()->GetPartitionDevice(
334 "KERNEL", rollback_slot, &name)) {
335 LOG(ERROR) << "Invalid rollback device";
336 return false;
337 }
338
339 LOG(INFO) << "Getting rollback partition name. Result: " << name;
340 *out_rollback_partition_name = name;
341 return true;
342}
343
Shuqian Zhao29971732016-02-05 11:29:32 -0800344bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
345 int32_t* out_last_attempt_error) {
346 ErrorCode error_code = system_state_->payload_state()->GetAttemptErrorCode();
347 *out_last_attempt_error = static_cast<int>(error_code);
348 return true;
349}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700350
351bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
352 int32_t* out_eol_status) {
353 PrefsInterface* prefs = system_state_->prefs();
354
355 string str_eol_status;
356 if (prefs->Exists(kPrefsOmahaEolStatus) &&
357 !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
358 LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
359 return false;
360 }
361
362 // StringToEolStatus will return kSupported for invalid values.
363 *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
364 return true;
365}
366
Casey Dahlina93cd532016-01-14 16:55:11 -0800367} // namespace chromeos_update_engine