作家
登录

Linux inotify功能及实现原理

作者: 来源: 2017-10-11 16:08:01 阅读 我要评论

fsnotify_move,文件年腋荷琐目次移动到另一个目次fsnotify_nameremove,文件大年夜目次中删除fsnotify_inoderemove,自删除fsnotify_create,创建新文件fsnotify_mkdir,创建新目次fsnotify_access,文件被读fsnotify_modify,文件被写fsnotify_open,文件被打开fsnotify_close,文件被封闭fsnotify_xattr,文件的扩大属性被修改fsnotify_change,文件被修改或原数据被修改有一个例外情况,就是 inotify_unmount_inodes,它会在文件体系被 umount 时调用来通知 umount 事宜给 inotify 体系。

  1. struct inotify_device { 
  2.  
  3. wait_queue_head_t wq; /* wait queue for i/o */ 
  4.  
  5. struct idr idr; /* idr mapping wd -> watch */ 
  6.  
  7. struct semaphore sem; /* protects this bad boy */ 
  8.  
  9. struct list_head events; /* list of queued events */ 
  10.  
  11. struct list_head watches; /* list of watches */ 
  12.  
  13. atomic_t count; /* reference count */ 
  14.  
  15. struct user_struct *user; /* user who opened this dev */ 
  16.  
  17. unsigned int queue_size; /* size of the queue (bytes) */ 
  18.  
  19. unsigned int event_count; /* number of pending events */ 
  20.  
  21. unsigned int max_events; /* maximum number of events */ 
  22.  
  23. u32 last_wd; /* the last wd allocated */ 
  24.  
  25. };  

4. 应用示例

以上提到的通知函数最后都调用 inotify_inode_queue_event(inotify_unmount_inodes直接调用 inotify_dev_queue_event ),该函数起首断定对应的inode是否被监督,这经由过程查看 inotify_watches 列表是否为空来实现,如不雅发明 inode 没有被监督,什么也不做,急速返回,反之,遍历 inotify_watches 列表,看是否当前的文件操作事宜被某个 watch 监督,如不雅是,调用 inotify_dev_queue_event,不然,返回。函数inotify_dev_queue_event 起首断定该事宜是否是上一个事宜的反复,如不雅是就丢弃该事宜并返回,不然,它断定是否 inotify 实例即 inotify_device 的事宜队列是否溢出,如不雅溢出,产生一个溢出事宜,不然产生一个当前的文件操作事宜,这些事宜经由过程kernel_event 构建,kernel_event 将创建一个 inotify_kernel_event 构造,然后把该构造插入到对应的 inotify_device 的 events 事宜列表,然后唤醒等待在inotify_device 构造中的 wq 指向的等待队列。想监督文件体系事宜的用户态过程在inotify 实例(即 inotify_init() 返回的文件描述符)上调用 read 时但没有事宜时就挂在等待队列 wq 上。

  1. #include  
  2. #include  
  3. #include  
  4.  
  5. _syscall0(int, inotify_init) 
  6. _syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask) 
  7. _syscall2(int, inotify_rm_watch, int, fd, __u32, mask) 
  8.  
  9. char * monitored_files[] = { 

      推荐阅读

      AI重新定义Web安全

    云给安然带来的影响距离2006年Amazon宣布EC2办事已经以前了11年,在这11年迈,产生的不仅仅是AWS收入大年夜几十万美金上涨到100多亿美金,更重要的是云计算已经走进每一俭朴业。根据信通院宣布的&ldquo>>>详细阅读


    本文标题:Linux inotify功能及实现原理

    地址:http://www.17bianji.com/lsqh/37802.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)