Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 3 | * Copyright (C) 2015-2017 Richard Hughes <richard@hughsie.com> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the GNU General Public License Version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 24 | #include <appstream-glib.h> |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 25 | #include <fwupd.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 26 | #include <gio/gunixfdlist.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <locale.h> |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 29 | #include <polkit/polkit.h> |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 31 | |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 32 | #include "fwupd-release-private.h" |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 33 | #include "fwupd-remote-private.h" |
Richard Hughes | d6db6b4 | 2017-04-12 15:03:10 +0100 | [diff] [blame] | 34 | #include "fwupd-resources.h" |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 35 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 36 | #include "fu-debug.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 37 | #include "fu-device.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 38 | #include "fu-engine.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 39 | |
Philip Withnall | bc339aa | 2016-11-22 16:13:22 +0000 | [diff] [blame] | 40 | #ifndef HAVE_POLKIT_0_114 |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 41 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthorizationResult, g_object_unref) |
| 42 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitSubject, g_object_unref) |
| 43 | #endif |
| 44 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 45 | typedef struct { |
| 46 | GDBusConnection *connection; |
| 47 | GDBusNodeInfo *introspection_daemon; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 48 | GDBusProxy *proxy_uid; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 49 | GMainLoop *loop; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 50 | PolkitAuthority *authority; |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 51 | guint owner_id; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 52 | FuEngine *engine; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 53 | } FuMainPrivate; |
| 54 | |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 55 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 56 | fu_main_engine_changed_cb (FuEngine *engine, FuMainPrivate *priv) |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 57 | { |
| 58 | /* not yet connected */ |
| 59 | if (priv->connection == NULL) |
| 60 | return; |
| 61 | g_dbus_connection_emit_signal (priv->connection, |
| 62 | NULL, |
| 63 | FWUPD_DBUS_PATH, |
| 64 | FWUPD_DBUS_INTERFACE, |
| 65 | "Changed", |
| 66 | NULL, NULL); |
| 67 | } |
| 68 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 69 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 70 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 71 | FuDevice *device, |
| 72 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 73 | { |
| 74 | GVariant *val; |
| 75 | |
| 76 | /* not yet connected */ |
| 77 | if (priv->connection == NULL) |
| 78 | return; |
| 79 | val = fwupd_result_to_data (FWUPD_RESULT (device), "(a{sv})"); |
| 80 | g_dbus_connection_emit_signal (priv->connection, |
| 81 | NULL, |
| 82 | FWUPD_DBUS_PATH, |
| 83 | FWUPD_DBUS_INTERFACE, |
| 84 | "DeviceAdded", |
| 85 | val, NULL); |
| 86 | } |
| 87 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 88 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 89 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 90 | FuDevice *device, |
| 91 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 92 | { |
| 93 | GVariant *val; |
| 94 | |
| 95 | /* not yet connected */ |
| 96 | if (priv->connection == NULL) |
| 97 | return; |
| 98 | val = fwupd_result_to_data (FWUPD_RESULT (device), "(a{sv})"); |
| 99 | g_dbus_connection_emit_signal (priv->connection, |
| 100 | NULL, |
| 101 | FWUPD_DBUS_PATH, |
| 102 | FWUPD_DBUS_INTERFACE, |
| 103 | "DeviceRemoved", |
| 104 | val, NULL); |
| 105 | } |
| 106 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 107 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 108 | fu_main_engine_device_changed_cb (FuEngine *engine, |
| 109 | FuDevice *device, |
| 110 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 111 | { |
| 112 | GVariant *val; |
| 113 | |
| 114 | /* not yet connected */ |
| 115 | if (priv->connection == NULL) |
| 116 | return; |
| 117 | val = fwupd_result_to_data (FWUPD_RESULT (device), "(a{sv})"); |
| 118 | g_dbus_connection_emit_signal (priv->connection, |
| 119 | NULL, |
| 120 | FWUPD_DBUS_PATH, |
| 121 | FWUPD_DBUS_INTERFACE, |
| 122 | "DeviceChanged", |
| 123 | val, NULL); |
| 124 | } |
| 125 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 126 | static void |
| 127 | fu_main_emit_property_changed (FuMainPrivate *priv, |
| 128 | const gchar *property_name, |
| 129 | GVariant *property_value) |
| 130 | { |
| 131 | GVariantBuilder builder; |
| 132 | GVariantBuilder invalidated_builder; |
| 133 | |
| 134 | /* not yet connected */ |
| 135 | if (priv->connection == NULL) |
| 136 | return; |
| 137 | |
| 138 | /* build the dict */ |
| 139 | g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as")); |
| 140 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 141 | g_variant_builder_add (&builder, |
| 142 | "{sv}", |
| 143 | property_name, |
| 144 | property_value); |
| 145 | g_dbus_connection_emit_signal (priv->connection, |
| 146 | NULL, |
| 147 | FWUPD_DBUS_PATH, |
| 148 | "org.freedesktop.DBus.Properties", |
| 149 | "PropertiesChanged", |
| 150 | g_variant_new ("(sa{sv}as)", |
| 151 | FWUPD_DBUS_INTERFACE, |
| 152 | &builder, |
| 153 | &invalidated_builder), |
| 154 | NULL); |
| 155 | g_variant_builder_clear (&builder); |
| 156 | g_variant_builder_clear (&invalidated_builder); |
| 157 | } |
| 158 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 159 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 160 | static void |
| 161 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 162 | FwupdStatus status, |
| 163 | FuMainPrivate *priv) |
| 164 | { |
Richard Hughes | 8818151 | 2015-03-19 10:57:44 +0000 | [diff] [blame] | 165 | g_debug ("Emitting PropertyChanged('Status'='%s')", |
| 166 | fwupd_status_to_string (status)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 167 | fu_main_emit_property_changed (priv, "Status", |
| 168 | g_variant_new_uint32 (status)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 171 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 172 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 173 | guint percentage, |
| 174 | FuMainPrivate *priv) |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 175 | { |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 176 | g_debug ("Emitting PropertyChanged('Percentage'='%u%%')", percentage); |
| 177 | fu_main_emit_property_changed (priv, "Percentage", |
| 178 | g_variant_new_uint32 (percentage)); |
| 179 | } |
| 180 | |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 181 | static GVariant * |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 182 | fu_main_device_array_to_variant (GPtrArray *devices) |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 183 | { |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 184 | GVariantBuilder builder; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 185 | g_return_val_if_fail (devices->len > 0, NULL); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 186 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 187 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 188 | FuDevice *device = g_ptr_array_index (devices, i); |
| 189 | GVariant *tmp = fwupd_result_to_data (FWUPD_RESULT (device), "{sa{sv}}"); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 190 | g_variant_builder_add_value (&builder, tmp); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 191 | } |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 192 | return g_variant_new ("(a{sa{sv}})", &builder); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 195 | static GVariant * |
| 196 | fu_main_release_array_to_variant (GPtrArray *results) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 197 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 198 | GVariantBuilder builder; |
| 199 | g_return_val_if_fail (results->len > 0, NULL); |
| 200 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 201 | for (guint i = 0; i < results->len; i++) { |
| 202 | FwupdRelease *rel = g_ptr_array_index (results, i); |
| 203 | GVariant *tmp = fwupd_release_to_data (rel, "a{sv}"); |
| 204 | g_variant_builder_add_value (&builder, tmp); |
| 205 | } |
| 206 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 209 | static GVariant * |
| 210 | fu_main_remote_array_to_variant (GPtrArray *remotes) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 211 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 212 | GVariantBuilder builder; |
| 213 | g_return_val_if_fail (remotes->len > 0, NULL); |
| 214 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 215 | for (guint i = 0; i < remotes->len; i++) { |
| 216 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
| 217 | GVariant *tmp = fwupd_remote_to_data (remote, "a{sv}"); |
| 218 | g_variant_builder_add_value (&builder, tmp); |
| 219 | } |
| 220 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 223 | static GVariant * |
| 224 | fu_main_result_array_to_variant (GPtrArray *results) |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 225 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 226 | GVariantBuilder builder; |
| 227 | g_return_val_if_fail (results->len > 0, NULL); |
| 228 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 229 | for (guint i = 0; i < results->len; i++) { |
| 230 | FwupdResult *result = g_ptr_array_index (results, i); |
| 231 | GVariant *tmp = fwupd_result_to_data (result, "{sa{sv}}"); |
| 232 | g_variant_builder_add_value (&builder, tmp); |
| 233 | } |
| 234 | return g_variant_new ("(a{sa{sv}})", &builder); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 237 | typedef struct { |
| 238 | GDBusMethodInvocation *invocation; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 239 | AsStore *store; |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 240 | FwupdInstallFlags flags; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 241 | GBytes *blob_cab; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 242 | FuMainPrivate *priv; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 243 | gchar *device_id; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 244 | } FuMainAuthHelper; |
| 245 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 246 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 247 | fu_main_auth_helper_free (FuMainAuthHelper *helper) |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 248 | { |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 249 | if (helper->blob_cab != NULL) |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 250 | g_bytes_unref (helper->blob_cab); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 251 | if (helper->store != NULL) |
| 252 | g_object_unref (helper->store); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 253 | g_free (helper->device_id); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 254 | g_object_unref (helper->invocation); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 255 | g_free (helper); |
| 256 | } |
| 257 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 258 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainAuthHelper, fu_main_auth_helper_free) |
| 259 | |
| 260 | /* error may or may not already have been set */ |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 261 | static gboolean |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 262 | fu_main_authorization_is_valid (PolkitAuthorizationResult *auth, GError **error) |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 263 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 264 | /* failed */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 265 | if (auth == NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 266 | g_autofree gchar *message = g_strdup ((*error)->message); |
| 267 | g_clear_error (error); |
| 268 | g_set_error (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 269 | FWUPD_ERROR, |
| 270 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 271 | "Could not check for auth: %s", message); |
| 272 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* did not auth */ |
| 276 | if (!polkit_authorization_result_get_is_authorized (auth)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 277 | g_set_error_literal (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 278 | FWUPD_ERROR, |
| 279 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 280 | "Failed to obtain auth"); |
| 281 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 285 | return TRUE; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 288 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 289 | fu_main_authorize_unlock_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 290 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 291 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 292 | g_autoptr(GError) error = NULL; |
| 293 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 294 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 295 | /* get result */ |
| 296 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 297 | res, &error); |
| 298 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 299 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 300 | return; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 301 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 302 | |
| 303 | /* authenticated */ |
| 304 | if (!fu_engine_unlock (helper->priv->engine, helper->device_id, &error)) { |
| 305 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | /* success */ |
| 310 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 314 | fu_main_authorize_verify_update_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 315 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 316 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 317 | g_autoptr(GError) error = NULL; |
| 318 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 319 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 320 | /* get result */ |
| 321 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 322 | res, &error); |
| 323 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 324 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 325 | return; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 326 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 327 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 328 | /* authenticated */ |
| 329 | if (!fu_engine_verify_update (helper->priv->engine, helper->device_id, &error)) { |
| 330 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 331 | return; |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 335 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 338 | static void |
| 339 | fu_main_authorize_install_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 340 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 341 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 342 | g_autoptr(GError) error = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 343 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 344 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 345 | /* get result */ |
| 346 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 347 | res, &error); |
| 348 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 349 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 350 | return; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 353 | /* authenticated */ |
| 354 | if (!fu_engine_install (helper->priv->engine, |
| 355 | helper->device_id, |
| 356 | helper->store, |
| 357 | helper->blob_cab, |
| 358 | helper->flags, |
| 359 | &error)) { |
| 360 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 361 | return; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 365 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 368 | static void |
| 369 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 370 | const gchar *object_path, const gchar *interface_name, |
| 371 | const gchar *method_name, GVariant *parameters, |
| 372 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 373 | { |
| 374 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 375 | GVariant *val = NULL; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 376 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 377 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 378 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 379 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 380 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 381 | devices = fu_engine_get_devices (priv->engine, &error); |
| 382 | if (devices == NULL) { |
| 383 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 384 | return; |
| 385 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 386 | val = fu_main_device_array_to_variant (devices); |
| 387 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 388 | return; |
| 389 | } |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 390 | if (g_strcmp0 (method_name, "GetUpdates") == 0) { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 391 | g_autoptr(GPtrArray) updates = NULL; |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 392 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 393 | updates = fu_engine_get_updates (priv->engine, &error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 394 | if (updates == NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 395 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 396 | return; |
| 397 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 398 | val = fu_main_device_array_to_variant (updates); |
| 399 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 402 | if (g_strcmp0 (method_name, "GetReleases") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 403 | const gchar *device_id; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 404 | g_autoptr(GPtrArray) releases = NULL; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 405 | g_variant_get (parameters, "(&s)", &device_id); |
| 406 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 407 | releases = fu_engine_get_releases (priv->engine, device_id, &error); |
| 408 | if (releases == NULL) { |
| 409 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 410 | return; |
| 411 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 412 | val = fu_main_release_array_to_variant (releases); |
| 413 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 414 | return; |
| 415 | } |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 416 | if (g_strcmp0 (method_name, "GetRemotes") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 417 | g_autoptr(GPtrArray) remotes = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 418 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 419 | remotes = fu_engine_get_remotes (priv->engine, &error); |
| 420 | if (remotes == NULL) { |
| 421 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 422 | return; |
| 423 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 424 | val = fu_main_remote_array_to_variant (remotes); |
| 425 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 426 | return; |
| 427 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 428 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 429 | const gchar *device_id; |
| 430 | g_variant_get (parameters, "(&s)", &device_id); |
| 431 | g_debug ("Called %s(%s)", method_name, device_id); |
| 432 | if (!fu_engine_clear_results (priv->engine, device_id, &error)) { |
| 433 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 436 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 439 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 440 | const gchar *device_id = NULL; |
| 441 | g_autoptr(FwupdResult) result = NULL; |
| 442 | g_variant_get (parameters, "(&s)", &device_id); |
| 443 | g_debug ("Called %s(%s)", method_name, device_id); |
| 444 | result = fu_engine_get_results (priv->engine, device_id, &error); |
| 445 | if (result == NULL) { |
| 446 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 449 | val = fwupd_result_to_data (result, "(a{sv})"); |
| 450 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 451 | return; |
| 452 | } |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 453 | if (g_strcmp0 (method_name, "UpdateMetadataWithId") == 0) { |
| 454 | GDBusMessage *message; |
| 455 | GUnixFDList *fd_list; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 456 | const gchar *remote_id = NULL; |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 457 | gint fd_data; |
| 458 | gint fd_sig; |
| 459 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 460 | g_variant_get (parameters, "(&shh)", &remote_id, &fd_data, &fd_sig); |
| 461 | g_debug ("Called %s(%s,%i,%i)", method_name, remote_id, fd_data, fd_sig); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 462 | |
Richard Hughes | 5935ebd | 2017-06-16 15:40:31 +0100 | [diff] [blame] | 463 | /* update the metadata store */ |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 464 | message = g_dbus_method_invocation_get_message (invocation); |
| 465 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 466 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 2) { |
| 467 | g_set_error (&error, |
| 468 | FWUPD_ERROR, |
| 469 | FWUPD_ERROR_INTERNAL, |
| 470 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 471 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 475 | if (fd_data < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 476 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 477 | return; |
| 478 | } |
| 479 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 480 | if (fd_sig < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 481 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 482 | return; |
| 483 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 484 | |
| 485 | /* store new metadata (will close the fds when done) */ |
| 486 | if (!fu_engine_update_metadata (priv->engine, remote_id, |
| 487 | fd_data, fd_sig, &error)) { |
| 488 | g_prefix_error (&error, "Failed to update metadata: "); |
| 489 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 490 | return; |
| 491 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 492 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 493 | return; |
| 494 | } |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 495 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 496 | FuMainAuthHelper *helper; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 497 | const gchar *device_id = NULL; |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 498 | g_autoptr(PolkitSubject) subject = NULL; |
| 499 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 500 | g_variant_get (parameters, "(&s)", &device_id); |
| 501 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 502 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 503 | /* authenticate */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 504 | helper = g_new0 (FuMainAuthHelper, 1); |
| 505 | helper->priv = priv; |
| 506 | helper->invocation = g_object_ref (invocation); |
| 507 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 508 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 509 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 510 | "org.freedesktop.fwupd.device-unlock", |
| 511 | NULL, |
| 512 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 513 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 514 | fu_main_authorize_unlock_cb, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 515 | helper); |
| 516 | return; |
| 517 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 518 | if (g_strcmp0 (method_name, "VerifyUpdate") == 0) { |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 519 | FuMainAuthHelper *helper; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 520 | const gchar *device_id = NULL; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 521 | g_autoptr(PolkitSubject) subject = NULL; |
| 522 | |
| 523 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 524 | g_variant_get (parameters, "(&s)", &device_id); |
| 525 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 526 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 527 | /* create helper object */ |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 528 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 529 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 530 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 531 | helper->priv = priv; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 532 | |
| 533 | /* authenticate */ |
| 534 | subject = polkit_system_bus_name_new (sender); |
| 535 | polkit_authority_check_authorization (helper->priv->authority, subject, |
| 536 | "org.freedesktop.fwupd.verify-update", |
| 537 | NULL, |
| 538 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 539 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 540 | fu_main_authorize_verify_update_cb, |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 541 | helper); |
| 542 | return; |
| 543 | } |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 544 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 545 | const gchar *device_id = NULL; |
| 546 | g_variant_get (parameters, "(&s)", &device_id); |
| 547 | g_debug ("Called %s(%s)", method_name, device_id); |
| 548 | if (!fu_engine_verify (priv->engine, device_id, &error)) { |
| 549 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 550 | return; |
| 551 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 552 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 553 | return; |
| 554 | } |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 555 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 556 | FuMainAuthHelper *helper; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 557 | GVariant *prop_value; |
Richard Hughes | a8e8394 | 2015-03-09 17:19:35 +0000 | [diff] [blame] | 558 | const gchar *action_id; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 559 | const gchar *device_id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 560 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 561 | gint32 fd_handle = 0; |
| 562 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 563 | GDBusMessage *message; |
| 564 | GUnixFDList *fd_list; |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 565 | g_autoptr(PolkitSubject) subject = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 566 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 567 | |
| 568 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 569 | g_variant_get (parameters, "(&sha{sv})", &device_id, &fd_handle, &iter); |
| 570 | g_debug ("Called %s(%s,%i)", method_name, device_id, fd_handle); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 571 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 572 | /* create helper object */ |
| 573 | helper = g_new0 (FuMainAuthHelper, 1); |
| 574 | helper->invocation = g_object_ref (invocation); |
| 575 | helper->device_id = g_strdup (device_id); |
| 576 | helper->priv = priv; |
| 577 | |
| 578 | /* get flags */ |
| 579 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 580 | g_debug ("got option %s", prop_key); |
| 581 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 582 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 583 | helper->flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 584 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 585 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 586 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 587 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 588 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 589 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 590 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 591 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 592 | helper->flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 593 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 596 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 597 | /* get the fd */ |
| 598 | message = g_dbus_method_invocation_get_message (invocation); |
| 599 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 600 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 601 | g_set_error (&error, |
| 602 | FWUPD_ERROR, |
| 603 | FWUPD_ERROR_INTERNAL, |
| 604 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 605 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 606 | return; |
| 607 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 608 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 609 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 610 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 614 | /* parse the cab file before authenticating so we can work out |
| 615 | * what action ID to use, for instance, if this is trusted -- |
| 616 | * this will also close the fd when done */ |
| 617 | helper->blob_cab = fu_engine_read_from_fd (fd, FU_ENGINE_FIRMWARE_SIZE_MAX, &error); |
| 618 | if (helper->blob_cab == NULL) { |
| 619 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 620 | return; |
| 621 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 622 | helper->store = fu_engine_get_store_from_blob (priv->engine, |
| 623 | helper->blob_cab, |
| 624 | &error); |
| 625 | if (helper->store == NULL) { |
| 626 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 630 | /* authenticate */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 631 | action_id = fu_engine_get_action_id_for_device (priv->engine, |
| 632 | helper->device_id, |
| 633 | helper->store, |
| 634 | helper->flags, |
| 635 | &error); |
| 636 | if (action_id == NULL) { |
| 637 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 638 | return; |
| 639 | } |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 640 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 641 | polkit_authority_check_authorization (priv->authority, subject, |
| 642 | action_id, NULL, |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 643 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 644 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 645 | fu_main_authorize_install_cb, |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 646 | helper); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 649 | if (g_strcmp0 (method_name, "GetDetailsLocal") == 0) { |
| 650 | GDBusMessage *message; |
| 651 | GUnixFDList *fd_list; |
| 652 | gint32 fd_handle = 0; |
| 653 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 654 | g_autoptr(GPtrArray) results = NULL; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 655 | |
| 656 | /* get parameters */ |
| 657 | g_variant_get (parameters, "(h)", &fd_handle); |
| 658 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 659 | |
| 660 | /* get the fd */ |
| 661 | message = g_dbus_method_invocation_get_message (invocation); |
| 662 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 663 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 664 | g_set_error (&error, |
| 665 | FWUPD_ERROR, |
| 666 | FWUPD_ERROR_INTERNAL, |
| 667 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 668 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 669 | return; |
| 670 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 671 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 672 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 673 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 677 | /* get details about the file (will close the fd when done) */ |
| 678 | results = fu_engine_get_details_local (priv->engine, fd, &error); |
| 679 | if (results == NULL) { |
| 680 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 681 | return; |
| 682 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 683 | val = fu_main_result_array_to_variant (results); |
| 684 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 685 | return; |
| 686 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 687 | g_set_error (&error, |
| 688 | G_DBUS_ERROR, |
| 689 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 690 | "no such method %s", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 691 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 694 | static GVariant * |
| 695 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 696 | const gchar *object_path, const gchar *interface_name, |
| 697 | const gchar *property_name, GError **error, |
| 698 | gpointer user_data) |
| 699 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 700 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 701 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 702 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
| 703 | return g_variant_new_string (VERSION); |
| 704 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 705 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 706 | return g_variant_new_uint32 (fu_engine_get_status (priv->engine)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 707 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 708 | /* return an error */ |
| 709 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 710 | G_DBUS_ERROR, |
| 711 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 712 | "failed to get daemon property %s", |
| 713 | property_name); |
| 714 | return NULL; |
| 715 | } |
| 716 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 717 | static void |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 718 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 719 | const gchar *name, |
| 720 | gpointer user_data) |
| 721 | { |
| 722 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 723 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 724 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 725 | static const GDBusInterfaceVTable interface_vtable = { |
| 726 | fu_main_daemon_method_call, |
| 727 | fu_main_daemon_get_property, |
| 728 | NULL |
| 729 | }; |
| 730 | |
| 731 | priv->connection = g_object_ref (connection); |
| 732 | registration_id = g_dbus_connection_register_object (connection, |
| 733 | FWUPD_DBUS_PATH, |
| 734 | priv->introspection_daemon->interfaces[0], |
| 735 | &interface_vtable, |
| 736 | priv, /* user_data */ |
| 737 | NULL, /* user_data_free_func */ |
| 738 | NULL); /* GError** */ |
| 739 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 740 | |
| 741 | /* connect to D-Bus directly */ |
| 742 | priv->proxy_uid = |
| 743 | g_dbus_proxy_new_sync (priv->connection, |
| 744 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 745 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 746 | NULL, |
| 747 | "org.freedesktop.DBus", |
| 748 | "/org/freedesktop/DBus", |
| 749 | "org.freedesktop.DBus", |
| 750 | NULL, |
| 751 | &error); |
| 752 | if (priv->proxy_uid == NULL) { |
| 753 | g_warning ("cannot connect to DBus: %s", error->message); |
| 754 | return; |
| 755 | } |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 756 | |
| 757 | /* dump startup profile data */ |
Richard Hughes | 2a1e75d | 2015-12-18 17:42:53 +0000 | [diff] [blame] | 758 | if (fu_debug_is_verbose ()) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 759 | fu_engine_profile_dump (priv->engine); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 762 | static void |
| 763 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 764 | const gchar *name, |
| 765 | gpointer user_data) |
| 766 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 767 | g_debug ("FuMain: acquired name: %s", name); |
| 768 | } |
| 769 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 770 | static void |
| 771 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 772 | const gchar *name, |
| 773 | gpointer user_data) |
| 774 | { |
| 775 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 776 | g_debug ("FuMain: lost name: %s", name); |
| 777 | g_main_loop_quit (priv->loop); |
| 778 | } |
| 779 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 780 | static gboolean |
| 781 | fu_main_timed_exit_cb (gpointer user_data) |
| 782 | { |
| 783 | GMainLoop *loop = (GMainLoop *) user_data; |
| 784 | g_main_loop_quit (loop); |
| 785 | return G_SOURCE_REMOVE; |
| 786 | } |
| 787 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 788 | static GDBusNodeInfo * |
| 789 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 790 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 791 | g_autoptr(GBytes) data = NULL; |
| 792 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 793 | |
| 794 | /* lookup data */ |
| 795 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 796 | data = g_resource_lookup_data (fu_get_resource (), |
| 797 | path, |
| 798 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 799 | error); |
| 800 | if (data == NULL) |
| 801 | return NULL; |
| 802 | |
| 803 | /* build introspection from XML */ |
| 804 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 805 | } |
| 806 | |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 807 | static gboolean |
| 808 | fu_main_perhaps_own_name (gpointer user_data) |
| 809 | { |
| 810 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 811 | g_autoptr(GError) error = NULL; |
| 812 | |
| 813 | /* are any plugins pending */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 814 | if (!fu_engine_check_plugins_pending (priv->engine, &error)) { |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 815 | g_debug ("trying again: %s", error->message); |
| 816 | return G_SOURCE_CONTINUE; |
| 817 | } |
| 818 | |
| 819 | /* own the object */ |
| 820 | g_debug ("registering D-Bus service"); |
| 821 | priv->owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 822 | FWUPD_DBUS_SERVICE, |
| 823 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 824 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 825 | fu_main_on_bus_acquired_cb, |
| 826 | fu_main_on_name_acquired_cb, |
| 827 | fu_main_on_name_lost_cb, |
| 828 | priv, NULL); |
| 829 | return G_SOURCE_REMOVE; |
| 830 | } |
| 831 | |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 832 | static void |
| 833 | fu_main_private_free (FuMainPrivate *priv) |
| 834 | { |
| 835 | if (priv->loop != NULL) |
| 836 | g_main_loop_unref (priv->loop); |
| 837 | if (priv->owner_id > 0) |
| 838 | g_bus_unown_name (priv->owner_id); |
| 839 | if (priv->proxy_uid != NULL) |
| 840 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 841 | if (priv->engine != NULL) |
| 842 | g_object_unref (priv->engine); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 843 | if (priv->connection != NULL) |
| 844 | g_object_unref (priv->connection); |
| 845 | if (priv->authority != NULL) |
| 846 | g_object_unref (priv->authority); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 847 | if (priv->introspection_daemon != NULL) |
| 848 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 849 | g_free (priv); |
| 850 | } |
| 851 | |
| 852 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainPrivate, fu_main_private_free) |
| 853 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 854 | int |
| 855 | main (int argc, char *argv[]) |
| 856 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 857 | gboolean immediate_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 858 | gboolean timed_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 859 | const GOptionEntry options[] = { |
| 860 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 861 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 862 | _("Exit after a small delay"), NULL }, |
| 863 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 864 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 865 | _("Exit after the engine has loaded"), NULL }, |
| 866 | { NULL} |
| 867 | }; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 868 | g_autoptr(FuMainPrivate) priv = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 869 | g_autoptr(GError) error = NULL; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 870 | g_autoptr(GOptionContext) context = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 871 | |
| 872 | setlocale (LC_ALL, ""); |
| 873 | |
| 874 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 875 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 876 | textdomain (GETTEXT_PACKAGE); |
| 877 | |
| 878 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 879 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 880 | context = g_option_context_new (NULL); |
| 881 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 882 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 883 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 884 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 885 | if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 886 | g_printerr ("Failed to parse command line: %s\n", error->message); |
| 887 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* create new objects */ |
| 891 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 892 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 893 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 894 | /* load engine */ |
| 895 | priv->engine = fu_engine_new (); |
| 896 | g_signal_connect (priv->engine, "changed", |
| 897 | G_CALLBACK (fu_main_engine_changed_cb), |
| 898 | priv); |
| 899 | g_signal_connect (priv->engine, "device-added", |
| 900 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 901 | priv); |
| 902 | g_signal_connect (priv->engine, "device-removed", |
| 903 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 904 | priv); |
| 905 | g_signal_connect (priv->engine, "device-changed", |
| 906 | G_CALLBACK (fu_main_engine_device_changed_cb), |
| 907 | priv); |
| 908 | g_signal_connect (priv->engine, "status-changed", |
| 909 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 910 | priv); |
| 911 | g_signal_connect (priv->engine, "percentage-changed", |
| 912 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 913 | priv); |
| 914 | if (!fu_engine_load (priv->engine, &error)) { |
| 915 | g_printerr ("Failed to load engine: %s\n", error->message); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 916 | return EXIT_FAILURE; |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 917 | } |
| 918 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame^] | 919 | /* keep polling until all the plugins are ready */ |
| 920 | g_timeout_add (200, fu_main_perhaps_own_name, priv); |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 921 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 922 | /* load introspection from file */ |
| 923 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 924 | &error); |
| 925 | if (priv->introspection_daemon == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 926 | g_printerr ("Failed to load introspection: %s\n", error->message); |
| 927 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 930 | /* get authority */ |
| 931 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 932 | if (priv->authority == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 933 | g_printerr ("Failed to load authority: %s\n", error->message); |
| 934 | return EXIT_FAILURE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 937 | /* Only timeout and close the mainloop if we have specified it |
| 938 | * on the command line */ |
| 939 | if (immediate_exit) |
| 940 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 941 | else if (timed_exit) |
| 942 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 943 | |
| 944 | /* wait */ |
Richard Hughes | 4619f9f | 2017-06-14 13:55:30 +0100 | [diff] [blame] | 945 | g_message ("Daemon ready for requests"); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 946 | g_main_loop_run (priv->loop); |
| 947 | |
| 948 | /* success */ |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 949 | return EXIT_SUCCESS; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 950 | } |