blob: aab074f8321d52b441e1ad5ec062aef0539f0266 [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>
Jay Srinivasan43488792012-06-19 00:25:31 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#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/hardware.h"
32#include "update_engine/common/utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070033#include "update_engine/update_manager/state_factory.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070034
Alex Deymoe97b39c2016-01-20 13:22:17 -080035using brillo::MessageLoop;
36
Jay Srinivasan43488792012-06-19 00:25:31 -070037namespace chromeos_update_engine {
38
Alex Deymo8c21b352016-01-20 16:38:33 -080039RealSystemState::~RealSystemState() {
40 // Prevent any DBus communication from UpdateAttempter when shutting down the
41 // daemon.
42 if (update_attempter_)
Alex Deymofa78f142016-01-26 21:36:16 -080043 update_attempter_->ClearObservers();
Alex Deymo8c21b352016-01-20 16:38:33 -080044}
45
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070046bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080047 metrics_lib_.Init();
48
Alex Deymob17327c2015-09-04 10:29:00 -070049 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040050 if (!boot_control_) {
51 LOG(WARNING) << "Unable to create BootControl instance, using stub "
52 << "instead. All update attempts will fail.";
53 boot_control_ = brillo::make_unique_ptr(new BootControlStub());
54 }
Alex Deymo763e7db2015-08-27 21:08:08 -070055
Alex Deymo40d86b22015-09-03 22:27:10 -070056 hardware_ = hardware::CreateHardware();
57 if (!hardware_) {
58 LOG(ERROR) << "Error intializing the HardwareInterface.";
59 return false;
60 }
61
Alex Deymo1c4e84a2015-09-22 16:58:10 -070062 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
63 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
64
Sen Jiangf5bebae2016-06-03 15:36:54 -070065 connection_manager_ = connection_manager::CreateConnectionManager(this);
66 if (!connection_manager_) {
67 LOG(ERROR) << "Error intializing the ConnectionManagerInterface.";
Alex Deymo30534502015-07-20 15:06:33 -070068 return false;
69 }
70
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070071 power_manager_ = power_manager::CreatePowerManager();
72 if (!power_manager_) {
73 LOG(ERROR) << "Error intializing the PowerManagerInterface.";
74 return false;
75 }
76
Alex Deymodd132f32015-09-14 19:12:07 -070077 // Initialize standard and powerwash-safe prefs.
78 base::FilePath non_volatile_path;
79 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
80 // available.
81 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
82 LOG(ERROR) << "Failed to get a non-volatile directory.";
83 return false;
84 }
85 Prefs* prefs;
86 prefs_.reset(prefs = new Prefs());
87 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080088 LOG(ERROR) << "Failed to initialize preferences.";
89 return false;
90 }
91
Alex Deymodd132f32015-09-14 19:12:07 -070092 base::FilePath powerwash_safe_path;
93 if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
94 // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
95 // directory, or disable powerwash feature.
96 powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
97 LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
98 }
99 powerwash_safe_prefs_.reset(prefs = new Prefs());
100 if (!prefs->Init(
101 powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700102 LOG(ERROR) << "Failed to initialize powerwash preferences.";
103 return false;
104 }
105
Alex Deymodd132f32015-09-14 19:12:07 -0700106 // Check the system rebooted marker file.
107 std::string boot_id;
108 if (utils::GetBootId(&boot_id)) {
109 std::string prev_boot_id;
110 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
111 prev_boot_id != boot_id);
112 prefs_->SetString(kPrefsBootId, boot_id);
113 } else {
114 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700115 system_rebooted_ = true;
116 }
117
Alex Deymo5378f5e2015-11-10 15:02:50 -0800118 // Initialize the OmahaRequestParams with the default settings. These settings
119 // will be re-initialized before every request using the actual request
120 // options. This initialization here pre-loads current channel and version, so
121 // the DBus service can access it.
122 if (!request_params_.Init("", "", false)) {
123 LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
124 "features might not work properly.";
125 }
126
Alex Deymo33e91e72015-12-01 18:26:08 -0300127 certificate_checker_.reset(
128 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
129 certificate_checker_->Init();
130
Sen Jiangc92195c2016-06-13 15:48:44 -0700131#if USE_LIBCROS
132 LibCrosProxy* libcros_proxy = &libcros_proxy_;
133#else
134 LibCrosProxy* libcros_proxy = nullptr;
135#endif // USE_LIBCROS
136
Alex Deymo33e91e72015-12-01 18:26:08 -0300137 // Initialize the UpdateAttempter before the UpdateManager.
138 update_attempter_.reset(
Sen Jiangc92195c2016-06-13 15:48:44 -0700139 new UpdateAttempter(this, certificate_checker_.get(), libcros_proxy));
Alex Deymo33e91e72015-12-01 18:26:08 -0300140 update_attempter_->Init();
141
Alex Deymo63784a52014-05-28 10:46:14 -0700142 // Initialize the Update Manager using the default state factory.
143 chromeos_update_manager::State* um_state =
144 chromeos_update_manager::DefaultStateFactory(
Sen Jiangc92195c2016-06-13 15:48:44 -0700145 &policy_provider_, libcros_proxy, this);
Alex Deymo63784a52014-05-28 10:46:14 -0700146 if (!um_state) {
147 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800148 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700149 }
Alex Deymo63784a52014-05-28 10:46:14 -0700150 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700151 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700152 &clock_, base::TimeDelta::FromSeconds(5),
153 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700154
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700155 // The P2P Manager depends on the Update Manager for its initialization.
156 p2p_manager_.reset(P2PManager::Construct(
157 nullptr, &clock_, update_manager_.get(), "cros_au",
158 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
159
Gilad Arnold1f847232014-04-07 12:07:49 -0700160 if (!payload_state_.Initialize(this)) {
161 LOG(ERROR) << "Failed to initialize the payload state object.";
162 return false;
163 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800164
165 // All is well. Initialization successful.
166 return true;
167}
Jay Srinivasan43488792012-06-19 00:25:31 -0700168
Alex Deymofa78f142016-01-26 21:36:16 -0800169bool RealSystemState::StartUpdater() {
Alex Deymoe97b39c2016-01-20 13:22:17 -0800170 // Initiate update checks.
171 update_attempter_->ScheduleUpdates();
172
173 // Update boot flags after 45 seconds.
174 MessageLoop::current()->PostDelayedTask(
175 FROM_HERE,
176 base::Bind(&UpdateAttempter::UpdateBootFlags,
177 base::Unretained(update_attempter_.get())),
178 base::TimeDelta::FromSeconds(45));
179
180 // Broadcast the update engine status on startup to ensure consistent system
181 // state on crashes.
182 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
183 &UpdateAttempter::BroadcastStatus,
184 base::Unretained(update_attempter_.get())));
185
186 // Run the UpdateEngineStarted() method on |update_attempter|.
187 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
188 &UpdateAttempter::UpdateEngineStarted,
189 base::Unretained(update_attempter_.get())));
Alex Deymofa78f142016-01-26 21:36:16 -0800190 return true;
191}
192
193void RealSystemState::AddObserver(ServiceObserverInterface* observer) {
194 CHECK(update_attempter_.get());
195 update_attempter_->AddObserver(observer);
196}
197
198void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) {
199 CHECK(update_attempter_.get());
200 update_attempter_->RemoveObserver(observer);
Alex Deymoe97b39c2016-01-20 13:22:17 -0800201}
202
Jay Srinivasan43488792012-06-19 00:25:31 -0700203} // namespace chromeos_update_engine