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 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 22 | #include <base/time/time.h> |
David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 23 | #include <brillo/make_unique_ptr.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/boot_control.h" |
| 26 | #include "update_engine/common/boot_control_stub.h" |
| 27 | #include "update_engine/common/constants.h" |
| 28 | #include "update_engine/common/hardware.h" |
| 29 | #include "update_engine/common/utils.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | #include "update_engine/update_manager/state_factory.h" |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 31 | #include "update_engine/weave_service_factory.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 32 | |
| 33 | namespace chromeos_update_engine { |
| 34 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 35 | RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus) |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame^] | 36 | : debugd_proxy_(bus), |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 37 | power_manager_proxy_(bus), |
| 38 | session_manager_proxy_(bus), |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 39 | shill_proxy_(bus), |
| 40 | libcros_proxy_(bus) { |
| 41 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 42 | |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 43 | bool RealSystemState::Initialize() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 44 | metrics_lib_.Init(); |
| 45 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 46 | boot_control_ = boot_control::CreateBootControl(); |
David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 47 | if (!boot_control_) { |
| 48 | LOG(WARNING) << "Unable to create BootControl instance, using stub " |
| 49 | << "instead. All update attempts will fail."; |
| 50 | boot_control_ = brillo::make_unique_ptr(new BootControlStub()); |
| 51 | } |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 52 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 53 | hardware_ = hardware::CreateHardware(); |
| 54 | if (!hardware_) { |
| 55 | LOG(ERROR) << "Error intializing the HardwareInterface."; |
| 56 | return false; |
| 57 | } |
| 58 | |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 59 | LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode."; |
| 60 | LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build."; |
| 61 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 62 | if (!shill_proxy_.Init()) { |
| 63 | LOG(ERROR) << "Failed to initialize shill proxy."; |
| 64 | return false; |
| 65 | } |
| 66 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 67 | // Initialize standard and powerwash-safe prefs. |
| 68 | base::FilePath non_volatile_path; |
| 69 | // TODO(deymo): Fall back to in-memory prefs if there's no physical directory |
| 70 | // available. |
| 71 | if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) { |
| 72 | LOG(ERROR) << "Failed to get a non-volatile directory."; |
| 73 | return false; |
| 74 | } |
| 75 | Prefs* prefs; |
| 76 | prefs_.reset(prefs = new Prefs()); |
| 77 | if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 78 | LOG(ERROR) << "Failed to initialize preferences."; |
| 79 | return false; |
| 80 | } |
| 81 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 82 | base::FilePath powerwash_safe_path; |
| 83 | if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) { |
| 84 | // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe |
| 85 | // directory, or disable powerwash feature. |
| 86 | powerwash_safe_path = non_volatile_path.Append("powerwash-safe"); |
| 87 | LOG(WARNING) << "No powerwash-safe directory, using non-volatile one."; |
| 88 | } |
| 89 | powerwash_safe_prefs_.reset(prefs = new Prefs()); |
| 90 | if (!prefs->Init( |
| 91 | powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 92 | LOG(ERROR) << "Failed to initialize powerwash preferences."; |
| 93 | return false; |
| 94 | } |
| 95 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 96 | // Check the system rebooted marker file. |
| 97 | std::string boot_id; |
| 98 | if (utils::GetBootId(&boot_id)) { |
| 99 | std::string prev_boot_id; |
| 100 | system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) || |
| 101 | prev_boot_id != boot_id); |
| 102 | prefs_->SetString(kPrefsBootId, boot_id); |
| 103 | } else { |
| 104 | LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted."; |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 105 | system_rebooted_ = true; |
| 106 | } |
| 107 | |
Alex Deymo | 5378f5e | 2015-11-10 15:02:50 -0800 | [diff] [blame] | 108 | // Initialize the OmahaRequestParams with the default settings. These settings |
| 109 | // will be re-initialized before every request using the actual request |
| 110 | // options. This initialization here pre-loads current channel and version, so |
| 111 | // the DBus service can access it. |
| 112 | if (!request_params_.Init("", "", false)) { |
| 113 | LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some " |
| 114 | "features might not work properly."; |
| 115 | } |
| 116 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 117 | certificate_checker_.reset( |
| 118 | new CertificateChecker(prefs_.get(), &openssl_wrapper_)); |
| 119 | certificate_checker_->Init(); |
| 120 | |
| 121 | // Initialize the UpdateAttempter before the UpdateManager. |
| 122 | update_attempter_.reset( |
| 123 | new UpdateAttempter(this, certificate_checker_.get(), &libcros_proxy_, |
| 124 | &debugd_proxy_)); |
| 125 | update_attempter_->Init(); |
| 126 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame^] | 127 | weave_service_ = ConstructWeaveService(update_attempter_.get()); |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 128 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 129 | // Initialize the Update Manager using the default state factory. |
| 130 | chromeos_update_manager::State* um_state = |
| 131 | chromeos_update_manager::DefaultStateFactory( |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 132 | &policy_provider_, &shill_proxy_, &session_manager_proxy_, this); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 133 | if (!um_state) { |
| 134 | LOG(ERROR) << "Failed to initialize the Update Manager."; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 135 | return false; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 136 | } |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 137 | update_manager_.reset( |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 138 | new chromeos_update_manager::UpdateManager( |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 139 | &clock_, base::TimeDelta::FromSeconds(5), |
| 140 | base::TimeDelta::FromHours(12), um_state)); |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 141 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 142 | // The P2P Manager depends on the Update Manager for its initialization. |
| 143 | p2p_manager_.reset(P2PManager::Construct( |
| 144 | nullptr, &clock_, update_manager_.get(), "cros_au", |
| 145 | kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays))); |
| 146 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 147 | if (!payload_state_.Initialize(this)) { |
| 148 | LOG(ERROR) << "Failed to initialize the payload state object."; |
| 149 | return false; |
| 150 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 151 | |
| 152 | // All is well. Initialization successful. |
| 153 | return true; |
| 154 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 155 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 156 | } // namespace chromeos_update_engine |