inotify

本文转载于:http://blog.csdn.net/myarrow/article/details/7096460(转载请注明原出处)

一、基本的使用方法

int fd = inotify_init ();-------------------------①

int wd = inotify_add_watch (fd, path, mask); -----②

size_t len = read (fd, buf, BUF_LEN); ------------③

int ret = inotify_rm_watch (fd, wd); -------------④

(1) 其中①为②③④提供fd,②为④提供wd。path是需要监视的文件或者目录,mask是事件掩码,每一位都对应一个事件,常见的掩码有IN_DELETE、IN_MOVED_FROM、IN_MOVED_TO、IN_CREATE、IN_CLOSE_WRITE等。

(2) 每一个 inotify 实例对应一个独立的排序的队列,①的目的就是初始化这个队列。

文件系统的变化事件被称做 watches 的一个对象管理

每一个 watch 是一个二元组(目标,事件掩码)

事件掩码的每一个位对应一个 inotify 事件

Watch 对象通过 watch描述符引用,watches 通过文件或目录的路径名来添加

watches 将返回在该目录下的所有文件上面发生的事件。

所以②的作用就是添加文件系统变化事件。

(3) 只要提供的 buf 足够大,通过 read 调用可以一次获得多个事件。

(4) ③的目的是删除一个watch对象。要找到那个文件描述符下的那个watch,所以需要传递fd和wd两个参数。

二、相关知识

参考网址:http://blog.csdn.net/myarrow/article/details/7096460

开源社区用户提出内核需要提供一些机制,以便让用户及时得知内核或者底层硬件发生了什么变化,从而更好地管理设备,为用户提供服务。如hotpluy、udev、inotify就是在这种需求下催生的。

Hotplug 是一种内核向用户态应用通报关于热插拔设备一些事件发生的机制,桌面系统能够利用它对设备进行有效的管理;

udev 动态地维护 /dev 下的设备文件;

inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立刻让用户态得知,该机制是著名的桌面搜索引擎项目 beagle 引入的,并在 Gamin 等项目中被应用。

文件事件用一个 inotify_event 结构表示

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

在内核中,每一个 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 */
};

无论是目录还是文件,在内核中都对应一个 inode 结构,inotify 系统在 inode 结构中增加了两个字段:

struct inotify_watch {

   struct list_head       d_list; /*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 */
};

无论是目录还是文件,在内核中都对应一个 inode 结构,inotify 系统在 inode 结构中增加了两个字段:

#ifdef CONFIG_INOTIFY
struct list_head inotify_watches; /* watches on this inode */
struct semaphore inotify_sem;/* protects the watches list */
#endif
posted on 2015-05-12 19:43  步孤天  阅读(2177)  评论(0)    收藏  举报