blob: f8396c96cc0afce355f72b54b93521a2b4b443ac [file] [log] [blame]
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov5a7f5652010-07-22 21:40:09 -07005#include <string>
6
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07007#include <gflags/gflags.h>
8#include <glib.h>
9
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070010#include "update_engine/marshal.glibmarshal.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070011#include "update_engine/dbus_constants.h"
12#include "update_engine/subprocess.h"
13#include "update_engine/utils.h"
14
15extern "C" {
16#include "update_engine/update_engine.dbusclient.h"
17}
18
19using chromeos_update_engine::kUpdateEngineServiceName;
20using chromeos_update_engine::kUpdateEngineServicePath;
21using chromeos_update_engine::kUpdateEngineServiceInterface;
Darin Petkova0b9e772011-10-06 05:05:56 -070022using chromeos_update_engine::utils::GetAndFreeGError;
Darin Petkov5a7f5652010-07-22 21:40:09 -070023using std::string;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024
Darin Petkov296889c2010-07-23 16:20:54 -070025DEFINE_string(app_version, "", "Force the current app version.");
26DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Darin Petkov296889c2010-07-23 16:20:54 -070027DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
28DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
Satoru Takabayashi583667b2010-10-27 13:09:57 +090029DEFINE_bool(show_track, false, "Show the update track.");
Darin Petkov5a7f5652010-07-22 21:40:09 -070030DEFINE_bool(status, false, "Print the status to stdout.");
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -070031DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Darin Petkov8daa3242010-10-25 13:28:47 -070032DEFINE_string(track, "", "Permanently change the update track.");
Darin Petkov58529db2010-08-13 09:19:47 -070033DEFINE_bool(update, false, "Forces an update and waits for its completion. "
34 "Exit status is 0 if the update succeeded, and 1 otherwise.");
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070035DEFINE_bool(watch_for_updates, false,
36 "Listen for status updates and print them to the screen.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070037
38namespace {
39
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040bool GetProxy(DBusGProxy** out_proxy) {
41 DBusGConnection* bus;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070042 DBusGProxy* proxy = NULL;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070043 GError* error = NULL;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070044 const int kTries = 4;
Darin Petkova0b9e772011-10-06 05:05:56 -070045 const int kRetrySeconds = 10;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070046
47 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070048 LOG_IF(FATAL, !bus) << "Failed to get bus: " << GetAndFreeGError(&error);
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070049 for (int i = 0; !proxy && i < kTries; ++i) {
Darin Petkova0b9e772011-10-06 05:05:56 -070050 if (i > 0) {
51 LOG(INFO) << "Retrying to get dbus proxy. Try "
52 << (i + 1) << "/" << kTries;
53 sleep(kRetrySeconds);
54 }
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070055 proxy = dbus_g_proxy_new_for_name_owner(bus,
56 kUpdateEngineServiceName,
57 kUpdateEngineServicePath,
58 kUpdateEngineServiceInterface,
59 &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070060 LOG_IF(WARNING, !proxy) << "Error getting dbus proxy for "
61 << kUpdateEngineServiceName << ": "
62 << GetAndFreeGError(&error);
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070063 }
Darin Petkova0b9e772011-10-06 05:05:56 -070064 LOG_IF(FATAL, !proxy) << "Giving up -- unable to get dbus proxy for "
65 << kUpdateEngineServiceName;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070066 *out_proxy = proxy;
67 return true;
68}
69
70static void StatusUpdateSignalHandler(DBusGProxy* proxy,
71 int64_t last_checked_time,
72 double progress,
73 gchar* current_operation,
74 gchar* new_version,
75 int64_t new_size,
76 void* user_data) {
77 LOG(INFO) << "Got status update:";
78 LOG(INFO) << " last_checked_time: " << last_checked_time;
79 LOG(INFO) << " progress: " << progress;
80 LOG(INFO) << " current_operation: " << current_operation;
81 LOG(INFO) << " new_version: " << new_version;
82 LOG(INFO) << " new_size: " << new_size;
83}
84
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -070085bool ResetStatus() {
86 DBusGProxy* proxy;
87 GError* error = NULL;
88
89 CHECK(GetProxy(&proxy));
90
91 gboolean rc =
92 org_chromium_UpdateEngineInterface_reset_status(proxy, &error);
93 return rc;
94}
95
96
Darin Petkov58529db2010-08-13 09:19:47 -070097// If |op| is non-NULL, sets it to the current operation string or an
98// empty string if unable to obtain the current status.
99bool GetStatus(string* op) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700100 DBusGProxy* proxy;
101 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700102
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700103 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700104
105 gint64 last_checked_time = 0;
106 gdouble progress = 0.0;
107 char* current_op = NULL;
108 char* new_version = NULL;
109 gint64 new_size = 0;
110
111 gboolean rc = org_chromium_UpdateEngineInterface_get_status(
112 proxy,
113 &last_checked_time,
114 &progress,
115 &current_op,
116 &new_version,
117 &new_size,
118 &error);
119 if (rc == FALSE) {
Darin Petkova0b9e772011-10-06 05:05:56 -0700120 LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700121 }
122 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
123 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
124 last_checked_time,
125 progress,
126 current_op,
127 new_version,
128 new_size);
Darin Petkov58529db2010-08-13 09:19:47 -0700129 if (op) {
130 *op = current_op ? current_op : "";
131 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700132 return true;
133}
134
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700135// Should never return.
136void WatchForUpdates() {
137 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700138
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700139 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700140
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700141 // Register marshaller
142 dbus_g_object_register_marshaller(
143 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
144 G_TYPE_NONE,
145 G_TYPE_INT64,
146 G_TYPE_DOUBLE,
147 G_TYPE_STRING,
148 G_TYPE_STRING,
149 G_TYPE_INT64,
150 G_TYPE_INVALID);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700151
152 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700153 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700154 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700155 G_TYPE_INT64,
156 G_TYPE_DOUBLE,
157 G_TYPE_STRING,
158 G_TYPE_STRING,
159 G_TYPE_INT64,
160 G_TYPE_INVALID);
161 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
162 dbus_g_proxy_connect_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700163 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700164 G_CALLBACK(StatusUpdateSignalHandler),
165 NULL,
166 NULL);
167 g_main_loop_run(loop);
168 g_main_loop_unref(loop);
169}
170
Darin Petkov58529db2010-08-13 09:19:47 -0700171bool CheckForUpdates(const string& app_version, const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700172 DBusGProxy* proxy;
173 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700174
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700175 CHECK(GetProxy(&proxy));
176
177 gboolean rc =
Darin Petkov5a7f5652010-07-22 21:40:09 -0700178 org_chromium_UpdateEngineInterface_attempt_update(proxy,
179 app_version.c_str(),
180 omaha_url.c_str(),
181 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700182 CHECK_EQ(rc, TRUE) << "Error checking for update: "
Darin Petkova0b9e772011-10-06 05:05:56 -0700183 << GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700184 return true;
185}
186
Darin Petkov296889c2010-07-23 16:20:54 -0700187bool RebootIfNeeded() {
188 DBusGProxy* proxy;
189 GError* error = NULL;
190
191 CHECK(GetProxy(&proxy));
192
193 gboolean rc =
194 org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error);
195 // Reboot error code doesn't necessarily mean that a reboot
196 // failed. For example, D-Bus may be shutdown before we receive the
197 // result.
Darin Petkova0b9e772011-10-06 05:05:56 -0700198 LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error);
Darin Petkov296889c2010-07-23 16:20:54 -0700199 return true;
200}
201
Darin Petkov8daa3242010-10-25 13:28:47 -0700202void SetTrack(const string& track) {
203 DBusGProxy* proxy;
204 GError* error = NULL;
205
206 CHECK(GetProxy(&proxy));
207
208 gboolean rc =
209 org_chromium_UpdateEngineInterface_set_track(proxy,
210 track.c_str(),
211 &error);
212 CHECK_EQ(rc, true) << "Error setting the track: "
Darin Petkova0b9e772011-10-06 05:05:56 -0700213 << GetAndFreeGError(&error);
Darin Petkov49d91322010-10-25 16:34:58 -0700214 LOG(INFO) << "Track permanently set to: " << track;
Darin Petkov8daa3242010-10-25 13:28:47 -0700215}
216
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900217string GetTrack() {
218 DBusGProxy* proxy;
219 GError* error = NULL;
220
221 CHECK(GetProxy(&proxy));
222
223 char* track = NULL;
224 gboolean rc =
225 org_chromium_UpdateEngineInterface_get_track(proxy,
226 &track,
227 &error);
Darin Petkova0b9e772011-10-06 05:05:56 -0700228 CHECK_EQ(rc, true) << "Error getting the track: " << GetAndFreeGError(&error);
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900229 string output = track;
230 g_free(track);
231 return output;
232}
233
Darin Petkov58529db2010-08-13 09:19:47 -0700234static gboolean CompleteUpdateSource(gpointer data) {
235 string current_op;
236 if (!GetStatus(&current_op) || current_op == "UPDATE_STATUS_IDLE") {
237 LOG(ERROR) << "Update failed.";
238 exit(1);
239 }
240 if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") {
241 LOG(INFO) << "Update succeeded -- reboot needed.";
242 exit(0);
243 }
244 return TRUE;
245}
246
247// This is similar to watching for updates but rather than registering
248// a signal watch, activelly poll the daemon just in case it stops
249// sending notifications.
250void CompleteUpdate() {
251 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
252 g_timeout_add_seconds(5, CompleteUpdateSource, NULL);
253 g_main_loop_run(loop);
254 g_main_loop_unref(loop);
255}
256
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700257} // namespace {}
258
259int main(int argc, char** argv) {
260 // Boilerplate init commands.
261 g_type_init();
262 g_thread_init(NULL);
263 dbus_g_thread_init();
264 chromeos_update_engine::Subprocess::Init();
265 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700266
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700267 // Update the status if requested.
268 if (FLAGS_reset_status) {
269 LOG(INFO) << "Setting Update Engine status to idle ...";
270 if (!ResetStatus()) {
271 LOG(ERROR) << "ResetStatus failed.";
272 return 1;
273 }
274
275 LOG(INFO) << "ResetStatus succeeded.";
276 return 0;
277 }
278
279
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700280 if (FLAGS_status) {
281 LOG(INFO) << "Querying Update Engine status...";
Darin Petkov58529db2010-08-13 09:19:47 -0700282 if (!GetStatus(NULL)) {
283 LOG(FATAL) << "GetStatus failed.";
284 return 1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700285 }
286 return 0;
287 }
Darin Petkov58529db2010-08-13 09:19:47 -0700288
Darin Petkov8daa3242010-10-25 13:28:47 -0700289 // First, update the track if requested.
290 if (!FLAGS_track.empty()) {
291 SetTrack(FLAGS_track);
292 }
293
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900294 // Show the track if requested.
295 if (FLAGS_show_track) {
296 LOG(INFO) << "Track: " << GetTrack();
297 }
298
Darin Petkov58529db2010-08-13 09:19:47 -0700299 // Initiate an update check, if necessary.
300 if (FLAGS_check_for_update ||
301 FLAGS_update ||
302 !FLAGS_app_version.empty() ||
303 !FLAGS_omaha_url.empty()) {
Darin Petkov296889c2010-07-23 16:20:54 -0700304 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700305 string app_version = FLAGS_app_version;
306 if (FLAGS_update && app_version.empty()) {
307 app_version = "ForcedUpdate";
308 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700309 }
Darin Petkov58529db2010-08-13 09:19:47 -0700310 LOG(INFO) << "Initiating update check and install.";
311 CHECK(CheckForUpdates(app_version, FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700312 << "Update check/initiate update failed.";
Darin Petkov58529db2010-08-13 09:19:47 -0700313
314 // Wait for an update to complete.
315 if (FLAGS_update) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700316 LOG(INFO) << "Waiting for update to complete.";
Darin Petkov58529db2010-08-13 09:19:47 -0700317 CompleteUpdate(); // Should never return.
318 return 1;
319 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700320 return 0;
321 }
Darin Petkov58529db2010-08-13 09:19:47 -0700322
323 // Start watching for updates.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700324 if (FLAGS_watch_for_updates) {
Darin Petkov296889c2010-07-23 16:20:54 -0700325 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700326 LOG(INFO) << "Watching for status updates.";
327 WatchForUpdates(); // Should never return.
328 return 1;
329 }
Darin Petkov58529db2010-08-13 09:19:47 -0700330
Darin Petkov296889c2010-07-23 16:20:54 -0700331 if (FLAGS_reboot) {
332 LOG(INFO) << "Requesting a reboot...";
333 CHECK(RebootIfNeeded());
334 return 0;
335 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700336
Darin Petkov8daa3242010-10-25 13:28:47 -0700337 LOG(INFO) << "Done.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700338 return 0;
339}