This next-gen, dnotify replacement meets file system event-monitoring needs in the 2.6 kernel
Level: Introductory |
Software Engineer, IBM Linux Test and Integration Center
12 Apr 2005
Inotify is a file system event-monitoring mechanism slated for inclusion in upcoming Linux™ kernels that is designed to serve as an effective replacement for dnotify, which was the de facto file-monitoring mechanism supported in older kernels. Inotify is a powerful, fine-grained, asynchronous mechanism ideally suited for a variety of file-monitoring needs including, but not limited to, security and performance. Learn how to install inotify and how to build a sample user-space application to respond to file system events.
File system event monitoring is a necessity for many types of programs ranging from file managers to security tools, but dnotify -- the standard in earlier kernels -- had limitations that left us hoping for something better. With that in mind, meet inotify, a more modern file system event-monitoring alternative.
Why inotify?
There are many reasons to use inotify instead of dnotify. The first is that dnotify requires opening one file descriptor for each directory that you intend to watch for changes. This can become quite costly when you are monitoring several directories at once, since it is possible to reach a per-process file descriptor limit.
Additionally, the file descriptor pins the directory, disallowing the backing device to be unmounted, which causes problems in scenarios involving removable media. When using inotify, if you are watching a file on a file system that is unmounted, the watch is automatically removed and you receive an unmount event.
The second reason dnotify is inferior to inotify is a bit more complex. It is important to note that straightforward file system-monitoring granularity using the dnotify infrastructure exists only at the directory level. For more fine-grained monitoring with dnotify, application programmers are forced to keep a cache of stat
structures relating to each directory being monitored. This user space cache of stat
structures is needed to determine exactly what change in the directory occurred when a notification signal is received. When notifications are obtained, a list of stat
structures is generated and compared with the last known state. Clearly, this technique is sub-optimal.
An additional advantage of inotify is that it uses a file descriptor as the basic interface to let application developers use select
and poll
to watch the device. This allows for efficient multiplexed I/O and integration with Glib's mainloop
. In contrast, dnotify uses signals with which programmers often have more difficulty and that they find to be less than elegant.
Inotify solves these issues by providing a more elegant API that uses minimal file descriptors and ensures finer granularity of monitoring. Communication with inotify is provided through a device node. For these reasons it should be your clear choice when monitoring files on Linux 2.6 platforms.
Brak komentarzy:
Prześlij komentarz