blob: ebfcd1d587291f73e7ba3b8ee99b0870042553a3 [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"
21#include "update_engine/omaha_request_prep_action.h"
22#include "update_engine/omaha_response_handler_action.h"
23#include "update_engine/postinstall_runner_action.h"
24#include "update_engine/set_bootable_flag_action.h"
25#include "update_engine/update_check_action.h"
26
27using std::tr1::shared_ptr;
28using std::string;
29using std::vector;
30
31namespace chromeos_update_engine {
32
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070033namespace {
34// Returns true on success.
35bool GetCPUClockTime(struct timespec* out) {
36 return clock_gettime(CLOCK_REALTIME, out) == 0;
37}
38// Returns stop - start.
39struct timespec CPUClockTimeElapsed(const struct timespec& start,
40 const struct timespec& stop) {
41 CHECK(start.tv_sec >= 0);
42 CHECK(stop.tv_sec >= 0);
43 CHECK(start.tv_nsec >= 0);
44 CHECK(stop.tv_nsec >= 0);
45
46 const int64_t kOneBillion = 1000000000L;
47 const int64_t start64 = start.tv_sec * kOneBillion + start.tv_nsec;
48 const int64_t stop64 = stop.tv_sec * kOneBillion + stop.tv_nsec;
49
50 const int64_t result64 = stop64 - start64;
51
52 struct timespec ret;
53 ret.tv_sec = result64 / kOneBillion;
54 ret.tv_nsec = result64 % kOneBillion;
55
56 return ret;
57}
58bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) {
59 if (spec.tv_sec >= 1)
60 return true;
61 return (spec.tv_nsec > 500000000);
62}
63}
64
65const char* UpdateStatusToString(UpdateStatus status) {
66 switch (status) {
67 case UPDATE_STATUS_IDLE:
68 return "UPDATE_STATUS_IDLE";
69 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
70 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
71 case UPDATE_STATUS_UPDATE_AVAILABLE:
72 return "UPDATE_STATUS_UPDATE_AVAILABLE";
73 case UPDATE_STATUS_DOWNLOADING:
74 return "UPDATE_STATUS_DOWNLOADING";
75 case UPDATE_STATUS_VERIFYING:
76 return "UPDATE_STATUS_VERIFYING";
77 case UPDATE_STATUS_FINALIZING:
78 return "UPDATE_STATUS_FINALIZING";
79 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
80 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
81 default:
82 return "unknown status";
83 }
84}
85
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070086void UpdateAttempter::Update(bool force_full_update) {
87 full_update_ = force_full_update;
88 CHECK(!processor_.IsRunning());
89 processor_.set_delegate(this);
90
91 // Actions:
92 shared_ptr<OmahaRequestPrepAction> request_prep_action(
93 new OmahaRequestPrepAction(force_full_update));
94 shared_ptr<UpdateCheckAction> update_check_action(
95 new UpdateCheckAction(new LibcurlHttpFetcher));
96 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
97 new OmahaResponseHandlerAction);
98 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -070099 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700100 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700101 new FilesystemCopierAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700102 shared_ptr<DownloadAction> download_action(
103 new DownloadAction(new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700104 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
105 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700106 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
107 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700108 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
109 new PostinstallRunnerAction(false));
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700110
111 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700112 response_handler_action_ = response_handler_action;
113
114 actions_.push_back(shared_ptr<AbstractAction>(request_prep_action));
115 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
116 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
117 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700118 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700119 kernel_filesystem_copier_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700120 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700121 actions_.push_back(shared_ptr<AbstractAction>(
122 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700123 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700124 actions_.push_back(shared_ptr<AbstractAction>(
125 postinstall_runner_action_postcommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700126
127 // Enqueue the actions
128 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
129 it != actions_.end(); ++it) {
130 processor_.EnqueueAction(it->get());
131 }
132
133 // Bond them together. We have to use the leaf-types when calling
134 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700135 BondActions(request_prep_action.get(),
136 update_check_action.get());
137 BondActions(update_check_action.get(),
138 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700139 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700140 filesystem_copier_action.get());
141 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700142 kernel_filesystem_copier_action.get());
143 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700144 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700145 BondActions(download_action.get(),
146 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700147 BondActions(postinstall_runner_action_precommit.get(),
148 set_bootable_flag_action.get());
149 BondActions(set_bootable_flag_action.get(),
150 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700151
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700152 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700153 processor_.StartProcessing();
154}
155
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700156void UpdateAttempter::CheckForUpdate() {
157 if (status_ != UPDATE_STATUS_IDLE) {
158 LOG(INFO) << "Check for update requested, but status is "
159 << UpdateStatusToString(status_) << ", so not checking.";
160 return;
161 }
162 Update(false);
163}
164
165// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700166void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
167 bool success) {
168 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700169 LOG(INFO) << "Processing Done.";
170 if (success) {
171 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
172 } else {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700173 LOG(INFO) << "Update failed.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700174 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700175 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700176}
177
178void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
179 download_progress_ = 0.0;
180 SetStatusAndNotify(UPDATE_STATUS_IDLE);
181}
182
183// Called whenever an action has finished processing, either successfully
184// or otherwise.
185void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
186 AbstractAction* action,
187 bool success) {
188 // Reset download progress regardless of whether or not the download action
189 // succeeded.
190 const string type = action->Type();
191 if (type == DownloadAction::StaticType())
192 download_progress_ = 0.0;
193 if (!success)
194 return;
195 // Find out which action completed.
196 if (type == OmahaResponseHandlerAction::StaticType()) {
197 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
198 OmahaResponseHandlerAction* omaha_response_handler_action =
199 dynamic_cast<OmahaResponseHandlerAction*>(action);
200 CHECK(omaha_response_handler_action);
201 const InstallPlan& plan = omaha_response_handler_action->install_plan();
202 last_checked_time_ = time(NULL);
203 // TODO(adlr): put version in InstallPlan
204 new_version_ = "0.0.0.0";
205 new_size_ = plan.size;
206 } else if (type == DownloadAction::StaticType()) {
207 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
208 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700209}
210
211// Stop updating. An attempt will be made to record status to the disk
212// so that updates can be resumed later.
213void UpdateAttempter::Terminate() {
214 // TODO(adlr): implement this method.
215 NOTIMPLEMENTED();
216}
217
218// Try to resume from a previously Terminate()d update.
219void UpdateAttempter::ResumeUpdating() {
220 // TODO(adlr): implement this method.
221 NOTIMPLEMENTED();
222}
223
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700224void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
225 if (status_ != UPDATE_STATUS_DOWNLOADING) {
226 LOG(ERROR) << "BytesReceived called while not downloading.";
227 return;
228 }
229 download_progress_ = static_cast<double>(bytes_received) /
230 static_cast<double>(total);
231 // We self throttle here
232 timespec now;
233 now.tv_sec = 0;
234 now.tv_nsec = 0;
235 if (GetCPUClockTime(&now) &&
236 CPUClockTimeGreaterThanHalfSecond(
237 CPUClockTimeElapsed(last_notify_time_, now))) {
238 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
239 }
240}
241
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700242bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
243 double* progress,
244 std::string* current_operation,
245 std::string* new_version,
246 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700247 *last_checked_time = last_checked_time_;
248 *progress = download_progress_;
249 *current_operation = UpdateStatusToString(status_);
250 *new_version = new_version_;
251 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700252 return true;
253}
254
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700255void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
256 status_ = status;
257 if (!dbus_service_)
258 return;
259 GetCPUClockTime(&last_notify_time_);
260 update_engine_service_emit_status_update(
261 dbus_service_,
262 last_checked_time_,
263 download_progress_,
264 UpdateStatusToString(status_),
265 new_version_.c_str(),
266 new_size_);
267}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700268
269} // namespace chromeos_update_engine