Tag: inotify

如何在C中使用inotify?

我搜索了与inotify相关的问题,这个问题有点不同…… 我使用以下代码来监视一个文件(而不是目录)的更改。 在测试中,当我保存目标文件时,read()会返回,这意味着它可以工作。 但是event-> mask是32768,它不是IN_MODIFY,名称是空的。 另一个问题:它无法持续监控。 当我第二次更改文件时,它没有响应。 感谢您的帮助! #include #include #include #define EVENT_SIZE (sizeof (struct inotify_event)) #define BUF_LEN (16 * (EVENT_SIZE + 16)) int main() { int fd; fd = inotify_init(); if (fd < 0) perror("inotify_init()"); int wd; wd = inotify_add_watch(fd, "target.txt", IN_MODIFY); if (wd 0) { int i = 0; while (i wd, event->mask, […]

使用带有inotify的read

我一直在研究inotify调用,但是当涉及到读取接口时,我仍然有点不稳定。 这些是关于如何使用read(2)正确连接inotify的最相关的资源: http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html http://www.linuxjournal.com/article/8478 它们都以相同的方式实现它们,它们首先定义以下大小: #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) 然后他们以这种方式使用它们: length = read( fd, buffer, BUF_LEN ); if ( length < 0 ) { perror( "read" ); } while ( i len; } 现在,我们知道name是struct inotify_event一部分,并且它具有可变长度。 那么,缓冲区中的最后一个inotify_event是否会被截断? 假设有1023个inotify_events,路径为16个字节,另一个路径为32个字节。 那会发生什么? 后来会被截断吗? 或者内核是否会看到它不适合缓冲区并完全放弃?

在C中使用inotify文件

我试图在C中运行inotify的例子但是它不起作用。 我想监视对文件的修改(文件是tmp.cfg),但它不起作用..我不知道我是否正确运行它,因为我理解如何监视目录,但不是一个file这是一个例子: #include #include #include #include #include #include #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main( int argc, char **argv ) { int length, i = 0; int fd; int wd; char buffer[BUF_LEN]; fd = inotify_init(); if ( fd < 0 ) { […]

inotify_add_watch在/ sys / class / net / eth0 / operstate上失败

我在Linux中使用了inotify,以便在网络接口链接发生变化时引发事件。 每当接口链接发生变化时,/ sys / class / net / eth40 / operstate / file都会被修改。 但是在下面的代码片段中,即使文件被修改,read函数仍然处于阻塞状态。 #include #include #include #include #include #define FILE_TO_WATCH “/sys/class/net/eth40/operstate” #define EVENT_SIZE (sizeof (struct inotify_event)) #define EVENT_BUFFER_LENGTH (1024 * EVENT_SIZE + NAME_MAX + 1) void print_event(struct inotify_event *event) { int ret = 0; if (event->mask & IN_CREATE) printf(“file created in directory\n”); if (event->mask […]