libkernel: fcntl F_SETFL that changes O_APPEND on a file throws on Windows; implementation ready #4941
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T05:44:02Z.
On Windows,
fcntl(fd, F_SETFL, ...)that turnsO_APPENDon or off for a file throwsfcntl: changing O_APPEND on a file is not implementedand the process ends. FreeBSD and the Linux build change the flag. This is the gap docs/dev/debt/libkernel.md records ("ChangingO_APPENDon a Windows file ... throw"), andguest_kernel_fcntlexpects the throw on Windows.The usual way a program reaches it: a logger or a library that takes a descriptor it did not open and makes sure it appends with
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_APPEND), or one that clears the flag on a descriptor opened withO_APPENDbefore it seeks and overwrites.I have not found a title that stops on this. I am offering it because the pieces for it are already in the tree after #4735.
Feature, ready to cherry-pick.
DavidSamir:fix/libkernel-fcntl-append-windows(c52947f1e, three commits onmaine51428bf2, three files, +52/-6). I can't open it as a PR while #4532 is open (one open PR without a merged one).The three commits, each of which builds and passes the tests on its own:
feat(libkernel): change O_APPEND with fcntl on a file on Windows: the change inDescriptorControl.cpp, with the Windows-only expectation of the throw taken out ofguest_kernel_fcntl, which it makes false.test(libkernel): check where writes land after fcntl changes O_APPEND: the new sequence.docs(debt): describe how fcntl changes O_APPEND on Windows.Windows only, in
ApplyHostStatus. On Windows appending is a flag of the C runtime's descriptor, set when the descriptor is made (_open_osfhandlewith_O_APPEND, which is whatsceKernelOpenuses forO_APPEND). To change it,SetAppendduplicates the descriptor's handle, makes a descriptor for the copy with the flag set or cleared and the same text or binary setting, and moves it onto the guest's descriptor with_dup2. The descriptor keeps its number and its file object, and with that its offset, itsflockand its sharing with duplicates.O_APPENDbelongs to the open file, so the change is applied to every descriptor that shares it: the ones the guest table already ties to the same status flags (dup,dup2,F_DUPFD), checked withCompareObjectHandlesto be the same open file, so a number that was closed and reused behind the table's back is left alone. Descriptors made bydupafterwards inherit the flag from the C runtime.Still throws, as before: descriptors 0 to 2, whose handle is also the Win32 standard handle (#4631, #4640). The debt entry is rewritten to say that. Pipes, directories and devices only record the flag, as today.
Seen while testing and not changed:
pwriteon a descriptor withO_APPENDwrites at the given offset on Windows and appends on the Linux build. That is the same for a descriptor opened withO_APPENDonmain.guest_kernel_fcntl: the Windows-only expectation of the throw is removed, soF_SETFLwith 0 on a descriptor opened withO_APPENDmust succeed on both hosts, and a new sequence on a second file checks the behaviour: a write after a seek to 0 overwrites; afterF_SETFL O_APPENDthrough a duplicate,F_GETFLon the first descriptor shows the flag and writes through either land at the end whatever the offset; afterF_SETFL 0a write at offset 1 overwrites again.Checked on Windows (c52947f1e on
maine51428bf2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):The runs below were made on b4f4e06d5, the same tree as c52947f1e before it was split into the three commits; nothing but the commit boundaries differs.
DescriptorControl.cppas onmainand the test kept,guest_kernel_fcntlfails (exit 0xc0000409, the throw). With the change it passes 5/5 (--repeat until-fail:5), andguest_filesystem,guest_file_vector,guest_poll,guest_dup,guest_standard_streams,file_streams,guest_aio,guest_ioctl,guest_file_lock_lifetime,guest_file_lock_duplicateandguest_udppass 3/3.preadunaffected, setting it twice, together withO_NONBLOCK), clearing it, a closed descriptor, adupand anF_DUPFDcopy made before and adupmade after, an independent open of the same file staying without the flag, clearing through a duplicate,flockheld across the change and still excluding another open,FD_CLOEXECkept,fstat,fsync,ftruncate,renameandunlinkon the descriptor, a descriptor wrapped byfdopen,dup2of another file onto the descriptor, and a read-only descriptor: 85 lines are identical to Linux, and the two that differ are thepwritecase above.O_APPEND(F_SETFL O_NONBLOCKon a file, a descriptor opened withO_APPEND,F_SETFL O_APPENDon a pipe), 15 lines, is identical withmain's libraries, the branch's and on Linux.F_GETFL336 to 432 ns withmain's libraries and 330 to 375 ns with the branch's;F_SETFLtogglingO_NONBLOCK185 to 249 ns and 184 to 211 ns; seek and write 1.71 to 1.86 µs and 1.71 to 1.89 µs. No difference I can separate from the spread.fdopen, run with both sets of libraries: no line differs, apart from the size a file reaches when eight threads append to it at once, which changes from run to run with either set.poll: POLLPRI is not supported by WSAPollin both (#4478); its number of stderr lines varies from run to run with either set.ctest --parallel 4: 1026 of 1028 pass. The two that fail,agc_driver_bda_span_readsandagc_driver_pixel_interlock, fail the same way withmaine51428bf2built without this change on this machine (vkWaitForFences recorder: Vulkan result -4, RTX 3070); they do not touch this code.guest_kernel_fcntl,guest_filesystem,guest_dupandguest_file_lock_duplicatepass 3/3.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Checked on Linux (the same tree, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
guest_kernel_fcntlpasses 3/3, so the new sequence matches the existing Linux behaviour. Only that target andlibswere built there.Not checked on a console, on a Windows version older than this one, or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by karthikeya-tummala on 2026-10-13T10:37:45Z.
Hey David,
Can I cherry pick them and open a PR?
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:53:55Z.
Closing: no title is known to reach this, and functions are only changed when a title needs them. The catalog titles in the report run the same with and without the change. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.