Skip to content

Carry/libkernel fcntl files - #4094

Closed
itsbo5 wants to merge 2 commits into
boykopovar:mainfrom
itsbo5:carry/libkernel-fcntl-files
Closed

itsbo5 wants to merge 2 commits into
boykopovar:mainfrom
itsbo5:carry/libkernel-fcntl-files

Conversation

@itsbo5

@itsbo5 itsbo5 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Carry #3757 by @screamm (feat(libkernel): support fcntl on non-socket descriptors and feat(libkernel): support ioctl on non-socket descriptors).

fcntl, sceKernelFcntl, ioctl and _ioctl previously answered EBADF for every descriptor outside the libSceNet socket table. This blocked titles such as Yamagi Quake II (PPSA99007, stops with "Sys_Error: change stdin to blocking Bad file descriptor"), XPSemu (PPSA97358), VLC (PPSA85300), RetroArch (PPSA99169), Porpoise (PPSA99764), and Ruffle.

  • Add DescriptorControl.cpp tracking close-on-exec flags and shared status flags across duplicated file descriptors (dup, dup2, F_DUPFD), matching FreeBSD's open file description model.
  • Support F_GETFD/F_SETFD, F_GETFL/F_SETFL, F_DUPFD/F_DUPFD_CLOEXEC, F_DUP2FD*, and F_RDAHEAD/F_READAHEAD.
  • Support ioctl requests FIONBIO and FIONREAD on non-socket descriptors (files, pipes, random device), returning ENOTTY for terminal group 't' requests (TIOCGWINSZ).
  • Non-blocking pipes properly report EAGAIN on empty reads and full writes on both Windows and Linux.
  • Sockets retain their existing behavior.

Closes #3757.

Tested

  • guest_kernel_fcntl and guest_dup tests pass.
  • tools/check_conventions.py passes with zero errors.

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
  • AI-assisted: yes

screamm and others added 2 commits October 12, 2026 03:00
fcntl and sceKernelFcntl answered EBADF (sceKernelFcntl threw) for
every descriptor outside the libSceNet socket table, so files, pipes,
directories and descriptors 0 to 2 could not be queried or changed.

Observed: Yamagi Quake II (PPSA99007) stops with
"Sys_Error: change stdin to blocking Bad file descriptor", from
fcntl(fileno(stdin), F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY)
(yquake2 src/backends/unix/system.c:81-86).

Predicted from the titles' sources and decoded call sites: XPSemu
(PPSA97358, 97 fcntl sites: F_GETFD 12, F_SETFD 20, F_GETFL 8,
F_SETFL 9, F_DUPFD 1, F_DUPFD_CLOEXEC 7, F_GETLK 9) fails in
qemu_init_main_loop, where its pipe2 (pipe plus F_SETFD FD_CLOEXEC) and
glib's g_unix_set_fd_nonblocking (F_GETFL, F_SETFL) build the event
notifier (util/event_notifier-posix.c:44-67); VLC (PPSA85300, 43 sites,
F_RDAHEAD on every file it opens), RetroArch (PPSA99169, 17),
Porpoise (PPSA99764, 13) and Ruffle (9) call it on files and pipes.

The commands follow FreeBSD 11.4 kern_fcntl and kern_dup
(sys/kern/kern_descrip.c:479-799, 812, release/11.4.0 8c639945):
F_GETFD/F_SETFD keep the close-on-exec flag per descriptor, F_GETFL
reports the host access mode with the status flags, F_SETFL changes
only O_NONBLOCK, O_APPEND, O_ASYNC, O_FSYNC, O_DIRECT and FRDAHEAD,
F_DUPFD/F_DUPFD_CLOEXEC give the lowest free descriptor at or above the
start through dup (EINVAL for a negative start), F_DUP2FD goes through
dup2, F_RDAHEAD/F_READAHEAD set FRDAHEAD on files (EBADF on pipes,
ENOTTY elsewhere), and unknown commands fail with EINVAL. Status flags
live in a guest table shared by dup, dup2 and F_DUPFD like an open file
description. O_NONBLOCK switches a pipe (PIPE_NOWAIT on Windows, where an
empty read and a full write now report EAGAIN; the host flag on Linux).
Record locks use the host POSIX locks on Linux and throw on Windows.
Changes the host cannot model throw naming the flag and descriptor kind.
Sockets keep their path unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
ioctl and _ioctl answered EBADF for every descriptor outside the
libSceNet socket table. Decoded call sites: PS5MC (PPSA99420, FIONBIO x3,
FIONREAD x2, TIOCGWINSZ x3), ProsperoTV (PPSA99003, FIONBIO on a pipe
once poll on pipes is in), XPSemu (PPSA97358, 21 sites).

Following FreeBSD 11.4 kern_ioctl (sys/kern/sys_generic.c:758),
vn_ioctl (sys/kern/vfs_vnops.c:1488), pipe_ioctl (sys/kern/sys_pipe.c:1346)
and randomdev_ioctl (sys/dev/random/randomdev.c:301), release/11.4.0
8c639945: FIONBIO sets or clears O_NONBLOCK through the same status
flags as fcntl F_SETFL (EFAULT for a null argument before EBADF);
FIONREAD reports the size past the offset of a file, the queued bytes of
a pipe's read end, 0 on its write end, ENOTTY on the random device, and
throws on a directory or another character device; termios requests
(group 't', TIOCGWINSZ included) fail with ENOTTY, as tcgetattr already
takes these descriptors as not a terminal; any other request throws
naming the request and descriptor kind. Sockets keep their path
unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

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
#3764 core/libs/prx/libkernel/CMakeLists.txt L5
#3787 core/libs/prx/libkernel/File/src/Open.cpp L227
core/libs/prx/libkernel/File/src/Stdio.cpp L469, L495, L519, L529
#3840 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
core/libs/prx/libkernel/File/src/Open.cpp L8, L83, L89-90, L196, L222, L227, L324-329, L331-334, L336-339, L341-344
core/libs/prx/libkernel/Socket/src/Udp.cpp L18, L436, L651
core/libs/tests/GuestKernelFcntl.cpp L11, L14, L16, L18, L20, L24, L35, L40, L42-51
core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
#3968 core/libs/prx/libkernel/File/src/Open.cpp L188-199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libkernel: fcntl and ioctl reject every non-socket descriptor (Yamagi Quake II, XPSemu, VLC, RetroArch); branch ready

2 participants