feat(libkernel): implement named POSIX semaphores #9167

Merged
slider merged 4 commits from upstream-pr/9167 into main 2026-10-24 08:09:31 +00:00
slider commented 2026-10-21 14:09:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-21T14:09:19Z.


What

Implements named POSIX semaphores in libkernel: sem_open, sem_close and sem_unlink, following FreeBSD 11.4 libc's sem_new.c.

  • sem_open(name, flags, ...) checks in FreeBSD's order:

    • a name without a leading / gives EINVAL;
    • a /tmp/SEMD<name> path of PATH_MAX or more gives ENAMETOOLONG;
    • a flag other than O_CREAT/O_EXCL gives EINVAL;
    • a file name over NAME_MAX gives ENAMETOOLONG;
    • a missing name without O_CREAT gives ENOENT;
    • O_CREAT | O_EXCL on an existing name gives EEXIST.

    The mode and initial value are read from the variadic arguments only with O_CREAT, as _open reads its mode. Opening a name that is already open returns the same sem_t and counts the open.

  • sem_close drops one open and returns EINVAL for a semaphore that was not opened by name. The semaphore and its count stay until sem_unlink and the last close, as with FreeBSD's file.

  • sem_unlink returns ENOENT for a missing name or one without a leading /. Semaphores still open stay usable, and a new sem_open creates a new semaphore.

  • sem_destroy now returns EINVAL for a named semaphore, as FreeBSD's _sem_destroy does.

  • A named semaphore is a sem_t whose first word points to the same host semaphore as sem_init's, so sem_wait, sem_trywait, sem_timedwait, sem_post and sem_getvalue work on it unchanged.

  • Removes the three NotImplemented placeholders from libkernel_sys/Export.Discovery.cpp (duplicate-export rule). Imports bind by NID, so titles importing from libkernel_sys still resolve.

  • Debt (docs/dev/debt/libkernel.md): named semaphores live in a table inside the process instead of /tmp/SEMD<name> files. They are not shared between processes, and the mode is ignored. Whether /tmp is writable for a title on the console is unverified. A name with a further / and an initial value above SEM_VALUE_MAX throw.

No other open PR implements these; the branch merges cleanly with boykopovar/AnyPS5#8616, boykopovar/AnyPS5#8987 and boykopovar/AnyPS5#9141 (git merge-tree). boykopovar/AnyPS5#6740 already conflicts with main in docs/dev/debt/libkernel.md, independently of this change.

Tested

Linux (Ubuntu 24.04, GCC 13.3, Release):

  • New guest_posix_named_sem passes. It covers every name, flag and length error and the order of the checks, EEXIST, and same-pointer reopens with open counting. It also checks a post from another thread through its own sem_open, sem_destroy/sem_close misuse, the count persisting across a full close, and unlink while open followed by re-creation.
  • guest_posix_sem_init, guest_posix_sem_post, guest_pthread_sem and guest_kernel_sys pass.
  • Each of three mutations makes the new test fail: letting sem_destroy accept a named semaphore, dropping the EEXIST check, and checking NAME_MAX before the flags.
  • tools/check_conventions.py --base origin/main passes.

Windows: the changed sources and the test pass a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or run.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952970953 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9167). Originally posted by [slider](https://github.com/slider) on 2026-10-21T14:09:19Z. --- ### What Implements named POSIX semaphores in libkernel: `sem_open`, `sem_close` and `sem_unlink`, following FreeBSD 11.4 libc's `sem_new.c`. - `sem_open(name, flags, ...)` checks in FreeBSD's order: - a name without a leading `/` gives `EINVAL`; - a `/tmp/SEMD<name>` path of `PATH_MAX` or more gives `ENAMETOOLONG`; - a flag other than `O_CREAT`/`O_EXCL` gives `EINVAL`; - a file name over `NAME_MAX` gives `ENAMETOOLONG`; - a missing name without `O_CREAT` gives `ENOENT`; - `O_CREAT | O_EXCL` on an existing name gives `EEXIST`. The mode and initial value are read from the variadic arguments only with `O_CREAT`, as `_open` reads its mode. Opening a name that is already open returns the same `sem_t` and counts the open. - `sem_close` drops one open and returns `EINVAL` for a semaphore that was not opened by name. The semaphore and its count stay until `sem_unlink` and the last close, as with FreeBSD's file. - `sem_unlink` returns `ENOENT` for a missing name or one without a leading `/`. Semaphores still open stay usable, and a new `sem_open` creates a new semaphore. - `sem_destroy` now returns `EINVAL` for a named semaphore, as FreeBSD's `_sem_destroy` does. - A named semaphore is a `sem_t` whose first word points to the same host semaphore as `sem_init`'s, so `sem_wait`, `sem_trywait`, `sem_timedwait`, `sem_post` and `sem_getvalue` work on it unchanged. - Removes the three `NotImplemented` placeholders from `libkernel_sys/Export.Discovery.cpp` (duplicate-export rule). Imports bind by NID, so titles importing from `libkernel_sys` still resolve. - Debt (`docs/dev/debt/libkernel.md`): named semaphores live in a table inside the process instead of `/tmp/SEMD<name>` files. They are not shared between processes, and the mode is ignored. Whether `/tmp` is writable for a title on the console is unverified. A name with a further `/` and an initial value above `SEM_VALUE_MAX` throw. No other open PR implements these; the branch merges cleanly with boykopovar/AnyPS5#8616, boykopovar/AnyPS5#8987 and boykopovar/AnyPS5#9141 (`git merge-tree`). boykopovar/AnyPS5#6740 already conflicts with `main` in `docs/dev/debt/libkernel.md`, independently of this change. ### Tested Linux (Ubuntu 24.04, GCC 13.3, Release): - New `guest_posix_named_sem` passes. It covers every name, flag and length error and the order of the checks, `EEXIST`, and same-pointer reopens with open counting. It also checks a post from another thread through its own `sem_open`, `sem_destroy`/`sem_close` misuse, the count persisting across a full close, and unlink while open followed by re-creation. - `guest_posix_sem_init`, `guest_posix_sem_post`, `guest_pthread_sem` and `guest_kernel_sys` pass. - Each of three mutations makes the new test fail: letting `sem_destroy` accept a named semaphore, dropping the `EEXIST` check, and checking `NAME_MAX` before the flags. - `tools/check_conventions.py --base origin/main` passes. Windows: the changed sources and the test pass a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or run. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-22 05:47:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T05:47:45Z.

Review: COMMENTED


Checked against main 4a17c7175 and FreeBSD lib/libc/gen/sem_new.c (releng/11.4):

  • The checks run in the same order as _sem_open: the leading /, the strlcat PATH_MAX check, flags, va_arg, then NAME_MAX and ENOENT/EEXIST at _open. The errno values and O_CREAT/O_EXCL bits match too, and so do reopen refcounting, _sem_close/_sem_destroy EINVAL and _sem_unlink ENOENT. The PS5 SDK's sce_stubs/libkernel.c exports all three from libkernel, so moving them out of libkernel_sys is right.
  • docs/dev/debt/libkernel.md:126, last sentence: I think this divergence doesn't exist. In _sem_open's sem_list loop, a name that is already open and still linked makes _open(path, O_CREAT|O_EXCL|…) fail with EEXIST, and the code takes ni = NULL; goto error; without touching ni->name. ni->name = NULL happens only when that _open succeeds, which needs the file to have been unlinked first. The PR already models that: unlink erases linked, so the next create gets a new address. Could you remove the sentence so the debt file doesn't record a gap that isn't there?
  • On overlap: #9284/#9293 implement the ksem_* syscall exports in Semaphore/src/Ksem.cpp, which are different NIDs from the libc sem_* here. Their hunks in Export.Discovery.cpp, libkernel/CMakeLists.txt and libkernel.md are well away from this PR's, so I don't see a duplicate or a conflict.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609775188 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9167#pullrequestreview-5609775188). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T05:47:45Z. **Review:** COMMENTED --- Checked against main 4a17c7175 and FreeBSD `lib/libc/gen/sem_new.c` (releng/11.4): - The checks run in the same order as `_sem_open`: the leading `/`, the `strlcat` PATH_MAX check, flags, va_arg, then NAME_MAX and ENOENT/EEXIST at `_open`. The errno values and O_CREAT/O_EXCL bits match too, and so do reopen refcounting, `_sem_close`/`_sem_destroy` EINVAL and `_sem_unlink` ENOENT. The PS5 SDK's `sce_stubs/libkernel.c` exports all three from libkernel, so moving them out of libkernel_sys is right. - `docs/dev/debt/libkernel.md:126`, last sentence: I think this divergence doesn't exist. In `_sem_open`'s `sem_list` loop, a name that is already open and still linked makes `_open(path, O_CREAT|O_EXCL|…)` fail with EEXIST, and the code takes `ni = NULL; goto error;` without touching `ni->name`. `ni->name = NULL` happens only when that `_open` succeeds, which needs the file to have been unlinked first. The PR already models that: unlink erases `linked`, so the next create gets a new address. Could you remove the sentence so the debt file doesn't record a gap that isn't there? - On overlap: #9284/#9293 implement the `ksem_*` syscall exports in `Semaphore/src/Ksem.cpp`, which are different NIDs from the libc `sem_*` here. Their hunks in `Export.Discovery.cpp`, `libkernel/CMakeLists.txt` and `libkernel.md` are well away from this PR's, so I don't see a duplicate or a conflict. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
slider commented 2026-10-22 09:15:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-22T09:15:46Z.


Thanks @harisibrar1, you're right: with the name still linked, _open fails with EEXIST before ni->name = NULL, which only runs after an unlink and re-create, already modelled. Removed the sentence in https://github.com/boykopovar/AnyPS5/pull/9167/commits/f2766b244740052b40b5df046821787b874e8fb1.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337194989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9167#issuecomment-6337194989). Originally posted by [slider](https://github.com/slider) on 2026-10-22T09:15:46Z. --- Thanks @harisibrar1, you're right: with the name still linked, `_open` fails with EEXIST before `ni->name = NULL`, which only runs after an unlink and re-create, already modelled. Removed the sentence in https://github.com/boykopovar/AnyPS5/pull/9167/commits/f2766b244740052b40b5df046821787b874e8fb1.
harisibrar1 commented 2026-10-22 12:54:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T12:54:54Z.

Review: APPROVED


Rechecked at f2766b2447:

  • The O_EXCL sentence is gone from the sem_open/sem_close/sem_unlink entry (libkernel.md:126), so the debt file no longer records a gap that FreeBSD's _sem_open doesn't have. The rest of the entry still describes what Sem.cpp does.
  • The change is docs-only. Main hasn't touched Sem.cpp or the test since your base, and the PR still merges cleanly with current main.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5614477733 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9167#pullrequestreview-5614477733). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T12:54:54Z. **Review:** APPROVED --- Rechecked at f2766b2447: - The O_EXCL sentence is gone from the sem_open/sem_close/sem_unlink entry (libkernel.md:126), so the debt file no longer records a gap that FreeBSD's `_sem_open` doesn't have. The rest of the entry still describes what Sem.cpp does. - The change is docs-only. Main hasn't touched Sem.cpp or the test since your base, and the PR still merges cleanly with current main. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9167
No description provided.