blob: 3f2012670b78ddf91724a2d03eaa8badc38ee50e [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 <tr1/memory>
14#include <string>
15#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070016
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070017#include <glib.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070018
19#include "metrics/metrics_library.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070020#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/download_action.h"
22#include "update_engine/filesystem_copier_action.h"
23#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070024#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070025#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/omaha_response_handler_action.h"
27#include "update_engine/postinstall_runner_action.h"
28#include "update_engine/set_bootable_flag_action.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029
30using std::tr1::shared_ptr;
31using std::string;
32using std::vector;
33
34namespace chromeos_update_engine {
35
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070036const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
37
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070038namespace {
39// Returns true on success.
40bool GetCPUClockTime(struct timespec* out) {
41 return clock_gettime(CLOCK_REALTIME, out) == 0;
42}
43// Returns stop - start.
44struct timespec CPUClockTimeElapsed(const struct timespec& start,
45 const struct timespec& stop) {
46 CHECK(start.tv_sec >= 0);
47 CHECK(stop.tv_sec >= 0);
48 CHECK(start.tv_nsec >= 0);
49 CHECK(stop.tv_nsec >= 0);
50
51 const int64_t kOneBillion = 1000000000L;
52 const int64_t start64 = start.tv_sec * kOneBillion + start.tv_nsec;
53 const int64_t stop64 = stop.tv_sec * kOneBillion + stop.tv_nsec;
54
55 const int64_t result64 = stop64 - start64;
56
57 struct timespec ret;
58 ret.tv_sec = result64 / kOneBillion;
59 ret.tv_nsec = result64 % kOneBillion;
60
61 return ret;
62}
63bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) {
64 if (spec.tv_sec >= 1)
65 return true;
66 return (spec.tv_nsec > 500000000);
67}
68}
69
70const char* UpdateStatusToString(UpdateStatus status) {
71 switch (status) {
72 case UPDATE_STATUS_IDLE:
73 return "UPDATE_STATUS_IDLE";
74 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
75 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
76 case UPDATE_STATUS_UPDATE_AVAILABLE:
77 return "UPDATE_STATUS_UPDATE_AVAILABLE";
78 case UPDATE_STATUS_DOWNLOADING:
79 return "UPDATE_STATUS_DOWNLOADING";
80 case UPDATE_STATUS_VERIFYING:
81 return "UPDATE_STATUS_VERIFYING";
82 case UPDATE_STATUS_FINALIZING:
83 return "UPDATE_STATUS_FINALIZING";
84 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
85 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
86 default:
87 return "unknown status";
88 }
89}
90
Darin Petkova4a8a8c2010-07-15 22:21:12 -070091void UpdateAttempter::Update() {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070092 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
93 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
94 << "reboot";
95 return;
96 }
97 if (status_ != UPDATE_STATUS_IDLE) {
98 // Update in progress. Do nothing
99 return;
100 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700101 if (!omaha_request_params_.Init()) {
102 LOG(ERROR) << "Unable to initialize Omaha request device params.";
103 return;
104 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700105 CHECK(!processor_.IsRunning());
106 processor_.set_delegate(this);
107
108 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700109 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700110 new OmahaRequestAction(omaha_request_params_,
111 NULL,
112 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700113 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
114 new OmahaResponseHandlerAction);
115 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700116 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700117 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700118 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700119 shared_ptr<OmahaRequestAction> download_started_action(
120 new OmahaRequestAction(omaha_request_params_,
121 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700122 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700123 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700124 shared_ptr<DownloadAction> download_action(
125 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700126 shared_ptr<OmahaRequestAction> download_finished_action(
127 new OmahaRequestAction(omaha_request_params_,
128 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700129 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700130 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700131 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
132 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700133 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
134 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700135 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
136 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700137 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700138 new OmahaRequestAction(omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700139 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700140 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700141
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700142 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700143 response_handler_action_ = response_handler_action;
144
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700145 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
146 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
147 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700148 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700149 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700150 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700151 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700152 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700153 actions_.push_back(shared_ptr<AbstractAction>(
154 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700155 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700156 actions_.push_back(shared_ptr<AbstractAction>(
157 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700158 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700159
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700160 // Enqueue the actions
161 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
162 it != actions_.end(); ++it) {
163 processor_.EnqueueAction(it->get());
164 }
165
166 // Bond them together. We have to use the leaf-types when calling
167 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700168 BondActions(update_check_action.get(),
169 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700170 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700171 filesystem_copier_action.get());
172 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700173 kernel_filesystem_copier_action.get());
174 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700175 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700176 BondActions(download_action.get(),
177 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700178 BondActions(postinstall_runner_action_precommit.get(),
179 set_bootable_flag_action.get());
180 BondActions(set_bootable_flag_action.get(),
181 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700183 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700184 processor_.StartProcessing();
185}
186
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700187void UpdateAttempter::CheckForUpdate() {
188 if (status_ != UPDATE_STATUS_IDLE) {
189 LOG(INFO) << "Check for update requested, but status is "
190 << UpdateStatusToString(status_) << ", so not checking.";
191 return;
192 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700193 Update();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700194}
195
196// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700197void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700198 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700199 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700200 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700201 actions_.clear();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700202 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700203 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700204 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700205
206 // Report the time it took to update the system.
207 int64_t update_time = time(NULL) - last_checked_time_;
208 metrics_lib_->SendToUMA("Installer.UpdateTime",
209 static_cast<int>(update_time), // sample
210 1, // min = 1 second
211 20 * 60, // max = 20 minutes
212 50); // buckets
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700213 } else {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700214 LOG(INFO) << "Update failed.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700215 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700216 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700217}
218
219void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
220 download_progress_ = 0.0;
221 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700222 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700223}
224
225// Called whenever an action has finished processing, either successfully
226// or otherwise.
227void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
228 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700229 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700230 // Reset download progress regardless of whether or not the download action
231 // succeeded.
232 const string type = action->Type();
233 if (type == DownloadAction::StaticType())
234 download_progress_ = 0.0;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700235 if (code != kActionCodeSuccess)
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700236 return;
237 // Find out which action completed.
238 if (type == OmahaResponseHandlerAction::StaticType()) {
239 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700240 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700241 dynamic_cast<OmahaResponseHandlerAction*>(action);
242 CHECK(omaha_response_handler_action);
243 const InstallPlan& plan = omaha_response_handler_action->install_plan();
244 last_checked_time_ = time(NULL);
245 // TODO(adlr): put version in InstallPlan
246 new_version_ = "0.0.0.0";
247 new_size_ = plan.size;
248 } else if (type == DownloadAction::StaticType()) {
249 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
250 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700251}
252
253// Stop updating. An attempt will be made to record status to the disk
254// so that updates can be resumed later.
255void UpdateAttempter::Terminate() {
256 // TODO(adlr): implement this method.
257 NOTIMPLEMENTED();
258}
259
260// Try to resume from a previously Terminate()d update.
261void UpdateAttempter::ResumeUpdating() {
262 // TODO(adlr): implement this method.
263 NOTIMPLEMENTED();
264}
265
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700266void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
267 if (status_ != UPDATE_STATUS_DOWNLOADING) {
268 LOG(ERROR) << "BytesReceived called while not downloading.";
269 return;
270 }
271 download_progress_ = static_cast<double>(bytes_received) /
272 static_cast<double>(total);
273 // We self throttle here
274 timespec now;
275 now.tv_sec = 0;
276 now.tv_nsec = 0;
277 if (GetCPUClockTime(&now) &&
278 CPUClockTimeGreaterThanHalfSecond(
279 CPUClockTimeElapsed(last_notify_time_, now))) {
280 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
281 }
282}
283
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700284bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
285 double* progress,
286 std::string* current_operation,
287 std::string* new_version,
288 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700289 *last_checked_time = last_checked_time_;
290 *progress = download_progress_;
291 *current_operation = UpdateStatusToString(status_);
292 *new_version = new_version_;
293 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700294 return true;
295}
296
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700297void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
298 status_ = status;
299 if (!dbus_service_)
300 return;
301 GetCPUClockTime(&last_notify_time_);
302 update_engine_service_emit_status_update(
303 dbus_service_,
304 last_checked_time_,
305 download_progress_,
306 UpdateStatusToString(status_),
307 new_version_.c_str(),
308 new_size_);
309}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700310
311} // namespace chromeos_update_engine