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. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值