Compare commits

...

2 commits

Author SHA1 Message Date
finga 35f059f6a2 mqrs: Implement send and recv of sysvmq [wip]
TBD: CHANGELOG.md and adapt to recent changes is sysvmq
2023-10-28 20:13:49 +02:00
finga c6ebf08ecb sysvmq: Refactor and implement send and recv
Implement sending to and receiving from SysV IPC message queues. While
at it refactor the whole library.
2023-10-28 20:13:49 +02:00
7 changed files with 387 additions and 126 deletions

View file

@ -1,5 +1,7 @@
use anyhow::Result;
use clap::{ArgAction, Parser};
use clap::Parser;
use log::Level;
use std::env;
mod posix;
mod sysv;
@ -30,6 +32,8 @@ enum SysvCommand {
Info(sysv::Info),
List(sysv::List),
Unlink(sysv::Unlink),
Send(sysv::Send),
Recv(sysv::Recv),
}
#[derive(Debug, Parser)]
@ -39,9 +43,9 @@ enum SysvCommand {
subcommand_required = true
)]
struct Opts {
/// Produce verbose output, multiple -v options increase the verbosity (max. 3)
#[clap(short, long, global = true, action = ArgAction::Count)]
verbose: u32,
/// Set a log level
#[arg(short, long, value_name = "LEVEL", default_value_t = Level::Info)]
pub log_level: Level,
/// Backend to be used
#[clap(subcommand)]
backend: Backend,
@ -50,15 +54,11 @@ struct Opts {
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(
match opts.verbose {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
},
))
.init();
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", format!("{}", opts.log_level));
}
env_logger::init();
match opts.backend {
Backend::Posix(p) => match p {
@ -74,6 +74,8 @@ fn main() -> Result<()> {
SysvCommand::Info(i) => i.run()?,
SysvCommand::List(l) => l.run()?,
SysvCommand::Unlink(u) => u.run()?,
SysvCommand::Send(s) => s.run()?,
SysvCommand::Recv(r) => r.run()?,
},
}

View file

@ -1,9 +1,13 @@
mod create;
mod info;
mod list;
mod recv;
mod send;
mod unlink;
pub use create::Create;
pub use info::Info;
pub use list::List;
pub use recv::Recv;
pub use send::Send;
pub use unlink::Unlink;

42
src/sysv/recv.rs Normal file
View file

@ -0,0 +1,42 @@
use anyhow::Result;
// use chrono::DateTime;
use clap::Parser;
// use humantime::Duration;
use log::info;
use sysvmq::SysvMq;
/// Send a message to a message queue
#[derive(Debug, Parser)]
pub struct Recv {
// /// Set a different priority, priority >= 0
// #[clap(short, long, default_value = "0")]
// priority: u32,
// /// Do not block
// #[clap(short, long)]
// non_blocking: bool,
// /// Timeout, example "5h 23min 42ms"
// #[clap(short = 'o', long, conflicts_with = "deadline")]
// timeout: Option<String>,
// /// Deadline until messages are sent (format: "%Y-%m-%d %H:%M:%S")
// #[clap(short, long, conflicts_with = "timeout")]
// deadline: Option<String>,
// /// Name of the queue
// #[clap(value_name = "QUEUE")]
// queue: String,
/// Key of of the queue to write to
#[clap(value_name = "Key")]
key: i32,
// /// Message to be sent to the queue
// #[clap(value_name = "MESSAGE")]
// msg: String,
}
impl Recv {
pub fn run(&self) -> Result<()> {
let mq = SysvMq::<String>::new();
mq.open(self.key)?.recv();
Ok(())
}
}

42
src/sysv/send.rs Normal file
View file

@ -0,0 +1,42 @@
use anyhow::Result;
// use chrono::DateTime;
use clap::Parser;
// use humantime::Duration;
use log::info;
use sysvmq::SysvMq;
/// Send a message to a message queue
#[derive(Debug, Parser)]
pub struct Send {
// /// Set a different priority, priority >= 0
// #[clap(short, long, default_value = "0")]
// priority: u32,
// /// Do not block
// #[clap(short, long)]
// non_blocking: bool,
// /// Timeout, example "5h 23min 42ms"
// #[clap(short = 'o', long, conflicts_with = "deadline")]
// timeout: Option<String>,
// /// Deadline until messages are sent (format: "%Y-%m-%d %H:%M:%S")
// #[clap(short, long, conflicts_with = "timeout")]
// deadline: Option<String>,
// /// Name of the queue
// #[clap(value_name = "QUEUE")]
// queue: String,
/// Key of of the queue to write to
#[clap(value_name = "Key")]
key: i32,
/// Message to be sent to the queue
#[clap(value_name = "MESSAGE")]
msg: String,
}
impl Send {
pub fn run(&self) -> Result<()> {
let mq = SysvMq::<String>::new();
mq.open(self.key)?.send(self.msg.as_bytes());
Ok(())
}
}

View file

@ -8,8 +8,37 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- Basic tests to create, send, receive and delete a message queue as
well as changing the mode of a queue.
- The functions `SysvMq::stat()`, `SysvMq::info()`,
`SysvMq::msg_stat()` and `SysvMq::msg_info()` are added to receive
parameters of a queue.
- The function `new()` passes now a key which is used for the new
message queue.
- A basic test to test the happy path of a queue.
- The function `get()` to gather facts from a message queue.
- The function `set()` to set facts of a message queue.
- The function `create()` to create a message queue.
- The functions `send()` and `SysvMq::send()` to send messages to a
queue.
- The functions `recv()` and `SysvMq::recv()` to receive messages from
a queue.
- The function `SysvMq::delete()` to delete a queue.
### Changed
- The function `SysvMq::mode()` now also updates the message queue
accordingly.
- Remove `PhantomData` from the `SysvMq` struct.
- The function `SysvMq::create()` is replaced by `SysvMq::new()`.
- Remove generic type for `SysvMq`.
- Rename `unlink_id()` to `delete()`.
- Update to the latest verscion of `nix`.
- Fix several clippy findings in preperation to enable several lint
groups.
### Removed
- The function `id_from_key()` was removed.

View file

@ -1,24 +1,118 @@
use libc::{
msgctl, msgget, msqid_ds, IPC_CREAT, IPC_INFO, IPC_NOWAIT, IPC_RMID, MSG_INFO,g MSG_STAT,
c_void, msgctl, msgget, msgrcv, msgsnd, msqid_ds, IPC_CREAT, IPC_INFO, IPC_NOWAIT, IPC_RMID,
IPC_SET, IPC_STAT, MSG_INFO, MSG_STAT,
};
use nix::errno::{errno, Errno};
use std::{marker::PhantomData, mem::MaybeUninit, ptr};
use std::{convert::TryFrom, mem::MaybeUninit, ptr};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum SysvMqError {
#[error("SysV message queue: {0}")]
ErrnoError(&'static str),
#[error("Cannot convert integer")]
From(#[from] std::num::TryFromIntError),
}
#[allow(clippy::doc_markdown)]
/// Unlink (delete) an existing SysV IPC message queue.
/// Create a new SysV IPC message queue.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// Return `SysvMqError` when the queue cannot be created.
pub fn create(key: i32, mode: i32) -> Result<i32, SysvMqError> {
let mq = unsafe { msgget(key, IPC_CREAT | mode) };
match mq {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
id => Ok(id),
}
}
#[allow(clippy::doc_markdown)]
/// Open an existing SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the queue cannot be opened.
pub fn open(key: i32, mode: i32) -> Result<i32, SysvMqError> {
let mq = unsafe { msgget(key, mode) };
match mq {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
id => Ok(id),
}
}
#[allow(clippy::doc_markdown)]
/// Send a message to a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the message cannot be sent to the queue.
pub fn send(id: i32, msg: &[u8], mask: i32) -> Result<(), SysvMqError> {
match unsafe { msgsnd(id, msg.as_ptr().cast::<c_void>(), msg.len(), mask) } {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
_ => Ok(()),
}
}
#[allow(clippy::doc_markdown)]
/// Receive a message from a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the message cannot be received from the
/// queue.
pub fn recv(id: i32, msg: &mut [u8], mask: i32) -> Result<(), SysvMqError> {
match unsafe { msgrcv(id, msg.as_mut_ptr().cast::<c_void>(), msg.len(), 0, mask) } {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
_ => Ok(()),
}
}
#[allow(clippy::doc_markdown)]
/// Get parameters about a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the parameters cannot be gathered.
pub fn get(id: i32, cmd: i32) -> Result<msqid_ds, SysvMqError> {
let mut ipc_info = MaybeUninit::<msqid_ds>::uninit();
let ret;
let ipc_info = unsafe {
ret = msgctl(id, cmd, ipc_info.as_mut_ptr());
ipc_info.assume_init()
};
match ret {
0 => Ok(ipc_info),
_ => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
}
}
#[allow(clippy::doc_markdown)]
/// Set parameters about a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the parameters cannot be set.
pub fn set(id: i32, cmd: i32, data: &mut msqid_ds) -> Result<(), SysvMqError> {
match unsafe { msgctl(id, cmd, data) } {
0 => Ok(()),
_ => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
}
}
#[allow(clippy::doc_markdown)]
/// Delete a SysV IPC message queue by id.
///
/// # Errors
///
/// Return `SysvMqError` when no queue with the given key can be
/// found.
pub fn unlink_id(id: i32) -> Result<(), SysvMqError> {
pub fn delete(id: i32) -> Result<(), SysvMqError> {
let res = unsafe { msgctl(id, IPC_RMID, ptr::null::<msqid_ds>().cast_mut()) };
match res {
@ -27,127 +121,144 @@ pub fn unlink_id(id: i32) -> Result<(), SysvMqError> {
}
}
#[allow(clippy::doc_markdown)]
/// Get the id of an existing SysV IPC message queue by passing its
/// key.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// found.
pub fn id_from_key(key: i32) -> Result<i32, SysvMqError> {
let id = unsafe { msgget(key, 0) };
pub struct SysvMq {
pub id: i32,
pub key: i32,
pub mask: i32,
pub mode: i32,
}
match id {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
id => Ok(id),
impl Default for SysvMq {
fn default() -> Self {
Self {
id: -1,
key: 0,
mask: 0,
mode: 0o644,
}
}
}
#[allow(clippy::doc_markdown)]
/// Get the info about an existing SysV IPC message queue.
pub fn ipc_info(id: i32) {
let mut ipc_info = MaybeUninit::<msqid_ds>::uninit();
let ipc_info = unsafe {
msgctl(id, IPC_INFO, ipc_info.as_mut_ptr());
ipc_info.assume_init()
};
println!("info: {ipc_info:?}");
}
#[allow(clippy::doc_markdown)]
/// Get the stats about an existing SysV IPC message queue.
pub fn stat_info(id: i32) {
let mut stat_info = MaybeUninit::<msqid_ds>::uninit();
let stat_info = unsafe {
msgctl(id, MSG_STAT, stat_info.as_mut_ptr());
stat_info.assume_init()
};
println!("info: {stat_info:?}");
}
#[allow(clippy::doc_markdown)]
/// Get the message info about an existing SysV IPC message queue.
pub fn msg_info(id: i32) {
let mut msg_info = MaybeUninit::<msqid_ds>::uninit();
let msg_info = unsafe {
msgctl(id, MSG_INFO, msg_info.as_mut_ptr());
msg_info.assume_init()
};
println!("info: {msg_info:?}");
}
pub struct SysvMq<T> {
pub id: i32,
pub key: i32,
message_mask: i32,
mode: i32,
types: PhantomData<T>,
}
impl<T> SysvMq<T> {
/// Create a new message queye with the given key.
impl SysvMq {
#[allow(clippy::doc_markdown)]
/// Create a new message SysV IPC message queue with the given
/// key.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// created.
pub fn create(&mut self, key: i32) -> Result<&Self, SysvMqError> {
self.key = key;
self.id = unsafe { msgget(self.key, IPC_CREAT | self.mode) };
match self.id {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
_ => Ok(self),
}
/// Return `SysvMqError` when the queue cannot be created.
pub fn new(key: i32) -> Result<Self, SysvMqError> {
let mut mq = Self::default();
mq.key = key;
mq.id = create(mq.key, mq.mode)?;
Ok(mq)
}
/// Open an existing message queye with the given key.
#[allow(clippy::doc_markdown)]
/// Open an existing SysV IPC message queye with the given key.
///
/// # Errors
///
/// Return `SysvMqError` when the queue cannot be opened.
pub fn open(mut self, key: i32) -> Result<Self, SysvMqError> {
self.key = key;
self.id = open(self.key, self.mode)?;
Ok(self)
}
#[allow(clippy::doc_markdown)]
/// Set the mode of a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the mode of the queue cannot be set.
pub fn mode(&mut self, mode: i32) -> Result<(), SysvMqError> {
self.mode = mode;
let mut stats = get(self.id, IPC_STAT)?;
stats.msg_perm.mode = u16::try_from(self.mode)?;
set(self.id, IPC_SET, &mut stats)
}
#[allow(clippy::doc_markdown)]
/// Set the mask of a SysV IPC message queue to nonblocking
/// (`IPC_NOWAIT`).
pub fn nonblocking(&mut self) {
self.mask |= IPC_NOWAIT;
}
#[allow(clippy::doc_markdown)]
/// Send a message to a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the message cannot be sent to the queue.
pub fn send(&mut self, msg: &[u8]) -> Result<(), SysvMqError> {
send(self.id, msg, self.mask)
}
#[allow(clippy::doc_markdown)]
/// Receive a message from a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the message cannot be received from the
/// queue.
pub fn recv(&mut self, msg: &mut [u8]) -> Result<(), SysvMqError> {
recv(self.id, msg, self.mask)
}
#[allow(clippy::doc_markdown)]
/// Receive stats from a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the stats cannot be gathered from
/// the queue.
pub fn stat(&self) -> Result<msqid_ds, SysvMqError> {
get(self.id, IPC_STAT)
}
#[allow(clippy::doc_markdown)]
/// Receive info from a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the info cannot be gathered from the
/// queue.
pub fn info(&self) -> Result<msqid_ds, SysvMqError> {
get(self.id, IPC_INFO)
}
#[allow(clippy::doc_markdown)]
/// Receive message stats from a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the message stats cannot be gathered
/// from the queue.
pub fn msg_stat(&self) -> Result<msqid_ds, SysvMqError> {
get(self.id, MSG_STAT)
}
#[allow(clippy::doc_markdown)]
/// Receive message info from a SysV IPC message queue.
///
/// # Errors
///
/// Return `SysvMqError` when the message info cannot be gathered
/// from the queue.
pub fn msg_info(&self) -> Result<msqid_ds, SysvMqError> {
get(self.id, MSG_INFO)
}
#[allow(clippy::doc_markdown)]
/// Delete an existing SysV IPC message queue.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// found.
pub fn open(mut self, key: i32) -> Result<Self, SysvMqError> {
self.key = key;
self.id = unsafe { msgget(self.key, self.mode) };
match self.id {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
_ => Ok(self),
}
}
pub fn mode(&mut self, mode: i32) -> &Self {
self.mode = mode;
self
}
pub fn nonblocking(&mut self) -> &Self {
self.message_mask |= IPC_NOWAIT;
self
}
#[must_use]
pub const fn new() -> Self {
Self {
id: -1,
key: 0,
message_mask: 0,
mode: 0o644,
types: PhantomData,
}
}
}
impl<T> Default for SysvMq<T> {
fn default() -> Self {
Self::new()
pub fn delete(&mut self) -> Result<(), SysvMqError> {
delete(self.id)
}
}

31
sysvmq/tests/main.rs Normal file
View file

@ -0,0 +1,31 @@
use sysvmq::SysvMq;
#[test]
fn new_send_recv_delete() {
let mut mq = SysvMq::new(0).expect("could not create SysV message queue with key 0");
let msg = b"this is a test";
let mut buf = [0u8; 14];
mq.send(msg)
.expect("could not send message to SysV message queue");
mq.recv(&mut buf)
.expect("could not receive message from SysV message queue");
mq.delete().expect("could not destroy SysV message queue");
assert_eq!(msg, &buf);
}
#[test]
fn change_mode() {
let mut mq = SysvMq::new(0).expect("could not create SysV message queue with key 0");
let init_stats = mq
.stat()
.expect("could not get stats from SysV message queue");
mq.mode(0o666)
.expect("could not set mode of SysV message queue");
let new_stats = mq
.stat()
.expect("could not get stats from SysV message queue");
mq.delete().expect("could not destroy SysV message queue");
assert_eq!(0o644, init_stats.msg_perm.mode);
assert_eq!(0o666, new_stats.msg_perm.mode);
}