diff options
author | Krzysztof Richert <krzysztof.richert@intel.com> | 2021-10-27 15:07:45 +0300 |
---|---|---|
committer | Krzysztof Richert <krzysztof.richert@intel.com> | 2021-10-28 17:21:37 +0300 |
commit | 90e0e1648c3ee168deb7615450f3a2771c04b8eb (patch) | |
tree | 7d6297d9ca228b211467bf32c7d673911ef7e6e0 | |
parent | a5176f324cf5d9cbdb15fa57a6c3f54bcb6e6f48 (diff) | |
download | virtual-media-90e0e1648c3ee168deb7615450f3a2771c04b8eb.tar.xz |
Cleanup mounted resources after application crash.
On initial state application cleans mounted resources which
were allocated by user befor application crashed.
Without that busy slot is not avaialble anymore for user.
Tested:
1. Mount CIFS share.
2. Send terminate signal to virtual-media (kill -9).
3. Mount CIFS share on the same slot as during step 1.
Change-Id: I7088e94832fb7bec171a56f73bd66cd29e9b246f
Signed-off-by: Krzysztof Richert <krzysztof.richert@intel.com>
-rw-r--r-- | src/state/initial_state.hpp | 45 | ||||
-rw-r--r-- | src/system.hpp | 27 |
2 files changed, 67 insertions, 5 deletions
diff --git a/src/state/initial_state.hpp b/src/state/initial_state.hpp index 7fcc10b..02fe479 100644 --- a/src/state/initial_state.hpp +++ b/src/state/initial_state.hpp @@ -25,6 +25,10 @@ struct InitialState : public BasicStateT<InitialState> "Legacy mode is not supported"); } #endif + if (isLegacy) + { + cleanUpMountPoint(); + } addMountPointInterface(event); addProcessInterface(event); addServiceInterface(event, isLegacy); @@ -79,6 +83,47 @@ struct InitialState : public BasicStateT<InitialState> processIface->initialize(); } + void cleanUpMountPoint() + { + if (UsbGadget::isConfigured(std::string(machine.getName()))) + { + int result = UsbGadget::configure(std::string(machine.getName()), + machine.getConfig().nbdDevice, + StateChange::removed); + LogMsg(Logger::Info, "UsbGadget cleanup"); + + if (result != 0) + { + LogMsg(Logger::Critical, machine.getName(), + "Some serious failure happened! Cleanup failed."); + } + } + + auto localFile = std::filesystem::temp_directory_path() / + std::string(machine.getName()); + + if (fs::exists(localFile)) + { + if (0 == ::umount2(localFile.c_str(), MNT_FORCE)) + { + LogMsg(Logger::Info, "Cleanup directory ", localFile); + std::error_code ec; + if (!std::filesystem::remove(localFile, ec)) + { + LogMsg(Logger::Error, ec, + "Cleanup failed - unable to remove directory ", + localFile); + } + } + else + { + LogMsg(Logger::Error, + "Cleanup failed - unable to unmount directory ", + localFile); + } + } + } + void addMountPointInterface(const RegisterDbusEvent& event) { std::string objPath = getObjectPath(machine); diff --git a/src/system.hpp b/src/system.hpp index cc8bdad..cd16802 100644 --- a/src/system.hpp +++ b/src/system.hpp @@ -490,6 +490,13 @@ struct UsbGadget return true; } + static const std::string getGadgetDirPrefix() + { + const std::string gadgetDirPrefix = + "/sys/kernel/config/usb_gadget/mass-storage-"; + return gadgetDirPrefix; + } + public: static int32_t configure(const std::string& name, const NBDDevice& nbd, StateChange change, const bool rw = false) @@ -511,8 +518,7 @@ struct UsbGadget return -1; } - const fs::path gadgetDir = - "/sys/kernel/config/usb_gadget/mass-storage-" + name; + const fs::path gadgetDir = getGadgetDirPrefix() + name; const fs::path funcMassStorageDir = gadgetDir / "functions/mass_storage.usb0"; const fs::path stringsDir = gadgetDir / "strings/0x409"; @@ -595,9 +601,8 @@ struct UsbGadget static std::optional<std::string> getStats(const std::string& name) { - const fs::path statsPath = - "/sys/kernel/config/usb_gadget/mass-storage-" + name + - "/functions/mass_storage.usb0/lun.0/stats"; + const fs::path statsPath = getGadgetDirPrefix() + name + + "/functions/mass_storage.usb0/lun.0/stats"; std::ifstream ifs(statsPath); if (!ifs.is_open()) @@ -609,4 +614,16 @@ struct UsbGadget return std::string{std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>()}; } + + static bool isConfigured(const std::string& name) + { + const fs::path gadgetDir = getGadgetDirPrefix() + name; + + if (!fs::exists(gadgetDir)) + { + return false; + } + + return true; + } }; |