monitoring file and directory events you will read about inotify in the Love or
ID: 3822980 • Letter: M
Question
monitoring file and directory events
you will read about inotify in the Love or Kerrisk textbooks, on read about inotify in section 7 of the man pages (man 7 inotfiy) to answer the following questions.
Explain in your own words the purpose of the inotify API. Give an example of where it could be used.
List the three major functions that implement and use an inotify instance. Give a short description of each function.
What functions can be used to access (read) inotify events?
How is an instance of inotify destroyed?
Explanation / Answer
Explain in your own words the purpose of the inotify API. Give an example of where it could be used.
The inotify API provides device for monitoring file-system events and it can be used to monitor individual files or to monitor directories and when monitoring a directory it will return events for the directory itself and for files inside the directory.
fd = inotify_init(); /*creating the INOTIFY instance*/
length = read( fd, buffer, EVENT_BUF_LEN ); /*read to determine the event change happens */
List the three major functions that implement and use an inotify instance. Give a short description of each function.
inotify_init(2) creates an inotify instance and returns a file descriptor referring to the inotify instance and the more recent inotify_init1(2) has a flags argument that provides access to some extra functionality.
inotify_add_watch(2) control the watch-list associated with an inotify instance and each item watch in the watch-list specifies the pathname of a file or directory along with some set of events that the kernel should monitor for the file referred to by that pathname.
inotify_rm_watch(2) removes an item from an inotify watch list.
What functions can be used to access (read) inotify events?
The inotify_add_watch(2) mask argument and the mask field of the inotify_event structure returned when read(2)ing an inotify file descriptor are both bit masks identifying inotify events. The following bits can be specified in mask when calling inotify_add_watch(2) and may be returned in the mask field returned by read(2):
IN_ACCESS (+)
File was accessed
IN_ATTRIB (*)
Metadata changed
IN_CLOSE_WRITE (+)
File opened for writing was closed
IN_CLOSE_NOWRITE (*)
File or directory 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 (+)
Generated for the directory containing the old filename when a file is renamed.
IN_MOVED_TO (+)
Generated for the directory containing the new filename when a file is renamed.
IN_OPEN (*)
File or directory was opened.
How is an instance of inotify destroyed?
The close() method destroys the inotify instance, all associated watches and empties all pending events in the queue.
IN_ACCESS (+)
File was accessed
IN_ATTRIB (*)
Metadata changed
IN_CLOSE_WRITE (+)
File opened for writing was closed
IN_CLOSE_NOWRITE (*)
File or directory 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 (+)
Generated for the directory containing the old filename when a file is renamed.
IN_MOVED_TO (+)
Generated for the directory containing the new filename when a file is renamed.
IN_OPEN (*)
File or directory was opened.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.