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

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

【VUE】跨域问题的概念,以及解决方法。

目录 1.跨域概念 2.解决方法 2.1 配置网络请求代理 2.2 使用@CrossOrigin 注解 2.3 通过配置文件实现跨域 2.4 添加 CorsWebFilter 来解决跨域问题 1.跨域概念 跨域问题是由于浏览器实施了同源策略,该策略要求请求的域名、协议和端口必须与提供资源的服务相同。如果不相同,则需要服务器显式地允许这种跨域请求。一般在springbo

题目1254:N皇后问题

题目1254:N皇后问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在同一斜线上。因为皇后可以直走,横走和斜走如下图)。 你的任务是,对于给定的N,求出有多少种合法的放置方法。输出N皇后问题所有不同的摆放情况个数。 输入

vscode中文乱码问题,注释,终端,调试乱码一劳永逸版

忘记咋回事突然出现了乱码问题,很多方法都试了,注释乱码解决了,终端又乱码,调试窗口也乱码,最后经过本人不懈努力,终于全部解决了,现在分享给大家我的方法。 乱码的原因是各个地方用的编码格式不统一,所以把他们设成统一的utf8. 1.电脑的编码格式 开始-设置-时间和语言-语言和区域 管理语言设置-更改系统区域设置-勾选Bata版:使用utf8-确定-然后按指示重启 2.vscode

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

form表单提交编码的问题

浏览器在form提交后,会生成一个HTTP的头部信息"content-type",标准规定其形式为Content-type: application/x-www-form-urlencoded; charset=UTF-8        那么我们如果需要修改编码,不使用默认的,那么可以如下这样操作修改编码,来满足需求: hmtl代码:   <meta http-equiv="Conte