Intotify观察者在LS或观看命令后停止工作

inotify watcher stops working after ls or watch commands

本文关键字:命令 停止工作 观察者 LS Intotify      更新时间:2023-10-16

我从Internet上的许多示例中获取了有关如何使用Inotify的代码。

我尝试了以下实验:
1)在下面运行观察者
2)在单独的外壳中,CD纳入'/mypath'中,为您正在观看的文件夹创建一些文件。例如,'date > output.txt'更多次矿。
3)您将看到观察者的通知。
4)类型'ls /mypath'(甚至'watch -n 1 /mypath'
5)在/我的路径中尝试'date > output.txt'。您将不再看到观察者的通知。或者至少,这是我与Ubuntu 12/13测试时发生的情况。

关于如何修复它的任何想法?

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>
#include <unistd.h>

#define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
#define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
int main() 
{
  int length, i = 0, wd;
  int fd;
  char buffer[BUF_LEN];
  /* Initialize Inotify*/
   fd = inotify_init();
  if ( fd < 0 ) {
    perror( "Couldn't initialize inotify");
  }
  /* add watch to starting directory */
  wd = inotify_add_watch(fd, "/mypath", IN_CLOSE_WRITE | IN_CLOSE_NOWRITE); 
  if (wd == -1)
    {
      printf("Couldn't add watch to %sn","/mypath");
    }
  else
    {
      printf("Watching:: %sn","/mypath");
    }
  /* do it forever*/
  while(1)
    {  
      i = 0;
      length = read( fd, buffer, BUF_LEN );  

      if ( length < 0 ) {
        perror( "read" );
      }  
      while ( i < length ) {
        struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
        if ( event->len ) {
          if ( event->mask & IN_CLOSE_WRITE) {
            if (event->mask & IN_ISDIR)
              printf( "The directory %s was Created.n", event->name );       
            else
              printf( "The file %s was closed (write) with WD %dn", event->name, event->wd );       
          }
          if ( event->mask & IN_CLOSE_NOWRITE) {
            if (event->mask & IN_ISDIR)
              printf( "The directory %s was Created.n", event->name );       
            else
              printf( "The file %s was closed (nowrite) with WD %dn", event->name, event->wd );       
          }

          i += EVENT_SIZE + event->len;
        }
      }     
    }
  /* Clean up*/
  inotify_rm_watch( fd, wd );
  close( fd );
  return 0;
}

您不应将i += EVENT_SIZE + event->len;放入if ( event->len )块中。如果事件的长度名称为零,则指针仍应由EVENT_SIZE递增(如果您将该语句放在块之外,这将发生)。我认为您可能会在iNotify程序中看到一个无限的循环,这是第一个事件开始的,该活动恰好具有零长度的名称。(这正是ls:目录正在打开,而不是其文件,因此name字段中没有任何内容。)

您进入了一个永恒的循环,因为您不会在event->len == 0时更改i添加此:

else
    i += EVENT_SIZE ;

如果if ( event->len == 0 )

事实证明,当您的程序停止工作时,它会吃掉所有CPU。我进行了一些更改,现在似乎可以使用。这是详细信息:

声明buf_len处理16个事件(您可以增加该值):

#define BUF_LEN (16 * (sizeof(struct inotify_event) + NAME_MAX + 1))

更改将事件处理为以下循环的while (i < length)循环:

for ( p = buffer; p < buffer + length; ) {
    struct inotify_event *event = ( struct inotify_event * ) p;
    p += sizeof(struct inotify_event) + event->len;
    if ( event->len ) {
        /* SNIP */
    }
}

p变量应声明为char *,您可以删除不再使用的i