Skip to content

Commit 79c3ff5

Browse files
committed
x11: dup fd before passing to shm_attach_fd
This function takes `Into<RawFdContainer>`. 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<RawFdContainer>` is implemented for anything implementing `IntoRawFd`, so passing `OwnedFd` works. Fixes #168. Should be backported to 0.3.x.
1 parent 2eef592 commit 79c3ff5

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/x11.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use raw_window_handle::{
1111
HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle, XcbDisplayHandle,
1212
XcbWindowHandle,
1313
};
14-
use rustix::{fd, mm, shm as posix_shm};
14+
use rustix::{
15+
fd::{AsFd, BorrowedFd, OwnedFd},
16+
mm, shm as posix_shm,
17+
};
1518

1619
use std::{
1720
fmt,
@@ -618,7 +621,7 @@ impl ShmBuffer {
618621
) -> Result<(), PushBufferError> {
619622
// Register the guard.
620623
let new_id = conn.generate_id()?;
621-
conn.shm_attach_fd(new_id, fd::AsRawFd::as_raw_fd(&seg), true)?
624+
conn.shm_attach_fd(new_id, seg.as_fd().try_clone_to_owned().unwrap(), true)?
622625
.ignore_error();
623626

624627
// Take out the old one and detach it.
@@ -738,9 +741,9 @@ impl ShmSegment {
738741
}
739742
}
740743

741-
impl fd::AsRawFd for ShmSegment {
742-
fn as_raw_fd(&self) -> fd::RawFd {
743-
self.id.as_raw_fd()
744+
impl AsFd for ShmSegment {
745+
fn as_fd(&self) -> BorrowedFd<'_> {
746+
self.id.as_fd()
744747
}
745748
}
746749

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

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

791794
let mut rng = fastrand::Rng::new();
@@ -838,7 +841,7 @@ fn is_shm_available(c: &impl Connection) -> bool {
838841
};
839842

840843
let (attach, detach) = {
841-
let attach = c.shm_attach_fd(seg_id, fd::AsRawFd::as_raw_fd(&seg), false);
844+
let attach = c.shm_attach_fd(seg_id, seg.as_fd().try_clone_to_owned().unwrap(), false);
842845
let detach = c.shm_detach(seg_id);
843846

844847
match (attach, detach) {

0 commit comments

Comments
 (0)