From bb2033898781affb1dbca2cb9e8635e1db237c4f Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Tue, 12 Nov 2024 23:53:52 -0500 Subject: [PATCH 1/4] core:sys/linux - implemented inotify core:sys/linux - added constants and spacing --- core/sys/linux/bits.odin | 22 ++++++++++++++++++++++ core/sys/linux/constants.odin | 8 ++++++++ core/sys/linux/sys.odin | 26 +++++++++++++++++++++++--- core/sys/linux/types.odin | 19 +++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/core/sys/linux/bits.odin b/core/sys/linux/bits.odin index 9f2c7a5d8..b22a021f6 100644 --- a/core/sys/linux/bits.odin +++ b/core/sys/linux/bits.odin @@ -519,6 +519,28 @@ Fd_Poll_Events_Bits :: enum { RDHUP = 13, } +Inotify_Init_Bits :: enum { + IN_NONBLOCK = 11, + IN_CLOEXEC = 19, +} + +Inotify_Events_Bits :: enum u32 { + IN_ACCESS = 0, + IN_MODIFY = 1, + IN_ATTRIB = 2, + IN_CLOSE_WRITE = 3, + IN_CLOSE_NOWRITE = 4, + IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE), + IN_OPEN = 5, + IN_MOVED_FROM = 6, + IN_MOVED_TO = 7, + IN_MOVE = (IN_MOVED_FROM | IN_MOVED_TO), + IN_CREATE = 8, + IN_DELETE = 9, + IN_DELETE_SELF = 10, + IN_MOVE_SELF = 11, +} + /* Bits for Mem_Protection bitfield */ diff --git a/core/sys/linux/constants.odin b/core/sys/linux/constants.odin index 129444d0f..551a4fc40 100644 --- a/core/sys/linux/constants.odin +++ b/core/sys/linux/constants.odin @@ -135,6 +135,14 @@ STATX_BASIC_STATS :: Statx_Mask { .BLOCKS, } +IN_ONLYDIR :: 0x01000000 +IN_DONT_FOLLOW :: 0x02000000 +IN_EXCL_UNLINK :: 0x04000000 +IN_MASK_CREATE :: 0x10000000 +IN_MASK_ADD :: 0x20000000 +IN_ISDIR :: 0x40000000 +IN_ONESHOT :: 0x80000000 + /* Tell `shmget` to create a new key */ diff --git a/core/sys/linux/sys.odin b/core/sys/linux/sys.odin index c5894d78b..819edc4a5 100644 --- a/core/sys/linux/sys.odin +++ b/core/sys/linux/sys.odin @@ -2536,11 +2536,31 @@ 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) { + 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) +} + +// helper procedure to access the data within an `Inotify_Event` +// since Odin doesn't have an alternative to `__flexarr` +inotify_event_name :: proc "contextless" (event: ^Inotify_Event) -> cstring { + return transmute(cstring)&event.name +} // TODO(flysand): migrate_pages diff --git a/core/sys/linux/types.odin b/core/sys/linux/types.odin index 07c654749..88b8f3f3c 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,20 @@ 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, + // used in place of __flexarr + // use inotify_event_name to read + name: [0]u8, +} + +Inotify_Event_Mask :: bit_set[Inotify_Events_Bits; u32] + /* Specifies protection for memory pages. */ From 37441bd7306d8f7c5775ec84d0c8efb4d73a8fe4 Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Wed, 13 Nov 2024 00:09:38 -0500 Subject: [PATCH 2/4] core:sys/linux - fixed vet errors --- core/sys/linux/sys.odin | 2 +- core/sys/linux/types.odin | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/sys/linux/sys.odin b/core/sys/linux/sys.odin index 819edc4a5..9adeda610 100644 --- a/core/sys/linux/sys.odin +++ b/core/sys/linux/sys.odin @@ -2559,7 +2559,7 @@ inotify_rm_watch :: proc "contextless" (fd: Fd, wd: Wd) -> (Errno) { // helper procedure to access the data within an `Inotify_Event` // since Odin doesn't have an alternative to `__flexarr` inotify_event_name :: proc "contextless" (event: ^Inotify_Event) -> cstring { - return transmute(cstring)&event.name + return transmute(cstring)uintptr(&event.name) } // TODO(flysand): migrate_pages diff --git a/core/sys/linux/types.odin b/core/sys/linux/types.odin index 88b8f3f3c..97e593935 100644 --- a/core/sys/linux/types.odin +++ b/core/sys/linux/types.odin @@ -348,7 +348,7 @@ Poll_Fd :: struct { revents: Fd_Poll_Events, } -Inotify_Init_Flags :: bit_set[Inotify_Init_Bits; i32]; +Inotify_Init_Flags :: bit_set[Inotify_Init_Bits; i32] Inotify_Event :: struct { wd: Wd, From 1b313a4db055750a3760414ad4e8c82a749792df Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Wed, 13 Nov 2024 08:52:33 -0500 Subject: [PATCH 3/4] core:sys/linux - flags, spacing, inotify_init --- core/sys/linux/bits.odin | 12 +++++++++++- core/sys/linux/constants.odin | 21 ++++++++++++++------- core/sys/linux/sys.odin | 15 +++++++-------- core/sys/linux/types.odin | 12 +++++------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/core/sys/linux/bits.odin b/core/sys/linux/bits.odin index b22a021f6..f9dcb45d5 100644 --- a/core/sys/linux/bits.odin +++ b/core/sys/linux/bits.odin @@ -524,7 +524,7 @@ Inotify_Init_Bits :: enum { IN_CLOEXEC = 19, } -Inotify_Events_Bits :: enum u32 { +Inotify_Event_Bits :: enum u32 { IN_ACCESS = 0, IN_MODIFY = 1, IN_ATTRIB = 2, @@ -539,6 +539,16 @@ Inotify_Events_Bits :: enum u32 { IN_DELETE = 9, IN_DELETE_SELF = 10, IN_MOVE_SELF = 11, + IN_UNMOUNT = 13, + IN_Q_OVERFLOW = 14, + IN_IGNORED = 15, + IN_ONLYDIR = 24, + IN_DONT_FOLLOW = 25, + IN_EXCL_UNLINK = 26, + IN_MASK_CREATE = 28, + IN_MASK_ADD = 29, + IN_ISDIR = 30, + IN_ONESHOT = 31, } /* diff --git a/core/sys/linux/constants.odin b/core/sys/linux/constants.odin index 551a4fc40..e568ff2f2 100644 --- a/core/sys/linux/constants.odin +++ b/core/sys/linux/constants.odin @@ -135,13 +135,20 @@ STATX_BASIC_STATS :: Statx_Mask { .BLOCKS, } -IN_ONLYDIR :: 0x01000000 -IN_DONT_FOLLOW :: 0x02000000 -IN_EXCL_UNLINK :: 0x04000000 -IN_MASK_CREATE :: 0x10000000 -IN_MASK_ADD :: 0x20000000 -IN_ISDIR :: 0x40000000 -IN_ONESHOT :: 0x80000000 +IN_ALL_EVENTS :: Inotify_Event_Mask { + .IN_ACCESS, + .IN_MODIFY, + .IN_ATTRIB, + .IN_CLOSE_WRITE, + .IN_CLOSE_NOWRITE, + .IN_OPEN, + .IN_MOVED_FROM, + .IN_MOVED_TO, + .IN_CREATE, + .IN_DELETE, + .IN_DELETE_SELF, + .IN_MOVE_SELF, +} /* Tell `shmget` to create a new key diff --git a/core/sys/linux/sys.odin b/core/sys/linux/sys.odin index 9adeda610..690902f07 100644 --- a/core/sys/linux/sys.odin +++ b/core/sys/linux/sys.odin @@ -2537,8 +2537,13 @@ waitid :: proc "contextless" (id_type: Id_Type, id: Id, sig_info: ^Sig_Info, opt // TODO(flysand): ioprio_get inotify_init :: proc "contextless" () -> (Fd, Errno) { - ret := syscall(SYS_inotify_init) - return errno_unwrap(ret, Fd) + 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) + } } inotify_init1 :: proc "contextless" (flags: Inotify_Init_Flags) -> (Fd, Errno) { @@ -2556,12 +2561,6 @@ inotify_rm_watch :: proc "contextless" (fd: Fd, wd: Wd) -> (Errno) { return Errno(-ret) } -// helper procedure to access the data within an `Inotify_Event` -// since Odin doesn't have an alternative to `__flexarr` -inotify_event_name :: proc "contextless" (event: ^Inotify_Event) -> cstring { - return transmute(cstring)uintptr(&event.name) -} - // TODO(flysand): migrate_pages /* diff --git a/core/sys/linux/types.odin b/core/sys/linux/types.odin index 97e593935..42d5cc988 100644 --- a/core/sys/linux/types.odin +++ b/core/sys/linux/types.odin @@ -351,16 +351,14 @@ Poll_Fd :: struct { Inotify_Init_Flags :: bit_set[Inotify_Init_Bits; i32] Inotify_Event :: struct { - wd: Wd, - mask: Inotify_Event_Mask, + wd: Wd, + mask: Inotify_Event_Mask, cookie: u32, - len: u32, - // used in place of __flexarr - // use inotify_event_name to read - name: [0]u8, + len: u32, + name: [0]u8, } -Inotify_Event_Mask :: bit_set[Inotify_Events_Bits; u32] +Inotify_Event_Mask :: bit_set[Inotify_Event_Bits; u32] /* Specifies protection for memory pages. From 8f80e9765eeb840adc26ecb863eaf260ca7ceeed Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Wed, 13 Nov 2024 12:45:58 -0500 Subject: [PATCH 4/4] core:sys/linux - prefixing + moved IN_CLOSE/IN_MOVE to constants --- core/sys/linux/bits.odin | 50 +++++++++++++++++------------------ core/sys/linux/constants.odin | 34 +++++++++++++++--------- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/core/sys/linux/bits.odin b/core/sys/linux/bits.odin index f9dcb45d5..9ce2e206e 100644 --- a/core/sys/linux/bits.odin +++ b/core/sys/linux/bits.odin @@ -520,35 +520,33 @@ Fd_Poll_Events_Bits :: enum { } Inotify_Init_Bits :: enum { - IN_NONBLOCK = 11, - IN_CLOEXEC = 19, + NONBLOCK = 11, + CLOEXEC = 19, } Inotify_Event_Bits :: enum u32 { - IN_ACCESS = 0, - IN_MODIFY = 1, - IN_ATTRIB = 2, - IN_CLOSE_WRITE = 3, - IN_CLOSE_NOWRITE = 4, - IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE), - IN_OPEN = 5, - IN_MOVED_FROM = 6, - IN_MOVED_TO = 7, - IN_MOVE = (IN_MOVED_FROM | IN_MOVED_TO), - IN_CREATE = 8, - IN_DELETE = 9, - IN_DELETE_SELF = 10, - IN_MOVE_SELF = 11, - IN_UNMOUNT = 13, - IN_Q_OVERFLOW = 14, - IN_IGNORED = 15, - IN_ONLYDIR = 24, - IN_DONT_FOLLOW = 25, - IN_EXCL_UNLINK = 26, - IN_MASK_CREATE = 28, - IN_MASK_ADD = 29, - IN_ISDIR = 30, - IN_ONESHOT = 31, + 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, } /* diff --git a/core/sys/linux/constants.odin b/core/sys/linux/constants.odin index e568ff2f2..b3bbcafb3 100644 --- a/core/sys/linux/constants.odin +++ b/core/sys/linux/constants.odin @@ -136,18 +136,28 @@ STATX_BASIC_STATS :: Statx_Mask { } IN_ALL_EVENTS :: Inotify_Event_Mask { - .IN_ACCESS, - .IN_MODIFY, - .IN_ATTRIB, - .IN_CLOSE_WRITE, - .IN_CLOSE_NOWRITE, - .IN_OPEN, - .IN_MOVED_FROM, - .IN_MOVED_TO, - .IN_CREATE, - .IN_DELETE, - .IN_DELETE_SELF, - .IN_MOVE_SELF, + .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, } /*