blob: 7fca0cada9544591301969524ae2be6ef0cb10d9 [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo94c06162014-03-21 20:34:46 -07005#include "update_engine/real_system_state.h"
6
Jay Srinivasan43488792012-06-19 00:25:31 -07007#include <base/file_util.h>
8
David Zeuthen639aa362014-02-03 16:23:44 -08009#include "update_engine/constants.h"
Alex Deymo94c06162014-03-21 20:34:46 -070010#include "update_engine/policy_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070011#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070012
13namespace chromeos_update_engine {
14
Jay Srinivasan34b5d862012-07-23 11:43:22 -070015RealSystemState::RealSystemState()
Alex Vakulenko75039d72014-03-25 12:36:28 -070016 : device_policy_(nullptr),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070017 connection_manager_(this),
Gilad Arnold1f847232014-04-07 12:07:49 -070018 update_attempter_(this, &dbus_),
Chris Sosabe45bef2013-04-09 18:25:12 -070019 request_params_(this),
20 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080022bool RealSystemState::Initialize(bool enable_gpio) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023 metrics_lib_.Init();
24
Alex Vakulenko75039d72014-03-25 12:36:28 -070025 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026 LOG(ERROR) << "Failed to initialize preferences.";
27 return false;
28 }
29
Alex Vakulenko75039d72014-03-25 12:36:28 -070030 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070031 LOG(ERROR) << "Failed to initialize powerwash preferences.";
32 return false;
33 }
34
Chris Sosabe45bef2013-04-09 18:25:12 -070035 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
36 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
37 LOG(ERROR) << "Could not create reboot marker file";
38 return false;
39 }
40 system_rebooted_ = true;
41 }
42
David Zeuthen526cb582013-08-06 12:26:18 -070043 p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au",
44 kMaxP2PFilesToKeep));
45
Alex Deymo94c06162014-03-21 20:34:46 -070046 // Initialize the PolicyManager using the default State Factory.
Alex Deymo680d0222014-04-24 21:00:08 -070047 chromeos_policy_manager::State* pm_state =
48 chromeos_policy_manager::DefaultStateFactory(
49 &policy_provider_, &dbus_, this);
50 if (!pm_state) {
Gilad Arnold1f847232014-04-07 12:07:49 -070051 LOG(ERROR) << "Failed to initialize the policy manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070053 }
Alex Deymo680d0222014-04-24 21:00:08 -070054 policy_manager_.reset(
55 new chromeos_policy_manager::PolicyManager(&clock_, pm_state));
Gilad Arnold1f847232014-04-07 12:07:49 -070056
57 if (!payload_state_.Initialize(this)) {
58 LOG(ERROR) << "Failed to initialize the payload state object.";
59 return false;
60 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080061
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080062 // Initialize the GPIO handler as instructed.
63 if (enable_gpio) {
64 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
65 // time to export the devices. Also require that test mode is physically
66 // queried at most once and the result cached, for a more consistent update
67 // behavior.
68 udev_iface_.reset(new StandardUdevInterface());
69 file_descriptor_.reset(new EintrSafeFileDescriptor());
70 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
71 file_descriptor_.get(),
72 true, true));
73 } else {
74 // A no-op GPIO handler, always indicating a non-test mode.
75 gpio_handler_.reset(new NoopGpioHandler(false));
76 }
77
Gilad Arnold1f847232014-04-07 12:07:49 -070078 // Initialize the update attempter.
79 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080080
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080081 // All is well. Initialization successful.
82 return true;
83}
Jay Srinivasan43488792012-06-19 00:25:31 -070084
Jay Srinivasan43488792012-06-19 00:25:31 -070085} // namespace chromeos_update_engine