From 3b98da7e28ef30ba4805d3eebe6d61f92233172d Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Tue, 31 Oct 2023 07:09:50 -0700 Subject: [PATCH] x11: `dup` fd before passing to `shm_attach_fd` This function takes `Into`. So it accepts an owned fd, and closes it. So as long as the API is like this, we need to dup a new fd it can close when calling it. `Into` is implemented for anything implementing `IntoRawFd`, so passing `OwnedFd` works. Fixes https://github.com/rust-windowing/softbuffer/issues/168. Should be backported to 0.3.x. --- src/x11.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/x11.rs b/src/x11.rs index 7427e897..0711d340 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -11,7 +11,10 @@ use raw_window_handle::{ HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle, }; -use rustix::{fd, mm, shm as posix_shm}; +use rustix::{ + fd::{AsFd, BorrowedFd, OwnedFd}, + mm, shm as posix_shm, +}; use std::{ fmt, @@ -618,7 +621,7 @@ impl ShmBuffer { ) -> Result<(), PushBufferError> { // Register the guard. let new_id = conn.generate_id()?; - conn.shm_attach_fd(new_id, fd::AsRawFd::as_raw_fd(&seg), true)? + conn.shm_attach_fd(new_id, seg.as_fd().try_clone_to_owned().unwrap(), true)? .ignore_error(); // Take out the old one and detach it. @@ -738,9 +741,9 @@ impl ShmSegment { } } -impl fd::AsRawFd for ShmSegment { - fn as_raw_fd(&self) -> fd::RawFd { - self.id.as_raw_fd() +impl AsFd for ShmSegment { + fn as_fd(&self) -> BorrowedFd<'_> { + self.id.as_fd() } } @@ -785,7 +788,7 @@ impl Drop for X11Impl { } /// Create a shared memory identifier. -fn create_shm_id() -> io::Result { +fn create_shm_id() -> io::Result { use posix_shm::{Mode, ShmOFlags}; let mut rng = fastrand::Rng::new(); @@ -838,7 +841,7 @@ fn is_shm_available(c: &impl Connection) -> bool { }; let (attach, detach) = { - let attach = c.shm_attach_fd(seg_id, fd::AsRawFd::as_raw_fd(&seg), false); + let attach = c.shm_attach_fd(seg_id, seg.as_fd().try_clone_to_owned().unwrap(), false); let detach = c.shm_detach(seg_id); match (attach, detach) {