From d0d38810995f2524155f72dedcbace76a9861b96 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 13 Oct 2023 15:41:23 -0700 Subject: [PATCH] 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. --- Cargo.toml | 9 +++++---- src/kms.rs | 6 ++---- src/wayland/buffer.rs | 38 +++++++++++--------------------------- src/x11.rs | 2 +- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b87a13c..2a0915c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,10 +18,10 @@ harness = false [features] default = ["kms", "x11", "x11-dlopen", "wayland", "wayland-dlopen"] -kms = ["bytemuck", "drm", "nix"] -wayland = ["wayland-backend", "wayland-client", "memmap2", "nix", "fastrand"] +kms = ["bytemuck", "drm", "libc", "rustix"] +wayland = ["wayland-backend", "wayland-client", "memmap2", "rustix", "fastrand"] 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"] [dependencies] @@ -33,7 +33,8 @@ as-raw-xcb-connection = { version = "1.0.0", optional = true } bytemuck = { version = "1.12.3", optional = true } drm = { version = "0.10.0", default-features = false, 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 } wayland-backend = { version = "0.3.0", features = ["client_system"], optional = true } wayland-client = { version = "0.31.0", optional = true } diff --git a/src/kms.rs b/src/kms.rs index 76f05a4..18f458f 100644 --- a/src/kms.rs +++ b/src/kms.rs @@ -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 // returns `ENOSYS` and check that before allocating the above and running this. match self.display.dirty_framebuffer(self.front_fb, &rectangles) { - Ok(()) - | Err(drm::SystemError::Unknown { - errno: nix::errno::Errno::ENOSYS, - }) => {} + Ok(()) => {} + Err(drm::SystemError::Unknown { errno }) if errno as i32 == libc::ENOSYS => {} Err(e) => { return Err(SoftBufferError::PlatformError( Some("failed to dirty framebuffer".into()), diff --git a/src/wayland/buffer.rs b/src/wayland/buffer.rs index 2f044ca..0c8bf86 100644 --- a/src/wayland/buffer.rs +++ b/src/wayland/buffer.rs @@ -18,35 +18,19 @@ use super::State; #[cfg(any(target_os = "linux", target_os = "freebsd"))] fn create_memfile() -> File { - use nix::{ - fcntl::{fcntl, FcntlArg, SealFlag}, - sys::memfd::{memfd_create, MemFdCreateFlag}, - }; + use rustix::fs::{MemfdFlags, SealFlags}; let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) }; - let fd = memfd_create( - name, - MemFdCreateFlag::MFD_CLOEXEC | MemFdCreateFlag::MFD_ALLOW_SEALING, - ) - .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."); + let fd = rustix::fs::memfd_create(name, MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING) + .expect("Failed to create memfd to store buffer."); + rustix::fs::fcntl_add_seals(&fd, SealFlags::SHRINK | SealFlags::SEAL) + .expect("Failed to seal memfd."); File::from(fd) } #[cfg(not(any(target_os = "linux", target_os = "freebsd")))] fn create_memfile() -> File { - use nix::{ - errno::Errno, - fcntl::OFlag, - sys::{ - mman::{shm_open, shm_unlink}, - stat::Mode, - }, - }; + use rustix::{fs::Mode, io::Errno, shm::ShmOFlags}; use std::iter; // 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()) }; // `CLOEXEC` is implied with `shm_open` - let fd = shm_open( + let fd = rustix::shm::shm_open( name, - OFlag::O_RDWR | OFlag::O_CREAT | OFlag::O_EXCL, - Mode::S_IRWXU, + ShmOFlags::RDWR | ShmOFlags::CREATE | ShmOFlags::EXCL, + 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 _ = shm_unlink(name); + let _ = rustix::shm::shm_unlink(name); return File::from(fd); } } diff --git a/src/x11.rs b/src/x11.rs index c9e546f..8fcd7c2 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -7,7 +7,7 @@ use crate::error::SwResultExt; 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 std::ptr::{null_mut, NonNull}; use std::{