diff options
author | Alan Somers <asomers@gmail.com> | 2021-02-01 20:00:48 -0700 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2021-02-07 15:53:40 -0700 |
commit | c9cb83a5f6a4ec315b187afcf899d5758647dd0f (patch) | |
tree | e0879116b5302f17e3c3deee98e3e0581812e866 /src/dir.rs | |
parent | a279f789bde7d1e5ba738166d214b994fa7564e6 (diff) | |
download | nix-c9cb83a5f6a4ec315b187afcf899d5758647dd0f.zip |
Don't implement Clone on Dir, SignalFd, and PtyMaster
Since they close their file descriptors on Drop, it's almost impossible
to use Clone without creating a double-close situation.
Also, check for EBADF in SignalFd::drop and Dir::drop.
Diffstat (limited to 'src/dir.rs')
-rw-r--r-- | src/dir.rs | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -25,7 +25,7 @@ use libc::{dirent, readdir_r}; /// * returns entries for `.` (current directory) and `..` (parent directory). /// * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc /// does). -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Debug, Eq, Hash, PartialEq)] pub struct Dir( ptr::NonNull<libc::DIR> ); @@ -85,7 +85,10 @@ impl AsRawFd for Dir { impl Drop for Dir { fn drop(&mut self) { - unsafe { libc::closedir(self.0.as_ptr()) }; + let e = Errno::result(unsafe { libc::closedir(self.0.as_ptr()) }); + if !std::thread::panicking() && e == Err(Error::Sys(Errno::EBADF)) { + panic!("Closing an invalid file descriptor!"); + }; } } |