Skip to content

Commit

Permalink
Use rustix/libc instead of nix
Browse files Browse the repository at this point in the history
Partly addresses
#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.
  • Loading branch information
ids1024 committed Oct 13, 2023
1 parent ea81ff2 commit 8de6207
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 }
Expand Down
6 changes: 2 additions & 4 deletions src/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
38 changes: 11 additions & 27 deletions src/wayland/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
let _ = rustix::fs::fcntl_add_seals(&fd, SealFlags::SHRINK | SealFlags::SEAL)

Check failure on line 26 in src/wayland/buffer.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest)

this let-binding has unit value
.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.
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit 8de6207

Please sign in to comment.