Linux2.6内核中的文件系统变化通知机制

Inotify:是一种强大的、细粒度的、异步的机制,它满足各种各校的文件监控需要。

inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。 
 
inotify源码安装

安装inotify
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install

实例:

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

using namespace std;

//_syscall0(int, inotify_init)
//_syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask)
//_syscall2(int, inotify_rm_watch, int, fd, __u32, mask)
char * monitored_files[] = {
"/root/work/LogCollection/conf/control.xml",
"/root/work/LogCollection/Logdata/"
};
struct wd_name {
int wd;
char * name;
};
#define WD_NUM 2
struct wd_name wd_array[WD_NUM];
char * event_array[] = {
"File was accessed",
"File was modified",
"File attributes were changed",
"writtable file closed",
"Unwrittable file closed",
"File was opened",
"File was moved from X",
"File was moved to Y",
"Subfile was created",
"Subfile was deleted",
"Self was deleted",
"Self was moved",
"",
"Backing fs was unmounted",
"Event queued overflowed",
"File was ignored"
};
#define EVENT_NUM 16
#define MAX_BUF_SIZE 1024

int main(void)
{
int fd;
int wd;
char buffer[1024];
char * offset = NULL;
struct inotify_event * event;
int len, tmp_len;
char strbuf[16];
int i = 0;

fd = inotify_init();
if (fd < 0) {
cout<<"Fail to initialize inotify."<<endl;
return -1;
}
for (i=0; i<WD_NUM; i++) {
wd_array[i].name = monitored_files[i];
wd = inotify_add_watch(fd, wd_array[i].name, IN_ALL_EVENTS);
if (wd < 0) {
cout<<"Can't add watch for"<<wd_array[i].name<<endl;
return -1;
}
wd_array[i].wd = wd;
}
while(len = read(fd, buffer, MAX_BUF_SIZE)) {
offset = buffer;
cout<<"Some event happens, len="<<len<<endl;
event = (struct inotify_event *)buffer;
while (((char *)event - buffer) < len) {
if (event->mask & IN_ISDIR) {
memcpy(strbuf, "Direcotory", 11);
}
else {
memcpy(strbuf, "File", 5);
}
cout<<"Object type: "<<strbuf<<endl;
for (i=0; i<WD_NUM; i++) {
if (event->wd != wd_array[i].wd) continue;
cout<<"Object name: "<<wd_array[i].name<<endl;
break;
}
cout<<"Event mask:"<<hex<<event->mask<<endl;
for (i=0; i<EVENT_NUM; i++) {
if (event_array[i][0] == '\0') continue;
if (event->mask & (1<<i)) {
cout<<"Event: "<<event_array[i]<<endl;
}
}
tmp_len = sizeof(struct inotify_event) + event->len;
event = (struct inotify_event *)(offset + tmp_len);
offset += tmp_len;
}
}
}

 

 

转载于:https://www.cnblogs.com/codershuai/archive/2012/10/24/2737445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值