From 0bff16f5b2a5a1fd32277e6a5e8f6c1663831847 Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 7 Jul 2021 20:48:07 +0200 Subject: [PATCH] Implement deletion of SysV IPC message queues As usual also the readme and the man page are updated. --- src/lib.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7f7e4e3..bb2c21d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ use libc::{ - msgget, 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, + 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}; +use std::{marker::PhantomData, num::ParseIntError, ptr}; use thiserror::Error; #[derive(Debug, Error)] @@ -54,6 +54,27 @@ pub enum ControlCommands { 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,