blob: 33425924a7999ad02221293bf7772d5fd3d03cf6 [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>
Jae Hoon Kimc14801d2019-09-03 13:13:59 -070029#include <base/strings/string_number_conversions.h>
Xiaochu Liu88d90382018-08-29 16:09:11 -070030#include <base/strings/string_split.h>
Amin Hassani1a53ba02017-07-28 15:03:14 -070031#include <base/threading/platform_thread.h>
32#include <brillo/daemons/daemon.h>
33#include <brillo/flag_helper.h>
Jae Hoon Kimc14801d2019-09-03 13:13:59 -070034#include <brillo/key_value_store.h>
Amin Hassani1a53ba02017-07-28 15:03:14 -070035
Alex Deymo9a069222016-03-02 16:14:42 -080036#include "update_engine/client.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080037#include "update_engine/common/error_code.h"
38#include "update_engine/common/error_code_utils.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070039#include "update_engine/omaha_utils.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080040#include "update_engine/status_update_handler.h"
41#include "update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080042#include "update_engine/update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070043
Jae Hoon Kimc14801d2019-09-03 13:13:59 -070044using brillo::KeyValueStore;
Jae Hoon Kim051627a2019-09-03 12:56:32 -070045using chromeos_update_engine::EolDate;
46using chromeos_update_engine::EolDateToString;
Shuqian Zhao29971732016-02-05 11:29:32 -080047using chromeos_update_engine::ErrorCode;
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070048using chromeos_update_engine::UpdateEngineStatusToString;
Alex Deymo9a069222016-03-02 16:14:42 -080049using chromeos_update_engine::UpdateStatusToString;
50using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070051using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080052using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080053using std::vector;
Amin Hassanieb463ee2019-06-20 19:23:03 -070054using update_engine::UpdateEngineStatus;
Casey Dahlin97c87052016-01-06 14:33:55 -080055using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070056
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070057namespace {
58
Alex Deymo72aa0022015-06-19 21:16:49 -070059// Constant to signal that we need to continue running the daemon after
60// initialization.
61const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070062
Amin Hassani3584bce2017-06-29 14:21:19 -070063// The ShowStatus request will be retried `kShowStatusRetryCount` times at
64// `kShowStatusRetryInterval` second intervals on failure.
65const int kShowStatusRetryCount = 30;
Amin Hassani1a53ba02017-07-28 15:03:14 -070066const int kShowStatusRetryIntervalInSeconds = 2;
Amin Hassani3584bce2017-06-29 14:21:19 -070067
Casey Dahlin19441412016-01-07 14:56:40 -080068class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070069 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080070 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
Casey Dahline844c1a2015-12-16 14:30:58 -080071
Alex Deymo72aa0022015-06-19 21:16:49 -070072 ~UpdateEngineClient() override = default;
73
74 protected:
75 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080076 int ret = Daemon::OnInit();
Amin Hassani7cc8bb02019-01-14 16:29:47 -080077 if (ret != EX_OK)
78 return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080079
80 client_ = update_engine::UpdateEngineClient::CreateInstance();
81
82 if (!client_) {
83 LOG(ERROR) << "UpdateEngineService not available.";
84 return 1;
85 }
86
Alex Deymo690810b2016-01-19 16:11:40 -080087 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
88 // of the ProcessFlags method after the Daemon initialization is done.
Eric Caruso22951672018-01-23 14:37:01 -080089 base::MessageLoop::current()->task_runner()->PostTask(
Alex Deymo690810b2016-01-19 16:11:40 -080090 FROM_HERE,
91 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
92 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070093 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070094 }
Alex Deymo72aa0022015-06-19 21:16:49 -070095
96 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070097 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080098 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070099
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800100 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
101 // occurred, 2 if no reboot is needed.
102 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -0700103
Alex Deymo72aa0022015-06-19 21:16:49 -0700104 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -0800105 // flags. Returns the exit code of the program of kContinueRunning if it
106 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -0700107 int ProcessFlags();
108
Alex Deymo690810b2016-01-19 16:11:40 -0800109 // Processes the flags and exits the program accordingly.
110 void ProcessFlagsAndExit();
111
Alex Deymo72aa0022015-06-19 21:16:49 -0700112 // Copy of argc and argv passed to main().
113 int argc_;
114 char** argv_;
115
Casey Dahline844c1a2015-12-16 14:30:58 -0800116 // Library-based client
117 unique_ptr<update_engine::UpdateEngineClient> client_;
118
Casey Dahlin97c87052016-01-06 14:33:55 -0800119 // Pointers to handlers for cleanup
120 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
121
Alex Deymo72aa0022015-06-19 21:16:49 -0700122 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
123};
124
Casey Dahlin97c87052016-01-06 14:33:55 -0800125class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
126 public:
127 ~ExitingStatusUpdateHandler() override = default;
128
129 void IPCError(const string& error) override;
130};
131
132void ExitingStatusUpdateHandler::IPCError(const string& error) {
133 LOG(ERROR) << error;
134 exit(1);
135}
136
137class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
138 public:
139 ~WatchingStatusUpdateHandler() override = default;
140
Amin Hassanieb463ee2019-06-20 19:23:03 -0700141 void HandleStatusUpdate(const UpdateEngineStatus& status) override;
Casey Dahlin97c87052016-01-06 14:33:55 -0800142};
143
144void WatchingStatusUpdateHandler::HandleStatusUpdate(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700145 const UpdateEngineStatus& status) {
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -0700146 LOG(INFO) << "Got status update: " << UpdateEngineStatusToString(status);
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 Hassani766135a2019-12-13 11:29:32 -0800159 LOG(WARNING)
160 << "Failed to get the update_engine status. This can happen when the"
161 " update_engine is busy doing a heavy operation or if the"
162 " update-engine service is down. If it doesn't resolve, a restart of"
163 " the update-engine service is needed."
164 " Will try "
165 << retry_count << " more times!";
Amin Hassani1a53ba02017-07-28 15:03:14 -0700166 base::PlatformThread::Sleep(
167 base::TimeDelta::FromSeconds(kShowStatusRetryIntervalInSeconds));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800168 }
169
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -0700170 printf("%s", UpdateEngineStatusToString(status).c_str());
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800171
172 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700173}
174
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800175int UpdateEngineClient::GetNeedReboot() {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700176 UpdateEngineStatus status;
177 if (!client_->GetStatus(&status)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800178 return 1;
179 }
180
Amin Hassanieb463ee2019-06-20 19:23:03 -0700181 if (status.status == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800182 return 0;
183 }
184
185 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700186}
187
Casey Dahlin97c87052016-01-06 14:33:55 -0800188class UpdateWaitHandler : public ExitingStatusUpdateHandler {
189 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800190 explicit UpdateWaitHandler(bool exit_on_error,
191 update_engine::UpdateEngineClient* client)
192 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800193
194 ~UpdateWaitHandler() override = default;
195
Amin Hassanieb463ee2019-06-20 19:23:03 -0700196 void HandleStatusUpdate(const UpdateEngineStatus& status) override;
Casey Dahlin97c87052016-01-06 14:33:55 -0800197
198 private:
199 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800200 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800201};
202
Amin Hassanieb463ee2019-06-20 19:23:03 -0700203void UpdateWaitHandler::HandleStatusUpdate(const UpdateEngineStatus& status) {
204 if (exit_on_error_ && status.status == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800205 int last_attempt_error;
206 ErrorCode code = ErrorCode::kSuccess;
207 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
208 code = static_cast<ErrorCode>(last_attempt_error);
209
210 LOG(ERROR) << "Update failed, current operation is "
Amin Hassanieb463ee2019-06-20 19:23:03 -0700211 << UpdateStatusToString(status.status) << ", last error code is "
212 << ErrorCodeToString(code) << "(" << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700213 exit(1);
214 }
Amin Hassanieb463ee2019-06-20 19:23:03 -0700215 if (status.status == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700216 LOG(INFO) << "Update succeeded -- reboot needed.";
217 exit(0);
218 }
Darin Petkov58529db2010-08-13 09:19:47 -0700219}
220
Alex Deymo72aa0022015-06-19 21:16:49 -0700221int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700222 DEFINE_string(app_version, "", "Force the current app version.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800223 DEFINE_string(channel,
224 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700225 "Set the target channel. The device will be powerwashed if the "
226 "target channel is more stable than the current channel unless "
227 "--nopowerwash is specified.");
228 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Alex Deymod63fab32016-10-06 15:40:49 -0700229 DEFINE_string(
230 cohort_hint, "", "Set the current cohort hint to the passed value.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800231 DEFINE_bool(follow,
232 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800233 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700234 "Exit status is 0 if the update succeeded, and 1 otherwise.");
235 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
236 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800237 DEFINE_string(p2p_update,
238 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700239 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
240 " sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800241 DEFINE_bool(powerwash,
242 true,
Casey Dahlin97c87052016-01-06 14:33:55 -0800243 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700244 "do a powerwash or allow it respectively.");
245 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800246 DEFINE_bool(is_reboot_needed,
247 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800248 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700249 "2 if reboot is not needed or 1 if an error occurred.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800250 DEFINE_bool(block_until_reboot_is_needed,
251 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800252 "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.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800255 DEFINE_bool(rollback,
256 false,
Alex Deymo1ac8b592015-01-26 13:22:58 -0800257 "Perform a rollback to the previous partition. The device will "
258 "be powerwashed unless --nopowerwash is specified.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800259 DEFINE_bool(can_rollback,
260 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800261 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700262 "is available.");
263 DEFINE_bool(show_channel, false, "Show the current and target channels.");
Alex Deymod63fab32016-10-06 15:40:49 -0700264 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800265 DEFINE_bool(show_p2p_update,
266 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700267 "Show the current setting for peer-to-peer update sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800268 DEFINE_bool(show_update_over_cellular,
269 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700270 "Show the current setting for updates over cellular networks.");
271 DEFINE_bool(status, false, "Print the status to stdout.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800272 DEFINE_bool(update,
273 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800274 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700275 "Implies --follow.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800276 DEFINE_string(update_over_cellular,
277 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700278 "Enables (\"yes\") or disables (\"no\") the updates over "
279 "cellular networks.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800280 DEFINE_bool(watch_for_updates,
281 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700282 "Listen for status updates and print them to the screen.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800283 DEFINE_bool(prev_version,
284 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700285 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800286 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Alex Deymob3fa53b2016-04-18 19:57:58 -0700287 DEFINE_bool(eol_status, false, "Show the current end-of-life status.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700288
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700289 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700290 base::CommandLine::Init(argc_, argv_);
Sen Jiang49a08972017-07-26 15:54:18 -0700291 brillo::FlagHelper::Init(argc_, argv_, "A/B Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700292
Alex Deymo8ce80d62015-01-27 15:10:43 -0800293 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800294 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800295 base::CommandLine::ForCurrentProcess()->GetArgs();
296 if (!positional_args.empty()) {
297 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
298 << "'. If you want to pass a value to a flag, pass it as "
299 "--flag=value.";
300 return 1;
301 }
302
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700303 // Update the status if requested.
304 if (FLAGS_reset_status) {
305 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800306
307 if (client_->ResetStatus()) {
308 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
309 "run:\n"
310 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
311 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
312 } else {
313 LOG(ERROR) << "ResetStatus failed";
314 return 1;
315 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700316 }
Darin Petkov58529db2010-08-13 09:19:47 -0700317
Alex Deymof4867c42013-06-28 14:41:39 -0700318 // Changes the current update over cellular network setting.
319 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700320 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700321 if (!allowed && FLAGS_update_over_cellular != "no") {
322 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
323 << "\". Please specify \"yes\" or \"no\".";
324 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800325 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800326 LOG(ERROR) << "Error setting the update over cellular setting.";
327 return 1;
328 }
Alex Deymof4867c42013-06-28 14:41:39 -0700329 }
330 }
331
332 // Show the current update over cellular network setting.
333 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800334 bool allowed;
335
336 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
337 LOG(ERROR) << "Error getting the update over cellular setting.";
338 return 1;
339 }
340
Alex Deymof4867c42013-06-28 14:41:39 -0700341 LOG(INFO) << "Current update over cellular network setting: "
342 << (allowed ? "ENABLED" : "DISABLED");
343 }
344
Alex Deymod63fab32016-10-06 15:40:49 -0700345 // Change/show the cohort hint.
346 bool set_cohort_hint =
347 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint");
348 if (set_cohort_hint) {
349 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\"";
350 if (!client_->SetCohortHint(FLAGS_cohort_hint)) {
351 LOG(ERROR) << "Error setting the cohort hint.";
352 return 1;
353 }
354 }
355
356 if (FLAGS_show_cohort_hint || set_cohort_hint) {
357 string cohort_hint;
358 if (!client_->GetCohortHint(&cohort_hint)) {
359 LOG(ERROR) << "Error getting the cohort hint.";
360 return 1;
361 }
362
363 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\"";
364 }
365
Chris Sosacb7fa882013-07-25 17:02:59 -0700366 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700367 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700368 return 1;
369 }
370
Alex Deymo5fdf7762013-07-17 20:01:40 -0700371 // Change the P2P enabled setting.
372 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700373 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700374 if (!enabled && FLAGS_p2p_update != "no") {
375 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
376 << "\". Please specify \"yes\" or \"no\".";
377 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800378 if (!client_->SetP2PUpdatePermission(enabled)) {
379 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
380 return 1;
381 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700382 }
383 }
384
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800385 // Show the rollback availability.
386 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800387 string rollback_partition;
388
389 if (!client_->GetRollbackPartition(&rollback_partition)) {
Sen Jiang771f6482018-04-04 17:59:10 -0700390 LOG(ERROR) << "Error while querying rollback partition availability.";
Casey Dahlinef361132015-12-17 13:02:37 -0800391 return 1;
392 }
393
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700394 bool can_rollback = true;
395 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700396 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700397 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700398 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700399 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700400 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700401
402 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700403 if (!can_rollback) {
404 return 1;
405 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800406 }
407
Alex Deymo5fdf7762013-07-17 20:01:40 -0700408 // Show the current P2P enabled setting.
409 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800410 bool enabled;
411
412 if (!client_->GetP2PUpdatePermission(&enabled)) {
413 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
414 return 1;
415 }
416
Alex Deymo5fdf7762013-07-17 20:01:40 -0700417 LOG(INFO) << "Current update using P2P setting: "
418 << (enabled ? "ENABLED" : "DISABLED");
419 }
420
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700421 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800422 if (!FLAGS_channel.empty()) {
423 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
424 LOG(ERROR) << "Error setting the channel.";
425 return 1;
426 }
427
428 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
429 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700430
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700431 // Show the current and target channels if requested.
432 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800433 string current_channel;
434 string target_channel;
435
436 if (!client_->GetChannel(&current_channel)) {
437 LOG(ERROR) << "Error getting the current channel.";
438 return 1;
439 }
440
441 if (!client_->GetTargetChannel(&target_channel)) {
442 LOG(ERROR) << "Error getting the target channel.";
443 return 1;
444 }
445
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700446 LOG(INFO) << "Current Channel: " << current_channel;
447
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700448 if (!target_channel.empty())
449 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900450 }
451
Amin Hassani89b9a2d2018-04-20 16:01:45 -0700452 bool do_update_request = FLAGS_check_for_update || FLAGS_update ||
453 !FLAGS_app_version.empty() ||
Casey Dahlin97c87052016-01-06 14:33:55 -0800454 !FLAGS_omaha_url.empty();
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800455 if (FLAGS_update)
456 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700457
Chris Sosad317e402013-06-12 13:47:09 -0700458 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700459 LOG(ERROR) << "Incompatible flags specified with rollback."
460 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700461 return 1;
462 }
463
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700464 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700465 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800466 if (!client_->Rollback(FLAGS_powerwash)) {
467 LOG(ERROR) << "Rollback request failed.";
468 return 1;
469 }
Chris Sosad317e402013-06-12 13:47:09 -0700470 }
471
Darin Petkov58529db2010-08-13 09:19:47 -0700472 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700473 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700474 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700475 string app_version = FLAGS_app_version;
476 if (FLAGS_update && app_version.empty()) {
477 app_version = "ForcedUpdate";
478 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700479 }
Amin Hassania7add992019-03-18 11:36:48 -0700480 LOG(INFO) << "Initiating update check.";
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800481 if (!client_->AttemptUpdate(
482 app_version, FLAGS_omaha_url, FLAGS_interactive)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800483 LOG(ERROR) << "Error checking for update.";
484 return 1;
485 }
Chris Sosa192449e2013-10-28 14:16:19 -0700486 }
Darin Petkov58529db2010-08-13 09:19:47 -0700487
Chris Sosa192449e2013-10-28 14:16:19 -0700488 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800489 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
490 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
491 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700492 LOG(ERROR) << "Multiple exclusive options selected. "
493 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700494 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700495 << "or --status.";
496 return 1;
497 }
498
499 if (FLAGS_status) {
500 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800501 if (!ShowStatus()) {
502 LOG(ERROR) << "Failed to query status";
503 return 1;
504 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700505 return 0;
506 }
Darin Petkov58529db2010-08-13 09:19:47 -0700507
Chris Sosa192449e2013-10-28 14:16:19 -0700508 if (FLAGS_follow) {
509 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800510 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800511 handlers_.emplace_back(handler);
512 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700513 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700514 }
515
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700516 if (FLAGS_watch_for_updates) {
517 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800518 auto handler = new WatchingStatusUpdateHandler();
519 handlers_.emplace_back(handler);
520 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700521 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700522 }
Darin Petkov58529db2010-08-13 09:19:47 -0700523
Darin Petkov296889c2010-07-23 16:20:54 -0700524 if (FLAGS_reboot) {
525 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800526 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700527 return 0;
528 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700529
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700530 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800531 string prev_version;
532
533 if (!client_->GetPrevVersion(&prev_version)) {
534 LOG(ERROR) << "Error getting previous version.";
535 } else {
536 LOG(INFO) << "Previous version = " << prev_version;
537 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700538 }
539
David Zeuthen9d73a722014-04-04 14:52:46 -0700540 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800541 int ret = GetNeedReboot();
542
543 if (ret == 1) {
544 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700545 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800546
547 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700548 }
549
550 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800551 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800552 handlers_.emplace_back(handler);
553 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700554 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700555 }
556
Shuqian Zhao29971732016-02-05 11:29:32 -0800557 if (FLAGS_last_attempt_error) {
558 int last_attempt_error;
559 if (!client_->GetLastAttemptError(&last_attempt_error)) {
560 LOG(ERROR) << "Error getting last attempt error.";
561 } else {
562 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Jae Hoon Kimc14801d2019-09-03 13:13:59 -0700563
564 KeyValueStore last_attempt_error_store;
565#if BASE_VER < 576279
566 last_attempt_error_store.SetString(
567 "ERROR_CODE", base::Int64ToString(last_attempt_error));
568#else
569 last_attempt_error_store.SetString(
570 "ERROR_CODE", base::NumberToString(last_attempt_error));
571#endif
572 last_attempt_error_store.SetString("ERROR_MESSAGE",
573 ErrorCodeToString(code));
574 printf("%s", last_attempt_error_store.SaveToString().c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800575 }
Alex Deymo9a069222016-03-02 16:14:42 -0800576 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800577
Alex Deymob3fa53b2016-04-18 19:57:58 -0700578 if (FLAGS_eol_status) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -0700579 UpdateEngineStatus status;
580 if (!client_->GetStatus(&status)) {
581 LOG(ERROR) << "Error GetStatus() for getting EOL info.";
Alex Deymob3fa53b2016-04-18 19:57:58 -0700582 } else {
Jae Hoon Kim051627a2019-09-03 12:56:32 -0700583 EolDate eol_date_code = status.eol_date;
Jae Hoon Kimc14801d2019-09-03 13:13:59 -0700584
585 KeyValueStore eol_status_store;
Jae Hoon Kim051627a2019-09-03 12:56:32 -0700586 eol_status_store.SetString("EOL_DATE", EolDateToString(eol_date_code));
Jae Hoon Kimc14801d2019-09-03 13:13:59 -0700587 printf("%s", eol_status_store.SaveToString().c_str());
Alex Deymob3fa53b2016-04-18 19:57:58 -0700588 }
589 }
590
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700591 return 0;
592}
Alex Deymo72aa0022015-06-19 21:16:49 -0700593
Alex Deymo690810b2016-01-19 16:11:40 -0800594void UpdateEngineClient::ProcessFlagsAndExit() {
595 int ret = ProcessFlags();
596 if (ret != kContinueRunning)
597 QuitWithExitCode(ret);
598}
599
Alex Deymo72aa0022015-06-19 21:16:49 -0700600} // namespace
601
602int main(int argc, char** argv) {
603 UpdateEngineClient client(argc, argv);
604 return client.Run();
605}