mirror of
https://github.com/Fishwaldo/Star64_linux.git
synced 2025-07-05 05:42:36 +00:00
Input: evdev - properly handle read/write with count 0
According to the standard count 0 is special - no IO should happen but we can check error conditions (device gone away, etc), and return 0 if there are no errors. We used to return -EINVAL instead and we also could return 0 if an event was "stolen" by another thread. Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
This commit is contained in:
parent
dba4258068
commit
2872a9b521
1 changed files with 42 additions and 27 deletions
|
@ -345,7 +345,7 @@ static ssize_t evdev_write(struct file *file, const char __user *buffer,
|
||||||
struct input_event event;
|
struct input_event event;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
if (count < input_event_size())
|
if (count != 0 && count < input_event_size())
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
retval = mutex_lock_interruptible(&evdev->mutex);
|
retval = mutex_lock_interruptible(&evdev->mutex);
|
||||||
|
@ -357,7 +357,8 @@ static ssize_t evdev_write(struct file *file, const char __user *buffer,
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
while (retval + input_event_size() <= count) {
|
||||||
|
|
||||||
if (input_event_from_user(buffer + retval, &event)) {
|
if (input_event_from_user(buffer + retval, &event)) {
|
||||||
retval = -EFAULT;
|
retval = -EFAULT;
|
||||||
goto out;
|
goto out;
|
||||||
|
@ -366,7 +367,7 @@ static ssize_t evdev_write(struct file *file, const char __user *buffer,
|
||||||
|
|
||||||
input_inject_event(&evdev->handle,
|
input_inject_event(&evdev->handle,
|
||||||
event.type, event.code, event.value);
|
event.type, event.code, event.value);
|
||||||
} while (retval + input_event_size() <= count);
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
mutex_unlock(&evdev->mutex);
|
mutex_unlock(&evdev->mutex);
|
||||||
|
@ -397,35 +398,49 @@ static ssize_t evdev_read(struct file *file, char __user *buffer,
|
||||||
struct evdev_client *client = file->private_data;
|
struct evdev_client *client = file->private_data;
|
||||||
struct evdev *evdev = client->evdev;
|
struct evdev *evdev = client->evdev;
|
||||||
struct input_event event;
|
struct input_event event;
|
||||||
int retval = 0;
|
size_t read = 0;
|
||||||
|
int error;
|
||||||
|
|
||||||
if (count < input_event_size())
|
if (count != 0 && count < input_event_size())
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (!(file->f_flags & O_NONBLOCK)) {
|
for (;;) {
|
||||||
retval = wait_event_interruptible(evdev->wait,
|
if (!evdev->exist)
|
||||||
client->packet_head != client->tail ||
|
return -ENODEV;
|
||||||
!evdev->exist);
|
|
||||||
if (retval)
|
if (client->packet_head == client->tail &&
|
||||||
return retval;
|
(file->f_flags & O_NONBLOCK))
|
||||||
|
return -EAGAIN;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* count == 0 is special - no IO is done but we check
|
||||||
|
* for error conditions (see above).
|
||||||
|
*/
|
||||||
|
if (count == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
while (read + input_event_size() <= count &&
|
||||||
|
evdev_fetch_next_event(client, &event)) {
|
||||||
|
|
||||||
|
if (input_event_to_user(buffer + read, &event))
|
||||||
|
return -EFAULT;
|
||||||
|
|
||||||
|
read += input_event_size();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (read)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (!(file->f_flags & O_NONBLOCK)) {
|
||||||
|
error = wait_event_interruptible(evdev->wait,
|
||||||
|
client->packet_head != client->tail ||
|
||||||
|
!evdev->exist);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!evdev->exist)
|
return read;
|
||||||
return -ENODEV;
|
|
||||||
|
|
||||||
while (retval + input_event_size() <= count &&
|
|
||||||
evdev_fetch_next_event(client, &event)) {
|
|
||||||
|
|
||||||
if (input_event_to_user(buffer + retval, &event))
|
|
||||||
return -EFAULT;
|
|
||||||
|
|
||||||
retval += input_event_size();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (retval == 0 && (file->f_flags & O_NONBLOCK))
|
|
||||||
return -EAGAIN;
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* No kernel lock - fine */
|
/* No kernel lock - fine */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue