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 | |
| 8 | #include "update_engine/dbus_constants.h" |
| 9 | #include "update_engine/subprocess.h" |
| 10 | #include "update_engine/utils.h" |
| 11 | |
| 12 | extern "C" { |
| 13 | #include "update_engine/update_engine.dbusclient.h" |
| 14 | } |
| 15 | |
| 16 | using chromeos_update_engine::kUpdateEngineServiceName; |
| 17 | using chromeos_update_engine::kUpdateEngineServicePath; |
| 18 | using chromeos_update_engine::kUpdateEngineServiceInterface; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame^] | 19 | using chromeos_update_engine::utils::GetGErrorMessage; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 20 | |
| 21 | DEFINE_bool(status, false, "Print the status to stdout."); |
| 22 | DEFINE_bool(force_update, false, |
| 23 | "Force an update, even over an expensive network."); |
| 24 | DEFINE_bool(check_for_update, false, |
| 25 | "Initiate check for updates."); |
| 26 | |
| 27 | namespace { |
| 28 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 29 | bool GetStatus() { |
| 30 | DBusGConnection *bus; |
| 31 | DBusGProxy *proxy; |
| 32 | GError *error = NULL; |
| 33 | |
| 34 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 35 | if (!bus) { |
| 36 | LOG(FATAL) << "Failed to get bus"; |
| 37 | } |
| 38 | proxy = dbus_g_proxy_new_for_name_owner(bus, |
| 39 | kUpdateEngineServiceName, |
| 40 | kUpdateEngineServicePath, |
| 41 | kUpdateEngineServiceInterface, |
| 42 | &error); |
| 43 | if (!proxy) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame^] | 44 | LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | gint64 last_checked_time = 0; |
| 48 | gdouble progress = 0.0; |
| 49 | char* current_op = NULL; |
| 50 | char* new_version = NULL; |
| 51 | gint64 new_size = 0; |
| 52 | |
| 53 | gboolean rc = org_chromium_UpdateEngineInterface_get_status( |
| 54 | proxy, |
| 55 | &last_checked_time, |
| 56 | &progress, |
| 57 | ¤t_op, |
| 58 | &new_version, |
| 59 | &new_size, |
| 60 | &error); |
| 61 | if (rc == FALSE) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame^] | 62 | LOG(INFO) << "Error getting status: " << GetGErrorMessage(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 63 | } |
| 64 | printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
| 65 | "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
| 66 | last_checked_time, |
| 67 | progress, |
| 68 | current_op, |
| 69 | new_version, |
| 70 | new_size); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool CheckForUpdates(bool force) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | } // namespace {} |
| 79 | |
| 80 | int main(int argc, char** argv) { |
| 81 | // Boilerplate init commands. |
| 82 | g_type_init(); |
| 83 | g_thread_init(NULL); |
| 84 | dbus_g_thread_init(); |
| 85 | chromeos_update_engine::Subprocess::Init(); |
| 86 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 87 | |
| 88 | if (FLAGS_status) { |
| 89 | LOG(INFO) << "Querying Update Engine status..."; |
| 90 | if (!GetStatus()) { |
| 91 | LOG(FATAL) << "GetStatus() failed."; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | if (FLAGS_force_update || FLAGS_check_for_update) { |
| 96 | LOG(INFO) << "Initiating update check and install."; |
| 97 | if (FLAGS_force_update) { |
| 98 | LOG(INFO) << "Will not abort due to being on expensive network."; |
| 99 | } |
| 100 | CHECK(CheckForUpdates(FLAGS_force_update)) |
| 101 | << "Update check/initiate update failed."; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | LOG(INFO) << "No flags specified. Exiting."; |
| 106 | return 0; |
| 107 | } |