1717//! signal handlers.
1818use crate :: errno:: Errno ;
1919pub use crate :: sys:: signal:: { self , SigSet } ;
20- use crate :: unistd;
2120use crate :: Result ;
2221pub use libc:: signalfd_siginfo as siginfo;
2322
2423use std:: mem;
25- use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
24+ use std:: os:: unix:: io:: { AsRawFd , RawFd , FromRawFd , OwnedFd , AsFd , BorrowedFd } ;
2625
2726libc_bitflags ! {
2827 pub struct SfdFlags : libc:: c_int {
@@ -31,7 +30,6 @@ libc_bitflags! {
3130 }
3231}
3332
34- pub const SIGNALFD_NEW : RawFd = -1 ;
3533#[ deprecated( since = "0.23.0" , note = "use mem::size_of::<siginfo>() instead" ) ]
3634pub const SIGNALFD_SIGINFO_SIZE : usize = mem:: size_of :: < siginfo > ( ) ;
3735
@@ -46,13 +44,14 @@ pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();
4644/// signalfd (the default handler will be invoked instead).
4745///
4846/// 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 ( ) ) ;
5049 unsafe {
5150 Errno :: result ( libc:: signalfd (
52- fd as libc :: c_int ,
51+ raw_fd ,
5352 mask. as_ref ( ) ,
5453 flags. bits ( ) ,
55- ) )
54+ ) ) . map ( |raw_fd| FromRawFd :: from_raw_fd ( raw_fd ) )
5655 }
5756}
5857
@@ -82,30 +81,30 @@ pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
8281/// Err(err) => (), // some error happend
8382/// }
8483/// ```
85- #[ derive( Debug , Eq , Hash , PartialEq ) ]
86- pub struct SignalFd ( RawFd ) ;
84+ #[ derive( Debug ) ]
85+ pub struct SignalFd ( OwnedFd ) ;
8786
8887impl SignalFd {
8988 pub fn new ( mask : & SigSet ) -> Result < SignalFd > {
9089 Self :: with_flags ( mask, SfdFlags :: empty ( ) )
9190 }
9291
9392 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) ?;
9594
9695 Ok ( SignalFd ( fd) )
9796 }
9897
9998 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)
101100 }
102101
103102 pub fn read_signal ( & mut self ) -> Result < Option < siginfo > > {
104103 let mut buffer = mem:: MaybeUninit :: < siginfo > :: uninit ( ) ;
105104
106105 let size = mem:: size_of_val ( & buffer) ;
107106 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)
109108 } )
110109 . map ( |r| r as usize ) ;
111110 match res {
@@ -117,18 +116,14 @@ impl SignalFd {
117116 }
118117}
119118
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 ( )
126122 }
127123}
128-
129124impl AsRawFd for SignalFd {
130125 fn as_raw_fd ( & self ) -> RawFd {
131- self . 0
126+ self . 0 . as_raw_fd ( )
132127 }
133128}
134129
0 commit comments