blob: 74921e2ff8b1a5218807ca00c4620385b47ef732 [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
Alex Deymoe97b39c2016-01-20 13:22:17 -080021#include <base/bind.h>
Ben Chan06c76a42014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080023#include <base/location.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070024#include <base/time/time.h>
David Zeuthen6eddf262015-10-16 15:23:53 -040025#include <brillo/make_unique_ptr.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080026#include <brillo/message_loops/message_loop.h>
Daniel Erat941cf232017-04-20 12:09:58 -060027#include <chromeos/dbus/service_constants.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/boot_control.h"
30#include "update_engine/common/boot_control_stub.h"
31#include "update_engine/common/constants.h"
32#include "update_engine/common/hardware.h"
33#include "update_engine/common/utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070034#include "update_engine/update_manager/state_factory.h"
Alex Deymof7ead812015-10-23 17:37:27 -070035#include "update_engine/weave_service_factory.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070036
Alex Deymoe97b39c2016-01-20 13:22:17 -080037using brillo::MessageLoop;
38
Jay Srinivasan43488792012-06-19 00:25:31 -070039namespace chromeos_update_engine {
40
Alex Deymo30534502015-07-20 15:06:33 -070041RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080042 : debugd_proxy_(bus),
Daniel Erat941cf232017-04-20 12:09:58 -060043 libcros_proxy_(bus, chromeos::kLibCrosServiceName),
44 network_proxy_service_proxy_(bus, chromeos::kNetworkProxyServiceName),
Alex Deymod6deb1d2015-08-28 15:54:37 -070045 power_manager_proxy_(bus),
46 session_manager_proxy_(bus),
Daniel Erat941cf232017-04-20 12:09:58 -060047 shill_proxy_(bus) {
Alex Deymo30534502015-07-20 15:06:33 -070048}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049
Alex Deymo8c21b352016-01-20 16:38:33 -080050RealSystemState::~RealSystemState() {
51 // Prevent any DBus communication from UpdateAttempter when shutting down the
52 // daemon.
53 if (update_attempter_)
Alex Deymofa78f142016-01-26 21:36:16 -080054 update_attempter_->ClearObservers();
Alex Deymo8c21b352016-01-20 16:38:33 -080055}
56
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070057bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080058 metrics_lib_.Init();
59
Alex Deymob17327c2015-09-04 10:29:00 -070060 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040061 if (!boot_control_) {
62 LOG(WARNING) << "Unable to create BootControl instance, using stub "
63 << "instead. All update attempts will fail.";
64 boot_control_ = brillo::make_unique_ptr(new BootControlStub());
65 }
Alex Deymo763e7db2015-08-27 21:08:08 -070066
Alex Deymo40d86b22015-09-03 22:27:10 -070067 hardware_ = hardware::CreateHardware();
68 if (!hardware_) {
69 LOG(ERROR) << "Error intializing the HardwareInterface.";
70 return false;
71 }
72
Alex Deymo1c4e84a2015-09-22 16:58:10 -070073 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
74 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
75
Alex Deymo30534502015-07-20 15:06:33 -070076 if (!shill_proxy_.Init()) {
77 LOG(ERROR) << "Failed to initialize shill proxy.";
78 return false;
79 }
80
Alex Deymodd132f32015-09-14 19:12:07 -070081 // Initialize standard and powerwash-safe prefs.
82 base::FilePath non_volatile_path;
83 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
84 // available.
85 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
86 LOG(ERROR) << "Failed to get a non-volatile directory.";
87 return false;
88 }
89 Prefs* prefs;
90 prefs_.reset(prefs = new Prefs());
91 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092 LOG(ERROR) << "Failed to initialize preferences.";
93 return false;
94 }
95
Alex Deymodd132f32015-09-14 19:12:07 -070096 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))) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700106 LOG(ERROR) << "Failed to initialize powerwash preferences.";
107 return false;
108 }
109
Alex Deymodd132f32015-09-14 19:12:07 -0700110 // Check the system rebooted marker file.
111 std::string boot_id;
112 if (utils::GetBootId(&boot_id)) {
113 std::string prev_boot_id;
114 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
115 prev_boot_id != boot_id);
116 prefs_->SetString(kPrefsBootId, boot_id);
117 } else {
118 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700119 system_rebooted_ = true;
120 }
121
Alex Deymo5378f5e2015-11-10 15:02:50 -0800122 // Initialize the OmahaRequestParams with the default settings. These settings
123 // will be re-initialized before every request using the actual request
124 // options. This initialization here pre-loads current channel and version, so
125 // the DBus service can access it.
126 if (!request_params_.Init("", "", false)) {
127 LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
128 "features might not work properly.";
129 }
130
Alex Deymo33e91e72015-12-01 18:26:08 -0300131 certificate_checker_.reset(
132 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
133 certificate_checker_->Init();
134
135 // Initialize the UpdateAttempter before the UpdateManager.
Daniel Erat941cf232017-04-20 12:09:58 -0600136 update_attempter_.reset(new UpdateAttempter(this, certificate_checker_.get(),
137 &network_proxy_service_proxy_,
138 &debugd_proxy_));
Alex Deymo33e91e72015-12-01 18:26:08 -0300139 update_attempter_->Init();
140
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800141 weave_service_ = ConstructWeaveService(update_attempter_.get());
Alex Deymofa78f142016-01-26 21:36:16 -0800142 if (weave_service_)
143 update_attempter_->AddObserver(weave_service_.get());
Alex Deymof7ead812015-10-23 17:37:27 -0700144
Alex Deymo63784a52014-05-28 10:46:14 -0700145 // Initialize the Update Manager using the default state factory.
146 chromeos_update_manager::State* um_state =
147 chromeos_update_manager::DefaultStateFactory(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800148 &policy_provider_, &shill_proxy_, &session_manager_proxy_,
149 &libcros_proxy_, this);
Alex Deymo63784a52014-05-28 10:46:14 -0700150 if (!um_state) {
151 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800152 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700153 }
Alex Deymo63784a52014-05-28 10:46:14 -0700154 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700155 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700156 &clock_, base::TimeDelta::FromSeconds(5),
157 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700158
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700159 // The P2P Manager depends on the Update Manager for its initialization.
160 p2p_manager_.reset(P2PManager::Construct(
161 nullptr, &clock_, update_manager_.get(), "cros_au",
162 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
163
Gilad Arnold1f847232014-04-07 12:07:49 -0700164 if (!payload_state_.Initialize(this)) {
165 LOG(ERROR) << "Failed to initialize the payload state object.";
166 return false;
167 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800168
169 // All is well. Initialization successful.
170 return true;
171}
Jay Srinivasan43488792012-06-19 00:25:31 -0700172
Alex Deymofa78f142016-01-26 21:36:16 -0800173bool RealSystemState::StartUpdater() {
Alex Deymoe97b39c2016-01-20 13:22:17 -0800174 // Initiate update checks.
175 update_attempter_->ScheduleUpdates();
176
177 // Update boot flags after 45 seconds.
178 MessageLoop::current()->PostDelayedTask(
179 FROM_HERE,
180 base::Bind(&UpdateAttempter::UpdateBootFlags,
181 base::Unretained(update_attempter_.get())),
182 base::TimeDelta::FromSeconds(45));
183
184 // Broadcast the update engine status on startup to ensure consistent system
185 // state on crashes.
186 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
187 &UpdateAttempter::BroadcastStatus,
188 base::Unretained(update_attempter_.get())));
189
190 // Run the UpdateEngineStarted() method on |update_attempter|.
191 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
192 &UpdateAttempter::UpdateEngineStarted,
193 base::Unretained(update_attempter_.get())));
Alex Deymofa78f142016-01-26 21:36:16 -0800194 return true;
195}
196
197void RealSystemState::AddObserver(ServiceObserverInterface* observer) {
198 CHECK(update_attempter_.get());
199 update_attempter_->AddObserver(observer);
200}
201
202void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) {
203 CHECK(update_attempter_.get());
204 update_attempter_->RemoveObserver(observer);
Alex Deymoe97b39c2016-01-20 13:22:17 -0800205}
206
Jay Srinivasan43488792012-06-19 00:25:31 -0700207} // namespace chromeos_update_engine