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