inotify监控时为什么出现了IN_IGNORED事件

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

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

#include <errno.h>  
#include <stdio.h>  
#include <sys/inotify.h>  
  
static const char kszConfigPath[] = "/usr/local/path/to/config";  
static int s_running = 0;  
  
extern int reparse_config();  
  
int inotify_loop()  
{  
    int inot_fd = -1;  
    int watch_fd = -1;  
    unsigned int watch_flag = IN_MODIFY;  
    fd_set read_fds;  
    struct timeval seltime;  
    char buffer[16384];  
    int buffer_i = 0;  
  
    int inot_fd = inotify_init();  
    if (inot_fd < 0) {  
        printf("inotify_init error %d\n", errno);  
        return -1;  
    }  
  
    /* Watch config file. */  
    watch_fd = inotify_add_watch(inot_fd, kszConfigPath, watch_flag);  
    if (watch_fd < 0) {  
        printf("inotify_init error %d\n", errno);  
        close(inot_fd);  
        return -1;  
    }  
  
    while (s_running) {  
        int selret = 0;  
        int read_cnt = 0;  
  
        FD_ZERO(&read_fds);  
        FD_SET(inot_fd, &read_fds);  
        seltime.tv_sec = 1;  
        seltime.tv_usec = 0;  
  
        selret = select(inot_fd + 1, &read_fds, NULL, NULL, &seltime);  
        if (selret < 0) {  
            printf("select error %d\n", errno);  
            continue;  
        } else if (selret == 0) {  
            continue;  
        }else if (!FD_ISSET(inot_fd, &read_fds)) {  
            printf("inot_fd not in fdset\n");  
            continue;  
        }  
  
        read_cnt = read(fd, buffer, sizeof(buffer));  
        if (read_cnt <= 0) {  
            printf("read <= 0 (%d)\n", read_cnt);  
            continue;  
        }  
  
        buffer_i = 0;  
        while (buffer_i < read_cnt) {  
            /* Parse events and queue them. */  
            struct inotify_event* pevent = (struct inotify_event*)&buffer[buffer_i];  
            if (pevent->mask & IN_MODIFY) {  
                printf("config %s modified\n", kszConfigPath);  
                reparse_config();  
            } else {  
                printf("Unrecognized event mask %d\n", pevent->mask);  
            }  
            buffer_i += sizeof(struct inotify_event) + pevent->len;  
        }  
    }  
  
    /** Remove watch. */  
    if (inotify_rm_watch(inot_fd, watch_fd) < 0) {  
        printf("inotify_rm_watch error %d\n", errno);  
    }  
  
    return 0;  
}  

一、使用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文档 inotify(7) - Linux manual page ,发现32768是IN_IGNORED,表示使用者对watch标识符调用了inotify_rm_watch,或者watch标识符被自动从inotify中移除。意思很明显,就是文件被删除了。inotify只是监视句柄,并非文件路径(实际上监听的是inode),当文件标示符对应的文件被删除时,这个文件标识符也被自动从inotify中移除,在以上的代码中,也就意味着inotify中没有任何watch标识符了,自然也就不会通知IN_MODIFY事件。
     等等,在第一个步骤中,我们不是修改了a文件吗?那至少应该打出IN_MODIFY吧。这里要解释一下,vi编辑时会先将文件保存为a.swap,等编辑完毕后再移动回来或者删除,具体移动回来还是删除,取决于是退出vi时是保存操作,还是取消操作。所以这里根本没有触发IN_MODIFY事件,实际上触发的是IN_MOVE_TO事件(a文件被移出),以及IN_IGNORED事件等等。

        另外,使用gedit等编辑工具打开文件,编辑后再保存,也会出现上述问题。原理一样。可以使用ll -i 查看一下文件对应的inode,发现根本不是同一个文件。但是如果用程序进行修改,则不会发生变更。
     这里要注意的是:文件被删除,不一定会产生IN_IGNORED事件,也就是说文件被删除时,不一定会把文件对应的标识符自动从inotify中删除。在这些情况下,我们需要重新对文件重新 注册 监控。
     在第三个测试中,我们看到inotify只会对存在的文件进行监控,如果要实现无论文件是否存在都持续监控,那么我们需要自动重新注册 监控 。
     以下是修改后的代码:

#include <errno.h>  
#include <stdio.h>  
#include <sys/inotify.h>  
  
static const char kszConfigPath[] = "/usr/local/path/to/config";  
static int s_running = 0;  
  
extern int reparse_config();  
  
#ifdef _WIN32  
struct inotify_event {  
    int      wd;       /* Watch descriptor */  
    uint32_t mask;     /* Mask of events */  
    uint32_t cookie;   /* Unique cookie associating related 
                         events (for rename(2)) */  
    uint32_t len;      /* Size of name field */  
    char     name[];   /* Optional null-terminated name */  
};  
  
int inotify_init(void);  
int inotify_add_watch(int fd, const char *pathname, uint32_t mask);  
int inotify_rm_watch(int fd, int wd);  
#endif  
  
int set_non_blocking(fd)  
{  
    int fd_flag = 0;  
    if (fd < 0)  
        return EINVAL;  
    fd_flag = fcntl(fd, F_GETFL, 0);  
    if (fd_flag < 0)  
        fd_flag = 0;  
    return fcntl(fd, F_SETFL, fd_flag | O_NONBLOCK);  
}  
  
int rm_inotify_wd(int fd, int wd)  
{  
    char ignore[1024];  
    if (fd < 0 || wd < 0)  
        return EINVAL;  
    while (read(fd, ignore) > 0) {  
        /* Ignore previous unread notify events. */  
        continue;  
    }  
    return inotify_rm_watch(fd, wd);   
}  
  
int inotify_loop()  
{  
    int selret = 0;  
    int read_cnt = 0;  
    int inot_fd = -1;  
    int watch_fd = -1;  
    int need_parse = 0;  
    int need_remove_wd = 0;  
    unsigned int watch_flag = IN_CLOSE_WRITE | IN_CREATE | IN_DELETE  
        | IN_DELETE_SELF | IN_MOVE | IN_MOVE_SELF;  
    fd_set read_fds;  
    struct timeval seltime;  
    char buffer[16384];  
    int buffer_i = 0;  
  
    if ((inot_fd = inotify_init()) < 0) {  
        printf("inotify_init error %d\n", errno);  
        return -1;  
    }  
  
    if (set_non_blocking(inot_fd) < 0) {  
        printf("set_non_blocking error %d\n", errno);  
    }  
  
    while (s_running) {  
        new_init= 0;  
  
        if (watch_fd < 0) {  
            /* Watch config file. */  
            if ((watch_fd = inotify_add_watch(inot_fd, kszConfigPath, watch_flag)) < 0) {  
                printf("inotify_init error %d\n", errno);  
            } else {  
                /* Non-existed -> existed, reparse immediately.*/  
                reparse_config();  
            }  
        }  
  
        seltime.tv_sec = 1;  
        seltime.tv_usec = 0;  
        if (watch_fd < 0) {  
            selret = select(NULL, NULL, NULL, NULL, &seltime);  
        } else {  
            FD_ZERO(&read_fds);  
            FD_SET(inot_fd, &read_fds);  
            selret = select(inot_fd + 1, &read_fds, NULL, NULL, &seltime);  
        }  
          
        if (selret < 0) {  
            printf("select error %d\n", errno);  
            continue;  
        } else if (selret == 0) {  
            continue;  
        } else if (!FD_ISSET(inot_fd, &read_fds)) {  
            printf("inot_fd not in fdset\n");  
            continue;  
        }  
  
        read_cnt = read(fd, buffer, sizeof(buffer));  
        if (read_cnt <= 0) {  
            printf("read <= 0 (%d)\n", read_cnt);  
            continue;  
        }  
  
        need_parse = 0;  
        need_remove_wd = 0;  
        buffer_i = 0;  
        while (buffer_i < read_cnt) {  
            /* Parse events and queue them. */  
            struct inotify_event* pevent = (struct inotify_event*)&buffer[buffer_i];  
            if (pevent->mask & IN_MODIFY) {  
                printf("config %s modified\n", kszConfigPath);  
            } else if (pevent->mask & IN_CLOSE_WRITE) {  
                printf("config %s close for writing\n", kszConfigPath);  
                need_parse = 1;  
            } else if (pevent->mask & IN_CREATE) {  
                printf("config %s created\n", kszConfigPath);  
                need_parse = 1;  
            } else if (pevent->mask & (IN_DELETE | IN_DELETE_SELF)) {  
                printf("config %s was deleted\n", kszConfigPath);  
                need_parse = 1;  
                need_remove_wd = 1;  
            } else if (pevent->mask & (IN_MOVE | IN_MOVE_SELF)) {  
                printf("config %s was removed\n", kszConfigPath);  
                need_parse = 1;  
                need_remove_wd = 1;  
            } else {  
                printf("Unrecognized event mask %d\n", pevent->mask);  
            }  
            buffer_i += sizeof(struct inotify_event) + pevent->len;  
        }  
  
        if (need_parse) {  
            reparse_config();  
        }  
  
        if (need_remove_wd) {  
            int ret = rm_inotify_wd(inot_fd, watch_fd);  
            if (ret < 0) {  
                printf("rm_inotify_wd error %d\n", ret);  
            }  
            watch_fd = -1;  
        }  
    }  
  
    /** Remove watch. */  
    if (inotify_rm_watch(inot_fd, watch_fd) < 0) {  
        printf("inotify_rm_watch error %d\n", errno);  
    }  
  
    close(inot_fd);  
  
    return 0;  
}  

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值