blob: bb4beaa328b03fdd97f53bb1cf0ecfb0a15731ad [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
Darin Petkov5a7f5652010-07-22 21:40:09 -07005#include <string>
6
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07007#include <gflags/gflags.h>
8#include <glib.h>
9
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070010#include "update_engine/marshal.glibmarshal.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070011#include "update_engine/dbus_constants.h"
12#include "update_engine/subprocess.h"
13#include "update_engine/utils.h"
14
15extern "C" {
16#include "update_engine/update_engine.dbusclient.h"
17}
18
19using chromeos_update_engine::kUpdateEngineServiceName;
20using chromeos_update_engine::kUpdateEngineServicePath;
21using chromeos_update_engine::kUpdateEngineServiceInterface;
Andrew de los Reyesc7020782010-04-28 10:46:04 -070022using chromeos_update_engine::utils::GetGErrorMessage;
Darin Petkov5a7f5652010-07-22 21:40:09 -070023using std::string;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024
Darin Petkov5a7f5652010-07-22 21:40:09 -070025DEFINE_string(app_version, "",
26 "Force the current app version.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027DEFINE_bool(check_for_update, false,
28 "Initiate check for updates.");
Darin Petkov5a7f5652010-07-22 21:40:09 -070029DEFINE_bool(force_update, false,
30 "Force an update, even over an expensive network.");
31DEFINE_string(omaha_url, "",
32 "The URL of the Omaha update server.");
33DEFINE_bool(status, false, "Print the status to stdout.");
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070034DEFINE_bool(watch_for_updates, false,
35 "Listen for status updates and print them to the screen.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
37namespace {
38
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070039bool GetProxy(DBusGProxy** out_proxy) {
40 DBusGConnection* bus;
41 DBusGProxy* proxy;
42 GError* error = NULL;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070043
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 Reyesc7020782010-04-28 10:46:04 -070054 LOG(FATAL) << "Error getting proxy: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070055 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070056 *out_proxy = proxy;
57 return true;
58}
59
60static 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
75bool GetStatus() {
76 DBusGProxy* proxy;
77 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -070078
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070079 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070080
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 &current_op,
92 &new_version,
93 &new_size,
94 &error);
95 if (rc == FALSE) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070096 LOG(INFO) << "Error getting status: " << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070097 }
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 Reyes63b96d72010-05-10 13:08:54 -0700108// Should never return.
109void WatchForUpdates() {
110 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700111
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700112 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700113
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700114 // 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 Reyesada42202010-07-15 22:23:20 -0700124
125 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700126 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700127 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700128 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 Reyesada42202010-07-15 22:23:20 -0700136 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700137 G_CALLBACK(StatusUpdateSignalHandler),
138 NULL,
139 NULL);
140 g_main_loop_run(loop);
141 g_main_loop_unref(loop);
142}
143
Darin Petkov5a7f5652010-07-22 21:40:09 -0700144bool CheckForUpdates(bool force, const string& app_version,
145 const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700146 DBusGProxy* proxy;
147 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700148
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700149 CHECK(GetProxy(&proxy));
150
151 gboolean rc =
Darin Petkov5a7f5652010-07-22 21:40:09 -0700152 org_chromium_UpdateEngineInterface_attempt_update(proxy,
153 app_version.c_str(),
154 omaha_url.c_str(),
155 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700156 CHECK_EQ(rc, TRUE) << "Error checking for update: "
157 << GetGErrorMessage(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700158 return true;
159}
160
161} // namespace {}
162
163int 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 Reyesada42202010-07-15 22:23:20 -0700170
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700171 if (FLAGS_status) {
172 LOG(INFO) << "Querying Update Engine status...";
173 if (!GetStatus()) {
174 LOG(FATAL) << "GetStatus() failed.";
175 }
176 return 0;
177 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700178 if (FLAGS_force_update || FLAGS_check_for_update ||
179 !FLAGS_app_version.empty() || !FLAGS_omaha_url.empty()) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700180 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 Petkov5a7f5652010-07-22 21:40:09 -0700184 CHECK(CheckForUpdates(FLAGS_force_update, FLAGS_app_version,
185 FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700186 << "Update check/initiate update failed.";
187 return 0;
188 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700189 if (FLAGS_watch_for_updates) {
190 LOG(INFO) << "Watching for status updates.";
191 WatchForUpdates(); // Should never return.
192 return 1;
193 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700194
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700195 LOG(INFO) << "No flags specified. Exiting.";
196 return 0;
197}