Skip to content

Commit

Permalink
Merge pull request #128 from dfreese/anyhow
Browse files Browse the repository at this point in the history
errors: add internal helpers to simplify error handling
  • Loading branch information
lucab authored Dec 19, 2022
2 parents 2de3b5c + 320c4c8 commit 8c781c9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 40 deletions.
56 changes: 54 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use thiserror::Error;
use std::fmt::Display;

/// Library errors.
#[derive(Error, Debug)]
#[derive(thiserror::Error, Debug)]
#[error("libsystemd error: {msg}")]
pub struct SdError {
pub(crate) kind: ErrorKind,
Expand Down Expand Up @@ -32,3 +32,55 @@ pub(crate) enum ErrorKind {
Generic,
SysusersUnknownType,
}

/// Context is similar to anyhow::Context, in that it provides a mechanism internally to adapt
/// errors from systemd into SdError, while providing additional context in a readable manner.
pub(crate) trait Context<T, E> {
/// Prepend the error with context.
fn context<C>(self, context: C) -> Result<T, SdError>
where
C: Display + Send + Sync + 'static;

/// Prepend the error with context that is lazily evaluated.
fn with_context<C, F>(self, f: F) -> Result<T, SdError>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C;
}

impl<T, E> Context<T, E> for Result<T, E>
where
E: std::error::Error + Send + Sync + 'static,
{
fn context<C>(self, context: C) -> Result<T, SdError>
where
C: Display + Send + Sync + 'static,
{
self.map_err(|e| format!("{}: {}", context, e).into())
}

fn with_context<C, F>(self, context: F) -> Result<T, SdError>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C,
{
self.map_err(|e| format!("{}: {}", context(), e).into())
}
}

impl<T> Context<T, core::convert::Infallible> for Option<T> {
fn context<C>(self, context: C) -> Result<T, SdError>
where
C: Display + Send + Sync + 'static,
{
self.ok_or_else(|| format!("{}", context).into())
}

fn with_context<C, F>(self, context: F) -> Result<T, SdError>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C,
{
self.ok_or_else(|| format!("{}", context()).into())
}
}
77 changes: 39 additions & 38 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::SdError;
use crate::errors::{Context, SdError};
use nix::fcntl::*;
use nix::sys::memfd::memfd_create;
use nix::sys::memfd::MemFdCreateFlag;
Expand Down Expand Up @@ -144,9 +144,9 @@ where
K: AsRef<str>,
V: AsRef<str>,
{
let sock = SD_SOCK.get_or_try_init(|| {
UnixDatagram::unbound().map_err(|e| format!("failed to open datagram socket: {}", e))
})?;
let sock = SD_SOCK
.get_or_try_init(UnixDatagram::unbound)
.context("failed to open datagram socket")?;

let mut data = Vec::new();
add_field_and_payload(&mut data, "PRIORITY", &(u8::from(priority)).to_string());
Expand All @@ -163,21 +163,18 @@ where
//
// Maximum data size is system dependent, thus this always tries the fast path and
// falls back to the slow path if the former fails with `EMSGSIZE`.
let fast_res = sock.send_to(&data, SD_JOURNAL_SOCK_PATH);
let res = match fast_res {
match sock.send_to(&data, SD_JOURNAL_SOCK_PATH) {
Ok(x) => Ok(x),
// `EMSGSIZE` (errno code 90) means the message was too long for a UNIX socket,
Err(ref err) if err.raw_os_error() == Some(90) => send_memfd_payload(sock, &data),
r => r.map_err(|err| err.to_string().into()),
};

res.map_err(|e| {
format!(
"failed to print to journal at '{}': {}",
SD_JOURNAL_SOCK_PATH, e
)
})?;
Ok(())
Err(ref err) if err.raw_os_error() == Some(90) => {
send_memfd_payload(sock, &data).context("sending with memfd failed")
}
Err(e) => Err(e).context("send_to failed"),
}
.map(|_| ())
.with_context(|| format!("failed to print to journal at '{}'", SD_JOURNAL_SOCK_PATH))
}

/// Print a message to the journal with the given priority.
pub fn journal_print(priority: Priority, msg: &str) -> Result<(), SdError> {
let map: HashMap<&str, &str> = HashMap::new();
Expand All @@ -191,30 +188,31 @@ pub fn journal_print(priority: Priority, msg: &str) -> Result<(), SdError> {
/// data.
fn send_memfd_payload(sock: &UnixDatagram, data: &[u8]) -> Result<usize, SdError> {
let memfd = {
let fdname = &CString::new("libsystemd-rs-logging").map_err(|e| e.to_string())?;
let tmpfd =
memfd_create(fdname, MemFdCreateFlag::MFD_ALLOW_SEALING).map_err(|e| e.to_string())?;
let fdname = &CString::new("libsystemd-rs-logging").context("unable to create cstring")?;
let tmpfd = memfd_create(fdname, MemFdCreateFlag::MFD_ALLOW_SEALING)
.context("unable to create memfd")?;

// SAFETY: `memfd_create` just returned this FD.
let mut file = unsafe { File::from_raw_fd(tmpfd) };
file.write_all(data).map_err(|e| e.to_string())?;
file.write_all(data).context("failed to write to memfd")?;
file
};

// Seal the memfd, so that journald knows it can safely mmap/read it.
fcntl(memfd.as_raw_fd(), FcntlArg::F_ADD_SEALS(SealFlag::all())).map_err(|e| e.to_string())?;
fcntl(memfd.as_raw_fd(), FcntlArg::F_ADD_SEALS(SealFlag::all()))
.context("unable to seal memfd")?;

let fds = &[memfd.as_raw_fd()];
let ancillary = [ControlMessage::ScmRights(fds)];
let path = UnixAddr::new(SD_JOURNAL_SOCK_PATH).map_err(|e| e.to_string())?;
let path = UnixAddr::new(SD_JOURNAL_SOCK_PATH).context("unable to create new unix address")?;
sendmsg(
sock.as_raw_fd(),
&[],
&ancillary,
MsgFlags::empty(),
Some(&path),
)
.map_err(|e| e.to_string())?;
.context("sendmsg failed")?;

// Close our side of the memfd after we send it to systemd.
drop(memfd);
Expand All @@ -236,36 +234,39 @@ impl JournalStream {
///
/// See also [`JournalStream::from_env()`].
pub(crate) fn parse<S: AsRef<OsStr>>(value: S) -> Result<Self, SdError> {
let s = value.as_ref().to_str().ok_or_else(|| {
let s = value.as_ref().to_str().with_context(|| {
format!(
"Failed to parse journal stream: Value {:?} not UTF-8 encoded",
value.as_ref()
)
})?;
let (device_s, inode_s) = s.find(':').map(|i| (&s[..i], &s[i + 1..])).ok_or_else(|| {
format!(
"Failed to parse journal stream: Missing separator ':' in value '{}'",
s
)
})?;
let device = u64::from_str(device_s).map_err(|err| {
let (device_s, inode_s) =
s.find(':')
.map(|i| (&s[..i], &s[i + 1..]))
.with_context(|| {
format!(
"Failed to parse journal stream: Missing separator ':' in value '{}'",
s
)
})?;
let device = u64::from_str(device_s).with_context(|| {
format!(
"Failed to parse journal stream: Device part is not a number '{}': {}",
device_s, err
"Failed to parse journal stream: Device part is not a number '{}'",
device_s
)
})?;
let inode = u64::from_str(inode_s).map_err(|err| {
let inode = u64::from_str(inode_s).with_context(|| {
format!(
"Failed to parse journal stream: Inode part is not a number '{}': {}",
inode_s, err
"Failed to parse journal stream: Inode part is not a number '{}'",
inode_s
)
})?;
Ok(JournalStream { device, inode })
}

/// Parse the device and inode number of the systemd journal stream denoted by the given environment variable.
pub(crate) fn from_env_impl<S: AsRef<OsStr>>(key: S) -> Result<Self, SdError> {
Self::parse(std::env::var_os(&key).ok_or_else(|| {
Self::parse(std::env::var_os(&key).with_context(|| {
format!(
"Failed to parse journal stream: Environment variable {:?} unset",
key.as_ref()
Expand Down

0 comments on commit 8c781c9

Please sign in to comment.