kernel: return -1 and errno from POSIX open, close, write and unlink #1207

Closed
github-bot wants to merge 1 commit from gh-pr/1207/head into gh-pr/1207/base
Owner

@⁠Nawid3333 on GitHub · 2026-10-08 12:03 UTC · original
Nawid3333:kernel/posix-file-exports → main · +79 −5 · 2 files · 1 commits · closed without merging

Problem

Guest code that calls the POSIX open, close, write or unlink exported by libkernel gets SCE
error codes back instead of -1 and errno. For example open("/savedata0/missing.dat", O_RDONLY)
returns 0x80020002 and leaves errno alone, so a caller that checks for -1 takes the error code for a
file descriptor, and code that looks at errno (ENOENT → create the file) never sees it.

_write() on a negative descriptor, for example the -1 from a failed _open(), ends the emulator
with "Not implemented (d < 0)".

Cause

InitLibKernel_1_Pthread binds the NIDs of the plain POSIX names straight to the sceKernel
functions:

NID Name Bound to
wuCroIGjt2g open FileSystem::KernelOpen
bY-PO6JhzhQ close FileSystem::KernelClose
FN4gaPmuFV8 write FileSystem::KernelWrite
VAzswvTOCzI unlink FileSystem::KernelUnlink

Those return SCE error codes and don't set errno. The same file already has POSIX wrappers for
_open, _close and _write. LibKernel::write (_write) starts with
EXIT_NOT_IMPLEMENTED(d < 0), while close and read return -1 with EBADF.

Fix

  • The open and close NIDs point at the same functions as _open and _close.
  • New Posix::unlink (POSIX_CALL around KernelUnlink) and Posix::write (POSIX_N_CALL around
    KernelWrite). write doesn't reuse _write's function because that one returns the size for
    descriptors 0-2 without writing anything. The write export printed guest stdout/stderr through
    KernelWrite before this change and still does.
  • _write() on a negative descriptor returns -1 with EBADF, as close() does.

Success results don't change. One other visible difference: close() on descriptors 0-2 now returns
0, like _close(), instead of an SCE EPERM code.

Testing

New TestPosixFileExports in kernel_file_system: through the export NIDs, open and unlink of a
missing file return -1/ENOENT, close(-1), write(-1) and _write(-1) return -1/EBADF, and
open/write/close/unlink of a new file return the descriptor, the byte count and 0, with close and
write on the closed descriptor returning -1/EBADF.

  • Windows 11, clang-cl 19.1.5, Release, on main 3c62359e: without the fix the test stops at
    POSIX open returns -1 and ENOENT for a missing file; with it, all new checks pass and the binary
    goes on to the existing Net unknown socket options return protocol-option errors check, which fails
    on Windows until #1159 is merged.
  • Fork CI (Windows, Linux, macOS): green, https://github.com/Nawid3333/KytyPS5/actions/runs/37772097160. CI builds the emulator, so libKernel.cpp compiles on all
    three, but it doesn't build or run kernel_file_system.

AI use (per the README's AI policy): Claude (Anthropic) found these bindings while scanning the kernel exports for small fixes, wrote the change and the test, and ran the tests above on my machine.

🤖 Generated with Claude Code

**@⁠Nawid3333** on GitHub · 2026-10-08 12:03 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1207) `Nawid3333:kernel/posix-file-exports` → `main` · +79 −5 · 2 files · 1 commits · closed without merging ## Problem Guest code that calls the POSIX `open`, `close`, `write` or `unlink` exported by libkernel gets SCE error codes back instead of -1 and errno. For example `open("/savedata0/missing.dat", O_RDONLY)` returns 0x80020002 and leaves errno alone, so a caller that checks for -1 takes the error code for a file descriptor, and code that looks at errno (`ENOENT` → create the file) never sees it. `_write()` on a negative descriptor, for example the -1 from a failed `_open()`, ends the emulator with "Not implemented (d < 0)". ## Cause `InitLibKernel_1_Pthread` binds the NIDs of the plain POSIX names straight to the sceKernel functions: | NID | Name | Bound to | |---|---|---| | `wuCroIGjt2g` | `open` | `FileSystem::KernelOpen` | | `bY-PO6JhzhQ` | `close` | `FileSystem::KernelClose` | | `FN4gaPmuFV8` | `write` | `FileSystem::KernelWrite` | | `VAzswvTOCzI` | `unlink` | `FileSystem::KernelUnlink` | Those return SCE error codes and don't set errno. The same file already has POSIX wrappers for `_open`, `_close` and `_write`. `LibKernel::write` (`_write`) starts with `EXIT_NOT_IMPLEMENTED(d < 0)`, while `close` and `read` return -1 with EBADF. ## Fix - The `open` and `close` NIDs point at the same functions as `_open` and `_close`. - New `Posix::unlink` (`POSIX_CALL` around `KernelUnlink`) and `Posix::write` (`POSIX_N_CALL` around `KernelWrite`). `write` doesn't reuse `_write`'s function because that one returns the size for descriptors 0-2 without writing anything. The `write` export printed guest stdout/stderr through `KernelWrite` before this change and still does. - `_write()` on a negative descriptor returns -1 with EBADF, as `close()` does. Success results don't change. One other visible difference: `close()` on descriptors 0-2 now returns 0, like `_close()`, instead of an SCE EPERM code. ## Testing New `TestPosixFileExports` in `kernel_file_system`: through the export NIDs, `open` and `unlink` of a missing file return -1/ENOENT, `close(-1)`, `write(-1)` and `_write(-1)` return -1/EBADF, and open/write/close/unlink of a new file return the descriptor, the byte count and 0, with `close` and `write` on the closed descriptor returning -1/EBADF. - Windows 11, clang-cl 19.1.5, Release, on `main` `3c62359e`: without the fix the test stops at `POSIX open returns -1 and ENOENT for a missing file`; with it, all new checks pass and the binary goes on to the existing `Net unknown socket options return protocol-option errors` check, which fails on Windows until #1159 is merged. - Fork CI (Windows, Linux, macOS): green, https://github.com/Nawid3333/KytyPS5/actions/runs/37772097160. CI builds the emulator, so `libKernel.cpp` compiles on all three, but it doesn't build or run `kernel_file_system`. **AI use** (per the README's AI policy): Claude (Anthropic) found these bindings while scanning the kernel exports for small fixes, wrote the change and the test, and ran the tests above on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The libkernel NIDs of the plain POSIX names open, close, write and unlink
were bound straight to sceKernelOpen, sceKernelClose, sceKernelWrite and
sceKernelUnlink. Those return SCE error codes such as 0x80020002 and leave
errno untouched, so a POSIX caller that checks for -1 takes the error code
for a descriptor or a byte count and never sees an errno.

Point open and close at the POSIX wrappers that _open and _close are
bound to, and add POSIX wrappers for write and unlink. The write wrapper
still goes through sceKernelWrite, so guest stdout and stderr output keeps
reaching the host. Success results don't change; close() on descriptors
0-2 now returns 0 like _close() instead of an SCE EPERM code.

_write() on a negative descriptor ended the emulator through
EXIT_NOT_IMPLEMENTED. Return -1 with EBADF instead, as close() does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:47:51 +00:00
github-bot closed this pull request 2026-11-26 09:47:51 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 12:07 UTC · original

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 53dd1d14-b5c0-4c50-a8dd-65f85b2c0423
📥 Commits

Reviewing files that changed from the base of the PR and between 3c62359e05 and 8a55299810.

📒 Files selected for processing (2)
  • src/libs/libKernel.cpp
  • tests/KernelFileSystemTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The change adds POSIX unlink and write wrappers and routes the related file API exports through library wrappers. Negative descriptors passed to LibKernel::write now return -1 with EBADF. Tests cover error cases and successful file creation, writing, closing, and removal.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 8a552

The file exports preserve successful results and return POSIX-style failures as intended. No actionable merge-blocking issue remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly explains the POSIX export behavior changes, the cause, the implementation, and the test results. It is directly related to the changeset.
Title check ✅ Passed The title clearly and concisely identifies the main change: POSIX open, close, write, and unlink now return -1 and set errno on failure.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 12:07 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1207#issuecomment-6059500072) <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1207?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `53dd1d14-b5c0-4c50-a8dd-65f85b2c0423` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 3c62359e05854ea3ae8f09d176ae2f82d3bbf817 and 8a552998109f0c225c5b82e6296f98e4cac02db7. </details> <details> <summary>📒 Files selected for processing (2)</summary> * `src/libs/libKernel.cpp` * `tests/KernelFileSystemTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough The change adds POSIX `unlink` and `write` wrappers and routes the related file API exports through library wrappers. Negative descriptors passed to `LibKernel::write` now return `-1` with `EBADF`. Tests cover error cases and successful file creation, writing, closing, and removal. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"8a552998109f0c225c5b82e6296f98e4cac02db7" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** **⚪ Minimal** · up to `8a552` <!-- final_review_risk_coverage:{"sourceCommitId":"8a552998109f0c225c5b82e6296f98e4cac02db7","coveredCommitId":"8a552998109f0c225c5b82e6296f98e4cac02db7","kind":"reviewed"} --> The file exports preserve successful results and return POSIX-style failures as intended. No actionable merge-blocking issue remains. <!-- final_review_risk_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :---------------- | :--------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ⚠️ Warning | Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 2 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------ | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Description check | ✅ Passed | The description clearly explains the POSIX export behavior changes, the cause, the implementation, and the test results. It is directly related to the changeset. | | Title check | ✅ Passed | The title clearly and concisely identifies the main change: POSIX open, close, write, and unlink now return -1 and set errno on failure. | | Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | </details> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1207)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.