1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Copyright 2020 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <gio/gunixfdlist.h>
#include <glib/gstdio.h>
#include "fu-logind-plugin.h"
struct _FuLogindPlugin {
FuPlugin parent_instance;
GDBusProxy *logind_proxy;
gint logind_fd;
};
G_DEFINE_TYPE(FuLogindPlugin, fu_logind_plugin, FU_TYPE_PLUGIN)
static void
fu_logind_plugin_to_string(FuPlugin *plugin, guint idt, GString *str)
{
FuLogindPlugin *self = FU_LOGIND_PLUGIN(plugin);
fwupd_codec_string_append_hex(str, idt, "LogindFd", self->logind_fd);
}
static gboolean
fu_logind_plugin_startup(FuPlugin *plugin, FuProgress *progress, GError **error)
{
FuLogindPlugin *self = FU_LOGIND_PLUGIN(plugin);
g_autofree gchar *name_owner = NULL;
self->logind_proxy = g_dbus_proxy_new_for_bus_sync(
G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
NULL,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
NULL,
error);
if (self->logind_proxy == NULL) {
g_prefix_error_literal(error, "failed to connect to logind: ");
return FALSE;
}
name_owner = g_dbus_proxy_get_name_owner(self->logind_proxy);
if (name_owner == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"no owner for %s",
g_dbus_proxy_get_name(self->logind_proxy));
return FALSE;
}
return TRUE;
}
static gboolean
fu_logind_plugin_composite_prepare(FuPlugin *plugin, GPtrArray *devices, GError **error)
{
FuLogindPlugin *self = FU_LOGIND_PLUGIN(plugin);
g_autoptr(GError) error_local = NULL;
g_autoptr(GUnixFDList) out_fd_list = NULL;
g_autoptr(GVariant) res = NULL;
const gchar *what = "shutdown:sleep:idle:handle-power-key:handle-suspend-key:"
"handle-hibernate-key:handle-lid-switch";
/* already inhibited */
if (self->logind_fd >= 0)
return TRUE;
/* not yet connected */
if (self->logind_proxy == NULL) {
g_warning("no logind connection to use");
return TRUE;
}
/* block shutdown and idle */
res = g_dbus_proxy_call_with_unix_fd_list_sync(
self->logind_proxy,
"Inhibit",
g_variant_new("(ssss)", what, PACKAGE_NAME, "Firmware Update in Progress", "block"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL, /* fd_list */
&out_fd_list,
NULL, /* GCancellable */
&error_local);
if (res == NULL) {
g_warning("failed to Inhibit using logind: %s", error_local->message);
return TRUE;
}
/* keep fd as cookie */
if (g_unix_fd_list_get_length(out_fd_list) != 1) {
g_warning("invalid response from logind");
return TRUE;
}
self->logind_fd = g_unix_fd_list_get(out_fd_list, 0, NULL);
g_debug("opened logind fd %i", self->logind_fd);
return TRUE;
}
static gboolean
fu_logind_plugin_composite_cleanup(FuPlugin *plugin, GPtrArray *devices, GError **error)
{
FuLogindPlugin *self = FU_LOGIND_PLUGIN(plugin);
if (self->logind_fd < 0)
return TRUE;
g_debug("closed logind fd %i", self->logind_fd);
if (!g_close(self->logind_fd, error))
return FALSE;
self->logind_fd = -1;
return TRUE;
}
static void
fu_logind_plugin_init(FuLogindPlugin *self)
{
self->logind_fd = -1;
}
static void
fu_logind_plugin_finalize(GObject *obj)
{
FuLogindPlugin *self = FU_LOGIND_PLUGIN(obj);
if (self->logind_fd >= 0)
g_close(self->logind_fd, NULL);
if (self->logind_proxy != NULL)
g_object_unref(self->logind_proxy);
G_OBJECT_CLASS(fu_logind_plugin_parent_class)->finalize(obj);
}
static void
fu_logind_plugin_class_init(FuLogindPluginClass *klass)
{
FuPluginClass *plugin_class = FU_PLUGIN_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = fu_logind_plugin_finalize;
plugin_class->to_string = fu_logind_plugin_to_string;
plugin_class->startup = fu_logind_plugin_startup;
plugin_class->composite_cleanup = fu_logind_plugin_composite_cleanup;
plugin_class->composite_prepare = fu_logind_plugin_composite_prepare;
}
|