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,