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

Implement safe bindings around pthread_sigqueue. #1798

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ targets = [
]

[dependencies]
libc = { version = "0.2.127", features = [ "extra_traits" ] }
libc = { version = "0.2.132", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "1.0"
pin-utils = { version = "0.1.0", optional = true }
Expand Down
60 changes: 60 additions & 0 deletions src/sys/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,64 @@ pub fn pthread_kill<T>(thread: Pthread, signal: T) -> Result<()>
let res = unsafe { libc::pthread_kill(thread, sig) };
Errno::result(res).map(drop)
}

/// Value to pass with a signal. Can be either integer or pointer.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum SigVal {
/// Use this variant to pass a integer to the sigval union
Int(libc::c_int),
/// Use this variant to pass a pointer to the sigval union
Ptr(*mut libc::c_void),
}

// Because of macro/trait machinery in libc, libc doesn't provide this union.
Copy link
Member

Choose a reason for hiding this comment

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

Why not? It's possible to define unions in libc these days. Unless there is a very compelling reason not to, you should submit this as a PR for libc.

Copy link
Author

@pirocks pirocks Aug 18, 2022

Choose a reason for hiding this comment

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

The problem is there already is a sigval in libc and is defined as a struct(which is used in many places since signal handling has several syscalls associated with it). That struct has PartialEq/Hash/Debug etc implemented, and has done so for a long time. There's no practical way to implement PartialEq/Hash/Debug for a union b/c that would lead to hashing/equality checking/printing uninitialized memory. So changing the current struct to a union is a breaking change. (see: rust-lang/libc#2816)

I can add a second definition in libc, but it would have to have a different name. If that's desired I guess reply to this comment. I'll also update that comment with the info in this one.

Copy link
Member

Choose a reason for hiding this comment

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

Have you seen rust-lang/libc#2813 ? It deals with another union that was originally defined as a struct in libc. The solution we came up with there was to define a sigevent_0_2_126 struct identical to the old sigevent, and then define sigevent with union fields as it ought to be. Then implement Deref and DerefMut for sigevent to sigevent_0_2_126 . That way consumers of the old version can still access the fields that should've been in a union. Would something similar work for sigval?

// There already is a sigval in libc and is defined as a struct. That struct
// has PartialEq/Hash/Debug etc implemented, and has done so for a long time.
// There's no practical way to implement PartialEq/Hash/Debug for a union b/c
// that would lead to hashing/equality checking/printing uninitialized memory.
// So changing the current struct to a union is a breaking change, because of
// the removal of the union trait implementations.
// So we have to have this definition which is nly used to ensure that union
// type conversion is done exactly as C would.
#[repr(C)]
union sigval_union {
ptr: *mut libc::c_void,
int: libc::c_int,
}

use std::convert::From;

impl From<SigVal> for libc::sigval {
fn from(sigval: SigVal) -> Self {
match sigval {
SigVal::Int(int) => {
let as_ptr = unsafe { sigval_union { int }.ptr };
libc::sigval { sival_ptr: as_ptr }
}
SigVal::Ptr(ptr) => {
libc::sigval { sival_ptr: ptr }
}
}
}
}

/// Queue a signal and data to a thread (see [`pthread_sigqueue(3)`]).
///
/// If `signal` is `None`, `pthread_sigqueue` will only preform error checking and
/// won't send any signal.
///
/// `pthread_sigqueue` is a GNU extension and is not available on other libcs
///
/// [`pthread_sigqueue(3)`]: https://man7.org/linux/man-pages/man3/pthread_sigqueue.3.html
#[cfg(target_env = "gnu")]
pub fn pthread_sigqueue<T>(thread: Pthread, signal: T, sigval: SigVal) -> Result<()>
where T: Into<Option<crate::sys::signal::Signal>>
{
let sig = match signal.into() {
Some(s) => s as libc::c_int,
None => 0,
};
let res = unsafe { libc::pthread_sigqueue(thread, sig, sigval.into()) };
Errno::result(res).map(drop)
}
}
12 changes: 12 additions & 0 deletions test/sys/test_pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ fn test_pthread_kill_none() {
pthread_kill(pthread_self(), None)
.expect("Should be able to send signal to my thread.");
}

#[test]
#[cfg(target_env = "gnu")]
fn test_pthread_sigqueue_none() {
use std::ptr::null_mut;
pthread_sigqueue(pthread_self(), None, SigVal::Int(0)).expect(
"Should be able to send signal to my thread, with an integer sigval.",
);
pthread_sigqueue(pthread_self(), None, SigVal::Ptr(null_mut())).expect(
"Should be able to send signal to my thread, with an ptr sigval.",
);
}