Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_ATTEMPTER_H_ |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 7 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 8 | #include <time.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 9 | |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 10 | #include <memory> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 13 | #include <utility> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 14 | |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 15 | #include <base/bind.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 16 | #include <base/time/time.h> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 17 | #include <glib.h> |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 18 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 19 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 20 | #include "update_engine/action_processor.h" |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 21 | #include "update_engine/chrome_browser_proxy_resolver.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 22 | #include "update_engine/download_action.h" |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 23 | #include "update_engine/filesystem_copier_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 24 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 25 | #include "update_engine/omaha_response_handler_action.h" |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 26 | #include "update_engine/proxy_resolver.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 27 | #include "update_engine/system_state.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 28 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 29 | class MetricsLibraryInterface; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 30 | struct UpdateEngineService; |
| 31 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 32 | namespace policy { |
| 33 | class PolicyProvider; |
| 34 | } |
| 35 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 36 | namespace chromeos_update_engine { |
| 37 | |
Daniel Erat | 65f1da0 | 2014-06-27 22:05:38 -0700 | [diff] [blame] | 38 | class DBusWrapperInterface; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 39 | class UpdateCheckScheduler; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 40 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 41 | enum UpdateStatus { |
| 42 | UPDATE_STATUS_IDLE = 0, |
| 43 | UPDATE_STATUS_CHECKING_FOR_UPDATE, |
| 44 | UPDATE_STATUS_UPDATE_AVAILABLE, |
| 45 | UPDATE_STATUS_DOWNLOADING, |
| 46 | UPDATE_STATUS_VERIFYING, |
| 47 | UPDATE_STATUS_FINALIZING, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 48 | UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 49 | UPDATE_STATUS_REPORTING_ERROR_EVENT, |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 50 | UPDATE_STATUS_ATTEMPTING_ROLLBACK |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | const char* UpdateStatusToString(UpdateStatus status); |
| 54 | |
| 55 | class UpdateAttempter : public ActionProcessorDelegate, |
| 56 | public DownloadActionDelegate { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 57 | public: |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 58 | static const int kMaxDeltaUpdateFailures; |
| 59 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 60 | UpdateAttempter(SystemState* system_state, |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 61 | DBusWrapperInterface* dbus_iface); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 62 | virtual ~UpdateAttempter(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 63 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 64 | // Further initialization to be done post construction. |
| 65 | void Init(); |
| 66 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 67 | // Checks for update and, if a newer version is available, attempts to update |
| 68 | // the system. Non-empty |in_app_version| or |in_update_url| prevents |
| 69 | // automatic detection of the parameter. If |obey_proxies| is true, the |
| 70 | // update will likely respect Chrome's proxy setting. For security reasons, we |
Gilad Arnold | d470708 | 2014-06-24 12:29:59 -0700 | [diff] [blame] | 71 | // may still not honor them. |interactive| should be true if this was called |
| 72 | // from the user (ie dbus). |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 73 | virtual void Update(const std::string& app_version, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 74 | const std::string& omaha_url, |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 75 | bool obey_proxies, |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 76 | bool interactive); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 77 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 78 | // ActionProcessorDelegate methods: |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 79 | void ProcessingDone(const ActionProcessor* processor, ErrorCode code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 80 | void ProcessingStopped(const ActionProcessor* processor); |
| 81 | void ActionCompleted(ActionProcessor* processor, |
| 82 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 83 | ErrorCode code); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 84 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 85 | // Stop updating. An attempt will be made to record status to the disk |
| 86 | // so that updates can be resumed later. |
| 87 | void Terminate(); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 88 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 89 | // Try to resume from a previously Terminate()d update. |
| 90 | void ResumeUpdating(); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 91 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 92 | // Resets the current state to UPDATE_STATUS_IDLE. |
| 93 | // Used by update_engine_client for restarting a new update without |
| 94 | // having to reboot once the previous update has reached |
| 95 | // UPDATE_STATUS_UPDATED_NEED_REBOOT state. This is used only |
| 96 | // for testing purposes. |
| 97 | bool ResetStatus(); |
| 98 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 99 | // Returns the current status in the out params. Returns true on success. |
Gilad Arnold | 76b2b48 | 2014-04-01 13:32:43 -0700 | [diff] [blame] | 100 | virtual bool GetStatus(int64_t* last_checked_time, |
| 101 | double* progress, |
| 102 | std::string* current_operation, |
| 103 | std::string* new_version, |
| 104 | int64_t* new_size); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 105 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 106 | // Runs chromeos-setgoodkernel, whose responsibility it is to mark the |
| 107 | // currently booted partition has high priority/permanent/etc. The execution |
| 108 | // is asynchronous. On completion, the action processor may be started |
| 109 | // depending on the |start_action_processor_| field. Note that every update |
| 110 | // attempt goes through this method. |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 111 | void UpdateBootFlags(); |
| 112 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 113 | // Subprocess::Exec callback. |
| 114 | void CompleteUpdateBootFlags(int return_code); |
| 115 | static void StaticCompleteUpdateBootFlags(int return_code, |
| 116 | const std::string& output, |
| 117 | void* p); |
| 118 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 119 | UpdateStatus status() const { return status_; } |
| 120 | |
| 121 | int http_response_code() const { return http_response_code_; } |
| 122 | void set_http_response_code(int code) { http_response_code_ = code; } |
| 123 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 124 | void set_dbus_service(struct UpdateEngineService* dbus_service) { |
| 125 | dbus_service_ = dbus_service; |
| 126 | } |
| 127 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 128 | UpdateCheckScheduler* update_check_scheduler() const { |
| 129 | return update_check_scheduler_; |
| 130 | } |
| 131 | void set_update_check_scheduler(UpdateCheckScheduler* scheduler) { |
| 132 | update_check_scheduler_ = scheduler; |
| 133 | } |
| 134 | |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 135 | // This is the internal entry point for going through an |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 136 | // update. If the current status is idle invokes Update. |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 137 | // This is called by the DBus implementation. |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 138 | void CheckForUpdate(const std::string& app_version, |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 139 | const std::string& omaha_url, |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 140 | bool is_interactive); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 141 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 142 | // This is the internal entry point for going through a rollback. This will |
| 143 | // attempt to run the postinstall on the non-active partition and set it as |
| 144 | // the partition to boot from. If |powerwash| is True, perform a powerwash |
Chris Sosa | 44b9b7e | 2014-04-02 13:53:46 -0700 | [diff] [blame] | 145 | // as part of rollback. Returns True on success. |
| 146 | bool Rollback(bool powerwash); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 147 | |
Chris Sosa | 44b9b7e | 2014-04-02 13:53:46 -0700 | [diff] [blame] | 148 | // This is the internal entry point for checking if we can rollback. |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 149 | bool CanRollback() const; |
| 150 | |
Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 151 | // This is the internal entry point for getting a rollback partition name, |
| 152 | // if one exists. It returns the bootable rollback kernel device partition |
| 153 | // name or empty string if none is available. |
| 154 | std::string GetRollbackPartition() const; |
| 155 | |
| 156 | // Returns a list of available kernel partitions along with information |
| 157 | // whether it is possible to boot from it. |
| 158 | std::vector<std::pair<std::string, bool>> GetKernelDevices() const; |
| 159 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 160 | // Initiates a reboot if the current state is |
| 161 | // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise. |
| 162 | bool RebootIfNeeded(); |
| 163 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 164 | // DownloadActionDelegate methods |
| 165 | void SetDownloadStatus(bool active); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 166 | void BytesReceived(uint64_t bytes_received, uint64_t total); |
| 167 | |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 168 | // Broadcasts the current status over D-Bus. |
| 169 | void BroadcastStatus(); |
| 170 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 171 | // Returns the special flags to be added to ErrorCode values based on the |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 172 | // parameters used in the current update attempt. |
| 173 | uint32_t GetErrorCodeFlags(); |
| 174 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 175 | // Returns true if we should cancel the current download attempt based on the |
| 176 | // current state of the system, in which case |cancel_reason| indicates the |
| 177 | // reason for the cancellation. False otherwise, in which case |
| 178 | // |cancel_reason| is untouched. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 179 | bool ShouldCancel(ErrorCode* cancel_reason); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 180 | |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 181 | // Called at update_engine startup to do various house-keeping. |
| 182 | void UpdateEngineStarted(); |
| 183 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 184 | // Reloads the device policy from libchromeos. Note: This method doesn't |
| 185 | // cause a real-time policy fetch from the policy server. It just reloads the |
| 186 | // latest value that libchromeos has cached. libchromeos fetches the policies |
| 187 | // from the server asynchronously at its own frequency. |
| 188 | void RefreshDevicePolicy(); |
| 189 | |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 190 | // Returns the boottime (CLOCK_BOOTTIME) recorded at the last |
| 191 | // successful update. Returns false if the device has not updated. |
Gilad Arnold | 76b2b48 | 2014-04-01 13:32:43 -0700 | [diff] [blame] | 192 | virtual bool GetBootTimeAtUpdate(base::Time *out_boot_time); |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 193 | |
Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 194 | // Returns a version OS version that was being used before the last reboot, |
| 195 | // and if that reboot happended to be into an update (current version). |
| 196 | // This will return an empty string otherwise. |
| 197 | std::string const& GetPrevVersion() const { return prev_version_; } |
| 198 | |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 199 | // Returns the number of consecutive failed update checks. |
Gilad Arnold | a6dab94 | 2014-04-25 11:46:03 -0700 | [diff] [blame] | 200 | virtual unsigned int consecutive_failed_update_checks() const { |
| 201 | return consecutive_failed_update_checks_; |
| 202 | } |
| 203 | |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 204 | // Returns the poll interval dictated by Omaha, if provided; zero otherwise. |
| 205 | virtual unsigned int server_dictated_poll_interval() const { |
| 206 | return server_dictated_poll_interval_; |
| 207 | } |
| 208 | |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 209 | // Sets a callback to be used when either an interactive update request is |
| 210 | // received (true) or cleared by an update attempt (false). Takes ownership of |
| 211 | // the callback object. A null value disables callback on these events. Note |
| 212 | // that only one callback can be set, so effectively at most one client can be |
| 213 | // notified. |
| 214 | virtual void set_interactive_update_pending_callback( |
| 215 | base::Callback<void(bool)>* callback) { // NOLINT(readability/function) |
| 216 | interactive_update_pending_callback_.reset(callback); |
| 217 | } |
| 218 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 219 | private: |
Gilad Arnold | 28e2f39 | 2012-02-09 14:36:46 -0800 | [diff] [blame] | 220 | // Update server URL for automated lab test. |
| 221 | static const char* const kTestUpdateUrl; |
| 222 | |
Gilad Arnold | 70e476e | 2013-07-30 16:01:13 -0700 | [diff] [blame] | 223 | // Special ctor + friend declarations for testing purposes. |
| 224 | UpdateAttempter(SystemState* system_state, |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 225 | DBusWrapperInterface* dbus_iface, |
Gilad Arnold | 70e476e | 2013-07-30 16:01:13 -0700 | [diff] [blame] | 226 | const std::string& update_completed_marker); |
| 227 | |
| 228 | friend class UpdateAttempterUnderTest; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 229 | friend class UpdateAttempterTest; |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 230 | FRIEND_TEST(UpdateAttempterTest, ActionCompletedDownloadTest); |
| 231 | FRIEND_TEST(UpdateAttempterTest, ActionCompletedErrorTest); |
| 232 | FRIEND_TEST(UpdateAttempterTest, ActionCompletedOmahaRequestTest); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 233 | FRIEND_TEST(UpdateAttempterTest, CreatePendingErrorEventTest); |
| 234 | FRIEND_TEST(UpdateAttempterTest, CreatePendingErrorEventResumedTest); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 235 | FRIEND_TEST(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest); |
| 236 | FRIEND_TEST(UpdateAttempterTest, MarkDeltaUpdateFailureTest); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 237 | FRIEND_TEST(UpdateAttempterTest, ReadTrackFromPolicy); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 238 | FRIEND_TEST(UpdateAttempterTest, PingOmahaTest); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 239 | FRIEND_TEST(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest); |
| 240 | FRIEND_TEST(UpdateAttempterTest, ScheduleErrorEventActionTest); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 241 | FRIEND_TEST(UpdateAttempterTest, UpdateTest); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 242 | FRIEND_TEST(UpdateAttempterTest, ReportDailyMetrics); |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 243 | FRIEND_TEST(UpdateAttempterTest, BootTimeInUpdateMarkerFile); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 244 | |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 245 | // Checks if it's more than 24 hours since daily metrics were last |
| 246 | // reported and, if so, reports daily metrics. Returns |true| if |
| 247 | // metrics were reported, |false| otherwise. |
| 248 | bool CheckAndReportDailyMetrics(); |
| 249 | |
| 250 | // Calculates and reports the age of the currently running OS. This |
| 251 | // is defined as the age of the /etc/lsb-release file. |
| 252 | void ReportOSAge(); |
| 253 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 254 | // Sets the status to the given status and notifies a status update over dbus. |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 255 | void SetStatusAndNotify(UpdateStatus status); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 256 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 257 | // Sets up the download parameters after receiving the update check response. |
| 258 | void SetupDownload(); |
| 259 | |
| 260 | // Creates an error event object in |error_event_| to be included in an |
| 261 | // OmahaRequestAction once the current action processor is done. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 262 | void CreatePendingErrorEvent(AbstractAction* action, ErrorCode code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 263 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 264 | // If there's a pending error event allocated in |error_event_|, schedules an |
| 265 | // OmahaRequestAction with that event in the current processor, clears the |
| 266 | // pending event, updates the status and returns true. Returns false |
| 267 | // otherwise. |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 268 | bool ScheduleErrorEventAction(); |
| 269 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 270 | // Sets the cpu shares to |shares| and updates |shares_| if the new |
| 271 | // |shares| is different than the current |shares_|, otherwise simply |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 272 | // returns. |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 273 | void SetCpuShares(utils::CpuShares shares); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 274 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 275 | // Sets the cpu shares to low and sets up timeout events to increase it. |
| 276 | void SetupCpuSharesManagement(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 277 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 278 | // Resets the cpu shares to normal and destroys any scheduled timeout |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 279 | // sources. |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 280 | void CleanupCpuSharesManagement(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 281 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 282 | // The cpu shares timeout source callback sets the current cpu shares to |
Darin Petkov | f622ef7 | 2010-10-26 13:49:24 -0700 | [diff] [blame] | 283 | // normal. Returns false so that GLib destroys the timeout source. |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 284 | static gboolean StaticManageCpuSharesCallback(gpointer data); |
| 285 | bool ManageCpuSharesCallback(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 286 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 287 | // Callback to start the action processor. |
| 288 | static gboolean StaticStartProcessing(gpointer data); |
| 289 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 290 | // Schedules an event loop callback to start the action processor. This is |
| 291 | // scheduled asynchronously to unblock the event loop. |
| 292 | void ScheduleProcessingStart(); |
| 293 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 294 | // Checks if a full update is needed and forces it by updating the Omaha |
| 295 | // request params. |
| 296 | void DisableDeltaUpdateIfNeeded(); |
| 297 | |
| 298 | // If this was a delta update attempt that failed, count it so that a full |
| 299 | // update can be tried when needed. |
| 300 | void MarkDeltaUpdateFailure(); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 301 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 302 | ProxyResolver* GetProxyResolver() { |
| 303 | return obeying_proxies_ ? |
| 304 | reinterpret_cast<ProxyResolver*>(&chrome_proxy_resolver_) : |
| 305 | reinterpret_cast<ProxyResolver*>(&direct_proxy_resolver_); |
| 306 | } |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 307 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 308 | // Sends a ping to Omaha. |
| 309 | // This is used after an update has been applied and we're waiting for the |
| 310 | // user to reboot. This ping helps keep the number of actives count |
| 311 | // accurate in case a user takes a long time to reboot the device after an |
| 312 | // update has been applied. |
| 313 | void PingOmaha(); |
| 314 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 315 | // Helper method of Update() to calculate the update-related parameters |
| 316 | // from various sources and set the appropriate state. Please refer to |
| 317 | // Update() method for the meaning of the parametes. |
| 318 | bool CalculateUpdateParams(const std::string& app_version, |
| 319 | const std::string& omaha_url, |
| 320 | bool obey_proxies, |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 321 | bool interactive); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 322 | |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 323 | // Calculates all the scattering related parameters (such as waiting period, |
| 324 | // which type of scattering is enabled, etc.) and also updates/deletes |
| 325 | // the corresponding prefs file used in scattering. Should be called |
| 326 | // only after the device policy has been loaded and set in the system_state_. |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 327 | void CalculateScatteringParams(bool is_interactive); |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 328 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 329 | // Sets a random value for the waiting period to wait for before downloading |
| 330 | // an update, if one available. This value will be upperbounded by the |
| 331 | // scatter factor value specified from policy. |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 332 | void GenerateNewWaitingPeriod(); |
| 333 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 334 | // Helper method of Update() and Rollback() to construct the sequence of |
| 335 | // actions to be performed for the postinstall. |
| 336 | // |previous_action| is the previous action to get |
| 337 | // bonded with the install_plan that gets passed to postinstall. |
| 338 | void BuildPostInstallActions(InstallPlanAction* previous_action); |
| 339 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 340 | // Helper method of Update() to construct the sequence of actions to |
| 341 | // be performed for an update check. Please refer to |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 342 | // Update() method for the meaning of the parameters. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 343 | void BuildUpdateActions(bool interactive); |
| 344 | |
| 345 | // Decrements the count in the kUpdateCheckCountFilePath. |
| 346 | // Returns True if successfully decremented, false otherwise. |
| 347 | bool DecrementUpdateCheckCount(); |
| 348 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 349 | // Starts p2p and performs housekeeping. Returns true only if p2p is |
| 350 | // running and housekeeping was done. |
| 351 | bool StartP2PAndPerformHousekeeping(); |
| 352 | |
| 353 | // Calculates whether peer-to-peer should be used. Sets the |
| 354 | // |use_p2p_to_download_| and |use_p2p_to_share_| parameters |
| 355 | // on the |omaha_request_params_| object. |
| 356 | void CalculateP2PParams(bool interactive); |
| 357 | |
| 358 | // Starts P2P if it's enabled and there are files to actually share. |
| 359 | // Called only at program startup. Returns true only if p2p was |
| 360 | // started and housekeeping was performed. |
| 361 | bool StartP2PAtStartup(); |
| 362 | |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 363 | // Writes to the processing completed marker. Does nothing if |
| 364 | // |update_completed_marker_| is empty. |
| 365 | void WriteUpdateCompletedMarker(); |
| 366 | |
Daniel Erat | 65f1da0 | 2014-06-27 22:05:38 -0700 | [diff] [blame] | 367 | // Sends a D-Bus message to the Chrome OS power manager asking it to reboot |
| 368 | // the system. Returns true on success. |
| 369 | bool RequestPowerManagerReboot(); |
| 370 | |
| 371 | // Reboots the system directly by calling /sbin/shutdown. Returns true on |
| 372 | // success. |
| 373 | bool RebootDirectly(); |
| 374 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 375 | // Last status notification timestamp used for throttling. Use monotonic |
| 376 | // TimeTicks to ensure that notifications are sent even if the system clock is |
| 377 | // set back in the middle of an update. |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 378 | base::TimeTicks last_notify_time_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 379 | |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 380 | std::vector<std::shared_ptr<AbstractAction> > actions_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 381 | scoped_ptr<ActionProcessor> processor_; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 382 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 383 | // External state of the system outside the update_engine process |
| 384 | // carved out separately to mock out easily in unit tests. |
| 385 | SystemState* system_state_; |
| 386 | |
Daniel Erat | 65f1da0 | 2014-06-27 22:05:38 -0700 | [diff] [blame] | 387 | // Interface for getting D-Bus connections. |
| 388 | DBusWrapperInterface* dbus_iface_ = nullptr; |
| 389 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 390 | // If non-null, this UpdateAttempter will send status updates over this |
| 391 | // dbus service. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 392 | UpdateEngineService* dbus_service_ = nullptr; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 393 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 394 | // Pointer to the OmahaResponseHandlerAction in the actions_ vector. |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 395 | std::shared_ptr<OmahaResponseHandlerAction> response_handler_action_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 396 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 397 | // Pointer to the DownloadAction in the actions_ vector. |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 398 | std::shared_ptr<DownloadAction> download_action_; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 399 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 400 | // Pointer to the preferences store interface. This is just a cached |
| 401 | // copy of system_state->prefs() because it's used in many methods and |
| 402 | // is convenient this way. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 403 | PrefsInterface* prefs_ = nullptr; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 404 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 405 | // The current UpdateCheckScheduler to notify of state transitions. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 406 | UpdateCheckScheduler* update_check_scheduler_ = nullptr; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 407 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 408 | // Pending error event, if any. |
| 409 | scoped_ptr<OmahaEvent> error_event_; |
| 410 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 411 | // If we should request a reboot even tho we failed the update |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 412 | bool fake_update_success_ = false; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 413 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 414 | // HTTP server response code from the last HTTP request action. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 415 | int http_response_code_ = 0; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 416 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 417 | // Current cpu shares. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 418 | utils::CpuShares shares_ = utils::kCpuSharesNormal; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 419 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 420 | // The cpu shares management timeout source. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 421 | GSource* manage_shares_source_ = nullptr; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 422 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 423 | // Set to true if an update download is active (and BytesReceived |
| 424 | // will be called), set to false otherwise. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 425 | bool download_active_ = false; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 426 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 427 | // For status: |
| 428 | UpdateStatus status_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 429 | double download_progress_ = 0.0; |
| 430 | int64_t last_checked_time_ = 0; |
Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 431 | std::string prev_version_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 432 | std::string new_version_ = "0.0.0.0"; |
| 433 | int64_t new_payload_size_ = 0; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 434 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 435 | // Common parameters for all Omaha requests. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 436 | OmahaRequestParams* omaha_request_params_ = nullptr; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 437 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 438 | // Number of consecutive manual update checks we've had where we obeyed |
| 439 | // Chrome's proxy settings. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 440 | int proxy_manual_checks_ = 0; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 441 | |
| 442 | // If true, this update cycle we are obeying proxies |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 443 | bool obeying_proxies_ = true; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 444 | |
| 445 | // Our two proxy resolvers |
| 446 | DirectProxyResolver direct_proxy_resolver_; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 447 | ChromeBrowserProxyResolver chrome_proxy_resolver_; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 448 | |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 449 | // Originally, both of these flags are false. Once UpdateBootFlags is called, |
| 450 | // |update_boot_flags_running_| is set to true. As soon as UpdateBootFlags |
| 451 | // completes its asynchronous run, |update_boot_flags_running_| is reset to |
| 452 | // false and |updated_boot_flags_| is set to true. From that point on there |
| 453 | // will be no more changes to these flags. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 454 | // |
| 455 | // True if UpdateBootFlags has completed. |
| 456 | bool updated_boot_flags_ = false; |
| 457 | // True if UpdateBootFlags is running. |
| 458 | bool update_boot_flags_running_ = false; |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 459 | |
| 460 | // True if the action processor needs to be started by the boot flag updater. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 461 | bool start_action_processor_ = false; |
Andrew de los Reyes | 6dbf30a | 2011-04-19 10:58:16 -0700 | [diff] [blame] | 462 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 463 | // Used for fetching information about the device policy. |
| 464 | scoped_ptr<policy::PolicyProvider> policy_provider_; |
| 465 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 466 | // The current scatter factor as found in the policy setting. |
| 467 | base::TimeDelta scatter_factor_; |
| 468 | |
Gilad Arnold | 70e476e | 2013-07-30 16:01:13 -0700 | [diff] [blame] | 469 | // Update completed marker file. An empty string means this marker is being |
| 470 | // ignored (nor is it being written), which is useful for testing situations. |
| 471 | std::string update_completed_marker_; |
| 472 | |
Gilad Arnold | a6dab94 | 2014-04-25 11:46:03 -0700 | [diff] [blame] | 473 | // The number of consecutive failed update checks. Needed for calculating the |
| 474 | // next update check interval. |
| 475 | unsigned int consecutive_failed_update_checks_ = 0; |
| 476 | |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 477 | // The poll interval (in seconds) that was dictated by Omaha, if any; zero |
| 478 | // otherwise. This is needed for calculating the update check interval. |
| 479 | unsigned int server_dictated_poll_interval_ = 0; |
| 480 | |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 481 | // A callback to use when either an interactive update request is received |
| 482 | // (true) or cleared by an update attempt (false). |
| 483 | scoped_ptr<base::Callback<void(bool)>> // NOLINT(readability/function) |
| 484 | interactive_update_pending_callback_; |
| 485 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 486 | DISALLOW_COPY_AND_ASSIGN(UpdateAttempter); |
| 487 | }; |
| 488 | |
| 489 | } // namespace chromeos_update_engine |
| 490 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 491 | #endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_H_ |