天天看点

inotify通知机制

#include <unistd.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <string.h>
#include <errno.h>


/*
 *参考: frameworks\native\services\inputflinger\EventHub.cpp
 */

/*Usage: inotify <dir> */

/*
*	inotify主要功能:它是一个内核用于通知用户空间程序文件系统变化的机制
*	众所周知,Linux 桌面系统与 MAC 或 Windows 相比有许多不如人意的地方,
	为了改善这种状况,开源社区提出用户态需要内核提供一些机制,以便用户
	态能够及时地得知内核或底层硬件设备发生了什么,从而能够更好地管理设
	备,给用户提供更好的服务,如 hotplug、udev 和 inotify 就是这种需求
	催生的。
	Hotplug 是一种内核向用户态应用通报关于热插拔设备一些事件发生的机制,
	桌面系统能够利用它对设备进行有效的管理
	udev 动态地维护 /dev 下的设备文件
	inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立
	刻让用户态得知,该机制是著名的桌面搜索引擎项目 beagle 引入的,并在
	Gamin 等项目中被应用。
*	
*/

#if 0
len长度分配以16字节对齐分配
如name为hi,len为16
  name为hello_world_123456 len为48

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

int read_process_inotify_fd(int fd)
{
	int res;
	char event_buf[512];
	int event_size;
	int event_pos = 0;
	struct inotify_event *event;

	/* read */	
	res = read(fd, event_buf, sizeof(event_buf));

	printf("res:%d (int)sizeof(*event):%d\n",res,(int)sizeof(*event));
	if(res < (int)sizeof(*event)) {
		if(errno == EINTR)
			return 0;
		printf("could not get event, %s\n", strerror(errno));
		return -1;
	}

	/* process
	 * 读到的数据是1个或者多个inotify_event,如touch/rm 多个文件,需要遍历处理
	 * 它们的长度不一样,逐个处理
	 */

	while(res >= (int)sizeof(*event)) {
		event = (struct inotify_event *)(event_buf + event_pos);
		printf("event->wd:%d event->mask:0x%x event->len:%d event->name:%s\n",event->wd,event->mask,event->len,event->name);
		if(event->len) {
			if(event->mask & IN_CREATE) {
				printf("create file: %s\n", event->name);
			} else {
				printf("delete file: %s\n", event->name);
			}
		}
		event_size = sizeof(*event) + event->len;
		res -= event_size;
		event_pos += event_size;
	}

	return 0;
}

int main(int argc, char **argv)
{
	int mINotifyFd;
	int result;

	if (argc != 2){
		printf("Usage: %s <dir>\n", argv[0]);
		return -1;
	}

	/* 1.inotify_init 创建 inotify 实例*/

	mINotifyFd = inotify_init();

	/* 
	每一个 inotify 实例对应一个独立的排序的队列。
    文件系统的变化事件被称做 watches 的一个对象管理,
    每一个 watch 是一个二元组(目标,事件掩码),
    目标可以是文件或目录,事件掩码表示应用希望关注的 inotify 事件,
    每一个位对应一个 inotify 事件。Watch 对象通过 watch描述符引用,
    watches 通过文件或目录的路径名来添加。目录 watches 将返回在该
    目录下的所有文件上面发生的事件。
    */

	/* 2.add watch 添加一个 watch
		int inotify_add_watch(int fd, const char *pathname, uint32_t mask)
		fd: 		fd是inotify_init()返回的文件描述符
		pathname: 	pathname是被监视的目标的路径名,即文件名或目录名
		mask:		mask是事件掩码, 在头文件linux/inotify.h中定义了每一位代表的事件
					IN_DELETE:监听删除
					IN_CREATE:监听创建
		return value:返回非负的watch descriptor(监听描述符)
	*/
	result = inotify_add_watch(mINotifyFd, argv[1], IN_DELETE | IN_CREATE);

	/* read */
	while (1) {
		read_process_inotify_fd(mINotifyFd);
	}

	return 0;
}

           

调试结果: [email protected]:~/code/C$ gcc inotify.c -o inotify.c [email protected]:~/code/C$ mkdir tmp [email protected]:~/code/C$ ./inotify tmp& [email protected]:~/code/C$ touch tmp/test tmp/hello res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x100 event->len:16 event->name:test create file: test res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x100 event->len:16 event->name:hello create file: hello [email protected]:~/code/C$ rm tmp/test tmp/hello res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x200 event->len:16 event->name:test delete file: test res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x200 event->len:16 event->name:hello delete file: hello