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

相关文章

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2