@@ -22,7 +22,8 @@ use crate::Result;
22
22
pub use libc:: signalfd_siginfo as siginfo;
23
23
24
24
use std:: mem;
25
- use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
25
+ use std:: os:: unix:: io:: { AsRawFd , RawFd , BorrowedFd , FromRawFd , OwnedFd , AsFd } ;
26
+ use std:: mem:: ManuallyDrop ;
26
27
27
28
libc_bitflags ! {
28
29
pub struct SfdFlags : libc:: c_int {
@@ -31,7 +32,6 @@ libc_bitflags! {
31
32
}
32
33
}
33
34
34
- pub const SIGNALFD_NEW : RawFd = -1 ;
35
35
#[ deprecated( since = "0.23.0" , note = "use mem::size_of::<siginfo>() instead" ) ]
36
36
pub const SIGNALFD_SIGINFO_SIZE : usize = mem:: size_of :: < siginfo > ( ) ;
37
37
@@ -46,13 +46,14 @@ pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();
46
46
/// signalfd (the default handler will be invoked instead).
47
47
///
48
48
/// See [the signalfd man page for more information](https://man7.org/linux/man-pages/man2/signalfd.2.html)
49
- pub fn signalfd ( fd : RawFd , mask : & SigSet , flags : SfdFlags ) -> Result < RawFd > {
49
+ pub fn signalfd ( fd : Option < BorrowedFd > , mask : & SigSet , flags : SfdFlags ) -> Result < OwnedFd > {
50
+ let raw_fd = fd. as_ref ( ) . map_or ( -1 , AsRawFd :: as_raw_fd) ;
50
51
unsafe {
51
52
Errno :: result ( libc:: signalfd (
52
- fd as libc :: c_int ,
53
+ raw_fd ,
53
54
mask. as_ref ( ) ,
54
55
flags. bits ( ) ,
55
- ) )
56
+ ) ) . map ( |raw_fd| FromRawFd :: from_raw_fd ( raw_fd ) )
56
57
}
57
58
}
58
59
@@ -82,30 +83,30 @@ pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
82
83
/// Err(err) => (), // some error happend
83
84
/// }
84
85
/// ```
85
- #[ derive( Debug , Eq , Hash , PartialEq ) ]
86
- pub struct SignalFd ( RawFd ) ;
86
+ #[ derive( Debug ) ]
87
+ pub struct SignalFd ( ManuallyDrop < OwnedFd > ) ;
87
88
88
89
impl SignalFd {
89
90
pub fn new ( mask : & SigSet ) -> Result < SignalFd > {
90
91
Self :: with_flags ( mask, SfdFlags :: empty ( ) )
91
92
}
92
93
93
94
pub fn with_flags ( mask : & SigSet , flags : SfdFlags ) -> Result < SignalFd > {
94
- let fd = signalfd ( SIGNALFD_NEW , mask, flags) ?;
95
+ let fd = signalfd ( None , mask, flags) ?;
95
96
96
- Ok ( SignalFd ( fd ) )
97
+ Ok ( SignalFd ( ManuallyDrop :: new ( fd ) ) )
97
98
}
98
99
99
100
pub fn set_mask ( & mut self , mask : & SigSet ) -> Result < ( ) > {
100
- signalfd ( self . 0 , mask, SfdFlags :: empty ( ) ) . map ( drop)
101
+ signalfd ( Some ( self . 0 . as_fd ( ) ) , mask, SfdFlags :: empty ( ) ) . map ( drop)
101
102
}
102
103
103
104
pub fn read_signal ( & mut self ) -> Result < Option < siginfo > > {
104
105
let mut buffer = mem:: MaybeUninit :: < siginfo > :: uninit ( ) ;
105
106
106
107
let size = mem:: size_of_val ( & buffer) ;
107
108
let res = Errno :: result ( unsafe {
108
- libc:: read ( self . 0 , buffer. as_mut_ptr ( ) as * mut libc:: c_void , size)
109
+ libc:: read ( self . 0 . as_raw_fd ( ) , buffer. as_mut_ptr ( ) as * mut libc:: c_void , size)
109
110
} )
110
111
. map ( |r| r as usize ) ;
111
112
match res {
@@ -119,7 +120,7 @@ impl SignalFd {
119
120
120
121
impl Drop for SignalFd {
121
122
fn drop ( & mut self ) {
122
- let e = unistd:: close ( self . 0 ) ;
123
+ let e = unistd:: close ( self . 0 . as_raw_fd ( ) ) ;
123
124
if !std:: thread:: panicking ( ) && e == Err ( Errno :: EBADF ) {
124
125
panic ! ( "Closing an invalid file descriptor!" ) ;
125
126
} ;
@@ -128,7 +129,7 @@ impl Drop for SignalFd {
128
129
129
130
impl AsRawFd for SignalFd {
130
131
fn as_raw_fd ( & self ) -> RawFd {
131
- self . 0
132
+ self . 0 . as_raw_fd ( )
132
133
}
133
134
}
134
135
0 commit comments