Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System V shared memory APIs (2nd draft) #2314

Open
wants to merge 58 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
096cb21
First try at implementing SystemV
IzawGithub Feb 13, 2024
072352c
Add permission struct
IzawGithub Feb 14, 2024
adeaaa2
Add crude semaphore support
IzawGithub Feb 14, 2024
135488e
Add documentation that was missing
IzawGithub Feb 14, 2024
ac357e2
Add PR changelog file
IzawGithub Feb 14, 2024
3f6a4c3
Move SystemV to it's own file
IzawGithub Feb 19, 2024
5047036
Fix not compiling on Musl because of missing struct
IzawGithub Feb 20, 2024
b428de0
Fix clippy unsafe ptr arg dereference lint
IzawGithub Feb 20, 2024
07a6f33
Formatting parse
IzawGithub Feb 20, 2024
ac33ca0
Add missing import that I forgot to commit
IzawGithub Feb 20, 2024
11aad71
Update doctest path that changed
IzawGithub Feb 20, 2024
d8db9fa
Add basic IPC test.
IzawGithub Feb 20, 2024
bd938e6
Fix using latest Rust feature that was unstable before
IzawGithub Feb 20, 2024
b9a5905
Forgot a formatting parse
IzawGithub Feb 20, 2024
50916a4
Enable SystemV only for linux, but not Android
IzawGithub Feb 20, 2024
f3e4771
Update conditional compilation for GNU extension
IzawGithub Feb 20, 2024
9c71d97
Fix macro not compiling for NetBSD
IzawGithub Feb 20, 2024
5d0d37c
Fix moving shmid_ds to GNU extension when it's not
IzawGithub Feb 20, 2024
315eb31
Fix mistake in conditional compilation that didn't disable SystemV fo…
IzawGithub Feb 20, 2024
9374dcf
Add back cfg that disable some flags for BSD
IzawGithub Feb 20, 2024
bb07d5b
Final (I hope) formatting pass
IzawGithub Feb 20, 2024
19c713d
Disable test for non systemV platform
IzawGithub Feb 20, 2024
17561e6
Fix wrong changelog number and content
IzawGithub Mar 21, 2024
2b76711
Remove first try at SystemV, replacing with refractor
IzawGithub Mar 22, 2024
94e4e11
First try at refractoring SystemV to be more Rusty
IzawGithub Mar 22, 2024
aea8588
Add a feature gate
IzawGithub Mar 22, 2024
02b0de2
Add missing doc
IzawGithub Mar 22, 2024
ed99763
Fix doctest copy and paste error
IzawGithub Mar 22, 2024
b614d81
Remove Clone derive, wasn't working correctly
IzawGithub Mar 22, 2024
2629192
Update test to create an entirely new SharedMemory object
IzawGithub Mar 22, 2024
b7b8818
Misc formatting
IzawGithub Mar 22, 2024
930f352
Remove unused static mutex for system_v
IzawGithub Mar 24, 2024
4bd41cf
Add `Shm` wrapper for creating a shared memory segment
IzawGithub Mar 24, 2024
2c714c2
Remove function that create a memory segment from `SharedMemory`
IzawGithub Mar 24, 2024
6877712
Make `buf` parameter pass by ref
IzawGithub Mar 24, 2024
b7f6a09
Remove the `shmat` enum value that work with incompatible void ptr
IzawGithub Mar 24, 2024
d07c9b8
Update unit test
IzawGithub Mar 24, 2024
5697ffd
Update imported function
IzawGithub Mar 24, 2024
3d70ae2
Remove stable ptr_from_ref feature that isn't stable on Rust 1.69
IzawGithub Mar 24, 2024
238fe62
Remove unused comment mark
IzawGithub Mar 25, 2024
0ff180a
Add back `shmat` address. Use a raw pointer.
IzawGithub Mar 25, 2024
f5f2f59
Fix mismatched target os
IzawGithub Mar 25, 2024
2803e31
Fix wrong comment formatting
IzawGithub Mar 25, 2024
35c2f4e
Move `shmctl` function to `Shm`.
IzawGithub Mar 25, 2024
d05db61
Update doctest
IzawGithub Mar 25, 2024
ef1a839
Remove `id` from `SharedMemory` don't need it now that `shmctl` isn't…
IzawGithub Mar 25, 2024
e91efa3
Move test to independent file
IzawGithub Mar 25, 2024
d96d5ee
Follow Nix module convention
IzawGithub Mar 25, 2024
6aa8740
Update independent test import
IzawGithub Mar 25, 2024
fd108e4
Rename `system_v` feature to `sysvipc`, like Linux
IzawGithub Mar 25, 2024
2fde2c5
Update doctest import path
IzawGithub Mar 25, 2024
e531e8b
Remove `IPC_PRIVATE` value from `ShmgetFlag`, because it wasn't actua…
IzawGithub Mar 25, 2024
fee5acb
Formatting parse
IzawGithub Mar 25, 2024
52473ce
Update comment of function that moved
IzawGithub Mar 25, 2024
d526780
Fix typo `test_smh` to `test_shm`
IzawGithub Mar 26, 2024
400f71e
Add a test for unsafe `shmget`. Branch coverage is now 89.29%
IzawGithub Mar 26, 2024
d619c48
Improve code comment
IzawGithub Mar 26, 2024
ca0c5f9
Formatting parse
IzawGithub Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
First try at implementing SystemV
IzawGithub committed Feb 13, 2024
commit 096cb21703f5663fc4ec3e111cc68fadb55795b7
114 changes: 114 additions & 0 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
@@ -633,3 +633,117 @@ pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {

Errno::result(ret).map(drop)
}


libc_bitflags! {
pub struct ShmgetFlag: c_int
{
IPC_PRIVATE;
IPC_CREAT;
IPC_EXCL;
SHM_HUGETLB;
SHM_NORESERVE;
}
}
/// Creates and returns a new, or returns an existing, System V shared memory
/// segment identifier.
///
/// For more information, see [`shmget(2)`].
///
/// [`shmget(2)`]: https://man7.org/linux/man-pages/man2/shmget.2.html
pub fn shmget(
key: key_t,
size: size_t,
shmflg: Vec<ShmgetFlag>,
) -> Result<i32> {
let mut flags: c_int = ShmgetFlag::empty().bits();
for flag in shmflg {
flags |= flag.bits;
}
Errno::result(unsafe { libc::shmget(key, size, flags) })
}

libc_bitflags! {
pub struct ShmatFlag: c_int
{
SHM_EXEC;
SHM_RND;
SHM_RDONLY;
#[cfg(any(target_os = "linux"))]
SHM_REMAP;
}
}
/// Attaches the System V shared memory segment identified by `shmid` to the
/// address space of the calling process.
///
/// For more information, see [`shmat(2)`].
///
/// # Safety
///
/// `shmid` should be a valid shared memory identifier and
/// `shmaddr` must meet the requirements described in the [`shmat(2)`] man page.
///
/// [`shmat(2)`]: https://man7.org/linux/man-pages/man2/shmat.2.html
pub fn shmat(
shmid: c_int,
shmaddr: *const c_void,
shmflg: Vec<ShmatFlag>,
) -> Result<*mut c_void> {
let mut flags: c_int = ShmatFlag::empty().bits();
for flag in shmflg {
flags |= flag.bits;
}
Errno::result(unsafe { libc::shmat(shmid, shmaddr, flags) })
}

/// Performs the reverse of [`shmat`], detaching the shared memory segment at
/// the given address from the address space of the calling process.
///
/// For more information, see [`shmdt(2)`].
///
/// # Safety
///
/// `shmaddr` must meet the requirements described in the [`shmdt(2)`] man page.
///
/// [`shmdt(2)`]: https://man7.org/linux/man-pages/man2/shmdt.2.html
pub fn shmdt(shmaddr: *const c_void) -> Result<()> {
Errno::result(unsafe {libc::shmdt(shmaddr)}).map(drop)
}

libc_bitflags! {
pub struct ShmctlFlag: c_int {
#[cfg(any(target_os = "linux"))]
IPC_INFO;
IPC_SET;
IPC_STAT;
IPC_RMID;
// not available in libc but should be?
// #[cfg(any(target_os = "linux"))]
// SHM_INFO;
// #[cfg(any(target_os = "linux"))]
// SHM_STAT;
// #[cfg(any(target_os = "linux"))]
// SHM_STAT_ANY;
#[cfg(any(target_os = "linux"))]
SHM_LOCK;
#[cfg(any(target_os = "linux"))]
SHM_UNLOCK;
}
}
/// Performs control operation specified by `cmd` on the System V shared
/// memory segment given by `shmid`.
///
/// For more information, see [`shmctl(2)`].
///
/// # Safety
///
/// All arguments should be valid and meet the requirements described in the [`shmctl(2)`] man page.
///
/// [`shmctl(2)`]: https://man7.org/linux/man-pages/man2/shmctl.2.html
pub fn shmctl(
shmid: c_int,
cmd: c_int,
buf: *mut shmid_ds,
) -> Result<c_int> {
Errno::result(unsafe {libc::shmctl(shmid, cmd, buf)} )
}