inotify框架的使用和原理!如何添加对于目标文件的watch呢?

描述

1. 概论

inotify是Linux中用于监控文件系统变化的一个框架,不同于前一个框架dnotify, inotify可以实现基于inode的文件监控。也就是说监控对象不再局限于目录,也包含了文件。不仅如此,在事件的通知方面,inotify摈弃了dnotify的信号方式,采用在文件系统的处理函数中放置hook函数的方式实现。

2. 用户层

2.1 数据结构

在inotify中,对于一个文件或目录的监控被称为一个watch。 给某一个文件或目录添加一个watch就表示要对该文件添加某一类型的监控。监控的类型由一个掩码Mask表示,mask有: 

IN_ACCESS : 文件的读操作 

IN_ATTRIB : 文件属性变化 

IN_CLOSE_WRITE : 文件被关闭之前被写 

IN_CLOSE_NOWRITE : 文件被关闭 

IN_CREATE : 新建文件 

IN_DELETE : 删除文件 

IN_MODIFY : 修改文件 

IN_MOVE_SELF : 被监控的文件或者目录被移动 

IN_MOVED_FROM : 文件从被监控的目录中移出 

IN_MOVED_TO : 文件从被监控的目录中移入 

IN_OPEN : 文件被打开 

事件的类型有了,我们还需要一个结构体去表示一次事件, 在用户空间,inotify使用inotify_event表示一个事件,每一个事件都有一个特定的身份标示wd, wd是一个整型变量。每一个事件都有一组事件类型与其关联(IN_CREATE | IN_OPEN)。 事件中还应包含文件名。

struct inotify_event {

int wd;/* Watch descriptor */

uint32_t mask;/* Mask of events */

uint32_t cookie;/* Unique cookie associating related

events (for rename(2)) */

uint32_t len;/* Size of name field */

char name[];/* Optional null-terminated name */

};

2.2函数及inotify的使用

为了防止文件描述符fd的快速消耗,inotify提出了一个inotify instance(inotify实例)的概念。每一个inotify实例表示一个可读写的fd, 一个inotify实例链接有多个对于文件的watch。而函数inotify_init的工作就是生成一个inotify实例。

如何添加对于目标文件的watch呢?使用inotify_add_watch完成该任务,inotify_add_watch有三个参数,第一个参数是该watch所属的实例的fd, 第二个参数是被监控的文件名,第三个参数要监控的事件类型。

有添加就有删除, inotify_rm_watch(int fd, int wd)完成watch的删除工作,类似的, fd表示实例,wd表示即将删除的watch.

void handle_event(int fd){

for(;;){

int len =0;

char buf[BUFSIZE];

read(fd, buf, BUFSIZE);

int i =0;

char*p;

for(p = buf; p <(buf+len); p +=sizeof(struct inotify_event)+ even->len){

event =(struct inotify_event *)p;

if(event -> mask & IN_OPEN)

printf("IN_OPEN ");

}

}

}

int main(void){

int fd;

if((fd = inotify_init())<0){

perror("init error");

}

if(inotify_add_watch(fd,"/home", IN_OPEN|IN_DELETE)<0){

perror("add watch");

}

handle_event(fd);

return0;

}

从以上代码可以看出,inotify的使用很简单,由于一个inotify实例被抽象为一个文件,所以我们可以通过read函数直接读取其中的事件。

#include

int inotify_init(void);int inotify_init1(int flags);

int inotify_add_watch(int fd,constchar*pathname,uint32_t mask);

int inotify_rm_watch(int fd,int wd);

3. 内核原理

3.1 hook函数

inotify通过在文件系统的操作函数(vfs_open, vfs_unlink等)中插入hook函数改变代码的执行路径,从而产生相应的事件。以下是一个hook函数的列表:

图3-1  <图片来自引用1>

函数

下图是sys_open函数的函数调用流程,可以看到sys_open函数调用的是fsnotify_open函数去处理open事件。

而fsnotify又调用inotify_dentry_parent_queue_event函数和inotify_inode_queue_event函数.

图3-2 

其中inotify_dentry_parent_queue_event本身也调用了inode_queue_event函数,只是参数不同罢了. 

图3-3 

可见在inotify_dentry_parent_queue_event中,第一个参数变成了被监控目录的父目录的inode. 关于这两个函数,我们先按下不表, 留待后文再说.

3.2 inotifyfs (inotify.c)

在内核中inotify被抽象为一个虚拟文件系统. 在inotifyfs的初始化函数中,完成了以下三件事:

初始化inotifyfs( 调用register_filesystem() , kern_mount() )

设置事件队列的长度等

创建inotify_event和inotify_watch结构的slab缓存

图3-5 

3.3 数据结构

3.2.1. inotify_device

struct inotify_device:表示一个inotify实例(inotify instance).,在linux2.16.13中,inotify是以模块的形式出现的,在module_init中会调用setup函数. 

在inotify_device结构中保存有两个链表头部,一个事件链表,链表中保存的是该inotify实例上所有事件,另一个是watch链表,保存的是该实例上所有的watch.

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 */

unsignedint        queue_size;    /* size of the queue (bytes) */

unsignedint        event_count;    /* number of pending events */

unsignedint        max_events;    /* maximum number of events */

u32            last_wd;    /* the last wd allocated */

};

图3-7 

函数

3.2.2 inotify_kernel_event

kernel_event结构封装了一个用户态的event结构, 代表相应文件产生的一次事件, 该结构链接在inotify_device中的events链表.

struct inotify_kernel_event {

struct inotify_event   event;/* the user-space event */

struct list_head list;/* entry in inotify_device's list */

char   *name;/* filename, if any */

};

3.2.2 inotify_watch

inotify_watch表示我们向文件添加一个监控. 他分别链接到两个链表,一个链表头在inode结构中, 另一个在inotify_device结构中.

struct inotify_watch {

struct list_head   d_list;/* entry in inotify_device's list */

struct list_head   i_list;/* entry in inde'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 */

};

3.3 深入api

接下来我以inotify的用户接口为例, 带大家深入探索一下这些函数究竟做了什么.

3.3.1 inotify_init

inotify_init函数的作用是给进程分配一个用于读写inotify事件缓冲区的一个fd. 

图3-8 

函数

3.3.2 inotify_add_watch

inotify_add_watch有三个参数, watch所属的文件描述符,被监控的目标文件或者目录的路径, 事件掩码. 

究竟add_watch是怎样的一个过程, 让我们拭目以待. 

图3-9 

函数

3.4 事件究竟从何而来

上文提到, inotify在文件系统的每个文件操作函数中插入了一系列的钩子函数, 由此inotify就可以记录用户对于文件的各种操作. 简单粗暴有没有 … …

其中一个主要的函数是 inotify_inode_queue_event, 该函数的主要功能是遍历Inode的inotify_watches链表, 由watch为根, 找到挂在inotify_device上的事件, 并将事件插入事件队列(inotify_dev_queue_event).

图3-10

函数

可以看到这个函数最终还是调用了inotify_dev_queue_event函数, inotify_dev_queue_event的主要功能是将生成事件并将其插入inotify_device结构的events链表.

总结

以上我以2.6.13版本的内核为例阐述了inotify框架的使用和原理. 本来打算是以最新版本内核为例的, 但是在4.15中, 内核合并dnotify inotify fanotify这三个框架并且抽象出一个新的接口fsnotify, 代码改动较大, 不利于讲解inotify的原理, 所以我选择了第一次合并inotify的2.6.13内核. 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分