blob: e675aad11e86402bfbe9317213cdc73961c3abd9 [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
5#include <base/file_util.h>
David Zeuthen639aa362014-02-03 16:23:44 -08006#include <base/time.h>
Jay Srinivasan43488792012-06-19 00:25:31 -07007
David Zeuthen639aa362014-02-03 16:23:44 -08008#include "update_engine/constants.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -08009#include "update_engine/real_system_state.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070010#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070011
12namespace chromeos_update_engine {
13
14static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
15
Jay Srinivasan34b5d862012-07-23 11:43:22 -070016RealSystemState::RealSystemState()
17 : device_policy_(NULL),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070018 connection_manager_(this),
Chris Sosabe45bef2013-04-09 18:25:12 -070019 request_params_(this),
David Zeuthen526cb582013-08-06 12:26:18 -070020 p2p_manager_(NULL),
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
26 if (!prefs_.Init(FilePath(kPrefsDirectory))) {
27 LOG(ERROR) << "Failed to initialize preferences.";
28 return false;
29 }
30
Chris Sosaaa18e162013-06-20 13:20:30 -070031 if (!powerwash_safe_prefs_.Init(FilePath(kPowerwashSafePrefsDir))) {
32 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
Jay Srinivasan19409b72013-04-12 19:23:36 -070047 if (!payload_state_.Initialize(this))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080048 return false;
49
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080050 // Initialize the GPIO handler as instructed.
51 if (enable_gpio) {
52 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
53 // time to export the devices. Also require that test mode is physically
54 // queried at most once and the result cached, for a more consistent update
55 // behavior.
56 udev_iface_.reset(new StandardUdevInterface());
57 file_descriptor_.reset(new EintrSafeFileDescriptor());
58 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
59 file_descriptor_.get(),
60 true, true));
61 } else {
62 // A no-op GPIO handler, always indicating a non-test mode.
63 gpio_handler_.reset(new NoopGpioHandler(false));
64 }
65
Jay Srinivasan55f50c22013-01-10 19:24:35 -080066 // Create the update attempter.
67 update_attempter_.reset(new UpdateAttempter(this, &dbus_));
68
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080069 // All is well. Initialization successful.
70 return true;
71}
Jay Srinivasan43488792012-06-19 00:25:31 -070072
David Zeuthen639aa362014-02-03 16:23:44 -080073bool RealSystemState::IsOOBEComplete(base::Time* out_time_of_oobe) {
74 struct stat statbuf;
75 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
76 if (errno != ENOENT) {
77 PLOG(ERROR) << "Error getting information about "
78 << kOOBECompletedMarker;
79 }
80 return false;
81 }
82
83 if (out_time_of_oobe != NULL)
84 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
85 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -070086}
87
Jay Srinivasan43488792012-06-19 00:25:31 -070088} // namespace chromeos_update_engine