fix(libkernel): answer F_GETFD and F_SETFD on a guest socket #5020

Merged
xXPBSXx merged 2 commits from upstream-pr/5020 into main 2026-10-14 19:03:19 +00:00
xXPBSXx commented 2026-10-13 08:37:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T08:37:20Z.


What

fcntl on a guest socket answered F_GETFL and F_SETFL and failed every other command with EINVAL. libcurl calls fcntl(fd, F_SETFD, FD_CLOEXEC) on every new socket, and it failed:

  • Title evidence: PPSA99001 (ProsperoRadio, bundled libcurl 8.18.0 and OpenSSL 3.5.2 over libkernel sockets), traced by screamm on main in this comment on #4796: fcntl(F_SETFD) on a socket returns EINVAL (the comment lists it among the "smaller mismatches curl tolerates, for later"); libcurl issues fcntl(fd, F_SETFD, FD_CLOEXEC) on every new socket. The same group of titles (PSXS5, SwanStation, ProsperoTV, ProsperoRadio) uses that libcurl. No title fails because of this answer: curl ignores the error, so the change replaces a wrong answer in the trace of these titles and does not unblock one.

F_GETFD and F_SETFD are now answered for a guest socket as FreeBSD 11.4's kern_fcntl does (FD_CLOEXEC, value 1):

  • F_SETFD sets or clears the flag, F_GETFD reads it back (a new socket starts at 0);
  • SOCK_CLOEXEC in the type of socket sets it at creation;
  • dup and dup2 give the copy no flag (FreeBSD's kern_dup clears it) and leave the original's; close forgets it, so the next socket on the same number starts at 0.

The flag is only recorded: there is no exec in the guest. Not changed, and in the TechnicalDebt entry for libkernel: the other commands on a socket still fail with EINVAL (F_DUPFD, F_DUPFD_CLOEXEC, F_GETOWN, F_SETOWN), and an accepted socket starts without the flag.

Overlap: this branch was merged with main 00f4b3351 (conflict in docs/dev/debt/libkernel.md next to the ioctl entry, resolved by keeping both entries; git merge-tree against main is clean). #4954 (fcntl and O_APPEND), #5186 (FIOCLEX and FIONCLEX) and #5569 (ENOTTY for malformed ioctl requests) touch the same part of docs/dev/debt/libkernel.md; whichever lands second rebases its entry.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, head 863e506b2 (this branch merged with main 00f4b3351):

  • The socket group passes, 25 of 25 (guest_socket*, including the twelve guest_socket_host_write_*, guest_sce_net, guest_udp, guest_tcp, guest_poll, guest_dup, guest_kernel_fcntl), ctest -V, serial, after building the test executables of exactly these tests (log run_5020_fix2.log). guest_socket_descriptor_flags (new) is one of them: F_GETFD on a new socket, F_SETFD and read-back, dup and dup2 give the copy no flag and keep the original's, F_SETFD 0, SOCK_CLOEXEC, and the next socket after a closed one starts at 0.
  • Without the change to Udp.cpp (taken from main, test kept; measured on the previous head b09eccdc4, whose Udp.cpp is the same) it fails: a new socket must not be close-on-exec (F_GETFD 0) (errno 22) (log run_5020.log).
  • A first run on that previous head showed 12 failures in the guest_socket_host_write_* tests; the cause was an outdated test executable (not rebuilt). After rebuilding it they pass, here and on main.
  • python tools/check_conventions.py --base upstream/main passes.
  • Not tested: Linux (one source for both hosts, only Windows was built), a console, a title run with this change (the evidence is screamm's trace).

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 (docs/dev/debt/libkernel.md)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA99001 (ProsperoRadio) and the titles of its group: a clean fcntl trace on their sockets; no bring-up discussion exists
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4840604200 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5020). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T08:37:20Z. --- ### What `fcntl` on a guest socket answered `F_GETFL` and `F_SETFL` and failed every other command with `EINVAL`. libcurl calls `fcntl(fd, F_SETFD, FD_CLOEXEC)` on every new socket, and it failed: - Title evidence: PPSA99001 (ProsperoRadio, bundled libcurl 8.18.0 and OpenSSL 3.5.2 over libkernel sockets), traced by screamm on `main` in [this comment on #4796](https://github.com/boykopovar/AnyPS5/pull/4796#issuecomment-6140849638): `fcntl(F_SETFD)` on a socket returns `EINVAL` (the comment lists it among the "smaller mismatches curl tolerates, for later"); libcurl issues `fcntl(fd, F_SETFD, FD_CLOEXEC)` on every new socket. The same group of titles (PSXS5, SwanStation, ProsperoTV, ProsperoRadio) uses that libcurl. No title fails because of this answer: curl ignores the error, so the change replaces a wrong answer in the trace of these titles and does not unblock one. `F_GETFD` and `F_SETFD` are now answered for a guest socket as FreeBSD 11.4's `kern_fcntl` does (`FD_CLOEXEC`, value 1): - `F_SETFD` sets or clears the flag, `F_GETFD` reads it back (a new socket starts at 0); - `SOCK_CLOEXEC` in the type of `socket` sets it at creation; - `dup` and `dup2` give the copy no flag (FreeBSD's `kern_dup` clears it) and leave the original's; `close` forgets it, so the next socket on the same number starts at 0. The flag is only recorded: there is no `exec` in the guest. Not changed, and in the TechnicalDebt entry for libkernel: the other commands on a socket still fail with `EINVAL` (`F_DUPFD`, `F_DUPFD_CLOEXEC`, `F_GETOWN`, `F_SETOWN`), and an accepted socket starts without the flag. Overlap: this branch was merged with `main` `00f4b3351` (conflict in `docs/dev/debt/libkernel.md` next to the `ioctl` entry, resolved by keeping both entries; `git merge-tree` against `main` is clean). #4954 (`fcntl` and `O_APPEND`), #5186 (`FIOCLEX` and `FIONCLEX`) and #5569 (`ENOTTY` for malformed `ioctl` requests) touch the same part of `docs/dev/debt/libkernel.md`; whichever lands second rebases its entry. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, head `863e506b2` (this branch merged with `main` `00f4b3351`): - The socket group passes, 25 of 25 (`guest_socket*`, including the twelve `guest_socket_host_write_*`, `guest_sce_net`, `guest_udp`, `guest_tcp`, `guest_poll`, `guest_dup`, `guest_kernel_fcntl`), ctest `-V`, serial, after building the test executables of exactly these tests (log `run_5020_fix2.log`). `guest_socket_descriptor_flags` (new) is one of them: `F_GETFD` on a new socket, `F_SETFD` and read-back, `dup` and `dup2` give the copy no flag and keep the original's, `F_SETFD 0`, `SOCK_CLOEXEC`, and the next socket after a closed one starts at 0. - Without the change to `Udp.cpp` (taken from `main`, test kept; measured on the previous head `b09eccdc4`, whose `Udp.cpp` is the same) it fails: `a new socket must not be close-on-exec (F_GETFD 0) (errno 22)` (log `run_5020.log`). - A first run on that previous head showed 12 failures in the `guest_socket_host_write_*` tests; the cause was an outdated test executable (not rebuilt). After rebuilding it they pass, here and on `main`. - `python tools/check_conventions.py --base upstream/main` passes. - Not tested: Linux (one source for both hosts, only Windows was built), a console, a title run with this change (the evidence is screamm's trace). ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt (`docs/dev/debt/libkernel.md`) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA99001 (ProsperoRadio) and the titles of its group: a clean `fcntl` trace on their sockets; no bring-up discussion exists - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-14 17:31:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:31:57Z.


Checked on current main (merged and rebased): build + ctest green (169 tests in core/libs and libkernel), and guest_socket_descriptor_flags fails without the Udp.cpp change. F_GETFD/F_SETFD, SOCK_CLOEXEC, dup/dup2 and close match FreeBSD's kern_fcntl/kern_dup.

Added on your branch: rebased onto main (conflict in docs/dev/debt/libkernel.md, kept both entries) and updated the fcntl entry, which still said F_GETFD/F_SETFD on a socket fail with EINVAL. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174359256 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5020#issuecomment-6174359256). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T17:31:57Z. --- Checked on current main (merged and rebased): build + ctest green (169 tests in `core/libs` and libkernel), and `guest_socket_descriptor_flags` fails without the `Udp.cpp` change. `F_GETFD`/`F_SETFD`, `SOCK_CLOEXEC`, `dup`/`dup2` and `close` match FreeBSD's `kern_fcntl`/`kern_dup`. Added on your branch: rebased onto main (conflict in `docs/dev/debt/libkernel.md`, kept both entries) and updated the `fcntl` entry, which still said `F_GETFD`/`F_SETFD` on a socket fail with `EINVAL`. Merging once the full Build is green.
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!5020
No description provided.