blob: 9474741f436eea7720297d3c7715122a56db03f0 [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>
Amin Hassani0882a512018-04-05 16:25:44 -070021#include <utility>
Alex Deymof7ead812015-10-23 17:37:27 -070022
Alex Deymoe97b39c2016-01-20 13:22:17 -080023#include <base/bind.h>
Ben Chan06c76a42014-09-05 08:21:06 -070024#include <base/files/file_util.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080025#include <base/location.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070026#include <base/time/time.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080027#include <brillo/message_loops/message_loop.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070028#if USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060029#include <chromeos/dbus/service_constants.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070030#endif // USE_CHROME_KIOSK_APP
Jay Srinivasan43488792012-06-19 00:25:31 -070031
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/boot_control.h"
33#include "update_engine/common/boot_control_stub.h"
34#include "update_engine/common/constants.h"
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080035#include "update_engine/common/dlcservice.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/hardware.h"
37#include "update_engine/common/utils.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070038#include "update_engine/metrics_reporter_omaha.h"
Amin Hassani0882a512018-04-05 16:25:44 -070039#include "update_engine/update_boot_flags_action.h"
Daniel Erate5f6f252017-04-20 12:09:58 -060040#if USE_DBUS
41#include "update_engine/dbus_connection.h"
42#endif // USE_DBUS
Alex Deymo63784a52014-05-28 10:46:14 -070043#include "update_engine/update_manager/state_factory.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070044
Alex Deymoe97b39c2016-01-20 13:22:17 -080045using brillo::MessageLoop;
46
Jay Srinivasan43488792012-06-19 00:25:31 -070047namespace chromeos_update_engine {
48
Alex Deymo8c21b352016-01-20 16:38:33 -080049RealSystemState::~RealSystemState() {
50 // Prevent any DBus communication from UpdateAttempter when shutting down the
51 // daemon.
52 if (update_attempter_)
Alex Deymofa78f142016-01-26 21:36:16 -080053 update_attempter_->ClearObservers();
Alex Deymo8c21b352016-01-20 16:38:33 -080054}
55
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070056bool RealSystemState::Initialize() {
Tianjie Xu282aa1f2017-09-05 13:42:45 -070057 metrics_reporter_.Initialize();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080058
Alex Deymob17327c2015-09-04 10:29:00 -070059 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040060 if (!boot_control_) {
61 LOG(WARNING) << "Unable to create BootControl instance, using stub "
62 << "instead. All update attempts will fail.";
Ben Chanab5a0af2017-10-12 14:57:50 -070063 boot_control_ = std::make_unique<BootControlStub>();
David Zeuthen6eddf262015-10-16 15:23:53 -040064 }
Alex Deymo763e7db2015-08-27 21:08:08 -070065
Alex Deymo40d86b22015-09-03 22:27:10 -070066 hardware_ = hardware::CreateHardware();
67 if (!hardware_) {
Sen Jiang771f6482018-04-04 17:59:10 -070068 LOG(ERROR) << "Error initializing the HardwareInterface.";
Alex Deymo40d86b22015-09-03 22:27:10 -070069 return false;
70 }
71
Amin Hassanie94ece52017-08-01 15:03:08 -070072#if USE_CHROME_KIOSK_APP
Daniel Erat04df23a2018-03-29 17:55:35 -070073 kiosk_app_proxy_.reset(new org::chromium::KioskAppServiceInterfaceProxy(
74 DBusConnection::Get()->GetDBus(), chromeos::kKioskAppServiceName));
Amin Hassanie94ece52017-08-01 15:03:08 -070075#endif // USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060076
Alex Deymo1c4e84a2015-09-22 16:58:10 -070077 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
78 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
79
Sen Jiangf5bebae2016-06-03 15:36:54 -070080 connection_manager_ = connection_manager::CreateConnectionManager(this);
81 if (!connection_manager_) {
Sen Jiang771f6482018-04-04 17:59:10 -070082 LOG(ERROR) << "Error initializing the ConnectionManagerInterface.";
Alex Deymo30534502015-07-20 15:06:33 -070083 return false;
84 }
85
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070086 power_manager_ = power_manager::CreatePowerManager();
87 if (!power_manager_) {
Sen Jiang771f6482018-04-04 17:59:10 -070088 LOG(ERROR) << "Error initializing the PowerManagerInterface.";
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070089 return false;
90 }
91
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080092 dlcservice_ = CreateDlcService();
93 if (!dlcservice_) {
94 LOG(ERROR) << "Error initializing the DlcServiceInterface.";
95 return false;
96 }
97
Alex Deymodd132f32015-09-14 19:12:07 -070098 // Initialize standard and powerwash-safe prefs.
99 base::FilePath non_volatile_path;
100 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
101 // available.
102 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
103 LOG(ERROR) << "Failed to get a non-volatile directory.";
104 return false;
105 }
106 Prefs* prefs;
107 prefs_.reset(prefs = new Prefs());
108 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800109 LOG(ERROR) << "Failed to initialize preferences.";
110 return false;
111 }
112
Alex Deymodd132f32015-09-14 19:12:07 -0700113 base::FilePath powerwash_safe_path;
114 if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
115 // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
116 // directory, or disable powerwash feature.
117 powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
118 LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
119 }
120 powerwash_safe_prefs_.reset(prefs = new Prefs());
121 if (!prefs->Init(
122 powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700123 LOG(ERROR) << "Failed to initialize powerwash preferences.";
124 return false;
125 }
126
Alex Deymodd132f32015-09-14 19:12:07 -0700127 // Check the system rebooted marker file.
128 std::string boot_id;
129 if (utils::GetBootId(&boot_id)) {
130 std::string prev_boot_id;
131 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
132 prev_boot_id != boot_id);
133 prefs_->SetString(kPrefsBootId, boot_id);
134 } else {
135 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700136 system_rebooted_ = true;
137 }
138
Alex Deymo5378f5e2015-11-10 15:02:50 -0800139 // Initialize the OmahaRequestParams with the default settings. These settings
140 // will be re-initialized before every request using the actual request
141 // options. This initialization here pre-loads current channel and version, so
142 // the DBus service can access it.
143 if (!request_params_.Init("", "", false)) {
144 LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
145 "features might not work properly.";
146 }
147
Alex Deymo33e91e72015-12-01 18:26:08 -0300148 certificate_checker_.reset(
149 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
150 certificate_checker_->Init();
151
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700152 update_attempter_.reset(new UpdateAttempter(this,
153 certificate_checker_.get()));
Sen Jiangc92195c2016-06-13 15:48:44 -0700154
Alex Deymo33e91e72015-12-01 18:26:08 -0300155 // Initialize the UpdateAttempter before the UpdateManager.
Alex Deymo33e91e72015-12-01 18:26:08 -0300156 update_attempter_->Init();
157
Alex Deymo63784a52014-05-28 10:46:14 -0700158 // Initialize the Update Manager using the default state factory.
159 chromeos_update_manager::State* um_state =
Amin Hassanie94ece52017-08-01 15:03:08 -0700160 chromeos_update_manager::DefaultStateFactory(&policy_provider_,
161#if USE_CHROME_KIOSK_APP
Daniel Erat04df23a2018-03-29 17:55:35 -0700162 kiosk_app_proxy_.get(),
Amin Hassanie94ece52017-08-01 15:03:08 -0700163#else
164 nullptr,
165#endif // USE_CHROME_KIOSK_APP
166 this);
167
Alex Deymo63784a52014-05-28 10:46:14 -0700168 if (!um_state) {
169 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800170 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700171 }
Alex Deymo63784a52014-05-28 10:46:14 -0700172 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700173 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700174 &clock_, base::TimeDelta::FromSeconds(5),
175 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700176
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700177 // The P2P Manager depends on the Update Manager for its initialization.
178 p2p_manager_.reset(P2PManager::Construct(
179 nullptr, &clock_, update_manager_.get(), "cros_au",
180 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
181
Gilad Arnold1f847232014-04-07 12:07:49 -0700182 if (!payload_state_.Initialize(this)) {
183 LOG(ERROR) << "Failed to initialize the payload state object.";
184 return false;
185 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800186
Amin Hassanief13c922019-01-10 17:30:38 -0800187 // For devices that are not rollback enabled (ie. consumer devices),
188 // initialize max kernel key version to 0xfffffffe, which is logically
189 // infinity.
190 if (policy_provider_.IsConsumerDevice()) {
191 if (!hardware()->SetMaxKernelKeyRollforward(
192 chromeos_update_manager::kRollforwardInfinity)) {
193 LOG(ERROR) << "Failed to set kernel_max_rollforward to infinity for"
194 << " consumer devices";
195 }
196 }
197
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198 // All is well. Initialization successful.
199 return true;
200}
Jay Srinivasan43488792012-06-19 00:25:31 -0700201
Alex Deymofa78f142016-01-26 21:36:16 -0800202bool RealSystemState::StartUpdater() {
Alex Deymoe97b39c2016-01-20 13:22:17 -0800203 // Initiate update checks.
204 update_attempter_->ScheduleUpdates();
205
Amin Hassani0882a512018-04-05 16:25:44 -0700206 auto update_boot_flags_action =
207 std::make_unique<UpdateBootFlagsAction>(boot_control_.get());
208 processor_.EnqueueAction(std::move(update_boot_flags_action));
Alex Deymoe97b39c2016-01-20 13:22:17 -0800209 // Update boot flags after 45 seconds.
210 MessageLoop::current()->PostDelayedTask(
211 FROM_HERE,
Amin Hassani0882a512018-04-05 16:25:44 -0700212 base::Bind(&ActionProcessor::StartProcessing,
213 base::Unretained(&processor_)),
Alex Deymoe97b39c2016-01-20 13:22:17 -0800214 base::TimeDelta::FromSeconds(45));
215
216 // Broadcast the update engine status on startup to ensure consistent system
217 // state on crashes.
218 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
219 &UpdateAttempter::BroadcastStatus,
220 base::Unretained(update_attempter_.get())));
221
222 // Run the UpdateEngineStarted() method on |update_attempter|.
223 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
224 &UpdateAttempter::UpdateEngineStarted,
225 base::Unretained(update_attempter_.get())));
Alex Deymofa78f142016-01-26 21:36:16 -0800226 return true;
227}
228
229void RealSystemState::AddObserver(ServiceObserverInterface* observer) {
230 CHECK(update_attempter_.get());
231 update_attempter_->AddObserver(observer);
232}
233
234void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) {
235 CHECK(update_attempter_.get());
236 update_attempter_->RemoveObserver(observer);
Alex Deymoe97b39c2016-01-20 13:22:17 -0800237}
238
Jay Srinivasan43488792012-06-19 00:25:31 -0700239} // namespace chromeos_update_engine