blob: 6a8817797efff45b3403b9e881701013c98a8c33 [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
Darin Petkov5a7f5652010-07-22 21:40:09 -070021#include <string>
22
Alex Deymo72aa0022015-06-19 21:16:49 -070023#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080024#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070025#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070026#include <base/macros.h>
27#include <chromeos/daemons/dbus_daemon.h>
David Zeuthen9d73a722014-04-04 14:52:46 -070028#include <chromeos/dbus/service_constants.h>
Steve Fung97b6f5a2014-10-07 12:39:51 -070029#include <chromeos/flag_helper.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070030#include <dbus/bus.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070031
32#include "update_engine/dbus_constants.h"
Alex Deymod2956cc2015-08-17 15:39:40 -070033#include "update_engine/dbus-proxies.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070034
David Zeuthen75a4c3e2013-09-06 11:36:59 -070035using chromeos_update_engine::kAttemptUpdateFlagNonInteractive;
Alex Deymo44666f92014-07-22 20:29:24 -070036using chromeos_update_engine::kUpdateEngineServiceName;
Darin Petkov5a7f5652010-07-22 21:40:09 -070037using std::string;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070038
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070039namespace {
40
Alex Deymo72aa0022015-06-19 21:16:49 -070041// Constant to signal that we need to continue running the daemon after
42// initialization.
43const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070044
Alex Deymo72aa0022015-06-19 21:16:49 -070045class UpdateEngineClient : public chromeos::DBusDaemon {
46 public:
47 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
48 ~UpdateEngineClient() override = default;
49
50 protected:
51 int OnInit() override {
52 int ret = DBusDaemon::OnInit();
53 if (ret != EX_OK)
54 return ret;
55 if (!InitProxy())
56 return 1;
Alex Deymo13e0dd62015-06-30 23:11:44 -070057 // Wait for the UpdateEngine to be available or timeout.
58 proxy_->GetObjectProxy()->WaitForServiceToBeAvailable(
59 base::Bind(&UpdateEngineClient::OnServiceAvailable,
60 base::Unretained(this)));
61 base::MessageLoop::current()->PostDelayedTask(
62 FROM_HERE,
63 base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout,
64 base::Unretained(this)),
65 base::TimeDelta::FromSeconds(10));
Alex Deymo72aa0022015-06-19 21:16:49 -070066 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070067 }
Alex Deymo72aa0022015-06-19 21:16:49 -070068
69 private:
70 bool InitProxy();
71
Alex Deymo13e0dd62015-06-30 23:11:44 -070072 // Callback called when the UpdateEngine service becomes available.
73 void OnServiceAvailable(bool service_is_available);
74
75 // Callback called when the UpdateEngine service doesn't become available
76 // after a timeout.
77 void OnServiceAvailableTimeout();
78
79
Alex Deymo72aa0022015-06-19 21:16:49 -070080 // Callback called when a StatusUpdate signal is received.
81 void OnStatusUpdateSignal(int64_t last_checked_time,
82 double progress,
83 const string& current_operation,
84 const string& new_version,
85 int64_t new_size);
86 // Callback called when the OnStatusUpdateSignal() handler is registered.
87 void OnStatusUpdateSignalRegistration(const string& interface,
88 const string& signal_name,
89 bool success);
90
91 // Registers a callback that prints on stderr the received StatusUpdate
92 // signals.
93 // The daemon should continue running for this to work.
94 void WatchForUpdates();
95
96 void ResetStatus();
97
98 // Show the status of the update engine in stdout.
99 // Blocking call. Exits the program with error 1 in case of an error.
100 void ShowStatus();
101
102 // Return the current operation status, such as UPDATE_STATUS_IDLE.
103 // Blocking call. Exits the program with error 1 in case of an error.
104 string GetCurrentOperation();
105
106 void Rollback(bool rollback);
107 string GetRollbackPartition();
108 string GetKernelDevices();
109 void CheckForUpdates(const string& app_version,
110 const string& omaha_url,
111 bool interactive);
112
113 // Reboot the device if a reboot is needed.
114 // Blocking call. Ignores failures.
115 void RebootIfNeeded();
116
117 // Getter and setter for the target channel. If |get_current_channel| is true,
118 // the current channel instead of the target channel will be returned.
119 // Blocking call. Exits the program with error 1 in case of an error.
120 void SetTargetChannel(const string& target_channel, bool allow_powerwash);
121 string GetChannel(bool get_current_channel);
122
123 // Getter and setter for the updates over cellular connections.
124 // Blocking call. Exits the program with error 1 in case of an error.
125 void SetUpdateOverCellularPermission(bool allowed);
126 bool GetUpdateOverCellularPermission();
127
128 // Getter and setter for the updates from P2P permission.
129 // Blocking call. Exits the program with error 1 in case of an error.
130 void SetP2PUpdatePermission(bool enabled);
131 bool GetP2PUpdatePermission();
132
133 // This is similar to watching for updates but rather than registering
134 // a signal watch, actively poll the daemon just in case it stops
135 // sending notifications.
136 void WaitForUpdateComplete();
137 void OnUpdateCompleteCheck(int64_t last_checked_time,
138 double progress,
139 const string& current_operation,
140 const string& new_version,
141 int64_t new_size);
142
143 // Blocking call. Exits the program with error 1 in case of an error.
144 void ShowPrevVersion();
145
146 // Returns whether the current status is such that a reboot is needed.
147 // Blocking call. Exits the program with error 1 in case of an error.
148 bool GetIsRebootNeeded();
149
150 // Blocks until a reboot is needed. If the reboot is needed, exits the program
151 // with 0. Otherwise it exits the program with 1 if an error occurs before
152 // the reboot is needed.
153 void WaitForRebootNeeded();
154 void OnRebootNeededCheck(int64_t last_checked_time,
155 double progress,
156 const string& current_operation,
157 const string& new_version,
158 int64_t new_size);
159 // Callback called when the OnRebootNeededCheck() handler is registered. This
160 // is useful to check at this point if the reboot is needed, without loosing
161 // any StatusUpdate signals and avoiding the race condition.
162 void OnRebootNeededCheckRegistration(const string& interface,
163 const string& signal_name,
164 bool success);
165
166 // Main method that parses and triggers all the actions based on the passed
167 // flags.
168 int ProcessFlags();
169
170 // DBus Proxy to the update_engine daemon object used for all the calls.
171 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
172
173 // Copy of argc and argv passed to main().
174 int argc_;
175 char** argv_;
176
Alex Deymo13e0dd62015-06-30 23:11:44 -0700177 // Tell whether the UpdateEngine service is available after startup.
178 bool service_is_available_{false};
179
Alex Deymo72aa0022015-06-19 21:16:49 -0700180 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
181};
182
Alex Deymo72aa0022015-06-19 21:16:49 -0700183bool UpdateEngineClient::InitProxy() {
184 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
185
186 if (!proxy_->GetObjectProxy()) {
187 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
188 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800189 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700190 return true;
191}
192
Alex Deymo13e0dd62015-06-30 23:11:44 -0700193void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
194 service_is_available_ = service_is_available;
195 if (!service_is_available) {
196 LOG(ERROR) << "UpdateEngineService not available.";
197 QuitWithExitCode(-1);
198 }
199 int ret = ProcessFlags();
200 if (ret != kContinueRunning)
201 QuitWithExitCode(ret);
202}
203
204void UpdateEngineClient::OnServiceAvailableTimeout() {
205 if (!service_is_available_) {
206 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
207 "daemon running?";
208 QuitWithExitCode(-1);
209 }
210}
211
Alex Deymo72aa0022015-06-19 21:16:49 -0700212void UpdateEngineClient::OnStatusUpdateSignal(
213 int64_t last_checked_time,
214 double progress,
215 const string& current_operation,
216 const string& new_version,
217 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700218 LOG(INFO) << "Got status update:";
219 LOG(INFO) << " last_checked_time: " << last_checked_time;
220 LOG(INFO) << " progress: " << progress;
221 LOG(INFO) << " current_operation: " << current_operation;
222 LOG(INFO) << " new_version: " << new_version;
223 LOG(INFO) << " new_size: " << new_size;
224}
225
Alex Deymo72aa0022015-06-19 21:16:49 -0700226void UpdateEngineClient::OnStatusUpdateSignalRegistration(
227 const string& interface,
228 const string& signal_name,
229 bool success) {
230 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
231 << signal_name << ", " << success << ");";
232 if (!success) {
233 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
234 exit(1);
235 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700236}
237
Alex Deymo72aa0022015-06-19 21:16:49 -0700238void UpdateEngineClient::WatchForUpdates() {
239 proxy_->RegisterStatusUpdateSignalHandler(
240 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
241 base::Unretained(this)),
242 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
243 base::Unretained(this)));
244}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700245
Alex Deymo72aa0022015-06-19 21:16:49 -0700246void UpdateEngineClient::ResetStatus() {
247 bool ret = proxy_->ResetStatus(nullptr);
248 CHECK(ret) << "ResetStatus() failed.";
249}
Andrew de los Reyesada42202010-07-15 22:23:20 -0700250
Alex Deymo72aa0022015-06-19 21:16:49 -0700251void UpdateEngineClient::ShowStatus() {
252 int64_t last_checked_time = 0;
253 double progress = 0.0;
254 string current_op;
255 string new_version;
256 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700257
Alex Deymo72aa0022015-06-19 21:16:49 -0700258 bool ret = proxy_->GetStatus(
259 &last_checked_time, &progress, &current_op, &new_version, &new_size,
260 nullptr);
261 CHECK(ret) << "GetStatus() failed";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700262 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
263 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
264 last_checked_time,
265 progress,
Alex Deymo72aa0022015-06-19 21:16:49 -0700266 current_op.c_str(),
267 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700268 new_size);
Alex Deymo72aa0022015-06-19 21:16:49 -0700269}
270
271string UpdateEngineClient::GetCurrentOperation() {
272 int64_t last_checked_time = 0;
273 double progress = 0.0;
274 string current_op;
275 string new_version;
276 int64_t new_size = 0;
277
278 bool ret = proxy_->GetStatus(
279 &last_checked_time, &progress, &current_op, &new_version, &new_size,
280 nullptr);
281 CHECK(ret) << "GetStatus() failed";
282 return current_op;
283}
284
285void UpdateEngineClient::Rollback(bool rollback) {
286 bool ret = proxy_->AttemptRollback(rollback, nullptr);
287 CHECK(ret) << "Rollback request failed.";
288}
289
290string UpdateEngineClient::GetRollbackPartition() {
291 string rollback_partition;
292 bool ret = proxy_->GetRollbackPartition(&rollback_partition, nullptr);
293 CHECK(ret) << "Error while querying rollback partition availabilty.";
294 return rollback_partition;
295}
296
297string UpdateEngineClient::GetKernelDevices() {
298 string kernel_devices;
299 bool ret = proxy_->GetKernelDevices(&kernel_devices, nullptr);
300 CHECK(ret) << "Error while getting a list of kernel devices";
301 return kernel_devices;
302}
303
304void UpdateEngineClient::CheckForUpdates(const string& app_version,
305 const string& omaha_url,
306 bool interactive) {
307 int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
308 bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
309 nullptr);
310 CHECK(ret) << "Error checking for update.";
311}
312
313void UpdateEngineClient::RebootIfNeeded() {
314 bool ret = proxy_->RebootIfNeeded(nullptr);
315 if (!ret) {
316 // Reboot error code doesn't necessarily mean that a reboot
317 // failed. For example, D-Bus may be shutdown before we receive the
318 // result.
319 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700320 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700321}
322
Alex Deymo72aa0022015-06-19 21:16:49 -0700323void UpdateEngineClient::SetTargetChannel(const string& target_channel,
324 bool allow_powerwash) {
325 bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
326 CHECK(ret) << "Error setting the channel.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700327 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700328}
329
Alex Deymo72aa0022015-06-19 21:16:49 -0700330string UpdateEngineClient::GetChannel(bool get_current_channel) {
331 string channel;
332 bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
333 CHECK(ret) << "Error getting the channel.";
334 return channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900335}
336
Alex Deymo72aa0022015-06-19 21:16:49 -0700337void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
338 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
339 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700340}
341
Alex Deymo72aa0022015-06-19 21:16:49 -0700342bool UpdateEngineClient::GetUpdateOverCellularPermission() {
343 bool allowed;
344 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
345 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700346 return allowed;
347}
348
Alex Deymo72aa0022015-06-19 21:16:49 -0700349void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
350 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
351 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700352}
353
Alex Deymo72aa0022015-06-19 21:16:49 -0700354bool UpdateEngineClient::GetP2PUpdatePermission() {
355 bool enabled;
356 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
357 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700358 return enabled;
359}
360
Alex Deymo72aa0022015-06-19 21:16:49 -0700361void UpdateEngineClient::OnUpdateCompleteCheck(
362 int64_t /* last_checked_time */,
363 double /* progress */,
364 const string& current_operation,
365 const string& /* new_version */,
366 int64_t /* new_size */) {
367 if (current_operation == update_engine::kUpdateStatusIdle) {
368 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700369 exit(1);
370 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700371 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700372 LOG(INFO) << "Update succeeded -- reboot needed.";
373 exit(0);
374 }
Darin Petkov58529db2010-08-13 09:19:47 -0700375}
376
Alex Deymo72aa0022015-06-19 21:16:49 -0700377void UpdateEngineClient::WaitForUpdateComplete() {
378 proxy_->RegisterStatusUpdateSignalHandler(
379 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
380 base::Unretained(this)),
381 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
382 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700383}
384
Alex Deymo72aa0022015-06-19 21:16:49 -0700385void UpdateEngineClient::ShowPrevVersion() {
386 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700387
Alex Deymo72aa0022015-06-19 21:16:49 -0700388 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
389 if (!ret) {
390 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700391 } else {
392 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700393 }
394}
395
Alex Deymo72aa0022015-06-19 21:16:49 -0700396bool UpdateEngineClient::GetIsRebootNeeded() {
397 return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
David Zeuthen9d73a722014-04-04 14:52:46 -0700398}
399
Alex Deymo72aa0022015-06-19 21:16:49 -0700400void UpdateEngineClient::OnRebootNeededCheck(
401 int64_t /* last_checked_time */,
402 double /* progress */,
403 const string& current_operation,
404 const string& /* new_version */,
405 int64_t /* new_size */) {
406 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
407 LOG(INFO) << "Reboot needed.";
408 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700409 }
410}
411
Alex Deymo72aa0022015-06-19 21:16:49 -0700412void UpdateEngineClient::OnRebootNeededCheckRegistration(
413 const string& interface,
414 const string& signal_name,
415 bool success) {
416 if (GetIsRebootNeeded())
417 exit(0);
418 if (!success) {
419 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
420 exit(1);
421 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700422}
423
424// Blocks until a reboot is needed. Returns true if waiting succeeded,
425// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700426void UpdateEngineClient::WaitForRebootNeeded() {
427 proxy_->RegisterStatusUpdateSignalHandler(
428 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
429 base::Unretained(this)),
430 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
431 base::Unretained(this)));
432 if (GetIsRebootNeeded())
433 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700434}
435
Alex Deymo72aa0022015-06-19 21:16:49 -0700436int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700437 DEFINE_string(app_version, "", "Force the current app version.");
438 DEFINE_string(channel, "",
439 "Set the target channel. The device will be powerwashed if the "
440 "target channel is more stable than the current channel unless "
441 "--nopowerwash is specified.");
442 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
443 DEFINE_bool(follow, false, "Wait for any update operations to complete."
444 "Exit status is 0 if the update succeeded, and 1 otherwise.");
445 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
446 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
447 DEFINE_string(p2p_update, "",
448 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
449 " sharing.");
450 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
451 "do a powerwash or allow it respectively.");
452 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
453 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
454 "2 if reboot is not needed or 1 if an error occurred.");
455 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
456 "needed. Returns non-zero exit status if an error occurred.");
457 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800458 DEFINE_bool(rollback, false,
459 "Perform a rollback to the previous partition. The device will "
460 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700461 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
462 "is available.");
463 DEFINE_bool(show_channel, false, "Show the current and target channels.");
464 DEFINE_bool(show_p2p_update, false,
465 "Show the current setting for peer-to-peer update sharing.");
466 DEFINE_bool(show_update_over_cellular, false,
467 "Show the current setting for updates over cellular networks.");
468 DEFINE_bool(status, false, "Print the status to stdout.");
469 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
470 "Implies --follow.");
471 DEFINE_string(update_over_cellular, "",
472 "Enables (\"yes\") or disables (\"no\") the updates over "
473 "cellular networks.");
474 DEFINE_bool(watch_for_updates, false,
475 "Listen for status updates and print them to the screen.");
476 DEFINE_bool(prev_version, false,
477 "Show the previous OS version used before the update reboot.");
478 DEFINE_bool(show_kernels, false, "Show the list of kernel patritions and "
479 "whether each of them is bootable or not");
480
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700481 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700482 base::CommandLine::Init(argc_, argv_);
483 chromeos::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700484
Alex Deymo8ce80d62015-01-27 15:10:43 -0800485 // Ensure there are no positional arguments.
486 const std::vector<string> positional_args =
487 base::CommandLine::ForCurrentProcess()->GetArgs();
488 if (!positional_args.empty()) {
489 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
490 << "'. If you want to pass a value to a flag, pass it as "
491 "--flag=value.";
492 return 1;
493 }
494
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700495 // Update the status if requested.
496 if (FLAGS_reset_status) {
497 LOG(INFO) << "Setting Update Engine status to idle ...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700498 ResetStatus();
Gilad Arnold50c60632013-01-25 10:27:19 -0800499 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n"
500 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} "
501 "| sed 's/^[^0-9]*//')-1)) $D;)";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700502 }
Darin Petkov58529db2010-08-13 09:19:47 -0700503
Alex Deymof4867c42013-06-28 14:41:39 -0700504 // Changes the current update over cellular network setting.
505 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700506 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700507 if (!allowed && FLAGS_update_over_cellular != "no") {
508 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
509 << "\". Please specify \"yes\" or \"no\".";
510 } else {
511 SetUpdateOverCellularPermission(allowed);
512 }
513 }
514
515 // Show the current update over cellular network setting.
516 if (FLAGS_show_update_over_cellular) {
517 bool allowed = GetUpdateOverCellularPermission();
518 LOG(INFO) << "Current update over cellular network setting: "
519 << (allowed ? "ENABLED" : "DISABLED");
520 }
521
Chris Sosacb7fa882013-07-25 17:02:59 -0700522 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700523 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700524 return 1;
525 }
526
Alex Deymo5fdf7762013-07-17 20:01:40 -0700527 // Change the P2P enabled setting.
528 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700529 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700530 if (!enabled && FLAGS_p2p_update != "no") {
531 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
532 << "\". Please specify \"yes\" or \"no\".";
533 } else {
534 SetP2PUpdatePermission(enabled);
535 }
536 }
537
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800538 // Show the rollback availability.
539 if (FLAGS_can_rollback) {
Alex Deymof329b932014-10-30 01:37:48 -0700540 string rollback_partition = GetRollbackPartition();
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700541 bool can_rollback = true;
542 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700543 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700544 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700545 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700546 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700547 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700548
549 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700550 if (!can_rollback) {
551 return 1;
552 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800553 }
554
Alex Deymo5fdf7762013-07-17 20:01:40 -0700555 // Show the current P2P enabled setting.
556 if (FLAGS_show_p2p_update) {
557 bool enabled = GetP2PUpdatePermission();
558 LOG(INFO) << "Current update using P2P setting: "
559 << (enabled ? "ENABLED" : "DISABLED");
560 }
561
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700562 // First, update the target channel if requested.
563 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700564 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700565
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700566 // Show the current and target channels if requested.
567 if (FLAGS_show_channel) {
568 string current_channel = GetChannel(true);
569 LOG(INFO) << "Current Channel: " << current_channel;
570
571 string target_channel = GetChannel(false);
572 if (!target_channel.empty())
573 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900574 }
575
Chris Sosad317e402013-06-12 13:47:09 -0700576 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
577 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700578 if (FLAGS_update)
579 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700580
Chris Sosad317e402013-06-12 13:47:09 -0700581 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700582 LOG(ERROR) << "Incompatible flags specified with rollback."
583 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700584 return 1;
585 }
586
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700587 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700588 LOG(INFO) << "Requesting rollback.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700589 Rollback(FLAGS_powerwash);
Chris Sosad317e402013-06-12 13:47:09 -0700590 }
591
Darin Petkov58529db2010-08-13 09:19:47 -0700592 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700593 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700594 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700595 string app_version = FLAGS_app_version;
596 if (FLAGS_update && app_version.empty()) {
597 app_version = "ForcedUpdate";
598 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700599 }
Darin Petkov58529db2010-08-13 09:19:47 -0700600 LOG(INFO) << "Initiating update check and install.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700601 CheckForUpdates(app_version, FLAGS_omaha_url, FLAGS_interactive);
Chris Sosa192449e2013-10-28 14:16:19 -0700602 }
Darin Petkov58529db2010-08-13 09:19:47 -0700603
Chris Sosa192449e2013-10-28 14:16:19 -0700604 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700605 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
606 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700607 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700608 LOG(ERROR) << "Multiple exclusive options selected. "
609 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700610 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700611 << "or --status.";
612 return 1;
613 }
614
615 if (FLAGS_status) {
616 LOG(INFO) << "Querying Update Engine status...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700617 ShowStatus();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700618 return 0;
619 }
Darin Petkov58529db2010-08-13 09:19:47 -0700620
Chris Sosa192449e2013-10-28 14:16:19 -0700621 if (FLAGS_follow) {
622 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700623 WaitForUpdateComplete();
624 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700625 }
626
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700627 if (FLAGS_watch_for_updates) {
628 LOG(INFO) << "Watching for status updates.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700629 WatchForUpdates();
630 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700631 }
Darin Petkov58529db2010-08-13 09:19:47 -0700632
Darin Petkov296889c2010-07-23 16:20:54 -0700633 if (FLAGS_reboot) {
634 LOG(INFO) << "Requesting a reboot...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700635 RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700636 return 0;
637 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700638
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700639 if (FLAGS_prev_version) {
640 ShowPrevVersion();
641 }
642
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700643 if (FLAGS_show_kernels) {
644 LOG(INFO) << "Kernel partitions:\n"
645 << GetKernelDevices();
646 }
647
David Zeuthen9d73a722014-04-04 14:52:46 -0700648 if (FLAGS_is_reboot_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700649 // In case of error GetIsRebootNeeded() will exit with 1.
650 if (GetIsRebootNeeded()) {
651 return 0;
652 } else {
David Zeuthen9d73a722014-04-04 14:52:46 -0700653 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700654 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700655 }
656
657 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700658 WaitForRebootNeeded();
659 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700660 }
661
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700662 return 0;
663}
Alex Deymo72aa0022015-06-19 21:16:49 -0700664
665} // namespace
666
667int main(int argc, char** argv) {
668 UpdateEngineClient client(argc, argv);
669 return client.Run();
670}