blob: 1b680d12112585b50c1fcffdba94efc1c50413fb [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;
Alex Deymo9a069222016-03-02 16:14:42 -080044using chromeos_update_engine::UpdateStatusToString;
45using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070046using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080047using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080048using std::vector;
Amin Hassanieb463ee2019-06-20 19:23:03 -070049using update_engine::UpdateEngineStatus;
Casey Dahlin97c87052016-01-06 14:33:55 -080050using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070051
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070052namespace {
53
Alex Deymo72aa0022015-06-19 21:16:49 -070054// Constant to signal that we need to continue running the daemon after
55// initialization.
56const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070057
Amin Hassani3584bce2017-06-29 14:21:19 -070058// The ShowStatus request will be retried `kShowStatusRetryCount` times at
59// `kShowStatusRetryInterval` second intervals on failure.
60const int kShowStatusRetryCount = 30;
Amin Hassani1a53ba02017-07-28 15:03:14 -070061const int kShowStatusRetryIntervalInSeconds = 2;
Amin Hassani3584bce2017-06-29 14:21:19 -070062
Casey Dahlin19441412016-01-07 14:56:40 -080063class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070064 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080065 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
Casey Dahline844c1a2015-12-16 14:30:58 -080066
Alex Deymo72aa0022015-06-19 21:16:49 -070067 ~UpdateEngineClient() override = default;
68
69 protected:
70 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080071 int ret = Daemon::OnInit();
Amin Hassani7cc8bb02019-01-14 16:29:47 -080072 if (ret != EX_OK)
73 return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080074
75 client_ = update_engine::UpdateEngineClient::CreateInstance();
76
77 if (!client_) {
78 LOG(ERROR) << "UpdateEngineService not available.";
79 return 1;
80 }
81
Alex Deymo690810b2016-01-19 16:11:40 -080082 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
83 // of the ProcessFlags method after the Daemon initialization is done.
Eric Caruso22951672018-01-23 14:37:01 -080084 base::MessageLoop::current()->task_runner()->PostTask(
Alex Deymo690810b2016-01-19 16:11:40 -080085 FROM_HERE,
86 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
87 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070088 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070089 }
Alex Deymo72aa0022015-06-19 21:16:49 -070090
91 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070092 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080093 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070094
Casey Dahlin87ab88e2015-12-16 17:58:05 -080095 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
96 // occurred, 2 if no reboot is needed.
97 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070098
Alex Deymo72aa0022015-06-19 21:16:49 -070099 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -0800100 // flags. Returns the exit code of the program of kContinueRunning if it
101 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -0700102 int ProcessFlags();
103
Alex Deymo690810b2016-01-19 16:11:40 -0800104 // Processes the flags and exits the program accordingly.
105 void ProcessFlagsAndExit();
106
Alex Deymo72aa0022015-06-19 21:16:49 -0700107 // Copy of argc and argv passed to main().
108 int argc_;
109 char** argv_;
110
Casey Dahline844c1a2015-12-16 14:30:58 -0800111 // Library-based client
112 unique_ptr<update_engine::UpdateEngineClient> client_;
113
Casey Dahlin97c87052016-01-06 14:33:55 -0800114 // Pointers to handlers for cleanup
115 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
116
Alex Deymo72aa0022015-06-19 21:16:49 -0700117 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
118};
119
Casey Dahlin97c87052016-01-06 14:33:55 -0800120class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
121 public:
122 ~ExitingStatusUpdateHandler() override = default;
123
124 void IPCError(const string& error) override;
125};
126
127void ExitingStatusUpdateHandler::IPCError(const string& error) {
128 LOG(ERROR) << error;
129 exit(1);
130}
131
132class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
133 public:
134 ~WatchingStatusUpdateHandler() override = default;
135
Amin Hassanieb463ee2019-06-20 19:23:03 -0700136 void HandleStatusUpdate(const UpdateEngineStatus& status) override;
Casey Dahlin97c87052016-01-06 14:33:55 -0800137};
138
139void WatchingStatusUpdateHandler::HandleStatusUpdate(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700140 const UpdateEngineStatus& status) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700141 LOG(INFO) << "Got status update:";
Amin Hassanieb463ee2019-06-20 19:23:03 -0700142 LOG(INFO) << " last_checked_time: " << status.last_checked_time;
143 LOG(INFO) << " progress: " << status.progress;
144 LOG(INFO) << " current_operation: " << UpdateStatusToString(status.status);
145 LOG(INFO) << " new_version: " << status.new_version;
146 LOG(INFO) << " new_size: " << status.new_size_bytes;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700147}
148
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800149bool UpdateEngineClient::ShowStatus() {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700150 UpdateEngineStatus status;
Amin Hassani3584bce2017-06-29 14:21:19 -0700151 int retry_count = kShowStatusRetryCount;
152 while (retry_count > 0) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700153 if (client_->GetStatus(&status)) {
Amin Hassani3584bce2017-06-29 14:21:19 -0700154 break;
155 }
156 if (--retry_count == 0) {
157 return false;
158 }
Amin Hassani1a53ba02017-07-28 15:03:14 -0700159 LOG(WARNING) << "Will try " << retry_count << " more times!";
160 base::PlatformThread::Sleep(
161 base::TimeDelta::FromSeconds(kShowStatusRetryIntervalInSeconds));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800162 }
163
Casey Dahlin97c87052016-01-06 14:33:55 -0800164 printf("LAST_CHECKED_TIME=%" PRIi64
165 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700166 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Amin Hassanieb463ee2019-06-20 19:23:03 -0700167 status.last_checked_time,
168 status.progress,
169 UpdateStatusToString(status.status),
170 status.new_version.c_str(),
171 status.new_size_bytes);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800172
173 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700174}
175
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800176int UpdateEngineClient::GetNeedReboot() {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700177 UpdateEngineStatus status;
178 if (!client_->GetStatus(&status)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800179 return 1;
180 }
181
Amin Hassanieb463ee2019-06-20 19:23:03 -0700182 if (status.status == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800183 return 0;
184 }
185
186 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700187}
188
Casey Dahlin97c87052016-01-06 14:33:55 -0800189class UpdateWaitHandler : public ExitingStatusUpdateHandler {
190 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800191 explicit UpdateWaitHandler(bool exit_on_error,
192 update_engine::UpdateEngineClient* client)
193 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800194
195 ~UpdateWaitHandler() override = default;
196
Amin Hassanieb463ee2019-06-20 19:23:03 -0700197 void HandleStatusUpdate(const UpdateEngineStatus& status) override;
Casey Dahlin97c87052016-01-06 14:33:55 -0800198
199 private:
200 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800201 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800202};
203
Amin Hassanieb463ee2019-06-20 19:23:03 -0700204void UpdateWaitHandler::HandleStatusUpdate(const UpdateEngineStatus& status) {
205 if (exit_on_error_ && status.status == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800206 int last_attempt_error;
207 ErrorCode code = ErrorCode::kSuccess;
208 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
209 code = static_cast<ErrorCode>(last_attempt_error);
210
211 LOG(ERROR) << "Update failed, current operation is "
Amin Hassanieb463ee2019-06-20 19:23:03 -0700212 << UpdateStatusToString(status.status) << ", last error code is "
213 << ErrorCodeToString(code) << "(" << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700214 exit(1);
215 }
Amin Hassanieb463ee2019-06-20 19:23:03 -0700216 if (status.status == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700217 LOG(INFO) << "Update succeeded -- reboot needed.";
218 exit(0);
219 }
Darin Petkov58529db2010-08-13 09:19:47 -0700220}
221
Alex Deymo72aa0022015-06-19 21:16:49 -0700222int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700223 DEFINE_string(app_version, "", "Force the current app version.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800224 DEFINE_string(channel,
225 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700226 "Set the target channel. The device will be powerwashed if the "
227 "target channel is more stable than the current channel unless "
228 "--nopowerwash is specified.");
229 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Alex Deymod63fab32016-10-06 15:40:49 -0700230 DEFINE_string(
231 cohort_hint, "", "Set the current cohort hint to the passed value.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800232 DEFINE_bool(follow,
233 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800234 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700235 "Exit status is 0 if the update succeeded, and 1 otherwise.");
236 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
237 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800238 DEFINE_string(p2p_update,
239 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700240 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
241 " sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800242 DEFINE_bool(powerwash,
243 true,
Casey Dahlin97c87052016-01-06 14:33:55 -0800244 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700245 "do a powerwash or allow it respectively.");
246 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800247 DEFINE_bool(is_reboot_needed,
248 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800249 "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.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800251 DEFINE_bool(block_until_reboot_is_needed,
252 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800253 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700254 "needed. Returns non-zero exit status if an error occurred.");
255 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800256 DEFINE_bool(rollback,
257 false,
Alex Deymo1ac8b592015-01-26 13:22:58 -0800258 "Perform a rollback to the previous partition. The device will "
259 "be powerwashed unless --nopowerwash is specified.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800260 DEFINE_bool(can_rollback,
261 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800262 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700263 "is available.");
264 DEFINE_bool(show_channel, false, "Show the current and target channels.");
Alex Deymod63fab32016-10-06 15:40:49 -0700265 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800266 DEFINE_bool(show_p2p_update,
267 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700268 "Show the current setting for peer-to-peer update sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800269 DEFINE_bool(show_update_over_cellular,
270 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700271 "Show the current setting for updates over cellular networks.");
272 DEFINE_bool(status, false, "Print the status to stdout.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800273 DEFINE_bool(update,
274 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800275 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700276 "Implies --follow.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800277 DEFINE_string(update_over_cellular,
278 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700279 "Enables (\"yes\") or disables (\"no\") the updates over "
280 "cellular networks.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800281 DEFINE_bool(watch_for_updates,
282 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700283 "Listen for status updates and print them to the screen.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800284 DEFINE_bool(prev_version,
285 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700286 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800287 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Alex Deymob3fa53b2016-04-18 19:57:58 -0700288 DEFINE_bool(eol_status, false, "Show the current end-of-life status.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700289
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700290 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700291 base::CommandLine::Init(argc_, argv_);
Sen Jiang49a08972017-07-26 15:54:18 -0700292 brillo::FlagHelper::Init(argc_, argv_, "A/B Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700293
Alex Deymo8ce80d62015-01-27 15:10:43 -0800294 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800295 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800296 base::CommandLine::ForCurrentProcess()->GetArgs();
297 if (!positional_args.empty()) {
298 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
299 << "'. If you want to pass a value to a flag, pass it as "
300 "--flag=value.";
301 return 1;
302 }
303
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700304 // Update the status if requested.
305 if (FLAGS_reset_status) {
306 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800307
308 if (client_->ResetStatus()) {
309 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
310 "run:\n"
311 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
312 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
313 } else {
314 LOG(ERROR) << "ResetStatus failed";
315 return 1;
316 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700317 }
Darin Petkov58529db2010-08-13 09:19:47 -0700318
Alex Deymof4867c42013-06-28 14:41:39 -0700319 // Changes the current update over cellular network setting.
320 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700321 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700322 if (!allowed && FLAGS_update_over_cellular != "no") {
323 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
324 << "\". Please specify \"yes\" or \"no\".";
325 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800326 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800327 LOG(ERROR) << "Error setting the update over cellular setting.";
328 return 1;
329 }
Alex Deymof4867c42013-06-28 14:41:39 -0700330 }
331 }
332
333 // Show the current update over cellular network setting.
334 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800335 bool allowed;
336
337 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
338 LOG(ERROR) << "Error getting the update over cellular setting.";
339 return 1;
340 }
341
Alex Deymof4867c42013-06-28 14:41:39 -0700342 LOG(INFO) << "Current update over cellular network setting: "
343 << (allowed ? "ENABLED" : "DISABLED");
344 }
345
Alex Deymod63fab32016-10-06 15:40:49 -0700346 // Change/show the cohort hint.
347 bool set_cohort_hint =
348 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint");
349 if (set_cohort_hint) {
350 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\"";
351 if (!client_->SetCohortHint(FLAGS_cohort_hint)) {
352 LOG(ERROR) << "Error setting the cohort hint.";
353 return 1;
354 }
355 }
356
357 if (FLAGS_show_cohort_hint || set_cohort_hint) {
358 string cohort_hint;
359 if (!client_->GetCohortHint(&cohort_hint)) {
360 LOG(ERROR) << "Error getting the cohort hint.";
361 return 1;
362 }
363
364 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\"";
365 }
366
Chris Sosacb7fa882013-07-25 17:02:59 -0700367 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700368 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700369 return 1;
370 }
371
Alex Deymo5fdf7762013-07-17 20:01:40 -0700372 // Change the P2P enabled setting.
373 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700374 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700375 if (!enabled && FLAGS_p2p_update != "no") {
376 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
377 << "\". Please specify \"yes\" or \"no\".";
378 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800379 if (!client_->SetP2PUpdatePermission(enabled)) {
380 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
381 return 1;
382 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700383 }
384 }
385
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800386 // Show the rollback availability.
387 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800388 string rollback_partition;
389
390 if (!client_->GetRollbackPartition(&rollback_partition)) {
Sen Jiang771f6482018-04-04 17:59:10 -0700391 LOG(ERROR) << "Error while querying rollback partition availability.";
Casey Dahlinef361132015-12-17 13:02:37 -0800392 return 1;
393 }
394
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700395 bool can_rollback = true;
396 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700397 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700398 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700399 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700400 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700401 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700402
403 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700404 if (!can_rollback) {
405 return 1;
406 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800407 }
408
Alex Deymo5fdf7762013-07-17 20:01:40 -0700409 // Show the current P2P enabled setting.
410 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800411 bool enabled;
412
413 if (!client_->GetP2PUpdatePermission(&enabled)) {
414 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
415 return 1;
416 }
417
Alex Deymo5fdf7762013-07-17 20:01:40 -0700418 LOG(INFO) << "Current update using P2P setting: "
419 << (enabled ? "ENABLED" : "DISABLED");
420 }
421
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700422 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800423 if (!FLAGS_channel.empty()) {
424 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
425 LOG(ERROR) << "Error setting the channel.";
426 return 1;
427 }
428
429 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
430 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700431
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700432 // Show the current and target channels if requested.
433 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800434 string current_channel;
435 string target_channel;
436
437 if (!client_->GetChannel(&current_channel)) {
438 LOG(ERROR) << "Error getting the current channel.";
439 return 1;
440 }
441
442 if (!client_->GetTargetChannel(&target_channel)) {
443 LOG(ERROR) << "Error getting the target channel.";
444 return 1;
445 }
446
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700447 LOG(INFO) << "Current Channel: " << current_channel;
448
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700449 if (!target_channel.empty())
450 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900451 }
452
Amin Hassani89b9a2d2018-04-20 16:01:45 -0700453 bool do_update_request = FLAGS_check_for_update || FLAGS_update ||
454 !FLAGS_app_version.empty() ||
Casey Dahlin97c87052016-01-06 14:33:55 -0800455 !FLAGS_omaha_url.empty();
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800456 if (FLAGS_update)
457 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700458
Chris Sosad317e402013-06-12 13:47:09 -0700459 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700460 LOG(ERROR) << "Incompatible flags specified with rollback."
461 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700462 return 1;
463 }
464
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700465 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700466 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800467 if (!client_->Rollback(FLAGS_powerwash)) {
468 LOG(ERROR) << "Rollback request failed.";
469 return 1;
470 }
Chris Sosad317e402013-06-12 13:47:09 -0700471 }
472
Darin Petkov58529db2010-08-13 09:19:47 -0700473 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700474 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700475 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700476 string app_version = FLAGS_app_version;
477 if (FLAGS_update && app_version.empty()) {
478 app_version = "ForcedUpdate";
479 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700480 }
Amin Hassania7add992019-03-18 11:36:48 -0700481 LOG(INFO) << "Initiating update check.";
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800482 if (!client_->AttemptUpdate(
483 app_version, FLAGS_omaha_url, FLAGS_interactive)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800484 LOG(ERROR) << "Error checking for update.";
485 return 1;
486 }
Chris Sosa192449e2013-10-28 14:16:19 -0700487 }
Darin Petkov58529db2010-08-13 09:19:47 -0700488
Chris Sosa192449e2013-10-28 14:16:19 -0700489 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800490 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
491 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
492 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700493 LOG(ERROR) << "Multiple exclusive options selected. "
494 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700495 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700496 << "or --status.";
497 return 1;
498 }
499
500 if (FLAGS_status) {
501 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800502 if (!ShowStatus()) {
503 LOG(ERROR) << "Failed to query status";
504 return 1;
505 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700506 return 0;
507 }
Darin Petkov58529db2010-08-13 09:19:47 -0700508
Chris Sosa192449e2013-10-28 14:16:19 -0700509 if (FLAGS_follow) {
510 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800511 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800512 handlers_.emplace_back(handler);
513 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700514 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700515 }
516
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700517 if (FLAGS_watch_for_updates) {
518 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800519 auto handler = new WatchingStatusUpdateHandler();
520 handlers_.emplace_back(handler);
521 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700522 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700523 }
Darin Petkov58529db2010-08-13 09:19:47 -0700524
Darin Petkov296889c2010-07-23 16:20:54 -0700525 if (FLAGS_reboot) {
526 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800527 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700528 return 0;
529 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700530
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700531 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800532 string prev_version;
533
534 if (!client_->GetPrevVersion(&prev_version)) {
535 LOG(ERROR) << "Error getting previous version.";
536 } else {
537 LOG(INFO) << "Previous version = " << prev_version;
538 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700539 }
540
David Zeuthen9d73a722014-04-04 14:52:46 -0700541 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800542 int ret = GetNeedReboot();
543
544 if (ret == 1) {
545 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700546 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800547
548 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700549 }
550
551 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800552 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800553 handlers_.emplace_back(handler);
554 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700555 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700556 }
557
Shuqian Zhao29971732016-02-05 11:29:32 -0800558 if (FLAGS_last_attempt_error) {
559 int last_attempt_error;
560 if (!client_->GetLastAttemptError(&last_attempt_error)) {
561 LOG(ERROR) << "Error getting last attempt error.";
562 } else {
563 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800564 printf(
565 "ERROR_CODE=%i\n"
566 "ERROR_MESSAGE=%s\n",
567 last_attempt_error,
568 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800569 }
Alex Deymo9a069222016-03-02 16:14:42 -0800570 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800571
Alex Deymob3fa53b2016-04-18 19:57:58 -0700572 if (FLAGS_eol_status) {
573 int eol_status;
574 if (!client_->GetEolStatus(&eol_status)) {
575 LOG(ERROR) << "Error getting the end-of-life status.";
576 } else {
577 EolStatus eol_status_code = static_cast<EolStatus>(eol_status);
578 printf("EOL_STATUS=%s\n", EolStatusToString(eol_status_code));
579 }
580 }
581
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700582 return 0;
583}
Alex Deymo72aa0022015-06-19 21:16:49 -0700584
Alex Deymo690810b2016-01-19 16:11:40 -0800585void UpdateEngineClient::ProcessFlagsAndExit() {
586 int ret = ProcessFlags();
587 if (ret != kContinueRunning)
588 QuitWithExitCode(ret);
589}
590
Alex Deymo72aa0022015-06-19 21:16:49 -0700591} // namespace
592
593int main(int argc, char** argv) {
594 UpdateEngineClient client(argc, argv);
595 return client.Run();
596}