blob: 55d7e649538256072515b745731370b810200649 [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
Alex Deymo9a069222016-03-02 16:14:42 -080032#include "update_engine/client.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080033#include "update_engine/common/error_code.h"
34#include "update_engine/common/error_code_utils.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070035#include "update_engine/omaha_utils.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080036#include "update_engine/status_update_handler.h"
37#include "update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080038#include "update_engine/update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070039
Alex Deymob3fa53b2016-04-18 19:57:58 -070040using chromeos_update_engine::EolStatus;
Shuqian Zhao29971732016-02-05 11:29:32 -080041using chromeos_update_engine::ErrorCode;
Alex Deymo9a069222016-03-02 16:14:42 -080042using chromeos_update_engine::UpdateStatusToString;
43using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070044using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080045using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080046using std::vector;
Casey Dahlin97c87052016-01-06 14:33:55 -080047using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070048
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070049namespace {
50
Alex Deymo72aa0022015-06-19 21:16:49 -070051// Constant to signal that we need to continue running the daemon after
52// initialization.
53const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070054
Casey Dahlin19441412016-01-07 14:56:40 -080055class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070056 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080057 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
Casey Dahline844c1a2015-12-16 14:30:58 -080058 }
59
Alex Deymo72aa0022015-06-19 21:16:49 -070060 ~UpdateEngineClient() override = default;
61
62 protected:
63 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080064 int ret = Daemon::OnInit();
Casey Dahlin97c87052016-01-06 14:33:55 -080065 if (ret != EX_OK) return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080066
67 client_ = update_engine::UpdateEngineClient::CreateInstance();
68
69 if (!client_) {
70 LOG(ERROR) << "UpdateEngineService not available.";
71 return 1;
72 }
73
Alex Deymo690810b2016-01-19 16:11:40 -080074 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
75 // of the ProcessFlags method after the Daemon initialization is done.
76 base::MessageLoop::current()->PostTask(
77 FROM_HERE,
78 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
79 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070080 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070081 }
Alex Deymo72aa0022015-06-19 21:16:49 -070082
83 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070084 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080085 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070086
Casey Dahlin87ab88e2015-12-16 17:58:05 -080087 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
88 // occurred, 2 if no reboot is needed.
89 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070090
Alex Deymo72aa0022015-06-19 21:16:49 -070091 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -080092 // flags. Returns the exit code of the program of kContinueRunning if it
93 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -070094 int ProcessFlags();
95
Alex Deymo690810b2016-01-19 16:11:40 -080096 // Processes the flags and exits the program accordingly.
97 void ProcessFlagsAndExit();
98
Alex Deymo72aa0022015-06-19 21:16:49 -070099 // Copy of argc and argv passed to main().
100 int argc_;
101 char** argv_;
102
Casey Dahline844c1a2015-12-16 14:30:58 -0800103 // Library-based client
104 unique_ptr<update_engine::UpdateEngineClient> client_;
105
Casey Dahlin97c87052016-01-06 14:33:55 -0800106 // Pointers to handlers for cleanup
107 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
108
Alex Deymo72aa0022015-06-19 21:16:49 -0700109 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
110};
111
Casey Dahlin97c87052016-01-06 14:33:55 -0800112class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
113 public:
114 ~ExitingStatusUpdateHandler() override = default;
115
116 void IPCError(const string& error) override;
117};
118
119void ExitingStatusUpdateHandler::IPCError(const string& error) {
120 LOG(ERROR) << error;
121 exit(1);
122}
123
124class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
125 public:
126 ~WatchingStatusUpdateHandler() override = default;
127
Alex Deymo690810b2016-01-19 16:11:40 -0800128 void HandleStatusUpdate(int64_t last_checked_time,
129 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800130 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800131 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800132 int64_t new_size) override;
133};
134
135void WatchingStatusUpdateHandler::HandleStatusUpdate(
136 int64_t last_checked_time, double progress, UpdateStatus current_operation,
137 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700138 LOG(INFO) << "Got status update:";
139 LOG(INFO) << " last_checked_time: " << last_checked_time;
140 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800141 LOG(INFO) << " current_operation: "
142 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700143 LOG(INFO) << " new_version: " << new_version;
144 LOG(INFO) << " new_size: " << new_size;
145}
146
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800147bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700148 int64_t last_checked_time = 0;
149 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800150 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700151 string new_version;
152 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700153
Casey Dahlin97c87052016-01-06 14:33:55 -0800154 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
155 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800156 return false;
157 }
158
Casey Dahlin97c87052016-01-06 14:33:55 -0800159 printf("LAST_CHECKED_TIME=%" PRIi64
160 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700161 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800162 last_checked_time, progress, UpdateStatusToString(current_op),
163 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800164
165 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700166}
167
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800168int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700169 int64_t last_checked_time = 0;
170 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800171 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700172 string new_version;
173 int64_t new_size = 0;
174
Casey Dahlin97c87052016-01-06 14:33:55 -0800175 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
176 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800177 return 1;
178 }
179
Casey Dahlin97c87052016-01-06 14:33:55 -0800180 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800181 return 0;
182 }
183
184 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700185}
186
Casey Dahlin97c87052016-01-06 14:33:55 -0800187class UpdateWaitHandler : public ExitingStatusUpdateHandler {
188 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800189 explicit UpdateWaitHandler(bool exit_on_error,
190 update_engine::UpdateEngineClient* client)
191 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800192
193 ~UpdateWaitHandler() override = default;
194
Alex Deymo690810b2016-01-19 16:11:40 -0800195 void HandleStatusUpdate(int64_t last_checked_time,
196 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800197 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800198 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800199 int64_t new_size) override;
200
201 private:
202 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800203 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800204};
205
206void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
207 double /* progress */,
208 UpdateStatus current_operation,
209 const string& /* new_version */,
210 int64_t /* new_size */) {
211 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800212 int last_attempt_error;
213 ErrorCode code = ErrorCode::kSuccess;
214 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
215 code = static_cast<ErrorCode>(last_attempt_error);
216
217 LOG(ERROR) << "Update failed, current operation is "
218 << UpdateStatusToString(current_operation)
219 << ", last error code is " << ErrorCodeToString(code) << "("
220 << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700221 exit(1);
222 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800223 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700224 LOG(INFO) << "Update succeeded -- reboot needed.";
225 exit(0);
226 }
Darin Petkov58529db2010-08-13 09:19:47 -0700227}
228
Alex Deymo72aa0022015-06-19 21:16:49 -0700229int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700230 DEFINE_string(app_version, "", "Force the current app version.");
231 DEFINE_string(channel, "",
232 "Set the target channel. The device will be powerwashed if the "
233 "target channel is more stable than the current channel unless "
234 "--nopowerwash is specified.");
235 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800236 DEFINE_bool(follow, false,
237 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700238 "Exit status is 0 if the update succeeded, and 1 otherwise.");
239 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
240 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
241 DEFINE_string(p2p_update, "",
242 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
243 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800244 DEFINE_bool(powerwash, true,
245 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700246 "do a powerwash or allow it respectively.");
247 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800248 DEFINE_bool(is_reboot_needed, false,
249 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700250 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800251 DEFINE_bool(block_until_reboot_is_needed, false,
252 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700253 "needed. Returns non-zero exit status if an error occurred.");
254 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800255 DEFINE_bool(rollback, false,
256 "Perform a rollback to the previous partition. The device will "
257 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800258 DEFINE_bool(can_rollback, false,
259 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700260 "is available.");
261 DEFINE_bool(show_channel, false, "Show the current and target channels.");
262 DEFINE_bool(show_p2p_update, false,
263 "Show the current setting for peer-to-peer update sharing.");
264 DEFINE_bool(show_update_over_cellular, false,
265 "Show the current setting for updates over cellular networks.");
266 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800267 DEFINE_bool(update, false,
268 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700269 "Implies --follow.");
270 DEFINE_string(update_over_cellular, "",
271 "Enables (\"yes\") or disables (\"no\") the updates over "
272 "cellular networks.");
273 DEFINE_bool(watch_for_updates, false,
274 "Listen for status updates and print them to the screen.");
275 DEFINE_bool(prev_version, false,
276 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800277 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Alex Deymob3fa53b2016-04-18 19:57:58 -0700278 DEFINE_bool(eol_status, false, "Show the current end-of-life status.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700279
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700280 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700281 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700282 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700283
Alex Deymo8ce80d62015-01-27 15:10:43 -0800284 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800285 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800286 base::CommandLine::ForCurrentProcess()->GetArgs();
287 if (!positional_args.empty()) {
288 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
289 << "'. If you want to pass a value to a flag, pass it as "
290 "--flag=value.";
291 return 1;
292 }
293
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700294 // Update the status if requested.
295 if (FLAGS_reset_status) {
296 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800297
298 if (client_->ResetStatus()) {
299 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
300 "run:\n"
301 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
302 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
303 } else {
304 LOG(ERROR) << "ResetStatus failed";
305 return 1;
306 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700307 }
Darin Petkov58529db2010-08-13 09:19:47 -0700308
Alex Deymof4867c42013-06-28 14:41:39 -0700309 // Changes the current update over cellular network setting.
310 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700311 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700312 if (!allowed && FLAGS_update_over_cellular != "no") {
313 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
314 << "\". Please specify \"yes\" or \"no\".";
315 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800316 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800317 LOG(ERROR) << "Error setting the update over cellular setting.";
318 return 1;
319 }
Alex Deymof4867c42013-06-28 14:41:39 -0700320 }
321 }
322
323 // Show the current update over cellular network setting.
324 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800325 bool allowed;
326
327 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
328 LOG(ERROR) << "Error getting the update over cellular setting.";
329 return 1;
330 }
331
Alex Deymof4867c42013-06-28 14:41:39 -0700332 LOG(INFO) << "Current update over cellular network setting: "
333 << (allowed ? "ENABLED" : "DISABLED");
334 }
335
Chris Sosacb7fa882013-07-25 17:02:59 -0700336 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700337 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700338 return 1;
339 }
340
Alex Deymo5fdf7762013-07-17 20:01:40 -0700341 // Change the P2P enabled setting.
342 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700343 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700344 if (!enabled && FLAGS_p2p_update != "no") {
345 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
346 << "\". Please specify \"yes\" or \"no\".";
347 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800348 if (!client_->SetP2PUpdatePermission(enabled)) {
349 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
350 return 1;
351 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700352 }
353 }
354
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800355 // Show the rollback availability.
356 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800357 string rollback_partition;
358
359 if (!client_->GetRollbackPartition(&rollback_partition)) {
360 LOG(ERROR) << "Error while querying rollback partition availabilty.";
361 return 1;
362 }
363
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700364 bool can_rollback = true;
365 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700366 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700367 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700368 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700369 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700370 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700371
372 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700373 if (!can_rollback) {
374 return 1;
375 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800376 }
377
Alex Deymo5fdf7762013-07-17 20:01:40 -0700378 // Show the current P2P enabled setting.
379 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800380 bool enabled;
381
382 if (!client_->GetP2PUpdatePermission(&enabled)) {
383 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
384 return 1;
385 }
386
Alex Deymo5fdf7762013-07-17 20:01:40 -0700387 LOG(INFO) << "Current update using P2P setting: "
388 << (enabled ? "ENABLED" : "DISABLED");
389 }
390
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700391 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800392 if (!FLAGS_channel.empty()) {
393 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
394 LOG(ERROR) << "Error setting the channel.";
395 return 1;
396 }
397
398 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
399 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700400
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700401 // Show the current and target channels if requested.
402 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800403 string current_channel;
404 string target_channel;
405
406 if (!client_->GetChannel(&current_channel)) {
407 LOG(ERROR) << "Error getting the current channel.";
408 return 1;
409 }
410
411 if (!client_->GetTargetChannel(&target_channel)) {
412 LOG(ERROR) << "Error getting the target channel.";
413 return 1;
414 }
415
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700416 LOG(INFO) << "Current Channel: " << current_channel;
417
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700418 if (!target_channel.empty())
419 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900420 }
421
Chris Sosad317e402013-06-12 13:47:09 -0700422 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800423 !FLAGS_app_version.empty() |
424 !FLAGS_omaha_url.empty();
425 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700426
Chris Sosad317e402013-06-12 13:47:09 -0700427 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700428 LOG(ERROR) << "Incompatible flags specified with rollback."
429 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700430 return 1;
431 }
432
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700433 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700434 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800435 if (!client_->Rollback(FLAGS_powerwash)) {
436 LOG(ERROR) << "Rollback request failed.";
437 return 1;
438 }
Chris Sosad317e402013-06-12 13:47:09 -0700439 }
440
Darin Petkov58529db2010-08-13 09:19:47 -0700441 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700442 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700443 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700444 string app_version = FLAGS_app_version;
445 if (FLAGS_update && app_version.empty()) {
446 app_version = "ForcedUpdate";
447 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700448 }
Darin Petkov58529db2010-08-13 09:19:47 -0700449 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800450 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
451 FLAGS_interactive)) {
452 LOG(ERROR) << "Error checking for update.";
453 return 1;
454 }
Chris Sosa192449e2013-10-28 14:16:19 -0700455 }
Darin Petkov58529db2010-08-13 09:19:47 -0700456
Chris Sosa192449e2013-10-28 14:16:19 -0700457 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800458 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
459 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
460 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700461 LOG(ERROR) << "Multiple exclusive options selected. "
462 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700463 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700464 << "or --status.";
465 return 1;
466 }
467
468 if (FLAGS_status) {
469 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800470 if (!ShowStatus()) {
471 LOG(ERROR) << "Failed to query status";
472 return 1;
473 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700474 return 0;
475 }
Darin Petkov58529db2010-08-13 09:19:47 -0700476
Chris Sosa192449e2013-10-28 14:16:19 -0700477 if (FLAGS_follow) {
478 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800479 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800480 handlers_.emplace_back(handler);
481 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700482 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700483 }
484
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700485 if (FLAGS_watch_for_updates) {
486 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800487 auto handler = new WatchingStatusUpdateHandler();
488 handlers_.emplace_back(handler);
489 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700490 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700491 }
Darin Petkov58529db2010-08-13 09:19:47 -0700492
Darin Petkov296889c2010-07-23 16:20:54 -0700493 if (FLAGS_reboot) {
494 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800495 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700496 return 0;
497 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700498
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700499 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800500 string prev_version;
501
502 if (!client_->GetPrevVersion(&prev_version)) {
503 LOG(ERROR) << "Error getting previous version.";
504 } else {
505 LOG(INFO) << "Previous version = " << prev_version;
506 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700507 }
508
David Zeuthen9d73a722014-04-04 14:52:46 -0700509 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800510 int ret = GetNeedReboot();
511
512 if (ret == 1) {
513 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700514 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800515
516 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700517 }
518
519 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800520 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800521 handlers_.emplace_back(handler);
522 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700523 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700524 }
525
Shuqian Zhao29971732016-02-05 11:29:32 -0800526 if (FLAGS_last_attempt_error) {
527 int last_attempt_error;
528 if (!client_->GetLastAttemptError(&last_attempt_error)) {
529 LOG(ERROR) << "Error getting last attempt error.";
530 } else {
531 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800532 printf(
533 "ERROR_CODE=%i\n"
534 "ERROR_MESSAGE=%s\n",
535 last_attempt_error,
536 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800537 }
Alex Deymo9a069222016-03-02 16:14:42 -0800538 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800539
Alex Deymob3fa53b2016-04-18 19:57:58 -0700540 if (FLAGS_eol_status) {
541 int eol_status;
542 if (!client_->GetEolStatus(&eol_status)) {
543 LOG(ERROR) << "Error getting the end-of-life status.";
544 } else {
545 EolStatus eol_status_code = static_cast<EolStatus>(eol_status);
546 printf("EOL_STATUS=%s\n", EolStatusToString(eol_status_code));
547 }
548 }
549
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700550 return 0;
551}
Alex Deymo72aa0022015-06-19 21:16:49 -0700552
Alex Deymo690810b2016-01-19 16:11:40 -0800553void UpdateEngineClient::ProcessFlagsAndExit() {
554 int ret = ProcessFlags();
555 if (ret != kContinueRunning)
556 QuitWithExitCode(ret);
557}
558
Alex Deymo72aa0022015-06-19 21:16:49 -0700559} // namespace
560
561int main(int argc, char** argv) {
562 UpdateEngineClient client(argc, argv);
563 return client.Run();
564}