blob: 9384228559bb72add8364c9ffe833f3b665c2179 [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),
Alex Deymo41a75a72014-04-15 15:36:22 -070020 policy_manager_(&clock_),
Chris Sosabe45bef2013-04-09 18:25:12 -070021 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080022
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080023bool RealSystemState::Initialize(bool enable_gpio) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024 metrics_lib_.Init();
25
Alex Vakulenko75039d72014-03-25 12:36:28 -070026 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080027 LOG(ERROR) << "Failed to initialize preferences.";
28 return false;
29 }
30
Alex Vakulenko75039d72014-03-25 12:36:28 -070031 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070032 LOG(ERROR) << "Failed to initialize powerwash preferences.";
33 return false;
34 }
35
Chris Sosabe45bef2013-04-09 18:25:12 -070036 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
37 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
38 LOG(ERROR) << "Could not create reboot marker file";
39 return false;
40 }
41 system_rebooted_ = true;
42 }
43
David Zeuthen526cb582013-08-06 12:26:18 -070044 p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au",
45 kMaxP2PFilesToKeep));
46
Alex Deymo94c06162014-03-21 20:34:46 -070047 // Initialize the PolicyManager using the default State Factory.
Alex Deymoc83baf62014-04-02 17:43:35 -070048 if (!policy_manager_.Init(chromeos_policy_manager::DefaultStateFactory(
49 &policy_provider_, &dbus_, this))) {
Gilad Arnold1f847232014-04-07 12:07:49 -070050 LOG(ERROR) << "Failed to initialize the policy manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080051 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070052 }
53
54 if (!payload_state_.Initialize(this)) {
55 LOG(ERROR) << "Failed to initialize the payload state object.";
56 return false;
57 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080058
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080059 // Initialize the GPIO handler as instructed.
60 if (enable_gpio) {
61 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
62 // time to export the devices. Also require that test mode is physically
63 // queried at most once and the result cached, for a more consistent update
64 // behavior.
65 udev_iface_.reset(new StandardUdevInterface());
66 file_descriptor_.reset(new EintrSafeFileDescriptor());
67 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
68 file_descriptor_.get(),
69 true, true));
70 } else {
71 // A no-op GPIO handler, always indicating a non-test mode.
72 gpio_handler_.reset(new NoopGpioHandler(false));
73 }
74
Gilad Arnold1f847232014-04-07 12:07:49 -070075 // Initialize the update attempter.
76 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080077
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078 // All is well. Initialization successful.
79 return true;
80}
Jay Srinivasan43488792012-06-19 00:25:31 -070081
Jay Srinivasan43488792012-06-19 00:25:31 -070082} // namespace chromeos_update_engine