blob: b5c9c24f00531c7670ab1dc8be938e317e0d6c9b [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070016
Alex Deymo72aa0022015-06-19 21:16:49 -070017#include <inttypes.h>
18#include <sysexits.h>
19#include <unistd.h>
20
Casey Dahline844c1a2015-12-16 14:30:58 -080021#include <memory>
Darin Petkov5a7f5652010-07-22 21:40:09 -070022#include <string>
Casey Dahlin97c87052016-01-06 14:33:55 -080023#include <vector>
Darin Petkov5a7f5652010-07-22 21:40:09 -070024
Alex Deymo72aa0022015-06-19 21:16:49 -070025#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080026#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070027#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070028#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/daemons/dbus_daemon.h>
30#include <brillo/flag_helper.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070031#include <dbus/bus.h>
Casey Dahlin87ab88e2015-12-16 17:58:05 -080032
33#include "update_engine/client.h"
Casey Dahlin87ab88e2015-12-16 17:58:05 -080034#include "update_engine/dbus-constants.h"
35#include "update_engine/dbus-proxies.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080036#include "update_engine/status_update_handler.h"
37#include "update_engine/update_status.h"
Casey Dahlin87ab88e2015-12-16 17:58:05 -080038#include "update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070039
Darin Petkov5a7f5652010-07-22 21:40:09 -070040using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080041using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080042using std::vector;
Alex Deymod6deb1d2015-08-28 15:54:37 -070043using update_engine::kAttemptUpdateFlagNonInteractive;
44using update_engine::kUpdateEngineServiceName;
Casey Dahlin97c87052016-01-06 14:33:55 -080045using update_engine::UpdateStatus;
Casey Dahlin87ab88e2015-12-16 17:58:05 -080046using chromeos_update_engine::UpdateStatusToString;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070047
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070048namespace {
49
Alex Deymo72aa0022015-06-19 21:16:49 -070050// Constant to signal that we need to continue running the daemon after
51// initialization.
52const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070053
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070054class UpdateEngineClient : public brillo::DBusDaemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070055 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080056 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
57 client_ = update_engine::UpdateEngineClient::CreateInstance();
58 }
59
Alex Deymo72aa0022015-06-19 21:16:49 -070060 ~UpdateEngineClient() override = default;
61
62 protected:
63 int OnInit() override {
64 int ret = DBusDaemon::OnInit();
Casey Dahlin97c87052016-01-06 14:33:55 -080065 if (ret != EX_OK) return ret;
66 if (!InitProxy()) return 1;
Alex Deymo13e0dd62015-06-30 23:11:44 -070067 // Wait for the UpdateEngine to be available or timeout.
Casey Dahlin97c87052016-01-06 14:33:55 -080068 proxy_->GetObjectProxy()->WaitForServiceToBeAvailable(base::Bind(
69 &UpdateEngineClient::OnServiceAvailable, base::Unretained(this)));
Alex Deymo13e0dd62015-06-30 23:11:44 -070070 base::MessageLoop::current()->PostDelayedTask(
Casey Dahlin97c87052016-01-06 14:33:55 -080071 FROM_HERE, base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout,
72 base::Unretained(this)),
Alex Deymo13e0dd62015-06-30 23:11:44 -070073 base::TimeDelta::FromSeconds(10));
Alex Deymo72aa0022015-06-19 21:16:49 -070074 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070075 }
Alex Deymo72aa0022015-06-19 21:16:49 -070076
77 private:
78 bool InitProxy();
79
Alex Deymo13e0dd62015-06-30 23:11:44 -070080 // Callback called when the UpdateEngine service becomes available.
81 void OnServiceAvailable(bool service_is_available);
82
83 // Callback called when the UpdateEngine service doesn't become available
84 // after a timeout.
85 void OnServiceAvailableTimeout();
86
Alex Deymo72aa0022015-06-19 21:16:49 -070087 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080088 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070089
Casey Dahlin87ab88e2015-12-16 17:58:05 -080090 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
91 // occurred, 2 if no reboot is needed.
92 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070093
Alex Deymo72aa0022015-06-19 21:16:49 -070094 // Main method that parses and triggers all the actions based on the passed
95 // flags.
96 int ProcessFlags();
97
98 // DBus Proxy to the update_engine daemon object used for all the calls.
99 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
100
101 // Copy of argc and argv passed to main().
102 int argc_;
103 char** argv_;
104
Casey Dahline844c1a2015-12-16 14:30:58 -0800105 // Library-based client
106 unique_ptr<update_engine::UpdateEngineClient> client_;
107
Casey Dahlin97c87052016-01-06 14:33:55 -0800108 // Pointers to handlers for cleanup
109 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
110
Alex Deymo13e0dd62015-06-30 23:11:44 -0700111 // Tell whether the UpdateEngine service is available after startup.
112 bool service_is_available_{false};
113
Alex Deymo72aa0022015-06-19 21:16:49 -0700114 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
115};
116
Alex Deymo72aa0022015-06-19 21:16:49 -0700117bool UpdateEngineClient::InitProxy() {
118 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
119
120 if (!proxy_->GetObjectProxy()) {
121 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
122 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800123 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700124 return true;
125}
126
Alex Deymo13e0dd62015-06-30 23:11:44 -0700127void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
128 service_is_available_ = service_is_available;
129 if (!service_is_available) {
130 LOG(ERROR) << "UpdateEngineService not available.";
131 QuitWithExitCode(-1);
132 }
133 int ret = ProcessFlags();
Casey Dahlin97c87052016-01-06 14:33:55 -0800134 if (ret != kContinueRunning) QuitWithExitCode(ret);
Alex Deymo13e0dd62015-06-30 23:11:44 -0700135}
136
137void UpdateEngineClient::OnServiceAvailableTimeout() {
138 if (!service_is_available_) {
139 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
140 "daemon running?";
141 QuitWithExitCode(-1);
142 }
143}
144
Casey Dahlin97c87052016-01-06 14:33:55 -0800145class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
146 public:
147 ~ExitingStatusUpdateHandler() override = default;
148
149 void IPCError(const string& error) override;
150};
151
152void ExitingStatusUpdateHandler::IPCError(const string& error) {
153 LOG(ERROR) << error;
154 exit(1);
155}
156
157class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
158 public:
159 ~WatchingStatusUpdateHandler() override = default;
160
161 void HandleStatusUpdate(int64_t last_checked_time, double progress,
162 UpdateStatus current_operation,
163 const std::string& new_version,
164 int64_t new_size) override;
165};
166
167void WatchingStatusUpdateHandler::HandleStatusUpdate(
168 int64_t last_checked_time, double progress, UpdateStatus current_operation,
169 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700170 LOG(INFO) << "Got status update:";
171 LOG(INFO) << " last_checked_time: " << last_checked_time;
172 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800173 LOG(INFO) << " current_operation: "
174 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700175 LOG(INFO) << " new_version: " << new_version;
176 LOG(INFO) << " new_size: " << new_size;
177}
178
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800179bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700180 int64_t last_checked_time = 0;
181 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800182 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700183 string new_version;
184 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700185
Casey Dahlin97c87052016-01-06 14:33:55 -0800186 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
187 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800188 return false;
189 }
190
Casey Dahlin97c87052016-01-06 14:33:55 -0800191 printf("LAST_CHECKED_TIME=%" PRIi64
192 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700193 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800194 last_checked_time, progress, UpdateStatusToString(current_op),
195 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800196
197 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700198}
199
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800200int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700201 int64_t last_checked_time = 0;
202 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800203 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700204 string new_version;
205 int64_t new_size = 0;
206
Casey Dahlin97c87052016-01-06 14:33:55 -0800207 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
208 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800209 return 1;
210 }
211
Casey Dahlin97c87052016-01-06 14:33:55 -0800212 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800213 return 0;
214 }
215
216 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700217}
218
Casey Dahlin97c87052016-01-06 14:33:55 -0800219class UpdateWaitHandler : public ExitingStatusUpdateHandler {
220 public:
221 UpdateWaitHandler(bool exit_on_error) : exit_on_error_(exit_on_error) {}
222
223 ~UpdateWaitHandler() override = default;
224
225 void HandleStatusUpdate(int64_t last_checked_time, double progress,
226 UpdateStatus current_operation,
227 const std::string& new_version,
228 int64_t new_size) override;
229
230 private:
231 bool exit_on_error_;
232};
233
234void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
235 double /* progress */,
236 UpdateStatus current_operation,
237 const string& /* new_version */,
238 int64_t /* new_size */) {
239 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
240 LOG(ERROR) << "Update failed, current operations is "
241 << UpdateStatusToString(current_operation);
Darin Petkov58529db2010-08-13 09:19:47 -0700242 exit(1);
243 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800244 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700245 LOG(INFO) << "Update succeeded -- reboot needed.";
246 exit(0);
247 }
Darin Petkov58529db2010-08-13 09:19:47 -0700248}
249
Alex Deymo72aa0022015-06-19 21:16:49 -0700250int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700251 DEFINE_string(app_version, "", "Force the current app version.");
252 DEFINE_string(channel, "",
253 "Set the target channel. The device will be powerwashed if the "
254 "target channel is more stable than the current channel unless "
255 "--nopowerwash is specified.");
256 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800257 DEFINE_bool(follow, false,
258 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700259 "Exit status is 0 if the update succeeded, and 1 otherwise.");
260 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
261 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
262 DEFINE_string(p2p_update, "",
263 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
264 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800265 DEFINE_bool(powerwash, true,
266 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700267 "do a powerwash or allow it respectively.");
268 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800269 DEFINE_bool(is_reboot_needed, false,
270 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700271 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800272 DEFINE_bool(block_until_reboot_is_needed, false,
273 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700274 "needed. Returns non-zero exit status if an error occurred.");
275 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800276 DEFINE_bool(rollback, false,
277 "Perform a rollback to the previous partition. The device will "
278 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800279 DEFINE_bool(can_rollback, false,
280 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700281 "is available.");
282 DEFINE_bool(show_channel, false, "Show the current and target channels.");
283 DEFINE_bool(show_p2p_update, false,
284 "Show the current setting for peer-to-peer update sharing.");
285 DEFINE_bool(show_update_over_cellular, false,
286 "Show the current setting for updates over cellular networks.");
287 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800288 DEFINE_bool(update, false,
289 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700290 "Implies --follow.");
291 DEFINE_string(update_over_cellular, "",
292 "Enables (\"yes\") or disables (\"no\") the updates over "
293 "cellular networks.");
294 DEFINE_bool(watch_for_updates, false,
295 "Listen for status updates and print them to the screen.");
296 DEFINE_bool(prev_version, false,
297 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700298
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700299 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700300 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700301 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700302
Alex Deymo8ce80d62015-01-27 15:10:43 -0800303 // Ensure there are no positional arguments.
304 const std::vector<string> positional_args =
305 base::CommandLine::ForCurrentProcess()->GetArgs();
306 if (!positional_args.empty()) {
307 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
308 << "'. If you want to pass a value to a flag, pass it as "
309 "--flag=value.";
310 return 1;
311 }
312
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700313 // Update the status if requested.
314 if (FLAGS_reset_status) {
315 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800316
317 if (client_->ResetStatus()) {
318 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
319 "run:\n"
320 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
321 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
322 } else {
323 LOG(ERROR) << "ResetStatus failed";
324 return 1;
325 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700326 }
Darin Petkov58529db2010-08-13 09:19:47 -0700327
Alex Deymof4867c42013-06-28 14:41:39 -0700328 // Changes the current update over cellular network setting.
329 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700330 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700331 if (!allowed && FLAGS_update_over_cellular != "no") {
332 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
333 << "\". Please specify \"yes\" or \"no\".";
334 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800335 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800336 LOG(ERROR) << "Error setting the update over cellular setting.";
337 return 1;
338 }
Alex Deymof4867c42013-06-28 14:41:39 -0700339 }
340 }
341
342 // Show the current update over cellular network setting.
343 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800344 bool allowed;
345
346 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
347 LOG(ERROR) << "Error getting the update over cellular setting.";
348 return 1;
349 }
350
Alex Deymof4867c42013-06-28 14:41:39 -0700351 LOG(INFO) << "Current update over cellular network setting: "
352 << (allowed ? "ENABLED" : "DISABLED");
353 }
354
Chris Sosacb7fa882013-07-25 17:02:59 -0700355 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700356 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700357 return 1;
358 }
359
Alex Deymo5fdf7762013-07-17 20:01:40 -0700360 // Change the P2P enabled setting.
361 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700362 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700363 if (!enabled && FLAGS_p2p_update != "no") {
364 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
365 << "\". Please specify \"yes\" or \"no\".";
366 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800367 if (!client_->SetP2PUpdatePermission(enabled)) {
368 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
369 return 1;
370 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700371 }
372 }
373
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800374 // Show the rollback availability.
375 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800376 string rollback_partition;
377
378 if (!client_->GetRollbackPartition(&rollback_partition)) {
379 LOG(ERROR) << "Error while querying rollback partition availabilty.";
380 return 1;
381 }
382
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700383 bool can_rollback = true;
384 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700385 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700386 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700387 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700388 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700389 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700390
391 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700392 if (!can_rollback) {
393 return 1;
394 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800395 }
396
Alex Deymo5fdf7762013-07-17 20:01:40 -0700397 // Show the current P2P enabled setting.
398 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800399 bool enabled;
400
401 if (!client_->GetP2PUpdatePermission(&enabled)) {
402 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
403 return 1;
404 }
405
Alex Deymo5fdf7762013-07-17 20:01:40 -0700406 LOG(INFO) << "Current update using P2P setting: "
407 << (enabled ? "ENABLED" : "DISABLED");
408 }
409
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700410 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800411 if (!FLAGS_channel.empty()) {
412 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
413 LOG(ERROR) << "Error setting the channel.";
414 return 1;
415 }
416
417 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
418 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700419
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700420 // Show the current and target channels if requested.
421 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800422 string current_channel;
423 string target_channel;
424
425 if (!client_->GetChannel(&current_channel)) {
426 LOG(ERROR) << "Error getting the current channel.";
427 return 1;
428 }
429
430 if (!client_->GetTargetChannel(&target_channel)) {
431 LOG(ERROR) << "Error getting the target channel.";
432 return 1;
433 }
434
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700435 LOG(INFO) << "Current Channel: " << current_channel;
436
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700437 if (!target_channel.empty())
438 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900439 }
440
Chris Sosad317e402013-06-12 13:47:09 -0700441 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800442 !FLAGS_app_version.empty() |
443 !FLAGS_omaha_url.empty();
444 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700445
Chris Sosad317e402013-06-12 13:47:09 -0700446 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700447 LOG(ERROR) << "Incompatible flags specified with rollback."
448 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700449 return 1;
450 }
451
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700452 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700453 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800454 if (!client_->Rollback(FLAGS_powerwash)) {
455 LOG(ERROR) << "Rollback request failed.";
456 return 1;
457 }
Chris Sosad317e402013-06-12 13:47:09 -0700458 }
459
Darin Petkov58529db2010-08-13 09:19:47 -0700460 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700461 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700462 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700463 string app_version = FLAGS_app_version;
464 if (FLAGS_update && app_version.empty()) {
465 app_version = "ForcedUpdate";
466 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700467 }
Darin Petkov58529db2010-08-13 09:19:47 -0700468 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800469 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
470 FLAGS_interactive)) {
471 LOG(ERROR) << "Error checking for update.";
472 return 1;
473 }
Chris Sosa192449e2013-10-28 14:16:19 -0700474 }
Darin Petkov58529db2010-08-13 09:19:47 -0700475
Chris Sosa192449e2013-10-28 14:16:19 -0700476 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800477 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
478 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
479 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700480 LOG(ERROR) << "Multiple exclusive options selected. "
481 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700482 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700483 << "or --status.";
484 return 1;
485 }
486
487 if (FLAGS_status) {
488 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800489 if (!ShowStatus()) {
490 LOG(ERROR) << "Failed to query status";
491 return 1;
492 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700493 return 0;
494 }
Darin Petkov58529db2010-08-13 09:19:47 -0700495
Chris Sosa192449e2013-10-28 14:16:19 -0700496 if (FLAGS_follow) {
497 LOG(INFO) << "Waiting for update to complete.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800498 auto handler = new UpdateWaitHandler(true);
499 handlers_.emplace_back(handler);
500 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700501 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700502 }
503
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700504 if (FLAGS_watch_for_updates) {
505 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800506 auto handler = new WatchingStatusUpdateHandler();
507 handlers_.emplace_back(handler);
508 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700509 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700510 }
Darin Petkov58529db2010-08-13 09:19:47 -0700511
Darin Petkov296889c2010-07-23 16:20:54 -0700512 if (FLAGS_reboot) {
513 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800514 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700515 return 0;
516 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700517
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700518 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800519 string prev_version;
520
521 if (!client_->GetPrevVersion(&prev_version)) {
522 LOG(ERROR) << "Error getting previous version.";
523 } else {
524 LOG(INFO) << "Previous version = " << prev_version;
525 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700526 }
527
David Zeuthen9d73a722014-04-04 14:52:46 -0700528 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800529 int ret = GetNeedReboot();
530
531 if (ret == 1) {
532 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700533 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800534
535 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700536 }
537
538 if (FLAGS_block_until_reboot_is_needed) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800539 auto handler = new UpdateWaitHandler(false);
540 handlers_.emplace_back(handler);
541 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700542 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700543 }
544
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700545 return 0;
546}
Alex Deymo72aa0022015-06-19 21:16:49 -0700547
548} // namespace
549
550int main(int argc, char** argv) {
551 UpdateEngineClient client(argc, argv);
552 return client.Run();
553}