Merge pull request #688 from aya-rs/get-fd-owned

sys: bpf_prog_get_fd_by_id returns OwnedFd
reviewable/pr637/r21
Tamir Duberstein 1 year ago committed by GitHub
commit 53d36a3fe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,5 @@
//! Lirc programs.
use std::os::fd::{AsRawFd, RawFd};
use std::os::fd::{AsRawFd, IntoRawFd as _, RawFd};
use crate::{
generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2},
@ -101,12 +101,12 @@ impl LircMode2 {
io_error,
})?;
prog_fds.push(fd as RawFd);
prog_fds.push(fd);
}
Ok(prog_fds
.into_iter()
.map(|prog_fd| LircLink::new(prog_fd, target_fd.as_raw_fd()))
.map(|prog_fd| LircLink::new(prog_fd.into_raw_fd(), target_fd.as_raw_fd()))
.collect())
}
}

@ -69,7 +69,7 @@ use libc::ENOSPC;
use std::{
ffi::CString,
io,
os::unix::io::{AsRawFd, RawFd},
os::fd::{AsRawFd, IntoRawFd as _, RawFd},
path::{Path, PathBuf},
};
use thiserror::Error;
@ -955,7 +955,7 @@ impl ProgramInfo {
call: "bpf_prog_get_fd_by_id",
io_error,
})?;
Ok(fd as RawFd)
Ok(fd.into_raw_fd())
}
/// Loads a program from a pinned path in bpffs.
@ -1003,14 +1003,12 @@ impl Iterator for ProgramsIter {
io_error,
})
.and_then(|fd| {
let info = bpf_prog_get_info_by_fd(fd)
bpf_prog_get_info_by_fd(fd.as_raw_fd())
.map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})
.map(ProgramInfo);
unsafe { libc::close(fd) };
info
.map(ProgramInfo)
}),
)
}

@ -3,7 +3,7 @@ use std::{
ffi::{CStr, CString},
io,
mem::{self, MaybeUninit},
os::unix::io::RawFd,
os::fd::{FromRawFd as _, OwnedFd, RawFd},
slice,
};
@ -460,13 +460,23 @@ pub(crate) fn bpf_prog_query(
ret
}
pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result<RawFd, io::Error> {
pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result<OwnedFd, io::Error> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id;
match sys_bpf(bpf_cmd::BPF_PROG_GET_FD_BY_ID, &attr) {
Ok(v) => Ok(v as RawFd),
Ok(v) => {
let v = v.try_into().map_err(|_err| {
// _err is std::num::TryFromIntError or std::convert::Infallible depending on
// target, so we can't ascribe.
io::Error::new(
io::ErrorKind::InvalidData,
format!("bpf_prog_get_fd_by_id: invalid fd returned: {}", v),
)
})?;
Ok(unsafe { OwnedFd::from_raw_fd(v) })
}
Err((_, err)) => Err(err),
}
}

Loading…
Cancel
Save