blob: f337dd64670de3d51311be01e0d6c4d25f7b5175 [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
Amin Hassani3584bce2017-06-29 14:21:19 -070017#include <base/bind.h>
18#include <base/command_line.h>
19#include <base/logging.h>
20#include <base/macros.h>
21#include <brillo/daemons/daemon.h>
22#include <brillo/flag_helper.h>
23
Alex Deymo72aa0022015-06-19 21:16:49 -070024#include <inttypes.h>
25#include <sysexits.h>
26#include <unistd.h>
27
Casey Dahline844c1a2015-12-16 14:30:58 -080028#include <memory>
Darin Petkov5a7f5652010-07-22 21:40:09 -070029#include <string>
Casey Dahlin97c87052016-01-06 14:33:55 -080030#include <vector>
Darin Petkov5a7f5652010-07-22 21:40:09 -070031
Alex Deymo9a069222016-03-02 16:14:42 -080032#include "update_engine/client.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080033#include "update_engine/common/error_code.h"
34#include "update_engine/common/error_code_utils.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070035#include "update_engine/omaha_utils.h"
Casey Dahlin97c87052016-01-06 14:33:55 -080036#include "update_engine/status_update_handler.h"
37#include "update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080038#include "update_engine/update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070039
Alex Deymob3fa53b2016-04-18 19:57:58 -070040using chromeos_update_engine::EolStatus;
Shuqian Zhao29971732016-02-05 11:29:32 -080041using chromeos_update_engine::ErrorCode;
Alex Deymo9a069222016-03-02 16:14:42 -080042using chromeos_update_engine::UpdateStatusToString;
43using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070044using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080045using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080046using std::vector;
Casey Dahlin97c87052016-01-06 14:33:55 -080047using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070048
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070049namespace {
50
Alex Deymo72aa0022015-06-19 21:16:49 -070051// Constant to signal that we need to continue running the daemon after
52// initialization.
53const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070054
Amin Hassani3584bce2017-06-29 14:21:19 -070055// The ShowStatus request will be retried `kShowStatusRetryCount` times at
56// `kShowStatusRetryInterval` second intervals on failure.
57const int kShowStatusRetryCount = 30;
58const int kShowStatusRetryInterval = 2;
59
Casey Dahlin19441412016-01-07 14:56:40 -080060class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070061 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080062 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
Casey Dahline844c1a2015-12-16 14:30:58 -080063 }
64
Alex Deymo72aa0022015-06-19 21:16:49 -070065 ~UpdateEngineClient() override = default;
66
67 protected:
68 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080069 int ret = Daemon::OnInit();
Casey Dahlin97c87052016-01-06 14:33:55 -080070 if (ret != EX_OK) return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080071
72 client_ = update_engine::UpdateEngineClient::CreateInstance();
73
74 if (!client_) {
75 LOG(ERROR) << "UpdateEngineService not available.";
76 return 1;
77 }
78
Alex Deymo690810b2016-01-19 16:11:40 -080079 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
80 // of the ProcessFlags method after the Daemon initialization is done.
81 base::MessageLoop::current()->PostTask(
82 FROM_HERE,
83 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
84 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070085 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070086 }
Alex Deymo72aa0022015-06-19 21:16:49 -070087
88 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070089 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080090 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070091
Casey Dahlin87ab88e2015-12-16 17:58:05 -080092 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
93 // occurred, 2 if no reboot is needed.
94 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070095
Alex Deymo72aa0022015-06-19 21:16:49 -070096 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -080097 // flags. Returns the exit code of the program of kContinueRunning if it
98 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -070099 int ProcessFlags();
100
Alex Deymo690810b2016-01-19 16:11:40 -0800101 // Processes the flags and exits the program accordingly.
102 void ProcessFlagsAndExit();
103
Alex Deymo72aa0022015-06-19 21:16:49 -0700104 // Copy of argc and argv passed to main().
105 int argc_;
106 char** argv_;
107
Casey Dahline844c1a2015-12-16 14:30:58 -0800108 // Library-based client
109 unique_ptr<update_engine::UpdateEngineClient> client_;
110
Casey Dahlin97c87052016-01-06 14:33:55 -0800111 // Pointers to handlers for cleanup
112 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
113
Alex Deymo72aa0022015-06-19 21:16:49 -0700114 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
115};
116
Casey Dahlin97c87052016-01-06 14:33:55 -0800117class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
118 public:
119 ~ExitingStatusUpdateHandler() override = default;
120
121 void IPCError(const string& error) override;
122};
123
124void ExitingStatusUpdateHandler::IPCError(const string& error) {
125 LOG(ERROR) << error;
126 exit(1);
127}
128
129class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
130 public:
131 ~WatchingStatusUpdateHandler() override = default;
132
Alex Deymo690810b2016-01-19 16:11:40 -0800133 void HandleStatusUpdate(int64_t last_checked_time,
134 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800135 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800136 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800137 int64_t new_size) override;
138};
139
140void WatchingStatusUpdateHandler::HandleStatusUpdate(
141 int64_t last_checked_time, double progress, UpdateStatus current_operation,
142 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700143 LOG(INFO) << "Got status update:";
144 LOG(INFO) << " last_checked_time: " << last_checked_time;
145 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800146 LOG(INFO) << " current_operation: "
147 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700148 LOG(INFO) << " new_version: " << new_version;
149 LOG(INFO) << " new_size: " << new_size;
150}
151
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800152bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700153 int64_t last_checked_time = 0;
154 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800155 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700156 string new_version;
157 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700158
Amin Hassani3584bce2017-06-29 14:21:19 -0700159 int retry_count = kShowStatusRetryCount;
160 while (retry_count > 0) {
161 if (client_->GetStatus(&last_checked_time, &progress, &current_op,
162 &new_version, &new_size)) {
163 break;
164 }
165 if (--retry_count == 0) {
166 return false;
167 }
168 LOG(WARNING) << "Trying " << retry_count << " more times";
169 sleep(kShowStatusRetryInterval);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800170 }
171
Casey Dahlin97c87052016-01-06 14:33:55 -0800172 printf("LAST_CHECKED_TIME=%" PRIi64
173 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700174 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800175 last_checked_time, progress, UpdateStatusToString(current_op),
176 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800177
178 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700179}
180
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800181int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700182 int64_t last_checked_time = 0;
183 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800184 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700185 string new_version;
186 int64_t new_size = 0;
187
Casey Dahlin97c87052016-01-06 14:33:55 -0800188 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
189 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800190 return 1;
191 }
192
Casey Dahlin97c87052016-01-06 14:33:55 -0800193 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800194 return 0;
195 }
196
197 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700198}
199
Casey Dahlin97c87052016-01-06 14:33:55 -0800200class UpdateWaitHandler : public ExitingStatusUpdateHandler {
201 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800202 explicit UpdateWaitHandler(bool exit_on_error,
203 update_engine::UpdateEngineClient* client)
204 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800205
206 ~UpdateWaitHandler() override = default;
207
Alex Deymo690810b2016-01-19 16:11:40 -0800208 void HandleStatusUpdate(int64_t last_checked_time,
209 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800210 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800211 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800212 int64_t new_size) override;
213
214 private:
215 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800216 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800217};
218
219void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
220 double /* progress */,
221 UpdateStatus current_operation,
222 const string& /* new_version */,
223 int64_t /* new_size */) {
224 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800225 int last_attempt_error;
226 ErrorCode code = ErrorCode::kSuccess;
227 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
228 code = static_cast<ErrorCode>(last_attempt_error);
229
230 LOG(ERROR) << "Update failed, current operation is "
231 << UpdateStatusToString(current_operation)
232 << ", last error code is " << ErrorCodeToString(code) << "("
233 << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700234 exit(1);
235 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800236 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700237 LOG(INFO) << "Update succeeded -- reboot needed.";
238 exit(0);
239 }
Darin Petkov58529db2010-08-13 09:19:47 -0700240}
241
Alex Deymo72aa0022015-06-19 21:16:49 -0700242int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700243 DEFINE_string(app_version, "", "Force the current app version.");
244 DEFINE_string(channel, "",
245 "Set the target channel. The device will be powerwashed if the "
246 "target channel is more stable than the current channel unless "
247 "--nopowerwash is specified.");
248 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Alex Deymod63fab32016-10-06 15:40:49 -0700249 DEFINE_string(
250 cohort_hint, "", "Set the current cohort hint to the passed value.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800251 DEFINE_bool(follow, false,
252 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700253 "Exit status is 0 if the update succeeded, and 1 otherwise.");
254 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
255 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
256 DEFINE_string(p2p_update, "",
257 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
258 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800259 DEFINE_bool(powerwash, true,
260 "When performing rollback or channel change, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700261 "do a powerwash or allow it respectively.");
262 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800263 DEFINE_bool(is_reboot_needed, false,
264 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700265 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800266 DEFINE_bool(block_until_reboot_is_needed, false,
267 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700268 "needed. Returns non-zero exit status if an error occurred.");
269 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800270 DEFINE_bool(rollback, false,
271 "Perform a rollback to the previous partition. The device will "
272 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800273 DEFINE_bool(can_rollback, false,
274 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700275 "is available.");
276 DEFINE_bool(show_channel, false, "Show the current and target channels.");
Alex Deymod63fab32016-10-06 15:40:49 -0700277 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700278 DEFINE_bool(show_p2p_update, false,
279 "Show the current setting for peer-to-peer update sharing.");
280 DEFINE_bool(show_update_over_cellular, false,
281 "Show the current setting for updates over cellular networks.");
282 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800283 DEFINE_bool(update, false,
284 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700285 "Implies --follow.");
286 DEFINE_string(update_over_cellular, "",
287 "Enables (\"yes\") or disables (\"no\") the updates over "
288 "cellular networks.");
289 DEFINE_bool(watch_for_updates, false,
290 "Listen for status updates and print them to the screen.");
291 DEFINE_bool(prev_version, false,
292 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800293 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Alex Deymob3fa53b2016-04-18 19:57:58 -0700294 DEFINE_bool(eol_status, false, "Show the current end-of-life status.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700295
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700296 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700297 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700298 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700299
Alex Deymo8ce80d62015-01-27 15:10:43 -0800300 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800301 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800302 base::CommandLine::ForCurrentProcess()->GetArgs();
303 if (!positional_args.empty()) {
304 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
305 << "'. If you want to pass a value to a flag, pass it as "
306 "--flag=value.";
307 return 1;
308 }
309
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700310 // Update the status if requested.
311 if (FLAGS_reset_status) {
312 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800313
314 if (client_->ResetStatus()) {
315 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
316 "run:\n"
317 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
318 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
319 } else {
320 LOG(ERROR) << "ResetStatus failed";
321 return 1;
322 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700323 }
Darin Petkov58529db2010-08-13 09:19:47 -0700324
Alex Deymof4867c42013-06-28 14:41:39 -0700325 // Changes the current update over cellular network setting.
326 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700327 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700328 if (!allowed && FLAGS_update_over_cellular != "no") {
329 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
330 << "\". Please specify \"yes\" or \"no\".";
331 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800332 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800333 LOG(ERROR) << "Error setting the update over cellular setting.";
334 return 1;
335 }
Alex Deymof4867c42013-06-28 14:41:39 -0700336 }
337 }
338
339 // Show the current update over cellular network setting.
340 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800341 bool allowed;
342
343 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
344 LOG(ERROR) << "Error getting the update over cellular setting.";
345 return 1;
346 }
347
Alex Deymof4867c42013-06-28 14:41:39 -0700348 LOG(INFO) << "Current update over cellular network setting: "
349 << (allowed ? "ENABLED" : "DISABLED");
350 }
351
Alex Deymod63fab32016-10-06 15:40:49 -0700352 // Change/show the cohort hint.
353 bool set_cohort_hint =
354 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint");
355 if (set_cohort_hint) {
356 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\"";
357 if (!client_->SetCohortHint(FLAGS_cohort_hint)) {
358 LOG(ERROR) << "Error setting the cohort hint.";
359 return 1;
360 }
361 }
362
363 if (FLAGS_show_cohort_hint || set_cohort_hint) {
364 string cohort_hint;
365 if (!client_->GetCohortHint(&cohort_hint)) {
366 LOG(ERROR) << "Error getting the cohort hint.";
367 return 1;
368 }
369
370 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\"";
371 }
372
Chris Sosacb7fa882013-07-25 17:02:59 -0700373 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700374 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700375 return 1;
376 }
377
Alex Deymo5fdf7762013-07-17 20:01:40 -0700378 // Change the P2P enabled setting.
379 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700380 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700381 if (!enabled && FLAGS_p2p_update != "no") {
382 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
383 << "\". Please specify \"yes\" or \"no\".";
384 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800385 if (!client_->SetP2PUpdatePermission(enabled)) {
386 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
387 return 1;
388 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700389 }
390 }
391
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800392 // Show the rollback availability.
393 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800394 string rollback_partition;
395
396 if (!client_->GetRollbackPartition(&rollback_partition)) {
397 LOG(ERROR) << "Error while querying rollback partition availabilty.";
398 return 1;
399 }
400
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700401 bool can_rollback = true;
402 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700403 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700404 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700405 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700406 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700407 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700408
409 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700410 if (!can_rollback) {
411 return 1;
412 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800413 }
414
Alex Deymo5fdf7762013-07-17 20:01:40 -0700415 // Show the current P2P enabled setting.
416 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800417 bool enabled;
418
419 if (!client_->GetP2PUpdatePermission(&enabled)) {
420 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
421 return 1;
422 }
423
Alex Deymo5fdf7762013-07-17 20:01:40 -0700424 LOG(INFO) << "Current update using P2P setting: "
425 << (enabled ? "ENABLED" : "DISABLED");
426 }
427
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700428 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800429 if (!FLAGS_channel.empty()) {
430 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
431 LOG(ERROR) << "Error setting the channel.";
432 return 1;
433 }
434
435 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
436 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700437
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700438 // Show the current and target channels if requested.
439 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800440 string current_channel;
441 string target_channel;
442
443 if (!client_->GetChannel(&current_channel)) {
444 LOG(ERROR) << "Error getting the current channel.";
445 return 1;
446 }
447
448 if (!client_->GetTargetChannel(&target_channel)) {
449 LOG(ERROR) << "Error getting the target channel.";
450 return 1;
451 }
452
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700453 LOG(INFO) << "Current Channel: " << current_channel;
454
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700455 if (!target_channel.empty())
456 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900457 }
458
Chris Sosad317e402013-06-12 13:47:09 -0700459 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800460 !FLAGS_app_version.empty() |
461 !FLAGS_omaha_url.empty();
462 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700463
Chris Sosad317e402013-06-12 13:47:09 -0700464 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700465 LOG(ERROR) << "Incompatible flags specified with rollback."
466 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700467 return 1;
468 }
469
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700470 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700471 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800472 if (!client_->Rollback(FLAGS_powerwash)) {
473 LOG(ERROR) << "Rollback request failed.";
474 return 1;
475 }
Chris Sosad317e402013-06-12 13:47:09 -0700476 }
477
Darin Petkov58529db2010-08-13 09:19:47 -0700478 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700479 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700480 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700481 string app_version = FLAGS_app_version;
482 if (FLAGS_update && app_version.empty()) {
483 app_version = "ForcedUpdate";
484 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700485 }
Darin Petkov58529db2010-08-13 09:19:47 -0700486 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800487 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
488 FLAGS_interactive)) {
489 LOG(ERROR) << "Error checking for update.";
490 return 1;
491 }
Chris Sosa192449e2013-10-28 14:16:19 -0700492 }
Darin Petkov58529db2010-08-13 09:19:47 -0700493
Chris Sosa192449e2013-10-28 14:16:19 -0700494 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800495 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
496 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
497 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700498 LOG(ERROR) << "Multiple exclusive options selected. "
499 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700500 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700501 << "or --status.";
502 return 1;
503 }
504
505 if (FLAGS_status) {
506 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800507 if (!ShowStatus()) {
508 LOG(ERROR) << "Failed to query status";
509 return 1;
510 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700511 return 0;
512 }
Darin Petkov58529db2010-08-13 09:19:47 -0700513
Chris Sosa192449e2013-10-28 14:16:19 -0700514 if (FLAGS_follow) {
515 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800516 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800517 handlers_.emplace_back(handler);
518 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700519 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700520 }
521
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700522 if (FLAGS_watch_for_updates) {
523 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800524 auto handler = new WatchingStatusUpdateHandler();
525 handlers_.emplace_back(handler);
526 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700527 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700528 }
Darin Petkov58529db2010-08-13 09:19:47 -0700529
Darin Petkov296889c2010-07-23 16:20:54 -0700530 if (FLAGS_reboot) {
531 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800532 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700533 return 0;
534 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700535
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700536 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800537 string prev_version;
538
539 if (!client_->GetPrevVersion(&prev_version)) {
540 LOG(ERROR) << "Error getting previous version.";
541 } else {
542 LOG(INFO) << "Previous version = " << prev_version;
543 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700544 }
545
David Zeuthen9d73a722014-04-04 14:52:46 -0700546 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800547 int ret = GetNeedReboot();
548
549 if (ret == 1) {
550 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700551 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800552
553 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700554 }
555
556 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800557 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800558 handlers_.emplace_back(handler);
559 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700560 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700561 }
562
Shuqian Zhao29971732016-02-05 11:29:32 -0800563 if (FLAGS_last_attempt_error) {
564 int last_attempt_error;
565 if (!client_->GetLastAttemptError(&last_attempt_error)) {
566 LOG(ERROR) << "Error getting last attempt error.";
567 } else {
568 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800569 printf(
570 "ERROR_CODE=%i\n"
571 "ERROR_MESSAGE=%s\n",
572 last_attempt_error,
573 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800574 }
Alex Deymo9a069222016-03-02 16:14:42 -0800575 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800576
Alex Deymob3fa53b2016-04-18 19:57:58 -0700577 if (FLAGS_eol_status) {
578 int eol_status;
579 if (!client_->GetEolStatus(&eol_status)) {
580 LOG(ERROR) << "Error getting the end-of-life status.";
581 } else {
582 EolStatus eol_status_code = static_cast<EolStatus>(eol_status);
583 printf("EOL_STATUS=%s\n", EolStatusToString(eol_status_code));
584 }
585 }
586
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700587 return 0;
588}
Alex Deymo72aa0022015-06-19 21:16:49 -0700589
Alex Deymo690810b2016-01-19 16:11:40 -0800590void UpdateEngineClient::ProcessFlagsAndExit() {
591 int ret = ProcessFlags();
592 if (ret != kContinueRunning)
593 QuitWithExitCode(ret);
594}
595
Alex Deymo72aa0022015-06-19 21:16:49 -0700596} // namespace
597
598int main(int argc, char** argv) {
599 UpdateEngineClient client(argc, argv);
600 return client.Run();
601}