ferry
ferry

Reputation: 437

writing to /dev/uinput (on ubuntu 12.04)

I'm developing a little program that creates virtual joysticks on linux, with a python front end. It is a fork of Linux-Virtual-Joystick.

I need to write to dev/uinput in order to create the user-defined joystick. The file is opened with O_RDWR (I temporarily added read/write access others for the file whilst debugging). When I do

write(uifd, &uidev, sizeof(uinput_user_dev));

it returns -1 and sets errno to 22(EINVAL). The arguments are correct, and the file was successfully opened.

Did anyone else encounter this problem? I shelved the project for about a month, but I remember that it worked in the last version of Ubuntu.

Update: uinput works on ubuntu 12.10

Upvotes: 6

Views: 23417

Answers (3)

OM55
OM55

Reputation: 1058

I think the problem you have is with access rights to uinput. The error message you receive is typical of that and I have seen the identical behaviour before with other devices.

In order to test that assumption, change the /dev/uinput permissions to allow access to all:

chmod +0666 /dev/uinput

Then try again your code. If now it works fine, you will need to make that change permanent, since otherwise it will revert back to the original permissions after reboot.

To do that in a safe fashion, add a rule file to be located at: /etc/dev/rules.d

with the following line:

KERNEL=="uinput", GROUP="udev_group"

To see how a rule file should look like, check the udev rules file located at:

/lib/udev/rules.d/50-udev-default.rules

When ready, add a a group named udev_group and add your user name to it (or any user that is supposed to have write access to uinput).

You may need to reboot to get the new rule working.

The outcome would be that any user who's member of that group will have full access to uinput, which is exactly what you wanted.


to add the group you can install "Users and Groups":

sudo apt-get install gnome-system-tools

and launch it at:

Application -> System Tools -> Administration -> Users and Groups**

or in terminal:

gnome-system-tools

Upvotes: 12

daemacles
daemacles

Reputation: 302

I had this error in Ubuntu 14.04 too, from your repo (https://github.com/ferry-/Linux-Virtual-Joystick-cpp) . I fixed it by zeroing out the device::uidev member in the device constructor in device.h.

memset(&uidev, 0, sizeof(uidev));

Upvotes: 0

A.J.
A.J.

Reputation: 1027

Since uinput module is missing, you should consider building it before going further.

I've never rebuilt a Linux kernel module this way, so you can follow the explanation here

First, you need to get the corresponding Linux source code and headers. Also install module-init-tools
Then, change dir to /usr/src/linux and do as root

cp /boot/config-* ./.config
make drivers/input/misc/uinput.ko

It'll take a minutes to build uinput.ko

Check if it works before move uinput.ko to /lib/modules/<"yourkernelversion">/kernel/drivers/input/misc

insmod ./drivers/input/misc/uinput.ko

Edit 1:

It seems that since Linux 2.6.35-17.23, uinput is a built-in module. That's why it's not shown by lsmod.

I have just taken a look at your code, and I think the problem is in this line

if (write(uifd, &uidev, sizeof(uinput_user_dev) != sizeof(uinput_user_dev))) 

It should be

if (write(uifd, &uidev, sizeof(uinput_user_dev)) != sizeof(uinput_user_dev))

Hope that helps

Upvotes: 0

Related Questions