简体   繁体   English

使用inotify时/ dev / sdX缺少通知

[英]Missing notifications for /dev/sdX when using inotify

I want to get notified whenever data on hard disk / hard disk partition gets modified. 每当硬盘/硬盘分区上的数据被修改时,我都希望得到通知。 I am on Linux and want it to check from C++. 我在Linux上,希望它从C ++中进行检查。

My approach was to use inotify on linux device file /dev/sdX (sdX = appropriate hard disk / disk partition file). 我的方法是在Linux设备文件/ dev / sdX(s​​dX =适当的硬盘/磁盘分区文件)上使用inotify。 I wrote program for /dev/sda1 file. 我为/ dev / sda1文件编写了程序。 My expectation was that whenever I create/delete a file/folder anywhere in home directory, the file /dev/sda1 should dynamically get modified (since my /dev/sda1 is mounted at location "/") and I should get notified for modification. 我的期望是,每当我在主目录中的任何地方创建/删除文件/文件夹时,文件/ dev / sda1都应该动态地修改(因为我的/ dev / sda1挂载在位置“ /”),并且应该得到修改通知。 。 But, I did not get notification. 但是,我没有收到通知。

Here is my code:- 这是我的代码:

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

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

const char * file_path = "/dev/sda1";

int main( int argc, char **argv ) 
{
  int length, i;
  int fd;
  int wd;
  char buffer[BUF_LEN];


  while(1)
  {

    fd = inotify_init();

     if ( fd < 0 ) {
       perror( "inotify_init" );
     }

     wd = inotify_add_watch(fd, file_path, IN_MODIFY);

     if (wd < 0)
            perror ("inotify_add_watch");


      length = read( fd, buffer, BUF_LEN );

      printf("here too\n");

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

          i = 0;

      while ( i < length ) {
          struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
          printf("inotify event\n");


         if ( IN_MODIFY ) {
             if ( event->mask & IN_ISDIR ) {
                  printf( "The directory %s was modified.\n", file_path );
            }
              else {
                  printf( "The file %s was modified.\n", file_path );
              }
          }

          i += EVENT_SIZE + event->len;

      }

      ( void ) inotify_rm_watch( fd, wd );
      ( void ) close( fd );
  }



  exit( 0 );
}

This code notifies for a normal file correctly whenever the file gets modified. 每当修改文件时,此代码都会正确通知正常文件。 But, it doesn't work on device file, whenever there are changes in corresponding device mount directory. 但是,无论何时在相应的设备安装目录中进行更改,它都不会对设备文件起作用。 Is there anything wrong with my approach? 我的方法有什么问题吗? Shouldn't /dev/sdX file get modified dynamically, whenever file system mounted on it gets changed? 每当安装在其上的文件系统发生更改时,是否应该动态修改/ dev / sdX文件?

I found a similar question Get notified about the change in raw data in hard disk sector - File change notification , but there were no useful answers on it. 我发现了一个类似的问题,即有关硬盘扇区中原始数据的更改的通知-文件更改通知 ,但没有任何有用的答案。

/dev/sda1 is a block device, not a regular file. /dev/sda1是块设备,不是常规文件。 inotify cannot observe devices for modifications. inotify无法观察设备进行修改。 (Consider what this would even mean for other types of devices, like /dev/random …) (考虑一下这对于其他类型的设备(例如/dev/random ……)甚至意味着什么。)

If you want to watch for any change on a filesystem, use fanotify instead. 如果要监视文件系统上的任何更改,请改用fanotify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM