Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x11: dup fd before passing to shm_attach_fd #170

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -785,7 +788,7 @@ impl<D: ?Sized, W: ?Sized> Drop for X11Impl<D, W> {
}

/// Create a shared memory identifier.
fn create_shm_id() -> io::Result<fd::OwnedFd> {
fn create_shm_id() -> io::Result<OwnedFd> {
use posix_shm::{Mode, ShmOFlags};

let mut rng = fastrand::Rng::new();
Expand Down Expand Up @@ -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) {
Expand Down