ubuntu,debian,redhat,fedora,centos
« »
2009-10-17C/C++, kernel

26

使用 inotify 监控 Linux 文件系统事件

Inotify 是文件系统事件监控机制,计划包含在即将发布的 Linux 内核中作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。下面让我们一起学习如何安装 inotify 和如何构建一个示例用户空间应用程序来响应文件系统事件。
文件系统事件监控对于从文件管理器到安全工具的各种程序都是必要的,但是 dnotify(早期内核中的标准)存在一些局限性,这使我们期待出现一种更加完善的机制。抱着这种期待,我们发现了 inotify,一种更加现代化的文件系统事件监控替代品。

为什么使用 inotify?

使用 inotify 取代 dnotify 的原因有很多。第一个原因是,dnotify 需要您为每个打算监控是否发生改变的目录打开一个文件描述符。当同时监控多个目录时,这会消耗大量的资源,因为有可能达到每个进程的文件描述符限制。

除此之外,文件描述符会锁定目录,不允许卸载(unmount)支持的设备,这在存在可移动介质的环境中会引发问题。在使用 inotify 时,如果正在监控被卸载的文件系统上的文件,那么监控会被自动移除并且您会接收到一个卸载事件。

inotify 的优点是它使用文件描述符作为基本接口,使应用程序开发者使用 select 和 poll 来监控设备。这允许有效的多路 I/O 和与 Glib 的 mainloop 的集成。相反,dnotify 所使用的信号常常使程序员头疼并且感觉不太优雅。

inotify 通过提供一个更优雅的 API 解决了这些问题,该 API 使用最少的文件描述符,并确保更细粒度的监控。与 inotify 的通信是通过设备节点提供的。基于以上原因,对于监控 Linux 2.6 平台上的文件,inotify 是您最明智的选择。

在简单应用程序中使用 inotify

为演示 inotify 的使用,我将展示如何为文件系统事件构造一个监控任意目录(或单个文件)的示例程序。我将站在一个较高的层次上来展示 inotify 使文件系统监控变得多么容易。

Main 方法

这个简单的示例向我们展示 inotify 在任意目录上设置监控是多么容易。稍后我们将看到主要的帮助器例程。您可以在本文的 下载 一节获取这些例子中使用的示例代码。

清单 1. 在目录上设置监控

  1. /* This program will take as argument a directory name and monitor it,
  2.    printing event notifications to the console.
  3. */
  4. int main (int argc, char **argv)
  5. {
  6.    /* This is the file descriptor for the inotify device */
  7.    int inotify_fd;
  8.    /* First we open the inotify dev entry */
  9.    inotify_fd = open_inotify_dev();
  10.    if (inotify_fd < 0)
  11.    {
  12.       return 0;
  13.    }
  14.    /* We will need a place to enqueue inotify events,
  15.       this is needed because if you do not read events
  16.       fast enough, you will miss them.
  17.    */
  18.    queue_t q;
  19.    q = queue_create (128);
  20.    /* Watch the directory passed in as argument
  21.       Read on for why you might want to alter this for
  22.       more efficient inotify use in your app.
  23.    */
  24.    watch_dir (inotify_fd, argv[1], ALL_MASK);
  25.    process_inotify_events (q, inotify_fd);
  26.    /* Finish up by destroying the queue, closing the fd,
  27.       and returning a proper code
  28.    */
  29.    queue_destroy (q);
  30.    close_inotify_dev (inotify_fd);
  31.    return 0;
  32. }

重要的帮助器方法

以下是每个基于 inotify 的应用程序共同的最重要的帮助器例程:

为读取而打开 inotify 设备。
对从该设备读取的事件进行排队。
允许应用程序对事件通知进行有用处理的实际的每事件处理器。
我不会深入钻研事件排队的细节,因为我们能够使用一些策略来避免排队。提供的代码中就展示了一个这样的方法;更先进的多线程方法可以并且已经在其他地方实现。在那些实现中,读者线程简单地在 inotify 设备上执行 select(),然后将事件拷贝到一些线程共享的存储空间(或者一些像 Glib 的异步消息队列的东西),以后处理器线程会处理这里的事件。

清单 2. 打开 inotify 设备

  1. /* This simply opens the inotify node in dev (read only) */
  2. int open_inotify_dev ()
  3. {
  4.    int fd;
  5.    fd = open("/dev/inotify", O_RDONLY);
  6.    if (fd < 0)
  7.    {
  8.       perror ("open(\"/dev/inotify\", O_RDONLY) = ");
  9.    }
  10.    return fd;
  11. }

这对任何一个在 Linux 系统上进行过文件编程的人来说都应该是熟悉的。

清单 3. 实际的事件处理例程

  1. /* This method does the dirty work of determining what happened,
  2.    then allows us to act appropriately
  3. */
  4. void handle_event (struct inotify_event *event)
  5. {
  6.    /* If the event was associated with a filename, we will store it here */
  7.    char * cur_event_filename = NULL;
  8.    /* This is the watch descriptor the event occurred on */
  9.    int cur_event_wd = event->wd;
  10.    if (event->len)
  11.    {
  12.       cur_event_filename = event->filename;
  13.    }
  14.    printf("FILENAME=%s\n", cur_event_filename);
  15.         printf("\n");
  16.    /* Perform event dependent handler routines */
  17.    /* The mask is the magic that tells us what file operation occurred */
  18.    switch (event->mask)
  19.    {
  20.       /* File was accessed */
  21.       case IN_ACCESS:
  22.          printf("ACCESS EVENT OCCURRED: File \"%s\" on WD #%i\n",
  23.                  cur_event_filename, cur_event_wd);
  24.       break;
  25.       /* File was modified */
  26.       case IN_MODIFY:
  27.          printf("MODIFY EVENT OCCURRED: File \"%s\" on WD #%i\n",
  28.                  cur_event_filename, cur_event_wd);
  29.       break;
  30.       /* File changed attributes */
  31.       case IN_ATTRIB:
  32.          printf("ATTRIB EVENT OCCURRED: File \"%s\" on WD #%i\n",
  33.                  cur_event_filename, cur_event_wd);
  34.       break;
  35.       /* File was closed */
  36.       case IN_CLOSE:
  37.          printf("CLOSE EVENT OCCURRED: File \"%s\" on WD #%i\n",
  38.                  cur_event_filename, cur_event_wd);
  39.       break;
  40.       /* File was opened */
  41.       case IN_OPEN:
  42.          printf("OPEN EVENT OCCURRED: File \"%s\" on WD #%i\n",
  43.                  cur_event_filename, cur_event_wd);
  44.       break;
  45.       /* File was moved from X */
  46.       case IN_MOVED_FROM:
  47.          printf("MOVE_FROM EVENT OCCURRED: File \"%s\" on WD #%i\n",
  48.                  cur_event_filename, cur_event_wd);
  49.       break;
  50.       /* File was moved to X */
  51.       case IN_MOVED_TO:
  52.          printf("MOVE_TO EVENT OCCURRED: File \"%s\" on WD #%i\n",
  53.                  cur_event_filename, cur_event_wd);
  54.       break;
  55.       /* Subdir was deleted */
  56.       case IN_DELETE_SUBDIR:
  57.          printf("DELETE_SUBDIR EVENT OCCURRED: File \"%s\" on WD #%i\n",
  58.                  cur_event_filename, cur_event_wd);
  59.       break;
  60.       /* File was deleted */
  61.       case IN_DELETE_FILE:
  62.          printf("DELETE_FILE EVENT OCCURRED: File \"%s\" on WD #%i\n",
  63.                  cur_event_filename, cur_event_wd);
  64.       break;
  65.       /* Subdir was created */
  66.       case IN_CREATE_SUBDIR:
  67.          printf("CREATE_SUBDIR EVENT OCCURRED: File \"%s\" on WD #%i\n",
  68.                  cur_event_filename, cur_event_wd);
  69.       break;
  70.       /* File was created */
  71.       case IN_CREATE_FILE:
  72.          printf("CREATE_FILE EVENT OCCURRED: File \"%s\" on WD #%i\n",
  73.                  cur_event_filename, cur_event_wd);
  74.       break;
  75.       /* Watched entry was deleted */
  76.       case IN_DELETE_SELF:
  77.          printf("DELETE_SELF EVENT OCCURRED: File \"%s\" on WD #%i\n",
  78.                  cur_event_filename, cur_event_wd);
  79.       break;
  80.       /* Backing FS was unmounted */
  81.       case IN_UNMOUNT:
  82.          printf("UNMOUNT EVENT OCCURRED: File \"%s\" on WD #%i\n",
  83.                  cur_event_filename, cur_event_wd);
  84.       break;
  85.       /* Too many FS events were received without reading them
  86.          some event notifications were potentially lost.  */
  87.       case IN_Q_OVERFLOW:
  88.          printf("Warning: AN OVERFLOW EVENT OCCURRED: \n");
  89.       break;
  90.       case IN_IGNORED:
  91.          printf("IGNORED EVENT OCCURRED: \n");
  92.       break;
  93.       /* Some unknown message received */
  94.       default:
  95.          printf ("UNKNOWN EVENT OCCURRED for file \"%s\" on WD #%i\n",
  96.               cur_event_filename, cur_event_wd);
  97.       break;
  98.    }
  99. }

在每一条 case 语句中,您可以随意执行任意已实现并且满足需要的方法。

至于性能监控,您可以确定哪些文件是最经常被读取的和它们打开的持续时间。这种监控非常方便,因为在某些情况下,如果文件在短时间内被应用程序重复地读取,它会将文件缓存在内存中而不用返回磁盘去读取,从而提高性能。

很容易举出一些执行有趣操作的特定于事件的处理器的例子。比如,如果您是在为底层文件系统实现一个元数据存储索引,您可能会寻找文件创建事件,不久还会在该文件上触发一个元数据挖掘操作。在安全环境中,如果文件被写入一个无人可以写入的目录,您会触发某些形式的系统警报。

请注意,inotify 支持许多非常细粒度的事件 —— 例如 CLOSE 与 CLOSE_WRITE。

本文中的代码所列举的许多事件,可能您并不希望在每次代码运行时都看到。实际上,只要可能,可以并且应该只请求对您的应用程序有用的事件子集。出于测试目的,本文章提供的代码通过严格使用完整掩码,main 方法的第 51 行附近或者上面的 清单 1 中的第 29 行所执行的)展示了许多事件。应用程序员通常想要有更多选择,而您则需要更特定的掩码来满足您的需要。这使您可以从上述的 handle_event() 方法中的 catch 语句删除不感兴趣的条目。

您还可能感兴趣的内容

日志信息 »

该日志于2009-10-17 20:16由 admin 发表在C/C++, kernel分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »

返回顶部