blob: e79f692c0666a25675eec12f2e442f1028308729 [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
Darin Petkovcd1666f2010-09-23 09:53:44 -070049const char* kUpdateCompletedMarker =
50 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070051
Andrew de los Reyes45168102010-11-22 11:13:50 -080052namespace {
53const int kMaxConsecutiveObeyProxyRequests = 20;
54} // namespace {}
55
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070056const char* UpdateStatusToString(UpdateStatus status) {
57 switch (status) {
58 case UPDATE_STATUS_IDLE:
59 return "UPDATE_STATUS_IDLE";
60 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
61 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
62 case UPDATE_STATUS_UPDATE_AVAILABLE:
63 return "UPDATE_STATUS_UPDATE_AVAILABLE";
64 case UPDATE_STATUS_DOWNLOADING:
65 return "UPDATE_STATUS_DOWNLOADING";
66 case UPDATE_STATUS_VERIFYING:
67 return "UPDATE_STATUS_VERIFYING";
68 case UPDATE_STATUS_FINALIZING:
69 return "UPDATE_STATUS_FINALIZING";
70 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
71 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070072 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
73 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070074 default:
75 return "unknown status";
76 }
77}
78
Darin Petkov777dbfa2010-07-20 15:03:37 -070079// Turns a generic kActionCodeError to a generic error code specific
80// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
81// not kActionCodeError, or the action is not matched, returns |code|
82// unchanged.
83ActionExitCode GetErrorCodeForAction(AbstractAction* action,
84 ActionExitCode code) {
85 if (code != kActionCodeError)
86 return code;
87
88 const string type = action->Type();
89 if (type == OmahaRequestAction::StaticType())
90 return kActionCodeOmahaRequestError;
91 if (type == OmahaResponseHandlerAction::StaticType())
92 return kActionCodeOmahaResponseHandlerError;
93 if (type == FilesystemCopierAction::StaticType())
94 return kActionCodeFilesystemCopierError;
95 if (type == PostinstallRunnerAction::StaticType())
96 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070097
98 return code;
99}
100
Darin Petkovc6c135c2010-08-11 13:36:18 -0700101UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800102 MetricsLibraryInterface* metrics_lib,
103 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700104 : processor_(new ActionProcessor()),
105 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700106 prefs_(prefs),
107 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700108 update_check_scheduler_(NULL),
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700109 fake_update_success_(false),
Darin Petkov1023a602010-08-30 13:47:51 -0700110 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700111 priority_(utils::kProcessPriorityNormal),
112 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700113 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700114 status_(UPDATE_STATUS_IDLE),
115 download_progress_(0.0),
116 last_checked_time_(0),
117 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700118 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800119 proxy_manual_checks_(0),
120 obeying_proxies_(true),
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700121 chrome_proxy_resolver_(dbus_iface),
Darin Petkov58dd1342011-05-06 12:05:13 -0700122 updated_boot_flags_(false),
123 update_boot_flags_running_(false),
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200124 start_action_processor_(false),
125 policy_provider_(NULL) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700126 if (utils::FileExists(kUpdateCompletedMarker))
127 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
128}
129
130UpdateAttempter::~UpdateAttempter() {
131 CleanupPriorityManagement();
132}
133
Darin Petkov5a7f5652010-07-22 21:40:09 -0700134void UpdateAttempter::Update(const std::string& app_version,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800135 const std::string& omaha_url,
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700136 bool obey_proxies,
137 bool interactive) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800138 chrome_proxy_resolver_.Init();
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700139 fake_update_success_ = false;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700140 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Thieu Le116fda32011-04-19 11:01:54 -0700141 // Although we have applied an update, we still want to ping Omaha
142 // to ensure the number of active statistics is accurate.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700143 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
Thieu Le116fda32011-04-19 11:01:54 -0700144 << "reboot, we'll ping Omaha instead";
145 PingOmaha();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700146 return;
147 }
148 if (status_ != UPDATE_STATUS_IDLE) {
149 // Update in progress. Do nothing
150 return;
151 }
Darin Petkov1023a602010-08-30 13:47:51 -0700152 http_response_code_ = 0;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200153
154 // Lazy initialize the policy provider, or reload the latest policy data.
155 if (!policy_provider_.get()) {
156 policy_provider_.reset(new policy::PolicyProvider());
157 } else {
158 policy_provider_->Reload();
159 }
160
161 // If the release_track is specified by policy, that takes precedence.
162 string release_track;
163 if (policy_provider_->device_policy_is_loaded())
164 policy_provider_->GetDevicePolicy().GetReleaseChannel(&release_track);
165
166 if (!omaha_request_params_.Init(app_version, omaha_url, release_track)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700167 LOG(ERROR) << "Unable to initialize Omaha request device params.";
168 return;
169 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800170
Andrew de los Reyes45168102010-11-22 11:13:50 -0800171 obeying_proxies_ = true;
172 if (obey_proxies || proxy_manual_checks_ == 0) {
173 LOG(INFO) << "forced to obey proxies";
174 // If forced to obey proxies, every 20th request will not use proxies
175 proxy_manual_checks_++;
176 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
177 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
178 proxy_manual_checks_ = 0;
179 obeying_proxies_ = false;
180 }
181 } else if (base::RandInt(0, 4) == 0) {
182 obeying_proxies_ = false;
183 }
184 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
185 "check we are ignoring the proxy settings and using "
186 "direct connections.";
187
Darin Petkov36275772010-10-01 11:40:57 -0700188 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700189 CHECK(!processor_->IsRunning());
190 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700191
192 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800193 LibcurlHttpFetcher* update_check_fetcher =
194 new LibcurlHttpFetcher(GetProxyResolver());
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700195 // Try harder to connect to the network, esp when not interactive.
196 // See comment in libcurl_http_fetcher.cc.
197 update_check_fetcher->set_no_network_max_retries(interactive ? 1 : 3);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700198 update_check_fetcher->set_check_certificate(CertificateChecker::kUpdate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700199 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700200 new OmahaRequestAction(prefs_,
201 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700202 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700203 update_check_fetcher, // passes ownership
204 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700205 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700206 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700207 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800208 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700209 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800210 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700211 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700212 new OmahaRequestAction(prefs_,
213 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700214 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700215 OmahaEvent::kTypeUpdateDownloadStarted),
Thieu Le116fda32011-04-19 11:01:54 -0700216 new LibcurlHttpFetcher(GetProxyResolver()),
217 false));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700218 LibcurlHttpFetcher* download_fetcher =
219 new LibcurlHttpFetcher(GetProxyResolver());
220 download_fetcher->set_check_certificate(CertificateChecker::kDownload);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700221 shared_ptr<DownloadAction> download_action(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700222 new DownloadAction(prefs_,
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800223 new MultiRangeHttpFetcher(
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700224 download_fetcher))); // passes ownership
Darin Petkov8c2980e2010-07-16 15:16:49 -0700225 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700226 new OmahaRequestAction(prefs_,
227 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700228 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700229 OmahaEvent::kTypeUpdateDownloadFinished),
Thieu Le116fda32011-04-19 11:01:54 -0700230 new LibcurlHttpFetcher(GetProxyResolver()),
231 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800232 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
233 new FilesystemCopierAction(false, true));
234 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
235 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800236 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
237 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700238 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700239 new OmahaRequestAction(prefs_,
240 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700241 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700242 new LibcurlHttpFetcher(GetProxyResolver()),
243 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700244
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700245 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700246 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700247 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700248
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700249 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
250 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
251 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700252 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700253 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700254 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700255 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700256 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800257 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
258 actions_.push_back(shared_ptr<AbstractAction>(
259 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800260 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700261 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700263 // Enqueue the actions
264 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
265 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700266 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700267 }
268
269 // Bond them together. We have to use the leaf-types when calling
270 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700271 BondActions(update_check_action.get(),
272 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700273 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700274 filesystem_copier_action.get());
275 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700276 kernel_filesystem_copier_action.get());
277 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700278 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700279 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800280 filesystem_verifier_action.get());
281 BondActions(filesystem_verifier_action.get(),
282 kernel_filesystem_verifier_action.get());
283 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800284 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700285
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700286 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800287
Darin Petkov58dd1342011-05-06 12:05:13 -0700288 // Just in case we didn't update boot flags yet, make sure they're updated
289 // before any update processing starts.
290 start_action_processor_ = true;
291 UpdateBootFlags();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700292}
293
Darin Petkov5a7f5652010-07-22 21:40:09 -0700294void UpdateAttempter::CheckForUpdate(const std::string& app_version,
295 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700296 if (status_ != UPDATE_STATUS_IDLE) {
297 LOG(INFO) << "Check for update requested, but status is "
298 << UpdateStatusToString(status_) << ", so not checking.";
299 return;
300 }
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700301 Update(app_version, omaha_url, true, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700302}
303
Darin Petkov296889c2010-07-23 16:20:54 -0700304bool UpdateAttempter::RebootIfNeeded() {
305 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
306 LOG(INFO) << "Reboot requested, but status is "
307 << UpdateStatusToString(status_) << ", so not rebooting.";
308 return false;
309 }
310 TEST_AND_RETURN_FALSE(utils::Reboot());
311 return true;
312}
313
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700314// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700315void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700316 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700317 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700318 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700319 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700320
Darin Petkovc6c135c2010-08-11 13:36:18 -0700321 // Reset process priority back to normal.
322 CleanupPriorityManagement();
323
Darin Petkov09f96c32010-07-20 09:24:57 -0700324 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
325 LOG(INFO) << "Error event sent.";
326 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700327 if (!fake_update_success_) {
328 return;
329 }
330 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
331 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700332 }
333
Darin Petkovc1a8b422010-07-19 11:34:49 -0700334 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700335 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700336 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800337 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700338 DeltaPerformer::ResetUpdateProgress(prefs_, false);
339 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700340
341 // Report the time it took to update the system.
342 int64_t update_time = time(NULL) - last_checked_time_;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700343 if (!fake_update_success_)
344 metrics_lib_->SendToUMA("Installer.UpdateTime",
345 static_cast<int>(update_time), // sample
346 1, // min = 1 second
347 20 * 60, // max = 20 minutes
348 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700349 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700350 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700351
Darin Petkov1023a602010-08-30 13:47:51 -0700352 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700353 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700354 }
355 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700356 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700357}
358
359void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700360 // Reset process priority back to normal.
361 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700362 download_progress_ = 0.0;
363 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700364 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700365 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700366}
367
368// Called whenever an action has finished processing, either successfully
369// or otherwise.
370void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
371 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700372 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700373 // Reset download progress regardless of whether or not the download
374 // action succeeded. Also, get the response code from HTTP request
375 // actions (update download as well as the initial update check
376 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700377 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700378 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700379 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700380 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
381 http_response_code_ = download_action->GetHTTPResponseCode();
382 } else if (type == OmahaRequestAction::StaticType()) {
383 OmahaRequestAction* omaha_request_action =
384 dynamic_cast<OmahaRequestAction*>(action);
385 // If the request is not an event, then it's the update-check.
386 if (!omaha_request_action->IsEvent()) {
387 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700388 // Forward the server-dictated poll interval to the update check
389 // scheduler, if any.
390 if (update_check_scheduler_) {
391 update_check_scheduler_->set_poll_interval(
392 omaha_request_action->GetOutputObject().poll_interval);
393 }
Darin Petkov1023a602010-08-30 13:47:51 -0700394 }
395 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700396 if (code != kActionCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700397 // If the current state is at or past the download phase, count the failure
398 // in case a switch to full update becomes necessary. Ignore network
399 // transfer timeouts and failures.
Darin Petkov36275772010-10-01 11:40:57 -0700400 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
Darin Petkov36275772010-10-01 11:40:57 -0700401 code != kActionCodeDownloadTransferError) {
402 MarkDeltaUpdateFailure();
403 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700404 // On failure, schedule an error event to be sent to Omaha.
405 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700406 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700407 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700408 // Find out which action completed.
409 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700410 // Note that the status will be updated to DOWNLOADING when some bytes get
411 // actually downloaded from the server and the BytesReceived callback is
412 // invoked. This avoids notifying the user that a download has started in
413 // cases when the server and the client are unable to initiate the download.
414 CHECK(action == response_handler_action_.get());
415 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700416 last_checked_time_ = time(NULL);
417 // TODO(adlr): put version in InstallPlan
418 new_version_ = "0.0.0.0";
419 new_size_ = plan.size;
Darin Petkov9b230572010-10-08 10:20:09 -0700420 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700421 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700422 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700423 } else if (type == DownloadAction::StaticType()) {
424 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
425 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700426}
427
428// Stop updating. An attempt will be made to record status to the disk
429// so that updates can be resumed later.
430void UpdateAttempter::Terminate() {
431 // TODO(adlr): implement this method.
432 NOTIMPLEMENTED();
433}
434
435// Try to resume from a previously Terminate()d update.
436void UpdateAttempter::ResumeUpdating() {
437 // TODO(adlr): implement this method.
438 NOTIMPLEMENTED();
439}
440
Darin Petkov9d911fa2010-08-19 09:36:08 -0700441void UpdateAttempter::SetDownloadStatus(bool active) {
442 download_active_ = active;
443 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
444}
445
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700446void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700447 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700448 LOG(ERROR) << "BytesReceived called while not downloading.";
449 return;
450 }
Darin Petkovaf183052010-08-23 12:07:13 -0700451 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700452 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700453 // Self throttle based on progress. Also send notifications if
454 // progress is too slow.
455 const double kDeltaPercent = 0.01; // 1%
456 if (status_ != UPDATE_STATUS_DOWNLOADING ||
457 bytes_received == total ||
458 progress - download_progress_ >= kDeltaPercent ||
459 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
460 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700461 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
462 }
463}
464
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700465bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
466 double* progress,
467 std::string* current_operation,
468 std::string* new_version,
469 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700470 *last_checked_time = last_checked_time_;
471 *progress = download_progress_;
472 *current_operation = UpdateStatusToString(status_);
473 *new_version = new_version_;
474 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700475 return true;
476}
477
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700478void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700479 if (update_boot_flags_running_) {
480 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700481 return;
482 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700483 if (updated_boot_flags_) {
484 LOG(INFO) << "Already updated boot flags. Skipping.";
485 if (start_action_processor_) {
486 ScheduleProcessingStart();
487 }
488 return;
489 }
490 // This is purely best effort. Failures should be logged by Subprocess. Run
491 // the script asynchronously to avoid blocking the event loop regardless of
492 // the script runtime.
493 update_boot_flags_running_ = true;
494 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700495 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700496 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
497 CompleteUpdateBootFlags(1);
498 }
499}
500
501void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
502 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700503 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700504 if (start_action_processor_) {
505 ScheduleProcessingStart();
506 }
507}
508
509void UpdateAttempter::StaticCompleteUpdateBootFlags(
510 int return_code,
511 const std::string& output,
512 void* p) {
513 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700514}
515
Darin Petkov61635a92011-05-18 16:20:36 -0700516void UpdateAttempter::BroadcastStatus() {
517 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700518 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700519 }
Darin Petkovaf183052010-08-23 12:07:13 -0700520 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700521 update_engine_service_emit_status_update(
522 dbus_service_,
523 last_checked_time_,
524 download_progress_,
525 UpdateStatusToString(status_),
526 new_version_.c_str(),
527 new_size_);
528}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700529
Darin Petkov61635a92011-05-18 16:20:36 -0700530void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
531 status_ = status;
532 if (update_check_scheduler_) {
533 update_check_scheduler_->SetUpdateStatus(status_);
534 }
535 BroadcastStatus();
536}
537
Darin Petkov777dbfa2010-07-20 15:03:37 -0700538void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
539 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700540 if (error_event_.get()) {
541 // This shouldn't really happen.
542 LOG(WARNING) << "There's already an existing pending error event.";
543 return;
544 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700545
Darin Petkovabc7bc02011-02-23 14:39:43 -0800546 // For now assume that a generic Omaha response action failure means that
547 // there's no update so don't send an event. Also, double check that the
548 // failure has not occurred while sending an error event -- in which case
549 // don't schedule another. This shouldn't really happen but just in case...
550 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
551 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700552 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
553 return;
554 }
555
556 code = GetErrorCodeForAction(action, code);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700557 fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700558
559 // Apply the bit modifiers to the error code.
560 if (!utils::IsNormalBootMode()) {
561 code = static_cast<ActionExitCode>(code | kActionCodeBootModeFlag);
562 }
563 if (response_handler_action_.get() &&
564 response_handler_action_->install_plan().is_resume) {
565 code = static_cast<ActionExitCode>(code | kActionCodeResumedFlag);
566 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700567 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
568 OmahaEvent::kResultError,
569 code));
570}
571
572bool UpdateAttempter::ScheduleErrorEventAction() {
573 if (error_event_.get() == NULL)
574 return false;
575
Darin Petkov1023a602010-08-30 13:47:51 -0700576 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700577 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700578 new OmahaRequestAction(prefs_,
579 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700580 error_event_.release(), // Pass ownership.
Thieu Le116fda32011-04-19 11:01:54 -0700581 new LibcurlHttpFetcher(GetProxyResolver()),
582 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700583 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700584 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700585 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700586 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700587 return true;
588}
589
Darin Petkovc6c135c2010-08-11 13:36:18 -0700590void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
591 if (priority_ == priority) {
592 return;
593 }
594 if (utils::SetProcessPriority(priority)) {
595 priority_ = priority;
596 LOG(INFO) << "Process priority = " << priority_;
597 }
598}
599
600void UpdateAttempter::SetupPriorityManagement() {
601 if (manage_priority_source_) {
602 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
603 CleanupPriorityManagement();
604 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700605 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700606 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
607 g_source_set_callback(manage_priority_source_,
608 StaticManagePriorityCallback,
609 this,
610 NULL);
611 g_source_attach(manage_priority_source_, NULL);
612 SetPriority(utils::kProcessPriorityLow);
613}
614
615void UpdateAttempter::CleanupPriorityManagement() {
616 if (manage_priority_source_) {
617 g_source_destroy(manage_priority_source_);
618 manage_priority_source_ = NULL;
619 }
620 SetPriority(utils::kProcessPriorityNormal);
621}
622
623gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
624 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
625}
626
Darin Petkove6ef2f82011-03-07 17:31:11 -0800627gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
628 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
629 return FALSE; // Don't call this callback again.
630}
631
Darin Petkov58dd1342011-05-06 12:05:13 -0700632void UpdateAttempter::ScheduleProcessingStart() {
633 LOG(INFO) << "Scheduling an action processor start.";
634 start_action_processor_ = false;
635 g_idle_add(&StaticStartProcessing, this);
636}
637
Darin Petkovc6c135c2010-08-11 13:36:18 -0700638bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700639 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700640 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700641 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700642}
643
Darin Petkov36275772010-10-01 11:40:57 -0700644void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
645 int64_t delta_failures;
646 if (omaha_request_params_.delta_okay &&
647 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
648 delta_failures >= kMaxDeltaUpdateFailures) {
649 LOG(WARNING) << "Too many delta update failures, forcing full update.";
650 omaha_request_params_.delta_okay = false;
651 }
652}
653
654void UpdateAttempter::MarkDeltaUpdateFailure() {
Darin Petkov2dd01092010-10-08 15:43:05 -0700655 // Don't try to resume a failed delta update.
656 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700657 int64_t delta_failures;
658 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
659 delta_failures < 0) {
660 delta_failures = 0;
661 }
662 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
663}
664
Darin Petkov9b230572010-10-08 10:20:09 -0700665void UpdateAttempter::SetupDownload() {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800666 MultiRangeHttpFetcher* fetcher =
667 dynamic_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800668 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700669 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700670 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700671 int64_t manifest_metadata_size = 0;
672 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800673 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700674 // If there're remaining unprocessed data blobs, fetch them. Be careful not
675 // to request data beyond the end of the payload to avoid 416 HTTP response
676 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700677 int64_t next_data_offset = 0;
678 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700679 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
680 if (resume_offset < response_handler_action_->install_plan().size) {
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800681 fetcher->AddRange(resume_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700682 }
Darin Petkov9b230572010-10-08 10:20:09 -0700683 } else {
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800684 fetcher->AddRange(0);
Darin Petkov9b230572010-10-08 10:20:09 -0700685 }
Darin Petkov9b230572010-10-08 10:20:09 -0700686}
687
Thieu Le116fda32011-04-19 11:01:54 -0700688void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -0700689 if (!processor_->IsRunning()) {
690 shared_ptr<OmahaRequestAction> ping_action(
691 new OmahaRequestAction(prefs_,
692 omaha_request_params_,
693 NULL,
694 new LibcurlHttpFetcher(GetProxyResolver()),
695 true));
696 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
697 processor_->set_delegate(NULL);
698 processor_->EnqueueAction(ping_action.get());
699 // Call StartProcessing() synchronously here to avoid any race conditions
700 // caused by multiple outstanding ping Omaha requests. If we call
701 // StartProcessing() asynchronously, the device can be suspended before we
702 // get a chance to callback to StartProcessing(). When the device resumes
703 // (assuming the device sleeps longer than the next update check period),
704 // StartProcessing() is called back and at the same time, the next update
705 // check is fired which eventually invokes StartProcessing(). A crash
706 // can occur because StartProcessing() checks to make sure that the
707 // processor is idle which it isn't due to the two concurrent ping Omaha
708 // requests.
709 processor_->StartProcessing();
710 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -0700711 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -0700712 }
Thieu Led88a8572011-05-26 09:09:19 -0700713
714 // Update the status which will schedule the next update check
Thieu Le116fda32011-04-19 11:01:54 -0700715 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
716}
717
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700718} // namespace chromeos_update_engine