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 | |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 19 | #include <memory> |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 22 | #include <base/bind.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 23 | #include <base/files/file_util.h> |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 24 | #include <base/location.h> |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 25 | #include <base/time/time.h> |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 26 | #include <brillo/message_loops/message_loop.h> |
Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 27 | #if USE_CHROME_KIOSK_APP |
Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 28 | #include <chromeos/dbus/service_constants.h> |
Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 29 | #endif // USE_CHROME_KIOSK_APP |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #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" |
Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 36 | #if USE_DBUS |
| 37 | #include "update_engine/dbus_connection.h" |
| 38 | #endif // USE_DBUS |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 39 | #include "update_engine/update_manager/state_factory.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 40 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 41 | using brillo::MessageLoop; |
| 42 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 43 | namespace chromeos_update_engine { |
| 44 | |
Alex Deymo | 8c21b35 | 2016-01-20 16:38:33 -0800 | [diff] [blame] | 45 | RealSystemState::~RealSystemState() { |
| 46 | // Prevent any DBus communication from UpdateAttempter when shutting down the |
| 47 | // daemon. |
| 48 | if (update_attempter_) |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 49 | update_attempter_->ClearObservers(); |
Alex Deymo | 8c21b35 | 2016-01-20 16:38:33 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 52 | bool RealSystemState::Initialize() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 53 | metrics_lib_.Init(); |
| 54 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 55 | boot_control_ = boot_control::CreateBootControl(); |
David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 56 | if (!boot_control_) { |
| 57 | LOG(WARNING) << "Unable to create BootControl instance, using stub " |
| 58 | << "instead. All update attempts will fail."; |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 59 | boot_control_ = std::make_unique<BootControlStub>(); |
David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 60 | } |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 61 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 62 | hardware_ = hardware::CreateHardware(); |
| 63 | if (!hardware_) { |
| 64 | LOG(ERROR) << "Error intializing the HardwareInterface."; |
| 65 | return false; |
| 66 | } |
| 67 | |
Amin Hassani | 4718629 | 2017-08-01 15:03:08 -0700 | [diff] [blame] | 68 | #if USE_CHROME_KIOSK_APP |
Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 69 | libcros_proxy_.reset(new org::chromium::LibCrosServiceInterfaceProxy( |
| 70 | DBusConnection::Get()->GetDBus(), chromeos::kLibCrosServiceName)); |
Amin Hassani | 4718629 | 2017-08-01 15:03:08 -0700 | [diff] [blame] | 71 | #endif // USE_CHROME_KIOSK_APP |
Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 72 | |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 73 | LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode."; |
| 74 | LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build."; |
| 75 | |
Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 76 | connection_manager_ = connection_manager::CreateConnectionManager(this); |
| 77 | if (!connection_manager_) { |
| 78 | LOG(ERROR) << "Error intializing the ConnectionManagerInterface."; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
Sen Jiang | b8c6a8f | 2016-06-07 17:33:17 -0700 | [diff] [blame] | 82 | power_manager_ = power_manager::CreatePowerManager(); |
| 83 | if (!power_manager_) { |
| 84 | LOG(ERROR) << "Error intializing the PowerManagerInterface."; |
| 85 | return false; |
| 86 | } |
| 87 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 88 | // Initialize standard and powerwash-safe prefs. |
| 89 | base::FilePath non_volatile_path; |
| 90 | // TODO(deymo): Fall back to in-memory prefs if there's no physical directory |
| 91 | // available. |
| 92 | if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) { |
| 93 | LOG(ERROR) << "Failed to get a non-volatile directory."; |
| 94 | return false; |
| 95 | } |
| 96 | Prefs* prefs; |
| 97 | prefs_.reset(prefs = new Prefs()); |
| 98 | if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 99 | LOG(ERROR) << "Failed to initialize preferences."; |
| 100 | return false; |
| 101 | } |
| 102 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 103 | base::FilePath powerwash_safe_path; |
| 104 | if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) { |
| 105 | // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe |
| 106 | // directory, or disable powerwash feature. |
| 107 | powerwash_safe_path = non_volatile_path.Append("powerwash-safe"); |
| 108 | LOG(WARNING) << "No powerwash-safe directory, using non-volatile one."; |
| 109 | } |
| 110 | powerwash_safe_prefs_.reset(prefs = new Prefs()); |
| 111 | if (!prefs->Init( |
| 112 | powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 113 | LOG(ERROR) << "Failed to initialize powerwash preferences."; |
| 114 | return false; |
| 115 | } |
| 116 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 117 | // Check the system rebooted marker file. |
| 118 | std::string boot_id; |
| 119 | if (utils::GetBootId(&boot_id)) { |
| 120 | std::string prev_boot_id; |
| 121 | system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) || |
| 122 | prev_boot_id != boot_id); |
| 123 | prefs_->SetString(kPrefsBootId, boot_id); |
| 124 | } else { |
| 125 | LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted."; |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 126 | system_rebooted_ = true; |
| 127 | } |
| 128 | |
Alex Deymo | 5378f5e | 2015-11-10 15:02:50 -0800 | [diff] [blame] | 129 | // Initialize the OmahaRequestParams with the default settings. These settings |
| 130 | // will be re-initialized before every request using the actual request |
| 131 | // options. This initialization here pre-loads current channel and version, so |
| 132 | // the DBus service can access it. |
| 133 | if (!request_params_.Init("", "", false)) { |
| 134 | LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some " |
| 135 | "features might not work properly."; |
| 136 | } |
| 137 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 138 | certificate_checker_.reset( |
| 139 | new CertificateChecker(prefs_.get(), &openssl_wrapper_)); |
| 140 | certificate_checker_->Init(); |
| 141 | |
Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 142 | update_attempter_.reset(new UpdateAttempter(this, |
| 143 | certificate_checker_.get())); |
Sen Jiang | c92195c | 2016-06-13 15:48:44 -0700 | [diff] [blame] | 144 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 145 | // Initialize the UpdateAttempter before the UpdateManager. |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 146 | update_attempter_->Init(); |
| 147 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 148 | // Initialize the Update Manager using the default state factory. |
| 149 | chromeos_update_manager::State* um_state = |
Amin Hassani | 4718629 | 2017-08-01 15:03:08 -0700 | [diff] [blame] | 150 | chromeos_update_manager::DefaultStateFactory(&policy_provider_, |
| 151 | #if USE_CHROME_KIOSK_APP |
| 152 | libcros_proxy_.get(), |
| 153 | #else |
| 154 | nullptr, |
| 155 | #endif // USE_CHROME_KIOSK_APP |
| 156 | this); |
| 157 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 158 | if (!um_state) { |
| 159 | LOG(ERROR) << "Failed to initialize the Update Manager."; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 160 | return false; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 161 | } |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 162 | update_manager_.reset( |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 163 | new chromeos_update_manager::UpdateManager( |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 164 | &clock_, base::TimeDelta::FromSeconds(5), |
| 165 | base::TimeDelta::FromHours(12), um_state)); |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 166 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 167 | // The P2P Manager depends on the Update Manager for its initialization. |
| 168 | p2p_manager_.reset(P2PManager::Construct( |
| 169 | nullptr, &clock_, update_manager_.get(), "cros_au", |
| 170 | kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays))); |
| 171 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 172 | if (!payload_state_.Initialize(this)) { |
| 173 | LOG(ERROR) << "Failed to initialize the payload state object."; |
| 174 | return false; |
| 175 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 176 | |
| 177 | // All is well. Initialization successful. |
| 178 | return true; |
| 179 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 180 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 181 | bool RealSystemState::StartUpdater() { |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 182 | // Initiate update checks. |
| 183 | update_attempter_->ScheduleUpdates(); |
| 184 | |
| 185 | // Update boot flags after 45 seconds. |
| 186 | MessageLoop::current()->PostDelayedTask( |
| 187 | FROM_HERE, |
| 188 | base::Bind(&UpdateAttempter::UpdateBootFlags, |
| 189 | base::Unretained(update_attempter_.get())), |
| 190 | base::TimeDelta::FromSeconds(45)); |
| 191 | |
| 192 | // Broadcast the update engine status on startup to ensure consistent system |
| 193 | // state on crashes. |
| 194 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 195 | &UpdateAttempter::BroadcastStatus, |
| 196 | base::Unretained(update_attempter_.get()))); |
| 197 | |
| 198 | // Run the UpdateEngineStarted() method on |update_attempter|. |
| 199 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 200 | &UpdateAttempter::UpdateEngineStarted, |
| 201 | base::Unretained(update_attempter_.get()))); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
| 205 | void RealSystemState::AddObserver(ServiceObserverInterface* observer) { |
| 206 | CHECK(update_attempter_.get()); |
| 207 | update_attempter_->AddObserver(observer); |
| 208 | } |
| 209 | |
| 210 | void RealSystemState::RemoveObserver(ServiceObserverInterface* observer) { |
| 211 | CHECK(update_attempter_.get()); |
| 212 | update_attempter_->RemoveObserver(observer); |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 215 | } // namespace chromeos_update_engine |