Home | History | Annotate | Download | only in update_engine
      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/real_system_state.h"
     18 
     19 #include <string>
     20 
     21 #include <base/bind.h>
     22 #include <base/files/file_util.h>
     23 #include <base/location.h>
     24 #include <base/time/time.h>
     25 #include <brillo/make_unique_ptr.h>
     26 #include <brillo/message_loops/message_loop.h>
     27 
     28 #include "update_engine/common/boot_control.h"
     29 #include "update_engine/common/boot_control_stub.h"
     30 #include "update_engine/common/constants.h"
     31 #include "update_engine/common/hardware.h"
     32 #include "update_engine/common/utils.h"
     33 #include "update_engine/update_manager/state_factory.h"
     34 #include "update_engine/weave_service_factory.h"
     35 
     36 using brillo::MessageLoop;
     37 
     38 namespace chromeos_update_engine {
     39 
     40 RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
     41     : debugd_proxy_(bus),
     42       power_manager_proxy_(bus),
     43       session_manager_proxy_(bus),
     44       shill_proxy_(bus),
     45       libcros_proxy_(bus) {
     46 }
     47 
     48 RealSystemState::~RealSystemState() {
     49   // Prevent any DBus communication from UpdateAttempter when shutting down the
     50   // daemon.
     51   if (update_attempter_)
     52     update_attempter_->ClearObservers();
     53 }
     54 
     55 bool RealSystemState::Initialize() {
     56   metrics_lib_.Init();
     57 
     58   boot_control_ = boot_control::CreateBootControl();
     59   if (!boot_control_) {
     60     LOG(WARNING) << "Unable to create BootControl instance, using stub "
     61                  << "instead. All update attempts will fail.";
     62     boot_control_ = brillo::make_unique_ptr(new BootControlStub());
     63   }
     64 
     65   hardware_ = hardware::CreateHardware();
     66   if (!hardware_) {
     67     LOG(ERROR) << "Error intializing the HardwareInterface.";
     68     return false;
     69   }
     70 
     71   LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
     72   LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
     73 
     74   if (!shill_proxy_.Init()) {
     75     LOG(ERROR) << "Failed to initialize shill proxy.";
     76     return false;
     77   }
     78 
     79   // Initialize standard and powerwash-safe prefs.
     80   base::FilePath non_volatile_path;
     81   // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
     82   // available.
     83   if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
     84     LOG(ERROR) << "Failed to get a non-volatile directory.";
     85     return false;
     86   }
     87   Prefs* prefs;
     88   prefs_.reset(prefs = new Prefs());
     89   if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
     90     LOG(ERROR) << "Failed to initialize preferences.";
     91     return false;
     92   }
     93 
     94   base::FilePath powerwash_safe_path;
     95   if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
     96     // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
     97     // directory, or disable powerwash feature.
     98     powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
     99     LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
    100   }
    101   powerwash_safe_prefs_.reset(prefs = new Prefs());
    102   if (!prefs->Init(
    103           powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
    104     LOG(ERROR) << "Failed to initialize powerwash preferences.";
    105     return false;
    106   }
    107 
    108   // Check the system rebooted marker file.
    109   std::string boot_id;
    110   if (utils::GetBootId(&boot_id)) {
    111     std::string prev_boot_id;
    112     system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
    113                         prev_boot_id != boot_id);
    114     prefs_->SetString(kPrefsBootId, boot_id);
    115   } else {
    116     LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
    117     system_rebooted_ = true;
    118   }
    119 
    120   // Initialize the OmahaRequestParams with the default settings. These settings
    121   // will be re-initialized before every request using the actual request
    122   // options. This initialization here pre-loads current channel and version, so
    123   // the DBus service can access it.
    124   if (!request_params_.Init("", "", false)) {
    125     LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
    126                     "features might not work properly.";
    127   }
    128 
    129   certificate_checker_.reset(
    130       new CertificateChecker(prefs_.get(), &openssl_wrapper_));
    131   certificate_checker_->Init();
    132 
    133   // Initialize the UpdateAttempter before the UpdateManager.
    134   update_attempter_.reset(
    135       new UpdateAttempter(this, certificate_checker_.get(), &libcros_proxy_,
    136                           &debugd_proxy_));
    137   update_attempter_->Init();
    138 
    139   weave_service_ = ConstructWeaveService(update_attempter_.get());
    140   if (weave_service_)
    141     update_attempter_->AddObserver(weave_service_.get());
    142 
    143   // Initialize the Update Manager using the default state factory.
    144   chromeos_update_manager::State* um_state =
    145       chromeos_update_manager::DefaultStateFactory(
    146           &policy_provider_, &shill_proxy_, &session_manager_proxy_, this);
    147   if (!um_state) {
    148     LOG(ERROR) << "Failed to initialize the Update Manager.";
    149     return false;
    150   }
    151   update_manager_.reset(
    152       new chromeos_update_manager::UpdateManager(
    153           &clock_, base::TimeDelta::FromSeconds(5),
    154           base::TimeDelta::FromHours(12), um_state));
    155 
    156   // The P2P Manager depends on the Update Manager for its initialization.
    157   p2p_manager_.reset(P2PManager::Construct(
    158           nullptr, &clock_, update_manager_.get(), "cros_au",
    159           kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
    160 
    161   if (!payload_state_.Initialize(this)) {
    162     LOG(ERROR) << "Failed to initialize the payload state object.";
    163     return false;
    164   }
    165 
    166   // All is well. Initialization successful.
    167   return true;
    168 }
    169 
    170 bool RealSystemState::StartUpdater() {
    171   // Initiate update checks.
    172   update_attempter_->ScheduleUpdates();
    173 
    174   // Update boot flags after 45 seconds.
    175   MessageLoop::current()->PostDelayedTask(
    176       FROM_HERE,
    177       base::Bind(&UpdateAttempter::UpdateBootFlags,
    178                  base::Unretained(update_attempter_.get())),
    179       base::TimeDelta::FromSeconds(45));
    180 
    181   // Broadcast the update engine status on startup to ensure consistent system
    182   // state on crashes.
    183   MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
    184       &UpdateAttempter::BroadcastStatus,
    185       base::Unretained(update_attempter_.get())));
    186 
    187   // Run the UpdateEngineStarted() method on |update_attempter|.
    188   MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
    189       &UpdateAttempter::UpdateEngineStarted,
    190       base::Unretained(update_attempter_.get())));
    191   return true;
    192 }
    193 
    194 void RealSystemState::AddObserver(ServiceObserverInterface* observer) {
    195   CHECK(update_attempter_.get());
    196   update_attempter_->AddObserver(observer);
    197 }
    198 
    199 void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) {
    200   CHECK(update_attempter_.get());
    201   update_attempter_->RemoveObserver(observer);
    202 }
    203 
    204 }  // namespace chromeos_update_engine
    205