Skip to contentSkip to conversation

uinput: Support injecting multiple events in one write() call

PATCH In review Ryan Mallon 2 participants 6 messages Sep 10, 2013 – Sep 11, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 uinput: Support injecting multiple events in one write() call

Rework the code in uinput_inject_event so that it matches the code in evdev_write and allows injecting more than one event, or zero events.

---
 drivers/input/misc/uinput.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/input/misc/uinput.c +9 -5
index a0a4bba..6aea346 100644
@@ -434,16 +434,20 @@ static ssize_t uinput_inject_event(struct uinput_device *udev,
 				   const char __user *buffer, size_t count)
 {
 	struct input_event ev;
+	size_t bytes = 0;
 
-	if (count < input_event_size())
+	if (count != 0 && count < input_event_size())
 		return -EINVAL;
 
-	if (input_event_from_user(buffer, &ev))
-		return -EFAULT;
+	while (bytes + input_event_size() <= count) {
+		if (input_event_from_user(buffer + bytes, &ev))
+			return -EFAULT;
 
-	input_event(udev->dev, ev.type, ev.code, ev.value);
+		input_event(udev->dev, ev.type, ev.code, ev.value);
+		bytes += input_event_size();
+	}
 
-	return input_event_size();
+	return bytes;
 }
 
 static ssize_t uinput_write(struct file *file, const char __user *buffer,
Signature · 1 line
1.7.9.7
#1 lore raw

PATCH 2/2 uinput: Use input_inject_event in uinput_inject_event

Call input_inject_event rather than input_event in uinput_inject_event. This mirrors the behaviour of evdev_write. input_inject_event will ignore the injected event if the uinput input device has been grabbed for exclusive access by a handler other than uinput.

---
 drivers/input/misc/uinput.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/uinput.c +1 -1
index 6aea346..0962ecb 100644
@@ -443,7 +443,7 @@ static ssize_t uinput_inject_event(struct uinput_device *udev,
 		if (input_event_from_user(buffer + bytes, &ev))
 			return -EFAULT;
 
-		input_event(udev->dev, ev.type, ev.code, ev.value);
+		input_inject_event(udev->dev, ev.type, ev.code, ev.value);
 		bytes += input_event_size();
 	}
 
Signature · 1 line
1.7.9.7
maintainer
#2 lore raw

Re: [PATCH 2/2] uinput: Use input_inject_event in uinput_inject_event

Hi Ryan,

Quoted 4 lines · Ryan Mallon

Call input_inject_event rather than input_event in uinput_inject_event. This mirrors the behaviour of evdev_write. input_inject_event will ignore the injected event if the uinput input device has been grabbed for exclusive access by a handler other than uinput.

uinput is not an input handler but input driver and therefore should continue using input_event().

Thanks.

Signature · 1 line
Dmitry
#3 lore raw

Re: [PATCH 2/2] uinput: Use input_inject_event in uinput_inject_event

Quoted 10 lines · Dmitry Torokhov

Hi Ryan,

Quoted 4 lines · Ryan Mallon

Call input_inject_event rather than input_event in uinput_inject_event. This mirrors the behaviour of evdev_write. input_inject_event will ignore the injected event if the uinput input device has been grabbed for exclusive access by a handler other than uinput.

uinput is not an input handler but input driver and therefore should continue using input_event().

Ah, I wasn't sure about this, which is why I broke it out into a separate patch. Sorry for the noise.

~Ryan

maintainer
#4 lore raw
In reply to Ryan Mallon

Hi Ryan,

Quoted 17 lines · Ryan Mallon

Rework the code in uinput_inject_event so that it matches the code in evdev_write and allows injecting more than one event, or zero events.

---
 drivers/input/misc/uinput.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/input/misc/uinput.c +1 -0
index a0a4bba..6aea346 100644
@@ -434,16 +434,20 @@ static ssize_t uinput_inject_event(struct uinput_device *udev,
 				   const char __user *buffer, size_t count)
 {
 	struct input_event ev;
+	size_t bytes = 0;

drivers/input/misc/uinput.c static ssize_t uinput_inject_event(struct uinput_device *udev, · line 444

Quoted 10 lines
drivers/input/misc/uinput.c +4 -3
 
-	if (count < input_event_size())
+	if (count != 0 && count < input_event_size())
 		return -EINVAL;
 
-	if (input_event_from_user(buffer, &ev))
-		return -EFAULT;
+	while (bytes + input_event_size() <= count) {
+		if (input_event_from_user(buffer + bytes, &ev))
+			return -EFAULT;

Unless the failure is in the very first packet we should tell the user how many bytes we have transferred successfully instead of returning -EFAULT.

Thanks.

Signature · 1 line
Dmitry
#5 lore raw
Quoted 25 lines · Dmitry Torokhov

Hi Ryan,

Quoted 22 lines · Ryan Mallon

Rework the code in uinput_inject_event so that it matches the code in evdev_write and allows injecting more than one event, or zero events.

---
 drivers/input/misc/uinput.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/input/misc/uinput.c +2 -1
index a0a4bba..6aea346 100644
@@ -434,16 +434,20 @@ static ssize_t uinput_inject_event(struct uinput_device *udev,
 				   const char __user *buffer, size_t count)
 {
 	struct input_event ev;
+	size_t bytes = 0;
 
-	if (count < input_event_size())
+	if (count != 0 && count < input_event_size())
 		return -EINVAL;
 
Quoted 8 lines

drivers/input/misc/uinput.c static ssize_t uinput_inject_event(struct uinput_device *udev, · line 444

Quoted 5 lines
drivers/input/misc/uinput.c +3 -2
-	if (input_event_from_user(buffer, &ev))
-		return -EFAULT;
+	while (bytes + input_event_size() <= count) {
+		if (input_event_from_user(buffer + bytes, &ev))
+			return -EFAULT;

Unless the failure is in the very first packet we should tell the user how many bytes we have transferred successfully instead of returning -EFAULT.

Good point. evdev is also broken in this regard, I'll send a follow-up patch for that once you are happy with this one. Updated patch is below:

~Ryan

---

drivers/input/misc/uinput.c +9 -5
index a0a4bba..92ca2c5 100644
@@ -434,16 +434,20 @@ static ssize_t uinput_inject_event(struct uinput_device *u
                                   const char __user *buffer, size_t count)
 {
        struct input_event ev;
+       size_t bytes = 0;
 
-       if (count < input_event_size())
+       if (count != 0 && count < input_event_size())
                return -EINVAL;
 
-       if (input_event_from_user(buffer, &ev))
-               return -EFAULT;
+       while (bytes + input_event_size() <= count) {
+               if (input_event_from_user(buffer + bytes, &ev))
+                       return bytes ?: -EFAULT;
 
-       input_event(udev->dev, ev.type, ev.code, ev.value);
+               input_event(udev->dev, ev.type, ev.code, ev.value);
+               bytes += input_event_size();
+       }
 
-       return input_event_size();
+       return bytes;
 }
 
 static ssize_t uinput_write(struct file *file, const char __user *buffer,