Linux file system change notification mechanism-inotify__linux

Source: Internet
Author: User
Tags inotify
Overview

Inotify-a powerful yet simple file change notification system.
INotify is a feature that the Linux kernel 2.6.13 later supports, which is to monitor changes in the file system and to monitor changes in the file system
Later, a change event is sent to the appropriate application.
INotify is a file system change notification mechanism, such as file additions, deletions and other events can immediately let users know that the mechanism is well-known
Desktop Search engine Project Beagle introduced.

Monitoring Events

Header files: #include <sys/inotify.h>

Supported events suitable for MASK parameter of Inotify_add_watch.

/* File was accessed. File is accessed. * * #define IN_ACCESS 0x00000001/* FIle was modified. The file was modified. * * #define IN_MODIFY 0x00000002/* Metadata changed. File attributes are modified, such as chmod, Chown, touch, and so on. * * #define IN_ATTRIB 0x00000004/* writtable file was closed. Writable files are closed. * * #define IN_CLOSE_WRITE 0x00000008/* unwrittable file closed. Non-writable files are closed. * * #define IN_CLOSE_NOWRITE 0x00000010/* file is closed. * * #define IN_CLOSE (In_close_write | In_close_nowrite)/* File was opened. The file is open. * * #define IN_OPEN 0x00000020/* file is moved from X. files are removed, such as MV. * * #define IN_MOVED_FROM 0x00000040/* file is moved to Y. Files are moved, such as MV, CP. * * #define IN_MOVED_TO 0x00000080/* moves. File is moved. * * #define IN_MOVED (In_moved_from | IN_MOVED_TO)/* Subfile was created. Create a new file. * * #define IN_CREATE 0x00000100/* subfile was deleted. Files are deleted, such as RM. * * #define IN_DELETE 0x00000200/* Self was deleted. Since deletion, that is, an executable file deletes itself at execution time.  * * #define IN_DELETE_SELF 0x00000400/* SELF was moved. Self-moving, that is, an executable file that moves itself when executed. * * #define IN_MOVE_SELF 0x00000800

Events sent by the kernel.

/* Backing FS was unmounted. The host file system is unmount. * *
#define IN_UNMOUNT 0x00002000/* Event

queued overflowed. In the kernel, the data for events exceeds the
 inotify_device in the * max_events. * *
#define IN_Q_OVERFLOW 0x00004000/* file was

ignored. Indicates that the system has deleted the watch from the INotify instance
 because the file has been deleted. * *
#define IN_IGNORED 0x00008000  

Special flags.

/* Only watch the path if it is a directory. * *
#define IN_ONLYDIR 0x01000000/* Does not 

follow a sym link. */
#define IN_DONT_FOLLOW 0x02000000/

* Ad D to the mask of a already existing watch. * *
#define IN_MASK_ADD 0x20000000/ 

* Event occurred against dir. */
#define IN_ISDIR 0x40000000/ 

* Only send event once. * *
#define IN_ONESHOT 0x80000000

All events which a can be wait on.

#define In_all_events (in_access | in_modify | In_attrib | In_close_write \
    | In_close_nowrite | In_open | In_moved_from | in_moved_to \
    | In_create | In_delete | in_delete_self | In_move_self)

API Functions

INotify mainly provides the following APIs.

(1) Create inotify Entities

/* Create and Initialize inotify instance. *
/extern int inotify_init (void) __throw;

Inotify_init () creates an entity in the kernel: Inotify_device, and returns a file descriptor.
Use: int fd = Inotify_init ();

(2) Create a Monitor

/* Add watch of object NAME to inotify instance FD. Notify about Events 
 * specified by MASK. 
 * *
extern int inotify_add_watch (int __fd, const char * __name, uint32_t __mask) 
    __throw;

Inotify_add_watch is used to add a monitor to the list of monitors in Inotify_device: Inotify_watch.
Create a monitor to provide:
(1) INotify instance inotify_device file descriptor: FD
(2) Monitoring the target path: name
(3) Monitor Events list: Mask
If successful, return the monitor descriptor WD, or return-1.

(3) Remove monitor

/* Remove the watch specified by WD from the INotify instance FD. * *
extern int inotify_rm_watch (int __fd, uint32_t __wd) __throw;

Use to remove a monitor from the list of Inotify_device monitors.

Read Events

In order to determine which file system events occur, read system calls are required to fetch the file descriptor returned by Inotify_init ().
Read () returns one or more inotify_event.

/* Structure describing an inotify event. * *

struct inotify_event
{
    int wd;/* Watch descriptor. * * unit32_t mask
    ;/* Watch mask/
    unit32_t COO Kie; /* Cookies to synchronize two events. *
    /unit32_t len/* Length (including NULLs) of name. */
    char name __flexarr;/* name. */
};

Inotify_event is a file system event.
Where WD is the watch descriptor for the monitored target, mask is the event mask, name is the path name for the monitoring target,
Len is the length of name.
The structure size of each notify_event is: sizeof (struct inotify_event) + len.
Through the inotify_event can be seen:

(1) Monitoring target (what file/directory)-> Name,len
(2) Monitor-> WD
(3) What events are monitoring the target-> mask

With read () You can get multiple events at once, provided the BUF is large enough.
size_t len = Read (FD, buf, max_buf_size);
Len for the actual obtained event lumped length.
You can use Select () or poll (), Epoll (), or use the IOCTL command on the FD of the file descriptor returned by the function inotify ().
Fionread to get the length of the current queue.
Close (FD) deletes all watch added to the FD and makes the necessary cleanup.

Kernel Implementation Brief

Each inotify instance are represented by a inotify_handle structure.
Inotify ' s userspace consumers also have an inotify_device which
are associated with the Inotify_handle, and on which EV Ents are queued.
Each watch are associated with a inotify_watch structure. Watches are chained off of the
associated Inotify_handle and each associated inode.
Fs/notify/inotify/inotify_fsnotify.c and FS/NOTIFY/INOTIFY/INOTIFY_USER.C for the
locking and lifetime rules.

Whether it is a directory or a file, in the kernel corresponds to an INODE structure, inotify system again inode structure added two fields:
#ifdef config_inotify
struct List_head inotify_watches; /* Watches on this inode * *
struct semaphore Inotify_sem; /* Protects the watches list * *
#endif

Reference

1 documentation \ filesystems \ inotify.txt
2. include \ linux \ inotify.h
3. Http://www.ibm.com/developerwork s/cn/linux/l-inotifynew/
4. http://tianyapiaozi.blogbus.com/logs/61783047.html

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.