blob: bb19632c9d2a96493eec89b127ad8d935f491062 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070016
Alex Deymo72aa0022015-06-19 21:16:49 -070017#include <inttypes.h>
18#include <sysexits.h>
19#include <unistd.h>
20
Casey Dahline844c1a2015-12-16 14:30:58 -080021#include <memory>
Darin Petkov5a7f5652010-07-22 21:40:09 -070022#include <string>
Casey Dahlin97c87052016-01-06 14:33:55 -080023#include <vector>
Darin Petkov5a7f5652010-07-22 21:40:09 -070024
Alex Deymo72aa0022015-06-19 21:16:49 -070025#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080026#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070027#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070028#include <base/macros.h>
Amin Hassani1a53ba02017-07-28 15:03:14 -070029#include <base/threading/platform_thread.h>
Casey Dahlin19441412016-01-07 14:56:40 -080030#include <brillo/daemons/daemon.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/flag_helper.h>
Casey Dahlin87ab88e2015-12-16 17:58:05 -080032
Alex Deymo9a069222016-03-02 16:14:42 -080033#include "update_engine/client.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080034#include "update_engine/common/error_code.h"
35#include "update_engine/common/error_code_utils.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070036#include "update_engine/omaha_utils.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080037#include "update_engine/status_update_handler.h"
38#include "update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080039#include "update_engine/update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070040
Alex Deymob3fa53b2016-04-18 19:57:58 -070041using chromeos_update_engine::EolStatus;
Shuqian Zhao29971732016-02-05 11:29:32 -080042using chromeos_update_engine::ErrorCode;
Alex Deymo9a069222016-03-02 16:14:42 -080043using chromeos_update_engine::UpdateStatusToString;
44using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070045using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080046using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080047using std::vector;
Casey Dahlin97c87052016-01-06 14:33:55 -080048using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070049
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070050namespace {
51
Alex Deymo72aa0022015-06-19 21:16:49 -070052// Constant to signal that we need to continue running the daemon after
53// initialization.
54const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070055
Amin Hassani3584bce2017-06-29 14:21:19 -070056// The ShowStatus request will be retried `kShowStatusRetryCount` times at
57// `kShowStatusRetryInterval` second intervals on failure.
58const int kShowStatusRetryCount = 30;
Amin Hassani1a53ba02017-07-28 15:03:14 -070059const int kShowStatusRetryIntervalInSeconds = 2;
Amin Hassani3584bce2017-06-29 14:21:19 -070060
Casey Dahlin19441412016-01-07 14:56:40 -080061class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070062 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080063 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
Casey Dahline844c1a2015-12-16 14:30:58 -080064 }
65
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();
Casey Dahlin97c87052016-01-06 14:33:55 -080071 if (ret != EX_OK) return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080072
73 client_ = update_engine::UpdateEngineClient::CreateInstance();
74
75 if (!client_) {
76 LOG(ERROR) << "UpdateEngineService not available.";
77 return 1;
78 }
79
Alex Deymo690810b2016-01-19 16:11:40 -080080 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
81 // of the ProcessFlags method after the Daemon initialization is done.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090082 base::MessageLoop::current()->task_runner()->PostTask(
Alex Deymo690810b2016-01-19 16:11:40 -080083 FROM_HERE,
84 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
85 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070086 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070087 }
Alex Deymo72aa0022015-06-19 21:16:49 -070088
89 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070090 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080091 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070092
Casey Dahlin87ab88e2015-12-16 17:58:05 -080093 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
94 // occurred, 2 if no reboot is needed.
95 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070096
Alex Deymo72aa0022015-06-19 21:16:49 -070097 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -080098 // flags. Returns the exit code of the program of kContinueRunning if it
99 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -0700100 int ProcessFlags();
101
Alex Deymo690810b2016-01-19 16:11:40 -0800102 // Processes the flags and exits the program accordingly.
103 void ProcessFlagsAndExit();
104
Alex Deymo72aa0022015-06-19 21:16:49 -0700105 // Copy of argc and argv passed to main().
106 int argc_;
107 char** argv_;
108
Casey Dahline844c1a2015-12-16 14:30:58 -0800109 // Library-based client
110 unique_ptr<update_engine::UpdateEngineClient> client_;
111
Casey Dahlin97c87052016-01-06 14:33:55 -0800112 // Pointers to handlers for cleanup
113 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
114
Alex Deymo72aa0022015-06-19 21:16:49 -0700115 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
116};
117
Casey Dahlin97c87052016-01-06 14:33:55 -0800118class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
119 public:
120 ~ExitingStatusUpdateHandler() override = default;
121
122 void IPCError(const string& error) override;
123};
124
125void ExitingStatusUpdateHandler::IPCError(const string& error) {
126 LOG(ERROR) << error;
127 exit(1);
128}
129
130class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
131 public:
132 ~WatchingStatusUpdateHandler() override = default;
133
Alex Deymo690810b2016-01-19 16:11:40 -0800134 void HandleStatusUpdate(int64_t last_checked_time,
135 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800136 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800137 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800138 int64_t new_size) override;
139};
140
141void WatchingStatusUpdateHandler::HandleStatusUpdate(
142 int64_t last_checked_time, double progress, UpdateStatus current_operation,
143 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700144 LOG(INFO) << "Got status update:";
145 LOG(INFO) << " last_checked_time: " << last_checked_time;
146 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800147 LOG(INFO) << " current_operation: "
148 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700149 LOG(INFO) << " new_version: " << new_version;
150 LOG(INFO) << " new_size: " << new_size;
151}
152
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800153bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700154 int64_t last_checked_time = 0;
155 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800156 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700157 string new_version;
158 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700159
Amin Hassani3584bce2017-06-29 14:21:19 -0700160 int retry_count = kShowStatusRetryCount;
161 while (retry_count > 0) {
162 if (client_->GetStatus(&last_checked_time, &progress, &current_op,
163 &new_version, &new_size)) {
164 break;
165 }
166 if (--retry_count == 0) {
167 return false;
168 }
Amin Hassani1a53ba02017-07-28 15:03:14 -0700169 LOG(WARNING) << "Will try " << retry_count << " more times!";
170 base::PlatformThread::Sleep(
171 base::TimeDelta::FromSeconds(kShowStatusRetryIntervalInSeconds));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800172 }
173
Casey Dahlin97c87052016-01-06 14:33:55 -0800174 printf("LAST_CHECKED_TIME=%" PRIi64
175 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700176 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800177 last_checked_time, progress, UpdateStatusToString(current_op),
178 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800179
180 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700181}
182
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800183int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700184 int64_t last_checked_time = 0;
185 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800186 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700187 string new_version;
188 int64_t new_size = 0;
189
Casey Dahlin97c87052016-01-06 14:33:55 -0800190 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
191 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800192 return 1;
193 }
194
Casey Dahlin97c87052016-01-06 14:33:55 -0800195 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800196 return 0;
197 }
198
199 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700200}
201
Casey Dahlin97c87052016-01-06 14:33:55 -0800202class UpdateWaitHandler : public ExitingStatusUpdateHandler {
203 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800204 explicit UpdateWaitHandler(bool exit_on_error,
205 update_engine::UpdateEngineClient* client)
206 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800207
208 ~UpdateWaitHandler() override = default;
209
Alex Deymo690810b2016-01-19 16:11:40 -0800210 void HandleStatusUpdate(int64_t last_checked_time,
211 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800212 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800213 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800214 int64_t new_size) override;
215
216 private:
217 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800218 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800219};
220
221void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
222 double /* progress */,
223 UpdateStatus current_operation,
224 const string& /* new_version */,
225 int64_t /* new_size */) {
226 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800227 int last_attempt_error;
228 ErrorCode code = ErrorCode::kSuccess;
229 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
230 code = static_cast<ErrorCode>(last_attempt_error);
231
232 LOG(ERROR) << "Update failed, current operation is "
233 << UpdateStatusToString(current_operation)
234 << ", last error code is " << ErrorCodeToString(code) << "("
235 << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700236 exit(1);
237 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800238 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700239 LOG(INFO) << "Update succeeded -- reboot needed.";
240 exit(0);
241 }
Darin Petkov58529db2010-08-13 09:19:47 -0700242}
243
Alex Deymo72aa0022015-06-19 21:16:49 -0700244int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700245 DEFINE_string(app_version, "", "Force the current app version.");
246 DEFINE_string(channel, "",
247 "Set the target channel. The device will be powerwashed if the "
248 "target channel is more stable than the current channel unless "
249 "--nopowerwash is specified.");
250 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700251 DEFINE_string(
252 cohort_hint, "", "Set the current cohort hint to the passed value.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800253 DEFINE_bool(follow, false,
254 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700255 "Exit status is 0 if the update succeeded, and 1 otherwise.");
256 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
257 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
258 DEFINE_string(p2p_update, "",
259 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
260 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800261 DEFINE_bool(powerwash, true,
262 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700263 "do a powerwash or allow it respectively.");
264 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800265 DEFINE_bool(is_reboot_needed, false,
266 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700267 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800268 DEFINE_bool(block_until_reboot_is_needed, false,
269 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700270 "needed. Returns non-zero exit status if an error occurred.");
271 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800272 DEFINE_bool(rollback, false,
273 "Perform a rollback to the previous partition. The device will "
274 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800275 DEFINE_bool(can_rollback, false,
276 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700277 "is available.");
278 DEFINE_bool(show_channel, false, "Show the current and target channels.");
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700279 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700280 DEFINE_bool(show_p2p_update, false,
281 "Show the current setting for peer-to-peer update sharing.");
282 DEFINE_bool(show_update_over_cellular, false,
283 "Show the current setting for updates over cellular networks.");
284 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800285 DEFINE_bool(update, false,
286 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700287 "Implies --follow.");
288 DEFINE_string(update_over_cellular, "",
289 "Enables (\"yes\") or disables (\"no\") the updates over "
290 "cellular networks.");
291 DEFINE_bool(watch_for_updates, false,
292 "Listen for status updates and print them to the screen.");
293 DEFINE_bool(prev_version, false,
294 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800295 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Alex Deymob3fa53b2016-04-18 19:57:58 -0700296 DEFINE_bool(eol_status, false, "Show the current end-of-life status.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700297
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700298 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700299 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700300 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700301
Alex Deymo8ce80d62015-01-27 15:10:43 -0800302 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800303 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800304 base::CommandLine::ForCurrentProcess()->GetArgs();
305 if (!positional_args.empty()) {
306 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
307 << "'. If you want to pass a value to a flag, pass it as "
308 "--flag=value.";
309 return 1;
310 }
311
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700312 // Update the status if requested.
313 if (FLAGS_reset_status) {
314 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800315
316 if (client_->ResetStatus()) {
317 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
318 "run:\n"
319 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
320 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
321 } else {
322 LOG(ERROR) << "ResetStatus failed";
323 return 1;
324 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700325 }
Darin Petkov58529db2010-08-13 09:19:47 -0700326
Alex Deymof4867c42013-06-28 14:41:39 -0700327 // Changes the current update over cellular network setting.
328 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700329 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700330 if (!allowed && FLAGS_update_over_cellular != "no") {
331 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
332 << "\". Please specify \"yes\" or \"no\".";
333 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800334 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800335 LOG(ERROR) << "Error setting the update over cellular setting.";
336 return 1;
337 }
Alex Deymof4867c42013-06-28 14:41:39 -0700338 }
339 }
340
341 // Show the current update over cellular network setting.
342 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800343 bool allowed;
344
345 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
346 LOG(ERROR) << "Error getting the update over cellular setting.";
347 return 1;
348 }
349
Alex Deymof4867c42013-06-28 14:41:39 -0700350 LOG(INFO) << "Current update over cellular network setting: "
351 << (allowed ? "ENABLED" : "DISABLED");
352 }
353
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700354 // Change/show the cohort hint.
355 bool set_cohort_hint =
356 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint");
357 if (set_cohort_hint) {
358 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\"";
359 if (!client_->SetCohortHint(FLAGS_cohort_hint)) {
360 LOG(ERROR) << "Error setting the cohort hint.";
361 return 1;
362 }
363 }
364
365 if (FLAGS_show_cohort_hint || set_cohort_hint) {
366 string cohort_hint;
367 if (!client_->GetCohortHint(&cohort_hint)) {
368 LOG(ERROR) << "Error getting the cohort hint.";
369 return 1;
370 }
371
372 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\"";
373 }
374
Chris Sosacb7fa882013-07-25 17:02:59 -0700375 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700376 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700377 return 1;
378 }
379
Alex Deymo5fdf7762013-07-17 20:01:40 -0700380 // Change the P2P enabled setting.
381 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700382 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700383 if (!enabled && FLAGS_p2p_update != "no") {
384 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
385 << "\". Please specify \"yes\" or \"no\".";
386 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800387 if (!client_->SetP2PUpdatePermission(enabled)) {
388 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
389 return 1;
390 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700391 }
392 }
393
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800394 // Show the rollback availability.
395 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800396 string rollback_partition;
397
398 if (!client_->GetRollbackPartition(&rollback_partition)) {
399 LOG(ERROR) << "Error while querying rollback partition availabilty.";
400 return 1;
401 }
402
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700403 bool can_rollback = true;
404 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700405 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700406 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700407 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700408 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700409 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700410
411 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700412 if (!can_rollback) {
413 return 1;
414 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800415 }
416
Alex Deymo5fdf7762013-07-17 20:01:40 -0700417 // Show the current P2P enabled setting.
418 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800419 bool enabled;
420
421 if (!client_->GetP2PUpdatePermission(&enabled)) {
422 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
423 return 1;
424 }
425
Alex Deymo5fdf7762013-07-17 20:01:40 -0700426 LOG(INFO) << "Current update using P2P setting: "
427 << (enabled ? "ENABLED" : "DISABLED");
428 }
429
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700430 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800431 if (!FLAGS_channel.empty()) {
432 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
433 LOG(ERROR) << "Error setting the channel.";
434 return 1;
435 }
436
437 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
438 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700439
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700440 // Show the current and target channels if requested.
441 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800442 string current_channel;
443 string target_channel;
444
445 if (!client_->GetChannel(&current_channel)) {
446 LOG(ERROR) << "Error getting the current channel.";
447 return 1;
448 }
449
450 if (!client_->GetTargetChannel(&target_channel)) {
451 LOG(ERROR) << "Error getting the target channel.";
452 return 1;
453 }
454
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700455 LOG(INFO) << "Current Channel: " << current_channel;
456
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700457 if (!target_channel.empty())
458 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900459 }
460
Chris Sosad317e402013-06-12 13:47:09 -0700461 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800462 !FLAGS_app_version.empty() |
463 !FLAGS_omaha_url.empty();
464 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700465
Chris Sosad317e402013-06-12 13:47:09 -0700466 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700467 LOG(ERROR) << "Incompatible flags specified with rollback."
468 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700469 return 1;
470 }
471
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700472 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700473 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800474 if (!client_->Rollback(FLAGS_powerwash)) {
475 LOG(ERROR) << "Rollback request failed.";
476 return 1;
477 }
Chris Sosad317e402013-06-12 13:47:09 -0700478 }
479
Darin Petkov58529db2010-08-13 09:19:47 -0700480 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700481 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700482 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700483 string app_version = FLAGS_app_version;
484 if (FLAGS_update && app_version.empty()) {
485 app_version = "ForcedUpdate";
486 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700487 }
Darin Petkov58529db2010-08-13 09:19:47 -0700488 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800489 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
490 FLAGS_interactive)) {
491 LOG(ERROR) << "Error checking for update.";
492 return 1;
493 }
Chris Sosa192449e2013-10-28 14:16:19 -0700494 }
Darin Petkov58529db2010-08-13 09:19:47 -0700495
Chris Sosa192449e2013-10-28 14:16:19 -0700496 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800497 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
498 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
499 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700500 LOG(ERROR) << "Multiple exclusive options selected. "
501 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700502 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700503 << "or --status.";
504 return 1;
505 }
506
507 if (FLAGS_status) {
508 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800509 if (!ShowStatus()) {
510 LOG(ERROR) << "Failed to query status";
511 return 1;
512 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700513 return 0;
514 }
Darin Petkov58529db2010-08-13 09:19:47 -0700515
Chris Sosa192449e2013-10-28 14:16:19 -0700516 if (FLAGS_follow) {
517 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800518 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800519 handlers_.emplace_back(handler);
520 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700521 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700522 }
523
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700524 if (FLAGS_watch_for_updates) {
525 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800526 auto handler = new WatchingStatusUpdateHandler();
527 handlers_.emplace_back(handler);
528 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700529 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700530 }
Darin Petkov58529db2010-08-13 09:19:47 -0700531
Darin Petkov296889c2010-07-23 16:20:54 -0700532 if (FLAGS_reboot) {
533 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800534 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700535 return 0;
536 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700537
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700538 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800539 string prev_version;
540
541 if (!client_->GetPrevVersion(&prev_version)) {
542 LOG(ERROR) << "Error getting previous version.";
543 } else {
544 LOG(INFO) << "Previous version = " << prev_version;
545 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700546 }
547
David Zeuthen9d73a722014-04-04 14:52:46 -0700548 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800549 int ret = GetNeedReboot();
550
551 if (ret == 1) {
552 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700553 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800554
555 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700556 }
557
558 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800559 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800560 handlers_.emplace_back(handler);
561 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700562 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700563 }
564
Shuqian Zhao29971732016-02-05 11:29:32 -0800565 if (FLAGS_last_attempt_error) {
566 int last_attempt_error;
567 if (!client_->GetLastAttemptError(&last_attempt_error)) {
568 LOG(ERROR) << "Error getting last attempt error.";
569 } else {
570 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800571 printf(
572 "ERROR_CODE=%i\n"
573 "ERROR_MESSAGE=%s\n",
574 last_attempt_error,
575 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800576 }
Alex Deymo9a069222016-03-02 16:14:42 -0800577 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800578
Alex Deymob3fa53b2016-04-18 19:57:58 -0700579 if (FLAGS_eol_status) {
580 int eol_status;
581 if (!client_->GetEolStatus(&eol_status)) {
582 LOG(ERROR) << "Error getting the end-of-life status.";
583 } else {
584 EolStatus eol_status_code = static_cast<EolStatus>(eol_status);
585 printf("EOL_STATUS=%s\n", EolStatusToString(eol_status_code));
586 }
587 }
588
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700589 return 0;
590}
Alex Deymo72aa0022015-06-19 21:16:49 -0700591
Alex Deymo690810b2016-01-19 16:11:40 -0800592void UpdateEngineClient::ProcessFlagsAndExit() {
593 int ret = ProcessFlags();
594 if (ret != kContinueRunning)
595 QuitWithExitCode(ret);
596}
597
Alex Deymo72aa0022015-06-19 21:16:49 -0700598} // namespace
599
600int main(int argc, char** argv) {
601 UpdateEngineClient client(argc, argv);
602 return client.Run();
603}