blob: 27d3a91a523dc42e02d187d9056ceae2883b9d86 [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
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070023#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024#include "update_engine/download_action.h"
25#include "update_engine/filesystem_copier_action.h"
26#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyes819fef22010-12-17 11:33:58 -080027#include "update_engine/multi_range_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070028#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070029#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070030#include "update_engine/omaha_response_handler_action.h"
31#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070032#include "update_engine/prefs_interface.h"
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -070033#include "update_engine/subprocess.h"
Darin Petkov1023a602010-08-30 13:47:51 -070034#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070035
Darin Petkovaf183052010-08-23 12:07:13 -070036using base::TimeDelta;
37using base::TimeTicks;
Andrew de los Reyes21816e12011-04-07 14:18:56 -070038using google::protobuf::NewPermanentCallback;
Darin Petkov9b230572010-10-08 10:20:09 -070039using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070040using std::tr1::shared_ptr;
41using std::string;
42using std::vector;
43
44namespace chromeos_update_engine {
45
Darin Petkov36275772010-10-01 11:40:57 -070046const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
47
Darin Petkovcd1666f2010-09-23 09:53:44 -070048const char* kUpdateCompletedMarker =
49 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070050
Andrew de los Reyes45168102010-11-22 11:13:50 -080051namespace {
52const int kMaxConsecutiveObeyProxyRequests = 20;
53} // namespace {}
54
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070055const char* UpdateStatusToString(UpdateStatus status) {
56 switch (status) {
57 case UPDATE_STATUS_IDLE:
58 return "UPDATE_STATUS_IDLE";
59 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
60 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
61 case UPDATE_STATUS_UPDATE_AVAILABLE:
62 return "UPDATE_STATUS_UPDATE_AVAILABLE";
63 case UPDATE_STATUS_DOWNLOADING:
64 return "UPDATE_STATUS_DOWNLOADING";
65 case UPDATE_STATUS_VERIFYING:
66 return "UPDATE_STATUS_VERIFYING";
67 case UPDATE_STATUS_FINALIZING:
68 return "UPDATE_STATUS_FINALIZING";
69 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
70 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070071 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
72 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070073 default:
74 return "unknown status";
75 }
76}
77
Darin Petkov777dbfa2010-07-20 15:03:37 -070078// Turns a generic kActionCodeError to a generic error code specific
79// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
80// not kActionCodeError, or the action is not matched, returns |code|
81// unchanged.
82ActionExitCode GetErrorCodeForAction(AbstractAction* action,
83 ActionExitCode code) {
84 if (code != kActionCodeError)
85 return code;
86
87 const string type = action->Type();
88 if (type == OmahaRequestAction::StaticType())
89 return kActionCodeOmahaRequestError;
90 if (type == OmahaResponseHandlerAction::StaticType())
91 return kActionCodeOmahaResponseHandlerError;
92 if (type == FilesystemCopierAction::StaticType())
93 return kActionCodeFilesystemCopierError;
94 if (type == PostinstallRunnerAction::StaticType())
95 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070096
97 return code;
98}
99
Darin Petkovc6c135c2010-08-11 13:36:18 -0700100UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800101 MetricsLibraryInterface* metrics_lib,
102 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700103 : processor_(new ActionProcessor()),
104 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700105 prefs_(prefs),
106 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700107 update_check_scheduler_(NULL),
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700108 fake_update_success_(false),
Darin Petkov1023a602010-08-30 13:47:51 -0700109 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700110 priority_(utils::kProcessPriorityNormal),
111 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700112 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700113 status_(UPDATE_STATUS_IDLE),
114 download_progress_(0.0),
115 last_checked_time_(0),
116 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700117 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800118 is_full_update_(false),
119 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);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700198 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700199 new OmahaRequestAction(prefs_,
200 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700201 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700202 update_check_fetcher, // passes ownership
203 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700204 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700205 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700206 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800207 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700208 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800209 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700210 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700211 new OmahaRequestAction(prefs_,
212 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700213 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700214 OmahaEvent::kTypeUpdateDownloadStarted),
Thieu Le116fda32011-04-19 11:01:54 -0700215 new LibcurlHttpFetcher(GetProxyResolver()),
216 false));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700217 shared_ptr<DownloadAction> download_action(
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800218 new DownloadAction(prefs_, new MultiRangeHTTPFetcher(
219 new LibcurlHttpFetcher(GetProxyResolver()))));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700220 // This action is always initially in place to warn OS vendor of a
221 // signature failure. If it's not needed, it will be told to skip.
222 shared_ptr<OmahaRequestAction> download_signature_warning(
223 new OmahaRequestAction(
224 prefs_,
225 omaha_request_params_,
226 new OmahaEvent(
227 OmahaEvent::kTypeUpdateDownloadFinished,
228 OmahaEvent::kResultError,
229 kActionCodeDownloadPayloadPubKeyVerificationError),
Thieu Le116fda32011-04-19 11:01:54 -0700230 new LibcurlHttpFetcher(GetProxyResolver()),
231 false));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700232 download_action->set_skip_reporting_signature_fail(
233 NewPermanentCallback(download_signature_warning.get(),
234 &OmahaRequestAction::set_should_skip,
235 true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700236 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700237 new OmahaRequestAction(prefs_,
238 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700239 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700240 OmahaEvent::kTypeUpdateDownloadFinished),
Thieu Le116fda32011-04-19 11:01:54 -0700241 new LibcurlHttpFetcher(GetProxyResolver()),
242 false));
Darin Petkov3aefa862010-12-07 14:45:00 -0800243 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
244 new FilesystemCopierAction(false, true));
245 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
246 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800247 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
248 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700249 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700250 new OmahaRequestAction(prefs_,
251 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700252 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700253 new LibcurlHttpFetcher(GetProxyResolver()),
254 false));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700255
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700256 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700257 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700258 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700259
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700260 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
261 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
262 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700263 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700264 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700265 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700266 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700267 actions_.push_back(shared_ptr<AbstractAction>(download_signature_warning));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700268 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800269 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
270 actions_.push_back(shared_ptr<AbstractAction>(
271 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800272 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700273 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700274
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700275 // Enqueue the actions
276 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
277 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700278 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700279 }
280
281 // Bond them together. We have to use the leaf-types when calling
282 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700283 BondActions(update_check_action.get(),
284 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700285 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700286 filesystem_copier_action.get());
287 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700288 kernel_filesystem_copier_action.get());
289 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700290 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700291 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800292 filesystem_verifier_action.get());
293 BondActions(filesystem_verifier_action.get(),
294 kernel_filesystem_verifier_action.get());
295 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800296 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700297
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700298 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800299
Darin Petkov58dd1342011-05-06 12:05:13 -0700300 // Just in case we didn't update boot flags yet, make sure they're updated
301 // before any update processing starts.
302 start_action_processor_ = true;
303 UpdateBootFlags();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700304}
305
Darin Petkov5a7f5652010-07-22 21:40:09 -0700306void UpdateAttempter::CheckForUpdate(const std::string& app_version,
307 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700308 if (status_ != UPDATE_STATUS_IDLE) {
309 LOG(INFO) << "Check for update requested, but status is "
310 << UpdateStatusToString(status_) << ", so not checking.";
311 return;
312 }
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700313 Update(app_version, omaha_url, true, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700314}
315
Darin Petkov296889c2010-07-23 16:20:54 -0700316bool UpdateAttempter::RebootIfNeeded() {
317 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
318 LOG(INFO) << "Reboot requested, but status is "
319 << UpdateStatusToString(status_) << ", so not rebooting.";
320 return false;
321 }
322 TEST_AND_RETURN_FALSE(utils::Reboot());
323 return true;
324}
325
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700326// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700327void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700328 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700329 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700330 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700331 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700332
Darin Petkovc6c135c2010-08-11 13:36:18 -0700333 // Reset process priority back to normal.
334 CleanupPriorityManagement();
335
Darin Petkov09f96c32010-07-20 09:24:57 -0700336 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
337 LOG(INFO) << "Error event sent.";
338 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700339 if (!fake_update_success_) {
340 return;
341 }
342 LOG(INFO) << "Booted from FW B and tried to install new firmware, "
343 "so requesting reboot from user.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700344 }
345
Darin Petkovc1a8b422010-07-19 11:34:49 -0700346 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700347 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700348 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800349 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700350 DeltaPerformer::ResetUpdateProgress(prefs_, false);
351 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700352
353 // Report the time it took to update the system.
354 int64_t update_time = time(NULL) - last_checked_time_;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700355 if (!fake_update_success_)
356 metrics_lib_->SendToUMA("Installer.UpdateTime",
357 static_cast<int>(update_time), // sample
358 1, // min = 1 second
359 20 * 60, // max = 20 minutes
360 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700361 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700362 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700363
Darin Petkov1023a602010-08-30 13:47:51 -0700364 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700365 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700366 }
367 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700368 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700369}
370
371void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700372 // Reset process priority back to normal.
373 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700374 download_progress_ = 0.0;
375 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700376 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700377 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700378}
379
380// Called whenever an action has finished processing, either successfully
381// or otherwise.
382void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
383 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700384 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700385 // Reset download progress regardless of whether or not the download
386 // action succeeded. Also, get the response code from HTTP request
387 // actions (update download as well as the initial update check
388 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700389 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700390 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700391 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700392 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
393 http_response_code_ = download_action->GetHTTPResponseCode();
394 } else if (type == OmahaRequestAction::StaticType()) {
395 OmahaRequestAction* omaha_request_action =
396 dynamic_cast<OmahaRequestAction*>(action);
397 // If the request is not an event, then it's the update-check.
398 if (!omaha_request_action->IsEvent()) {
399 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700400 // Forward the server-dictated poll interval to the update check
401 // scheduler, if any.
402 if (update_check_scheduler_) {
403 update_check_scheduler_->set_poll_interval(
404 omaha_request_action->GetOutputObject().poll_interval);
405 }
Darin Petkov1023a602010-08-30 13:47:51 -0700406 }
407 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700408 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700409 // If this was a delta update attempt and the current state is at or past
410 // the download phase, count the failure in case a switch to full update
411 // becomes necessary. Ignore network transfer timeouts and failures.
412 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
413 !is_full_update_ &&
414 code != kActionCodeDownloadTransferError) {
415 MarkDeltaUpdateFailure();
416 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700417 // On failure, schedule an error event to be sent to Omaha.
418 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700419 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700420 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700421 // Find out which action completed.
422 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700423 // Note that the status will be updated to DOWNLOADING when some bytes get
424 // actually downloaded from the server and the BytesReceived callback is
425 // invoked. This avoids notifying the user that a download has started in
426 // cases when the server and the client are unable to initiate the download.
427 CHECK(action == response_handler_action_.get());
428 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700429 last_checked_time_ = time(NULL);
430 // TODO(adlr): put version in InstallPlan
431 new_version_ = "0.0.0.0";
432 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700433 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700434 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700435 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700436 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700437 } else if (type == DownloadAction::StaticType()) {
438 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
439 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700440}
441
442// Stop updating. An attempt will be made to record status to the disk
443// so that updates can be resumed later.
444void UpdateAttempter::Terminate() {
445 // TODO(adlr): implement this method.
446 NOTIMPLEMENTED();
447}
448
449// Try to resume from a previously Terminate()d update.
450void UpdateAttempter::ResumeUpdating() {
451 // TODO(adlr): implement this method.
452 NOTIMPLEMENTED();
453}
454
Darin Petkov9d911fa2010-08-19 09:36:08 -0700455void UpdateAttempter::SetDownloadStatus(bool active) {
456 download_active_ = active;
457 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
458}
459
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700460void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700461 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700462 LOG(ERROR) << "BytesReceived called while not downloading.";
463 return;
464 }
Darin Petkovaf183052010-08-23 12:07:13 -0700465 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700466 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700467 // Self throttle based on progress. Also send notifications if
468 // progress is too slow.
469 const double kDeltaPercent = 0.01; // 1%
470 if (status_ != UPDATE_STATUS_DOWNLOADING ||
471 bytes_received == total ||
472 progress - download_progress_ >= kDeltaPercent ||
473 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
474 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700475 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
476 }
477}
478
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700479bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
480 double* progress,
481 std::string* current_operation,
482 std::string* new_version,
483 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700484 *last_checked_time = last_checked_time_;
485 *progress = download_progress_;
486 *current_operation = UpdateStatusToString(status_);
487 *new_version = new_version_;
488 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700489 return true;
490}
491
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700492void UpdateAttempter::UpdateBootFlags() {
Darin Petkov58dd1342011-05-06 12:05:13 -0700493 if (update_boot_flags_running_) {
494 LOG(INFO) << "Update boot flags running, nothing to do.";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700495 return;
496 }
Darin Petkov58dd1342011-05-06 12:05:13 -0700497 if (updated_boot_flags_) {
498 LOG(INFO) << "Already updated boot flags. Skipping.";
499 if (start_action_processor_) {
500 ScheduleProcessingStart();
501 }
502 return;
503 }
504 // This is purely best effort. Failures should be logged by Subprocess. Run
505 // the script asynchronously to avoid blocking the event loop regardless of
506 // the script runtime.
507 update_boot_flags_running_ = true;
508 LOG(INFO) << "Updating boot flags...";
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700509 vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel");
Darin Petkov58dd1342011-05-06 12:05:13 -0700510 if (!Subprocess::Get().Exec(cmd, StaticCompleteUpdateBootFlags, this)) {
511 CompleteUpdateBootFlags(1);
512 }
513}
514
515void UpdateAttempter::CompleteUpdateBootFlags(int return_code) {
516 update_boot_flags_running_ = false;
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700517 updated_boot_flags_ = true;
Darin Petkov58dd1342011-05-06 12:05:13 -0700518 if (start_action_processor_) {
519 ScheduleProcessingStart();
520 }
521}
522
523void UpdateAttempter::StaticCompleteUpdateBootFlags(
524 int return_code,
525 const std::string& output,
526 void* p) {
527 reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
Andrew de los Reyes6dbf30a2011-04-19 10:58:16 -0700528}
529
Darin Petkov61635a92011-05-18 16:20:36 -0700530void UpdateAttempter::BroadcastStatus() {
531 if (!dbus_service_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700532 return;
Darin Petkov61635a92011-05-18 16:20:36 -0700533 }
Darin Petkovaf183052010-08-23 12:07:13 -0700534 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700535 update_engine_service_emit_status_update(
536 dbus_service_,
537 last_checked_time_,
538 download_progress_,
539 UpdateStatusToString(status_),
540 new_version_.c_str(),
541 new_size_);
542}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700543
Darin Petkov61635a92011-05-18 16:20:36 -0700544void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
545 status_ = status;
546 if (update_check_scheduler_) {
547 update_check_scheduler_->SetUpdateStatus(status_);
548 }
549 BroadcastStatus();
550}
551
Darin Petkov777dbfa2010-07-20 15:03:37 -0700552void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
553 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700554 if (error_event_.get()) {
555 // This shouldn't really happen.
556 LOG(WARNING) << "There's already an existing pending error event.";
557 return;
558 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700559
Darin Petkovabc7bc02011-02-23 14:39:43 -0800560 // For now assume that a generic Omaha response action failure means that
561 // there's no update so don't send an event. Also, double check that the
562 // failure has not occurred while sending an error event -- in which case
563 // don't schedule another. This shouldn't really happen but just in case...
564 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
565 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700566 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
567 return;
568 }
569
570 code = GetErrorCodeForAction(action, code);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700571 fake_update_success_ = code == kActionCodePostinstallBootedFromFirmwareB;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700572
573 // Apply the bit modifiers to the error code.
574 if (!utils::IsNormalBootMode()) {
575 code = static_cast<ActionExitCode>(code | kActionCodeBootModeFlag);
576 }
577 if (response_handler_action_.get() &&
578 response_handler_action_->install_plan().is_resume) {
579 code = static_cast<ActionExitCode>(code | kActionCodeResumedFlag);
580 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700581 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
582 OmahaEvent::kResultError,
583 code));
584}
585
586bool UpdateAttempter::ScheduleErrorEventAction() {
587 if (error_event_.get() == NULL)
588 return false;
589
Darin Petkov1023a602010-08-30 13:47:51 -0700590 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700591 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700592 new OmahaRequestAction(prefs_,
593 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700594 error_event_.release(), // Pass ownership.
Thieu Le116fda32011-04-19 11:01:54 -0700595 new LibcurlHttpFetcher(GetProxyResolver()),
596 false));
Darin Petkov09f96c32010-07-20 09:24:57 -0700597 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700598 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700599 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700600 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700601 return true;
602}
603
Darin Petkovc6c135c2010-08-11 13:36:18 -0700604void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
605 if (priority_ == priority) {
606 return;
607 }
608 if (utils::SetProcessPriority(priority)) {
609 priority_ = priority;
610 LOG(INFO) << "Process priority = " << priority_;
611 }
612}
613
614void UpdateAttempter::SetupPriorityManagement() {
615 if (manage_priority_source_) {
616 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
617 CleanupPriorityManagement();
618 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700619 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700620 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
621 g_source_set_callback(manage_priority_source_,
622 StaticManagePriorityCallback,
623 this,
624 NULL);
625 g_source_attach(manage_priority_source_, NULL);
626 SetPriority(utils::kProcessPriorityLow);
627}
628
629void UpdateAttempter::CleanupPriorityManagement() {
630 if (manage_priority_source_) {
631 g_source_destroy(manage_priority_source_);
632 manage_priority_source_ = NULL;
633 }
634 SetPriority(utils::kProcessPriorityNormal);
635}
636
637gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
638 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
639}
640
Darin Petkove6ef2f82011-03-07 17:31:11 -0800641gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
642 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
643 return FALSE; // Don't call this callback again.
644}
645
Darin Petkov58dd1342011-05-06 12:05:13 -0700646void UpdateAttempter::ScheduleProcessingStart() {
647 LOG(INFO) << "Scheduling an action processor start.";
648 start_action_processor_ = false;
649 g_idle_add(&StaticStartProcessing, this);
650}
651
Darin Petkovc6c135c2010-08-11 13:36:18 -0700652bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700653 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700654 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700655 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700656}
657
Darin Petkov36275772010-10-01 11:40:57 -0700658void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
659 int64_t delta_failures;
660 if (omaha_request_params_.delta_okay &&
661 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
662 delta_failures >= kMaxDeltaUpdateFailures) {
663 LOG(WARNING) << "Too many delta update failures, forcing full update.";
664 omaha_request_params_.delta_okay = false;
665 }
666}
667
668void UpdateAttempter::MarkDeltaUpdateFailure() {
669 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700670 // Don't try to resume a failed delta update.
671 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700672 int64_t delta_failures;
673 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
674 delta_failures < 0) {
675 delta_failures = 0;
676 }
677 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
678}
679
Darin Petkov9b230572010-10-08 10:20:09 -0700680void UpdateAttempter::SetupDownload() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800681 MultiRangeHTTPFetcher* fetcher =
682 dynamic_cast<MultiRangeHTTPFetcher*>(download_action_->http_fetcher());
683 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700684 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700685 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700686 int64_t manifest_metadata_size = 0;
687 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800688 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700689 // If there're remaining unprocessed data blobs, fetch them. Be careful not
690 // to request data beyond the end of the payload to avoid 416 HTTP response
691 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700692 int64_t next_data_offset = 0;
693 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700694 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
695 if (resume_offset < response_handler_action_->install_plan().size) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800696 fetcher->AddRange(resume_offset, -1);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700697 }
Darin Petkov9b230572010-10-08 10:20:09 -0700698 } else {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800699 fetcher->AddRange(0, -1);
Darin Petkov9b230572010-10-08 10:20:09 -0700700 }
Darin Petkov9b230572010-10-08 10:20:09 -0700701}
702
Thieu Le116fda32011-04-19 11:01:54 -0700703void UpdateAttempter::PingOmaha() {
Thieu Led88a8572011-05-26 09:09:19 -0700704 if (!processor_->IsRunning()) {
705 shared_ptr<OmahaRequestAction> ping_action(
706 new OmahaRequestAction(prefs_,
707 omaha_request_params_,
708 NULL,
709 new LibcurlHttpFetcher(GetProxyResolver()),
710 true));
711 actions_.push_back(shared_ptr<OmahaRequestAction>(ping_action));
712 processor_->set_delegate(NULL);
713 processor_->EnqueueAction(ping_action.get());
714 // Call StartProcessing() synchronously here to avoid any race conditions
715 // caused by multiple outstanding ping Omaha requests. If we call
716 // StartProcessing() asynchronously, the device can be suspended before we
717 // get a chance to callback to StartProcessing(). When the device resumes
718 // (assuming the device sleeps longer than the next update check period),
719 // StartProcessing() is called back and at the same time, the next update
720 // check is fired which eventually invokes StartProcessing(). A crash
721 // can occur because StartProcessing() checks to make sure that the
722 // processor is idle which it isn't due to the two concurrent ping Omaha
723 // requests.
724 processor_->StartProcessing();
725 } else {
Darin Petkov58dd1342011-05-06 12:05:13 -0700726 LOG(WARNING) << "Action processor running, Omaha ping suppressed.";
Darin Petkov58dd1342011-05-06 12:05:13 -0700727 }
Thieu Led88a8572011-05-26 09:09:19 -0700728
729 // Update the status which will schedule the next update check
Thieu Le116fda32011-04-19 11:01:54 -0700730 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
731}
732
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700733} // namespace chromeos_update_engine