OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <gflags/gflags.h> | 5 #include <gflags/gflags.h> |
6 #include <glib.h> | 6 #include <glib.h> |
7 | 7 |
| 8 #include "update_engine/marshal.glibmarshal.h" |
8 #include "update_engine/dbus_constants.h" | 9 #include "update_engine/dbus_constants.h" |
9 #include "update_engine/subprocess.h" | 10 #include "update_engine/subprocess.h" |
10 #include "update_engine/utils.h" | 11 #include "update_engine/utils.h" |
11 | 12 |
12 extern "C" { | 13 extern "C" { |
13 #include "update_engine/update_engine.dbusclient.h" | 14 #include "update_engine/update_engine.dbusclient.h" |
14 } | 15 } |
15 | 16 |
16 using chromeos_update_engine::kUpdateEngineServiceName; | 17 using chromeos_update_engine::kUpdateEngineServiceName; |
17 using chromeos_update_engine::kUpdateEngineServicePath; | 18 using chromeos_update_engine::kUpdateEngineServicePath; |
18 using chromeos_update_engine::kUpdateEngineServiceInterface; | 19 using chromeos_update_engine::kUpdateEngineServiceInterface; |
19 using chromeos_update_engine::utils::GetGErrorMessage; | 20 using chromeos_update_engine::utils::GetGErrorMessage; |
20 | 21 |
21 DEFINE_bool(status, false, "Print the status to stdout."); | 22 DEFINE_bool(status, false, "Print the status to stdout."); |
22 DEFINE_bool(force_update, false, | 23 DEFINE_bool(force_update, false, |
23 "Force an update, even over an expensive network."); | 24 "Force an update, even over an expensive network."); |
24 DEFINE_bool(check_for_update, false, | 25 DEFINE_bool(check_for_update, false, |
25 "Initiate check for updates."); | 26 "Initiate check for updates."); |
| 27 DEFINE_bool(watch_for_updates, false, |
| 28 "Listen for status updates and print them to the screen."); |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 bool GetStatus() { | 32 bool GetProxy(DBusGProxy** out_proxy) { |
30 DBusGConnection *bus; | 33 DBusGConnection* bus; |
31 DBusGProxy *proxy; | 34 DBusGProxy* proxy; |
32 GError *error = NULL; | 35 GError* error = NULL; |
33 | 36 |
34 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); | 37 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
35 if (!bus) { | 38 if (!bus) { |
36 LOG(FATAL) << "Failed to get bus"; | 39 LOG(FATAL) << "Failed to get bus"; |
37 } | 40 } |
38 proxy = dbus_g_proxy_new_for_name_owner(bus, | 41 proxy = dbus_g_proxy_new_for_name_owner(bus, |
39 kUpdateEngineServiceName, | 42 kUpdateEngineServiceName, |
40 kUpdateEngineServicePath, | 43 kUpdateEngineServicePath, |
41 kUpdateEngineServiceInterface, | 44 kUpdateEngineServiceInterface, |
42 &error); | 45 &error); |
43 if (!proxy) { | 46 if (!proxy) { |
44 LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error); | 47 LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error); |
45 } | 48 } |
| 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)); |
46 | 73 |
47 gint64 last_checked_time = 0; | 74 gint64 last_checked_time = 0; |
48 gdouble progress = 0.0; | 75 gdouble progress = 0.0; |
49 char* current_op = NULL; | 76 char* current_op = NULL; |
50 char* new_version = NULL; | 77 char* new_version = NULL; |
51 gint64 new_size = 0; | 78 gint64 new_size = 0; |
52 | 79 |
53 gboolean rc = org_chromium_UpdateEngineInterface_get_status( | 80 gboolean rc = org_chromium_UpdateEngineInterface_get_status( |
54 proxy, | 81 proxy, |
55 &last_checked_time, | 82 &last_checked_time, |
56 &progress, | 83 &progress, |
57 ¤t_op, | 84 ¤t_op, |
58 &new_version, | 85 &new_version, |
59 &new_size, | 86 &new_size, |
60 &error); | 87 &error); |
61 if (rc == FALSE) { | 88 if (rc == FALSE) { |
62 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); | 89 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); |
63 } | 90 } |
64 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" | 91 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
65 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", | 92 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
66 last_checked_time, | 93 last_checked_time, |
67 progress, | 94 progress, |
68 current_op, | 95 current_op, |
69 new_version, | 96 new_version, |
70 new_size); | 97 new_size); |
71 return true; | 98 return true; |
72 } | 99 } |
73 | 100 |
| 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 |
74 bool CheckForUpdates(bool force) { | 137 bool CheckForUpdates(bool force) { |
| 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); |
75 return true; | 147 return true; |
76 } | 148 } |
77 | 149 |
78 } // namespace {} | 150 } // namespace {} |
79 | 151 |
80 int main(int argc, char** argv) { | 152 int main(int argc, char** argv) { |
81 // Boilerplate init commands. | 153 // Boilerplate init commands. |
82 g_type_init(); | 154 g_type_init(); |
83 g_thread_init(NULL); | 155 g_thread_init(NULL); |
84 dbus_g_thread_init(); | 156 dbus_g_thread_init(); |
85 chromeos_update_engine::Subprocess::Init(); | 157 chromeos_update_engine::Subprocess::Init(); |
86 google::ParseCommandLineFlags(&argc, &argv, true); | 158 google::ParseCommandLineFlags(&argc, &argv, true); |
87 | 159 |
88 if (FLAGS_status) { | 160 if (FLAGS_status) { |
89 LOG(INFO) << "Querying Update Engine status..."; | 161 LOG(INFO) << "Querying Update Engine status..."; |
90 if (!GetStatus()) { | 162 if (!GetStatus()) { |
91 LOG(FATAL) << "GetStatus() failed."; | 163 LOG(FATAL) << "GetStatus() failed."; |
92 } | 164 } |
93 return 0; | 165 return 0; |
94 } | 166 } |
95 if (FLAGS_force_update || FLAGS_check_for_update) { | 167 if (FLAGS_force_update || FLAGS_check_for_update) { |
96 LOG(INFO) << "Initiating update check and install."; | 168 LOG(INFO) << "Initiating update check and install."; |
97 if (FLAGS_force_update) { | 169 if (FLAGS_force_update) { |
98 LOG(INFO) << "Will not abort due to being on expensive network."; | 170 LOG(INFO) << "Will not abort due to being on expensive network."; |
99 } | 171 } |
100 CHECK(CheckForUpdates(FLAGS_force_update)) | 172 CHECK(CheckForUpdates(FLAGS_force_update)) |
101 << "Update check/initiate update failed."; | 173 << "Update check/initiate update failed."; |
102 return 0; | 174 return 0; |
103 } | 175 } |
| 176 if (FLAGS_watch_for_updates) { |
| 177 LOG(INFO) << "Watching for status updates."; |
| 178 WatchForUpdates(); // Should never return. |
| 179 return 1; |
| 180 } |
104 | 181 |
105 LOG(INFO) << "No flags specified. Exiting."; | 182 LOG(INFO) << "No flags specified. Exiting."; |
106 return 0; | 183 return 0; |
107 } | 184 } |
OLD | NEW |