fix(libkernel): return SCE error codes from sceKernelFsync #3682

Merged
zmeizy merged 1 commit from upstream-pr/3682 into main 2026-10-11 22:23:22 +00:00
zmeizy commented 2026-10-11 15:19:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-11T15:19:19Z.


What

sceKernelFsync returned the raw result of the host fsync (_commit on Windows), which is -1 on failure, not an SCE error code. Guest sockets, whose descriptors are not host descriptors, went straight to the host call. fsync then read the host errno, which uses host numbering.

This makes sceKernelFsync behave like sceKernelFdatasync next to it:

  • A socket descriptor returns EINVAL (0x80020016).
  • A closed descriptor in the socket range returns EBADF (0x80020009).
  • A host failure returns SceErrorFromErrno(errno).
  • fsync derives errno from that code through PosixResult, as fdatasync does.

Sources: FreeBSD fsync(2) lists EBADF for an invalid descriptor and EINVAL for a descriptor that refers to a socket. The SCE codes follow the 0x80020000 | errno scheme used by the rest of the file.

Overlap: #3578 changes the Windows line of sceKernelFsync to NativeSync. The two touch the same lines, so whichever merges second needs a one-line rebase. The behaviour does not conflict.

Tested

Ubuntu 24.04, GCC 13.3, on main a11c8f7f.

  • guest_filesystem: new checks mirroring the existing fdatasync ones:
    • On a closed descriptor, fsync sets errno 9 and sceKernelFsync returns 0x80020009.
    • On a socket, fsync sets errno 22 and sceKernelFsync returns 0x80020016.
    • On a closed socket, fsync sets errno 9 and sceKernelFsync returns 0x80020009.
    • sceKernelFsync(0x7fffffff) returns 0x80020009.
  • The test aborts on main without the change and passes with it.
  • All guest_* tests (ctest -R '^guest_' --parallel 4) with the change: all pass except guest_truncate (the container runs as root) and guest_kernel_stack_protect (fails on main too; #3520 fixes it).

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 (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820098869 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3682). Originally posted by [zmeizy](https://github.com/zmeizy) on 2026-10-11T15:19:19Z. --- ### What `sceKernelFsync` returned the raw result of the host `fsync` (`_commit` on Windows), which is -1 on failure, not an SCE error code. Guest sockets, whose descriptors are not host descriptors, went straight to the host call. `fsync` then read the host `errno`, which uses host numbering. This makes `sceKernelFsync` behave like `sceKernelFdatasync` next to it: - A socket descriptor returns `EINVAL` (`0x80020016`). - A closed descriptor in the socket range returns `EBADF` (`0x80020009`). - A host failure returns `SceErrorFromErrno(errno)`. - `fsync` derives `errno` from that code through `PosixResult`, as `fdatasync` does. Sources: FreeBSD `fsync(2)` lists `EBADF` for an invalid descriptor and `EINVAL` for a descriptor that refers to a socket. The SCE codes follow the `0x80020000 | errno` scheme used by the rest of the file. Overlap: #3578 changes the Windows line of `sceKernelFsync` to `NativeSync`. The two touch the same lines, so whichever merges second needs a one-line rebase. The behaviour does not conflict. ### Tested Ubuntu 24.04, GCC 13.3, on `main` `a11c8f7f`. - `guest_filesystem`: new checks mirroring the existing `fdatasync` ones: - On a closed descriptor, `fsync` sets `errno` 9 and `sceKernelFsync` returns `0x80020009`. - On a socket, `fsync` sets `errno` 22 and `sceKernelFsync` returns `0x80020016`. - On a closed socket, `fsync` sets `errno` 9 and `sceKernelFsync` returns `0x80020009`. - `sceKernelFsync(0x7fffffff)` returns `0x80020009`. - The test aborts on `main` without the change and passes with it. - All `guest_*` tests (`ctest -R '^guest_' --parallel 4`) with the change: all pass except `guest_truncate` (the container runs as root) and `guest_kernel_stack_protect` (fails on `main` too; #3520 fixes it). ### 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) (n/a) - [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 (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA
adriadam10 commented 2026-10-11 21:58:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:58:01Z.


Rebased onto current main (#3578 landed first and touched the same line): the Windows branch of sceKernelFsync now calls NativeSync(fd) and maps errno with SceErrorFromErrno, exactly like sceKernelFdatasync. The rest is unchanged. guest_filesystem passes on Linux. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114255257 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3682#issuecomment-6114255257). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:58:01Z. --- Rebased onto current `main` (#3578 landed first and touched the same line): the Windows branch of `sceKernelFsync` now calls `NativeSync(fd)` and maps `errno` with `SceErrorFromErrno`, exactly like `sceKernelFdatasync`. The rest is unchanged. `guest_filesystem` passes on Linux. Merging once CI 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!3682
No description provided.