feat: S2 name canonicalization — sem_* prefix shm_open paths with 'sem.'

sem_open/map_named_semaphore now canonicalizes names: /mysem → sem./mysem
Prevents namespace collisions with raw shm_open usage. Matches glibc's
/dev/shm/sem.NAME convention.
This commit is contained in:
2026-05-05 21:36:57 +01:00
parent ef96d33ead
commit 70122892e5
@@ -1,4 +1,5 @@
diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs
index 0ca2fa9..8a1cad4 100644
--- a/src/header/semaphore/mod.rs
+++ b/src/header/semaphore/mod.rs
@@ -2,12 +2,27 @@
@@ -30,7 +31,7 @@ diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs
};
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/semaphore.h.html>.
@@ -19,11 +34,86 @@
@@ -19,11 +34,87 @@ pub union sem_t {
pub align: c_long,
}
pub type RlctSempahore = crate::sync::Semaphore;
@@ -67,7 +68,8 @@ diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs
+) -> *mut sem_t {
+ let create = (oflag & O_CREAT) != 0;
+ let shm_flags = if create { O_CREAT | O_EXCL | O_RDWR } else { O_RDWR };
+ let fd = unsafe { shm_open(name, shm_flags, mode) };
+ let canonical = alloc::format!("sem.{name_cstr}", name_cstr = unsafe { CStr::from_ptr(name) }.to_str().unwrap_or(""));
+ let fd = unsafe { shm_open(canonical.as_ptr() as *const c_char, shm_flags, mode) };
+ if fd < 0 {
+ return SEM_FAILED_PTR;
+ }
@@ -118,7 +120,7 @@ diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_destroy.html>.
@@ -50,13 +140,31 @@
@@ -50,13 +141,31 @@ pub unsafe extern "C" fn sem_init(sem: *mut sem_t, _pshared: c_int, value: c_uin
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_open.html>.
@@ -154,7 +156,7 @@ diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_post.html>.
@@ -76,9 +184,12 @@
@@ -76,9 +185,12 @@ pub unsafe extern "C" fn sem_trywait(sem: *mut sem_t) -> c_int {
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_unlink.html>.
@@ -169,19 +171,3 @@ diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_trywait.html>.
diff --git a/src/header/semaphore/cbindgen.toml b/src/header/semaphore/cbindgen.toml
--- a/src/header/semaphore/cbindgen.toml
+++ b/src/header/semaphore/cbindgen.toml
@@ -3,6 +3,9 @@
after_includes = """
#include <bits/timespec.h> // for timespec
"""
+trailer = """
+#define SEM_FAILED ((sem_t *) -1)
+"""
language = "C"
style = "Type"
no_includes = true