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; |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 41 | DBusGProxy* proxy = NULL; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 42 | GError* error = NULL; |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 43 | const int kTries = 4; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 44 | |
| 45 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 46 | if (!bus) { |
| 47 | LOG(FATAL) << "Failed to get bus"; |
| 48 | } |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 49 | 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 57 | if (!proxy) { |
Kenneth Waters | 71e8e5c | 2010-09-10 09:50:40 -0700 | [diff] [blame] | 58 | LOG(FATAL) << "Error getting dbus proxy for " |
| 59 | << kUpdateEngineServiceName << ": " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 60 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 61 | *out_proxy = proxy; |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | static 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 Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 80 | // If |op| is non-NULL, sets it to the current operation string or an |
| 81 | // empty string if unable to obtain the current status. |
| 82 | bool GetStatus(string* op) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 83 | DBusGProxy* proxy; |
| 84 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 85 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 86 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 87 | |
| 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 | ¤t_op, |
| 99 | &new_version, |
| 100 | &new_size, |
| 101 | &error); |
| 102 | if (rc == FALSE) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 103 | LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 104 | } |
| 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 Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 112 | if (op) { |
| 113 | *op = current_op ? current_op : ""; |
| 114 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 118 | // Should never return. |
| 119 | void WatchForUpdates() { |
| 120 | DBusGProxy* proxy; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 121 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 122 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 123 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 124 | // 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 Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 134 | |
| 135 | static const char kStatusUpdate[] = "StatusUpdate"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 136 | dbus_g_proxy_add_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 137 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 138 | 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 Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 146 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 147 | G_CALLBACK(StatusUpdateSignalHandler), |
| 148 | NULL, |
| 149 | NULL); |
| 150 | g_main_loop_run(loop); |
| 151 | g_main_loop_unref(loop); |
| 152 | } |
| 153 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 154 | bool CheckForUpdates(const string& app_version, const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 155 | DBusGProxy* proxy; |
| 156 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 157 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 158 | CHECK(GetProxy(&proxy)); |
| 159 | |
| 160 | gboolean rc = |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 161 | org_chromium_UpdateEngineInterface_attempt_update(proxy, |
| 162 | app_version.c_str(), |
| 163 | omaha_url.c_str(), |
| 164 | &error); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 165 | CHECK_EQ(rc, TRUE) << "Error checking for update: " |
| 166 | << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 170 | bool 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 Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 185 | void 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 Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 197 | LOG(INFO) << "Track permanently set to: " << track; |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 200 | string 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 Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 218 | static gboolean CompleteUpdateSource(gpointer data) { |
| 219 | string current_op; |
| 220 | if (!GetStatus(¤t_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. |
| 234 | void 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 241 | } // namespace {} |
| 242 | |
| 243 | int 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 Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 250 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 251 | if (FLAGS_status) { |
| 252 | LOG(INFO) << "Querying Update Engine status..."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 253 | if (!GetStatus(NULL)) { |
| 254 | LOG(FATAL) << "GetStatus failed."; |
| 255 | return 1; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 256 | } |
| 257 | return 0; |
| 258 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 259 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 260 | // First, update the track if requested. |
| 261 | if (!FLAGS_track.empty()) { |
| 262 | SetTrack(FLAGS_track); |
| 263 | } |
| 264 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 265 | // Show the track if requested. |
| 266 | if (FLAGS_show_track) { |
| 267 | LOG(INFO) << "Track: " << GetTrack(); |
| 268 | } |
| 269 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 270 | // 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 Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 275 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 276 | 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 280 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 281 | LOG(INFO) << "Initiating update check and install."; |
| 282 | CHECK(CheckForUpdates(app_version, FLAGS_omaha_url)) |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 283 | << "Update check/initiate update failed."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 284 | |
| 285 | // Wait for an update to complete. |
| 286 | if (FLAGS_update) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 287 | LOG(INFO) << "Waiting for update to complete."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 288 | CompleteUpdate(); // Should never return. |
| 289 | return 1; |
| 290 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 291 | return 0; |
| 292 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 293 | |
| 294 | // Start watching for updates. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 295 | if (FLAGS_watch_for_updates) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 296 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 297 | LOG(INFO) << "Watching for status updates."; |
| 298 | WatchForUpdates(); // Should never return. |
| 299 | return 1; |
| 300 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 301 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 302 | if (FLAGS_reboot) { |
| 303 | LOG(INFO) << "Requesting a reboot..."; |
| 304 | CHECK(RebootIfNeeded()); |
| 305 | return 0; |
| 306 | } |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 307 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 308 | LOG(INFO) << "Done."; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |