update_attempter.cc revision 22ad86121ba56c576bfcaa23e085dab881bd4ff5
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/update_attempter.h" 18 19#include <stdint.h> 20 21#include <algorithm> 22#include <memory> 23#include <set> 24#include <string> 25#include <utility> 26#include <vector> 27 28#include <base/bind.h> 29#include <base/files/file_util.h> 30#include <base/logging.h> 31#include <base/rand_util.h> 32#include <base/strings/string_util.h> 33#include <base/strings/stringprintf.h> 34#include <brillo/bind_lambda.h> 35#include <brillo/message_loops/message_loop.h> 36#include <debugd/dbus-constants.h> 37#include <metrics/metrics_library.h> 38#include <policy/device_policy.h> 39#include <policy/libpolicy.h> 40#include <power_manager/dbus-constants.h> 41#include <power_manager/dbus-proxies.h> 42#include <update_engine/dbus-constants.h> 43 44#include "update_engine/common/boot_control_interface.h" 45#include "update_engine/common/certificate_checker.h" 46#include "update_engine/common/clock_interface.h" 47#include "update_engine/common/constants.h" 48#include "update_engine/common/hardware_interface.h" 49#include "update_engine/common/libcurl_http_fetcher.h" 50#include "update_engine/common/multi_range_http_fetcher.h" 51#include "update_engine/common/platform_constants.h" 52#include "update_engine/common/prefs_interface.h" 53#include "update_engine/common/subprocess.h" 54#include "update_engine/common/utils.h" 55#include "update_engine/dbus_service.h" 56#include "update_engine/metrics.h" 57#include "update_engine/omaha_request_action.h" 58#include "update_engine/omaha_request_params.h" 59#include "update_engine/omaha_response_handler_action.h" 60#include "update_engine/p2p_manager.h" 61#include "update_engine/payload_consumer/download_action.h" 62#include "update_engine/payload_consumer/filesystem_verifier_action.h" 63#include "update_engine/payload_consumer/postinstall_runner_action.h" 64#include "update_engine/payload_state_interface.h" 65#include "update_engine/system_state.h" 66#include "update_engine/update_manager/policy.h" 67#include "update_engine/update_manager/update_manager.h" 68#include "update_engine/update_status_utils.h" 69 70using base::Bind; 71using base::Callback; 72using base::Time; 73using base::TimeDelta; 74using base::TimeTicks; 75using brillo::MessageLoop; 76using chromeos_update_manager::EvalStatus; 77using chromeos_update_manager::Policy; 78using chromeos_update_manager::UpdateCheckParams; 79using std::set; 80using std::shared_ptr; 81using std::string; 82using std::vector; 83 84namespace chromeos_update_engine { 85 86const int UpdateAttempter::kMaxDeltaUpdateFailures = 3; 87 88namespace { 89const int kMaxConsecutiveObeyProxyRequests = 20; 90 91// By default autest bypasses scattering. If we want to test scattering, 92// use kScheduledAUTestURLRequest. The URL used is same in both cases, but 93// different params are passed to CheckForUpdate(). 94const char kAUTestURLRequest[] = "autest"; 95const char kScheduledAUTestURLRequest[] = "autest-scheduled"; 96} // namespace 97 98// Turns a generic ErrorCode::kError to a generic error code specific 99// to |action| (e.g., ErrorCode::kFilesystemVerifierError). If |code| is 100// not ErrorCode::kError, or the action is not matched, returns |code| 101// unchanged. 102ErrorCode GetErrorCodeForAction(AbstractAction* action, 103 ErrorCode code) { 104 if (code != ErrorCode::kError) 105 return code; 106 107 const string type = action->Type(); 108 if (type == OmahaRequestAction::StaticType()) 109 return ErrorCode::kOmahaRequestError; 110 if (type == OmahaResponseHandlerAction::StaticType()) 111 return ErrorCode::kOmahaResponseHandlerError; 112 if (type == FilesystemVerifierAction::StaticType()) 113 return ErrorCode::kFilesystemVerifierError; 114 if (type == PostinstallRunnerAction::StaticType()) 115 return ErrorCode::kPostinstallRunnerError; 116 117 return code; 118} 119 120UpdateAttempter::UpdateAttempter( 121 SystemState* system_state, 122 LibCrosProxy* libcros_proxy, 123 org::chromium::debugdProxyInterface* debugd_proxy) 124 : processor_(new ActionProcessor()), 125 system_state_(system_state), 126 chrome_proxy_resolver_(libcros_proxy), 127 debugd_proxy_(debugd_proxy) { 128} 129 130UpdateAttempter::~UpdateAttempter() { 131 CleanupCpuSharesManagement(); 132} 133 134void UpdateAttempter::Init() { 135 // Pulling from the SystemState can only be done after construction, since 136 // this is an aggregate of various objects (such as the UpdateAttempter), 137 // which requires them all to be constructed prior to it being used. 138 prefs_ = system_state_->prefs(); 139 omaha_request_params_ = system_state_->request_params(); 140 141 // In case of update_engine restart without a reboot we need to restore the 142 // reboot needed state. 143 if (GetBootTimeAtUpdate(nullptr)) 144 status_ = UpdateStatus::UPDATED_NEED_REBOOT; 145 else 146 status_ = UpdateStatus::IDLE; 147} 148 149void UpdateAttempter::ScheduleUpdates() { 150 if (IsUpdateRunningOrScheduled()) 151 return; 152 153 chromeos_update_manager::UpdateManager* const update_manager = 154 system_state_->update_manager(); 155 CHECK(update_manager); 156 Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind( 157 &UpdateAttempter::OnUpdateScheduled, base::Unretained(this)); 158 // We limit the async policy request to a reasonably short time, to avoid a 159 // starvation due to a transient bug. 160 update_manager->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); 161 waiting_for_scheduled_check_ = true; 162} 163 164bool UpdateAttempter::CheckAndReportDailyMetrics() { 165 int64_t stored_value; 166 Time now = system_state_->clock()->GetWallclockTime(); 167 if (system_state_->prefs()->Exists(kPrefsDailyMetricsLastReportedAt) && 168 system_state_->prefs()->GetInt64(kPrefsDailyMetricsLastReportedAt, 169 &stored_value)) { 170 Time last_reported_at = Time::FromInternalValue(stored_value); 171 TimeDelta time_reported_since = now - last_reported_at; 172 if (time_reported_since.InSeconds() < 0) { 173 LOG(WARNING) << "Last reported daily metrics " 174 << utils::FormatTimeDelta(time_reported_since) << " ago " 175 << "which is negative. Either the system clock is wrong or " 176 << "the kPrefsDailyMetricsLastReportedAt state variable " 177 << "is wrong."; 178 // In this case, report daily metrics to reset. 179 } else { 180 if (time_reported_since.InSeconds() < 24*60*60) { 181 LOG(INFO) << "Last reported daily metrics " 182 << utils::FormatTimeDelta(time_reported_since) << " ago."; 183 return false; 184 } 185 LOG(INFO) << "Last reported daily metrics " 186 << utils::FormatTimeDelta(time_reported_since) << " ago, " 187 << "which is more than 24 hours ago."; 188 } 189 } 190 191 LOG(INFO) << "Reporting daily metrics."; 192 system_state_->prefs()->SetInt64(kPrefsDailyMetricsLastReportedAt, 193 now.ToInternalValue()); 194 195 ReportOSAge(); 196 197 return true; 198} 199 200void UpdateAttempter::ReportOSAge() { 201 struct stat sb; 202 203 if (system_state_ == nullptr) 204 return; 205 206 if (stat("/etc/lsb-release", &sb) != 0) { 207 PLOG(ERROR) << "Error getting file status for /etc/lsb-release " 208 << "(Note: this may happen in some unit tests)"; 209 return; 210 } 211 212 Time lsb_release_timestamp = utils::TimeFromStructTimespec(&sb.st_ctim); 213 Time now = system_state_->clock()->GetWallclockTime(); 214 TimeDelta age = now - lsb_release_timestamp; 215 if (age.InSeconds() < 0) { 216 LOG(ERROR) << "The OS age (" << utils::FormatTimeDelta(age) 217 << ") is negative. Maybe the clock is wrong? " 218 << "(Note: this may happen in some unit tests.)"; 219 return; 220 } 221 222 metrics::ReportDailyMetrics(system_state_, age); 223} 224 225void UpdateAttempter::Update(const string& app_version, 226 const string& omaha_url, 227 const string& target_channel, 228 const string& target_version_prefix, 229 bool obey_proxies, 230 bool interactive) { 231 // This is normally called frequently enough so it's appropriate to use as a 232 // hook for reporting daily metrics. 233 // TODO(garnold) This should be hooked to a separate (reliable and consistent) 234 // timeout event. 235 CheckAndReportDailyMetrics(); 236 237 // Notify of the new update attempt, clearing prior interactive requests. 238 if (forced_update_pending_callback_.get()) 239 forced_update_pending_callback_->Run(false, false); 240 241 chrome_proxy_resolver_.Init(); 242 fake_update_success_ = false; 243 if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { 244 // Although we have applied an update, we still want to ping Omaha 245 // to ensure the number of active statistics is accurate. 246 // 247 // Also convey to the UpdateEngine.Check.Result metric that we're 248 // not performing an update check because of this. 249 LOG(INFO) << "Not updating b/c we already updated and we're waiting for " 250 << "reboot, we'll ping Omaha instead"; 251 metrics::ReportUpdateCheckMetrics(system_state_, 252 metrics::CheckResult::kRebootPending, 253 metrics::CheckReaction::kUnset, 254 metrics::DownloadErrorCode::kUnset); 255 PingOmaha(); 256 return; 257 } 258 if (status_ != UpdateStatus::IDLE) { 259 // Update in progress. Do nothing 260 return; 261 } 262 263 if (!CalculateUpdateParams(app_version, 264 omaha_url, 265 target_channel, 266 target_version_prefix, 267 obey_proxies, 268 interactive)) { 269 return; 270 } 271 272 BuildUpdateActions(interactive); 273 274 SetStatusAndNotify(UpdateStatus::CHECKING_FOR_UPDATE); 275 276 // Update the last check time here; it may be re-updated when an Omaha 277 // response is received, but this will prevent us from repeatedly scheduling 278 // checks in the case where a response is not received. 279 UpdateLastCheckedTime(); 280 281 // Just in case we didn't update boot flags yet, make sure they're updated 282 // before any update processing starts. 283 start_action_processor_ = true; 284 UpdateBootFlags(); 285} 286 287void UpdateAttempter::RefreshDevicePolicy() { 288 // Lazy initialize the policy provider, or reload the latest policy data. 289 if (!policy_provider_.get()) 290 policy_provider_.reset(new policy::PolicyProvider()); 291 policy_provider_->Reload(); 292 293 const policy::DevicePolicy* device_policy = nullptr; 294 if (policy_provider_->device_policy_is_loaded()) 295 device_policy = &policy_provider_->GetDevicePolicy(); 296 297 if (device_policy) 298 LOG(INFO) << "Device policies/settings present"; 299 else 300 LOG(INFO) << "No device policies/settings present."; 301 302 system_state_->set_device_policy(device_policy); 303 system_state_->p2p_manager()->SetDevicePolicy(device_policy); 304} 305 306void UpdateAttempter::CalculateP2PParams(bool interactive) { 307 bool use_p2p_for_downloading = false; 308 bool use_p2p_for_sharing = false; 309 310 // Never use p2p for downloading in interactive checks unless the 311 // developer has opted in for it via a marker file. 312 // 313 // (Why would a developer want to opt in? If he's working on the 314 // update_engine or p2p codebases so he can actually test his 315 // code.). 316 317 if (system_state_ != nullptr) { 318 if (!system_state_->p2p_manager()->IsP2PEnabled()) { 319 LOG(INFO) << "p2p is not enabled - disallowing p2p for both" 320 << " downloading and sharing."; 321 } else { 322 // Allow p2p for sharing, even in interactive checks. 323 use_p2p_for_sharing = true; 324 if (!interactive) { 325 LOG(INFO) << "Non-interactive check - allowing p2p for downloading"; 326 use_p2p_for_downloading = true; 327 } else { 328 LOG(INFO) << "Forcibly disabling use of p2p for downloading " 329 << "since this update attempt is interactive."; 330 } 331 } 332 } 333 334 PayloadStateInterface* const payload_state = system_state_->payload_state(); 335 payload_state->SetUsingP2PForDownloading(use_p2p_for_downloading); 336 payload_state->SetUsingP2PForSharing(use_p2p_for_sharing); 337} 338 339bool UpdateAttempter::CalculateUpdateParams(const string& app_version, 340 const string& omaha_url, 341 const string& target_channel, 342 const string& target_version_prefix, 343 bool obey_proxies, 344 bool interactive) { 345 http_response_code_ = 0; 346 PayloadStateInterface* const payload_state = system_state_->payload_state(); 347 348 // Refresh the policy before computing all the update parameters. 349 RefreshDevicePolicy(); 350 351 // Set the target version prefix, if provided. 352 if (!target_version_prefix.empty()) 353 omaha_request_params_->set_target_version_prefix(target_version_prefix); 354 355 CalculateScatteringParams(interactive); 356 357 CalculateP2PParams(interactive); 358 if (payload_state->GetUsingP2PForDownloading() || 359 payload_state->GetUsingP2PForSharing()) { 360 // OK, p2p is to be used - start it and perform housekeeping. 361 if (!StartP2PAndPerformHousekeeping()) { 362 // If this fails, disable p2p for this attempt 363 LOG(INFO) << "Forcibly disabling use of p2p since starting p2p or " 364 << "performing housekeeping failed."; 365 payload_state->SetUsingP2PForDownloading(false); 366 payload_state->SetUsingP2PForSharing(false); 367 } 368 } 369 370 if (!omaha_request_params_->Init(app_version, 371 omaha_url, 372 interactive)) { 373 LOG(ERROR) << "Unable to initialize Omaha request params."; 374 return false; 375 } 376 377 // Set the target channel, if one was provided. 378 if (target_channel.empty()) { 379 LOG(INFO) << "No target channel mandated by policy."; 380 } else { 381 LOG(INFO) << "Setting target channel as mandated: " << target_channel; 382 // Pass in false for powerwash_allowed until we add it to the policy 383 // protobuf. 384 string error_message; 385 if (!omaha_request_params_->SetTargetChannel(target_channel, false, 386 &error_message)) { 387 LOG(ERROR) << "Setting the channel failed: " << error_message; 388 } 389 390 // Since this is the beginning of a new attempt, update the download 391 // channel. The download channel won't be updated until the next attempt, 392 // even if target channel changes meanwhile, so that how we'll know if we 393 // should cancel the current download attempt if there's such a change in 394 // target channel. 395 omaha_request_params_->UpdateDownloadChannel(); 396 } 397 398 LOG(INFO) << "target_version_prefix = " 399 << omaha_request_params_->target_version_prefix() 400 << ", scatter_factor_in_seconds = " 401 << utils::FormatSecs(scatter_factor_.InSeconds()); 402 403 LOG(INFO) << "Wall Clock Based Wait Enabled = " 404 << omaha_request_params_->wall_clock_based_wait_enabled() 405 << ", Update Check Count Wait Enabled = " 406 << omaha_request_params_->update_check_count_wait_enabled() 407 << ", Waiting Period = " << utils::FormatSecs( 408 omaha_request_params_->waiting_period().InSeconds()); 409 410 LOG(INFO) << "Use p2p For Downloading = " 411 << payload_state->GetUsingP2PForDownloading() 412 << ", Use p2p For Sharing = " 413 << payload_state->GetUsingP2PForSharing(); 414 415 obeying_proxies_ = true; 416 if (obey_proxies || proxy_manual_checks_ == 0) { 417 LOG(INFO) << "forced to obey proxies"; 418 // If forced to obey proxies, every 20th request will not use proxies 419 proxy_manual_checks_++; 420 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_; 421 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) { 422 proxy_manual_checks_ = 0; 423 obeying_proxies_ = false; 424 } 425 } else if (base::RandInt(0, 4) == 0) { 426 obeying_proxies_ = false; 427 } 428 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update " 429 "check we are ignoring the proxy settings and using " 430 "direct connections."; 431 432 DisableDeltaUpdateIfNeeded(); 433 return true; 434} 435 436void UpdateAttempter::CalculateScatteringParams(bool interactive) { 437 // Take a copy of the old scatter value before we update it, as 438 // we need to update the waiting period if this value changes. 439 TimeDelta old_scatter_factor = scatter_factor_; 440 const policy::DevicePolicy* device_policy = system_state_->device_policy(); 441 if (device_policy) { 442 int64_t new_scatter_factor_in_secs = 0; 443 device_policy->GetScatterFactorInSeconds(&new_scatter_factor_in_secs); 444 if (new_scatter_factor_in_secs < 0) // sanitize input, just in case. 445 new_scatter_factor_in_secs = 0; 446 scatter_factor_ = TimeDelta::FromSeconds(new_scatter_factor_in_secs); 447 } 448 449 bool is_scatter_enabled = false; 450 if (scatter_factor_.InSeconds() == 0) { 451 LOG(INFO) << "Scattering disabled since scatter factor is set to 0"; 452 } else if (interactive) { 453 LOG(INFO) << "Scattering disabled as this is an interactive update check"; 454 } else if (!system_state_->hardware()->IsOOBEComplete(nullptr)) { 455 LOG(INFO) << "Scattering disabled since OOBE is not complete yet"; 456 } else { 457 is_scatter_enabled = true; 458 LOG(INFO) << "Scattering is enabled"; 459 } 460 461 if (is_scatter_enabled) { 462 // This means the scattering policy is turned on. 463 // Now check if we need to update the waiting period. The two cases 464 // in which we'd need to update the waiting period are: 465 // 1. First time in process or a scheduled check after a user-initiated one. 466 // (omaha_request_params_->waiting_period will be zero in this case). 467 // 2. Admin has changed the scattering policy value. 468 // (new scattering value will be different from old one in this case). 469 int64_t wait_period_in_secs = 0; 470 if (omaha_request_params_->waiting_period().InSeconds() == 0) { 471 // First case. Check if we have a suitable value to set for 472 // the waiting period. 473 if (prefs_->GetInt64(kPrefsWallClockWaitPeriod, &wait_period_in_secs) && 474 wait_period_in_secs > 0 && 475 wait_period_in_secs <= scatter_factor_.InSeconds()) { 476 // This means: 477 // 1. There's a persisted value for the waiting period available. 478 // 2. And that persisted value is still valid. 479 // So, in this case, we should reuse the persisted value instead of 480 // generating a new random value to improve the chances of a good 481 // distribution for scattering. 482 omaha_request_params_->set_waiting_period( 483 TimeDelta::FromSeconds(wait_period_in_secs)); 484 LOG(INFO) << "Using persisted wall-clock waiting period: " << 485 utils::FormatSecs( 486 omaha_request_params_->waiting_period().InSeconds()); 487 } else { 488 // This means there's no persisted value for the waiting period 489 // available or its value is invalid given the new scatter_factor value. 490 // So, we should go ahead and regenerate a new value for the 491 // waiting period. 492 LOG(INFO) << "Persisted value not present or not valid (" 493 << utils::FormatSecs(wait_period_in_secs) 494 << ") for wall-clock waiting period."; 495 GenerateNewWaitingPeriod(); 496 } 497 } else if (scatter_factor_ != old_scatter_factor) { 498 // This means there's already a waiting period value, but we detected 499 // a change in the scattering policy value. So, we should regenerate the 500 // waiting period to make sure it's within the bounds of the new scatter 501 // factor value. 502 GenerateNewWaitingPeriod(); 503 } else { 504 // Neither the first time scattering is enabled nor the scattering value 505 // changed. Nothing to do. 506 LOG(INFO) << "Keeping current wall-clock waiting period: " << 507 utils::FormatSecs( 508 omaha_request_params_->waiting_period().InSeconds()); 509 } 510 511 // The invariant at this point is that omaha_request_params_->waiting_period 512 // is non-zero no matter which path we took above. 513 LOG_IF(ERROR, omaha_request_params_->waiting_period().InSeconds() == 0) 514 << "Waiting Period should NOT be zero at this point!!!"; 515 516 // Since scattering is enabled, wall clock based wait will always be 517 // enabled. 518 omaha_request_params_->set_wall_clock_based_wait_enabled(true); 519 520 // If we don't have any issues in accessing the file system to update 521 // the update check count value, we'll turn that on as well. 522 bool decrement_succeeded = DecrementUpdateCheckCount(); 523 omaha_request_params_->set_update_check_count_wait_enabled( 524 decrement_succeeded); 525 } else { 526 // This means the scattering feature is turned off or disabled for 527 // this particular update check. Make sure to disable 528 // all the knobs and artifacts so that we don't invoke any scattering 529 // related code. 530 omaha_request_params_->set_wall_clock_based_wait_enabled(false); 531 omaha_request_params_->set_update_check_count_wait_enabled(false); 532 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds(0)); 533 prefs_->Delete(kPrefsWallClockWaitPeriod); 534 prefs_->Delete(kPrefsUpdateCheckCount); 535 // Don't delete the UpdateFirstSeenAt file as we don't want manual checks 536 // that result in no-updates (e.g. due to server side throttling) to 537 // cause update starvation by having the client generate a new 538 // UpdateFirstSeenAt for each scheduled check that follows a manual check. 539 } 540} 541 542void UpdateAttempter::GenerateNewWaitingPeriod() { 543 omaha_request_params_->set_waiting_period(TimeDelta::FromSeconds( 544 base::RandInt(1, scatter_factor_.InSeconds()))); 545 546 LOG(INFO) << "Generated new wall-clock waiting period: " << utils::FormatSecs( 547 omaha_request_params_->waiting_period().InSeconds()); 548 549 // Do a best-effort to persist this in all cases. Even if the persistence 550 // fails, we'll still be able to scatter based on our in-memory value. 551 // The persistence only helps in ensuring a good overall distribution 552 // across multiple devices if they tend to reboot too often. 553 system_state_->payload_state()->SetScatteringWaitPeriod( 554 omaha_request_params_->waiting_period()); 555} 556 557void UpdateAttempter::BuildPostInstallActions( 558 InstallPlanAction* previous_action) { 559 shared_ptr<PostinstallRunnerAction> postinstall_runner_action( 560 new PostinstallRunnerAction(system_state_)); 561 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); 562 BondActions(previous_action, 563 postinstall_runner_action.get()); 564} 565 566void UpdateAttempter::BuildUpdateActions(bool interactive) { 567 CHECK(!processor_->IsRunning()); 568 processor_->set_delegate(this); 569 570 // Actions: 571 LibcurlHttpFetcher* update_check_fetcher = 572 new LibcurlHttpFetcher(GetProxyResolver(), system_state_); 573 // Try harder to connect to the network, esp when not interactive. 574 // See comment in libcurl_http_fetcher.cc. 575 update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3); 576 update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate); 577 shared_ptr<OmahaRequestAction> update_check_action( 578 new OmahaRequestAction(system_state_, 579 nullptr, 580 update_check_fetcher, // passes ownership 581 false)); 582 shared_ptr<OmahaResponseHandlerAction> response_handler_action( 583 new OmahaResponseHandlerAction(system_state_)); 584 shared_ptr<FilesystemVerifierAction> src_filesystem_verifier_action( 585 new FilesystemVerifierAction(system_state_->boot_control(), 586 VerifierMode::kComputeSourceHash)); 587 588 shared_ptr<OmahaRequestAction> download_started_action( 589 new OmahaRequestAction(system_state_, 590 new OmahaEvent( 591 OmahaEvent::kTypeUpdateDownloadStarted), 592 new LibcurlHttpFetcher(GetProxyResolver(), 593 system_state_), 594 false)); 595 LibcurlHttpFetcher* download_fetcher = 596 new LibcurlHttpFetcher(GetProxyResolver(), system_state_); 597 download_fetcher->set_check_certificate(CertificateChecker::kDownload); 598 shared_ptr<DownloadAction> download_action( 599 new DownloadAction(prefs_, 600 system_state_, 601 new MultiRangeHttpFetcher( 602 download_fetcher))); // passes ownership 603 shared_ptr<OmahaRequestAction> download_finished_action( 604 new OmahaRequestAction(system_state_, 605 new OmahaEvent( 606 OmahaEvent::kTypeUpdateDownloadFinished), 607 new LibcurlHttpFetcher(GetProxyResolver(), 608 system_state_), 609 false)); 610 shared_ptr<FilesystemVerifierAction> dst_filesystem_verifier_action( 611 new FilesystemVerifierAction(system_state_->boot_control(), 612 VerifierMode::kVerifyTargetHash)); 613 shared_ptr<OmahaRequestAction> update_complete_action( 614 new OmahaRequestAction(system_state_, 615 new OmahaEvent(OmahaEvent::kTypeUpdateComplete), 616 new LibcurlHttpFetcher(GetProxyResolver(), 617 system_state_), 618 false)); 619 620 download_action->set_delegate(this); 621 response_handler_action_ = response_handler_action; 622 download_action_ = download_action; 623 624 actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); 625 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); 626 actions_.push_back(shared_ptr<AbstractAction>( 627 src_filesystem_verifier_action)); 628 actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); 629 actions_.push_back(shared_ptr<AbstractAction>(download_action)); 630 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); 631 actions_.push_back(shared_ptr<AbstractAction>( 632 dst_filesystem_verifier_action)); 633 634 // Bond them together. We have to use the leaf-types when calling 635 // BondActions(). 636 BondActions(update_check_action.get(), 637 response_handler_action.get()); 638 BondActions(response_handler_action.get(), 639 src_filesystem_verifier_action.get()); 640 BondActions(src_filesystem_verifier_action.get(), 641 download_action.get()); 642 BondActions(download_action.get(), 643 dst_filesystem_verifier_action.get()); 644 BuildPostInstallActions(dst_filesystem_verifier_action.get()); 645 646 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); 647 648 // Enqueue the actions 649 for (const shared_ptr<AbstractAction>& action : actions_) { 650 processor_->EnqueueAction(action.get()); 651 } 652} 653 654bool UpdateAttempter::Rollback(bool powerwash) { 655 if (!CanRollback()) { 656 return false; 657 } 658 659 // Extra check for enterprise-enrolled devices since they don't support 660 // powerwash. 661 if (powerwash) { 662 // Enterprise-enrolled devices have an empty owner in their device policy. 663 string owner; 664 RefreshDevicePolicy(); 665 const policy::DevicePolicy* device_policy = system_state_->device_policy(); 666 if (device_policy && (!device_policy->GetOwner(&owner) || owner.empty())) { 667 LOG(ERROR) << "Enterprise device detected. " 668 << "Cannot perform a powerwash for enterprise devices."; 669 return false; 670 } 671 } 672 673 processor_->set_delegate(this); 674 675 // Initialize the default request params. 676 if (!omaha_request_params_->Init("", "", true)) { 677 LOG(ERROR) << "Unable to initialize Omaha request params."; 678 return false; 679 } 680 681 LOG(INFO) << "Setting rollback options."; 682 InstallPlan install_plan; 683 684 install_plan.target_slot = GetRollbackSlot(); 685 install_plan.source_slot = system_state_->boot_control()->GetCurrentSlot(); 686 687 TEST_AND_RETURN_FALSE(install_plan.LoadPartitionsFromSlots(system_state_)); 688 install_plan.powerwash_required = powerwash; 689 690 LOG(INFO) << "Using this install plan:"; 691 install_plan.Dump(); 692 693 shared_ptr<InstallPlanAction> install_plan_action( 694 new InstallPlanAction(install_plan)); 695 actions_.push_back(shared_ptr<AbstractAction>(install_plan_action)); 696 697 BuildPostInstallActions(install_plan_action.get()); 698 699 // Enqueue the actions 700 for (const shared_ptr<AbstractAction>& action : actions_) { 701 processor_->EnqueueAction(action.get()); 702 } 703 704 // Update the payload state for Rollback. 705 system_state_->payload_state()->Rollback(); 706 707 SetStatusAndNotify(UpdateStatus::ATTEMPTING_ROLLBACK); 708 709 // Just in case we didn't update boot flags yet, make sure they're updated 710 // before any update processing starts. This also schedules the start of the 711 // actions we just posted. 712 start_action_processor_ = true; 713 UpdateBootFlags(); 714 return true; 715} 716 717bool UpdateAttempter::CanRollback() const { 718 // We can only rollback if the update_engine isn't busy and we have a valid 719 // rollback partition. 720 return (status_ == UpdateStatus::IDLE && 721 GetRollbackSlot() != BootControlInterface::kInvalidSlot); 722} 723 724BootControlInterface::Slot UpdateAttempter::GetRollbackSlot() const { 725 LOG(INFO) << "UpdateAttempter::GetRollbackSlot"; 726 const unsigned int num_slots = system_state_->boot_control()->GetNumSlots(); 727 const BootControlInterface::Slot current_slot = 728 system_state_->boot_control()->GetCurrentSlot(); 729 730 LOG(INFO) << " Installed slots: " << num_slots; 731 LOG(INFO) << " Booted from slot: " 732 << BootControlInterface::SlotName(current_slot); 733 734 if (current_slot == BootControlInterface::kInvalidSlot || num_slots < 2) { 735 LOG(INFO) << "Device is not updateable."; 736 return BootControlInterface::kInvalidSlot; 737 } 738 739 vector<BootControlInterface::Slot> bootable_slots; 740 for(BootControlInterface::Slot slot = 0; slot < num_slots; slot++) { 741 if (slot != current_slot && 742 system_state_->boot_control()->IsSlotBootable(slot)) { 743 LOG(INFO) << "Found bootable slot " 744 << BootControlInterface::SlotName(slot); 745 return slot; 746 } 747 } 748 LOG(INFO) << "No other bootable slot found."; 749 return BootControlInterface::kInvalidSlot; 750} 751 752void UpdateAttempter::CheckForUpdate(const string& app_version, 753 const string& omaha_url, 754 bool interactive) { 755 LOG(INFO) << "Forced update check requested."; 756 forced_app_version_.clear(); 757 forced_omaha_url_.clear(); 758 759 // Certain conditions must be met to allow setting custom version and update 760 // server URLs. However, kScheduledAUTestURLRequest and kAUTestURLRequest are 761 // always allowed regardless of device state. 762 if (IsAnyUpdateSourceAllowed()) { 763 forced_app_version_ = app_version; 764 forced_omaha_url_ = omaha_url; 765 } 766 if (omaha_url == kScheduledAUTestURLRequest) { 767 forced_omaha_url_ = constants::kOmahaDefaultAUTestURL; 768 // Pretend that it's not user-initiated even though it is, 769 // so as to test scattering logic, etc. which get kicked off 770 // only in scheduled update checks. 771 interactive = false; 772 } else if (omaha_url == kAUTestURLRequest) { 773 forced_omaha_url_ = constants::kOmahaDefaultAUTestURL; 774 } 775 776 if (forced_update_pending_callback_.get()) { 777 // Make sure that a scheduling request is made prior to calling the forced 778 // update pending callback. 779 ScheduleUpdates(); 780 forced_update_pending_callback_->Run(true, interactive); 781 } 782} 783 784bool UpdateAttempter::RebootIfNeeded() { 785 if (status_ != UpdateStatus::UPDATED_NEED_REBOOT) { 786 LOG(INFO) << "Reboot requested, but status is " 787 << UpdateStatusToString(status_) << ", so not rebooting."; 788 return false; 789 } 790 791 if (USE_POWER_MANAGEMENT && RequestPowerManagerReboot()) 792 return true; 793 794 return RebootDirectly(); 795} 796 797void UpdateAttempter::WriteUpdateCompletedMarker() { 798 string boot_id; 799 if (!utils::GetBootId(&boot_id)) 800 return; 801 prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id); 802 803 int64_t value = system_state_->clock()->GetBootTime().ToInternalValue(); 804 prefs_->SetInt64(kPrefsUpdateCompletedBootTime, value); 805} 806 807bool UpdateAttempter::RequestPowerManagerReboot() { 808 org::chromium::PowerManagerProxyInterface* power_manager_proxy = 809 system_state_->power_manager_proxy(); 810 if (!power_manager_proxy) { 811 LOG(WARNING) << "No PowerManager proxy defined, skipping reboot."; 812 return false; 813 } 814 LOG(INFO) << "Calling " << power_manager::kPowerManagerInterface << "." 815 << power_manager::kRequestRestartMethod; 816 brillo::ErrorPtr error; 817 return power_manager_proxy->RequestRestart( 818 power_manager::REQUEST_RESTART_FOR_UPDATE, &error); 819} 820 821bool UpdateAttempter::RebootDirectly() { 822 vector<string> command; 823 command.push_back("/sbin/shutdown"); 824 command.push_back("-r"); 825 command.push_back("now"); 826 LOG(INFO) << "Running \"" << JoinString(command, ' ') << "\""; 827 int rc = 0; 828 Subprocess::SynchronousExec(command, &rc, nullptr); 829 return rc == 0; 830} 831 832void UpdateAttempter::OnUpdateScheduled(EvalStatus status, 833 const UpdateCheckParams& params) { 834 waiting_for_scheduled_check_ = false; 835 836 if (status == EvalStatus::kSucceeded) { 837 if (!params.updates_enabled) { 838 LOG(WARNING) << "Updates permanently disabled."; 839 // Signal disabled status, then switch right back to idle. This is 840 // necessary for ensuring that observers waiting for a signal change will 841 // actually notice one on subsequent calls. Note that we don't need to 842 // re-schedule a check in this case as updates are permanently disabled; 843 // further (forced) checks may still initiate a scheduling call. 844 SetStatusAndNotify(UpdateStatus::DISABLED); 845 SetStatusAndNotify(UpdateStatus::IDLE); 846 return; 847 } 848 849 LOG(INFO) << "Running " 850 << (params.is_interactive ? "interactive" : "periodic") 851 << " update."; 852 853 string app_version, omaha_url; 854 if (params.is_interactive) { 855 app_version = forced_app_version_; 856 omaha_url = forced_omaha_url_; 857 } else { 858 // Flush previously generated UMA reports before periodic updates. 859 CertificateChecker::FlushReport(); 860 } 861 862 Update(app_version, omaha_url, params.target_channel, 863 params.target_version_prefix, false, params.is_interactive); 864 } else { 865 LOG(WARNING) 866 << "Update check scheduling failed (possibly timed out); retrying."; 867 ScheduleUpdates(); 868 } 869 870 // This check ensures that future update checks will be or are already 871 // scheduled. The check should never fail. A check failure means that there's 872 // a bug that will most likely prevent further automatic update checks. It 873 // seems better to crash in such cases and restart the update_engine daemon 874 // into, hopefully, a known good state. 875 CHECK(IsUpdateRunningOrScheduled()); 876} 877 878void UpdateAttempter::UpdateLastCheckedTime() { 879 last_checked_time_ = system_state_->clock()->GetWallclockTime().ToTimeT(); 880} 881 882// Delegate methods: 883void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, 884 ErrorCode code) { 885 LOG(INFO) << "Processing Done."; 886 actions_.clear(); 887 888 // Reset cpu shares back to normal. 889 CleanupCpuSharesManagement(); 890 891 if (status_ == UpdateStatus::REPORTING_ERROR_EVENT) { 892 LOG(INFO) << "Error event sent."; 893 894 // Inform scheduler of new status; 895 SetStatusAndNotify(UpdateStatus::IDLE); 896 ScheduleUpdates(); 897 898 if (!fake_update_success_) { 899 return; 900 } 901 LOG(INFO) << "Booted from FW B and tried to install new firmware, " 902 "so requesting reboot from user."; 903 } 904 905 if (code == ErrorCode::kSuccess) { 906 WriteUpdateCompletedMarker(); 907 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); 908 prefs_->SetString(kPrefsPreviousVersion, 909 omaha_request_params_->app_version()); 910 DeltaPerformer::ResetUpdateProgress(prefs_, false); 911 912 system_state_->payload_state()->UpdateSucceeded(); 913 914 // Since we're done with scattering fully at this point, this is the 915 // safest point delete the state files, as we're sure that the status is 916 // set to reboot (which means no more updates will be applied until reboot) 917 // This deletion is required for correctness as we want the next update 918 // check to re-create a new random number for the update check count. 919 // Similarly, we also delete the wall-clock-wait period that was persisted 920 // so that we start with a new random value for the next update check 921 // after reboot so that the same device is not favored or punished in any 922 // way. 923 prefs_->Delete(kPrefsUpdateCheckCount); 924 system_state_->payload_state()->SetScatteringWaitPeriod(TimeDelta()); 925 prefs_->Delete(kPrefsUpdateFirstSeenAt); 926 927 SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); 928 ScheduleUpdates(); 929 LOG(INFO) << "Update successfully applied, waiting to reboot."; 930 931 // This pointer is null during rollback operations, and the stats 932 // don't make much sense then anyway. 933 if (response_handler_action_) { 934 const InstallPlan& install_plan = 935 response_handler_action_->install_plan(); 936 937 // Generate an unique payload identifier. 938 const string target_version_uid = 939 install_plan.payload_hash + ":" + install_plan.metadata_signature; 940 941 // Expect to reboot into the new version to send the proper metric during 942 // next boot. 943 system_state_->payload_state()->ExpectRebootInNewVersion( 944 target_version_uid); 945 } else { 946 // If we just finished a rollback, then we expect to have no Omaha 947 // response. Otherwise, it's an error. 948 if (system_state_->payload_state()->GetRollbackVersion().empty()) { 949 LOG(ERROR) << "Can't send metrics because expected " 950 "response_handler_action_ missing."; 951 } 952 } 953 return; 954 } 955 956 if (ScheduleErrorEventAction()) { 957 return; 958 } 959 LOG(INFO) << "No update."; 960 SetStatusAndNotify(UpdateStatus::IDLE); 961 ScheduleUpdates(); 962} 963 964void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { 965 // Reset cpu shares back to normal. 966 CleanupCpuSharesManagement(); 967 download_progress_ = 0.0; 968 SetStatusAndNotify(UpdateStatus::IDLE); 969 ScheduleUpdates(); 970 actions_.clear(); 971 error_event_.reset(nullptr); 972} 973 974// Called whenever an action has finished processing, either successfully 975// or otherwise. 976void UpdateAttempter::ActionCompleted(ActionProcessor* processor, 977 AbstractAction* action, 978 ErrorCode code) { 979 // Reset download progress regardless of whether or not the download 980 // action succeeded. Also, get the response code from HTTP request 981 // actions (update download as well as the initial update check 982 // actions). 983 const string type = action->Type(); 984 if (type == DownloadAction::StaticType()) { 985 download_progress_ = 0.0; 986 DownloadAction* download_action = static_cast<DownloadAction*>(action); 987 http_response_code_ = download_action->GetHTTPResponseCode(); 988 } else if (type == OmahaRequestAction::StaticType()) { 989 OmahaRequestAction* omaha_request_action = 990 static_cast<OmahaRequestAction*>(action); 991 // If the request is not an event, then it's the update-check. 992 if (!omaha_request_action->IsEvent()) { 993 http_response_code_ = omaha_request_action->GetHTTPResponseCode(); 994 995 // Record the number of consecutive failed update checks. 996 if (http_response_code_ == kHttpResponseInternalServerError || 997 http_response_code_ == kHttpResponseServiceUnavailable) { 998 consecutive_failed_update_checks_++; 999 } else { 1000 consecutive_failed_update_checks_ = 0; 1001 } 1002 1003 // Store the server-dictated poll interval, if any. 1004 server_dictated_poll_interval_ = 1005 std::max(0, omaha_request_action->GetOutputObject().poll_interval); 1006 } 1007 } 1008 if (code != ErrorCode::kSuccess) { 1009 // If the current state is at or past the download phase, count the failure 1010 // in case a switch to full update becomes necessary. Ignore network 1011 // transfer timeouts and failures. 1012 if (status_ >= UpdateStatus::DOWNLOADING && 1013 code != ErrorCode::kDownloadTransferError) { 1014 MarkDeltaUpdateFailure(); 1015 } 1016 // On failure, schedule an error event to be sent to Omaha. 1017 CreatePendingErrorEvent(action, code); 1018 return; 1019 } 1020 // Find out which action completed. 1021 if (type == OmahaResponseHandlerAction::StaticType()) { 1022 // Note that the status will be updated to DOWNLOADING when some bytes get 1023 // actually downloaded from the server and the BytesReceived callback is 1024 // invoked. This avoids notifying the user that a download has started in 1025 // cases when the server and the client are unable to initiate the download. 1026 CHECK(action == response_handler_action_.get()); 1027 const InstallPlan& plan = response_handler_action_->install_plan(); 1028 UpdateLastCheckedTime(); 1029 new_version_ = plan.version; 1030 new_payload_size_ = plan.payload_size; 1031 SetupDownload(); 1032 SetupCpuSharesManagement(); 1033 SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); 1034 } else if (type == DownloadAction::StaticType()) { 1035 SetStatusAndNotify(UpdateStatus::FINALIZING); 1036 } 1037} 1038 1039void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { 1040 double progress = static_cast<double>(bytes_received) / 1041 static_cast<double>(total); 1042 // Self throttle based on progress. Also send notifications if 1043 // progress is too slow. 1044 const double kDeltaPercent = 0.01; // 1% 1045 if (status_ != UpdateStatus::DOWNLOADING || 1046 bytes_received == total || 1047 progress - download_progress_ >= kDeltaPercent || 1048 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) { 1049 download_progress_ = progress; 1050 SetStatusAndNotify(UpdateStatus::DOWNLOADING); 1051 } 1052} 1053 1054bool UpdateAttempter::ResetStatus() { 1055 LOG(INFO) << "Attempting to reset state from " 1056 << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; 1057 1058 switch (status_) { 1059 case UpdateStatus::IDLE: 1060 // no-op. 1061 return true; 1062 1063 case UpdateStatus::UPDATED_NEED_REBOOT: { 1064 bool ret_value = true; 1065 status_ = UpdateStatus::IDLE; 1066 1067 // Remove the reboot marker so that if the machine is rebooted 1068 // after resetting to idle state, it doesn't go back to 1069 // UpdateStatus::UPDATED_NEED_REBOOT state. 1070 ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId) && ret_value; 1071 ret_value = prefs_->Delete(kPrefsUpdateCompletedBootTime) && ret_value; 1072 1073 // Update the boot flags so the current slot has higher priority. 1074 BootControlInterface* boot_control = system_state_->boot_control(); 1075 if (!boot_control->SetActiveBootSlot(boot_control->GetCurrentSlot())) 1076 ret_value = false; 1077 1078 // Notify the PayloadState that the successful payload was canceled. 1079 system_state_->payload_state()->ResetUpdateStatus(); 1080 1081 // The previous version is used to report back to omaha after reboot that 1082 // we actually rebooted into the new version from this "prev-version". We 1083 // need to clear out this value now to prevent it being sent on the next 1084 // updatecheck request. 1085 ret_value = prefs_->SetString(kPrefsPreviousVersion, "") && ret_value; 1086 1087 LOG(INFO) << "Reset status " << (ret_value ? "successful" : "failed"); 1088 return ret_value; 1089 } 1090 1091 default: 1092 LOG(ERROR) << "Reset not allowed in this state."; 1093 return false; 1094 } 1095} 1096 1097bool UpdateAttempter::GetStatus(int64_t* last_checked_time, 1098 double* progress, 1099 string* current_operation, 1100 string* new_version, 1101 int64_t* new_payload_size) { 1102 *last_checked_time = last_checked_time_; 1103 *progress = download_progress_; 1104 *current_operation = UpdateStatusToString(status_); 1105 *new_version = new_version_; 1106 *new_payload_size = new_payload_size_; 1107 return true; 1108} 1109 1110void UpdateAttempter::UpdateBootFlags() { 1111 if (update_boot_flags_running_) { 1112 LOG(INFO) << "Update boot flags running, nothing to do."; 1113 return; 1114 } 1115 if (updated_boot_flags_) { 1116 LOG(INFO) << "Already updated boot flags. Skipping."; 1117 if (start_action_processor_) { 1118 ScheduleProcessingStart(); 1119 } 1120 return; 1121 } 1122 // This is purely best effort. Failures should be logged by Subprocess. Run 1123 // the script asynchronously to avoid blocking the event loop regardless of 1124 // the script runtime. 1125 update_boot_flags_running_ = true; 1126 LOG(INFO) << "Marking booted slot as good."; 1127 if (!system_state_->boot_control()->MarkBootSuccessfulAsync(Bind( 1128 &UpdateAttempter::CompleteUpdateBootFlags, base::Unretained(this)))) { 1129 LOG(ERROR) << "Failed to mark current boot as successful."; 1130 CompleteUpdateBootFlags(false); 1131 } 1132} 1133 1134void UpdateAttempter::CompleteUpdateBootFlags(bool successful) { 1135 update_boot_flags_running_ = false; 1136 updated_boot_flags_ = true; 1137 if (start_action_processor_) { 1138 ScheduleProcessingStart(); 1139 } 1140} 1141 1142void UpdateAttempter::BroadcastStatus() { 1143 if (!dbus_adaptor_) 1144 return; 1145 last_notify_time_ = TimeTicks::Now(); 1146 dbus_adaptor_->SendStatusUpdateSignal( 1147 last_checked_time_, 1148 download_progress_, 1149 UpdateStatusToString(status_), 1150 new_version_.c_str(), 1151 new_payload_size_); 1152} 1153 1154uint32_t UpdateAttempter::GetErrorCodeFlags() { 1155 uint32_t flags = 0; 1156 1157 if (!system_state_->hardware()->IsNormalBootMode()) 1158 flags |= static_cast<uint32_t>(ErrorCode::kDevModeFlag); 1159 1160 if (response_handler_action_.get() && 1161 response_handler_action_->install_plan().is_resume) 1162 flags |= static_cast<uint32_t>(ErrorCode::kResumedFlag); 1163 1164 if (!system_state_->hardware()->IsOfficialBuild()) 1165 flags |= static_cast<uint32_t>(ErrorCode::kTestImageFlag); 1166 1167 if (omaha_request_params_->update_url() != 1168 constants::kOmahaDefaultProductionURL) { 1169 flags |= static_cast<uint32_t>(ErrorCode::kTestOmahaUrlFlag); 1170 } 1171 1172 return flags; 1173} 1174 1175bool UpdateAttempter::ShouldCancel(ErrorCode* cancel_reason) { 1176 // Check if the channel we're attempting to update to is the same as the 1177 // target channel currently chosen by the user. 1178 OmahaRequestParams* params = system_state_->request_params(); 1179 if (params->download_channel() != params->target_channel()) { 1180 LOG(ERROR) << "Aborting download as target channel: " 1181 << params->target_channel() 1182 << " is different from the download channel: " 1183 << params->download_channel(); 1184 *cancel_reason = ErrorCode::kUpdateCanceledByChannelChange; 1185 return true; 1186 } 1187 1188 return false; 1189} 1190 1191void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { 1192 status_ = status; 1193 BroadcastStatus(); 1194} 1195 1196void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, 1197 ErrorCode code) { 1198 if (error_event_.get()) { 1199 // This shouldn't really happen. 1200 LOG(WARNING) << "There's already an existing pending error event."; 1201 return; 1202 } 1203 1204 // For now assume that a generic Omaha response action failure means that 1205 // there's no update so don't send an event. Also, double check that the 1206 // failure has not occurred while sending an error event -- in which case 1207 // don't schedule another. This shouldn't really happen but just in case... 1208 if ((action->Type() == OmahaResponseHandlerAction::StaticType() && 1209 code == ErrorCode::kError) || 1210 status_ == UpdateStatus::REPORTING_ERROR_EVENT) { 1211 return; 1212 } 1213 1214 // Classify the code to generate the appropriate result so that 1215 // the Borgmon charts show up the results correctly. 1216 // Do this before calling GetErrorCodeForAction which could potentially 1217 // augment the bit representation of code and thus cause no matches for 1218 // the switch cases below. 1219 OmahaEvent::Result event_result; 1220 switch (code) { 1221 case ErrorCode::kOmahaUpdateIgnoredPerPolicy: 1222 case ErrorCode::kOmahaUpdateDeferredPerPolicy: 1223 case ErrorCode::kOmahaUpdateDeferredForBackoff: 1224 event_result = OmahaEvent::kResultUpdateDeferred; 1225 break; 1226 default: 1227 event_result = OmahaEvent::kResultError; 1228 break; 1229 } 1230 1231 code = GetErrorCodeForAction(action, code); 1232 fake_update_success_ = code == ErrorCode::kPostinstallBootedFromFirmwareB; 1233 1234 // Compute the final error code with all the bit flags to be sent to Omaha. 1235 code = static_cast<ErrorCode>( 1236 static_cast<uint32_t>(code) | GetErrorCodeFlags()); 1237 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, 1238 event_result, 1239 code)); 1240} 1241 1242bool UpdateAttempter::ScheduleErrorEventAction() { 1243 if (error_event_.get() == nullptr) 1244 return false; 1245 1246 LOG(ERROR) << "Update failed."; 1247 system_state_->payload_state()->UpdateFailed(error_event_->error_code); 1248 1249 // Send it to Omaha. 1250 LOG(INFO) << "Reporting the error event"; 1251 shared_ptr<OmahaRequestAction> error_event_action( 1252 new OmahaRequestAction(system_state_, 1253 error_event_.release(), // Pass ownership. 1254 new LibcurlHttpFetcher(GetProxyResolver(), 1255 system_state_), 1256 false)); 1257 actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); 1258 processor_->EnqueueAction(error_event_action.get()); 1259 SetStatusAndNotify(UpdateStatus::REPORTING_ERROR_EVENT); 1260 processor_->StartProcessing(); 1261 return true; 1262} 1263 1264void UpdateAttempter::SetCpuShares(utils::CpuShares shares) { 1265 if (shares_ == shares) { 1266 return; 1267 } 1268 if (utils::SetCpuShares(shares)) { 1269 shares_ = shares; 1270 LOG(INFO) << "CPU shares = " << shares_; 1271 } 1272} 1273 1274void UpdateAttempter::SetupCpuSharesManagement() { 1275 if (manage_shares_id_ != MessageLoop::kTaskIdNull) { 1276 LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed."; 1277 CleanupCpuSharesManagement(); 1278 } 1279 const int kCpuSharesTimeout = 2 * 60 * 60; // 2 hours 1280 manage_shares_id_ = MessageLoop::current()->PostDelayedTask( 1281 FROM_HERE, 1282 Bind(&UpdateAttempter::ManageCpuSharesCallback, base::Unretained(this)), 1283 TimeDelta::FromSeconds(kCpuSharesTimeout)); 1284 SetCpuShares(utils::kCpuSharesLow); 1285} 1286 1287void UpdateAttempter::CleanupCpuSharesManagement() { 1288 if (manage_shares_id_ != MessageLoop::kTaskIdNull) { 1289 // The UpdateAttempter is instantiated by default by the FakeSystemState, 1290 // even when it is not used. We check the manage_shares_id_ before calling 1291 // the MessageLoop::current() since the unit test using a FakeSystemState 1292 // may have not define a MessageLoop for the current thread. 1293 MessageLoop::current()->CancelTask(manage_shares_id_); 1294 manage_shares_id_ = MessageLoop::kTaskIdNull; 1295 } 1296 SetCpuShares(utils::kCpuSharesNormal); 1297} 1298 1299void UpdateAttempter::ScheduleProcessingStart() { 1300 LOG(INFO) << "Scheduling an action processor start."; 1301 start_action_processor_ = false; 1302 MessageLoop::current()->PostTask( 1303 FROM_HERE, 1304 Bind([this] { this->processor_->StartProcessing(); })); 1305} 1306 1307void UpdateAttempter::ManageCpuSharesCallback() { 1308 SetCpuShares(utils::kCpuSharesNormal); 1309 manage_shares_id_ = MessageLoop::kTaskIdNull; 1310} 1311 1312void UpdateAttempter::DisableDeltaUpdateIfNeeded() { 1313 int64_t delta_failures; 1314 if (omaha_request_params_->delta_okay() && 1315 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) && 1316 delta_failures >= kMaxDeltaUpdateFailures) { 1317 LOG(WARNING) << "Too many delta update failures, forcing full update."; 1318 omaha_request_params_->set_delta_okay(false); 1319 } 1320} 1321 1322void UpdateAttempter::MarkDeltaUpdateFailure() { 1323 // Don't try to resume a failed delta update. 1324 DeltaPerformer::ResetUpdateProgress(prefs_, false); 1325 int64_t delta_failures; 1326 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) || 1327 delta_failures < 0) { 1328 delta_failures = 0; 1329 } 1330 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures); 1331} 1332 1333void UpdateAttempter::SetupDownload() { 1334 MultiRangeHttpFetcher* fetcher = 1335 static_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher()); 1336 fetcher->ClearRanges(); 1337 if (response_handler_action_->install_plan().is_resume) { 1338 // Resuming an update so fetch the update manifest metadata first. 1339 int64_t manifest_metadata_size = 0; 1340 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size); 1341 fetcher->AddRange(0, manifest_metadata_size); 1342 // If there're remaining unprocessed data blobs, fetch them. Be careful not 1343 // to request data beyond the end of the payload to avoid 416 HTTP response 1344 // error codes. 1345 int64_t next_data_offset = 0; 1346 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset); 1347 uint64_t resume_offset = manifest_metadata_size + next_data_offset; 1348 if (resume_offset < response_handler_action_->install_plan().payload_size) { 1349 fetcher->AddRange(resume_offset); 1350 } 1351 } else { 1352 fetcher->AddRange(0); 1353 } 1354} 1355 1356void UpdateAttempter::PingOmaha() { 1357 if (!processor_->IsRunning()) { 1358 shared_ptr<OmahaRequestAction> ping_action( 1359 new OmahaRequestAction(system_state_, 1360 nullptr, 1361 new LibcurlHttpFetcher(GetProxyResolver(), 1362 system_state_), 1363 true)); 1364 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action)); 1365 processor_->set_delegate(nullptr); 1366 processor_->EnqueueAction(ping_action.get()); 1367 // Call StartProcessing() synchronously here to avoid any race conditions 1368 // caused by multiple outstanding ping Omaha requests. If we call 1369 // StartProcessing() asynchronously, the device can be suspended before we 1370 // get a chance to callback to StartProcessing(). When the device resumes 1371 // (assuming the device sleeps longer than the next update check period), 1372 // StartProcessing() is called back and at the same time, the next update 1373 // check is fired which eventually invokes StartProcessing(). A crash 1374 // can occur because StartProcessing() checks to make sure that the 1375 // processor is idle which it isn't due to the two concurrent ping Omaha 1376 // requests. 1377 processor_->StartProcessing(); 1378 } else { 1379 LOG(WARNING) << "Action processor running, Omaha ping suppressed."; 1380 } 1381 1382 // Update the last check time here; it may be re-updated when an Omaha 1383 // response is received, but this will prevent us from repeatedly scheduling 1384 // checks in the case where a response is not received. 1385 UpdateLastCheckedTime(); 1386 1387 // Update the status which will schedule the next update check 1388 SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); 1389 ScheduleUpdates(); 1390} 1391 1392 1393bool UpdateAttempter::DecrementUpdateCheckCount() { 1394 int64_t update_check_count_value; 1395 1396 if (!prefs_->Exists(kPrefsUpdateCheckCount)) { 1397 // This file does not exist. This means we haven't started our update 1398 // check count down yet, so nothing more to do. This file will be created 1399 // later when we first satisfy the wall-clock-based-wait period. 1400 LOG(INFO) << "No existing update check count. That's normal."; 1401 return true; 1402 } 1403 1404 if (prefs_->GetInt64(kPrefsUpdateCheckCount, &update_check_count_value)) { 1405 // Only if we're able to read a proper integer value, then go ahead 1406 // and decrement and write back the result in the same file, if needed. 1407 LOG(INFO) << "Update check count = " << update_check_count_value; 1408 1409 if (update_check_count_value == 0) { 1410 // It could be 0, if, for some reason, the file didn't get deleted 1411 // when we set our status to waiting for reboot. so we just leave it 1412 // as is so that we can prevent another update_check wait for this client. 1413 LOG(INFO) << "Not decrementing update check count as it's already 0."; 1414 return true; 1415 } 1416 1417 if (update_check_count_value > 0) 1418 update_check_count_value--; 1419 else 1420 update_check_count_value = 0; 1421 1422 // Write out the new value of update_check_count_value. 1423 if (prefs_->SetInt64(kPrefsUpdateCheckCount, update_check_count_value)) { 1424 // We successfully wrote out te new value, so enable the 1425 // update check based wait. 1426 LOG(INFO) << "New update check count = " << update_check_count_value; 1427 return true; 1428 } 1429 } 1430 1431 LOG(INFO) << "Deleting update check count state due to read/write errors."; 1432 1433 // We cannot read/write to the file, so disable the update check based wait 1434 // so that we don't get stuck in this OS version by any chance (which could 1435 // happen if there's some bug that causes to read/write incorrectly). 1436 // Also attempt to delete the file to do our best effort to cleanup. 1437 prefs_->Delete(kPrefsUpdateCheckCount); 1438 return false; 1439} 1440 1441 1442void UpdateAttempter::UpdateEngineStarted() { 1443 // If we just booted into a new update, keep the previous OS version 1444 // in case we rebooted because of a crash of the old version, so we 1445 // can do a proper crash report with correct information. 1446 // This must be done before calling 1447 // system_state_->payload_state()->UpdateEngineStarted() since it will 1448 // delete SystemUpdated marker file. 1449 if (system_state_->system_rebooted() && 1450 prefs_->Exists(kPrefsSystemUpdatedMarker)) { 1451 if (!prefs_->GetString(kPrefsPreviousVersion, &prev_version_)) { 1452 // If we fail to get the version string, make sure it stays empty. 1453 prev_version_.clear(); 1454 } 1455 } 1456 1457 system_state_->payload_state()->UpdateEngineStarted(); 1458 StartP2PAtStartup(); 1459} 1460 1461bool UpdateAttempter::StartP2PAtStartup() { 1462 if (system_state_ == nullptr || 1463 !system_state_->p2p_manager()->IsP2PEnabled()) { 1464 LOG(INFO) << "Not starting p2p at startup since it's not enabled."; 1465 return false; 1466 } 1467 1468 if (system_state_->p2p_manager()->CountSharedFiles() < 1) { 1469 LOG(INFO) << "Not starting p2p at startup since our application " 1470 << "is not sharing any files."; 1471 return false; 1472 } 1473 1474 return StartP2PAndPerformHousekeeping(); 1475} 1476 1477bool UpdateAttempter::StartP2PAndPerformHousekeeping() { 1478 if (system_state_ == nullptr) 1479 return false; 1480 1481 if (!system_state_->p2p_manager()->IsP2PEnabled()) { 1482 LOG(INFO) << "Not starting p2p since it's not enabled."; 1483 return false; 1484 } 1485 1486 LOG(INFO) << "Ensuring that p2p is running."; 1487 if (!system_state_->p2p_manager()->EnsureP2PRunning()) { 1488 LOG(ERROR) << "Error starting p2p."; 1489 return false; 1490 } 1491 1492 LOG(INFO) << "Performing p2p housekeeping."; 1493 if (!system_state_->p2p_manager()->PerformHousekeeping()) { 1494 LOG(ERROR) << "Error performing housekeeping for p2p."; 1495 return false; 1496 } 1497 1498 LOG(INFO) << "Done performing p2p housekeeping."; 1499 return true; 1500} 1501 1502bool UpdateAttempter::GetBootTimeAtUpdate(Time *out_boot_time) { 1503 // In case of an update_engine restart without a reboot, we stored the boot_id 1504 // when the update was completed by setting a pref, so we can check whether 1505 // the last update was on this boot or a previous one. 1506 string boot_id; 1507 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); 1508 1509 string update_completed_on_boot_id; 1510 if (!prefs_->Exists(kPrefsUpdateCompletedOnBootId) || 1511 !prefs_->GetString(kPrefsUpdateCompletedOnBootId, 1512 &update_completed_on_boot_id) || 1513 update_completed_on_boot_id != boot_id) 1514 return false; 1515 1516 // Short-circuit avoiding the read in case out_boot_time is nullptr. 1517 if (out_boot_time) { 1518 int64_t boot_time = 0; 1519 // Since the kPrefsUpdateCompletedOnBootId was correctly set, this pref 1520 // should not fail. 1521 TEST_AND_RETURN_FALSE( 1522 prefs_->GetInt64(kPrefsUpdateCompletedBootTime, &boot_time)); 1523 *out_boot_time = Time::FromInternalValue(boot_time); 1524 } 1525 return true; 1526} 1527 1528bool UpdateAttempter::IsUpdateRunningOrScheduled() { 1529 return ((status_ != UpdateStatus::IDLE && 1530 status_ != UpdateStatus::UPDATED_NEED_REBOOT) || 1531 waiting_for_scheduled_check_); 1532} 1533 1534bool UpdateAttempter::IsAnyUpdateSourceAllowed() { 1535 // We allow updates from any source if either of these are true: 1536 // * The device is running an unofficial (dev/test) image. 1537 // * The debugd dev features are accessible (i.e. in devmode with no owner). 1538 // This protects users running a base image, while still allowing a specific 1539 // window (gated by the debug dev features) where `cros flash` is usable. 1540 if (!system_state_->hardware()->IsOfficialBuild()) { 1541 LOG(INFO) << "Non-official build; allowing any update source."; 1542 return true; 1543 } 1544 1545 // Even though the debugd tools are also gated on devmode, checking here can 1546 // save us a D-Bus call so it's worth doing explicitly. 1547 if (system_state_->hardware()->IsNormalBootMode()) { 1548 LOG(INFO) << "Not in devmode; disallowing custom update sources."; 1549 return false; 1550 } 1551 1552 // Official images in devmode are allowed a custom update source iff the 1553 // debugd dev tools are enabled. 1554 if (!debugd_proxy_) 1555 return false; 1556 int32_t dev_features = debugd::DEV_FEATURES_DISABLED; 1557 brillo::ErrorPtr error; 1558 bool success = debugd_proxy_->QueryDevFeatures(&dev_features, &error); 1559 1560 // Some boards may not include debugd so it's expected that this may fail, 1561 // in which case we default to disallowing custom update sources. 1562 if (success && !(dev_features & debugd::DEV_FEATURES_DISABLED)) { 1563 LOG(INFO) << "Debugd dev tools enabled; allowing any update source."; 1564 return true; 1565 } 1566 LOG(INFO) << "Debugd dev tools disabled; disallowing custom update sources."; 1567 return false; 1568} 1569 1570} // namespace chromeos_update_engine 1571