完善资料让更多小伙伴认识你,还能领取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; |
|
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
TuyaOpen开源 + DshanPi-A1(RK3576)开发板的Tuya IoT平台接入与AI大模型整合方案(笔记)
1327 浏览 0 评论
「含关键代码」基于AM3352/AM3354/AM3359的Linux开发案例分享
5241 浏览 0 评论
87854 浏览 0 评论
【高手问答】如何做到精通linux技术?资深工程师带你突破难点
5306 浏览 2 评论
3997 浏览 2 评论
2328浏览 1评论
SiRider S1芯擎工业开发板测评+1.防止黑客入侵通信监控系统(PSA)
1657浏览 0评论
TuyaOpen开源 + DshanPi-A1(RK3576)开发板的Tuya IoT平台接入与AI大模型整合方案(笔记)
1327浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 15:38 , Processed in 0.928896 second(s), Total 67, Slave 49 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖