diff options
Diffstat (limited to 'ops/posix_mq.rs')
-rw-r--r-- | ops/posix_mq.rs/Cargo.lock | 57 | ||||
-rw-r--r-- | ops/posix_mq.rs/Cargo.toml | 9 | ||||
-rw-r--r-- | ops/posix_mq.rs/README.md | 13 | ||||
-rw-r--r-- | ops/posix_mq.rs/src/error.rs | 80 | ||||
-rw-r--r-- | ops/posix_mq.rs/src/lib.rs | 62 | ||||
-rw-r--r-- | ops/posix_mq.rs/src/tests.rs | 3 |
6 files changed, 107 insertions, 117 deletions
diff --git a/ops/posix_mq.rs/Cargo.lock b/ops/posix_mq.rs/Cargo.lock index fdd0086c4dd3..dc344613d052 100644 --- a/ops/posix_mq.rs/Cargo.lock +++ b/ops/posix_mq.rs/Cargo.lock @@ -1,54 +1,63 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "cc" version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" [[package]] name = "cfg-if" -version = "0.1.10" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "libc" -version = "0.2.66" +version = "0.2.117" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" [[package]] -name = "nix" -version = "0.16.1" +name = "memoffset" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" dependencies = [ - "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "autocfg", ] [[package]] -name = "posix_mq" -version = "0.9.0" +name = "nix" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f866317acbd3a240710c63f065ffb1e4fd466259045ccb504130b7f668f35c6" dependencies = [ - "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "nix 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags", + "cc", + "cfg-if", + "libc", + "memoffset", ] [[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[metadata] -"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" -"checksum cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" -"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" -"checksum nix 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" -"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +name = "posix_mq" +version = "3771.0.0" +dependencies = [ + "libc", + "nix", +] diff --git a/ops/posix_mq.rs/Cargo.toml b/ops/posix_mq.rs/Cargo.toml index d72e87a3dcef..8390b80b86f0 100644 --- a/ops/posix_mq.rs/Cargo.toml +++ b/ops/posix_mq.rs/Cargo.toml @@ -1,11 +1,12 @@ [package] name = "posix_mq" -version = "0.9.0" -authors = ["Vincent Ambo <mail@tazj.in>"] +version = "3771.0.0" +authors = ["Vincent Ambo <tazjin@tvl.su>"] description = "(Higher-level) Rust bindings to POSIX message queues" license = "MIT" -repository = "https://git.tazj.in/tree/ops/posix_mq.rs" +homepage = "https://cs.tvl.fyi/depot/-/tree/ops/posix_mq.rs" +repository = "https://code.tvl.fyi/depot.git:/ops/posix_mq.rs.git" [dependencies] -nix = "0.16" +nix = "0.23" libc = "0.2" diff --git a/ops/posix_mq.rs/README.md b/ops/posix_mq.rs/README.md index 9370c6c08740..800d2221e492 100644 --- a/ops/posix_mq.rs/README.md +++ b/ops/posix_mq.rs/README.md @@ -1,7 +1,6 @@ posix_mq ======== -[![Build Status](https://travis-ci.org/aprilabank/posix_mq.rs.svg?branch=master)](https://travis-ci.org/aprilabank/posix_mq.rs) [![crates.io](https://img.shields.io/crates/v/posix_mq.svg)](https://crates.io/crates/posix_mq) This is a simple, relatively high-level library for the POSIX [message queue API][]. It wraps the lower-level API in a @@ -29,5 +28,17 @@ queue.send(&message).expect("message sending failed"); let result = queue.receive().expect("message receiving failed"); ``` +## Development + +Development happens in the [TVL +monorepo](https://cs.tvl.fyi/depot/-/tree/ops/posix_mq.rs). + +Starting from version `3771.0.0`, the version numbers correspond to +_revisions_ of the TVL repository, available as git refs (e.g. +`refs/r/3771`). + +See the TVL documentation for more information about how to contribute +to the codebase. + [message queue API]: https://linux.die.net/man/7/mq_overview [sister library]: https://github.com/aprilabank/posix_mq.kt diff --git a/ops/posix_mq.rs/src/error.rs b/ops/posix_mq.rs/src/error.rs index 1ef585c01efb..bacd2aeb39e0 100644 --- a/ops/posix_mq.rs/src/error.rs +++ b/ops/posix_mq.rs/src/error.rs @@ -1,8 +1,5 @@ use nix; -use std::error; -use std::fmt; -use std::io; -use std::num; +use std::{error, fmt, io, num}; /// 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. @@ -17,7 +14,7 @@ use std::num; /// * ENAMETOOLONG: This crate performs name validation /// /// If an unexpected error is encountered it will be wrapped appropriately and should be reported -/// as a bug on https://github.com/aprilabank/posix_mq.rs +/// as a bug on https://b.tvl.fyi #[derive(Debug)] pub enum Error { @@ -47,13 +44,13 @@ pub enum Error { // 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>), + UnknownInternalError(), } -impl error::Error for Error { - fn description(&self) -> &str { +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use Error::*; - match *self { + f.write_str(match *self { // This error contains more sensible description strings already InvalidQueueName(e) => e, ValueReadingError(_) => "error reading system configuration for message queues", @@ -67,31 +64,44 @@ impl error::Error for Error { QueueNotFound() => "the specified queue could not be found", InsufficientMemory() => "insufficient memory to call queue method", InsufficientSpace() => "insufficient space to call queue method", - ProcessFileDescriptorLimitReached() => - "maximum number of process file descriptors reached", - SystemFileDescriptorLimitReached() => - "maximum number of system file descriptors reached", + ProcessFileDescriptorLimitReached() => { + "maximum number of process file descriptors reached" + } + SystemFileDescriptorLimitReached() => { + "maximum number of system file descriptors reached" + } UnknownForeignError(_) => "unknown foreign error occured: please report a bug!", - UnknownInternalError(_) => "unknown internal error occured: please report a bug!", - } + UnknownInternalError() => "unknown internal error occured: please report a bug!", + }) } } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - // Explicitly import this to gain access to Error::description() - use std::error::Error; - f.write_str(self.description()) +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, + } } } /// 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)), +impl From<nix::errno::Errno> for Error { + fn from(err: nix::Error) -> Self { + use nix::errno::Errno::*; + match err { + EACCES => Error::PermissionDenied(), + EBADF => Error::InvalidQueueDescriptor(), + EINTR => Error::QueueCallInterrupted(), + EEXIST => Error::QueueAlreadyExists(), + EMFILE => Error::ProcessFileDescriptorLimitReached(), + ENFILE => Error::SystemFileDescriptorLimitReached(), + ENOENT => Error::QueueNotFound(), + ENOMEM => Error::InsufficientMemory(), + ENOSPC => Error::InsufficientSpace(), + _ => Error::UnknownForeignError(err), } } } @@ -107,24 +117,6 @@ impl From<io::Error> for Error { // 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) - } -} - - -fn match_errno(err: nix::errno::Errno) -> Error { - use nix::errno::Errno::*; - - match err { - EACCES => Error::PermissionDenied(), - EBADF => Error::InvalidQueueDescriptor(), - EINTR => Error::QueueCallInterrupted(), - EEXIST => Error::QueueAlreadyExists(), - EMFILE => Error::ProcessFileDescriptorLimitReached(), - ENFILE => Error::SystemFileDescriptorLimitReached(), - ENOENT => Error::QueueNotFound(), - ENOMEM => Error::InsufficientMemory(), - ENOSPC => Error::InsufficientSpace(), - _ => Error::UnknownForeignError(err), + Error::UnknownInternalError() } } diff --git a/ops/posix_mq.rs/src/lib.rs b/ops/posix_mq.rs/src/lib.rs index 057601eccfbd..ed35fb03be82 100644 --- a/ops/posix_mq.rs/src/lib.rs +++ b/ops/posix_mq.rs/src/lib.rs @@ -1,5 +1,5 @@ -extern crate nix; extern crate libc; +extern crate nix; use error::Error; use libc::mqd_t; @@ -8,8 +8,8 @@ use nix::sys::stat; use std::ffi::CString; use std::fs::File; use std::io::Read; -use std::string::ToString; use std::ops::Drop; +use std::string::ToString; pub mod error; @@ -33,16 +33,20 @@ impl Name { // have tried just using '/' as a queue name. if string.len() == 1 { return Err(Error::InvalidQueueName( - "Queue name must be a slash followed by one or more characters" + "Queue name must be a slash followed by one or more characters", )); } if string.len() > 255 { - return Err(Error::InvalidQueueName("Queue name must not exceed 255 characters")); + return Err(Error::InvalidQueueName( + "Queue name must not exceed 255 characters", + )); } if string.matches('/').count() > 1 { - return Err(Error::InvalidQueueName("Queue name can not contain more than one slash")); + return Err(Error::InvalidQueueName( + "Queue name can not contain more than one slash", + )); } // TODO: What error is being thrown away here? Is it possible? @@ -97,16 +101,9 @@ impl Queue { flags }; - let attr = mqueue::MqAttr::new( - 0, max_pending, max_size, 0 - ); + let attr = mqueue::MqAttr::new(0, max_pending, max_size, 0); - let queue_descriptor = mqueue::mq_open( - &name.0, - oflags, - default_mode(), - Some(&attr), - )?; + let queue_descriptor = mqueue::mq_open(&name.0, oflags, default_mode(), Some(&attr))?; Ok(Queue { name, @@ -121,12 +118,7 @@ impl Queue { // No extra flags need to be constructed as the default is to open and fail if the // queue does not exist yet - which is what we want here. let oflags = mqueue::MQ_OFlag::O_RDWR; - let queue_descriptor = mqueue::mq_open( - &name.0, - oflags, - default_mode(), - None, - )?; + let queue_descriptor = mqueue::mq_open(&name.0, oflags, default_mode(), None)?; let attr = mq_getattr(queue_descriptor)?; @@ -151,16 +143,9 @@ impl Queue { let default_pending = read_i64_from_file(MSG_DEFAULT)?; let default_size = read_i64_from_file(MSGSIZE_DEFAULT)?; - let attr = mqueue::MqAttr::new( - 0, default_pending, default_size, 0 - ); + let attr = mqueue::MqAttr::new(0, default_pending, default_size, 0); - let queue_descriptor = mqueue::mq_open( - &name.0, - oflags, - default_mode(), - Some(&attr), - )?; + let queue_descriptor = mqueue::mq_open(&name.0, oflags, default_mode(), Some(&attr))?; let actual_attr = mq_getattr(queue_descriptor)?; @@ -187,11 +172,8 @@ impl Queue { return Err(Error::MessageSizeExceeded()); } - mqueue::mq_send( - self.queue_descriptor, - msg.data.as_ref(), - msg.priority, - ).map_err(|e| e.into()) + mqueue::mq_send(self.queue_descriptor, msg.data.as_ref(), msg.priority) + .map_err(|e| e.into()) } /// Receive a message from the message queue. @@ -200,11 +182,7 @@ impl Queue { let mut data: Vec<u8> = vec![0; self.max_size as usize]; let mut priority: u32 = 0; - let msg_size = mqueue::mq_receive( - self.queue_descriptor, - data.as_mut(), - &mut priority, - )?; + let msg_size = mqueue::mq_receive(self.queue_descriptor, data.as_mut(), &mut priority)?; data.truncate(msg_size); Ok(Message { data, priority }) @@ -261,9 +239,9 @@ fn read_i64_from_file(name: &str) -> Result<i64, Error> { /// To work around it, this method calls the C-function directly. fn mq_getattr(mqd: mqd_t) -> Result<libc::mq_attr, Error> { use std::mem; - let mut attr = unsafe { mem::uninitialized::<libc::mq_attr>() }; - let res = unsafe { libc::mq_getattr(mqd, &mut attr) }; + let mut attr = mem::MaybeUninit::<libc::mq_attr>::uninit(); + let res = unsafe { libc::mq_getattr(mqd, attr.as_mut_ptr()) }; nix::errno::Errno::result(res) - .map(|_| attr) + .map(|_| unsafe { attr.assume_init() }) .map_err(|e| e.into()) } diff --git a/ops/posix_mq.rs/src/tests.rs b/ops/posix_mq.rs/src/tests.rs index 7a08876aeacd..1f4ea9a58da6 100644 --- a/ops/posix_mq.rs/src/tests.rs +++ b/ops/posix_mq.rs/src/tests.rs @@ -4,8 +4,7 @@ use super::*; fn test_open_delete() { // Simple test with default queue settings let name = Name::new("/test-queue").unwrap(); - let queue = Queue::open_or_create(name) - .expect("Opening queue failed"); + let queue = Queue::open_or_create(name).expect("Opening queue failed"); let message = Message { data: "test-message".as_bytes().to_vec(), |