# Problem reading /dev/input in Linux

**URL:** <https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585>\
**Category:** ruby-talk\
**Created:** [11 October 2013 11:25 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585 "2013-10-11T11:25:31Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [11 October 2013 11:25 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/1 "2013-10-11T11:25:31Z")

</div>

Hi,

I'm trying to use a simple script in Linux:

File.open('/dev/input/event4', 'r') do |f|  
&nbsp;&nbsp;p f.read(16)  
end

But I'm getting this:

test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
from test.rb:4:in `block in \<main\>'  
from test.rb:3:in `open'  
from test.rb:3:in `\<main\>'

Doing exactly the same in C works just fine, plus I have rw  
permissions to everyone on that file.

What is going on? Anybody else can reproduce?

> **···**
>
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 October 2013 13:51 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/2 "2013-10-11T13:51:32Z")

</div>

Felipe Contreras wrote in post #1124259:

> Hi,
> 
> I'm trying to use a simple script in Linux:
> 
> File.open('/dev/input/event4', 'r') do |f|  
> &nbsp;&nbsp;p f.read(16)  
> end
> 
> But I'm getting this:
> 
> test.rb:4:in `read': Invalid argument - /dev/input/event4  
> (Errno::EINVAL)  
> from test.rb:4:in `block in \<main\>'  
> from test.rb:3:in `open'  
> from test.rb:3:in `\<main\>'
> 
> Doing exactly the same in C works just fine, plus I have rw  
> permissions to everyone on that file.
> 
> What is going on? Anybody else can reproduce?

Just a wild guess. Is /dev/input/event4 a File ? Did you try IO.open ?  
\_md

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Eric\_Wong2](https://avatars.discourse-cdn.com/v4/letter/e/b9e5f3/32.png) [@Eric\_Wong2](https://rubytalk.org/u/Eric_Wong2)\
**Post date:** [11 October 2013 22:28 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/3 "2013-10-11T22:28:52Z")

</div>

strace both versions and compare the differences between the relevant  
syscalls

> **···**
>
> Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I'm trying to use a simple script in Linux:
> > 
> > File.open('/dev/input/event4', 'r') do |f|  
> > &nbsp;&nbsp;p f.read(16)  
> > end
> > 
> > But I'm getting this:
> > 
> > test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > from test.rb:4:in `block in \<main\>'  
> > from test.rb:3:in `open'  
> > from test.rb:3:in `\<main\>'
> > 
> > Doing exactly the same in C works just fine, plus I have rw  
> > permissions to everyone on that file.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 October 2013 01:12 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/4 "2013-10-12T01:12:55Z")

</div>

Felipe Contreras wrote in post #1124259:

> File.open('/dev/input/event4', 'r') do |f|  
> &nbsp;&nbsp;p f.read(16)  
> end
> 
> test.rb:4:in `read': Invalid argument - /dev/input/event4  
> (Errno::EINVAL)  
> from test.rb:4:in `block in \<main\>'  
> from test.rb:3:in `open'  
> from test.rb:3:in `\<main\>'

I'm assuming the f.read is line 4; which version of ruby are you using?  
I've had a cursory look through the source on github (trunk/2.1 and  
1.9.3), and can't see where that error would be raised in that fashion.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [12 October 2013 18:07 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/5 "2013-10-12T18:07:21Z")

</div>

From another data point - I cannot reproduce this on linux kernel 2.6.32-5-686, ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]

> **···**
>
> On Oct 11, 2013, at 6:25 AM, Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I'm trying to use a simple script in Linux:
> > 
> > File.open('/dev/input/event4', 'r') do |f|  
> > p f.read(16)  
> > end
> > 
> > But I'm getting this:
> > 
> > test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > from test.rb:4:in `block in \<main\>'  
> > from test.rb:3:in `open'  
> > from test.rb:3:in `\<main\>'
> > 
> > Doing exactly the same in C works just fine, plus I have rw  
> > permissions to everyone on that file.
> > 
> > What is going on? Anybody else can reproduce?
> > 
> > --  
> > Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [11 October 2013 21:31 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/6 "2013-10-11T21:31:51Z")

</div>

I don't know what you mean, '/dev/input/event4' is a string, IO.open  
takes a file descriptor.

> **···**
>
> On Fri, Oct 11, 2013 at 8:51 AM, Michel Demazure \<lists@ruby-forum.com\> wrote:
> 
> > Felipe Contreras wrote in post #1124259:
> > 
> > > Hi,
> > > 
> > > I'm trying to use a simple script in Linux:
> > > 
> > > File.open('/dev/input/event4', 'r') do |f|  
> > > &nbsp;&nbsp;p f.read(16)  
> > > end
> > > 
> > > But I'm getting this:
> > > 
> > > test.rb:4:in `read': Invalid argument - /dev/input/event4  
> > > (Errno::EINVAL)  
> > > from test.rb:4:in `block in \<main\>'  
> > > from test.rb:3:in `open'  
> > > from test.rb:3:in `\<main\>'
> > > 
> > > Doing exactly the same in C works just fine, plus I have rw  
> > > permissions to everyone on that file.
> > > 
> > > What is going on? Anybody else can reproduce?
> > 
> > Just a wild guess. Is /dev/input/event4 a File ? Did you try IO.open ?
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [12 October 2013 07:45 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/7 "2013-10-12T07:45:58Z")

</div>

Well:

open("/dev/input/event4", O\_RDONLY|O\_CLOEXEC) = 7  
fcntl(7, F\_GETFD) = 0x1 (flags FD\_CLOEXEC)  
fstat(7, {st\_mode=S\_IFCHR|0640, st\_rdev=makedev(13, 68), ...}) = 0  
ioctl(7, SNDCTL\_TMR\_TIMEBASE or SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or  
TCGETS, 0x7fff295c31b0) = -1 EINVAL (Invalid argument)  
ppoll([{fd=7, events=POLLIN}], 1, NULL, NULL, 8) = 1 ([{fd=7, revents=POLLIN}])  
read(7, 0x1bc5ae8, 16) = -1 EINVAL (Invalid argument)  
close(7) = 0

My C version doesn't do that SNDCTL\_TMR\_TIMEBASE or  
SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or TCGETS ioctl.

> **···**
>
> On Fri, Oct 11, 2013 at 5:28 PM, Eric Wong \<normalperson@yhbt.net\> wrote:
> 
> > Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> > 
> > > Hi,
> > > 
> > > I'm trying to use a simple script in Linux:
> > > 
> > > File.open('/dev/input/event4', 'r') do |f|  
> > > &nbsp;&nbsp;p f.read(16)  
> > > end
> > > 
> > > But I'm getting this:
> > > 
> > > test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > > from test.rb:4:in `block in \<main\>'  
> > > from test.rb:3:in `open'  
> > > from test.rb:3:in `\<main\>'
> > > 
> > > Doing exactly the same in C works just fine, plus I have rw  
> > > permissions to everyone on that file.
> > 
> > strace both versions and compare the differences between the relevant  
> > syscalls
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [12 October 2013 07:47 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/8 "2013-10-12T07:47:12Z")

</div>

I tried both 2.0.0p247 and 1.9.3. I suppose -EINVAL comes from the kernel.

> **···**
>
> On Fri, Oct 11, 2013 at 8:12 PM, Matthew Kerwin \<lists@ruby-forum.com\> wrote:
> 
> > Felipe Contreras wrote in post #1124259:
> > 
> > > File.open('/dev/input/event4', 'r') do |f|  
> > > &nbsp;&nbsp;p f.read(16)  
> > > end
> > > 
> > > test.rb:4:in `read': Invalid argument - /dev/input/event4  
> > > (Errno::EINVAL)  
> > > from test.rb:4:in `block in \<main\>'  
> > > from test.rb:3:in `open'  
> > > from test.rb:3:in `\<main\>'
> > 
> > I'm assuming the f.read is line 4; which version of ruby are you using?  
> > I've had a cursory look through the source on github (trunk/2.1 and  
> > 1.9.3), and can't see where that error would be raised in that fashion.
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Jonathan\_Hudson1](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@Jonathan\_Hudson1](https://rubytalk.org/u/Jonathan_Hudson1)\
**Post date:** [12 October 2013 11:52 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/9 "2013-10-12T11:52:31Z")

</div>

You might try replacing read with sysread.

-jh

> **···**
>
> On Sat, 12 Oct 2013 02:47:12 -0500, Felipe Contreras wrote:
> 
> > On Fri, Oct 11, 2013 at 8:12 PM, Matthew Kerwin [lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org](mailto:lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org) wrote:
> > 
> > > Felipe Contreras wrote in post #1124259:
> > > 
> > > > File.open(‘/dev/input/event4’, ‘r’) do |f|  
> > > > p f.read(16)  
> > > > end
> > > > 
> > > > test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL) from test.rb:4:in `block in ’  
> > > > from test.rb:3:in `open' from test.rb:3:in `’
> > > 
> > > I’m assuming the f.read is line 4; which version of ruby are you using?  
> > > I’ve had a cursory look through the source on github (trunk/2.1 and  
> > > 1.9.3), and can’t see where that error would be raised in that fashion.
> > 
> > I tried both 2.0.0p247 and 1.9.3. I suppose -EINVAL comes from the kernel.
> > 
> > –  
> > Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [12 October 2013 22:45 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/10 "2013-10-12T22:45:52Z")

</div>

It's probably related to the version of the Linux kernel: 3.11.4+.

> **···**
>
> On Sat, Oct 12, 2013 at 1:07 PM, Tamara Temple \<tamouse.lists@gmail.com\> wrote:
> 
> > On Oct 11, 2013, at 6:25 AM, Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> > 
> > > Hi,
> > > 
> > > I'm trying to use a simple script in Linux:
> > > 
> > > File.open('/dev/input/event4', 'r') do |f|  
> > > p f.read(16)  
> > > end
> > > 
> > > But I'm getting this:
> > > 
> > > test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > > from test.rb:4:in `block in \<main\>'  
> > > from test.rb:3:in `open'  
> > > from test.rb:3:in `\<main\>'
> > > 
> > > Doing exactly the same in C works just fine, plus I have rw  
> > > permissions to everyone on that file.
> > > 
> > > What is going on? Anybody else can reproduce?
> > > 
> > > --  
> > > Felipe Contreras
> > 
> > From another data point - I cannot reproduce this on linux kernel 2.6.32-5-686, ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [12 October 2013 15:12 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/11 "2013-10-12T15:12:08Z")

</div>

Jonathan Hudson wrote:

> **···**
>
> > On Sat, 12 Oct 2013 02:47:12 -0500, Felipe Contreras wrote:
> > 
> > \> On Fri, Oct 11, 2013 at 8:12 PM, Matthew Kerwin \<lists@ruby-forum.com\> wrote:  
> > \> \> Felipe Contreras wrote in post #1124259:  
> > \> \>\>  
> > \> \>\> File.open('/dev/input/event4', 'r') do |f|  
> > \> \>\> p f.read(16)  
> > \> \>\> end  
> > \> \>\>  
> > \> \>\>  
> > \> \>\> test.rb:4:in `read': Invalid argument - /dev/input/event4  
> > \> \>\> (Errno::EINVAL)  
> > \> \>\> from test.rb:4:in `block in \<main\>'  
> > \> \>\> from test.rb:3:in `open'  
> > \> \>\> from test.rb:3:in `\<main\>'  
> > \> \>  
> > \> \> I'm assuming the f.read is line 4; which version of ruby are you using?  
> > \> \> I've had a cursory look through the source on github (trunk/2.1 and  
> > \> \> 1.9.3), and can't see where that error would be raised in that fashion.  
> > \>  
> > \> I tried both 2.0.0p247 and 1.9.3. I suppose -EINVAL comes from the kernel.
> > 
> > You might try replacing read with sysread.
> 
> I did. Doesn't make a difference.
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 October 2013 14:12 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/12 "2013-10-12T14:12:20Z")

</div>

Felipe Contreras wrote in post #1124361:

> open("/dev/input/event4", O\_RDONLY|O\_CLOEXEC) = 7  
> fcntl(7, F\_GETFD) = 0x1 (flags FD\_CLOEXEC)  
> fstat(7, {st\_mode=S\_IFCHR|0640, st\_rdev=makedev(13, 68), ...}) = 0  
> ioctl(7, SNDCTL\_TMR\_TIMEBASE or SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or  
> TCGETS, 0x7fff295c31b0) = -1 EINVAL (Invalid argument)  
> ppoll([{fd=7, events=POLLIN}], 1, NULL, NULL, 8) = 1 ([{fd=7,  
> revents=POLLIN}])  
> read(7, 0x1bc5ae8, 16) = -1 EINVAL (Invalid argument)  
> close(7) = 0
> 
> My C version doesn't do that SNDCTL\_TMR\_TIMEBASE or  
> SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or TCGETS ioctl.

SNDCTL\_TMR\_TIMEBASE or SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or TCGETS all have  
the same value; strace just didn't know which name to give it (TCGETS).  
I guess that's just ruby poking the new fd to see if it's a file or a  
tty. (Perl does this, I assume ruby might too.)

It's odd that the failed ioctl() apparently sets errno to EINVAL instead  
of ENOTTY; but in any case, ruby apparently doesn't care, because it  
goes on to call ppoll() and read() anyway.

And fails.

My unsupported hunch tells me that ioctl()'s EINVAL is related to  
read()'s, but I couldn't tell you why. Sorry.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Eric\_Wong2](https://avatars.discourse-cdn.com/v4/letter/e/b9e5f3/32.png) [@Eric\_Wong2](https://rubytalk.org/u/Eric_Wong2)\
**Post date:** [12 October 2013 21:08 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/13 "2013-10-12T21:08:32Z")

</div>

My equivalent C version failed, too (event3 is my mouse, I don't  
expect to read from my event4 (USB DAC)):

&nbsp;&nbsp;#include \<unistd.h\>  
&nbsp;&nbsp;#include \<sys/types.h\>  
&nbsp;&nbsp;#include \<sys/stat.h\>  
&nbsp;&nbsp;#include \<fcntl.h\>  
&nbsp;&nbsp;int main(void)  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;int fd = open("/dev/input/event3", O\_RDONLY|O\_CLOEXEC);  
&nbsp;&nbsp;&nbsp;&nbsp;char buf[16];

&nbsp;&nbsp;&nbsp;&nbsp;read(fd, buf, sizeof(buf));

&nbsp;&nbsp;&nbsp;&nbsp;return 0;  
&nbsp;&nbsp;}

I used event3 to capture my mouse with a bigger buffer and it worked fine.

OK, reading the source of evdev\_read() in drivers/input/evdev.c of the  
Linux kernel, EINVAL seems to be caused by the buffer being too small:

&nbsp;&nbsp;if (count != 0 && count \< input\_event\_size())  
&nbsp;&nbsp;&nbsp;&nbsp;return -EINVAL;

I just used readpartial(64) since I didn't feel like calculating  
input\_event\_size() to get the exact size:

&nbsp;&nbsp;File.open('/dev/input/event3', 'r') do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;p f.readpartial(64) # =\> 48 bytes  
&nbsp;&nbsp;end

Bumping up the buf size in my C version to 64 succeeded, too.

> **···**
>
> Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> 
> > On Fri, Oct 11, 2013 at 5:28 PM, Eric Wong \<normalperson@yhbt.net\> wrote:  
> > \> Felipe Contreras \<felipe.contreras@gmail.com\> wrote:  
> > \>\> I'm trying to use a simple script in Linux:  
> > \>\>  
> > \>\> File.open('/dev/input/event4', 'r') do |f|  
> > \>\> p f.read(16)  
> > \>\> end  
> > \>\>  
> > \>\> But I'm getting this:  
> > \>\>  
> > \>\> test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > \>\> from test.rb:4:in `block in \<main\>'  
> > \>\> from test.rb:3:in `open'  
> > \>\> from test.rb:3:in `\<main\>'  
> > \>\>  
> > \>\> Doing exactly the same in C works just fine, plus I have rw  
> > \>\> permissions to everyone on that file.  
> > \>  
> > \> strace both versions and compare the differences between the relevant  
> > \> syscalls
> > 
> > Well:
> > 
> > open("/dev/input/event4", O\_RDONLY|O\_CLOEXEC) = 7  
> > fcntl(7, F\_GETFD) = 0x1 (flags FD\_CLOEXEC)  
> > fstat(7, {st\_mode=S\_IFCHR|0640, st\_rdev=makedev(13, 68), ...}) = 0  
> > ioctl(7, SNDCTL\_TMR\_TIMEBASE or SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or  
> > TCGETS, 0x7fff295c31b0) = -1 EINVAL (Invalid argument)  
> > ppoll([{fd=7, events=POLLIN}], 1, NULL, NULL, 8) = 1 ([{fd=7, revents=POLLIN}])  
> > read(7, 0x1bc5ae8, 16) = -1 EINVAL (Invalid argument)  
> > close(7) = 0

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [13 October 2013 10:28 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/14 "2013-10-13T10:28:52Z")

</div>

Or maybe it's because your system is 32 bits, so you can read 16, but  
if you read less, you should get the same error.

> **···**
>
> On Sat, Oct 12, 2013 at 5:45 PM, Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> 
> > On Sat, Oct 12, 2013 at 1:07 PM, Tamara Temple \<tamouse.lists@gmail.com\> wrote:
> > 
> > > On Oct 11, 2013, at 6:25 AM, Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm trying to use a simple script in Linux:
> > > > 
> > > > File.open('/dev/input/event4', 'r') do |f|  
> > > > p f.read(16)  
> > > > end
> > > > 
> > > > But I'm getting this:
> > > > 
> > > > test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > > > from test.rb:4:in `block in \<main\>'  
> > > > from test.rb:3:in `open'  
> > > > from test.rb:3:in `\<main\>'
> > > > 
> > > > Doing exactly the same in C works just fine, plus I have rw  
> > > > permissions to everyone on that file.
> > > > 
> > > > What is going on? Anybody else can reproduce?
> > > > 
> > > > --  
> > > > Felipe Contreras
> > > 
> > > From another data point - I cannot reproduce this on linux kernel 2.6.32-5-686, ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
> > 
> > It's probably related to the version of the Linux kernel: 3.11.4+.
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [12 October 2013 22:45 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/15 "2013-10-12T22:45:12Z")

</div>

That is weird. This code works:

FILE \*f;  
char \*buf[16];  
int r;  
f = fopen("/dev/input/event4", "r");  
r = fread(buf, 16, 1, f);  
printf("r=%i\n", r);  
fclose(f);

But this one doesn't:

int fd, r;  
char \*buf[16];  
fd = open("/dev/input/event4", O\_RDONLY);  
r = read(fd, &ev, sizeof(ev));  
printf("r=%i\n", r);  
close(fd);

I don't know what's going on, but using the size of input even structs  
(24) works everywhere.

Thanks!

> **···**
>
> On Sat, Oct 12, 2013 at 4:08 PM, Eric Wong \<normalperson@yhbt.net\> wrote:
> 
> > Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> > 
> > > On Fri, Oct 11, 2013 at 5:28 PM, Eric Wong \<normalperson@yhbt.net\> wrote:  
> > > \> Felipe Contreras \<felipe.contreras@gmail.com\> wrote:  
> > > \>\> I'm trying to use a simple script in Linux:  
> > > \>\>  
> > > \>\> File.open('/dev/input/event4', 'r') do |f|  
> > > \>\> p f.read(16)  
> > > \>\> end  
> > > \>\>  
> > > \>\> But I'm getting this:  
> > > \>\>  
> > > \>\> test.rb:4:in `read': Invalid argument - /dev/input/event4 (Errno::EINVAL)  
> > > \>\> from test.rb:4:in `block in \<main\>'  
> > > \>\> from test.rb:3:in `open'  
> > > \>\> from test.rb:3:in `\<main\>'  
> > > \>\>  
> > > \>\> Doing exactly the same in C works just fine, plus I have rw  
> > > \>\> permissions to everyone on that file.  
> > > \>  
> > > \> strace both versions and compare the differences between the relevant  
> > > \> syscalls
> > > 
> > > Well:
> > > 
> > > open("/dev/input/event4", O\_RDONLY|O\_CLOEXEC) = 7  
> > > fcntl(7, F\_GETFD) = 0x1 (flags FD\_CLOEXEC)  
> > > fstat(7, {st\_mode=S\_IFCHR|0640, st\_rdev=makedev(13, 68), ...}) = 0  
> > > ioctl(7, SNDCTL\_TMR\_TIMEBASE or SNDRV\_TIMER\_IOCTL\_NEXT\_DEVICE or  
> > > TCGETS, 0x7fff295c31b0) = -1 EINVAL (Invalid argument)  
> > > ppoll([{fd=7, events=POLLIN}], 1, NULL, NULL, 8) = 1 ([{fd=7, revents=POLLIN}])  
> > > read(7, 0x1bc5ae8, 16) = -1 EINVAL (Invalid argument)  
> > > close(7) = 0
> > 
> > My equivalent C version failed, too (event3 is my mouse, I don't  
> > expect to read from my event4 (USB DAC)):
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include \<unistd.h\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include \<sys/types.h\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include \<sys/stat.h\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include \<fcntl.h\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int main(void)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int fd = open("/dev/input/event3", O\_RDONLY|O\_CLOEXEC);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char buf[16];
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;read(fd, buf, sizeof(buf));
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > I used event3 to capture my mouse with a bigger buffer and it worked fine.
> > 
> > OK, reading the source of evdev\_read() in drivers/input/evdev.c of the  
> > Linux kernel, EINVAL seems to be caused by the buffer being too small:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (count != 0 && count \< input\_event\_size())  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -EINVAL;
> > 
> > I just used readpartial(64) since I didn't feel like calculating  
> > input\_event\_size() to get the exact size:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.open('/dev/input/event3', 'r') do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f.readpartial(64) # =\> 48 bytes  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > Bumping up the buf size in my C version to 64 succeeded, too.
> 
> --  
> Felipe Contreras

---

<div class="post-metadata">

**Author:** ![Eric\_Wong2](https://avatars.discourse-cdn.com/v4/letter/e/b9e5f3/32.png) [@Eric\_Wong2](https://rubytalk.org/u/Eric_Wong2)\
**Post date:** [13 October 2013 01:13 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/16 "2013-10-13T01:13:30Z")

</div>

> That is weird. This code works:
> 
> FILE \*f;  
> char \*buf[16];  
> int r;  
> f = fopen("/dev/input/event4", "r");  
> r = fread(buf, 16, 1, f);

I bet fread() issues a bigger read() syscall behind-the-scenes

> **···**
>
> Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> 
> > printf("r=%i\n", r);  
> > fclose(f);

---

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [13 October 2013 10:27 UTC](https://rubytalk.org/t/problem-reading-dev-input-in-linux/68585/17 "2013-10-13T10:27:42Z")

</div>

Yeap:

read(3, "\345sZR\0\0\0\0\333\371\10\0\0\0\0\0\4\0\4\0\34\0\0\0\345sZR\0\0\0\0"...,  
4096) = 72

> **···**
>
> On Sat, Oct 12, 2013 at 8:13 PM, Eric Wong \<normalperson@yhbt.net\> wrote:
> 
> > Felipe Contreras \<felipe.contreras@gmail.com\> wrote:
> > 
> > > That is weird. This code works:
> > > 
> > > FILE \*f;  
> > > char \*buf[16];  
> > > int r;  
> > > f = fopen("/dev/input/event4", "r");  
> > > r = fread(buf, 16, 1, f);
> > 
> > I bet fread() issues a bigger read() syscall behind-the-scenes
> 
> --  
> Felipe Contreras
