-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
shm_open
/shm_unlink
(POSIX shared memory)
Fixes #705.
- Loading branch information
Showing
11 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub(crate) mod syscalls; | ||
pub(crate) mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::ffi::CStr; | ||
|
||
use crate::backend::c; | ||
use crate::backend::conv::{c_str, ret, ret_owned_fd}; | ||
use crate::fd::OwnedFd; | ||
use crate::fs::Mode; | ||
use crate::io; | ||
use crate::shm::ShmOFlags; | ||
|
||
pub(crate) fn shm_open(name: &CStr, oflags: ShmOFlags, mode: Mode) -> io::Result<OwnedFd> { | ||
// On this platforms, `mode_t` is `u16` and can't be passed directly to a | ||
// variadic function. | ||
#[cfg(apple)] | ||
let mode: c::c_uint = mode.bits().into(); | ||
|
||
// Otherwise, cast to `mode_t` as that's what `open` is documented to take. | ||
#[cfg(not(apple))] | ||
let mode: c::mode_t = mode.bits() as _; | ||
|
||
unsafe { ret_owned_fd(c::shm_open(c_str(name), bitflags_bits!(oflags), mode)) } | ||
} | ||
|
||
pub(crate) fn shm_unlink(name: &CStr) -> io::Result<()> { | ||
unsafe { ret(c::shm_unlink(c_str(name))) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::backend::c; | ||
use bitflags::bitflags; | ||
|
||
bitflags! { | ||
/// `O_*` constants for use with [`shm_open`]. | ||
/// | ||
/// [`shm_open`]: crate:shm::shm_open | ||
#[repr(transparent)] | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] | ||
pub struct ShmOFlags: u32 { | ||
/// `O_CREAT` | ||
#[doc(alias = "CREAT")] | ||
const CREATE = bitcast!(c::O_CREAT); | ||
|
||
/// `O_EXCL` | ||
const EXCL = bitcast!(c::O_EXCL); | ||
|
||
/// `O_RDONLY` | ||
const RDONLY = bitcast!(c::O_RDONLY); | ||
|
||
/// `O_RDWR` | ||
const RDWR = bitcast!(c::O_RDWR); | ||
|
||
/// `O_TRUNC` | ||
const TRUNC = bitcast!(c::O_TRUNC); | ||
|
||
/// <https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags> | ||
const _ = !0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub(crate) mod syscalls; | ||
pub(crate) mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::ffi::CStr; | ||
|
||
use crate::backend::fs::{ | ||
syscalls::{open, unlink}, | ||
types::{Mode, OFlags}, | ||
}; | ||
use crate::fd::OwnedFd; | ||
use crate::io; | ||
use crate::shm::ShmOFlags; | ||
|
||
const NAME_MAX: usize = 255; | ||
const SHM_DIR: &[u8] = b"/dev/shm/"; | ||
|
||
fn get_shm_name(name: &CStr) -> io::Result<([u8; NAME_MAX + SHM_DIR.len() + 1], usize)> { | ||
let name = name.to_bytes(); | ||
|
||
if name.len() > NAME_MAX { | ||
return Err(io::Errno::NAMETOOLONG); | ||
} | ||
|
||
let num_slashes = name.into_iter().take_while(|x| **x == b'/').count(); | ||
let after_slashes = &name[num_slashes..]; | ||
if after_slashes.is_empty() | ||
|| after_slashes == b"." | ||
|| after_slashes == b".." | ||
|| after_slashes.contains(&b'/') | ||
{ | ||
return Err(io::Errno::INVAL); | ||
} | ||
|
||
let mut path = [0; NAME_MAX + SHM_DIR.len() + 1]; | ||
path[..SHM_DIR.len()].copy_from_slice(SHM_DIR); | ||
path[SHM_DIR.len()..SHM_DIR.len() + name.len()].copy_from_slice(name); | ||
Ok((path, SHM_DIR.len() + name.len() + 1)) | ||
} | ||
|
||
pub(crate) fn shm_open(name: &CStr, oflags: ShmOFlags, mode: Mode) -> io::Result<OwnedFd> { | ||
let (path, len) = get_shm_name(name)?; | ||
open( | ||
CStr::from_bytes_with_nul(&path[..len]).unwrap(), | ||
OFlags::from_bits(oflags.bits()).unwrap() | OFlags::CLOEXEC, | ||
mode, | ||
) | ||
} | ||
|
||
pub(crate) fn shm_unlink(name: &CStr) -> io::Result<()> { | ||
let (path, len) = get_shm_name(name)?; | ||
unlink(CStr::from_bytes_with_nul(&path[..len]).unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::backend::c; | ||
use bitflags::bitflags; | ||
|
||
bitflags! { | ||
/// `O_*` constants for use with [`shm_open`]. | ||
/// | ||
/// [`shm_open`]: crate:shm::shm_open | ||
#[repr(transparent)] | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] | ||
pub struct ShmOFlags: c::c_uint { | ||
/// `O_CREAT` | ||
#[doc(alias = "CREAT")] | ||
const CREATE = linux_raw_sys::general::O_CREAT; | ||
|
||
/// `O_EXCL` | ||
const EXCL = linux_raw_sys::general::O_EXCL; | ||
|
||
/// `O_RDONLY` | ||
const RDONLY = linux_raw_sys::general::O_RDONLY; | ||
|
||
/// `O_RDWR` | ||
const RDWR = linux_raw_sys::general::O_RDWR; | ||
|
||
/// `O_TRUNC` | ||
const TRUNC = linux_raw_sys::general::O_TRUNC; | ||
|
||
/// <https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags> | ||
const _ = !0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! POSIX shared memory | ||
use crate::fd::OwnedFd; | ||
use crate::{backend, io, path}; | ||
|
||
pub use crate::backend::fs::types::Mode; | ||
pub use crate::backend::shm::types::ShmOFlags; | ||
|
||
/// `shm_open(name, oflags, mode)`—Opens a shared memory object. | ||
/// | ||
/// For portability, `name` should begin with a slash, contain no other slashes, | ||
/// and be no longer than an implementation-defined limit (255 on Linux). | ||
/// | ||
/// Exactly one of [ShmOFlags::RDONLY] and [ShmOFlags::RDWR] should be passed. The file | ||
/// descriptor will be opened with `FD_CLOEXEC` set. | ||
/// | ||
/// # References | ||
/// - [POSIX] | ||
/// - [Linux] | ||
/// | ||
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_open.html | ||
/// [Linux]: https://man7.org/linux/man-pages/man3/shm_open.3.html | ||
#[inline] | ||
pub fn shm_open<P: path::Arg>(name: P, flags: ShmOFlags, mode: Mode) -> io::Result<OwnedFd> { | ||
name.into_with_c_str(|name| backend::shm::syscalls::shm_open(name, flags, mode)) | ||
} | ||
|
||
/// `shm_unlink(name)`—Unlinks a shared memory object. | ||
/// | ||
/// # References | ||
/// - [POSIX] | ||
/// - [Linux] | ||
/// | ||
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_unlink.html | ||
/// [Linux]: https://man7.org/linux/man-pages/man3/shm_unlink.3.html | ||
#[inline] | ||
pub fn shm_unlink<P: path::Arg>(name: P) -> io::Result<()> { | ||
name.into_with_c_str(|name| backend::shm::syscalls::shm_unlink(name)) | ||
} |