blob: 936e81999fae687a0513230d87ff8de6232b1dc5 [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>
Alex Vakulenko75039d72014-03-25 12:36:28 -07008#include <base/time/time.h>
Jay Srinivasan43488792012-06-19 00:25:31 -07009
David Zeuthen639aa362014-02-03 16:23:44 -080010#include "update_engine/constants.h"
Alex Deymo94c06162014-03-21 20:34:46 -070011#include "update_engine/policy_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070012#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070013
14namespace chromeos_update_engine {
15
16static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
17
Jay Srinivasan34b5d862012-07-23 11:43:22 -070018RealSystemState::RealSystemState()
Alex Vakulenko75039d72014-03-25 12:36:28 -070019 : device_policy_(nullptr),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070020 connection_manager_(this),
Chris Sosabe45bef2013-04-09 18:25:12 -070021 request_params_(this),
Alex Vakulenko75039d72014-03-25 12:36:28 -070022 p2p_manager_(),
Chris Sosabe45bef2013-04-09 18:25:12 -070023 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080025bool RealSystemState::Initialize(bool enable_gpio) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026 metrics_lib_.Init();
27
Alex Vakulenko75039d72014-03-25 12:36:28 -070028 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029 LOG(ERROR) << "Failed to initialize preferences.";
30 return false;
31 }
32
Alex Vakulenko75039d72014-03-25 12:36:28 -070033 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070034 LOG(ERROR) << "Failed to initialize powerwash preferences.";
35 return false;
36 }
37
Chris Sosabe45bef2013-04-09 18:25:12 -070038 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
39 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
40 LOG(ERROR) << "Could not create reboot marker file";
41 return false;
42 }
43 system_rebooted_ = true;
44 }
45
David Zeuthen526cb582013-08-06 12:26:18 -070046 p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au",
47 kMaxP2PFilesToKeep));
48
Alex Deymo94c06162014-03-21 20:34:46 -070049 // Initialize the PolicyManager using the default State Factory.
50 policy_manager_.Init(
51 chromeos_policy_manager::DefaultStateFactory(&dbus_, &clock_));
52
Jay Srinivasan19409b72013-04-12 19:23:36 -070053 if (!payload_state_.Initialize(this))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080054 return false;
55
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080056 // Initialize the GPIO handler as instructed.
57 if (enable_gpio) {
58 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
59 // time to export the devices. Also require that test mode is physically
60 // queried at most once and the result cached, for a more consistent update
61 // behavior.
62 udev_iface_.reset(new StandardUdevInterface());
63 file_descriptor_.reset(new EintrSafeFileDescriptor());
64 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
65 file_descriptor_.get(),
66 true, true));
67 } else {
68 // A no-op GPIO handler, always indicating a non-test mode.
69 gpio_handler_.reset(new NoopGpioHandler(false));
70 }
71
Jay Srinivasan55f50c22013-01-10 19:24:35 -080072 // Create the update attempter.
73 update_attempter_.reset(new UpdateAttempter(this, &dbus_));
74
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080075 // All is well. Initialization successful.
76 return true;
77}
Jay Srinivasan43488792012-06-19 00:25:31 -070078
David Zeuthen639aa362014-02-03 16:23:44 -080079bool RealSystemState::IsOOBEComplete(base::Time* out_time_of_oobe) {
80 struct stat statbuf;
81 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
82 if (errno != ENOENT) {
83 PLOG(ERROR) << "Error getting information about "
84 << kOOBECompletedMarker;
85 }
86 return false;
87 }
88
89 if (out_time_of_oobe != NULL)
90 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
91 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -070092}
93
Jay Srinivasan43488792012-06-19 00:25:31 -070094} // namespace chromeos_update_engine