Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close file descriptor on drop in TimerFd #1381

Merged
merged 2 commits into from Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed

### Fixed
- `TimerFd` now closes the underlying fd on drop.
([#1381](https://github.com/nix-rust/nix/pull/1381))
- Define `*_MAGIC` filesystem constants on Linux s390x
(#[1372](https://github.com/nix-rust/nix/pull/1372))
- mqueue, sysinfo, timespec, statfs, test_ptrace_syscall() on x32
Expand All @@ -39,6 +41,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Removed `SockLevel`, which hasn't been used for a few years
(#[1362](https://github.com/nix-rust/nix/pull/1362))
- Removed both `Copy` and `Clone` from `TimerFd`.
([#1381](https://github.com/nix-rust/nix/pull/1381))

## [0.19.1] - 28 November 2020
### Fixed
Expand Down
17 changes: 15 additions & 2 deletions src/sys/timerfd.rs
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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");
}
}
}
}