Skip to content

Commit d0d3881

Browse files
committed
Use rustix/libc instead of nix
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.
1 parent ea81ff2 commit d0d3881

File tree

4 files changed

+19
-36
lines changed

4 files changed

+19
-36
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ harness = false
1818

1919
[features]
2020
default = ["kms", "x11", "x11-dlopen", "wayland", "wayland-dlopen"]
21-
kms = ["bytemuck", "drm", "nix"]
22-
wayland = ["wayland-backend", "wayland-client", "memmap2", "nix", "fastrand"]
21+
kms = ["bytemuck", "drm", "libc", "rustix"]
22+
wayland = ["wayland-backend", "wayland-client", "memmap2", "rustix", "fastrand"]
2323
wayland-dlopen = ["wayland-sys/dlopen"]
24-
x11 = ["as-raw-xcb-connection", "bytemuck", "nix", "tiny-xlib", "x11rb"]
24+
x11 = ["as-raw-xcb-connection", "bytemuck", "libc", "rustix", "tiny-xlib", "x11rb"]
2525
x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"]
2626

2727
[dependencies]
@@ -33,7 +33,8 @@ as-raw-xcb-connection = { version = "1.0.0", optional = true }
3333
bytemuck = { version = "1.12.3", optional = true }
3434
drm = { version = "0.10.0", default-features = false, optional = true }
3535
memmap2 = { version = "0.9.0", optional = true }
36-
nix = { version = "0.27.0", features = ["fs", "mman"], optional = true }
36+
libc = { version = "0.2.149", optional = true }
37+
rustix = { version = "0.38.19", features = ["fs", "mm", "shm"], optional = true }
3738
tiny-xlib = { version = "0.2.1", optional = true }
3839
wayland-backend = { version = "0.3.0", features = ["client_system"], optional = true }
3940
wayland-client = { version = "0.31.0", optional = true }

src/kms.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,8 @@ impl BufferImpl<'_> {
332332
// this is going to fail. Low hanging fruit PR: add a flag that's set to false if this
333333
// returns `ENOSYS` and check that before allocating the above and running this.
334334
match self.display.dirty_framebuffer(self.front_fb, &rectangles) {
335-
Ok(())
336-
| Err(drm::SystemError::Unknown {
337-
errno: nix::errno::Errno::ENOSYS,
338-
}) => {}
335+
Ok(()) => {}
336+
Err(drm::SystemError::Unknown { errno }) if errno as i32 == libc::ENOSYS => {}
339337
Err(e) => {
340338
return Err(SoftBufferError::PlatformError(
341339
Some("failed to dirty framebuffer".into()),

src/wayland/buffer.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,19 @@ use super::State;
1818

1919
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
2020
fn create_memfile() -> File {
21-
use nix::{
22-
fcntl::{fcntl, FcntlArg, SealFlag},
23-
sys::memfd::{memfd_create, MemFdCreateFlag},
24-
};
21+
use rustix::fs::{MemfdFlags, SealFlags};
2522

2623
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
27-
let fd = memfd_create(
28-
name,
29-
MemFdCreateFlag::MFD_CLOEXEC | MemFdCreateFlag::MFD_ALLOW_SEALING,
30-
)
31-
.expect("Failed to create memfd to store buffer.");
32-
let _ = fcntl(
33-
fd.as_raw_fd(),
34-
FcntlArg::F_ADD_SEALS(SealFlag::F_SEAL_SHRINK | SealFlag::F_SEAL_SEAL),
35-
)
36-
.expect("Failed to seal memfd.");
24+
let fd = rustix::fs::memfd_create(name, MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING)
25+
.expect("Failed to create memfd to store buffer.");
26+
rustix::fs::fcntl_add_seals(&fd, SealFlags::SHRINK | SealFlags::SEAL)
27+
.expect("Failed to seal memfd.");
3728
File::from(fd)
3829
}
3930

4031
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
4132
fn create_memfile() -> File {
42-
use nix::{
43-
errno::Errno,
44-
fcntl::OFlag,
45-
sys::{
46-
mman::{shm_open, shm_unlink},
47-
stat::Mode,
48-
},
49-
};
33+
use rustix::{fs::Mode, io::Errno, shm::ShmOFlags};
5034
use std::iter;
5135

5236
// Use a cached RNG to avoid hammering the thread local.
@@ -59,14 +43,14 @@ fn create_memfile() -> File {
5943

6044
let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) };
6145
// `CLOEXEC` is implied with `shm_open`
62-
let fd = shm_open(
46+
let fd = rustix::shm::shm_open(
6347
name,
64-
OFlag::O_RDWR | OFlag::O_CREAT | OFlag::O_EXCL,
65-
Mode::S_IRWXU,
48+
ShmOFlags::RDWR | ShmOFlags::CREATE | ShmOFlags::EXCL,
49+
Mode::RWXU,
6650
);
67-
if !matches!(fd, Err(Errno::EEXIST)) {
51+
if !matches!(fd, Err(Errno::EXIST)) {
6852
let fd = fd.expect("Failed to create POSIX shm to store buffer.");
69-
let _ = shm_unlink(name);
53+
let _ = rustix::shm::shm_unlink(name);
7054
return File::from(fd);
7155
}
7256
}

src/x11.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use crate::error::SwResultExt;
99
use crate::{Rect, SoftBufferError};
10-
use nix::libc::{shmat, shmctl, shmdt, shmget, IPC_PRIVATE, IPC_RMID};
10+
use libc::{shmat, shmctl, shmdt, shmget, IPC_PRIVATE, IPC_RMID};
1111
use raw_window_handle::{XcbDisplayHandle, XcbWindowHandle, XlibDisplayHandle, XlibWindowHandle};
1212
use std::ptr::{null_mut, NonNull};
1313
use std::{

0 commit comments

Comments
 (0)