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 | |
| 5 | #include <gflags/gflags.h> |
| 6 | #include <glib.h> |
| 7 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 8 | #include "update_engine/marshal.glibmarshal.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 9 | #include "update_engine/dbus_constants.h" |
| 10 | #include "update_engine/subprocess.h" |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | extern "C" { |
| 14 | #include "update_engine/update_engine.dbusclient.h" |
| 15 | } |
| 16 | |
| 17 | using chromeos_update_engine::kUpdateEngineServiceName; |
| 18 | using chromeos_update_engine::kUpdateEngineServicePath; |
| 19 | using chromeos_update_engine::kUpdateEngineServiceInterface; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 20 | using chromeos_update_engine::utils::GetGErrorMessage; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | |
| 22 | DEFINE_bool(status, false, "Print the status to stdout."); |
| 23 | DEFINE_bool(force_update, false, |
| 24 | "Force an update, even over an expensive network."); |
| 25 | DEFINE_bool(check_for_update, false, |
| 26 | "Initiate check for updates."); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 27 | DEFINE_bool(watch_for_updates, false, |
| 28 | "Listen for status updates and print them to the screen."); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 32 | bool GetProxy(DBusGProxy** out_proxy) { |
| 33 | DBusGConnection* bus; |
| 34 | DBusGProxy* proxy; |
| 35 | GError* error = NULL; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 36 | |
| 37 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 38 | if (!bus) { |
| 39 | LOG(FATAL) << "Failed to get bus"; |
| 40 | } |
| 41 | proxy = dbus_g_proxy_new_for_name_owner(bus, |
| 42 | kUpdateEngineServiceName, |
| 43 | kUpdateEngineServicePath, |
| 44 | kUpdateEngineServiceInterface, |
| 45 | &error); |
| 46 | if (!proxy) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 47 | LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 48 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 49 | *out_proxy = proxy; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | static void StatusUpdateSignalHandler(DBusGProxy* proxy, |
| 54 | int64_t last_checked_time, |
| 55 | double progress, |
| 56 | gchar* current_operation, |
| 57 | gchar* new_version, |
| 58 | int64_t new_size, |
| 59 | void* user_data) { |
| 60 | LOG(INFO) << "Got status update:"; |
| 61 | LOG(INFO) << " last_checked_time: " << last_checked_time; |
| 62 | LOG(INFO) << " progress: " << progress; |
| 63 | LOG(INFO) << " current_operation: " << current_operation; |
| 64 | LOG(INFO) << " new_version: " << new_version; |
| 65 | LOG(INFO) << " new_size: " << new_size; |
| 66 | } |
| 67 | |
| 68 | bool GetStatus() { |
| 69 | DBusGProxy* proxy; |
| 70 | GError* error = NULL; |
| 71 | |
| 72 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 73 | |
| 74 | gint64 last_checked_time = 0; |
| 75 | gdouble progress = 0.0; |
| 76 | char* current_op = NULL; |
| 77 | char* new_version = NULL; |
| 78 | gint64 new_size = 0; |
| 79 | |
| 80 | gboolean rc = org_chromium_UpdateEngineInterface_get_status( |
| 81 | proxy, |
| 82 | &last_checked_time, |
| 83 | &progress, |
| 84 | ¤t_op, |
| 85 | &new_version, |
| 86 | &new_size, |
| 87 | &error); |
| 88 | if (rc == FALSE) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 89 | LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 90 | } |
| 91 | printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
| 92 | "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
| 93 | last_checked_time, |
| 94 | progress, |
| 95 | current_op, |
| 96 | new_version, |
| 97 | new_size); |
| 98 | return true; |
| 99 | } |
| 100 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 101 | // Should never return. |
| 102 | void WatchForUpdates() { |
| 103 | DBusGProxy* proxy; |
| 104 | |
| 105 | CHECK(GetProxy(&proxy)); |
| 106 | |
| 107 | // Register marshaller |
| 108 | dbus_g_object_register_marshaller( |
| 109 | update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64, |
| 110 | G_TYPE_NONE, |
| 111 | G_TYPE_INT64, |
| 112 | G_TYPE_DOUBLE, |
| 113 | G_TYPE_STRING, |
| 114 | G_TYPE_STRING, |
| 115 | G_TYPE_INT64, |
| 116 | G_TYPE_INVALID); |
| 117 | |
| 118 | // TODO(adlr): make StatusUpdate a const string |
| 119 | dbus_g_proxy_add_signal(proxy, |
| 120 | "StatusUpdate", |
| 121 | G_TYPE_INT64, |
| 122 | G_TYPE_DOUBLE, |
| 123 | G_TYPE_STRING, |
| 124 | G_TYPE_STRING, |
| 125 | G_TYPE_INT64, |
| 126 | G_TYPE_INVALID); |
| 127 | GMainLoop* loop = g_main_loop_new (NULL, TRUE); |
| 128 | dbus_g_proxy_connect_signal(proxy, |
| 129 | "StatusUpdate", |
| 130 | G_CALLBACK(StatusUpdateSignalHandler), |
| 131 | NULL, |
| 132 | NULL); |
| 133 | g_main_loop_run(loop); |
| 134 | g_main_loop_unref(loop); |
| 135 | } |
| 136 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 137 | bool CheckForUpdates(bool force) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 138 | DBusGProxy* proxy; |
| 139 | GError* error = NULL; |
| 140 | |
| 141 | CHECK(GetProxy(&proxy)); |
| 142 | |
| 143 | gboolean rc = |
| 144 | org_chromium_UpdateEngineInterface_check_for_update(proxy, &error); |
| 145 | CHECK_EQ(rc, TRUE) << "Error checking for update: " |
| 146 | << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
| 150 | } // namespace {} |
| 151 | |
| 152 | int main(int argc, char** argv) { |
| 153 | // Boilerplate init commands. |
| 154 | g_type_init(); |
| 155 | g_thread_init(NULL); |
| 156 | dbus_g_thread_init(); |
| 157 | chromeos_update_engine::Subprocess::Init(); |
| 158 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 159 | |
| 160 | if (FLAGS_status) { |
| 161 | LOG(INFO) << "Querying Update Engine status..."; |
| 162 | if (!GetStatus()) { |
| 163 | LOG(FATAL) << "GetStatus() failed."; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | if (FLAGS_force_update || FLAGS_check_for_update) { |
| 168 | LOG(INFO) << "Initiating update check and install."; |
| 169 | if (FLAGS_force_update) { |
| 170 | LOG(INFO) << "Will not abort due to being on expensive network."; |
| 171 | } |
| 172 | CHECK(CheckForUpdates(FLAGS_force_update)) |
| 173 | << "Update check/initiate update failed."; |
| 174 | return 0; |
| 175 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 176 | if (FLAGS_watch_for_updates) { |
| 177 | LOG(INFO) << "Watching for status updates."; |
| 178 | WatchForUpdates(); // Should never return. |
| 179 | return 1; |
| 180 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 181 | |
| 182 | LOG(INFO) << "No flags specified. Exiting."; |
| 183 | return 0; |
| 184 | } |