blob: 141380601d6f093cce64a266c72c5fbd102e0329 [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>
23
Alex Deymo72aa0022015-06-19 21:16:49 -070024#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080025#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070026#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070027#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070028#include <brillo/daemons/dbus_daemon.h>
29#include <brillo/flag_helper.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070030#include <dbus/bus.h>
Casey Dahline844c1a2015-12-16 14:30:58 -080031#include <update_engine/client.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070032#include <update_engine/dbus-constants.h>
33#include <update_engine/dbus-proxies.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070034
Darin Petkov5a7f5652010-07-22 21:40:09 -070035using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080036using std::unique_ptr;
Alex Deymod6deb1d2015-08-28 15:54:37 -070037using update_engine::kAttemptUpdateFlagNonInteractive;
38using update_engine::kUpdateEngineServiceName;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070039
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070040namespace {
41
Alex Deymo72aa0022015-06-19 21:16:49 -070042// Constant to signal that we need to continue running the daemon after
43// initialization.
44const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070045
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070046class UpdateEngineClient : public brillo::DBusDaemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070047 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080048 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
49 client_ = update_engine::UpdateEngineClient::CreateInstance();
50 }
51
Alex Deymo72aa0022015-06-19 21:16:49 -070052 ~UpdateEngineClient() override = default;
53
54 protected:
55 int OnInit() override {
56 int ret = DBusDaemon::OnInit();
57 if (ret != EX_OK)
58 return ret;
59 if (!InitProxy())
60 return 1;
Alex Deymo13e0dd62015-06-30 23:11:44 -070061 // Wait for the UpdateEngine to be available or timeout.
62 proxy_->GetObjectProxy()->WaitForServiceToBeAvailable(
63 base::Bind(&UpdateEngineClient::OnServiceAvailable,
64 base::Unretained(this)));
65 base::MessageLoop::current()->PostDelayedTask(
66 FROM_HERE,
67 base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout,
68 base::Unretained(this)),
69 base::TimeDelta::FromSeconds(10));
Alex Deymo72aa0022015-06-19 21:16:49 -070070 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070071 }
Alex Deymo72aa0022015-06-19 21:16:49 -070072
73 private:
74 bool InitProxy();
75
Alex Deymo13e0dd62015-06-30 23:11:44 -070076 // Callback called when the UpdateEngine service becomes available.
77 void OnServiceAvailable(bool service_is_available);
78
79 // Callback called when the UpdateEngine service doesn't become available
80 // after a timeout.
81 void OnServiceAvailableTimeout();
82
83
Alex Deymo72aa0022015-06-19 21:16:49 -070084 // Callback called when a StatusUpdate signal is received.
85 void OnStatusUpdateSignal(int64_t last_checked_time,
86 double progress,
87 const string& current_operation,
88 const string& new_version,
89 int64_t new_size);
90 // Callback called when the OnStatusUpdateSignal() handler is registered.
91 void OnStatusUpdateSignalRegistration(const string& interface,
92 const string& signal_name,
93 bool success);
94
95 // Registers a callback that prints on stderr the received StatusUpdate
96 // signals.
97 // The daemon should continue running for this to work.
98 void WatchForUpdates();
99
Alex Deymo72aa0022015-06-19 21:16:49 -0700100 // Show the status of the update engine in stdout.
101 // Blocking call. Exits the program with error 1 in case of an error.
102 void ShowStatus();
103
104 // Return the current operation status, such as UPDATE_STATUS_IDLE.
105 // Blocking call. Exits the program with error 1 in case of an error.
106 string GetCurrentOperation();
107
108 void Rollback(bool rollback);
109 string GetRollbackPartition();
Alex Deymo72aa0022015-06-19 21:16:49 -0700110 void CheckForUpdates(const string& app_version,
111 const string& omaha_url,
112 bool interactive);
113
114 // Reboot the device if a reboot is needed.
115 // Blocking call. Ignores failures.
116 void RebootIfNeeded();
117
118 // Getter and setter for the target channel. If |get_current_channel| is true,
119 // the current channel instead of the target channel will be returned.
120 // Blocking call. Exits the program with error 1 in case of an error.
121 void SetTargetChannel(const string& target_channel, bool allow_powerwash);
122 string GetChannel(bool get_current_channel);
123
124 // Getter and setter for the updates over cellular connections.
125 // Blocking call. Exits the program with error 1 in case of an error.
126 void SetUpdateOverCellularPermission(bool allowed);
127 bool GetUpdateOverCellularPermission();
128
129 // Getter and setter for the updates from P2P permission.
130 // Blocking call. Exits the program with error 1 in case of an error.
131 void SetP2PUpdatePermission(bool enabled);
132 bool GetP2PUpdatePermission();
133
134 // This is similar to watching for updates but rather than registering
135 // a signal watch, actively poll the daemon just in case it stops
136 // sending notifications.
137 void WaitForUpdateComplete();
138 void OnUpdateCompleteCheck(int64_t last_checked_time,
139 double progress,
140 const string& current_operation,
141 const string& new_version,
142 int64_t new_size);
143
144 // Blocking call. Exits the program with error 1 in case of an error.
145 void ShowPrevVersion();
146
147 // Returns whether the current status is such that a reboot is needed.
148 // Blocking call. Exits the program with error 1 in case of an error.
149 bool GetIsRebootNeeded();
150
151 // Blocks until a reboot is needed. If the reboot is needed, exits the program
152 // with 0. Otherwise it exits the program with 1 if an error occurs before
153 // the reboot is needed.
154 void WaitForRebootNeeded();
155 void OnRebootNeededCheck(int64_t last_checked_time,
156 double progress,
157 const string& current_operation,
158 const string& new_version,
159 int64_t new_size);
160 // Callback called when the OnRebootNeededCheck() handler is registered. This
161 // is useful to check at this point if the reboot is needed, without loosing
162 // any StatusUpdate signals and avoiding the race condition.
163 void OnRebootNeededCheckRegistration(const string& interface,
164 const string& signal_name,
165 bool success);
166
167 // Main method that parses and triggers all the actions based on the passed
168 // flags.
169 int ProcessFlags();
170
171 // DBus Proxy to the update_engine daemon object used for all the calls.
172 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
173
174 // Copy of argc and argv passed to main().
175 int argc_;
176 char** argv_;
177
Casey Dahline844c1a2015-12-16 14:30:58 -0800178 // Library-based client
179 unique_ptr<update_engine::UpdateEngineClient> client_;
180
Alex Deymo13e0dd62015-06-30 23:11:44 -0700181 // Tell whether the UpdateEngine service is available after startup.
182 bool service_is_available_{false};
183
Alex Deymo72aa0022015-06-19 21:16:49 -0700184 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
185};
186
Alex Deymo72aa0022015-06-19 21:16:49 -0700187bool UpdateEngineClient::InitProxy() {
188 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
189
190 if (!proxy_->GetObjectProxy()) {
191 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
192 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800193 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700194 return true;
195}
196
Alex Deymo13e0dd62015-06-30 23:11:44 -0700197void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
198 service_is_available_ = service_is_available;
199 if (!service_is_available) {
200 LOG(ERROR) << "UpdateEngineService not available.";
201 QuitWithExitCode(-1);
202 }
203 int ret = ProcessFlags();
204 if (ret != kContinueRunning)
205 QuitWithExitCode(ret);
206}
207
208void UpdateEngineClient::OnServiceAvailableTimeout() {
209 if (!service_is_available_) {
210 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
211 "daemon running?";
212 QuitWithExitCode(-1);
213 }
214}
215
Alex Deymo72aa0022015-06-19 21:16:49 -0700216void UpdateEngineClient::OnStatusUpdateSignal(
217 int64_t last_checked_time,
218 double progress,
219 const string& current_operation,
220 const string& new_version,
221 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700222 LOG(INFO) << "Got status update:";
223 LOG(INFO) << " last_checked_time: " << last_checked_time;
224 LOG(INFO) << " progress: " << progress;
225 LOG(INFO) << " current_operation: " << current_operation;
226 LOG(INFO) << " new_version: " << new_version;
227 LOG(INFO) << " new_size: " << new_size;
228}
229
Alex Deymo72aa0022015-06-19 21:16:49 -0700230void UpdateEngineClient::OnStatusUpdateSignalRegistration(
231 const string& interface,
232 const string& signal_name,
233 bool success) {
234 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
235 << signal_name << ", " << success << ");";
236 if (!success) {
237 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
238 exit(1);
239 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700240}
241
Alex Deymo72aa0022015-06-19 21:16:49 -0700242void UpdateEngineClient::WatchForUpdates() {
243 proxy_->RegisterStatusUpdateSignalHandler(
244 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
245 base::Unretained(this)),
246 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
247 base::Unretained(this)));
248}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700249
Alex Deymo72aa0022015-06-19 21:16:49 -0700250void UpdateEngineClient::ShowStatus() {
251 int64_t last_checked_time = 0;
252 double progress = 0.0;
253 string current_op;
254 string new_version;
255 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700256
Alex Deymo72aa0022015-06-19 21:16:49 -0700257 bool ret = proxy_->GetStatus(
258 &last_checked_time, &progress, &current_op, &new_version, &new_size,
259 nullptr);
260 CHECK(ret) << "GetStatus() failed";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700261 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
262 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
263 last_checked_time,
264 progress,
Alex Deymo72aa0022015-06-19 21:16:49 -0700265 current_op.c_str(),
266 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700267 new_size);
Alex Deymo72aa0022015-06-19 21:16:49 -0700268}
269
270string UpdateEngineClient::GetCurrentOperation() {
271 int64_t last_checked_time = 0;
272 double progress = 0.0;
273 string current_op;
274 string new_version;
275 int64_t new_size = 0;
276
277 bool ret = proxy_->GetStatus(
278 &last_checked_time, &progress, &current_op, &new_version, &new_size,
279 nullptr);
280 CHECK(ret) << "GetStatus() failed";
281 return current_op;
282}
283
284void UpdateEngineClient::Rollback(bool rollback) {
285 bool ret = proxy_->AttemptRollback(rollback, nullptr);
286 CHECK(ret) << "Rollback request failed.";
287}
288
289string UpdateEngineClient::GetRollbackPartition() {
290 string rollback_partition;
291 bool ret = proxy_->GetRollbackPartition(&rollback_partition, nullptr);
292 CHECK(ret) << "Error while querying rollback partition availabilty.";
293 return rollback_partition;
294}
295
Alex Deymo72aa0022015-06-19 21:16:49 -0700296void UpdateEngineClient::CheckForUpdates(const string& app_version,
297 const string& omaha_url,
298 bool interactive) {
299 int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
300 bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
301 nullptr);
302 CHECK(ret) << "Error checking for update.";
303}
304
305void UpdateEngineClient::RebootIfNeeded() {
306 bool ret = proxy_->RebootIfNeeded(nullptr);
307 if (!ret) {
308 // Reboot error code doesn't necessarily mean that a reboot
309 // failed. For example, D-Bus may be shutdown before we receive the
310 // result.
311 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700312 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700313}
314
Alex Deymo72aa0022015-06-19 21:16:49 -0700315void UpdateEngineClient::SetTargetChannel(const string& target_channel,
316 bool allow_powerwash) {
317 bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
318 CHECK(ret) << "Error setting the channel.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700319 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700320}
321
Alex Deymo72aa0022015-06-19 21:16:49 -0700322string UpdateEngineClient::GetChannel(bool get_current_channel) {
323 string channel;
324 bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
325 CHECK(ret) << "Error getting the channel.";
326 return channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900327}
328
Alex Deymo72aa0022015-06-19 21:16:49 -0700329void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
330 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
331 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700332}
333
Alex Deymo72aa0022015-06-19 21:16:49 -0700334bool UpdateEngineClient::GetUpdateOverCellularPermission() {
335 bool allowed;
336 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
337 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700338 return allowed;
339}
340
Alex Deymo72aa0022015-06-19 21:16:49 -0700341void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
342 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
343 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700344}
345
Alex Deymo72aa0022015-06-19 21:16:49 -0700346bool UpdateEngineClient::GetP2PUpdatePermission() {
347 bool enabled;
348 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
349 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700350 return enabled;
351}
352
Alex Deymo72aa0022015-06-19 21:16:49 -0700353void UpdateEngineClient::OnUpdateCompleteCheck(
354 int64_t /* last_checked_time */,
355 double /* progress */,
356 const string& current_operation,
357 const string& /* new_version */,
358 int64_t /* new_size */) {
359 if (current_operation == update_engine::kUpdateStatusIdle) {
360 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700361 exit(1);
362 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700363 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700364 LOG(INFO) << "Update succeeded -- reboot needed.";
365 exit(0);
366 }
Darin Petkov58529db2010-08-13 09:19:47 -0700367}
368
Alex Deymo72aa0022015-06-19 21:16:49 -0700369void UpdateEngineClient::WaitForUpdateComplete() {
370 proxy_->RegisterStatusUpdateSignalHandler(
371 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
372 base::Unretained(this)),
373 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
374 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700375}
376
Alex Deymo72aa0022015-06-19 21:16:49 -0700377void UpdateEngineClient::ShowPrevVersion() {
378 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700379
Alex Deymo72aa0022015-06-19 21:16:49 -0700380 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
381 if (!ret) {
382 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700383 } else {
384 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700385 }
386}
387
Alex Deymo72aa0022015-06-19 21:16:49 -0700388bool UpdateEngineClient::GetIsRebootNeeded() {
389 return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
David Zeuthen9d73a722014-04-04 14:52:46 -0700390}
391
Alex Deymo72aa0022015-06-19 21:16:49 -0700392void UpdateEngineClient::OnRebootNeededCheck(
393 int64_t /* last_checked_time */,
394 double /* progress */,
395 const string& current_operation,
396 const string& /* new_version */,
397 int64_t /* new_size */) {
398 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
399 LOG(INFO) << "Reboot needed.";
400 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700401 }
402}
403
Alex Deymo72aa0022015-06-19 21:16:49 -0700404void UpdateEngineClient::OnRebootNeededCheckRegistration(
405 const string& interface,
406 const string& signal_name,
407 bool success) {
408 if (GetIsRebootNeeded())
409 exit(0);
410 if (!success) {
411 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
412 exit(1);
413 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700414}
415
416// Blocks until a reboot is needed. Returns true if waiting succeeded,
417// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700418void UpdateEngineClient::WaitForRebootNeeded() {
419 proxy_->RegisterStatusUpdateSignalHandler(
420 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
421 base::Unretained(this)),
422 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
423 base::Unretained(this)));
424 if (GetIsRebootNeeded())
425 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700426}
427
Alex Deymo72aa0022015-06-19 21:16:49 -0700428int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700429 DEFINE_string(app_version, "", "Force the current app version.");
430 DEFINE_string(channel, "",
431 "Set the target channel. The device will be powerwashed if the "
432 "target channel is more stable than the current channel unless "
433 "--nopowerwash is specified.");
434 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
435 DEFINE_bool(follow, false, "Wait for any update operations to complete."
436 "Exit status is 0 if the update succeeded, and 1 otherwise.");
437 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
438 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
439 DEFINE_string(p2p_update, "",
440 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
441 " sharing.");
442 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
443 "do a powerwash or allow it respectively.");
444 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
445 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
446 "2 if reboot is not needed or 1 if an error occurred.");
447 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
448 "needed. Returns non-zero exit status if an error occurred.");
449 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800450 DEFINE_bool(rollback, false,
451 "Perform a rollback to the previous partition. The device will "
452 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700453 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
454 "is available.");
455 DEFINE_bool(show_channel, false, "Show the current and target channels.");
456 DEFINE_bool(show_p2p_update, false,
457 "Show the current setting for peer-to-peer update sharing.");
458 DEFINE_bool(show_update_over_cellular, false,
459 "Show the current setting for updates over cellular networks.");
460 DEFINE_bool(status, false, "Print the status to stdout.");
461 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
462 "Implies --follow.");
463 DEFINE_string(update_over_cellular, "",
464 "Enables (\"yes\") or disables (\"no\") the updates over "
465 "cellular networks.");
466 DEFINE_bool(watch_for_updates, false,
467 "Listen for status updates and print them to the screen.");
468 DEFINE_bool(prev_version, false,
469 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700470
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700471 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700472 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700473 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700474
Alex Deymo8ce80d62015-01-27 15:10:43 -0800475 // Ensure there are no positional arguments.
476 const std::vector<string> positional_args =
477 base::CommandLine::ForCurrentProcess()->GetArgs();
478 if (!positional_args.empty()) {
479 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
480 << "'. If you want to pass a value to a flag, pass it as "
481 "--flag=value.";
482 return 1;
483 }
484
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700485 // Update the status if requested.
486 if (FLAGS_reset_status) {
487 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800488
489 if (client_->ResetStatus()) {
490 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
491 "run:\n"
492 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
493 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
494 } else {
495 LOG(ERROR) << "ResetStatus failed";
496 return 1;
497 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700498 }
Darin Petkov58529db2010-08-13 09:19:47 -0700499
Alex Deymof4867c42013-06-28 14:41:39 -0700500 // Changes the current update over cellular network setting.
501 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700502 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700503 if (!allowed && FLAGS_update_over_cellular != "no") {
504 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
505 << "\". Please specify \"yes\" or \"no\".";
506 } else {
507 SetUpdateOverCellularPermission(allowed);
508 }
509 }
510
511 // Show the current update over cellular network setting.
512 if (FLAGS_show_update_over_cellular) {
513 bool allowed = GetUpdateOverCellularPermission();
514 LOG(INFO) << "Current update over cellular network setting: "
515 << (allowed ? "ENABLED" : "DISABLED");
516 }
517
Chris Sosacb7fa882013-07-25 17:02:59 -0700518 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700519 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700520 return 1;
521 }
522
Alex Deymo5fdf7762013-07-17 20:01:40 -0700523 // Change the P2P enabled setting.
524 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700525 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700526 if (!enabled && FLAGS_p2p_update != "no") {
527 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
528 << "\". Please specify \"yes\" or \"no\".";
529 } else {
530 SetP2PUpdatePermission(enabled);
531 }
532 }
533
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800534 // Show the rollback availability.
535 if (FLAGS_can_rollback) {
Alex Deymof329b932014-10-30 01:37:48 -0700536 string rollback_partition = GetRollbackPartition();
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700537 bool can_rollback = true;
538 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700539 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700540 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700541 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700542 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700543 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700544
545 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700546 if (!can_rollback) {
547 return 1;
548 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800549 }
550
Alex Deymo5fdf7762013-07-17 20:01:40 -0700551 // Show the current P2P enabled setting.
552 if (FLAGS_show_p2p_update) {
553 bool enabled = GetP2PUpdatePermission();
554 LOG(INFO) << "Current update using P2P setting: "
555 << (enabled ? "ENABLED" : "DISABLED");
556 }
557
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700558 // First, update the target channel if requested.
559 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700560 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700561
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700562 // Show the current and target channels if requested.
563 if (FLAGS_show_channel) {
564 string current_channel = GetChannel(true);
565 LOG(INFO) << "Current Channel: " << current_channel;
566
567 string target_channel = GetChannel(false);
568 if (!target_channel.empty())
569 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900570 }
571
Chris Sosad317e402013-06-12 13:47:09 -0700572 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
573 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700574 if (FLAGS_update)
575 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700576
Chris Sosad317e402013-06-12 13:47:09 -0700577 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700578 LOG(ERROR) << "Incompatible flags specified with rollback."
579 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700580 return 1;
581 }
582
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700583 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700584 LOG(INFO) << "Requesting rollback.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700585 Rollback(FLAGS_powerwash);
Chris Sosad317e402013-06-12 13:47:09 -0700586 }
587
Darin Petkov58529db2010-08-13 09:19:47 -0700588 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700589 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700590 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700591 string app_version = FLAGS_app_version;
592 if (FLAGS_update && app_version.empty()) {
593 app_version = "ForcedUpdate";
594 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700595 }
Darin Petkov58529db2010-08-13 09:19:47 -0700596 LOG(INFO) << "Initiating update check and install.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700597 CheckForUpdates(app_version, FLAGS_omaha_url, FLAGS_interactive);
Chris Sosa192449e2013-10-28 14:16:19 -0700598 }
Darin Petkov58529db2010-08-13 09:19:47 -0700599
Chris Sosa192449e2013-10-28 14:16:19 -0700600 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700601 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
602 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700603 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700604 LOG(ERROR) << "Multiple exclusive options selected. "
605 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700606 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700607 << "or --status.";
608 return 1;
609 }
610
611 if (FLAGS_status) {
612 LOG(INFO) << "Querying Update Engine status...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700613 ShowStatus();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700614 return 0;
615 }
Darin Petkov58529db2010-08-13 09:19:47 -0700616
Chris Sosa192449e2013-10-28 14:16:19 -0700617 if (FLAGS_follow) {
618 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700619 WaitForUpdateComplete();
620 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700621 }
622
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700623 if (FLAGS_watch_for_updates) {
624 LOG(INFO) << "Watching for status updates.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700625 WatchForUpdates();
626 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700627 }
Darin Petkov58529db2010-08-13 09:19:47 -0700628
Darin Petkov296889c2010-07-23 16:20:54 -0700629 if (FLAGS_reboot) {
630 LOG(INFO) << "Requesting a reboot...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700631 RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700632 return 0;
633 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700634
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700635 if (FLAGS_prev_version) {
636 ShowPrevVersion();
637 }
638
David Zeuthen9d73a722014-04-04 14:52:46 -0700639 if (FLAGS_is_reboot_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700640 // In case of error GetIsRebootNeeded() will exit with 1.
641 if (GetIsRebootNeeded()) {
642 return 0;
643 } else {
David Zeuthen9d73a722014-04-04 14:52:46 -0700644 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700645 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700646 }
647
648 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700649 WaitForRebootNeeded();
650 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700651 }
652
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700653 return 0;
654}
Alex Deymo72aa0022015-06-19 21:16:49 -0700655
656} // namespace
657
658int main(int argc, char** argv) {
659 UpdateEngineClient client(argc, argv);
660 return client.Run();
661}