blob: b06830f6509612f442ec64d8469e8f27f8abe083 [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.
Gilad Arnold1f847232014-04-07 12:07:49 -070047 if (!policy_manager_.Init(
48 chromeos_policy_manager::DefaultStateFactory(&dbus_, this))) {
49 LOG(ERROR) << "Failed to initialize the policy manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070051 }
52
53 if (!payload_state_.Initialize(this)) {
54 LOG(ERROR) << "Failed to initialize the payload state object.";
55 return false;
56 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080057
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080058 // Initialize the GPIO handler as instructed.
59 if (enable_gpio) {
60 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
61 // time to export the devices. Also require that test mode is physically
62 // queried at most once and the result cached, for a more consistent update
63 // behavior.
64 udev_iface_.reset(new StandardUdevInterface());
65 file_descriptor_.reset(new EintrSafeFileDescriptor());
66 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
67 file_descriptor_.get(),
68 true, true));
69 } else {
70 // A no-op GPIO handler, always indicating a non-test mode.
71 gpio_handler_.reset(new NoopGpioHandler(false));
72 }
73
Gilad Arnold1f847232014-04-07 12:07:49 -070074 // Initialize the update attempter.
75 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080076
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080077 // All is well. Initialization successful.
78 return true;
79}
Jay Srinivasan43488792012-06-19 00:25:31 -070080
Jay Srinivasan43488792012-06-19 00:25:31 -070081} // namespace chromeos_update_engine