blob: 6ade12cfaafbb08f700d4ab0b77fea8769724970 [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>
Casey Dahlin19441412016-01-07 14:56:40 -080029#include <brillo/daemons/daemon.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/flag_helper.h>
Casey Dahlin87ab88e2015-12-16 17:58:05 -080031
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"
Casey Dahlin97c87052016-01-06 14:33:55 -080035#include "update_engine/status_update_handler.h"
36#include "update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080037#include "update_engine/update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070038
Shuqian Zhao29971732016-02-05 11:29:32 -080039using chromeos_update_engine::ErrorCode;
Alex Deymo9a069222016-03-02 16:14:42 -080040using chromeos_update_engine::UpdateStatusToString;
41using chromeos_update_engine::utils::ErrorCodeToString;
Darin Petkov5a7f5652010-07-22 21:40:09 -070042using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080043using std::unique_ptr;
Casey Dahlin97c87052016-01-06 14:33:55 -080044using std::vector;
Casey Dahlin97c87052016-01-06 14:33:55 -080045using update_engine::UpdateStatus;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070046
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070047namespace {
48
Alex Deymo72aa0022015-06-19 21:16:49 -070049// Constant to signal that we need to continue running the daemon after
50// initialization.
51const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070052
Casey Dahlin19441412016-01-07 14:56:40 -080053class UpdateEngineClient : public brillo::Daemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070054 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080055 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
Casey Dahline844c1a2015-12-16 14:30:58 -080056 }
57
Alex Deymo72aa0022015-06-19 21:16:49 -070058 ~UpdateEngineClient() override = default;
59
60 protected:
61 int OnInit() override {
Casey Dahlin19441412016-01-07 14:56:40 -080062 int ret = Daemon::OnInit();
Casey Dahlin97c87052016-01-06 14:33:55 -080063 if (ret != EX_OK) return ret;
Casey Dahlin19441412016-01-07 14:56:40 -080064
65 client_ = update_engine::UpdateEngineClient::CreateInstance();
66
67 if (!client_) {
68 LOG(ERROR) << "UpdateEngineService not available.";
69 return 1;
70 }
71
Alex Deymo690810b2016-01-19 16:11:40 -080072 // We can't call QuitWithExitCode from OnInit(), so we delay the execution
73 // of the ProcessFlags method after the Daemon initialization is done.
74 base::MessageLoop::current()->PostTask(
75 FROM_HERE,
76 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit,
77 base::Unretained(this)));
Alex Deymo72aa0022015-06-19 21:16:49 -070078 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070079 }
Alex Deymo72aa0022015-06-19 21:16:49 -070080
81 private:
Alex Deymo72aa0022015-06-19 21:16:49 -070082 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -080083 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -070084
Casey Dahlin87ab88e2015-12-16 17:58:05 -080085 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
86 // occurred, 2 if no reboot is needed.
87 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -070088
Alex Deymo72aa0022015-06-19 21:16:49 -070089 // Main method that parses and triggers all the actions based on the passed
Alex Deymo690810b2016-01-19 16:11:40 -080090 // flags. Returns the exit code of the program of kContinueRunning if it
91 // should not exit.
Alex Deymo72aa0022015-06-19 21:16:49 -070092 int ProcessFlags();
93
Alex Deymo690810b2016-01-19 16:11:40 -080094 // Processes the flags and exits the program accordingly.
95 void ProcessFlagsAndExit();
96
Alex Deymo72aa0022015-06-19 21:16:49 -070097 // Copy of argc and argv passed to main().
98 int argc_;
99 char** argv_;
100
Casey Dahline844c1a2015-12-16 14:30:58 -0800101 // Library-based client
102 unique_ptr<update_engine::UpdateEngineClient> client_;
103
Casey Dahlin97c87052016-01-06 14:33:55 -0800104 // Pointers to handlers for cleanup
105 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_;
106
Alex Deymo72aa0022015-06-19 21:16:49 -0700107 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
108};
109
Casey Dahlin97c87052016-01-06 14:33:55 -0800110class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler {
111 public:
112 ~ExitingStatusUpdateHandler() override = default;
113
114 void IPCError(const string& error) override;
115};
116
117void ExitingStatusUpdateHandler::IPCError(const string& error) {
118 LOG(ERROR) << error;
119 exit(1);
120}
121
122class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler {
123 public:
124 ~WatchingStatusUpdateHandler() override = default;
125
Alex Deymo690810b2016-01-19 16:11:40 -0800126 void HandleStatusUpdate(int64_t last_checked_time,
127 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800128 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800129 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800130 int64_t new_size) override;
131};
132
133void WatchingStatusUpdateHandler::HandleStatusUpdate(
134 int64_t last_checked_time, double progress, UpdateStatus current_operation,
135 const string& new_version, int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700136 LOG(INFO) << "Got status update:";
137 LOG(INFO) << " last_checked_time: " << last_checked_time;
138 LOG(INFO) << " progress: " << progress;
Casey Dahlin97c87052016-01-06 14:33:55 -0800139 LOG(INFO) << " current_operation: "
140 << UpdateStatusToString(current_operation);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700141 LOG(INFO) << " new_version: " << new_version;
142 LOG(INFO) << " new_size: " << new_size;
143}
144
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800145bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700146 int64_t last_checked_time = 0;
147 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800148 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700149 string new_version;
150 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700151
Casey Dahlin97c87052016-01-06 14:33:55 -0800152 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
153 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800154 return false;
155 }
156
Casey Dahlin97c87052016-01-06 14:33:55 -0800157 printf("LAST_CHECKED_TIME=%" PRIi64
158 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700159 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
Casey Dahlin97c87052016-01-06 14:33:55 -0800160 last_checked_time, progress, UpdateStatusToString(current_op),
161 new_version.c_str(), new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800162
163 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700164}
165
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800166int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700167 int64_t last_checked_time = 0;
168 double progress = 0.0;
Casey Dahlin97c87052016-01-06 14:33:55 -0800169 UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700170 string new_version;
171 int64_t new_size = 0;
172
Casey Dahlin97c87052016-01-06 14:33:55 -0800173 if (!client_->GetStatus(&last_checked_time, &progress, &current_op,
174 &new_version, &new_size)) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800175 return 1;
176 }
177
Casey Dahlin97c87052016-01-06 14:33:55 -0800178 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800179 return 0;
180 }
181
182 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700183}
184
Casey Dahlin97c87052016-01-06 14:33:55 -0800185class UpdateWaitHandler : public ExitingStatusUpdateHandler {
186 public:
Alex Deymo9a069222016-03-02 16:14:42 -0800187 explicit UpdateWaitHandler(bool exit_on_error,
188 update_engine::UpdateEngineClient* client)
189 : exit_on_error_(exit_on_error), client_(client) {}
Casey Dahlin97c87052016-01-06 14:33:55 -0800190
191 ~UpdateWaitHandler() override = default;
192
Alex Deymo690810b2016-01-19 16:11:40 -0800193 void HandleStatusUpdate(int64_t last_checked_time,
194 double progress,
Casey Dahlin97c87052016-01-06 14:33:55 -0800195 UpdateStatus current_operation,
Alex Deymo690810b2016-01-19 16:11:40 -0800196 const string& new_version,
Casey Dahlin97c87052016-01-06 14:33:55 -0800197 int64_t new_size) override;
198
199 private:
200 bool exit_on_error_;
Alex Deymo9a069222016-03-02 16:14:42 -0800201 update_engine::UpdateEngineClient* client_;
Casey Dahlin97c87052016-01-06 14:33:55 -0800202};
203
204void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */,
205 double /* progress */,
206 UpdateStatus current_operation,
207 const string& /* new_version */,
208 int64_t /* new_size */) {
209 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) {
Alex Deymo9a069222016-03-02 16:14:42 -0800210 int last_attempt_error;
211 ErrorCode code = ErrorCode::kSuccess;
212 if (client_ && client_->GetLastAttemptError(&last_attempt_error))
213 code = static_cast<ErrorCode>(last_attempt_error);
214
215 LOG(ERROR) << "Update failed, current operation is "
216 << UpdateStatusToString(current_operation)
217 << ", last error code is " << ErrorCodeToString(code) << "("
218 << last_attempt_error << ")";
Darin Petkov58529db2010-08-13 09:19:47 -0700219 exit(1);
220 }
Casey Dahlin97c87052016-01-06 14:33:55 -0800221 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) {
Darin Petkov58529db2010-08-13 09:19:47 -0700222 LOG(INFO) << "Update succeeded -- reboot needed.";
223 exit(0);
224 }
Darin Petkov58529db2010-08-13 09:19:47 -0700225}
226
Alex Deymo72aa0022015-06-19 21:16:49 -0700227int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700228 DEFINE_string(app_version, "", "Force the current app version.");
229 DEFINE_string(channel, "",
230 "Set the target channel. The device will be powerwashed if the "
231 "target channel is more stable than the current channel unless "
232 "--nopowerwash is specified.");
233 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800234 DEFINE_bool(follow, false,
235 "Wait for any update operations to complete."
Steve Fung97b6f5a2014-10-07 12:39:51 -0700236 "Exit status is 0 if the update succeeded, and 1 otherwise.");
237 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
238 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
239 DEFINE_string(p2p_update, "",
240 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
241 " sharing.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800242 DEFINE_bool(powerwash, true,
243 "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.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800246 DEFINE_bool(is_reboot_needed, false,
247 "Exit status 0 if reboot is needed, "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700248 "2 if reboot is not needed or 1 if an error occurred.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800249 DEFINE_bool(block_until_reboot_is_needed, false,
250 "Blocks until reboot is "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700251 "needed. Returns non-zero exit status if an error occurred.");
252 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800253 DEFINE_bool(rollback, false,
254 "Perform a rollback to the previous partition. The device will "
255 "be powerwashed unless --nopowerwash is specified.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800256 DEFINE_bool(can_rollback, false,
257 "Shows whether rollback partition "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700258 "is available.");
259 DEFINE_bool(show_channel, false, "Show the current and target channels.");
260 DEFINE_bool(show_p2p_update, false,
261 "Show the current setting for peer-to-peer update sharing.");
262 DEFINE_bool(show_update_over_cellular, false,
263 "Show the current setting for updates over cellular networks.");
264 DEFINE_bool(status, false, "Print the status to stdout.");
Casey Dahlin97c87052016-01-06 14:33:55 -0800265 DEFINE_bool(update, false,
266 "Forces an update and waits for it to complete. "
Steve Fung97b6f5a2014-10-07 12:39:51 -0700267 "Implies --follow.");
268 DEFINE_string(update_over_cellular, "",
269 "Enables (\"yes\") or disables (\"no\") the updates over "
270 "cellular networks.");
271 DEFINE_bool(watch_for_updates, false,
272 "Listen for status updates and print them to the screen.");
273 DEFINE_bool(prev_version, false,
274 "Show the previous OS version used before the update reboot.");
Shuqian Zhao29971732016-02-05 11:29:32 -0800275 DEFINE_bool(last_attempt_error, false, "Show the last attempt error.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700276
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700277 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700278 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700279 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700280
Alex Deymo8ce80d62015-01-27 15:10:43 -0800281 // Ensure there are no positional arguments.
Alex Deymo690810b2016-01-19 16:11:40 -0800282 const vector<string> positional_args =
Alex Deymo8ce80d62015-01-27 15:10:43 -0800283 base::CommandLine::ForCurrentProcess()->GetArgs();
284 if (!positional_args.empty()) {
285 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
286 << "'. If you want to pass a value to a flag, pass it as "
287 "--flag=value.";
288 return 1;
289 }
290
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700291 // Update the status if requested.
292 if (FLAGS_reset_status) {
293 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800294
295 if (client_->ResetStatus()) {
296 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
297 "run:\n"
298 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
299 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
300 } else {
301 LOG(ERROR) << "ResetStatus failed";
302 return 1;
303 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700304 }
Darin Petkov58529db2010-08-13 09:19:47 -0700305
Alex Deymof4867c42013-06-28 14:41:39 -0700306 // Changes the current update over cellular network setting.
307 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700308 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700309 if (!allowed && FLAGS_update_over_cellular != "no") {
310 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
311 << "\". Please specify \"yes\" or \"no\".";
312 } else {
Casey Dahlin97c87052016-01-06 14:33:55 -0800313 if (!client_->SetUpdateOverCellularPermission(allowed)) {
Casey Dahlinef361132015-12-17 13:02:37 -0800314 LOG(ERROR) << "Error setting the update over cellular setting.";
315 return 1;
316 }
Alex Deymof4867c42013-06-28 14:41:39 -0700317 }
318 }
319
320 // Show the current update over cellular network setting.
321 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800322 bool allowed;
323
324 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
325 LOG(ERROR) << "Error getting the update over cellular setting.";
326 return 1;
327 }
328
Alex Deymof4867c42013-06-28 14:41:39 -0700329 LOG(INFO) << "Current update over cellular network setting: "
330 << (allowed ? "ENABLED" : "DISABLED");
331 }
332
Chris Sosacb7fa882013-07-25 17:02:59 -0700333 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700334 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700335 return 1;
336 }
337
Alex Deymo5fdf7762013-07-17 20:01:40 -0700338 // Change the P2P enabled setting.
339 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700340 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700341 if (!enabled && FLAGS_p2p_update != "no") {
342 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
343 << "\". Please specify \"yes\" or \"no\".";
344 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800345 if (!client_->SetP2PUpdatePermission(enabled)) {
346 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
347 return 1;
348 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700349 }
350 }
351
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800352 // Show the rollback availability.
353 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800354 string rollback_partition;
355
356 if (!client_->GetRollbackPartition(&rollback_partition)) {
357 LOG(ERROR) << "Error while querying rollback partition availabilty.";
358 return 1;
359 }
360
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700361 bool can_rollback = true;
362 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700363 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700364 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700365 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700366 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700367 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700368
369 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700370 if (!can_rollback) {
371 return 1;
372 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800373 }
374
Alex Deymo5fdf7762013-07-17 20:01:40 -0700375 // Show the current P2P enabled setting.
376 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800377 bool enabled;
378
379 if (!client_->GetP2PUpdatePermission(&enabled)) {
380 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
381 return 1;
382 }
383
Alex Deymo5fdf7762013-07-17 20:01:40 -0700384 LOG(INFO) << "Current update using P2P setting: "
385 << (enabled ? "ENABLED" : "DISABLED");
386 }
387
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700388 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800389 if (!FLAGS_channel.empty()) {
390 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
391 LOG(ERROR) << "Error setting the channel.";
392 return 1;
393 }
394
395 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
396 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700397
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700398 // Show the current and target channels if requested.
399 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800400 string current_channel;
401 string target_channel;
402
403 if (!client_->GetChannel(&current_channel)) {
404 LOG(ERROR) << "Error getting the current channel.";
405 return 1;
406 }
407
408 if (!client_->GetTargetChannel(&target_channel)) {
409 LOG(ERROR) << "Error getting the target channel.";
410 return 1;
411 }
412
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700413 LOG(INFO) << "Current Channel: " << current_channel;
414
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700415 if (!target_channel.empty())
416 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900417 }
418
Chris Sosad317e402013-06-12 13:47:09 -0700419 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
Casey Dahlin97c87052016-01-06 14:33:55 -0800420 !FLAGS_app_version.empty() |
421 !FLAGS_omaha_url.empty();
422 if (FLAGS_update) FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700423
Chris Sosad317e402013-06-12 13:47:09 -0700424 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700425 LOG(ERROR) << "Incompatible flags specified with rollback."
426 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700427 return 1;
428 }
429
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700430 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700431 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800432 if (!client_->Rollback(FLAGS_powerwash)) {
433 LOG(ERROR) << "Rollback request failed.";
434 return 1;
435 }
Chris Sosad317e402013-06-12 13:47:09 -0700436 }
437
Darin Petkov58529db2010-08-13 09:19:47 -0700438 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700439 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700440 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700441 string app_version = FLAGS_app_version;
442 if (FLAGS_update && app_version.empty()) {
443 app_version = "ForcedUpdate";
444 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700445 }
Darin Petkov58529db2010-08-13 09:19:47 -0700446 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800447 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
448 FLAGS_interactive)) {
449 LOG(ERROR) << "Error checking for update.";
450 return 1;
451 }
Chris Sosa192449e2013-10-28 14:16:19 -0700452 }
Darin Petkov58529db2010-08-13 09:19:47 -0700453
Chris Sosa192449e2013-10-28 14:16:19 -0700454 // These final options are all mutually exclusive with one another.
Casey Dahlin97c87052016-01-06 14:33:55 -0800455 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status +
456 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed >
457 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700458 LOG(ERROR) << "Multiple exclusive options selected. "
459 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700460 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700461 << "or --status.";
462 return 1;
463 }
464
465 if (FLAGS_status) {
466 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800467 if (!ShowStatus()) {
468 LOG(ERROR) << "Failed to query status";
469 return 1;
470 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700471 return 0;
472 }
Darin Petkov58529db2010-08-13 09:19:47 -0700473
Chris Sosa192449e2013-10-28 14:16:19 -0700474 if (FLAGS_follow) {
475 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo9a069222016-03-02 16:14:42 -0800476 auto handler = new UpdateWaitHandler(true, client_.get());
Casey Dahlin97c87052016-01-06 14:33:55 -0800477 handlers_.emplace_back(handler);
478 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700479 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700480 }
481
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700482 if (FLAGS_watch_for_updates) {
483 LOG(INFO) << "Watching for status updates.";
Casey Dahlin97c87052016-01-06 14:33:55 -0800484 auto handler = new WatchingStatusUpdateHandler();
485 handlers_.emplace_back(handler);
486 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700487 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700488 }
Darin Petkov58529db2010-08-13 09:19:47 -0700489
Darin Petkov296889c2010-07-23 16:20:54 -0700490 if (FLAGS_reboot) {
491 LOG(INFO) << "Requesting a reboot...";
Casey Dahlinef361132015-12-17 13:02:37 -0800492 client_->RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700493 return 0;
494 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700495
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700496 if (FLAGS_prev_version) {
Casey Dahlinef361132015-12-17 13:02:37 -0800497 string prev_version;
498
499 if (!client_->GetPrevVersion(&prev_version)) {
500 LOG(ERROR) << "Error getting previous version.";
501 } else {
502 LOG(INFO) << "Previous version = " << prev_version;
503 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700504 }
505
David Zeuthen9d73a722014-04-04 14:52:46 -0700506 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800507 int ret = GetNeedReboot();
508
509 if (ret == 1) {
510 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700511 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800512
513 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700514 }
515
516 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo9a069222016-03-02 16:14:42 -0800517 auto handler = new UpdateWaitHandler(false, nullptr);
Casey Dahlin97c87052016-01-06 14:33:55 -0800518 handlers_.emplace_back(handler);
519 client_->RegisterStatusUpdateHandler(handler);
Alex Deymo72aa0022015-06-19 21:16:49 -0700520 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700521 }
522
Shuqian Zhao29971732016-02-05 11:29:32 -0800523 if (FLAGS_last_attempt_error) {
524 int last_attempt_error;
525 if (!client_->GetLastAttemptError(&last_attempt_error)) {
526 LOG(ERROR) << "Error getting last attempt error.";
527 } else {
528 ErrorCode code = static_cast<ErrorCode>(last_attempt_error);
Alex Deymo9a069222016-03-02 16:14:42 -0800529 printf(
530 "ERROR_CODE=%i\n"
531 "ERROR_MESSAGE=%s\n",
532 last_attempt_error,
533 ErrorCodeToString(code).c_str());
Shuqian Zhao29971732016-02-05 11:29:32 -0800534 }
Alex Deymo9a069222016-03-02 16:14:42 -0800535 }
Shuqian Zhao29971732016-02-05 11:29:32 -0800536
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700537 return 0;
538}
Alex Deymo72aa0022015-06-19 21:16:49 -0700539
Alex Deymo690810b2016-01-19 16:11:40 -0800540void UpdateEngineClient::ProcessFlagsAndExit() {
541 int ret = ProcessFlags();
542 if (ret != kContinueRunning)
543 QuitWithExitCode(ret);
544}
545
Alex Deymo72aa0022015-06-19 21:16:49 -0700546} // namespace
547
548int main(int argc, char** argv) {
549 UpdateEngineClient client(argc, argv);
550 return client.Run();
551}