blob: 61c9947a787b21246200d20b33bf7019aa21b908 [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// 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),
106 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700107 priority_(utils::kProcessPriorityNormal),
108 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700109 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700110 status_(UPDATE_STATUS_IDLE),
111 download_progress_(0.0),
112 last_checked_time_(0),
113 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700114 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800115 is_full_update_(false),
116 proxy_manual_checks_(0),
117 obeying_proxies_(true),
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700118 chrome_proxy_resolver_(dbus_iface),
119 updated_boot_flags_(false) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700120 if (utils::FileExists(kUpdateCompletedMarker))
121 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
122}
123
124UpdateAttempter::~UpdateAttempter() {
125 CleanupPriorityManagement();
126}
127
Darin Petkov5a7f5652010-07-22 21:40:09 -0700128void UpdateAttempter::Update(const std::string& app_version,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800129 const std::string& omaha_url,
130 bool obey_proxies) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800131 chrome_proxy_resolver_.Init();
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700132 UpdateBootFlags(); // Just in case we didn't do this yet.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700133 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Thieu Le116fda32011-04-19 11:01:54 -0700134 // Although we have applied an update, we still want to ping Omaha
135 // to ensure the number of active statistics is accurate.
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700136 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
Thieu Le116fda32011-04-19 11:01:54 -0700137 << "reboot, we'll ping Omaha instead";
138 PingOmaha();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700139 return;
140 }
141 if (status_ != UPDATE_STATUS_IDLE) {
142 // Update in progress. Do nothing
143 return;
144 }
Darin Petkov1023a602010-08-30 13:47:51 -0700145 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700146 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700147 LOG(ERROR) << "Unable to initialize Omaha request device params.";
148 return;
149 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800150
Andrew de los Reyes45168102010-11-22 11:13:50 -0800151 obeying_proxies_ = true;
152 if (obey_proxies || proxy_manual_checks_ == 0) {
153 LOG(INFO) << "forced to obey proxies";
154 // If forced to obey proxies, every 20th request will not use proxies
155 proxy_manual_checks_++;
156 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
157 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
158 proxy_manual_checks_ = 0;
159 obeying_proxies_ = false;
160 }
161 } else if (base::RandInt(0, 4) == 0) {
162 obeying_proxies_ = false;
163 }
164 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
165 "check we are ignoring the proxy settings and using "
166 "direct connections.";
167
Darin Petkov36275772010-10-01 11:40:57 -0700168 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700169 CHECK(!processor_->IsRunning());
170 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700171
172 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800173 LibcurlHttpFetcher* update_check_fetcher =
174 new LibcurlHttpFetcher(GetProxyResolver());
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -0800175 // Try harder to connect to the network. See comment in
176 // libcurl_http_fetcher.cc.
177 update_check_fetcher->set_no_network_max_retries(3);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700178 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700179 new OmahaRequestAction(prefs_,
180 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700181 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700182 update_check_fetcher, // passes ownership
183 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700184 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700185 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700186 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800187 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700188 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800189 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700190 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700191 new OmahaRequestAction(prefs_,
192 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700193 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700194 OmahaEvent::kTypeUpdateDownloadStarted),
Thieu Le116fda32011-04-19 11:01:54 -0700195 new LibcurlHttpFetcher(GetProxyResolver()),
196 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700197 shared_ptr<DownloadAction> download_action(
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800198 new DownloadAction(prefs_, new MultiRangeHTTPFetcher(
199 new LibcurlHttpFetcher(GetProxyResolver()))));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700200 // This action is always initially in place to warn OS vendor of a
201 // signature failure. If it's not needed, it will be told to skip.
202 shared_ptr<OmahaRequestAction> download_signature_warning(
203 new OmahaRequestAction(
204 prefs_,
205 omaha_request_params_,
206 new OmahaEvent(
207 OmahaEvent::kTypeUpdateDownloadFinished,
208 OmahaEvent::kResultError,
209 kActionCodeDownloadPayloadPubKeyVerificationError),
Thieu Le116fda32011-04-19 11:01:54 -0700210 new LibcurlHttpFetcher(GetProxyResolver()),
211 false));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700212 download_action->set_skip_reporting_signature_fail(
213 NewPermanentCallback(download_signature_warning.get(),
214 &OmahaRequestAction::set_should_skip,
215 true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700216 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700217 new OmahaRequestAction(prefs_,
218 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700219 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700220 OmahaEvent::kTypeUpdateDownloadFinished),
Thieu Le116fda32011-04-19 11:01:54 -0700221 new LibcurlHttpFetcher(GetProxyResolver()),
222 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800223 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
224 new FilesystemCopierAction(false, true));
225 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
226 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800227 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
228 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700229 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700230 new OmahaRequestAction(prefs_,
231 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700232 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700233 new LibcurlHttpFetcher(GetProxyResolver()),
234 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700235
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700236 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700237 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700238 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700239
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700240 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
241 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
242 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700243 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700244 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700245 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700246 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700247 actions_.push_back(shared_ptr<AbstractAction>(download_signature_warning));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700248 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800249 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
250 actions_.push_back(shared_ptr<AbstractAction>(
251 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800252 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700253 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700254
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700255 // Enqueue the actions
256 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
257 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700258 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700259 }
260
261 // Bond them together. We have to use the leaf-types when calling
262 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700263 BondActions(update_check_action.get(),
264 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700265 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700266 filesystem_copier_action.get());
267 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700268 kernel_filesystem_copier_action.get());
269 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700270 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700271 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800272 filesystem_verifier_action.get());
273 BondActions(filesystem_verifier_action.get(),
274 kernel_filesystem_verifier_action.get());
275 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800276 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700277
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700278 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800279
280 // Start the processing asynchronously to unblock the event loop.
281 g_idle_add(&StaticStartProcessing, this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700282}
283
Darin Petkov5a7f5652010-07-22 21:40:09 -0700284void UpdateAttempter::CheckForUpdate(const std::string& app_version,
285 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700286 if (status_ != UPDATE_STATUS_IDLE) {
287 LOG(INFO) << "Check for update requested, but status is "
288 << UpdateStatusToString(status_) << ", so not checking.";
289 return;
290 }
Andrew de los Reyes45168102010-11-22 11:13:50 -0800291 Update(app_version, omaha_url, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700292}
293
Darin Petkov296889c2010-07-23 16:20:54 -0700294bool UpdateAttempter::RebootIfNeeded() {
295 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
296 LOG(INFO) << "Reboot requested, but status is "
297 << UpdateStatusToString(status_) << ", so not rebooting.";
298 return false;
299 }
300 TEST_AND_RETURN_FALSE(utils::Reboot());
301 return true;
302}
303
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700304// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700305void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700306 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700307 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700308 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700309 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700310
Darin Petkovc6c135c2010-08-11 13:36:18 -0700311 // Reset process priority back to normal.
312 CleanupPriorityManagement();
313
Darin Petkov09f96c32010-07-20 09:24:57 -0700314 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
315 LOG(INFO) << "Error event sent.";
316 SetStatusAndNotify(UPDATE_STATUS_IDLE);
317 return;
318 }
319
Darin Petkovc1a8b422010-07-19 11:34:49 -0700320 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700321 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700322 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800323 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700324 DeltaPerformer::ResetUpdateProgress(prefs_, false);
325 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700326
327 // Report the time it took to update the system.
328 int64_t update_time = time(NULL) - last_checked_time_;
329 metrics_lib_->SendToUMA("Installer.UpdateTime",
330 static_cast<int>(update_time), // sample
331 1, // min = 1 second
332 20 * 60, // max = 20 minutes
333 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700334 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700335 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700336
Darin Petkov1023a602010-08-30 13:47:51 -0700337 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700338 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700339 }
340 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700341 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700342}
343
344void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700345 // Reset process priority back to normal.
346 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700347 download_progress_ = 0.0;
348 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700349 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700350 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700351}
352
353// Called whenever an action has finished processing, either successfully
354// or otherwise.
355void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
356 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700357 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700358 // Reset download progress regardless of whether or not the download
359 // action succeeded. Also, get the response code from HTTP request
360 // actions (update download as well as the initial update check
361 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700362 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700363 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700364 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700365 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
366 http_response_code_ = download_action->GetHTTPResponseCode();
367 } else if (type == OmahaRequestAction::StaticType()) {
368 OmahaRequestAction* omaha_request_action =
369 dynamic_cast<OmahaRequestAction*>(action);
370 // If the request is not an event, then it's the update-check.
371 if (!omaha_request_action->IsEvent()) {
372 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700373 // Forward the server-dictated poll interval to the update check
374 // scheduler, if any.
375 if (update_check_scheduler_) {
376 update_check_scheduler_->set_poll_interval(
377 omaha_request_action->GetOutputObject().poll_interval);
378 }
Darin Petkov1023a602010-08-30 13:47:51 -0700379 }
380 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700381 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700382 // If this was a delta update attempt and the current state is at or past
383 // the download phase, count the failure in case a switch to full update
384 // becomes necessary. Ignore network transfer timeouts and failures.
385 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
386 !is_full_update_ &&
387 code != kActionCodeDownloadTransferError) {
388 MarkDeltaUpdateFailure();
389 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700390 // On failure, schedule an error event to be sent to Omaha.
391 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700392 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700393 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700394 // Find out which action completed.
395 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700396 // Note that the status will be updated to DOWNLOADING when some bytes get
397 // actually downloaded from the server and the BytesReceived callback is
398 // invoked. This avoids notifying the user that a download has started in
399 // cases when the server and the client are unable to initiate the download.
400 CHECK(action == response_handler_action_.get());
401 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700402 last_checked_time_ = time(NULL);
403 // TODO(adlr): put version in InstallPlan
404 new_version_ = "0.0.0.0";
405 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700406 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700407 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700408 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700409 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700410 } else if (type == DownloadAction::StaticType()) {
411 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
412 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700413}
414
415// Stop updating. An attempt will be made to record status to the disk
416// so that updates can be resumed later.
417void UpdateAttempter::Terminate() {
418 // TODO(adlr): implement this method.
419 NOTIMPLEMENTED();
420}
421
422// Try to resume from a previously Terminate()d update.
423void UpdateAttempter::ResumeUpdating() {
424 // TODO(adlr): implement this method.
425 NOTIMPLEMENTED();
426}
427
Darin Petkov9d911fa2010-08-19 09:36:08 -0700428void UpdateAttempter::SetDownloadStatus(bool active) {
429 download_active_ = active;
430 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
431}
432
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700433void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700434 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700435 LOG(ERROR) << "BytesReceived called while not downloading.";
436 return;
437 }
Darin Petkovaf183052010-08-23 12:07:13 -0700438 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700439 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700440 // Self throttle based on progress. Also send notifications if
441 // progress is too slow.
442 const double kDeltaPercent = 0.01; // 1%
443 if (status_ != UPDATE_STATUS_DOWNLOADING ||
444 bytes_received == total ||
445 progress - download_progress_ >= kDeltaPercent ||
446 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
447 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700448 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
449 }
450}
451
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700452bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
453 double* progress,
454 std::string* current_operation,
455 std::string* new_version,
456 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700457 *last_checked_time = last_checked_time_;
458 *progress = download_progress_;
459 *current_operation = UpdateStatusToString(status_);
460 *new_version = new_version_;
461 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700462 return true;
463}
464
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700465void UpdateAttempter::UpdateBootFlags() {
466 if (updated_boot_flags_) {
467 LOG(INFO) << "Already updated boot flags. Skipping.";
468 return;
469 }
470 // This is purely best effort. Failures should be logged by Subprocess.
471 int unused = 0;
472 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
473 Subprocess::SynchronousExec(cmd, &unused);
474 updated_boot_flags_ = true;
475 return;
476}
477
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700478void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
479 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700480 if (update_check_scheduler_) {
481 update_check_scheduler_->SetUpdateStatus(status_);
482 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700483 if (!dbus_service_)
484 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700485 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700486 update_engine_service_emit_status_update(
487 dbus_service_,
488 last_checked_time_,
489 download_progress_,
490 UpdateStatusToString(status_),
491 new_version_.c_str(),
492 new_size_);
493}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700494
Darin Petkov777dbfa2010-07-20 15:03:37 -0700495void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
496 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700497 if (error_event_.get()) {
498 // This shouldn't really happen.
499 LOG(WARNING) << "There's already an existing pending error event.";
500 return;
501 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700502
Darin Petkovabc7bc02011-02-23 14:39:43 -0800503 // For now assume that a generic Omaha response action failure means that
504 // there's no update so don't send an event. Also, double check that the
505 // failure has not occurred while sending an error event -- in which case
506 // don't schedule another. This shouldn't really happen but just in case...
507 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
508 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700509 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
510 return;
511 }
512
513 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700514 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
515 OmahaEvent::kResultError,
516 code));
517}
518
519bool UpdateAttempter::ScheduleErrorEventAction() {
520 if (error_event_.get() == NULL)
521 return false;
522
Darin Petkov1023a602010-08-30 13:47:51 -0700523 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700524 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700525 new OmahaRequestAction(prefs_,
526 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700527 error_event_.release(), // Pass ownership.
Thieu Le116fda32011-04-19 11:01:54 -0700528 new LibcurlHttpFetcher(GetProxyResolver()),
529 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700530 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700531 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700532 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700533 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700534 return true;
535}
536
Darin Petkovc6c135c2010-08-11 13:36:18 -0700537void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
538 if (priority_ == priority) {
539 return;
540 }
541 if (utils::SetProcessPriority(priority)) {
542 priority_ = priority;
543 LOG(INFO) << "Process priority = " << priority_;
544 }
545}
546
547void UpdateAttempter::SetupPriorityManagement() {
548 if (manage_priority_source_) {
549 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
550 CleanupPriorityManagement();
551 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700552 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700553 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
554 g_source_set_callback(manage_priority_source_,
555 StaticManagePriorityCallback,
556 this,
557 NULL);
558 g_source_attach(manage_priority_source_, NULL);
559 SetPriority(utils::kProcessPriorityLow);
560}
561
562void UpdateAttempter::CleanupPriorityManagement() {
563 if (manage_priority_source_) {
564 g_source_destroy(manage_priority_source_);
565 manage_priority_source_ = NULL;
566 }
567 SetPriority(utils::kProcessPriorityNormal);
568}
569
570gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
571 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
572}
573
Darin Petkove6ef2f82011-03-07 17:31:11 -0800574gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
575 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
576 return FALSE; // Don't call this callback again.
577}
578
Darin Petkovc6c135c2010-08-11 13:36:18 -0700579bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700580 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700581 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700582 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700583}
584
Darin Petkov36275772010-10-01 11:40:57 -0700585void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
586 int64_t delta_failures;
587 if (omaha_request_params_.delta_okay &&
588 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
589 delta_failures >= kMaxDeltaUpdateFailures) {
590 LOG(WARNING) << "Too many delta update failures, forcing full update.";
591 omaha_request_params_.delta_okay = false;
592 }
593}
594
595void UpdateAttempter::MarkDeltaUpdateFailure() {
596 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700597 // Don't try to resume a failed delta update.
598 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700599 int64_t delta_failures;
600 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
601 delta_failures < 0) {
602 delta_failures = 0;
603 }
604 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
605}
606
Darin Petkov9b230572010-10-08 10:20:09 -0700607void UpdateAttempter::SetupDownload() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800608 MultiRangeHTTPFetcher* fetcher =
609 dynamic_cast<MultiRangeHTTPFetcher*>(download_action_->http_fetcher());
610 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700611 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700612 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700613 int64_t manifest_metadata_size = 0;
614 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800615 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700616 // If there're remaining unprocessed data blobs, fetch them. Be careful not
617 // to request data beyond the end of the payload to avoid 416 HTTP response
618 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700619 int64_t next_data_offset = 0;
620 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700621 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
622 if (resume_offset < response_handler_action_->install_plan().size) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800623 fetcher->AddRange(resume_offset, -1);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700624 }
Darin Petkov9b230572010-10-08 10:20:09 -0700625 } else {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800626 fetcher->AddRange(0, -1);
Darin Petkov9b230572010-10-08 10:20:09 -0700627 }
Darin Petkov9b230572010-10-08 10:20:09 -0700628}
629
Thieu Le116fda32011-04-19 11:01:54 -0700630void UpdateAttempter::PingOmaha() {
631 shared_ptr<OmahaRequestAction> ping_action(
632 new OmahaRequestAction(prefs_,
633 omaha_request_params_,
634 NULL,
635 new LibcurlHttpFetcher(GetProxyResolver()),
636 true));
637 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
638 CHECK(!processor_->IsRunning());
639 processor_->set_delegate(NULL);
640 processor_->EnqueueAction(ping_action.get());
641 g_idle_add(&StaticStartProcessing, this);
642 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
643}
644
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700645} // namespace chromeos_update_engine