inotify不生效问题

    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
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Inotify是一种文件系统变化通知机制,可以即刻反映文件或文件夹的增删等事件在用户空间中。它是Linux内核从2.6.13版本开始引入的一种强大的、细粒度的、异步的文件系统事件监控机制。通过Inotify,第三方软件可以监控文件系统中文件的各种变化情况,包括添加、删除、移动和修改等。使用Inotify可以检测单个文件的变化,也可以监控整个目录。当监控目录时,目录本身和目录下的内容都会成为监控的对象。可以使用select、poll、epoll等接口监听Inotify文件描述符的可读事件,当有事件发生时,程序可以及时处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux inotify](https://download.csdn.net/download/chenggong526214/8942667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [linux - inotify](https://blog.csdn.net/iteye_12332/article/details/82511287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Linux Inotify详解和使用](https://blog.csdn.net/zhanglei_admin/article/details/97636301)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值