Linux Use INotify Monitor file action __linux

Source: Internet
Author: User
Tags inotify
In everyday applications, you will often experience the following scenarios, monitor folder A, and execute the C command if the B file in the folder changes. Linux can perform this function through INotify.
Since Linux kernel 2.6.13, INotify is part of the kernel and requires GLIBC 2.4 or more versions.
1. Correlation function
Inotify_init ()-Create a INotify instance
Inotify_add_watch (int fd, const char *pathname, uint32_t Mask)-Add files or directories to inotify for monitoring
Inotify_rm_watch (int fd, int wd)-Remove a Watcher
2. Related structure
struct Inotify_event {int wd; uint32_t mask; uint32_t cookie; uint32_t len; char name[];
3. Mask
Suitable for Inotify_add_watch mask and read returned inotify_event mask
in_access file is accessed
In_attrib file properties changed
In_close_write Open the file as WRITE and close
In_close_nowrite Open the file and close it in a non-write way
In_create files or directories are created
In_delete files or directories are deleted (files in the monitored folder A are deleted)
In_delete_self the monitored files or directories are deleted (the monitored folder A is deleted)
In_modify file is modified
In_move_self the file or directory being monitored for movement
In_moved_from files are moved out of the monitored directory
In_moved_to file moved into the monitored directory
In_open file is open
The set of the above flag
In_all_events the collection of all flag above
In_move in_moved_to| In_moved_from
In_close in_close_write| In_close_nowrite
A flag that is not commonly used
In_dont_follow not FOLLOW symbolic link (since 2.6.15)
In_excl_unlink when the document is UNLINK from the monitor, the relevant event for the file is no longer reported, such as monitoring/tmp use (since 2.6.36)
In_mask_add chased MASK to the pathname that was monitored
In_oneshot is only monitored once
In_onlydir only Monitor Directory
Returned by Read only
In_ignored Inotify_rm_watch, files are deleted or file systems are Umount
The In_isdir event is a directory
In_q_overflow Event Queue Overflow
In_unmount File System Unmount
4. Examples
Purpose: To monitor all actions of the specified file or directory (or not specified as the current directory).
Use: inotify [file or directory]
View Plaincopy to Clipboardprint?
#include
#include
#include
#include
#include
#include
#define Error (Text) error (1, errno, "%s", text)
struct Eventmask {
int flag;
const char *name;
};
int freadsome (void *dest, size_t remain, FILE *file)
{
Char *offset = (char*) dest;
while (remain) {
int n = fread (offset, 1, remain, file);
if (n==0) {
return-1;
}
remain = n;
offset = n;
}
return 0;
}
int main (int argc, char *argv[])
{
const char *target;
if (argc = = 1) {
target = ".";
} else {
target = argv[1];
}
Eventmask event_masks[] = {
{in_access, "in_access"},
{in_attrib, "In_attrib"},
{in_close_write, "In_close_write"},
{in_close_nowrite, "In_close_nowrite"},
{in_create, "in_create"},
{in_delete, "In_delete"},
{in_delete_self, "in_delete_self"},
{in_modify, "in_modify"},
{in_move_self, "in_move_self"},
{in_moved_from, "In_moved_from"},
{in_moved_to, "in_moved_to"},
{in_open, "In_open"},
{in_dont_follow, "In_dont_follow"},
{in_excl_unlink, "In_excl_unlink"},
{in_mask_add, "In_mask_add"},
{in_oneshot, "in_oneshot"},
{in_onlydir, "In_onlydir"},
{in_ignored, "in_ignored"},
{in_isdir, "In_isdir"},
{in_q_overflow, "In_q_overflow"},
{in_unmount, "In_unmount"},
};
int monitor = Inotify_init ();
if (-1 = monitor) {
ERROR ("Monitor");
}
int watcher = Inotify_add_watch (monitor, Target, in_all_events);
if (-1 = Watcher) {
ERROR ("Inotify_add_watch");
}
FILE *monitor_file = Fdopen (monitor, "R");
Char last_name[1024];
Char name[1024];
while (true) {
Inotify_event event;
if (-1 = = Freadsome (&event, sizeof (event), Monitor_file)) {
ERROR ("Freadsome");
}
if (Event.len) {
Freadsome (name, Event.len, Monitor_file);
} else {
sprintf (Name, "FD:%d\n", EVENT.WD);
}
if (strcmp (name, last_name)!= 0) {
puts (name);
strcpy (last_name, name);
}
for (int i=0; i<</span>sizeof (event_masks)/sizeof (eventmask); ++i) {
if (Event.mask & Event_masks[i].flag) {
printf ("\t%s\n", event_masks[i].name);
}
}
}
return 0;
}
Run Result: The inotify.cc in the monitoring directory is saved in vim
4913 in_create in_open in_attrib in_close_write in_delete inotify.cc in_moved_from inotify.cc~ IN_MOVED_TO inotify.cc IN_ CREATE in_open in_modify in_close_write in_attrib inotify.cc~ in_delete
As you can see, Vim saves the file in the process
Create a 4913 file to detect Vim's control over the directory
Rename inotify.cc to inotify.cc~.
Create a new inotify.cc and write buffer
Delete inotify.cc~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.