blob: d6c54962d3e511e6dfa960cca88bd1938958dbba [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#include "update_engine/update_attempter.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -07006
7// From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L
8#ifndef _POSIX_C_SOURCE
9#define _POSIX_C_SOURCE 199309L
10#endif // _POSIX_C_SOURCE
11#include <time.h>
12
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070013#include <string>
Darin Petkov9b230572010-10-08 10:20:09 -070014#include <tr1/memory>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070015#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070016
Andrew de los Reyes45168102010-11-22 11:13:50 -080017#include <base/rand_util.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070018#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070019#include <metrics/metrics_library.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020020#include <policy/libpolicy.h>
21#include <policy/device_policy.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070022
Bruno Rocha7f9aea22011-09-12 14:31:24 -070023#include "update_engine/certificate_checker.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070024#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070025#include "update_engine/download_action.h"
26#include "update_engine/filesystem_copier_action.h"
27#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyes819fef22010-12-17 11:33:58 -080028#include "update_engine/multi_range_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070029#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070030#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070031#include "update_engine/omaha_response_handler_action.h"
32#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070033#include "update_engine/prefs_interface.h"
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -070034#include "update_engine/subprocess.h"
Darin Petkov1023a602010-08-30 13:47:51 -070035#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
Darin Petkovaf183052010-08-23 12:07:13 -070037using base::TimeDelta;
38using base::TimeTicks;
Andrew de los Reyes21816e12011-04-07 14:18:56 -070039using google::protobuf::NewPermanentCallback;
Darin Petkov9b230572010-10-08 10:20:09 -070040using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070041using std::tr1::shared_ptr;
42using std::string;
43using std::vector;
44
45namespace chromeos_update_engine {
46
Darin Petkov36275772010-10-01 11:40:57 -070047const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
48
Gilad Arnold1ebd8132012-03-05 10:19:29 -080049// Private test server URL w/ custom port number.
Gilad Arnolded747312012-03-15 18:20:41 -070050// TODO(garnold) This is a temporary hack to allow us to test the closed loop
51// automated update testing. To be replaced with an hard-coded local IP address.
52const char* const UpdateAttempter::kTestUpdateUrl(
53 "http://garnold.mtv.corp.google.com:8080/update");
Gilad Arnold28e2f392012-02-09 14:36:46 -080054
Darin Petkovcd1666f2010-09-23 09:53:44 -070055const char* kUpdateCompletedMarker =
56 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070057
Andrew de los Reyes45168102010-11-22 11:13:50 -080058namespace {
59const int kMaxConsecutiveObeyProxyRequests = 20;
60} // namespace {}
61
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070062const char* UpdateStatusToString(UpdateStatus status) {
63 switch (status) {
64 case UPDATE_STATUS_IDLE:
65 return "UPDATE_STATUS_IDLE";
66 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
67 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
68 case UPDATE_STATUS_UPDATE_AVAILABLE:
69 return "UPDATE_STATUS_UPDATE_AVAILABLE";
70 case UPDATE_STATUS_DOWNLOADING:
71 return "UPDATE_STATUS_DOWNLOADING";
72 case UPDATE_STATUS_VERIFYING:
73 return "UPDATE_STATUS_VERIFYING";
74 case UPDATE_STATUS_FINALIZING:
75 return "UPDATE_STATUS_FINALIZING";
76 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
77 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070078 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
79 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070080 default:
81 return "unknown status";
82 }
83}
84
Darin Petkov777dbfa2010-07-20 15:03:37 -070085// Turns a generic kActionCodeError to a generic error code specific
86// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
87// not kActionCodeError, or the action is not matched, returns |code|
88// unchanged.
89ActionExitCode GetErrorCodeForAction(AbstractAction* action,
90 ActionExitCode code) {
91 if (code != kActionCodeError)
92 return code;
93
94 const string type = action->Type();
95 if (type == OmahaRequestAction::StaticType())
96 return kActionCodeOmahaRequestError;
97 if (type == OmahaResponseHandlerAction::StaticType())
98 return kActionCodeOmahaResponseHandlerError;
99 if (type == FilesystemCopierAction::StaticType())
100 return kActionCodeFilesystemCopierError;
101 if (type == PostinstallRunnerAction::StaticType())
102 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -0700103
104 return code;
105}
106
Darin Petkovc6c135c2010-08-11 13:36:18 -0700107UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800108 MetricsLibraryInterface* metrics_lib,
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700109 DbusGlibInterface* dbus_iface,
110 GpioHandler* gpio_handler)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700111 : processor_(new ActionProcessor()),
112 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700113 prefs_(prefs),
114 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700115 update_check_scheduler_(NULL),
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700116 fake_update_success_(false),
Darin Petkov1023a602010-08-30 13:47:51 -0700117 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700118 priority_(utils::kProcessPriorityNormal),
119 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700120 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700121 status_(UPDATE_STATUS_IDLE),
122 download_progress_(0.0),
123 last_checked_time_(0),
124 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700125 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800126 proxy_manual_checks_(0),
127 obeying_proxies_(true),
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700128 chrome_proxy_resolver_(dbus_iface),
Darin Petkov58dd1342011-05-06 12:05:13 -0700129 updated_boot_flags_(false),
130 update_boot_flags_running_(false),
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200131 start_action_processor_(false),
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800132 policy_provider_(NULL),
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700133 is_using_test_url_(false),
134 is_test_update_attempted_(false),
135 gpio_handler_(gpio_handler) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700136 if (utils::FileExists(kUpdateCompletedMarker))
137 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
138}
139
140UpdateAttempter::~UpdateAttempter() {
141 CleanupPriorityManagement();
142}
143
Gilad Arnold28e2f392012-02-09 14:36:46 -0800144void UpdateAttempter::Update(const string& app_version,
145 const string& omaha_url,
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700146 bool obey_proxies,
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800147 bool interactive,
148 bool is_test) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800149 chrome_proxy_resolver_.Init();
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700150 fake_update_success_ = false;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700151 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Thieu Le116fda32011-04-19 11:01:54 -0700152 // Although we have applied an update, we still want to ping Omaha
153 // to ensure the number of active statistics is accurate.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700154 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
Thieu Le116fda32011-04-19 11:01:54 -0700155 << "reboot, we'll ping Omaha instead";
156 PingOmaha();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700157 return;
158 }
159 if (status_ != UPDATE_STATUS_IDLE) {
160 // Update in progress. Do nothing
161 return;
162 }
Darin Petkov1023a602010-08-30 13:47:51 -0700163 http_response_code_ = 0;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200164
165 // Lazy initialize the policy provider, or reload the latest policy data.
166 if (!policy_provider_.get()) {
167 policy_provider_.reset(new policy::PolicyProvider());
168 } else {
169 policy_provider_->Reload();
170 }
171
172 // If the release_track is specified by policy, that takes precedence.
173 string release_track;
Jay Srinivasan0a708742012-03-20 11:26:12 -0700174 if (policy_provider_->device_policy_is_loaded()) {
175 const policy::DevicePolicy& device_policy =
176 policy_provider_->GetDevicePolicy();
177 device_policy.GetReleaseChannel(&release_track);
178 device_policy.GetUpdateDisabled(&omaha_request_params_.update_disabled);
179 device_policy.GetTargetVersionPrefix(
180 &omaha_request_params_.target_version_prefix);
181 }
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200182
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800183 // Determine whether an alternative test address should be used.
Gilad Arnold28e2f392012-02-09 14:36:46 -0800184 string omaha_url_to_use = omaha_url;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800185 if ((is_using_test_url_ = (omaha_url_to_use.empty() && is_test))) {
186 omaha_url_to_use = kTestUpdateUrl;
187 LOG(INFO) << "using alternative server address: " << omaha_url_to_use;
Gilad Arnold28e2f392012-02-09 14:36:46 -0800188 }
189
Jay Srinivasan0a708742012-03-20 11:26:12 -0700190 if (!omaha_request_params_.Init(app_version,
191 omaha_url_to_use,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800192 release_track)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700193 LOG(ERROR) << "Unable to initialize Omaha request device params.";
194 return;
195 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800196
Jay Srinivasan0a708742012-03-20 11:26:12 -0700197 LOG(INFO) << "update_disabled = "
198 << (omaha_request_params_.update_disabled ? "true" : "false")
199 << ", target_version_prefix = "
200 << omaha_request_params_.target_version_prefix;
201
Andrew de los Reyes45168102010-11-22 11:13:50 -0800202 obeying_proxies_ = true;
203 if (obey_proxies || proxy_manual_checks_ == 0) {
204 LOG(INFO) << "forced to obey proxies";
205 // If forced to obey proxies, every 20th request will not use proxies
206 proxy_manual_checks_++;
207 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
208 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
209 proxy_manual_checks_ = 0;
210 obeying_proxies_ = false;
211 }
212 } else if (base::RandInt(0, 4) == 0) {
213 obeying_proxies_ = false;
214 }
215 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
216 "check we are ignoring the proxy settings and using "
217 "direct connections.";
218
Darin Petkov36275772010-10-01 11:40:57 -0700219 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700220 CHECK(!processor_->IsRunning());
221 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700222
223 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800224 LibcurlHttpFetcher* update_check_fetcher =
225 new LibcurlHttpFetcher(GetProxyResolver());
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700226 // Try harder to connect to the network, esp when not interactive.
227 // See comment in libcurl_http_fetcher.cc.
228 update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700229 update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700230 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700231 new OmahaRequestAction(prefs_,
232 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700233 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700234 update_check_fetcher, // passes ownership
235 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700236 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700237 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700238 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800239 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700240 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800241 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700242 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700243 new OmahaRequestAction(prefs_,
244 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700245 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700246 OmahaEvent::kTypeUpdateDownloadStarted),
Thieu Le116fda32011-04-19 11:01:54 -0700247 new LibcurlHttpFetcher(GetProxyResolver()),
248 false));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700249 LibcurlHttpFetcher* download_fetcher =
250 new LibcurlHttpFetcher(GetProxyResolver());
251 download_fetcher->set_check_certificate(CertificateChecker::kDownload);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700252 shared_ptr<DownloadAction> download_action(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700253 new DownloadAction(prefs_,
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800254 new MultiRangeHttpFetcher(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700255 download_fetcher))); // passes ownership
Darin Petkov8c2980e2010-07-16 15:16:49 -0700256 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700257 new OmahaRequestAction(prefs_,
258 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700259 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700260 OmahaEvent::kTypeUpdateDownloadFinished),
Thieu Le116fda32011-04-19 11:01:54 -0700261 new LibcurlHttpFetcher(GetProxyResolver()),
262 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800263 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
264 new FilesystemCopierAction(false, true));
265 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
266 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800267 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
268 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700269 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700270 new OmahaRequestAction(prefs_,
271 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700272 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700273 new LibcurlHttpFetcher(GetProxyResolver()),
274 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700275
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700276 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700277 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700278 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700279
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700280 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
281 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
282 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700283 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700284 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700285 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700286 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700287 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800288 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
289 actions_.push_back(shared_ptr<AbstractAction>(
290 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800291 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700292 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700293
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700294 // Enqueue the actions
295 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
296 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700297 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700298 }
299
300 // Bond them together. We have to use the leaf-types when calling
301 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700302 BondActions(update_check_action.get(),
303 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700304 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700305 filesystem_copier_action.get());
306 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700307 kernel_filesystem_copier_action.get());
308 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700309 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700310 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800311 filesystem_verifier_action.get());
312 BondActions(filesystem_verifier_action.get(),
313 kernel_filesystem_verifier_action.get());
314 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800315 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700316
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800317 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE,
318 kUpdateNoticeUnspecified);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800319
Darin Petkov58dd1342011-05-06 12:05:13 -0700320 // Just in case we didn't update boot flags yet, make sure they're updated
321 // before any update processing starts.
322 start_action_processor_ = true;
323 UpdateBootFlags();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700324}
325
Gilad Arnold28e2f392012-02-09 14:36:46 -0800326void UpdateAttempter::CheckForUpdate(const string& app_version,
327 const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700328 if (status_ != UPDATE_STATUS_IDLE) {
329 LOG(INFO) << "Check for update requested, but status is "
330 << UpdateStatusToString(status_) << ", so not checking.";
331 return;
332 }
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800333
334 // Read GPIO signals and determine whether this is an automated test scenario.
335 // For safety, we only allow a test update to be performed once; subsequent
336 // update requests will be carried out normally.
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700337 bool is_test = (!is_test_update_attempted_ && gpio_handler_ &&
338 gpio_handler_->IsTestModeSignaled());
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800339 if (is_test) {
340 LOG(INFO) << "test mode signaled";
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700341 is_test_update_attempted_ = true;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800342 }
343
344 Update(app_version, omaha_url, true, true, is_test);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700345}
346
Darin Petkov296889c2010-07-23 16:20:54 -0700347bool UpdateAttempter::RebootIfNeeded() {
348 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
349 LOG(INFO) << "Reboot requested, but status is "
350 << UpdateStatusToString(status_) << ", so not rebooting.";
351 return false;
352 }
353 TEST_AND_RETURN_FALSE(utils::Reboot());
354 return true;
355}
356
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700357// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700358void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700359 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700360 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700361 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700362 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700363
Darin Petkovc6c135c2010-08-11 13:36:18 -0700364 // Reset process priority back to normal.
365 CleanupPriorityManagement();
366
Darin Petkov09f96c32010-07-20 09:24:57 -0700367 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
368 LOG(INFO) << "Error event sent.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800369
370 // Inform scheduler of new status; also specifically inform about a failed
371 // update attempt with a test address.
372 SetStatusAndNotify(UPDATE_STATUS_IDLE,
373 (is_using_test_url_ ? kUpdateNoticeTestAddrFailed :
374 kUpdateNoticeUnspecified));
375
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700376 if (!fake_update_success_) {
377 return;
378 }
379 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
380 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700381 }
382
Darin Petkovc1a8b422010-07-19 11:34:49 -0700383 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700384 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700385 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800386 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700387 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800388 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
389 kUpdateNoticeUnspecified);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700390
391 // Report the time it took to update the system.
392 int64_t update_time = time(NULL) - last_checked_time_;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700393 if (!fake_update_success_)
394 metrics_lib_->SendToUMA("Installer.UpdateTime",
395 static_cast<int>(update_time), // sample
396 1, // min = 1 second
397 20 * 60, // max = 20 minutes
398 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700399 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700400 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700401
Darin Petkov1023a602010-08-30 13:47:51 -0700402 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700403 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700404 }
405 LOG(INFO) << "No update.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800406 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700407}
408
409void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700410 // Reset process priority back to normal.
411 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700412 download_progress_ = 0.0;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800413 SetStatusAndNotify(UPDATE_STATUS_IDLE, kUpdateNoticeUnspecified);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700414 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700415 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700416}
417
418// Called whenever an action has finished processing, either successfully
419// or otherwise.
420void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
421 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700422 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700423 // Reset download progress regardless of whether or not the download
424 // action succeeded. Also, get the response code from HTTP request
425 // actions (update download as well as the initial update check
426 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700427 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700428 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700429 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700430 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
431 http_response_code_ = download_action->GetHTTPResponseCode();
432 } else if (type == OmahaRequestAction::StaticType()) {
433 OmahaRequestAction* omaha_request_action =
434 dynamic_cast<OmahaRequestAction*>(action);
435 // If the request is not an event, then it's the update-check.
436 if (!omaha_request_action->IsEvent()) {
437 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700438 // Forward the server-dictated poll interval to the update check
439 // scheduler, if any.
440 if (update_check_scheduler_) {
441 update_check_scheduler_->set_poll_interval(
442 omaha_request_action->GetOutputObject().poll_interval);
443 }
Darin Petkov1023a602010-08-30 13:47:51 -0700444 }
445 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700446 if (code != kActionCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700447 // If the current state is at or past the download phase, count the failure
448 // in case a switch to full update becomes necessary. Ignore network
449 // transfer timeouts and failures.
Darin Petkov36275772010-10-01 11:40:57 -0700450 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
Darin Petkov36275772010-10-01 11:40:57 -0700451 code != kActionCodeDownloadTransferError) {
452 MarkDeltaUpdateFailure();
453 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700454 // On failure, schedule an error event to be sent to Omaha.
455 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700456 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700457 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700458 // Find out which action completed.
459 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700460 // Note that the status will be updated to DOWNLOADING when some bytes get
461 // actually downloaded from the server and the BytesReceived callback is
462 // invoked. This avoids notifying the user that a download has started in
463 // cases when the server and the client are unable to initiate the download.
464 CHECK(action == response_handler_action_.get());
465 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700466 last_checked_time_ = time(NULL);
467 // TODO(adlr): put version in InstallPlan
468 new_version_ = "0.0.0.0";
469 new_size_ = plan.size;
Darin Petkov9b230572010-10-08 10:20:09 -0700470 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700471 SetupPriorityManagement();
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800472 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE,
473 kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700474 } else if (type == DownloadAction::StaticType()) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800475 SetStatusAndNotify(UPDATE_STATUS_FINALIZING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700476 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700477}
478
479// Stop updating. An attempt will be made to record status to the disk
480// so that updates can be resumed later.
481void UpdateAttempter::Terminate() {
482 // TODO(adlr): implement this method.
483 NOTIMPLEMENTED();
484}
485
486// Try to resume from a previously Terminate()d update.
487void UpdateAttempter::ResumeUpdating() {
488 // TODO(adlr): implement this method.
489 NOTIMPLEMENTED();
490}
491
Darin Petkov9d911fa2010-08-19 09:36:08 -0700492void UpdateAttempter::SetDownloadStatus(bool active) {
493 download_active_ = active;
494 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
495}
496
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700497void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700498 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700499 LOG(ERROR) << "BytesReceived called while not downloading.";
500 return;
501 }
Darin Petkovaf183052010-08-23 12:07:13 -0700502 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700503 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700504 // Self throttle based on progress. Also send notifications if
505 // progress is too slow.
506 const double kDeltaPercent = 0.01; // 1%
507 if (status_ != UPDATE_STATUS_DOWNLOADING ||
508 bytes_received == total ||
509 progress - download_progress_ >= kDeltaPercent ||
510 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
511 download_progress_ = progress;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800512 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING, kUpdateNoticeUnspecified);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700513 }
514}
515
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700516bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
517 double* progress,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800518 string* current_operation,
519 string* new_version,
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700520 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700521 *last_checked_time = last_checked_time_;
522 *progress = download_progress_;
523 *current_operation = UpdateStatusToString(status_);
524 *new_version = new_version_;
525 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700526 return true;
527}
528
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700529void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700530 if (update_boot_flags_running_) {
531 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700532 return;
533 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700534 if (updated_boot_flags_) {
535 LOG(INFO) << "Already updated boot flags. Skipping.";
536 if (start_action_processor_) {
537 ScheduleProcessingStart();
538 }
539 return;
540 }
541 // This is purely best effort. Failures should be logged by Subprocess. Run
542 // the script asynchronously to avoid blocking the event loop regardless of
543 // the script runtime.
544 update_boot_flags_running_ = true;
545 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700546 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700547 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
548 CompleteUpdateBootFlags(1);
549 }
550}
551
552void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
553 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700554 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700555 if (start_action_processor_) {
556 ScheduleProcessingStart();
557 }
558}
559
560void UpdateAttempter::StaticCompleteUpdateBootFlags(
561 int return_code,
Gilad Arnold28e2f392012-02-09 14:36:46 -0800562 const string& output,
Darin Petkov58dd1342011-05-06 12:05:13 -0700563 void* p) {
564 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700565}
566
Darin Petkov61635a92011-05-18 16:20:36 -0700567void UpdateAttempter::BroadcastStatus() {
568 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700569 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700570 }
Darin Petkovaf183052010-08-23 12:07:13 -0700571 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700572 update_engine_service_emit_status_update(
573 dbus_service_,
574 last_checked_time_,
575 download_progress_,
576 UpdateStatusToString(status_),
577 new_version_.c_str(),
578 new_size_);
579}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700580
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800581void UpdateAttempter::SetStatusAndNotify(UpdateStatus status,
582 UpdateNotice notice) {
Darin Petkov61635a92011-05-18 16:20:36 -0700583 status_ = status;
584 if (update_check_scheduler_) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800585 update_check_scheduler_->SetUpdateStatus(status_, notice);
Darin Petkov61635a92011-05-18 16:20:36 -0700586 }
587 BroadcastStatus();
588}
589
Darin Petkov777dbfa2010-07-20 15:03:37 -0700590void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
591 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700592 if (error_event_.get()) {
593 // This shouldn't really happen.
594 LOG(WARNING) << "There's already an existing pending error event.";
595 return;
596 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700597
Darin Petkovabc7bc02011-02-23 14:39:43 -0800598 // For now assume that a generic Omaha response action failure means that
599 // there's no update so don't send an event. Also, double check that the
600 // failure has not occurred while sending an error event -- in which case
601 // don't schedule another. This shouldn't really happen but just in case...
602 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
603 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700604 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
605 return;
606 }
607
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700608 // Classify the code to generate the appropriate result so that
609 // the Borgmon charts show up the results correctly.
610 // Do this before calling GetErrorCodeForAction which could potentially
611 // augment the bit representation of code and thus cause no matches for
612 // the switch cases below.
613 OmahaEvent::Result event_result;
614 switch (code) {
615 case kActionCodeOmahaUpdateIgnoredPerPolicy:
616 case kActionCodeOmahaUpdateDeferredPerPolicy:
617 event_result = OmahaEvent::kResultUpdateDeferred;
618 break;
619 default:
620 event_result = OmahaEvent::kResultError;
621 break;
622 }
623
Darin Petkov777dbfa2010-07-20 15:03:37 -0700624 code = GetErrorCodeForAction(action, code);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700625 fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700626
627 // Apply the bit modifiers to the error code.
628 if (!utils::IsNormalBootMode()) {
629 code = static_cast<ActionExitCode>(code | kActionCodeBootModeFlag);
630 }
631 if (response_handler_action_.get() &&
632 response_handler_action_->install_plan().is_resume) {
633 code = static_cast<ActionExitCode>(code | kActionCodeResumedFlag);
634 }
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700635
Darin Petkov09f96c32010-07-20 09:24:57 -0700636 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700637 event_result,
Darin Petkov09f96c32010-07-20 09:24:57 -0700638 code));
639}
640
641bool UpdateAttempter::ScheduleErrorEventAction() {
642 if (error_event_.get() == NULL)
643 return false;
644
Darin Petkov1023a602010-08-30 13:47:51 -0700645 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700646 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700647 new OmahaRequestAction(prefs_,
648 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700649 error_event_.release(), // Pass ownership.
Thieu Le116fda32011-04-19 11:01:54 -0700650 new LibcurlHttpFetcher(GetProxyResolver()),
651 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700652 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700653 processor_->EnqueueAction(error_event_action.get());
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800654 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT,
655 kUpdateNoticeUnspecified);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700656 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700657 return true;
658}
659
Darin Petkovc6c135c2010-08-11 13:36:18 -0700660void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
661 if (priority_ == priority) {
662 return;
663 }
664 if (utils::SetProcessPriority(priority)) {
665 priority_ = priority;
666 LOG(INFO) << "Process priority = " << priority_;
667 }
668}
669
670void UpdateAttempter::SetupPriorityManagement() {
671 if (manage_priority_source_) {
672 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
673 CleanupPriorityManagement();
674 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700675 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700676 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
677 g_source_set_callback(manage_priority_source_,
678 StaticManagePriorityCallback,
679 this,
680 NULL);
681 g_source_attach(manage_priority_source_, NULL);
682 SetPriority(utils::kProcessPriorityLow);
683}
684
685void UpdateAttempter::CleanupPriorityManagement() {
686 if (manage_priority_source_) {
687 g_source_destroy(manage_priority_source_);
688 manage_priority_source_ = NULL;
689 }
690 SetPriority(utils::kProcessPriorityNormal);
691}
692
693gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
694 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
695}
696
Darin Petkove6ef2f82011-03-07 17:31:11 -0800697gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
698 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
699 return FALSE; // Don't call this callback again.
700}
701
Darin Petkov58dd1342011-05-06 12:05:13 -0700702void UpdateAttempter::ScheduleProcessingStart() {
703 LOG(INFO) << "Scheduling an action processor start.";
704 start_action_processor_ = false;
705 g_idle_add(&StaticStartProcessing, this);
706}
707
Darin Petkovc6c135c2010-08-11 13:36:18 -0700708bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700709 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700710 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700711 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700712}
713
Darin Petkov36275772010-10-01 11:40:57 -0700714void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
715 int64_t delta_failures;
716 if (omaha_request_params_.delta_okay &&
717 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
718 delta_failures >= kMaxDeltaUpdateFailures) {
719 LOG(WARNING) << "Too many delta update failures, forcing full update.";
720 omaha_request_params_.delta_okay = false;
721 }
722}
723
724void UpdateAttempter::MarkDeltaUpdateFailure() {
Darin Petkov2dd01092010-10-08 15:43:05 -0700725 // Don't try to resume a failed delta update.
726 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700727 int64_t delta_failures;
728 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
729 delta_failures < 0) {
730 delta_failures = 0;
731 }
732 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
733}
734
Darin Petkov9b230572010-10-08 10:20:09 -0700735void UpdateAttempter::SetupDownload() {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800736 MultiRangeHttpFetcher* fetcher =
737 dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800738 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700739 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700740 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700741 int64_t manifest_metadata_size = 0;
742 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800743 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700744 // If there're remaining unprocessed data blobs, fetch them. Be careful not
745 // to request data beyond the end of the payload to avoid 416 HTTP response
746 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700747 int64_t next_data_offset = 0;
748 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700749 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
750 if (resume_offset < response_handler_action_->install_plan().size) {
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800751 fetcher->AddRange(resume_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700752 }
Darin Petkov9b230572010-10-08 10:20:09 -0700753 } else {
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800754 fetcher->AddRange(0);
Darin Petkov9b230572010-10-08 10:20:09 -0700755 }
Darin Petkov9b230572010-10-08 10:20:09 -0700756}
757
Thieu Le116fda32011-04-19 11:01:54 -0700758void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -0700759 if (!processor_->IsRunning()) {
760 shared_ptr<OmahaRequestAction> ping_action(
761 new OmahaRequestAction(prefs_,
762 omaha_request_params_,
763 NULL,
764 new LibcurlHttpFetcher(GetProxyResolver()),
765 true));
766 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
767 processor_->set_delegate(NULL);
768 processor_->EnqueueAction(ping_action.get());
769 // Call StartProcessing() synchronously here to avoid any race conditions
770 // caused by multiple outstanding ping Omaha requests. If we call
771 // StartProcessing() asynchronously, the device can be suspended before we
772 // get a chance to callback to StartProcessing(). When the device resumes
773 // (assuming the device sleeps longer than the next update check period),
774 // StartProcessing() is called back and at the same time, the next update
775 // check is fired which eventually invokes StartProcessing(). A crash
776 // can occur because StartProcessing() checks to make sure that the
777 // processor is idle which it isn't due to the two concurrent ping Omaha
778 // requests.
779 processor_->StartProcessing();
780 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -0700781 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -0700782 }
Thieu Led88a8572011-05-26 09:09:19 -0700783
784 // Update the status which will schedule the next update check
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800785 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT,
786 kUpdateNoticeUnspecified);
Thieu Le116fda32011-04-19 11:01:54 -0700787}
788
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700789} // namespace chromeos_update_engine