Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 1 | // |
| 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 Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 19 | #include <set> |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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" |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 36 | #include "update_engine/omaha_utils.h" |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 37 | #include "update_engine/p2p_manager.h" |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 38 | #include "update_engine/payload_state_interface.h" |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 39 | #include "update_engine/update_attempter.h" |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 40 | |
| 41 | using base::StringPrintf; |
| 42 | using brillo::ErrorPtr; |
| 43 | using brillo::string_utils::ToString; |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 44 | using std::set; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 45 | using std::string; |
| 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | |
| 49 | namespace { |
| 50 | // Log and set the error on the passed ErrorPtr. |
| 51 | void LogAndSetError(ErrorPtr* error, |
| 52 | const tracked_objects::Location& location, |
| 53 | const string& reason) { |
| 54 | brillo::Error::AddTo(error, |
| 55 | location, |
| 56 | UpdateEngineService::kErrorDomain, |
| 57 | UpdateEngineService::kErrorFailed, |
| 58 | reason); |
| 59 | LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": " |
| 60 | << reason; |
| 61 | } |
| 62 | } // namespace |
| 63 | |
| 64 | const char* const UpdateEngineService::kErrorDomain = "update_engine"; |
| 65 | const char* const UpdateEngineService::kErrorFailed = |
| 66 | "org.chromium.UpdateEngine.Error.Failed"; |
| 67 | |
| 68 | UpdateEngineService::UpdateEngineService(SystemState* system_state) |
| 69 | : system_state_(system_state) { |
| 70 | } |
| 71 | |
| 72 | // org::chromium::UpdateEngineInterfaceInterface methods implementation. |
| 73 | |
| 74 | bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */, |
| 75 | const string& in_app_version, |
| 76 | const string& in_omaha_url, |
| 77 | int32_t in_flags_as_int) { |
| 78 | AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>(in_flags_as_int); |
| 79 | bool interactive = !(flags & kAttemptUpdateFlagNonInteractive); |
| 80 | |
| 81 | LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" " |
| 82 | << "omaha_url=\"" << in_omaha_url << "\" " |
| 83 | << "flags=0x" << std::hex << flags << " " |
| 84 | << "interactive=" << (interactive ? "yes" : "no"); |
| 85 | system_state_->update_attempter()->CheckForUpdate( |
| 86 | in_app_version, in_omaha_url, interactive); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) { |
| 91 | LOG(INFO) << "Attempting rollback to non-active partitions."; |
| 92 | |
| 93 | if (!system_state_->update_attempter()->Rollback(in_powerwash)) { |
| 94 | // TODO(dgarrett): Give a more specific error code/reason. |
| 95 | LogAndSetError(error, FROM_HERE, "Rollback attempt failed."); |
| 96 | return false; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool UpdateEngineService::CanRollback(ErrorPtr* /* error */, |
| 102 | bool* out_can_rollback) { |
| 103 | bool can_rollback = system_state_->update_attempter()->CanRollback(); |
| 104 | LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback; |
| 105 | *out_can_rollback = can_rollback; |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool UpdateEngineService::ResetStatus(ErrorPtr* error) { |
| 110 | if (!system_state_->update_attempter()->ResetStatus()) { |
| 111 | // TODO(dgarrett): Give a more specific error code/reason. |
| 112 | LogAndSetError(error, FROM_HERE, "ResetStatus failed."); |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool UpdateEngineService::GetStatus(ErrorPtr* error, |
| 119 | int64_t* out_last_checked_time, |
| 120 | double* out_progress, |
| 121 | string* out_current_operation, |
| 122 | string* out_new_version, |
| 123 | int64_t* out_new_size) { |
| 124 | if (!system_state_->update_attempter()->GetStatus(out_last_checked_time, |
| 125 | out_progress, |
| 126 | out_current_operation, |
| 127 | out_new_version, |
| 128 | out_new_size)) { |
| 129 | LogAndSetError(error, FROM_HERE, "GetStatus failed."); |
| 130 | return false; |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) { |
| 136 | if (!system_state_->update_attempter()->RebootIfNeeded()) { |
| 137 | // TODO(dgarrett): Give a more specific error code/reason. |
| 138 | LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed."); |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | bool UpdateEngineService::SetChannel(ErrorPtr* error, |
| 145 | const string& in_target_channel, |
| 146 | bool in_is_powerwash_allowed) { |
| 147 | const policy::DevicePolicy* device_policy = system_state_->device_policy(); |
| 148 | |
| 149 | // The device_policy is loaded in a lazy way before an update check. Load it |
| 150 | // now from the libbrillo cache if it wasn't already loaded. |
| 151 | if (!device_policy) { |
| 152 | UpdateAttempter* update_attempter = system_state_->update_attempter(); |
| 153 | if (update_attempter) { |
| 154 | update_attempter->RefreshDevicePolicy(); |
| 155 | device_policy = system_state_->device_policy(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | bool delegated = false; |
| 160 | if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) && |
| 161 | !delegated) { |
| 162 | LogAndSetError(error, |
| 163 | FROM_HERE, |
| 164 | "Cannot set target channel explicitly when channel " |
| 165 | "policy/settings is not delegated"); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | LOG(INFO) << "Setting destination channel to: " << in_target_channel; |
| 170 | string error_message; |
| 171 | if (!system_state_->request_params()->SetTargetChannel( |
| 172 | in_target_channel, in_is_powerwash_allowed, &error_message)) { |
| 173 | LogAndSetError(error, FROM_HERE, error_message); |
| 174 | return false; |
| 175 | } |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool 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 Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 188 | bool 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 | |
| 206 | bool 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 219 | bool 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 | |
| 234 | bool 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 | |
| 249 | bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error, |
| 250 | bool in_allowed) { |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 251 | set<string> allowed_types; |
| 252 | const policy::DevicePolicy* device_policy = system_state_->device_policy(); |
| 253 | |
| 254 | // The device_policy is loaded in a lazy way before an update check. Load it |
| 255 | // now from the libbrillo cache if it wasn't already loaded. |
| 256 | if (!device_policy) { |
| 257 | UpdateAttempter* update_attempter = system_state_->update_attempter(); |
| 258 | if (update_attempter) { |
| 259 | update_attempter->RefreshDevicePolicy(); |
| 260 | device_policy = system_state_->device_policy(); |
| 261 | } |
| 262 | } |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 263 | |
| 264 | // Check if this setting is allowed by the device policy. |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 265 | if (device_policy && |
| 266 | device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 267 | LogAndSetError(error, |
| 268 | FROM_HERE, |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 269 | "Ignoring the update over cellular setting since there's " |
| 270 | "a device policy enforcing this setting."); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | // If the policy wasn't loaded yet, then it is still OK to change the local |
| 275 | // setting because the policy will be checked again during the update check. |
| 276 | |
| 277 | PrefsInterface* prefs = system_state_->prefs(); |
| 278 | |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 279 | if (!prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) { |
| 280 | LogAndSetError(error, |
| 281 | FROM_HERE, |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 282 | string("Error setting the update over cellular to ") + |
| 283 | (in_allowed ? "true" : "false")); |
| 284 | return false; |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 289 | bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* /* error */, |
Weidong Guo | 4b0d603 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 290 | bool* out_allowed) { |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 291 | ConnectionManagerInterface* cm = system_state_->connection_manager(); |
Weidong Guo | 4b0d603 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 292 | |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 293 | // The device_policy is loaded in a lazy way before an update check and is |
| 294 | // used to determine if an update is allowed over cellular. Load the device |
| 295 | // policy now from the libbrillo cache if it wasn't already loaded. |
| 296 | if (!system_state_->device_policy()) { |
| 297 | UpdateAttempter* update_attempter = system_state_->update_attempter(); |
| 298 | if (update_attempter) |
| 299 | update_attempter->RefreshDevicePolicy(); |
Weidong Guo | 4b0d603 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 300 | } |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 301 | |
| 302 | // Return the current setting based on the same logic used while checking for |
| 303 | // updates. A log message could be printed as the result of this test. |
| 304 | LOG(INFO) << "Checking if updates over cellular networks are allowed:"; |
| 305 | *out_allowed = cm->IsUpdateAllowedOver(ConnectionType::kCellular, |
| 306 | ConnectionTethering::kUnknown); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
| 310 | bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error, |
| 311 | int64_t* out_usec_wallclock) { |
| 312 | base::Time time; |
| 313 | if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) { |
| 314 | LogAndSetError(error, FROM_HERE, "No pending update."); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | ClockInterface* clock = system_state_->clock(); |
| 319 | *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds(); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */, |
| 324 | string* out_prev_version) { |
| 325 | *out_prev_version = system_state_->update_attempter()->GetPrevVersion(); |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | bool UpdateEngineService::GetRollbackPartition( |
| 330 | ErrorPtr* /* error */, string* out_rollback_partition_name) { |
| 331 | BootControlInterface::Slot rollback_slot = |
| 332 | system_state_->update_attempter()->GetRollbackSlot(); |
| 333 | |
| 334 | if (rollback_slot == BootControlInterface::kInvalidSlot) { |
| 335 | out_rollback_partition_name->clear(); |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | string name; |
| 340 | if (!system_state_->boot_control()->GetPartitionDevice( |
| 341 | "KERNEL", rollback_slot, &name)) { |
| 342 | LOG(ERROR) << "Invalid rollback device"; |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | LOG(INFO) << "Getting rollback partition name. Result: " << name; |
| 347 | *out_rollback_partition_name = name; |
| 348 | return true; |
| 349 | } |
| 350 | |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 351 | bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */, |
| 352 | int32_t* out_last_attempt_error) { |
| 353 | ErrorCode error_code = system_state_->payload_state()->GetAttemptErrorCode(); |
| 354 | *out_last_attempt_error = static_cast<int>(error_code); |
| 355 | return true; |
| 356 | } |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 357 | |
| 358 | bool UpdateEngineService::GetEolStatus(ErrorPtr* error, |
| 359 | int32_t* out_eol_status) { |
| 360 | PrefsInterface* prefs = system_state_->prefs(); |
| 361 | |
| 362 | string str_eol_status; |
| 363 | if (prefs->Exists(kPrefsOmahaEolStatus) && |
| 364 | !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) { |
| 365 | LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status."); |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | // StringToEolStatus will return kSupported for invalid values. |
| 370 | *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status)); |
| 371 | return true; |
| 372 | } |
| 373 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 374 | } // namespace chromeos_update_engine |