blob: 7bbc62f6de5edfc38764409adabe60903260df85 [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.");
Darin Petkov5a7f5652010-07-22 21:40:09 -070029DEFINE_bool(status, false, "Print the status to stdout.");
Darin Petkov58529db2010-08-13 09:19:47 -070030DEFINE_bool(update, false, "Forces an update and waits for its completion. "
31 "Exit status is 0 if the update succeeded, and 1 otherwise.");
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070032DEFINE_bool(watch_for_updates, false,
33 "Listen for status updates and print them to the screen.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070034
35namespace {
36
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070037bool GetProxy(DBusGProxy** out_proxy) {
38 DBusGConnection* bus;
39 DBusGProxy* proxy;
40 GError* error = NULL;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070041
42 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
43 if (!bus) {
44 LOG(FATAL) << "Failed to get bus";
45 }
46 proxy = dbus_g_proxy_new_for_name_owner(bus,
47 kUpdateEngineServiceName,
48 kUpdateEngineServicePath,
49 kUpdateEngineServiceInterface,
50 &error);
51 if (!proxy) {
Kenneth Waters71e8e5c2010-09-10 09:50:40 -070052 LOG(FATAL) << "Error getting dbus proxy for "
53 << kUpdateEngineServiceName << ": " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070054 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070055 *out_proxy = proxy;
56 return true;
57}
58
59static void StatusUpdateSignalHandler(DBusGProxy* proxy,
60 int64_t last_checked_time,
61 double progress,
62 gchar* current_operation,
63 gchar* new_version,
64 int64_t new_size,
65 void* user_data) {
66 LOG(INFO) << "Got status update:";
67 LOG(INFO) << " last_checked_time: " << last_checked_time;
68 LOG(INFO) << " progress: " << progress;
69 LOG(INFO) << " current_operation: " << current_operation;
70 LOG(INFO) << " new_version: " << new_version;
71 LOG(INFO) << " new_size: " << new_size;
72}
73
Darin Petkov58529db2010-08-13 09:19:47 -070074// If |op| is non-NULL, sets it to the current operation string or an
75// empty string if unable to obtain the current status.
76bool GetStatus(string* op) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070077 DBusGProxy* proxy;
78 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -070079
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070080 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070081
82 gint64 last_checked_time = 0;
83 gdouble progress = 0.0;
84 char* current_op = NULL;
85 char* new_version = NULL;
86 gint64 new_size = 0;
87
88 gboolean rc = org_chromium_UpdateEngineInterface_get_status(
89 proxy,
90 &last_checked_time,
91 &progress,
92 &current_op,
93 &new_version,
94 &new_size,
95 &error);
96 if (rc == FALSE) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070097 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070098 }
99 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
100 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
101 last_checked_time,
102 progress,
103 current_op,
104 new_version,
105 new_size);
Darin Petkov58529db2010-08-13 09:19:47 -0700106 if (op) {
107 *op = current_op ? current_op : "";
108 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700109 return true;
110}
111
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700112// Should never return.
113void WatchForUpdates() {
114 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700115
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700116 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700117
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700118 // Register marshaller
119 dbus_g_object_register_marshaller(
120 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
121 G_TYPE_NONE,
122 G_TYPE_INT64,
123 G_TYPE_DOUBLE,
124 G_TYPE_STRING,
125 G_TYPE_STRING,
126 G_TYPE_INT64,
127 G_TYPE_INVALID);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700128
129 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700130 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700131 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700132 G_TYPE_INT64,
133 G_TYPE_DOUBLE,
134 G_TYPE_STRING,
135 G_TYPE_STRING,
136 G_TYPE_INT64,
137 G_TYPE_INVALID);
138 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
139 dbus_g_proxy_connect_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700140 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700141 G_CALLBACK(StatusUpdateSignalHandler),
142 NULL,
143 NULL);
144 g_main_loop_run(loop);
145 g_main_loop_unref(loop);
146}
147
Darin Petkov58529db2010-08-13 09:19:47 -0700148bool CheckForUpdates(const string& app_version, const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700149 DBusGProxy* proxy;
150 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700151
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700152 CHECK(GetProxy(&proxy));
153
154 gboolean rc =
Darin Petkov5a7f5652010-07-22 21:40:09 -0700155 org_chromium_UpdateEngineInterface_attempt_update(proxy,
156 app_version.c_str(),
157 omaha_url.c_str(),
158 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700159 CHECK_EQ(rc, TRUE) << "Error checking for update: "
160 << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700161 return true;
162}
163
Darin Petkov296889c2010-07-23 16:20:54 -0700164bool RebootIfNeeded() {
165 DBusGProxy* proxy;
166 GError* error = NULL;
167
168 CHECK(GetProxy(&proxy));
169
170 gboolean rc =
171 org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error);
172 // Reboot error code doesn't necessarily mean that a reboot
173 // failed. For example, D-Bus may be shutdown before we receive the
174 // result.
175 LOG_IF(INFO, !rc) << "Reboot error message: " << GetGErrorMessage(error);
176 return true;
177}
178
Darin Petkov58529db2010-08-13 09:19:47 -0700179static gboolean CompleteUpdateSource(gpointer data) {
180 string current_op;
181 if (!GetStatus(&current_op) || current_op == "UPDATE_STATUS_IDLE") {
182 LOG(ERROR) << "Update failed.";
183 exit(1);
184 }
185 if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") {
186 LOG(INFO) << "Update succeeded -- reboot needed.";
187 exit(0);
188 }
189 return TRUE;
190}
191
192// This is similar to watching for updates but rather than registering
193// a signal watch, activelly poll the daemon just in case it stops
194// sending notifications.
195void CompleteUpdate() {
196 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
197 g_timeout_add_seconds(5, CompleteUpdateSource, NULL);
198 g_main_loop_run(loop);
199 g_main_loop_unref(loop);
200}
201
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700202} // namespace {}
203
204int main(int argc, char** argv) {
205 // Boilerplate init commands.
206 g_type_init();
207 g_thread_init(NULL);
208 dbus_g_thread_init();
209 chromeos_update_engine::Subprocess::Init();
210 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700211
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700212 if (FLAGS_status) {
213 LOG(INFO) << "Querying Update Engine status...";
Darin Petkov58529db2010-08-13 09:19:47 -0700214 if (!GetStatus(NULL)) {
215 LOG(FATAL) << "GetStatus failed.";
216 return 1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700217 }
218 return 0;
219 }
Darin Petkov58529db2010-08-13 09:19:47 -0700220
221 // Initiate an update check, if necessary.
222 if (FLAGS_check_for_update ||
223 FLAGS_update ||
224 !FLAGS_app_version.empty() ||
225 !FLAGS_omaha_url.empty()) {
Darin Petkov296889c2010-07-23 16:20:54 -0700226 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700227 string app_version = FLAGS_app_version;
228 if (FLAGS_update && app_version.empty()) {
229 app_version = "ForcedUpdate";
230 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700231 }
Darin Petkov58529db2010-08-13 09:19:47 -0700232 LOG(INFO) << "Initiating update check and install.";
233 CHECK(CheckForUpdates(app_version, FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700234 << "Update check/initiate update failed.";
Darin Petkov58529db2010-08-13 09:19:47 -0700235
236 // Wait for an update to complete.
237 if (FLAGS_update) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700238 LOG(INFO) << "Waiting for update to complete.";
Darin Petkov58529db2010-08-13 09:19:47 -0700239 CompleteUpdate(); // Should never return.
240 return 1;
241 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700242 return 0;
243 }
Darin Petkov58529db2010-08-13 09:19:47 -0700244
245 // Start watching for updates.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700246 if (FLAGS_watch_for_updates) {
Darin Petkov296889c2010-07-23 16:20:54 -0700247 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700248 LOG(INFO) << "Watching for status updates.";
249 WatchForUpdates(); // Should never return.
250 return 1;
251 }
Darin Petkov58529db2010-08-13 09:19:47 -0700252
Darin Petkov296889c2010-07-23 16:20:54 -0700253 if (FLAGS_reboot) {
254 LOG(INFO) << "Requesting a reboot...";
255 CHECK(RebootIfNeeded());
256 return 0;
257 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700258
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700259 LOG(INFO) << "No flags specified. Exiting.";
260 return 0;
261}