blob: 906ec97255118ae865fd52792ffbe174e493bb79 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Jay Srinivasan43488792012-06-19 00:25:31 -070016
Alex Deymo94c06162014-03-21 20:34:46 -070017#include "update_engine/real_system_state.h"
18
Alex Deymof7ead812015-10-23 17:37:27 -070019#include <string>
20
Ben Chan06c76a42014-09-05 08:21:06 -070021#include <base/files/file_util.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070022#include <base/time/time.h>
David Zeuthen6eddf262015-10-16 15:23:53 -040023#include <brillo/make_unique_ptr.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/boot_control.h"
26#include "update_engine/common/boot_control_stub.h"
27#include "update_engine/common/constants.h"
28#include "update_engine/common/hardware.h"
29#include "update_engine/common/utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070030#include "update_engine/update_manager/state_factory.h"
Alex Deymof7ead812015-10-23 17:37:27 -070031#include "update_engine/weave_service_factory.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070032
33namespace chromeos_update_engine {
34
Alex Deymo30534502015-07-20 15:06:33 -070035RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080036 : debugd_proxy_(bus),
Alex Deymod6deb1d2015-08-28 15:54:37 -070037 power_manager_proxy_(bus),
38 session_manager_proxy_(bus),
Alex Deymo30534502015-07-20 15:06:33 -070039 shill_proxy_(bus),
40 libcros_proxy_(bus) {
41}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042
Alex Deymo8c21b352016-01-20 16:38:33 -080043RealSystemState::~RealSystemState() {
44 // Prevent any DBus communication from UpdateAttempter when shutting down the
45 // daemon.
46 if (update_attempter_)
47 update_attempter_->set_dbus_adaptor(nullptr);
48}
49
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070050bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080051 metrics_lib_.Init();
52
Alex Deymob17327c2015-09-04 10:29:00 -070053 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040054 if (!boot_control_) {
55 LOG(WARNING) << "Unable to create BootControl instance, using stub "
56 << "instead. All update attempts will fail.";
57 boot_control_ = brillo::make_unique_ptr(new BootControlStub());
58 }
Alex Deymo763e7db2015-08-27 21:08:08 -070059
Alex Deymo40d86b22015-09-03 22:27:10 -070060 hardware_ = hardware::CreateHardware();
61 if (!hardware_) {
62 LOG(ERROR) << "Error intializing the HardwareInterface.";
63 return false;
64 }
65
Alex Deymo1c4e84a2015-09-22 16:58:10 -070066 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
67 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
68
Alex Deymo30534502015-07-20 15:06:33 -070069 if (!shill_proxy_.Init()) {
70 LOG(ERROR) << "Failed to initialize shill proxy.";
71 return false;
72 }
73
Alex Deymodd132f32015-09-14 19:12:07 -070074 // Initialize standard and powerwash-safe prefs.
75 base::FilePath non_volatile_path;
76 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
77 // available.
78 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
79 LOG(ERROR) << "Failed to get a non-volatile directory.";
80 return false;
81 }
82 Prefs* prefs;
83 prefs_.reset(prefs = new Prefs());
84 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080085 LOG(ERROR) << "Failed to initialize preferences.";
86 return false;
87 }
88
Alex Deymodd132f32015-09-14 19:12:07 -070089 base::FilePath powerwash_safe_path;
90 if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
91 // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
92 // directory, or disable powerwash feature.
93 powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
94 LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
95 }
96 powerwash_safe_prefs_.reset(prefs = new Prefs());
97 if (!prefs->Init(
98 powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070099 LOG(ERROR) << "Failed to initialize powerwash preferences.";
100 return false;
101 }
102
Alex Deymodd132f32015-09-14 19:12:07 -0700103 // Check the system rebooted marker file.
104 std::string boot_id;
105 if (utils::GetBootId(&boot_id)) {
106 std::string prev_boot_id;
107 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
108 prev_boot_id != boot_id);
109 prefs_->SetString(kPrefsBootId, boot_id);
110 } else {
111 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700112 system_rebooted_ = true;
113 }
114
Alex Deymo5378f5e2015-11-10 15:02:50 -0800115 // Initialize the OmahaRequestParams with the default settings. These settings
116 // will be re-initialized before every request using the actual request
117 // options. This initialization here pre-loads current channel and version, so
118 // the DBus service can access it.
119 if (!request_params_.Init("", "", false)) {
120 LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
121 "features might not work properly.";
122 }
123
Alex Deymo33e91e72015-12-01 18:26:08 -0300124 certificate_checker_.reset(
125 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
126 certificate_checker_->Init();
127
128 // Initialize the UpdateAttempter before the UpdateManager.
129 update_attempter_.reset(
130 new UpdateAttempter(this, certificate_checker_.get(), &libcros_proxy_,
131 &debugd_proxy_));
132 update_attempter_->Init();
133
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800134 weave_service_ = ConstructWeaveService(update_attempter_.get());
Alex Deymof7ead812015-10-23 17:37:27 -0700135
Alex Deymo63784a52014-05-28 10:46:14 -0700136 // Initialize the Update Manager using the default state factory.
137 chromeos_update_manager::State* um_state =
138 chromeos_update_manager::DefaultStateFactory(
Alex Deymo30534502015-07-20 15:06:33 -0700139 &policy_provider_, &shill_proxy_, &session_manager_proxy_, this);
Alex Deymo63784a52014-05-28 10:46:14 -0700140 if (!um_state) {
141 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800142 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700143 }
Alex Deymo63784a52014-05-28 10:46:14 -0700144 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700145 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700146 &clock_, base::TimeDelta::FromSeconds(5),
147 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700148
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700149 // The P2P Manager depends on the Update Manager for its initialization.
150 p2p_manager_.reset(P2PManager::Construct(
151 nullptr, &clock_, update_manager_.get(), "cros_au",
152 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
153
Gilad Arnold1f847232014-04-07 12:07:49 -0700154 if (!payload_state_.Initialize(this)) {
155 LOG(ERROR) << "Failed to initialize the payload state object.";
156 return false;
157 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800158
159 // All is well. Initialization successful.
160 return true;
161}
Jay Srinivasan43488792012-06-19 00:25:31 -0700162
Jay Srinivasan43488792012-06-19 00:25:31 -0700163} // namespace chromeos_update_engine