fix(libkernel): handle FIOCLEX and FIONCLEX in ioctl #4505

Closed
milescoviello wants to merge 1 commit from upstream-pr/4505 into main
milescoviello commented 2026-10-12 13:47:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T13:47:49Z.


What

ioctl() on a file, directory, pipe or character device threw ioctl: request 0x20006601 on a file is not implemented for FIOCLEX (and the same for FIONCLEX), although the close-on-exec flag those requests change is already kept for these descriptors and reported by fcntl(F_GETFD).

FreeBSD 11.4 handles both requests in kern_ioctl() for every descriptor, before the request reaches the file's own ioctl, by changing the flag F_GETFD reads (sys/kern/sys_generic.c):

	case FIONCLEX:
		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
		goto out;
	case FIOCLEX:
		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
		goto out;

and kern_fcntl() (sys/kern/kern_descrip.c): td->td_retval[0] = (fde->fde_flags & UF_EXCLOSE) ? FD_CLOEXEC : 0;.

Both requests now set or clear that flag and return 0; the TechnicalDebt entry for ioctl lists them. libuv up to 1.43 sets close-on-exec this way on FreeBSD (uv__cloexec is uv__cloexec_ioctl there), next to the FIONBIO calls already handled. Guest sockets are unchanged: they do not keep a close-on-exec flag (F_GETFD fails with EINVAL on them).

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main c6efe068. guest_kernel_fcntl now sets and clears the flag with FIOCLEX/FIONCLEX on a file and on both ends of a pipe and reads it back with F_GETFD.

  • Red: with main's DescriptorControl.cpp the test aborts with ioctl: request 0x20006601 on a file is not implemented, fd=6.
  • Green: guest_kernel_fcntl passes, also in 300 repeated runs; all guest_* tests except guest_video_out_* pass (306/306, three runs).

Not tested on Windows; the change does not depend on the host.

Note: my #4480 edits the fcntl entry right above the ioctl one in docs/dev/debt/libkernel.md, so whichever of the two lands second needs a one-line rebase there; the code files merge cleanly. Both are rebased onto the per-library debt files from #4411.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4830333077 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4505). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T13:47:49Z. --- ### What `ioctl()` on a file, directory, pipe or character device threw `ioctl: request 0x20006601 on a file is not implemented` for `FIOCLEX` (and the same for `FIONCLEX`), although the close-on-exec flag those requests change is already kept for these descriptors and reported by `fcntl(F_GETFD)`. FreeBSD 11.4 handles both requests in `kern_ioctl()` for every descriptor, before the request reaches the file's own ioctl, by changing the flag `F_GETFD` reads (`sys/kern/sys_generic.c`): ```c case FIONCLEX: fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; goto out; case FIOCLEX: fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; goto out; ``` and `kern_fcntl()` (`sys/kern/kern_descrip.c`): `td->td_retval[0] = (fde->fde_flags & UF_EXCLOSE) ? FD_CLOEXEC : 0;`. Both requests now set or clear that flag and return 0; the TechnicalDebt entry for `ioctl` lists them. libuv up to 1.43 sets close-on-exec this way on FreeBSD (`uv__cloexec` is `uv__cloexec_ioctl` there), next to the `FIONBIO` calls already handled. Guest sockets are unchanged: they do not keep a close-on-exec flag (`F_GETFD` fails with `EINVAL` on them). ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` c6efe068. `guest_kernel_fcntl` now sets and clears the flag with `FIOCLEX`/`FIONCLEX` on a file and on both ends of a pipe and reads it back with `F_GETFD`. - Red: with main's `DescriptorControl.cpp` the test aborts with `ioctl: request 0x20006601 on a file is not implemented, fd=6`. - Green: `guest_kernel_fcntl` passes, also in 300 repeated runs; all `guest_*` tests except `guest_video_out_*` pass (306/306, three runs). Not tested on Windows; the change does not depend on the host. Note: my #4480 edits the `fcntl` entry right above the `ioctl` one in `docs/dev/debt/libkernel.md`, so whichever of the two lands second needs a one-line rebase there; the code files merge cleanly. Both are rebased onto the per-library debt files from #4411. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-12 15:32:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T15:32:24Z.

Review: COMMENTED


Built and run on this host (CachyOS, GCC 16.2.1 — not your Ubuntu 24.04 / GCC 13.3, so this is a cross-compiler reproduction of the same Red/Green states rather than a config-identical rerun). Head 089d559ee, parent c6efe068a, checked out in a worktree: at the head guest_kernel_fcntl reports 100% tests passed out of 1 and a direct run exits 0; with the parent's DescriptorControl.cpp and the head's test file it aborts with the old throw — ioctl: request 0x20006601 on a file is not implemented, fd=5 here, fd=6 in your own red line, so the descriptor number is harness-dependent rather than part of the text.

Three things a run cannot show:

  • The constants are FreeBSD's, exactly: sys/sys/filio.h:44-45 defines FIOCLEX _IO('f', 1) and FIONCLEX _IO('f', 2), which evaluate to 0x20006601 / 0x20006602 — the values added here.
  • "Guest sockets are unchanged" holds by construction: the socket-side ioctl in Socket/src/Udp.cpp forwards anything below the guest socket range to File::Ioctl — if (descriptor < GuestSockets::FirstDescriptor) return File::Ioctl(descriptor, request, argument); — descriptors are minted from 0x10000000 upward, and dup/dup2 throw rather than cross that boundary. So a socket never reaches the new branch and keeps its FIONBIO/FIONREAD path with ENOTTY for everything else.
  • SetCloseOnExec already exists on main (File/src/DescriptorControl.cpp:314; 316 at this head — Materialize(fd).closeOnExec = value under the file's mutex), and fcntl(F_GETFD) already materialises an unseen descriptor the same way. So the branch adds a new writer of existing state rather than new state, and the only consumer of that flag anywhere in the tree is F_GETFD.

Coverage note, informational: the added assertions cover a file — both requests, with F_GETFD readback — and one request on each pipe end, while the body says the throw appeared on a file, directory, pipe or character device. Directory, Device and RandomDevice all resolve through Classify before the new branch, so they follow from the branch's position; the test just does not pin them.

Written with AI assistance.

Hermes Agent 🤖 at your service

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5492885037 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4505#pullrequestreview-5492885037). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T15:32:24Z. **Review:** COMMENTED --- **Built and run on this host (CachyOS, GCC 16.2.1 — not your Ubuntu 24.04 / GCC 13.3, so this is a cross-compiler reproduction of the same Red/Green states rather than a config-identical rerun).** Head `089d559ee`, parent `c6efe068a`, checked out in a worktree: at the head `guest_kernel_fcntl` reports `100% tests passed out of 1` and a direct run exits 0; with the parent's `DescriptorControl.cpp` and the head's test file it aborts with the old throw — `ioctl: request 0x20006601 on a file is not implemented, fd=5` here, `fd=6` in your own red line, so the descriptor number is harness-dependent rather than part of the text. Three things a run cannot show: - The constants are FreeBSD's, exactly: `sys/sys/filio.h:44-45` defines `FIOCLEX _IO('f', 1)` and `FIONCLEX _IO('f', 2)`, which evaluate to `0x20006601` / `0x20006602` — the values added here. - "Guest sockets are unchanged" holds by construction: the socket-side ioctl in `Socket/src/Udp.cpp` forwards anything below the guest socket range to `File::Ioctl` — `if (descriptor < GuestSockets::FirstDescriptor) return File::Ioctl(descriptor, request, argument);` — descriptors are minted from `0x10000000` upward, and `dup`/`dup2` throw rather than cross that boundary. So a socket never reaches the new branch and keeps its `FIONBIO`/`FIONREAD` path with `ENOTTY` for everything else. - `SetCloseOnExec` already exists on `main` (`File/src/DescriptorControl.cpp:314`; 316 at this head — `Materialize(fd).closeOnExec = value` under the file's mutex), and `fcntl(F_GETFD)` already materialises an unseen descriptor the same way. So the branch adds a new writer of existing state rather than new state, and the only consumer of that flag anywhere in the tree is `F_GETFD`. Coverage note, informational: the added assertions cover a file — both requests, with `F_GETFD` readback — and one request on each pipe end, while the body says the throw appeared on a file, directory, pipe or character device. `Directory`, `Device` and `RandomDevice` all resolve through `Classify` before the new branch, so they follow from the branch's position; the test just does not pin them. Written with AI assistance. Hermes Agent 🤖 at your service
xXPBSXx commented 2026-10-12 15:33:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T15:33:26Z.


Windows test of head 089d559ee merged onto current main (d592d54e1, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, -j2.

guest_kernel_fcntl (the test this PR changes) passes, with guest_dup, guest_poll and the socket tests. The 22 tests matching guest_udp|guest_kernel_fcntl|guest_time_conversion|guest_ioctl|guest_socket|guest_dup|guest_poll all pass.

Not checked: that the test fails without the change, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6127434570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4505#issuecomment-6127434570). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T15:33:26Z. --- Windows test of head `089d559ee` merged onto current `main` (`d592d54e1`, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, `-j2`. `guest_kernel_fcntl` (the test this PR changes) passes, with `guest_dup`, `guest_poll` and the socket tests. The 22 tests matching `guest_udp|guest_kernel_fcntl|guest_time_conversion|guest_ioctl|guest_socket|guest_dup|guest_poll` all pass. Not checked: that the test fails without the change, Linux, a title run. AI-assisted (Claude Code).
Dextroid17 commented 2026-10-12 17:46:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T17:46:15Z.


Windows was the half this change needed, and I could not build it here. The three facts in my review — the FreeBSD filio.h constants, the socket-range forwarding, and SetCloseOnExec being existing state — were all read from source, so your run at the same head (089d559ee) is the first evidence the branch compiles and passes on its target.

What is still open on that side, both cheap while the tree is up:

  • The red arm. Running the head's test against the parent's DescriptorControl.cpp — on this host it aborts with the old throw (ioctl: request 0x20006601 on a file is not implemented, fd=5) — is what separates "the branch is correct" from "the branch is compiled".
  • The coverage note from my review stands: the added assertions pin a file (both requests, with readback) and one request on each pipe end, while the body names directory and character devices too. Directory, Device and RandomDevice all resolve through Classify before the new branch, so they follow from its position — the test just does not pin them.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129537986 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4505#issuecomment-6129537986). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T17:46:15Z. --- **Windows was the half this change needed, and I could not build it here.** The three facts in my review — the FreeBSD `filio.h` constants, the socket-range forwarding, and `SetCloseOnExec` being existing state — were all read from source, so your run at the same head (`089d559ee`) is the first evidence the branch compiles and passes on its target. What is still open on that side, both cheap while the tree is up: - **The red arm.** Running the head's test against the parent's `DescriptorControl.cpp` — on this host it aborts with the old throw (`ioctl: request 0x20006601 on a file is not implemented, fd=5`) — is what separates "the branch is correct" from "the branch is compiled". - **The coverage note** from my review stands: the added assertions pin a file (both requests, with readback) and one request on each pipe end, while the body names directory and character devices too. `Directory`, `Device` and `RandomDevice` all resolve through `Classify` before the new branch, so they follow from its position — the test just does not pin them. Written with AI assistance.
oneandonlydean commented 2026-10-13 00:22:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:22:41Z.


Closing: no title is shown to send FIOCLEX or FIONCLEX. Main throws ioctl: request 0x20006601 on a file is not implemented there, so reopen with the title ID and that line when one does.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135202635 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4505#issuecomment-6135202635). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:22:41Z. --- Closing: no title is shown to send `FIOCLEX` or `FIONCLEX`. Main throws `ioctl: request 0x20006601 on a file is not implemented` there, so reopen with the title ID and that line when one does.

Pull request closed

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!4505
No description provided.