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

相关文章

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Mysql如何解决死锁问题

《Mysql如何解决死锁问题》:本文主要介绍Mysql如何解决死锁问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录【一】mysql中锁分类和加锁情况【1】按锁的粒度分类全局锁表级锁行级锁【2】按锁的模式分类【二】加锁方式的影响因素【三】Mysql的死锁情况【1

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t