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