From 615510efa02ceb50cb734039f4e2a00a3f4bb126 Mon Sep 17 00:00:00 2001 From: Christopher Skane Date: Sun, 20 Aug 2023 12:45:41 -0400 Subject: [PATCH 1/4] Changed `name` parameter of `mq_open` and `mq_unlink` to be generic over `NixPath`. --- CHANGELOG.md | 2 ++ src/mqueue.rs | 28 ++++++++++++++++++---------- test/test_mq.rs | 16 +++++++--------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85bd94b2a3..7537fbcadc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). (#[1906](https://github.com/nix-rust/nix/pull/1906)) - Implemented AsFd, AsRawFd, FromRawFd, and IntoRawFd for `mqueue::MqdT`. See ([#2097](https://github.com/nix-rust/nix/pull/2097)) +- Refactored `name` parameter of `mq_open` and `mq_unlink` to be generic over + `NixPath`. See (!TODO). ### Fixed - Fix: send `ETH_P_ALL` in htons format diff --git a/src/mqueue.rs b/src/mqueue.rs index e33797c81f..e7cdd446f6 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -31,11 +31,11 @@ //! [Further reading and details on the C API](https://man7.org/linux/man-pages/man7/mq_overview.7.html) use crate::errno::Errno; +use crate::NixPath; use crate::Result; use crate::sys::stat::Mode; use libc::{self, c_char, mqd_t, size_t}; -use std::ffi::CStr; use std::mem; #[cfg(any( target_os = "linux", @@ -149,31 +149,39 @@ impl MqAttr { /// See also [`mq_open(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html) // The mode.bits() cast is only lossless on some OSes #[allow(clippy::cast_lossless)] -pub fn mq_open( - name: &CStr, +pub fn mq_open

( + name: &P, oflag: MQ_OFlag, mode: Mode, attr: Option<&MqAttr>, -) -> Result { - let res = match attr { +) -> Result +where + P: ?Sized + NixPath, +{ + let res = name.with_nix_path(|cstr| match attr { Some(mq_attr) => unsafe { libc::mq_open( - name.as_ptr(), + cstr.as_ptr(), oflag.bits(), mode.bits() as libc::c_int, &mq_attr.mq_attr as *const libc::mq_attr, ) }, - None => unsafe { libc::mq_open(name.as_ptr(), oflag.bits()) }, - }; + None => unsafe { libc::mq_open(cstr.as_ptr(), oflag.bits()) }, + })?; + Errno::result(res).map(MqdT) } /// Remove a message queue /// /// See also [`mq_unlink(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_unlink.html) -pub fn mq_unlink(name: &CStr) -> Result<()> { - let res = unsafe { libc::mq_unlink(name.as_ptr()) }; +pub fn mq_unlink

(name: &P) -> Result<()> +where + P: ?Sized + NixPath, +{ + let res = + name.with_nix_path(|cstr| unsafe { libc::mq_unlink(cstr.as_ptr()) })?; Errno::result(res).map(drop) } diff --git a/test/test_mq.rs b/test/test_mq.rs index f232434e12..1fd8929c17 100644 --- a/test/test_mq.rs +++ b/test/test_mq.rs @@ -1,5 +1,4 @@ use cfg_if::cfg_if; -use std::ffi::CString; use std::str; use nix::errno::Errno; @@ -34,7 +33,7 @@ macro_rules! assert_attr_eq { fn test_mq_send_and_receive() { const MSG_SIZE: mq_attr_member_t = 32; let attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap(); + let mq_name = "/a_nix_test_queue"; let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; @@ -63,7 +62,7 @@ fn test_mq_send_and_receive() { fn test_mq_timedreceive() { const MSG_SIZE: mq_attr_member_t = 32; let attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap(); + let mq_name = "/a_nix_test_queue"; let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; @@ -95,7 +94,7 @@ fn test_mq_getattr() { use nix::mqueue::mq_getattr; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap(); + let mq_name = "/attr_test_get_attr"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name, oflag, mode, Some(&initial_attr)); @@ -120,7 +119,7 @@ fn test_mq_setattr() { use nix::mqueue::{mq_getattr, mq_setattr}; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap(); + let mq_name = "/attr_test_get_attr"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name, oflag, mode, Some(&initial_attr)); @@ -170,7 +169,7 @@ fn test_mq_set_nonblocking() { use nix::mqueue::{mq_getattr, mq_remove_nonblock, mq_set_nonblock}; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap(); + let mq_name = "/attr_test_get_attr"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name, oflag, mode, Some(&initial_attr)); @@ -194,10 +193,9 @@ fn test_mq_unlink() { use nix::mqueue::mq_unlink; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name_opened = &CString::new(b"/mq_unlink_test".as_ref()).unwrap(); + let mq_name_opened = "/mq_unlink_test"; #[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))] - let mq_name_not_opened = - &CString::new(b"/mq_unlink_test".as_ref()).unwrap(); + let mq_name_not_opened = "/mq_unlink_test"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name_opened, oflag, mode, Some(&initial_attr)); From 3e4c1b4ed676f9849bd6696bfc35cf631758597a Mon Sep 17 00:00:00 2001 From: Christopher Skane Date: Sun, 20 Aug 2023 13:05:50 -0400 Subject: [PATCH 2/4] Updated with PR link. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7537fbcadc..c07e61b85d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Implemented AsFd, AsRawFd, FromRawFd, and IntoRawFd for `mqueue::MqdT`. See ([#2097](https://github.com/nix-rust/nix/pull/2097)) - Refactored `name` parameter of `mq_open` and `mq_unlink` to be generic over - `NixPath`. See (!TODO). + `NixPath`. See ([#2102](https://github.com/nix-rust/nix/pull/2102)). ### Fixed - Fix: send `ETH_P_ALL` in htons format From 13d7e95d2e24fe92c1b3b217c31c60f8b8f51bb9 Mon Sep 17 00:00:00 2001 From: Christopher Skane Date: Sun, 20 Aug 2023 13:10:47 -0400 Subject: [PATCH 3/4] Fixed docs example code. --- src/mqueue.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mqueue.rs b/src/mqueue.rs index e7cdd446f6..28c16c9cfe 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -9,7 +9,7 @@ //! use nix::sys::stat::Mode; //! //! const MSG_SIZE: mq_attr_member_t = 32; -//! let mq_name= CString::new("/a_nix_test_queue").unwrap(); +//! let mq_name= "/a_nix_test_queue"; //! //! let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; //! let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; From 17d8acadb445b339ef14d5029035c4be62fe19a9 Mon Sep 17 00:00:00 2001 From: Christopher Skane Date: Sun, 20 Aug 2023 13:18:32 -0400 Subject: [PATCH 4/4] Fixed docs example code for real this time. --- src/mqueue.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mqueue.rs b/src/mqueue.rs index 28c16c9cfe..fb07d2accb 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -13,12 +13,12 @@ //! //! let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; //! let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; -//! let mqd0 = mq_open(&mq_name, oflag0, mode, None).unwrap(); +//! let mqd0 = mq_open(mq_name, oflag0, mode, None).unwrap(); //! let msg_to_send = b"msg_1"; //! mq_send(&mqd0, msg_to_send, 1).unwrap(); //! //! let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY; -//! let mqd1 = mq_open(&mq_name, oflag1, mode, None).unwrap(); +//! let mqd1 = mq_open(mq_name, oflag1, mode, None).unwrap(); //! let mut buf = [0u8; 32]; //! let mut prio = 0u32; //! let len = mq_receive(&mqd1, &mut buf, &mut prio).unwrap();