-
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
12 changed files
with
143 additions
and
12 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
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 @@ | ||
pub(crate) mod syscalls; |
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,28 @@ | ||
use std::ffi::CStr; | ||
|
||
use crate::backend::c; | ||
use crate::backend::conv::{c_str, ret, ret_owned_fd}; | ||
use crate::fd::OwnedFd; | ||
use crate::fs::{Mode, OFlags}; | ||
use crate::io; | ||
|
||
#[cfg(not(any(target_os = "android", target_os = "espidf", target_os = "wasi")))] | ||
pub(crate) fn shm_open(name: &CStr, oflags: OFlags, 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)) } | ||
} | ||
|
||
#[cfg(not(any(target_os = "android", target_os = "espidf", target_os = "wasi")))] | ||
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
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 @@ | ||
pub(crate) mod syscalls; |
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,48 @@ | ||
use std::ffi::CStr; | ||
|
||
use crate::fd::OwnedFd; | ||
use crate::backend::fs::{ | ||
syscalls::{open, unlink}, | ||
types::{Mode, OFlags}, | ||
}; | ||
use crate::io; | ||
|
||
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: OFlags, mode: Mode) -> io::Result<OwnedFd> { | ||
let (path, len) = get_shm_name(name)?; | ||
open( | ||
CStr::from_bytes_with_nul(&path[..len]).unwrap(), | ||
oflags, | ||
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
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,34 @@ | ||
//! POSIX shared memory | ||
use crate::backend::fs::types::{Mode, OFlags}; | ||
use crate::fd::OwnedFd; | ||
use crate::{backend, io, path}; | ||
|
||
/// `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). | ||
/// | ||
/// # 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: OFlags, 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)) | ||
} |