Commit Graph
21 Commits
Author SHA1 Message Date
Dean 64df590f4c Merge pull request #7076 from AlexOfTheForest/fix/socket-write-space
fix(libkernel): report free send space for socket write events on Linux
2026-10-18 09:44:37 +13:00
Dean b5e570d06a Merge pull request #7092 from milescoviello/fix/kevent-note-lowat
fix(libkernel): take NOTE_LOWAT on socket read and write events
2026-10-18 09:40:43 +13:00
Dean bdcd8a67d2 Merge pull request #6173 from PYTHONRETARDE/fix/kqueue-test-deny-sid
test(libkernel): deny the kqueue parent listing to the token user's SID
2026-10-18 00:33:09 +13:00
Dean 9ab27e5a4e Merge pull request #6599 from MarcZX100/codex/kqueue-elevated-token-privileges-6597
test(libkernel): drop backup privileges in denied parent watch
2026-10-18 00:32:40 +13:00
milescovielloandClaude Opus 5.5 91fbafd94a fix(libkernel): keep refusing a read low-water mark above 1 on non-stream sockets
On Linux, FIONREAD on a datagram socket reports the next datagram only,
where FreeBSD's sbavail counts every queued byte, so a read mark above 1
there could never fire. kevent now throws for it as before; stream
sockets and marks of 1 are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 11:20:03 +00:00
milescovielloandClaude Opus 5.5 e1d4ccba49 fix(libkernel): take NOTE_LOWAT on socket read and write events
kevent threw for NOTE_LOWAT. On a socket's EVFILT_READ it now sets the
low-water mark as FreeBSD 11's filt_soread does: the event fires once the
buffered bytes reach it, or at end of file or on a listening socket, and
a later change of the knote replaces it. EVFILT_WRITE takes a mark of 1,
where the host's writability stands in for a free byte; other write marks
and marks below 1 still throw. A pipe ignores the mark, as filt_piperead
does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 10:58:41 +00:00
Alexej Szpevák 31cd0e2604 test(libkernel): check socket write event space shrinks and recovers
On a Unix stream socketpair the write event's data drops by at least the bytes sent while the peer has not read them, and comes back to the idle value once the peer drains them. Off Linux the test only checks that the value stays positive and never grows.
2026-10-17 12:28:11 +02:00
MarcZX100andAdam Franke 083f1cc9d8 test(libkernel): drop backup privileges in denied parent watch
An elevated Windows process can bypass the parent directory DACL while holding SeBackupPrivilege or SeRestorePrivilege. Remove both privileges before DeniedParentWatch so the test exercises the denied-open path reliably.

Co-Authored-By: Adam Franke <10361523+frankea@users.noreply.github.com>
2026-10-15 22:24:04 +00:00
Felipe Arce 46d49131e7 feat(libkernel): support EVFILT_READ on the read end of a pipe
kevent threw for any descriptor that is not a guest socket. The read end
of a pipe now gets the descriptor knote, following FreeBSD 11's
filt_piperead: data is the number of buffered bytes and EV_EOF is set
once the write end is closed. GuestSockets::Ready falls back to the new
PipeReadState for a descriptor that is not a socket, and WaitAny waits on
the pipe with ppoll on Linux or in 1 ms WSAPoll slices on Windows, only
when the queue holds a pipe. On Windows the equeue waker now starts
Winsock itself, since a queue can wait on a pipe before any socket exists.
2026-10-15 21:56:19 +00:00
akkadbakkad89andDevin a7da412a71 fix(libkernel): follow kqueue_register for EV_FORCEONESHOT and modify flags
On a new knote EV_FORCEONESHOT is dropped from the flags; on an
existing one it sets EV_ONESHOT and fires the knote, as
kqueue_register does at l.1339-1341. A modify no longer ORs
EV_ONESHOT, EV_CLEAR or EV_DISPATCH into the knote.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-15 06:01:06 +00:00
akkadbakkad89andDevin b32c5a50cd feat(libkernel): implement kevent EV_DISABLE, EV_DISPATCH and EV_FORCEONESHOT
A knote registered or modified with EV_DISABLE stays registered but is
not polled, fired or reported until an EV_ENABLE change re-arms it, as
kern_event.c does with KN_DISABLED. EV_DISPATCH reports an event once
and then disables the knote, and EV_FORCEONESHOT registers as oneshot.
A negative nevents fails with EINVAL instead of throwing.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-15 06:00:43 +00:00
PYTHONRETARDEandClaude Opus 5.5 c494f33526 test(libkernel): deny the kqueue parent listing to the token user's SID
DeniedParentWatch denied FILE_LIST_DIRECTORY to the name GetUserNameW
returns. When the user name equals the computer name, LookupAccountNameW
resolves that bare name to the computer's domain SID (SidTypeDomain), so
the deny entry matched no one, the parent watch was allowed and the test
aborted on an empty error. The entry now names the SID of the process
token's user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 00:18:40 +00:00
Adrià FranchandClaude 970ff0c795 fix(libkernel): keep sem_timedwait debt entry on its own line, include <algorithm>/<cctype> in the kqueue test
Co-Authored-By: Claude <noreply@anthropic.com>
2026-10-14 22:43:43 +02:00
Rujul Talekar e9d367776d fix(libkernel): name the failing vnode watch in the kqueue notification error
The Windows error for a refused FindFirstChangeNotification was one string shared
by WatchParent and WatchContents, so the log could not tell which handle or path
was denied. Build it from the watcher, the path the API was given and
GetLastError instead.

Still throws: the EVFILT_VNODE attach path is vfs_kqfilter, which returns only
EINVAL, ENOMEM or 0, with EBADF from fget before it, and consults no directory
permission, so no FreeBSD errno stands for a host that refuses to notify.
2026-10-14 22:43:39 +02:00
Oleksandr SierovandClaude Opus 5.5 84bb450d48 feat(libkernel): EVFILT_VNODE on macOS through the host kqueue
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 10:48:35 +01:00
Jesus 50b06110bb fix(libkernel): keep the Windows vnode watch when the watched file is deleted
Windows 10 1903 deletes a file with open handles by moving it into
$Extend\$Deleted until the last handle closes, so the next snapshot of a
watched file that a title (or the test) deleted saw a new path, tried to
watch that directory's changes, got ERROR_ACCESS_DENIED and threw from
the watch thread, which aborts the process; the path change was also
reported as NOTE_RENAME next to NOTE_DELETE. A snapshot with a pending
delete or no links now reports NOTE_DELETE alone and drops the parent
directory watch instead of replacing it, as the file has no directory
to be renamed or removed from any more.
2026-10-13 13:41:05 -05:00
Dean Galvin 59fcdea9c4 fix(libkernel): pair the inotify halves of a rename in a watched directory
inotify queues IN_MOVED_FROM and IN_MOVED_TO of a rename one after the
other. The watcher thread could read the first before the second was
queued and report a rename inside the directory as a move to another
directory (NOTE_WRITE | NOTE_EXTEND instead of NOTE_WRITE), which made
guest_kqueue abort on loaded runners.

The watcher now reads the queue until it is empty and, when a move half
has no partner, calls getdents64 on the directory before reading again:
that takes the directory lock rename holds until both halves are queued.
2026-10-13 11:53:02 +13:00
Yossi Yadgar 28043ff928 test(libkernel): rename, unlink and rmdir entries of a watched directory 2026-10-12 22:37:08 +02:00
Yossi Yadgar 2aacb762dd test(libkernel): rename and delete a watched file through kqueue 2026-10-12 22:37:08 +02:00
DavidandClaude Fable 5.1 2979da6808 feat(libkernel): implement file events
Cloudberry Kingdom (PPSA23827, #3177) registers its first kevent change
as a file watch: ident 3, EVFILT_VNODE, flags 0x25
(EV_ADD|EV_ENABLE|EV_CLEAR), fflags 0x37
(NOTE_DELETE|WRITE|EXTEND|LINK|RENAME). Palworld's Discord module
(PPSA23126, #2648) imports sceKernelAddFileEvent and
sceKernelDeleteFileEvent, which no library exported.

EVFILT_VNODE watches the host file or directory behind the descriptor
and turns host changes into FreeBSD 11 vnode hints, added whole when
they share a bit with the requested fflags as filt_vfsvnode does
(sys/kern/vfs_subr.c, release/11.0.1 34bd3475): a growing write is
NOTE_WRITE|NOTE_EXTEND, a shrinking truncate or an attribute change
NOTE_ATTRIB, a link count change NOTE_LINK or NOTE_DELETE, a pending
delete NOTE_DELETE, a new path NOTE_RENAME, and in a directory a new,
removed or renamed entry NOTE_WRITE (with NOTE_LINK for a subdirectory,
as vop_mkdir_post/vop_rmdir_post). Windows uses FindFirstChangeNotification
on the parent directory (and the watched directory) and compares handle
snapshots every 50 ms, since a write through an open handle is not
reported until it is flushed or closed (measured); Linux uses inotify.
Closing the descriptor, deleting the event or the queue stops the
watcher.

sceKernelAddFileEvent(eq, fd, watch, udata) and
sceKernelDeleteFileEvent(eq, fd) take the OpenOrbis 0a1aaf9d
(libkernel.h:35,109) and SharpProspero 5dfdb4b0 (KernelEqueue.cs:164,168)
signatures; NIDs cG3t15OK4Ow and UmXngHKB6is confirmed with
tools/nid_names.py. The watch bits are the fflags and the event is added
with EV_CLEAR, which is not confirmed on a title.

Throws: NOTE_REVOKE, NOTE_OPEN, NOTE_CLOSE, NOTE_CLOSE_WRITE, NOTE_READ
and unknown bits, NOTE_EXTEND on a directory on Windows (a move from
another directory cannot be told from a creation) and descriptors that
are not files, directories, pipes or sockets (pipes, sockets and kqueues
fail with EINVAL as in FreeBSD). Recorded in TechnicalDebt.

guest_kqueue adds a vnode watch that sees a write to a temporary file
with Cloudberry's flags, fflags filtering, a re-registration, the sce
pair on an equeue, EINVAL for sockets, pipes and the kqueue itself, the
close of a watched descriptor and a directory watch.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-12 22:37:08 +02:00
DavidandClaude Fable 5.1 9ef4aa2c40 feat(libkernel): implement kqueue and kevent over the event queue
Cloudberry Kingdom (PPSA23827, #3177) calls kqueue right after start-up
and stopped on the throwing declarations merged with #3337; Palworld's
Discord module (PPSA23126, #2648) imports kevent.

kqueue reserves a host null-device descriptor in the file descriptor
space and registers it as an event queue that reports each ready event
once per call and moves level-triggered ones behind the others, as
kqueue_scan does. close and sceKernelClose close the queue and drop the
descriptor's events from every queue, dup2 drops them for the target,
and dup/dup2 of a kqueue throw, so a reused descriptor number is never
read as a stale kqueue.

kevent follows FreeBSD 11 kern_event.c (release/11.0.1 34bd3475):
kqueue_kevent for the changelist (filter 0 skipped, EV_SYSFLAGS
cleared, EV_ERROR receipts with the errno in data, EV_RECEIPT, the
early return when nevents is exhausted), kqueue_register for EV_ADD,
EV_DELETE, EV_ENABLE, EBADF, ENOENT and EINVAL, kqueue_scan for the
timeout (EINVAL for a bad timespec, zero polls, above INT32_MAX waits
forever) and filt_timerattach, filt_usertouch for the timer and user
filters. Read and write events reuse the guest socket poll of
sceKernelAddReadEvent/WriteEvent.

Throws: other filters, EV_DISABLE, EV_DISPATCH, EV_FORCEONESHOT,
NOTE_LOWAT, EV_CLEAR on a socket, read/write events on non-socket
descriptors or another kqueue, changes to an existing timer (11.0 and
11.4 differ), a NOTE_NSECONDS fraction above one second and a negative
nevents. Recorded in TechnicalDebt.

Test guest_kqueue covers creation and close, timers, a user event
triggered from another thread, read and write events on a socket pair,
EV_ERROR receipts, the timeout and the error cases.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-12 22:36:59 +02:00