blob: a1efa95c58b25d9fabbafcadf2b36eb3962b18a1 [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>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070020
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070021#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070022#include "update_engine/download_action.h"
23#include "update_engine/filesystem_copier_action.h"
24#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyes819fef22010-12-17 11:33:58 -080025#include "update_engine/multi_range_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070026#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070028#include "update_engine/omaha_response_handler_action.h"
29#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070030#include "update_engine/prefs_interface.h"
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -070031#include "update_engine/subprocess.h"
Darin Petkov1023a602010-08-30 13:47:51 -070032#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070033
Darin Petkovaf183052010-08-23 12:07:13 -070034using base::TimeDelta;
35using base::TimeTicks;
Andrew de los Reyes21816e12011-04-07 14:18:56 -070036using google::protobuf::NewPermanentCallback;
Darin Petkov9b230572010-10-08 10:20:09 -070037using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070038using std::tr1::shared_ptr;
39using std::string;
40using std::vector;
41
42namespace chromeos_update_engine {
43
Darin Petkov36275772010-10-01 11:40:57 -070044const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
45
Darin Petkovcd1666f2010-09-23 09:53:44 -070046const char* kUpdateCompletedMarker =
47 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070048
Andrew de los Reyes45168102010-11-22 11:13:50 -080049namespace {
50const int kMaxConsecutiveObeyProxyRequests = 20;
51} // namespace {}
52
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070053const char* UpdateStatusToString(UpdateStatus status) {
54 switch (status) {
55 case UPDATE_STATUS_IDLE:
56 return "UPDATE_STATUS_IDLE";
57 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
58 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
59 case UPDATE_STATUS_UPDATE_AVAILABLE:
60 return "UPDATE_STATUS_UPDATE_AVAILABLE";
61 case UPDATE_STATUS_DOWNLOADING:
62 return "UPDATE_STATUS_DOWNLOADING";
63 case UPDATE_STATUS_VERIFYING:
64 return "UPDATE_STATUS_VERIFYING";
65 case UPDATE_STATUS_FINALIZING:
66 return "UPDATE_STATUS_FINALIZING";
67 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
68 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070069 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
70 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070071 default:
72 return "unknown status";
73 }
74}
75
Darin Petkov777dbfa2010-07-20 15:03:37 -070076// Turns a generic kActionCodeError to a generic error code specific
77// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
78// not kActionCodeError, or the action is not matched, returns |code|
79// unchanged.
80ActionExitCode GetErrorCodeForAction(AbstractAction* action,
81 ActionExitCode code) {
82 if (code != kActionCodeError)
83 return code;
84
85 const string type = action->Type();
86 if (type == OmahaRequestAction::StaticType())
87 return kActionCodeOmahaRequestError;
88 if (type == OmahaResponseHandlerAction::StaticType())
89 return kActionCodeOmahaResponseHandlerError;
90 if (type == FilesystemCopierAction::StaticType())
91 return kActionCodeFilesystemCopierError;
92 if (type == PostinstallRunnerAction::StaticType())
93 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070094
95 return code;
96}
97
Darin Petkovc6c135c2010-08-11 13:36:18 -070098UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -080099 MetricsLibraryInterface* metrics_lib,
100 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700101 : processor_(new ActionProcessor()),
102 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700103 prefs_(prefs),
104 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700105 update_check_scheduler_(NULL),
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700106 fake_update_success_(false),
Darin Petkov1023a602010-08-30 13:47:51 -0700107 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700108 priority_(utils::kProcessPriorityNormal),
109 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700110 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700111 status_(UPDATE_STATUS_IDLE),
112 download_progress_(0.0),
113 last_checked_time_(0),
114 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700115 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800116 is_full_update_(false),
117 proxy_manual_checks_(0),
118 obeying_proxies_(true),
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700119 chrome_proxy_resolver_(dbus_iface),
Darin Petkov58dd1342011-05-06 12:05:13 -0700120 updated_boot_flags_(false),
121 update_boot_flags_running_(false),
122 start_action_processor_(false) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700123 if (utils::FileExists(kUpdateCompletedMarker))
124 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
125}
126
127UpdateAttempter::~UpdateAttempter() {
128 CleanupPriorityManagement();
129}
130
Darin Petkov5a7f5652010-07-22 21:40:09 -0700131void UpdateAttempter::Update(const std::string& app_version,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800132 const std::string& omaha_url,
133 bool obey_proxies) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800134 chrome_proxy_resolver_.Init();
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700135 fake_update_success_ = false;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700136 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Thieu Le116fda32011-04-19 11:01:54 -0700137 // Although we have applied an update, we still want to ping Omaha
138 // to ensure the number of active statistics is accurate.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700139 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
Thieu Le116fda32011-04-19 11:01:54 -0700140 << "reboot, we'll ping Omaha instead";
141 PingOmaha();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700142 return;
143 }
144 if (status_ != UPDATE_STATUS_IDLE) {
145 // Update in progress. Do nothing
146 return;
147 }
Darin Petkov1023a602010-08-30 13:47:51 -0700148 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700149 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700150 LOG(ERROR) << "Unable to initialize Omaha request device params.";
151 return;
152 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800153
Andrew de los Reyes45168102010-11-22 11:13:50 -0800154 obeying_proxies_ = true;
155 if (obey_proxies || proxy_manual_checks_ == 0) {
156 LOG(INFO) << "forced to obey proxies";
157 // If forced to obey proxies, every 20th request will not use proxies
158 proxy_manual_checks_++;
159 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
160 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
161 proxy_manual_checks_ = 0;
162 obeying_proxies_ = false;
163 }
164 } else if (base::RandInt(0, 4) == 0) {
165 obeying_proxies_ = false;
166 }
167 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
168 "check we are ignoring the proxy settings and using "
169 "direct connections.";
170
Darin Petkov36275772010-10-01 11:40:57 -0700171 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700172 CHECK(!processor_->IsRunning());
173 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700174
175 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800176 LibcurlHttpFetcher* update_check_fetcher =
177 new LibcurlHttpFetcher(GetProxyResolver());
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -0800178 // Try harder to connect to the network. See comment in
179 // libcurl_http_fetcher.cc.
180 update_check_fetcher->set_no_network_max_retries(3);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700181 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700182 new OmahaRequestAction(prefs_,
183 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700184 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700185 update_check_fetcher, // passes ownership
186 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700187 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700188 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700189 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800190 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700191 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800192 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700193 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700194 new OmahaRequestAction(prefs_,
195 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700196 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700197 OmahaEvent::kTypeUpdateDownloadStarted),
Thieu Le116fda32011-04-19 11:01:54 -0700198 new LibcurlHttpFetcher(GetProxyResolver()),
199 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700200 shared_ptr<DownloadAction> download_action(
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800201 new DownloadAction(prefs_, new MultiRangeHTTPFetcher(
202 new LibcurlHttpFetcher(GetProxyResolver()))));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700203 // This action is always initially in place to warn OS vendor of a
204 // signature failure. If it's not needed, it will be told to skip.
205 shared_ptr<OmahaRequestAction> download_signature_warning(
206 new OmahaRequestAction(
207 prefs_,
208 omaha_request_params_,
209 new OmahaEvent(
210 OmahaEvent::kTypeUpdateDownloadFinished,
211 OmahaEvent::kResultError,
212 kActionCodeDownloadPayloadPubKeyVerificationError),
Thieu Le116fda32011-04-19 11:01:54 -0700213 new LibcurlHttpFetcher(GetProxyResolver()),
214 false));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700215 download_action->set_skip_reporting_signature_fail(
216 NewPermanentCallback(download_signature_warning.get(),
217 &OmahaRequestAction::set_should_skip,
218 true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700219 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700220 new OmahaRequestAction(prefs_,
221 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700222 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700223 OmahaEvent::kTypeUpdateDownloadFinished),
Thieu Le116fda32011-04-19 11:01:54 -0700224 new LibcurlHttpFetcher(GetProxyResolver()),
225 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800226 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
227 new FilesystemCopierAction(false, true));
228 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
229 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800230 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
231 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700232 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700233 new OmahaRequestAction(prefs_,
234 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700235 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700236 new LibcurlHttpFetcher(GetProxyResolver()),
237 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700239 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700240 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700241 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700242
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700243 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
244 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
245 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700246 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700247 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700248 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700249 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700250 actions_.push_back(shared_ptr<AbstractAction>(download_signature_warning));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700251 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800252 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
253 actions_.push_back(shared_ptr<AbstractAction>(
254 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800255 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700256 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700257
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700258 // Enqueue the actions
259 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
260 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700261 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700262 }
263
264 // Bond them together. We have to use the leaf-types when calling
265 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700266 BondActions(update_check_action.get(),
267 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700268 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700269 filesystem_copier_action.get());
270 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700271 kernel_filesystem_copier_action.get());
272 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700273 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700274 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800275 filesystem_verifier_action.get());
276 BondActions(filesystem_verifier_action.get(),
277 kernel_filesystem_verifier_action.get());
278 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800279 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700280
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700281 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800282
Darin Petkov58dd1342011-05-06 12:05:13 -0700283 // Just in case we didn't update boot flags yet, make sure they're updated
284 // before any update processing starts.
285 start_action_processor_ = true;
286 UpdateBootFlags();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700287}
288
Darin Petkov5a7f5652010-07-22 21:40:09 -0700289void UpdateAttempter::CheckForUpdate(const std::string& app_version,
290 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700291 if (status_ != UPDATE_STATUS_IDLE) {
292 LOG(INFO) << "Check for update requested, but status is "
293 << UpdateStatusToString(status_) << ", so not checking.";
294 return;
295 }
Andrew de los Reyes45168102010-11-22 11:13:50 -0800296 Update(app_version, omaha_url, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700297}
298
Darin Petkov296889c2010-07-23 16:20:54 -0700299bool UpdateAttempter::RebootIfNeeded() {
300 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
301 LOG(INFO) << "Reboot requested, but status is "
302 << UpdateStatusToString(status_) << ", so not rebooting.";
303 return false;
304 }
305 TEST_AND_RETURN_FALSE(utils::Reboot());
306 return true;
307}
308
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700309// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700310void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700311 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700312 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700313 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700314 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700315
Darin Petkovc6c135c2010-08-11 13:36:18 -0700316 // Reset process priority back to normal.
317 CleanupPriorityManagement();
318
Darin Petkov09f96c32010-07-20 09:24:57 -0700319 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
320 LOG(INFO) << "Error event sent.";
321 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700322 if (!fake_update_success_) {
323 return;
324 }
325 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
326 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700327 }
328
Darin Petkovc1a8b422010-07-19 11:34:49 -0700329 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700330 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700331 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800332 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700333 DeltaPerformer::ResetUpdateProgress(prefs_, false);
334 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700335
336 // Report the time it took to update the system.
337 int64_t update_time = time(NULL) - last_checked_time_;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700338 if (!fake_update_success_)
339 metrics_lib_->SendToUMA("Installer.UpdateTime",
340 static_cast<int>(update_time), // sample
341 1, // min = 1 second
342 20 * 60, // max = 20 minutes
343 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700344 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700345 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700346
Darin Petkov1023a602010-08-30 13:47:51 -0700347 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700348 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700349 }
350 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700351 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700352}
353
354void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700355 // Reset process priority back to normal.
356 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700357 download_progress_ = 0.0;
358 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700359 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700360 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700361}
362
363// Called whenever an action has finished processing, either successfully
364// or otherwise.
365void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
366 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700367 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700368 // Reset download progress regardless of whether or not the download
369 // action succeeded. Also, get the response code from HTTP request
370 // actions (update download as well as the initial update check
371 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700372 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700373 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700374 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700375 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
376 http_response_code_ = download_action->GetHTTPResponseCode();
377 } else if (type == OmahaRequestAction::StaticType()) {
378 OmahaRequestAction* omaha_request_action =
379 dynamic_cast<OmahaRequestAction*>(action);
380 // If the request is not an event, then it's the update-check.
381 if (!omaha_request_action->IsEvent()) {
382 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700383 // Forward the server-dictated poll interval to the update check
384 // scheduler, if any.
385 if (update_check_scheduler_) {
386 update_check_scheduler_->set_poll_interval(
387 omaha_request_action->GetOutputObject().poll_interval);
388 }
Darin Petkov1023a602010-08-30 13:47:51 -0700389 }
390 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700391 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700392 // If this was a delta update attempt and the current state is at or past
393 // the download phase, count the failure in case a switch to full update
394 // becomes necessary. Ignore network transfer timeouts and failures.
395 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
396 !is_full_update_ &&
397 code != kActionCodeDownloadTransferError) {
398 MarkDeltaUpdateFailure();
399 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700400 // On failure, schedule an error event to be sent to Omaha.
401 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700402 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700403 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700404 // Find out which action completed.
405 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700406 // Note that the status will be updated to DOWNLOADING when some bytes get
407 // actually downloaded from the server and the BytesReceived callback is
408 // invoked. This avoids notifying the user that a download has started in
409 // cases when the server and the client are unable to initiate the download.
410 CHECK(action == response_handler_action_.get());
411 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700412 last_checked_time_ = time(NULL);
413 // TODO(adlr): put version in InstallPlan
414 new_version_ = "0.0.0.0";
415 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700416 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700417 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700418 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700419 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700420 } else if (type == DownloadAction::StaticType()) {
421 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
422 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700423}
424
425// Stop updating. An attempt will be made to record status to the disk
426// so that updates can be resumed later.
427void UpdateAttempter::Terminate() {
428 // TODO(adlr): implement this method.
429 NOTIMPLEMENTED();
430}
431
432// Try to resume from a previously Terminate()d update.
433void UpdateAttempter::ResumeUpdating() {
434 // TODO(adlr): implement this method.
435 NOTIMPLEMENTED();
436}
437
Darin Petkov9d911fa2010-08-19 09:36:08 -0700438void UpdateAttempter::SetDownloadStatus(bool active) {
439 download_active_ = active;
440 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
441}
442
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700443void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700444 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700445 LOG(ERROR) << "BytesReceived called while not downloading.";
446 return;
447 }
Darin Petkovaf183052010-08-23 12:07:13 -0700448 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700449 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700450 // Self throttle based on progress. Also send notifications if
451 // progress is too slow.
452 const double kDeltaPercent = 0.01; // 1%
453 if (status_ != UPDATE_STATUS_DOWNLOADING ||
454 bytes_received == total ||
455 progress - download_progress_ >= kDeltaPercent ||
456 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
457 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700458 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
459 }
460}
461
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700462bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
463 double* progress,
464 std::string* current_operation,
465 std::string* new_version,
466 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700467 *last_checked_time = last_checked_time_;
468 *progress = download_progress_;
469 *current_operation = UpdateStatusToString(status_);
470 *new_version = new_version_;
471 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700472 return true;
473}
474
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700475void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700476 if (update_boot_flags_running_) {
477 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700478 return;
479 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700480 if (updated_boot_flags_) {
481 LOG(INFO) << "Already updated boot flags. Skipping.";
482 if (start_action_processor_) {
483 ScheduleProcessingStart();
484 }
485 return;
486 }
487 // This is purely best effort. Failures should be logged by Subprocess. Run
488 // the script asynchronously to avoid blocking the event loop regardless of
489 // the script runtime.
490 update_boot_flags_running_ = true;
491 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700492 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700493 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
494 CompleteUpdateBootFlags(1);
495 }
496}
497
498void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
499 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700500 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700501 if (start_action_processor_) {
502 ScheduleProcessingStart();
503 }
504}
505
506void UpdateAttempter::StaticCompleteUpdateBootFlags(
507 int return_code,
508 const std::string& output,
509 void* p) {
510 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700511}
512
Darin Petkov61635a92011-05-18 16:20:36 -0700513void UpdateAttempter::BroadcastStatus() {
514 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700515 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700516 }
Darin Petkovaf183052010-08-23 12:07:13 -0700517 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700518 update_engine_service_emit_status_update(
519 dbus_service_,
520 last_checked_time_,
521 download_progress_,
522 UpdateStatusToString(status_),
523 new_version_.c_str(),
524 new_size_);
525}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700526
Darin Petkov61635a92011-05-18 16:20:36 -0700527void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
528 status_ = status;
529 if (update_check_scheduler_) {
530 update_check_scheduler_->SetUpdateStatus(status_);
531 }
532 BroadcastStatus();
533}
534
Darin Petkov777dbfa2010-07-20 15:03:37 -0700535void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
536 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700537 if (error_event_.get()) {
538 // This shouldn't really happen.
539 LOG(WARNING) << "There's already an existing pending error event.";
540 return;
541 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700542
Darin Petkovabc7bc02011-02-23 14:39:43 -0800543 // For now assume that a generic Omaha response action failure means that
544 // there's no update so don't send an event. Also, double check that the
545 // failure has not occurred while sending an error event -- in which case
546 // don't schedule another. This shouldn't really happen but just in case...
547 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
548 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700549 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
550 return;
551 }
552
553 code = GetErrorCodeForAction(action, code);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700554 fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB;
Darin Petkov09f96c32010-07-20 09:24:57 -0700555 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
556 OmahaEvent::kResultError,
557 code));
558}
559
560bool UpdateAttempter::ScheduleErrorEventAction() {
561 if (error_event_.get() == NULL)
562 return false;
563
Darin Petkov1023a602010-08-30 13:47:51 -0700564 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700565 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700566 new OmahaRequestAction(prefs_,
567 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700568 error_event_.release(), // Pass ownership.
Thieu Le116fda32011-04-19 11:01:54 -0700569 new LibcurlHttpFetcher(GetProxyResolver()),
570 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700571 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700572 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700573 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700574 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700575 return true;
576}
577
Darin Petkovc6c135c2010-08-11 13:36:18 -0700578void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
579 if (priority_ == priority) {
580 return;
581 }
582 if (utils::SetProcessPriority(priority)) {
583 priority_ = priority;
584 LOG(INFO) << "Process priority = " << priority_;
585 }
586}
587
588void UpdateAttempter::SetupPriorityManagement() {
589 if (manage_priority_source_) {
590 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
591 CleanupPriorityManagement();
592 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700593 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700594 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
595 g_source_set_callback(manage_priority_source_,
596 StaticManagePriorityCallback,
597 this,
598 NULL);
599 g_source_attach(manage_priority_source_, NULL);
600 SetPriority(utils::kProcessPriorityLow);
601}
602
603void UpdateAttempter::CleanupPriorityManagement() {
604 if (manage_priority_source_) {
605 g_source_destroy(manage_priority_source_);
606 manage_priority_source_ = NULL;
607 }
608 SetPriority(utils::kProcessPriorityNormal);
609}
610
611gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
612 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
613}
614
Darin Petkove6ef2f82011-03-07 17:31:11 -0800615gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
616 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
617 return FALSE; // Don't call this callback again.
618}
619
Darin Petkov58dd1342011-05-06 12:05:13 -0700620void UpdateAttempter::ScheduleProcessingStart() {
621 LOG(INFO) << "Scheduling an action processor start.";
622 start_action_processor_ = false;
623 g_idle_add(&StaticStartProcessing, this);
624}
625
Darin Petkovc6c135c2010-08-11 13:36:18 -0700626bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700627 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700628 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700629 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700630}
631
Darin Petkov36275772010-10-01 11:40:57 -0700632void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
633 int64_t delta_failures;
634 if (omaha_request_params_.delta_okay &&
635 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
636 delta_failures >= kMaxDeltaUpdateFailures) {
637 LOG(WARNING) << "Too many delta update failures, forcing full update.";
638 omaha_request_params_.delta_okay = false;
639 }
640}
641
642void UpdateAttempter::MarkDeltaUpdateFailure() {
643 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700644 // Don't try to resume a failed delta update.
645 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700646 int64_t delta_failures;
647 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
648 delta_failures < 0) {
649 delta_failures = 0;
650 }
651 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
652}
653
Darin Petkov9b230572010-10-08 10:20:09 -0700654void UpdateAttempter::SetupDownload() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800655 MultiRangeHTTPFetcher* fetcher =
656 dynamic_cast<MultiRangeHTTPFetcher*>(download_action_->http_fetcher());
657 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700658 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700659 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700660 int64_t manifest_metadata_size = 0;
661 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800662 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700663 // If there're remaining unprocessed data blobs, fetch them. Be careful not
664 // to request data beyond the end of the payload to avoid 416 HTTP response
665 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700666 int64_t next_data_offset = 0;
667 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700668 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
669 if (resume_offset < response_handler_action_->install_plan().size) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800670 fetcher->AddRange(resume_offset, -1);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700671 }
Darin Petkov9b230572010-10-08 10:20:09 -0700672 } else {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800673 fetcher->AddRange(0, -1);
Darin Petkov9b230572010-10-08 10:20:09 -0700674 }
Darin Petkov9b230572010-10-08 10:20:09 -0700675}
676
Thieu Le116fda32011-04-19 11:01:54 -0700677void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -0700678 if (!processor_->IsRunning()) {
679 shared_ptr<OmahaRequestAction> ping_action(
680 new OmahaRequestAction(prefs_,
681 omaha_request_params_,
682 NULL,
683 new LibcurlHttpFetcher(GetProxyResolver()),
684 true));
685 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
686 processor_->set_delegate(NULL);
687 processor_->EnqueueAction(ping_action.get());
688 // Call StartProcessing() synchronously here to avoid any race conditions
689 // caused by multiple outstanding ping Omaha requests. If we call
690 // StartProcessing() asynchronously, the device can be suspended before we
691 // get a chance to callback to StartProcessing(). When the device resumes
692 // (assuming the device sleeps longer than the next update check period),
693 // StartProcessing() is called back and at the same time, the next update
694 // check is fired which eventually invokes StartProcessing(). A crash
695 // can occur because StartProcessing() checks to make sure that the
696 // processor is idle which it isn't due to the two concurrent ping Omaha
697 // requests.
698 processor_->StartProcessing();
699 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -0700700 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -0700701 }
Thieu Led88a8572011-05-26 09:09:19 -0700702
703 // Update the status which will schedule the next update check
Thieu Le116fda32011-04-19 11:01:54 -0700704 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
705}
706
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700707} // namespace chromeos_update_engine