ecbff1
From 4f9b03c28555799f8672b905323bf6d1f95eb13f Mon Sep 17 00:00:00 2001
ecbff1
From: Peter Hutterer <peter.hutterer@who-t.net>
ecbff1
Date: Fri, 20 Mar 2015 12:52:46 +1000
ecbff1
Subject: [PATCH] udev: builtin-keyboard: immediately EVIOCSKEYCODE when we
ecbff1
 have a pair
ecbff1
ecbff1
Rather than building a map and looping through the map, immediately call the
ecbff1
ioctl when we have a successfully parsed property.
ecbff1
ecbff1
This has a side-effect: before the maximum number of ioctls was limited to the
ecbff1
size of the map (1024), now it is unlimited.
ecbff1
ecbff1
(cherry picked from commit cfba2656e3b4a9c5e03db4ec0a8f76c3762d35a8)
ecbff1
ecbff1
Resolves: #1500119
ecbff1
---
ecbff1
 src/udev/udev-builtin-keyboard.c | 45 ++++++++++++++++------------------------
ecbff1
 1 file changed, 18 insertions(+), 27 deletions(-)
ecbff1
ecbff1
diff --git a/src/udev/udev-builtin-keyboard.c b/src/udev/udev-builtin-keyboard.c
ecbff1
index bde7bf07f..515edd45c 100644
ecbff1
--- a/src/udev/udev-builtin-keyboard.c
ecbff1
+++ b/src/udev/udev-builtin-keyboard.c
ecbff1
@@ -71,10 +71,10 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
ecbff1
         struct {
ecbff1
                 unsigned scan;
ecbff1
                 unsigned key;
ecbff1
-        } map[1024];
ecbff1
-        unsigned map_count = 0;
ecbff1
+        } map;
ecbff1
         unsigned release[1024];
ecbff1
         unsigned release_count = 0;
ecbff1
+        _cleanup_close_ int fd = -1;
ecbff1
         const char *node;
ecbff1
 
ecbff1
         node = udev_device_get_devnode(dev);
ecbff1
@@ -128,37 +128,28 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
ecbff1
                         }
ecbff1
                 }
ecbff1
 
ecbff1
-                map[map_count].scan = scancode;
ecbff1
-                map[map_count].key = keycode_num;
ecbff1
-                if (map_count < ELEMENTSOF(map)-1)
ecbff1
-                        map_count++;
ecbff1
-        }
ecbff1
-
ecbff1
-        if (map_count > 0 || release_count > 0) {
ecbff1
-                int fd;
ecbff1
-                unsigned i;
ecbff1
-
ecbff1
-                fd = open(node, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
ecbff1
-                if (fd < 0) {
ecbff1
-                        log_error_errno(errno, "Error, opening device '%s': %m", node);
ecbff1
-                        return EXIT_FAILURE;
ecbff1
+                if (fd == -1) {
ecbff1
+                        fd = open(node, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
ecbff1
+                        if (fd < 0) {
ecbff1
+                                log_error_errno(errno, "Error, opening device '%s': %m", node);
ecbff1
+                                return EXIT_FAILURE;
ecbff1
+                        }
ecbff1
                 }
ecbff1
 
ecbff1
-                /* install list of map codes */
ecbff1
-                for (i = 0; i < map_count; i++) {
ecbff1
-                        log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
ecbff1
-                                  map[i].scan, map[i].scan, map[i].key, map[i].key);
ecbff1
-                        if (ioctl(fd, EVIOCSKEYCODE, &map[i]) < 0)
ecbff1
-                                log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", node, map[i].scan, map[i].key);
ecbff1
-                }
ecbff1
+                map.scan = scancode;
ecbff1
+                map.key = keycode_num;
ecbff1
 
ecbff1
-                /* install list of force-release codes */
ecbff1
-                if (release_count > 0)
ecbff1
-                        install_force_release(dev, release, release_count);
ecbff1
+                log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
ecbff1
+                          map.scan, map.scan, map.key, map.key);
ecbff1
 
ecbff1
-                close(fd);
ecbff1
+                if (ioctl(fd, EVIOCSKEYCODE, &map) < 0)
ecbff1
+                        log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", node, map.scan, map.key);
ecbff1
         }
ecbff1
 
ecbff1
+        /* install list of force-release codes */
ecbff1
+        if (release_count > 0)
ecbff1
+                install_force_release(dev, release, release_count);
ecbff1
+
ecbff1
         return EXIT_SUCCESS;
ecbff1
 }
ecbff1