gliderlabs / docker-alpine

Alpine Linux Docker image. Win at minimalism!
http://gliderlabs.viewdocs.io/docker-alpine
BSD 2-Clause "Simplified" License
5.71k stars 531 forks source link

Python2 cannot read keyboard input device #206

Open vimagick opened 8 years ago

vimagick commented 8 years ago

I'm trying to read usb keyboard inside alpine docker container.

Run container

# docker run --privileged --rm -it -v /dev/input:/dev/input alpine sh

/ # ls /dev/input/by-path/
pci-0000:00:01.2-usb-0:1:1.0-event-mouse  platform-i8042-serio-1-event-mouse
pci-0000:00:01.2-usb-0:1:1.0-mouse        platform-i8042-serio-1-mouse
platform-i8042-serio-0-event-kbd          platform-pcspkr-event-spkr

Run python2 (Raise error)

/ # apk add -U python
/ # python
Python 2.7.12 (default, Jun 29 2016, 08:57:23)
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> kbd = open('/dev/input/by-path/platform-i8042-serio-0-event-kbd', 'rb')
>>> kbd.read(16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 22] Invalid argument
>>> exit()

Run python3 (No error)

/ # apk add python3
/ # python3
Python 3.5.1 (default, May 30 2016, 16:01:04)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> kbd = open('/dev/input/by-path/platform-i8042-serio-0-event-kbd', 'rb')
>>> kbd.read(16)
^C
andyshinn commented 8 years ago

Do we know if this only happens in a container? At first glance, Python 2 appears to be using readv here Python 3 is not:

Python 2:

open("/dev/input/by-path/platform-i8042-serio-0-event-kbd", O_RDONLY) = 3
fstat(3, {st_mode=S_IFCHR|0660, st_rdev=makedev(13, 66), ...}) = 0
readv(3, 0x7ffd72898280, 2)             = -1 EINVAL (Invalid argument)
writev(2, [{"", 0}, {"Traceback (most recent call last):\n", 35}], 2Traceback (most recent call last):
) = 35

Python 3:

open("/dev/input/by-path/platform-i8042-serio-0-event-kbd", O_RDONLY|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
fstat(3, {st_mode=S_IFCHR|0660, st_rdev=makedev(13, 66), ...}) = 0
ioctl(3, TIOCGWINSZ, 0x7fffb343a570)    = -1 EINVAL (Invalid argument)
lseek(3, 0, SEEK_CUR)                   = -1 ESPIPE (Invalid seek)
read(3, ^Cstrace: Process 18 detached

Not yet sure if related...

vimagick commented 8 years ago

Python2 open().read() works well outside of alpine container.