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

@ -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()),

View file

@ -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);
}
}

View file

@ -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::{