blob: a99d10c87278f67a0e3712a1607e7c1d0ebc01e2 [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,
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070053 const base::Location& location,
Casey Dahlina93cd532016-01-14 16:55:11 -080054 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)
Amin Hassani7cc8bb02019-01-14 16:29:47 -080070 : system_state_(system_state) {}
Casey Dahlina93cd532016-01-14 16:55:11 -080071
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
Xiaochu Liu88d90382018-08-29 16:09:11 -0700106bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
107 const string& omaha_url,
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800108 const vector<string>& dlc_module_ids) {
109 if (!system_state_->update_attempter()->CheckForInstall(dlc_module_ids,
110 omaha_url)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700111 // TODO(xiaochu): support more detailed error messages.
112 LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
113 return false;
114 }
115 return true;
116}
117
Casey Dahlina93cd532016-01-14 16:55:11 -0800118bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
119 LOG(INFO) << "Attempting rollback to non-active partitions.";
120
121 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
122 // TODO(dgarrett): Give a more specific error code/reason.
123 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
124 return false;
125 }
126 return true;
127}
128
129bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
130 bool* out_can_rollback) {
131 bool can_rollback = system_state_->update_attempter()->CanRollback();
132 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
133 *out_can_rollback = can_rollback;
134 return true;
135}
136
137bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
138 if (!system_state_->update_attempter()->ResetStatus()) {
139 // TODO(dgarrett): Give a more specific error code/reason.
140 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
141 return false;
142 }
143 return true;
144}
145
146bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700147 UpdateEngineStatus* out_status) {
148 if (!system_state_->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800149 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
150 return false;
151 }
152 return true;
153}
154
155bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
156 if (!system_state_->update_attempter()->RebootIfNeeded()) {
157 // TODO(dgarrett): Give a more specific error code/reason.
158 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
159 return false;
160 }
161 return true;
162}
163
164bool UpdateEngineService::SetChannel(ErrorPtr* error,
165 const string& in_target_channel,
166 bool in_is_powerwash_allowed) {
167 const policy::DevicePolicy* device_policy = system_state_->device_policy();
168
169 // The device_policy is loaded in a lazy way before an update check. Load it
170 // now from the libbrillo cache if it wasn't already loaded.
171 if (!device_policy) {
172 UpdateAttempter* update_attempter = system_state_->update_attempter();
173 if (update_attempter) {
174 update_attempter->RefreshDevicePolicy();
175 device_policy = system_state_->device_policy();
176 }
177 }
178
179 bool delegated = false;
180 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
181 !delegated) {
182 LogAndSetError(error,
183 FROM_HERE,
184 "Cannot set target channel explicitly when channel "
185 "policy/settings is not delegated");
186 return false;
187 }
188
189 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
190 string error_message;
191 if (!system_state_->request_params()->SetTargetChannel(
192 in_target_channel, in_is_powerwash_allowed, &error_message)) {
193 LogAndSetError(error, FROM_HERE, error_message);
194 return false;
195 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800196 return true;
197}
198
199bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
200 bool in_get_current_channel,
201 string* out_channel) {
202 OmahaRequestParams* rp = system_state_->request_params();
203 *out_channel =
204 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
205 return true;
206}
207
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700208bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
209 string in_cohort_hint) {
210 PrefsInterface* prefs = system_state_->prefs();
211
212 // It is ok to override the cohort hint with an invalid value since it is
213 // stored in stateful partition. The code reading it should sanitize it
214 // anyway.
215 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
216 LogAndSetError(
217 error,
218 FROM_HERE,
219 StringPrintf("Error setting the cohort hint value to \"%s\".",
220 in_cohort_hint.c_str()));
221 return false;
222 }
223 return true;
224}
225
226bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
227 string* out_cohort_hint) {
228 PrefsInterface* prefs = system_state_->prefs();
229
230 *out_cohort_hint = "";
231 if (prefs->Exists(kPrefsOmahaCohortHint) &&
232 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
233 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
234 return false;
235 }
236 return true;
237}
238
Casey Dahlina93cd532016-01-14 16:55:11 -0800239bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
240 bool in_enabled) {
241 PrefsInterface* prefs = system_state_->prefs();
242
243 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
244 LogAndSetError(
245 error,
246 FROM_HERE,
247 StringPrintf("Error setting the update via p2p permission to %s.",
248 ToString(in_enabled).c_str()));
249 return false;
250 }
251 return true;
252}
253
254bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
255 bool* out_enabled) {
256 PrefsInterface* prefs = system_state_->prefs();
257
258 bool p2p_pref = false; // Default if no setting is present.
259 if (prefs->Exists(kPrefsP2PEnabled) &&
260 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
261 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
262 return false;
263 }
264
265 *out_enabled = p2p_pref;
266 return true;
267}
268
269bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
270 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700271 ConnectionManagerInterface* connection_manager =
272 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800273
274 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700275 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700276 LogAndSetError(error,
277 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800278 "Ignoring the update over cellular setting since there's "
279 "a device policy enforcing this setting.");
280 return false;
281 }
282
283 // If the policy wasn't loaded yet, then it is still OK to change the local
284 // setting because the policy will be checked again during the update check.
285
286 PrefsInterface* prefs = system_state_->prefs();
287
Weidong Guo421ff332017-04-17 10:08:38 -0700288 if (!prefs ||
289 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700290 LogAndSetError(error,
291 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800292 string("Error setting the update over cellular to ") +
293 (in_allowed ? "true" : "false"));
294 return false;
295 }
296 return true;
297}
298
Weidong Guo421ff332017-04-17 10:08:38 -0700299bool UpdateEngineService::SetUpdateOverCellularTarget(
300 brillo::ErrorPtr* error,
301 const std::string& target_version,
302 int64_t target_size) {
303 ConnectionManagerInterface* connection_manager =
304 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700305
Weidong Guo421ff332017-04-17 10:08:38 -0700306 // Check if this setting is allowed by the device policy.
307 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
308 LogAndSetError(error,
309 FROM_HERE,
310 "Ignoring the update over cellular setting since there's "
311 "a device policy enforcing this setting.");
312 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700313 }
Tao Bao5688d162017-06-06 13:09:06 -0700314
Weidong Guo421ff332017-04-17 10:08:38 -0700315 // If the policy wasn't loaded yet, then it is still OK to change the local
316 // setting because the policy will be checked again during the update check.
317
318 PrefsInterface* prefs = system_state_->prefs();
319
320 if (!prefs ||
321 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
322 target_version) ||
323 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
324 LogAndSetError(
325 error, FROM_HERE, "Error setting the target for update over cellular.");
326 return false;
327 }
328 return true;
329}
330
331bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
332 bool* out_allowed) {
333 ConnectionManagerInterface* connection_manager =
334 system_state_->connection_manager();
335
336 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
337 // We have device policy, so ignore the user preferences.
338 *out_allowed = connection_manager->IsUpdateAllowedOver(
339 ConnectionType::kCellular, ConnectionTethering::kUnknown);
340 } else {
341 PrefsInterface* prefs = system_state_->prefs();
342
343 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
344 // Update is not allowed as user preference is not set or not available.
345 *out_allowed = false;
346 return true;
347 }
348
349 bool is_allowed;
350
351 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
352 LogAndSetError(error,
353 FROM_HERE,
354 "Error getting the update over cellular preference.");
355 return false;
356 }
357 *out_allowed = is_allowed;
358 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800359 return true;
360}
361
362bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
363 int64_t* out_usec_wallclock) {
364 base::Time time;
365 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
366 LogAndSetError(error, FROM_HERE, "No pending update.");
367 return false;
368 }
369
370 ClockInterface* clock = system_state_->clock();
371 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
372 return true;
373}
374
375bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
376 string* out_prev_version) {
377 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
378 return true;
379}
380
381bool UpdateEngineService::GetRollbackPartition(
382 ErrorPtr* /* error */, string* out_rollback_partition_name) {
383 BootControlInterface::Slot rollback_slot =
384 system_state_->update_attempter()->GetRollbackSlot();
385
386 if (rollback_slot == BootControlInterface::kInvalidSlot) {
387 out_rollback_partition_name->clear();
388 return true;
389 }
390
391 string name;
392 if (!system_state_->boot_control()->GetPartitionDevice(
393 "KERNEL", rollback_slot, &name)) {
394 LOG(ERROR) << "Invalid rollback device";
395 return false;
396 }
397
398 LOG(INFO) << "Getting rollback partition name. Result: " << name;
399 *out_rollback_partition_name = name;
400 return true;
401}
402
Shuqian Zhao29971732016-02-05 11:29:32 -0800403bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
404 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700405 ErrorCode error_code =
406 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800407 *out_last_attempt_error = static_cast<int>(error_code);
408 return true;
409}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700410
Casey Dahlina93cd532016-01-14 16:55:11 -0800411} // namespace chromeos_update_engine