blob: a1ae7720a916b194667fc10be2c86a07a5866c47 [file] [log] [blame]
Darin Petkov58dd1342011-05-06 12:05:13 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__
7
Andrew de los Reyes63b96d72010-05-10 13:08:54 -07008#include <time.h>
Darin Petkovc6c135c2010-08-11 13:36:18 -07009
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070010#include <tr1/memory>
11#include <string>
12#include <vector>
Darin Petkovc6c135c2010-08-11 13:36:18 -070013
Darin Petkov85ced132010-09-01 10:20:56 -070014#include <base/time.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070015#include <glib.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Gilad Arnold28e2f392012-02-09 14:36:46 -080017#include <libudev.h>
Darin Petkovc6c135c2010-08-11 13:36:18 -070018
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070019#include "update_engine/action_processor.h"
Andrew de los Reyes000d8952011-03-02 15:21:14 -080020#include "update_engine/chrome_browser_proxy_resolver.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070021#include "update_engine/download_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070023#include "update_engine/omaha_response_handler_action.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080024#include "update_engine/proxy_resolver.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070025
Darin Petkov9d65b7b2010-07-20 09:13:01 -070026class MetricsLibraryInterface;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070027struct UpdateEngineService;
28
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020029namespace policy {
30 class PolicyProvider;
31}
32
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070033namespace chromeos_update_engine {
34
Darin Petkov1023a602010-08-30 13:47:51 -070035class UpdateCheckScheduler;
Darin Petkovc6c135c2010-08-11 13:36:18 -070036
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070037extern const char* kUpdateCompletedMarker;
38
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070039enum UpdateStatus {
40 UPDATE_STATUS_IDLE = 0,
41 UPDATE_STATUS_CHECKING_FOR_UPDATE,
42 UPDATE_STATUS_UPDATE_AVAILABLE,
43 UPDATE_STATUS_DOWNLOADING,
44 UPDATE_STATUS_VERIFYING,
45 UPDATE_STATUS_FINALIZING,
Darin Petkov09f96c32010-07-20 09:24:57 -070046 UPDATE_STATUS_UPDATED_NEED_REBOOT,
47 UPDATE_STATUS_REPORTING_ERROR_EVENT,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070048};
49
50const char* UpdateStatusToString(UpdateStatus status);
51
52class UpdateAttempter : public ActionProcessorDelegate,
53 public DownloadActionDelegate {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070054 public:
Darin Petkov36275772010-10-01 11:40:57 -070055 static const int kMaxDeltaUpdateFailures;
56
Andrew de los Reyes45168102010-11-22 11:13:50 -080057 UpdateAttempter(PrefsInterface* prefs,
58 MetricsLibraryInterface* metrics_lib,
59 DbusGlibInterface* dbus_iface);
Darin Petkov1023a602010-08-30 13:47:51 -070060 virtual ~UpdateAttempter();
Darin Petkovc6c135c2010-08-11 13:36:18 -070061
Darin Petkov5a7f5652010-07-22 21:40:09 -070062 // Checks for update and, if a newer version is available, attempts
63 // to update the system. Non-empty |in_app_version| or
64 // |in_update_url| prevents automatic detection of the parameter.
Andrew de los Reyes45168102010-11-22 11:13:50 -080065 // If |obey_proxies| is true, the update will likely respect Chrome's
66 // proxy setting. For security reasons, we may still not honor them.
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -070067 // Interactive should be true if this was called from the user (ie dbus).
Darin Petkov1023a602010-08-30 13:47:51 -070068 virtual void Update(const std::string& app_version,
Andrew de los Reyes45168102010-11-22 11:13:50 -080069 const std::string& omaha_url,
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -070070 bool obey_proxies,
71 bool interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070072
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070073 // ActionProcessorDelegate methods:
Darin Petkovc1a8b422010-07-19 11:34:49 -070074 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070075 void ProcessingStopped(const ActionProcessor* processor);
76 void ActionCompleted(ActionProcessor* processor,
77 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070078 ActionExitCode code);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070079
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070080 // Stop updating. An attempt will be made to record status to the disk
81 // so that updates can be resumed later.
82 void Terminate();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070083
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070084 // Try to resume from a previously Terminate()d update.
85 void ResumeUpdating();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070086
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070087 // Returns the current status in the out params. Returns true on success.
88 bool GetStatus(int64_t* last_checked_time,
89 double* progress,
90 std::string* current_operation,
91 std::string* new_version,
92 int64_t* new_size);
93
Darin Petkov58dd1342011-05-06 12:05:13 -070094 // Runs chromeos-setgoodkernel, whose responsibility it is to mark the
95 // currently booted partition has high priority/permanent/etc. The execution
96 // is asynchronous. On completion, the action processor may be started
97 // depending on the |start_action_processor_| field. Note that every update
98 // attempt goes through this method.
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -070099 void UpdateBootFlags();
100
Darin Petkov58dd1342011-05-06 12:05:13 -0700101 // Subprocess::Exec callback.
102 void CompleteUpdateBootFlags(int return_code);
103 static void StaticCompleteUpdateBootFlags(int return_code,
104 const std::string& output,
105 void* p);
106
Darin Petkov1023a602010-08-30 13:47:51 -0700107 UpdateStatus status() const { return status_; }
108
109 int http_response_code() const { return http_response_code_; }
110 void set_http_response_code(int code) { http_response_code_ = code; }
111
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700112 void set_dbus_service(struct UpdateEngineService* dbus_service) {
113 dbus_service_ = dbus_service;
114 }
115
Darin Petkov1023a602010-08-30 13:47:51 -0700116 UpdateCheckScheduler* update_check_scheduler() const {
117 return update_check_scheduler_;
118 }
119 void set_update_check_scheduler(UpdateCheckScheduler* scheduler) {
120 update_check_scheduler_ = scheduler;
121 }
122
Darin Petkov5a7f5652010-07-22 21:40:09 -0700123 // This is the D-Bus service entry point for going through an
124 // update. If the current status is idle invokes Update.
125 void CheckForUpdate(const std::string& app_version,
126 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700127
Darin Petkov296889c2010-07-23 16:20:54 -0700128 // Initiates a reboot if the current state is
129 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
130 bool RebootIfNeeded();
131
Darin Petkov9d911fa2010-08-19 09:36:08 -0700132 // DownloadActionDelegate methods
133 void SetDownloadStatus(bool active);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700134 void BytesReceived(uint64_t bytes_received, uint64_t total);
135
Darin Petkov61635a92011-05-18 16:20:36 -0700136 // Broadcasts the current status over D-Bus.
137 void BroadcastStatus();
138
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700139 private:
Gilad Arnold28e2f392012-02-09 14:36:46 -0800140 // Update server URL for automated lab test.
141 static const char* const kTestUpdateUrl;
142
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700143 friend class UpdateAttempterTest;
Darin Petkov1b003102010-11-30 10:18:36 -0800144 FRIEND_TEST(UpdateAttempterTest, ActionCompletedDownloadTest);
145 FRIEND_TEST(UpdateAttempterTest, ActionCompletedErrorTest);
146 FRIEND_TEST(UpdateAttempterTest, ActionCompletedOmahaRequestTest);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700147 FRIEND_TEST(UpdateAttempterTest, CreatePendingErrorEventTest);
148 FRIEND_TEST(UpdateAttempterTest, CreatePendingErrorEventResumedTest);
Darin Petkov36275772010-10-01 11:40:57 -0700149 FRIEND_TEST(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest);
150 FRIEND_TEST(UpdateAttempterTest, MarkDeltaUpdateFailureTest);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200151 FRIEND_TEST(UpdateAttempterTest, ReadTrackFromPolicy);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700152 FRIEND_TEST(UpdateAttempterTest, PingOmahaTest);
Darin Petkov1b003102010-11-30 10:18:36 -0800153 FRIEND_TEST(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest);
154 FRIEND_TEST(UpdateAttempterTest, ScheduleErrorEventActionTest);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700155 FRIEND_TEST(UpdateAttempterTest, UpdateTest);
156
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700157 // Sets the status to the given status and notifies a status update
158 // over dbus.
159 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700160
Darin Petkov9b230572010-10-08 10:20:09 -0700161 // Sets up the download parameters after receiving the update check response.
162 void SetupDownload();
163
164 // Creates an error event object in |error_event_| to be included in an
165 // OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700166 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700167
Darin Petkov9b230572010-10-08 10:20:09 -0700168 // If there's a pending error event allocated in |error_event_|, schedules an
169 // OmahaRequestAction with that event in the current processor, clears the
170 // pending event, updates the status and returns true. Returns false
171 // otherwise.
Darin Petkov09f96c32010-07-20 09:24:57 -0700172 bool ScheduleErrorEventAction();
173
Darin Petkov9b230572010-10-08 10:20:09 -0700174 // Sets the process priority to |priority| and updates |priority_| if the new
175 // |priority| is different than the current |priority_|, otherwise simply
176 // returns.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700177 void SetPriority(utils::ProcessPriority priority);
178
Darin Petkovf622ef72010-10-26 13:49:24 -0700179 // Sets the process priority to low and sets up timeout events to increase it.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700180 void SetupPriorityManagement();
181
Darin Petkov9b230572010-10-08 10:20:09 -0700182 // Resets the process priority to normal and destroys any scheduled timeout
183 // sources.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700184 void CleanupPriorityManagement();
185
Darin Petkovf622ef72010-10-26 13:49:24 -0700186 // The process priority timeout source callback sets the current priority to
187 // normal. Returns false so that GLib destroys the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700188 static gboolean StaticManagePriorityCallback(gpointer data);
189 bool ManagePriorityCallback();
190
Darin Petkove6ef2f82011-03-07 17:31:11 -0800191 // Callback to start the action processor.
192 static gboolean StaticStartProcessing(gpointer data);
193
Darin Petkov58dd1342011-05-06 12:05:13 -0700194 // Schedules an event loop callback to start the action processor. This is
195 // scheduled asynchronously to unblock the event loop.
196 void ScheduleProcessingStart();
197
Darin Petkov36275772010-10-01 11:40:57 -0700198 // Checks if a full update is needed and forces it by updating the Omaha
199 // request params.
200 void DisableDeltaUpdateIfNeeded();
201
202 // If this was a delta update attempt that failed, count it so that a full
203 // update can be tried when needed.
204 void MarkDeltaUpdateFailure();
Darin Petkov1b003102010-11-30 10:18:36 -0800205
Andrew de los Reyes45168102010-11-22 11:13:50 -0800206 ProxyResolver* GetProxyResolver() {
207 return obeying_proxies_ ?
208 reinterpret_cast<ProxyResolver*>(&chrome_proxy_resolver_) :
209 reinterpret_cast<ProxyResolver*>(&direct_proxy_resolver_);
210 }
Darin Petkov36275772010-10-01 11:40:57 -0700211
Thieu Le116fda32011-04-19 11:01:54 -0700212 // Sends a ping to Omaha.
213 // This is used after an update has been applied and we're waiting for the
214 // user to reboot. This ping helps keep the number of actives count
215 // accurate in case a user takes a long time to reboot the device after an
216 // update has been applied.
217 void PingOmaha();
218
Gilad Arnold28e2f392012-02-09 14:36:46 -0800219 // Gets the fully qualified sysfs name of a dutflag device. |udev| is a live
220 // libudev instance; |gpio_dutflag_str| is the identifier for the requested
221 // dutflag GPIO. The output is stored in the string pointed to by
222 // |dutflag_dev_name_p|. Returns true upon success, false otherwise.
223 bool GetDutflagGpioDevName(struct udev* udev,
224 const std::string& gpio_dutflag_str,
225 std::string* dutflag_dev_name_p);
226
227 // Gets the dut_flaga/b GPIO device names and copies them into the two string
228 // arguments, respectively. The function caches these strings, which are
229 // assumed to be hardware constants. Returns true upon success, false
230 // otherwise.
231 bool GetDutflagGpioDevNames(std::string* dutflaga_dev_name_p,
232 std::string* dutflagb_dev_name_p);
233
234 // Writes the dut_flaga GPIO status into its argument, where true/false stand
235 // for "on"/"off", respectively. Returns true upon success, false otherwise
236 // (in which case no value is written to |status|).
237 bool GetDutflagaGpio(bool* status);
238
Darin Petkov9b230572010-10-08 10:20:09 -0700239 // Last status notification timestamp used for throttling. Use monotonic
240 // TimeTicks to ensure that notifications are sent even if the system clock is
241 // set back in the middle of an update.
Darin Petkovaf183052010-08-23 12:07:13 -0700242 base::TimeTicks last_notify_time_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700243
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700244 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700245 scoped_ptr<ActionProcessor> processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700246
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700247 // If non-null, this UpdateAttempter will send status updates over this
248 // dbus service.
249 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700250
Darin Petkov9b230572010-10-08 10:20:09 -0700251 // Pointer to the OmahaResponseHandlerAction in the actions_ vector.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700252 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700253
Darin Petkov9b230572010-10-08 10:20:09 -0700254 // Pointer to the DownloadAction in the actions_ vector.
255 std::tr1::shared_ptr<DownloadAction> download_action_;
256
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700257 // Pointer to the preferences store interface.
258 PrefsInterface* prefs_;
259
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700260 // Pointer to the UMA metrics collection library.
261 MetricsLibraryInterface* metrics_lib_;
262
Darin Petkov1023a602010-08-30 13:47:51 -0700263 // The current UpdateCheckScheduler to notify of state transitions.
264 UpdateCheckScheduler* update_check_scheduler_;
265
Darin Petkov09f96c32010-07-20 09:24:57 -0700266 // Pending error event, if any.
267 scoped_ptr<OmahaEvent> error_event_;
268
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700269 // If we should request a reboot even tho we failed the update
270 bool fake_update_success_;
271
Darin Petkov85ced132010-09-01 10:20:56 -0700272 // HTTP server response code from the last HTTP request action.
Darin Petkov1023a602010-08-30 13:47:51 -0700273 int http_response_code_;
274
Darin Petkovc6c135c2010-08-11 13:36:18 -0700275 // Current process priority.
276 utils::ProcessPriority priority_;
277
278 // The process priority management timeout source.
279 GSource* manage_priority_source_;
280
Darin Petkov9d911fa2010-08-19 09:36:08 -0700281 // Set to true if an update download is active (and BytesReceived
282 // will be called), set to false otherwise.
283 bool download_active_;
284
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700285 // For status:
286 UpdateStatus status_;
287 double download_progress_;
288 int64_t last_checked_time_;
289 std::string new_version_;
290 int64_t new_size_;
291
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700292 // Device paramaters common to all Omaha requests.
293 OmahaRequestDeviceParams omaha_request_params_;
294
Andrew de los Reyes45168102010-11-22 11:13:50 -0800295 // Number of consecutive manual update checks we've had where we obeyed
296 // Chrome's proxy settings.
297 int proxy_manual_checks_;
298
299 // If true, this update cycle we are obeying proxies
300 bool obeying_proxies_;
301
302 // Our two proxy resolvers
303 DirectProxyResolver direct_proxy_resolver_;
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800304 ChromeBrowserProxyResolver chrome_proxy_resolver_;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800305
Darin Petkov58dd1342011-05-06 12:05:13 -0700306 // Originally, both of these flags are false. Once UpdateBootFlags is called,
307 // |update_boot_flags_running_| is set to true. As soon as UpdateBootFlags
308 // completes its asynchronous run, |update_boot_flags_running_| is reset to
309 // false and |updated_boot_flags_| is set to true. From that point on there
310 // will be no more changes to these flags.
311 bool updated_boot_flags_; // True if UpdateBootFlags has completed.
312 bool update_boot_flags_running_; // True if UpdateBootFlags is running.
313
314 // True if the action processor needs to be started by the boot flag updater.
315 bool start_action_processor_;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700316
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200317 // Used for fetching information about the device policy.
318 scoped_ptr<policy::PolicyProvider> policy_provider_;
319
Gilad Arnold28e2f392012-02-09 14:36:46 -0800320 // Dutflaga/b GPIO device names.
321 std::string dutflaga_dev_name_;
322 std::string dutflagb_dev_name_;
323
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700324 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
325};
326
327} // namespace chromeos_update_engine
328
329#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__