From 59d4a56ab779559a868795d88108875b4422b191 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Thu, 20 Aug 2020 10:04:02 +0200 Subject: [PATCH] vmm: memory_manager: Don't truncate backing file In case the provided backing file is an actual file and not a directory, we should not truncate it, as we expect the file to already be the right size. This change will be important once we try to map the same file through multiple memory mappings. We can't let the file be truncated as the second mapping wouldn't work properly. Signed-off-by: Sebastien Boeuf --- vmm/src/memory_manager.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 9b0955510..31519c7b2 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -519,7 +519,9 @@ impl MemoryManager { let path_ptr = path.as_mut_ptr() as *mut _; let fd = unsafe { libc::mkstemp(path_ptr) }; unsafe { libc::unlink(path_ptr) }; - unsafe { File::from_raw_fd(fd) } + let f = unsafe { File::from_raw_fd(fd) }; + f.set_len(size as u64).map_err(Error::SharedFileSetLen)?; + f } else { OpenOptions::new() .read(true) @@ -528,8 +530,6 @@ impl MemoryManager { .map_err(Error::SharedFileCreate)? }; - f.set_len(size as u64).map_err(Error::SharedFileSetLen)?; - let mut mmap_flags = if copy_on_write { libc::MAP_NORESERVE | libc::MAP_PRIVATE } else {