inotify

2012/12/19 15:29
阅读数 233

简介: 当需要对 Linux®文件系统进行高效率、细粒度、异步地监控时,可以采用 inotify。可利用它对用户空间进行安全、性能、以及其他方面的监控。

至于inotify的基本介绍可以看下面链接

http://www.ibm.com/developerworks/cn/linux/l-inotify/index.html?ca=drs-

 

这里主要说下自己试验的总结:

  1. 何时需要自己调用inotify_rm_watch
  2. 合适需要自己调用inotify_add_watch
  3. read调用注意事项

 

对于1,出现moved_from而没有出现moved_to时,这就需要自己调用inotify_rm_watch把移走的文件目录(移到非监控目录下)监控删除,这个目录下面的子目录会自动删除的 。像其它操作:删除监控目录,监控目录在大的监控目录下移进移出是不需要自己调用inotify_rm_watch的,因为系统自动处理,产生的事件分别为delete_self和move_self。

 

对于2, 这要看你是否需要递归监控每个目录,如果是,那么当你在监控目录下建立一个新目录时,就需要调用inotify_add_watch;放心系统不会出现单独的moved_to, 如果你从非监控目录下copy一个目录到监控目录下,那么inotify产生的事件是create (目录或文件),而不会是moved_to的,但可以单独产生moved_from事件,如情况1所说。

对与3,要知道下面红色的限制,

/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 N_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.

如果你监控的目录很大,那么其它限制你也必须考虑,调用read时,要注意返回的是一个完整的

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 */ 
          };

结构。

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部