linux文件监控(网站拷贝英文仅转存浏览)


NAME | DESCRIPTION | VERSIONS | CONFORMING TO | NOTES | BUGS | EXAMPLE | SEE ALSO | COLOPHON

 
INOTIFY(7)                Linux Programmer's Manual               INOTIFY(7)

NAME         top

       inotify - monitoring filesystem events

DESCRIPTION         top

       The inotify API provides a mechanism for monitoring filesystem
       events.  Inotify can be used to monitor individual files, or to
       monitor directories.  When a directory is monitored, inotify will
       return events for the directory itself, and for files inside the
       directory.

       The following system calls are used with this API:

       *  inotify_init(2) creates an inotify instance and returns a file
          descriptor referring to the inotify instance.  The more recent
          inotify_init1(2) is like inotify_init(2), but has a flags argument
          that provides access to some extra functionality.

       *  inotify_add_watch(2) manipulates the "watch list" associated with
          an inotify instance.  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_add_watch(2) either creates a new watch
          item, or modifies an existing watch.  Each watch has a unique
          "watch descriptor", an integer returned by inotify_add_watch(2)
          when the watch is created.

       *  When events occur for monitored files and directories, those
          events are made available to the application as structured data
          that can be read from the inotify file descriptor using read(2)
          (see below).

       *  inotify_rm_watch(2) removes an item from an inotify watch list.

       *  When all file descriptors referring to an inotify instance have
          been closed (using close(2)), the underlying object and its
          resources are freed for reuse by the kernel; all associated
          watches are automatically freed.

          With careful programming, an application can use inotify to
          efficiently monitor and cache the state of a set of filesystem
          objects.  However, robust applications should allow for the fact
          that bugs in the monitoring logic or races of the kind described
          below may leave the cache inconsistent with the filesystem state.
          It is probably wise to to do some consistency checking, and
          rebuild the cache when inconsistencies are detected.

   Reading events from an inotify file descriptor
       To determine what events have occurred, an application read(2)s from
       the inotify file descriptor.  If no events have so far occurred,
       then, assuming a blocking file descriptor, read(2) will block until
       at least one event occurs (unless interrupted by a signal, in which
       case the call fails with the error EINTR; see signal(7)).

       Each successful read(2) returns a buffer containing one or more of
       the following structures:

           struct inotify_event {
               int      wd;       /* Watch descriptor */
               uint32_t mask;     /* Mask of events */
               uint32_t cookie;   /* Unique cookie associating related
                                     events (for rename(2)) */
               uint32_t len;      /* Size of name field */
               char     name[];   /* Optional null-terminated name */
           };

       wd identifies the watch for which this event occurs.  It is one of
       the watch descriptors returned by a previous call to
       inotify_add_watch(2).

       mask contains bits that describe the event that occurred (see below).

       cookie is a unique integer that connects related events.  Currently
       this is used only for rename events, and allows the resulting pair of
       IN_MOVED_FROM and IN_MOVED_TO events to be connected by the
       application.  For all other event types, cookie is set to 0.

       The name field is present only when an event is returned for a file
       inside a watched directory; it identifies the file pathname relative
       to the watched directory.  This pathname is null-terminated, and may
       include further null bytes ('\0') to align subsequent reads to a
       suitable address boundary.

       The len field counts all of the bytes in name, including the null
       bytes; the length of each inotify_event structure is thus
       sizeof(struct inotify_event)+len.

       The behavior when the buffer given to read(2) is too small to return
       information about the next event depends on the kernel version: in
       kernels before 2.6.21, read(2) returns 0; since kernel 2.6.21,
       read(2) fails with the error EINVAL.  Specifying a buffer of size

           sizeof(struct inotify_event) + NAME_MAX + 1

       will be sufficient to read at least one event.

   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 (e.g., read(2), execve(2)).

           IN_ATTRIB (*)
                  Metadata changed—for example, permissions (e.g.,
                  chmod(2)), timestamps (e.g., utimensat(2)), extended
                  attributes (setxattr(2)), link count (since Linux 2.6.25;
                  e.g., for the target of link(2) and for unlink(2)), and
                  user/group ID (e.g., chown(2)).

           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 (e.g., open(2)
                  O_CREAT, mkdir(2), link(2), symlink(2), bind(2) on a UNIX
                  domain socket).

           IN_DELETE (*)
                  File/directory deleted from watched directory.

           IN_DELETE_SELF
                  Watched file/directory was itself deleted.  (This event
                  also occurs if an object is moved to another filesystem,
                  since mv(1) in effect copies the file to the other
                  filesystem and then deletes it from the original
                  filesystem.)  In addition, an IN_IGNORED event will
                  subsequently be generated for the watch descriptor.

           IN_MODIFY (*)
                  File was modified (e.g., write(2), truncate(2)).

           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 was opened.

       When monitoring a directory, the events marked with an asterisk (*)
       above can occur for files in the directory, in which case the name
       field in the returned inotify_event structure identifies the name of
       the file within the directory.

       The IN_ALL_EVENTS macro is defined as a bit mask of all of the above
       events.  This macro can be used as the mask argument when calling
       inotify_add_watch(2).

       Two additional convenience macros are defined:

           IN_MOVE
                  Equates to IN_MOVED_FROM | IN_MOVED_TO.

           IN_CLOSE
                  Equates to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE.

       The following further bits can be specified in mask when calling
       inotify_add_watch(2):

           IN_DONT_FOLLOW (since Linux 2.6.15)
                  Don't dereference pathname if it is a symbolic link.

           IN_EXCL_UNLINK (since Linux 2.6.36)
                  By default, when watching events on the children of a
                  directory, events are generated for children even after
                  they have been unlinked from the directory.  This can
                  result in large numbers of uninteresting events for some
                  applications (e.g., if watching /tmp, in which many
                  applications create temporary files whose names are
                  immediately unlinked).  Specifying IN_EXCL_UNLINK changes
                  the default behavior, so that events are not generated for
                  children after they have been unlinked from the watched
                  directory.

           IN_MASK_ADD
                  Add (OR) events to watch mask for this pathname if it
                  already exists (instead of replacing mask).

           IN_ONESHOT
                  Monitor pathname for one event, then remove from watch
                  list.

           IN_ONLYDIR (since Linux 2.6.15)
                  Only watch pathname if it is a directory.

       The following bits may be set in the mask field returned by read(2):

           IN_IGNORED
                  Watch was removed explicitly (inotify_rm_watch(2)) or
                  automatically (file was deleted, or filesystem was
                  unmounted).  See also BUGS.

           IN_ISDIR
                  Subject of this event is a directory.

           IN_Q_OVERFLOW
                  Event queue overflowed (wd is -1 for this event).

           IN_UNMOUNT
                  Filesystem containing watched object was unmounted.  In
                  addition, an IN_IGNORED event will subsequently be
                  generated for the watch descriptor.

   Examples
       Suppose an application is watching the directory dir and the file
       dir/myfile for all events.  The examples below show some events that
       will be generated for these two objects.

           fd = open("dir/myfile", O_RDWR);
                  Generates IN_OPEN events for both dir and dir/myfile.

           read(fd, buf, count);
                  Generates IN_ACCESS events for both dir and dir/myfile.

           write(fd, buf, count);
                  Generates IN_MODIFY events for both dir and dir/myfile.

           fchmod(fd, mode);
                  Generates IN_ATTRIB events for both dir and dir/myfile.

           close(fd);
                  Generates IN_CLOSE_WRITE events for both dir and
                  dir/myfile.

       Suppose an application is watching the directories dir1 and dir2, and
       the file dir1/myfile.  The following examples show some events that
       may be generated.

           link("dir1/myfile", "dir2/new");
                  Generates an IN_ATTRIB event for myfile and an IN_CREATE
                  event for dir2.

           rename("dir1/myfile", "dir2/myfile");
                  Generates an IN_MOVED_FROM event for dir1, an IN_MOVED_TO
                  event for dir2, and an IN_MOVE_SELF event for myfile.  The
                  IN_MOVED_FROM and IN_MOVED_TO events will have the same
                  cookie value.

       Suppose that dir1/xx and dir2/yy are (the only) links to the same
       file, and an application is watching dir1, dir2, dir1/xx, and
       dir2/yy.  Executing the following calls in the order given below will
       generate the following events:

           unlink("dir2/yy");
                  Generates an IN_ATTRIB event for xx (because its link
                  count changes) and an IN_DELETE event for dir2.

           unlink("dir1/xx");
                  Generates IN_ATTRIB, IN_DELETE_SELF, and IN_IGNORED events
                  for xx, and an IN_DELETE event for dir1.

       Suppose an application is watching the directory dir and (the empty)
       directory dir/subdir.  The following examples show some events that
       may be generated.

           mkdir("dir/new", mode);
                  Generates an IN_CREATE | IN_ISDIR event for dir.

           rmdir("dir/subdir");
                  Generates IN_DELETE_SELF and IN_IGNORED events for subdir,
                  and an IN_DELETE | IN_ISDIR event for dir.

   /proc interfaces
       The following interfaces can be used to limit the amount of kernel
       memory consumed by inotify:

       /proc/sys/fs/inotify/max_queued_events
              The value in this file is used when an application calls
              inotify_init(2) to set an upper limit on the number of events
              that can be queued to the corresponding inotify instance.
              Events in excess of this limit are dropped, but an
              IN_Q_OVERFLOW event is always generated.

       /proc/sys/fs/inotify/max_user_instances
              This specifies an upper limit on the number of inotify
              instances that can be created per real user ID.

       /proc/sys/fs/inotify/max_user_watches
              This specifies an upper limit on the number of watches that
              can be created per real user ID.

VERSIONS         top

       Inotify was merged into the 2.6.13 Linux kernel.  The required
       library interfaces were added to glibc in version 2.4.
       (IN_DONT_FOLLOW, IN_MASK_ADD, and IN_ONLYDIR were added in glibc
       version 2.5.)

CONFORMING TO         top

       The inotify API is Linux-specific.

NOTES         top

       Inotify file descriptors can be monitored using select(2), poll(2),
       and epoll(7).  When an event is available, the file descriptor
       indicates as readable.

       Since Linux 2.6.25, signal-driven I/O notification is available for
       inotify file descriptors; see the discussion of F_SETFL (for setting
       the O_ASYNC flag), F_SETOWN, and F_SETSIG in fcntl(2).  The siginfo_t
       structure (described in sigaction(2)) that is passed to the signal
       handler has the following fields set: si_fd is set to the inotify
       file descriptor number; si_signo is set to the signal number; si_code
       is set to POLL_IN; and POLLIN is set in si_band.

       If successive output inotify events produced on the inotify file
       descriptor are identical (same wd, mask, cookie, and name), then they
       are coalesced into a single event if the older event has not yet been
       read (but see BUGS).  This reduces the amount of kernel memory
       required for the event queue, but also means that an application
       can't use inotify to reliably count file events.

       The events returned by reading from an inotify file descriptor form
       an ordered queue.  Thus, for example, it is guaranteed that when
       renaming from one directory to another, events will be produced in
       the correct order on the inotify file descriptor.

       The FIONREAD ioctl(2) returns the number of bytes available to read
       from an inotify file descriptor.

   Limitations and caveats
       The inotify API provides no information about the user or process
       that triggered the inotify event.  In particular, there is no easy
       way for a process that is monitoring events via inotify to
       distinguish events that it triggers itself from those that are
       triggered by other processes.

       Inotify reports only events that a user-space program triggers
       through the filesystem API.  As a result, it does not catch remote
       events that occur on network filesystems.  (Applications must fall
       back to polling the filesystem to catch such events.)  Furthermore,
       various pseudo-filesystems such as /proc, /sys, and /dev/pts are not
       monitorable with inotify.

       The inotify API does not report file accesses and modifications that
       may occur because of mmap(2), msync(2), and munmap(2).

       The inotify API identifies affected files by filename.  However, by
       the time an application processes an inotify event, the filename may
       already have been deleted or renamed.

       The inotify API identifies events via watch descriptors.  It is the
       application's responsibility to cache a mapping (if one is needed)
       between watch descriptors and pathnames.  Be aware that directory
       renamings may affect multiple cached pathnames.

       Inotify monitoring of directories is not recursive: to monitor
       subdirectories under a directory, additional watches must be created.
       This can take a significant amount time for large directory trees.

       If monitoring an entire directory subtree, and a new subdirectory is
       created in that tree or an existing directory is renamed into that
       tree, be aware that by the time you create a watch for the new
       subdirectory, new files (and subdirectories) may already exist inside
       the subdirectory.  Therefore, you may want to scan the contents of
       the subdirectory immediately after adding the watch (and, if desired,
       recursively add watches for any subdirectories that it contains).

       Note that the event queue can overflow.  In this case, events are
       lost.  Robust applications should handle the possibility of lost
       events gracefully.  For example, it may be necessary to rebuild part
       or all of the application cache.  (One simple, but possibly
       expensive, approach is to close the inotify file descriptor, empty
       the cache, create a new inotify file descriptor, and then re-create
       watches and cache entries for the objects to be monitored.)

   Dealing with rename() events
       As noted above, the IN_MOVED_FROM and IN_MOVED_TO event pair that is
       generated by rename(2) can be matched up via their shared cookie
       value.  However, the task of matching has some challenges.

       These two events are usually consecutive in the event stream
       available when reading from the inotify file descriptor.  However,
       this is not guaranteed.  If multiple processes are triggering events
       for monitored objects, then (on rare occasions) an arbitrary number
       of other events may appear between the IN_MOVED_FROM and IN_MOVED_TO
       events.

       Matching up the IN_MOVED_FROM and IN_MOVED_TO event pair generated by
       rename(2) is thus inherently racy.  (Don't forget that if an object
       is renamed outside of a monitored directory, there may not even be an
       IN_MOVED_TO event.)  Heuristic approaches (e.g., assume the events
       are always consecutive) can be used to ensure a match in most cases,
       but will inevitably miss some cases, causing the application to
       perceive the IN_MOVED_FROM and IN_MOVED_TO events as being unrelated.
       If watch descriptors are destroyed and re-created as a result, then
       those watch descriptors will be inconsistent with the watch
       descriptors in any pending events.  (Re-creating the inotify file
       descriptor and rebuilding the cache may be useful to deal with this
       scenario.)

       Applications should also allow for the possibility that the
       IN_MOVED_FROM event was the last event that could fit in the buffer
       returned by the current call to read(2), and the accompanying
       IN_MOVED_TO event might be fetched only on the next read(2).

BUGS         top

       In kernels before 2.6.16, the IN_ONESHOT mask flag does not work.

       As originally designed and implemented, the IN_ONESHOT flag did not
       cause an IN_IGNORED event to be generated when the watch was dropped
       after one event.  However, as an unintended effect of other changes,
       since Linux 2.6.36, an IN_IGNORED event is generated in this case.

       Before kernel 2.6.25, the kernel code that was intended to coalesce
       successive identical events (i.e., the two most recent events could
       potentially be coalesced if the older had not yet been read) instead
       checked if the most recent event could be coalesced with the oldest
       unread event.

EXAMPLE         top

       The following program demonstrates the usage of the inotify API.  It
       marks the directories passed as a command-line arguments and waits
       for events of type IN_OPEN, IN_CLOSE_NOWRITE and IN_CLOSE_WRITE.

       The following output was recorded while editing the file
       /home/user/temp/foo and listing directory /tmp.  Before the file and
       the directory were opened, IN_OPEN events occurred.  After the file
       was closed, an IN_CLOSE_WRITE event occurred.  After the directory
       was closed, an IN_CLOSE_NOWRITE event occurred.  Execution of the
       program ended when the user pressed the ENTER key.

   Example output
           $ ./a.out /tmp /home/user/temp
           Press enter key to terminate.
           Listening for events.
           IN_OPEN: /home/user/temp/foo [file]
           IN_CLOSE_WRITE: /home/user/temp/foo [file]
           IN_OPEN: /tmp/ [directory]
           IN_CLOSE_NOWRITE: /tmp/ [directory]

           Listening for events stopped.

   Program source
       #include <errno.h>
       #include <poll.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <sys/inotify.h>
       #include <unistd.h>

       /* Read all available inotify events from the file descriptor 'fd'.
          wd is the table of watch descriptors for the directories in argv.
          argc is the length of wd and argv.
          argv is the list of watched directories.
          Entry 0 of wd and argv is unused. */

       static void
       handle_events(int fd, int *wd, int argc, char* argv[])
       {
           /* Some systems cannot read integer variables if they are not
              properly aligned. On other systems, incorrect alignment may
              decrease performance. Hence, the buffer used for reading from
              the inotify file descriptor should have the same alignment as
              struct inotify_event. */

           char buf[4096]
               __attribute__ ((aligned(__alignof__(struct inotify_event))));
           const struct inotify_event *event;
           int i;
           ssize_t len;
           char *ptr;

           /* Loop while events can be read from inotify file descriptor. */

           for (;;) {

               /* Read some events. */

               len = read(fd, buf, sizeof buf);
               if (len == -1 && errno != EAGAIN) {
                   perror("read");
                   exit(EXIT_FAILURE);
               }

               /* If the nonblocking read() found no events to read, then
                  it returns -1 with errno set to EAGAIN. In that case,
                  we exit the loop. */

               if (len <= 0)
                   break;

               /* Loop over all events in the buffer */

               for (ptr = buf; ptr < buf + len;
                       ptr += sizeof(struct inotify_event) + event->len) {

                   event = (const struct inotify_event *) ptr;

                   /* Print event type */

                   if (event->mask & IN_OPEN)
                       printf("IN_OPEN: ");
                   if (event->mask & IN_CLOSE_NOWRITE)
                       printf("IN_CLOSE_NOWRITE: ");
                   if (event->mask & IN_CLOSE_WRITE)
                       printf("IN_CLOSE_WRITE: ");

                   /* Print the name of the watched directory */

                   for (i = 1; i < argc; ++i) {
                       if (wd[i] == event->wd) {
                           printf("%s/", argv[i]);
                           break;
                       }
                   }

                   /* Print the name of the file */

                   if (event->len)
                       printf("%s", event->name);

                   /* Print type of filesystem object */

                   if (event->mask & IN_ISDIR)
                       printf(" [directory]\n");
                   else
                       printf(" [file]\n");
               }
           }
       }

       int
       main(int argc, char* argv[])
       {
           char buf;
           int fd, i, poll_num;
           int *wd;
           nfds_t nfds;
           struct pollfd fds[2];

           if (argc < 2) {
               printf("Usage: %s PATH [PATH ...]\n", argv[0]);
               exit(EXIT_FAILURE);
           }

           printf("Press ENTER key to terminate.\n");

           /* Create the file descriptor for accessing the inotify API */

           fd = inotify_init1(IN_NONBLOCK);
           if (fd == -1) {
               perror("inotify_init1");
               exit(EXIT_FAILURE);
           }

           /* Allocate memory for watch descriptors */

           wd = calloc(argc, sizeof(int));
           if (wd == NULL) {
               perror("calloc");
               exit(EXIT_FAILURE);
           }

           /* Mark directories for events
              - file was opened
              - file was closed */

           for (i = 1; i < argc; i++) {
               wd[i] = inotify_add_watch(fd, argv[i],
                                         IN_OPEN | IN_CLOSE);
               if (wd[i] == -1) {
                   fprintf(stderr, "Cannot watch '%s'\n", argv[i]);
                   perror("inotify_add_watch");
                   exit(EXIT_FAILURE);
               }
           }

           /* Prepare for polling */

           nfds = 2;

           /* Console input */

           fds[0].fd = STDIN_FILENO;
           fds[0].events = POLLIN;

           /* Inotify input */

           fds[1].fd = fd;
           fds[1].events = POLLIN;

           /* Wait for events and/or terminal input */

           printf("Listening for events.\n");
           while (1) {
               poll_num = poll(fds, nfds, -1);
               if (poll_num == -1) {
                   if (errno == EINTR)
                       continue;
                   perror("poll");
                   exit(EXIT_FAILURE);
               }

               if (poll_num > 0) {

                   if (fds[0].revents & POLLIN) {

                       /* Console input is available. Empty stdin and quit */

                       while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\n')
                           continue;
                       break;
                   }

                   if (fds[1].revents & POLLIN) {

                       /* Inotify events are available */

                       handle_events(fd, wd, argc, argv);
                   }
               }
           }

           printf("Listening for events stopped.\n");

           /* Close inotify file descriptor */

           close(fd);

           free(wd);
           exit(EXIT_SUCCESS);
       }

SEE ALSO         top

       inotifywait(1), inotifywatch(1), inotify_add_watch(2),
       inotify_init(2), inotify_init1(2), inotify_rm_watch(2), read(2),
       stat(2), fanotify(7)

       Documentation/filesystems/inotify.txt in the Linux kernel source tree

COLOPHON         top

       This page is part of release 3.69 of the Linux man-pages project.  A
       description of the project, information about reporting bugs, and the
       latest version of this page, can be found at
       http://www.kernel.org/doc/man-pages/.

Linux                            2014-05-23                       INOTIFY(7)



NAME | SYNOPSIS | DESCRIPTION | RETURN VALUE | ERRORS | VERSIONS | CONFORMING TO | SEE ALSO | COLOPHON

 

INOTIFY_ADD_WATCH(2)      Linux Programmer's Manual     INOTIFY_ADD_WATCH(2)

NAME         top

       inotify_add_watch - add a watch to an initialized inotify instance

SYNOPSIS         top

       #include <sys/inotify.h>

       int inotify_add_watch(int fd, const char *pathname, uint32_t mask);

DESCRIPTION         top

       inotify_add_watch() adds a new watch, or modifies an existing watch,
       for the file whose location is specified in pathname; the caller must
       have read permission for this file.  The fd argument is a file
       descriptor referring to the inotify instance whose watch list is to
       be modified.  The events to be monitored for pathname are specified
       in the mask bit-mask argument.  See inotify(7) for a description of
       the bits that can be set in mask.

       A successful call to inotify_add_watch() returns the unique watch
       descriptor associated with pathname for this inotify instance.  If
       pathname was not previously being watched by this inotify instance,
       then the watch descriptor is newly allocated.  If pathname was
       already being watched, then the descriptor for the existing watch is
       returned.

       The watch descriptor is returned by later read(2)s from the inotify
       file descriptor.  These reads fetch inotify_event structures (see
       inotify(7)) indicating filesystem events; the watch descriptor inside
       this structure identifies the object for which the event occurred.

RETURN VALUE         top

       On success, inotify_add_watch() returns a nonnegative watch
       descriptor.  On error, -1 is returned and errno is set appropriately.

ERRORS         top

       EACCES Read access to the given file is not permitted.

       EBADF  The given file descriptor is not valid.

       EFAULT pathname points outside of the process's accessible address
              space.

       EINVAL The given event mask contains no valid events; or fd is not an
              inotify file descriptor.

       ENAMETOOLONG
              pathname is too long.

       ENOENT A directory component in pathname does not exist or is a
              dangling symbolic link.

       ENOMEM Insufficient kernel memory was available.

       ENOSPC The user limit on the total number of inotify watches was
              reached or the kernel failed to allocate a needed resource.

VERSIONS         top

       Inotify was merged into the 2.6.13 Linux kernel.

CONFORMING TO         top

       This system call is Linux-specific.

SEE ALSO         top

       inotify_init(2), inotify_rm_watch(2), inotify(7)

COLOPHON         top

       This page is part of release 3.69 of the Linux man-pages project.  A
       description of the project, information about reporting bugs, and the
       latest version of this page, can be found at
       http://www.kernel.org/doc/man-pages/.

Linux                            2014-03-28             INOTIFY_ADD_WATCH(2)



INOTIFY_INIT(2)           Linux Programmer's Manual          INOTIFY_INIT(2)

NAME         top

       inotify_init, inotify_init1 - initialize an inotify instance

SYNOPSIS         top

       #include <sys/inotify.h>

       int inotify_init(void);
       int inotify_init1(int flags);

DESCRIPTION         top

       For an overview of the inotify API, see inotify(7).

       inotify_init() initializes a new inotify instance and returns a file
       descriptor associated with a new inotify event queue.

       If flags is 0, then inotify_init1() is the same as inotify_init().
       The following values can be bitwise ORed in flags to obtain different
       behavior:

       IN_NONBLOCK Set the O_NONBLOCK file status flag on the new open file
                   description.  Using this flag saves extra calls to
                   fcntl(2) to achieve the same result.

       IN_CLOEXEC  Set the close-on-exec (FD_CLOEXEC) flag on the new file
                   descriptor.  See the description of the O_CLOEXEC flag in
                   open(2) for reasons why this may be useful.

RETURN VALUE         top

       On success, these system calls return a new file descriptor.  On
       error, -1 is returned, and errno is set to indicate the error.

ERRORS         top

       EINVAL (inotify_init1()) An invalid value was specified in flags.

       EMFILE The user limit on the total number of inotify instances has
              been reached.

       ENFILE The system limit on the total number of file descriptors has
              been reached.

       ENOMEM Insufficient kernel memory is available.

VERSIONS         top

       inotify_init() first appeared in Linux 2.6.13; library support was
       added to glibc in version 2.4.  inotify_init1() was added in Linux
       2.6.27; library support was added to glibc in version 2.9.

CONFORMING TO         top

       These system calls are Linux-specific.

SEE ALSO         top

       inotify_add_watch(2), inotify_rm_watch(2), inotify(7)

COLOPHON         top

       This page is part of release 3.69 of the Linux man-pages project.  A
       description of the project, information about reporting bugs, and the
       latest version of this page, can be found at
       http://www.kernel.org/doc/man-pages/.

Linux                            2014-03-28                  INOTIFY_INIT(2)




INOTIFY_RM_WATCH(2)       Linux Programmer's Manual      INOTIFY_RM_WATCH(2)

NAME         top

       inotify_rm_watch - remove an existing watch from an inotify instance

SYNOPSIS         top

       #include <sys/inotify.h>

       int inotify_rm_watch(int fd, int wd);

DESCRIPTION         top

       inotify_rm_watch() removes the watch associated with the watch
       descriptor wd from the inotify instance associated with the file
       descriptor fd.

       Removing a watch causes an IN_IGNORED event to be generated for this
       watch descriptor.  (See inotify(7).)

RETURN VALUE         top

       On success, inotify_rm_watch() returns zero.  On error, -1 is
       returned and errno is set to indicate the cause of the error.

ERRORS         top

       EBADF  fd is not a valid file descriptor.

       EINVAL The watch descriptor wd is not valid; or fd is not an inotify
              file descriptor.

VERSIONS         top

       Inotify was merged into the 2.6.13 Linux kernel.

CONFORMING TO         top

       This system call is Linux-specific.

SEE ALSO         top

       inotify_add_watch(2), inotify_init(2), inotify(7)

COLOPHON         top

       This page is part of release 3.69 of the Linux man-pages project.  A
       description of the project, information about reporting bugs, and the
       latest version of this page, can be found at
       http://www.kernel.org/doc/man-pages/.

Linux                            2010-10-15              INOTIFY_RM_WATCH(2)




英文:http://linux.die.net/man/2/inotify_add_watch

notify是什么?用它能干些什么?

         通俗点说它是一个内核用于通知用户空间程序文件系统变化的系统,并且它是powerful yet simple的。

 

inotify的用户接口原型主要有以下3个:
#include  <sys/inotify.h>

初始化:int inotify_init(void);

               int    fd  =  inotify_init();
添加监视对象:int inotify_add_watch(int fd, const char *path, uint32_t mask);

             int   wd   = inotify_add_watch(fd,path,mask);

             fd是inotify_init()的返回值。

             const  char *path是要监控的文件(目录)的路径。

             uint32_t   mask是:


如何使用inotify_init,inotify_add_watch,inotify_rm_watch编写程序 - xychenbaihu@yeah - 无影的博客

还有非常多的事件可以使用。使用man   inotify可以查看所有可以监听的事件。

             mask是上面这些事件的或。例如IN_ACCESS|IN_MODIFY。

             返回值:wd表示对那个文件进行监控。
删除监视对象:int inotify_rm_watch(int fd, uint32_t wd);

             参数fd是inotify_init的返回值。

                    wd是inotify_add_watch的返回值。

             inotify_rm_watch删除对wd所指向的文件的监控。

读取监控发生的事件:

             size_t len = read(fd, buf, BUF_LEN);

             读取事件数据,buf应是一个指向inotify_event结构数组的指针。不过要注意的是inotify_event的name成员长度是可变的,这个问题后面再解释。

             注意:其中buf是一个指向struct  inotify_event数组的指针。

                      由于struct   inotify_event长度是可变的,因此在读取inotify_event数组内容的时候需要动态计算一下时间数据的偏移量。index += sizeof(struct inotify_event)+event->len,len即name成员的长度。

        

其实还是没有讲的很清楚,不过看了下面的例子,一定非常清楚:

#include <stdio.h>   
#include <unistd.h>   
#include <sys/select.h>   
#include <errno.h>   
#include <sys/inotify.h>   
  
static void   _inotify_event_handler(struct inotify_event *event)      //从buf中取出一个事件。  
{   
         printf("event->mask: 0x%08x\n", event->mask);   
         printf("event->name: %s\n", event->name);   
}   
  
int  main(int argc, char **argv)   
{   
  if (argc != 2) {   
    printf("Usage: %s <file/dir>\n", argv[0]);   
    return -1;   
  }   
  
  unsigned char buf[1024] = {0};   
  struct inotify_event *event = NULL;              


  int fd = inotify_init();                 //初始化
  int wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);                  //监控指定文件的ALL_EVENTS。
  
  for (;;) 

  {   
       fd_set fds;   
       FD_ZERO(&fds);                
       FD_SET(fd, &fds);   


       if (select(fd + 1, &fds, NULL, NULL, NULL) > 0)                //监控fd的事件。当有事件发生时,返回值>0

       {   
           int len, index = 0;   
           while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));       //没有读取到事件。
           while (index < len) 

           {   
                  event = (struct inotify_event *)(buf + index);                       
                  _inotify_event_handler(event);                                             //获取事件。
                  index += sizeof(struct inotify_event) + event->len;             //移动index指向下一个事件
           }   
       }   
  }   
  
  inotify_rm_watch(fd, wd);              //删除对指定文件的监控。
  
  return 0;   
}  








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值