linux

Linux inotify API使用教程

小樊
43
2025-07-03 12:07:10
栏目: 智能运维

Linux的inotify API是一个强大的文件系统监控机制,它允许应用程序实时监控文件或目录的变化,如创建、修改、删除等。以下是一个基本的使用教程,包括安装、初始化和使用inotify API的步骤。

安装inotify-tools

在基于Debian的系统(如Ubuntu)上,可以使用以下命令安装:

sudo apt-get install inotify-tools

在基于RHEL的系统(如CentOS)上,可以使用以下命令安装:

sudo yum install inotify-tools

使用inotify-tools

inotify-tools提供了两个主要的命令:inotifywaitinotifywatch

使用inotify API

1. 包含头文件

在C程序中,需要包含sys/inotify.h头文件。

#include <sys/inotify.h>

2. 初始化inotify实例

使用inotify_init()系统调用创建一个inotify实例,该调用在内核中实例化一个inotify实例并返回一个关联的文件描述符。

int fd = inotify_init();
if (fd < 0) {
    perror("inotify_init");
    return -1;
}

3. 添加监控

使用inotify_add_watch()函数向inotify实例添加要监控的文件或目录。该函数接受三个参数:文件描述符、要监控的路径名和要监控的事件掩码。

int wd = inotify_add_watch(fd, "/path/to/watch", IN_MODIFY | IN_CREATE | IN_DELETE);
if (wd < 0) {
    perror("inotify_add_watch");
    return -1;
}

4. 读取事件

使用read()系统调用从inotify实例中读取事件。当有事件发生时,read()会返回事件信息。

char buffer[4096];
while (1) {
    ssize_t len = read(fd, buffer, sizeof(buffer));
    if (len < 0) {
        perror("read");
        return -1;
    }
    while (i < len) {
        struct inotify_event *event = (struct inotify_event *)&buffer[i];
        if (event->len) {
            if (event->mask & IN_CREATE) {
                printf("File %s created.\n", event->name);
            }
            if (event->mask & IN_DELETE) {
                printf("File %s deleted.\n", event->name);
            }
            if (event->mask & IN_MODIFY) {
                printf("File %s modified.\n", event->name);
            }
        }
        i += EVENT_SIZE + event->len;
    }
}

5. 移除监控并关闭inotify实例

使用inotify_rm_watch()函数移除监控项,并使用close()关闭inotify文件描述符。

inotify_rm_watch(fd, wd);
close(fd);

示例代码

以下是一个简单的C语言示例,展示了如何使用inotify API监控日志文件的变化:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>

#define EVENT_SIZE (sizeof(struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + 16))

int main(int argc, char **argv) {
    int length, i = 0;
    int fd;
    int wd;
    char buffer[BUF_LEN];

    if (argc != 2) {
        printf("Usage: %s <logfile>\n", argv[1]);
        exit(1);
    }

    // 创建inotify实例
    fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        exit(1);
    }

    // 添加要监控的文件
    wd = inotify_add_watch(fd, argv[1], IN_MODIFY | IN_CREATE | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        exit(1);
    }

    // 读取inotify事件
    while (1) {
        length = read(fd, buffer, BUF_LEN);
        if (length < 0) {
            perror("read");
            exit(1);
        }
        while (i < length) {
            struct inotify_event *event = (struct inotify_event *)&buffer[i];
            if (event->len) {
                if (event->mask & IN_CREATE) {
                    printf("File %s created.\n", event->name);
                } else if (event->mask & IN_DELETE) {
                    printf("File %s deleted.\n", event->name);
                } else if (event->mask & IN_MODIFY) {
                    printf("File %s modified.\n", event->name);
                }
            }
            i += EVENT_SIZE + event->len;
        }
    }

    // 移除监控并关闭inotify实例
    inotify_rm_watch(fd, wd);
    close(fd);
    return 0;
}

编译并运行此程序,它将实时监控指定的日志文件,并在发生修改、创建或删除事件时输出相关信息。

以上就是使用Linux inotify API的基本教程。你可以根据自己的需求进行扩展和修改。

0
看了该问题的人还看了