完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
Linux提供了hotplup(热插拔),udev和inotify机制帮助我们可以看到底层硬件设备发生了什么,从而能够更好地管理设备,给用户提供更好地服务。 Inotify通过如下三个系统调用和返回的文件描述符上的文件IO操作来使用。 Inotify的使用有如下几个步骤: 1、创建'inotify实例(inotify_init()) 2、添加一个watch(inotify_add_watch(fd,path,mask)) 删除一个watch(inotify_rm_watch(fd,wd)) 3、文件事件用一个inotify_event结构表示 struct inotify_event { __s32 wd; /* watch descriptor */ __u32 mask; /* watch mask */ __u32 cookie; /* cookie to synchronize two event*/ __u32 len; /* length (including nulls) of name */ char name[0]; /* stub for possible name */ }; 4、Read调用可以一次获得多个事件size_t len = read(fd,buf,BUF_LEN) 系统内核的实现原理: 每个inotify实例对应一个inotify_device结构: struct inotify_device { wait_queue_head_t wq; /* wait queue for i/o */ struct idr idr; /* idr mapping wd -> watch */ struct semaphore sem; /* protects this bad boy */ struct list_head events; /* list of queued events */ struct list_head watches; /* list of watches */ atomic_t count; /* reference count */ struct user_struct *user; /* user who opened this dev */ unsigned int queue_size; /* size of the queue (bytes) */ unsigned int event_count; /* number of pending events */ unsigned int max_events; /* maximum number of events */ u32 last_wd; /* the last wd allocated */ }; 使用实例: #include #include #include _syscall0(int, inotify_init) _syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask) _syscall2(int, inotify_rm_watch, int, fd, __u32, mask) char * monitored_files[] = { "./tmp_file", "./tmp_dir", "/mnt/sda3/windows_file" }; struct wd_name { int wd; char * name; }; #define WD_NUM 3 struct wd_name wd_array[WD_NUM]; char * event_array[] = { "File was accessed", "File was modified", "File attributes were changed", "writtable file closed", "Unwrittable file closed", "File was opened", "File was moved from X", "File was moved to Y", "Subfile was created", "Subfile was deleted", "Self was deleted", "Self was moved", "", "Backing fs was unmounted", "Event queued overflowed", "File was ignored" }; #define EVENT_NUM 16 #define MAX_BUF_SIZE 1024 int main(void) { int fd; int wd; char buffer[1024]; char * offset = NULL; struct inotify_event * event; int len, tmp_len; char strbuf[16]; int i = 0; fd = inotify_init(); if (fd < 0) { printf("Fail to initialize inotify.n"); exit(-1); } for (i=0; ifor (i=0; imask & IN_ISDIR) { memcpy(strbuf, "Direcotory", 11); } else { memcpy(strbuf, "File", 5); } printf("Object type: %sn", strbuf); for (i=0; iwd != wd_array.wd) continue; printf("Object name: %sn", wd_array.name); break; } printf("Event mask: %08Xn", event->mask); for (i=0; imask & (1 event = (struct inotify_event *)(offset + tmp_len); offset += tmp_len; |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
「含关键代码」基于AM3352/AM3354/AM3359的Linux开发案例分享
4902 浏览 0 评论
87423 浏览 0 评论
【高手问答】如何做到精通linux技术?资深工程师带你突破难点
4742 浏览 2 评论
3628 浏览 2 评论
解读Linux :先从创建一个文件夹用来存放jdk压缩文件开始
2489 浏览 0 评论
2018浏览 3评论
1301浏览 1评论
求解:aarch64交叉编译工具已经安装成功,环境变量已经配置,怎么将系统架构切换为ARM的架构
1360浏览 0评论
电脑和虚拟机可以互ping,电脑和开发板也可以互ping,但是虚拟机和开发板ping不通是什么原因
1244浏览 0评论
1182浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 00:17 , Processed in 1.150402 second(s), Total 45, Slave 35 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号