Skip to content

Commit 9ec238c

Browse files
committed
Expose si_pid, si_uid, and si_status from siginfo_t as functions
On Linux, siginfo_t cannot expose these fields directly due to #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.
1 parent ab3c229 commit 9ec238c

File tree

5 files changed

+105
-18
lines changed

5 files changed

+105
-18
lines changed

src/unix/bsd/apple/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,14 @@ impl siginfo_t {
680680

681681
(*(self as *const siginfo_t as *const siginfo_timer)).si_value
682682
}
683+
684+
pub unsafe fn si_pid(&self) -> ::pid_t {
685+
self.si_pid
686+
}
687+
688+
pub unsafe fn si_uid(&self) -> ::uid_t {
689+
self.si_uid
690+
}
683691
}
684692

685693
cfg_if! {

src/unix/bsd/freebsdlike/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ impl siginfo_t {
3131
pub unsafe fn si_value(&self) -> ::sigval {
3232
self.si_value
3333
}
34+
35+
pub unsafe fn si_pid(&self) -> ::pid_t {
36+
self.si_pid
37+
}
38+
39+
pub unsafe fn si_uid(&self) -> ::uid_t {
40+
self.si_uid
41+
}
3442
}
3543

3644
s! {

src/unix/haiku/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ impl ::Clone for timezone {
3838
}
3939
}
4040

41+
impl siginfo_t {
42+
pub unsafe fn si_addr(&self) -> *mut ::c_void {
43+
self.si_addr
44+
}
45+
46+
pub unsafe fn si_pid(&self) -> ::pid_t {
47+
self.si_pid
48+
}
49+
50+
pub unsafe fn si_uid(&self) -> ::uid_t {
51+
self.si_uid
52+
}
53+
}
54+
4155
s! {
4256
pub struct in_addr {
4357
pub s_addr: ::in_addr_t,

src/unix/linux_like/linux/gnu/mod.rs

+57-18
Original file line numberDiff line numberDiff line change
@@ -287,29 +287,68 @@ s! {
287287
}
288288
}
289289

290+
// Internal, for casts to access union fields
291+
#[repr(C)]
292+
#[derive(Copy,Clone)]
293+
struct sifields_sigchld {
294+
si_pid: ::pid_t,
295+
si_uid: ::uid_t,
296+
si_status: ::c_int,
297+
}
298+
299+
// Internal, for casts to access union fields
300+
#[repr(C)]
301+
#[derive(Copy,Clone)]
302+
struct sifields_sigfault {
303+
si_addr: *mut ::c_void,
304+
}
305+
306+
// Internal, for casts to access union fields
307+
#[repr(C)]
308+
#[derive(Copy,Clone)]
309+
struct sifields_timer {
310+
_si_tid: ::c_int,
311+
_si_overrun: ::c_int,
312+
si_sigval: ::sigval,
313+
}
314+
315+
// Internal, for casts to access union fields
316+
#[repr(C)]
317+
union sifields {
318+
sigchld: sifields_sigchld,
319+
sigfault: sifields_sigfault,
320+
timer: sifields_timer,
321+
}
322+
323+
// Internal, for casts to access union fields. Note that some variants of sifields start with a
324+
// pointer, which makes the alignment of sifields vary on 32-bit and 64-bit architectures.
325+
#[repr(C)]
326+
struct siginfo_sifields {
327+
_si_signo: ::c_int,
328+
_si_errno: ::c_int,
329+
_si_code: ::c_int,
330+
sifields: sifields,
331+
}
332+
290333
impl siginfo_t {
291334
pub unsafe fn si_addr(&self) -> *mut ::c_void {
292-
#[repr(C)]
293-
struct siginfo_sigfault {
294-
_si_signo: ::c_int,
295-
_si_errno: ::c_int,
296-
_si_code: ::c_int,
297-
si_addr: *mut ::c_void,
298-
}
299-
(*(self as *const siginfo_t as *const siginfo_sigfault)).si_addr
335+
(*(self as *const siginfo_t as *const siginfo_sifields)).sifields.sigfault.si_addr
300336
}
301337

302338
pub unsafe fn si_value(&self) -> ::sigval {
303-
#[repr(C)]
304-
struct siginfo_timer {
305-
_si_signo: ::c_int,
306-
_si_errno: ::c_int,
307-
_si_code: ::c_int,
308-
_si_tid: ::c_int,
309-
_si_overrun: ::c_int,
310-
si_sigval: ::sigval,
311-
}
312-
(*(self as *const siginfo_t as *const siginfo_timer)).si_sigval
339+
(*(self as *const siginfo_t as *const siginfo_sifields)).sifields.timer.si_sigval
340+
}
341+
342+
pub unsafe fn si_pid(&self) -> ::pid_t {
343+
(*(self as *const siginfo_t as *const siginfo_sifields)).sifields.sigchld.si_pid
344+
}
345+
346+
pub unsafe fn si_uid(&self) -> ::uid_t {
347+
(*(self as *const siginfo_t as *const siginfo_sifields)).sifields.sigchld.si_uid
348+
}
349+
350+
pub unsafe fn si_status(&self) -> ::c_int {
351+
(*(self as *const siginfo_t as *const siginfo_sifields)).sifields.sigchld.si_status
313352
}
314353
}
315354

src/vxworks/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ impl ::Clone for _Vx_semaphore {
112112
}
113113
}
114114

115+
impl siginfo_t {
116+
pub unsafe fn si_addr(&self) -> *mut ::c_void {
117+
self.si_addr
118+
}
119+
120+
pub unsafe fn si_value(&self) -> ::sigval {
121+
self.si_value
122+
}
123+
124+
pub unsafe fn si_pid(&self) -> ::pid_t {
125+
self.si_pid
126+
}
127+
128+
pub unsafe fn si_uid(&self) -> ::uid_t {
129+
self.si_uid
130+
}
131+
}
132+
115133
s! {
116134
// b_pthread_condattr_t.h
117135
pub struct pthread_condattr_t {

0 commit comments

Comments
 (0)