blob: 3c77c93e0235837777afae1f0048a8a1ab10d299 [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
19#include <set>
20#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"
36#include "update_engine/p2p_manager.h"
37#include "update_engine/update_attempter.h"
38
39using base::StringPrintf;
40using brillo::ErrorPtr;
41using brillo::string_utils::ToString;
42using std::set;
43using 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
188bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
189 bool in_enabled) {
190 PrefsInterface* prefs = system_state_->prefs();
191
192 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
193 LogAndSetError(
194 error,
195 FROM_HERE,
196 StringPrintf("Error setting the update via p2p permission to %s.",
197 ToString(in_enabled).c_str()));
198 return false;
199 }
200 return true;
201}
202
203bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
204 bool* out_enabled) {
205 PrefsInterface* prefs = system_state_->prefs();
206
207 bool p2p_pref = false; // Default if no setting is present.
208 if (prefs->Exists(kPrefsP2PEnabled) &&
209 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
210 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
211 return false;
212 }
213
214 *out_enabled = p2p_pref;
215 return true;
216}
217
218bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
219 bool in_allowed) {
220 set<string> allowed_types;
221 const policy::DevicePolicy* device_policy = system_state_->device_policy();
222
223 // The device_policy is loaded in a lazy way before an update check. Load it
224 // now from the libbrillo cache if it wasn't already loaded.
225 if (!device_policy) {
226 UpdateAttempter* update_attempter = system_state_->update_attempter();
227 if (update_attempter) {
228 update_attempter->RefreshDevicePolicy();
229 device_policy = system_state_->device_policy();
230 }
231 }
232
233 // Check if this setting is allowed by the device policy.
234 if (device_policy &&
235 device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
236 LogAndSetError(error,
237 FROM_HERE,
238 "Ignoring the update over cellular setting since there's "
239 "a device policy enforcing this setting.");
240 return false;
241 }
242
243 // If the policy wasn't loaded yet, then it is still OK to change the local
244 // setting because the policy will be checked again during the update check.
245
246 PrefsInterface* prefs = system_state_->prefs();
247
248 if (!prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
249 LogAndSetError(error,
250 FROM_HERE,
251 string("Error setting the update over cellular to ") +
252 (in_allowed ? "true" : "false"));
253 return false;
254 }
255 return true;
256}
257
258bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* /* error */,
259 bool* out_allowed) {
260 ConnectionManagerInterface* cm = system_state_->connection_manager();
261
262 // The device_policy is loaded in a lazy way before an update check and is
263 // used to determine if an update is allowed over cellular. Load the device
264 // policy now from the libbrillo cache if it wasn't already loaded.
265 if (!system_state_->device_policy()) {
266 UpdateAttempter* update_attempter = system_state_->update_attempter();
267 if (update_attempter)
268 update_attempter->RefreshDevicePolicy();
269 }
270
271 // Return the current setting based on the same logic used while checking for
272 // updates. A log message could be printed as the result of this test.
273 LOG(INFO) << "Checking if updates over cellular networks are allowed:";
274 *out_allowed = cm->IsUpdateAllowedOver(
275 chromeos_update_engine::NetworkConnectionType::kCellular,
276 chromeos_update_engine::NetworkTethering::kUnknown);
277 return true;
278}
279
280bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
281 int64_t* out_usec_wallclock) {
282 base::Time time;
283 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
284 LogAndSetError(error, FROM_HERE, "No pending update.");
285 return false;
286 }
287
288 ClockInterface* clock = system_state_->clock();
289 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
290 return true;
291}
292
293bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
294 string* out_prev_version) {
295 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
296 return true;
297}
298
299bool UpdateEngineService::GetRollbackPartition(
300 ErrorPtr* /* error */, string* out_rollback_partition_name) {
301 BootControlInterface::Slot rollback_slot =
302 system_state_->update_attempter()->GetRollbackSlot();
303
304 if (rollback_slot == BootControlInterface::kInvalidSlot) {
305 out_rollback_partition_name->clear();
306 return true;
307 }
308
309 string name;
310 if (!system_state_->boot_control()->GetPartitionDevice(
311 "KERNEL", rollback_slot, &name)) {
312 LOG(ERROR) << "Invalid rollback device";
313 return false;
314 }
315
316 LOG(INFO) << "Getting rollback partition name. Result: " << name;
317 *out_rollback_partition_name = name;
318 return true;
319}
320
321} // namespace chromeos_update_engine