blob: b5d9644970e94abc67efd75a35cca9247f839654 [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"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070023#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
Darin Petkova4a8a8c2010-07-15 22:21:12 -070088void UpdateAttempter::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 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -070098 if (!omaha_request_params_.Init()) {
99 LOG(ERROR) << "Unable to initialize Omaha request device params.";
100 return;
101 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700102 CHECK(!processor_.IsRunning());
103 processor_.set_delegate(this);
104
105 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700107 new OmahaRequestAction(omaha_request_params_,
108 NULL,
109 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700110 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
111 new OmahaResponseHandlerAction);
112 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700113 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700114 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700115 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700116 shared_ptr<OmahaRequestAction> download_started_action(
117 new OmahaRequestAction(omaha_request_params_,
118 new OmahaEvent(
119 OmahaEvent::kTypeUpdateDownloadStarted,
120 OmahaEvent::kResultSuccess,
121 0),
122 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700123 shared_ptr<DownloadAction> download_action(
124 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700125 shared_ptr<OmahaRequestAction> download_finished_action(
126 new OmahaRequestAction(omaha_request_params_,
127 new OmahaEvent(
128 OmahaEvent::kTypeUpdateDownloadFinished,
129 OmahaEvent::kResultSuccess,
130 0),
131 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700132 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
133 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700134 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
135 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700136 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
137 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700138 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700139 new OmahaRequestAction(omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700140 new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700141 OmahaEvent::kResultSuccess,
142 0),
143 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700144
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700145 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700146 response_handler_action_ = response_handler_action;
147
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700148 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
149 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
150 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700151 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700152 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700153 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700154 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700155 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700156 actions_.push_back(shared_ptr<AbstractAction>(
157 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700158 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700159 actions_.push_back(shared_ptr<AbstractAction>(
160 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700161 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700162
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700163 // Enqueue the actions
164 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
165 it != actions_.end(); ++it) {
166 processor_.EnqueueAction(it->get());
167 }
168
169 // Bond them together. We have to use the leaf-types when calling
170 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700171 BondActions(update_check_action.get(),
172 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700173 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700174 filesystem_copier_action.get());
175 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700176 kernel_filesystem_copier_action.get());
177 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700178 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700179 BondActions(download_action.get(),
180 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700181 BondActions(postinstall_runner_action_precommit.get(),
182 set_bootable_flag_action.get());
183 BondActions(set_bootable_flag_action.get(),
184 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700185
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700186 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700187 processor_.StartProcessing();
188}
189
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700190void UpdateAttempter::CheckForUpdate() {
191 if (status_ != UPDATE_STATUS_IDLE) {
192 LOG(INFO) << "Check for update requested, but status is "
193 << UpdateStatusToString(status_) << ", so not checking.";
194 return;
195 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700196 Update();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700197}
198
199// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700200void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700201 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700202 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700203 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700204 actions_.clear();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700205 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700206 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700207 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700208 } else {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700209 LOG(INFO) << "Update failed.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700210 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700211 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700212}
213
214void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
215 download_progress_ = 0.0;
216 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700217 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700218}
219
220// Called whenever an action has finished processing, either successfully
221// or otherwise.
222void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
223 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700224 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700225 // Reset download progress regardless of whether or not the download action
226 // succeeded.
227 const string type = action->Type();
228 if (type == DownloadAction::StaticType())
229 download_progress_ = 0.0;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700230 if (code != kActionCodeSuccess)
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700231 return;
232 // Find out which action completed.
233 if (type == OmahaResponseHandlerAction::StaticType()) {
234 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700235 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700236 dynamic_cast<OmahaResponseHandlerAction*>(action);
237 CHECK(omaha_response_handler_action);
238 const InstallPlan& plan = omaha_response_handler_action->install_plan();
239 last_checked_time_ = time(NULL);
240 // TODO(adlr): put version in InstallPlan
241 new_version_ = "0.0.0.0";
242 new_size_ = plan.size;
243 } else if (type == DownloadAction::StaticType()) {
244 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
245 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700246}
247
248// Stop updating. An attempt will be made to record status to the disk
249// so that updates can be resumed later.
250void UpdateAttempter::Terminate() {
251 // TODO(adlr): implement this method.
252 NOTIMPLEMENTED();
253}
254
255// Try to resume from a previously Terminate()d update.
256void UpdateAttempter::ResumeUpdating() {
257 // TODO(adlr): implement this method.
258 NOTIMPLEMENTED();
259}
260
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700261void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
262 if (status_ != UPDATE_STATUS_DOWNLOADING) {
263 LOG(ERROR) << "BytesReceived called while not downloading.";
264 return;
265 }
266 download_progress_ = static_cast<double>(bytes_received) /
267 static_cast<double>(total);
268 // We self throttle here
269 timespec now;
270 now.tv_sec = 0;
271 now.tv_nsec = 0;
272 if (GetCPUClockTime(&now) &&
273 CPUClockTimeGreaterThanHalfSecond(
274 CPUClockTimeElapsed(last_notify_time_, now))) {
275 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
276 }
277}
278
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700279bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
280 double* progress,
281 std::string* current_operation,
282 std::string* new_version,
283 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700284 *last_checked_time = last_checked_time_;
285 *progress = download_progress_;
286 *current_operation = UpdateStatusToString(status_);
287 *new_version = new_version_;
288 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700289 return true;
290}
291
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700292void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
293 status_ = status;
294 if (!dbus_service_)
295 return;
296 GetCPUClockTime(&last_notify_time_);
297 update_engine_service_emit_status_update(
298 dbus_service_,
299 last_checked_time_,
300 download_progress_,
301 UpdateStatusToString(status_),
302 new_version_.c_str(),
303 new_size_);
304}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700305
306} // namespace chromeos_update_engine