diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/rustix/src/backend/linux_raw/shm | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/rustix/src/backend/linux_raw/shm')
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/shm/mod.rs | 2 | ||||
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/shm/syscalls.rs | 47 | ||||
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/shm/types.rs | 30 |
3 files changed, 79 insertions, 0 deletions
diff --git a/vendor/rustix/src/backend/linux_raw/shm/mod.rs b/vendor/rustix/src/backend/linux_raw/shm/mod.rs new file mode 100644 index 0000000..1e0181a --- /dev/null +++ b/vendor/rustix/src/backend/linux_raw/shm/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod syscalls; +pub(crate) mod types; diff --git a/vendor/rustix/src/backend/linux_raw/shm/syscalls.rs b/vendor/rustix/src/backend/linux_raw/shm/syscalls.rs new file mode 100644 index 0000000..3b083f4 --- /dev/null +++ b/vendor/rustix/src/backend/linux_raw/shm/syscalls.rs @@ -0,0 +1,47 @@ +use crate::ffi::CStr; + +use crate::backend::fs::syscalls::{open, unlink}; +use crate::backend::fs::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.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()) +} diff --git a/vendor/rustix/src/backend/linux_raw/shm/types.rs b/vendor/rustix/src/backend/linux_raw/shm/types.rs new file mode 100644 index 0000000..3343d44 --- /dev/null +++ b/vendor/rustix/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; + + /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags> + const _ = !0; + } +} |