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