real_system_state.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #include "update_engine/real_system_state.h"
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #include <base/bind.h>
  21. #include <base/files/file_util.h>
  22. #include <base/location.h>
  23. #include <base/time/time.h>
  24. #include <brillo/message_loops/message_loop.h>
  25. #if USE_CHROME_KIOSK_APP
  26. #include <chromeos/dbus/service_constants.h>
  27. #endif // USE_CHROME_KIOSK_APP
  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/dlcservice.h"
  32. #include "update_engine/common/hardware.h"
  33. #include "update_engine/common/utils.h"
  34. #include "update_engine/metrics_reporter_omaha.h"
  35. #include "update_engine/update_boot_flags_action.h"
  36. #if USE_DBUS
  37. #include "update_engine/dbus_connection.h"
  38. #endif // USE_DBUS
  39. #include "update_engine/update_manager/state_factory.h"
  40. using brillo::MessageLoop;
  41. namespace chromeos_update_engine {
  42. RealSystemState::~RealSystemState() {
  43. // Prevent any DBus communication from UpdateAttempter when shutting down the
  44. // daemon.
  45. if (update_attempter_)
  46. update_attempter_->ClearObservers();
  47. }
  48. bool RealSystemState::Initialize() {
  49. metrics_reporter_.Initialize();
  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_ = std::make_unique<BootControlStub>();
  55. }
  56. hardware_ = hardware::CreateHardware();
  57. if (!hardware_) {
  58. LOG(ERROR) << "Error initializing the HardwareInterface.";
  59. return false;
  60. }
  61. #if USE_CHROME_KIOSK_APP
  62. kiosk_app_proxy_.reset(new org::chromium::KioskAppServiceInterfaceProxy(
  63. DBusConnection::Get()->GetDBus(), chromeos::kKioskAppServiceName));
  64. #endif // USE_CHROME_KIOSK_APP
  65. LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
  66. LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
  67. connection_manager_ = connection_manager::CreateConnectionManager(this);
  68. if (!connection_manager_) {
  69. LOG(ERROR) << "Error initializing the ConnectionManagerInterface.";
  70. return false;
  71. }
  72. power_manager_ = power_manager::CreatePowerManager();
  73. if (!power_manager_) {
  74. LOG(ERROR) << "Error initializing the PowerManagerInterface.";
  75. return false;
  76. }
  77. dlcservice_ = CreateDlcService();
  78. if (!dlcservice_) {
  79. LOG(ERROR) << "Error initializing the DlcServiceInterface.";
  80. return false;
  81. }
  82. // Initialize standard and powerwash-safe prefs.
  83. base::FilePath non_volatile_path;
  84. // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
  85. // available.
  86. if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
  87. LOG(ERROR) << "Failed to get a non-volatile directory.";
  88. return false;
  89. }
  90. Prefs* prefs;
  91. prefs_.reset(prefs = new Prefs());
  92. if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
  93. LOG(ERROR) << "Failed to initialize preferences.";
  94. return false;
  95. }
  96. base::FilePath powerwash_safe_path;
  97. if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
  98. // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
  99. // directory, or disable powerwash feature.
  100. powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
  101. LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
  102. }
  103. powerwash_safe_prefs_.reset(prefs = new Prefs());
  104. if (!prefs->Init(
  105. powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
  106. LOG(ERROR) << "Failed to initialize powerwash preferences.";
  107. return false;
  108. }
  109. // Check the system rebooted marker file.
  110. std::string boot_id;
  111. if (utils::GetBootId(&boot_id)) {
  112. std::string prev_boot_id;
  113. system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
  114. prev_boot_id != boot_id);
  115. prefs_->SetString(kPrefsBootId, boot_id);
  116. } else {
  117. LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
  118. system_rebooted_ = true;
  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. certificate_checker_.reset(
  129. new CertificateChecker(prefs_.get(), &openssl_wrapper_));
  130. certificate_checker_->Init();
  131. update_attempter_.reset(
  132. new UpdateAttempter(this, certificate_checker_.get()));
  133. // Initialize the UpdateAttempter before the UpdateManager.
  134. update_attempter_->Init();
  135. // Initialize the Update Manager using the default state factory.
  136. chromeos_update_manager::State* um_state =
  137. chromeos_update_manager::DefaultStateFactory(&policy_provider_,
  138. #if USE_CHROME_KIOSK_APP
  139. kiosk_app_proxy_.get(),
  140. #else
  141. nullptr,
  142. #endif // USE_CHROME_KIOSK_APP
  143. this);
  144. if (!um_state) {
  145. LOG(ERROR) << "Failed to initialize the Update Manager.";
  146. return false;
  147. }
  148. update_manager_.reset(new chromeos_update_manager::UpdateManager(
  149. &clock_,
  150. base::TimeDelta::FromSeconds(5),
  151. base::TimeDelta::FromHours(12),
  152. um_state));
  153. // The P2P Manager depends on the Update Manager for its initialization.
  154. p2p_manager_.reset(
  155. P2PManager::Construct(nullptr,
  156. &clock_,
  157. update_manager_.get(),
  158. "cros_au",
  159. kMaxP2PFilesToKeep,
  160. base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
  161. if (!payload_state_.Initialize(this)) {
  162. LOG(ERROR) << "Failed to initialize the payload state object.";
  163. return false;
  164. }
  165. // For devices that are not rollback enabled (ie. consumer devices),
  166. // initialize max kernel key version to 0xfffffffe, which is logically
  167. // infinity.
  168. if (policy_provider_.IsConsumerDevice()) {
  169. if (!hardware()->SetMaxKernelKeyRollforward(
  170. chromeos_update_manager::kRollforwardInfinity)) {
  171. LOG(ERROR) << "Failed to set kernel_max_rollforward to infinity for"
  172. << " consumer devices";
  173. }
  174. }
  175. // All is well. Initialization successful.
  176. return true;
  177. }
  178. bool RealSystemState::StartUpdater() {
  179. // Initiate update checks.
  180. update_attempter_->ScheduleUpdates();
  181. auto update_boot_flags_action =
  182. std::make_unique<UpdateBootFlagsAction>(boot_control_.get());
  183. processor_.EnqueueAction(std::move(update_boot_flags_action));
  184. // Update boot flags after 45 seconds.
  185. MessageLoop::current()->PostDelayedTask(
  186. FROM_HERE,
  187. base::Bind(&ActionProcessor::StartProcessing,
  188. base::Unretained(&processor_)),
  189. base::TimeDelta::FromSeconds(45));
  190. // Broadcast the update engine status on startup to ensure consistent system
  191. // state on crashes.
  192. MessageLoop::current()->PostTask(
  193. FROM_HERE,
  194. base::Bind(&UpdateAttempter::BroadcastStatus,
  195. base::Unretained(update_attempter_.get())));
  196. // Run the UpdateEngineStarted() method on |update_attempter|.
  197. MessageLoop::current()->PostTask(
  198. FROM_HERE,
  199. base::Bind(&UpdateAttempter::UpdateEngineStarted,
  200. base::Unretained(update_attempter_.get())));
  201. return true;
  202. }
  203. void RealSystemState::AddObserver(ServiceObserverInterface* observer) {
  204. CHECK(update_attempter_.get());
  205. update_attempter_->AddObserver(observer);
  206. }
  207. void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) {
  208. CHECK(update_attempter_.get());
  209. update_attempter_->RemoveObserver(observer);
  210. }
  211. } // namespace chromeos_update_engine