diff --git a/Cargo.lock b/Cargo.lock index 3c4a517..afa11c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,12 +40,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" -[[package]] -name = "cc" -version = "1.0.68" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" - [[package]] name = "cfg-if" version = "1.0.0" @@ -158,9 +152,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.98" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" +checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" [[package]] name = "log" @@ -177,15 +171,6 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" -[[package]] -name = "memoffset" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" -dependencies = [ - "autocfg", -] - [[package]] name = "mqrs" version = "0.1.1" @@ -197,20 +182,6 @@ dependencies = [ "humantime", "log", "posixmq", - "sysvmq", -] - -[[package]] -name = "nix" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c3728fec49d363a50a8828a190b379a446cc5cf085c06259bbbeb34447e4ec7" -dependencies = [ - "bitflags", - "cc", - "cfg-if", - "libc", - "memoffset", ] [[package]] @@ -323,15 +294,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "sysvmq" -version = "0.1.0" -dependencies = [ - "libc", - "nix", - "thiserror", -] - [[package]] name = "termcolor" version = "1.1.2" @@ -350,26 +312,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "thiserror" -version = "1.0.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "time" version = "0.1.44" @@ -383,9 +325,9 @@ dependencies = [ [[package]] name = "unicode-segmentation" -version = "1.8.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" [[package]] name = "unicode-width" diff --git a/Cargo.toml b/Cargo.toml index edbaea8..a42c1c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,12 +3,9 @@ name = "mqrs" version = "0.1.1" authors = ["finga "] edition = "2018" -repository = "https://git.onders.org/finga/mqrs" license = "GPL-3.0-or-later" readme = "README.md" description = "A CLI program for interacting with Posix Message Queues." -keywords = ["message_queue", "mq", "mqueue", "queue"] -categories = ["command-line-utilities"] [dependencies] anyhow = "1.0" @@ -18,10 +15,6 @@ chrono = "0.4" humantime = "2.1" log = "0.4" env_logger = "0.8" -sysvmq = { path = "sysvmq" } - -[workspace] -members = ["sysvmq"] [package.metadata.deb] extended-description = "`mqrs` is a small cli application to handle POSIX message queues." diff --git a/INSTALL.md b/INSTALL.md deleted file mode 100644 index 7b638ed..0000000 --- a/INSTALL.md +++ /dev/null @@ -1,49 +0,0 @@ -# Installation -To build `mqrs` having the rust toolchain installed is mandatory. - -## Install Rust -Install the Rust toolchain from [rustup.rs](https://rustup.rs). - -## Build `mqrs` -`mqrs` can be built for development: -```sh -cargo b -``` - -or for releasing: -```sh -cargo b --release -``` - -## Build the `mqrs` Debian package -For that [`cargo-deb`](https://github.com/mmstick/cargo-deb) is -required, which can be installed with: -```sh -cargo install cargo-deb -``` - -A Debian package can be built with: -```sh -cargo deb -``` - -## Install `mqrs` -When a Rust toolchain installed you can also install `mqrs` -directly without cloning it manually: -``` sh -cargo install --git https://git.onders.org/finga/mqrs.git mqrs -``` - -or from within the project: -```sh -cargo install mqrs -``` - -## Run `mqrs` -`mqrs` can either be run from the project directory with: -```sh -cargo b -``` - -or you can copy the produced binary somewhere else or link to them -from `target/{debug,release}/mqrs` depending on which one you built. diff --git a/README.md b/README.md index e7251f7..d274168 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,49 @@ # mqrs `mqrs` is a small cli application to handle POSIX message queues. -## Install `mqrs` -For information about how to build, install and run `mqrs` please see -[`INSTALL.md`](INSTALL.md). +## Installation +To build `mqrs` a rust toolchain is necessary. + +### Install Rust +Install the Rust toolchain from [rustup.rs](https://rustup.rs). + +### Build `mqrs` +`mqrs` can be built for development: +```sh +cargo b +``` + +or for releasing: +```sh +cargo b --release +``` + +### Build the `mqrs` Debian package +```sh +cargo deb +``` + +### Install `mqrs` +When a Rust toolchain installed you can also install `mqrs` +directly without cloning it manually: +``` sh +cargo install --git https://git.onders.org/finga/mqrs.git mqrs +``` + +or from within the project: +```sh +cargo install mqrs +``` + +### Run `mqrs` +`mqrs` can either be run from the project directory with: +```sh +cargo b +``` + +or you can copy the produced binary somewhere else or link to them +from `target/{debug,release}/mqrs` depending on which +one you built. ## Using `mqrs` Depending on which backend you want to use there are different subsets @@ -22,7 +62,7 @@ The POSIX backend supports six commands: `create`, `info`, `list`, Use the `create` command to create a new POSIX message queue. Following optional arguments are supported: - `-c`, `--capacity`: Maximum number of messages in the queue -- `-m`, `--mode`: Permissions (octal) to create the queue with +- `-p`, `--permissions`: Permissions (octal) to create the queue with - `-s`, `--msgsize`: Message size in bytes #### Print information about a message queue @@ -58,16 +98,4 @@ queue. Following optional arguments are supported: - `-o,` `--timeout `: As for example in "5h 23min 42ms" ### SysV IPC message queues -The SysV IPC backend supports two commands: `create` and `unlink`. - -#### Create a message queue -Use the `create` command to create a new SysV IPC message -queue. Following optional arguments are supported: -- `-m`, `--mode`: Permissions (octal) to create the queue - with. Default: 0644. - -#### Delete a message queue -Use the `unlink` command to delete a message queue. This can either be -done by providing a `key` or an `id` of the queue: -- `-i`, `--id `: Id of the queue -- `-k`, `--key `: Key of the queue +The SysV IPC backend supports no commands yet. diff --git a/mqrs.1 b/mqrs.1 index eb7b912..9934081 100644 --- a/mqrs.1 +++ b/mqrs.1 @@ -62,14 +62,13 @@ Produce verbose output .TP 8 .SS OPTIONS .RS -.TP 8 .B \-c, \-\-capacity \fI\fP Maximum number of messages in the queue .TP 8 .B \-s, \-\-msgsize \fI\fP Message size in bytes .TP 8 -.B \-m, \-\-mode \fI\fP +.B \-p, \-\-permissions \fI\fP Permissions (octal) to create the queue with .RE .SS help [SUBCOMMAND] @@ -192,7 +191,7 @@ Set a different priority than default, priority >= 0 [default: 0] Timeout as for example in "5h 23min 42ms" .RE .SS unlink [FLAGS] \fI\fP -Delete an existing POSIX message queue. +Deletes an existing POSIX message queue. .TP 8 .SS ARGS .RS @@ -211,65 +210,7 @@ Prints help information Produce verbose output .RE .SH SYSV IPC MESSAGE QUEUE SUBCOMMANDS -The SysV IPC backend supports two commands: -.B create\ -and -.B unlink\ -. -.SS create [FLAGS] [OPTIONS] \fI\fP -Create a new SysV IPC message queue. -.TP 8 -.SS ARGS -.RS -.TP 8 -.B \fI\fP -Key of the new queue -.RE -.TP 8 -.SS FLAGS -.RS -.TP 8 -.B \-h, \-\-help -Prints help information -.TP 8 -.B \-v, \-\-verbose -Produce verbose output -.TP 8 -.SS OPTIONS -.RS -.TP 8 -.B \-m, \-\-mode \fI\fP -Permissions (octal) to create the queue with (default: 0644) -.RE -.SS unlink [FLAGS] [OPTIONS] -Delete an existing SysV IPC message queue. It is mandatory to pass -exactly one OPTION. -.TP 8 -.SS ARGS -.RS -.TP 8 -.B \fI\fP -Key of the new queue -.RE -.TP 8 -.SS FLAGS -.RS -.TP 8 -.B \-h, \-\-help -Prints help information -.TP 8 -.B \-v, \-\-verbose -Produce verbose output -.TP 8 -.SS OPTIONS -.RS -.TP 8 -.B \-i, \-\-id \fI\fP -Id of the queue -.TP 8 -.B \-k, \-\-key \fI\fP -Key of the queue -.RE +The SysV IPC backend supports no commands yet. .SH SEE ALSO mq_overview(7) .SH BUGS diff --git a/src/main.rs b/src/main.rs index 5e4db83..cc1d185 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ use anyhow::Result; use clap::{crate_authors, crate_version, AppSettings, Clap}; mod posix; -mod sysv; #[derive(Clap, Debug)] enum Backend { @@ -24,8 +23,6 @@ enum PosixCommand { #[derive(Clap, Debug)] enum SysvCommand { - Create(sysv::Create), - Unlink(sysv::Unlink), } #[derive(Clap, Debug)] @@ -68,8 +65,6 @@ fn main() -> Result<()> { PosixCommand::Recv(r) => r.run()?, }, Backend::Sysv(s) => match s { - SysvCommand::Create(c) => c.run()?, - SysvCommand::Unlink(u) => u.run()?, }, } diff --git a/src/posix/create.rs b/src/posix/create.rs index be4606b..d475d36 100644 --- a/src/posix/create.rs +++ b/src/posix/create.rs @@ -9,7 +9,7 @@ use std::fs; pub struct Create { /// Permissions (octal) to create the queue with #[clap(short, long)] - mode: Option, + permissions: Option, /// Maximum number of messages in the queue #[clap(short, long)] capacity: Option, @@ -39,7 +39,7 @@ impl Create { pub fn run(&self) -> Result<()> { let mq = &mut posixmq::OpenOptions::readonly(); - if let Some(m) = &self.mode { + if let Some(m) = &self.permissions { mq.mode(u32::from_str_radix(&m, 8)?); } diff --git a/src/sysv.rs b/src/sysv.rs deleted file mode 100644 index 91ee1a5..0000000 --- a/src/sysv.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod create; -mod unlink; - -pub use create::Create; -pub use unlink::Unlink; diff --git a/src/sysv/create.rs b/src/sysv/create.rs deleted file mode 100644 index e4b03a6..0000000 --- a/src/sysv/create.rs +++ /dev/null @@ -1,31 +0,0 @@ -use anyhow::Result; -use clap::Clap; -use log::info; -use sysvmq::SysvMq; - -/// Create a SysV message queue -#[derive(Clap, Debug)] -pub struct Create { - /// Permissions (octal) to create the queue with (default: 0644) - #[clap(short, long)] - mode: Option, - /// Key of the new queue - #[clap(value_name = "KEY")] - key: i32, -} - -impl Create { - pub fn run(&self) -> Result<()> { - let mut mq = SysvMq::::new(); - - if let Some(m) = &self.mode { - mq.mode(i32::from_str_radix(&m, 8)?); - } - - mq.create(self.key)?; - - info!("SysV message queue created, key: {}, id: {}", mq.key, mq.id); - - Ok(()) - } -} diff --git a/src/sysv/unlink.rs b/src/sysv/unlink.rs deleted file mode 100644 index 277edb6..0000000 --- a/src/sysv/unlink.rs +++ /dev/null @@ -1,35 +0,0 @@ -use anyhow::Result; -use clap::Clap; -use log::info; - -/// Delete a message queue -#[derive(Clap, Debug)] -pub struct Unlink { - /// Id of the queue - #[clap( - long, - short, - required_unless_present_any = &["key"], - conflicts_with = "key" - )] - pub id: Option, - /// Key of the queue - #[clap(long, short, required_unless_present_any = &["id"], conflicts_with = "id")] - pub key: Option, -} - -impl Unlink { - pub fn run(&self) -> Result<()> { - if let Some(id) = self.id { - sysvmq::unlink_id(id)?; - - info!("Removed message queue with id: {}", id); - } else if let Some(key) = self.key { - sysvmq::unlink_key(key)?; - - info!("Removed message queue key: {}", key); - } - - Ok(()) - } -} diff --git a/sysvmq/.gitignore b/sysvmq/.gitignore deleted file mode 100644 index 96ef6c0..0000000 --- a/sysvmq/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -Cargo.lock diff --git a/sysvmq/Cargo.toml b/sysvmq/Cargo.toml deleted file mode 100644 index a2cee0f..0000000 --- a/sysvmq/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "sysvmq" -version = "0.1.0" -edition = "2018" -authors = ["finga "] -repository = "https://git.onders.org/finga/mqrs" -license = "GPL-3.0-or-later" - -[dependencies] -libc = "0.2.98" -thiserror = "1.0.26" -nix = "0.21.0" diff --git a/sysvmq/src/lib.rs b/sysvmq/src/lib.rs deleted file mode 100644 index bb2c21d..0000000 --- a/sysvmq/src/lib.rs +++ /dev/null @@ -1,132 +0,0 @@ -use libc::{ - msgctl, msgget, msqid_ds, IPC_CREAT, IPC_EXCL, IPC_INFO, IPC_NOWAIT, IPC_PRIVATE, IPC_RMID, - IPC_SET, IPC_STAT, MSG_COPY, MSG_EXCEPT, MSG_INFO, MSG_NOERROR, MSG_STAT, -}; -use nix::errno::{errno, Errno}; -use std::{marker::PhantomData, num::ParseIntError, ptr}; -use thiserror::Error; - -#[derive(Debug, Error)] -pub enum SysvMqError { - #[error("SysV message queue: {0}")] - ErrnoError(&'static str), - #[error("No message queue found with key {0}")] - KeyNotFound(i32), - #[error("IO Error: {0}")] - IoError(#[from] std::io::Error), - #[error("Parse Error: {0}")] - ParserError(#[from] ParseIntError), -} - -/// IPC bit flags -#[repr(i32)] -pub enum Flags { - /// Create key if key does not exist. - CreateKey = IPC_CREAT, - /// Fail if key exists. - Exclusive = IPC_EXCL, - /// Return error on wait. - NoWait = IPC_NOWAIT, - /// No error if message is too big. - NoError = MSG_NOERROR, - /// Receive any message except of specified type. - Except = MSG_EXCEPT, - /// Copy (not remove) all queue messages. - Copy = MSG_COPY, - /// Private key (Special key value). - Private = IPC_PRIVATE, -} - -/// Commands for `msgctl()` -#[repr(i32)] -pub enum ControlCommands { - /// Remove identifier (Control command for `msgctl`, `semctl`, and `shmctl`). - Remove = IPC_RMID, - /// Set `ipc_perm` options (Control command for `msgctl`, `semctl`, and `shmctl`). - SetPerm = IPC_SET, - /// Get `ipc_perm` options (Control command for `msgctl`, `semctl`, and `shmctl`). - GetPerm = IPC_STAT, - /// See ipcs (Control command for `msgctl`, `semctl`, and `shmctl`). - IpcInfo = IPC_INFO, - /// IPCS control command. - Stat = MSG_STAT, - /// IPCS control command. - MsgInfo = MSG_INFO, -} - -pub fn unlink_id(id: i32) -> Result<(), SysvMqError> { - let res = unsafe { - msgctl( - id, - ControlCommands::Remove as i32, - ptr::null::() as *mut msqid_ds, - ) - }; - - match res { - -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), - _ => Ok(()), - } -} - -pub fn unlink_key(key: i32) -> Result<(), SysvMqError> { - let id = unsafe { msgget(key, 0) }; - - unlink_id(id) -} - -pub struct SysvMq { - pub id: i32, - pub key: i32, - message_mask: i32, - mode: i32, - types: PhantomData, -} - -impl SysvMq { - pub fn create(&mut self, key: i32) -> Result<&Self, SysvMqError> { - self.key = key; - self.id = unsafe { msgget(self.key, Flags::CreateKey as i32 | self.mode) }; - - match self.id { - -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), - _ => Ok(self), - } - } - - pub fn open(mut self, key: i32) -> Result { - 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 |= Flags::NoWait as i32; - self - } - - pub fn new() -> Self { - SysvMq { - id: -1, - key: 0, - message_mask: 0, - mode: 0o644, - types: PhantomData, - } - } -} - -impl Default for SysvMq { - fn default() -> Self { - Self::new() - } -}