neil / rpms / udisks2

Forked from rpms/udisks2 a year ago
Clone

Blame SOURCES/udisks-2.9.2-udisksdaemonutil-Refactor-udisks_daemon_util_trigger.patch

04e847
From 8d5c90a1f4bbe548a1ae361f8d69970669d6e72e Mon Sep 17 00:00:00 2001
04e847
From: Tomas Bzatek <tbzatek@redhat.com>
04e847
Date: Fri, 13 Nov 2020 16:52:11 +0100
04e847
Subject: [PATCH 1/2] udisksdaemonutil: Refactor
04e847
 udisks_daemon_util_trigger_uevent() out of UDisksLinuxBlockObject
04e847
04e847
This decouples uevent triggering from UDisksLinuxBlockObject as sometimes
04e847
we don't have a block object yet or it's outdated.
04e847
---
04e847
 doc/udisks2-sections.txt.daemon.sections.in |   2 +
04e847
 src/udisksdaemonutil.c                      | 230 ++++++++++++++++++++
04e847
 src/udisksdaemonutil.h                      |   7 +
04e847
 src/udiskslinuxblockobject.c                | 180 ++-------------
04e847
 4 files changed, 253 insertions(+), 166 deletions(-)
04e847
04e847
diff --git a/doc/udisks2-sections.txt.daemon.sections.in b/doc/udisks2-sections.txt.daemon.sections.in
04e847
index 26c3c2cd..3030fa95 100644
04e847
--- a/doc/udisks2-sections.txt.daemon.sections.in
04e847
+++ b/doc/udisks2-sections.txt.daemon.sections.in
04e847
@@ -307,6 +307,8 @@ udisks_daemon_util_file_set_contents
04e847
 udisks_daemon_util_on_user_seat
04e847
 udisks_daemon_util_get_free_mdraid_device
04e847
 udisks_ata_identify_get_word
04e847
+udisks_daemon_util_trigger_uevent
04e847
+udisks_daemon_util_trigger_uevent_sync
04e847
 </SECTION>
04e847
 
04e847
 <SECTION>
04e847
diff --git a/src/udisksdaemonutil.c b/src/udisksdaemonutil.c
04e847
index 75b877a0..d16c0766 100644
04e847
--- a/src/udisksdaemonutil.c
04e847
+++ b/src/udisksdaemonutil.c
04e847
@@ -34,10 +34,14 @@
04e847
 #include <limits.h>
04e847
 #include <stdlib.h>
04e847
 
04e847
+#include <blockdev/blockdev.h>
04e847
+
04e847
 #include "udisksdaemon.h"
04e847
 #include "udisksdaemonutil.h"
04e847
 #include "udisksstate.h"
04e847
 #include "udiskslogging.h"
04e847
+#include "udiskslinuxdevice.h"
04e847
+#include "udiskslinuxprovider.h"
04e847
 #include "udiskslinuxblockobject.h"
04e847
 #include "udiskslinuxdriveobject.h"
04e847
 
04e847
@@ -1626,3 +1630,229 @@ udisks_ata_identify_get_word (const guchar *identify_data, guint word_number)
04e847
  out:
04e847
   return ret;
04e847
 }
04e847
+
04e847
+
04e847
+/* ---------------------------------------------------------------------------------------------------- */
04e847
+
04e847
+static volatile guint uevent_serial = 0;
04e847
+
04e847
+static gboolean
04e847
+trigger_uevent (const gchar *path, const gchar *str)
04e847
+{
04e847
+  gint fd;
04e847
+
04e847
+  fd = open (path, O_WRONLY);
04e847
+  if (fd < 0)
04e847
+    {
04e847
+      udisks_warning ("Error opening %s while triggering uevent: %m", path);
04e847
+      return FALSE;
04e847
+    }
04e847
+
04e847
+  if (write (fd, str, strlen (str)) != (ssize_t) strlen (str))
04e847
+    {
04e847
+      udisks_warning ("Error writing '%s' to file %s: %m", str, path);
04e847
+      close (fd);
04e847
+      return FALSE;
04e847
+    }
04e847
+
04e847
+  close (fd);
04e847
+  return TRUE;
04e847
+}
04e847
+
04e847
+typedef struct
04e847
+{
04e847
+  UDisksDaemon *daemon;
04e847
+  GMainLoop *main_loop;
04e847
+  guint serial;
04e847
+  gchar *uevent_path;
04e847
+  gboolean success;
04e847
+} SynthUeventData;
04e847
+
04e847
+static gboolean
04e847
+trigger_uevent_idle_cb (gpointer user_data)
04e847
+{
04e847
+  SynthUeventData *data = user_data;
04e847
+  gchar *str;
04e847
+
04e847
+  str = g_strdup_printf ("change %s UDISKSSERIAL=%u", udisks_daemon_get_uuid (data->daemon), data->serial);
04e847
+
04e847
+  if (! trigger_uevent (data->uevent_path, str))
04e847
+    {
04e847
+      /* kernel refused our string, try simple "change" but don't wait for it */
04e847
+      trigger_uevent (data->uevent_path, "change");
04e847
+      data->success = FALSE;
04e847
+      g_main_loop_quit (data->main_loop);
04e847
+    }
04e847
+  g_free (str);
04e847
+
04e847
+  /* remove the source */
04e847
+  return FALSE;
04e847
+}
04e847
+
04e847
+static gboolean
04e847
+uevent_wait_timeout_cb (gpointer user_data)
04e847
+{
04e847
+  SynthUeventData *data = user_data;
04e847
+
04e847
+  data->success = FALSE;
04e847
+  g_main_loop_quit (data->main_loop);
04e847
+
04e847
+  /* remove the source */
04e847
+  return FALSE;
04e847
+}
04e847
+
04e847
+static void
04e847
+uevent_probed_cb (UDisksLinuxProvider *provider,
04e847
+                  const gchar         *action,
04e847
+                  UDisksLinuxDevice   *device,
04e847
+                  gpointer             user_data)
04e847
+{
04e847
+  SynthUeventData *data = user_data;
04e847
+  const gchar *received_serial_str;
04e847
+  gint64 received_serial;
04e847
+  gchar *endptr;
04e847
+
04e847
+  received_serial_str = g_udev_device_get_property (device->udev_device, "SYNTH_ARG_UDISKSSERIAL");
04e847
+  if (received_serial_str != NULL)
04e847
+    {
04e847
+      endptr = (gchar *) received_serial_str;
04e847
+      received_serial = g_ascii_strtoll (received_serial_str, &endptr, 0);
04e847
+      if (endptr != received_serial_str && received_serial == data->serial)
04e847
+        {
04e847
+          data->success = TRUE;
04e847
+          g_main_loop_quit (data->main_loop);
04e847
+        }
04e847
+    }
04e847
+}
04e847
+
04e847
+/**
04e847
+ * udisks_daemon_util_trigger_uevent:
04e847
+ * @daemon: A #UDisksDaemon.
04e847
+ * @device_path: Block device path.
04e847
+ *
04e847
+ * Triggers a 'change' uevent in the kernel.
04e847
+ *
04e847
+ * The triggered event will bubble up from the kernel through the udev
04e847
+ * stack and will eventually be received by the udisks daemon process
04e847
+ * itself. This method does not wait for the event to be received.
04e847
+ */
04e847
+void
04e847
+udisks_daemon_util_trigger_uevent (UDisksDaemon *daemon,
04e847
+                                   const gchar  *device_path)
04e847
+{
04e847
+  GUdevClient *gudev_client;
04e847
+  GUdevDevice *gudev_device;
04e847
+  gchar *path;
04e847
+
04e847
+  g_return_if_fail (UDISKS_IS_DAEMON (daemon));
04e847
+  g_return_if_fail (device_path != NULL);
04e847
+
04e847
+  gudev_client = udisks_linux_provider_get_udev_client (udisks_daemon_get_linux_provider (daemon));
04e847
+  gudev_device = g_udev_client_query_by_device_file (gudev_client, device_path);
04e847
+  if (gudev_device == NULL)
04e847
+    {
04e847
+      udisks_critical ("Device %s not found in udev database, skipping uevent trigger", device_path);
04e847
+      return;
04e847
+    }
04e847
+
04e847
+  path = g_build_filename (g_udev_device_get_sysfs_path (gudev_device), "uevent", NULL);
04e847
+  trigger_uevent (path, "change");
04e847
+  g_free (path);
04e847
+
04e847
+  g_object_unref (gudev_device);
04e847
+}
04e847
+
04e847
+/**
04e847
+ * udisks_daemon_util_trigger_uevent_sync:
04e847
+ * @daemon: A #UDisksDaemon.
04e847
+ * @device_path: Block device path.
04e847
+ * @timeout_seconds: Maximum time to wait for the uevent (in seconds).
04e847
+ *
04e847
+ * Triggers a 'change' uevent in the kernel and waits until it's received and
04e847
+ * processed by udisks.
04e847
+ *
04e847
+ * Unlike udisks_daemon_util_trigger_uevent() that just triggers
04e847
+ * a synthetic uevent to the kernel, this call will actually block and wait until
04e847
+ * the #UDisksLinuxProvider receives the uevent, performs probing and processes
04e847
+ * the uevent further down the UDisks object stack. Upon returning from this
04e847
+ * function call the caller may assume the event has been fully processed, all
04e847
+ * D-Bus objects are updated and settled. Typically used in busy wait for
04e847
+ * a particular D-Bus interface.
04e847
+ *
04e847
+ * Note that this uses synthetic uevent tagging and only works on linux kernel
04e847
+ * 4.13 and higher. In case an older kernel is detected this acts like the classic
04e847
+ * udisks_daemon_util_trigger_uevent() call and %FALSE is returned.
04e847
+ *
04e847
+ * Returns: %TRUE if the uevent has been successfully received, %FALSE otherwise
04e847
+ * or when the kernel version is too old.
04e847
+ */
04e847
+gboolean
04e847
+udisks_daemon_util_trigger_uevent_sync (UDisksDaemon *daemon,
04e847
+                                        const gchar  *device_path,
04e847
+                                        guint         timeout_seconds)
04e847
+{
04e847
+  UDisksLinuxProvider *provider;
04e847
+  GUdevClient *gudev_client;
04e847
+  GUdevDevice *gudev_device;
04e847
+  SynthUeventData data;
04e847
+  GMainContext *main_context;
04e847
+  GSource *idle_source;
04e847
+  GSource *timeout_source;
04e847
+
04e847
+  g_return_val_if_fail (UDISKS_IS_DAEMON (daemon), FALSE);
04e847
+  g_return_val_if_fail (device_path != NULL, FALSE);
04e847
+
04e847
+  if (bd_utils_check_linux_version (4, 13, 0) < 0)
04e847
+    {
04e847
+      udisks_daemon_util_trigger_uevent (daemon, device_path);
04e847
+      return FALSE;
04e847
+    }
04e847
+
04e847
+  provider = udisks_daemon_get_linux_provider (daemon);
04e847
+  gudev_client = udisks_linux_provider_get_udev_client (provider);
04e847
+  gudev_device = g_udev_client_query_by_device_file (gudev_client, device_path);
04e847
+  if (gudev_device == NULL)
04e847
+    {
04e847
+      udisks_critical ("Device %s not found in udev database, skipping uevent trigger", device_path);
04e847
+      return FALSE;
04e847
+    }
04e847
+
04e847
+  data.daemon = daemon;
04e847
+  data.uevent_path = g_build_filename (g_udev_device_get_sysfs_path (gudev_device), "uevent", NULL);
04e847
+  data.serial = g_atomic_int_add (&uevent_serial, 1);
04e847
+
04e847
+  main_context = g_main_context_new ();
04e847
+  g_main_context_push_thread_default (main_context);
04e847
+  data.main_loop = g_main_loop_new (main_context, FALSE);
04e847
+
04e847
+  /* queue the actual trigger in the loop */
04e847
+  idle_source = g_idle_source_new ();
04e847
+  g_source_set_callback (idle_source, (GSourceFunc) trigger_uevent_idle_cb, &data, NULL);
04e847
+  g_source_attach (idle_source, main_context);
04e847
+  g_source_unref (idle_source);
04e847
+
04e847
+  /* add timeout as a fallback */
04e847
+  timeout_source = g_timeout_source_new_seconds (timeout_seconds);
04e847
+  g_source_set_callback (timeout_source, (GSourceFunc) uevent_wait_timeout_cb, &data, NULL);
04e847
+  g_source_attach (timeout_source, main_context);
04e847
+  g_source_unref (timeout_source);
04e847
+
04e847
+  /* catch incoming uevents */
04e847
+  g_signal_connect (provider, "uevent-probed", G_CALLBACK (uevent_probed_cb), &data);
04e847
+
04e847
+  data.success = FALSE;
04e847
+  g_main_loop_run (data.main_loop);
04e847
+
04e847
+  g_signal_handlers_disconnect_by_func (provider, uevent_probed_cb, &data);
04e847
+  g_main_context_pop_thread_default (main_context);
04e847
+
04e847
+  g_main_loop_unref (data.main_loop);
04e847
+  g_main_context_unref (main_context);
04e847
+
04e847
+  g_free (data.uevent_path);
04e847
+  g_object_unref (gudev_device);
04e847
+
04e847
+  return data.success;
04e847
+}
04e847
+
04e847
+/* ---------------------------------------------------------------------------------------------------- */
04e847
diff --git a/src/udisksdaemonutil.h b/src/udisksdaemonutil.h
04e847
index 4fe36214..2edf2122 100644
04e847
--- a/src/udisksdaemonutil.h
04e847
+++ b/src/udisksdaemonutil.h
04e847
@@ -51,6 +51,13 @@ guint64 udisks_daemon_util_block_get_size (GUdevDevice *device,
04e847
                                            gboolean    *out_media_available,
04e847
                                            gboolean    *out_media_change_detected);
04e847
 
04e847
+void udisks_daemon_util_trigger_uevent (UDisksDaemon *daemon,
04e847
+                                        const gchar  *device_path);
04e847
+
04e847
+gboolean udisks_daemon_util_trigger_uevent_sync (UDisksDaemon *daemon,
04e847
+                                                 const gchar  *device_path,
04e847
+                                                 guint         timeout_seconds);
04e847
+
04e847
 gchar *udisks_daemon_util_resolve_link (const gchar *path,
04e847
                                         const gchar *name);
04e847
 
04e847
diff --git a/src/udiskslinuxblockobject.c b/src/udiskslinuxblockobject.c
04e847
index 42ab17d7..5a68c84b 100644
04e847
--- a/src/udiskslinuxblockobject.c
04e847
+++ b/src/udiskslinuxblockobject.c
04e847
@@ -38,8 +38,6 @@
04e847
 #include <stdlib.h>
04e847
 #include <glib/gstdio.h>
04e847
 
04e847
-#include <blockdev/blockdev.h>
04e847
-
04e847
 #include "udiskslogging.h"
04e847
 #include "udisksdaemon.h"
04e847
 #include "udisksdaemonutil.h"
04e847
@@ -959,122 +957,24 @@ on_mount_monitor_mount_removed (UDisksMountMonitor  *monitor,
04e847
 
04e847
 /* ---------------------------------------------------------------------------------------------------- */
04e847
 
04e847
-static volatile guint uevent_serial = 0;
04e847
-
04e847
-static gboolean
04e847
-trigger_uevent (const gchar *path, const gchar *str)
04e847
-{
04e847
-  gint fd;
04e847
-
04e847
-  fd = open (path, O_WRONLY);
04e847
-  if (fd < 0)
04e847
-    {
04e847
-      udisks_warning ("Error opening %s while triggering uevent: %m", path);
04e847
-      return FALSE;
04e847
-    }
04e847
-
04e847
-  if (write (fd, str, strlen (str)) != (ssize_t) strlen (str))
04e847
-    {
04e847
-      udisks_warning ("Error writing '%s' to file %s: %m", str, path);
04e847
-      close (fd);
04e847
-      return FALSE;
04e847
-    }
04e847
-
04e847
-  close (fd);
04e847
-  return TRUE;
04e847
-}
04e847
-
04e847
-typedef struct
04e847
-{
04e847
-  UDisksLinuxBlockObject *object;
04e847
-  GMainLoop *main_loop;
04e847
-  guint serial;
04e847
-  gchar *uevent_path;
04e847
-  gboolean success;
04e847
-} SynthUeventData;
04e847
-
04e847
-static gboolean
04e847
-trigger_uevent_idle_cb (gpointer user_data)
04e847
-{
04e847
-  SynthUeventData *data = user_data;
04e847
-  gchar *str;
04e847
-
04e847
-  str = g_strdup_printf ("change %s UDISKSSERIAL=%u", udisks_daemon_get_uuid (data->object->daemon), data->serial);
04e847
-
04e847
-  if (! trigger_uevent (data->uevent_path, str))
04e847
-    {
04e847
-      /* kernel refused our string, try simple "change" but don't wait for it */
04e847
-      trigger_uevent (data->uevent_path, "change");
04e847
-      data->success = FALSE;
04e847
-      g_main_loop_quit (data->main_loop);
04e847
-    }
04e847
-  g_free (str);
04e847
-
04e847
-  /* remove the source */
04e847
-  return FALSE;
04e847
-}
04e847
-
04e847
-static gboolean
04e847
-uevent_wait_timeout_cb (gpointer user_data)
04e847
-{
04e847
-  SynthUeventData *data = user_data;
04e847
-
04e847
-  data->success = FALSE;
04e847
-  g_main_loop_quit (data->main_loop);
04e847
-
04e847
-  /* remove the source */
04e847
-  return FALSE;
04e847
-}
04e847
-
04e847
-static void
04e847
-uevent_probed_cb (UDisksLinuxProvider *provider,
04e847
-                  const gchar         *action,
04e847
-                  UDisksLinuxDevice   *device,
04e847
-                  gpointer             user_data)
04e847
-{
04e847
-  SynthUeventData *data = user_data;
04e847
-  const gchar *received_serial_str;
04e847
-  gint64 received_serial;
04e847
-  gchar *endptr;
04e847
-
04e847
-  received_serial_str = g_udev_device_get_property (device->udev_device, "SYNTH_ARG_UDISKSSERIAL");
04e847
-  if (received_serial_str != NULL)
04e847
-    {
04e847
-      endptr = (gchar *) received_serial_str;
04e847
-      received_serial = g_ascii_strtoll (received_serial_str, &endptr, 0);
04e847
-      if (endptr != received_serial_str && received_serial == data->serial)
04e847
-        {
04e847
-          data->success = TRUE;
04e847
-          g_main_loop_quit (data->main_loop);
04e847
-        }
04e847
-    }
04e847
-}
04e847
-
04e847
 /**
04e847
  * udisks_linux_block_object_trigger_uevent:
04e847
  * @object: A #UDisksLinuxBlockObject.
04e847
  *
04e847
  * Triggers a 'change' uevent in the kernel.
04e847
  *
04e847
- * The triggered event will bubble up from the kernel through the udev
04e847
- * stack and will eventually be received by the udisks daemon process
04e847
- * itself. This method does not wait for the event to be received.
04e847
+ * Refer to udisks_daemon_util_trigger_uevent() for detailed description.
04e847
  */
04e847
 void
04e847
 udisks_linux_block_object_trigger_uevent (UDisksLinuxBlockObject *object)
04e847
 {
04e847
-  UDisksLinuxDevice *device;
04e847
-  gchar *path;
04e847
+  gchar *device_file;
04e847
 
04e847
   g_return_if_fail (UDISKS_IS_LINUX_BLOCK_OBJECT (object));
04e847
 
04e847
-  device = udisks_linux_block_object_get_device (object);
04e847
-  path = g_strconcat (g_udev_device_get_sysfs_path (device->udev_device), "/uevent", NULL);
04e847
-
04e847
-  trigger_uevent (path, "change");
04e847
-
04e847
-  g_free (path);
04e847
-  g_object_unref (device);
04e847
+  device_file = udisks_linux_block_object_get_device_file (object);
04e847
+  udisks_daemon_util_trigger_uevent (object->daemon, device_file);
04e847
+  g_free (device_file);
04e847
 }
04e847
 
04e847
 /**
04e847
@@ -1083,19 +983,10 @@ udisks_linux_block_object_trigger_uevent (UDisksLinuxBlockObject *object)
04e847
  * @timeout_seconds: Maximum time to wait for the uevent (in seconds).
04e847
  *
04e847
  * Triggers a 'change' uevent in the kernel and waits until it's received and
04e847
- * processed by udisks.
04e847
- *
04e847
- * Unlike udisks_linux_block_object_trigger_uevent() that just triggers
04e847
- * a synthetic uevent to the kernel, this call will actually block and wait until
04e847
- * the #UDisksLinuxProvider receives the uevent, performs probing and processes
04e847
- * the uevent further down the UDisks object stack. Upon returning from this
04e847
- * function call the caller may assume the event has been fully processed, all
04e847
- * D-Bus objects are updated and settled. Typically used in busy wait for
04e847
- * a particular D-Bus interface.
04e847
+ * processed through the uevent queue.
04e847
  *
04e847
- * Note that this uses synthetic uevent tagging and only works on linux kernel
04e847
- * 4.13 and higher. In case an older kernel is detected this acts like the classic
04e847
- * udisks_linux_block_object_trigger_uevent() call and %FALSE is returned.
04e847
+ * This is a convenient wrapper around udisks_daemon_util_trigger_uevent_sync().
04e847
+ * Refer to this function for detailed documentation.
04e847
  *
04e847
  * Returns: %TRUE if the uevent has been successfully received, %FALSE otherwise
04e847
  * or when the kernel version is too old.
04e847
@@ -1104,59 +995,16 @@ gboolean
04e847
 udisks_linux_block_object_trigger_uevent_sync (UDisksLinuxBlockObject *object,
04e847
                                                guint                   timeout_seconds)
04e847
 {
04e847
-  UDisksLinuxDevice *device;
04e847
-  UDisksLinuxProvider *provider;
04e847
-  SynthUeventData data;
04e847
-  GMainContext *main_context;
04e847
-  GSource *idle_source;
04e847
-  GSource *timeout_source;
04e847
+  gchar *device_file;
04e847
+  gboolean ret;
04e847
 
04e847
   g_return_val_if_fail (UDISKS_IS_LINUX_BLOCK_OBJECT (object), FALSE);
04e847
 
04e847
-  if (bd_utils_check_linux_version (4, 13, 0) < 0)
04e847
-    {
04e847
-      udisks_linux_block_object_trigger_uevent (object);
04e847
-      return FALSE;
04e847
-    }
04e847
-
04e847
-  data.object = object;
04e847
-  device = udisks_linux_block_object_get_device (object);
04e847
-  data.uevent_path = g_strconcat (g_udev_device_get_sysfs_path (device->udev_device), "/uevent", NULL);
04e847
-  data.serial = g_atomic_int_add (&uevent_serial, 1);
04e847
-
04e847
-  main_context = g_main_context_new ();
04e847
-  g_main_context_push_thread_default (main_context);
04e847
-  data.main_loop = g_main_loop_new (main_context, FALSE);
04e847
-
04e847
-  /* queue the actual trigger in the loop */
04e847
-  idle_source = g_idle_source_new ();
04e847
-  g_source_set_callback (idle_source, (GSourceFunc) trigger_uevent_idle_cb, &data, NULL);
04e847
-  g_source_attach (idle_source, main_context);
04e847
-  g_source_unref (idle_source);
04e847
+  device_file = udisks_linux_block_object_get_device_file (object);
04e847
+  ret = udisks_daemon_util_trigger_uevent_sync (object->daemon, device_file, timeout_seconds);
04e847
+  g_free (device_file);
04e847
 
04e847
-  /* add timeout as a fallback */
04e847
-  timeout_source = g_timeout_source_new_seconds (timeout_seconds);
04e847
-  g_source_set_callback (timeout_source, (GSourceFunc) uevent_wait_timeout_cb, &data, NULL);
04e847
-  g_source_attach (timeout_source, main_context);
04e847
-  g_source_unref (timeout_source);
04e847
-
04e847
-  /* catch incoming uevents */
04e847
-  provider = udisks_daemon_get_linux_provider (object->daemon);
04e847
-  g_signal_connect (provider, "uevent-probed", G_CALLBACK (uevent_probed_cb), &data);
04e847
-
04e847
-  data.success = FALSE;
04e847
-  g_main_loop_run (data.main_loop);
04e847
-
04e847
-  g_signal_handlers_disconnect_by_func (provider, uevent_probed_cb, &data);
04e847
-  g_main_context_pop_thread_default (main_context);
04e847
-
04e847
-  g_main_loop_unref (data.main_loop);
04e847
-  g_main_context_unref (main_context);
04e847
-
04e847
-  g_free (data.uevent_path);
04e847
-  g_object_unref (device);
04e847
-
04e847
-  return data.success;
04e847
+  return ret;
04e847
 }
04e847
 
04e847
 /* ---------------------------------------------------------------------------------------------------- */
04e847
-- 
04e847
2.26.2
04e847