blob: ee1cf00f7bd64ac6abe7cd12f135ddb039d903e5 [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";
Darin Petkov09f96c32010-07-20 09:24:57 -070086 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
87 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070088 default:
89 return "unknown status";
90 }
91}
92
Darin Petkova4a8a8c2010-07-15 22:21:12 -070093void UpdateAttempter::Update() {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070094 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
95 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
96 << "reboot";
97 return;
98 }
99 if (status_ != UPDATE_STATUS_IDLE) {
100 // Update in progress. Do nothing
101 return;
102 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700103 if (!omaha_request_params_.Init()) {
104 LOG(ERROR) << "Unable to initialize Omaha request device params.";
105 return;
106 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700107 CHECK(!processor_.IsRunning());
108 processor_.set_delegate(this);
109
110 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700111 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700112 new OmahaRequestAction(omaha_request_params_,
113 NULL,
114 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700115 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
116 new OmahaResponseHandlerAction);
117 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700118 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700119 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700120 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700121 shared_ptr<OmahaRequestAction> download_started_action(
122 new OmahaRequestAction(omaha_request_params_,
123 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700124 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700125 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700126 shared_ptr<DownloadAction> download_action(
127 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700128 shared_ptr<OmahaRequestAction> download_finished_action(
129 new OmahaRequestAction(omaha_request_params_,
130 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700131 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700132 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700133 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
134 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700135 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
136 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700137 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
138 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700139 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700140 new OmahaRequestAction(omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700141 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700142 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700143
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700144 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700145 response_handler_action_ = response_handler_action;
146
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700147 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
148 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
149 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700151 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700152 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700153 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700154 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700155 actions_.push_back(shared_ptr<AbstractAction>(
156 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700157 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700158 actions_.push_back(shared_ptr<AbstractAction>(
159 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700160 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700161
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700162 // Enqueue the actions
163 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
164 it != actions_.end(); ++it) {
165 processor_.EnqueueAction(it->get());
166 }
167
168 // Bond them together. We have to use the leaf-types when calling
169 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700170 BondActions(update_check_action.get(),
171 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700172 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700173 filesystem_copier_action.get());
174 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700175 kernel_filesystem_copier_action.get());
176 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700177 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700178 BondActions(download_action.get(),
179 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700180 BondActions(postinstall_runner_action_precommit.get(),
181 set_bootable_flag_action.get());
182 BondActions(set_bootable_flag_action.get(),
183 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700184
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700185 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700186 processor_.StartProcessing();
187}
188
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700189void UpdateAttempter::CheckForUpdate() {
190 if (status_ != UPDATE_STATUS_IDLE) {
191 LOG(INFO) << "Check for update requested, but status is "
192 << UpdateStatusToString(status_) << ", so not checking.";
193 return;
194 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700195 Update();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700196}
197
198// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700199void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700200 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700201 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700202 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700203 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700204
205 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
206 LOG(INFO) << "Error event sent.";
207 SetStatusAndNotify(UPDATE_STATUS_IDLE);
208 return;
209 }
210
Darin Petkovc1a8b422010-07-19 11:34:49 -0700211 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700212 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700213 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700214
215 // Report the time it took to update the system.
216 int64_t update_time = time(NULL) - last_checked_time_;
217 metrics_lib_->SendToUMA("Installer.UpdateTime",
218 static_cast<int>(update_time), // sample
219 1, // min = 1 second
220 20 * 60, // max = 20 minutes
221 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700222 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700223 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700224
225 LOG(INFO) << "Update failed.";
226 if (ScheduleErrorEventAction())
227 return;
228 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700229}
230
231void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
232 download_progress_ = 0.0;
233 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700234 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700235 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700236}
237
238// Called whenever an action has finished processing, either successfully
239// or otherwise.
240void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
241 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700242 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700243 // Reset download progress regardless of whether or not the download action
244 // succeeded.
245 const string type = action->Type();
246 if (type == DownloadAction::StaticType())
247 download_progress_ = 0.0;
Darin Petkov09f96c32010-07-20 09:24:57 -0700248 if (code != kActionCodeSuccess) {
249 // On failure, schedule an error event to be sent to Omaha. For
250 // now assume that Omaha response action failure means that
251 // there's no update so don't send an event. Also, double check
252 // that the failure has not occurred while sending an error event
253 // -- in which case don't schedule another. This shouldn't really
254 // happen but just in case...
255 if (type != OmahaResponseHandlerAction::StaticType() &&
256 status_ != UPDATE_STATUS_REPORTING_ERROR_EVENT) {
257 CreatePendingErrorEvent(code);
258 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700259 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700260 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700261 // Find out which action completed.
262 if (type == OmahaResponseHandlerAction::StaticType()) {
263 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700264 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700265 dynamic_cast<OmahaResponseHandlerAction*>(action);
266 CHECK(omaha_response_handler_action);
267 const InstallPlan& plan = omaha_response_handler_action->install_plan();
268 last_checked_time_ = time(NULL);
269 // TODO(adlr): put version in InstallPlan
270 new_version_ = "0.0.0.0";
271 new_size_ = plan.size;
272 } else if (type == DownloadAction::StaticType()) {
273 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
274 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700275}
276
277// Stop updating. An attempt will be made to record status to the disk
278// so that updates can be resumed later.
279void UpdateAttempter::Terminate() {
280 // TODO(adlr): implement this method.
281 NOTIMPLEMENTED();
282}
283
284// Try to resume from a previously Terminate()d update.
285void UpdateAttempter::ResumeUpdating() {
286 // TODO(adlr): implement this method.
287 NOTIMPLEMENTED();
288}
289
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700290void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
291 if (status_ != UPDATE_STATUS_DOWNLOADING) {
292 LOG(ERROR) << "BytesReceived called while not downloading.";
293 return;
294 }
295 download_progress_ = static_cast<double>(bytes_received) /
296 static_cast<double>(total);
297 // We self throttle here
298 timespec now;
299 now.tv_sec = 0;
300 now.tv_nsec = 0;
301 if (GetCPUClockTime(&now) &&
302 CPUClockTimeGreaterThanHalfSecond(
303 CPUClockTimeElapsed(last_notify_time_, now))) {
304 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
305 }
306}
307
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700308bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
309 double* progress,
310 std::string* current_operation,
311 std::string* new_version,
312 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700313 *last_checked_time = last_checked_time_;
314 *progress = download_progress_;
315 *current_operation = UpdateStatusToString(status_);
316 *new_version = new_version_;
317 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700318 return true;
319}
320
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700321void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
322 status_ = status;
323 if (!dbus_service_)
324 return;
325 GetCPUClockTime(&last_notify_time_);
326 update_engine_service_emit_status_update(
327 dbus_service_,
328 last_checked_time_,
329 download_progress_,
330 UpdateStatusToString(status_),
331 new_version_.c_str(),
332 new_size_);
333}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700334
Darin Petkov09f96c32010-07-20 09:24:57 -0700335void UpdateAttempter::CreatePendingErrorEvent(ActionExitCode code) {
336 if (error_event_.get()) {
337 // This shouldn't really happen.
338 LOG(WARNING) << "There's already an existing pending error event.";
339 return;
340 }
341 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
342 OmahaEvent::kResultError,
343 code));
344}
345
346bool UpdateAttempter::ScheduleErrorEventAction() {
347 if (error_event_.get() == NULL)
348 return false;
349
350 shared_ptr<OmahaRequestAction> error_event_action(
351 new OmahaRequestAction(omaha_request_params_,
352 error_event_.release(), // Pass ownership.
353 new LibcurlHttpFetcher));
354 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
355 processor_.EnqueueAction(error_event_action.get());
356 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
357 processor_.StartProcessing();
358 return true;
359}
360
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700361} // namespace chromeos_update_engine