blob: a8a3207c592f9abe31958ec7fd216c1ef783cf46 [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:
103 shared_ptr<OmahaRequestPrepAction> request_prep_action(
104 new OmahaRequestPrepAction(force_full_update));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105 shared_ptr<OmahaRequestAction> update_check_action(
106 new OmahaRequestAction(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 Petkov6a5b3222010-07-13 14:55:28 -0700121
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700122 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700123 response_handler_action_ = response_handler_action;
124
125 actions_.push_back(shared_ptr<AbstractAction>(request_prep_action));
126 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
127 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
128 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700129 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700130 kernel_filesystem_copier_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700131 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700132 actions_.push_back(shared_ptr<AbstractAction>(
133 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700134 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700135 actions_.push_back(shared_ptr<AbstractAction>(
136 postinstall_runner_action_postcommit));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700137
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700138 // Enqueue the actions
139 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
140 it != actions_.end(); ++it) {
141 processor_.EnqueueAction(it->get());
142 }
143
144 // Bond them together. We have to use the leaf-types when calling
145 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700146 BondActions(request_prep_action.get(),
147 update_check_action.get());
148 BondActions(update_check_action.get(),
149 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700151 filesystem_copier_action.get());
152 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700153 kernel_filesystem_copier_action.get());
154 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700155 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700156 BondActions(download_action.get(),
157 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700158 BondActions(postinstall_runner_action_precommit.get(),
159 set_bootable_flag_action.get());
160 BondActions(set_bootable_flag_action.get(),
161 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700162
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700163 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700164 processor_.StartProcessing();
165}
166
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700167void UpdateAttempter::CheckForUpdate() {
168 if (status_ != UPDATE_STATUS_IDLE) {
169 LOG(INFO) << "Check for update requested, but status is "
170 << UpdateStatusToString(status_) << ", so not checking.";
171 return;
172 }
173 Update(false);
174}
175
176// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700177void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
178 bool success) {
179 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700180 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700181 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700182 if (success) {
183 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700184 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700185 } else {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700186 LOG(INFO) << "Update failed.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700187 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700189}
190
191void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
192 download_progress_ = 0.0;
193 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700194 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700195}
196
197// Called whenever an action has finished processing, either successfully
198// or otherwise.
199void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
200 AbstractAction* action,
201 bool success) {
202 // Reset download progress regardless of whether or not the download action
203 // succeeded.
204 const string type = action->Type();
205 if (type == DownloadAction::StaticType())
206 download_progress_ = 0.0;
207 if (!success)
208 return;
209 // Find out which action completed.
210 if (type == OmahaResponseHandlerAction::StaticType()) {
211 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700212 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700213 dynamic_cast<OmahaResponseHandlerAction*>(action);
214 CHECK(omaha_response_handler_action);
215 const InstallPlan& plan = omaha_response_handler_action->install_plan();
216 last_checked_time_ = time(NULL);
217 // TODO(adlr): put version in InstallPlan
218 new_version_ = "0.0.0.0";
219 new_size_ = plan.size;
220 } else if (type == DownloadAction::StaticType()) {
221 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
222 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700223}
224
225// Stop updating. An attempt will be made to record status to the disk
226// so that updates can be resumed later.
227void UpdateAttempter::Terminate() {
228 // TODO(adlr): implement this method.
229 NOTIMPLEMENTED();
230}
231
232// Try to resume from a previously Terminate()d update.
233void UpdateAttempter::ResumeUpdating() {
234 // TODO(adlr): implement this method.
235 NOTIMPLEMENTED();
236}
237
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700238void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
239 if (status_ != UPDATE_STATUS_DOWNLOADING) {
240 LOG(ERROR) << "BytesReceived called while not downloading.";
241 return;
242 }
243 download_progress_ = static_cast<double>(bytes_received) /
244 static_cast<double>(total);
245 // We self throttle here
246 timespec now;
247 now.tv_sec = 0;
248 now.tv_nsec = 0;
249 if (GetCPUClockTime(&now) &&
250 CPUClockTimeGreaterThanHalfSecond(
251 CPUClockTimeElapsed(last_notify_time_, now))) {
252 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
253 }
254}
255
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700256bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
257 double* progress,
258 std::string* current_operation,
259 std::string* new_version,
260 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700261 *last_checked_time = last_checked_time_;
262 *progress = download_progress_;
263 *current_operation = UpdateStatusToString(status_);
264 *new_version = new_version_;
265 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700266 return true;
267}
268
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700269void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
270 status_ = status;
271 if (!dbus_service_)
272 return;
273 GetCPUClockTime(&last_notify_time_);
274 update_engine_service_emit_status_update(
275 dbus_service_,
276 last_checked_time_,
277 download_progress_,
278 UpdateStatusToString(status_),
279 new_version_.c_str(),
280 new_size_);
281}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700282
283} // namespace chromeos_update_engine