summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristopher Dumoulin <cdumouli@akamai.com>2021-02-01 14:46:24 +0000
committerChristopher Dumoulin <cdumouli@akamai.com>2021-02-16 18:45:30 +0000
commitc33fa7498a66c45ce2a708a514df1a05ad1ef7c9 (patch)
treead5db75808bd70b120b861ee5460f771f9aeef98 /src
parenta279f789bde7d1e5ba738166d214b994fa7564e6 (diff)
downloadnix-c33fa7498a66c45ce2a708a514df1a05ad1ef7c9.zip
Close file descriptor on drop in TimerFd
Diffstat (limited to 'src')
-rw-r--r--src/sys/timerfd.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/sys/timerfd.rs b/src/sys/timerfd.rs
index 3086309e..4a247194 100644
--- a/src/sys/timerfd.rs
+++ b/src/sys/timerfd.rs
@@ -37,7 +37,7 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
/// A timerfd instance. This is also a file descriptor, you can feed it to
/// other interfaces consuming file descriptors, epoll for example.
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug)]
pub struct TimerFd {
fd: RawFd,
}
@@ -166,7 +166,7 @@ pub enum Expiration {
impl TimerFd {
/// Creates a new timer based on the clock defined by `clockid`. The
/// underlying fd can be assigned specific flags with `flags` (CLOEXEC,
- /// NONBLOCK).
+ /// NONBLOCK). The underlying fd will be closed on drop.
pub fn new(clockid: ClockId, flags: TimerFlags) -> Result<Self> {
Errno::result(unsafe { libc::timerfd_create(clockid as i32, flags.bits()) })
.map(|fd| Self { fd })
@@ -270,3 +270,16 @@ impl TimerFd {
Ok(())
}
}
+
+impl Drop for TimerFd {
+ fn drop(&mut self) {
+ if !std::thread::panicking() {
+ let result = Errno::result(unsafe {
+ libc::close(self.fd)
+ });
+ if let Err(Error::Sys(Errno::EBADF)) = result {
+ panic!("close of TimerFd encountered EBADF");
+ }
+ }
+ }
+}