【问题标题】:Inotify listening to moved_from/moved_to eventsInotify 监听moved_from/moved_to 事件
【发布时间】:2018-07-09 19:37:49
【问题描述】:

我正在使用 inotify 监听目录 IN_MOVED_FROM/IN_MOVED_TO 对上的事件。这是简单的汇编程序:

SYS_exit equ 0x3C

SYS_inotify_init equ 253

SYS_read equ 0x00
MEMPAGE_SIZE equ 4096

SYS_inotify_add_watch equ 254
IN_MOVED_FROM equ 0x40
IN_MOVED_TO equ 0x80

section .text
    global _start

_start:
    mov rax, SYS_inotify_init
    syscall
    mov r13, rax            ;storing inotify instance

    mov rax, SYS_inotify_add_watch
    mov rdi, r13
    mov rsi, watcher_dir
    mov rdx, IN_MOVED_TO
    syscall 

    mov rax, SYS_inotify_add_watch
    mov rdi, r13
    mov rsi, watcher_dir
    mov rdx, IN_MOVED_FROM
    syscall

    mov rax, SYS_read
    mov rdi, r13
    mov rsi, buf
    mov rdx, MEMPAGE_SIZE
    syscall          <------ I set gdb breakpoint here

    mov eax, SYS_exit
    mov rdi, 0x00
    syscall

section .bss
    buf resb MEMPAGE_SIZE           ;reserving a page
section .data
    watcher_dir: db '/home/me/asm/inotify/data', 0

好的,我在 gdb 中运行程序,断点设置为从inotify 描述符读取并检查

struct inotify_event {
     int      wd;       /* Watch descriptor */
     uint32_t mask;     /* Mask describing event */
     uint32_t cookie;   /* Unique cookie associating related
                           events (for rename(2)) */
     uint32_t len;      /* Size of name field */
     char     name[];   /* Optional null-terminated name */
};

当我按如下方式移动文件时

mv data/test data/test.moved

我只有一个事件IN_MOVED_FROM

(gdb) x/1xw $rsi + 4                                                                                      
0x600138:       0x00000040

长度为0x10:

(gdb) x/1xw $rsi + 12
0x600140:       0x00000010

(gdb) x/1xw $rsi + 32
0x600154:       0x00000000

但我预计buf 中的IN_MOVED_TO 事件(迷路了?)。当我在代码中交换inotify_add_watch 的顺序时,第一个再次丢失。为什么?

【问题讨论】:

  • 为什么需要使用汇编来处理inotify事件?
  • @Barmar 这不适用于生产。我正在学习 x86 汇编和 linux 系统调用。
  • 使用strace。另外,用C重写,看看它是否有效。重复read 以查看其他通知是否只是缓冲(是的,我看到您可以在一次读取中获得多个,但也许它只是没有发生)。
  • @Jester 我使用并看到了与我描述的一样的事件。

标签: linux assembly x86-64 inotify


【解决方案1】:

这不是汇编问题,只是你使用了系统调用错误。您忘记指定IN_MASK_ADD。这就是man inotify 所说的:

IN_MASK_ADD

如果文件系统的监视实例已经存在 对应于路径名的对象,添加(或)事件 掩码到手表掩码(而不是替换掩码)。

诚然,inotify_add_watch 默认替换并且仅在您明确要求时才添加是违反直觉的。

【讨论】:

  • 这确实有效。非常感谢!我的错,没有仔细阅读文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2015-06-23
  • 2020-06-25
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多