Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f63e489

Browse files
committedJan 18, 2025
fix(fd): drop all non-closed fds when UhyveFileMap is dropped
1 parent 76aa537 commit f63e489

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed
 

‎src/isolation/filemap.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::{
22
collections::{HashMap, HashSet},
33
ffi::{CString, OsString},
4-
fs::canonicalize,
4+
fs::{canonicalize, File},
55
io::ErrorKind,
6-
os::{fd::RawFd, unix::ffi::OsStrExt},
6+
os::{
7+
fd::{FromRawFd, RawFd},
8+
unix::ffi::OsStrExt,
9+
},
710
path::{absolute, PathBuf},
811
};
912

@@ -125,6 +128,16 @@ impl UhyveFileMap {
125128
ret
126129
}
127130

131+
// Drops all file descriptors present in fdmap and unlinkedfd.
132+
pub fn drop_all_fds(&self) {
133+
for (fd, _) in &self.fdmap {
134+
unsafe { File::from_raw_fd(*fd) };
135+
}
136+
for fd in self.unlinkedfd.iter() {
137+
unsafe { File::from_raw_fd(*fd) };
138+
}
139+
}
140+
128141
/// Checks whether the fd is mapped to a guest path or belongs
129142
/// to an unlinked file.
130143
///
@@ -201,6 +214,12 @@ impl UhyveFileMap {
201214
}
202215
}
203216

217+
impl Drop for UhyveFileMap {
218+
fn drop(&mut self) {
219+
self.drop_all_fds();
220+
}
221+
}
222+
204223
#[cfg(test)]
205224
mod tests {
206225
use super::*;

0 commit comments

Comments
 (0)
Please sign in to comment.