mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-21 12:05:19 +00:00
287887c99c
Rebooting a VM fails with the following error when debug assertions are enabled: fatal runtime error: IO Safety violation: owned file descriptor already closed This happens because FromRawFd::from_raw_fd is used on RawFds stored in ConsoleInfo every time a VM begins to boot, so the second time (after a reboot, or if the first attempt to boot via the API failed), the fd will be closed. Until this assertion is hit, the code is operating on either closed file descriptors, or new file descriptors for something completely different. If debug assertions are disabled, it will just continue doing this with unpredictable results. To fix this, and prevent the problem reocurring, ownership of the console file descriptors needs to be properly tracked, using Rust's type system, so this commit refactors the console code to do that. The file descriptors are now passed around with reference counts, so they won't be closed prematurely. The obvious way to do this would be to just have each member of ConsoleInfo be an Arc<File>, but we need to accomodate that serial console file descriptors can also be sockets. We can't just store an OwnedFd and convert it when it's used, because we only get a reference from the Arc, so we need to store the descriptors as their concrete types in an enum. Since this basically duplicates the ConsoleOutputMode enum from the config, the ConsoleOutputMode enum is now not used past constructing the ConsoleInfo. So that ownership can be represented consistently, the debug console's tty mode now uses its own stdout descriptor. I'm still using .try_clone().unwrap() (i.e. dup()) to clone file descriptors for Endpoint::FilePair and Endpoint::TtyPair, because I assume there's a reason for them not just to hold a single file descriptor. I've also retained the existing behaviour of having serial manager ignore the tty file descriptor passed to it (which is stdout), and instead using stdin. It looks a lot weirder now, because it has to explicitly indicate it's ignoring the fd with an underscore binding. Fixes: 52eebaf6 ("vmm: refactor DeviceManager to use console_info") Signed-off-by: Alyssa Ross <hi@alyssa.is>