blob: e68a7aa6135c9d28561a2b0a6defc3aef1d161fd [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// 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 Reyes63b96d72010-05-10 13:08:54 -07008#include "update_engine/marshal.glibmarshal.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07009#include "update_engine/dbus_constants.h"
10#include "update_engine/subprocess.h"
11#include "update_engine/utils.h"
12
13extern "C" {
14#include "update_engine/update_engine.dbusclient.h"
15}
16
17using chromeos_update_engine::kUpdateEngineServiceName;
18using chromeos_update_engine::kUpdateEngineServicePath;
19using chromeos_update_engine::kUpdateEngineServiceInterface;
Andrew de los Reyesc7020782010-04-28 10:46:04 -070020using chromeos_update_engine::utils::GetGErrorMessage;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021
22DEFINE_bool(status, false, "Print the status to stdout.");
23DEFINE_bool(force_update, false,
24 "Force an update, even over an expensive network.");
25DEFINE_bool(check_for_update, false,
26 "Initiate check for updates.");
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070027DEFINE_bool(watch_for_updates, false,
28 "Listen for status updates and print them to the screen.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029
30namespace {
31
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070032bool GetProxy(DBusGProxy** out_proxy) {
33 DBusGConnection* bus;
34 DBusGProxy* proxy;
35 GError* error = NULL;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
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 Reyesc7020782010-04-28 10:46:04 -070047 LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070048 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070049 *out_proxy = proxy;
50 return true;
51}
52
53static 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
68bool GetStatus() {
69 DBusGProxy* proxy;
70 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -070071
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070072 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070073
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 &current_op,
85 &new_version,
86 &new_size,
87 &error);
88 if (rc == FALSE) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070089 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070090 }
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 Reyes63b96d72010-05-10 13:08:54 -0700101// Should never return.
102void WatchForUpdates() {
103 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700104
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700105 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700106
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700107 // 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);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700117
118 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700119 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700120 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700121 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,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700129 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700130 G_CALLBACK(StatusUpdateSignalHandler),
131 NULL,
132 NULL);
133 g_main_loop_run(loop);
134 g_main_loop_unref(loop);
135}
136
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700137bool CheckForUpdates(bool force) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700138 DBusGProxy* proxy;
139 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700140
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700141 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 Reyes4e9b9f42010-04-26 15:06:43 -0700147 return true;
148}
149
150} // namespace {}
151
152int 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);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700159
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700160 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 Reyes63b96d72010-05-10 13:08:54 -0700176 if (FLAGS_watch_for_updates) {
177 LOG(INFO) << "Watching for status updates.";
178 WatchForUpdates(); // Should never return.
179 return 1;
180 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700181
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182 LOG(INFO) << "No flags specified. Exiting.";
183 return 0;
184}