blob: c0b61feca620401d247b942e408df5deab06ca18 [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;
41 DBusGProxy* proxy;
42 GError* error = NULL;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070043
44 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
45 if (!bus) {
46 LOG(FATAL) << "Failed to get bus";
47 }
48 proxy = dbus_g_proxy_new_for_name_owner(bus,
49 kUpdateEngineServiceName,
50 kUpdateEngineServicePath,
51 kUpdateEngineServiceInterface,
52 &error);
53 if (!proxy) {
Kenneth Waters71e8e5c2010-09-10 09:50:40 -070054 LOG(FATAL) << "Error getting dbus proxy for "
55 << kUpdateEngineServiceName << ": " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070056 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070057 *out_proxy = proxy;
58 return true;
59}
60
61static void StatusUpdateSignalHandler(DBusGProxy* proxy,
62 int64_t last_checked_time,
63 double progress,
64 gchar* current_operation,
65 gchar* new_version,
66 int64_t new_size,
67 void* user_data) {
68 LOG(INFO) << "Got status update:";
69 LOG(INFO) << " last_checked_time: " << last_checked_time;
70 LOG(INFO) << " progress: " << progress;
71 LOG(INFO) << " current_operation: " << current_operation;
72 LOG(INFO) << " new_version: " << new_version;
73 LOG(INFO) << " new_size: " << new_size;
74}
75
Darin Petkov58529db2010-08-13 09:19:47 -070076// If |op| is non-NULL, sets it to the current operation string or an
77// empty string if unable to obtain the current status.
78bool GetStatus(string* op) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070079 DBusGProxy* proxy;
80 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -070081
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070082 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070083
84 gint64 last_checked_time = 0;
85 gdouble progress = 0.0;
86 char* current_op = NULL;
87 char* new_version = NULL;
88 gint64 new_size = 0;
89
90 gboolean rc = org_chromium_UpdateEngineInterface_get_status(
91 proxy,
92 &last_checked_time,
93 &progress,
94 &current_op,
95 &new_version,
96 &new_size,
97 &error);
98 if (rc == FALSE) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070099 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700100 }
101 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
102 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
103 last_checked_time,
104 progress,
105 current_op,
106 new_version,
107 new_size);
Darin Petkov58529db2010-08-13 09:19:47 -0700108 if (op) {
109 *op = current_op ? current_op : "";
110 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700111 return true;
112}
113
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700114// Should never return.
115void WatchForUpdates() {
116 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700117
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700118 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700119
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700120 // Register marshaller
121 dbus_g_object_register_marshaller(
122 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
123 G_TYPE_NONE,
124 G_TYPE_INT64,
125 G_TYPE_DOUBLE,
126 G_TYPE_STRING,
127 G_TYPE_STRING,
128 G_TYPE_INT64,
129 G_TYPE_INVALID);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700130
131 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700132 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700133 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700134 G_TYPE_INT64,
135 G_TYPE_DOUBLE,
136 G_TYPE_STRING,
137 G_TYPE_STRING,
138 G_TYPE_INT64,
139 G_TYPE_INVALID);
140 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
141 dbus_g_proxy_connect_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700142 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700143 G_CALLBACK(StatusUpdateSignalHandler),
144 NULL,
145 NULL);
146 g_main_loop_run(loop);
147 g_main_loop_unref(loop);
148}
149
Darin Petkov58529db2010-08-13 09:19:47 -0700150bool CheckForUpdates(const string& app_version, const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700151 DBusGProxy* proxy;
152 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700153
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700154 CHECK(GetProxy(&proxy));
155
156 gboolean rc =
Darin Petkov5a7f5652010-07-22 21:40:09 -0700157 org_chromium_UpdateEngineInterface_attempt_update(proxy,
158 app_version.c_str(),
159 omaha_url.c_str(),
160 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700161 CHECK_EQ(rc, TRUE) << "Error checking for update: "
162 << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700163 return true;
164}
165
Darin Petkov296889c2010-07-23 16:20:54 -0700166bool RebootIfNeeded() {
167 DBusGProxy* proxy;
168 GError* error = NULL;
169
170 CHECK(GetProxy(&proxy));
171
172 gboolean rc =
173 org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error);
174 // Reboot error code doesn't necessarily mean that a reboot
175 // failed. For example, D-Bus may be shutdown before we receive the
176 // result.
177 LOG_IF(INFO, !rc) << "Reboot error message: " << GetGErrorMessage(error);
178 return true;
179}
180
Darin Petkov8daa3242010-10-25 13:28:47 -0700181void SetTrack(const string& track) {
182 DBusGProxy* proxy;
183 GError* error = NULL;
184
185 CHECK(GetProxy(&proxy));
186
187 gboolean rc =
188 org_chromium_UpdateEngineInterface_set_track(proxy,
189 track.c_str(),
190 &error);
191 CHECK_EQ(rc, true) << "Error setting the track: "
192 << GetGErrorMessage(error);
Darin Petkov49d91322010-10-25 16:34:58 -0700193 LOG(INFO) << "Track permanently set to: " << track;
Darin Petkov8daa3242010-10-25 13:28:47 -0700194}
195
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900196string GetTrack() {
197 DBusGProxy* proxy;
198 GError* error = NULL;
199
200 CHECK(GetProxy(&proxy));
201
202 char* track = NULL;
203 gboolean rc =
204 org_chromium_UpdateEngineInterface_get_track(proxy,
205 &track,
206 &error);
207 CHECK_EQ(rc, true) << "Error getting the track: "
208 << GetGErrorMessage(error);
209 string output = track;
210 g_free(track);
211 return output;
212}
213
Darin Petkov58529db2010-08-13 09:19:47 -0700214static gboolean CompleteUpdateSource(gpointer data) {
215 string current_op;
216 if (!GetStatus(&current_op) || current_op == "UPDATE_STATUS_IDLE") {
217 LOG(ERROR) << "Update failed.";
218 exit(1);
219 }
220 if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") {
221 LOG(INFO) << "Update succeeded -- reboot needed.";
222 exit(0);
223 }
224 return TRUE;
225}
226
227// This is similar to watching for updates but rather than registering
228// a signal watch, activelly poll the daemon just in case it stops
229// sending notifications.
230void CompleteUpdate() {
231 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
232 g_timeout_add_seconds(5, CompleteUpdateSource, NULL);
233 g_main_loop_run(loop);
234 g_main_loop_unref(loop);
235}
236
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700237} // namespace {}
238
239int main(int argc, char** argv) {
240 // Boilerplate init commands.
241 g_type_init();
242 g_thread_init(NULL);
243 dbus_g_thread_init();
244 chromeos_update_engine::Subprocess::Init();
245 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700246
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700247 if (FLAGS_status) {
248 LOG(INFO) << "Querying Update Engine status...";
Darin Petkov58529db2010-08-13 09:19:47 -0700249 if (!GetStatus(NULL)) {
250 LOG(FATAL) << "GetStatus failed.";
251 return 1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700252 }
253 return 0;
254 }
Darin Petkov58529db2010-08-13 09:19:47 -0700255
Darin Petkov8daa3242010-10-25 13:28:47 -0700256 // First, update the track if requested.
257 if (!FLAGS_track.empty()) {
258 SetTrack(FLAGS_track);
259 }
260
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900261 // Show the track if requested.
262 if (FLAGS_show_track) {
263 LOG(INFO) << "Track: " << GetTrack();
264 }
265
Darin Petkov58529db2010-08-13 09:19:47 -0700266 // Initiate an update check, if necessary.
267 if (FLAGS_check_for_update ||
268 FLAGS_update ||
269 !FLAGS_app_version.empty() ||
270 !FLAGS_omaha_url.empty()) {
Darin Petkov296889c2010-07-23 16:20:54 -0700271 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700272 string app_version = FLAGS_app_version;
273 if (FLAGS_update && app_version.empty()) {
274 app_version = "ForcedUpdate";
275 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700276 }
Darin Petkov58529db2010-08-13 09:19:47 -0700277 LOG(INFO) << "Initiating update check and install.";
278 CHECK(CheckForUpdates(app_version, FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700279 << "Update check/initiate update failed.";
Darin Petkov58529db2010-08-13 09:19:47 -0700280
281 // Wait for an update to complete.
282 if (FLAGS_update) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700283 LOG(INFO) << "Waiting for update to complete.";
Darin Petkov58529db2010-08-13 09:19:47 -0700284 CompleteUpdate(); // Should never return.
285 return 1;
286 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700287 return 0;
288 }
Darin Petkov58529db2010-08-13 09:19:47 -0700289
290 // Start watching for updates.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700291 if (FLAGS_watch_for_updates) {
Darin Petkov296889c2010-07-23 16:20:54 -0700292 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700293 LOG(INFO) << "Watching for status updates.";
294 WatchForUpdates(); // Should never return.
295 return 1;
296 }
Darin Petkov58529db2010-08-13 09:19:47 -0700297
Darin Petkov296889c2010-07-23 16:20:54 -0700298 if (FLAGS_reboot) {
299 LOG(INFO) << "Requesting a reboot...";
300 CHECK(RebootIfNeeded());
301 return 0;
302 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700303
Darin Petkov8daa3242010-10-25 13:28:47 -0700304 LOG(INFO) << "Done.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700305 return 0;
306}