您的位置:首页 > 运维架构 > Linux

linux 监视文件系统inotify 测试

2016-04-20 15:20 459 查看
原址:http://blog.csdn.net/hepeng597/article/details/7792565

一、简介

Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除、读、写和卸载操作等。您还可以跟踪活动的源头和目标等细节。在实际项目中,如果项目带有配置文件,那么怎么让配置文件的改变和项目程序同步而不需要重启程序呢?一个明显的应用是:在一个程序中,使用Inotify监视它的配置文件,如果该配置文件发生了更改(更新,修改)时,Inotify会产生修改的事件给程序,应用程序就可以实现重新加载配置文件,检测哪些参数发生了变化,并在应用程序内存的一些变量做相应的修改。当然另一种方法可以是通过cgi注册命令,并通过命令更新内存数据及更新配置文件

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)

注:上面所说的文件也包括目录。

二、使用Inofity

要使用 inotify,您必须具备一台带有 2.6.13 或更新内核的 Linux 机器(以前的 Linux 内核版本使用更低级的文件监控器dnotify)。如果您不知道内核的版本,请转到 shell,输入
uname -a




您还可以检查机器的 /usr/include/sys/inotify.h 文件。如果它存在,表明您的内核支持 inotify。

使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视器(一个监视器 是一个路径和一组事件),然后使用
read()
方法从描述符获取事件信息。
read()
并不会用光整个周期,它在事件发生之前是被阻塞的。

更好的是,因为 inotify 通过传统的文件描述符工作,您可以利用传统的
select()
系统调用来被动地监控监视器和许多其他输入源。两种方法 — 阻塞文件描述符和使用
select()
— 都避免了繁忙轮询。

Inotify 提供 3 个系统调用,它们可以构建各种各样的文件系统监控器:

int fd = inotify_init()
在内核中创建 inotify 子系统的一个实例,成功的话将返回一个文件描述符,失败则返回 -1。就像其他系统调用一样,如果
inotify_init()
失败,请检查
errno
以获得诊断信息。

顾名思义, int wd =
inotify_add_watch(fd,path,mask)
用于添加监视器。每个监视器必须提供一个路径名和相关事件的列表(每个事件由一个常量指定,比如 IN_MODIFY)。要监控多个事件,只需在事件之间使用逻辑操作符或 — C 语言中的管道线(
|
)操作符。如果
inotify_add_watch()
成功,该调用会为已注册的监视器返回一个惟一的标识符;否则,返回
-1。使用这个标识符更改或删除相关的监视器。

int ret = inotify_rm_watch(fd, wd)
删除一个监视器。

此外,还需要
read()
close()
系统调用。如果描述符由
inotify_init()
生成,则调用
read()
等待警告。假设有一个典型的文件描述符,应用程序将阻塞对事件的接收,这些事件在流中表现为数据。文件描述符上的由
inotify_init()
生成的通用
close()
删除所有活动监视器,并释放与
inotify 实例相关联的所有内存(这里也用到典型的引用计数警告。与实例相关联的所有文件描述符必须在监视器和 inotify 消耗的内存被释放之前关闭)。

三、测试Inotify

在文件 /usr/include/sys/inotify.h. 中,您可以找到事件结构的定义,它是一种 C 结构,如下所示:

struct inotify_event
{
int wd; 		/* The watch descriptor */
uint32_t mask; 	/* Watch mask */
uint32_t cookie;	/* A cookie to tie two events together */
uint32_t len;		/* The length of the filename found in the name field */
char name __flexarr;	/* The name of the file, padding to the end with NULs */
}


结构中的char name是不占空间的,相当于char name[0],所以sizeof(struct inotify_event)长度是16,实际上该结构数据的总长度应该是16+len,数据是紧跟在uinit32_t len后面的数据。

测试代码如下:

#include <stdio.h>//printf
#include <string.h> //strcmp
#include <sys/inotify.h>//inotify_init inotify_add_watch....
#include <sys/select.h>//select timeval
#include <unistd.h>//close

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
#define ERR_EXIT(msg,flag)	{perror(msg);goto flag;}

int main( int argc, char **argv )
{
int length, i = 0;
int fd;
int wd;
char buffer[BUF_LEN];

if((fd = inotify_init()) < 0)
ERR_EXIT("inotify_init",inotify_init_err);

if( (wd = inotify_add_watch( fd, "/tmp",	IN_MODIFY | IN_CREATE | IN_DELETE ) ) < 0)
ERR_EXIT("inofity_add_watch", inotify_add_watch_err);

fd_set rfd;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;//10millsecond
while(true)
{
int retval;
FD_ZERO(&rfd);
FD_SET(fd, &rfd);
retval = select(fd + 1, &rfd, NULL, NULL, &tv);
if(retval == 0) continue;
else if(retval == -1)
ERR_EXIT("select",select_err);

// retval > 0
length = read( fd, buffer, BUF_LEN );
if(length < 0)
ERR_EXIT("read",read_err);

//length >= 0
int i = 0;
while ( i < length )
{
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len )
{
if ( event->mask & IN_CREATE )
{
if ( event->mask & IN_ISDIR )
printf( "The directory %s was created.\n", event->name );
else
printf( "The file %s was created.\n", event->name );
if(strcmp(event->name,"kill") == 0)
ERR_EXIT("success exit",success_exit);

}
else if ( event->mask & IN_DELETE )
{
if ( event->mask & IN_ISDIR )
printf( "The directory %s was deleted.\n", event->name );
else
printf( "The file %s was deleted.\n", event->name );
}
else if ( event->mask & IN_MODIFY )
{
if ( event->mask & IN_ISDIR )
printf( "The directory %s was modified.\n", event->name );
else
printf( "The file %s was modified.\n", event->name );
}
}else
{
//TODO
//when only a file(not directory) is specified by add watch function, event->len's value may be zero, we can handle it here
}
i += EVENT_SIZE + event->len;
}
}
success_exit:
( void ) inotify_rm_watch( fd, wd );
( void ) close( fd );
return 0;

read_err:
select_err:
inotify_add_watch_err:
( void ) inotify_rm_watch( fd, wd );
inotify_init_err:
( void ) close( fd );

return -1;
}


以上代码需要注意的地方:
1.如果在/tmp目录下touch kill文件,程序则会退出

2.如果只有一个add watch 一个file,那么这个file的更改产生的event事件中event->len是为0,需要额外的处理,此代码省略了具体的处理过程,以注释代替

3.如果监测的是文件或目录的更改,使用 echo "xxx" >> file,会产生一个event事件,而使用echo "xxx" > file 会产生两个event事件,查了相关的资料,可能是因为后者需要先清空file文件内容,造成第一次event事件,再将xxx写入file保存,造成了第二次的event事件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: