Detailed explanation of Linux INotif mechanism and example code

高洛峰
Release: 2023-03-05 14:34:02
Original
1313 people have browsed it

Linux INotif Mechanism

1. Foreword:

As we all know, the Linux desktop system has many unsatisfactory features compared with MAC or Windows. In order to improve this situation, open source The community proposed that the user state needs the kernel to provide some mechanisms so that the user state can know in time what happened to the kernel or underlying hardware devices, so that it can better manage the devices and provide better services to users, such as hotplug, udev and inotify. Born out of this need. Hotplug is a mechanism for the kernel to notify user-mode applications about the occurrence of some events on hot-plug devices. Desktop systems can use it to effectively manage devices. udev dynamically maintains device files under /dev. inotify is a file system The change notification mechanism, such as file addition, deletion and other events, can be immediately notified to the user state. This mechanism was introduced by the famous desktop search engine project beagle and is used in projects such as Gamin.

In fact, there was a similar mechanism called dnotify before inotify, but it has many flaws:

1. For each directory that needs to be monitored, the user needs to open a file descriptor, so if there are many directories that need to be monitored, many file descriptors will be opened, especially if the monitored directory is on removable media (such as CD and USB disk), will result in the inability to umount these file systems because the file descriptor opened by the application using dnotify is using the file system.

2. dnotify is based on directories. It can only get directory change events. Of course, changes to files in the directory will affect the directory where they are located and trigger directory change events. However, if you want to know which files have changed through directory events, you need to cache many stat Structured data.

3. Dnotify's interface is very unfriendly, it uses signal.

Inotify is designed to replace dnotify. It overcomes the shortcomings of dnotify and provides a more usable, concise and powerful file change notification mechanism:

1. Inotify does not need to open a file descriptor for the monitored target, and if the monitored target is on a removable media, then after umounting the file system on the media, the watch corresponding to the monitored target will be automatically deleted and a umount event.

2. Inotify can monitor both files and directories.

3. Inotify uses system calls instead of SIGIO to notify file system events.

4. Inotify uses file descriptors as an interface, so the usual file I/O operations select and poll can be used to monitor file system changes.

Inotify can monitor file system events including:

IN_ACCESS, that is, the file is accessed
IN_MODIFY, the file is written
IN_ATTRIB, and the file attributes are modified , such as chmod, chown, touch, etc.
IN_CLOSE_WRITE, writable files are closed
IN_CLOSE_NOWRITE, unwritable files are closed
IN_OPEN, files are open
IN_MOVED_FROM, files are moved, such as mv
IN_MOVED_TO, the file is moved, such as mv, cp
IN_CREATE, creates a new file
IN_DELETE, the file is deleted, such as rm
IN_DELETE_SELF, self-deletes, that is, an executable file deletes itself when executed
IN_MOVE_SELF, self-move, that is, an executable file moves itself during execution
IN_UNMOUNT, the host file system is umounted
IN_CLOSE, the file is closed, equivalent to (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE, the file is Move, equivalent to (IN_MOVED_FROM | IN_MOVED_TO)

Note: The files mentioned above also include directories.

2. User interface

In user mode, inotify is used through three system calls and file I/operations on the returned file descriptor. Use inotify The first step is to create an inotify instance:

int fd = inotify_init ();
Copy after login

Each inotify instance corresponds to an independent sorted queue.

The file system change events are managed by an object called watches. Each watch is a tuple (target, event mask). The target can be a file or directory, and the event mask represents The inotify event that the application wants to pay attention to, each bit corresponds to an inotify event. Watch objects are referenced by watch descriptors, and watches are added by the pathname of a file or directory. Directory watches will return events that occurred on all files in that directory.

The following function is used to add a watch:

int wd = inotify_add_watch (fd, path, mask);
Copy after login

fd is the file descriptor returned by inotify_init(), path is the path name of the monitored target (i.e. file name or directory name), mask is an event mask, and the event represented by each bit is defined in the header file linux/inotify.h. The event mask can be modified in the same way by changing the inotify events that you want to be notified of. Wd is the watch descriptor.
The following function is used to delete a watch:

int ret = inotify_rm_watch (fd, wd);
Copy after login

fd is the file descriptor returned by inotify_init(), wd is the watch descriptor returned by inotify_add_watch(). Ret is the return value of the function.
File events are represented by an inotify_event structure, which is obtained using the usual file reading function read through the file descriptor returned by inotify_init()

struct inotify_event {
    __s32      wd;       /* watch descriptor */
    __u32      mask;      /* watch mask */
    __u32      cookie;     /* cookie to synchronize two events */
    __u32      len;      /* length (including nulls) of name */
    char      name[0];    /* stub for possible name */
};
Copy after login

结构中的 wd 为被监视目标的 watch 描述符,mask 为事件掩码,len 为 name字符串的长度,name 为被监视目标的路径名,该结构的 name 字段为一个桩,它只是为了用户方面引用文件名,文件名是变长的,它实际紧跟在该结构的后面,文件名将被 0 填充以使下一个事件结构能够 4 字节对齐。注意,len 也把填充字节数统计在内。
通过 read 调用可以一次获得多个事件,只要提供的 buf 足够大。

size_t len = read (fd, buf, BUF_LEN);
Copy after login

buf 是一个 inotify_event 结构的数组指针,BUF_LEN 指定要读取的总长度,buf 大小至少要不小于 BUF_LEN,该调用返回的事件数取决于 BUF_LEN 以及事件中文件名的长度。Len 为实际读去的字节数,即获得的事件的总长度。
可以在函数 inotify_init() 返回的文件描述符 fd 上使用 select() 或poll(), 也可以在 fd 上使用 ioctl 命令 FIONREAD 来得到当前队列的长度。close(fd)将删除所有添加到 fd 中的 watch 并做必要的清理。

    int inotify_init (void);
int inotify_add_watch (int fd, const char *path, __u32 mask);
int inotify_rm_watch (int fd, __u32 mask);
Copy after login

三、内核实现原理

在内核中,每一个 inotify 实例对应一个 inotify_device 结构:

struct inotify_device {
    wait_queue_head_t    wq;       /* wait queue for i/o */
    struct idr       idr;      /* idr mapping wd -> watch */
    struct semaphore    sem;      /* protects this bad boy */
    struct list_head    events;     /* list of queued events */
    struct list_head    watches;    /* list of watches */
    atomic_t        count;     /* reference count */
    struct user_struct   *user;     /* user who opened this dev */
    unsigned int      queue_size;   /* size of the queue (bytes) */
    unsigned int      event_count;  /* number of pending events */
    unsigned int      max_events;   /* maximum number of events */
    u32           last_wd;    /* the last wd allocated */
};
Copy after login


wq 是等待队列,被 read 调用阻塞的进程将挂在该等待队列上,idr 用于把 watch 描述符映射到对应的 inotify_watch,sem 用于同步对该结构的访问,events 为该 inotify 实例上发生的事件的列表,被该 inotify 实例监视的所有事件在发生后都将插入到这个列表,watches 是给 inotify 实例监视的 watch 列表,inotify_add_watch 将把新添加的 watch 插入到该列表,count 是引用计数,user 用于描述创建该 inotify 实例的用户,queue_size 表示该 inotify 实例的事件队列的字节数,event_count 是 events 列表的事件数,max_events 为最大允许的事件数,last_wd 是上次分配的 watch 描述符。

每一个 watch 对应一个 inotify_watch 结构:

struct inotify_watch {
    struct list_head    d_list; /* entry in inotify_device's list */
    struct list_head    i_list; /* entry in inode's list */
    atomic_t        count; /* reference count */
    struct inotify_device  *dev;  /* associated device */
    struct inode      *inode; /* associated inode */
    s32           wd;   /* watch descriptor */
    u32           mask;  /* event mask for this watch */
};
Copy after login


d_list 指向所有 inotify_device 组成的列表的,i_list 指向所有被监视 inode 组成的列表,count 是引用计数,dev 指向该 watch 所在的 inotify 实例对应的 inotify_device 结构,inode 指向该 watch 要监视的 inode,wd 是分配给该 watch 的描述符,mask 是该 watch 的事件掩码,表示它对哪些文件系统事件感兴趣。

结构 inotify_device 在用户态调用 inotify_init() 时创建,当关闭 inotify_init()返回的文件描述符时将被释放。结构 inotify_watch 在用户态调用 inotify_add_watch()时创建,在用户态调用 inotify_rm_watch() 或 close(fd) 时被释放。
无论是目录还是文件,在内核中都对应一个 inode 结构,inotify 系统在 inode 结构中增加了两个字段:

#ifdef CONFIG_INOTIFY
  struct list_head  inotify_watches; /* watches on this inode */
  struct semaphore  inotify_sem;  /* protects the watches list */
#endif
Copy after login

inotify_watches 是在被监视目标上的 watch 列表,每当用户调用 inotify_add_watch()时,内核就为添加的 watch 创建一个 inotify_watch 结构,并把它插入到被监视目标对应的 inode 的 inotify_watches 列表。inotify_sem 用于同步对 inotify_watches 列表的访问。当文件系统发生第一部分提到的事件之一时,相应的文件系统代码将显示调用fsnotify_* 来把相应的事件报告给

inotify 系统,其中*号就是相应的事件名,目前实现包括:

fsnotify_move,文件从一个目录移动到另一个目录

fsnotify_nameremove,文件从目录中删除
fsnotify_inoderemove,自删除
fsnotify_create,创建新文件
fsnotify_mkdir,创建新目录
fsnotify_access,文件被读
fsnotify_modify,文件被写
fsnotify_open,文件被打开
fsnotify_close,文件被关闭
fsnotify_xattr,文件的扩展属性被修改
fsnotify_change,文件被修改或原数据被修改

有一个例外情况,就是 inotify_unmount_inodes,它会在文件系统被 umount 时调用来通知 umount 事件给 inotify 系统。
以上提到的通知函数最后都调用 inotify_inode_queue_event(inotify_unmount_inodes直接调用 inotify_dev_queue_event ),该函数首先判断对应的inode是否被监视,这通过查看 inotify_watches 列表是否为空来实现,如果发现 inode 没有被监视,什么也不做,立刻返回,反之,遍历 inotify_watches 列表,看是否当前的文件操作事件被某个 watch 监视,如果是,调用 inotify_dev_queue_event,否则,返回。函数inotify_dev_queue_event 首先判断该事件是否是上一个事件的重复,如果是就丢弃该事件并返回,否则,它判断是否 inotify 实例即 inotify_device 的事件队列是否溢出,如果溢出,产生一个溢出事件,否则产生一个当前的文件操作事件,这些事件通过kernel_event 构建,kernel_event 将创建一个 inotify_kernel_event 结构,然后把该结构插入到对应的 inotify_device 的 events 事件列表,然后唤醒等待在inotify_device 结构中的 wq 指向的等待队列。想监视文件系统事件的用户态进程在inotify 实例(即 inotify_init() 返回的文件描述符)上调用 read 时但没有事件时就挂在等待队列 wq 上。

四、使用示例

下面是一个使用 inotify 来监视文件系统事件的例子:

#include <linux/unistd.h>
#include <linux/inotify.h>
#include <errno.h>
_syscall0(int, inotify_init)
_syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask)
_syscall2(int, inotify_rm_watch, int, fd, __u32, mask)
char * monitored_files[] = {
  "./tmp_file",
  "./tmp_dir",
  "/mnt/sda3/windows_file"
};
struct wd_name {
  int wd;
  char * name;
};
#define WD_NUM 3
struct wd_name wd_array[WD_NUM];
char * event_array[] = {
  "File was accessed",
  "File was modified",
  "File attributes were changed",
  "writtable file closed",
  "Unwrittable file closed",
  "File was opened",
  "File was moved from X",
  "File was moved to Y",
  "Subfile was created",
  "Subfile was deleted",
  "Self was deleted",
  "Self was moved",
  "",
  "Backing fs was unmounted",
  "Event queued overflowed",
  "File was ignored"
};
#define EVENT_NUM 16
#define MAX_BUF_SIZE 1024
    
int main(void)
{
  int fd;
  int wd;
  char buffer[1024];
  char * offset = NULL;
  struct inotify_event * event;
  int len, tmp_len;
  char strbuf[16];
  int i = 0;
    
  fd = inotify_init();
  if (fd < 0) {
    printf("Fail to initialize inotify.\n");
    exit(-1);
  }
  for (i=0; i<WD_NUM; i++) {
    wd_array[i].name = monitored_files[i];
    wd = inotify_add_watch(fd, wd_array[i].name, IN_ALL_EVENTS);
    if (wd < 0) {
      printf("Can&#39;t add watch for %s.\n", wd_array[i].name);
      exit(-1);
    }
    wd_array[i].wd = wd;
  }
  while(len = read(fd, buffer, MAX_BUF_SIZE)) {
    offset = buffer;
    printf("Some event happens, len = %d.\n", len);
    event = (struct inotify_event *)buffer;
    while (((char *)event - buffer) < len) {
      if (event->mask & IN_ISDIR) {
        memcpy(strbuf, "Direcotory", 11);
      }
      else {
        memcpy(strbuf, "File", 5);
      }
      printf("Object type: %s\n", strbuf);
      for (i=0; i<WD_NUM; i++) {
        if (event->wd != wd_array[i].wd) continue;
        printf("Object name: %s\n", wd_array[i].name);
        break;
      }
      printf("Event mask: %08X\n", event->mask);
      for (i=0; i<EVENT_NUM; i++) {
        if (event_array[i][0] == &#39;\0&#39;) continue;
        if (event->mask & (1<<i)) {
          printf("Event: %s\n", event_array[i]);
        }
      }
      tmp_len = sizeof(struct inotify_event) + event->len;
      event = (struct inotify_event *)(offset + tmp_len); 
      offset += tmp_len;
    }
  }
}
Copy after login


该程序将监视发生在当前目录下的文件 tmp_file 与当前目录下的目录 tmp_dir 上的所有文件系统事件, 同时它也将监视发生在文件 /mnt/sda3/windows_file 上的文件系统事件,注意,/mnt/sda3 是 SATA 硬盘分区 3 的挂接点。

细心的读者可能注意到,该程序首部使用 _syscallN 来声明 inotify 系统调用,原因是这些系统调用是在最新的稳定内核 2.6.13 中引入的,glibc 并没有实现这些系统调用的库函数版本,因此,为了能在程序中使用这些系统调用,必须通过 _syscallN 来声明这些新的系统,其中的 N 为要声明的系统调用实际的参数数。还有需要注意的地方是系统的头文件必须与被启动的内核匹配,为了让上面的程序能够成功编译,必须让 2.6.13 的内核头文件(包括 include/linux/*, include/asm/* 和 include/asm-generic/*)在头文件搜索路径内,并且是第一优先搜索的头文件路径,因为 _syscallN 需要用到这些头文件中的 linux/unistd.h 和 asm/unistd.h,它们包含了 inotify 的三个系统调用的系统调用号 __NR_inotify_init、__NR_inotify_add_watch 和 __NR_inotify_rm_watch。

因此,要想成功编译此程序,只要把用户编译好的内核的头文件拷贝到该程序所在的路径,并使用如下命令编译即可:

$gcc -o inotify_example -I. inotify_example.c
Copy after login

注意:当前目录下应当包含 linux、asm 和 asm-generic 三个已编译好的 2.6.13 内核的有文件目录,asm 是一个链接,因此拷贝 asm 头文件的时候需要拷贝 asm 与 asm-ARCH(对于 x86 平台应当是 asm-i386)。 然后,为了运行该程序,需要在当前目录下创建文件 tmp_file 和目录 tmp_dir,对于/mnt/sda3/windows_file 文件,用户需要依自己的实际情况而定,可能是/mnt/dosc/windows_file,即 /mnt/dosc 是一个 FAT32 的 windows 硬盘,因此用户在编译该程序时需要根据自己的实际情况来修改 /mnt/sda3。Windows_file 是在被 mount 硬盘上创建的一个文件,为了运行该程序,它必须被创建。
以下是作者在 redhat 9.0 上运行此程序得到的一些结果:

当运行此程序的时候在另一个虚拟终端执行 cat ./tmp_file,此程序的输出为:

Some event happens, len = 48.
Object type: File
Object name: ./tmp_file
Event mask: 00000020
Event: File was opened
Object type: File
Object name: ./tmp_file
Event mask: 00000001
Event: File was accessed
Object type: File
Object name: ./tmp_file
Event mask: 00000010
Event: Unwrittable file closed
Copy after login

以上事件清楚地说明了 cat 指令执行了文件 open 和 close 操作,当然 open 和 close操作都属于 access 操作,任何对文件的操作都是 access 操作。

此外,运行 vi ./tmp_file,发现 vi实际在编辑文件时复制了一个副本,在未保存之前是对副本进行操作。 运行 vi ./tmp_file, 修改并保存退出时,发现 vi 实际在保存修改时删除了最初的文件并把那个副本文件名更改为最初的文件的名称。注意,事件"File was ignored"表示系统把该文件对应的 watch 从 inotify 实例的 watch 列表中删除,因为文件已经被删除。 读者可以自己分别执行命令:echo "abc" > ./tmp_file 、rm -f tmp_file、 ls tmp_dir、 cd tmp_dir;touch c.txt、 rm c.txt 、 umount /mnt/sda3(实际用户需要使用自己当时的 mount 点路径名),然后分析一下结果。Umount 触发两个事件,一个表示文件已经被删除或不在存在,另一个表示该文件的 watch被从 watch 列表中删除。

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed explanations of the Linux INotif mechanism and articles related to example codes, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template