blob: f034b0bbb870e1755aa45926239aa6d76378cb72 [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>
16#include <glib.h>
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070017#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070018#include "update_engine/download_action.h"
19#include "update_engine/filesystem_copier_action.h"
20#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070021#include "update_engine/omaha_request_action.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070022#include "update_engine/omaha_request_prep_action.h"
23#include "update_engine/omaha_response_handler_action.h"
24#include "update_engine/postinstall_runner_action.h"
25#include "update_engine/set_bootable_flag_action.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026
27using std::tr1::shared_ptr;
28using std::string;
29using std::vector;
30
31namespace chromeos_update_engine {
32
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070033const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
34
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070035namespace {
36// Returns true on success.
37bool GetCPUClockTime(struct timespec* out) {
38 return clock_gettime(CLOCK_REALTIME, out) == 0;
39}
40// Returns stop - start.
41struct timespec CPUClockTimeElapsed(const struct timespec& start,
42 const struct timespec& stop) {
43 CHECK(start.tv_sec >= 0);
44 CHECK(stop.tv_sec >= 0);
45 CHECK(start.tv_nsec >= 0);
46 CHECK(stop.tv_nsec >= 0);
47
48 const int64_t kOneBillion = 1000000000L;
49 const int64_t start64 = start.tv_sec * kOneBillion + start.tv_nsec;
50 const int64_t stop64 = stop.tv_sec * kOneBillion + stop.tv_nsec;
51
52 const int64_t result64 = stop64 - start64;
53
54 struct timespec ret;
55 ret.tv_sec = result64 / kOneBillion;
56 ret.tv_nsec = result64 % kOneBillion;
57
58 return ret;
59}
60bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) {
61 if (spec.tv_sec >= 1)
62 return true;
63 return (spec.tv_nsec > 500000000);
64}
65}
66
67const char* UpdateStatusToString(UpdateStatus status) {
68 switch (status) {
69 case UPDATE_STATUS_IDLE:
70 return "UPDATE_STATUS_IDLE";
71 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
72 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
73 case UPDATE_STATUS_UPDATE_AVAILABLE:
74 return "UPDATE_STATUS_UPDATE_AVAILABLE";
75 case UPDATE_STATUS_DOWNLOADING:
76 return "UPDATE_STATUS_DOWNLOADING";
77 case UPDATE_STATUS_VERIFYING:
78 return "UPDATE_STATUS_VERIFYING";
79 case UPDATE_STATUS_FINALIZING:
80 return "UPDATE_STATUS_FINALIZING";
81 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
82 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
83 default:
84 return "unknown status";
85 }
86}
87
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070088void UpdateAttempter::Update(bool force_full_update) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070089 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
90 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
91 << "reboot";
92 return;
93 }
94 if (status_ != UPDATE_STATUS_IDLE) {
95 // Update in progress. Do nothing
96 return;
97 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070098 full_update_ = force_full_update;
99 CHECK(!processor_.IsRunning());
100 processor_.set_delegate(this);
101
102 // Actions:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700103 shared_ptr<OmahaRequestPrepAction> update_check_prep_action(
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700104 new OmahaRequestPrepAction(force_full_update));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700106 new OmahaRequestAction(NULL, new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700107 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
108 new OmahaResponseHandlerAction);
109 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700110 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700111 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700112 new FilesystemCopierAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700113 shared_ptr<DownloadAction> download_action(
114 new DownloadAction(new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700115 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
116 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700117 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
118 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700119 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
120 new PostinstallRunnerAction(false));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700121 shared_ptr<OmahaRequestPrepAction> install_success_prep_action(
122 new OmahaRequestPrepAction(false));
123 shared_ptr<OmahaRequestAction> install_success_action(
124 new OmahaRequestAction(new OmahaEvent(OmahaEvent::kTypeInstallComplete,
125 OmahaEvent::kResultSuccess,
126 0),
127 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700128
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700129 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700130 response_handler_action_ = response_handler_action;
131
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700132 actions_.push_back(shared_ptr<AbstractAction>(update_check_prep_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700133 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
134 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
135 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700136 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700137 kernel_filesystem_copier_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700138 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700139 actions_.push_back(shared_ptr<AbstractAction>(
140 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700141 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700142 actions_.push_back(shared_ptr<AbstractAction>(
143 postinstall_runner_action_postcommit));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700144 actions_.push_back(shared_ptr<AbstractAction>(install_success_prep_action));
145 actions_.push_back(shared_ptr<AbstractAction>(install_success_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700146
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700147 // Enqueue the actions
148 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
149 it != actions_.end(); ++it) {
150 processor_.EnqueueAction(it->get());
151 }
152
153 // Bond them together. We have to use the leaf-types when calling
154 // BondActions().
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700155 BondActions(update_check_prep_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700156 update_check_action.get());
157 BondActions(update_check_action.get(),
158 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700159 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700160 filesystem_copier_action.get());
161 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700162 kernel_filesystem_copier_action.get());
163 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700164 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700165 BondActions(download_action.get(),
166 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700167 BondActions(postinstall_runner_action_precommit.get(),
168 set_bootable_flag_action.get());
169 BondActions(set_bootable_flag_action.get(),
170 postinstall_runner_action_postcommit.get());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700171 BondActions(install_success_prep_action.get(),
172 install_success_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700173
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700174 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700175 processor_.StartProcessing();
176}
177
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700178void UpdateAttempter::CheckForUpdate() {
179 if (status_ != UPDATE_STATUS_IDLE) {
180 LOG(INFO) << "Check for update requested, but status is "
181 << UpdateStatusToString(status_) << ", so not checking.";
182 return;
183 }
184 Update(false);
185}
186
187// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
189 bool success) {
190 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700191 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700192 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700193 if (success) {
194 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700195 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700196 } else {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700197 LOG(INFO) << "Update failed.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700198 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700199 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700200}
201
202void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
203 download_progress_ = 0.0;
204 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700205 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700206}
207
208// Called whenever an action has finished processing, either successfully
209// or otherwise.
210void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
211 AbstractAction* action,
212 bool success) {
213 // Reset download progress regardless of whether or not the download action
214 // succeeded.
215 const string type = action->Type();
216 if (type == DownloadAction::StaticType())
217 download_progress_ = 0.0;
218 if (!success)
219 return;
220 // Find out which action completed.
221 if (type == OmahaResponseHandlerAction::StaticType()) {
222 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700223 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700224 dynamic_cast<OmahaResponseHandlerAction*>(action);
225 CHECK(omaha_response_handler_action);
226 const InstallPlan& plan = omaha_response_handler_action->install_plan();
227 last_checked_time_ = time(NULL);
228 // TODO(adlr): put version in InstallPlan
229 new_version_ = "0.0.0.0";
230 new_size_ = plan.size;
231 } else if (type == DownloadAction::StaticType()) {
232 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
233 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700234}
235
236// Stop updating. An attempt will be made to record status to the disk
237// so that updates can be resumed later.
238void UpdateAttempter::Terminate() {
239 // TODO(adlr): implement this method.
240 NOTIMPLEMENTED();
241}
242
243// Try to resume from a previously Terminate()d update.
244void UpdateAttempter::ResumeUpdating() {
245 // TODO(adlr): implement this method.
246 NOTIMPLEMENTED();
247}
248
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700249void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
250 if (status_ != UPDATE_STATUS_DOWNLOADING) {
251 LOG(ERROR) << "BytesReceived called while not downloading.";
252 return;
253 }
254 download_progress_ = static_cast<double>(bytes_received) /
255 static_cast<double>(total);
256 // We self throttle here
257 timespec now;
258 now.tv_sec = 0;
259 now.tv_nsec = 0;
260 if (GetCPUClockTime(&now) &&
261 CPUClockTimeGreaterThanHalfSecond(
262 CPUClockTimeElapsed(last_notify_time_, now))) {
263 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
264 }
265}
266
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700267bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
268 double* progress,
269 std::string* current_operation,
270 std::string* new_version,
271 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700272 *last_checked_time = last_checked_time_;
273 *progress = download_progress_;
274 *current_operation = UpdateStatusToString(status_);
275 *new_version = new_version_;
276 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700277 return true;
278}
279
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700280void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
281 status_ = status;
282 if (!dbus_service_)
283 return;
284 GetCPUClockTime(&last_notify_time_);
285 update_engine_service_emit_status_update(
286 dbus_service_,
287 last_checked_time_,
288 download_progress_,
289 UpdateStatusToString(status_),
290 new_version_.c_str(),
291 new_size_);
292}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700293
294} // namespace chromeos_update_engine