溫馨提示×

Linux inotify API使用教程

小樊
36
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。

  • inotifywait用于等待文件或文件集上的一個特定事件,它可以監控任何文件和目錄設置,并且可以遞歸地監控整個目錄樹。
  • inotifywatch用于收集被監控的文件系統統計數據,包括每個inotify事件發生多少次等信息。

使用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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女