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 | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 27 | DEFINE_bool(force_update, false, |
| 28 | "Force an update, even over an expensive network."); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 29 | DEFINE_string(omaha_url, "", "The URL of the Omaha update server."); |
| 30 | DEFINE_bool(reboot, false, "Initiate a reboot if needed."); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 31 | DEFINE_bool(status, false, "Print the status to stdout."); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 32 | DEFINE_bool(watch_for_updates, false, |
| 33 | "Listen for status updates and print them to the screen."); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 34 | |
| 35 | namespace { |
| 36 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 37 | bool GetProxy(DBusGProxy** out_proxy) { |
| 38 | DBusGConnection* bus; |
| 39 | DBusGProxy* proxy; |
| 40 | GError* error = NULL; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 41 | |
| 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) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 52 | LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 53 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 54 | *out_proxy = proxy; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | static void StatusUpdateSignalHandler(DBusGProxy* proxy, |
| 59 | int64_t last_checked_time, |
| 60 | double progress, |
| 61 | gchar* current_operation, |
| 62 | gchar* new_version, |
| 63 | int64_t new_size, |
| 64 | void* user_data) { |
| 65 | LOG(INFO) << "Got status update:"; |
| 66 | LOG(INFO) << " last_checked_time: " << last_checked_time; |
| 67 | LOG(INFO) << " progress: " << progress; |
| 68 | LOG(INFO) << " current_operation: " << current_operation; |
| 69 | LOG(INFO) << " new_version: " << new_version; |
| 70 | LOG(INFO) << " new_size: " << new_size; |
| 71 | } |
| 72 | |
| 73 | bool GetStatus() { |
| 74 | DBusGProxy* proxy; |
| 75 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 76 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 77 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 78 | |
| 79 | gint64 last_checked_time = 0; |
| 80 | gdouble progress = 0.0; |
| 81 | char* current_op = NULL; |
| 82 | char* new_version = NULL; |
| 83 | gint64 new_size = 0; |
| 84 | |
| 85 | gboolean rc = org_chromium_UpdateEngineInterface_get_status( |
| 86 | proxy, |
| 87 | &last_checked_time, |
| 88 | &progress, |
| 89 | ¤t_op, |
| 90 | &new_version, |
| 91 | &new_size, |
| 92 | &error); |
| 93 | if (rc == FALSE) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 94 | LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 95 | } |
| 96 | printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
| 97 | "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
| 98 | last_checked_time, |
| 99 | progress, |
| 100 | current_op, |
| 101 | new_version, |
| 102 | new_size); |
| 103 | return true; |
| 104 | } |
| 105 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 106 | // Should never return. |
| 107 | void WatchForUpdates() { |
| 108 | DBusGProxy* proxy; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 109 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 110 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 111 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 112 | // Register marshaller |
| 113 | dbus_g_object_register_marshaller( |
| 114 | update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64, |
| 115 | G_TYPE_NONE, |
| 116 | G_TYPE_INT64, |
| 117 | G_TYPE_DOUBLE, |
| 118 | G_TYPE_STRING, |
| 119 | G_TYPE_STRING, |
| 120 | G_TYPE_INT64, |
| 121 | G_TYPE_INVALID); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 122 | |
| 123 | static const char kStatusUpdate[] = "StatusUpdate"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 124 | dbus_g_proxy_add_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 125 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 126 | G_TYPE_INT64, |
| 127 | G_TYPE_DOUBLE, |
| 128 | G_TYPE_STRING, |
| 129 | G_TYPE_STRING, |
| 130 | G_TYPE_INT64, |
| 131 | G_TYPE_INVALID); |
| 132 | GMainLoop* loop = g_main_loop_new (NULL, TRUE); |
| 133 | dbus_g_proxy_connect_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 134 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 135 | G_CALLBACK(StatusUpdateSignalHandler), |
| 136 | NULL, |
| 137 | NULL); |
| 138 | g_main_loop_run(loop); |
| 139 | g_main_loop_unref(loop); |
| 140 | } |
| 141 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 142 | bool CheckForUpdates(bool force, const string& app_version, |
| 143 | const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 144 | DBusGProxy* proxy; |
| 145 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 146 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 147 | CHECK(GetProxy(&proxy)); |
| 148 | |
| 149 | gboolean rc = |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 150 | org_chromium_UpdateEngineInterface_attempt_update(proxy, |
| 151 | app_version.c_str(), |
| 152 | omaha_url.c_str(), |
| 153 | &error); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 154 | CHECK_EQ(rc, TRUE) << "Error checking for update: " |
| 155 | << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 159 | bool RebootIfNeeded() { |
| 160 | DBusGProxy* proxy; |
| 161 | GError* error = NULL; |
| 162 | |
| 163 | CHECK(GetProxy(&proxy)); |
| 164 | |
| 165 | gboolean rc = |
| 166 | org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error); |
| 167 | // Reboot error code doesn't necessarily mean that a reboot |
| 168 | // failed. For example, D-Bus may be shutdown before we receive the |
| 169 | // result. |
| 170 | LOG_IF(INFO, !rc) << "Reboot error message: " << GetGErrorMessage(error); |
| 171 | return true; |
| 172 | } |
| 173 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 174 | } // namespace {} |
| 175 | |
| 176 | int main(int argc, char** argv) { |
| 177 | // Boilerplate init commands. |
| 178 | g_type_init(); |
| 179 | g_thread_init(NULL); |
| 180 | dbus_g_thread_init(); |
| 181 | chromeos_update_engine::Subprocess::Init(); |
| 182 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 183 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 184 | if (FLAGS_status) { |
| 185 | LOG(INFO) << "Querying Update Engine status..."; |
| 186 | if (!GetStatus()) { |
| 187 | LOG(FATAL) << "GetStatus() failed."; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 191 | if (FLAGS_force_update || FLAGS_check_for_update || |
| 192 | !FLAGS_app_version.empty() || !FLAGS_omaha_url.empty()) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 193 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 194 | LOG(INFO) << "Initiating update check and install."; |
| 195 | if (FLAGS_force_update) { |
| 196 | LOG(INFO) << "Will not abort due to being on expensive network."; |
| 197 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 198 | CHECK(CheckForUpdates(FLAGS_force_update, FLAGS_app_version, |
| 199 | FLAGS_omaha_url)) |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 200 | << "Update check/initiate update failed."; |
| 201 | return 0; |
| 202 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 203 | if (FLAGS_watch_for_updates) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 204 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 205 | LOG(INFO) << "Watching for status updates."; |
| 206 | WatchForUpdates(); // Should never return. |
| 207 | return 1; |
| 208 | } |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 209 | if (FLAGS_reboot) { |
| 210 | LOG(INFO) << "Requesting a reboot..."; |
| 211 | CHECK(RebootIfNeeded()); |
| 212 | return 0; |
| 213 | } |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 214 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 215 | LOG(INFO) << "No flags specified. Exiting."; |
| 216 | return 0; |
| 217 | } |