I'm creating a Linux module for a game library that let's you hotplug multiple joysticks, it uses inotify to watch /dev/input
.
I am testing it with 3 joysticks:
- First I connect 2 joysticks.
- Then I start the application, the joysticks work and I don't get a error.
- After that I connect the third joystick,
perror
gives:/dev/input/js1: Permission denied
. - When I check
ls -l /proc/<pid-of-process>/fd
it lists/dev/input/js0
and/dev/input/js2
.
All the joysticks work fine when I run it as root.
This is how it's initialized:
static void createGamepad(char *locName){
char dirName[30];
int fd;
snprintf(dirName, 30, "/dev/input/%s", locName);
fd = open(dirName, O_RDONLY | O_NONBLOCK, 0);
if(fd < 0){
perror(dirName);
}
}
struct dirent *dir;
DIR *d;
int i, notifyfd, watch;
// Attach notifications to check if a device connects/disconnects
notifyfd = inotify_init();
watch = inotify_add_watch(notifyfd, "/dev/input", IN_CREATE | IN_DELETE);
d = opendir("/dev/input");
i = 0;
while((dir = readdir(d)) != NULL){
if(*dir->d_name == 'j' && *(dir->d_name + 1) == 's'){
createGamepad(dir->d_name, i);
i++;
}
}
closedir(d);
After that inotify handles it like this in the while(1)
loop:
static bool canReadINotify(){
fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(notifyfd, &set);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(notifyfd + 1, &set, NULL, NULL, &timeout) > 0 &&
FD_ISSET(notifyfd, &set);
}
// Inside the event loop
struct inotify_event ne;
while(canReadINotify()){
if(read(notifyfd, &ne, sizeof(struct inotify_event) + 16) >= 0){
if(*ne.name != 'j' || *(ne.name + 1) != 's'){
continue;
}
if(ne.mask & IN_CREATE){
createGamepad(ne.name);
}
}
}
Is it even possible with inotify or should I use udev? And if it's possible, how can I solve this?