inotify不生效问题

2024-06-03 15:32
文章标签 问题 生效 inotify

本文主要是介绍inotify不生效问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

inotify还是不错的,玩着似乎很简单,但是坑也不少,如果不仔细查看官方文档,可能就真的不知道哪里存在坑,哪里需要注意。前段时间,在项目中使用inotify监控配置文件,以达到实时感知配置改变的目的。但近日查看线上日志发现,配置文件改变后,inotify并没有通知,结果导致配置一直未被更改。

    在描述之前,要说明一下,我代码中的inotify使用方式,这个方式和网上大多方式一样:

[cpp]  view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <errno.h>  
  2. #include <stdio.h>  
  3. #include <sys/inotify.h>  
  4.   
  5. static const char kszConfigPath[] = "/usr/local/path/to/config";  
  6. static int s_running = 0;  
  7.   
  8. extern int reparse_config();  
  9.   
  10. int inotify_loop()  
  11. {  
  12.     int inot_fd = -1;  
  13.     int watch_fd = -1;  
  14.     unsigned int watch_flag = IN_MODIFY;  
  15.     fd_set read_fds;  
  16.     struct timeval seltime;  
  17.     char buffer[16384];  
  18.     int buffer_i = 0;  
  19.   
  20.     int inot_fd = inotify_init();  
  21.     if (inot_fd < 0) {  
  22.         printf("inotify_init error %d\n", errno);  
  23.         return -1;  
  24.     }  
  25.   
  26.     /* Watch config file. */  
  27.     watch_fd = inotify_add_watch(inot_fd, kszConfigPath, watch_flag);  
  28.     if (watch_fd < 0) {  
  29.         printf("inotify_init error %d\n", errno);  
  30.         close(inot_fd);  
  31.         return -1;  
  32.     }  
  33.   
  34.     while (s_running) {  
  35.         int selret = 0;  
  36.         int read_cnt = 0;  
  37.   
  38.         FD_ZERO(&read_fds);  
  39.         FD_SET(inot_fd, &read_fds);  
  40.         seltime.tv_sec = 1;  
  41.         seltime.tv_usec = 0;  
  42.   
  43.         selret = select(inot_fd + 1, &read_fds, NULL, NULL, &seltime);  
  44.         if (selret < 0) {  
  45.             printf("select error %d\n", errno);  
  46.             continue;  
  47.         } else if (selret == 0) {  
  48.             continue;  
  49.         }else if (!FD_ISSET(inot_fd, &read_fds)) {  
  50.             printf("inot_fd not in fdset\n");  
  51.             continue;  
  52.         }  
  53.   
  54.         read_cnt = read(fd, buffer, sizeof(buffer));  
  55.         if (read_cnt <= 0) {  
  56.             printf("read <= 0 (%d)\n", read_cnt);  
  57.             continue;  
  58.         }  
  59.   
  60.         buffer_i = 0;  
  61.         while (buffer_i < read_cnt) {  
  62.             /* Parse events and queue them. */  
  63.             struct inotify_event* pevent = (struct inotify_event*)&buffer[buffer_i];  
  64.             if (pevent->mask & IN_MODIFY) {  
  65.                 printf("config %s modified\n", kszConfigPath);  
  66.                 reparse_config();  
  67.             } else {  
  68.                 printf("Unrecognized event mask %d\n", pevent->mask);  
  69.             }  
  70.             buffer_i += sizeof(struct inotify_event) + pevent->len;  
  71.         }  
  72.     }  
  73.   
  74.     /** Remove watch. */  
  75.     if (inotify_rm_watch(inot_fd, watch_fd) < 0) {  
  76.         printf("inotify_rm_watch error %d\n", errno);  
  77.     }  
  78.   
  79.     return 0;  
  80. }  
一、使用vi编辑a文件,然后保存。程序没有截获IN_MODIFY事件,打印的是Unrecongnized event mask 32768。
二、使用mv更改文件,也就是使用命令mv a a.bak。在mv后,发现程序没能截获IN_MODIFY事件,打印的是Unrecongnized event mask 32768。
三、删除a文件,并重启程序,发现inotify_init注册失败,而errno为2。
      看来,我的程序是有问题的。那么32768到底是什么呢?查看官方的inotify文档 http://man7.org/linux/man-pages/man7/inotify.7.html ,发现32768是IN_IGNORED,表示使用者对watch标识符调用了inotify_rm_watch,或者watch标识符被自动从inotify中移除。意思很明显,就是文件被删除了。inotify只是监视句柄,并非文件路径,当文件标示符对应的文件被删除时,这个文件标识符也被自动从inotify中移除,在以上的代码中,也就意味着inotify中没有任何watch标识符了,自然也就不会通知IN_MODIFY事件。
     等等,在第一个步骤中,我们不是修改了a文件吗?那至少应该打出IN_MODIFY吧。这里要解释一下,vi编辑时会先将文件保存为a.swap,等编辑完毕后再移动回来或者删除,具体移动回来还是删除,取决于是退出vi时是保存操作,还是取消操作。所以这里根本没有触发IN_MODIFY事件,实际上触发的是IN_MOVE_TO事件(a文件被移出),以及IN_IGNORED事件等等。
     这里要注意的是:文件被删除,不一定会产生IN_IGNORED事件,也就是说文件被删除时,不一定会把文件对应的标识符自动从inotify中删除。在这些情况下,我们需要重新对文件重新 注册 监控。
     在第三个测试中,我们看到inotify只会对存在的文件进行监控,如果要实现无论文件是否存在都持续监控,那么我们需要自动重新注册 监控
     以下是修改后的代码:

[cpp]  view plain copy
print ?
  1. #include <errno.h>  
  2. #include <stdio.h>  
  3. #include <sys/inotify.h>  
  4.   
  5. static const char kszConfigPath[] = "/usr/local/path/to/config";  
  6. static int s_running = 0;  
  7.   
  8. extern int reparse_config();  
  9.   
  10. #ifdef _WIN32  
  11. struct inotify_event {  
  12.     int      wd;       /* Watch descriptor */  
  13.     uint32_t mask;     /* Mask of events */  
  14.     uint32_t cookie;   /* Unique cookie associating related 
  15.                          events (for rename(2)) */  
  16.     uint32_t len;      /* Size of name field */  
  17.     char     name[];   /* Optional null-terminated name */  
  18. };  
  19.   
  20. int inotify_init(void);  
  21. int inotify_add_watch(int fd, const char *pathname, uint32_t mask);  
  22. int inotify_rm_watch(int fd, int wd);  
  23. #endif  
  24.   
  25. int set_non_blocking(fd)  
  26. {  
  27.     int fd_flag = 0;  
  28.     if (fd < 0)  
  29.         return EINVAL;  
  30.     fd_flag = fcntl(fd, F_GETFL, 0);  
  31.     if (fd_flag < 0)  
  32.         fd_flag = 0;  
  33.     return fcntl(fd, F_SETFL, fd_flag | O_NONBLOCK);  
  34. }  
  35.   
  36. int rm_inotify_wd(int fd, int wd)  
  37. {  
  38.     char ignore[1024];  
  39.     if (fd < 0 || wd < 0)  
  40.         return EINVAL;  
  41.     while (read(fd, ignore) > 0) {  
  42.         /* Ignore previous unread notify events. */  
  43.         continue;  
  44.     }  
  45.     return inotify_rm_watch(fd, wd);   
  46. }  
  47.   
  48. int inotify_loop()  
  49. {  
  50.     int selret = 0;  
  51.     int read_cnt = 0;  
  52.     int inot_fd = -1;  
  53.     int watch_fd = -1;  
  54.     int need_parse = 0;  
  55.     int need_remove_wd = 0;  
  56.     unsigned int watch_flag = IN_CLOSE_WRITE | IN_CREATE | IN_DELETE  
  57.         | IN_DELETE_SELF | IN_MOVE | IN_MOVE_SELF;  
  58.     fd_set read_fds;  
  59.     struct timeval seltime;  
  60.     char buffer[16384];  
  61.     int buffer_i = 0;  
  62.   
  63.     if ((inot_fd = inotify_init()) < 0) {  
  64.         printf("inotify_init error %d\n", errno);  
  65.         return -1;  
  66.     }  
  67.   
  68.     if (set_non_blocking(inot_fd) < 0) {  
  69.         printf("set_non_blocking error %d\n", errno);  
  70.     }  
  71.   
  72.     while (s_running) {  
  73.         new_init= 0;  
  74.   
  75.         if (watch_fd < 0) {  
  76.             /* Watch config file. */  
  77.             if ((watch_fd = inotify_add_watch(inot_fd, kszConfigPath, watch_flag)) < 0) {  
  78.                 printf("inotify_init error %d\n", errno);  
  79.             } else {  
  80.                 /* Non-existed -> existed, reparse immediately.*/  
  81.                 reparse_config();  
  82.             }  
  83.         }  
  84.   
  85.         seltime.tv_sec = 1;  
  86.         seltime.tv_usec = 0;  
  87.         if (watch_fd < 0) {  
  88.             selret = select(NULL, NULL, NULL, NULL, &seltime);  
  89.         } else {  
  90.             FD_ZERO(&read_fds);  
  91.             FD_SET(inot_fd, &read_fds);  
  92.             selret = select(inot_fd + 1, &read_fds, NULL, NULL, &seltime);  
  93.         }  
  94.           
  95.         if (selret < 0) {  
  96.             printf("select error %d\n", errno);  
  97.             continue;  
  98.         } else if (selret == 0) {  
  99.             continue;  
  100.         } else if (!FD_ISSET(inot_fd, &read_fds)) {  
  101.             printf("inot_fd not in fdset\n");  
  102.             continue;  
  103.         }  
  104.   
  105.         read_cnt = read(fd, buffer, sizeof(buffer));  
  106.         if (read_cnt <= 0) {  
  107.             printf("read <= 0 (%d)\n", read_cnt);  
  108.             continue;  
  109.         }  
  110.   
  111.         need_parse = 0;  
  112.         need_remove_wd = 0;  
  113.         buffer_i = 0;  
  114.         while (buffer_i < read_cnt) {  
  115.             /* Parse events and queue them. */  
  116.             struct inotify_event* pevent = (struct inotify_event*)&buffer[buffer_i];  
  117.             if (pevent->mask & IN_MODIFY) {  
  118.                 printf("config %s modified\n", kszConfigPath);  
  119.             } else if (pevent->mask & IN_CLOSE_WRITE) {  
  120.                 printf("config %s close for writing\n", kszConfigPath);  
  121.                 need_parse = 1;  
  122.             } else if (pevent->mask & IN_CREATE) {  
  123.                 printf("config %s created\n", kszConfigPath);  
  124.                 need_parse = 1;  
  125.             } else if (pevent->mask & (IN_DELETE | IN_DELETE_SELF)) {  
  126.                 printf("config %s was deleted\n", kszConfigPath);  
  127.                 need_parse = 1;  
  128.                 need_remove_wd = 1;  
  129.             } else if (pevent->mask & (IN_MOVE | IN_MOVE_SELF)) {  
  130.                 printf("config %s was removed\n", kszConfigPath);  
  131.                 need_parse = 1;  
  132.                 need_remove_wd = 1;  
  133.             } else {  
  134.                 printf("Unrecognized event mask %d\n", pevent->mask);  
  135.             }  
  136.             buffer_i += sizeof(struct inotify_event) + pevent->len;  
  137.         }  
  138.   
  139.         if (need_parse) {  
  140.             reparse_config();  
  141.         }  
  142.   
  143.         if (need_remove_wd) {  
  144.             int ret = rm_inotify_wd(inot_fd, watch_fd);  
  145.             if (ret < 0) {  
  146.                 printf("rm_inotify_wd error %d\n", ret);  
  147.             }  
  148.             watch_fd = -1;  
  149.         }  
  150.     }  
  151.   
  152.     /** Remove watch. */  
  153.     if (inotify_rm_watch(inot_fd, watch_fd) < 0) {  
  154.         printf("inotify_rm_watch error %d\n", errno);  
  155.     }  
  156.   
  157.     close(inot_fd);  
  158.   
  159.     return 0;  
  160. }  

这篇关于inotify不生效问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1027385

相关文章

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Idea插件MybatisX失效的问题解决

《Idea插件MybatisX失效的问题解决》:本文主要介绍Idea插件MybatisX失效的问题解决,详细的介绍了4种问题的解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、重启idea或者卸载重装MyBATis插件(无需多言)二、检查.XML文件与.Java(该文件后缀Idea可能会隐藏

Nginx 访问 /root/下 403 Forbidden问题解决

《Nginx访问/root/下403Forbidden问题解决》在使用Nginx作为Web服务器时,可能会遇到403Forbidden错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录解决 Nginx 访问 /root/test/1.html 403 Forbidden 问题问题复现Ng

Python的pip在命令行无法使用问题的解决方法

《Python的pip在命令行无法使用问题的解决方法》PIP是通用的Python包管理工具,提供了对Python包的查找、下载、安装、卸载、更新等功能,安装诸如Pygame、Pymysql等Pyt... 目录前言一. pip是什么?二. 为什么无法使用?1. 当我们在命令行输入指令并回车时,一般主要是出现以

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

Python解决雅努斯问题实例方案详解

《Python解决雅努斯问题实例方案详解》:本文主要介绍Python解决雅努斯问题实例方案,雅努斯问题是指AI生成的3D对象在不同视角下出现不一致性的问题,即从不同角度看物体时,物体的形状会出现不... 目录一、雅努斯简介二、雅努斯问题三、示例代码四、解决方案五、完整解决方案一、雅努斯简介雅努斯(Janu

MySQL索引失效问题及解决方案

《MySQL索引失效问题及解决方案》:本文主要介绍MySQL索引失效问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql索引失效一、概要二、常见的导致MpythonySQL索引失效的原因三、如何诊断MySQL索引失效四、如何解决MySQL索引失

一文教你如何解决Python开发总是import出错的问题

《一文教你如何解决Python开发总是import出错的问题》经常朋友碰到Python开发的过程中import包报错的问题,所以本文将和大家介绍一下可编辑安装(EditableInstall)模式,可... 目录摘要1. 可编辑安装(Editable Install)模式到底在解决什么问题?2. 原理3.

Redis中的数据一致性问题以及解决方案

《Redis中的数据一致性问题以及解决方案》:本文主要介绍Redis中的数据一致性问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Redis 数据一致性问题的产生1. 单节点环境的一致性问题2. 网络分区和宕机3. 并发写入导致的脏数据4. 持

vscode不能打开终端问题的解决办法

《vscode不能打开终端问题的解决办法》:本文主要介绍vscode不能打开终端问题的解决办法,问题的根源是Windows的安全软件限制了PowerShell的运行,而VSCode默认使用Powe... 遇到vscode不能打开终端问题,一直以为是安全软件限制问题,也没搜到解决方案,因为影响也不大,就没有管