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