blob: f062813679c0d572b33b490467de395f72fe1dbb [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>
Casey Dahlin19441412016-01-07 14:56:40 -080029#include <brillo/daemons/daemon.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/flag_helper.h>
Casey Dahlin87ab88e2015-12-16 17:58:05 -080031
32#include "update_engine/client.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080033#include "update_engine/status_update_handler.h"
34#include "update_engine/update_status.h"
Casey Dahlin87ab88e2015-12-16 17:58:05 -080035#include "update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
Darin Petkov5a7f5652010-07-22 21:40:09 -070037using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080038using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080039using std::vector;
Casey Dahlin97c87052016-01-06 14:33:55 -080040using update_engine::UpdateStatus;
Casey Dahlin87ab88e2015-12-16 17:58:05 -080041using chromeos_update_engine::UpdateStatusToString;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070042
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070043namespace {
44
Alex Deymo72aa0022015-06-19 21:16:49 -070045// Constant to signal that we need to continue running the daemon after
46// initialization.
47const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070048
Casey Dahlin19441412016-01-07 14:56:40 -080049class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070050 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080051 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
Casey Dahline844c1a2015-12-16 14:30:58 -080052 }
53
Alex Deymo72aa0022015-06-19 21:16:49 -070054 ~UpdateEngineClient() override = default;
55
56 protected:
57 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080058 int ret = Daemon::OnInit();
Casey Dahlin97c87052016-01-06 14:33:55 -080059 if (ret != EX_OK) return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080060
61 client_ = update_engine::UpdateEngineClient::CreateInstance();
62
63 if (!client_) {
64 LOG(ERROR) << "UpdateEngineService not available.";
65 return 1;
66 }
67
68 ret = ProcessFlags();
69 if (ret != kContinueRunning) QuitWithExitCode(ret);
70
Alex Deymo72aa0022015-06-19 21:16:49 -070071 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070072 }
Alex Deymo72aa0022015-06-19 21:16:49 -070073
74 private:
Alex Deymo13e0dd62015-06-30 23:11:44 -070075
Alex Deymo72aa0022015-06-19 21:16:49 -070076 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080077 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070078
Casey Dahlin87ab88e2015-12-16 17:58:05 -080079 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
80 // occurred, 2 if no reboot is needed.
81 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070082
Alex Deymo72aa0022015-06-19 21:16:49 -070083 // Main method that parses and triggers all the actions based on the passed
84 // flags.
85 int ProcessFlags();
86
Alex Deymo72aa0022015-06-19 21:16:49 -070087 // Copy of argc and argv passed to main().
88 int argc_;
89 char** argv_;
90
Casey Dahline844c1a2015-12-16 14:30:58 -080091 // Library-based client
92 unique_ptr<update_engine::UpdateEngineClient> client_;
93
Casey Dahlin97c87052016-01-06 14:33:55 -080094 // Pointers to handlers for cleanup
95 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
96
Alex Deymo13e0dd62015-06-30 23:11:44 -070097 // Tell whether the UpdateEngine service is available after startup.
98 bool service_is_available_{false};
99
Alex Deymo72aa0022015-06-19 21:16:49 -0700100 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
101};
102
Casey Dahlin97c87052016-01-06 14:33:55 -0800103class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
104 public:
105 ~ExitingStatusUpdateHandler() override = default;
106
107 void IPCError(const string& error) override;
108};
109
110void ExitingStatusUpdateHandler::IPCError(const string& error) {
111 LOG(ERROR) << error;
112 exit(1);
113}
114
115class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
116 public:
117 ~WatchingStatusUpdateHandler() override = default;
118
119 void HandleStatusUpdate(int64_t last_checked_time, double progress,
120 UpdateStatus current_operation,
121 const std::string& new_version,
122 int64_t new_size) override;
123};
124
125void WatchingStatusUpdateHandler::HandleStatusUpdate(
126 int64_t last_checked_time, double progress, UpdateStatus current_operation,
127 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700128 LOG(INFO) << "Got status update:";
129 LOG(INFO) << " last_checked_time: " << last_checked_time;
130 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800131 LOG(INFO) << " current_operation: "
132 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700133 LOG(INFO) << " new_version: " << new_version;
134 LOG(INFO) << " new_size: " << new_size;
135}
136
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800137bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700138 int64_t last_checked_time = 0;
139 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800140 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700141 string new_version;
142 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700143
Casey Dahlin97c87052016-01-06 14:33:55 -0800144 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
145 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800146 return false;
147 }
148
Casey Dahlin97c87052016-01-06 14:33:55 -0800149 printf("LAST_CHECKED_TIME=%" PRIi64
150 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700151 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800152 last_checked_time, progress, UpdateStatusToString(current_op),
153 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800154
155 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700156}
157
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800158int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700159 int64_t last_checked_time = 0;
160 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800161 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700162 string new_version;
163 int64_t new_size = 0;
164
Casey Dahlin97c87052016-01-06 14:33:55 -0800165 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
166 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800167 return 1;
168 }
169
Casey Dahlin97c87052016-01-06 14:33:55 -0800170 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800171 return 0;
172 }
173
174 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700175}
176
Casey Dahlin97c87052016-01-06 14:33:55 -0800177class UpdateWaitHandler : public ExitingStatusUpdateHandler {
178 public:
179 UpdateWaitHandler(bool exit_on_error) : exit_on_error_(exit_on_error) {}
180
181 ~UpdateWaitHandler() override = default;
182
183 void HandleStatusUpdate(int64_t last_checked_time, double progress,
184 UpdateStatus current_operation,
185 const std::string& new_version,
186 int64_t new_size) override;
187
188 private:
189 bool exit_on_error_;
190};
191
192void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
193 double /* progress */,
194 UpdateStatus current_operation,
195 const string& /* new_version */,
196 int64_t /* new_size */) {
197 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
198 LOG(ERROR) << "Update failed, current operations is "
199 << UpdateStatusToString(current_operation);
Darin Petkov58529db2010-08-13 09:19:47 -0700200 exit(1);
201 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800202 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700203 LOG(INFO) << "Update succeeded -- reboot needed.";
204 exit(0);
205 }
Darin Petkov58529db2010-08-13 09:19:47 -0700206}
207
Alex Deymo72aa0022015-06-19 21:16:49 -0700208int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700209 DEFINE_string(app_version, "", "Force the current app version.");
210 DEFINE_string(channel, "",
211 "Set the target channel. The device will be powerwashed if the "
212 "target channel is more stable than the current channel unless "
213 "--nopowerwash is specified.");
214 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800215 DEFINE_bool(follow, false,
216 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700217 "Exit status is 0 if the update succeeded, and 1 otherwise.");
218 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
219 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
220 DEFINE_string(p2p_update, "",
221 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
222 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800223 DEFINE_bool(powerwash, true,
224 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700225 "do a powerwash or allow it respectively.");
226 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800227 DEFINE_bool(is_reboot_needed, false,
228 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700229 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800230 DEFINE_bool(block_until_reboot_is_needed, false,
231 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700232 "needed. Returns non-zero exit status if an error occurred.");
233 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800234 DEFINE_bool(rollback, false,
235 "Perform a rollback to the previous partition. The device will "
236 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800237 DEFINE_bool(can_rollback, false,
238 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700239 "is available.");
240 DEFINE_bool(show_channel, false, "Show the current and target channels.");
241 DEFINE_bool(show_p2p_update, false,
242 "Show the current setting for peer-to-peer update sharing.");
243 DEFINE_bool(show_update_over_cellular, false,
244 "Show the current setting for updates over cellular networks.");
245 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800246 DEFINE_bool(update, false,
247 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700248 "Implies --follow.");
249 DEFINE_string(update_over_cellular, "",
250 "Enables (\"yes\") or disables (\"no\") the updates over "
251 "cellular networks.");
252 DEFINE_bool(watch_for_updates, false,
253 "Listen for status updates and print them to the screen.");
254 DEFINE_bool(prev_version, false,
255 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700256
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700257 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700258 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700259 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700260
Alex Deymo8ce80d62015-01-27 15:10:43 -0800261 // Ensure there are no positional arguments.
262 const std::vector<string> positional_args =
263 base::CommandLine::ForCurrentProcess()->GetArgs();
264 if (!positional_args.empty()) {
265 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
266 << "'. If you want to pass a value to a flag, pass it as "
267 "--flag=value.";
268 return 1;
269 }
270
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700271 // Update the status if requested.
272 if (FLAGS_reset_status) {
273 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800274
275 if (client_->ResetStatus()) {
276 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
277 "run:\n"
278 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
279 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
280 } else {
281 LOG(ERROR) << "ResetStatus failed";
282 return 1;
283 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700284 }
Darin Petkov58529db2010-08-13 09:19:47 -0700285
Alex Deymof4867c42013-06-28 14:41:39 -0700286 // Changes the current update over cellular network setting.
287 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700288 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700289 if (!allowed && FLAGS_update_over_cellular != "no") {
290 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
291 << "\". Please specify \"yes\" or \"no\".";
292 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800293 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800294 LOG(ERROR) << "Error setting the update over cellular setting.";
295 return 1;
296 }
Alex Deymof4867c42013-06-28 14:41:39 -0700297 }
298 }
299
300 // Show the current update over cellular network setting.
301 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800302 bool allowed;
303
304 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
305 LOG(ERROR) << "Error getting the update over cellular setting.";
306 return 1;
307 }
308
Alex Deymof4867c42013-06-28 14:41:39 -0700309 LOG(INFO) << "Current update over cellular network setting: "
310 << (allowed ? "ENABLED" : "DISABLED");
311 }
312
Chris Sosacb7fa882013-07-25 17:02:59 -0700313 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700314 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700315 return 1;
316 }
317
Alex Deymo5fdf7762013-07-17 20:01:40 -0700318 // Change the P2P enabled setting.
319 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700320 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700321 if (!enabled && FLAGS_p2p_update != "no") {
322 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
323 << "\". Please specify \"yes\" or \"no\".";
324 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800325 if (!client_->SetP2PUpdatePermission(enabled)) {
326 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
327 return 1;
328 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700329 }
330 }
331
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800332 // Show the rollback availability.
333 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800334 string rollback_partition;
335
336 if (!client_->GetRollbackPartition(&rollback_partition)) {
337 LOG(ERROR) << "Error while querying rollback partition availabilty.";
338 return 1;
339 }
340
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700341 bool can_rollback = true;
342 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700343 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700344 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700345 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700346 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700347 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700348
349 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700350 if (!can_rollback) {
351 return 1;
352 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800353 }
354
Alex Deymo5fdf7762013-07-17 20:01:40 -0700355 // Show the current P2P enabled setting.
356 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800357 bool enabled;
358
359 if (!client_->GetP2PUpdatePermission(&enabled)) {
360 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
361 return 1;
362 }
363
Alex Deymo5fdf7762013-07-17 20:01:40 -0700364 LOG(INFO) << "Current update using P2P setting: "
365 << (enabled ? "ENABLED" : "DISABLED");
366 }
367
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700368 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800369 if (!FLAGS_channel.empty()) {
370 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
371 LOG(ERROR) << "Error setting the channel.";
372 return 1;
373 }
374
375 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
376 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700377
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700378 // Show the current and target channels if requested.
379 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800380 string current_channel;
381 string target_channel;
382
383 if (!client_->GetChannel(&current_channel)) {
384 LOG(ERROR) << "Error getting the current channel.";
385 return 1;
386 }
387
388 if (!client_->GetTargetChannel(&target_channel)) {
389 LOG(ERROR) << "Error getting the target channel.";
390 return 1;
391 }
392
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700393 LOG(INFO) << "Current Channel: " << current_channel;
394
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700395 if (!target_channel.empty())
396 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900397 }
398
Chris Sosad317e402013-06-12 13:47:09 -0700399 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800400 !FLAGS_app_version.empty() |
401 !FLAGS_omaha_url.empty();
402 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700403
Chris Sosad317e402013-06-12 13:47:09 -0700404 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700405 LOG(ERROR) << "Incompatible flags specified with rollback."
406 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700407 return 1;
408 }
409
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700410 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700411 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800412 if (!client_->Rollback(FLAGS_powerwash)) {
413 LOG(ERROR) << "Rollback request failed.";
414 return 1;
415 }
Chris Sosad317e402013-06-12 13:47:09 -0700416 }
417
Darin Petkov58529db2010-08-13 09:19:47 -0700418 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700419 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700420 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700421 string app_version = FLAGS_app_version;
422 if (FLAGS_update && app_version.empty()) {
423 app_version = "ForcedUpdate";
424 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700425 }
Darin Petkov58529db2010-08-13 09:19:47 -0700426 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800427 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
428 FLAGS_interactive)) {
429 LOG(ERROR) << "Error checking for update.";
430 return 1;
431 }
Chris Sosa192449e2013-10-28 14:16:19 -0700432 }
Darin Petkov58529db2010-08-13 09:19:47 -0700433
Chris Sosa192449e2013-10-28 14:16:19 -0700434 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800435 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
436 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
437 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700438 LOG(ERROR) << "Multiple exclusive options selected. "
439 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700440 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700441 << "or --status.";
442 return 1;
443 }
444
445 if (FLAGS_status) {
446 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800447 if (!ShowStatus()) {
448 LOG(ERROR) << "Failed to query status";
449 return 1;
450 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700451 return 0;
452 }
Darin Petkov58529db2010-08-13 09:19:47 -0700453
Chris Sosa192449e2013-10-28 14:16:19 -0700454 if (FLAGS_follow) {
455 LOG(INFO) << "Waiting for update to complete.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800456 auto handler = new UpdateWaitHandler(true);
457 handlers_.emplace_back(handler);
458 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700459 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700460 }
461
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700462 if (FLAGS_watch_for_updates) {
463 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800464 auto handler = new WatchingStatusUpdateHandler();
465 handlers_.emplace_back(handler);
466 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700467 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700468 }
Darin Petkov58529db2010-08-13 09:19:47 -0700469
Darin Petkov296889c2010-07-23 16:20:54 -0700470 if (FLAGS_reboot) {
471 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800472 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700473 return 0;
474 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700475
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700476 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800477 string prev_version;
478
479 if (!client_->GetPrevVersion(&prev_version)) {
480 LOG(ERROR) << "Error getting previous version.";
481 } else {
482 LOG(INFO) << "Previous version = " << prev_version;
483 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700484 }
485
David Zeuthen9d73a722014-04-04 14:52:46 -0700486 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800487 int ret = GetNeedReboot();
488
489 if (ret == 1) {
490 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700491 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800492
493 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700494 }
495
496 if (FLAGS_block_until_reboot_is_needed) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800497 auto handler = new UpdateWaitHandler(false);
498 handlers_.emplace_back(handler);
499 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700500 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700501 }
502
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700503 return 0;
504}
Alex Deymo72aa0022015-06-19 21:16:49 -0700505
506} // namespace
507
508int main(int argc, char** argv) {
509 UpdateEngineClient client(argc, argv);
510 return client.Run();
511}