Commit Graph
37 Commits
Author SHA1 Message Date
Federico Marras cc7b9a7652 Merge remote-tracking branch 'upstream/main' into feat/kqueue-read-on-kqueue
# Conflicts:
#	docs/dev/debt/libkernel.md
2026-10-21 13:17:12 +02:00
Ethan Barr 084dcf8736 fix(libkernel): fire EVFILT_WRITE on a socket once free send space reaches NOTE_LOWAT
kevent threw NotImplemented for EVFILT_WRITE with NOTE_LOWAT above 1 on a socket, although the socket readiness check already reports free send-buffer space (SO_SNDBUF less SIOCOUTQ on Linux). The write event now fires once that space reaches the mark, as FreeBSD 11's filt_sowrite compares sbspace with kn_sdata, and stays silent while the space is below it. The debt entry no longer says a write mark is limited to 1. Reference: https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_socket.c
2026-10-20 13:32:17 -04:00
Federico Marras 5f6c30547e Merge remote-tracking branch 'upstream/main' into feat/kqueue-read-on-kqueue
# Conflicts:
#	docs/dev/debt/libkernel.md
2026-10-20 17:18:22 +02:00
Federico Marras 6ead14f9e8 feat(libkernel): watch a kqueue with EVFILT_READ 2026-10-20 16:56:06 +02:00
Adrià FranchandClaude Sonnet 5.5 99e56464a1 fix(libkernel): keep the PipeWrite full-pipe case off Windows and start its clock before the reader
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 13:35:07 +02:00
Alexej Szpevák bd8ee518c4 feat(libkernel): support EVFILT_WRITE on the write end of a pipe
kevent threw for EVFILT_WRITE on any descriptor that is not a socket. On the write end of a pipe it now follows FreeBSD 11's filt_pipewrite: data is the free buffer space, and once the read end is closed the event reports EV_EOF with data 0. Adding the event after the read end is closed fails with EPIPE, as in pipe_kqfilter. EVFILT_WRITE on the read end still throws, because FreeBSD's pipe ends are bidirectional and host pipes are not.
2026-10-20 13:34:50 +02:00
Adrià Franch 8dd6217ded test(libkernel): take the EV_CLEAR wait clock before starting the sender 2026-10-19 15:05:52 +02:00
Alexej Szpevák d2cae8847d test(libkernel): cover EV_CLEAR on a pipe and with EV_DISPATCH
An EV_CLEAR read knote on a pipe fires once per write and reports EV_EOF with no data once the write end closes. Combined with EV_DISPATCH it reports once and disables itself; EV_ENABLE re-runs the filter as kqueue_register does, so data that arrived while disabled is reported, and an enable with nothing buffered reports nothing.
2026-10-19 15:05:28 +02:00
Alexej Szpevák 34447f7956 test(libkernel): cover EV_CLEAR writes on a socket
An EV_CLEAR write knote fires once when added to a writable socket, stays quiet after the guest's own send, fires again when the peer drains the data and frees the send space, fires on a fresh EV_ADD, and reports EV_EOF when the peer closes.
2026-10-19 15:05:28 +02:00
Alexej Szpevák 07fd38ae7c test(libkernel): cover EV_CLEAR reads on a socket
On a Unix stream socketpair an EV_CLEAR read knote fires once per arrival with the full byte count, stays quiet while data sits unread, without the wait spinning on the readable socket, fires again when re-added, is dropped when an arrival was read before the next scan, wakes a blocking kevent, and reports EV_EOF once when the peer closes.
2026-10-19 15:05:28 +02:00
Alexej Szpevák cb9a4c7417 feat(libkernel): support EV_CLEAR on sockets and pipes in kevent on Linux
kevent threw for EV_CLEAR on a socket or the read end of a pipe; Boost.Asio's kqueue reactor, among others, registers every socket that way. On Linux such a knote now watches its host descriptor through an edge watch. It is armed when added, on every later EV_ADD or EV_ENABLE while enabled, and by each host wakeup; when armed it is checked with the level-triggered readiness test and reported with the current data and EV_EOF, or disarmed if no longer ready, as kqueue_scan re-runs the filter; reporting it disarms it until the next edge. Waits block on the watch's epoll descriptor, so a socket that stays readable does not wake them. Windows and macOS still throw. GuestSockets::NativeDescriptor exposes the host socket behind a guest one for the watch.
2026-10-19 15:05:28 +02:00
Dean 1c2824c8de Merge pull request #7670 from thuanquang/test/kqueue-pipe-read-start
test(libkernel): time blocking waits from before the writer thread starts
2026-10-19 23:53:40 +13:00
thuanquangandClaude Opus 5.5 e0da1fb1c9 test(libkernel): time blocking waits from before the writer thread starts
PipeRead (guest_kqueue) and the blocking read in guest_socket_events took
their start time after creating the thread that writes after 50 ms, so a
main thread preempted between the two measured less than the writer's
sleep and failed the 40 ms check although the wait did block until the
write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 04:18:05 +07:00
klosixgaming-arch ce5b0c395b feat(libkernel): accept a kevent read event on descriptor 0
PPSA34015 (EA SPORTS FC 27) registers EVFILT_READ for descriptor 0 in a
new kqueue at start-up, ignores the result, and stopped with "kevent:
EVFILT_READ on a non-socket descriptor not implemented". A read event on
the host stream on descriptor 0 is now accepted and never triggers, as
select never reports descriptor 0 readable.
2026-10-17 21:34:51 +00:00
Dean d8f36fd749 Merge pull request #7206 from MarcZX100/codex/kevent-nanosecond-period
fix(libkernel): accept fractional nanosecond kevent periods
2026-10-18 10:01:02 +13:00
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
MarcZX100 d71a1c5775 fix(libkernel): accept fractional nanosecond kevent periods 2026-10-17 16:28:55 +00: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