This adds the `sysvmq` package which handles SysV IPC message queues. For consistency the parameter for `permissions` is renamed to `mode` also for POSIX message queues.
64 lines
1.8 KiB
Rust
64 lines
1.8 KiB
Rust
use anyhow::Result;
|
|
use clap::Clap;
|
|
use log::{info, log_enabled, Level::Info};
|
|
use posixmq::PosixMq;
|
|
use std::fs;
|
|
|
|
/// Create a POSIX message queue
|
|
#[derive(Clap, Debug)]
|
|
pub struct Create {
|
|
/// Permissions (octal) to create the queue with
|
|
#[clap(short, long)]
|
|
mode: Option<String>,
|
|
/// Maximum number of messages in the queue
|
|
#[clap(short, long)]
|
|
capacity: Option<usize>,
|
|
/// Message size in bytes
|
|
#[clap(short = 's', long)]
|
|
msgsize: Option<usize>,
|
|
/// Name of the new queue
|
|
#[clap(value_name = "QUEUE")]
|
|
queue: String,
|
|
}
|
|
|
|
fn msgsize_default() -> usize {
|
|
match fs::read_to_string("/proc/sys/fs/mqueue/msgsize_default") {
|
|
Ok(m) => m.trim().parse::<usize>().expect("can never fail"),
|
|
_ => 8192,
|
|
}
|
|
}
|
|
|
|
fn msg_default() -> usize {
|
|
match fs::read_to_string("/proc/sys/fs/mqueue/msg_default") {
|
|
Ok(m) => m.trim().parse::<usize>().expect("can never fail"),
|
|
_ => 10,
|
|
}
|
|
}
|
|
|
|
impl Create {
|
|
pub fn run(&self) -> Result<()> {
|
|
let mq = &mut posixmq::OpenOptions::readonly();
|
|
|
|
if let Some(m) = &self.mode {
|
|
mq.mode(u32::from_str_radix(&m, 8)?);
|
|
}
|
|
|
|
mq.max_msg_len(self.msgsize.unwrap_or_else(msgsize_default))
|
|
.capacity(self.capacity.unwrap_or_else(msg_default))
|
|
.create_new()
|
|
.open(&self.queue)?;
|
|
|
|
if log_enabled!(Info) {
|
|
let mq = PosixMq::open(&self.queue)?;
|
|
let attributes = mq.attributes()?;
|
|
|
|
info!("Created message queue: {} with attributes msgsize: {}, capacity: {}, current_messages: {}",
|
|
&self.queue,
|
|
&attributes.max_msg_len,
|
|
&attributes.capacity,
|
|
&attributes.current_messages);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|