From 4c82d41f8e198a0a9aac83527e2734f384238044 Mon Sep 17 00:00:00 2001 From: finga Date: Thu, 8 Jul 2021 14:46:10 +0200 Subject: [PATCH] Implement printing a list of SysV IPC mqs What could be improved is when printing the information to use a better human readable format. As usual the readme and the man page are both updated. --- README.md | 4 ++++ mqrs.1 | 12 ++++++++++++ src/main.rs | 2 ++ src/sysv.rs | 2 ++ src/sysv/list.rs | 26 ++++++++++++++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 src/sysv/list.rs diff --git a/README.md b/README.md index 12bbbff..5f68f7c 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,10 @@ queue. Following optional arguments are supported: - `-m`, `--mode`: Permissions (octal) to create the queue with. Default: 0644. +#### List all message queues +Use the `list` command to print a list of all message queues. No +further arguments are supported. + #### 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: diff --git a/mqrs.1 b/mqrs.1 index eb7b912..af46d24 100644 --- a/mqrs.1 +++ b/mqrs.1 @@ -241,6 +241,18 @@ Produce verbose output .B \-m, \-\-mode \fI\fP Permissions (octal) to create the queue with (default: 0644) .RE +.SS list [FLAGS] +Print a list of all existing SysV IPC message queues. +.TP 8 +.SS FLAGS +.RS +.TP 8 +.B \-h, \-\-help +Prints help information +.TP 8 +.B \-v, \-\-verbose +Produce verbose output +.RE .SS unlink [FLAGS] [OPTIONS] Delete an existing SysV IPC message queue. It is mandatory to pass exactly one OPTION. diff --git a/src/main.rs b/src/main.rs index 5e4db83..854ae95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ enum PosixCommand { #[derive(Clap, Debug)] enum SysvCommand { Create(sysv::Create), + List(sysv::List), Unlink(sysv::Unlink), } @@ -69,6 +70,7 @@ fn main() -> Result<()> { }, Backend::Sysv(s) => match s { SysvCommand::Create(c) => c.run()?, + SysvCommand::List(l) => l.run()?, SysvCommand::Unlink(u) => u.run()?, }, } diff --git a/src/sysv.rs b/src/sysv.rs index 91ee1a5..05edc1e 100644 --- a/src/sysv.rs +++ b/src/sysv.rs @@ -1,5 +1,7 @@ mod create; +mod list; mod unlink; pub use create::Create; +pub use list::List; pub use unlink::Unlink; diff --git a/src/sysv/list.rs b/src/sysv/list.rs new file mode 100644 index 0000000..f7e6ff7 --- /dev/null +++ b/src/sysv/list.rs @@ -0,0 +1,26 @@ +use anyhow::Result; +use clap::Clap; +use std::{ + fs::File, + io::{BufRead, BufReader}, +}; + +/// Print a list of existing message queues +#[derive(Clap, Debug)] +pub struct List {} + +impl List { + pub fn run(&self) -> Result<()> { + let file = BufReader::new(File::open("/proc/sysvipc/msg")?); + + for line in file.lines() { + for field in line?.split_whitespace().collect::>() { + print!("{0: <10}", field); + } + + println!(); + } + + Ok(()) + } +}