Skip to content

Creating proper type for sighandler_t using anonymous union on Unix #2107

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

Open
wants to merge 8 commits into
base: main
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
3 changes: 3 additions & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ fn test_apple(target: &str) {
}

cfg.skip_struct(move |ty| {
if ty.starts_with("__c_anonymous_") {
return true;
}
match ty {
// FIXME: actually a union
"sigval" => true,
Expand Down
45 changes: 41 additions & 4 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub type ssize_t = isize;
pub type pid_t = i32;
pub type in_addr_t = u32;
pub type in_port_t = u16;
pub type sighandler_t = ::size_t;
pub type sighandler_t = __c_anonymous_sigaction_handler;
Copy link
Member

Choose a reason for hiding this comment

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

Is the current type wrong on all the UNIX platforms?

Copy link
Author

Choose a reason for hiding this comment

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

pub type cc_t = ::c_uchar;

cfg_if! {
Expand All @@ -51,6 +51,18 @@ impl ::Clone for DIR {
}
pub type locale_t = *mut ::c_void;

s_no_extra_traits! {
pub union __c_anonymous_sigaction_handler {
pub sa_handler: Option<extern "C" fn(c_int) -> ()>,
pub sa_sigaction: Option<extern "C" fn(
c_int,
*mut siginfo_t,
*mut c_void
) -> ()>,
pub default: size_t,
}
}

s! {
pub struct group {
pub gr_name: *mut ::c_char,
Expand Down Expand Up @@ -206,12 +218,36 @@ s! {
}
}

cfg_if! {
if #[cfg(feature = "extra_traits")] {
impl PartialEq for __c_anonymous_sigaction_handler {
fn eq(&self, other: &__c_anonymous_sigaction_handler) -> bool {
unsafe { self.default == other.default }
}
}
impl Eq for __c_anonymous_sigaction_handler {}
impl ::fmt::Debug for __c_anonymous_sigaction_handler {
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
f.debug_struct("sigaction_t")
.field("value", unsafe { &self.default } )
.finish()
}
}
impl ::hash::Hash for __c_anonymous_sigaction_handler {
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
unsafe { self.default.hash(state) };
}
}
}
}

pub const INT_MIN: c_int = -2147483648;
pub const INT_MAX: c_int = 2147483647;

pub const SIG_DFL: sighandler_t = 0 as sighandler_t;
pub const SIG_IGN: sighandler_t = 1 as sighandler_t;
pub const SIG_ERR: sighandler_t = !0 as sighandler_t;
pub const SIG_DFL: sighandler_t = sighandler_t { default: 0 };
pub const SIG_IGN: sighandler_t = sighandler_t { default: 1 };
pub const SIG_ERR: sighandler_t = sighandler_t { default: !0 };

cfg_if! {
if #[cfg(not(target_os = "nto"))] {
pub const DT_UNKNOWN: u8 = 0;
Expand All @@ -224,6 +260,7 @@ cfg_if! {
pub const DT_SOCK: u8 = 12;
}
}

cfg_if! {
if #[cfg(not(target_os = "redox"))] {
pub const FD_CLOEXEC: ::c_int = 0x1;
Expand Down