From f4796e7da65ad00053c9b4baacd2fbd40526030c Mon Sep 17 00:00:00 2001 From: finga Date: Thu, 8 Jul 2021 14:20:53 +0200 Subject: [PATCH] Refactor `sysvmq::unlink_*` and check for error Use `sysvmq::id_from_key(key)` to receive the id of a queue identified with `key`. Here an error check is added as well. Adapt `Sysv::Unlink::run()` to comply with that change. --- src/sysv/unlink.rs | 6 ++++-- sysvmq/src/lib.rs | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/sysv/unlink.rs b/src/sysv/unlink.rs index ede5b67..981289b 100644 --- a/src/sysv/unlink.rs +++ b/src/sysv/unlink.rs @@ -25,9 +25,11 @@ impl Unlink { info!("Removed message queue with id: {}", id); } else if let Some(key) = self.key { - sysvmq::unlink_key(key)?; + let id = sysvmq::id_from_key(key)?; - info!("Removed message queue key: {}", key); + sysvmq::unlink_id(id)?; + + info!("Removed message queue key: {} (id: {})", key, id); } Ok(()) diff --git a/sysvmq/src/lib.rs b/sysvmq/src/lib.rs index bb2c21d..b9de136 100644 --- a/sysvmq/src/lib.rs +++ b/sysvmq/src/lib.rs @@ -69,10 +69,13 @@ pub fn unlink_id(id: i32) -> Result<(), SysvMqError> { } } -pub fn unlink_key(key: i32) -> Result<(), SysvMqError> { +pub fn id_from_key(key: i32) -> Result { let id = unsafe { msgget(key, 0) }; - unlink_id(id) + match id { + -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), + id => Ok(id), + } } pub struct SysvMq {