-
Notifications
You must be signed in to change notification settings - Fork 661
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
mmap returns EBADF #2313
Comments
// nix 0.27.1
pub unsafe fn mmap<F: AsFd>(
addr: Option<NonZeroUsize>,
length: NonZeroUsize,
prot: ProtFlags,
flags: MapFlags,
f: Option<F>,
offset: off_t,
) -> Result<*mut c_void> {
let ptr =
addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void);
// `f` dropped here, making `fd` an invalid file descriptor
let fd = f.map(|f| f.as_fd().as_raw_fd()).unwrap_or(-1);
let ret =
libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset); This is right, if you move an owned file descriptor (like the A workaround would be passing fd by reference: nix::sys::mman::mmap(None, NonZeroUsize::new(4096).unwrap(), ProtFlags::PROT_READ,
MapFlags::MAP_SHARED, Some(&fd), 0).expect("mamp"); But anyway, workarounds are just workarounds, let me file a PR to get this issue fixed so that the file descriptor can survive the underlying |
Just realized that this issue does not exist in the master branch as we have separated this API into
so that the $ rg nix Cargo.toml
9:nix = { git = "https://github.com/nix-rust/nix" , features = ["mman"]}
$ cat src/main.rs
use std::fs::File;
use std::num::NonZeroUsize;
use nix::sys::mman::{MapFlags, ProtFlags};
fn main() {
unsafe {
let fd = File::open("Cargo.toml").expect("open");
nix::sys::mman::mmap(None, NonZeroUsize::new(4096).unwrap(), ProtFlags::PROT_READ,
MapFlags::MAP_SHARED, fd, 0).expect("mamp");
}
}%
$ cargo r -q
$ echo $?
0 |
mmap fails to keep the file descriptor alive long enough to pass to mmap. The file descriptor gets closed and then mmap fails with EBADF. This shows the bug:
Resulting output:
The text was updated successfully, but these errors were encountered: