Richard Hughes | 02c90d8 | 2018-08-09 12:13:03 +0100 | [diff] [blame] | 1 | /* |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 Richard Hughes <richard@hughsie.com> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 3 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuMain" |
| 8 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 11 | #include <xmlb.h> |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 12 | #include <fwupd.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 13 | #include <gio/gunixfdlist.h> |
| 14 | #include <glib/gi18n.h> |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 15 | #include <glib-unix.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 16 | #include <locale.h> |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 17 | #include <polkit/polkit.h> |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 19 | |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 20 | #include "fwupd-device-private.h" |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 21 | #include "fwupd-release-private.h" |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 22 | #include "fwupd-remote-private.h" |
Richard Hughes | d6db6b4 | 2017-04-12 15:03:10 +0100 | [diff] [blame] | 23 | #include "fwupd-resources.h" |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 24 | |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 25 | #include "fu-common.h" |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 26 | #include "fu-debug.h" |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 27 | #include "fu-device-private.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 28 | #include "fu-engine.h" |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 29 | #include "fu-install-task.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 30 | |
Philip Withnall | bc339aa | 2016-11-22 16:13:22 +0000 | [diff] [blame] | 31 | #ifndef HAVE_POLKIT_0_114 |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 32 | #pragma clang diagnostic push |
| 33 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 34 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthorizationResult, g_object_unref) |
| 35 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitSubject, g_object_unref) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 36 | #pragma clang diagnostic pop |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 39 | typedef struct { |
| 40 | GDBusConnection *connection; |
| 41 | GDBusNodeInfo *introspection_daemon; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 42 | GDBusProxy *proxy_uid; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 43 | GMainLoop *loop; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 44 | PolkitAuthority *authority; |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 45 | guint owner_id; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 46 | FuEngine *engine; |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 47 | gboolean update_in_progress; |
| 48 | gboolean pending_sigterm; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 49 | } FuMainPrivate; |
| 50 | |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 51 | static gboolean |
| 52 | fu_main_sigterm_cb (gpointer user_data) |
| 53 | { |
| 54 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 55 | if (!priv->update_in_progress) { |
| 56 | g_main_loop_quit (priv->loop); |
| 57 | return G_SOURCE_REMOVE; |
| 58 | } |
| 59 | g_warning ("Received SIGTERM during a firmware update, ignoring"); |
| 60 | priv->pending_sigterm = TRUE; |
| 61 | return G_SOURCE_CONTINUE; |
| 62 | } |
| 63 | |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 64 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 65 | fu_main_engine_changed_cb (FuEngine *engine, FuMainPrivate *priv) |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 66 | { |
| 67 | /* not yet connected */ |
| 68 | if (priv->connection == NULL) |
| 69 | return; |
| 70 | g_dbus_connection_emit_signal (priv->connection, |
| 71 | NULL, |
| 72 | FWUPD_DBUS_PATH, |
| 73 | FWUPD_DBUS_INTERFACE, |
| 74 | "Changed", |
| 75 | NULL, NULL); |
| 76 | } |
| 77 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 78 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 79 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 80 | FuDevice *device, |
| 81 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 82 | { |
| 83 | GVariant *val; |
| 84 | |
| 85 | /* not yet connected */ |
| 86 | if (priv->connection == NULL) |
| 87 | return; |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 88 | val = fwupd_device_to_variant (FWUPD_DEVICE (device)); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 89 | g_dbus_connection_emit_signal (priv->connection, |
| 90 | NULL, |
| 91 | FWUPD_DBUS_PATH, |
| 92 | FWUPD_DBUS_INTERFACE, |
| 93 | "DeviceAdded", |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 94 | g_variant_new_tuple (&val, 1), NULL); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 97 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 98 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 99 | FuDevice *device, |
| 100 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 101 | { |
| 102 | GVariant *val; |
| 103 | |
| 104 | /* not yet connected */ |
| 105 | if (priv->connection == NULL) |
| 106 | return; |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 107 | val = fwupd_device_to_variant (FWUPD_DEVICE (device)); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 108 | g_dbus_connection_emit_signal (priv->connection, |
| 109 | NULL, |
| 110 | FWUPD_DBUS_PATH, |
| 111 | FWUPD_DBUS_INTERFACE, |
| 112 | "DeviceRemoved", |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 113 | g_variant_new_tuple (&val, 1), NULL); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 116 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 117 | fu_main_engine_device_changed_cb (FuEngine *engine, |
| 118 | FuDevice *device, |
| 119 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 120 | { |
| 121 | GVariant *val; |
| 122 | |
| 123 | /* not yet connected */ |
| 124 | if (priv->connection == NULL) |
| 125 | return; |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 126 | val = fwupd_device_to_variant (FWUPD_DEVICE (device)); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 127 | g_dbus_connection_emit_signal (priv->connection, |
| 128 | NULL, |
| 129 | FWUPD_DBUS_PATH, |
| 130 | FWUPD_DBUS_INTERFACE, |
| 131 | "DeviceChanged", |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 132 | g_variant_new_tuple (&val, 1), NULL); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 135 | static void |
| 136 | fu_main_emit_property_changed (FuMainPrivate *priv, |
| 137 | const gchar *property_name, |
| 138 | GVariant *property_value) |
| 139 | { |
| 140 | GVariantBuilder builder; |
| 141 | GVariantBuilder invalidated_builder; |
| 142 | |
| 143 | /* not yet connected */ |
Richard Hughes | 34fcc02 | 2018-09-19 16:16:15 +0100 | [diff] [blame] | 144 | if (priv->connection == NULL) { |
| 145 | g_variant_unref (g_variant_ref_sink (property_value)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 146 | return; |
Richard Hughes | 34fcc02 | 2018-09-19 16:16:15 +0100 | [diff] [blame] | 147 | } |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 148 | |
| 149 | /* build the dict */ |
| 150 | g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as")); |
| 151 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 152 | g_variant_builder_add (&builder, |
| 153 | "{sv}", |
| 154 | property_name, |
| 155 | property_value); |
| 156 | g_dbus_connection_emit_signal (priv->connection, |
| 157 | NULL, |
| 158 | FWUPD_DBUS_PATH, |
| 159 | "org.freedesktop.DBus.Properties", |
| 160 | "PropertiesChanged", |
| 161 | g_variant_new ("(sa{sv}as)", |
| 162 | FWUPD_DBUS_INTERFACE, |
| 163 | &builder, |
| 164 | &invalidated_builder), |
| 165 | NULL); |
| 166 | g_variant_builder_clear (&builder); |
| 167 | g_variant_builder_clear (&invalidated_builder); |
| 168 | } |
| 169 | |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 170 | static void |
| 171 | fu_main_set_status (FuMainPrivate *priv, FwupdStatus status) |
| 172 | { |
| 173 | g_debug ("Emitting PropertyChanged('Status'='%s')", |
| 174 | fwupd_status_to_string (status)); |
| 175 | fu_main_emit_property_changed (priv, "Status", |
| 176 | g_variant_new_uint32 (status)); |
| 177 | } |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 178 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 179 | static void |
| 180 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 181 | FwupdStatus status, |
| 182 | FuMainPrivate *priv) |
| 183 | { |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 184 | fu_main_set_status (priv, status); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 185 | |
| 186 | /* engine has gone idle */ |
| 187 | if (status == FWUPD_STATUS_SHUTDOWN) |
| 188 | g_main_loop_quit (priv->loop); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 191 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 192 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 193 | guint percentage, |
| 194 | FuMainPrivate *priv) |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 195 | { |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 196 | g_debug ("Emitting PropertyChanged('Percentage'='%u%%')", percentage); |
| 197 | fu_main_emit_property_changed (priv, "Percentage", |
| 198 | g_variant_new_uint32 (percentage)); |
| 199 | } |
| 200 | |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 201 | static gboolean |
| 202 | fu_main_get_device_flags_for_sender (FuMainPrivate *priv, const char *sender, |
| 203 | FwupdDeviceFlags *flags, GError **error) |
| 204 | { |
| 205 | uid_t calling_uid; |
| 206 | g_autoptr(GVariant) value = NULL; |
| 207 | |
| 208 | g_return_val_if_fail (sender != NULL, FALSE); |
| 209 | g_return_val_if_fail (flags != NULL, FALSE); |
| 210 | |
| 211 | value = g_dbus_proxy_call_sync (priv->proxy_uid, |
| 212 | "GetConnectionUnixUser", |
| 213 | g_variant_new ("(s)", sender), |
| 214 | G_DBUS_CALL_FLAGS_NONE, |
| 215 | 2000, |
| 216 | NULL, |
| 217 | error); |
| 218 | if (value == NULL) { |
| 219 | g_prefix_error (error, "failed to read user id of caller: "); |
| 220 | return FALSE; |
| 221 | } |
| 222 | g_variant_get (value, "(u)", &calling_uid); |
| 223 | if (calling_uid == 0) |
| 224 | *flags |= FWUPD_DEVICE_FLAG_TRUSTED; |
| 225 | |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 229 | static GVariant * |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 230 | fu_main_device_array_to_variant (FuMainPrivate *priv, const gchar *sender, |
| 231 | GPtrArray *devices, GError **error) |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 232 | { |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 233 | GVariantBuilder builder; |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 234 | FwupdDeviceFlags flags = FWUPD_DEVICE_FLAG_NONE; |
| 235 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 236 | g_return_val_if_fail (devices->len > 0, NULL); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 237 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 238 | |
| 239 | if (!fu_main_get_device_flags_for_sender (priv, sender, &flags, error)) |
Richard Hughes | 83cce1b | 2018-09-10 16:42:30 +0100 | [diff] [blame] | 240 | return NULL; |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 241 | |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 242 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 243 | FuDevice *device = g_ptr_array_index (devices, i); |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 244 | GVariant *tmp = fwupd_device_to_variant_full (FWUPD_DEVICE (device), |
| 245 | flags); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 246 | g_variant_builder_add_value (&builder, tmp); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 247 | } |
Richard Hughes | 9e1b140 | 2017-09-15 16:29:54 +0100 | [diff] [blame] | 248 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 251 | static GVariant * |
| 252 | fu_main_release_array_to_variant (GPtrArray *results) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 253 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 254 | GVariantBuilder builder; |
| 255 | g_return_val_if_fail (results->len > 0, NULL); |
| 256 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 257 | for (guint i = 0; i < results->len; i++) { |
| 258 | FwupdRelease *rel = g_ptr_array_index (results, i); |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 259 | GVariant *tmp = fwupd_release_to_variant (rel); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 260 | g_variant_builder_add_value (&builder, tmp); |
| 261 | } |
| 262 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 265 | static GVariant * |
| 266 | fu_main_remote_array_to_variant (GPtrArray *remotes) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 267 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 268 | GVariantBuilder builder; |
| 269 | g_return_val_if_fail (remotes->len > 0, NULL); |
| 270 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 271 | for (guint i = 0; i < remotes->len; i++) { |
| 272 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 273 | GVariant *tmp = fwupd_remote_to_variant (remote); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 274 | g_variant_builder_add_value (&builder, tmp); |
| 275 | } |
| 276 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 279 | static GVariant * |
| 280 | fu_main_result_array_to_variant (GPtrArray *results) |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 281 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 282 | GVariantBuilder builder; |
| 283 | g_return_val_if_fail (results->len > 0, NULL); |
| 284 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 285 | for (guint i = 0; i < results->len; i++) { |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 286 | FwupdDevice *result = g_ptr_array_index (results, i); |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 287 | GVariant *tmp = fwupd_device_to_variant (result); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 288 | g_variant_builder_add_value (&builder, tmp); |
| 289 | } |
Richard Hughes | 9e1b140 | 2017-09-15 16:29:54 +0100 | [diff] [blame] | 290 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 293 | typedef struct { |
| 294 | GDBusMethodInvocation *invocation; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 295 | PolkitSubject *subject; |
| 296 | GPtrArray *install_tasks; |
| 297 | GPtrArray *action_ids; |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 298 | FwupdInstallFlags flags; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 299 | GBytes *blob_cab; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 300 | FuMainPrivate *priv; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 301 | gchar *device_id; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 302 | gchar *remote_id; |
| 303 | gchar *key; |
| 304 | gchar *value; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 305 | XbSilo *silo; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 306 | } FuMainAuthHelper; |
| 307 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 308 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 309 | fu_main_auth_helper_free (FuMainAuthHelper *helper) |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 310 | { |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 311 | if (helper->blob_cab != NULL) |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 312 | g_bytes_unref (helper->blob_cab); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 313 | if (helper->subject != NULL) |
| 314 | g_object_unref (helper->subject); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 315 | if (helper->silo != NULL) |
| 316 | g_object_unref (helper->silo); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 317 | if (helper->install_tasks != NULL) |
| 318 | g_ptr_array_unref (helper->install_tasks); |
| 319 | if (helper->action_ids != NULL) |
| 320 | g_ptr_array_unref (helper->action_ids); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 321 | g_free (helper->device_id); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 322 | g_free (helper->remote_id); |
| 323 | g_free (helper->key); |
| 324 | g_free (helper->value); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 325 | g_object_unref (helper->invocation); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 326 | g_free (helper); |
| 327 | } |
| 328 | |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 329 | #pragma clang diagnostic push |
| 330 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 331 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainAuthHelper, fu_main_auth_helper_free) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 332 | #pragma clang diagnostic pop |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 333 | |
| 334 | /* error may or may not already have been set */ |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 335 | static gboolean |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 336 | fu_main_authorization_is_valid (PolkitAuthorizationResult *auth, GError **error) |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 337 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 338 | /* failed */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 339 | if (auth == NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 340 | g_autofree gchar *message = g_strdup ((*error)->message); |
| 341 | g_clear_error (error); |
| 342 | g_set_error (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 343 | FWUPD_ERROR, |
| 344 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 345 | "Could not check for auth: %s", message); |
| 346 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* did not auth */ |
| 350 | if (!polkit_authorization_result_get_is_authorized (auth)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 351 | g_set_error_literal (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 352 | FWUPD_ERROR, |
| 353 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 354 | "Failed to obtain auth"); |
| 355 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 359 | return TRUE; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 362 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 363 | fu_main_authorize_unlock_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 364 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 365 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 366 | g_autoptr(GError) error = NULL; |
| 367 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 368 | |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 369 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 370 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 371 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 372 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 373 | res, &error); |
| 374 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 375 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 376 | return; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 377 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 378 | |
| 379 | /* authenticated */ |
| 380 | if (!fu_engine_unlock (helper->priv->engine, helper->device_id, &error)) { |
| 381 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | /* success */ |
| 386 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 390 | 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] | 391 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 392 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 393 | g_autoptr(GError) error = NULL; |
| 394 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 395 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 396 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 397 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 398 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 399 | res, &error); |
| 400 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 401 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 402 | return; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 403 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 404 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 405 | /* authenticated */ |
| 406 | if (!fu_engine_verify_update (helper->priv->engine, helper->device_id, &error)) { |
| 407 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 408 | return; |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 412 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 415 | static void |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 416 | fu_main_authorize_modify_remote_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 417 | { |
| 418 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 419 | g_autoptr(GError) error = NULL; |
| 420 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 421 | |
| 422 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 423 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 424 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 425 | res, &error); |
| 426 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 427 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | /* authenticated */ |
| 432 | if (!fu_engine_modify_remote (helper->priv->engine, |
| 433 | helper->remote_id, |
| 434 | helper->key, |
| 435 | helper->value, |
| 436 | &error)) { |
| 437 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | /* success */ |
| 442 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 443 | } |
| 444 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 445 | static void fu_main_authorize_install_queue (FuMainAuthHelper *helper); |
| 446 | |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 447 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 448 | fu_main_authorize_install_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 449 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 450 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 451 | g_autoptr(GError) error = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 452 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 453 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 454 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 455 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 456 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 457 | res, &error); |
| 458 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 459 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 460 | return; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 463 | /* do the next authentication action ID */ |
| 464 | fu_main_authorize_install_queue (g_steal_pointer (&helper)); |
| 465 | } |
| 466 | |
| 467 | static void |
| 468 | fu_main_authorize_install_queue (FuMainAuthHelper *helper_ref) |
| 469 | { |
| 470 | FuMainPrivate *priv = helper_ref->priv; |
| 471 | g_autoptr(FuMainAuthHelper) helper = helper_ref; |
| 472 | g_autoptr(GError) error = NULL; |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 473 | gboolean ret; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 474 | |
| 475 | /* still more things to to authenticate */ |
| 476 | if (helper->action_ids->len > 0) { |
| 477 | g_autofree gchar *action_id = g_strdup (g_ptr_array_index (helper->action_ids, 0)); |
| 478 | g_autoptr(PolkitSubject) subject = g_object_ref (helper->subject); |
| 479 | g_ptr_array_remove_index (helper->action_ids, 0); |
| 480 | polkit_authority_check_authorization (priv->authority, subject, |
| 481 | action_id, NULL, |
| 482 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 483 | NULL, |
| 484 | fu_main_authorize_install_cb, |
| 485 | g_steal_pointer (&helper)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 486 | return; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 489 | /* all authenticated, so install all the things */ |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 490 | priv->update_in_progress = TRUE; |
| 491 | ret = fu_engine_install_tasks (helper->priv->engine, |
| 492 | helper->install_tasks, |
| 493 | helper->blob_cab, |
| 494 | helper->flags, |
| 495 | &error); |
| 496 | priv->update_in_progress = FALSE; |
| 497 | if (priv->pending_sigterm) |
| 498 | g_main_loop_quit (priv->loop); |
| 499 | if (!ret) { |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 500 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 501 | return; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 504 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 505 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 508 | #if !GLIB_CHECK_VERSION(2,54,0) |
| 509 | static gboolean |
| 510 | g_ptr_array_find (GPtrArray *haystack, gconstpointer needle, guint *index_) |
| 511 | { |
| 512 | for (guint i = 0; i < haystack->len; i++) { |
| 513 | gconstpointer *tmp = g_ptr_array_index (haystack, i); |
| 514 | if (tmp == needle) { |
| 515 | if (index_ != NULL) { |
| 516 | *index_ = i; |
| 517 | return TRUE; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | return FALSE; |
| 522 | } |
| 523 | #endif |
| 524 | |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 525 | static gint |
| 526 | fu_main_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 527 | { |
| 528 | FuInstallTask *task_a = *((FuInstallTask **) a); |
| 529 | FuInstallTask *task_b = *((FuInstallTask **) b); |
Richard Hughes | c02cb83 | 2018-05-20 10:31:04 +0100 | [diff] [blame] | 530 | return fu_install_task_compare (task_a, task_b); |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 533 | static GPtrArray * |
| 534 | fu_main_get_device_family (FuMainAuthHelper *helper, GError **error) |
| 535 | { |
| 536 | FuDevice *parent; |
| 537 | GPtrArray *children; |
| 538 | g_autoptr(FuDevice) device = NULL; |
| 539 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 540 | |
| 541 | /* get the device */ |
| 542 | device = fu_engine_get_device (helper->priv->engine, helper->device_id, error); |
| 543 | if (device == NULL) |
| 544 | return NULL; |
| 545 | |
| 546 | /* device itself */ |
| 547 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 548 | g_ptr_array_add (devices_possible, g_object_ref (device)); |
| 549 | |
| 550 | /* add device children */ |
| 551 | children = fu_device_get_children (device); |
| 552 | for (guint i = 0; i < children->len; i++) { |
| 553 | FuDevice *child = g_ptr_array_index (children, i); |
| 554 | g_ptr_array_add (devices_possible, g_object_ref (child)); |
| 555 | } |
| 556 | |
| 557 | /* add parent and siblings, not including the device itself */ |
| 558 | parent = fu_device_get_parent (device); |
| 559 | if (parent != NULL) { |
| 560 | GPtrArray *siblings = fu_device_get_children (parent); |
| 561 | g_ptr_array_add (devices_possible, g_object_ref (parent)); |
| 562 | for (guint i = 0; i < siblings->len; i++) { |
| 563 | FuDevice *sibling = g_ptr_array_index (siblings, i); |
| 564 | if (sibling == device) |
| 565 | continue; |
| 566 | g_ptr_array_add (devices_possible, g_object_ref (sibling)); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* success */ |
| 571 | return g_steal_pointer (&devices_possible); |
| 572 | } |
| 573 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 574 | static gboolean |
| 575 | fu_main_install_with_helper (FuMainAuthHelper *helper_ref, GError **error) |
| 576 | { |
| 577 | FuMainPrivate *priv = helper_ref->priv; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 578 | g_autoptr(FuMainAuthHelper) helper = helper_ref; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 579 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 580 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 581 | g_autoptr(GPtrArray) errors = NULL; |
| 582 | |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 583 | /* get a list of devices that in some way match the device_id */ |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 584 | if (g_strcmp0 (helper->device_id, FWUPD_DEVICE_ID_ANY) == 0) { |
| 585 | devices_possible = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 586 | if (devices_possible == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 587 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 588 | } else { |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 589 | devices_possible = fu_main_get_device_family (helper, error); |
| 590 | if (devices_possible == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 591 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 594 | /* parse silo */ |
| 595 | helper->silo = fu_engine_get_silo_from_blob (priv->engine, |
| 596 | helper->blob_cab, |
| 597 | error); |
| 598 | if (helper->silo == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 599 | return FALSE; |
| 600 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 601 | /* for each component in the silo */ |
| 602 | components = xb_silo_query (helper->silo, "component", 0, error); |
| 603 | if (components == NULL) |
| 604 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 605 | helper->action_ids = g_ptr_array_new_with_free_func (g_free); |
| 606 | helper->install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 607 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 608 | for (guint i = 0; i < components->len; i++) { |
| 609 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 610 | |
| 611 | /* do any devices pass the requirements */ |
| 612 | for (guint j = 0; j < devices_possible->len; j++) { |
| 613 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 614 | const gchar *action_id; |
| 615 | g_autoptr(FuInstallTask) task = NULL; |
| 616 | g_autoptr(GError) error_local = NULL; |
| 617 | |
| 618 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 619 | task = fu_install_task_new (device, component); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 620 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 621 | task, |
| 622 | helper->flags, |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 623 | &error_local)) { |
| 624 | g_debug ("requirement on %s:%s failed: %s", |
| 625 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 626 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 627 | error_local->message); |
| 628 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | /* get the action IDs for the valid device */ |
| 633 | action_id = fu_install_task_get_action_id (task); |
| 634 | if (!g_ptr_array_find (helper->action_ids, action_id, NULL)) |
| 635 | g_ptr_array_add (helper->action_ids, g_strdup (action_id)); |
| 636 | g_ptr_array_add (helper->install_tasks, g_steal_pointer (&task)); |
| 637 | } |
| 638 | } |
| 639 | |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 640 | /* order the install tasks by the device priority */ |
| 641 | g_ptr_array_sort (helper->install_tasks, fu_main_install_task_sort_cb); |
| 642 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 643 | /* nothing suitable */ |
| 644 | if (helper->install_tasks->len == 0) { |
Richard Hughes | e82eef3 | 2018-05-20 10:41:26 +0100 | [diff] [blame] | 645 | GError *error_tmp = fu_common_error_array_get_best (errors); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 646 | g_propagate_error (error, error_tmp); |
| 647 | return FALSE; |
| 648 | } |
| 649 | |
| 650 | /* authenticate all things in the action_ids */ |
| 651 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 652 | fu_main_authorize_install_queue (g_steal_pointer (&helper)); |
| 653 | return TRUE; |
| 654 | } |
| 655 | |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 656 | static gboolean |
| 657 | fu_main_device_id_valid (const gchar *device_id, GError **error) |
| 658 | { |
| 659 | if (g_strcmp0 (device_id, FWUPD_DEVICE_ID_ANY) == 0) |
| 660 | return TRUE; |
| 661 | if (device_id != NULL && strlen (device_id) >= 4) |
| 662 | return TRUE; |
| 663 | g_set_error (error, |
| 664 | FWUPD_ERROR, |
| 665 | FWUPD_ERROR_INTERNAL, |
| 666 | "invalid device ID: %s", |
| 667 | device_id); |
| 668 | return FALSE; |
| 669 | } |
| 670 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 671 | static void |
| 672 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 673 | const gchar *object_path, const gchar *interface_name, |
| 674 | const gchar *method_name, GVariant *parameters, |
| 675 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 676 | { |
| 677 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 678 | GVariant *val = NULL; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 679 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 680 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 681 | /* activity */ |
| 682 | fu_engine_idle_reset (priv->engine); |
| 683 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 684 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 685 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 686 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 687 | devices = fu_engine_get_devices (priv->engine, &error); |
| 688 | if (devices == NULL) { |
| 689 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 690 | return; |
| 691 | } |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 692 | val = fu_main_device_array_to_variant (priv, sender, devices, &error); |
| 693 | if (val == NULL) { |
| 694 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 695 | return; |
| 696 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 697 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 698 | return; |
| 699 | } |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 700 | if (g_strcmp0 (method_name, "GetReleases") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 701 | const gchar *device_id; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 702 | g_autoptr(GPtrArray) releases = NULL; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 703 | g_variant_get (parameters, "(&s)", &device_id); |
| 704 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 705 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 706 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 707 | return; |
| 708 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 709 | releases = fu_engine_get_releases (priv->engine, device_id, &error); |
| 710 | if (releases == NULL) { |
| 711 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 712 | return; |
| 713 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 714 | val = fu_main_release_array_to_variant (releases); |
| 715 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 716 | return; |
| 717 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 718 | if (g_strcmp0 (method_name, "GetDowngrades") == 0) { |
| 719 | const gchar *device_id; |
| 720 | g_autoptr(GPtrArray) releases = NULL; |
| 721 | g_variant_get (parameters, "(&s)", &device_id); |
| 722 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 723 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 724 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 725 | return; |
| 726 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 727 | releases = fu_engine_get_downgrades (priv->engine, device_id, &error); |
| 728 | if (releases == NULL) { |
| 729 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 730 | return; |
| 731 | } |
| 732 | val = fu_main_release_array_to_variant (releases); |
| 733 | g_dbus_method_invocation_return_value (invocation, val); |
| 734 | return; |
| 735 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 736 | if (g_strcmp0 (method_name, "GetUpgrades") == 0) { |
| 737 | const gchar *device_id; |
| 738 | g_autoptr(GPtrArray) releases = NULL; |
| 739 | g_variant_get (parameters, "(&s)", &device_id); |
| 740 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 741 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 742 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 743 | return; |
| 744 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 745 | releases = fu_engine_get_upgrades (priv->engine, device_id, &error); |
| 746 | if (releases == NULL) { |
| 747 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 748 | return; |
| 749 | } |
| 750 | val = fu_main_release_array_to_variant (releases); |
| 751 | g_dbus_method_invocation_return_value (invocation, val); |
| 752 | return; |
| 753 | } |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 754 | if (g_strcmp0 (method_name, "GetRemotes") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 755 | g_autoptr(GPtrArray) remotes = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 756 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 757 | remotes = fu_engine_get_remotes (priv->engine, &error); |
| 758 | if (remotes == NULL) { |
| 759 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 760 | return; |
| 761 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 762 | val = fu_main_remote_array_to_variant (remotes); |
| 763 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 764 | return; |
| 765 | } |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 766 | if (g_strcmp0 (method_name, "GetHistory") == 0) { |
| 767 | g_autoptr(GPtrArray) devices = NULL; |
| 768 | g_debug ("Called %s()", method_name); |
| 769 | devices = fu_engine_get_history (priv->engine, &error); |
| 770 | if (devices == NULL) { |
| 771 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 772 | return; |
| 773 | } |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 774 | val = fu_main_device_array_to_variant (priv, sender, devices, &error); |
| 775 | if (val == NULL) { |
| 776 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 777 | return; |
| 778 | } |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 779 | g_dbus_method_invocation_return_value (invocation, val); |
| 780 | return; |
| 781 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 782 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 783 | const gchar *device_id; |
| 784 | g_variant_get (parameters, "(&s)", &device_id); |
| 785 | g_debug ("Called %s(%s)", method_name, device_id); |
| 786 | if (!fu_engine_clear_results (priv->engine, device_id, &error)) { |
| 787 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 788 | return; |
| 789 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 790 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 791 | return; |
| 792 | } |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 793 | if (g_strcmp0 (method_name, "ModifyDevice") == 0) { |
| 794 | const gchar *device_id; |
| 795 | const gchar *key = NULL; |
| 796 | const gchar *value = NULL; |
| 797 | |
| 798 | /* check the id exists */ |
| 799 | g_variant_get (parameters, "(&s&s&s)", &device_id, &key, &value); |
| 800 | g_debug ("Called %s(%s,%s=%s)", method_name, device_id, key, value); |
| 801 | if (!fu_engine_modify_device (priv->engine, device_id, key, value, &error)) { |
| 802 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 803 | return; |
| 804 | } |
| 805 | g_dbus_method_invocation_return_value (invocation, NULL); |
| 806 | return; |
| 807 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 808 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 809 | const gchar *device_id = NULL; |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 810 | g_autoptr(FwupdDevice) result = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 811 | g_variant_get (parameters, "(&s)", &device_id); |
| 812 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 813 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 814 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 815 | return; |
| 816 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 817 | result = fu_engine_get_results (priv->engine, device_id, &error); |
| 818 | if (result == NULL) { |
| 819 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 820 | return; |
| 821 | } |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 822 | val = fwupd_device_to_variant (result); |
| 823 | g_dbus_method_invocation_return_value (invocation, |
| 824 | g_variant_new_tuple (&val, 1)); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 825 | return; |
| 826 | } |
Richard Hughes | ba73c76 | 2017-09-15 14:31:17 +0100 | [diff] [blame] | 827 | if (g_strcmp0 (method_name, "UpdateMetadata") == 0) { |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 828 | GDBusMessage *message; |
| 829 | GUnixFDList *fd_list; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 830 | const gchar *remote_id = NULL; |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 831 | gint fd_data; |
| 832 | gint fd_sig; |
| 833 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 834 | g_variant_get (parameters, "(&shh)", &remote_id, &fd_data, &fd_sig); |
| 835 | 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] | 836 | |
Richard Hughes | 5935ebd | 2017-06-16 15:40:31 +0100 | [diff] [blame] | 837 | /* update the metadata store */ |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 838 | message = g_dbus_method_invocation_get_message (invocation); |
| 839 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 840 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 2) { |
| 841 | g_set_error (&error, |
| 842 | FWUPD_ERROR, |
| 843 | FWUPD_ERROR_INTERNAL, |
| 844 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 845 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 846 | return; |
| 847 | } |
| 848 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 849 | if (fd_data < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 850 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 851 | return; |
| 852 | } |
| 853 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 854 | if (fd_sig < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 855 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 856 | return; |
| 857 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 858 | |
| 859 | /* store new metadata (will close the fds when done) */ |
| 860 | if (!fu_engine_update_metadata (priv->engine, remote_id, |
| 861 | fd_data, fd_sig, &error)) { |
Richard Hughes | f3d46c6 | 2017-11-28 14:01:30 +0000 | [diff] [blame] | 862 | g_prefix_error (&error, "Failed to update metadata for %s: ", remote_id); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 863 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 864 | return; |
| 865 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 866 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 867 | return; |
| 868 | } |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 869 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 870 | const gchar *device_id = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 871 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 872 | g_autoptr(PolkitSubject) subject = NULL; |
| 873 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 874 | g_variant_get (parameters, "(&s)", &device_id); |
| 875 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 876 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 877 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 878 | return; |
| 879 | } |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 880 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 881 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 882 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 883 | helper = g_new0 (FuMainAuthHelper, 1); |
| 884 | helper->priv = priv; |
| 885 | helper->invocation = g_object_ref (invocation); |
| 886 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 887 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 888 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 889 | "org.freedesktop.fwupd.device-unlock", |
| 890 | NULL, |
| 891 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 892 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 893 | fu_main_authorize_unlock_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 894 | g_steal_pointer (&helper)); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 895 | return; |
| 896 | } |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 897 | if (g_strcmp0 (method_name, "ModifyRemote") == 0) { |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 898 | const gchar *remote_id = NULL; |
| 899 | const gchar *key = NULL; |
| 900 | const gchar *value = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 901 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 902 | g_autoptr(PolkitSubject) subject = NULL; |
| 903 | |
| 904 | /* check the id exists */ |
| 905 | g_variant_get (parameters, "(&s&s&s)", &remote_id, &key, &value); |
| 906 | g_debug ("Called %s(%s,%s=%s)", method_name, remote_id, key, value); |
| 907 | |
| 908 | /* create helper object */ |
| 909 | helper = g_new0 (FuMainAuthHelper, 1); |
| 910 | helper->invocation = g_object_ref (invocation); |
| 911 | helper->remote_id = g_strdup (remote_id); |
| 912 | helper->key = g_strdup (key); |
| 913 | helper->value = g_strdup (value); |
| 914 | helper->priv = priv; |
| 915 | |
| 916 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 917 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 918 | subject = polkit_system_bus_name_new (sender); |
Mario Limonciello | 6b9f07c | 2018-04-16 13:52:09 -0500 | [diff] [blame] | 919 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 920 | "org.freedesktop.fwupd.modify-remote", |
| 921 | NULL, |
| 922 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 923 | NULL, |
| 924 | fu_main_authorize_modify_remote_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 925 | g_steal_pointer (&helper)); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 926 | return; |
| 927 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 928 | if (g_strcmp0 (method_name, "VerifyUpdate") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 929 | const gchar *device_id = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 930 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 931 | g_autoptr(PolkitSubject) subject = NULL; |
| 932 | |
| 933 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 934 | g_variant_get (parameters, "(&s)", &device_id); |
| 935 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 936 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 937 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 938 | return; |
| 939 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 940 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 941 | /* create helper object */ |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 942 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 943 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 944 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 945 | helper->priv = priv; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 946 | |
| 947 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 948 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 949 | subject = polkit_system_bus_name_new (sender); |
Mario Limonciello | 6b9f07c | 2018-04-16 13:52:09 -0500 | [diff] [blame] | 950 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 951 | "org.freedesktop.fwupd.verify-update", |
| 952 | NULL, |
| 953 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 954 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 955 | fu_main_authorize_verify_update_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 956 | g_steal_pointer (&helper)); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 957 | return; |
| 958 | } |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 959 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 960 | const gchar *device_id = NULL; |
| 961 | g_variant_get (parameters, "(&s)", &device_id); |
| 962 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 963 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 964 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 965 | return; |
| 966 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 967 | if (!fu_engine_verify (priv->engine, device_id, &error)) { |
| 968 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 969 | return; |
| 970 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 971 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 972 | return; |
| 973 | } |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 974 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 975 | GVariant *prop_value; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 976 | const gchar *device_id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 977 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 978 | gint32 fd_handle = 0; |
| 979 | gint fd; |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 980 | guint64 archive_size_max; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 981 | GDBusMessage *message; |
| 982 | GUnixFDList *fd_list; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 983 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 984 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 985 | |
| 986 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 987 | g_variant_get (parameters, "(&sha{sv})", &device_id, &fd_handle, &iter); |
| 988 | g_debug ("Called %s(%s,%i)", method_name, device_id, fd_handle); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 989 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 990 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 991 | return; |
| 992 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 993 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 994 | /* create helper object */ |
| 995 | helper = g_new0 (FuMainAuthHelper, 1); |
| 996 | helper->invocation = g_object_ref (invocation); |
| 997 | helper->device_id = g_strdup (device_id); |
| 998 | helper->priv = priv; |
| 999 | |
| 1000 | /* get flags */ |
| 1001 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1002 | g_debug ("got option %s", prop_key); |
| 1003 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 1004 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1005 | helper->flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1006 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 1007 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1008 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1009 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 1010 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1011 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 1012 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 1013 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1014 | helper->flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 76e0f94 | 2018-05-14 16:24:00 +0100 | [diff] [blame] | 1015 | if (g_strcmp0 (prop_key, "no-history") == 0 && |
| 1016 | g_variant_get_boolean (prop_value) == TRUE) |
| 1017 | helper->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 1018 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1021 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1022 | /* get the fd */ |
| 1023 | message = g_dbus_method_invocation_get_message (invocation); |
| 1024 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1025 | 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] | 1026 | g_set_error (&error, |
| 1027 | FWUPD_ERROR, |
| 1028 | FWUPD_ERROR_INTERNAL, |
| 1029 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1030 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 1033 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1034 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1035 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1036 | return; |
| 1037 | } |
| 1038 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1039 | /* parse the cab file before authenticating so we can work out |
| 1040 | * what action ID to use, for instance, if this is trusted -- |
| 1041 | * this will also close the fd when done */ |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 1042 | archive_size_max = fu_engine_get_archive_size_max (priv->engine); |
| 1043 | helper->blob_cab = fu_common_get_contents_fd (fd, archive_size_max, &error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1044 | if (helper->blob_cab == NULL) { |
| 1045 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1046 | return; |
| 1047 | } |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 1048 | |
| 1049 | /* install all the things in the store */ |
| 1050 | helper->subject = polkit_system_bus_name_new (sender); |
| 1051 | if (!fu_main_install_with_helper (g_steal_pointer (&helper), &error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1052 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 1056 | /* async return */ |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1057 | return; |
| 1058 | } |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 1059 | if (g_strcmp0 (method_name, "GetDetails") == 0) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1060 | GDBusMessage *message; |
| 1061 | GUnixFDList *fd_list; |
| 1062 | gint32 fd_handle = 0; |
| 1063 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1064 | g_autoptr(GPtrArray) results = NULL; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1065 | |
| 1066 | /* get parameters */ |
| 1067 | g_variant_get (parameters, "(h)", &fd_handle); |
| 1068 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 1069 | |
| 1070 | /* get the fd */ |
| 1071 | message = g_dbus_method_invocation_get_message (invocation); |
| 1072 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1073 | 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] | 1074 | g_set_error (&error, |
| 1075 | FWUPD_ERROR, |
| 1076 | FWUPD_ERROR_INTERNAL, |
| 1077 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1078 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1079 | return; |
| 1080 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 1081 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1082 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1083 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1084 | return; |
| 1085 | } |
| 1086 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1087 | /* get details about the file (will close the fd when done) */ |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 1088 | results = fu_engine_get_details (priv->engine, fd, &error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1089 | if (results == NULL) { |
| 1090 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1091 | return; |
| 1092 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1093 | val = fu_main_result_array_to_variant (results); |
| 1094 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1095 | return; |
| 1096 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1097 | g_set_error (&error, |
| 1098 | G_DBUS_ERROR, |
| 1099 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 1100 | "no such method %s", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1101 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1104 | static GVariant * |
| 1105 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 1106 | const gchar *object_path, const gchar *interface_name, |
| 1107 | const gchar *property_name, GError **error, |
| 1108 | gpointer user_data) |
| 1109 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1110 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1111 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1112 | /* activity */ |
| 1113 | fu_engine_idle_reset (priv->engine); |
| 1114 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1115 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
| 1116 | return g_variant_new_string (VERSION); |
| 1117 | |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame^] | 1118 | if (g_strcmp0 (property_name, "Tainted") == 0) |
| 1119 | return g_variant_new_boolean (fu_engine_get_tainted (priv->engine)); |
| 1120 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1121 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1122 | return g_variant_new_uint32 (fu_engine_get_status (priv->engine)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1123 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1124 | /* return an error */ |
| 1125 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1126 | G_DBUS_ERROR, |
| 1127 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1128 | "failed to get daemon property %s", |
| 1129 | property_name); |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 1133 | static void |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1134 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 1135 | const gchar *name, |
| 1136 | gpointer user_data) |
| 1137 | { |
| 1138 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1139 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1140 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1141 | static const GDBusInterfaceVTable interface_vtable = { |
| 1142 | fu_main_daemon_method_call, |
| 1143 | fu_main_daemon_get_property, |
| 1144 | NULL |
| 1145 | }; |
| 1146 | |
| 1147 | priv->connection = g_object_ref (connection); |
| 1148 | registration_id = g_dbus_connection_register_object (connection, |
| 1149 | FWUPD_DBUS_PATH, |
| 1150 | priv->introspection_daemon->interfaces[0], |
| 1151 | &interface_vtable, |
| 1152 | priv, /* user_data */ |
| 1153 | NULL, /* user_data_free_func */ |
| 1154 | NULL); /* GError** */ |
| 1155 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1156 | |
| 1157 | /* connect to D-Bus directly */ |
| 1158 | priv->proxy_uid = |
| 1159 | g_dbus_proxy_new_sync (priv->connection, |
| 1160 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 1161 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 1162 | NULL, |
| 1163 | "org.freedesktop.DBus", |
| 1164 | "/org/freedesktop/DBus", |
| 1165 | "org.freedesktop.DBus", |
| 1166 | NULL, |
| 1167 | &error); |
| 1168 | if (priv->proxy_uid == NULL) { |
| 1169 | g_warning ("cannot connect to DBus: %s", error->message); |
| 1170 | return; |
| 1171 | } |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1174 | static void |
| 1175 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 1176 | const gchar *name, |
| 1177 | gpointer user_data) |
| 1178 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1179 | g_debug ("FuMain: acquired name: %s", name); |
| 1180 | } |
| 1181 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1182 | static void |
| 1183 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 1184 | const gchar *name, |
| 1185 | gpointer user_data) |
| 1186 | { |
| 1187 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1188 | g_debug ("FuMain: lost name: %s", name); |
| 1189 | g_main_loop_quit (priv->loop); |
| 1190 | } |
| 1191 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1192 | static gboolean |
| 1193 | fu_main_timed_exit_cb (gpointer user_data) |
| 1194 | { |
| 1195 | GMainLoop *loop = (GMainLoop *) user_data; |
| 1196 | g_main_loop_quit (loop); |
| 1197 | return G_SOURCE_REMOVE; |
| 1198 | } |
| 1199 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1200 | static GDBusNodeInfo * |
| 1201 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 1202 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1203 | g_autoptr(GBytes) data = NULL; |
| 1204 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* lookup data */ |
| 1207 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 1208 | data = g_resource_lookup_data (fu_get_resource (), |
| 1209 | path, |
| 1210 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 1211 | error); |
| 1212 | if (data == NULL) |
| 1213 | return NULL; |
| 1214 | |
| 1215 | /* build introspection from XML */ |
| 1216 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 1217 | } |
| 1218 | |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1219 | static void |
| 1220 | fu_main_private_free (FuMainPrivate *priv) |
| 1221 | { |
| 1222 | if (priv->loop != NULL) |
| 1223 | g_main_loop_unref (priv->loop); |
| 1224 | if (priv->owner_id > 0) |
| 1225 | g_bus_unown_name (priv->owner_id); |
| 1226 | if (priv->proxy_uid != NULL) |
| 1227 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1228 | if (priv->engine != NULL) |
| 1229 | g_object_unref (priv->engine); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1230 | if (priv->connection != NULL) |
| 1231 | g_object_unref (priv->connection); |
| 1232 | if (priv->authority != NULL) |
| 1233 | g_object_unref (priv->authority); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1234 | if (priv->introspection_daemon != NULL) |
| 1235 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1236 | g_free (priv); |
| 1237 | } |
| 1238 | |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 1239 | #pragma clang diagnostic push |
| 1240 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1241 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainPrivate, fu_main_private_free) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 1242 | #pragma clang diagnostic pop |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1243 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1244 | int |
| 1245 | main (int argc, char *argv[]) |
| 1246 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1247 | gboolean immediate_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1248 | gboolean timed_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1249 | const GOptionEntry options[] = { |
| 1250 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 1251 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 1252 | _("Exit after a small delay"), NULL }, |
| 1253 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 1254 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 1255 | _("Exit after the engine has loaded"), NULL }, |
| 1256 | { NULL} |
| 1257 | }; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1258 | g_autoptr(FuMainPrivate) priv = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1259 | g_autoptr(GError) error = NULL; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1260 | g_autoptr(GOptionContext) context = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1261 | |
| 1262 | setlocale (LC_ALL, ""); |
| 1263 | |
| 1264 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 1265 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 1266 | textdomain (GETTEXT_PACKAGE); |
| 1267 | |
| 1268 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1269 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1270 | context = g_option_context_new (NULL); |
| 1271 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1272 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 1273 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1274 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1275 | if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 1276 | g_printerr ("Failed to parse command line: %s\n", error->message); |
| 1277 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* create new objects */ |
| 1281 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1282 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1283 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1284 | /* load engine */ |
Richard Hughes | 5b5f655 | 2018-05-18 10:22:39 +0100 | [diff] [blame] | 1285 | priv->engine = fu_engine_new (FU_APP_FLAGS_NONE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1286 | g_signal_connect (priv->engine, "changed", |
| 1287 | G_CALLBACK (fu_main_engine_changed_cb), |
| 1288 | priv); |
| 1289 | g_signal_connect (priv->engine, "device-added", |
| 1290 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1291 | priv); |
| 1292 | g_signal_connect (priv->engine, "device-removed", |
| 1293 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1294 | priv); |
| 1295 | g_signal_connect (priv->engine, "device-changed", |
| 1296 | G_CALLBACK (fu_main_engine_device_changed_cb), |
| 1297 | priv); |
| 1298 | g_signal_connect (priv->engine, "status-changed", |
| 1299 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1300 | priv); |
| 1301 | g_signal_connect (priv->engine, "percentage-changed", |
| 1302 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1303 | priv); |
| 1304 | if (!fu_engine_load (priv->engine, &error)) { |
| 1305 | g_printerr ("Failed to load engine: %s\n", error->message); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1306 | return EXIT_FAILURE; |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 1307 | } |
| 1308 | |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 1309 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1310 | SIGTERM, fu_main_sigterm_cb, |
| 1311 | priv, NULL); |
| 1312 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1313 | /* load introspection from file */ |
| 1314 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 1315 | &error); |
| 1316 | if (priv->introspection_daemon == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1317 | g_printerr ("Failed to load introspection: %s\n", error->message); |
| 1318 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1321 | /* get authority */ |
| 1322 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 1323 | if (priv->authority == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1324 | g_printerr ("Failed to load authority: %s\n", error->message); |
| 1325 | return EXIT_FAILURE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Richard Hughes | ebae396 | 2018-09-09 13:40:49 +0100 | [diff] [blame] | 1328 | /* own the object */ |
| 1329 | priv->owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 1330 | FWUPD_DBUS_SERVICE, |
| 1331 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 1332 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 1333 | fu_main_on_bus_acquired_cb, |
| 1334 | fu_main_on_name_acquired_cb, |
| 1335 | fu_main_on_name_lost_cb, |
| 1336 | priv, NULL); |
| 1337 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1338 | /* Only timeout and close the mainloop if we have specified it |
| 1339 | * on the command line */ |
| 1340 | if (immediate_exit) |
| 1341 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 1342 | else if (timed_exit) |
| 1343 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 1344 | |
Mario Limonciello | 46bb4e9 | 2019-01-07 09:44:31 -0600 | [diff] [blame] | 1345 | g_debug ("Started with locale %s", g_getenv ("LANG")); |
| 1346 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1347 | /* wait */ |
Richard Hughes | 4619f9f | 2017-06-14 13:55:30 +0100 | [diff] [blame] | 1348 | g_message ("Daemon ready for requests"); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1349 | g_main_loop_run (priv->loop); |
| 1350 | |
| 1351 | /* success */ |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1352 | return EXIT_SUCCESS; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1353 | } |