blob: 34ac5ed55bdc59b408fee8129a73cd11434f204d [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 Chan06c76a42014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070020#include <base/time/time.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070021
Alex Deymob17327c2015-09-04 10:29:00 -070022#include "update_engine/boot_control.h"
David Zeuthen639aa362014-02-03 16:23:44 -080023#include "update_engine/constants.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070024#include "update_engine/hardware.h"
Alex Deymo63784a52014-05-28 10:46:14 -070025#include "update_engine/update_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070026#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070027
28namespace chromeos_update_engine {
29
Alex Deymo30534502015-07-20 15:06:33 -070030RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
Alex Deymod6deb1d2015-08-28 15:54:37 -070031 : debugd_proxy_(bus),
32 power_manager_proxy_(bus),
33 session_manager_proxy_(bus),
Alex Deymo30534502015-07-20 15:06:33 -070034 shill_proxy_(bus),
35 libcros_proxy_(bus) {
36}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080037
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070038bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080039 metrics_lib_.Init();
40
Alex Deymob17327c2015-09-04 10:29:00 -070041 boot_control_ = boot_control::CreateBootControl();
42 if (!boot_control_)
43 return false;
Alex Deymo763e7db2015-08-27 21:08:08 -070044
Alex Deymo40d86b22015-09-03 22:27:10 -070045 hardware_ = hardware::CreateHardware();
46 if (!hardware_) {
47 LOG(ERROR) << "Error intializing the HardwareInterface.";
48 return false;
49 }
50
Alex Deymo30534502015-07-20 15:06:33 -070051 if (!shill_proxy_.Init()) {
52 LOG(ERROR) << "Failed to initialize shill proxy.";
53 return false;
54 }
55
Alex Deymodd132f32015-09-14 19:12:07 -070056 // Initialize standard and powerwash-safe prefs.
57 base::FilePath non_volatile_path;
58 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
59 // available.
60 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
61 LOG(ERROR) << "Failed to get a non-volatile directory.";
62 return false;
63 }
64 Prefs* prefs;
65 prefs_.reset(prefs = new Prefs());
66 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080067 LOG(ERROR) << "Failed to initialize preferences.";
68 return false;
69 }
70
Alex Deymodd132f32015-09-14 19:12:07 -070071 base::FilePath powerwash_safe_path;
72 if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
73 // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
74 // directory, or disable powerwash feature.
75 powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
76 LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
77 }
78 powerwash_safe_prefs_.reset(prefs = new Prefs());
79 if (!prefs->Init(
80 powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070081 LOG(ERROR) << "Failed to initialize powerwash preferences.";
82 return false;
83 }
84
Alex Deymodd132f32015-09-14 19:12:07 -070085 // Check the system rebooted marker file.
86 std::string boot_id;
87 if (utils::GetBootId(&boot_id)) {
88 std::string prev_boot_id;
89 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
90 prev_boot_id != boot_id);
91 prefs_->SetString(kPrefsBootId, boot_id);
92 } else {
93 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -070094 system_rebooted_ = true;
95 }
96
Alex Deymo63784a52014-05-28 10:46:14 -070097 // Initialize the Update Manager using the default state factory.
98 chromeos_update_manager::State* um_state =
99 chromeos_update_manager::DefaultStateFactory(
Alex Deymo30534502015-07-20 15:06:33 -0700100 &policy_provider_, &shill_proxy_, &session_manager_proxy_, this);
Alex Deymo63784a52014-05-28 10:46:14 -0700101 if (!um_state) {
102 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800103 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700104 }
Alex Deymo63784a52014-05-28 10:46:14 -0700105 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700106 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700107 &clock_, base::TimeDelta::FromSeconds(5),
108 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700109
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700110 // The P2P Manager depends on the Update Manager for its initialization.
111 p2p_manager_.reset(P2PManager::Construct(
112 nullptr, &clock_, update_manager_.get(), "cros_au",
113 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
114
Gilad Arnold1f847232014-04-07 12:07:49 -0700115 if (!payload_state_.Initialize(this)) {
116 LOG(ERROR) << "Failed to initialize the payload state object.";
117 return false;
118 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800119
Gilad Arnold1f847232014-04-07 12:07:49 -0700120 // Initialize the update attempter.
121 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800122
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800123 // All is well. Initialization successful.
124 return true;
125}
Jay Srinivasan43488792012-06-19 00:25:31 -0700126
Jay Srinivasan43488792012-06-19 00:25:31 -0700127} // namespace chromeos_update_engine