Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 17 | #include "update_engine/real_system_state.h" |
| 18 | |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 21 | #include <base/bind.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 22 | #include <base/files/file_util.h> |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 23 | #include <base/location.h> |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 24 | #include <base/time/time.h> |
David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 25 | #include <brillo/make_unique_ptr.h> |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 26 | #include <brillo/message_loops/message_loop.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #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 Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 33 | #include "update_engine/update_manager/state_factory.h" |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 34 | #include "update_engine/weave_service_factory.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 35 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 36 | using brillo::MessageLoop; |
| 37 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 38 | namespace chromeos_update_engine { |
| 39 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 40 | RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus) |
Sen Jiang | 9730679 | 2016-06-08 16:04:06 -0700 | [diff] [blame] | 41 | : debugd_proxy_(bus) {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 42 | |
Alex Deymo | 8c21b35 | 2016-01-20 16:38:33 -0800 | [diff] [blame] | 43 | RealSystemState::~RealSystemState() { |
| 44 | // Prevent any DBus communication from UpdateAttempter when shutting down the |
| 45 | // daemon. |
| 46 | if (update_attempter_) |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 47 | update_attempter_->ClearObservers(); |
Alex Deymo | 8c21b35 | 2016-01-20 16:38:33 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 50 | bool RealSystemState::Initialize() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 51 | metrics_lib_.Init(); |
| 52 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 53 | boot_control_ = boot_control::CreateBootControl(); |
David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 54 | 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 Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 59 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 60 | hardware_ = hardware::CreateHardware(); |
| 61 | if (!hardware_) { |
| 62 | LOG(ERROR) << "Error intializing the HardwareInterface."; |
| 63 | return false; |
| 64 | } |
| 65 | |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 66 | LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode."; |
| 67 | LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build."; |
| 68 | |
Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 69 | connection_manager_ = connection_manager::CreateConnectionManager(this); |
| 70 | if (!connection_manager_) { |
| 71 | LOG(ERROR) << "Error intializing the ConnectionManagerInterface."; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
Sen Jiang | b8c6a8f | 2016-06-07 17:33:17 -0700 | [diff] [blame] | 75 | power_manager_ = power_manager::CreatePowerManager(); |
| 76 | if (!power_manager_) { |
| 77 | LOG(ERROR) << "Error intializing the PowerManagerInterface."; |
| 78 | return false; |
| 79 | } |
| 80 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 81 | // 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 92 | LOG(ERROR) << "Failed to initialize preferences."; |
| 93 | return false; |
| 94 | } |
| 95 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 96 | 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 Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 106 | LOG(ERROR) << "Failed to initialize powerwash preferences."; |
| 107 | return false; |
| 108 | } |
| 109 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 110 | // 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 Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 119 | system_rebooted_ = true; |
| 120 | } |
| 121 | |
Alex Deymo | 5378f5e | 2015-11-10 15:02:50 -0800 | [diff] [blame] | 122 | // 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 Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 131 | certificate_checker_.reset( |
| 132 | new CertificateChecker(prefs_.get(), &openssl_wrapper_)); |
| 133 | certificate_checker_->Init(); |
| 134 | |
| 135 | // Initialize the UpdateAttempter before the UpdateManager. |
| 136 | update_attempter_.reset( |
| 137 | new UpdateAttempter(this, certificate_checker_.get(), &libcros_proxy_, |
| 138 | &debugd_proxy_)); |
| 139 | update_attempter_->Init(); |
| 140 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 141 | weave_service_ = ConstructWeaveService(update_attempter_.get()); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 142 | if (weave_service_) |
| 143 | update_attempter_->AddObserver(weave_service_.get()); |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 144 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 145 | // Initialize the Update Manager using the default state factory. |
| 146 | chromeos_update_manager::State* um_state = |
| 147 | chromeos_update_manager::DefaultStateFactory( |
Sen Jiang | 9730679 | 2016-06-08 16:04:06 -0700 | [diff] [blame] | 148 | &policy_provider_, &libcros_proxy_, this); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 149 | if (!um_state) { |
| 150 | LOG(ERROR) << "Failed to initialize the Update Manager."; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 151 | return false; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 152 | } |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 153 | update_manager_.reset( |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 154 | new chromeos_update_manager::UpdateManager( |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 155 | &clock_, base::TimeDelta::FromSeconds(5), |
| 156 | base::TimeDelta::FromHours(12), um_state)); |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 157 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 158 | // The P2P Manager depends on the Update Manager for its initialization. |
| 159 | p2p_manager_.reset(P2PManager::Construct( |
| 160 | nullptr, &clock_, update_manager_.get(), "cros_au", |
| 161 | kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays))); |
| 162 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 163 | if (!payload_state_.Initialize(this)) { |
| 164 | LOG(ERROR) << "Failed to initialize the payload state object."; |
| 165 | return false; |
| 166 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 167 | |
| 168 | // All is well. Initialization successful. |
| 169 | return true; |
| 170 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 171 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 172 | bool RealSystemState::StartUpdater() { |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 173 | // Initiate update checks. |
| 174 | update_attempter_->ScheduleUpdates(); |
| 175 | |
| 176 | // Update boot flags after 45 seconds. |
| 177 | MessageLoop::current()->PostDelayedTask( |
| 178 | FROM_HERE, |
| 179 | base::Bind(&UpdateAttempter::UpdateBootFlags, |
| 180 | base::Unretained(update_attempter_.get())), |
| 181 | base::TimeDelta::FromSeconds(45)); |
| 182 | |
| 183 | // Broadcast the update engine status on startup to ensure consistent system |
| 184 | // state on crashes. |
| 185 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 186 | &UpdateAttempter::BroadcastStatus, |
| 187 | base::Unretained(update_attempter_.get()))); |
| 188 | |
| 189 | // Run the UpdateEngineStarted() method on |update_attempter|. |
| 190 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 191 | &UpdateAttempter::UpdateEngineStarted, |
| 192 | base::Unretained(update_attempter_.get()))); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | void RealSystemState::AddObserver(ServiceObserverInterface* observer) { |
| 197 | CHECK(update_attempter_.get()); |
| 198 | update_attempter_->AddObserver(observer); |
| 199 | } |
| 200 | |
| 201 | void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) { |
| 202 | CHECK(update_attempter_.get()); |
| 203 | update_attempter_->RemoveObserver(observer); |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 206 | } // namespace chromeos_update_engine |