Inotify Example: Introduction to Inotify with a C Program Example
2015-09-06 06:29
459 查看
inotify utility is an effective tool to monitor and notify filesystem changes. You can specify a list of files and directories that needs to be monitored by inotify. This library is used by various other programs. For example, CPAN module Linux::Inotify
is developed based on this library.
Create inotify monitoring list. Add the desired directories/files to the inotify monitoring list. Monitoring list can be changed as and when needed
Request Inotify to report specific event changes to the monitoring list of files and directories. For example, request inotify to report ON ACCESS, ON OPEN, ON WRITING, ON CLOSE,etc.,
Following are the inotify functions and their corresponding roles.
Create the inotify instance by inotify_init().
Add all the directories to be monitored to the inotify list using inotify_add_watch() function.
To determine the events occurred, do the read() on the inotify instance. This read will get blocked till the change event occurs. It is recommended to perform selective read on this inotify instance using
select() call.
Read returns list of events occurred on the monitored directories. Based on the return value of read(), we will know exactly what kind of changes occurred.
In case of removing the watch on directories / files, call inotify_rm_watch().
Be careful when using this module with NFS filesystem. It might not determine the events changes to the monitoring list that contains files/directories from the NFS filesystem.
IN_ACCESS – File was accessed
IN_ATTRIB – Metadata changed (permissions, timestamps, extended attributes, etc.)
IN_CLOSE_WRITE – File opened for writing was closed
IN_CLOSE_NOWRITE – File not opened for writing was closed
IN_CREATE – File/directory created in watched directory
IN_DELETE – File/directory deleted from watched directory
IN_DELETE_SELF – Watched file/directory was itself deleted
IN_MODIFY – File was modified
IN_MOVE_SELF – Watched file/directory was itself moved
IN_MOVED_FROM – File moved out of watched directory
IN_MOVED_TO – File moved into watched directory
IN_OPEN – File was opened
Check the libc6 version on your system and upgrade it if required.
http://www.thegeekstuff.com/2010/04/inotify-c-program-example/
is developed based on this library.
iNotify Execution Flow
On a high-level, you do the following with inotify utility.Create inotify monitoring list. Add the desired directories/files to the inotify monitoring list. Monitoring list can be changed as and when needed
Request Inotify to report specific event changes to the monitoring list of files and directories. For example, request inotify to report ON ACCESS, ON OPEN, ON WRITING, ON CLOSE,etc.,
Following are the inotify functions and their corresponding roles.
Create the inotify instance by inotify_init().
Add all the directories to be monitored to the inotify list using inotify_add_watch() function.
To determine the events occurred, do the read() on the inotify instance. This read will get blocked till the change event occurs. It is recommended to perform selective read on this inotify instance using
select() call.
Read returns list of events occurred on the monitored directories. Based on the return value of read(), we will know exactly what kind of changes occurred.
In case of removing the watch on directories / files, call inotify_rm_watch().
Be careful when using this module with NFS filesystem. It might not determine the events changes to the monitoring list that contains files/directories from the NFS filesystem.
Inotify Events
Following are the available inotify events:IN_ACCESS – File was accessed
IN_ATTRIB – Metadata changed (permissions, timestamps, extended attributes, etc.)
IN_CLOSE_WRITE – File opened for writing was closed
IN_CLOSE_NOWRITE – File not opened for writing was closed
IN_CREATE – File/directory created in watched directory
IN_DELETE – File/directory deleted from watched directory
IN_DELETE_SELF – Watched file/directory was itself deleted
IN_MODIFY – File was modified
IN_MOVE_SELF – Watched file/directory was itself moved
IN_MOVED_FROM – File moved out of watched directory
IN_MOVED_TO – File moved into watched directory
IN_OPEN – File was opened
Recommended modules / libraries for iNotify
Make sure libc6 2.3.6 module is installed on your system. If you have a previous version of libc module installed, you will get the following error message while compiling the inotify monitoring c program.error: linux/inotify.h: No such file or directory
Check the libc6 version on your system and upgrade it if required.
Sample C program for Monitoring of File/directory changes event
/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory*/ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <linux/inotify.h> #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main() { int length, i = 0; int fd; int wd; char buffer[EVENT_BUF_LEN]; /*creating the INOTIFY instance */ fd = inotify_init(); /*checking for error */ if (fd < 0) { perror("inotify_init"); } /*adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list. */ wd = inotify_add_watch(fd, "/tmp", IN_CREATE | IN_DELETE); /*read to determine the event change happens on “/tmp” directory. Actually this read blocks until the change event occurs */ length = read(fd, buffer, EVENT_BUF_LEN); /*checking for error */ if (length < 0) { perror("read"); } /*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly. */ 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("New directory %s created.\n", event->name); } else { printf("New file %s created.\n", event->name); } } else if (event->mask & IN_DELETE) { if (event->mask & IN_ISDIR) { printf("Directory %s deleted.\n", event->name); } else { printf("File %s deleted.\n", event->name); } } } i += EVENT_SIZE + event->len; } /*removing the “/tmp” directory from the watch list. */ inotify_rm_watch(fd, wd); /*closing the INOTIFY instance */ close(fd); }
http://www.thegeekstuff.com/2010/04/inotify-c-program-example/
相关文章推荐
- Python中使用Inotify监控文件实例
- linux系统中通过rsync+inotify实现网页自动同步
- linux系统中rsync+inotify实现服务器之间文件实时同步
- 通过rsync+inotify实现数据的实时备份配置
- linux下通过rsync+inotify 实现数据实时备份(远程容灾备份系统)
- nginx+rsync+inotify实现负载均衡配置方法
- rsync+inotify
- rsync+inotify
- 使用rsync+inotify配置触发式(实时)远程同步
- rsync+inotify实现实时同步案例
- 使用rsync+inotify做双机实时互备
- 利用rsync+inotify实现数据同步
- 通过rsync+inotify实现数据的实时备份
- 通过rsync+inotify实现数据的实时备份
- rsync+inotify配置
- inotify报错upper limit on inotify watches reached
- linux下两台服务器文件实时同步方案设计和实现
- linux下两台服务器文件实时同步方案设计和实现
- 在linux环境下利用rsync和inotify实现单向实时同步
- 通过rsync+inotify实现数据的实时同步