黄东升: inotify学习笔记

这个炎热的夏季除了救命空调

好吃的西瓜

你还需要的是这些

▽▽▽


来点清凉色给眼睛降降温~

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)。 事件中还应包含文件名。

 
 
  1. struct inotify_event {

  2.    int wd; /* Watch descriptor */

  3.    uint32_t mask; /* Mask of events */

  4.    uint32_t cookie; /* Unique cookie associating related

  5.                          events (for rename(2)) */

  6.    uint32_t len; /* Size of name field */

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

  8. };

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.

 
 
  1. void handle_event(int fd) {

  2.    for ( ; ; ) {

  3.            int len = 0;

  4.           char buf[BUFSIZE];

  5.           read(fd, buf, BUFSIZE);

  6.           int i = 0;

  7.           char *p;

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

  9.                    event = (struct inotify_event *)p;

  10.                    if (event -> mask & IN_OPEN)

  11.                        printf("IN_OPEN\n");

  12.            }

  13.    }

  14. }

  15. int main(void) {

  16.    int fd;

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

  18.            perror("init error");

  19.    }

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

  21.          perror("add watch");

  22.    }

  23.    handle_event(fd);

  24.    return 0;

  25. }

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

 
 
  1. #include <sys/inotify.h>

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

  3. int inotify_add_watch(int fd, const char *pathname, uint32_t mask);

  4. int inotify_rm_watch(int fd, int wd);

3. 内核原理

3.1 hook函数

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

图3-1  <图片来自引用1>
640?wx_fmt=png

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

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

图3-2 
640?wx_fmt=png

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

图3-3 
640?wx_fmt=png图3-4 

640?wx_fmt=png

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

3.2 inotifyfs (inotify.c)

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

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

  2. 设置事件队列的长度等

  3. 创建inotify_event和inotify_watch结构的slab缓存


图3-5 

640?wx_fmt=png图3-6 

640?wx_fmt=png


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.

 
 
  1. struct inotify_device {

  2.    wait_queue_head_t     wq;        /* wait queue for i/o */

  3.    struct idr        idr;        /* idr mapping wd -> watch */

  4.    struct semaphore    sem;        /* protects this bad boy */

  5.    struct list_head     events;        /* list of queued events */

  6.    struct list_head    watches;    /* list of watches */

  7.    atomic_t        count;        /* reference count */

  8.    struct user_struct    *user;        /* user who opened this dev */

  9.    unsigned int        queue_size;    /* size of the queue (bytes) */

  10.    unsigned int        event_count;    /* number of pending events */

  11.    unsigned int        max_events;    /* maximum number of events */

  12.    u32            last_wd;    /* the last wd allocated */

  13. };

图3-7 
640?wx_fmt=png

3.2.2 inotify_kernel_event

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

 
 
  1. struct inotify_kernel_event {

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

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

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

  5. };

3.2.2 inotify_watch

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

 
 
  1. struct inotify_watch {

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

  3. struct list_head   i_list; /* entry in in\de's list */

  4. atomic_t   count;  /* reference count */

  5. struct inotify_device  *dev;   /* associated device */

  6. struct inode   *inode; /* associated inode */

  7. s32 wd;    /* watch descriptor */

  8. u32    mask;   /* event mask for this watch */

  9. };

3.3 深入api

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

3.3.1 inotify_init

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

图3-8 

640?wx_fmt=png

3.3.2 inotify_add_watch

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

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

图3-9 
640?wx_fmt=png

3.4 事件究竟从何而来

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

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

图3-10

640?wx_fmt=png

    可以看到这个函数最终还是调用了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内核. 

    文章如果存在任何问题, 请大家积极拍砖.

参考

linux安全体系分析与编程 
Linux 2.6.13



查看"Linux阅码场"精华技术文章请移步:

Linux阅码场精华文章汇总

扫描二维码关注"Linux阅码场"

640?wx_fmt=png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值