feat(libkernel): support fcntl and ioctl on non-socket descriptors #3840

Merged
ahir061 merged 3 commits from upstream-pr/3840 into main 2026-10-12 10:53:25 +00:00
ahir061 commented 2026-10-11 17:42:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T17:42:00Z.


What

Carries screamm's screamm/AnyPS5:feat/libkernel-fcntl-files from #3757 unchanged, with their authorship (two commits on main a11c8f7f):

  • 70dd0c00 feat(libkernel): support fcntl on non-socket descriptors
  • 88c9ee8d feat(libkernel): support ioctl on non-socket descriptors

fcntl, sceKernelFcntl, ioctl and _ioctl worked only on libSceNet sockets, so every file, pipe and standard stream got EBADF. Yamagi Quake II stops on it (Sys_Error: change stdin to blocking Bad file descriptor), and XPSemu, VLC, RetroArch, Porpoise and Ruffle call fcntl on such descriptors (#3757).

The new File/src/DescriptorControl.cpp keeps a close-on-exec flag per descriptor and a status-flag set shared by duplicates, following FreeBSD 11.4 kern_fcntl/kern_dup. It covers F_GETFD/F_SETFD, F_GETFL/F_SETFL, F_DUPFD/F_DUPFD_CLOEXEC, F_RDAHEAD/F_READAHEAD and record locks on Linux. O_NONBLOCK on a pipe makes empty reads and full writes return EAGAIN. ioctl answers FIONBIO, FIONREAD (file size minus offset; queued bytes on a pipe's read end) and ENOTTY for the terminal requests. What it does not model throws with the flag and descriptor kind; #3757 lists the cases. Sockets keep their existing path. The fcntl, sceKernelFcntl and ioctl TechnicalDebt entries are updated.

It touches the same close/dup/pipe code in Stdio.cpp and Open.cpp as #3649, #3644 and #3681, so whichever merges second needs a rebase; I'll keep this one current.

Closes #3757.

Tested

  • Windows, by screamm (#3757): Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, BUILD_TESTING=ON, full build including libs: guest_kernel_fcntl, guest_dup, guest_udp, guest_socket_pair, guest_socket_read_write, guest_poll, guest_terminal, guest_file_vector, guest_aio 3/3.
  • Linux, for this carry: Ubuntu 24.04, GCC 13.3, Ninja, Release, BUILD_TESTING=ON, head 88c9ee8d:
    • The same nine tests pass. This also covers the Linux-only paths in guest_kernel_fcntl: record locks through the host's POSIX locks, and clearing O_APPEND on a file.
    • Built against main's libkernel, guest_kernel_fcntl aborts (sceKernelFcntl: file descriptors are not implemented, fd=5) and guest_dup aborts; both pass with these commits.
    • All 88 libkernel tests registered on Linux: 85 pass. guest_truncate and guest_kernel_stack_protect (#3459) abort the same way on unchanged main in this environment. guest_raise_exception passes but takes 72 s with these commits and 71 s on main, over its 30 s timeout only because the test machine has a single core.
  • tools/check_conventions.py --base main passes.
  • Not run: titles.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821024471 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-11T17:42:00Z. --- ### What Carries screamm's `screamm/AnyPS5:feat/libkernel-fcntl-files` from #3757 unchanged, with their authorship (two commits on `main` `a11c8f7f`): - `70dd0c00` feat(libkernel): support fcntl on non-socket descriptors - `88c9ee8d` feat(libkernel): support ioctl on non-socket descriptors `fcntl`, `sceKernelFcntl`, `ioctl` and `_ioctl` worked only on libSceNet sockets, so every file, pipe and standard stream got `EBADF`. Yamagi Quake II stops on it (`Sys_Error: change stdin to blocking Bad file descriptor`), and XPSemu, VLC, RetroArch, Porpoise and Ruffle call `fcntl` on such descriptors (#3757). The new `File/src/DescriptorControl.cpp` keeps a close-on-exec flag per descriptor and a status-flag set shared by duplicates, following FreeBSD 11.4 `kern_fcntl`/`kern_dup`. It covers `F_GETFD`/`F_SETFD`, `F_GETFL`/`F_SETFL`, `F_DUPFD`/`F_DUPFD_CLOEXEC`, `F_RDAHEAD`/`F_READAHEAD` and record locks on Linux. `O_NONBLOCK` on a pipe makes empty reads and full writes return `EAGAIN`. `ioctl` answers `FIONBIO`, `FIONREAD` (file size minus offset; queued bytes on a pipe's read end) and `ENOTTY` for the terminal requests. What it does not model throws with the flag and descriptor kind; #3757 lists the cases. Sockets keep their existing path. The `fcntl`, `sceKernelFcntl` and `ioctl` TechnicalDebt entries are updated. It touches the same `close`/`dup`/`pipe` code in `Stdio.cpp` and `Open.cpp` as #3649, #3644 and #3681, so whichever merges second needs a rebase; I'll keep this one current. Closes #3757. ### Tested - Windows, by screamm (#3757): Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `BUILD_TESTING=ON`, full build including `libs`: `guest_kernel_fcntl`, `guest_dup`, `guest_udp`, `guest_socket_pair`, `guest_socket_read_write`, `guest_poll`, `guest_terminal`, `guest_file_vector`, `guest_aio` 3/3. - Linux, for this carry: Ubuntu 24.04, GCC 13.3, Ninja, Release, `BUILD_TESTING=ON`, head `88c9ee8d`: - The same nine tests pass. This also covers the Linux-only paths in `guest_kernel_fcntl`: record locks through the host's POSIX locks, and clearing `O_APPEND` on a file. - Built against `main`'s libkernel, `guest_kernel_fcntl` aborts (`sceKernelFcntl: file descriptors are not implemented, fd=5`) and `guest_dup` aborts; both pass with these commits. - All 88 libkernel tests registered on Linux: 85 pass. `guest_truncate` and `guest_kernel_stack_protect` (#3459) abort the same way on unchanged `main` in this environment. `guest_raise_exception` passes but takes 72 s with these commits and 71 s on `main`, over its 30 s timeout only because the test machine has a single core. - `tools/check_conventions.py --base main` passes. - Not run: titles. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
MarcZX10 commented 2026-10-11 18:00:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T18:00:29Z.

Code review location: core/libs/prx/libkernel/Socket/src/Udp.cpp; line 494

@@ -486,6 +487,20 @@ bool GuestSockets::Ready(int descriptor, bool write, std::int64_t* data, bool* e
 
 extern "C" {
 int APS5_VABI fcntl_nid_postfix(int descriptor, int command, ...) {
+    if (descriptor < GuestSockets::FirstDescriptor) {
+#ifdef _WIN32
+        __builtin_sysv_va_list arguments;
+        __builtin_sysv_va_start(arguments, command);
+        const auto argument = __builtin_va_arg(arguments, long long);

Could this only read the variadic argument for commands that require one, and read the command’s actual argument type? F_GETFL and F_GETFD have no third argument, but the non-socket path always calls va_arg here (and reads long at the POSIX branch below). The new tests call fcntl_nid_postfix(file, GetFlags) without a third argument at GuestKernelFcntl.cpp:102 and :175; fetching a missing vararg is undefined behavior. Commands that do take an argument also use different promoted types: flag/descriptor commands pass int, while lock commands pass a pointer. A command-specific extraction would avoid both mismatches on Windows and POSIX.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4242608967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840#discussion_r4242608967). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T18:00:29Z. **Code review location:** `core/libs/prx/libkernel/Socket/src/Udp.cpp`; line 494 ~~~~diff @@ -486,6 +487,20 @@ bool GuestSockets::Ready(int descriptor, bool write, std::int64_t* data, bool* e extern "C" { int APS5_VABI fcntl_nid_postfix(int descriptor, int command, ...) { + if (descriptor < GuestSockets::FirstDescriptor) { +#ifdef _WIN32 + __builtin_sysv_va_list arguments; + __builtin_sysv_va_start(arguments, command); + const auto argument = __builtin_va_arg(arguments, long long); ~~~~ --- Could this only read the variadic argument for commands that require one, and read the command’s actual argument type? `F_GETFL` and `F_GETFD` have no third argument, but the non-socket path always calls `va_arg` here (and reads `long` at the POSIX branch below). The new tests call `fcntl_nid_postfix(file, GetFlags)` without a third argument at `GuestKernelFcntl.cpp:102` and `:175`; fetching a missing vararg is undefined behavior. Commands that do take an argument also use different promoted types: flag/descriptor commands pass `int`, while lock commands pass a pointer. A command-specific extraction would avoid both mismatches on Windows and POSIX.
ahir061 commented 2026-10-11 18:13:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T18:13:02Z.

Code review location: core/libs/prx/libkernel/Socket/src/Udp.cpp; line 494

@@ -486,6 +487,20 @@ bool GuestSockets::Ready(int descriptor, bool write, std::int64_t* data, bool* e
 
 extern "C" {
 int APS5_VABI fcntl_nid_postfix(int descriptor, int command, ...) {
+    if (descriptor < GuestSockets::FirstDescriptor) {
+#ifdef _WIN32
+        __builtin_sysv_va_list arguments;
+        __builtin_sysv_va_start(arguments, command);
+        const auto argument = __builtin_va_arg(arguments, long long);

Good catch, thanks. Fixed in the latest commit: File::FcntlArgumentOf maps each command to no argument, an int or a pointer, and fcntl and sceKernelFcntl now read only that. F_GETFD, F_GETFL, F_GETOWN and unknown commands read nothing; the flag, descriptor and read-ahead commands read an int; the lock commands read a pointer.

It also fixed a real case: F_READAHEAD with -1 was read as 4294967295 and enabled read-ahead. guest_kernel_fcntl now checks -1 and 4096 through both entry points; it fails before this commit and passes after (Linux, GCC 13.3). The nine related tests pass, the changed files compile with MinGW-w64, and screamm’s two commits are unchanged.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4242649080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840#discussion_r4242649080). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-11T18:13:02Z. **Code review location:** `core/libs/prx/libkernel/Socket/src/Udp.cpp`; line 494 ~~~~diff @@ -486,6 +487,20 @@ bool GuestSockets::Ready(int descriptor, bool write, std::int64_t* data, bool* e extern "C" { int APS5_VABI fcntl_nid_postfix(int descriptor, int command, ...) { + if (descriptor < GuestSockets::FirstDescriptor) { +#ifdef _WIN32 + __builtin_sysv_va_list arguments; + __builtin_sysv_va_start(arguments, command); + const auto argument = __builtin_va_arg(arguments, long long); ~~~~ --- Good catch, thanks. Fixed in the latest commit: File::FcntlArgumentOf maps each command to no argument, an int or a pointer, and fcntl and sceKernelFcntl now read only that. F_GETFD, F_GETFL, F_GETOWN and unknown commands read nothing; the flag, descriptor and read-ahead commands read an int; the lock commands read a pointer. It also fixed a real case: F_READAHEAD with -1 was read as 4294967295 and enabled read-ahead. guest_kernel_fcntl now checks -1 and 4096 through both entry points; it fails before this commit and passes after (Linux, GCC 13.3). The nine related tests pass, the changed files compile with MinGW-w64, and screamm’s two commits are unchanged.
ahir061 commented 2026-10-11 22:28:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T22:28:11Z.


Rebased onto current main (11d9c00c). Two conflicts: the ioctl, _ioctl TechnicalDebt entry next to the new sendfile entry from #3568 (both kept), and the Windows NativeWrite in Open.cpp, where main's shared-memory write path and this PR's pipe ENOSPC → EAGAIN mapping are now combined: shared-memory descriptors go through WriteSharedMemory_nid_no_patch first, everything else through _write with the pipe mapping. Re-tested on Linux (Ubuntu 24.04, GCC 13.3): the nine fcntl/ioctl tests and guest_ioctl_getsid pass, and 98 of 99 libkernel tests pass (guest_named_shm_concurrency fails the same way on unchanged main there, because the run is as root). The changed files compile with MinGW-w64, and tools/check_conventions.py passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114596754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840#issuecomment-6114596754). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-11T22:28:11Z. --- Rebased onto current main (11d9c00c). Two conflicts: the `ioctl, _ioctl` TechnicalDebt entry next to the new `sendfile` entry from #3568 (both kept), and the Windows `NativeWrite` in `Open.cpp`, where main's shared-memory write path and this PR's pipe `ENOSPC` → `EAGAIN` mapping are now combined: shared-memory descriptors go through `WriteSharedMemory_nid_no_patch` first, everything else through `_write` with the pipe mapping. Re-tested on Linux (Ubuntu 24.04, GCC 13.3): the nine fcntl/ioctl tests and `guest_ioctl_getsid` pass, and 98 of 99 libkernel tests pass (`guest_named_shm_concurrency` fails the same way on unchanged main there, because the run is as root). The changed files compile with MinGW-w64, and `tools/check_conventions.py` passes.
ahir061 commented 2026-10-12 06:50:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-12T06:50:43Z.


Rebased again onto current main (d06c0656). #3968 (openat) moved sceKernelOpen's open path into OpenNative; the descriptor registration from this PR now sits at the end of OpenNative, so descriptors opened through openat get the same fcntl and ioctl handling, and the random-device branch registers its descriptor as before. Re-tested on Linux: the nine fcntl/ioctl tests, guest_ioctl_getsid and guest_kernel_sys (openat) pass; the changed files compile with MinGW-w64; tools/check_conventions.py passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6119249843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840#issuecomment-6119249843). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-12T06:50:43Z. --- Rebased again onto current main (d06c0656). #3968 (openat) moved sceKernelOpen's open path into `OpenNative`; the descriptor registration from this PR now sits at the end of `OpenNative`, so descriptors opened through `openat` get the same `fcntl` and `ioctl` handling, and the random-device branch registers its descriptor as before. Re-tested on Linux: the nine fcntl/ioctl tests, `guest_ioctl_getsid` and `guest_kernel_sys` (openat) pass; the changed files compile with MinGW-w64; `tools/check_conventions.py` passes.
github-actions[bot] commented 2026-10-12 06:54:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T06:54:30Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3649 core/libs/prx/libkernel/CMakeLists.txt L5
core/libs/prx/libkernel/File/src/Stdio.cpp L14-15, L421, L526
#3787 core/libs/prx/libkernel/File/src/Open.cpp L206
core/libs/prx/libkernel/File/src/Stdio.cpp L476, L502, L526, L536
#4379 core/libs/prx/libkernel/File/src/Stdio.cpp L457
#4391 core/libs/prx/libkernel/File/src/Open.cpp L205-206, L303-308
#4411 docs/dev/TechnicalDebt.md L596-698
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6119296994 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840#issuecomment-6119296994). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T06:54:30Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3649 | `core/libs/prx/libkernel/CMakeLists.txt` [L5](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/CMakeLists.txt#L5)<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` [L14-15](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L14-L15), [L421](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L421), [L526](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L526) | | | | #3787 | `core/libs/prx/libkernel/File/src/Open.cpp` [L206](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Open.cpp#L206)<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` [L476](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L476), [L502](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L502), [L526](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L526), [L536](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L536) | | | | #4379 | `core/libs/prx/libkernel/File/src/Stdio.cpp` [L457](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Stdio.cpp#L457) | | | | #4391 | `core/libs/prx/libkernel/File/src/Open.cpp` [L205-206](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Open.cpp#L205-L206), [L303-308](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libkernel/File/src/Open.cpp#L303-L308) | | | | #4411 | `docs/dev/TechnicalDebt.md` [L596-698](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/docs/dev/TechnicalDebt.md#L596-L698) | | |
xXPBSXx commented 2026-10-12 07:53:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T07:53:27Z.


Title test for the fcntl/ioctl change (this PR; the commits come from screamm's branch in #3757), Windows 10, MinGW GCC, RTX 4080 SUPER: this PR merged locally onto current main d06c06565 (clean merge, no conflicts). Full build with -j4 succeeded; the ctest subset matching fcntl|ioctl|file|open|descriptor|stdio|pipe|dup|kernel ran 44 tests: 44 passed (video_out_flip_reopen skipped).

PPSA24423 (0 absent imports; stopped on main ceb342b94 at sceKernelFcntl: file descriptors are not implemented, fd=6, see #3757): with this change the fcntl abort is gone. The run (150 s limit) then ends after about 70 s with a different abort:

what():  AGC graphics: vertex buffer aliases the render target
FATAL: abort() (SIGABRT)

So the title gets past fcntl and reaches rendering. I did not look at the picture and one run only.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6120080526 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3840#issuecomment-6120080526). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T07:53:27Z. --- Title test for the fcntl/ioctl change (this PR; the commits come from screamm's branch in #3757), Windows 10, MinGW GCC, RTX 4080 SUPER: this PR merged locally onto current `main` `d06c06565` (clean merge, no conflicts). Full build with `-j4` succeeded; the `ctest` subset matching `fcntl|ioctl|file|open|descriptor|stdio|pipe|dup|kernel` ran 44 tests: 44 passed (`video_out_flip_reopen` skipped). PPSA24423 (0 absent imports; stopped on `main` `ceb342b94` at `sceKernelFcntl: file descriptors are not implemented, fd=6`, see #3757): with this change the fcntl abort is gone. The run (150 s limit) then ends after about 70 s with a different abort: ``` what(): AGC graphics: vertex buffer aliases the render target FATAL: abort() (SIGABRT) ``` So the title gets past `fcntl` and reaches rendering. I did not look at the picture and one run only. AI-assisted (Claude Code).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!3840
No description provided.