blob: 9748c4d43f5e43e916181342429bb5229fde2325 [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
Amin Hassani1a53ba02017-07-28 15:03:14 -070025#include <base/bind.h>
26#include <base/command_line.h>
27#include <base/logging.h>
28#include <base/macros.h>
Xiaochu Liu88d90382018-08-29 16:09:11 -070029#include <base/strings/string_split.h>
Amin Hassani1a53ba02017-07-28 15:03:14 -070030#include <base/threading/platform_thread.h>
31#include <brillo/daemons/daemon.h>
32#include <brillo/flag_helper.h>
33
Alex Deymo9a069222016-03-02 16:14:42 -080034#include "update_engine/client.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080035#include "update_engine/common/error_code.h"
36#include "update_engine/common/error_code_utils.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070037#include "update_engine/omaha_utils.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080038#include "update_engine/status_update_handler.h"
39#include "update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080040#include "update_engine/update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070041
Alex Deymob3fa53b2016-04-18 19:57:58 -070042using chromeos_update_engine::EolStatus;
Shuqian Zhao29971732016-02-05 11:29:32 -080043using chromeos_update_engine::ErrorCode;
Jae Hoon Kim893cae42019-08-16 17:13:49 -070044using chromeos_update_engine::MilestonesToEol;
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070045using chromeos_update_engine::UpdateEngineStatusToString;
Alex Deymo9a069222016-03-02 16:14:42 -080046using chromeos_update_engine::UpdateStatusToString;
47using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070048using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080049using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080050using std::vector;
Amin Hassanieb463ee2019-06-20 19:23:03 -070051using update_engine::UpdateEngineStatus;
Casey Dahlin97c87052016-01-06 14:33:55 -080052using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070053
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070054namespace {
55
Alex Deymo72aa0022015-06-19 21:16:49 -070056// Constant to signal that we need to continue running the daemon after
57// initialization.
58const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070059
Amin Hassani3584bce2017-06-29 14:21:19 -070060// The ShowStatus request will be retried `kShowStatusRetryCount` times at
61// `kShowStatusRetryInterval` second intervals on failure.
62const int kShowStatusRetryCount = 30;
Amin Hassani1a53ba02017-07-28 15:03:14 -070063const int kShowStatusRetryIntervalInSeconds = 2;
Amin Hassani3584bce2017-06-29 14:21:19 -070064
Casey Dahlin19441412016-01-07 14:56:40 -080065class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070066 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080067 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
Casey Dahline844c1a2015-12-16 14:30:58 -080068
Alex Deymo72aa0022015-06-19 21:16:49 -070069 ~UpdateEngineClient() override = default;
70
71 protected:
72 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080073 int ret = Daemon::OnInit();
Amin Hassani7cc8bb02019-01-14 16:29:47 -080074 if (ret != EX_OK)
75 return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080076
77 client_ = update_engine::UpdateEngineClient::CreateInstance();
78
79 if (!client_) {
80 LOG(ERROR) << "UpdateEngineService not available.";
81 return 1;
82 }
83
Alex Deymo690810b2016-01-19 16:11:40 -080084 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
85 // of the ProcessFlags method after the Daemon initialization is done.
Eric Caruso22951672018-01-23 14:37:01 -080086 base::MessageLoop::current()->task_runner()->PostTask(
Alex Deymo690810b2016-01-19 16:11:40 -080087 FROM_HERE,
88 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
89 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070090 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070091 }
Alex Deymo72aa0022015-06-19 21:16:49 -070092
93 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070094 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080095 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070096
Casey Dahlin87ab88e2015-12-16 17:58:05 -080097 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
98 // occurred, 2 if no reboot is needed.
99 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -0700100
Alex Deymo72aa0022015-06-19 21:16:49 -0700101 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -0800102 // flags. Returns the exit code of the program of kContinueRunning if it
103 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -0700104 int ProcessFlags();
105
Alex Deymo690810b2016-01-19 16:11:40 -0800106 // Processes the flags and exits the program accordingly.
107 void ProcessFlagsAndExit();
108
Alex Deymo72aa0022015-06-19 21:16:49 -0700109 // Copy of argc and argv passed to main().
110 int argc_;
111 char** argv_;
112
Casey Dahline844c1a2015-12-16 14:30:58 -0800113 // Library-based client
114 unique_ptr<update_engine::UpdateEngineClient> client_;
115
Casey Dahlin97c87052016-01-06 14:33:55 -0800116 // Pointers to handlers for cleanup
117 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
118
Alex Deymo72aa0022015-06-19 21:16:49 -0700119 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
120};
121
Casey Dahlin97c87052016-01-06 14:33:55 -0800122class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
123 public:
124 ~ExitingStatusUpdateHandler() override = default;
125
126 void IPCError(const string& error) override;
127};
128
129void ExitingStatusUpdateHandler::IPCError(const string& error) {
130 LOG(ERROR) << error;
131 exit(1);
132}
133
134class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
135 public:
136 ~WatchingStatusUpdateHandler() override = default;
137
Amin Hassanieb463ee2019-06-20 19:23:03 -0700138 void HandleStatusUpdate(const UpdateEngineStatus& status) override;
Casey Dahlin97c87052016-01-06 14:33:55 -0800139};
140
141void WatchingStatusUpdateHandler::HandleStatusUpdate(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700142 const UpdateEngineStatus& status) {
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -0700143 LOG(INFO) << "Got status update: " << UpdateEngineStatusToString(status);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700144}
145
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800146bool UpdateEngineClient::ShowStatus() {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700147 UpdateEngineStatus status;
Amin Hassani3584bce2017-06-29 14:21:19 -0700148 int retry_count = kShowStatusRetryCount;
149 while (retry_count > 0) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700150 if (client_->GetStatus(&status)) {
Amin Hassani3584bce2017-06-29 14:21:19 -0700151 break;
152 }
153 if (--retry_count == 0) {
154 return false;
155 }
Amin Hassani1a53ba02017-07-28 15:03:14 -0700156 LOG(WARNING) << "Will try " << retry_count << " more times!";
157 base::PlatformThread::Sleep(
158 base::TimeDelta::FromSeconds(kShowStatusRetryIntervalInSeconds));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800159 }
160
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -0700161 printf("%s", UpdateEngineStatusToString(status).c_str());
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800162
163 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700164}
165
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800166int UpdateEngineClient::GetNeedReboot() {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700167 UpdateEngineStatus status;
168 if (!client_->GetStatus(&status)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800169 return 1;
170 }
171
Amin Hassanieb463ee2019-06-20 19:23:03 -0700172 if (status.status == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800173 return 0;
174 }
175
176 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700177}
178
Casey Dahlin97c87052016-01-06 14:33:55 -0800179class UpdateWaitHandler : public ExitingStatusUpdateHandler {
180 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800181 explicit UpdateWaitHandler(bool exit_on_error,
182 update_engine::UpdateEngineClient* client)
183 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800184
185 ~UpdateWaitHandler() override = default;
186
Amin Hassanieb463ee2019-06-20 19:23:03 -0700187 void HandleStatusUpdate(const UpdateEngineStatus& status) override;
Casey Dahlin97c87052016-01-06 14:33:55 -0800188
189 private:
190 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800191 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800192};
193
Amin Hassanieb463ee2019-06-20 19:23:03 -0700194void UpdateWaitHandler::HandleStatusUpdate(const UpdateEngineStatus& status) {
195 if (exit_on_error_ && status.status == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800196 int last_attempt_error;
197 ErrorCode code = ErrorCode::kSuccess;
198 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
199 code = static_cast<ErrorCode>(last_attempt_error);
200
201 LOG(ERROR) << "Update failed, current operation is "
Amin Hassanieb463ee2019-06-20 19:23:03 -0700202 << UpdateStatusToString(status.status) << ", last error code is "
203 << ErrorCodeToString(code) << "(" << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700204 exit(1);
205 }
Amin Hassanieb463ee2019-06-20 19:23:03 -0700206 if (status.status == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700207 LOG(INFO) << "Update succeeded -- reboot needed.";
208 exit(0);
209 }
Darin Petkov58529db2010-08-13 09:19:47 -0700210}
211
Alex Deymo72aa0022015-06-19 21:16:49 -0700212int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700213 DEFINE_string(app_version, "", "Force the current app version.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800214 DEFINE_string(channel,
215 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700216 "Set the target channel. The device will be powerwashed if the "
217 "target channel is more stable than the current channel unless "
218 "--nopowerwash is specified.");
219 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Alex Deymod63fab32016-10-06 15:40:49 -0700220 DEFINE_string(
221 cohort_hint, "", "Set the current cohort hint to the passed value.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800222 DEFINE_bool(follow,
223 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800224 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700225 "Exit status is 0 if the update succeeded, and 1 otherwise.");
226 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
227 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800228 DEFINE_string(p2p_update,
229 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700230 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
231 " sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800232 DEFINE_bool(powerwash,
233 true,
Casey Dahlin97c87052016-01-06 14:33:55 -0800234 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700235 "do a powerwash or allow it respectively.");
236 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800237 DEFINE_bool(is_reboot_needed,
238 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800239 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700240 "2 if reboot is not needed or 1 if an error occurred.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800241 DEFINE_bool(block_until_reboot_is_needed,
242 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800243 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700244 "needed. Returns non-zero exit status if an error occurred.");
245 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800246 DEFINE_bool(rollback,
247 false,
Alex Deymo1ac8b592015-01-26 13:22:58 -0800248 "Perform a rollback to the previous partition. The device will "
249 "be powerwashed unless --nopowerwash is specified.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800250 DEFINE_bool(can_rollback,
251 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800252 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700253 "is available.");
254 DEFINE_bool(show_channel, false, "Show the current and target channels.");
Alex Deymod63fab32016-10-06 15:40:49 -0700255 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800256 DEFINE_bool(show_p2p_update,
257 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700258 "Show the current setting for peer-to-peer update sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800259 DEFINE_bool(show_update_over_cellular,
260 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700261 "Show the current setting for updates over cellular networks.");
262 DEFINE_bool(status, false, "Print the status to stdout.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800263 DEFINE_bool(update,
264 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800265 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700266 "Implies --follow.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800267 DEFINE_string(update_over_cellular,
268 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700269 "Enables (\"yes\") or disables (\"no\") the updates over "
270 "cellular networks.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800271 DEFINE_bool(watch_for_updates,
272 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700273 "Listen for status updates and print them to the screen.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800274 DEFINE_bool(prev_version,
275 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700276 "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_);
Sen Jiang49a08972017-07-26 15:54:18 -0700282 brillo::FlagHelper::Init(argc_, argv_, "A/B 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
Alex Deymod63fab32016-10-06 15:40:49 -0700336 // Change/show the cohort hint.
337 bool set_cohort_hint =
338 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint");
339 if (set_cohort_hint) {
340 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\"";
341 if (!client_->SetCohortHint(FLAGS_cohort_hint)) {
342 LOG(ERROR) << "Error setting the cohort hint.";
343 return 1;
344 }
345 }
346
347 if (FLAGS_show_cohort_hint || set_cohort_hint) {
348 string cohort_hint;
349 if (!client_->GetCohortHint(&cohort_hint)) {
350 LOG(ERROR) << "Error getting the cohort hint.";
351 return 1;
352 }
353
354 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\"";
355 }
356
Chris Sosacb7fa882013-07-25 17:02:59 -0700357 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700358 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700359 return 1;
360 }
361
Alex Deymo5fdf7762013-07-17 20:01:40 -0700362 // Change the P2P enabled setting.
363 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700364 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700365 if (!enabled && FLAGS_p2p_update != "no") {
366 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
367 << "\". Please specify \"yes\" or \"no\".";
368 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800369 if (!client_->SetP2PUpdatePermission(enabled)) {
370 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
371 return 1;
372 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700373 }
374 }
375
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800376 // Show the rollback availability.
377 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800378 string rollback_partition;
379
380 if (!client_->GetRollbackPartition(&rollback_partition)) {
Sen Jiang771f6482018-04-04 17:59:10 -0700381 LOG(ERROR) << "Error while querying rollback partition availability.";
Casey Dahlinef361132015-12-17 13:02:37 -0800382 return 1;
383 }
384
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700385 bool can_rollback = true;
386 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700387 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700388 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700389 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700390 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700391 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700392
393 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700394 if (!can_rollback) {
395 return 1;
396 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800397 }
398
Alex Deymo5fdf7762013-07-17 20:01:40 -0700399 // Show the current P2P enabled setting.
400 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800401 bool enabled;
402
403 if (!client_->GetP2PUpdatePermission(&enabled)) {
404 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
405 return 1;
406 }
407
Alex Deymo5fdf7762013-07-17 20:01:40 -0700408 LOG(INFO) << "Current update using P2P setting: "
409 << (enabled ? "ENABLED" : "DISABLED");
410 }
411
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700412 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800413 if (!FLAGS_channel.empty()) {
414 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
415 LOG(ERROR) << "Error setting the channel.";
416 return 1;
417 }
418
419 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
420 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700421
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700422 // Show the current and target channels if requested.
423 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800424 string current_channel;
425 string target_channel;
426
427 if (!client_->GetChannel(&current_channel)) {
428 LOG(ERROR) << "Error getting the current channel.";
429 return 1;
430 }
431
432 if (!client_->GetTargetChannel(&target_channel)) {
433 LOG(ERROR) << "Error getting the target channel.";
434 return 1;
435 }
436
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700437 LOG(INFO) << "Current Channel: " << current_channel;
438
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700439 if (!target_channel.empty())
440 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900441 }
442
Amin Hassani89b9a2d2018-04-20 16:01:45 -0700443 bool do_update_request = FLAGS_check_for_update || FLAGS_update ||
444 !FLAGS_app_version.empty() ||
Casey Dahlin97c87052016-01-06 14:33:55 -0800445 !FLAGS_omaha_url.empty();
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800446 if (FLAGS_update)
447 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700448
Chris Sosad317e402013-06-12 13:47:09 -0700449 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700450 LOG(ERROR) << "Incompatible flags specified with rollback."
451 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700452 return 1;
453 }
454
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700455 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700456 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800457 if (!client_->Rollback(FLAGS_powerwash)) {
458 LOG(ERROR) << "Rollback request failed.";
459 return 1;
460 }
Chris Sosad317e402013-06-12 13:47:09 -0700461 }
462
Darin Petkov58529db2010-08-13 09:19:47 -0700463 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700464 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700465 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700466 string app_version = FLAGS_app_version;
467 if (FLAGS_update && app_version.empty()) {
468 app_version = "ForcedUpdate";
469 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700470 }
Amin Hassania7add992019-03-18 11:36:48 -0700471 LOG(INFO) << "Initiating update check.";
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800472 if (!client_->AttemptUpdate(
473 app_version, FLAGS_omaha_url, FLAGS_interactive)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800474 LOG(ERROR) << "Error checking for update.";
475 return 1;
476 }
Chris Sosa192449e2013-10-28 14:16:19 -0700477 }
Darin Petkov58529db2010-08-13 09:19:47 -0700478
Chris Sosa192449e2013-10-28 14:16:19 -0700479 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800480 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
481 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
482 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700483 LOG(ERROR) << "Multiple exclusive options selected. "
484 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700485 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700486 << "or --status.";
487 return 1;
488 }
489
490 if (FLAGS_status) {
491 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800492 if (!ShowStatus()) {
493 LOG(ERROR) << "Failed to query status";
494 return 1;
495 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700496 return 0;
497 }
Darin Petkov58529db2010-08-13 09:19:47 -0700498
Chris Sosa192449e2013-10-28 14:16:19 -0700499 if (FLAGS_follow) {
500 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800501 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800502 handlers_.emplace_back(handler);
503 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700504 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700505 }
506
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700507 if (FLAGS_watch_for_updates) {
508 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800509 auto handler = new WatchingStatusUpdateHandler();
510 handlers_.emplace_back(handler);
511 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700512 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700513 }
Darin Petkov58529db2010-08-13 09:19:47 -0700514
Darin Petkov296889c2010-07-23 16:20:54 -0700515 if (FLAGS_reboot) {
516 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800517 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700518 return 0;
519 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700520
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700521 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800522 string prev_version;
523
524 if (!client_->GetPrevVersion(&prev_version)) {
525 LOG(ERROR) << "Error getting previous version.";
526 } else {
527 LOG(INFO) << "Previous version = " << prev_version;
528 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700529 }
530
David Zeuthen9d73a722014-04-04 14:52:46 -0700531 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800532 int ret = GetNeedReboot();
533
534 if (ret == 1) {
535 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700536 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800537
538 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700539 }
540
541 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800542 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800543 handlers_.emplace_back(handler);
544 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700545 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700546 }
547
Shuqian Zhao29971732016-02-05 11:29:32 -0800548 if (FLAGS_last_attempt_error) {
549 int last_attempt_error;
550 if (!client_->GetLastAttemptError(&last_attempt_error)) {
551 LOG(ERROR) << "Error getting last attempt error.";
552 } else {
553 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800554 printf(
555 "ERROR_CODE=%i\n"
556 "ERROR_MESSAGE=%s\n",
557 last_attempt_error,
558 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800559 }
Alex Deymo9a069222016-03-02 16:14:42 -0800560 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800561
Alex Deymob3fa53b2016-04-18 19:57:58 -0700562 if (FLAGS_eol_status) {
Jae Hoon Kim893cae42019-08-16 17:13:49 -0700563 int eol_status, milestones_to_eol;
564 if (!client_->GetEolStatus(&eol_status, &milestones_to_eol)) {
565 LOG(ERROR) << "Error getting the end-of-life status and milestones to "
566 "end-of-life.";
Alex Deymob3fa53b2016-04-18 19:57:58 -0700567 } else {
568 EolStatus eol_status_code = static_cast<EolStatus>(eol_status);
Jae Hoon Kim893cae42019-08-16 17:13:49 -0700569 MilestonesToEol milestones_to_eol_code = milestones_to_eol;
570 printf(
571 "EOL_STATUS=%s\n"
572 "MILESTONES_TO_EOL=%s\n",
573 EolStatusToString(eol_status_code),
574 chromeos_update_engine::MilestonesToEolToString(
575 milestones_to_eol_code)
576 .c_str());
Alex Deymob3fa53b2016-04-18 19:57:58 -0700577 }
578 }
579
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700580 return 0;
581}
Alex Deymo72aa0022015-06-19 21:16:49 -0700582
Alex Deymo690810b2016-01-19 16:11:40 -0800583void UpdateEngineClient::ProcessFlagsAndExit() {
584 int ret = ProcessFlags();
585 if (ret != kContinueRunning)
586 QuitWithExitCode(ret);
587}
588
Alex Deymo72aa0022015-06-19 21:16:49 -0700589} // namespace
590
591int main(int argc, char** argv) {
592 UpdateEngineClient client(argc, argv);
593 return client.Run();
594}