I'm creating a Linux module for a game library that let's you hotplug multiple joysticks, it uses inotify to watch /dev/input
.
我正在为一个游戏库创建一个Linux模块,让我们热插多个操纵杆,它使用inotify来监视/dev/input.
I am testing it with 3 joysticks:
我正在用3个操纵杆测试它:
- 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
. - 在那之后,我连接第三个操纵杆,perror给出:/dev/input/js1:权限被拒绝。
- When I check
ls -l /proc/<pid-of-process>/fd
it lists/dev/input/js0
and/dev/input/js2
. -
当我检查ls -l /proc/
/fd时,它会列出/dev/input/js0和/dev/ input/js2。
All the joysticks work fine when I run it as root.
当我以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:
之后,inotify在while(1)循环中是这样处理的:
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?
是否可以使用inotify或者我应该使用udev?如果可能的话,我怎么解决这个问题呢?
1 个解决方案
#1
3
It is very likely a race condition. You see, you get the inotify event when the device node is created (by udev using a mknod()
call), but the access permissions are set by udev using a separate chown()
call, just a tiny bit later.
这很可能是一种竞争状态。您看到,在创建设备节点时(通过使用mknod()调用的udev创建),您将获得inotify事件,但是访问权限是由udev使用一个单独的chown()调用设置的,稍后再进行。
See systemd src/udev/udev-node.c
, node_permissions_apply()
. In this particular case, /dev/input/jsX
is not a symlink, but the actual device node; at least with systemd the device node access mode gets set sometime later, after the actual node is created.
看到systemd src / udev / udev-node。c,node_permissions_apply()。在本例中,/dev/input/jsX不是符号链接,而是实际的设备节点;至少在systemd中,设备节点访问模式将在创建实际节点之后的某个时候设置。
One robust solution would be to modify your createGamepad()
function, so that instead of failing completely at fd == -1 && errno == EACCES
, you instead retry after a short while; at least a few times, say for up to a second or two.
一个健壮的解决方案是修改createGamepad()函数,这样就不会在fd = -1 && & errno == EACCES上完全失败,而是在短时间后重新尝试;至少有几次,比如说一两秒钟。
However, ninjalj pointed out a better suggestion: use also the access permissions change as a trigger to check the device node. This is trivially accomplished, by using IN_CREATE | IN_DELETE | IN_ATTRIBUTE
in the inotify_add_watch()
function!
不过,ninjalj给出了一个更好的建议:使用访问权限更改作为触发器来检查设备节点。通过在inotify_add_watch()函数中使用IN_CREATE | IN_DELETE IN_ATTRIBUTE来实现这一点。
(You'll also want to ignore open()==-1, errno==EACCES
errors in createGamepad()
, as they are likely caused by this race condition, and the following IN_ATTRIBUTE
inotify event will yield access to the same device.)
(您还需要忽略createGamepad()中的open()==-1, errno==EACCES错误(),因为它们可能是由该竞态条件引起的,下面的IN_ATTRIBUTE inotify事件将允许访问相同的设备。)
Prior to ninjalj's comment, I'd personally have used an array of input devices, and another for "possible" input devices that can/need to be retried after a short timeout to decide whether they are available or not, but I think his suggestion is much better.
在ninjalj发表评论之前,我个人会使用一系列的输入设备,以及另一个“可能的”输入设备,这些设备可以/需要在短时间后重新尝试,以决定是否可用,但我认为他的建议要好得多。
Need/want an example?
需要/想要举个例子吗?
#1
3
It is very likely a race condition. You see, you get the inotify event when the device node is created (by udev using a mknod()
call), but the access permissions are set by udev using a separate chown()
call, just a tiny bit later.
这很可能是一种竞争状态。您看到,在创建设备节点时(通过使用mknod()调用的udev创建),您将获得inotify事件,但是访问权限是由udev使用一个单独的chown()调用设置的,稍后再进行。
See systemd src/udev/udev-node.c
, node_permissions_apply()
. In this particular case, /dev/input/jsX
is not a symlink, but the actual device node; at least with systemd the device node access mode gets set sometime later, after the actual node is created.
看到systemd src / udev / udev-node。c,node_permissions_apply()。在本例中,/dev/input/jsX不是符号链接,而是实际的设备节点;至少在systemd中,设备节点访问模式将在创建实际节点之后的某个时候设置。
One robust solution would be to modify your createGamepad()
function, so that instead of failing completely at fd == -1 && errno == EACCES
, you instead retry after a short while; at least a few times, say for up to a second or two.
一个健壮的解决方案是修改createGamepad()函数,这样就不会在fd = -1 && & errno == EACCES上完全失败,而是在短时间后重新尝试;至少有几次,比如说一两秒钟。
However, ninjalj pointed out a better suggestion: use also the access permissions change as a trigger to check the device node. This is trivially accomplished, by using IN_CREATE | IN_DELETE | IN_ATTRIBUTE
in the inotify_add_watch()
function!
不过,ninjalj给出了一个更好的建议:使用访问权限更改作为触发器来检查设备节点。通过在inotify_add_watch()函数中使用IN_CREATE | IN_DELETE IN_ATTRIBUTE来实现这一点。
(You'll also want to ignore open()==-1, errno==EACCES
errors in createGamepad()
, as they are likely caused by this race condition, and the following IN_ATTRIBUTE
inotify event will yield access to the same device.)
(您还需要忽略createGamepad()中的open()==-1, errno==EACCES错误(),因为它们可能是由该竞态条件引起的,下面的IN_ATTRIBUTE inotify事件将允许访问相同的设备。)
Prior to ninjalj's comment, I'd personally have used an array of input devices, and another for "possible" input devices that can/need to be retried after a short timeout to decide whether they are available or not, but I think his suggestion is much better.
在ninjalj发表评论之前,我个人会使用一系列的输入设备,以及另一个“可能的”输入设备,这些设备可以/需要在短时间后重新尝试,以决定是否可用,但我认为他的建议要好得多。
Need/want an example?
需要/想要举个例子吗?