]> xenbits.xensource.com Git - libvirt.git/commitdiff
node_device_udev: Pass the `udevEventData` via parameter and use refcounting
authorMarc Hartmayer <mhartmay@linux.ibm.com>
Tue, 23 Apr 2024 18:09:05 +0000 (20:09 +0200)
committerJonathon Jongsma <jjongsma@redhat.com>
Tue, 18 Jun 2024 14:00:36 +0000 (09:00 -0500)
Instead of accessing the global `driver` object pass the `udevEventData` as
parameter to the thread handler and watch callback. This has the advantage that:
1. proper refcounting
2. easier to read and test

Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
src/node_device/node_device_udev.c

index 766dcaffd464da5be559c50d1bad22911a6e5b8e..370b272df6f9f1d386d8df3e478da2b4e1cc932c 100644 (file)
@@ -1863,7 +1863,7 @@ udevEventMonitorSanityCheck(udevEventData *priv,
 
 /**
  * udevEventHandleThread
- * @opaque: unused
+ * @opaque: udevEventData
  *
  * Thread to handle the udevEventHandleCallback processing when udev
  * tells us there's a device change for us (add, modify, delete, etc).
@@ -1882,9 +1882,9 @@ udevEventMonitorSanityCheck(udevEventData *priv,
  * would still come into play.
  */
 static void
-udevEventHandleThread(void *opaque G_GNUC_UNUSED)
+udevEventHandleThread(void *opaque)
 {
-    udevEventData *priv = driver->privateData;
+    g_autoptr(udevEventData) priv = opaque;
     struct udev_device *device = NULL;
 
     /* continue rather than break from the loop on non-fatal errors */
@@ -1950,9 +1950,9 @@ static void
 udevEventHandleCallback(int watch G_GNUC_UNUSED,
                         int fd,
                         int events G_GNUC_UNUSED,
-                        void *data G_GNUC_UNUSED)
+                        void *data)
 {
-    udevEventData *priv = driver->privateData;
+    udevEventData *priv = data;
     VIR_LOCK_GUARD lock = virObjectLockGuard(priv);
 
     if (!udevEventMonitorSanityCheck(priv, fd))
@@ -2489,7 +2489,7 @@ nodeStateInitialize(bool privileged,
 
     priv->udevThread = g_new0(virThread, 1);
     if (virThreadCreateFull(priv->udevThread, true, udevEventHandleThread,
-                            "udev-event", false, NULL) < 0) {
+                            "udev-event", false, virObjectRef(priv)) < 0) {
         virReportSystemError(errno, "%s",
                              _("failed to create udev handler thread"));
         goto unlock;
@@ -2505,7 +2505,7 @@ nodeStateInitialize(bool privileged,
      * that appear while the enumeration is taking place.  */
     priv->watch = virEventAddHandle(udev_monitor_get_fd(priv->udev_monitor),
                                     VIR_EVENT_HANDLE_READABLE,
-                                    udevEventHandleCallback, NULL, NULL);
+                                    udevEventHandleCallback, virObjectRef(priv), virObjectUnref);
     if (priv->watch == -1)
         goto unlock;