sys: bpf_prog_get_fd_by_id returns OwnedFd

pull/688/head
Tamir Duberstein 1 year ago
parent 51cd36378a
commit 76c78e3bf8
No known key found for this signature in database

@ -1,5 +1,5 @@
//! Lirc programs. //! Lirc programs.
use std::os::fd::{AsRawFd, RawFd}; use std::os::fd::{AsRawFd, IntoRawFd as _, RawFd};
use crate::{ use crate::{
generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2}, generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2},
@ -101,12 +101,12 @@ impl LircMode2 {
io_error, io_error,
})?; })?;
prog_fds.push(fd as RawFd); prog_fds.push(fd);
} }
Ok(prog_fds Ok(prog_fds
.into_iter() .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()) .collect())
} }
} }

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

@ -3,7 +3,7 @@ use std::{
ffi::{CStr, CString}, ffi::{CStr, CString},
io, io,
mem::{self, MaybeUninit}, mem::{self, MaybeUninit},
os::unix::io::RawFd, os::fd::{FromRawFd as _, OwnedFd, RawFd},
slice, slice,
}; };
@ -460,13 +460,23 @@ pub(crate) fn bpf_prog_query(
ret 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>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id; attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id;
match sys_bpf(bpf_cmd::BPF_PROG_GET_FD_BY_ID, &attr) { 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), Err((_, err)) => Err(err),
} }
} }

Loading…
Cancel
Save