Skip to content
New issue

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

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

Already on GitHub? # 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

Conversation

ChrysoliteAzalea
Copy link
Contributor

What does this PR do

This PR adds support for the mq_notify() API that allows a process to register for POSIX message queue notifications.

Checklist:

  • [yes] I have read CONTRIBUTING.md
  • [yes] I have written necessary tests and rustdoc comments
  • [yes] A change log has been added if this PR modifies nix's API

@ChrysoliteAzalea ChrysoliteAzalea marked this pull request as draft August 1, 2024 02:54
#[cfg(target_os = "linux")]
pub fn mq_notify(mqdes: &MqdT, notify: SigEvent) -> Result<()> {
let sig_event = notify.sigevent();
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.

/// 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<()> {
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.

///
/// 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 _ = 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")

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants