Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mq_notify() on Linux #2467

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2467.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a ```nix::mqueue::mq_notify()``` function to support the ```mq_notify()``` system call on Linux
16 changes: 16 additions & 0 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ pub fn mq_close(mqdes: MqdT) -> Result<()> {
Errno::result(res).map(drop)
}

feature! {
#![feature = "time"]
use crate::sys::signal::SigEvent;
use crate::sys::signal::libc_sigevent;

/// Register the process for message queue notification
///
/// See also [`mq_notify(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_notify.html)
#[cfg(target_os = "linux")]
pub fn mq_notify(mqdes: &MqdT, notify: SigEvent) -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we taking notify by value?

let sig_event = notify.sigevent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.sigevent() would copy the underlying structure, we can use as_mut_ptr() to get the pointer directly.

Update: Just found this function returns a mutable pointer, which is not needed by this PR, looks like we need to add an as_ptr() function, which should be implemented in a way similar to how as_mut_ptr() was implemented.

let res = unsafe { libc::syscall(libc::SYS_mq_notify, mqdes.0, &sig_event as *const libc_sigevent) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Linux manual, glibc has provided a library wrapper for this syscall, we should prefer to use that. If the symbol is not exposed by the libc crate, we should add it there first (to the libc-0.2 branch), and then we adjust the dependency and add it on the Nix side.

Errno::result(res).map(drop)
}
}

/// Receive a message from a message queue
///
/// See also [`mq_receive(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_receive.html)
Expand Down
52 changes: 52 additions & 0 deletions test/test_mq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,58 @@ fn test_mq_send_and_receive() {
assert_eq!(msg_to_send, str::from_utf8(&buf[0..len]).unwrap());
}

extern "C" fn signal_catcher(_: libc::c_int) {}

#[test]
fn test_mq_send_receive_notify() {
let action = nix::sys::signal::SigAction::new(
nix::sys::signal::SigHandler::Handler(signal_catcher),
nix::sys::signal::SaFlags::SA_RESTART,
nix::sys::signal::SigSet::empty(),
);
unsafe {
nix::sys::signal::sigaction(nix::sys::signal::Signal::SIGUSR1, &action)
.unwrap()
};
//let _ = SIGNAL_FLAG.fetch_and(false, std::sync::atomic::Ordering::SeqCst);
const MSG_SIZE: mq_attr_member_t = 32;
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = "/a_nix_test_queue";

let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY | MQ_OFlag::O_EXCL;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let _ = nix::mqueue::mq_unlink(mq_name);
let r0 = mq_open(mq_name, oflag0, mode, Some(&attr));
if let Err(Errno::ENOSYS) = r0 {
println!("message queues not supported or module not loaded?");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the skip!() macro provided in test/common/mod.rs, which uses io::stderr() directly to ensure it won't be captured by cargo test so that the message will always be printed:

skip!("test_mq_send_receive_notify: message queues not supported or module not loaded, skipping test")

return;
};
let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY;
let mqd1 = mq_open(mq_name, oflag1, mode, Some(&attr)).unwrap();
nix::mqueue::mq_notify(
&mqd1,
nix::sys::signal::SigEvent::new(
nix::sys::signal::SigevNotify::SigevSignal {
signal: nix::sys::signal::Signal::SIGUSR1,
si_value: 0,
},
),
)
.unwrap();
let mqd0 = r0.unwrap();
let msg_to_send = "msg_1";
mq_send(&mqd0, msg_to_send.as_bytes(), 1).unwrap();

let mut buf = [0u8; 32];
let mut prio = 0u32;
let len = mq_receive(&mqd1, &mut buf, &mut prio).unwrap();
assert_eq!(prio, 1);

mq_close(mqd1).unwrap();
mq_close(mqd0).unwrap();
assert_eq!(msg_to_send, str::from_utf8(&buf[0..len]).unwrap());
}

#[test]
fn test_mq_timedreceive() {
const MSG_SIZE: mq_attr_member_t = 32;
Expand Down
Loading