查看: 1306|回复: 0

关于Inotify对linux文件系统的监控

[复制链接]
  • TA的每日心情
    开心
    2017-12-22 08:59
  • 签到天数: 9 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2017-2-27 11:46:16 | 显示全部楼层 |阅读模式
    分享到:
    ]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结构表示
    1. struct inotify_event {
    2.      __s32           wd;             /* watch descriptor */
    3.      __u32           mask;           /* watch mask */
    4.      __u32           cookie;         /* cookie to synchronize two event*/
    5.      __u32           len;            /* length (including nulls) of name */
    6.       char            name[0];        /* stub for possible name */
    7. };
    复制代码
    1. #include
    2. #include
    3. #include

    4. _syscall0(int, inotify_init)
    5. _syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask)
    6. _syscall2(int, inotify_rm_watch, int, fd, __u32, mask)

    7. char * monitored_files[] = {
    8.         "./tmp_file",
    9.         "./tmp_dir",
    10.         "/mnt/sda3/windows_file"
    11. };

    12. struct wd_name {
    13.         int wd;
    14.         char * name;
    15. };

    16. #define WD_NUM 3
    17. struct wd_name wd_array[WD_NUM];

    18. char * event_array[] = {
    19.         "File was accessed",
    20.         "File was modified",
    21.         "File attributes were changed",
    22.         "writtable file closed",
    23.         "Unwrittable file closed",
    24.         "File was opened",
    25.         "File was moved from X",
    26.         "File was moved to Y",
    27.         "Subfile was created",
    28.         "Subfile was deleted",
    29.         "Self was deleted",
    30.         "Self was moved",
    31.         "",
    32.         "Backing fs was unmounted",
    33.         "Event queued overflowed",
    34.         "File was ignored"
    35. };
    36. #define EVENT_NUM 16
    37. #define MAX_BUF_SIZE 1024
    38.         
    39. int main(void)
    40. {
    41.         int fd;
    42.         int wd;
    43.         char buffer[1024];
    44.         char * offset = NULL;
    45.         struct inotify_event * event;
    46.         int len, tmp_len;
    47.         char strbuf[16];
    48.         int i = 0;
    49.         
    50.         fd = inotify_init();
    51.         if (fd < 0) {
    52.                 printf("Fail to initialize inotify.\n");
    53.                 exit(-1);
    54.         }

    55.         for (i=0; i<WD_NUM; wd="inotify_add_watch(fd," add (event- if { len) < buffer) - *)event (((char while *)buffer; inotify_event event len); len='%d.\n",' happens, printf(?Some offset="buffer;" MAX_BUF_SIZE)) buffer, while(len="read(fd," } wd_array[i].wd="wd;" exit(-1); wd_array[i].name); %s.\n?, for watch printf(?Can?t 0) (wd IN_ALL_EVENTS); wd_array[i].name, wd_array[i].name="monitored_files[i];" i++)>mask & IN_ISDIR) {
    56.                                 memcpy(strbuf, "Direcotory", 11);
    57.                         }
    58.                         else {
    59.                                 memcpy(strbuf, "File", 5);
    60.                         }
    61.                         printf("Object type: %s\n", strbuf);
    62.                         for (i=0; iwd != wd_array[i].wd) continue;
    63.                                 printf("Object name: %s\n", wd_array[i].name);
    64.                                 break;
    65.                         }
    66.                         printf("Event mask: %08X\n", event->mask);
    67.                         for (i=0; imask & (1<len;
    68.                         event = (struct inotify_event *)(offset + tmp_len);
    69.                         offset += tmp_len;
    70.                 }
    71.         }
    72. }
    复制代码
    更多好内容可关注:华清远见 微信公众号,关注即送价值 399 的嵌入式相关电子书!
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-24 15:24 , Processed in 0.107508 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.