blob: 151456144341b906bd69c086d74d9fd52a7595dd [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// 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;
Andrew de los Reyesc7020782010-04-28 10:46:04 -070022using chromeos_update_engine::utils::GetGErrorMessage;
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.");
Darin Petkov8daa3242010-10-25 13:28:47 -070031DEFINE_string(track, "", "Permanently change the update track.");
Darin Petkov58529db2010-08-13 09:19:47 -070032DEFINE_bool(update, false, "Forces an update and waits for its completion. "
33 "Exit status is 0 if the update succeeded, and 1 otherwise.");
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070034DEFINE_bool(watch_for_updates, false,
35 "Listen for status updates and print them to the screen.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
37namespace {
38
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070039bool GetProxy(DBusGProxy** out_proxy) {
40 DBusGConnection* bus;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070041 DBusGProxy* proxy = NULL;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070042 GError* error = NULL;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070043 const int kTries = 4;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070044
45 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
46 if (!bus) {
47 LOG(FATAL) << "Failed to get bus";
48 }
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070049 for (int i = 0; !proxy && i < kTries; ++i) {
50 LOG(INFO) << "Trying to get dbus proxy. Try " << (i + 1) << "/" << kTries;
51 proxy = dbus_g_proxy_new_for_name_owner(bus,
52 kUpdateEngineServiceName,
53 kUpdateEngineServicePath,
54 kUpdateEngineServiceInterface,
55 &error);
56 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070057 if (!proxy) {
Kenneth Waters71e8e5c2010-09-10 09:50:40 -070058 LOG(FATAL) << "Error getting dbus proxy for "
59 << kUpdateEngineServiceName << ": " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070060 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070061 *out_proxy = proxy;
62 return true;
63}
64
65static void StatusUpdateSignalHandler(DBusGProxy* proxy,
66 int64_t last_checked_time,
67 double progress,
68 gchar* current_operation,
69 gchar* new_version,
70 int64_t new_size,
71 void* user_data) {
72 LOG(INFO) << "Got status update:";
73 LOG(INFO) << " last_checked_time: " << last_checked_time;
74 LOG(INFO) << " progress: " << progress;
75 LOG(INFO) << " current_operation: " << current_operation;
76 LOG(INFO) << " new_version: " << new_version;
77 LOG(INFO) << " new_size: " << new_size;
78}
79
Darin Petkov58529db2010-08-13 09:19:47 -070080// If |op| is non-NULL, sets it to the current operation string or an
81// empty string if unable to obtain the current status.
82bool GetStatus(string* op) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070083 DBusGProxy* proxy;
84 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -070085
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070086 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070087
88 gint64 last_checked_time = 0;
89 gdouble progress = 0.0;
90 char* current_op = NULL;
91 char* new_version = NULL;
92 gint64 new_size = 0;
93
94 gboolean rc = org_chromium_UpdateEngineInterface_get_status(
95 proxy,
96 &last_checked_time,
97 &progress,
98 &current_op,
99 &new_version,
100 &new_size,
101 &error);
102 if (rc == FALSE) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700103 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700104 }
105 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
106 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
107 last_checked_time,
108 progress,
109 current_op,
110 new_version,
111 new_size);
Darin Petkov58529db2010-08-13 09:19:47 -0700112 if (op) {
113 *op = current_op ? current_op : "";
114 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700115 return true;
116}
117
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700118// Should never return.
119void WatchForUpdates() {
120 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700121
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700122 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700123
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700124 // Register marshaller
125 dbus_g_object_register_marshaller(
126 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
127 G_TYPE_NONE,
128 G_TYPE_INT64,
129 G_TYPE_DOUBLE,
130 G_TYPE_STRING,
131 G_TYPE_STRING,
132 G_TYPE_INT64,
133 G_TYPE_INVALID);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700134
135 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700136 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700137 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700138 G_TYPE_INT64,
139 G_TYPE_DOUBLE,
140 G_TYPE_STRING,
141 G_TYPE_STRING,
142 G_TYPE_INT64,
143 G_TYPE_INVALID);
144 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
145 dbus_g_proxy_connect_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700146 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700147 G_CALLBACK(StatusUpdateSignalHandler),
148 NULL,
149 NULL);
150 g_main_loop_run(loop);
151 g_main_loop_unref(loop);
152}
153
Darin Petkov58529db2010-08-13 09:19:47 -0700154bool CheckForUpdates(const string& app_version, const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700155 DBusGProxy* proxy;
156 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700157
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700158 CHECK(GetProxy(&proxy));
159
160 gboolean rc =
Darin Petkov5a7f5652010-07-22 21:40:09 -0700161 org_chromium_UpdateEngineInterface_attempt_update(proxy,
162 app_version.c_str(),
163 omaha_url.c_str(),
164 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700165 CHECK_EQ(rc, TRUE) << "Error checking for update: "
166 << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700167 return true;
168}
169
Darin Petkov296889c2010-07-23 16:20:54 -0700170bool RebootIfNeeded() {
171 DBusGProxy* proxy;
172 GError* error = NULL;
173
174 CHECK(GetProxy(&proxy));
175
176 gboolean rc =
177 org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error);
178 // Reboot error code doesn't necessarily mean that a reboot
179 // failed. For example, D-Bus may be shutdown before we receive the
180 // result.
181 LOG_IF(INFO, !rc) << "Reboot error message: " << GetGErrorMessage(error);
182 return true;
183}
184
Darin Petkov8daa3242010-10-25 13:28:47 -0700185void SetTrack(const string& track) {
186 DBusGProxy* proxy;
187 GError* error = NULL;
188
189 CHECK(GetProxy(&proxy));
190
191 gboolean rc =
192 org_chromium_UpdateEngineInterface_set_track(proxy,
193 track.c_str(),
194 &error);
195 CHECK_EQ(rc, true) << "Error setting the track: "
196 << GetGErrorMessage(error);
Darin Petkov49d91322010-10-25 16:34:58 -0700197 LOG(INFO) << "Track permanently set to: " << track;
Darin Petkov8daa3242010-10-25 13:28:47 -0700198}
199
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900200string GetTrack() {
201 DBusGProxy* proxy;
202 GError* error = NULL;
203
204 CHECK(GetProxy(&proxy));
205
206 char* track = NULL;
207 gboolean rc =
208 org_chromium_UpdateEngineInterface_get_track(proxy,
209 &track,
210 &error);
211 CHECK_EQ(rc, true) << "Error getting the track: "
212 << GetGErrorMessage(error);
213 string output = track;
214 g_free(track);
215 return output;
216}
217
Darin Petkov58529db2010-08-13 09:19:47 -0700218static gboolean CompleteUpdateSource(gpointer data) {
219 string current_op;
220 if (!GetStatus(&current_op) || current_op == "UPDATE_STATUS_IDLE") {
221 LOG(ERROR) << "Update failed.";
222 exit(1);
223 }
224 if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") {
225 LOG(INFO) << "Update succeeded -- reboot needed.";
226 exit(0);
227 }
228 return TRUE;
229}
230
231// This is similar to watching for updates but rather than registering
232// a signal watch, activelly poll the daemon just in case it stops
233// sending notifications.
234void CompleteUpdate() {
235 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
236 g_timeout_add_seconds(5, CompleteUpdateSource, NULL);
237 g_main_loop_run(loop);
238 g_main_loop_unref(loop);
239}
240
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700241} // namespace {}
242
243int main(int argc, char** argv) {
244 // Boilerplate init commands.
245 g_type_init();
246 g_thread_init(NULL);
247 dbus_g_thread_init();
248 chromeos_update_engine::Subprocess::Init();
249 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700250
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700251 if (FLAGS_status) {
252 LOG(INFO) << "Querying Update Engine status...";
Darin Petkov58529db2010-08-13 09:19:47 -0700253 if (!GetStatus(NULL)) {
254 LOG(FATAL) << "GetStatus failed.";
255 return 1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700256 }
257 return 0;
258 }
Darin Petkov58529db2010-08-13 09:19:47 -0700259
Darin Petkov8daa3242010-10-25 13:28:47 -0700260 // First, update the track if requested.
261 if (!FLAGS_track.empty()) {
262 SetTrack(FLAGS_track);
263 }
264
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900265 // Show the track if requested.
266 if (FLAGS_show_track) {
267 LOG(INFO) << "Track: " << GetTrack();
268 }
269
Darin Petkov58529db2010-08-13 09:19:47 -0700270 // Initiate an update check, if necessary.
271 if (FLAGS_check_for_update ||
272 FLAGS_update ||
273 !FLAGS_app_version.empty() ||
274 !FLAGS_omaha_url.empty()) {
Darin Petkov296889c2010-07-23 16:20:54 -0700275 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700276 string app_version = FLAGS_app_version;
277 if (FLAGS_update && app_version.empty()) {
278 app_version = "ForcedUpdate";
279 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700280 }
Darin Petkov58529db2010-08-13 09:19:47 -0700281 LOG(INFO) << "Initiating update check and install.";
282 CHECK(CheckForUpdates(app_version, FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700283 << "Update check/initiate update failed.";
Darin Petkov58529db2010-08-13 09:19:47 -0700284
285 // Wait for an update to complete.
286 if (FLAGS_update) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700287 LOG(INFO) << "Waiting for update to complete.";
Darin Petkov58529db2010-08-13 09:19:47 -0700288 CompleteUpdate(); // Should never return.
289 return 1;
290 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700291 return 0;
292 }
Darin Petkov58529db2010-08-13 09:19:47 -0700293
294 // Start watching for updates.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700295 if (FLAGS_watch_for_updates) {
Darin Petkov296889c2010-07-23 16:20:54 -0700296 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700297 LOG(INFO) << "Watching for status updates.";
298 WatchForUpdates(); // Should never return.
299 return 1;
300 }
Darin Petkov58529db2010-08-13 09:19:47 -0700301
Darin Petkov296889c2010-07-23 16:20:54 -0700302 if (FLAGS_reboot) {
303 LOG(INFO) << "Requesting a reboot...";
304 CHECK(RebootIfNeeded());
305 return 0;
306 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700307
Darin Petkov8daa3242010-10-25 13:28:47 -0700308 LOG(INFO) << "Done.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700309 return 0;
310}