使用带有inotify的read

Raf*_*ida 14 c inotify linux-kernel

我一直在研究inotify调用,但在读取界面方面我仍然有点不稳定.这些是关于如何使用read(2)正确连接inotify的最相关的资源:

它们都以相同的方式实现它们,它们首先定义以下大小:

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 )
Run Code Online (Sandbox Code Playgroud)

然后他们以这种方式使用它们:

length = read( fd, buffer, BUF_LEN );  

if ( length < 0 ) {
    perror( "read" );
}  

while ( i < length ) {
    struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
    /* some processing */
    i += EVENT_SIZE + event->len;
}
Run Code Online (Sandbox Code Playgroud)

现在,我们知道名称是其中的一部分,struct inotify_event并且它具有可变长度.那么,缓冲区中的最后一个inotify_event是否会被截断?

假设有1023个inotify_events,路径为16个字节,另一个路径为32个字节.那会发生什么?后来会被截断吗?或者内核是否会看到它不适合缓冲区并完全放弃?

Jos*_*ley 8

基本用法

根据inotify(7),您可以使用FIONREAD ioctl查找可读取的数据量并相应地调整缓冲区大小.这里有一些(非常粗糙的)代码可以完成这个:

unsigned int avail;
ioctl(inotify_fd, FIONREAD, &avail);

char buffer[avail];
read(fd, buffer, avail);

int offset = 0;
while (offset < avail) {
    struct inotify_event *event = (inotify_event*)(buffer + offset);

    // Insert logic here
    my_process_inotify_event(event);

    offset = offset + sizeof(inotify_event) + event->len;
}
Run Code Online (Sandbox Code Playgroud)

更强大的用法

inotify-toolsinotify提供了更高级别的界面.您可以使用它而不是访问inotify,或者您可以看到它如何实现inotifytools_next_events以安全可靠地读取所有可用事件.

部分事件和截断

在回答有关截断的问题时,如果给定的缓冲区对于所有事件来说太小,我认为内核不会返回部分inotify_event或截断inotify_event.inotify(7)联机帮助页的以下段落表明:

给read(2)的缓冲区太小而无法返回有关下一个事件的信息时的行为取决于内核版本:在2.6.21之前的内核中,read(2)返回0; 从内核2.6.21开始,read(2)失败并出现错误EINVAL.

inotifytools.c中的以下注释一样:

// oh... no.  this can't be happening.  An incomplete event.
// Copy what we currently have into first element, call self to
// read remainder.
// oh, and they BETTER NOT overlap.
// Boy I hope this code works.
// But I think this can never happen due to how inotify is written.
Run Code Online (Sandbox Code Playgroud)