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

Take OwnedFd instead of RawFd in new_from_fd #45

Merged
merged 1 commit into from
Oct 1, 2023

Conversation

ids1024
Copy link
Contributor

@ids1024 ids1024 commented Sep 30, 2023

This is a breaking API change and requires Rust 1.63.

The function still needs to be unsafe since the fd is mmaped, but this is more idiomatic and makes it clear that the file descriptor is owned rather than borrowed. (Which should match the existing implementation). Though maybe it should only borrow the FD?

It wasn't documented explicitly if this took ownership of the fd, and this wasn't handled correctly in smithay-client-toolkit: Smithay/client-toolkit#412.

This is a breaking API change and requires Rust 1.63.

The function still needs to be `unsafe` since the fd is mmaped, but this
is more idiomatic and makes it clear that the file descriptor is owned
rather than borrowed. (Which should match the existing implementation).
Though maybe it should only borrow the FD?
@rtbo
Copy link
Collaborator

rtbo commented Oct 1, 2023

Looking at the implementation, it seems that RawFd is the correct type here.
The fd is used to map the file to memory, and the map is dropped by when the function returns.
So the Keymap doesn't keep any reference to the file whatsoever.

@rtbo
Copy link
Collaborator

rtbo commented Oct 1, 2023

it seems that RawFd is the correct type here

In fact, no. Your PR is correct.
File::from_raw_fd consumes the descriptor, and closes it just after the memory is mapped.

@rtbo rtbo merged commit 011ea62 into rust-x-bindings:master Oct 1, 2023
1 check passed
size: usize,
format: KeymapFormat,
flags: KeymapCompileFlags,
) -> std::io::Result<Option<Keymap>> {
let map = MmapOptions::new()
.len(size as usize)
// Starting in version 7 of the wl_keyboard protocol, the keymap must be mapped using MAP_PRIVATE.
.map_copy_read_only(&fs::File::from_raw_fd(fd))?;
.map_copy_read_only(&fs::File::from(fd))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why convert to a file? &fd works just fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just preserved the behavior it had before, with a type that communicates it.

It looks like map_copy_read_only in memmap2 used to take an &File, so that's probably why a conversion was used here. But now uses a trait MmapAsRawDesc, which works of RawFd and references to types implementing AsRawFd.

So this change works. It also should be possible to accepted a BorrowedFd. (The caller needs to ensure the data isn't mutated while mapped for safety regardless.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants