Inotify 监听 moved_from/moved_to 个事件
Inotify listening to moved_from/moved_to events
我正在使用 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
的顺序时,第一个再次丢失。为什么?
这不是汇编问题,你只是系统调用用错了。您忘记指定 IN_MASK_ADD
。 man inotify 是这么说的:
IN_MASK_ADD
If a watch instance already exists for the filesystem
object corresponding to pathname, add (OR) the events in
mask to the watch mask (instead of replacing the mask).
诚然,inotify_add_watch
默认替换并且仅在您明确要求时才添加是违反直觉的。
我正在使用 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
的顺序时,第一个再次丢失。为什么?
这不是汇编问题,你只是系统调用用错了。您忘记指定 IN_MASK_ADD
。 man inotify 是这么说的:
IN_MASK_ADD
If a watch instance already exists for the filesystem object corresponding to pathname, add (OR) the events in mask to the watch mask (instead of replacing the mask).
诚然,inotify_add_watch
默认替换并且仅在您明确要求时才添加是违反直觉的。