blob: 8e7ad51c281b03fb3e45bca4f0e111c177647fac [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
Ben Chanab5a0af2017-10-12 14:57:50 -070019#include <memory>
Alex Deymof7ead812015-10-23 17:37:27 -070020#include <string>
21
Alex Deymoe97b39c2016-01-20 13:22:17 -080022#include <base/bind.h>
Ben Chan06c76a42014-09-05 08:21:06 -070023#include <base/files/file_util.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080024#include <base/location.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070025#include <base/time/time.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080026#include <brillo/message_loops/message_loop.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070027#if USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060028#include <chromeos/dbus/service_constants.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070029#endif // USE_CHROME_KIOSK_APP
Jay Srinivasan43488792012-06-19 00:25:31 -070030
Alex Deymo39910dc2015-11-09 17:04:30 -080031#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"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070036#include "update_engine/metrics_reporter_omaha.h"
Daniel Erate5f6f252017-04-20 12:09:58 -060037#if USE_DBUS
38#include "update_engine/dbus_connection.h"
39#endif // USE_DBUS
Alex Deymo63784a52014-05-28 10:46:14 -070040#include "update_engine/update_manager/state_factory.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070041
Alex Deymoe97b39c2016-01-20 13:22:17 -080042using brillo::MessageLoop;
43
Jay Srinivasan43488792012-06-19 00:25:31 -070044namespace chromeos_update_engine {
45
Alex Deymo8c21b352016-01-20 16:38:33 -080046RealSystemState::~RealSystemState() {
47 // Prevent any DBus communication from UpdateAttempter when shutting down the
48 // daemon.
49 if (update_attempter_)
Alex Deymofa78f142016-01-26 21:36:16 -080050 update_attempter_->ClearObservers();
Alex Deymo8c21b352016-01-20 16:38:33 -080051}
52
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070053bool RealSystemState::Initialize() {
Tianjie Xu282aa1f2017-09-05 13:42:45 -070054 metrics_reporter_.Initialize();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080055
Alex Deymob17327c2015-09-04 10:29:00 -070056 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040057 if (!boot_control_) {
58 LOG(WARNING) << "Unable to create BootControl instance, using stub "
59 << "instead. All update attempts will fail.";
Ben Chanab5a0af2017-10-12 14:57:50 -070060 boot_control_ = std::make_unique<BootControlStub>();
David Zeuthen6eddf262015-10-16 15:23:53 -040061 }
Alex Deymo763e7db2015-08-27 21:08:08 -070062
Alex Deymo40d86b22015-09-03 22:27:10 -070063 hardware_ = hardware::CreateHardware();
64 if (!hardware_) {
65 LOG(ERROR) << "Error intializing the HardwareInterface.";
66 return false;
67 }
68
Amin Hassanie94ece52017-08-01 15:03:08 -070069#if USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060070 libcros_proxy_.reset(new org::chromium::LibCrosServiceInterfaceProxy(
71 DBusConnection::Get()->GetDBus(), chromeos::kLibCrosServiceName));
Amin Hassanie94ece52017-08-01 15:03:08 -070072#endif // USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060073
Alex Deymo1c4e84a2015-09-22 16:58:10 -070074 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
75 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
76
Sen Jiangf5bebae2016-06-03 15:36:54 -070077 connection_manager_ = connection_manager::CreateConnectionManager(this);
78 if (!connection_manager_) {
79 LOG(ERROR) << "Error intializing the ConnectionManagerInterface.";
Alex Deymo30534502015-07-20 15:06:33 -070080 return false;
81 }
82
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070083 power_manager_ = power_manager::CreatePowerManager();
84 if (!power_manager_) {
85 LOG(ERROR) << "Error intializing the PowerManagerInterface.";
86 return false;
87 }
88
Alex Deymodd132f32015-09-14 19:12:07 -070089 // 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))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800100 LOG(ERROR) << "Failed to initialize preferences.";
101 return false;
102 }
103
Alex Deymodd132f32015-09-14 19:12:07 -0700104 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))) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700114 LOG(ERROR) << "Failed to initialize powerwash preferences.";
115 return false;
116 }
117
Alex Deymodd132f32015-09-14 19:12:07 -0700118 // 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.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700127 system_rebooted_ = true;
128 }
129
Alex Deymo5378f5e2015-11-10 15:02:50 -0800130 // 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
Alex Deymo33e91e72015-12-01 18:26:08 -0300139 certificate_checker_.reset(
140 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
141 certificate_checker_->Init();
142
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700143 update_attempter_.reset(new UpdateAttempter(this,
144 certificate_checker_.get()));
Sen Jiangc92195c2016-06-13 15:48:44 -0700145
Alex Deymo33e91e72015-12-01 18:26:08 -0300146 // Initialize the UpdateAttempter before the UpdateManager.
Alex Deymo33e91e72015-12-01 18:26:08 -0300147 update_attempter_->Init();
148
Alex Deymo63784a52014-05-28 10:46:14 -0700149 // Initialize the Update Manager using the default state factory.
150 chromeos_update_manager::State* um_state =
Amin Hassanie94ece52017-08-01 15:03:08 -0700151 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
Alex Deymo63784a52014-05-28 10:46:14 -0700159 if (!um_state) {
160 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800161 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700162 }
Alex Deymo63784a52014-05-28 10:46:14 -0700163 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700164 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700165 &clock_, base::TimeDelta::FromSeconds(5),
166 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700167
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700168 // 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
Gilad Arnold1f847232014-04-07 12:07:49 -0700173 if (!payload_state_.Initialize(this)) {
174 LOG(ERROR) << "Failed to initialize the payload state object.";
175 return false;
176 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177
178 // All is well. Initialization successful.
179 return true;
180}
Jay Srinivasan43488792012-06-19 00:25:31 -0700181
Alex Deymofa78f142016-01-26 21:36:16 -0800182bool RealSystemState::StartUpdater() {
Alex Deymoe97b39c2016-01-20 13:22:17 -0800183 // 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())));
Alex Deymofa78f142016-01-26 21:36:16 -0800203 return true;
204}
205
206void RealSystemState::AddObserver(ServiceObserverInterface* observer) {
207 CHECK(update_attempter_.get());
208 update_attempter_->AddObserver(observer);
209}
210
211void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) {
212 CHECK(update_attempter_.get());
213 update_attempter_->RemoveObserver(observer);
Alex Deymoe97b39c2016-01-20 13:22:17 -0800214}
215
Jay Srinivasan43488792012-06-19 00:25:31 -0700216} // namespace chromeos_update_engine