linux_unix编程手册-inotify API

本文介绍了一个使用Linux inotify API的示例程序,该程序能够监控指定文件路径的所有可能的文件事件,包括创建、删除、修改等。inotify API自Linux 2.6.13版本起可用,适用于Linux系统。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

/*************************************************************************\
*                  Copyright (C) Michael Kerrisk, 2018.                   *
*                                                                         *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the   *
* Free Software Foundation, either version 3 or (at your option) any      *
* later version. This program is distributed without any warranty.  See   *
* the file COPYING.gpl-v3 for details.                                    *
\*************************************************************************/

/* Listing 19-1 */

/* demo_inotify.c

   Demonstrate the use of the inotify API.

   Usage: demo_inotify pathname...

   The program monitors each of the files specified on the command line for all
   possible file events.

   This program is Linux-specific. The inotify API is available in Linux 2.6.13
   and later.
*/
#include <sys/inotify.h>
#include <limits.h>
#include "tlpi_hdr.h"

static void             /* Display information from inotify_event structure */
displayInotifyEvent(struct inotify_event *i)
{
    printf("    wd =%2d; ", i->wd);
    if (i->cookie > 0)
        printf("cookie =%4d; ", i->cookie);

    printf("mask = ");
    if (i->mask & IN_ACCESS)        printf("IN_ACCESS ");
    if (i->mask & IN_ATTRIB)        printf("IN_ATTRIB ");
    if (i->mask & IN_CLOSE_NOWRITE) printf("IN_CLOSE_NOWRITE ");
    if (i->mask & IN_CLOSE_WRITE)   printf("IN_CLOSE_WRITE ");
    if (i->mask & IN_CREATE)        printf("IN_CREATE ");
    if (i->mask & IN_DELETE)        printf("IN_DELETE ");
    if (i->mask & IN_DELETE_SELF)   printf("IN_DELETE_SELF ");
    if (i->mask & IN_IGNORED)       printf("IN_IGNORED ");
    if (i->mask & IN_ISDIR)         printf("IN_ISDIR ");
    if (i->mask & IN_MODIFY)        printf("IN_MODIFY ");
    if (i->mask & IN_MOVE_SELF)     printf("IN_MOVE_SELF ");
    if (i->mask & IN_MOVED_FROM)    printf("IN_MOVED_FROM ");
    if (i->mask & IN_MOVED_TO)      printf("IN_MOVED_TO ");
    if (i->mask & IN_OPEN)          printf("IN_OPEN ");
    if (i->mask & IN_Q_OVERFLOW)    printf("IN_Q_OVERFLOW ");
    if (i->mask & IN_UNMOUNT)       printf("IN_UNMOUNT ");
    printf("\n");

    if (i->len > 0)
        printf("        name = %s\n", i->name);
}

#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

int
main(int argc, char *argv[])
{
    int inotifyFd, wd, j;
    char buf[BUF_LEN] __attribute__ ((aligned(8)));
    ssize_t numRead;
    char *p;
    struct inotify_event *event;

    if (argc < 2 || strcmp(argv[1], "--help") == 0)
        usageErr("%s pathname...\n", argv[0]);

    inotifyFd = inotify_init();                 /* Create inotify instance */
    if (inotifyFd == -1)
        errExit("inotify_init");

    /* For each command-line argument, add a watch for all events */

    for (j = 1; j < argc; j++) {
        wd = inotify_add_watch(inotifyFd, argv[j], IN_ALL_EVENTS);
        if (wd == -1)
            errExit("inotify_add_watch");

        printf("Watching %s using wd %d\n", argv[j], wd);
    }

    for (;;) {                                  /* Read events forever */
        numRead = read(inotifyFd, buf, BUF_LEN);
        if (numRead == 0)
            fatal("read() from inotify fd returned 0!");

        if (numRead == -1)
            errExit("read");

        printf("Read %ld bytes from inotify fd\n", (long) numRead);

        /* Process all of the events in buffer returned by read() */

        for (p = buf; p < buf + numRead; ) {
            event = (struct inotify_event *) p;
            displayInotifyEvent(event);

            p += sizeof(struct inotify_event) + event->len;
        }
    }

    exit(EXIT_SUCCESS);
}

测试:
在这里插入图片描述

这段代码是 `tssc_loop_init` 函数,其作用是初始化配网主事件循环所需的资源,包括定时器和文件描述符的注册。该函数通常在配网流程启动时被调用,用于将配网上下文中的定时任务和文件监听任务注册到主事件循环中。 --- ### 函数说明: ```c static void tssc_loop_init(struct tssc_context *ctx) ``` - **功能**:初始化主事件循环中与配网相关的定时器和文件描述符。 - **参数**: - `ctx`:指向配网上下文结构体的指针,用于从中获取配置和资源。 - **返回值**:无。 --- ### 函数体解析: ```c if (!ctx) { return; } ``` - 如果传入的上下文指针为空,直接返回,避免空指针访问。 ```c if (ctx->inotify_fd > 0) inotify_fd = tss_loop_fd_init(ctx->inotify_fd, tssc_bind_file_changed_handler); ``` - 如果上下文中存在有效的 `inotify_fd`,则将其注册到事件循环中,并绑定回调函数 `tssc_bind_file_changed_handler`。 - `inotify_fd` 用于监听恢复状态文件的变化。 ```c scan_timer = tss_loop_timer_init(ctx, tssc_scan_handler); ``` - 初始化扫描定时器,并绑定回调函数 `tssc_scan_handler`。 - 该定时器可能用于周期性地触发信道扫描。 ```c tssc_joining_timer = tss_loop_timer_init(ctx, tssc_joining_handler); ``` - 初始化加入定时器,并绑定回调函数 `tssc_joining_handler`。 - 该定时器可能用于控制设备加入网络的逻辑。 ```c if (ctx->inotify_fd > 0) tss_add_loop_fd(inotify_fd); ``` - 如果 `inotify_fd` 有效,将其添加到事件循环中,使其开始监听事件。 ```c tss_add_loop_timer(scan_timer, TSSC_SCAN_DETECTION_INTERVAL); ``` - 将扫描定时器添加到事件循环中,并设置初始触发间隔为 `TSSC_SCAN_DETECTION_INTERVAL`。 ```c return; ``` - 函数结束。 --- ### 总结: `tssc_loop_init` 函数负责将配网上下文中的资源(如 inotify 文件描述符和定时器)注册到主事件循环中,使配网流程能够响应定时任务和文件变化事件。它是配网模块启动时的重要初始化步骤。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

andrewbytecoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值