2017-10-15 23:22:18 +02:00
|
|
|
use nix;
|
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
2017-10-16 01:08:08 +02:00
|
|
|
use std::io;
|
|
|
|
use std::num;
|
2017-10-15 23:22:18 +02:00
|
|
|
|
|
|
|
/// This module implements a simple error type to match the errors that can be thrown from the C
|
|
|
|
/// functions as well as some extra errors resulting from internal validations.
|
|
|
|
///
|
|
|
|
/// As this crate exposes an opinionated API to the POSIX queues certain errors have been
|
|
|
|
/// ignored:
|
|
|
|
///
|
|
|
|
/// * ETIMEDOUT: The low-level timed functions are not exported and this error can not occur.
|
|
|
|
/// * EAGAIN: Non-blocking queue calls are not supported.
|
|
|
|
/// * EINVAL: Same reason as ETIMEDOUT
|
|
|
|
/// * EMSGSIZE: The message size is immutable after queue creation and this crate checks it.
|
|
|
|
/// * ENAMETOOLONG: This crate performs name validation
|
|
|
|
///
|
|
|
|
/// If an unexpected error is encountered it will be wrapped appropriately and should be reported
|
2022-02-04 13:15:53 +01:00
|
|
|
/// as a bug on https://b.tvl.fyi
|
2017-10-15 23:22:18 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2017-10-16 01:08:08 +02:00
|
|
|
// These errors are raised inside of the library
|
|
|
|
InvalidQueueName(&'static str),
|
|
|
|
ValueReadingError(io::Error),
|
|
|
|
MessageSizeExceeded(),
|
|
|
|
MaximumMessageSizeExceeded(),
|
|
|
|
MaximumMessageCountExceeded(),
|
|
|
|
|
2017-10-15 23:22:18 +02:00
|
|
|
// These errors match what is described in the man pages (from mq_overview(7) onwards).
|
|
|
|
PermissionDenied(),
|
|
|
|
InvalidQueueDescriptor(),
|
|
|
|
QueueCallInterrupted(),
|
|
|
|
QueueAlreadyExists(),
|
|
|
|
QueueNotFound(),
|
|
|
|
InsufficientMemory(),
|
|
|
|
InsufficientSpace(),
|
|
|
|
|
|
|
|
// These two are (hopefully) unlikely in modern systems
|
|
|
|
ProcessFileDescriptorLimitReached(),
|
|
|
|
SystemFileDescriptorLimitReached(),
|
|
|
|
|
|
|
|
// If an unhandled / unknown / unexpected error occurs this error will be used.
|
|
|
|
// In those cases bug reports would be welcome!
|
2020-01-20 12:48:35 +01:00
|
|
|
UnknownForeignError(nix::errno::Errno),
|
2017-10-15 23:22:18 +02:00
|
|
|
|
|
|
|
// Some other unexpected / unknown error occured. This is probably an error from
|
|
|
|
// the nix crate. Bug reports also welcome for this!
|
|
|
|
UnknownInternalError(Option<nix::Error>),
|
|
|
|
}
|
|
|
|
|
2022-02-04 13:15:53 +01:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-10-15 23:22:18 +02:00
|
|
|
use Error::*;
|
2022-02-04 13:15:53 +01:00
|
|
|
f.write_str(match *self {
|
2017-10-16 01:08:08 +02:00
|
|
|
// This error contains more sensible description strings already
|
|
|
|
InvalidQueueName(e) => e,
|
|
|
|
ValueReadingError(_) => "error reading system configuration for message queues",
|
|
|
|
MessageSizeExceeded() => "message is larger than maximum size for specified queue",
|
|
|
|
MaximumMessageSizeExceeded() => "specified queue message size exceeds system maximum",
|
|
|
|
MaximumMessageCountExceeded() => "specified queue message count exceeds system maximum",
|
2017-10-15 23:22:18 +02:00
|
|
|
PermissionDenied() => "permission to the specified queue was denied",
|
|
|
|
InvalidQueueDescriptor() => "the internal queue descriptor was invalid",
|
|
|
|
QueueCallInterrupted() => "queue method interrupted by signal",
|
|
|
|
QueueAlreadyExists() => "the specified queue already exists",
|
|
|
|
QueueNotFound() => "the specified queue could not be found",
|
|
|
|
InsufficientMemory() => "insufficient memory to call queue method",
|
|
|
|
InsufficientSpace() => "insufficient space to call queue method",
|
2022-02-04 13:15:53 +01:00
|
|
|
ProcessFileDescriptorLimitReached() => {
|
|
|
|
"maximum number of process file descriptors reached"
|
|
|
|
}
|
|
|
|
SystemFileDescriptorLimitReached() => {
|
|
|
|
"maximum number of system file descriptors reached"
|
|
|
|
}
|
2017-10-15 23:22:18 +02:00
|
|
|
UnknownForeignError(_) => "unknown foreign error occured: please report a bug!",
|
|
|
|
UnknownInternalError(_) => "unknown internal error occured: please report a bug!",
|
2022-02-04 13:15:53 +01:00
|
|
|
})
|
2017-10-15 23:22:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 13:15:53 +01:00
|
|
|
impl error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
Error::ValueReadingError(e) => Some(e),
|
|
|
|
Error::UnknownForeignError(e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
}
|
2017-10-15 23:22:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This from implementation is used to translate errors from the lower-level
|
|
|
|
/// C-calls into sensible Rust errors.
|
|
|
|
impl From<nix::Error> for Error {
|
|
|
|
fn from(e: nix::Error) -> Self {
|
|
|
|
match e {
|
|
|
|
nix::Error::Sys(e) => match_errno(e),
|
|
|
|
_ => Error::UnknownInternalError(Some(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 01:08:08 +02:00
|
|
|
// This implementation is used when reading system queue settings.
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(e: io::Error) -> Self {
|
|
|
|
Error::ValueReadingError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This implementation is used when parsing system queue settings. The unknown error is returned
|
|
|
|
// here because the system is probably seriously broken if those files don't contain numbers.
|
|
|
|
impl From<num::ParseIntError> for Error {
|
|
|
|
fn from(_: num::ParseIntError) -> Self {
|
|
|
|
Error::UnknownInternalError(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:48:35 +01:00
|
|
|
fn match_errno(err: nix::errno::Errno) -> Error {
|
|
|
|
use nix::errno::Errno::*;
|
2017-10-15 23:22:18 +02:00
|
|
|
|
|
|
|
match err {
|
|
|
|
EACCES => Error::PermissionDenied(),
|
2022-02-04 13:15:53 +01:00
|
|
|
EBADF => Error::InvalidQueueDescriptor(),
|
|
|
|
EINTR => Error::QueueCallInterrupted(),
|
2017-10-15 23:22:18 +02:00
|
|
|
EEXIST => Error::QueueAlreadyExists(),
|
|
|
|
EMFILE => Error::ProcessFileDescriptorLimitReached(),
|
|
|
|
ENFILE => Error::SystemFileDescriptorLimitReached(),
|
|
|
|
ENOENT => Error::QueueNotFound(),
|
|
|
|
ENOMEM => Error::InsufficientMemory(),
|
|
|
|
ENOSPC => Error::InsufficientSpace(),
|
2022-02-04 13:15:53 +01:00
|
|
|
_ => Error::UnknownForeignError(err),
|
2017-10-15 23:22:18 +02:00
|
|
|
}
|
|
|
|
}
|