Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1 | // 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 Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 5 | #include "update_engine/real_system_state.h" |
| 6 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 7 | #include <base/file_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 8 | #include <base/time/time.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 9 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 10 | #include "update_engine/constants.h" |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 11 | #include "update_engine/policy_manager/state_factory.h" |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 12 | #include "update_engine/utils.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 13 | |
| 14 | namespace chromeos_update_engine { |
| 15 | |
| 16 | static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed"; |
| 17 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 18 | RealSystemState::RealSystemState() |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 19 | : device_policy_(nullptr), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 20 | connection_manager_(this), |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 21 | request_params_(this), |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 22 | p2p_manager_(), |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 23 | system_rebooted_(false) {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 24 | |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 25 | bool RealSystemState::Initialize(bool enable_gpio) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 26 | metrics_lib_.Init(); |
| 27 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 28 | if (!prefs_.Init(base::FilePath(kPrefsDirectory))) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 29 | LOG(ERROR) << "Failed to initialize preferences."; |
| 30 | return false; |
| 31 | } |
| 32 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 33 | if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 34 | LOG(ERROR) << "Failed to initialize powerwash preferences."; |
| 35 | return false; |
| 36 | } |
| 37 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 38 | 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 Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 46 | p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au", |
| 47 | kMaxP2PFilesToKeep)); |
| 48 | |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 49 | // Initialize the PolicyManager using the default State Factory. |
| 50 | policy_manager_.Init( |
| 51 | chromeos_policy_manager::DefaultStateFactory(&dbus_, &clock_)); |
| 52 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 53 | if (!payload_state_.Initialize(this)) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 54 | return false; |
| 55 | |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 56 | // 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 Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 72 | // Create the update attempter. |
| 73 | update_attempter_.reset(new UpdateAttempter(this, &dbus_)); |
| 74 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 75 | // All is well. Initialization successful. |
| 76 | return true; |
| 77 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 78 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 79 | bool 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 94 | } // namespace chromeos_update_engine |