diff options
-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; + } }; |