blob: b94ee61bad4349aaee92dfd21634739d38b82412 [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
Alex Deymo690810b2016-01-19 16:11:40 -080037using chromeos_update_engine::UpdateStatusToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070038using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080039using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080040using std::vector;
Casey Dahlin97c87052016-01-06 14:33:55 -080041using update_engine::UpdateStatus;
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
Alex Deymo690810b2016-01-19 16:11:40 -080068 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
69 // of the ProcessFlags method after the Daemon initialization is done.
70 base::MessageLoop::current()->PostTask(
71 FROM_HERE,
72 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
73 base::Unretained(this)));
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:
Alex Deymo72aa0022015-06-19 21:16:49 -070078 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080079 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070080
Casey Dahlin87ab88e2015-12-16 17:58:05 -080081 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
82 // occurred, 2 if no reboot is needed.
83 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070084
Alex Deymo72aa0022015-06-19 21:16:49 -070085 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -080086 // flags. Returns the exit code of the program of kContinueRunning if it
87 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -070088 int ProcessFlags();
89
Alex Deymo690810b2016-01-19 16:11:40 -080090 // Processes the flags and exits the program accordingly.
91 void ProcessFlagsAndExit();
92
Alex Deymo72aa0022015-06-19 21:16:49 -070093 // Copy of argc and argv passed to main().
94 int argc_;
95 char** argv_;
96
Casey Dahline844c1a2015-12-16 14:30:58 -080097 // Library-based client
98 unique_ptr<update_engine::UpdateEngineClient> client_;
99
Casey Dahlin97c87052016-01-06 14:33:55 -0800100 // Pointers to handlers for cleanup
101 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
102
Alex Deymo13e0dd62015-06-30 23:11:44 -0700103 // Tell whether the UpdateEngine service is available after startup.
104 bool service_is_available_{false};
105
Alex Deymo72aa0022015-06-19 21:16:49 -0700106 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
107};
108
Casey Dahlin97c87052016-01-06 14:33:55 -0800109class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
110 public:
111 ~ExitingStatusUpdateHandler() override = default;
112
113 void IPCError(const string& error) override;
114};
115
116void ExitingStatusUpdateHandler::IPCError(const string& error) {
117 LOG(ERROR) << error;
118 exit(1);
119}
120
121class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
122 public:
123 ~WatchingStatusUpdateHandler() override = default;
124
Alex Deymo690810b2016-01-19 16:11:40 -0800125 void HandleStatusUpdate(int64_t last_checked_time,
126 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800127 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800128 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800129 int64_t new_size) override;
130};
131
132void WatchingStatusUpdateHandler::HandleStatusUpdate(
133 int64_t last_checked_time, double progress, UpdateStatus current_operation,
134 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700135 LOG(INFO) << "Got status update:";
136 LOG(INFO) << " last_checked_time: " << last_checked_time;
137 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800138 LOG(INFO) << " current_operation: "
139 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700140 LOG(INFO) << " new_version: " << new_version;
141 LOG(INFO) << " new_size: " << new_size;
142}
143
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800144bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700145 int64_t last_checked_time = 0;
146 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800147 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700148 string new_version;
149 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700150
Casey Dahlin97c87052016-01-06 14:33:55 -0800151 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
152 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800153 return false;
154 }
155
Casey Dahlin97c87052016-01-06 14:33:55 -0800156 printf("LAST_CHECKED_TIME=%" PRIi64
157 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700158 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800159 last_checked_time, progress, UpdateStatusToString(current_op),
160 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800161
162 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700163}
164
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800165int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700166 int64_t last_checked_time = 0;
167 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800168 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700169 string new_version;
170 int64_t new_size = 0;
171
Casey Dahlin97c87052016-01-06 14:33:55 -0800172 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
173 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800174 return 1;
175 }
176
Casey Dahlin97c87052016-01-06 14:33:55 -0800177 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800178 return 0;
179 }
180
181 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700182}
183
Casey Dahlin97c87052016-01-06 14:33:55 -0800184class UpdateWaitHandler : public ExitingStatusUpdateHandler {
185 public:
Alex Deymo690810b2016-01-19 16:11:40 -0800186 explicit UpdateWaitHandler(bool exit_on_error)
187 : exit_on_error_(exit_on_error) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800188
189 ~UpdateWaitHandler() override = default;
190
Alex Deymo690810b2016-01-19 16:11:40 -0800191 void HandleStatusUpdate(int64_t last_checked_time,
192 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800193 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800194 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800195 int64_t new_size) override;
196
197 private:
198 bool exit_on_error_;
199};
200
201void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
202 double /* progress */,
203 UpdateStatus current_operation,
204 const string& /* new_version */,
205 int64_t /* new_size */) {
206 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
207 LOG(ERROR) << "Update failed, current operations is "
208 << UpdateStatusToString(current_operation);
Darin Petkov58529db2010-08-13 09:19:47 -0700209 exit(1);
210 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800211 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700212 LOG(INFO) << "Update succeeded -- reboot needed.";
213 exit(0);
214 }
Darin Petkov58529db2010-08-13 09:19:47 -0700215}
216
Alex Deymo72aa0022015-06-19 21:16:49 -0700217int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700218 DEFINE_string(app_version, "", "Force the current app version.");
219 DEFINE_string(channel, "",
220 "Set the target channel. The device will be powerwashed if the "
221 "target channel is more stable than the current channel unless "
222 "--nopowerwash is specified.");
223 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800224 DEFINE_bool(follow, false,
225 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700226 "Exit status is 0 if the update succeeded, and 1 otherwise.");
227 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
228 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
229 DEFINE_string(p2p_update, "",
230 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
231 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800232 DEFINE_bool(powerwash, true,
233 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700234 "do a powerwash or allow it respectively.");
235 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800236 DEFINE_bool(is_reboot_needed, false,
237 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700238 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800239 DEFINE_bool(block_until_reboot_is_needed, false,
240 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700241 "needed. Returns non-zero exit status if an error occurred.");
242 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800243 DEFINE_bool(rollback, false,
244 "Perform a rollback to the previous partition. The device will "
245 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800246 DEFINE_bool(can_rollback, false,
247 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700248 "is available.");
249 DEFINE_bool(show_channel, false, "Show the current and target channels.");
250 DEFINE_bool(show_p2p_update, false,
251 "Show the current setting for peer-to-peer update sharing.");
252 DEFINE_bool(show_update_over_cellular, false,
253 "Show the current setting for updates over cellular networks.");
254 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800255 DEFINE_bool(update, false,
256 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700257 "Implies --follow.");
258 DEFINE_string(update_over_cellular, "",
259 "Enables (\"yes\") or disables (\"no\") the updates over "
260 "cellular networks.");
261 DEFINE_bool(watch_for_updates, false,
262 "Listen for status updates and print them to the screen.");
263 DEFINE_bool(prev_version, false,
264 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700265
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700266 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700267 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700268 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700269
Alex Deymo8ce80d62015-01-27 15:10:43 -0800270 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800271 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800272 base::CommandLine::ForCurrentProcess()->GetArgs();
273 if (!positional_args.empty()) {
274 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
275 << "'. If you want to pass a value to a flag, pass it as "
276 "--flag=value.";
277 return 1;
278 }
279
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700280 // Update the status if requested.
281 if (FLAGS_reset_status) {
282 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800283
284 if (client_->ResetStatus()) {
285 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
286 "run:\n"
287 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
288 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
289 } else {
290 LOG(ERROR) << "ResetStatus failed";
291 return 1;
292 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700293 }
Darin Petkov58529db2010-08-13 09:19:47 -0700294
Alex Deymof4867c42013-06-28 14:41:39 -0700295 // Changes the current update over cellular network setting.
296 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700297 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700298 if (!allowed && FLAGS_update_over_cellular != "no") {
299 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
300 << "\". Please specify \"yes\" or \"no\".";
301 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800302 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800303 LOG(ERROR) << "Error setting the update over cellular setting.";
304 return 1;
305 }
Alex Deymof4867c42013-06-28 14:41:39 -0700306 }
307 }
308
309 // Show the current update over cellular network setting.
310 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800311 bool allowed;
312
313 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
314 LOG(ERROR) << "Error getting the update over cellular setting.";
315 return 1;
316 }
317
Alex Deymof4867c42013-06-28 14:41:39 -0700318 LOG(INFO) << "Current update over cellular network setting: "
319 << (allowed ? "ENABLED" : "DISABLED");
320 }
321
Chris Sosacb7fa882013-07-25 17:02:59 -0700322 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700323 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700324 return 1;
325 }
326
Alex Deymo5fdf7762013-07-17 20:01:40 -0700327 // Change the P2P enabled setting.
328 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700329 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700330 if (!enabled && FLAGS_p2p_update != "no") {
331 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
332 << "\". Please specify \"yes\" or \"no\".";
333 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800334 if (!client_->SetP2PUpdatePermission(enabled)) {
335 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
336 return 1;
337 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700338 }
339 }
340
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800341 // Show the rollback availability.
342 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800343 string rollback_partition;
344
345 if (!client_->GetRollbackPartition(&rollback_partition)) {
346 LOG(ERROR) << "Error while querying rollback partition availabilty.";
347 return 1;
348 }
349
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700350 bool can_rollback = true;
351 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700352 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700353 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700354 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700355 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700356 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700357
358 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700359 if (!can_rollback) {
360 return 1;
361 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800362 }
363
Alex Deymo5fdf7762013-07-17 20:01:40 -0700364 // Show the current P2P enabled setting.
365 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800366 bool enabled;
367
368 if (!client_->GetP2PUpdatePermission(&enabled)) {
369 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
370 return 1;
371 }
372
Alex Deymo5fdf7762013-07-17 20:01:40 -0700373 LOG(INFO) << "Current update using P2P setting: "
374 << (enabled ? "ENABLED" : "DISABLED");
375 }
376
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700377 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800378 if (!FLAGS_channel.empty()) {
379 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
380 LOG(ERROR) << "Error setting the channel.";
381 return 1;
382 }
383
384 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
385 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700386
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700387 // Show the current and target channels if requested.
388 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800389 string current_channel;
390 string target_channel;
391
392 if (!client_->GetChannel(&current_channel)) {
393 LOG(ERROR) << "Error getting the current channel.";
394 return 1;
395 }
396
397 if (!client_->GetTargetChannel(&target_channel)) {
398 LOG(ERROR) << "Error getting the target channel.";
399 return 1;
400 }
401
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700402 LOG(INFO) << "Current Channel: " << current_channel;
403
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700404 if (!target_channel.empty())
405 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900406 }
407
Chris Sosad317e402013-06-12 13:47:09 -0700408 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800409 !FLAGS_app_version.empty() |
410 !FLAGS_omaha_url.empty();
411 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700412
Chris Sosad317e402013-06-12 13:47:09 -0700413 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700414 LOG(ERROR) << "Incompatible flags specified with rollback."
415 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700416 return 1;
417 }
418
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700419 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700420 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800421 if (!client_->Rollback(FLAGS_powerwash)) {
422 LOG(ERROR) << "Rollback request failed.";
423 return 1;
424 }
Chris Sosad317e402013-06-12 13:47:09 -0700425 }
426
Darin Petkov58529db2010-08-13 09:19:47 -0700427 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700428 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700429 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700430 string app_version = FLAGS_app_version;
431 if (FLAGS_update && app_version.empty()) {
432 app_version = "ForcedUpdate";
433 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700434 }
Darin Petkov58529db2010-08-13 09:19:47 -0700435 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800436 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
437 FLAGS_interactive)) {
438 LOG(ERROR) << "Error checking for update.";
439 return 1;
440 }
Chris Sosa192449e2013-10-28 14:16:19 -0700441 }
Darin Petkov58529db2010-08-13 09:19:47 -0700442
Chris Sosa192449e2013-10-28 14:16:19 -0700443 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800444 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
445 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
446 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700447 LOG(ERROR) << "Multiple exclusive options selected. "
448 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700449 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700450 << "or --status.";
451 return 1;
452 }
453
454 if (FLAGS_status) {
455 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800456 if (!ShowStatus()) {
457 LOG(ERROR) << "Failed to query status";
458 return 1;
459 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700460 return 0;
461 }
Darin Petkov58529db2010-08-13 09:19:47 -0700462
Chris Sosa192449e2013-10-28 14:16:19 -0700463 if (FLAGS_follow) {
464 LOG(INFO) << "Waiting for update to complete.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800465 auto handler = new UpdateWaitHandler(true);
466 handlers_.emplace_back(handler);
467 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700468 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700469 }
470
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700471 if (FLAGS_watch_for_updates) {
472 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800473 auto handler = new WatchingStatusUpdateHandler();
474 handlers_.emplace_back(handler);
475 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700476 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700477 }
Darin Petkov58529db2010-08-13 09:19:47 -0700478
Darin Petkov296889c2010-07-23 16:20:54 -0700479 if (FLAGS_reboot) {
480 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800481 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700482 return 0;
483 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700484
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700485 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800486 string prev_version;
487
488 if (!client_->GetPrevVersion(&prev_version)) {
489 LOG(ERROR) << "Error getting previous version.";
490 } else {
491 LOG(INFO) << "Previous version = " << prev_version;
492 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700493 }
494
David Zeuthen9d73a722014-04-04 14:52:46 -0700495 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800496 int ret = GetNeedReboot();
497
498 if (ret == 1) {
499 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700500 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800501
502 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700503 }
504
505 if (FLAGS_block_until_reboot_is_needed) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800506 auto handler = new UpdateWaitHandler(false);
507 handlers_.emplace_back(handler);
508 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700509 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700510 }
511
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700512 return 0;
513}
Alex Deymo72aa0022015-06-19 21:16:49 -0700514
Alex Deymo690810b2016-01-19 16:11:40 -0800515void UpdateEngineClient::ProcessFlagsAndExit() {
516 int ret = ProcessFlags();
517 if (ret != kContinueRunning)
518 QuitWithExitCode(ret);
519}
520
Alex Deymo72aa0022015-06-19 21:16:49 -0700521} // namespace
522
523int main(int argc, char** argv) {
524 UpdateEngineClient client(argc, argv);
525 return client.Run();
526}