fu-util: Check that daemon is started by expected unit

Unexpected behaviors can happen if:
* a snapped daemon is running with a packaged frontend
* a packaged daemon is running with a snapped frontend

This should make sure that if the snap is installed on top of a
packaged frontend that people don't try to mix and match as much.
This commit is contained in:
Mario Limonciello 2019-05-07 15:52:39 -05:00 committed by Mario Limonciello
parent ea527ca2e7
commit 88f8b7f8e1
3 changed files with 71 additions and 0 deletions

View File

@ -27,6 +27,68 @@ fu_util_get_systemd_unit (void)
return SYSTEMD_FWUPD_UNIT;
}
static const gchar *
fu_util_get_expected_command (const gchar *target)
{
if (g_strcmp0 (target, SYSTEMD_SNAP_FWUPD_UNIT))
return "fwupd.fwupdmgr";
return "fwupdmgr";
}
gboolean
fu_util_using_correct_daemon (GError **error)
{
g_autoptr(GDBusConnection) connection = NULL;
g_autoptr(GVariant) default_target = NULL;
g_autoptr(GVariant) val_path = NULL;
g_autoptr(GError) error_local = NULL;
const gchar *target = fu_util_get_systemd_unit ();
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
if (connection == NULL)
return FALSE;
default_target = g_dbus_connection_call_sync (connection,
SYSTEMD_SERVICE,
SYSTEMD_OBJECT_PATH,
SYSTEMD_MANAGER_INTERFACE,
"GetDefaultTarget",
NULL,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error_local);
if (default_target == NULL) {
g_debug ("Systemd isn't accessible: %s\n", error_local->message);
return TRUE;
}
val_path = g_dbus_connection_call_sync (connection,
SYSTEMD_SERVICE,
SYSTEMD_OBJECT_PATH,
SYSTEMD_MANAGER_INTERFACE,
"GetUnit",
g_variant_new ("(s)",
target),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL);
if (val_path == NULL) {
g_set_error (error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_ARGS,
/* TRANSLATORS: error message */
_("Mismatched daemon and client, use %s instead"),
fu_util_get_expected_command (target));
return FALSE;
}
return TRUE;
}
gboolean
fu_util_stop_daemon (GError **error)
{

View File

@ -60,6 +60,7 @@ gboolean fu_util_cmd_array_run (GPtrArray *array,
GError **error);
gchar *fu_util_release_get_name (FwupdRelease *release);
gboolean fu_util_using_correct_daemon (GError **error);
gboolean fu_util_stop_daemon (GError **error);
G_END_DECLS

View File

@ -2481,6 +2481,14 @@ main (int argc, char *argv[])
"is no longer supported by the upstream developers!\n");
}
#ifdef HAVE_SYSTEMD
/* make sure the correct daemon is in use */
if (!fu_util_using_correct_daemon (&error)) {
g_printerr ("%s\n", error->message);
return EXIT_FAILURE;
}
#endif
/* run the specified command */
ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
if (!ret) {