diff --git a/core/sys/linux/bits.odin b/core/sys/linux/bits.odin index 9f2c7a5d8..9ce2e206e 100644 --- a/core/sys/linux/bits.odin +++ b/core/sys/linux/bits.odin @@ -519,6 +519,36 @@ Fd_Poll_Events_Bits :: enum { RDHUP = 13, } +Inotify_Init_Bits :: enum { + NONBLOCK = 11, + CLOEXEC = 19, +} + +Inotify_Event_Bits :: enum u32 { + ACCESS = 0, + MODIFY = 1, + ATTRIB = 2, + CLOSE_WRITE = 3, + CLOSE_NOWRITE = 4, + OPEN = 5, + MOVED_FROM = 6, + MOVED_TO = 7, + CREATE = 8, + DELETE = 9, + DELETE_SELF = 10, + MOVE_SELF = 11, + UNMOUNT = 13, + Q_OVERFLOW = 14, + IGNORED = 15, + ONLYDIR = 24, + DONT_FOLLOW = 25, + EXCL_UNLINK = 26, + MASK_CREATE = 28, + MASK_ADD = 29, + ISDIR = 30, + ONESHOT = 31, +} + /* Bits for Mem_Protection bitfield */ diff --git a/core/sys/linux/constants.odin b/core/sys/linux/constants.odin index 129444d0f..b3bbcafb3 100644 --- a/core/sys/linux/constants.odin +++ b/core/sys/linux/constants.odin @@ -135,6 +135,31 @@ STATX_BASIC_STATS :: Statx_Mask { .BLOCKS, } +IN_ALL_EVENTS :: Inotify_Event_Mask { + .ACCESS, + .MODIFY, + .ATTRIB, + .CLOSE_WRITE, + .CLOSE_NOWRITE, + .OPEN, + .MOVED_FROM, + .MOVED_TO, + .CREATE, + .DELETE, + .DELETE_SELF, + .MOVE_SELF, +} + +IN_CLOSE :: Inotify_Event_Mask { + .CLOSE_WRITE, + .CLOSE_NOWRITE, +} + +IN_MOVE :: Inotify_Event_Mask { + .MOVED_FROM, + .MOVED_TO, +} + /* Tell `shmget` to create a new key */ diff --git a/core/sys/linux/sys.odin b/core/sys/linux/sys.odin index c5894d78b..690902f07 100644 --- a/core/sys/linux/sys.odin +++ b/core/sys/linux/sys.odin @@ -2536,11 +2536,30 @@ waitid :: proc "contextless" (id_type: Id_Type, id: Id, sig_info: ^Sig_Info, opt // TODO(flysand): ioprio_get -// TODO(flysand): inotify_init +inotify_init :: proc "contextless" () -> (Fd, Errno) { + when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 { + ret := syscall(SYS_inotify_init1, 0) + return errno_unwrap(ret, Fd) + } else { + ret := syscall(SYS_inotify_init) + return errno_unwrap(ret, Fd) + } +} -// TODO(flysand): inotify_add_watch +inotify_init1 :: proc "contextless" (flags: Inotify_Init_Flags) -> (Fd, Errno) { + ret := syscall(SYS_inotify_init1, transmute(i32)flags) + return errno_unwrap(ret, Fd) +} -// TODO(flysand): inotify_rm_watch +inotify_add_watch :: proc "contextless" (fd: Fd, pathname: cstring, mask: Inotify_Event_Mask) -> (Wd, Errno) { + ret := syscall(SYS_inotify_add_watch, fd, transmute(uintptr) pathname, transmute(u32) mask) + return errno_unwrap(ret, Wd) +} + +inotify_rm_watch :: proc "contextless" (fd: Fd, wd: Wd) -> (Errno) { + ret := syscall(SYS_inotify_rm_watch, fd, wd) + return Errno(-ret) +} // TODO(flysand): migrate_pages diff --git a/core/sys/linux/types.odin b/core/sys/linux/types.odin index 07c654749..42d5cc988 100644 --- a/core/sys/linux/types.odin +++ b/core/sys/linux/types.odin @@ -30,6 +30,11 @@ Id :: distinct uint */ Fd :: distinct i32 +/* + Represents a watch descriptor. +*/ +Wd :: distinct i32 + /* Type for PID file descriptors. */ @@ -343,6 +348,18 @@ Poll_Fd :: struct { revents: Fd_Poll_Events, } +Inotify_Init_Flags :: bit_set[Inotify_Init_Bits; i32] + +Inotify_Event :: struct { + wd: Wd, + mask: Inotify_Event_Mask, + cookie: u32, + len: u32, + name: [0]u8, +} + +Inotify_Event_Mask :: bit_set[Inotify_Event_Bits; u32] + /* Specifies protection for memory pages. */