Inotify机制

描述

Inotify API用于检测文件系统变化的机制。Inotify可用于检测单个文件,也可以检测整个目录。当检测的对象是一个目录的时候,目录本身和目录里的内容都会成为检测的对象。

此种机制的出现的目的是当内核空间发生某种事件之后,可以立即通知到用户空间。方便用户做出具体的操作。

Inotify API

  • inotify_init(void)

用于创建一个inotify的实例,然后返回inotify事件队列的文件描述符。 同样内核也提供了inotify_init1(int flags)接口函数,当flag等于0的时候,该函数等价于inotify_init(void)函数。

  • inotify_add_watch(int fd, const char* pathname, uint32_t  mask)

该函数用于添加“watch list”,也就是检测列表。 可以是一个新的watch,也可以是一个已经存在的watch。其中fd就是inotify_init的返回值,pathname是要检测目录或者文件的路径,mask就是要检测的事件类型。该函数成功返回的是一个unique的watch描述符。

  • inotify_rm_watch(int fd, int wd)

用于从watch list种移除检测的对象。

 

数据结构

内核使用struct inotify_event代表一个文件事件。当检测的文件对象发生变化时,使用read系统调用就会返回一个或者多个inotify_event的文件事件对象。

 
  1. struct inotify_event {

  2. int wd; /* Watch descriptor */

  3. uint32_t mask; /* Mask of events */

  4. uint32_t cookie; /* Unique cookie associating related

  5. events (for rename(2)) */

  6. uint32_t len; /* Size of name field */

  7. char name[]; /* Optional null-terminated name */

  8. };

.wd:        就是检测的对象的watch descriptor

.mask:    检测事件的mask

.cookie:  和rename事件相关。

.len:        name字段的长度。

.name:    检测对象的name。

可以看到name字段的长度是0,也就是变长的。因为检测的对象的name不定,使用变长可以方便记录检测对象的name。

 

有关检测的事件类型分为好几种,如下:

 
  1. IN_ACCESS File was accessed (read) (*).

  2. IN_ATTRIB Metadata changed, e.g., permissions, timestamps, extended

  3. attributes, link count (since Linux 2.6.25), UID, GID, etc.(*).

  4. IN_CLOSE_WRITE File opened for writing was closed (*).

  5. IN_CLOSE_NOWRITE File not opened for writing was closed (*).

  6. IN_CREATE File/directory created in watched directory (*).

  7. IN_DELETE File/directory deleted from watched directory (*).

  8. IN_DELETE_SELF Watched file/directory was itself deleted.

  9. IN_MODIFY File was modified (*).

  10. IN_MOVE_SELF Watched file/directory was itself moved.

  11. IN_MOVED_FROM File moved out of watched directory (*).

  12. IN_MOVED_TO File moved into watched directory (*).

  13. IN_OPEN File was opened (*).

注释写的很清楚,不再一一解释了。

 

实例分析

 
  1. #include <sys/inotify.h>

  2. #include <unistd.h>

  3. #include <string.h>

  4. #include <stdio.h>

  5.  
  6. /*

  7. struct inotify_event {

  8. int wd; // Watch descriptor

  9. uint32_t mask; // Mask of events

  10. uint32_t cookie; // Unique cookie associating related events (for rename(2))

  11. uint32_t len; // Size of name field

  12. char name[]; // Optional null-terminated name

  13. };

  14.  
  15. */

  16.  
  17. int watch_inotify_events(int fd)

  18. {

  19. char event_buf[512];

  20. int ret;

  21. int event_pos = 0;

  22. int event_size = 0;

  23. struct inotify_event *event;

  24.  
  25. /*读事件是否发生,没有发生就会阻塞*/

  26. ret = read(fd, event_buf, sizeof(event_buf));

  27.  
  28. /*如果read的返回值,小于inotify_event大小出现错误*/

  29. if(ret < (int)sizeof(struct inotify_event))

  30. {

  31. printf("counld not get event!\n");

  32. return -1;

  33. }

  34.  
  35. /*因为read的返回值存在一个或者多个inotify_event对象,需要一个一个取出来处理*/

  36. while( ret >= (int)sizeof(struct inotify_event) )

  37. {

  38. event = (struct inotify_event*)(event_buf + event_pos);

  39. if(event->len)

  40. {

  41. if(event->mask & IN_CREATE)

  42. {

  43. printf("create file: %s\n",event->name);

  44. }

  45. else

  46. {

  47. printf("delete file: %s\n",event->name);

  48. }

  49. }

  50.  
  51. /*event_size就是一个事件的真正大小*/

  52. event_size = sizeof(struct inotify_event) + event->len;

  53. ret -= event_size;

  54. event_pos += event_size;

  55. }

  56.  
  57. return 0;

  58. }

  59.  
  60. int main(int argc, char** argv)

  61. {

  62. int InotifyFd;

  63. int ret;

  64.  
  65. if (argc != 2)

  66. {

  67. printf("Usage: %s <dir>\n", argv[0]);

  68. return -1;

  69. }

  70.  
  71. /*inotify初始化*/

  72. InotifyFd = inotify_init();

  73. if( InotifyFd == -1)

  74. {

  75. printf("inotify_init error!\n");

  76. return -1;

  77. }

  78.  
  79. /*添加watch对象*/

  80. ret = inotify_add_watch(InotifyFd, argv[1], IN_CREATE | IN_DELETE);

  81.  
  82. /*处理事件*/

  83. watch_inotify_events(InotifyFd);

  84.  
  85. /*删除inotify的watch对象*/

  86. if ( inotify_rm_watch(InotifyFd, ret) == -1)

  87. {

  88. printf("notify_rm_watch error!\n");

  89. return -1;

  90. }

  91.  
  92. /*关闭inotify描述符*/

  93. close(InotifyFd);

  94.  
  95. return 0;

  96. }

 

1.  编译代码

gcc inotify.c -o inotify


2. 在tmp目录下创建test目录

mkdir /tmp/test


3.  检测/tmp/test目录,使用inotify机制

./inotify /tmp/test &


4.  在/tmp/test下创建1.txt文件

 
  1. test$ touch /tmp/test/1.txt

  2. create file: 1.txt

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384968.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

文件操作(二进制文件加密解密)

加密 #include<stdio.h> #include<string.h>void code(char *p,size_t n) {size_t i;for(i 0; i < n; i){p[i] 3;} }int main() {FILE *p1 fopen("./a.txt","r");FILE *p2 fopen("./b.txt","w");char buf[1024] {…

北京加密机现场select问题

问题描述 北京项目通过调用我们提供的库libsigxt.a与加密机通信&#xff0c;c/s架构&#xff0c;客户端启用多个线程&#xff0c;每个线程流程有以下三步&#xff0c;连接加密机&#xff0c;签名&#xff0c;关闭链接。在正常运行一段时间后会出现不能连接加密机服务问题。 连…

拼接字符串(带参程序)

1.用strcat拼接函数可以实现 #include<stdio.h> #include<string.h>int main(int argc,char ** argv) {char str[100] {0};int i;for( i 1; i < argc; i){strcat(str,argv[i]);}printf("str %s\n",str);return 0; } 2.用sprintf函数也可以实现 #in…

详细解释signal和sigaction以及SIG_BLOCK

signal&#xff0c;此函数相对简单一些&#xff0c;给定一个信号&#xff0c;给出信号处理函数则可&#xff0c;当然&#xff0c;函数简单&#xff0c;其功能也相对简单许多&#xff0c;简单给出个函数例子如下&#xff1a; [cpp] view plain copy 1 #include <signal.h>…

处理SIGCHLD信号

在上一讲中&#xff0c;我们使用fork函数得到了一个简单的并发服务器。然而&#xff0c;这样的程序有一个问题&#xff0c;就是当子进程终止时&#xff0c;会向父进程发送一个SIGCHLD信号&#xff0c;父进程默认忽略&#xff0c;导致子进程变成一个僵尸进程。僵尸进程一定要处理…

文件操作(stat)

/*** stat.c ***/ #include<stdio.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h>int main() {struct stat st {0}; //定义一个结构体&#xff0c;名字叫ststat("./a.txt",&st); //调用完stat函数之后&…

nginx源码阅读(一).综述

前言 nginx作为一款开源的轻量级高性能web服务器,是非常值得立志从事服务端开发方向的人学习的。现今nginx的最新版本是nginx-1.13.6,代码量也日渐庞大,但是由于其核心思想并没改变,为了降低阅读难度,我选择的是nginx-1.0.15版本,并且由于时间和水平有限,重点关注的是nginx的启…

文件操作(stat函数)

stat函数可以获取文件信息 /*** stat.c ***/ #include<stdio.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h>int main() {struct stat st {0}; //定义一个结构体&#xff0c;名字叫ststat("./a.txt",&st); …

文件操作(结构体)

将结构体内容写入到文件中 #include<stdio.h> #include<string.h>struct student {char name[100];int age; };int main() {struct student st {"wangqinghe",30};FILE * p fopen("./c.txt","wb");fwrite(&st,sizeof(st),1,p…

nginx源码阅读(二).初始化:main函数及ngx_init_cycle函数

前言 在分析源码时,我们可以先把握主干,然后其他部分再挨个分析就行了。接下来我们先看看nginx的main函数干了些什么。 main函数 这里先介绍一些下面会遇到的变量类型: ngx_int_t: typedef intptr_t ngx_int_t; 64位机器上,intptr_t为long int, 即typedef long int intptr_…

EAGAIN、EWOULDBLOCK、EINTR与非阻塞

EWOULDBLOCK&#xff1a;用于非阻塞模式&#xff0c;不需要重新读或者写 EINTR&#xff1a;指操作被中断唤醒&#xff0c;需要重新读/写   在Linux环境下开发经常会碰到很多错误(设置errno)&#xff0c;其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中)。 从字面上…

文件操作(排序)

文本文件&#xff0c;每行代表一个整数&#xff0c;范围在0~512之间&#xff1b; 要求&#xff1a;对文件排序&#xff0c;不使用堆空间&#xff0c;只使用栈空间。 用srand()和rand()函数生成一定量的随机数 /*** file.c ***/ #include<stdio.h> #include<string.h&g…

Linux下send错误代码32

问题描述&#xff1a;今天写程序&#xff0c;socket后send出现这个问题&#xff0c;send的返回值为-1&#xff0c;而errno为32&#xff0c;这个错误代码为broken pipe&#xff0c;即管道破裂。 问题形成原因&#xff1a;后来通过排查研究&#xff0c;发现出现该种问题出现的可能…

系统级性能分析工具perf的介绍与使用

系统级性能优化通常包括两个阶段&#xff1a;性能剖析&#xff08;performance profiling&#xff09;和代码优化。 性能剖析的目标是寻找性能瓶颈&#xff0c;查找引发性能问题的原因及热点代码。 代码优化的目标是针对具体性能问题而优化代码或编译选项&#xff0c;以改善软…

linux C线程

一个应用程序可以启动若干个线程&#xff1b;线程&#xff0c;是程序执行的最小单位&#xff1b;一般一个最简单的程序最少有一个线程&#xff0c;就是程序本身&#xff0c;也是主函数&#xff1b;一个线程阻塞不会影响另一个线程&#xff1b;多线程的进程可以尽可能多的利用系…

fseek函数

fseek函数&#xff1a; int fseek(FILE *_FILE, long _Offset, int _Origin); 函数设置文件指针stream的位置&#xff0c;如果执行成功&#xff0c;stream将指向以fromwhere为基准&#xff0c;偏移量offset&#xff08;指针偏移量&#xff09;个字节的位置&#xff0c;函数返回…

linux ethtool 查看网卡状态

ethtool 工具关于网络协商功能介绍&#xff1b; ethtool - Display or change ethernet card settings&#xff08;ethtool 是用来显示和更改网卡设置的工具&#xff09;&#xff1b;这个工具比较复杂&#xff0c;功能也特别多。由于洋文比较难懂。所以我们还是把网络设备协商…

ftell函数

ftell函数用于得到文件位置指针当前位置相对于文件首的偏移字节数&#xff0c;在随机方式存储文件时&#xff0c;由于文件位置频繁的前后移动&#xff0c;程序不容易确定文件的当前位置。 /*** a.txt ***/ asd gsdert dfhjtew /*** ftell.c ***/ #include<stdio.h>int ma…

muduo网络库源码阅读Step by Step

Posted on: Nov 26 2015 Categories: muduo C Tags: muduo 一般写服务端程序都需要有一个称手的网络库来帮我们处理琐碎的网络通信细节&#xff0c;比如连接的建立、关闭&#xff0c;读取数据&#xff0c;发送数据&#xff0c;接收、发送缓冲区的管理等&#xff0c;常用的C/C网…

C/C++内存问题检查利器——Purify

C&#xff0f;C内存问题检查利器——Purify 一、 引言 我们都知道软件的测试&#xff08;在以产品为主的软件公司中叫做QA—Quality Assessment&#xff09;占了整个软件工程的30% -50%&#xff0c;但有这么一种说法&#xff0c;即使是最优秀测试专家设计出来的测试…