Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 1 | // 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 Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 7 | #include <gflags/gflags.h> |
| 8 | #include <glib.h> |
| 9 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 10 | #include "update_engine/marshal.glibmarshal.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 11 | #include "update_engine/dbus_constants.h" |
| 12 | #include "update_engine/subprocess.h" |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | extern "C" { |
| 16 | #include "update_engine/update_engine.dbusclient.h" |
| 17 | } |
| 18 | |
| 19 | using chromeos_update_engine::kUpdateEngineServiceName; |
| 20 | using chromeos_update_engine::kUpdateEngineServicePath; |
| 21 | using chromeos_update_engine::kUpdateEngineServiceInterface; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 22 | using chromeos_update_engine::utils::GetGErrorMessage; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 23 | using std::string; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 24 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 25 | DEFINE_string(app_version, "", "Force the current app version."); |
| 26 | DEFINE_bool(check_for_update, false, "Initiate check for updates."); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 27 | DEFINE_string(omaha_url, "", "The URL of the Omaha update server."); |
| 28 | DEFINE_bool(reboot, false, "Initiate a reboot if needed."); |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 29 | DEFINE_bool(show_track, false, "Show the update track."); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 30 | DEFINE_bool(status, false, "Print the status to stdout."); |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 31 | DEFINE_string(track, "", "Permanently change the update track."); |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 32 | DEFINE_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 Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 34 | DEFINE_bool(watch_for_updates, false, |
| 35 | "Listen for status updates and print them to the screen."); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 36 | |
| 37 | namespace { |
| 38 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 39 | bool GetProxy(DBusGProxy** out_proxy) { |
| 40 | DBusGConnection* bus; |
| 41 | DBusGProxy* proxy; |
| 42 | GError* error = NULL; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 43 | |
| 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 Waters | 71e8e5c | 2010-09-10 09:50:40 -0700 | [diff] [blame] | 54 | LOG(FATAL) << "Error getting dbus proxy for " |
| 55 | << kUpdateEngineServiceName << ": " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 56 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 57 | *out_proxy = proxy; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | static 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 Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 76 | // If |op| is non-NULL, sets it to the current operation string or an |
| 77 | // empty string if unable to obtain the current status. |
| 78 | bool GetStatus(string* op) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 79 | DBusGProxy* proxy; |
| 80 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 81 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 82 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 83 | |
| 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 | ¤t_op, |
| 95 | &new_version, |
| 96 | &new_size, |
| 97 | &error); |
| 98 | if (rc == FALSE) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 99 | LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 100 | } |
| 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 Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 108 | if (op) { |
| 109 | *op = current_op ? current_op : ""; |
| 110 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 114 | // Should never return. |
| 115 | void WatchForUpdates() { |
| 116 | DBusGProxy* proxy; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 117 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 118 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 119 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 120 | // 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 Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 130 | |
| 131 | static const char kStatusUpdate[] = "StatusUpdate"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 132 | dbus_g_proxy_add_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 133 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 134 | 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 Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 142 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 143 | G_CALLBACK(StatusUpdateSignalHandler), |
| 144 | NULL, |
| 145 | NULL); |
| 146 | g_main_loop_run(loop); |
| 147 | g_main_loop_unref(loop); |
| 148 | } |
| 149 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 150 | bool CheckForUpdates(const string& app_version, const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 151 | DBusGProxy* proxy; |
| 152 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 153 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 154 | CHECK(GetProxy(&proxy)); |
| 155 | |
| 156 | gboolean rc = |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 157 | org_chromium_UpdateEngineInterface_attempt_update(proxy, |
| 158 | app_version.c_str(), |
| 159 | omaha_url.c_str(), |
| 160 | &error); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 161 | CHECK_EQ(rc, TRUE) << "Error checking for update: " |
| 162 | << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 166 | bool 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 Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 181 | void 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 Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 193 | LOG(INFO) << "Track permanently set to: " << track; |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 196 | string 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 Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 214 | static gboolean CompleteUpdateSource(gpointer data) { |
| 215 | string current_op; |
| 216 | if (!GetStatus(¤t_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. |
| 230 | void 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 237 | } // namespace {} |
| 238 | |
| 239 | int 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 Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 246 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 247 | if (FLAGS_status) { |
| 248 | LOG(INFO) << "Querying Update Engine status..."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 249 | if (!GetStatus(NULL)) { |
| 250 | LOG(FATAL) << "GetStatus failed."; |
| 251 | return 1; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 252 | } |
| 253 | return 0; |
| 254 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 255 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 256 | // First, update the track if requested. |
| 257 | if (!FLAGS_track.empty()) { |
| 258 | SetTrack(FLAGS_track); |
| 259 | } |
| 260 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 261 | // Show the track if requested. |
| 262 | if (FLAGS_show_track) { |
| 263 | LOG(INFO) << "Track: " << GetTrack(); |
| 264 | } |
| 265 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 266 | // 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 Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 271 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 272 | 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 276 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 277 | LOG(INFO) << "Initiating update check and install."; |
| 278 | CHECK(CheckForUpdates(app_version, FLAGS_omaha_url)) |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 279 | << "Update check/initiate update failed."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 280 | |
| 281 | // Wait for an update to complete. |
| 282 | if (FLAGS_update) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 283 | LOG(INFO) << "Waiting for update to complete."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 284 | CompleteUpdate(); // Should never return. |
| 285 | return 1; |
| 286 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 287 | return 0; |
| 288 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 289 | |
| 290 | // Start watching for updates. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 291 | if (FLAGS_watch_for_updates) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 292 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 293 | LOG(INFO) << "Watching for status updates."; |
| 294 | WatchForUpdates(); // Should never return. |
| 295 | return 1; |
| 296 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 297 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 298 | if (FLAGS_reboot) { |
| 299 | LOG(INFO) << "Requesting a reboot..."; |
| 300 | CHECK(RebootIfNeeded()); |
| 301 | return 0; |
| 302 | } |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 303 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 304 | LOG(INFO) << "Done."; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 305 | return 0; |
| 306 | } |