From 6050931ed18e1006bccfdae19363fd65d5d270b9 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 22 Sep 2023 15:34:06 -0700 Subject: [PATCH] Add `shm_open`/`shm_unlink` (POSIX shared memory) Fixes https://github.com/bytecodealliance/rustix/issues/705. --- Cargo.toml | 4 +++ src/backend/libc/mod.rs | 8 +++++ src/backend/libc/shm/mod.rs | 2 ++ src/backend/libc/shm/syscalls.rs | 25 ++++++++++++++ src/backend/libc/shm/types.rs | 30 ++++++++++++++++ src/backend/linux_raw/mod.rs | 2 ++ src/backend/linux_raw/shm/mod.rs | 2 ++ src/backend/linux_raw/shm/syscalls.rs | 49 +++++++++++++++++++++++++++ src/backend/linux_raw/shm/types.rs | 30 ++++++++++++++++ src/lib.rs | 8 +++++ src/shm.rs | 39 +++++++++++++++++++++ 11 files changed, 199 insertions(+) create mode 100644 src/backend/libc/shm/mod.rs create mode 100644 src/backend/libc/shm/syscalls.rs create mode 100644 src/backend/libc/shm/types.rs create mode 100644 src/backend/linux_raw/shm/mod.rs create mode 100644 src/backend/linux_raw/shm/syscalls.rs create mode 100644 src/backend/linux_raw/shm/types.rs create mode 100644 src/shm.rs diff --git a/Cargo.toml b/Cargo.toml index 652c28491..42ca74328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -149,6 +149,9 @@ thread = ["linux-raw-sys/prctl"] # Enable `rustix::process::*`. process = ["linux-raw-sys/prctl"] +# Enable `rustix::shm::*`. +shm = ["fs"] + # Enable `rustix::time::*`. time = [] @@ -198,6 +201,7 @@ all-apis = [ "pty", "rand", "runtime", + "shm", "system", "stdio", "termios", diff --git a/src/backend/libc/mod.rs b/src/backend/libc/mod.rs index 59fc3bea1..09e5adf29 100644 --- a/src/backend/libc/mod.rs +++ b/src/backend/libc/mod.rs @@ -184,6 +184,14 @@ pub(crate) mod pid; #[cfg(any(feature = "process", feature = "thread"))] #[cfg(linux_kernel)] pub(crate) mod prctl; +#[cfg(not(any( + windows, + target_os = "android", + target_os = "espidf", + target_os = "wasi" +)))] +#[cfg(feature = "shm")] +pub(crate) mod shm; #[cfg(any(feature = "fs", feature = "thread", feature = "process"))] #[cfg(not(any(windows, target_os = "wasi")))] pub(crate) mod ugid; diff --git a/src/backend/libc/shm/mod.rs b/src/backend/libc/shm/mod.rs new file mode 100644 index 000000000..1e0181a99 --- /dev/null +++ b/src/backend/libc/shm/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod syscalls; +pub(crate) mod types; diff --git a/src/backend/libc/shm/syscalls.rs b/src/backend/libc/shm/syscalls.rs new file mode 100644 index 000000000..b0d907ff2 --- /dev/null +++ b/src/backend/libc/shm/syscalls.rs @@ -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 { + // 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))) } +} diff --git a/src/backend/libc/shm/types.rs b/src/backend/libc/shm/types.rs new file mode 100644 index 000000000..173d95108 --- /dev/null +++ b/src/backend/libc/shm/types.rs @@ -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); + + /// + const _ = !0; + } +} diff --git a/src/backend/linux_raw/mod.rs b/src/backend/linux_raw/mod.rs index 9a07467f4..388f573dc 100644 --- a/src/backend/linux_raw/mod.rs +++ b/src/backend/linux_raw/mod.rs @@ -67,6 +67,8 @@ pub(crate) mod pty; pub(crate) mod rand; #[cfg(feature = "runtime")] pub(crate) mod runtime; +#[cfg(feature = "shm")] +pub(crate) mod shm; #[cfg(feature = "system")] pub(crate) mod system; #[cfg(feature = "termios")] diff --git a/src/backend/linux_raw/shm/mod.rs b/src/backend/linux_raw/shm/mod.rs new file mode 100644 index 000000000..1e0181a99 --- /dev/null +++ b/src/backend/linux_raw/shm/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod syscalls; +pub(crate) mod types; diff --git a/src/backend/linux_raw/shm/syscalls.rs b/src/backend/linux_raw/shm/syscalls.rs new file mode 100644 index 000000000..114c36a8a --- /dev/null +++ b/src/backend/linux_raw/shm/syscalls.rs @@ -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 { + 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()) +} diff --git a/src/backend/linux_raw/shm/types.rs b/src/backend/linux_raw/shm/types.rs new file mode 100644 index 000000000..7c35642ec --- /dev/null +++ b/src/backend/linux_raw/shm/types.rs @@ -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; + + /// + const _ = !0; + } +} diff --git a/src/lib.rs b/src/lib.rs index a23006f9c..e5f1bc287 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,6 +246,14 @@ pub mod pty; #[cfg(feature = "rand")] #[cfg_attr(doc_cfg, doc(cfg(feature = "rand")))] pub mod rand; +#[cfg(not(any( + windows, + target_os = "android", + target_os = "espidf", + target_os = "wasi" +)))] +#[cfg(feature = "shm")] +pub mod shm; #[cfg(not(windows))] #[cfg(feature = "stdio")] #[cfg_attr(doc_cfg, doc(cfg(feature = "stdio")))] diff --git a/src/shm.rs b/src/shm.rs new file mode 100644 index 000000000..9b1f82b12 --- /dev/null +++ b/src/shm.rs @@ -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(name: P, flags: ShmOFlags, mode: Mode) -> io::Result { + 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(name: P) -> io::Result<()> { + name.into_with_c_str(|name| backend::shm::syscalls::shm_unlink(name)) +}