专业团队图片素材美国seo薪酬
在linux下开发过程中,用户态需要内核提供一些机制,以便用户态能够及时地得知内核或底层硬件设备发生了什么,从而能够更好地管理设备,给用户提供更好的服务,如 hotplug、udev 和 inotify 就是这种需求催生的。
Hotplug 是一种内核向用户态应用通报关于热插拔设备一些事件发生的机制,桌面系统能够利用它对设备进行有效的管理,udev 动态地维护 /dev 下的设备文件
inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立刻让用户态得知,该机制是著名的桌面搜索引擎项目beagle 引入的,并在 Gamin 等项目中被应用。
事实上,在 inotify 之前已经存在一种类似的机制叫 dnotify,但是它存在许多缺陷:
1. 对于想监视的每一个目录,用户都需要打开一个文件描述符,因此如果需要监视的目录较多,将导致打开许多文件描述符,特别是,如果被监视目录在移动介质上(如光盘和 USB 盘),将导致无法 umount 这些文件系统,因为使用 dnotify 的应用打开的文件描述符在使用该文件系统。
2. dnotify 是基于目录的,它只能得到目录变化事件,当然在目录内的文件的变化会影响到其所在目录从而引发目录变化事件,但是要想通过目录事件来得知哪个文件变化,需要缓存许多 stat 结构的数据。
3. Dnotify 的接口非常不友好,它使用 signal。
Inotify 是为替代 dnotify 而设计的,它克服了 dnotify 的缺陷,提供了更好用的,简洁而强大的文件变化通知机制:
1. Inotify 不需要对被监视的目标打开文件描述符,而且如果被监视目标在可移动介质上,那么在 umount 该介质上的文件系统后,被监视目标对应的 watch 将被自动删除,并且会产生一个 umount 事件。
2. Inotify 既可以监视文件,也可以监视目录。
3. Inotify 使用系统调用而非 SIGIO 来通知文件系统事件。
4. Inotify 使用文件描述符作为接口,因而可以使用通常的文件 I/O 操作select 和 poll 来监视文件系统的变化。
1、android 对于inotify的使用情况
以上就是android中主要实现代码,层次很简单都只有一个文件
2、inotify 用户接口使用
在用户态,inotify 通过三个系统调用和在返回的文件描述符上的文件 I/O 操作来使用
创建 inotify 实例:
int fd = inotify_init ();
每一个 inotify 实例对应一个独立的排序的队列。
文件系统的变化事件被称做 watches 的一个对象管理,每一个 watch 是一个二元组(目标,事件掩码),目标可以是文件或目录,事件掩码表示应用希望关注的 inotify 事件,每一个位对应一个 inotify 事件。Watch 对象通过 watch描述符引用,watches 通过文件或目录的路径名来添加。目录 watches 将返回在该目录下的所有文件上面发生的事件。
添加一个watch:
int wd = inotify_add_watch (fd, path, mask);
fd 是 inotify_init() 返回的文件描述符,path 是被监视的目标的路径名(即文件名或目录名),mask 是事件掩码,
在头文件 linux/inotify.h 中定义了每一位代表的事件。可以使用同样的方式来修改事件掩码,即改变希望被通知的inotify 事件。wd 是 watch 描述符。
删除一个 watch:
int ret = inotify_rm_watch (fd, wd);
文件事件变化的读取:
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 */
};
结构中的 wd 为被监视目标的 watch 描述符,mask 为事件掩码,len 为 name字符串的长度,name 为被监视目标的路径名,该结构的 name 字段为一个桩,它只是为了用户方面引用文件名,文件名是变长的,它实际紧跟在该结构的后面,文件名将被 0 填充以使下一个事件结构能够 4 字节对齐。注意,len 也把填充字节数统计在内。
通过 read 调用可以一次获得多个事件,只要提供的 buf 足够大。
size_t len = read (fd, buf, BUF_LEN);
buf 是一个 inotify_event 结构的数组指针,BUF_LEN 指定要读取的总长度,buf 大小至少要不小于 BUF_LEN,该调用返回的事件数取决于 BUF_LEN 以及事件中文件名的长度。Len 为实际读去的字节数,即获得的事件的总长度。可以在函数 inotify_init() 返回的文件描述符 fd 上使用 select() 或poll(), 也可以在 fd 上使用 ioctl 命令 FIONREAD 来得到当前队列的长度。close(fd)将删除所有添加到 fd 中的 watch 并做必要的清理。
总结整体用法:
- int fd = inotify_init();
- wfd = inotify_add_watch(fd, path, mask);
- for(;;){// 可以使用 select() / poll() 进行轮询
- char event_buf[512];
- struct inotify_event* event;
- int num_bytes = read(fd, event_buf, sizeof(event_buf));
- while (num_bytes >= (int)sizeof(*event)){
- int event_size;
- event = (struct inotify_event *)(event_buf + event_pos);
- //TODO 可以返回正在监控的 event->wd, event->mask ,event->name(path)
- event_size = sizeof(*event) + event->len;
- num_bytes -= event_size;
- event_pos += event_size;
- }
- }
- inotify_rm_watch(fd, wfd);
3、内核实现机理
fs\notify\inotify\inotify.c
fs\notify\inotify\inotify_user.c
先看重要的几个数据数据
- 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 */
- };
在内核中,每一个 inotify 实例对应一个 inotify_device 结构,这个里面有个重要的东东:idr其功能:idr 用于把 watch 描述符映射到对应的 inotify_watch ,如此对于wfd就有具体的描述符才能实现监控。这个 idr 是在 inotify_add_watch 中调用 idr_get_new_above 将 idr 与 wfd 进行映射的
所谓IDR,其实就是和身份证的含义差不多,我们知道,每个人有一个身份证,身份证只是 一串数字,从数字,我们就能知道这个人的信息。同样道理,idr的要完成的任务是给要管理的对象分配一个数字,可以通过这个数字找到要管理的对象。
events 为该 inotify 实例上发生的事件的列表,被该 inotify 实例监视的所有事件在发生后都将插入到这个列表,watches 是给 inotify 实例监视的 watch 列表,inotify_add_watch 将把新添加的 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 */
- };
d_list 指向所有 inotify_device 组成的列表的,i_list 指向所有被监视 inode 组成的列表 ,count 是引用计数,dev 指向该 watch 所在的 inotify 实例对应的 inotify_device 结构,inode 指向该 watch 要监视的 inode,wd 是分配给该 watch 的描述符,mask 是该 watch 的事件掩码,表示它对哪些文件系统事件感兴趣。
基本的数据结构理清楚,代码明了不介绍了。
还有一个问题就是如何对sdcard做到unmount事件通知呢?其实就是在内核中有个函数: inotify_unmount_inodes
它会在文件系统被 umount 时调用来通知 umount 事件给 inotify 系统
inotify hook 钩子函数
为了截获文件系统变化,inotify在每个文件操作函数中加入了hook函数。当调用文件操作函数时,就会触发hook函数。这些hook函数定位于include/linux/fsnotify.h,节选如下
/** fsnotify_d_instantiate - instantiate a dentry for inode* Called with dcache_lock held.*/
static inline void fsnotify_d_instantiate(struct dentry *entry,struct inode *inode)
{__fsnotify_d_instantiate(entry, inode);inotify_d_instantiate(entry, inode);
}/* Notify this dentry's parent about a child's events. */
static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
{__fsnotify_parent(dentry, mask);inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
}/** fsnotify_d_move - entry has been moved* Called with dcache_lock and entry->d_lock held.*/
static inline void fsnotify_d_move(struct dentry *entry)
{/** On move we need to update entry->d_flags to indicate if the new parent* cares about events from this entry.*/__fsnotify_update_dcache_flags(entry);inotify_d_move(entry);
}/** fsnotify_link_count - inode's link count changed*/
static inline void fsnotify_link_count(struct inode *inode)
{inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
}
以fsnotify_open 为例,当用户调用open,系统调用中的sys_open就会调用fsnotif_open产生通过inotify_inode_queue_event函数产生FS_OPEN事件(msk为FS_OPEN)。事实上事件处理函数是通过这样流程触发的:
用户执行文件操作=>找到具体inode=>找到的成员watch链表=>然后找到inotify_handel实例=>再找到处理函数本身。
/** fsnotify_open - file was opened*/
static inline void fsnotify_open(struct dentry *dentry)
{struct inode *inode = dentry->d_inode;__u32 mask = FS_OPEN;if (S_ISDIR(inode->i_mode))mask |= FS_IN_ISDIR;inotify_inode_queue_event(inode, mask, 0, NULL, NULL);fsnotify_parent(dentry, mask);fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
}
handle函数
struct inotify_handle {struct idr idr; /* idr mapping wd -> watch */struct mutex mutex; /* protects this bad boy */struct list_head watches; /* list of watches */atomic_t count; /* reference count */u32 last_wd; /* the last wd allocated */const struct inotify_operations *in_ops; /* inotify caller operations */
};
处理函数
struct inotify_operations {void (*handle_event)(struct inotify_watch *, u32, u32, u32,const char *, struct inode *);void (*destroy_watch)(struct inotify_watch *);
};
更多信息可以参考
http://blog.csdn.net/zhangskd/article/details/7572320
IBM写的比较全
http://blog.csdn.net/xzongyuan/article/details/20470217
inotify-tools
inotify-tools是为inotify机制提供的一套C语言接口。类似的工具还有incron和inotail等