Skip to content

Commit

Permalink
Expose si_pid, si_uid, and si_status from siginfo_t as functions
Browse files Browse the repository at this point in the history
On Linux, siginfo_t cannot expose these fields directly due to
rust-lang#716 , so expose them as
functions, just like si_addr and si_value.

In order to get alignment correct on both 32-bit and 64-bit
architectures, define the sifields union, which includes variants that
start with a pointer. Update the existing si_addr and si_value functions
to use that union.
  • Loading branch information
joshtriplett committed Aug 10, 2020
1 parent ab3c229 commit e3fbc3b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,14 @@ impl siginfo_t {

(*(self as *const siginfo_t as *const siginfo_timer)).si_value
}

pub unsafe fn si_pid(&self) -> ::pid_t {
self.si_pid
}

pub unsafe fn si_uid(&self) -> ::uid_t {
self.si_uid
}
}

cfg_if! {
Expand Down
8 changes: 8 additions & 0 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ impl siginfo_t {
pub unsafe fn si_value(&self) -> ::sigval {
self.si_value
}

pub unsafe fn si_pid(&self) -> ::pid_t {
self.si_pid
}

pub unsafe fn si_uid(&self) -> ::uid_t {
self.si_uid
}
}

s! {
Expand Down
14 changes: 14 additions & 0 deletions src/unix/haiku/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ impl ::Clone for timezone {
}
}

impl siginfo_t {
pub unsafe fn si_addr(&self) -> *mut ::c_void {
self.si_addr
}

pub unsafe fn si_pid(&self) -> ::pid_t {
self.si_pid
}

pub unsafe fn si_uid(&self) -> ::uid_t {
self.si_uid
}
}

s! {
pub struct in_addr {
pub s_addr: ::in_addr_t,
Expand Down
80 changes: 62 additions & 18 deletions src/unix/linux_like/linux/gnu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,29 +287,73 @@ s! {
}
}

// Internal, for casts to access union fields
#[repr(C)]
#[derive(Copy,Clone)]
struct sifields_sigchld {
si_pid: ::pid_t,
si_uid: ::uid_t,
si_status: ::c_int,
}

// Internal, for casts to access union fields
#[repr(C)]
#[derive(Copy,Clone)]
struct sifields_sigfault {
si_addr: *mut ::c_void,
}

// Internal, for casts to access union fields
#[repr(C)]
#[derive(Copy,Clone)]
struct sifields_timer {
_si_tid: ::c_int,
_si_overrun: ::c_int,
si_sigval: ::sigval,
}

// Internal, for casts to access union fields
#[repr(C)]
union sifields {
sigchld: sifields_sigchld,
sigfault: sifields_sigfault,
timer: sifields_timer,
}

// Internal, for casts to access union fields. Note that some variants of
// sifields start with a pointer, which makes the alignment of sifields vary on
// 32-bit and 64-bit architectures.
#[repr(C)]
struct siginfo_sifields {
_si_signo: ::c_int,
_si_errno: ::c_int,
_si_code: ::c_int,
sifields: sifields,
}

impl siginfo_t {
unsafe fn sifields(&self) -> &sifields {
&(*(self as *const siginfo_t as *const siginfo_sifields)).sifields
}

pub unsafe fn si_addr(&self) -> *mut ::c_void {
#[repr(C)]
struct siginfo_sigfault {
_si_signo: ::c_int,
_si_errno: ::c_int,
_si_code: ::c_int,
si_addr: *mut ::c_void,
}
(*(self as *const siginfo_t as *const siginfo_sigfault)).si_addr
self.sifields().sigfault.si_addr
}

pub unsafe fn si_value(&self) -> ::sigval {
#[repr(C)]
struct siginfo_timer {
_si_signo: ::c_int,
_si_errno: ::c_int,
_si_code: ::c_int,
_si_tid: ::c_int,
_si_overrun: ::c_int,
si_sigval: ::sigval,
}
(*(self as *const siginfo_t as *const siginfo_timer)).si_sigval
self.sifields().timer.si_sigval
}

pub unsafe fn si_pid(&self) -> ::pid_t {
self.sifields().sigchld.si_pid
}

pub unsafe fn si_uid(&self) -> ::uid_t {
self.sifields().sigchld.si_uid
}

pub unsafe fn si_status(&self) -> ::c_int {
self.sifields().sigchld.si_status
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/vxworks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ impl ::Clone for _Vx_semaphore {
}
}

impl siginfo_t {
pub unsafe fn si_addr(&self) -> *mut ::c_void {
self.si_addr
}

pub unsafe fn si_value(&self) -> ::sigval {
self.si_value
}

pub unsafe fn si_pid(&self) -> ::pid_t {
self.si_pid
}

pub unsafe fn si_uid(&self) -> ::uid_t {
self.si_uid
}
}

s! {
// b_pthread_condattr_t.h
pub struct pthread_condattr_t {
Expand Down

0 comments on commit e3fbc3b

Please sign in to comment.