blob: 7446041cb6e11b23d652b03c5341f4acccebf0a1 [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;
Casey Dahlin97c87052016-01-06 14:33:55 -080049using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070050
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070051namespace {
52
Alex Deymo72aa0022015-06-19 21:16:49 -070053// Constant to signal that we need to continue running the daemon after
54// initialization.
55const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070056
Amin Hassani3584bce2017-06-29 14:21:19 -070057// The ShowStatus request will be retried `kShowStatusRetryCount` times at
58// `kShowStatusRetryInterval` second intervals on failure.
59const int kShowStatusRetryCount = 30;
Amin Hassani1a53ba02017-07-28 15:03:14 -070060const int kShowStatusRetryIntervalInSeconds = 2;
Amin Hassani3584bce2017-06-29 14:21:19 -070061
Casey Dahlin19441412016-01-07 14:56:40 -080062class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070063 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
Casey Dahline844c1a2015-12-16 14:30:58 -080065
Alex Deymo72aa0022015-06-19 21:16:49 -070066 ~UpdateEngineClient() override = default;
67
68 protected:
69 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080070 int ret = Daemon::OnInit();
Amin Hassani7cc8bb02019-01-14 16:29:47 -080071 if (ret != EX_OK)
72 return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080073
74 client_ = update_engine::UpdateEngineClient::CreateInstance();
75
76 if (!client_) {
77 LOG(ERROR) << "UpdateEngineService not available.";
78 return 1;
79 }
80
Alex Deymo690810b2016-01-19 16:11:40 -080081 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
82 // of the ProcessFlags method after the Daemon initialization is done.
Eric Caruso22951672018-01-23 14:37:01 -080083 base::MessageLoop::current()->task_runner()->PostTask(
Alex Deymo690810b2016-01-19 16:11:40 -080084 FROM_HERE,
85 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
86 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070087 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070088 }
Alex Deymo72aa0022015-06-19 21:16:49 -070089
90 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070091 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080092 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070093
Casey Dahlin87ab88e2015-12-16 17:58:05 -080094 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
95 // occurred, 2 if no reboot is needed.
96 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070097
Alex Deymo72aa0022015-06-19 21:16:49 -070098 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -080099 // flags. Returns the exit code of the program of kContinueRunning if it
100 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -0700101 int ProcessFlags();
102
Alex Deymo690810b2016-01-19 16:11:40 -0800103 // Processes the flags and exits the program accordingly.
104 void ProcessFlagsAndExit();
105
Alex Deymo72aa0022015-06-19 21:16:49 -0700106 // Copy of argc and argv passed to main().
107 int argc_;
108 char** argv_;
109
Casey Dahline844c1a2015-12-16 14:30:58 -0800110 // Library-based client
111 unique_ptr<update_engine::UpdateEngineClient> client_;
112
Casey Dahlin97c87052016-01-06 14:33:55 -0800113 // Pointers to handlers for cleanup
114 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
115
Alex Deymo72aa0022015-06-19 21:16:49 -0700116 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
117};
118
Casey Dahlin97c87052016-01-06 14:33:55 -0800119class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
120 public:
121 ~ExitingStatusUpdateHandler() override = default;
122
123 void IPCError(const string& error) override;
124};
125
126void ExitingStatusUpdateHandler::IPCError(const string& error) {
127 LOG(ERROR) << error;
128 exit(1);
129}
130
131class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
132 public:
133 ~WatchingStatusUpdateHandler() override = default;
134
Alex Deymo690810b2016-01-19 16:11:40 -0800135 void HandleStatusUpdate(int64_t last_checked_time,
136 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800137 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800138 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800139 int64_t new_size) override;
140};
141
142void WatchingStatusUpdateHandler::HandleStatusUpdate(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800143 int64_t last_checked_time,
144 double progress,
145 UpdateStatus current_operation,
146 const string& new_version,
147 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700148 LOG(INFO) << "Got status update:";
149 LOG(INFO) << " last_checked_time: " << last_checked_time;
150 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800151 LOG(INFO) << " current_operation: "
152 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700153 LOG(INFO) << " new_version: " << new_version;
154 LOG(INFO) << " new_size: " << new_size;
155}
156
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800157bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700158 int64_t last_checked_time = 0;
159 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800160 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700161 string new_version;
162 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700163
Amin Hassani3584bce2017-06-29 14:21:19 -0700164 int retry_count = kShowStatusRetryCount;
165 while (retry_count > 0) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800166 if (client_->GetStatus(&last_checked_time,
167 &progress,
168 &current_op,
169 &new_version,
170 &new_size)) {
Amin Hassani3584bce2017-06-29 14:21:19 -0700171 break;
172 }
173 if (--retry_count == 0) {
174 return false;
175 }
Amin Hassani1a53ba02017-07-28 15:03:14 -0700176 LOG(WARNING) << "Will try " << retry_count << " more times!";
177 base::PlatformThread::Sleep(
178 base::TimeDelta::FromSeconds(kShowStatusRetryIntervalInSeconds));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800179 }
180
Casey Dahlin97c87052016-01-06 14:33:55 -0800181 printf("LAST_CHECKED_TIME=%" PRIi64
182 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700183 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800184 last_checked_time,
185 progress,
186 UpdateStatusToString(current_op),
187 new_version.c_str(),
188 new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800189
190 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700191}
192
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800193int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700194 int64_t last_checked_time = 0;
195 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800196 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700197 string new_version;
198 int64_t new_size = 0;
199
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800200 if (!client_->GetStatus(&last_checked_time,
201 &progress,
202 &current_op,
203 &new_version,
204 &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800205 return 1;
206 }
207
Casey Dahlin97c87052016-01-06 14:33:55 -0800208 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800209 return 0;
210 }
211
212 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700213}
214
Casey Dahlin97c87052016-01-06 14:33:55 -0800215class UpdateWaitHandler : public ExitingStatusUpdateHandler {
216 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800217 explicit UpdateWaitHandler(bool exit_on_error,
218 update_engine::UpdateEngineClient* client)
219 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800220
221 ~UpdateWaitHandler() override = default;
222
Alex Deymo690810b2016-01-19 16:11:40 -0800223 void HandleStatusUpdate(int64_t last_checked_time,
224 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800225 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800226 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800227 int64_t new_size) override;
228
229 private:
230 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800231 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800232};
233
234void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
235 double /* progress */,
236 UpdateStatus current_operation,
237 const string& /* new_version */,
238 int64_t /* new_size */) {
239 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800240 int last_attempt_error;
241 ErrorCode code = ErrorCode::kSuccess;
242 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
243 code = static_cast<ErrorCode>(last_attempt_error);
244
245 LOG(ERROR) << "Update failed, current operation is "
246 << UpdateStatusToString(current_operation)
247 << ", last error code is " << ErrorCodeToString(code) << "("
248 << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700249 exit(1);
250 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800251 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700252 LOG(INFO) << "Update succeeded -- reboot needed.";
253 exit(0);
254 }
Darin Petkov58529db2010-08-13 09:19:47 -0700255}
256
Alex Deymo72aa0022015-06-19 21:16:49 -0700257int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700258 DEFINE_string(app_version, "", "Force the current app version.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800259 DEFINE_string(channel,
260 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700261 "Set the target channel. The device will be powerwashed if the "
262 "target channel is more stable than the current channel unless "
263 "--nopowerwash is specified.");
264 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Alex Deymod63fab32016-10-06 15:40:49 -0700265 DEFINE_string(
266 cohort_hint, "", "Set the current cohort hint to the passed value.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800267 DEFINE_bool(follow,
268 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800269 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700270 "Exit status is 0 if the update succeeded, and 1 otherwise.");
271 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
272 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800273 DEFINE_string(p2p_update,
274 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700275 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
276 " sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800277 DEFINE_bool(powerwash,
278 true,
Casey Dahlin97c87052016-01-06 14:33:55 -0800279 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700280 "do a powerwash or allow it respectively.");
281 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800282 DEFINE_bool(is_reboot_needed,
283 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800284 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700285 "2 if reboot is not needed or 1 if an error occurred.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800286 DEFINE_bool(block_until_reboot_is_needed,
287 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800288 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700289 "needed. Returns non-zero exit status if an error occurred.");
290 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800291 DEFINE_bool(rollback,
292 false,
Alex Deymo1ac8b592015-01-26 13:22:58 -0800293 "Perform a rollback to the previous partition. The device will "
294 "be powerwashed unless --nopowerwash is specified.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800295 DEFINE_bool(can_rollback,
296 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800297 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700298 "is available.");
299 DEFINE_bool(show_channel, false, "Show the current and target channels.");
Alex Deymod63fab32016-10-06 15:40:49 -0700300 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800301 DEFINE_bool(show_p2p_update,
302 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700303 "Show the current setting for peer-to-peer update sharing.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800304 DEFINE_bool(show_update_over_cellular,
305 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700306 "Show the current setting for updates over cellular networks.");
307 DEFINE_bool(status, false, "Print the status to stdout.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800308 DEFINE_bool(update,
309 false,
Casey Dahlin97c87052016-01-06 14:33:55 -0800310 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700311 "Implies --follow.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800312 DEFINE_string(update_over_cellular,
313 "",
Steve Fung97b6f5a2014-10-07 12:39:51 -0700314 "Enables (\"yes\") or disables (\"no\") the updates over "
315 "cellular networks.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800316 DEFINE_bool(watch_for_updates,
317 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700318 "Listen for status updates and print them to the screen.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800319 DEFINE_bool(prev_version,
320 false,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700321 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800322 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Alex Deymob3fa53b2016-04-18 19:57:58 -0700323 DEFINE_bool(eol_status, false, "Show the current end-of-life status.");
Xiaochu Liu88d90382018-08-29 16:09:11 -0700324 DEFINE_bool(install, false, "Requests an install.");
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800325 DEFINE_string(dlc_module_ids, "", "colon-separated list of DLC IDs.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700326
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700327 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700328 base::CommandLine::Init(argc_, argv_);
Sen Jiang49a08972017-07-26 15:54:18 -0700329 brillo::FlagHelper::Init(argc_, argv_, "A/B Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700330
Alex Deymo8ce80d62015-01-27 15:10:43 -0800331 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800332 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800333 base::CommandLine::ForCurrentProcess()->GetArgs();
334 if (!positional_args.empty()) {
335 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
336 << "'. If you want to pass a value to a flag, pass it as "
337 "--flag=value.";
338 return 1;
339 }
340
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700341 // Update the status if requested.
342 if (FLAGS_reset_status) {
343 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800344
345 if (client_->ResetStatus()) {
346 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
347 "run:\n"
348 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
349 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
350 } else {
351 LOG(ERROR) << "ResetStatus failed";
352 return 1;
353 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700354 }
Darin Petkov58529db2010-08-13 09:19:47 -0700355
Alex Deymof4867c42013-06-28 14:41:39 -0700356 // Changes the current update over cellular network setting.
357 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700358 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700359 if (!allowed && FLAGS_update_over_cellular != "no") {
360 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
361 << "\". Please specify \"yes\" or \"no\".";
362 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800363 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800364 LOG(ERROR) << "Error setting the update over cellular setting.";
365 return 1;
366 }
Alex Deymof4867c42013-06-28 14:41:39 -0700367 }
368 }
369
370 // Show the current update over cellular network setting.
371 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800372 bool allowed;
373
374 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
375 LOG(ERROR) << "Error getting the update over cellular setting.";
376 return 1;
377 }
378
Alex Deymof4867c42013-06-28 14:41:39 -0700379 LOG(INFO) << "Current update over cellular network setting: "
380 << (allowed ? "ENABLED" : "DISABLED");
381 }
382
Alex Deymod63fab32016-10-06 15:40:49 -0700383 // Change/show the cohort hint.
384 bool set_cohort_hint =
385 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint");
386 if (set_cohort_hint) {
387 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\"";
388 if (!client_->SetCohortHint(FLAGS_cohort_hint)) {
389 LOG(ERROR) << "Error setting the cohort hint.";
390 return 1;
391 }
392 }
393
394 if (FLAGS_show_cohort_hint || set_cohort_hint) {
395 string cohort_hint;
396 if (!client_->GetCohortHint(&cohort_hint)) {
397 LOG(ERROR) << "Error getting the cohort hint.";
398 return 1;
399 }
400
401 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\"";
402 }
403
Chris Sosacb7fa882013-07-25 17:02:59 -0700404 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700405 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700406 return 1;
407 }
408
Alex Deymo5fdf7762013-07-17 20:01:40 -0700409 // Change the P2P enabled setting.
410 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700411 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700412 if (!enabled && FLAGS_p2p_update != "no") {
413 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
414 << "\". Please specify \"yes\" or \"no\".";
415 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800416 if (!client_->SetP2PUpdatePermission(enabled)) {
417 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
418 return 1;
419 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700420 }
421 }
422
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800423 // Show the rollback availability.
424 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800425 string rollback_partition;
426
427 if (!client_->GetRollbackPartition(&rollback_partition)) {
Sen Jiang771f6482018-04-04 17:59:10 -0700428 LOG(ERROR) << "Error while querying rollback partition availability.";
Casey Dahlinef361132015-12-17 13:02:37 -0800429 return 1;
430 }
431
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700432 bool can_rollback = true;
433 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700434 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700435 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700436 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700437 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700438 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700439
440 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700441 if (!can_rollback) {
442 return 1;
443 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800444 }
445
Alex Deymo5fdf7762013-07-17 20:01:40 -0700446 // Show the current P2P enabled setting.
447 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800448 bool enabled;
449
450 if (!client_->GetP2PUpdatePermission(&enabled)) {
451 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
452 return 1;
453 }
454
Alex Deymo5fdf7762013-07-17 20:01:40 -0700455 LOG(INFO) << "Current update using P2P setting: "
456 << (enabled ? "ENABLED" : "DISABLED");
457 }
458
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700459 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800460 if (!FLAGS_channel.empty()) {
461 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
462 LOG(ERROR) << "Error setting the channel.";
463 return 1;
464 }
465
466 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
467 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700468
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700469 // Show the current and target channels if requested.
470 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800471 string current_channel;
472 string target_channel;
473
474 if (!client_->GetChannel(&current_channel)) {
475 LOG(ERROR) << "Error getting the current channel.";
476 return 1;
477 }
478
479 if (!client_->GetTargetChannel(&target_channel)) {
480 LOG(ERROR) << "Error getting the target channel.";
481 return 1;
482 }
483
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700484 LOG(INFO) << "Current Channel: " << current_channel;
485
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700486 if (!target_channel.empty())
487 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900488 }
489
Amin Hassani89b9a2d2018-04-20 16:01:45 -0700490 bool do_update_request = FLAGS_check_for_update || FLAGS_update ||
491 !FLAGS_app_version.empty() ||
Casey Dahlin97c87052016-01-06 14:33:55 -0800492 !FLAGS_omaha_url.empty();
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800493 if (FLAGS_update)
494 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700495
Chris Sosad317e402013-06-12 13:47:09 -0700496 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700497 LOG(ERROR) << "Incompatible flags specified with rollback."
498 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700499 return 1;
500 }
501
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700502 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700503 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800504 if (!client_->Rollback(FLAGS_powerwash)) {
505 LOG(ERROR) << "Rollback request failed.";
506 return 1;
507 }
Chris Sosad317e402013-06-12 13:47:09 -0700508 }
509
Xiaochu Liu88d90382018-08-29 16:09:11 -0700510 if (FLAGS_install) {
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800511 // Parse DLC module IDs.
512 vector<string> dlc_module_ids;
513 if (!FLAGS_dlc_module_ids.empty()) {
514 dlc_module_ids = base::SplitString(FLAGS_dlc_module_ids,
515 ":",
516 base::TRIM_WHITESPACE,
517 base::SPLIT_WANT_ALL);
Xiaochu Liu88d90382018-08-29 16:09:11 -0700518 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800519 if (dlc_module_ids.empty()) {
520 LOG(ERROR) << "dlc_module_ids is empty:" << FLAGS_dlc_module_ids;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700521 return 1;
522 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800523 if (!client_->AttemptInstall(FLAGS_omaha_url, dlc_module_ids)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700524 LOG(ERROR) << "AttemptInstall failed.";
525 return 1;
526 }
527 return 0;
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800528 } else if (!FLAGS_dlc_module_ids.empty()) {
529 LOG(ERROR) << "dlc_module_ids is not empty while install is not set:"
530 << FLAGS_dlc_module_ids;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700531 return 1;
532 }
533
Darin Petkov58529db2010-08-13 09:19:47 -0700534 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700535 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700536 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700537 string app_version = FLAGS_app_version;
538 if (FLAGS_update && app_version.empty()) {
539 app_version = "ForcedUpdate";
540 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700541 }
Darin Petkov58529db2010-08-13 09:19:47 -0700542 LOG(INFO) << "Initiating update check and install.";
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800543 if (!client_->AttemptUpdate(
544 app_version, FLAGS_omaha_url, FLAGS_interactive)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800545 LOG(ERROR) << "Error checking for update.";
546 return 1;
547 }
Chris Sosa192449e2013-10-28 14:16:19 -0700548 }
Darin Petkov58529db2010-08-13 09:19:47 -0700549
Chris Sosa192449e2013-10-28 14:16:19 -0700550 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800551 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
552 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
553 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700554 LOG(ERROR) << "Multiple exclusive options selected. "
555 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700556 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700557 << "or --status.";
558 return 1;
559 }
560
561 if (FLAGS_status) {
562 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800563 if (!ShowStatus()) {
564 LOG(ERROR) << "Failed to query status";
565 return 1;
566 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700567 return 0;
568 }
Darin Petkov58529db2010-08-13 09:19:47 -0700569
Chris Sosa192449e2013-10-28 14:16:19 -0700570 if (FLAGS_follow) {
571 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800572 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800573 handlers_.emplace_back(handler);
574 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700575 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700576 }
577
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700578 if (FLAGS_watch_for_updates) {
579 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800580 auto handler = new WatchingStatusUpdateHandler();
581 handlers_.emplace_back(handler);
582 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700583 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700584 }
Darin Petkov58529db2010-08-13 09:19:47 -0700585
Darin Petkov296889c2010-07-23 16:20:54 -0700586 if (FLAGS_reboot) {
587 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800588 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700589 return 0;
590 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700591
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700592 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800593 string prev_version;
594
595 if (!client_->GetPrevVersion(&prev_version)) {
596 LOG(ERROR) << "Error getting previous version.";
597 } else {
598 LOG(INFO) << "Previous version = " << prev_version;
599 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700600 }
601
David Zeuthen9d73a722014-04-04 14:52:46 -0700602 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800603 int ret = GetNeedReboot();
604
605 if (ret == 1) {
606 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700607 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800608
609 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700610 }
611
612 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800613 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800614 handlers_.emplace_back(handler);
615 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700616 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700617 }
618
Shuqian Zhao29971732016-02-05 11:29:32 -0800619 if (FLAGS_last_attempt_error) {
620 int last_attempt_error;
621 if (!client_->GetLastAttemptError(&last_attempt_error)) {
622 LOG(ERROR) << "Error getting last attempt error.";
623 } else {
624 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800625 printf(
626 "ERROR_CODE=%i\n"
627 "ERROR_MESSAGE=%s\n",
628 last_attempt_error,
629 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800630 }
Alex Deymo9a069222016-03-02 16:14:42 -0800631 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800632
Alex Deymob3fa53b2016-04-18 19:57:58 -0700633 if (FLAGS_eol_status) {
634 int eol_status;
635 if (!client_->GetEolStatus(&eol_status)) {
636 LOG(ERROR) << "Error getting the end-of-life status.";
637 } else {
638 EolStatus eol_status_code = static_cast<EolStatus>(eol_status);
639 printf("EOL_STATUS=%s\n", EolStatusToString(eol_status_code));
640 }
641 }
642
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700643 return 0;
644}
Alex Deymo72aa0022015-06-19 21:16:49 -0700645
Alex Deymo690810b2016-01-19 16:11:40 -0800646void UpdateEngineClient::ProcessFlagsAndExit() {
647 int ret = ProcessFlags();
648 if (ret != kContinueRunning)
649 QuitWithExitCode(ret);
650}
651
Alex Deymo72aa0022015-06-19 21:16:49 -0700652} // namespace
653
654int main(int argc, char** argv) {
655 UpdateEngineClient client(argc, argv);
656 return client.Run();
657}