path: make sure udev devices are initialized before usage
authorPeter Hutterer <peter.hutterer@who-t.net>
Tue, 3 Feb 2015 04:18:15 +0000 (14:18 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 3 Feb 2015 22:14:50 +0000 (08:14 +1000)
When creating uinput devices, we get the devnode from the kernel directly
rather than through udev. When we add this to the path backend too quickly the
udev_device we get may not be fully initialized and properties may be missing.
This causes false test results.

Avoid this by making sure the handle we have is initialized. This should never
trigger on a real device anyway, even creating a device through litest is slow
enough to avoid this issue. Only affected are the tests in misc.c where we
create the uinput device directly.

Nonetheless, handle this for the generic case so we don't run into heisenbugs
later.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
src/path.c

index dd70efc645c60e74d00578502cd0355ede58a243..832a1fd8c20077a530ed178fab2f28791306b8fb 100644 (file)
@@ -291,14 +291,34 @@ libinput_path_create_context(const struct libinput_interface *interface,
 }
 
 static inline struct udev_device *
-udev_device_from_devnode(struct udev *udev, const char *devnode)
+udev_device_from_devnode(struct libinput *libinput,
+                        struct udev *udev,
+                        const char *devnode)
 {
+       struct udev_device *dev;
        struct stat st;
+       size_t count = 0;
 
        if (stat(devnode, &st) < 0)
                return NULL;
 
-       return udev_device_new_from_devnum(udev, 'c', st.st_rdev);
+       dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
+
+       while (dev && !udev_device_get_is_initialized(dev)) {
+               udev_device_unref(dev);
+               msleep(10);
+               dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
+
+               count++;
+               if (count > 10) {
+                       log_bug_libinput(libinput,
+                                       "udev device never initialized (%s)\n",
+                                       devnode);
+                       break;
+               }
+       }
+
+       return dev;
 }
 
 LIBINPUT_EXPORT struct libinput_device *
@@ -315,7 +335,7 @@ libinput_path_add_device(struct libinput *libinput,
                return NULL;
        }
 
-       udev_device = udev_device_from_devnode(udev, path);
+       udev_device = udev_device_from_devnode(libinput, udev, path);
        if (!udev_device) {
                log_bug_client(libinput, "Invalid path %s\n", path);
                return NULL;