博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
inotify通知机制
阅读量:4125 次
发布时间:2019-05-25

本文共 3381 字,大约阅读时间需要 11 分钟。

#include 
#include
#include
#include
#include
/* *参考: frameworks\native\services\inputflinger\EventHub.cpp *//*Usage: inotify
*//** inotify主要功能:它是一个内核用于通知用户空间程序文件系统变化的机制* 众所周知,Linux 桌面系统与 MAC 或 Windows 相比有许多不如人意的地方, 为了改善这种状况,开源社区提出用户态需要内核提供一些机制,以便用户 态能够及时地得知内核或底层硬件设备发生了什么,从而能够更好地管理设 备,给用户提供更好的服务,如 hotplug、udev 和 inotify 就是这种需求 催生的。 Hotplug 是一种内核向用户态应用通报关于热插拔设备一些事件发生的机制, 桌面系统能够利用它对设备进行有效的管理 udev 动态地维护 /dev 下的设备文件 inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立 刻让用户态得知,该机制是著名的桌面搜索引擎项目 beagle 引入的,并在 Gamin 等项目中被应用。* */#if 0len长度分配以16字节对齐分配如name为hi,len为16 name为hello_world_123456 len为48struct inotify_event { __s32 wd; /* watch descriptor */ __u32 mask; /* watch mask */ __u32 cookie; /* cookie to synchronize two events */ __u32 len; /* length (including nulls) of name */ char name[0]; /* stub for possible name */};#endifint read_process_inotify_fd(int fd){ int res; char event_buf[512]; int event_size; int event_pos = 0; struct inotify_event *event; /* read */ res = read(fd, event_buf, sizeof(event_buf)); printf("res:%d (int)sizeof(*event):%d\n",res,(int)sizeof(*event)); if(res < (int)sizeof(*event)) { if(errno == EINTR) return 0; printf("could not get event, %s\n", strerror(errno)); return -1; } /* process * 读到的数据是1个或者多个inotify_event,如touch/rm 多个文件,需要遍历处理 * 它们的长度不一样,逐个处理 */ while(res >= (int)sizeof(*event)) { event = (struct inotify_event *)(event_buf + event_pos); printf("event->wd:%d event->mask:0x%x event->len:%d event->name:%s\n",event->wd,event->mask,event->len,event->name); if(event->len) { if(event->mask & IN_CREATE) { printf("create file: %s\n", event->name); } else { printf("delete file: %s\n", event->name); } } event_size = sizeof(*event) + event->len; res -= event_size; event_pos += event_size; } return 0;}int main(int argc, char **argv){ int mINotifyFd; int result; if (argc != 2){ printf("Usage: %s
\n", argv[0]); return -1; } /* 1.inotify_init 创建 inotify 实例*/ mINotifyFd = inotify_init(); /* 每一个 inotify 实例对应一个独立的排序的队列。 文件系统的变化事件被称做 watches 的一个对象管理, 每一个 watch 是一个二元组(目标,事件掩码), 目标可以是文件或目录,事件掩码表示应用希望关注的 inotify 事件, 每一个位对应一个 inotify 事件。Watch 对象通过 watch描述符引用, watches 通过文件或目录的路径名来添加。目录 watches 将返回在该 目录下的所有文件上面发生的事件。 */ /* 2.add watch 添加一个 watch int inotify_add_watch(int fd, const char *pathname, uint32_t mask) fd: fd是inotify_init()返回的文件描述符 pathname: pathname是被监视的目标的路径名,即文件名或目录名 mask: mask是事件掩码, 在头文件linux/inotify.h中定义了每一位代表的事件 IN_DELETE:监听删除 IN_CREATE:监听创建 return value:返回非负的watch descriptor(监听描述符) */ result = inotify_add_watch(mINotifyFd, argv[1], IN_DELETE | IN_CREATE); /* read */ while (1) { read_process_inotify_fd(mINotifyFd); } return 0;}
调试结果:
lisongze@lisongze-ubuntu:~/code/C$ gcc inotify.c -o inotify.c
lisongze@lisongze-ubuntu:~/code/C$ mkdir tmp
lisongze@lisongze-ubuntu:~/code/C$ ./inotify tmp&
lisongze@lisongze-ubuntu:~/code/C$ touch tmp/test tmp/hello
res:32 (int)sizeof(*event):16
event->wd:1 event->mask:0x100 event->len:16 event->name:test
create file: test
res:32 (int)sizeof(*event):16
event->wd:1 event->mask:0x100 event->len:16 event->name:hello
create file: hello
lisongze@lisongze-ubuntu:~/code/C$ rm tmp/test tmp/hello
res:32 (int)sizeof(*event):16
event->wd:1 event->mask:0x200 event->len:16 event->name:test
delete file: test
res:32 (int)sizeof(*event):16
event->wd:1 event->mask:0x200 event->len:16 event->name:hello
delete file: hello

转载地址:http://cglpi.baihongyu.com/

你可能感兴趣的文章
svn out of date 无法更新到最新版本
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>
Qt文件夹选择对话框
查看>>
C++ Primer Plus学习笔记 第十八章 C++11新标准
查看>>
序列比对算法-计算生物学
查看>>
Java学习笔记-反射及其应用
查看>>
机器学习实战-5Logistic回归
查看>>
深入理解Java虚拟机-8虚拟机字节码执行引擎
查看>>
五大常用算法——分治法,动态规划,回溯法,分支界限法,贪心算法
查看>>
MySQL三大范式及其实践理解
查看>>
redis相关总结
查看>>
机器学习实战-11关联分析Apriori算法-pytohn3
查看>>