Use rustix/libc instead of nix

Partly addresses
https://github.com/rust-windowing/softbuffer/issues/147, though it would
still be desirable to have a good safe API for SYSV shm in Rustix. But
using `libc` directly for now is no worse than using the `nix::libc`
re-export, so we don't lose anything.
This commit is contained in:
Ian Douglas Scott 2023-10-13 15:41:23 -07:00
parent ea81ff2078
commit d0d3881099
4 changed files with 19 additions and 36 deletions

View file

@ -18,10 +18,10 @@ harness = false
[features] [features]
default = ["kms", "x11", "x11-dlopen", "wayland", "wayland-dlopen"] default = ["kms", "x11", "x11-dlopen", "wayland", "wayland-dlopen"]
kms = ["bytemuck", "drm", "nix"] kms = ["bytemuck", "drm", "libc", "rustix"]
wayland = ["wayland-backend", "wayland-client", "memmap2", "nix", "fastrand"] wayland = ["wayland-backend", "wayland-client", "memmap2", "rustix", "fastrand"]
wayland-dlopen = ["wayland-sys/dlopen"] wayland-dlopen = ["wayland-sys/dlopen"]
x11 = ["as-raw-xcb-connection", "bytemuck", "nix", "tiny-xlib", "x11rb"] x11 = ["as-raw-xcb-connection", "bytemuck", "libc", "rustix", "tiny-xlib", "x11rb"]
x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"] x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"]
[dependencies] [dependencies]
@ -33,7 +33,8 @@ as-raw-xcb-connection = { version = "1.0.0", optional = true }
bytemuck = { version = "1.12.3", optional = true } bytemuck = { version = "1.12.3", optional = true }
drm = { version = "0.10.0", default-features = false, optional = true } drm = { version = "0.10.0", default-features = false, optional = true }
memmap2 = { version = "0.9.0", optional = true } memmap2 = { version = "0.9.0", optional = true }
nix = { version = "0.27.0", features = ["fs", "mman"], optional = true } libc = { version = "0.2.149", optional = true }
rustix = { version = "0.38.19", features = ["fs", "mm", "shm"], optional = true }
tiny-xlib = { version = "0.2.1", optional = true } tiny-xlib = { version = "0.2.1", optional = true }
wayland-backend = { version = "0.3.0", features = ["client_system"], optional = true } wayland-backend = { version = "0.3.0", features = ["client_system"], optional = true }
wayland-client = { version = "0.31.0", optional = true } wayland-client = { version = "0.31.0", optional = true }

View file

@ -332,10 +332,8 @@ impl BufferImpl<'_> {
// this is going to fail. Low hanging fruit PR: add a flag that's set to false if this // this is going to fail. Low hanging fruit PR: add a flag that's set to false if this
// returns `ENOSYS` and check that before allocating the above and running this. // returns `ENOSYS` and check that before allocating the above and running this.
match self.display.dirty_framebuffer(self.front_fb, &rectangles) { match self.display.dirty_framebuffer(self.front_fb, &rectangles) {
Ok(()) Ok(()) => {}
| Err(drm::SystemError::Unknown { Err(drm::SystemError::Unknown { errno }) if errno as i32 == libc::ENOSYS => {}
errno: nix::errno::Errno::ENOSYS,
}) => {}
Err(e) => { Err(e) => {
return Err(SoftBufferError::PlatformError( return Err(SoftBufferError::PlatformError(
Some("failed to dirty framebuffer".into()), Some("failed to dirty framebuffer".into()),

View file

@ -18,35 +18,19 @@ use super::State;
#[cfg(any(target_os = "linux", target_os = "freebsd"))] #[cfg(any(target_os = "linux", target_os = "freebsd"))]
fn create_memfile() -> File { fn create_memfile() -> File {
use nix::{ use rustix::fs::{MemfdFlags, SealFlags};
fcntl::{fcntl, FcntlArg, SealFlag},
sys::memfd::{memfd_create, MemFdCreateFlag},
};
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) }; let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
let fd = memfd_create( let fd = rustix::fs::memfd_create(name, MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING)
name, .expect("Failed to create memfd to store buffer.");
MemFdCreateFlag::MFD_CLOEXEC | MemFdCreateFlag::MFD_ALLOW_SEALING, rustix::fs::fcntl_add_seals(&fd, SealFlags::SHRINK | SealFlags::SEAL)
) .expect("Failed to seal memfd.");
.expect("Failed to create memfd to store buffer.");
let _ = fcntl(
fd.as_raw_fd(),
FcntlArg::F_ADD_SEALS(SealFlag::F_SEAL_SHRINK | SealFlag::F_SEAL_SEAL),
)
.expect("Failed to seal memfd.");
File::from(fd) File::from(fd)
} }
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))] #[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
fn create_memfile() -> File { fn create_memfile() -> File {
use nix::{ use rustix::{fs::Mode, io::Errno, shm::ShmOFlags};
errno::Errno,
fcntl::OFlag,
sys::{
mman::{shm_open, shm_unlink},
stat::Mode,
},
};
use std::iter; use std::iter;
// Use a cached RNG to avoid hammering the thread local. // Use a cached RNG to avoid hammering the thread local.
@ -59,14 +43,14 @@ fn create_memfile() -> File {
let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) }; let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) };
// `CLOEXEC` is implied with `shm_open` // `CLOEXEC` is implied with `shm_open`
let fd = shm_open( let fd = rustix::shm::shm_open(
name, name,
OFlag::O_RDWR | OFlag::O_CREAT | OFlag::O_EXCL, ShmOFlags::RDWR | ShmOFlags::CREATE | ShmOFlags::EXCL,
Mode::S_IRWXU, Mode::RWXU,
); );
if !matches!(fd, Err(Errno::EEXIST)) { if !matches!(fd, Err(Errno::EXIST)) {
let fd = fd.expect("Failed to create POSIX shm to store buffer."); let fd = fd.expect("Failed to create POSIX shm to store buffer.");
let _ = shm_unlink(name); let _ = rustix::shm::shm_unlink(name);
return File::from(fd); return File::from(fd);
} }
} }

View file

@ -7,7 +7,7 @@
use crate::error::SwResultExt; use crate::error::SwResultExt;
use crate::{Rect, SoftBufferError}; use crate::{Rect, SoftBufferError};
use nix::libc::{shmat, shmctl, shmdt, shmget, IPC_PRIVATE, IPC_RMID}; use libc::{shmat, shmctl, shmdt, shmget, IPC_PRIVATE, IPC_RMID};
use raw_window_handle::{XcbDisplayHandle, XcbWindowHandle, XlibDisplayHandle, XlibWindowHandle}; use raw_window_handle::{XcbDisplayHandle, XcbWindowHandle, XlibDisplayHandle, XlibWindowHandle};
use std::ptr::{null_mut, NonNull}; use std::ptr::{null_mut, NonNull};
use std::{ use std::{