diff --git a/aya-tool/src/rustfmt.rs b/aya-tool/src/rustfmt.rs index cc7ac292..b89ea32d 100644 --- a/aya-tool/src/rustfmt.rs +++ b/aya-tool/src/rustfmt.rs @@ -1,6 +1,6 @@ use std::{ io::{self, Write}, - process::{Command, Stdio}, + process::{Command, Output, Stdio}, }; pub fn format(code: &str) -> Result { @@ -11,15 +11,16 @@ pub fn format(code: &str) -> Result { let stdin = child.stdin.as_mut().unwrap(); stdin.write_all(code.as_bytes())?; - let output = child.wait_with_output()?; - if !output.status.success() { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "rustfmt failed with exit code: {}", - output.status.code().unwrap() - ), - )); + let Output { + status, + stdout, + stderr, + } = child.wait_with_output()?; + if !status.success() { + let stderr = String::from_utf8(stderr).unwrap(); + return Err(io::Error::other(format!( + "rustfmt failed: {status:?}\n{stderr}" + ))); } - Ok(String::from_utf8(output.stdout).unwrap()) + Ok(String::from_utf8(stdout).unwrap()) } diff --git a/aya/src/maps/ring_buf.rs b/aya/src/maps/ring_buf.rs index 27b20914..65125464 100644 --- a/aya/src/maps/ring_buf.rs +++ b/aya/src/maps/ring_buf.rs @@ -436,10 +436,7 @@ impl MMap { // about a null pointer, we check it anyway. MapError::SyscallError(SyscallError { call: "mmap", - io_error: io::Error::new( - io::ErrorKind::Other, - "mmap returned null pointer", - ), + io_error: io::Error::other("mmap returned null pointer"), }), )?, len, diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index f5b796ca..7118460c 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -325,12 +325,7 @@ fn read_sys_fs_perf_type(pmu: &str) -> Result { .join("type"); fs::read_to_string(&file) - .and_then(|perf_ty| { - perf_ty - .trim() - .parse::() - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) - }) + .and_then(|perf_ty| perf_ty.trim().parse::().map_err(io::Error::other)) .map_err(|e| (file, e)) } @@ -344,11 +339,9 @@ fn read_sys_fs_perf_ret_probe(pmu: &str) -> Result { let mut parts = data.trim().splitn(2, ':').skip(1); let config = parts .next() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid format"))?; + .ok_or_else(|| io::Error::other("invalid format"))?; - config - .parse::() - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + config.parse::().map_err(io::Error::other) }) .map_err(|e| (file, e)) } diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index 9f0d3469..45ff5e55 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -1,5 +1,9 @@ //! Tracepoint programs. -use std::{fs, io, os::fd::AsFd as _, path::Path}; +use std::{ + fs, io, + os::fd::AsFd as _, + path::{Path, PathBuf}, +}; use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT}; use thiserror::Error; @@ -21,7 +25,7 @@ pub enum TracePointError { #[error("`{filename}`")] FileError { /// The file name - filename: String, + filename: PathBuf, /// The [`io::Error`] returned from the file operation #[source] io_error: io::Error, @@ -122,19 +126,21 @@ pub(crate) fn read_sys_fs_trace_point_id( category: &str, name: &Path, ) -> Result { - let file = tracefs.join("events").join(category).join(name).join("id"); + let filename = tracefs.join("events").join(category).join(name).join("id"); - let id = fs::read_to_string(&file).map_err(|io_error| TracePointError::FileError { - filename: file.display().to_string(), - io_error, - })?; - let id = id - .trim() - .parse::() - .map_err(|error| TracePointError::FileError { - filename: file.display().to_string(), - io_error: io::Error::new(io::ErrorKind::Other, error), - })?; + let id = match fs::read_to_string(&filename) { + Ok(id) => id, + Err(io_error) => return Err(TracePointError::FileError { filename, io_error }), + }; + let id = match id.trim().parse::() { + Ok(id) => id, + Err(error) => { + return Err(TracePointError::FileError { + filename, + io_error: io::Error::other(error), + }) + } + }; Ok(id) } diff --git a/aya/src/programs/utils.rs b/aya/src/programs/utils.rs index 9eabbd81..faa6effb 100644 --- a/aya/src/programs/utils.rs +++ b/aya/src/programs/utils.rs @@ -55,7 +55,8 @@ pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> { TRACE_FS .as_deref() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "tracefs not found").into()) + .ok_or_else(|| io::Error::other("tracefs not found")) + .map_err(Into::into) } /// The time at which the system is booted. diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index 985cc807..6b8b5e7b 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -212,10 +212,9 @@ pub(crate) unsafe fn netlink_qdisc_attach( None => { // if sock.recv() succeeds we should never get here unless there's a // bug in the kernel - return Err(NetlinkError(NetlinkErrorInternal::IoError(io::Error::new( - io::ErrorKind::Other, - "no RTM_NEWTFILTER reply received, this is a bug.", - )))); + return Err(NetlinkError(NetlinkErrorInternal::IoError( + io::Error::other("no RTM_NEWTFILTER reply received, this is a bug."), + ))); } }; @@ -495,28 +494,24 @@ struct NetlinkMessage { impl NetlinkMessage { fn read(buf: &[u8]) -> Result { if mem::size_of::() > buf.len() { - return Err(io::Error::new( - io::ErrorKind::Other, - "buffer smaller than nlmsghdr", - )); + return Err(io::Error::other("buffer smaller than nlmsghdr")); } // Safety: nlmsghdr is POD so read is safe let header = unsafe { ptr::read_unaligned(buf.as_ptr() as *const nlmsghdr) }; let msg_len = header.nlmsg_len as usize; if msg_len < mem::size_of::() || msg_len > buf.len() { - return Err(io::Error::new(io::ErrorKind::Other, "invalid nlmsg_len")); + return Err(io::Error::other("invalid nlmsg_len")); } let data_offset = align_to(mem::size_of::(), NLMSG_ALIGNTO as usize); if data_offset >= buf.len() { - return Err(io::Error::new(io::ErrorKind::Other, "need more data")); + return Err(io::Error::other("need more data")); } let (rest, error) = if header.nlmsg_type == NLMSG_ERROR as u16 { if data_offset + mem::size_of::() > buf.len() { - return Err(io::Error::new( - io::ErrorKind::Other, + return Err(io::Error::other( "NLMSG_ERROR but not enough space for nlmsgerr", )); } @@ -625,7 +620,7 @@ fn write_attr_header(buf: &mut [u8], offset: usize, attr: nlattr) -> Result Result { let align_len = align_to(value.len(), NLA_ALIGNTO as usize); if offset + align_len > buf.len() { - return Err(io::Error::new(io::ErrorKind::Other, "no space left")); + return Err(io::Error::other("no space left")); } buf[offset..offset + value.len()].copy_from_slice(value); @@ -706,8 +701,8 @@ pub(crate) enum NlAttrError { } impl From for io::Error { - fn from(e: NlAttrError) -> Self { - Self::new(io::ErrorKind::Other, e) + fn from(err: NlAttrError) -> Self { + Self::other(err) } } diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index cf46453f..158e82d5 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -6416,7 +6416,7 @@ pub fn aya::programs::tp_btf::BtfTracePointLinkId::from(t: T) -> T pub mod aya::programs::trace_point pub enum aya::programs::trace_point::TracePointError pub aya::programs::trace_point::TracePointError::FileError -pub aya::programs::trace_point::TracePointError::FileError::filename: alloc::string::String +pub aya::programs::trace_point::TracePointError::FileError::filename: std::path::PathBuf pub aya::programs::trace_point::TracePointError::FileError::io_error: std::io::error::Error impl core::convert::From for aya::programs::ProgramError pub fn aya::programs::ProgramError::from(source: aya::programs::trace_point::TracePointError) -> Self @@ -7887,7 +7887,7 @@ impl core::convert::From for aya::programs::tc::TcError pub fn aya::programs::tc::TcError::from(t: T) -> T pub enum aya::programs::TracePointError pub aya::programs::TracePointError::FileError -pub aya::programs::TracePointError::FileError::filename: alloc::string::String +pub aya::programs::TracePointError::FileError::filename: std::path::PathBuf pub aya::programs::TracePointError::FileError::io_error: std::io::error::Error impl core::convert::From for aya::programs::ProgramError pub fn aya::programs::ProgramError::from(source: aya::programs::trace_point::TracePointError) -> Self