Repository navigation
feat(libkernel): support kevent read and write events on regular files #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fb72b05
5f800c5
15d16b4
f4155dd
872c0f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -327,6 +327,11 @@ int AddKqueueKnote(const KernelEqueueRef& queue, const KernelEqueueRef& watched, | |
| return GuestErrno(queue->AddEvent(event)); | ||
| } | ||
|
|
||
| bool IsRegularFile(int descriptor) { | ||
| FileStat status{}; | ||
| return !GuestSockets::IsOpen(descriptor) && sceKernelFstat(descriptor, &status) == 0 && (status.st_mode & 0170000) == 0100000; | ||
| } | ||
|
|
||
| int AddDescriptorKnote(const KernelEqueueRef& queue, KernelEvent& change, std::uint16_t flags) { | ||
| const int descriptor = static_cast<int>(change.ident); | ||
| if (const auto watched = FindKqueue(descriptor)) { | ||
|
|
@@ -343,6 +348,15 @@ int AddDescriptorKnote(const KernelEqueueRef& queue, KernelEvent& change, std::u | |
| if (descriptor == 0 && change.filter == EVFILT_READ && File::IsHostStream(descriptor)) { | ||
| return AddStandardInputKnote(queue, change, flags); | ||
| } | ||
| if (IsRegularFile(descriptor)) { | ||
| if ((flags & EV_CLEAR) != 0) { | ||
| NotImplemented_nid_no_patch("kevent: EV_CLEAR on a regular file"); | ||
| } | ||
|
Comment on lines
+352
to
+354
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regular files still reject
Make
|
||
| const std::uint32_t notes = change.fflags; | ||
| change.fflags = 0; | ||
| change.data = 0; | ||
| return AddFileDataEvent(queue, descriptor, change.filter, notes, flags, change.udata); | ||
| } | ||
| const bool socket = GuestSockets::IsOpen(descriptor); | ||
| std::int64_t data = 0; | ||
| bool eof = false; | ||
|
|
@@ -578,6 +592,8 @@ int ModifyKnote(const KernelEqueueRef& queue, KernelEvent& change, bool setDefau | |
| RetriggerAioKnote(event); | ||
| } else if (change.filter == EVFILT_VNODE) { | ||
| SetVnodeNotes(event, change.fflags); | ||
| } else if (IsFileDataEvent(event)) { | ||
| SetFileDataNotes(event, change.fflags); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # libkernel | ||
|
|
||
| ## change [EVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvent] | ||
| ## change a shrinking truncate of a watched file can also report `NOTE_WRITE` on Windows | ||
|
Check warning on line 3 in docs/dev/debt/changes/docs-libkernel-vnode-attrib-console-1160.md
|
||
|
|
||
| - [EVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvent](../../../core/libs/prx/libkernel/Equeue/src/FileEvent.cpp) (libkernel) watch the host file or directory behind the descriptor and turn host changes into FreeBSD 11 vnode hints, which [`filt_vfsvnode`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L5032) adds whole when they share a bit with the requested `fflags`: a write that grows a file is `NOTE_WRITE | NOTE_EXTEND`, another write `NOTE_WRITE`, a shrinking truncate or an attribute change `NOTE_ATTRIB`, a higher link count `NOTE_LINK`, a lower one or a pending delete `NOTE_DELETE`, a new path `NOTE_RENAME`; in a directory, an entry created, removed or renamed is `NOTE_WRITE`, with `NOTE_LINK` for a subdirectory, and on Linux an entry moved from or to another directory adds `NOTE_EXTEND`; inotify queues the two halves of a rename one after the other, so a watcher woken by the first can read it alone, and a half read without the other is classified only after a `getdents64` on the watched directory, which takes the directory lock that `rename` holds until both halves are queued ([`do_renameat2`](https://github.com/torvalds/linux/blob/v6.8/fs/namei.c#L4975) around `vfs_rename` and its `fsnotify_move`, [`iterate_dir`](https://github.com/torvalds/linux/blob/v6.8/fs/readdir.c#L103)), an ordering taken from the kernel source, not from inotify(7), which promises none; a directory registered with `NOTE_EXTEND` on Windows is accepted, as `vfs_kqfilter` does not check it, but never receives it, because the directory handle cannot tell a move between directories from a removal plus an addition. FreeBSD's hints depend on the filesystem. On a PS5 payload process (firmware 11.60) every `ftruncate` (growing, shrinking or to the size the file has), `fchmod` to the mode the file has and `utimes` reported `NOTE_ATTRIB` alone, a `pwrite` that grew the file `NOTE_WRITE | NOTE_EXTEND` and one that did not `NOTE_WRITE`, and a second `EV_ADD` replaced the `fflags` of the first; titles are not measured. Here a growing `ftruncate` reports `NOTE_WRITE | NOTE_EXTEND` where the console reports `NOTE_ATTRIB`, and on Windows `utimes` on a file reports `NOTE_WRITE`. The three Windows notifications are told apart in the error, which names the `parent`, `entries` or `subdirectories` watch, the path it was given and `GetLastError`, but a `FindFirstChangeNotification` that fails still throws rather than returning an error to the guest: the EVFILT_VNODE attach path is [`vfs_kqfilter`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L4936), which returns `EINVAL`, `ENOMEM` or 0 after `fget` has turned a descriptor that is not open into `EBADF`, and consults no directory permission, so no FreeBSD errno stands for a host that refuses to notify, and what the console does on such a host is not measured. Only the `parent` watch can fail this way in practice: `entries` and `subdirectories` need the `FILE_LIST_DIRECTORY` that opening the watched directory already took. The parent handle produces no hint of its own, as `RunHostWatch` only calls `FindNextChangeNotification` on it, so a later change may leave the watch on its 50 ms poll instead of throwing; that is not done here. Windows uses `FindFirstChangeNotification` on the parent directory (and on a watched directory itself) and compares `GetFileInformationByHandleEx` snapshots at least every 50 ms, because a write through an open handle is reported only when the handle is flushed or closed, so file hints can arrive 50 ms late, and a file deleted while watched, which the POSIX delete of Windows 10 1903 moves into `$Extend\$Deleted` until its last handle closes, reports `NOTE_DELETE` without `NOTE_RENAME` and keeps its handle without a parent directory watch, as that directory cannot be watched; a shrinking truncate of a watched file can also report `NOTE_WRITE` on Windows, because NTFS can show the truncate's new `LastWriteTime` before its new size (2 to 12 times per million back-to-back shrinking `SetEndOfFile` calls on a loaded machine, also through a single `NtQueryInformationFile(FileAllInformation)`), and a snapshot taken in between sees a write at the old size; Linux uses inotify on the file and is not run. `NOTE_REVOKE`, `NOTE_OPEN`, `NOTE_CLOSE`, `NOTE_CLOSE_WRITE`, `NOTE_READ`, unknown bits and descriptors other than files, directories, pipes and sockets throw. `sceKernelAddFileEvent(eq, fd, watch, udata)` and `sceKernelDeleteFileEvent(eq, fd)` take the [OpenOrbis](https://github.com/OpenOrbis/OpenOrbis-PS4-Toolchain/blob/0a1aaf9d/include/orbis/libkernel.h#L35) and [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0/src/SharpProspero/Interop/Kernel/KernelEqueue.cs#L164) signatures; `watch` is taken as the `fflags` and the event is added with `EV_CLEAR`, neither confirmed on a title (feat(libkernel): implement kevent EV_DISABLE, EV_DISPATCH and EV_FORCEONESHOT) (fix(libkernel): follow kqueue_register for EV_FORCEONESHOT and modify flags) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed watcher startup leaks host handles and leaves the event without a watcher.
In
AddFileDataEvent, the event is registered beforestd::threadstarts the host watcher. If thread construction throws, the handles fromOpenHostWatchare not closed, and the registered event has no watcher. Host changes do not wake the event for that descriptor.Catch the exception from
std::thread. ClosehostwithCloseHostWatch, remove the event thatAddEventregistered, and return an error code. Alternatively, start the watcher before committing the event, and clean up the watcher if registration fails.Catch exceptions at the watcher thread entry.
On Windows,
RunHostWatchcan throw.WatchParentthrows whenFindFirstChangeNotificationWfails with an error other thanERROR_INVALID_FUNCTION. A rename can callWatchParent.RunHostWatchalso throws whenWaitForMultipleObjectsfails.This thread is detached and has no handler. An exception that leaves the thread function calls
std::terminate, which ends the process. TheAddVnodeEventworker wraps the same call in atry/catch. Use the same guard here.The guard must also release host resources when
RunHostWatchthrows.WatchParentthrows beforehost.parentis assigned, and the other handles inhoststay open. Close these handles on the failure path. Successful notifications must still reachTriggerFileReadunchanged.Suggested fix
The diff prevents process termination. It does not close the handles that
RunHostWatchowns when it throws. Close those handles on the failure path, either insideRunHostWatchor by passinghostby reference so the caller can close it.Committable suggestion